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()
42 : myIsPolygonClosed(Standard_True)
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()->SetTransparency (1.0);
49 myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
51 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
52 SetZLayer (Graphic3d_ZLayerId_TopOSD);
55 //=======================================================================
56 //function : Constructor
58 //=======================================================================
59 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
60 const Aspect_TypeOfLine theLineType,
61 const Standard_Real theWidth,
62 const Standard_Boolean theIsPolygonClosed)
63 : myIsPolygonClosed(theIsPolygonClosed)
65 myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theWidth));
66 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
67 myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_PLASTIC);
68 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_EMPTY);
69 myDrawer->ShadingAspect()->SetTransparency (1.0);
70 myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
72 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
73 SetZLayer (Graphic3d_ZLayerId_TopOSD);
76 //=======================================================================
77 //function : Constructor
79 //=======================================================================
80 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
81 const Aspect_TypeOfLine theLineType,
82 const Quantity_Color theFillColor,
83 const Standard_Real theTransparency,
84 const Standard_Real theLineWidth,
85 const Standard_Boolean theIsPolygonClosed)
86 : myIsPolygonClosed (theIsPolygonClosed)
88 myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theLineWidth));
89 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
90 myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_PLASTIC);
91 myDrawer->ShadingAspect()->SetColor (theFillColor);
92 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
93 myDrawer->ShadingAspect()->SetTransparency (theTransparency);
95 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
96 SetZLayer (Graphic3d_ZLayerId_TopOSD);
99 //=======================================================================
100 //function : Destructor
102 //=======================================================================
103 AIS_RubberBand::~AIS_RubberBand()
106 myTriangles.Nullify();
110 //=======================================================================
111 //function : SetRectangle
113 //=======================================================================
114 void AIS_RubberBand::SetRectangle (const Standard_Integer theMinX, const Standard_Integer theMinY,
115 const Standard_Integer theMaxX, const Standard_Integer theMaxY)
118 myPoints.Append (Graphic3d_Vec2i (theMinX, theMinY));
119 myPoints.Append (Graphic3d_Vec2i (theMinX, theMaxY));
120 myPoints.Append (Graphic3d_Vec2i (theMaxX, theMaxY));
121 myPoints.Append (Graphic3d_Vec2i (theMaxX, theMinY));
124 //=======================================================================
125 //function : AddPoint
127 //=======================================================================
128 void AIS_RubberBand::AddPoint (const Graphic3d_Vec2i& thePoint)
130 myPoints.Append (thePoint);
133 //=======================================================================
134 //function : AddPoint
136 //=======================================================================
137 void AIS_RubberBand::RemoveLastPoint()
139 myPoints.Remove (myPoints.Length());
142 //=======================================================================
143 //function : GetPoints
145 //=======================================================================
146 const NCollection_Sequence<Graphic3d_Vec2i>& AIS_RubberBand::Points() const
151 //=======================================================================
152 //function : LineColor
154 //=======================================================================
155 Quantity_Color AIS_RubberBand::LineColor() const
157 return myDrawer->LineAspect()->Aspect()->Color();
160 //=======================================================================
161 //function : SetLineColor
163 //=======================================================================
164 void AIS_RubberBand::SetLineColor (const Quantity_Color& theColor)
166 myDrawer->LineAspect()->SetColor (theColor);
169 //=======================================================================
170 //function : FillColor
172 //=======================================================================
173 Quantity_Color AIS_RubberBand::FillColor() const
175 return myDrawer->ShadingAspect()->Color();
178 //=======================================================================
179 //function : SetFillColor
181 //=======================================================================
182 void AIS_RubberBand::SetFillColor (const Quantity_Color& theColor)
184 myDrawer->ShadingAspect()->SetColor (theColor);
187 //=======================================================================
188 //function : SetLineWidth
190 //=======================================================================
191 void AIS_RubberBand::SetLineWidth (const Standard_Real theWidth) const
193 myDrawer->LineAspect()->SetWidth (theWidth);
196 //=======================================================================
197 //function : SetLineWidth
199 //=======================================================================
200 Standard_Real AIS_RubberBand::LineWidth() const
202 return myDrawer->LineAspect()->Aspect()->Width();
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 return myDrawer->LineAspect()->Aspect()->Type();
223 //=======================================================================
224 //function : SetFillTransparency
226 //=======================================================================
227 void AIS_RubberBand::SetFillTransparency (const Standard_Real theValue) const
229 myDrawer->ShadingAspect()->SetTransparency (theValue);
232 //=======================================================================
233 //function : SetFillTransparency
235 //=======================================================================
236 Standard_Real AIS_RubberBand::FillTransparency() const
238 return myDrawer->ShadingAspect()->Transparency();
241 //=======================================================================
242 //function : SetFilling
244 //=======================================================================
245 void AIS_RubberBand::SetFilling (const Standard_Boolean theIsFilling)
247 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (theIsFilling ? Aspect_IS_SOLID : Aspect_IS_EMPTY);
250 //=======================================================================
251 //function : SetFilling
253 //=======================================================================
254 void AIS_RubberBand::SetFilling (const Quantity_Color theColor, const Standard_Real theTransparency)
256 SetFilling (Standard_True);
257 SetFillTransparency (theTransparency);
258 SetFillColor (theColor);
261 //=======================================================================
262 //function : IsFilling
264 //=======================================================================
265 Standard_Boolean AIS_RubberBand::IsFilling() const
267 Aspect_InteriorStyle aStyle = myDrawer->ShadingAspect()->Aspect()->InteriorStyle();
268 return aStyle != Aspect_IS_EMPTY;
271 //=======================================================================
272 //function : IsPolygonClosed
274 //=======================================================================
275 Standard_Boolean AIS_RubberBand::IsPolygonClosed() const
277 return myIsPolygonClosed;
280 //=======================================================================
281 //function : SetPolygonClosed
283 //=======================================================================
284 void AIS_RubberBand::SetPolygonClosed(Standard_Boolean theIsPolygonClosed)
286 myIsPolygonClosed = theIsPolygonClosed;
289 //=======================================================================
290 //function : fillTriangles
292 //=======================================================================
293 Standard_Boolean AIS_RubberBand::fillTriangles()
295 Handle(NCollection_IncAllocator) anAllocator = new NCollection_IncAllocator (MEMORY_BLOCK_SIZE);
296 Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = new BRepMesh_DataStructureOfDelaun(anAllocator);
297 Standard_Integer aPtsLower = myPoints.Lower();
298 Standard_Integer aPtsUpper = myPoints.Upper();
299 BRepMesh::Array1OfInteger anIndexes (0, myPoints.Length() - 1);
300 for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx)
302 gp_XY aP ((Standard_Real)myPoints.Value (aPtIdx).x(),
303 (Standard_Real)myPoints.Value (aPtIdx).y());
304 BRepMesh_Vertex aVertex (aP, aPtIdx, BRepMesh_Frontier);
305 anIndexes.ChangeValue (aPtIdx - aPtsLower) = aMeshStructure->AddNode (aVertex);
308 Standard_Real aPtSum = 0;
309 for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
311 Standard_Integer aNextIdx = (aIdx % myPoints.Length()) + 1;
312 aPtSum += (Standard_Real)(myPoints.Value (aNextIdx).x() - myPoints.Value (aIdx).x())
313 * (Standard_Real)(myPoints.Value (aNextIdx).y() + myPoints.Value (aIdx).y());
315 Standard_Boolean isClockwiseOrdered = aPtSum < 0;
317 for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
319 Standard_Integer aPtIdx = isClockwiseOrdered ? aIdx : (aIdx + 1) % anIndexes.Length();
320 Standard_Integer aNextPtIdx = isClockwiseOrdered ? (aIdx + 1) % anIndexes.Length() : aIdx;
321 BRepMesh_Edge anEdge (anIndexes.Value (aPtIdx),
322 anIndexes.Value (aNextPtIdx),
324 aMeshStructure->AddLink (anEdge);
327 BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
328 const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
329 if (aTriangles.Extent() < 1)
330 return Standard_False;
333 Standard_Boolean toFill = Standard_False;
334 if (myTriangles.IsNull() || myTriangles->VertexNumber() != aTriangles.Extent() * 3)
336 toFill = Standard_True;
337 myTriangles = new Graphic3d_ArrayOfTriangles (aTriangles.Extent() * 3, 0, Standard_True);
340 Standard_Integer aVertexIndex = 1;
341 BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles);
342 for (; aTriangleIt.More(); aTriangleIt.Next())
344 const Standard_Integer aTriangleId = aTriangleIt.Key();
345 const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
347 if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
350 Standard_Integer aTriangleVerts[3];
351 aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
354 for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
356 const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
357 aPts[aVertIdx] = aVertex.Coord();
362 gp_Dir aNorm = gp::DZ();
363 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
365 myTriangles->AddVertex (aPts[anIt].X(), aPts[anIt].Y(), 0.0,
366 aNorm.X(), aNorm.Y(), aNorm.Z());
371 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
373 myTriangles->SetVertice (aVertexIndex++, (Standard_ShortReal)aPts[anIt].X(), (Standard_ShortReal)aPts[anIt].Y(), 0.0f);
378 aMeshStructure.Nullify();
379 anAllocator.Nullify();
381 return Standard_True;
384 //=======================================================================
387 //=======================================================================
388 void AIS_RubberBand::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
389 const Handle(Prs3d_Presentation)& thePresentation,
390 const Standard_Integer /*theMode*/)
392 Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
395 if (IsFilling() && fillTriangles())
397 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
398 aGroup->AddPrimitiveArray (myTriangles);
402 if (myBorders.IsNull() || myBorders->VertexNumber() != myPoints.Length() + (myIsPolygonClosed ? 1 : 0))
404 myBorders = new Graphic3d_ArrayOfPolylines(myPoints.Length() + (myIsPolygonClosed ? 1 : 0));
405 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
407 myBorders->AddVertex ((Standard_Real)myPoints.Value (anIt).x(),
408 (Standard_Real)myPoints.Value (anIt).y(), 0.0);
411 if (myIsPolygonClosed)
413 myBorders->AddVertex((Standard_Real)myPoints.Value(1).x(),
414 (Standard_Real)myPoints.Value(1).y(), 0.0);
420 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
422 myBorders->SetVertice (anIt, (Standard_ShortReal)myPoints.Value (anIt).x(),
423 (Standard_ShortReal)myPoints.Value (anIt).y(), 0.0f);
426 if (myIsPolygonClosed)
428 myBorders->SetVertice(myPoints.Length() + 1, (Standard_ShortReal)myPoints.Value(1).x(),
429 (Standard_ShortReal)myPoints.Value(1).y(), 0.0f);
433 aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
434 aGroup->AddPrimitiveArray (myBorders);