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