0030523: Visualization - Highlighting does not work anymore
[occt.git] / src / AIS / AIS_RubberBand.cxx
1 // Created on: 2015-11-23
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2015 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <AIS_RubberBand.hxx>
17 #include <BRepMesh_DataStructureOfDelaun.hxx>
18 #include <BRepMesh_Delaun.hxx>
19 #include <Graphic3d_ArrayOfPolygons.hxx>
20 #include <Graphic3d_ArrayOfPolylines.hxx>
21 #include <Graphic3d_AspectFillArea3d.hxx>
22 #include <Graphic3d_GraphicDriver.hxx>
23 #include <Graphic3d_ArrayOfTriangles.hxx>
24 #include <Graphic3d_TransModeFlags.hxx>
25 #include <Graphic3d_ZLayerId.hxx>
26 #include <Prs3d_LineAspect.hxx>
27 #include <Prs3d_Root.hxx>
28 #include <Prs3d_ShadingAspect.hxx>
29 #include <SelectMgr_EntityOwner.hxx>
30 #include <V3d_Viewer.hxx>
31 #include <V3d_View.hxx>
32
33
34 #define MEMORY_BLOCK_SIZE 512 * 7
35
36 IMPLEMENT_STANDARD_RTTIEXT(AIS_RubberBand, AIS_InteractiveObject)
37 //=======================================================================
38 //function : Constructor
39 //purpose  :
40 //=======================================================================
41 AIS_RubberBand::AIS_RubberBand()
42 : myIsPolygonClosed(Standard_True)
43 {
44   myDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_WHITE, Aspect_TOL_SOLID, 1.0));
45   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
46   myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_PLASTIC);
47   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_EMPTY);
48   myDrawer->ShadingAspect()->Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Blend);
49   myDrawer->ShadingAspect()->SetTransparency (1.0);
50   myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
51
52   SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
53   SetZLayer (Graphic3d_ZLayerId_TopOSD);
54 }
55
56 //=======================================================================
57 //function : Constructor
58 //purpose  :
59 //=======================================================================
60 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
61                                 const Aspect_TypeOfLine theLineType,
62                                 const Standard_Real theWidth,
63                                 const Standard_Boolean theIsPolygonClosed)
64 : myIsPolygonClosed(theIsPolygonClosed)
65 {
66   myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theWidth));
67   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
68   myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_PLASTIC);
69   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_EMPTY);
70   myDrawer->ShadingAspect()->Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Blend);
71   myDrawer->ShadingAspect()->SetTransparency (1.0);
72   myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
73
74   SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
75   SetZLayer (Graphic3d_ZLayerId_TopOSD);
76 }
77
78 //=======================================================================
79 //function : Constructor
80 //purpose  :
81 //=======================================================================
82 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
83                                 const Aspect_TypeOfLine theLineType,
84                                 const Quantity_Color theFillColor,
85                                 const Standard_Real theTransparency,
86                                 const Standard_Real theLineWidth,
87                                 const Standard_Boolean theIsPolygonClosed)
88 : myIsPolygonClosed (theIsPolygonClosed)
89 {
90   myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theLineWidth));
91   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
92   myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_PLASTIC);
93   myDrawer->ShadingAspect()->SetColor (theFillColor);
94   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
95   myDrawer->ShadingAspect()->Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Blend);
96   myDrawer->ShadingAspect()->SetTransparency (theTransparency);
97
98   SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
99   SetZLayer (Graphic3d_ZLayerId_TopOSD);
100 }
101
102 //=======================================================================
103 //function : Destructor
104 //purpose  :
105 //=======================================================================
106 AIS_RubberBand::~AIS_RubberBand()
107 {
108   myPoints.Clear();
109   myTriangles.Nullify();
110   myBorders.Nullify();
111 }
112
113 //=======================================================================
114 //function : SetRectangle
115 //purpose  :
116 //=======================================================================
117 void AIS_RubberBand::SetRectangle (const Standard_Integer theMinX, const Standard_Integer theMinY,
118                                    const Standard_Integer theMaxX, const Standard_Integer theMaxY)
119 {
120   myPoints.Clear();
121   myPoints.Append (Graphic3d_Vec2i (theMinX, theMinY));
122   myPoints.Append (Graphic3d_Vec2i (theMinX, theMaxY));
123   myPoints.Append (Graphic3d_Vec2i (theMaxX, theMaxY));
124   myPoints.Append (Graphic3d_Vec2i (theMaxX, theMinY));
125 }
126
127 //=======================================================================
128 //function : AddPoint
129 //purpose  :
130 //=======================================================================
131 void AIS_RubberBand::AddPoint (const Graphic3d_Vec2i& thePoint)
132 {
133   myPoints.Append (thePoint);
134 }
135
136 //=======================================================================
137 //function : AddPoint
138 //purpose  :
139 //=======================================================================
140 void AIS_RubberBand::RemoveLastPoint()
141 {
142   myPoints.Remove (myPoints.Length());
143 }
144
145 //=======================================================================
146 //function : GetPoints
147 //purpose  :
148 //=======================================================================
149 const NCollection_Sequence<Graphic3d_Vec2i>& AIS_RubberBand::Points() const
150 {
151   return myPoints;
152 }
153
154 //=======================================================================
155 //function : LineColor
156 //purpose  :
157 //=======================================================================
158 Quantity_Color AIS_RubberBand::LineColor() const
159 {
160   return myDrawer->LineAspect()->Aspect()->Color();
161 }
162
163 //=======================================================================
164 //function : SetLineColor
165 //purpose  :
166 //=======================================================================
167 void AIS_RubberBand::SetLineColor (const Quantity_Color& theColor)
168 {
169   myDrawer->LineAspect()->SetColor (theColor);
170 }
171
172 //=======================================================================
173 //function : FillColor
174 //purpose  :
175 //=======================================================================
176 Quantity_Color AIS_RubberBand::FillColor() const
177 {
178   return myDrawer->ShadingAspect()->Color();
179 }
180
181 //=======================================================================
182 //function : SetFillColor
183 //purpose  :
184 //=======================================================================
185 void AIS_RubberBand::SetFillColor (const Quantity_Color& theColor)
186 {
187   myDrawer->ShadingAspect()->SetColor (theColor);
188 }
189
190 //=======================================================================
191 //function : SetLineWidth
192 //purpose  :
193 //=======================================================================
194 void AIS_RubberBand::SetLineWidth (const Standard_Real theWidth) const
195 {
196   myDrawer->LineAspect()->SetWidth (theWidth);
197 }
198
199 //=======================================================================
200 //function : SetLineWidth
201 //purpose  :
202 //=======================================================================
203 Standard_Real AIS_RubberBand::LineWidth() const
204 {
205   return myDrawer->LineAspect()->Aspect()->Width();
206 }
207
208 //=======================================================================
209 //function : SetLineType
210 //purpose  :
211 //=======================================================================
212 void AIS_RubberBand::SetLineType (const Aspect_TypeOfLine theType)
213 {
214   myDrawer->LineAspect()->SetTypeOfLine (theType);
215 }
216
217 //=======================================================================
218 //function : LineType
219 //purpose  :
220 //=======================================================================
221 Aspect_TypeOfLine AIS_RubberBand::LineType() const
222 {
223   return myDrawer->LineAspect()->Aspect()->Type();
224 }
225
226 //=======================================================================
227 //function : SetFillTransparency
228 //purpose  :
229 //=======================================================================
230 void AIS_RubberBand::SetFillTransparency (const Standard_Real theValue) const
231 {
232   myDrawer->ShadingAspect()->SetTransparency (theValue);
233 }
234
235 //=======================================================================
236 //function : SetFillTransparency
237 //purpose  :
238 //=======================================================================
239 Standard_Real AIS_RubberBand::FillTransparency() const
240 {
241   return myDrawer->ShadingAspect()->Transparency();
242 }
243
244 //=======================================================================
245 //function : SetFilling
246 //purpose  :
247 //=======================================================================
248 void AIS_RubberBand::SetFilling (const Standard_Boolean theIsFilling)
249 {
250   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (theIsFilling ? Aspect_IS_SOLID : Aspect_IS_EMPTY);
251 }
252
253 //=======================================================================
254 //function : SetFilling
255 //purpose  :
256 //=======================================================================
257 void AIS_RubberBand::SetFilling (const Quantity_Color theColor, const Standard_Real theTransparency)
258 {
259   SetFilling (Standard_True);
260   SetFillTransparency (theTransparency);
261   SetFillColor (theColor);
262 }
263
264 //=======================================================================
265 //function : IsFilling
266 //purpose  :
267 //=======================================================================
268 Standard_Boolean AIS_RubberBand::IsFilling() const
269 {
270   Aspect_InteriorStyle aStyle = myDrawer->ShadingAspect()->Aspect()->InteriorStyle();
271   return aStyle != Aspect_IS_EMPTY;
272 }
273
274 //=======================================================================
275 //function : IsPolygonClosed
276 //purpose  :
277 //=======================================================================
278 Standard_Boolean AIS_RubberBand::IsPolygonClosed() const
279 {
280   return myIsPolygonClosed;
281 }
282
283 //=======================================================================
284 //function : SetPolygonClosed
285 //purpose  :
286 //=======================================================================
287 void AIS_RubberBand::SetPolygonClosed(Standard_Boolean theIsPolygonClosed)
288 {
289   myIsPolygonClosed = theIsPolygonClosed;
290 }
291
292 //=======================================================================
293 //function : fillTriangles
294 //purpose  :
295 //=======================================================================
296 Standard_Boolean AIS_RubberBand::fillTriangles()
297 {
298   Handle(NCollection_IncAllocator) anAllocator = new NCollection_IncAllocator (MEMORY_BLOCK_SIZE);
299   Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = new BRepMesh_DataStructureOfDelaun(anAllocator);
300   Standard_Integer aPtsLower = myPoints.Lower();
301   Standard_Integer aPtsUpper = myPoints.Upper();
302   IMeshData::VectorOfInteger anIndexes (myPoints.Length(), anAllocator);
303   for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx)
304   {
305     gp_XY aP ((Standard_Real)myPoints.Value (aPtIdx).x(),
306               (Standard_Real)myPoints.Value (aPtIdx).y());
307     BRepMesh_Vertex aVertex (aP, aPtIdx, BRepMesh_Frontier);
308     anIndexes.Append (aMeshStructure->AddNode (aVertex));
309   }
310
311   Standard_Real aPtSum = 0;
312   for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
313   {
314     Standard_Integer aNextIdx = (aIdx % myPoints.Length()) + 1;
315     aPtSum += (Standard_Real)(myPoints.Value (aNextIdx).x() - myPoints.Value (aIdx).x())
316              * (Standard_Real)(myPoints.Value (aNextIdx).y() + myPoints.Value (aIdx).y());
317   }
318   Standard_Boolean isClockwiseOrdered = aPtSum < 0;
319
320   for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
321   {
322     Standard_Integer aPtIdx = isClockwiseOrdered ? aIdx : (aIdx + 1) % anIndexes.Length();
323     Standard_Integer aNextPtIdx = isClockwiseOrdered ? (aIdx + 1) % anIndexes.Length() : aIdx;
324     BRepMesh_Edge anEdge (anIndexes.Value (aPtIdx),
325                           anIndexes.Value (aNextPtIdx),
326                           BRepMesh_Frontier);
327     aMeshStructure->AddLink (anEdge);
328   }
329
330   BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
331   const IMeshData::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
332   if (aTriangles.Extent() < 1)
333     return Standard_False;
334
335
336   Standard_Boolean toFill = Standard_False;
337   if (myTriangles.IsNull() || myTriangles->VertexNumber() != aTriangles.Extent() * 3)
338   {
339     toFill = Standard_True;
340     myTriangles = new Graphic3d_ArrayOfTriangles (aTriangles.Extent() * 3, 0, Standard_True);
341   }
342
343   Standard_Integer aVertexIndex = 1;
344   IMeshData::IteratorOfMapOfInteger aTriangleIt (aTriangles);
345   for (; aTriangleIt.More(); aTriangleIt.Next())
346   {
347     const Standard_Integer aTriangleId = aTriangleIt.Key();
348     const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
349
350     if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
351       continue;
352
353     Standard_Integer aTriangleVerts[3];
354     aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
355
356     gp_Pnt2d aPts[3];
357     for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
358     {
359       const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
360       aPts[aVertIdx] = aVertex.Coord();
361     }
362
363     if (toFill)
364     {
365       gp_Dir aNorm = gp::DZ();
366       for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
367       {
368         myTriangles->AddVertex (aPts[anIt].X(), aPts[anIt].Y(), 0.0,
369                                 aNorm.X(), aNorm.Y(), aNorm.Z());
370       }
371     }
372     else
373     {
374       for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
375       {
376         myTriangles->SetVertice (aVertexIndex++, (Standard_ShortReal)aPts[anIt].X(), (Standard_ShortReal)aPts[anIt].Y(), 0.0f);
377       }
378     }
379   }
380
381   aMeshStructure.Nullify();
382   anAllocator.Nullify();
383
384   return Standard_True;
385 }
386
387 //=======================================================================
388 //function : Compute
389 //purpose  :
390 //=======================================================================
391 void AIS_RubberBand::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
392                               const Handle(Prs3d_Presentation)& thePresentation,
393                               const Standard_Integer /*theMode*/)
394 {
395   Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
396
397   // Draw filling
398   if (IsFilling() && fillTriangles())
399   {
400     aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
401     aGroup->AddPrimitiveArray (myTriangles);
402   }
403
404   // Draw frame
405   if (myBorders.IsNull() || myBorders->VertexNumber() != myPoints.Length() + (myIsPolygonClosed ? 1 : 0))
406   {
407     myBorders = new Graphic3d_ArrayOfPolylines(myPoints.Length() + (myIsPolygonClosed ? 1 : 0));
408      for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
409      {
410        myBorders->AddVertex ((Standard_Real)myPoints.Value (anIt).x(),
411                              (Standard_Real)myPoints.Value (anIt).y(), 0.0);
412      }
413
414      if (myIsPolygonClosed)
415      {
416        myBorders->AddVertex((Standard_Real)myPoints.Value(1).x(),
417                             (Standard_Real)myPoints.Value(1).y(), 0.0);
418      }
419
420   }
421   else
422   {
423     for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
424     {
425           myBorders->SetVertice (anIt, (Standard_ShortReal)myPoints.Value (anIt).x(),
426                                  (Standard_ShortReal)myPoints.Value (anIt).y(), 0.0f);
427     }
428
429     if (myIsPolygonClosed)
430     {
431       myBorders->SetVertice(myPoints.Length() + 1, (Standard_ShortReal)myPoints.Value(1).x(),
432                            (Standard_ShortReal)myPoints.Value(1).y(), 0.0f);
433     }
434   }
435
436   aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
437   aGroup->AddPrimitiveArray (myBorders);
438 }