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 //#define ICON_TO_DEBUG
51 /// Function to convert opengl data type
52 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
55 case Graphic3d_TOD_USHORT:
57 return GL_UNSIGNED_SHORT;
58 case Graphic3d_TOD_UINT:
60 return GL_UNSIGNED_INT;
61 case Graphic3d_TOD_VEC2:
64 case Graphic3d_TOD_VEC3:
67 case Graphic3d_TOD_VEC4:
70 case Graphic3d_TOD_VEC4UB:
72 return GL_UNSIGNED_BYTE;
79 //*******************************************************************
80 //! Auxiliary class for Vertex buffer with interleaved attributes.
81 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
86 //! Create uninitialized VBO..
87 //! \param theAttribs attributes
88 //! \param theStride a flag
89 SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
90 const Standard_Integer theStride)
91 : Stride (theStride), NbAttributes(1)
94 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
97 //! Create uninitialized VBO.
98 SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
99 : Stride (theAttribs.Stride), NbAttributes(1)
101 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
104 /// Returns True if color attribute is defined
105 virtual bool HasColorAttribute() const
107 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
108 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
109 if (anAttrib.Id == Graphic3d_TOA_COLOR) {
116 /// Returns True if normal attribute is defined
117 virtual bool HasNormalAttribute() const
119 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
120 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
121 if (anAttrib.Id == Graphic3d_TOA_NORM) {
128 /// Bind position of the attribute
129 /// \param theGlCtx OpenGl context
130 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
132 if (!OpenGl_VertexBuffer::IsValid()) {
136 OpenGl_VertexBuffer::Bind (theGlCtx);
138 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
139 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
140 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
141 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
142 if (aDataType == GL_NONE) {
144 } else if (anAttrib.Id == Graphic3d_TOA_POS) {
145 OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
149 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
153 /// Bind all attributes
154 /// \param theGlCtx OpenGl context
155 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
157 if (!OpenGl_VertexBuffer::IsValid())
160 OpenGl_VertexBuffer::Bind (theGlCtx);
162 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
163 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
165 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
166 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
167 if (aDataType == GL_NONE)
170 OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
171 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
175 /// Unbind all attributes
176 /// \param theGlCtx OpenGl context
177 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
179 if (!OpenGl_VertexBuffer::IsValid())
181 OpenGl_VertexBuffer::Unbind (theGlCtx);
183 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
184 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
185 OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
191 /// Array of attributes
192 Graphic3d_Attribute Attribs[1];
195 Standard_Integer Stride;
197 /// Number of attributes
198 Standard_Integer NbAttributes;
201 //**************************************************************
202 //! Redefinition of OpenGl_Element
203 class SketcherPrs_Element: public OpenGl_Element
207 /// \param theObj a presentation
208 SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) :
209 OpenGl_Element(), myObj(theObj) {}
211 /// Render the current presentation
212 /// \param theWorkspace OpenGL workspace
213 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
216 myObj->Render(theWorkspace);
219 /// Releases OpenGL resources
220 /// \param theContext OpenGL context
221 virtual void Release (OpenGl_Context* theContext)
224 myObj->Release(theContext);
228 Handle(SketcherPrs_SymbolPrs) myObj;
232 //**************************************************************
233 //! Definition of call back
234 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
236 Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
237 if (anIObj.IsNull()) {
238 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
240 return new SketcherPrs_Element(anIObj);
244 //*****************************************************************************
245 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
246 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
249 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
252 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
253 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
254 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsConflicting(false)
256 SetAutoHilight(Standard_False);
259 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
261 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
262 // Empty memory in position manager
263 aMgr->deleteConstraint(this);
267 #pragma warning( disable : 4996 )
270 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
273 if (myIsConflicting) {
274 if (myErrorIcon.IsNull()) {
275 char* aEnv = getenv("NEWGEOM_ROOT_DIR");
277 TCollection_AsciiString aFile(aEnv);
281 aFile += "conflicting_icon.png";
282 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
283 if (aPixMap->Load(aFile)) {
284 myErrorIcon = aPixMap;
292 if (myIconsMap.count(iconName()) == 1) {
293 return myIconsMap[iconName()];
295 // Load icon for the presentation
296 char* aEnv = getenv("NEWGEOM_ROOT_DIR");
298 TCollection_AsciiString aFile(aEnv);
303 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
304 if (aPixMap->Load(aFile)) {
305 myIconsMap[iconName()] = aPixMap;
309 // The icon for constraint is not found
310 static const char aMsg[] = "Error! constraint images are not found";
312 Events_Error::send(aMsg);
313 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
314 return Handle(Image_AlienPixMap)();
317 void SketcherPrs_SymbolPrs::prepareAspect()
319 // Create an aspect with the icon
320 if (myAspect.IsNull()) {
321 Handle(Image_AlienPixMap) aIcon = icon();
323 myAspect = new Graphic3d_AspectMarker3d();
325 myAspect = new Graphic3d_AspectMarker3d(aIcon);
329 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
331 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
332 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
333 if (aLine.get() == NULL)
335 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
337 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
338 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
340 // Draw line by two points
341 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
342 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
343 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
344 theGroup->AddPrimitiveArray(aLines);
347 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
348 const SelectMgr_SequenceOfOwner& theOwners)
351 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
352 aSelectionPrs->Clear();
353 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
355 aSelectionPrs->SetDisplayPriority(9);
356 aSelectionPrs->Display();
357 thePM->Highlight(this);
360 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
361 const Quantity_NameOfColor theColor,
362 const Handle(SelectMgr_EntityOwner)& theOwner)
364 thePM->Color(this, theColor);
366 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
367 aHilightPrs->Clear();
368 drawLines(aHilightPrs, theColor);
370 if (thePM->IsImmediateModeOn())
371 thePM->AddToImmediateList(aHilightPrs);
374 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
375 const Handle(Prs3d_Presentation)& thePresentation,
376 const Standard_Integer theMode)
381 Handle(AIS_InteractiveContext) aCtx = GetContext();
382 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
383 if (!aDriver.IsNull()) {
384 // register the custom element factory function
385 aDriver->UserDrawCallback() = SymbolPrsCallBack;
388 // Update points with default shift value
389 if (!updatePoints(20)) {
393 int aNbVertex = myPntArray->VertexNumber();
394 if (myOwner.IsNull()) {
395 myOwner = new SelectMgr_EntityOwner(this);
398 // Create sensitive point for each symbol
400 for (int i = 1; i <= aNbVertex; i++) {
401 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
402 mySPoints.Append(aSP);
405 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
406 aGroup->SetPrimitivesAspect(myAspect);
408 // Recompute boundary box of the group
409 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
412 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
413 aVert = myPntArray->Vertice(i);
414 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
417 // Pint the group with custom procedure (see Render)
418 aGroup->UserDraw(this, true);
420 // Disable frustum culling for this object by marking it as mutable
421 aGroup->Structure()->SetMutable(true);
422 //aGroup->AddPrimitiveArray(myPntArray);
427 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
428 const Standard_Integer aMode)
431 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
432 for (int i = 1; i <= mySPoints.Length(); i++)
433 aSelection->Add(mySPoints.Value(i));
437 void SketcherPrs_SymbolPrs::SetConflictingConstraint(const bool& theConflicting,
438 const std::vector<int>& theColor)
441 if (myIsConflicting != theConflicting) {
442 myIsConflicting = theConflicting;
443 Handle(Image_AlienPixMap) anIcon = icon();
444 if (!anIcon.IsNull())
445 myAspect->SetMarkerImage(new Graphic3d_MarkerImage(anIcon));
450 if (!myAspect.IsNull())
451 myAspect->SetColor (Quantity_Color (theColor[0] / 255., theColor[1] / 255., theColor[2] / 255.,
453 myIsConflicting = true;
457 if (!myAspect.IsNull())
458 myAspect->SetColor (Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB));
459 myIsConflicting = false;
464 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
466 // this method is a combination of OCCT OpenGL functions. The main purpose is to have
467 // equal distance from the source object to symbol indpendently of zoom.
468 // It is base on OCCT 6.9.1 and might need changes when using later OCCT versions.
469 // The specific SHAPER modifications are marked by ShaperModification:start/end, other is OCCT code
471 // do not update presentation for invalid or already removed objects: the presentation
472 // should be removed soon
473 if (!myConstraint->data().get() || !myConstraint->data()->isValid())
476 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
477 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
478 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
480 // ShaperModification:start
481 double aScale = aView->Camera()->Scale();
482 // Update points coordinate taking the viewer scale into account
483 if (!updatePoints(MyDist * aScale))
485 // ShaperModification:end
487 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
489 if (myVboAttribs.IsNull()) {
490 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
493 // Update drawing attributes
494 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
495 myVboAttribs->Release (aCtx.operator->());
496 myVboAttribs.Nullify();
500 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
502 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
504 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
506 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
508 // ShaperModification:start : filling the presentation with color if there is a conflict
509 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0 || myIsConflicting;
510 // ShaperModification:end
513 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
514 ? anAspectMarker->SpriteHighlightRes(aCtx)
516 theWorkspace->EnableTexture (aSprite);
517 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
518 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
519 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
520 aLineColor = theWorkspace->HighlightColor;
522 // Set lighting of the symbol
524 aCtx->core11fwd->glDisable (GL_LIGHTING);
526 aCtx->core11fwd->glEnable (GL_LIGHTING);
528 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
531 myVboAttribs->BindAllAttributes(aCtx);
532 // Textured markers will be drawn with the point sprites
533 aCtx->SetPointSize (anAspectMarker->MarkerSize());
534 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
535 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
537 aCtx->core11fwd->glEnable (GL_BLEND);
538 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
540 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
542 aCtx->core11fwd->glDisable (GL_BLEND);
543 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
544 aCtx->SetPointSize (1.0f);
546 theWorkspace->EnableTexture (aTextureBack);
547 aCtx->BindProgram (NULL);
549 // Update selection position only if there is no selected object
550 // because it can corrupt selection of other objects
551 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
553 GetContext()->MainSelector()->RebuildSensitivesTree (this);
554 GetContext()->MainSelector()->RebuildObjectsTree (false);
559 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
561 // Release OpenGl resources
562 if (!myVboAttribs.IsNull()) {
564 theContext->DelayedRelease (myVboAttribs);
566 myVboAttribs.Nullify();
570 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
571 const Handle(Prs3d_Presentation)& thePrs) const
573 if (theShape->isEdge()) {
575 std::shared_ptr<GeomAPI_Curve> aCurve =
576 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
577 if (aCurve->isLine()) {
579 GeomAdaptor_Curve aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
580 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
582 // The shape is circle or arc
583 GeomAdaptor_Curve aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
584 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
586 } else if (theShape->isVertex()) {
588 std::shared_ptr<GeomAPI_Vertex> aVertex =
589 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
590 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
591 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
592 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
596 void SketcherPrs_SymbolPrs::drawListOfShapes(const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
597 const Handle(Prs3d_Presentation)& thePrs) const
599 int aNb = theListAttr->size();
604 for (i = 0; i < aNb; i++) {
605 aObj = theListAttr->object(i);
606 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
607 if (aShape.get() != NULL)
608 drawShape(aShape, thePrs);
612 void SketcherPrs_SymbolPrs::BoundingBox (Bnd_Box& theBndBox)
614 Select3D_BndBox3d aTmpBox;
615 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
616 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
617 aTmpBox.Combine (anEnt->BoundingBox());
620 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
621 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());