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