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()->Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Blend);
49 myDrawer->ShadingAspect()->SetTransparency (1.0);
50 myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
52 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
53 SetZLayer (Graphic3d_ZLayerId_TopOSD);
56 //=======================================================================
57 //function : Constructor
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)
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);
74 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
75 SetZLayer (Graphic3d_ZLayerId_TopOSD);
78 //=======================================================================
79 //function : Constructor
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)
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);
98 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER));
99 SetZLayer (Graphic3d_ZLayerId_TopOSD);
102 //=======================================================================
103 //function : Destructor
105 //=======================================================================
106 AIS_RubberBand::~AIS_RubberBand()
109 myTriangles.Nullify();
113 //=======================================================================
114 //function : SetRectangle
116 //=======================================================================
117 void AIS_RubberBand::SetRectangle (const Standard_Integer theMinX, const Standard_Integer theMinY,
118 const Standard_Integer theMaxX, const Standard_Integer theMaxY)
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));
127 //=======================================================================
128 //function : AddPoint
130 //=======================================================================
131 void AIS_RubberBand::AddPoint (const Graphic3d_Vec2i& thePoint)
133 myPoints.Append (thePoint);
136 //=======================================================================
137 //function : AddPoint
139 //=======================================================================
140 void AIS_RubberBand::RemoveLastPoint()
142 myPoints.Remove (myPoints.Length());
145 //=======================================================================
146 //function : GetPoints
148 //=======================================================================
149 const NCollection_Sequence<Graphic3d_Vec2i>& AIS_RubberBand::Points() const
154 //=======================================================================
155 //function : LineColor
157 //=======================================================================
158 Quantity_Color AIS_RubberBand::LineColor() const
160 return myDrawer->LineAspect()->Aspect()->Color();
163 //=======================================================================
164 //function : SetLineColor
166 //=======================================================================
167 void AIS_RubberBand::SetLineColor (const Quantity_Color& theColor)
169 myDrawer->LineAspect()->SetColor (theColor);
172 //=======================================================================
173 //function : FillColor
175 //=======================================================================
176 Quantity_Color AIS_RubberBand::FillColor() const
178 return myDrawer->ShadingAspect()->Color();
181 //=======================================================================
182 //function : SetFillColor
184 //=======================================================================
185 void AIS_RubberBand::SetFillColor (const Quantity_Color& theColor)
187 myDrawer->ShadingAspect()->SetColor (theColor);
190 //=======================================================================
191 //function : SetLineWidth
193 //=======================================================================
194 void AIS_RubberBand::SetLineWidth (const Standard_Real theWidth) const
196 myDrawer->LineAspect()->SetWidth (theWidth);
199 //=======================================================================
200 //function : SetLineWidth
202 //=======================================================================
203 Standard_Real AIS_RubberBand::LineWidth() const
205 return myDrawer->LineAspect()->Aspect()->Width();
208 //=======================================================================
209 //function : SetLineType
211 //=======================================================================
212 void AIS_RubberBand::SetLineType (const Aspect_TypeOfLine theType)
214 myDrawer->LineAspect()->SetTypeOfLine (theType);
217 //=======================================================================
218 //function : LineType
220 //=======================================================================
221 Aspect_TypeOfLine AIS_RubberBand::LineType() const
223 return myDrawer->LineAspect()->Aspect()->Type();
226 //=======================================================================
227 //function : SetFillTransparency
229 //=======================================================================
230 void AIS_RubberBand::SetFillTransparency (const Standard_Real theValue) const
232 myDrawer->ShadingAspect()->SetTransparency (theValue);
235 //=======================================================================
236 //function : SetFillTransparency
238 //=======================================================================
239 Standard_Real AIS_RubberBand::FillTransparency() const
241 return myDrawer->ShadingAspect()->Transparency();
244 //=======================================================================
245 //function : SetFilling
247 //=======================================================================
248 void AIS_RubberBand::SetFilling (const Standard_Boolean theIsFilling)
250 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (theIsFilling ? Aspect_IS_SOLID : Aspect_IS_EMPTY);
253 //=======================================================================
254 //function : SetFilling
256 //=======================================================================
257 void AIS_RubberBand::SetFilling (const Quantity_Color theColor, const Standard_Real theTransparency)
259 SetFilling (Standard_True);
260 SetFillTransparency (theTransparency);
261 SetFillColor (theColor);
264 //=======================================================================
265 //function : IsFilling
267 //=======================================================================
268 Standard_Boolean AIS_RubberBand::IsFilling() const
270 Aspect_InteriorStyle aStyle = myDrawer->ShadingAspect()->Aspect()->InteriorStyle();
271 return aStyle != Aspect_IS_EMPTY;
274 //=======================================================================
275 //function : IsPolygonClosed
277 //=======================================================================
278 Standard_Boolean AIS_RubberBand::IsPolygonClosed() const
280 return myIsPolygonClosed;
283 //=======================================================================
284 //function : SetPolygonClosed
286 //=======================================================================
287 void AIS_RubberBand::SetPolygonClosed(Standard_Boolean theIsPolygonClosed)
289 myIsPolygonClosed = theIsPolygonClosed;
292 //=======================================================================
293 //function : fillTriangles
295 //=======================================================================
296 Standard_Boolean AIS_RubberBand::fillTriangles()
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)
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));
311 Standard_Real aPtSum = 0;
312 for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
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());
318 Standard_Boolean isClockwiseOrdered = aPtSum < 0;
320 for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
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),
327 aMeshStructure->AddLink (anEdge);
330 BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
331 const IMeshData::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
332 if (aTriangles.Extent() < 1)
333 return Standard_False;
336 Standard_Boolean toFill = Standard_False;
337 if (myTriangles.IsNull() || myTriangles->VertexNumber() != aTriangles.Extent() * 3)
339 toFill = Standard_True;
340 myTriangles = new Graphic3d_ArrayOfTriangles (aTriangles.Extent() * 3, 0, Standard_True);
343 Standard_Integer aVertexIndex = 1;
344 IMeshData::IteratorOfMapOfInteger aTriangleIt (aTriangles);
345 for (; aTriangleIt.More(); aTriangleIt.Next())
347 const Standard_Integer aTriangleId = aTriangleIt.Key();
348 const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
350 if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
353 Standard_Integer aTriangleVerts[3];
354 aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
357 for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
359 const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
360 aPts[aVertIdx] = aVertex.Coord();
365 gp_Dir aNorm = gp::DZ();
366 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
368 myTriangles->AddVertex (aPts[anIt].X(), aPts[anIt].Y(), 0.0,
369 aNorm.X(), aNorm.Y(), aNorm.Z());
374 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
376 myTriangles->SetVertice (aVertexIndex++, (Standard_ShortReal)aPts[anIt].X(), (Standard_ShortReal)aPts[anIt].Y(), 0.0f);
381 aMeshStructure.Nullify();
382 anAllocator.Nullify();
384 return Standard_True;
387 //=======================================================================
390 //=======================================================================
391 void AIS_RubberBand::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
392 const Handle(Prs3d_Presentation)& thePresentation,
393 const Standard_Integer /*theMode*/)
395 Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
398 if (IsFilling() && fillTriangles())
400 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
401 aGroup->AddPrimitiveArray (myTriangles);
405 if (myBorders.IsNull() || myBorders->VertexNumber() != myPoints.Length() + (myIsPolygonClosed ? 1 : 0))
407 myBorders = new Graphic3d_ArrayOfPolylines(myPoints.Length() + (myIsPolygonClosed ? 1 : 0));
408 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
410 myBorders->AddVertex ((Standard_Real)myPoints.Value (anIt).x(),
411 (Standard_Real)myPoints.Value (anIt).y(), 0.0);
414 if (myIsPolygonClosed)
416 myBorders->AddVertex((Standard_Real)myPoints.Value(1).x(),
417 (Standard_Real)myPoints.Value(1).y(), 0.0);
423 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
425 myBorders->SetVertice (anIt, (Standard_ShortReal)myPoints.Value (anIt).x(),
426 (Standard_ShortReal)myPoints.Value (anIt).y(), 0.0f);
429 if (myIsPolygonClosed)
431 myBorders->SetVertice(myPoints.Length() + 1, (Standard_ShortReal)myPoints.Value(1).x(),
432 (Standard_ShortReal)myPoints.Value(1).y(), 0.0f);
436 aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
437 aGroup->AddPrimitiveArray (myBorders);