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_InfoMessage.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,
144 aDataType, Stride, anOffset);
148 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
152 /// Bind all attributes
153 /// \param theGlCtx OpenGl context
154 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
156 if (!OpenGl_VertexBuffer::IsValid())
159 OpenGl_VertexBuffer::Bind (theGlCtx);
161 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
162 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
164 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
165 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
166 if (aDataType == GL_NONE)
169 OpenGl_VertexBuffer::bindAttribute(theGlCtx, anAttrib.Id, aNbComp,
170 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()) {
239 "VUserDrawCallback error: null object passed, the custom scene element will not be rendered"
242 return new SketcherPrs_Element(anIObj);
246 //*****************************************************************************
247 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
248 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
251 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
254 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
255 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
256 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane), myIsConflicting(false)
258 SetAutoHilight(Standard_False);
259 myPntArray = new Graphic3d_ArrayOfPoints(1);
260 myPntArray->AddVertex(0., 0., 0.);
263 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
265 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
266 // Empty memory in position manager
267 aMgr->deleteConstraint(this);
271 #pragma warning( disable : 4996 )
274 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
276 if (myIconsMap.count(iconName()) == 1) {
277 return myIconsMap[iconName()];
279 // Load icon for the presentation
281 char* anEnv = getenv("SHAPER_ROOT_DIR");
283 aFile = std::string(anEnv) +
284 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
286 anEnv = getenv("OPENPARTS_ROOT_DIR");
288 aFile = std::string(anEnv) + FSEP + "resources";
293 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
294 if (aPixMap->Load(aFile.c_str())) {
295 myIconsMap[iconName()] = aPixMap;
298 // The icon for constraint is not found
299 static const char aMsg[] = "Error! constraint images are not found";
301 Events_InfoMessage("SketcherPrs_SymbolPrs", aMsg).send();
302 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
303 return Handle(Image_AlienPixMap)();
306 void SketcherPrs_SymbolPrs::prepareAspect()
308 // Create an aspect with the icon
309 if (myAspect.IsNull()) {
310 Handle(Image_AlienPixMap) aIcon = icon();
312 myAspect = new Graphic3d_AspectMarker3d();
314 myAspect = new Graphic3d_AspectMarker3d(aIcon);
318 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup,
319 std::string theAttrName) const
321 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
322 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
323 if (aLine.get() == NULL)
325 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
327 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
328 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
330 // Draw line by two points
331 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
332 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
333 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
334 theGroup->AddPrimitiveArray(aLines);
337 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
338 const SelectMgr_SequenceOfOwner& theOwners)
341 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
342 aSelectionPrs->Clear();
343 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
345 aSelectionPrs->SetDisplayPriority(9);
346 aSelectionPrs->Display();
347 thePM->Highlight(this);
350 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(
351 const Handle(PrsMgr_PresentationManager3d)& thePM,
352 const Quantity_NameOfColor theColor,
353 const Handle(SelectMgr_EntityOwner)& theOwner)
355 thePM->Color(this, theColor);
357 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
358 aHilightPrs->Clear();
359 drawLines(aHilightPrs, theColor);
361 if (thePM->IsImmediateModeOn())
362 thePM->AddToImmediateList(aHilightPrs);
365 void SketcherPrs_SymbolPrs::Compute(
366 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
367 const Handle(Prs3d_Presentation)& thePresentation,
368 const Standard_Integer theMode)
373 Handle(AIS_InteractiveContext) aCtx = GetContext();
374 Handle(OpenGl_GraphicDriver) aDriver =
375 Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
376 if (!aDriver.IsNull()) {
377 // register the custom element factory function
378 aDriver->UserDrawCallback() = SymbolPrsCallBack;
381 // Update points with default shift value
382 // it updates array of points if the presentation is ready to display, or the array of points
383 // contains the previous values
385 bool aReadyToDisplay = updateIfReadyToDisplay(20);
387 int aNbVertex = myPntArray->VertexNumber();
388 if (myOwner.IsNull()) {
389 myOwner = new SelectMgr_EntityOwner(this);
392 // Create sensitive point for each symbol
394 for (int i = 1; i <= aNbVertex; i++) {
395 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
396 mySPoints.Append(aSP);
399 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
400 aGroup->SetPrimitivesAspect(myAspect);
402 // Recompute boundary box of the group
403 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
406 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
407 aVert = myPntArray->Vertice(i);
408 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
411 // Pint the group with custom procedure (see Render)
412 aGroup->UserDraw(this, true);
414 // Disable frustum culling for this object by marking it as mutable
415 aGroup->Structure()->SetMutable(true);
416 //aGroup->AddPrimitiveArray(myPntArray);
418 if (!aReadyToDisplay)
419 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
420 "An empty AIS presentation: SketcherPrs_LengthDimension");
425 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
426 const Standard_Integer aMode)
429 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
430 for (int i = 1; i <= mySPoints.Length(); i++)
431 aSelection->Add(mySPoints.Value(i));
435 void SketcherPrs_SymbolPrs::SetConflictingConstraint(const bool& theConflicting,
436 const std::vector<int>& theColor)
440 if (!myAspect.IsNull())
441 myAspect->SetColor (Quantity_Color (theColor[0] / 255., theColor[1] / 255.,
442 theColor[2] / 255., Quantity_TOC_RGB));
443 myIsConflicting = true;
447 if (!myAspect.IsNull())
448 myAspect->SetColor (Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB));
449 myIsConflicting = false;
453 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
455 // this method is a combination of OCCT OpenGL functions. The main purpose is to have
456 // equal distance from the source object to symbol indpendently of zoom.
457 // It is base on OCCT 6.9.1 and might need changes when using later OCCT versions.
458 // The specific SHAPER modifications are marked by ShaperModification:start/end,
459 // other is OCCT code
461 // do not update presentation for invalid or already removed objects: the presentation
462 // should be removed soon
463 if (!myConstraint->data().get() || !myConstraint->data()->isValid())
466 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
467 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
468 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
470 // ShaperModification:start
471 double aScale = aView->Camera()->Scale();
472 // Update points coordinate taking the viewer scale into account
473 updateIfReadyToDisplay(MyDist * aScale);
475 // ShaperModification:end
477 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
479 if (myVboAttribs.IsNull()) {
480 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
483 // Update drawing attributes
484 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements,
485 aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
486 myVboAttribs->Release (aCtx.operator->());
487 myVboAttribs.Nullify();
491 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
493 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
495 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
496 // ShaperModification:start : filling the presentation with color if there is a conflict
497 const bool toHilight =
498 (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0 || myIsConflicting;
499 // ShaperModification:end
501 const Handle(OpenGl_PointSprite)& aSprite =
502 (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())?
503 anAspectMarker->SpriteHighlightRes(aCtx)
505 theWorkspace->EnableTexture (aSprite);
506 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False,
507 Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
508 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
509 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
510 aLineColor = theWorkspace->HighlightColor;
512 // Set lighting of the symbol
514 aCtx->core11fwd->glDisable (GL_LIGHTING);
516 aCtx->core11fwd->glEnable (GL_LIGHTING);
518 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
521 myVboAttribs->BindAllAttributes(aCtx);
522 // Textured markers will be drawn with the point sprites
523 aCtx->SetPointSize (anAspectMarker->MarkerSize());
524 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
525 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
527 aCtx->core11fwd->glEnable (GL_BLEND);
528 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
530 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
532 aCtx->core11fwd->glDisable (GL_BLEND);
533 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
534 aCtx->SetPointSize (1.0f);
536 theWorkspace->EnableTexture (aTextureBack);
537 aCtx->BindProgram (NULL);
539 // Update selection position only if there is no selected object
540 // because it can corrupt selection of other objects
541 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
543 GetContext()->MainSelector()->RebuildSensitivesTree (this);
544 GetContext()->MainSelector()->RebuildObjectsTree (false);
549 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
551 // Release OpenGl resources
552 if (!myVboAttribs.IsNull()) {
554 theContext->DelayedRelease (myVboAttribs);
556 myVboAttribs.Nullify();
560 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
561 const Handle(Prs3d_Presentation)& thePrs) const
563 if (theShape->isEdge()) {
565 std::shared_ptr<GeomAPI_Curve> aCurve =
566 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
567 if (aCurve->isLine()) {
570 aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
571 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
573 // The shape is circle or arc
575 aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
576 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
578 } else if (theShape->isVertex()) {
580 std::shared_ptr<GeomAPI_Vertex> aVertex =
581 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
582 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
583 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
584 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
588 void SketcherPrs_SymbolPrs::drawListOfShapes(
589 const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
590 const Handle(Prs3d_Presentation)& thePrs) const
592 int aNb = theListAttr->size();
597 for (i = 0; i < aNb; i++) {
598 aObj = theListAttr->object(i);
599 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
600 if (aShape.get() != NULL)
601 drawShape(aShape, thePrs);
605 void SketcherPrs_SymbolPrs::BoundingBox (Bnd_Box& theBndBox)
607 Select3D_BndBox3d aTmpBox;
608 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
609 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
610 aTmpBox.Combine (anEnt->BoundingBox());
613 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
614 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());