1 // Created on: 2015-11-23
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2015 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
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>
34 #define MEMORY_BLOCK_SIZE 512 * 7
36 IMPLEMENT_STANDARD_RTTIEXT(AIS_RubberBand, AIS_InteractiveObject)
37 //=======================================================================
38 //function : Constructor
40 //=======================================================================
41 AIS_RubberBand::AIS_RubberBand()
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);
49 SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
50 SetZLayer (Graphic3d_ZLayerId_TopOSD);
53 //=======================================================================
54 //function : Constructor
56 //=======================================================================
57 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
58 const Aspect_TypeOfLine theLineType,
59 const Standard_Real theWidth)
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);
67 SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
68 SetZLayer (Graphic3d_ZLayerId_TopOSD);
71 //=======================================================================
72 //function : Constructor
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)
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);
87 SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
88 SetZLayer (Graphic3d_ZLayerId_TopOSD);
91 //=======================================================================
92 //function : Destructor
94 //=======================================================================
95 AIS_RubberBand::~AIS_RubberBand()
98 myTriangles.Nullify();
102 //=======================================================================
103 //function : SetRectangle
105 //=======================================================================
106 void AIS_RubberBand::SetRectangle (const Standard_Integer theMinX, const Standard_Integer theMinY,
107 const Standard_Integer theMaxX, const Standard_Integer theMaxY)
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));
116 //=======================================================================
117 //function : AddPoint
119 //=======================================================================
120 void AIS_RubberBand::AddPoint (const Graphic3d_Vec2i& thePoint)
122 myPoints.Append (thePoint);
125 //=======================================================================
126 //function : AddPoint
128 //=======================================================================
129 void AIS_RubberBand::RemoveLastPoint()
131 myPoints.Remove (myPoints.Length());
134 //=======================================================================
135 //function : GetPoints
137 //=======================================================================
138 const NCollection_Sequence<Graphic3d_Vec2i>& AIS_RubberBand::Points() const
143 //=======================================================================
144 //function : LineColor
146 //=======================================================================
147 Quantity_Color AIS_RubberBand::LineColor() const
149 Quantity_Color aColor;
150 Standard_Real aWidth;
151 Aspect_TypeOfLine aTOL;
152 myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
156 //=======================================================================
157 //function : SetLineColor
159 //=======================================================================
160 void AIS_RubberBand::SetLineColor (const Quantity_Color& theColor)
162 myDrawer->LineAspect()->SetColor (theColor);
165 //=======================================================================
166 //function : FillColor
168 //=======================================================================
169 Quantity_Color AIS_RubberBand::FillColor() const
171 return myDrawer->ShadingAspect()->Color();
174 //=======================================================================
175 //function : SetFillColor
177 //=======================================================================
178 void AIS_RubberBand::SetFillColor (const Quantity_Color& theColor)
180 myDrawer->ShadingAspect()->SetColor (theColor);
183 //=======================================================================
184 //function : SetLineWidth
186 //=======================================================================
187 void AIS_RubberBand::SetLineWidth (const Standard_Real theWidth) const
189 myDrawer->LineAspect()->SetWidth (theWidth);
192 //=======================================================================
193 //function : SetLineWidth
195 //=======================================================================
196 Standard_Real AIS_RubberBand::LineWidth() const
198 Quantity_Color aColor;
199 Standard_Real aWidth;
200 Aspect_TypeOfLine aTOL;
201 myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
205 //=======================================================================
206 //function : SetLineType
208 //=======================================================================
209 void AIS_RubberBand::SetLineType (const Aspect_TypeOfLine theType)
211 myDrawer->LineAspect()->SetTypeOfLine (theType);
214 //=======================================================================
215 //function : LineType
217 //=======================================================================
218 Aspect_TypeOfLine AIS_RubberBand::LineType() const
220 Quantity_Color aColor;
221 Standard_Real aWidth;
222 Aspect_TypeOfLine aTOL;
223 myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
227 //=======================================================================
228 //function : SetFillTransparency
230 //=======================================================================
231 void AIS_RubberBand::SetFillTransparency (const Standard_Real theValue) const
233 myDrawer->ShadingAspect()->SetTransparency (theValue);
236 //=======================================================================
237 //function : SetFillTransparency
239 //=======================================================================
240 Standard_Real AIS_RubberBand::FillTransparency() const
242 return myDrawer->ShadingAspect()->Transparency();
245 //=======================================================================
246 //function : SetFilling
248 //=======================================================================
249 void AIS_RubberBand::SetFilling (const Standard_Boolean theIsFilling)
251 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (theIsFilling ? Aspect_IS_SOLID : Aspect_IS_EMPTY);
254 //=======================================================================
255 //function : SetFilling
257 //=======================================================================
258 void AIS_RubberBand::SetFilling (const Quantity_Color theColor, const Standard_Real theTransparency)
260 SetFilling (Standard_True);
261 SetFillTransparency (theTransparency);
262 SetFillColor (theColor);
265 //=======================================================================
266 //function : IsFilling
268 //=======================================================================
269 Standard_Boolean AIS_RubberBand::IsFilling() const
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;
279 //=======================================================================
280 //function : fillTriangles
282 //=======================================================================
283 Standard_Boolean AIS_RubberBand::fillTriangles()
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)
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);
298 Standard_Real aPtSum = 0;
299 for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
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());
305 Standard_Boolean isClockwiseOrdered = aPtSum < 0;
307 for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
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),
314 aMeshStructure->AddLink (anEdge);
317 BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
318 const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
319 if (aTriangles.Extent() < 1)
320 return Standard_False;
323 Standard_Boolean toFill = Standard_False;
324 if (myTriangles.IsNull() || myTriangles->VertexNumber() != myPoints.Length() + 1)
326 toFill = Standard_True;
327 myTriangles = new Graphic3d_ArrayOfTriangles (aTriangles.Extent() * 3);
330 Standard_Integer aVertexIndex = 1;
331 BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles);
332 for (; aTriangleIt.More(); aTriangleIt.Next())
334 const Standard_Integer aTriangleId = aTriangleIt.Key();
335 const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
337 if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
340 Standard_Integer aTriangleVerts[3];
341 aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
344 for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
346 const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
347 aPts[aVertIdx] = aVertex.Coord();
352 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
354 myTriangles->AddVertex (aPts[anIt].X(), aPts[anIt].Y(), 0.0);
359 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
361 myTriangles->SetVertice (aVertexIndex++, (Standard_ShortReal)aPts[anIt].X(), (Standard_ShortReal)aPts[anIt].Y(), 0.0f);
366 aMeshStructure.Nullify();
367 anAllocator.Nullify();
369 return Standard_True;
372 //=======================================================================
375 //=======================================================================
376 void AIS_RubberBand::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
377 const Handle(Prs3d_Presentation)& thePresentation,
378 const Standard_Integer /*theMode*/)
380 Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
383 if (IsFilling() && fillTriangles())
385 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
386 aGroup->AddPrimitiveArray (myTriangles);
390 if (myBorders.IsNull() || myBorders->VertexNumber() != myPoints.Length() + 1)
392 myBorders = new Graphic3d_ArrayOfPolylines (myPoints.Length() + 1);
393 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
395 myBorders->AddVertex ((Standard_Real)myPoints.Value (anIt).x(),
396 (Standard_Real)myPoints.Value (anIt).y(), 0.0);
399 myBorders->AddVertex ((Standard_Real)myPoints.Value(1).x(),
400 (Standard_Real)myPoints.Value(1).y(), 0.0);
405 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
407 myBorders->SetVertice (anIt, (Standard_ShortReal)myPoints.Value (anIt).x(),
408 (Standard_ShortReal)myPoints.Value (anIt).y(), 0.0f);
411 myBorders->SetVertice (myPoints.Length() + 1, (Standard_ShortReal)myPoints.Value(1).x(),
412 (Standard_ShortReal)myPoints.Value(1).y(), 0.0f);
415 aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
416 aGroup->AddPrimitiveArray (myBorders);