2169e8d5fab5649b950c16ff6ba2e2dfe420b86a
[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 {
43   myDrawer->SetLineAspect (new Prs3d_LineAspect (Quantity_NOC_WHITE, Aspect_TOL_SOLID, 1.0));
44   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
45   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_EMPTY);
46   myDrawer->ShadingAspect()->SetTransparency (1.0);
47   myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
48
49   SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
50   SetZLayer (Graphic3d_ZLayerId_TopOSD);
51 }
52
53 //=======================================================================
54 //function : Constructor
55 //purpose  :
56 //=======================================================================
57 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
58                                 const Aspect_TypeOfLine theLineType,
59                                 const Standard_Real theWidth)
60 {
61   myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theWidth));
62   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
63   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_EMPTY);
64   myDrawer->ShadingAspect()->SetTransparency (1.0);
65   myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
66
67   SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
68   SetZLayer (Graphic3d_ZLayerId_TopOSD);
69 }
70
71 //=======================================================================
72 //function : Constructor
73 //purpose  :
74 //=======================================================================
75 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
76                                 const Aspect_TypeOfLine theLineType,
77                                 const Quantity_Color theFillColor,
78                                 const Standard_Real theTransparency,
79                                 const Standard_Real theLineWidth)
80 {
81   myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theLineWidth));
82   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
83   myDrawer->ShadingAspect()->SetColor (theFillColor);
84   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
85   myDrawer->ShadingAspect()->SetTransparency (theTransparency);
86
87   SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
88   SetZLayer (Graphic3d_ZLayerId_TopOSD);
89 }
90
91 //=======================================================================
92 //function : Destructor
93 //purpose  :
94 //=======================================================================
95 AIS_RubberBand::~AIS_RubberBand()
96 {
97   myPoints.Clear();
98   myTriangles.Nullify();
99   myBorders.Nullify();
100 }
101
102 //=======================================================================
103 //function : SetRectangle
104 //purpose  :
105 //=======================================================================
106 void AIS_RubberBand::SetRectangle (const Standard_Integer theMinX, const Standard_Integer theMinY,
107                                    const Standard_Integer theMaxX, const Standard_Integer theMaxY)
108 {
109   myPoints.Clear();
110   myPoints.Append (Graphic3d_Vec2i (theMinX, theMinY));
111   myPoints.Append (Graphic3d_Vec2i (theMinX, theMaxY));
112   myPoints.Append (Graphic3d_Vec2i (theMaxX, theMaxY));
113   myPoints.Append (Graphic3d_Vec2i (theMaxX, theMinY));
114 }
115
116 //=======================================================================
117 //function : AddPoint
118 //purpose  :
119 //=======================================================================
120 void AIS_RubberBand::AddPoint (const Graphic3d_Vec2i& thePoint)
121 {
122   myPoints.Append (thePoint);
123 }
124
125 //=======================================================================
126 //function : AddPoint
127 //purpose  :
128 //=======================================================================
129 void AIS_RubberBand::RemoveLastPoint()
130 {
131   myPoints.Remove (myPoints.Length());
132 }
133
134 //=======================================================================
135 //function : GetPoints
136 //purpose  :
137 //=======================================================================
138 const NCollection_Sequence<Graphic3d_Vec2i>& AIS_RubberBand::Points() const
139 {
140   return myPoints;
141 }
142
143 //=======================================================================
144 //function : LineColor
145 //purpose  :
146 //=======================================================================
147 Quantity_Color AIS_RubberBand::LineColor() const
148 {
149   Quantity_Color aColor;
150   Standard_Real aWidth;
151   Aspect_TypeOfLine aTOL;
152   myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
153   return aColor;
154 }
155
156 //=======================================================================
157 //function : SetLineColor
158 //purpose  :
159 //=======================================================================
160 void AIS_RubberBand::SetLineColor (const Quantity_Color& theColor)
161 {
162   myDrawer->LineAspect()->SetColor (theColor);
163 }
164
165 //=======================================================================
166 //function : FillColor
167 //purpose  :
168 //=======================================================================
169 Quantity_Color AIS_RubberBand::FillColor() const
170 {
171   return myDrawer->ShadingAspect()->Color();
172 }
173
174 //=======================================================================
175 //function : SetFillColor
176 //purpose  :
177 //=======================================================================
178 void AIS_RubberBand::SetFillColor (const Quantity_Color& theColor)
179 {
180   myDrawer->ShadingAspect()->SetColor (theColor);
181 }
182
183 //=======================================================================
184 //function : SetLineWidth
185 //purpose  :
186 //=======================================================================
187 void AIS_RubberBand::SetLineWidth (const Standard_Real theWidth) const
188 {
189   myDrawer->LineAspect()->SetWidth (theWidth);
190 }
191
192 //=======================================================================
193 //function : SetLineWidth
194 //purpose  :
195 //=======================================================================
196 Standard_Real AIS_RubberBand::LineWidth() const
197 {
198   Quantity_Color aColor;
199   Standard_Real aWidth;
200   Aspect_TypeOfLine aTOL;
201   myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
202   return aWidth;
203 }
204
205 //=======================================================================
206 //function : SetLineType
207 //purpose  :
208 //=======================================================================
209 void AIS_RubberBand::SetLineType (const Aspect_TypeOfLine theType)
210 {
211   myDrawer->LineAspect()->SetTypeOfLine (theType);
212 }
213
214 //=======================================================================
215 //function : LineType
216 //purpose  :
217 //=======================================================================
218 Aspect_TypeOfLine AIS_RubberBand::LineType() const
219 {
220   Quantity_Color aColor;
221   Standard_Real aWidth;
222   Aspect_TypeOfLine aTOL;
223   myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
224   return aTOL;
225 }
226
227 //=======================================================================
228 //function : SetFillTransparency
229 //purpose  :
230 //=======================================================================
231 void AIS_RubberBand::SetFillTransparency (const Standard_Real theValue) const
232 {
233   myDrawer->ShadingAspect()->SetTransparency (theValue);
234 }
235
236 //=======================================================================
237 //function : SetFillTransparency
238 //purpose  :
239 //=======================================================================
240 Standard_Real AIS_RubberBand::FillTransparency() const
241 {
242   return myDrawer->ShadingAspect()->Transparency();
243 }
244
245 //=======================================================================
246 //function : SetFilling
247 //purpose  :
248 //=======================================================================
249 void AIS_RubberBand::SetFilling (const Standard_Boolean theIsFilling)
250 {
251   myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (theIsFilling ? Aspect_IS_SOLID : Aspect_IS_EMPTY);
252 }
253
254 //=======================================================================
255 //function : SetFilling
256 //purpose  :
257 //=======================================================================
258 void AIS_RubberBand::SetFilling (const Quantity_Color theColor, const Standard_Real theTransparency)
259 {
260   SetFilling (Standard_True);
261   SetFillTransparency (theTransparency);
262   SetFillColor (theColor);
263 }
264
265 //=======================================================================
266 //function : IsFilling
267 //purpose  :
268 //=======================================================================
269 Standard_Boolean AIS_RubberBand::IsFilling() const
270 {
271   Aspect_InteriorStyle aStyle;
272   Quantity_Color anIntColor, anEdgeColor;
273   Aspect_TypeOfLine aTOL;
274   Standard_Real aWidth;
275   myDrawer->ShadingAspect()->Aspect()->Values (aStyle, anIntColor, anEdgeColor, aTOL, aWidth);
276   return aStyle != Aspect_IS_EMPTY;
277 }
278
279 //=======================================================================
280 //function : fillTriangles
281 //purpose  :
282 //=======================================================================
283 Standard_Boolean AIS_RubberBand::fillTriangles()
284 {
285   Handle(NCollection_IncAllocator) anAllocator = new NCollection_IncAllocator (MEMORY_BLOCK_SIZE);
286   Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = new BRepMesh_DataStructureOfDelaun(anAllocator);
287   Standard_Integer aPtsLower = myPoints.Lower();
288   Standard_Integer aPtsUpper = myPoints.Upper();
289   BRepMesh::Array1OfInteger anIndexes (0, myPoints.Length() - 1);
290   for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx)
291   {
292     gp_XY aP ((Standard_Real)myPoints.Value (aPtIdx).x(),
293               (Standard_Real)myPoints.Value (aPtIdx).y());
294     BRepMesh_Vertex aVertex (aP, aPtIdx, BRepMesh_Frontier);
295     anIndexes.ChangeValue (aPtIdx - aPtsLower) = aMeshStructure->AddNode (aVertex);
296   }
297
298   Standard_Real aPtSum = 0;
299   for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
300   {
301     Standard_Integer aNextIdx = (aIdx % myPoints.Length()) + 1;
302     aPtSum += (Standard_Real)(myPoints.Value (aNextIdx).x() - myPoints.Value (aIdx).x())
303              * (Standard_Real)(myPoints.Value (aNextIdx).y() + myPoints.Value (aIdx).y());
304   }
305   Standard_Boolean isClockwiseOrdered = aPtSum < 0;
306
307   for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
308   {
309     Standard_Integer aPtIdx = isClockwiseOrdered ? aIdx : (aIdx + 1) % anIndexes.Length();
310     Standard_Integer aNextPtIdx = isClockwiseOrdered ? (aIdx + 1) % anIndexes.Length() : aIdx;
311     BRepMesh_Edge anEdge (anIndexes.Value (aPtIdx),
312                           anIndexes.Value (aNextPtIdx),
313                           BRepMesh_Frontier);
314     aMeshStructure->AddLink (anEdge);
315   }
316
317   BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
318   const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
319   if (aTriangles.Extent() < 1)
320     return Standard_False;
321
322
323   Standard_Boolean toFill = Standard_False;
324   if (myTriangles.IsNull() || myTriangles->VertexNumber() != myPoints.Length() + 1)
325   {
326     toFill = Standard_True;
327     myTriangles = new Graphic3d_ArrayOfTriangles (aTriangles.Extent() * 3);
328   }
329
330   Standard_Integer aVertexIndex = 1;
331   BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles);
332   for (; aTriangleIt.More(); aTriangleIt.Next())
333   {
334     const Standard_Integer aTriangleId = aTriangleIt.Key();
335     const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
336
337     if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
338       continue;
339
340     Standard_Integer aTriangleVerts[3];
341     aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
342
343     gp_Pnt2d aPts[3];
344     for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
345     {
346       const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
347       aPts[aVertIdx] = aVertex.Coord();
348     }
349
350     if (toFill)
351     {
352       for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
353       {
354         myTriangles->AddVertex (aPts[anIt].X(), aPts[anIt].Y(), 0.0);
355       }
356     }
357     else
358     {
359       for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
360       {
361         myTriangles->SetVertice (aVertexIndex++, (Standard_ShortReal)aPts[anIt].X(), (Standard_ShortReal)aPts[anIt].Y(), 0.0f);
362       }
363     }
364   }
365
366   aMeshStructure.Nullify();
367   anAllocator.Nullify();
368
369   return Standard_True;
370 }
371
372 //=======================================================================
373 //function : Compute
374 //purpose  :
375 //=======================================================================
376 void AIS_RubberBand::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
377                               const Handle(Prs3d_Presentation)& thePresentation,
378                               const Standard_Integer /*theMode*/)
379 {
380   Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
381
382   // Draw filling
383   if (IsFilling() && fillTriangles())
384   {
385     aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
386     aGroup->AddPrimitiveArray (myTriangles);
387   }
388
389   // Draw frame
390   if (myBorders.IsNull() || myBorders->VertexNumber() != myPoints.Length() + 1)
391   {
392      myBorders = new Graphic3d_ArrayOfPolylines (myPoints.Length() + 1);
393      for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
394      {
395        myBorders->AddVertex ((Standard_Real)myPoints.Value (anIt).x(),
396                              (Standard_Real)myPoints.Value (anIt).y(), 0.0);
397      }
398
399      myBorders->AddVertex ((Standard_Real)myPoints.Value(1).x(),
400                            (Standard_Real)myPoints.Value(1).y(), 0.0);
401
402   }
403   else
404   {
405     for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
406     {
407           myBorders->SetVertice (anIt, (Standard_ShortReal)myPoints.Value (anIt).x(),
408                                  (Standard_ShortReal)myPoints.Value (anIt).y(), 0.0f);
409     }
410
411     myBorders->SetVertice (myPoints.Length() + 1, (Standard_ShortReal)myPoints.Value(1).x(),
412                            (Standard_ShortReal)myPoints.Value(1).y(), 0.0f);
413   }
414
415   aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
416   aGroup->AddPrimitiveArray (myBorders);
417 }