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