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