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