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), myIsConflicting(false)
254 SetAutoHilight(Standard_False);
255 myPntArray = new Graphic3d_ArrayOfPoints(1);
256 myPntArray->AddVertex(0., 0., 0.);
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()
272 if (myIconsMap.count(iconName()) == 1) {
273 return myIconsMap[iconName()];
275 // Load icon for the presentation
277 char* anEnv = getenv("SHAPER_ROOT_DIR");
279 aFile = std::string(anEnv) +
280 FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
282 anEnv = getenv("OPENPARTS_ROOT_DIR");
284 aFile = std::string(anEnv) + FSEP + "resources";
289 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
290 if (aPixMap->Load(aFile.c_str())) {
291 myIconsMap[iconName()] = aPixMap;
294 // The icon for constraint is not found
295 static const char aMsg[] = "Error! constraint images are not found";
297 Events_Error::send(aMsg);
298 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
299 return Handle(Image_AlienPixMap)();
302 void SketcherPrs_SymbolPrs::prepareAspect()
304 // Create an aspect with the icon
305 if (myAspect.IsNull()) {
306 Handle(Image_AlienPixMap) aIcon = icon();
308 myAspect = new Graphic3d_AspectMarker3d();
310 myAspect = new Graphic3d_AspectMarker3d(aIcon);
314 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
316 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
317 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
318 if (aLine.get() == NULL)
320 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
322 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
323 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
325 // Draw line by two points
326 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
327 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
328 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
329 theGroup->AddPrimitiveArray(aLines);
332 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
333 const SelectMgr_SequenceOfOwner& theOwners)
336 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
337 aSelectionPrs->Clear();
338 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
340 aSelectionPrs->SetDisplayPriority(9);
341 aSelectionPrs->Display();
342 thePM->Highlight(this);
345 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
346 const Quantity_NameOfColor theColor,
347 const Handle(SelectMgr_EntityOwner)& theOwner)
349 thePM->Color(this, theColor);
351 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
352 aHilightPrs->Clear();
353 drawLines(aHilightPrs, theColor);
355 if (thePM->IsImmediateModeOn())
356 thePM->AddToImmediateList(aHilightPrs);
359 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
360 const Handle(Prs3d_Presentation)& thePresentation,
361 const Standard_Integer theMode)
366 Handle(AIS_InteractiveContext) aCtx = GetContext();
367 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
368 if (!aDriver.IsNull()) {
369 // register the custom element factory function
370 aDriver->UserDrawCallback() = SymbolPrsCallBack;
373 // Update points with default shift value
374 // it updates array of points if the presentation is ready to display, or the array of points
375 // contains the previous values
377 bool aReadyToDisplay = updateIfReadyToDisplay(20);
379 int aNbVertex = myPntArray->VertexNumber();
380 if (myOwner.IsNull()) {
381 myOwner = new SelectMgr_EntityOwner(this);
384 // Create sensitive point for each symbol
386 for (int i = 1; i <= aNbVertex; i++) {
387 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
388 mySPoints.Append(aSP);
391 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
392 aGroup->SetPrimitivesAspect(myAspect);
394 // Recompute boundary box of the group
395 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
398 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
399 aVert = myPntArray->Vertice(i);
400 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
403 // Pint the group with custom procedure (see Render)
404 aGroup->UserDraw(this, true);
406 // Disable frustum culling for this object by marking it as mutable
407 aGroup->Structure()->SetMutable(true);
408 //aGroup->AddPrimitiveArray(myPntArray);
410 if (!aReadyToDisplay)
411 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
412 "An empty AIS presentation: SketcherPrs_LengthDimension");
417 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
418 const Standard_Integer aMode)
421 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
422 for (int i = 1; i <= mySPoints.Length(); i++)
423 aSelection->Add(mySPoints.Value(i));
427 void SketcherPrs_SymbolPrs::SetConflictingConstraint(const bool& theConflicting,
428 const std::vector<int>& theColor)
432 if (!myAspect.IsNull())
433 myAspect->SetColor (Quantity_Color (theColor[0] / 255., theColor[1] / 255., theColor[2] / 255.,
435 myIsConflicting = true;
439 if (!myAspect.IsNull())
440 myAspect->SetColor (Quantity_Color (1.0, 1.0, 0.0, Quantity_TOC_RGB));
441 myIsConflicting = false;
445 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
447 // this method is a combination of OCCT OpenGL functions. The main purpose is to have
448 // equal distance from the source object to symbol indpendently of zoom.
449 // It is base on OCCT 6.9.1 and might need changes when using later OCCT versions.
450 // The specific SHAPER modifications are marked by ShaperModification:start/end, other is OCCT code
452 // do not update presentation for invalid or already removed objects: the presentation
453 // should be removed soon
454 if (!myConstraint->data().get() || !myConstraint->data()->isValid())
457 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
458 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
459 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
461 // ShaperModification:start
462 double aScale = aView->Camera()->Scale();
463 // Update points coordinate taking the viewer scale into account
464 updateIfReadyToDisplay(MyDist * aScale);
466 // ShaperModification:end
468 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
470 if (myVboAttribs.IsNull()) {
471 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
474 // Update drawing attributes
475 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
476 myVboAttribs->Release (aCtx.operator->());
477 myVboAttribs.Nullify();
481 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
483 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
485 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
486 // ShaperModification:start : filling the presentation with color if there is a conflict
487 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0 || myIsConflicting;
488 // ShaperModification:end
490 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
491 ? anAspectMarker->SpriteHighlightRes(aCtx)
493 theWorkspace->EnableTexture (aSprite);
494 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
495 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
496 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
497 aLineColor = theWorkspace->HighlightColor;
499 // Set lighting of the symbol
501 aCtx->core11fwd->glDisable (GL_LIGHTING);
503 aCtx->core11fwd->glEnable (GL_LIGHTING);
505 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
508 myVboAttribs->BindAllAttributes(aCtx);
509 // Textured markers will be drawn with the point sprites
510 aCtx->SetPointSize (anAspectMarker->MarkerSize());
511 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
512 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
514 aCtx->core11fwd->glEnable (GL_BLEND);
515 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
517 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
519 aCtx->core11fwd->glDisable (GL_BLEND);
520 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
521 aCtx->SetPointSize (1.0f);
523 theWorkspace->EnableTexture (aTextureBack);
524 aCtx->BindProgram (NULL);
526 // Update selection position only if there is no selected object
527 // because it can corrupt selection of other objects
528 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
530 GetContext()->MainSelector()->RebuildSensitivesTree (this);
531 GetContext()->MainSelector()->RebuildObjectsTree (false);
536 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
538 // Release OpenGl resources
539 if (!myVboAttribs.IsNull()) {
541 theContext->DelayedRelease (myVboAttribs);
543 myVboAttribs.Nullify();
547 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
548 const Handle(Prs3d_Presentation)& thePrs) const
550 if (theShape->isEdge()) {
552 std::shared_ptr<GeomAPI_Curve> aCurve =
553 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
554 if (aCurve->isLine()) {
556 GeomAdaptor_Curve aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
557 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
559 // The shape is circle or arc
560 GeomAdaptor_Curve aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
561 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
563 } else if (theShape->isVertex()) {
565 std::shared_ptr<GeomAPI_Vertex> aVertex =
566 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
567 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
568 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
569 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
573 void SketcherPrs_SymbolPrs::drawListOfShapes(const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
574 const Handle(Prs3d_Presentation)& thePrs) const
576 int aNb = theListAttr->size();
581 for (i = 0; i < aNb; i++) {
582 aObj = theListAttr->object(i);
583 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
584 if (aShape.get() != NULL)
585 drawShape(aShape, thePrs);
589 void SketcherPrs_SymbolPrs::BoundingBox (Bnd_Box& theBndBox)
591 Select3D_BndBox3d aTmpBox;
592 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next()) {
593 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
594 aTmpBox.Combine (anEnt->BoundingBox());
597 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
598 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());