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()->SetMaterial (Graphic3d_NOM_PLASTIC);
46 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_EMPTY);
47 myDrawer->ShadingAspect()->SetTransparency (1.0);
48 myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
50 SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
51 SetZLayer (Graphic3d_ZLayerId_TopOSD);
54 //=======================================================================
55 //function : Constructor
57 //=======================================================================
58 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
59 const Aspect_TypeOfLine theLineType,
60 const Standard_Real theWidth)
62 myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theWidth));
63 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
64 myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_PLASTIC);
65 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_EMPTY);
66 myDrawer->ShadingAspect()->SetTransparency (1.0);
67 myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
69 SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
70 SetZLayer (Graphic3d_ZLayerId_TopOSD);
73 //=======================================================================
74 //function : Constructor
76 //=======================================================================
77 AIS_RubberBand::AIS_RubberBand (const Quantity_Color& theLineColor,
78 const Aspect_TypeOfLine theLineType,
79 const Quantity_Color theFillColor,
80 const Standard_Real theTransparency,
81 const Standard_Real theLineWidth)
83 myDrawer->SetLineAspect (new Prs3d_LineAspect (theLineColor, theLineType, theLineWidth));
84 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
85 myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_PLASTIC);
86 myDrawer->ShadingAspect()->SetColor (theFillColor);
87 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (Aspect_IS_SOLID);
88 myDrawer->ShadingAspect()->SetTransparency (theTransparency);
90 SetTransformPersistence (Graphic3d_TMF_2d, gp_Pnt(-1, -1, 0));
91 SetZLayer (Graphic3d_ZLayerId_TopOSD);
94 //=======================================================================
95 //function : Destructor
97 //=======================================================================
98 AIS_RubberBand::~AIS_RubberBand()
101 myTriangles.Nullify();
105 //=======================================================================
106 //function : SetRectangle
108 //=======================================================================
109 void AIS_RubberBand::SetRectangle (const Standard_Integer theMinX, const Standard_Integer theMinY,
110 const Standard_Integer theMaxX, const Standard_Integer theMaxY)
113 myPoints.Append (Graphic3d_Vec2i (theMinX, theMinY));
114 myPoints.Append (Graphic3d_Vec2i (theMinX, theMaxY));
115 myPoints.Append (Graphic3d_Vec2i (theMaxX, theMaxY));
116 myPoints.Append (Graphic3d_Vec2i (theMaxX, theMinY));
119 //=======================================================================
120 //function : AddPoint
122 //=======================================================================
123 void AIS_RubberBand::AddPoint (const Graphic3d_Vec2i& thePoint)
125 myPoints.Append (thePoint);
128 //=======================================================================
129 //function : AddPoint
131 //=======================================================================
132 void AIS_RubberBand::RemoveLastPoint()
134 myPoints.Remove (myPoints.Length());
137 //=======================================================================
138 //function : GetPoints
140 //=======================================================================
141 const NCollection_Sequence<Graphic3d_Vec2i>& AIS_RubberBand::Points() const
146 //=======================================================================
147 //function : LineColor
149 //=======================================================================
150 Quantity_Color AIS_RubberBand::LineColor() const
152 Quantity_Color aColor;
153 Standard_Real aWidth;
154 Aspect_TypeOfLine aTOL;
155 myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
159 //=======================================================================
160 //function : SetLineColor
162 //=======================================================================
163 void AIS_RubberBand::SetLineColor (const Quantity_Color& theColor)
165 myDrawer->LineAspect()->SetColor (theColor);
168 //=======================================================================
169 //function : FillColor
171 //=======================================================================
172 Quantity_Color AIS_RubberBand::FillColor() const
174 return myDrawer->ShadingAspect()->Color();
177 //=======================================================================
178 //function : SetFillColor
180 //=======================================================================
181 void AIS_RubberBand::SetFillColor (const Quantity_Color& theColor)
183 myDrawer->ShadingAspect()->SetColor (theColor);
186 //=======================================================================
187 //function : SetLineWidth
189 //=======================================================================
190 void AIS_RubberBand::SetLineWidth (const Standard_Real theWidth) const
192 myDrawer->LineAspect()->SetWidth (theWidth);
195 //=======================================================================
196 //function : SetLineWidth
198 //=======================================================================
199 Standard_Real AIS_RubberBand::LineWidth() const
201 Quantity_Color aColor;
202 Standard_Real aWidth;
203 Aspect_TypeOfLine aTOL;
204 myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
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 Quantity_Color aColor;
224 Standard_Real aWidth;
225 Aspect_TypeOfLine aTOL;
226 myDrawer->LineAspect()->Aspect()->Values (aColor, aTOL, aWidth);
230 //=======================================================================
231 //function : SetFillTransparency
233 //=======================================================================
234 void AIS_RubberBand::SetFillTransparency (const Standard_Real theValue) const
236 myDrawer->ShadingAspect()->SetTransparency (theValue);
239 //=======================================================================
240 //function : SetFillTransparency
242 //=======================================================================
243 Standard_Real AIS_RubberBand::FillTransparency() const
245 return myDrawer->ShadingAspect()->Transparency();
248 //=======================================================================
249 //function : SetFilling
251 //=======================================================================
252 void AIS_RubberBand::SetFilling (const Standard_Boolean theIsFilling)
254 myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle (theIsFilling ? Aspect_IS_SOLID : Aspect_IS_EMPTY);
257 //=======================================================================
258 //function : SetFilling
260 //=======================================================================
261 void AIS_RubberBand::SetFilling (const Quantity_Color theColor, const Standard_Real theTransparency)
263 SetFilling (Standard_True);
264 SetFillTransparency (theTransparency);
265 SetFillColor (theColor);
268 //=======================================================================
269 //function : IsFilling
271 //=======================================================================
272 Standard_Boolean AIS_RubberBand::IsFilling() const
274 Aspect_InteriorStyle aStyle;
275 Quantity_Color anIntColor, anEdgeColor;
276 Aspect_TypeOfLine aTOL;
277 Standard_Real aWidth;
278 myDrawer->ShadingAspect()->Aspect()->Values (aStyle, anIntColor, anEdgeColor, aTOL, aWidth);
279 return aStyle != Aspect_IS_EMPTY;
282 //=======================================================================
283 //function : fillTriangles
285 //=======================================================================
286 Standard_Boolean AIS_RubberBand::fillTriangles()
288 Handle(NCollection_IncAllocator) anAllocator = new NCollection_IncAllocator (MEMORY_BLOCK_SIZE);
289 Handle(BRepMesh_DataStructureOfDelaun) aMeshStructure = new BRepMesh_DataStructureOfDelaun(anAllocator);
290 Standard_Integer aPtsLower = myPoints.Lower();
291 Standard_Integer aPtsUpper = myPoints.Upper();
292 BRepMesh::Array1OfInteger anIndexes (0, myPoints.Length() - 1);
293 for (Standard_Integer aPtIdx = aPtsLower; aPtIdx <= aPtsUpper; ++aPtIdx)
295 gp_XY aP ((Standard_Real)myPoints.Value (aPtIdx).x(),
296 (Standard_Real)myPoints.Value (aPtIdx).y());
297 BRepMesh_Vertex aVertex (aP, aPtIdx, BRepMesh_Frontier);
298 anIndexes.ChangeValue (aPtIdx - aPtsLower) = aMeshStructure->AddNode (aVertex);
301 Standard_Real aPtSum = 0;
302 for (Standard_Integer aIdx = aPtsLower; aIdx <= aPtsUpper; ++aIdx)
304 Standard_Integer aNextIdx = (aIdx % myPoints.Length()) + 1;
305 aPtSum += (Standard_Real)(myPoints.Value (aNextIdx).x() - myPoints.Value (aIdx).x())
306 * (Standard_Real)(myPoints.Value (aNextIdx).y() + myPoints.Value (aIdx).y());
308 Standard_Boolean isClockwiseOrdered = aPtSum < 0;
310 for (Standard_Integer aIdx = 0; aIdx < anIndexes.Length(); ++aIdx)
312 Standard_Integer aPtIdx = isClockwiseOrdered ? aIdx : (aIdx + 1) % anIndexes.Length();
313 Standard_Integer aNextPtIdx = isClockwiseOrdered ? (aIdx + 1) % anIndexes.Length() : aIdx;
314 BRepMesh_Edge anEdge (anIndexes.Value (aPtIdx),
315 anIndexes.Value (aNextPtIdx),
317 aMeshStructure->AddLink (anEdge);
320 BRepMesh_Delaun aTriangulation (aMeshStructure, anIndexes);
321 const BRepMesh::MapOfInteger& aTriangles = aMeshStructure->ElementsOfDomain();
322 if (aTriangles.Extent() < 1)
323 return Standard_False;
326 Standard_Boolean toFill = Standard_False;
327 if (myTriangles.IsNull() || myTriangles->VertexNumber() != myPoints.Length() + 1)
329 toFill = Standard_True;
330 myTriangles = new Graphic3d_ArrayOfTriangles (aTriangles.Extent() * 3, 0, Standard_True);
333 Standard_Integer aVertexIndex = 1;
334 BRepMesh::MapOfInteger::Iterator aTriangleIt (aTriangles);
335 for (; aTriangleIt.More(); aTriangleIt.Next())
337 const Standard_Integer aTriangleId = aTriangleIt.Key();
338 const BRepMesh_Triangle& aCurrentTriangle = aMeshStructure->GetElement (aTriangleId);
340 if (aCurrentTriangle.Movability() == BRepMesh_Deleted)
343 Standard_Integer aTriangleVerts[3];
344 aMeshStructure->ElementNodes (aCurrentTriangle, aTriangleVerts);
347 for (Standard_Integer aVertIdx = 0; aVertIdx < 3; ++aVertIdx)
349 const BRepMesh_Vertex& aVertex = aMeshStructure->GetNode (aTriangleVerts[aVertIdx]);
350 aPts[aVertIdx] = aVertex.Coord();
355 gp_Dir aNorm = gp::DZ();
356 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
358 myTriangles->AddVertex (aPts[anIt].X(), aPts[anIt].Y(), 0.0,
359 aNorm.X(), aNorm.Y(), aNorm.Z());
364 for (Standard_Integer anIt = 0; anIt < 3; ++anIt)
366 myTriangles->SetVertice (aVertexIndex++, (Standard_ShortReal)aPts[anIt].X(), (Standard_ShortReal)aPts[anIt].Y(), 0.0f);
371 aMeshStructure.Nullify();
372 anAllocator.Nullify();
374 return Standard_True;
377 //=======================================================================
380 //=======================================================================
381 void AIS_RubberBand::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePresentationManager*/,
382 const Handle(Prs3d_Presentation)& thePresentation,
383 const Standard_Integer /*theMode*/)
385 Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePresentation);
388 if (IsFilling() && fillTriangles())
390 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
391 aGroup->AddPrimitiveArray (myTriangles);
395 if (myBorders.IsNull() || myBorders->VertexNumber() != myPoints.Length() + 1)
397 myBorders = new Graphic3d_ArrayOfPolylines (myPoints.Length() + 1);
398 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
400 myBorders->AddVertex ((Standard_Real)myPoints.Value (anIt).x(),
401 (Standard_Real)myPoints.Value (anIt).y(), 0.0);
404 myBorders->AddVertex ((Standard_Real)myPoints.Value(1).x(),
405 (Standard_Real)myPoints.Value(1).y(), 0.0);
410 for (Standard_Integer anIt = 1; anIt <= myPoints.Length(); anIt++)
412 myBorders->SetVertice (anIt, (Standard_ShortReal)myPoints.Value (anIt).x(),
413 (Standard_ShortReal)myPoints.Value (anIt).y(), 0.0f);
416 myBorders->SetVertice (myPoints.Length() + 1, (Standard_ShortReal)myPoints.Value(1).x(),
417 (Standard_ShortReal)myPoints.Value(1).y(), 0.0f);
420 aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
421 aGroup->AddPrimitiveArray (myBorders);