0032742: Coding - get rid of unused headers [Adaptor2d to Approx]
[occt.git] / src / AIS / AIS_ColoredShape.cxx
CommitLineData
ad3217cd 1// Created on: 2014-04-24
2// Created by: Kirill Gavrilov
3// Copyright (c) 2014 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_ColoredShape.hxx>
17
18#include <AIS_InteractiveContext.hxx>
19#include <BRep_Builder.hxx>
20#include <BRepTools.hxx>
6985e642 21#include <BRepMesh_IncrementalMesh.hxx>
ad3217cd 22#include <Graphic3d_AspectFillArea3d.hxx>
23#include <Graphic3d_AspectLine3d.hxx>
9c86076b 24#include <Graphic3d_ArrayOfTriangles.hxx>
25#include <Graphic3d_ArrayOfSegments.hxx>
ad3217cd 26#include <Graphic3d_Group.hxx>
27#include <Graphic3d_StructureManager.hxx>
ad3217cd 28#include <Precision.hxx>
ad3217cd 29#include <Prs3d_Presentation.hxx>
30#include <Prs3d_ShadingAspect.hxx>
decbff0d 31#include <PrsMgr_PresentationManager.hxx>
6985e642 32#include <StdSelect_BRepSelectionTool.hxx>
ad3217cd 33#include <StdPrs_ShadedShape.hxx>
5ad8c033 34#include <StdPrs_ToolTriangulatedShape.hxx>
35#include <StdPrs_WFShape.hxx>
ad3217cd 36#include <TopExp_Explorer.hxx>
ad3217cd 37#include <TopoDS_Compound.hxx>
38#include <TopoDS_Iterator.hxx>
39
92efcf78 40IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredShape,AIS_Shape)
41IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredDrawer,Prs3d_Drawer)
42
9c86076b 43namespace
44{
45 //! Collect all sub-compounds into map.
46 static void collectSubCompounds (TopTools_MapOfShape& theMap,
47 const TopoDS_Shape& theShape)
48 {
49 for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
50 {
51 const TopoDS_Shape& aShape = aChildIter.Value();
52 if (aShape.ShapeType() == TopAbs_COMPOUND
53 && theMap.Add (aShape))
54 {
478ad1d1 55 collectSubCompounds (theMap, aShape);
9c86076b 56 }
57 }
58 }
59}
60
ad3217cd 61//=======================================================================
62//function : AIS_ColoredShape
63//purpose :
64//=======================================================================
65AIS_ColoredShape::AIS_ColoredShape (const TopoDS_Shape& theShape)
66: AIS_Shape (theShape)
67{
68 // disable dedicated line aspects
69 myDrawer->SetFreeBoundaryAspect (myDrawer->LineAspect());
70 myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
71 myDrawer->SetSeenLineAspect (myDrawer->LineAspect());
7604a153 72 myDrawer->SetFaceBoundaryAspect (myDrawer->LineAspect());
ad3217cd 73}
74
75//=======================================================================
76//function : AIS_ColoredShape
77//purpose :
78//=======================================================================
79AIS_ColoredShape::AIS_ColoredShape (const Handle(AIS_Shape)& theShape)
80: AIS_Shape (theShape->Shape())
81{
82 // disable dedicated line aspects
83 myDrawer->SetFreeBoundaryAspect (myDrawer->LineAspect());
84 myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
85 myDrawer->SetSeenLineAspect (myDrawer->LineAspect());
7604a153 86 myDrawer->SetFaceBoundaryAspect (myDrawer->LineAspect());
ad3217cd 87 if (theShape->HasMaterial())
88 {
89 SetMaterial (theShape->Material());
90 }
91 if (theShape->HasColor())
92 {
87432b82 93 Quantity_Color aColor;
94 theShape->Color (aColor);
95 SetColor (aColor);
ad3217cd 96 }
97 if (theShape->HasWidth())
98 {
99 SetWidth (theShape->Width());
100 }
101 if (theShape->IsTransparent())
102 {
103 SetTransparency (theShape->Transparency());
104 }
105}
106
107//=======================================================================
108//function : CustomAspects
109//purpose :
110//=======================================================================
111Handle(AIS_ColoredDrawer) AIS_ColoredShape::CustomAspects (const TopoDS_Shape& theShape)
112{
113 Handle(AIS_ColoredDrawer) aDrawer;
114 myShapeColors.Find (theShape, aDrawer);
115 if (aDrawer.IsNull())
116 {
117 aDrawer = new AIS_ColoredDrawer (myDrawer);
118 myShapeColors.Bind (theShape, aDrawer);
226fce20 119 SetToUpdate();
ad3217cd 120 }
121 return aDrawer;
122}
123
124//=======================================================================
125//function : ClearCustomAspects
126//purpose :
127//=======================================================================
128void AIS_ColoredShape::ClearCustomAspects()
129{
130 if (myShapeColors.IsEmpty())
131 {
132 return;
133 }
134 myShapeColors.Clear();
226fce20 135 SetToUpdate();
ad3217cd 136}
137
138//=======================================================================
139//function : UnsetCustomAspects
140//purpose :
141//=======================================================================
142void AIS_ColoredShape::UnsetCustomAspects (const TopoDS_Shape& theShape,
143 const Standard_Boolean theToUnregister)
144{
145 if (!myShapeColors.IsBound (theShape))
146 {
147 return;
148 }
149
226fce20 150 SetToUpdate();
ad3217cd 151 if (theToUnregister)
152 {
153 myShapeColors.UnBind (theShape);
154 return;
155 }
156
157 myShapeColors.ChangeFind (theShape) = new AIS_ColoredDrawer (myDrawer);
158}
159
160//=======================================================================
161//function : SetCustomColor
162//purpose :
163//=======================================================================
164void AIS_ColoredShape::SetCustomColor (const TopoDS_Shape& theShape,
165 const Quantity_Color& theColor)
166{
167 if (theShape.IsNull())
168 {
169 return;
170 }
171
172 const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
173 setColor (aDrawer, theColor);
174 aDrawer->SetOwnColor (theColor);
ad3217cd 175}
176
c1197a15 177//=======================================================================
178//function : SetCustomTransparency
179//purpose :
180//=======================================================================
181void AIS_ColoredShape::SetCustomTransparency (const TopoDS_Shape& theShape,
182 Standard_Real theTransparency)
183{
184 if (theShape.IsNull())
185 {
186 return;
187 }
188
189 const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
190 setTransparency (aDrawer, theTransparency);
191 aDrawer->SetOwnTransparency (theTransparency);
c1197a15 192}
193
ad3217cd 194//=======================================================================
195//function : SetCustomWidth
196//purpose :
197//=======================================================================
198void AIS_ColoredShape::SetCustomWidth (const TopoDS_Shape& theShape,
199 const Standard_Real theLineWidth)
200{
201 if (theShape.IsNull())
202 {
203 return;
204 }
205
206 const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
c1197a15 207 setWidth (aDrawer, theLineWidth);
ad3217cd 208 aDrawer->SetOwnWidth (theLineWidth);
ad3217cd 209}
210
211//=======================================================================
212//function : SetColor
213//purpose :
214//=======================================================================
215
216void AIS_ColoredShape::SetColor (const Quantity_Color& theColor)
217{
fb66bb28 218 for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
ad3217cd 219 {
220 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
221 if (aDrawer->HasOwnColor())
222 {
223 continue;
224 }
225
6262338c 226 if (aDrawer->HasOwnShadingAspect())
ad3217cd 227 {
228 aDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
229 }
6262338c 230 if (aDrawer->HasOwnLineAspect())
ad3217cd 231 {
232 aDrawer->LineAspect()->SetColor (theColor);
233 }
6262338c 234 if (aDrawer->HasOwnWireAspect())
ad3217cd 235 {
236 aDrawer->WireAspect()->SetColor (theColor);
237 }
7604a153 238 if (aDrawer->HasOwnFaceBoundaryAspect())
239 {
240 aDrawer->FaceBoundaryAspect()->SetColor (theColor);
241 }
ad3217cd 242 }
bf5f0ca2 243 AIS_Shape::SetColor (theColor);
ad3217cd 244}
245
246//=======================================================================
247//function : SetWidth
248//purpose :
249//=======================================================================
250
251void AIS_ColoredShape::SetWidth (const Standard_Real theLineWidth)
252{
fb66bb28 253 for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
ad3217cd 254 {
255 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
256 if (aDrawer->HasOwnWidth())
257 {
258 continue;
259 }
260
6262338c 261 if (aDrawer->HasOwnLineAspect())
ad3217cd 262 {
263 aDrawer->LineAspect()->SetWidth (theLineWidth);
264 }
6262338c 265 if (aDrawer->HasOwnWireAspect())
ad3217cd 266 {
267 aDrawer->WireAspect()->SetWidth (theLineWidth);
268 }
7604a153 269 if (aDrawer->HasOwnFaceBoundaryAspect())
270 {
271 aDrawer->FaceBoundaryAspect()->SetWidth (theLineWidth);
272 }
ad3217cd 273 }
bf5f0ca2 274 AIS_Shape::SetWidth (theLineWidth);
275}
276
277//=======================================================================
278//function : UnsetWidth
279//purpose :
280//=======================================================================
281void AIS_ColoredShape::UnsetWidth()
282{
283 SetWidth (1.0f);
ad3217cd 284}
285
286//=======================================================================
287//function : SetTransparency
288//purpose :
289//=======================================================================
290
291void AIS_ColoredShape::SetTransparency (const Standard_Real theValue)
292{
fb66bb28 293 for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
ad3217cd 294 {
c1197a15 295 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
296 if (aDrawer->HasOwnTransparency())
297 {
298 continue;
299 }
300
6262338c 301 if (aDrawer->HasOwnShadingAspect())
ad3217cd 302 {
303 aDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
304 }
305 }
bf5f0ca2 306 AIS_Shape::SetTransparency (theValue);
ad3217cd 307}
308
f6d4c5cf 309//=======================================================================
310//function : UnsetTransparency
311//purpose :
312//=======================================================================
313void AIS_ColoredShape::UnsetTransparency()
314{
bf5f0ca2 315 SetTransparency (0.0f);
f6d4c5cf 316}
317
e0608a8d 318//=======================================================================
319//function : SetMaterial
320//purpose :
321//=======================================================================
322
323void AIS_ColoredShape::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
324{
fb66bb28 325 for (AIS_DataMapOfShapeDrawer::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
e0608a8d 326 {
327 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
a4815d55 328 if (aDrawer->HasOwnMaterial())
329 {
330 continue;
331 }
332
6262338c 333 if (aDrawer->HasOwnShadingAspect())
e0608a8d 334 {
c1197a15 335 setMaterial (aDrawer, theMaterial, aDrawer->HasOwnColor(), aDrawer->HasOwnTransparency());
e0608a8d 336 }
337 }
bf5f0ca2 338 AIS_Shape::SetMaterial (theMaterial);
e0608a8d 339}
340
ad3217cd 341//=======================================================================
342//function : Compute
343//purpose :
344//=======================================================================
decbff0d 345void AIS_ColoredShape::Compute (const Handle(PrsMgr_PresentationManager)& thePrsMgr,
346 const Handle(Prs3d_Presentation)& thePrs,
347 const Standard_Integer theMode)
ad3217cd 348{
ede89abc 349 if (myshape.IsNull())
350 {
351 return;
352 }
353
ad3217cd 354 if (IsInfinite())
355 {
356 thePrs->SetInfiniteState (Standard_True);
357 }
358
f8e0c6c4 359 switch (theMode)
ad3217cd 360 {
f8e0c6c4 361 case AIS_WireFrame:
ad3217cd 362 {
83b0f13a 363 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
364
306f5893 365 // After this call if type of deflection is relative
366 // computed deflection coefficient is stored as absolute.
7f24b768 367 StdPrs_ToolTriangulatedShape::GetDeflection (myshape, myDrawer);
f8e0c6c4 368 break;
369 }
370 case AIS_Shaded:
371 {
372 if (myDrawer->IsAutoTriangulation())
5ad8c033 373 {
f8e0c6c4 374 // compute mesh for entire shape beforehand to ensure consistency and optimizations (parallelization)
375 StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (myshape, myDrawer, Standard_True);
376
377 // After this call if type of deflection is relative
378 // computed deflection coefficient is stored as absolute.
379 Standard_Boolean wasRecomputed = StdPrs_ToolTriangulatedShape::Tessellate (myshape, myDrawer);
380
381 // Set to update wireframe presentation on triangulation.
382 if (myDrawer->IsoOnTriangulation() && wasRecomputed)
383 {
384 SetToUpdate (AIS_WireFrame);
385 }
5ad8c033 386 }
f8e0c6c4 387 break;
388 }
389 case 2:
390 {
391 AIS_Shape::Compute (thePrsMgr, thePrs, theMode);
392 return;
393 }
394 default:
395 {
396 return;
ad3217cd 397 }
ad3217cd 398 }
399
6985e642 400 // Extract myShapeColors map (KeyshapeColored -> Color) to subshapes map (Subshape -> Color).
401 // This needed when colored shape is not part of BaseShape (but subshapes are) and actually container for subshapes.
9c86076b 402 AIS_DataMapOfShapeDrawer aSubshapeDrawerMap;
6985e642 403 fillSubshapeDrawerMap (aSubshapeDrawerMap);
404
405 Handle(AIS_ColoredDrawer) aBaseDrawer;
406 myShapeColors.Find (myshape, aBaseDrawer);
407
408 // myShapeColors + anOpened --> array[TopAbs_ShapeEnum] of map of color-to-compound
409 DataMapOfDrawerCompd aDispatchedOpened[(size_t)TopAbs_SHAPE];
410 DataMapOfDrawerCompd aDispatchedClosed;
411 dispatchColors (aBaseDrawer, myshape,
412 aSubshapeDrawerMap, TopAbs_COMPOUND, Standard_False,
413 aDispatchedOpened, theMode == AIS_Shaded ? aDispatchedClosed : aDispatchedOpened[TopAbs_FACE]);
414 addShapesWithCustomProps (thePrs, aDispatchedOpened, aDispatchedClosed, theMode);
415}
416
417//=======================================================================
418//function : fillSubshapeDrawerMap
419//purpose :
420//=======================================================================
421void AIS_ColoredShape::fillSubshapeDrawerMap (AIS_DataMapOfShapeDrawer& theSubshapeDrawerMap) const
422{
423 // unroll compounds specified for grouping sub-shapes with the same style
424 // (e.g. the compounds that are not a part of the main shape)
425 TopTools_MapOfShape aMapOfOwnCompounds;
426 if (myshape.ShapeType() == TopAbs_COMPOUND)
5bffb882 427 {
6985e642 428 aMapOfOwnCompounds.Add (myshape);
429 collectSubCompounds (aMapOfOwnCompounds, myshape);
430 }
431 for (AIS_DataMapOfShapeDrawer::Iterator aKeyShapeIter (myShapeColors);
432 aKeyShapeIter.More(); aKeyShapeIter.Next())
433 {
434 const TopoDS_Shape& aKeyShape = aKeyShapeIter.Key();
435 if (aKeyShape.ShapeType() != TopAbs_COMPOUND
436 || aMapOfOwnCompounds.Contains (aKeyShape))
5bffb882 437 {
6985e642 438 continue;
5bffb882 439 }
9c86076b 440
6985e642 441 for (TopoDS_Iterator aChildIter (aKeyShape); aChildIter.More(); aChildIter.Next())
442 {
443 const TopoDS_Shape& aShape = aChildIter.Value();
444 if (!myShapeColors.IsBound (aShape))
9c86076b 445 {
6985e642 446 bindSubShapes (theSubshapeDrawerMap, aShape, aKeyShapeIter.Value());
5bffb882 447 }
448 }
6985e642 449 }
9c86076b 450
6985e642 451 // assign other sub-shapes with styles
452 for (AIS_DataMapOfShapeDrawer::Iterator aKeyShapeIter (myShapeColors);
453 aKeyShapeIter.More(); aKeyShapeIter.Next())
454 {
455 const TopoDS_Shape& aKeyShape = aKeyShapeIter.Key();
456 if (myshape == aKeyShape
457 || (aKeyShape.ShapeType() == TopAbs_COMPOUND
458 && !aMapOfOwnCompounds.Contains (aKeyShape)))
9c86076b 459 {
6985e642 460 continue;
9c86076b 461 }
6985e642 462
463 bindSubShapes (theSubshapeDrawerMap, aKeyShape, aKeyShapeIter.Value());
464 }
465}
466
467//=======================================================================
468//function : ComputeSelection
469//purpose :
470//=======================================================================
471void AIS_ColoredShape::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
472 const Standard_Integer theMode)
473{
474 if (myshape.IsNull())
475 {
476 return;
477 }
478 else if (isShapeEntirelyVisible())
479 {
480 base_type::ComputeSelection (theSelection, theMode);
481 return;
482 }
483
484 const TopAbs_ShapeEnum aTypOfSel = AIS_Shape::SelectionType (theMode);
7f24b768 485 const Standard_Real aDeflection = StdPrs_ToolTriangulatedShape::GetDeflection (myshape, myDrawer);
67441d0c 486 const Standard_Real aDeviationAngle = myDrawer->DeviationAngle();
6985e642 487 const Standard_Integer aPriority = StdSelect_BRepSelectionTool::GetStandardPriority (myshape, aTypOfSel);
488 if (myDrawer->IsAutoTriangulation()
489 && !BRepTools::Triangulation (myshape, Precision::Infinite()))
490 {
491 BRepMesh_IncrementalMesh aMesher (myshape, aDeflection, Standard_False, aDeviationAngle);
492 }
493
494 AIS_DataMapOfShapeDrawer aSubshapeDrawerMap;
495 fillSubshapeDrawerMap (aSubshapeDrawerMap);
496
497 Handle(StdSelect_BRepOwner) aBrepOwner = new StdSelect_BRepOwner (myshape, aPriority);
498 if (aTypOfSel == TopAbs_SHAPE)
499 {
500 aBrepOwner = new StdSelect_BRepOwner (myshape, aPriority);
5bffb882 501 }
9c86076b 502
503 Handle(AIS_ColoredDrawer) aBaseDrawer;
504 myShapeColors.Find (myshape, aBaseDrawer);
6985e642 505 computeSubshapeSelection (aBaseDrawer, aSubshapeDrawerMap, myshape, aBrepOwner, theSelection,
506 aTypOfSel, aPriority, aDeflection, aDeviationAngle);
9c86076b 507
6985e642 508 Handle(SelectMgr_SelectableObject) aThis (this);
b5cce1ab 509 for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator aSelEntIter (theSelection->Entities()); aSelEntIter.More(); aSelEntIter.Next())
6985e642 510 {
0ef04197 511 const Handle(SelectMgr_EntityOwner)& anOwner = aSelEntIter.Value()->BaseSensitive()->OwnerId();
512 anOwner->SetSelectable (aThis);
6985e642 513 }
6985e642 514}
515
516//=======================================================================
517//function : computeSubshapeSelection
518//purpose :
519//=======================================================================
520void AIS_ColoredShape::computeSubshapeSelection (const Handle(AIS_ColoredDrawer)& theParentDrawer,
521 const AIS_DataMapOfShapeDrawer& theShapeDrawerMap,
522 const TopoDS_Shape& theShape,
523 const Handle(StdSelect_BRepOwner)& theOwner,
524 const Handle(SelectMgr_Selection)& theSelection,
525 const TopAbs_ShapeEnum theTypOfSel,
526 const Standard_Integer thePriority,
527 const Standard_Real theDeflection,
528 const Standard_Real theDeflAngle)
529{
530 Handle(AIS_ColoredDrawer) aDrawer = theParentDrawer;
531 theShapeDrawerMap.Find (theShape, aDrawer);
532 if (!aDrawer.IsNull()
533 && aDrawer->IsHidden())
534 {
535 return;
536 }
537
538 const Standard_Integer aNbPOnEdge = 9;
539 const Standard_Real aMaximalParameter = 500.0;
540 if (theTypOfSel == TopAbs_SHAPE
541 && theShape.ShapeType() >= TopAbs_FACE)
542 {
543 StdSelect_BRepSelectionTool::ComputeSensitive (theShape, theOwner, theSelection,
544 theDeflection, theDeflAngle, aNbPOnEdge, aMaximalParameter, myDrawer->IsAutoTriangulation());
545 return;
546 }
547 else if (theShape.ShapeType() == theTypOfSel)
548 {
549 const Standard_Boolean isComesFromDecomposition = !theShape.IsEqual (myshape);
550 Handle(StdSelect_BRepOwner) aBrepOwner = new StdSelect_BRepOwner (theShape, thePriority, isComesFromDecomposition);
551 StdSelect_BRepSelectionTool::ComputeSensitive (theShape, aBrepOwner, theSelection,
552 theDeflection, theDeflAngle, aNbPOnEdge, aMaximalParameter, myDrawer->IsAutoTriangulation());
553 return;
554 }
555
556 for (TopoDS_Iterator aSubShapeIter (theShape); aSubShapeIter.More(); aSubShapeIter.Next())
557 {
558 const TopoDS_Shape& aSubShape = aSubShapeIter.Value();
559 computeSubshapeSelection (aDrawer, theShapeDrawerMap, aSubShape,
560 theOwner, theSelection, theTypOfSel, thePriority,
561 theDeflection, theDeflAngle);
562 }
5bffb882 563}
ad3217cd 564
5bffb882 565//=======================================================================
566//function : addShapesWithCustomProps
567//purpose :
568//=======================================================================
569void AIS_ColoredShape::addShapesWithCustomProps (const Handle(Prs3d_Presentation)& thePrs,
9c86076b 570 const DataMapOfDrawerCompd* theDrawerOpenedShapePerType,
571 const DataMapOfDrawerCompd& theDrawerClosedFaces,
572 const Standard_Integer theMode)
5bffb882 573{
0493ffd0 574 Handle(Graphic3d_Group) anOpenGroup, aClosedGroup, anEdgesGroup;
9c86076b 575 for (size_t aShType = 0; aShType <= (size_t )TopAbs_SHAPE; ++aShType)
576 {
577 const Standard_Boolean isClosed = aShType == TopAbs_SHAPE;
578 Handle(Graphic3d_Group)& aShadedGroup = isClosed ? aClosedGroup : anOpenGroup;
579 const DataMapOfDrawerCompd& aDrawerShapeMap = isClosed
580 ? theDrawerClosedFaces
581 : theDrawerOpenedShapePerType[aShType];
582 for (DataMapOfDrawerCompd::Iterator aMapIter (aDrawerShapeMap);
ad3217cd 583 aMapIter.More(); aMapIter.Next())
584 {
9c86076b 585 const Handle(AIS_ColoredDrawer)& aCustomDrawer = aMapIter.Key();
ad3217cd 586 const TopoDS_Compound& aShapeDraw = aMapIter.Value(); // compound of subshapes with <aShType> type
6262338c 587 Handle(Prs3d_Drawer) aDrawer;
9c86076b 588 if (!aCustomDrawer.IsNull())
86766b0e 589 {
590 aDrawer = aCustomDrawer;
591 if (aCustomDrawer->IsHidden())
592 {
593 continue;
594 }
595 }
596 else
597 {
598 aDrawer = myDrawer;
599 }
ad3217cd 600
7f917335 601 // It is supposed that absolute deflection contains previously computed relative deflection
602 // (if deflection type is relative).
603 // In case of CustomDrawer it is taken from Link().
604 Aspect_TypeOfDeflection aPrevType = aDrawer->TypeOfDeflection();
605 aDrawer->SetTypeOfDeflection (Aspect_TOD_ABSOLUTE);
606
ad3217cd 607 // Draw each kind of subshapes and personal-colored shapes in a separate group
608 // since it's necessary to set transparency/material for all subshapes
609 // without affecting their unique colors
5bffb882 610 if (theMode == AIS_Shaded
611 && aShapeDraw.ShapeType() <= TopAbs_FACE
612 && !IsInfinite())
ad3217cd 613 {
9c86076b 614 // add wireframe presentation for isolated edges and vertices
615 StdPrs_ShadedShape::AddWireframeForFreeElements (thePrs, aShapeDraw, aDrawer);
616
617 // add special wireframe presentation for faces without triangulation
618 StdPrs_ShadedShape::AddWireframeForFacesWithoutTriangles (thePrs, aShapeDraw, aDrawer);
619
a6dee93d 620 Handle(Graphic3d_ArrayOfTriangles) aTriangles = StdPrs_ShadedShape::FillTriangles (aShapeDraw,
621 aDrawer->ShadingAspect()->Aspect()->ToMapTexture()
622 && !aDrawer->ShadingAspect()->Aspect()->TextureMap().IsNull(),
623 myUVOrigin, myUVRepeat, myUVScale);
9c86076b 624 if (!aTriangles.IsNull())
625 {
626 if (aShadedGroup.IsNull())
627 {
0493ffd0 628 aShadedGroup = thePrs->NewGroup();
9c86076b 629 aShadedGroup->SetClosed (isClosed);
630 }
631 aShadedGroup->SetPrimitivesAspect (aDrawer->ShadingAspect()->Aspect());
632 aShadedGroup->AddPrimitiveArray (aTriangles);
633 }
634
635 if (aDrawer->FaceBoundaryDraw())
636 {
0493ffd0 637 if (Handle(Graphic3d_ArrayOfSegments) aBndSegments = StdPrs_ShadedShape::FillFaceBoundaries (aShapeDraw, aDrawer->FaceBoundaryUpperContinuity()))
9c86076b 638 {
0493ffd0 639 if (anEdgesGroup.IsNull())
9c86076b 640 {
0493ffd0 641 anEdgesGroup = thePrs->NewGroup();
9c86076b 642 }
643
0493ffd0 644 anEdgesGroup->SetPrimitivesAspect (aDrawer->FaceBoundaryAspect()->Aspect());
645 anEdgesGroup->AddPrimitiveArray (aBndSegments);
9c86076b 646 }
647 }
5bffb882 648 }
649 else
650 {
5ad8c033 651 StdPrs_WFShape::Add (thePrs, aShapeDraw, aDrawer);
ad3217cd 652 }
7f917335 653 aDrawer->SetTypeOfDeflection (aPrevType);
ad3217cd 654 }
655 }
656}
657
658//=======================================================================
659//function : dispatchColors
660//purpose :
661//=======================================================================
9c86076b 662Standard_Boolean AIS_ColoredShape::dispatchColors (const Handle(AIS_ColoredDrawer)& theParentDrawer,
663 const TopoDS_Shape& theShapeToParse,
664 const AIS_DataMapOfShapeDrawer& theShapeDrawerMap,
665 const TopAbs_ShapeEnum theParentType,
666 const Standard_Boolean theIsParentClosed,
667 DataMapOfDrawerCompd* theDrawerOpenedShapePerType,
668 DataMapOfDrawerCompd& theDrawerClosedFaces)
ad3217cd 669{
9c86076b 670 const TopAbs_ShapeEnum aShapeType = theShapeToParse.ShapeType();
671 if (aShapeType == TopAbs_SHAPE)
ad3217cd 672 {
673 return Standard_False;
674 }
675
676 // check own setting of current shape
9c86076b 677 Handle(AIS_ColoredDrawer) aDrawer = theParentDrawer;
678 const Standard_Boolean isOverriden = theShapeDrawerMap.Find (theShapeToParse, aDrawer);
679 if (isOverriden
680 && aDrawer->IsHidden())
681 {
682 return Standard_True;
683 }
684
685 // handle compounds, solids and shells
686 Standard_Boolean isSubOverride = Standard_False;
687 if (aShapeType <= TopAbs_SHELL)
688 {
689 // detect parts of closed solids
690 Standard_Boolean isClosedShell = theParentType == TopAbs_SOLID
691 && aShapeType == TopAbs_SHELL
692 && BRep_Tool::IsClosed (theShapeToParse)
693 && StdPrs_ToolTriangulatedShape::IsTriangulated (theShapeToParse);
694 if (isClosedShell)
695 {
696 for (TopoDS_Iterator aFaceIter (theShapeToParse); aFaceIter.More(); aFaceIter.Next())
697 {
698 const TopoDS_Shape& aFace = aFaceIter.Value();
699 Handle(AIS_ColoredDrawer) aFaceDrawer;
c1197a15 700 if (aFace.ShapeType() != TopAbs_FACE
701 || !theShapeDrawerMap.Find (aFace, aFaceDrawer))
702 {
703 continue;
704 }
705
706 if (aFaceDrawer->IsHidden())
9c86076b 707 {
708 isClosedShell = Standard_False;
709 break;
710 }
c1197a15 711 else if (aFaceDrawer->HasOwnShadingAspect()
712 && aFaceDrawer->ShadingAspect()->Aspect()->AlphaMode() != Graphic3d_AlphaMode_Opaque)
713 {
714 if (aFaceDrawer->ShadingAspect()->Aspect()->AlphaMode() != Graphic3d_AlphaMode_BlendAuto
715 || aFaceDrawer->ShadingAspect()->Aspect()->FrontMaterial().Alpha() < 1.0f
716 || (aFaceDrawer->ShadingAspect()->Aspect()->Distinguish()
717 && aFaceDrawer->ShadingAspect()->Aspect()->BackMaterial().Alpha() < 1.0f))
718 {
719 isClosedShell = Standard_False;
720 break;
721 }
722 }
9c86076b 723 }
724 }
725
726 for (TopoDS_Iterator aSubShapeIter (theShapeToParse); aSubShapeIter.More(); aSubShapeIter.Next())
727 {
728 const TopoDS_Shape& aSubShape = aSubShapeIter.Value();
729 if (dispatchColors (aDrawer, aSubShape,
730 theShapeDrawerMap, aShapeType,
731 isClosedShell,
732 theDrawerOpenedShapePerType,
733 theDrawerClosedFaces))
734 {
735 isSubOverride = Standard_True;
736 }
737 }
738 return isOverriden || isSubOverride;
739 }
ad3217cd 740
741 // iterate on sub-shapes
742 BRep_Builder aBBuilder;
9c86076b 743 TopoDS_Shape aShapeCopy = theShapeToParse.EmptyCopied();
744 aShapeCopy.Closed (theShapeToParse.Closed());
ad3217cd 745 Standard_Integer nbDef = 0;
9c86076b 746 for (TopoDS_Iterator aSubShapeIter (theShapeToParse); aSubShapeIter.More(); aSubShapeIter.Next())
747 {
748 const TopoDS_Shape& aSubShape = aSubShapeIter.Value();
749 if (dispatchColors (aDrawer, aSubShape,
750 theShapeDrawerMap, aShapeType,
751 theIsParentClosed,
752 theDrawerOpenedShapePerType,
753 theDrawerClosedFaces))
ad3217cd 754 {
755 isSubOverride = Standard_True;
756 }
757 else
758 {
9c86076b 759 aBBuilder.Add (aShapeCopy, aSubShape);
ad3217cd 760 ++nbDef;
761 }
762 }
9c86076b 763 if (aShapeType == TopAbs_FACE || !isSubOverride)
ad3217cd 764 {
9c86076b 765 aShapeCopy = theShapeToParse;
ad3217cd 766 }
767 else if (nbDef == 0)
768 {
769 return isOverriden || isSubOverride; // empty compound
770 }
771
772 // if any of styles is overridden regarding to default one, add rest to map
773 if (isOverriden
774 || (isSubOverride && theParentType != TopAbs_WIRE // avoid drawing edges when vertex color is overridden
775 && theParentType != TopAbs_FACE) // avoid drawing edges of the same color as face
9c86076b 776 || (theParentType <= TopAbs_SHELL && !(isOverriden || isSubOverride))) // bind original shape to default color
ad3217cd 777 {
778 TopoDS_Compound aCompound;
9c86076b 779 DataMapOfDrawerCompd& aDrawerShapeMap = theIsParentClosed
780 && aShapeType == TopAbs_FACE
781 ? theDrawerClosedFaces
782 : theDrawerOpenedShapePerType[(size_t)aShapeType];
783 if (!aDrawerShapeMap.FindFromKey (aDrawer, aCompound))
ad3217cd 784 {
785 aBBuilder.MakeCompound (aCompound);
9c86076b 786 aDrawerShapeMap.Add (aDrawer, aCompound);
ad3217cd 787 }
788 aBBuilder.Add (aCompound, aShapeCopy);
789 }
790 return isOverriden || isSubOverride;
791}
792
5bffb882 793//=======================================================================
794//function : isShapeEntirelyVisible
795//purpose :
796//=======================================================================
797Standard_Boolean AIS_ColoredShape::isShapeEntirelyVisible() const
798{
fb66bb28 799 for (AIS_DataMapOfShapeDrawer::Iterator aMapIter (myShapeColors); aMapIter.More(); aMapIter.Next())
5bffb882 800 {
801 if (aMapIter.Value()->IsHidden())
802 {
803 return Standard_False;
804 }
805 }
806 return Standard_True;
807}
808
809//=======================================================================
810//function : bindSubShapes
811//purpose :
812//=======================================================================
9c86076b 813void AIS_ColoredShape::bindSubShapes (AIS_DataMapOfShapeDrawer& theShapeDrawerMap,
814 const TopoDS_Shape& theKeyShape,
6985e642 815 const Handle(AIS_ColoredDrawer)& theDrawer) const
5bffb882 816{
9c86076b 817 TopAbs_ShapeEnum aShapeWithColorType = theKeyShape.ShapeType();
5bffb882 818 if (aShapeWithColorType == TopAbs_COMPOUND)
819 {
9c86076b 820 theShapeDrawerMap.Bind (theKeyShape, theDrawer);
ad3217cd 821 }
5bffb882 822 else if (aShapeWithColorType == TopAbs_SOLID || aShapeWithColorType == TopAbs_SHELL)
823 {
9c86076b 824 for (TopExp_Explorer anExp (theKeyShape, TopAbs_FACE); anExp.More(); anExp.Next())
5bffb882 825 {
9c86076b 826 if (!theShapeDrawerMap.IsBound (anExp.Current()))
5bffb882 827 {
9c86076b 828 theShapeDrawerMap.Bind (anExp.Current(), theDrawer);
5bffb882 829 }
830 }
831 }
832 else if (aShapeWithColorType == TopAbs_WIRE)
833 {
9c86076b 834 for (TopExp_Explorer anExp (theKeyShape, TopAbs_EDGE); anExp.More(); anExp.Next())
5bffb882 835 {
9c86076b 836 if (!theShapeDrawerMap.IsBound (anExp.Current()))
5bffb882 837 {
9c86076b 838 theShapeDrawerMap.Bind (anExp.Current(), theDrawer);
5bffb882 839 }
840 }
841 }
842 else
843 {
844 // bind single face, edge and vertex
845 // force rebind if required due to the color of single shape has
846 // higher priority than the color of "compound" shape (wire is a
847 // compound of edges, shell is a compound of faces) that contains
848 // this single shape.
9c86076b 849 theShapeDrawerMap.Bind (theKeyShape, theDrawer);
5bffb882 850 }
ad3217cd 851}