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 SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
86 const Standard_Integer theStride)
87 : Stride (theStride), NbAttributes(1)
90 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
93 //! Create uninitialized VBO.
94 SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
95 : Stride (theAttribs.Stride), NbAttributes(1)
97 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
100 virtual bool HasColorAttribute() const
102 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
103 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
104 if (anAttrib.Id == Graphic3d_TOA_COLOR) {
111 virtual bool HasNormalAttribute() const
113 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
114 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
115 if (anAttrib.Id == Graphic3d_TOA_NORM) {
122 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
124 if (!OpenGl_VertexBuffer::IsValid()) {
128 OpenGl_VertexBuffer::Bind (theGlCtx);
130 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
131 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
132 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
133 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
134 if (aDataType == GL_NONE) {
136 } else if (anAttrib.Id == Graphic3d_TOA_POS) {
137 OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
141 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
145 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
147 if (!OpenGl_VertexBuffer::IsValid())
150 OpenGl_VertexBuffer::Bind (theGlCtx);
152 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
153 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
155 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
156 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
157 if (aDataType == GL_NONE)
160 OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
161 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
165 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
167 if (!OpenGl_VertexBuffer::IsValid())
169 OpenGl_VertexBuffer::Unbind (theGlCtx);
171 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
172 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
173 OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
179 Graphic3d_Attribute Attribs[1];
180 Standard_Integer Stride;
181 Standard_Integer NbAttributes;
184 //**************************************************************
185 //! Redefinition of OpenGl_Element
186 class SketcherPrs_Element: public OpenGl_Element
189 SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) :
190 OpenGl_Element(), myObj(theObj) {}
192 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
195 myObj->Render(theWorkspace);
198 virtual void Release (OpenGl_Context* theContext)
201 myObj->Release(theContext);
205 Handle(SketcherPrs_SymbolPrs) myObj;
209 //**************************************************************
210 //! Definition of call back
211 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
213 Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
214 if (anIObj.IsNull()) {
215 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
217 return new SketcherPrs_Element(anIObj);
221 //*****************************************************************************
222 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
223 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
226 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
229 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
230 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
231 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
233 SetAutoHilight(Standard_False);
236 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
238 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
239 aMgr->deleteConstraint(this);
243 #pragma warning( disable : 4996 )
246 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
248 if (myIconsMap.count(iconName()) == 1) {
249 return myIconsMap[iconName()];
251 char* aEnv = getenv("NEWGEOM_ROOT_DIR");
253 TCollection_AsciiString aFile(aEnv);
258 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
259 if (aPixMap->Load(aFile)) {
260 myIconsMap[iconName()] = aPixMap;
264 static const char aMsg[] = "Error! constraint images are not found";
266 Events_Error::send(aMsg);
267 myIconsMap[iconName()] = Handle(Image_AlienPixMap)();
268 return Handle(Image_AlienPixMap)();
271 void SketcherPrs_SymbolPrs::ClearSelected()
273 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );
274 if( !aSelectionPrs.IsNull() ) {
275 aSelectionPrs->Clear();
279 void SketcherPrs_SymbolPrs::prepareAspect()
281 if (myAspect.IsNull()) {
282 Handle(Image_AlienPixMap) aIcon = icon();
284 myAspect = new Graphic3d_AspectMarker3d();
286 myAspect = new Graphic3d_AspectMarker3d(aIcon);
290 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
292 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
293 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
294 if (aLine.get() == NULL)
296 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
298 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
299 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
301 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
302 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
303 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
304 theGroup->AddPrimitiveArray(aLines);
307 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
308 const SelectMgr_SequenceOfOwner& theOwners)
311 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
312 aSelectionPrs->Clear();
313 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
315 aSelectionPrs->SetDisplayPriority(9);
316 aSelectionPrs->Display();
317 thePM->Highlight(this);
320 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
321 const Quantity_NameOfColor theColor,
322 const Handle(SelectMgr_EntityOwner)& theOwner)
324 thePM->Color(this, theColor);
326 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
327 aHilightPrs->Clear();
328 drawLines(aHilightPrs, theColor);
330 if (thePM->IsImmediateModeOn())
331 thePM->AddToImmediateList(aHilightPrs);
334 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
335 const Handle(Prs3d_Presentation)& thePresentation,
336 const Standard_Integer theMode)
340 Handle(AIS_InteractiveContext) aCtx = GetContext();
341 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
342 if (!aDriver.IsNull()) {
343 // register the custom element factory function
344 aDriver->UserDrawCallback() = SymbolPrsCallBack;
347 if (!updatePoints(20)) {
351 int aNbVertex = myPntArray->VertexNumber();
352 if (myOwner.IsNull()) {
353 myOwner = new SelectMgr_EntityOwner(this);
357 for (int i = 1; i <= aNbVertex; i++) {
358 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, i);
359 mySPoints.Append(aSP);
362 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
363 aGroup->SetPrimitivesAspect(myAspect);
365 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
368 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
369 aVert = myPntArray->Vertice(i);
370 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
373 aGroup->UserDraw(this, true);
375 // Disable frustum culling for this object by marking it as mutable
376 aGroup->Structure()->SetMutable(true);
377 //aGroup->AddPrimitiveArray(myPntArray);
382 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
383 const Standard_Integer aMode)
386 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
387 for (int i = 1; i <= mySPoints.Length(); i++)
388 aSelection->Add(mySPoints.Value(i));
393 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
395 // do not update presentation for invalid or already removed objects: the presentation
396 // should be removed soon
397 if (!myConstraint->data().get() || !myConstraint->data()->isValid())
400 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
401 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
402 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
404 double aScale = aView->Camera()->Scale();
405 if (!updatePoints(MyDist * aScale))
408 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
410 if (myVboAttribs.IsNull()) {
411 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
414 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
415 myVboAttribs->Release (aCtx.operator->());
416 myVboAttribs.Nullify();
420 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
422 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
424 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
425 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
426 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
427 ? anAspectMarker->SpriteHighlightRes(aCtx)
429 theWorkspace->EnableTexture (aSprite);
430 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
431 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
432 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
433 aLineColor = theWorkspace->HighlightColor;
436 aCtx->core11fwd->glDisable (GL_LIGHTING);
438 aCtx->core11fwd->glEnable (GL_LIGHTING);
440 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
443 myVboAttribs->BindAllAttributes(aCtx);
444 // Textured markers will be drawn with the point sprites
445 aCtx->SetPointSize (anAspectMarker->MarkerSize());
446 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
447 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
449 aCtx->core11fwd->glEnable (GL_BLEND);
450 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
452 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
454 aCtx->core11fwd->glDisable (GL_BLEND);
455 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
456 aCtx->SetPointSize (1.0f);
458 theWorkspace->EnableTexture (aTextureBack);
459 aCtx->BindProgram (NULL);
461 // Update selection position only if there is no selected object
462 // because it can corrupt selection of other objects
463 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
465 GetContext()->MainSelector()->RebuildSensitivesTree (this);
466 GetContext()->MainSelector()->RebuildObjectsTree (false);
471 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
473 if (!myVboAttribs.IsNull()) {
475 theContext->DelayedRelease (myVboAttribs);
477 myVboAttribs.Nullify();
481 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
482 const Handle(Prs3d_Presentation)& thePrs) const
484 if (theShape->isEdge()) {
485 std::shared_ptr<GeomAPI_Curve> aCurve =
486 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
487 if (aCurve->isLine()) {
488 GeomAdaptor_Curve aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
489 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
491 GeomAdaptor_Curve aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
492 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
494 } else if (theShape->isVertex()) {
495 std::shared_ptr<GeomAPI_Vertex> aVertex =
496 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
497 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
498 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
499 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
503 void SketcherPrs_SymbolPrs::drawListOfShapes(const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
504 const Handle(Prs3d_Presentation)& thePrs) const
506 int aNb = theListAttr->size();
511 for (i = 0; i < aNb; i++) {
512 aObj = theListAttr->object(i);
513 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
514 if (aShape.get() != NULL)
515 drawShape(aShape, thePrs);
519 void SketcherPrs_SymbolPrs::BoundingBox (Bnd_Box& theBndBox)
521 Select3D_BndBox3d aTmpBox;
522 for (Select3D_EntitySequenceIter aPntIter (mySPoints); aPntIter.More(); aPntIter.Next())
524 const Handle(Select3D_SensitiveEntity)& anEnt = aPntIter.Value();
525 aTmpBox.Combine (anEnt->BoundingBox());
528 theBndBox.Update (aTmpBox.CornerMin().x(), aTmpBox.CornerMin().y(), aTmpBox.CornerMin().z(),
529 aTmpBox.CornerMax().x(), aTmpBox.CornerMax().y(), aTmpBox.CornerMax().z());