Salome HOME
Create mirror symbols
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_SymbolPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_SymbolPrs.cpp
4 // Created:     12 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_SymbolPrs.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
10
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Vertex.h>
13 #include <GeomAPI_Curve.h>
14
15 #include <Graphic3d_ArrayOfSegments.hxx>
16 #include <Graphic3d_BndBox4f.hxx>
17
18 #include <SelectMgr_Selection.hxx>
19 #include <SelectMgr_SelectionManager.hxx>
20 #include <Select3D_SensitivePoint.hxx>
21 #include <TopLoc_Location.hxx>
22 #include <AIS_InteractiveContext.hxx>
23 #include <V3d_Viewer.hxx>
24 #include <Prs3d_Root.hxx>
25 #include <Geom_CartesianPoint.hxx>
26 #include <GeomAdaptor_Curve.hxx>
27 #include <StdPrs_DeflectionCurve.hxx>
28 #include <StdPrs_Point.hxx>
29 #include <StdPrs_Curve.hxx>
30
31 #include <OpenGl_Element.hxx>
32 #include <OpenGl_GraphicDriver.hxx>
33 #include <OpenGl_Context.hxx>
34 #include <OpenGl_View.hxx>
35 #include <OpenGl_PointSprite.hxx>
36 #include <OpenGl_VertexBuffer.hxx>
37 #include <OpenGl_ShaderManager.hxx>
38 #include <OpenGl_VertexBufferCompat.hxx>
39 #include <OpenGl_GraphicDriver.hxx>
40
41 #ifdef WIN32
42 # define FSEP "\\"
43 #else
44 # define FSEP "/"
45 #endif
46
47 /// Step between icons
48 static const double MyDist = 0.02;
49
50 /// Function to convert opengl data type
51 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
52 {
53   switch (theType) {
54     case Graphic3d_TOD_USHORT:
55       theNbComp = 1;
56       return GL_UNSIGNED_SHORT;
57     case Graphic3d_TOD_UINT:
58       theNbComp = 1;
59       return GL_UNSIGNED_INT;
60     case Graphic3d_TOD_VEC2:
61       theNbComp = 2;
62       return GL_FLOAT;
63     case Graphic3d_TOD_VEC3:
64       theNbComp = 3;
65       return GL_FLOAT;
66     case Graphic3d_TOD_VEC4:
67       theNbComp = 4;
68       return GL_FLOAT;
69     case Graphic3d_TOD_VEC4UB:
70       theNbComp = 4;
71       return GL_UNSIGNED_BYTE;
72   }
73   theNbComp = 0;
74   return GL_NONE;
75 }
76
77
78 //*******************************************************************
79 //! Auxiliary class for Vertex buffer with interleaved attributes.
80 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
81 {
82
83 public:
84
85   //! Create uninitialized VBO..
86   SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
87                         const Standard_Integer     theStride)
88   : Stride (theStride), NbAttributes(1)
89   {
90
91     memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
92   }
93
94   //! Create uninitialized VBO.
95   SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
96   : Stride (theAttribs.Stride), NbAttributes(1)
97   {
98     memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
99   }
100
101   virtual bool HasColorAttribute() const
102   {
103     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
104       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
105       if (anAttrib.Id == Graphic3d_TOA_COLOR) {
106         return true;
107       }
108     }
109     return false;
110   }
111
112   virtual bool HasNormalAttribute() const
113   {
114     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
115       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
116       if (anAttrib.Id == Graphic3d_TOA_NORM) {
117         return true;
118       }
119     }
120     return false;
121   }
122
123   virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
124   {
125     if (!OpenGl_VertexBuffer::IsValid()) {
126       return;
127     }
128
129     OpenGl_VertexBuffer::Bind (theGlCtx);
130     GLint aNbComp;
131     const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
132     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
133       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
134       const GLenum   aDataType = toGlDataType (anAttrib.DataType, aNbComp);
135       if (aDataType == GL_NONE) {
136         continue;
137       } else if (anAttrib.Id == Graphic3d_TOA_POS) {
138         OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
139         break;
140       }
141
142       anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
143     }
144   }
145
146   virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
147   {
148     if (!OpenGl_VertexBuffer::IsValid())
149       return;
150
151     OpenGl_VertexBuffer::Bind (theGlCtx);
152     GLint aNbComp;
153     const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
154     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
155     {
156       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
157       const GLenum   aDataType = toGlDataType (anAttrib.DataType, aNbComp);
158       if (aDataType == GL_NONE)
159         continue;
160
161       OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
162       anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
163     }
164   }
165
166   virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
167   {
168     if (!OpenGl_VertexBuffer::IsValid())
169       return;
170     OpenGl_VertexBuffer::Unbind (theGlCtx);
171
172     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
173       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
174       OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
175     }
176   }
177
178 public:
179
180   Graphic3d_Attribute Attribs[1];
181   Standard_Integer    Stride;
182   Standard_Integer NbAttributes;
183 };
184
185 //**************************************************************
186 //! Redefinition of OpenGl_Element
187 class SketcherPrs_Element: public OpenGl_Element
188 {
189 public:
190   SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) : 
191   OpenGl_Element(), myObj(theObj) {}
192
193   virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
194   {
195     if (!myObj.IsNull())
196       myObj->Render(theWorkspace);
197   }
198
199   virtual void Release (OpenGl_Context* theContext) 
200   {
201     if (!myObj.IsNull())
202       myObj->Release(theContext);
203   }
204
205 private:
206   Handle(SketcherPrs_SymbolPrs) myObj;
207 };
208
209
210 //**************************************************************
211 //! Definition of call back
212 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
213 {
214   Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
215   if (anIObj.IsNull()) {
216     std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
217   }
218   return new SketcherPrs_Element(anIObj);
219 }
220
221
222 //*****************************************************************************
223 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
224 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
225
226
227 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
228
229
230 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint, 
231                                              const std::shared_ptr<GeomAPI_Ax3>& thePlane)
232  : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
233 {
234   SetAutoHilight(Standard_False);
235 }
236
237 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
238 {
239   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
240   aMgr->deleteConstraint(this);
241 }
242
243
244
245 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
246 {
247   if (myIconsMap.count(iconName()) == 1) {
248     return myIconsMap[iconName()];
249   }
250   TCollection_AsciiString aFile(getenv("NewGeomResources"));
251   aFile += FSEP;
252   aFile += iconName();
253   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
254   if (aPixMap->Load(aFile)) {
255     myIconsMap[iconName()] = aPixMap;
256     return aPixMap;
257   }
258   return Handle(Image_AlienPixMap)();
259 }
260
261 void SketcherPrs_SymbolPrs::ClearSelected()
262 {
263   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );  
264   if( !aSelectionPrs.IsNull() ) {
265     aSelectionPrs->Clear(); 
266   }
267 }
268
269 void SketcherPrs_SymbolPrs::prepareAspect()
270 {
271   if (myAspect.IsNull()) {
272     myAspect = new Graphic3d_AspectMarker3d(icon());
273   }
274 }
275
276 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
277 {
278   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
279   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
280   if (aLine.get() == NULL)
281     return;
282   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
283
284   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
285   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
286
287   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
288   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
289   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
290   theGroup->AddPrimitiveArray(aLines);
291 }
292
293 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
294                                            const SelectMgr_SequenceOfOwner& theOwners)
295 {
296
297   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
298   aSelectionPrs->Clear();
299   drawLines(aSelectionPrs, Quantity_NOC_WHITE);
300
301   aSelectionPrs->SetDisplayPriority(9);
302   aSelectionPrs->Display();
303   thePM->Highlight(this);
304 }
305
306 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
307                                                  const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
308 {
309   thePM->Color(this, theColor);
310
311   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
312   aHilightPrs->Clear();
313   drawLines(aHilightPrs, theColor);
314
315   if (thePM->IsImmediateModeOn())
316     thePM->AddToImmediateList(aHilightPrs);
317 }
318
319 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
320                                    const Handle(Prs3d_Presentation)& thePresentation, 
321                                    const Standard_Integer theMode)
322 {
323   prepareAspect();
324
325   Handle(AIS_InteractiveContext) aCtx = GetContext();
326   Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
327   if (!aDriver.IsNull()) {
328     // register the custom element factory function
329     aDriver->UserDrawCallback() = SymbolPrsCallBack;
330   }
331
332   if (!updatePoints(20))
333     return;
334
335   int aNbVertex = myPntArray->VertexNumber();
336   if (myOwner.IsNull()) {
337     myOwner = new SelectMgr_EntityOwner(this);
338     for (int i = 1; i <= aNbVertex; i++) {
339       Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, myPntArray, i);
340       mySPoints.Append(aSP);
341     }
342   }
343
344   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
345   aGroup->SetPrimitivesAspect(myAspect);
346
347   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
348   gp_Pnt aVert;
349   aBnd.Clear();
350   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
351     aVert = myPntArray->Vertice(i);
352     aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
353   }
354
355   aGroup->UserDraw(this, true);
356   //aGroup->AddPrimitiveArray(myPntArray);
357 }
358
359
360
361 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
362                                             const Standard_Integer aMode)
363 {
364   ClearSelected();
365   if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
366     for (int i = 1; i <= mySPoints.Length(); i++)
367       aSelection->Add(mySPoints.Value(i));
368   }
369 }
370
371
372 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
373 {
374   const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
375   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
376   Handle(OpenGl_View) aView = theWorkspace->ActiveView();
377   
378   double aScale = aView->Camera()->Scale();
379   if (!updatePoints(MyDist * aScale))
380     return;
381
382   Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
383
384   if (myVboAttribs.IsNull()) {
385     myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
386   }
387
388   if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
389     myVboAttribs->Release (aCtx.operator->());
390     myVboAttribs.Nullify();
391     return;
392   }
393     
394   Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
395
396   const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
397       
398   if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
399     const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
400     const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
401                                               ? anAspectMarker->SpriteHighlightRes(aCtx)
402                                               : aSpriteNorm;
403     theWorkspace->EnableTexture (aSprite);
404     aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
405     const TEL_COLOUR* aLineColor  =  &anAspectMarker->Color();
406     if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
407       aLineColor = theWorkspace->HighlightColor;
408
409     aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
410
411
412     myVboAttribs->BindAllAttributes(aCtx);
413     // Textured markers will be drawn with the point sprites
414     aCtx->SetPointSize (anAspectMarker->MarkerSize());
415     aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
416     aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
417
418     aCtx->core11fwd->glEnable (GL_BLEND);
419     aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
420
421     aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
422
423     aCtx->core11fwd->glDisable (GL_BLEND);
424     aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
425     aCtx->SetPointSize (1.0f);
426   }
427   theWorkspace->EnableTexture (aTextureBack);
428   aCtx->BindProgram (NULL);
429
430   // Update selection position only if there is no selected object
431   // because it can corrupt selection of other objects
432   if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
433     GetContext()->RecomputeSelectionOnly(this);
434 }
435
436
437 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
438 {
439   if (!myVboAttribs.IsNull()) {
440     if (theContext) {
441       theContext->DelayedRelease (myVboAttribs);
442     }
443     myVboAttribs.Nullify();
444   }
445 }
446
447 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape, 
448                                       const Handle(Prs3d_Presentation)& thePrs) const
449 {
450   if (theShape->isEdge()) {
451     std::shared_ptr<GeomAPI_Curve> aCurve = 
452       std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
453     if (aCurve->isLine()) {
454       GeomAdaptor_Curve aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
455       StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
456     } else {
457       GeomAdaptor_Curve aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
458       StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
459     }
460   } else if (theShape->isVertex()) {
461     std::shared_ptr<GeomAPI_Vertex> aVertex = 
462       std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
463     std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
464     Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
465     StdPrs_Point::Add(thePrs, aPoint, myDrawer);
466   }
467 }