1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_SymbolPrs.cpp
4 // Created: 12 March 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_SymbolPrs.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Vertex.h>
13 #include <GeomAPI_Curve.h>
15 #include <Events_Error.h>
17 #include <Graphic3d_ArrayOfSegments.hxx>
18 #include <Graphic3d_BndBox4f.hxx>
20 #include <SelectMgr_Selection.hxx>
21 #include <Select3D_SensitivePoint.hxx>
22 #include <TopLoc_Location.hxx>
23 #include <AIS_InteractiveContext.hxx>
24 #include <V3d_Viewer.hxx>
25 #include <Prs3d_Root.hxx>
26 #include <Geom_CartesianPoint.hxx>
27 #include <GeomAdaptor_Curve.hxx>
28 #include <StdPrs_DeflectionCurve.hxx>
29 #include <StdPrs_Point.hxx>
30 #include <StdPrs_Curve.hxx>
32 #include <OpenGl_Element.hxx>
33 #include <OpenGl_GraphicDriver.hxx>
34 #include <OpenGl_Context.hxx>
35 #include <OpenGl_View.hxx>
36 #include <OpenGl_PointSprite.hxx>
37 #include <OpenGl_VertexBuffer.hxx>
38 #include <OpenGl_ShaderManager.hxx>
46 /// Step between icons
47 static const double MyDist = 0.02;
49 /// Function to convert opengl data type
50 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
53 case Graphic3d_TOD_USHORT:
55 return GL_UNSIGNED_SHORT;
56 case Graphic3d_TOD_UINT:
58 return GL_UNSIGNED_INT;
59 case Graphic3d_TOD_VEC2:
62 case Graphic3d_TOD_VEC3:
65 case Graphic3d_TOD_VEC4:
68 case Graphic3d_TOD_VEC4UB:
70 return GL_UNSIGNED_BYTE;
77 //*******************************************************************
78 //! Auxiliary class for Vertex buffer with interleaved attributes.
79 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
84 //! Create uninitialized VBO..
85 //! \param theAttribs attributes
86 //! \param theStride a flag
87 SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
88 const Standard_Integer theStride)
89 : Stride (theStride), NbAttributes(1)
92 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
95 //! Create uninitialized VBO.
96 SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
97 : Stride (theAttribs.Stride), NbAttributes(1)
99 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
102 /// Returns True if color attribute is defined
103 virtual bool HasColorAttribute() const
105 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
106 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
107 if (anAttrib.Id == Graphic3d_TOA_COLOR) {
114 /// Returns True if normal attribute is defined
115 virtual bool HasNormalAttribute() const
117 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
118 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
119 if (anAttrib.Id == Graphic3d_TOA_NORM) {
126 /// Bind position of the attribute
127 /// \param theGlCtx OpenGl context
128 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
130 if (!OpenGl_VertexBuffer::IsValid()) {
134 OpenGl_VertexBuffer::Bind (theGlCtx);
136 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
137 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
138 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
139 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
140 if (aDataType == GL_NONE) {
142 } else if (anAttrib.Id == Graphic3d_TOA_POS) {
143 OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
147 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
151 /// Bind all attributes
152 /// \param theGlCtx OpenGl context
153 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
155 if (!OpenGl_VertexBuffer::IsValid())
158 OpenGl_VertexBuffer::Bind (theGlCtx);
160 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
161 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
163 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
164 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
165 if (aDataType == GL_NONE)
168 OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
169 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
173 /// Unbind all attributes
174 /// \param theGlCtx OpenGl context
175 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
177 if (!OpenGl_VertexBuffer::IsValid())
179 OpenGl_VertexBuffer::Unbind (theGlCtx);
181 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
182 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
183 OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
189 /// Array of attributes
190 Graphic3d_Attribute Attribs[1];
193 Standard_Integer Stride;
195 /// Number of attributes
196 Standard_Integer NbAttributes;
199 //**************************************************************
200 //! Redefinition of OpenGl_Element
201 class SketcherPrs_Element: public OpenGl_Element
205 /// \param theObj a presentation
206 SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) :
207 OpenGl_Element(), myObj(theObj) {}
209 /// Render the current presentation
210 /// \param theWorkspace OpenGL workspace
211 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
214 myObj->Render(theWorkspace);
217 /// Releases OpenGL resources
218 /// \param theContext OpenGL context
219 virtual void Release (OpenGl_Context* theContext)
222 myObj->Release(theContext);
226 Handle(SketcherPrs_SymbolPrs) myObj;
230 //**************************************************************
231 //! Definition of call back
232 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
234 Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
235 if (anIObj.IsNull()) {
236 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
238 return new SketcherPrs_Element(anIObj);
242 //*****************************************************************************
243 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
244 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
247 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
250 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
251 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
252 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
254 SetAutoHilight(Standard_False);
257 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
259 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
260 // Empty memory in position manager
261 aMgr->deleteConstraint(this);
265 #pragma warning( disable : 4996 )
268 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
270 if (myIconsMap.count(iconName()) == 1) {
271 return myIconsMap[iconName()];
273 // Load icon for the presentation
274 char* aEnv = getenv("NEWGEOM_ROOT_DIR");
276 TCollection_AsciiString aFile(aEnv);
281 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
282 if (aPixMap->Load(aFile)) {
283 myIconsMap[iconName()] = aPixMap;
287 // The icon for constraint is not found
288 static const char aMsg[] = "Error! constraint images are not found";
290 Events_Error::send(aMsg);
291 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
292 return Handle(Image_AlienPixMap)();
295 void SketcherPrs_SymbolPrs::prepareAspect()
297 // Create an aspect with the icon
298 if (myAspect.IsNull()) {
299 Handle(Image_AlienPixMap) aIcon = icon();
301 myAspect = new Graphic3d_AspectMarker3d();
303 myAspect = new Graphic3d_AspectMarker3d(aIcon);
307 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
309 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
310 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
311 if (aLine.get() == NULL)
313 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
315 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
316 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
318 // Draw line by two points
319 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
320 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
321 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
322 theGroup->AddPrimitiveArray(aLines);
325 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
326 const SelectMgr_SequenceOfOwner& theOwners)
329 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
330 aSelectionPrs->Clear();
331 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
333 aSelectionPrs->SetDisplayPriority(9);
334 aSelectionPrs->Display();
335 thePM->Highlight(this);
338 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
339 const Quantity_NameOfColor theColor,
340 const Handle(SelectMgr_EntityOwner)& theOwner)
342 thePM->Color(this, theColor);
344 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
345 aHilightPrs->Clear();
346 drawLines(aHilightPrs, theColor);
348 if (thePM->IsImmediateModeOn())
349 thePM->AddToImmediateList(aHilightPrs);
352 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
353 const Handle(Prs3d_Presentation)& thePresentation,
354 const Standard_Integer theMode)
359 Handle(AIS_InteractiveContext) aCtx = GetContext();
360 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
361 if (!aDriver.IsNull()) {
362 // register the custom element factory function
363 aDriver->UserDrawCallback() = SymbolPrsCallBack;
366 // Update points with default shift value
367 if (!updatePoints(20)) {
371 int aNbVertex = myPntArray->VertexNumber();
372 if (myOwner.IsNull()) {
373 myOwner = new SelectMgr_EntityOwner(this);
376 // Create sensitive point for each symbol
378 for (int i = 1; i <= aNbVertex; i++) {
379 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
380 mySPoints.Append(aSP);
383 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
384 aGroup->SetPrimitivesAspect(myAspect);
386 // Recompute boundary box of the group
387 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
390 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
391 aVert = myPntArray->Vertice(i);
392 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
395 // Pint the group with custom procedure (see Render)
396 aGroup->UserDraw(this, true);
398 // Disable frustum culling for this object by marking it as mutable
399 aGroup->Structure()->SetMutable(true);
400 //aGroup->AddPrimitiveArray(myPntArray);
405 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
406 const Standard_Integer aMode)
409 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
410 for (int i = 1; i <= mySPoints.Length(); i++)
411 aSelection->Add(mySPoints.Value(i));
416 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
418 // do not update presentation for invalid or already removed objects: the presentation
419 // should be removed soon
420 if (!myConstraint->data().get() || !myConstraint->data()->isValid())
423 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
424 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
425 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
427 double aScale = aView->Camera()->Scale();
428 // Update points coordinate taking the viewer scale into account
429 if (!updatePoints(MyDist * aScale))
432 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
434 if (myVboAttribs.IsNull()) {
435 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
438 // Update drawing attributes
439 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
440 myVboAttribs->Release (aCtx.operator->());
441 myVboAttribs.Nullify();
445 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
447 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
449 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
450 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
451 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
452 ? anAspectMarker->SpriteHighlightRes(aCtx)
454 theWorkspace->EnableTexture (aSprite);
455 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
456 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
457 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
458 aLineColor = theWorkspace->HighlightColor;
460 // Set lighting of the symbol
462 aCtx->core11fwd->glDisable (GL_LIGHTING);
464 aCtx->core11fwd->glEnable (GL_LIGHTING);
466 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
469 myVboAttribs->BindAllAttributes(aCtx);
470 // Textured markers will be drawn with the point sprites
471 aCtx->SetPointSize (anAspectMarker->MarkerSize());
472 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
473 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
475 aCtx->core11fwd->glEnable (GL_BLEND);
476 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
478 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
480 aCtx->core11fwd->glDisable (GL_BLEND);
481 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
482 aCtx->SetPointSize (1.0f);
484 theWorkspace->EnableTexture (aTextureBack);
485 aCtx->BindProgram (NULL);
487 // Update selection position only if there is no selected object
488 // because it can corrupt selection of other objects
489 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
491 GetContext()->MainSelector()->RebuildSensitivesTree (this);
492 GetContext()->MainSelector()->RebuildObjectsTree (false);
497 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
499 // Release OpenGl resources
500 if (!myVboAttribs.IsNull()) {
502 theContext->DelayedRelease (myVboAttribs);
504 myVboAttribs.Nullify();
508 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
509 const Handle(Prs3d_Presentation)& thePrs) const
511 if (theShape->isEdge()) {
513 std::shared_ptr<GeomAPI_Curve> aCurve =
514 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
515 if (aCurve->isLine()) {
517 GeomAdaptor_Curve aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
518 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
520 // The shape is circle or arc
521 GeomAdaptor_Curve aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
522 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
524 } else if (theShape->isVertex()) {
526 std::shared_ptr<GeomAPI_Vertex> aVertex =
527 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
528 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
529 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
530 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
534 void SketcherPrs_SymbolPrs::drawListOfShapes(const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
535 const Handle(Prs3d_Presentation)& thePrs) const
537 int aNb = theListAttr->size();
542 for (i = 0; i < aNb; i++) {
543 aObj = theListAttr->object(i);
544 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
545 if (aShape.get() != NULL)
546 drawShape(aShape, thePrs);
550 void SketcherPrs_SymbolPrs::BoundingBox (Bnd_Box& theBndBox)
552 Select3D_BndBox3d aTmpBox;
553 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
554 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
555 aTmpBox.Combine (anEnt->BoundingBox());
558 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
559 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());