0025484: Visualization - group sub-shapes with the same style in XCAFPrs_AISObject...
[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>
21#include <gp_Pnt2d.hxx>
22#include <Graphic3d_AspectFillArea3d.hxx>
23#include <Graphic3d_AspectLine3d.hxx>
24#include <Graphic3d_Group.hxx>
25#include <Graphic3d_StructureManager.hxx>
26#include <Graphic3d_Texture2Dmanual.hxx>
27#include <Precision.hxx>
28#include <Prs3d_LineAspect.hxx>
29#include <Prs3d_IsoAspect.hxx>
30#include <Prs3d_Presentation.hxx>
31#include <Prs3d_ShadingAspect.hxx>
32#include <Prs3d_Root.hxx>
33#include <PrsMgr_PresentationManager3d.hxx>
34#include <Standard_ErrorHandler.hxx>
35#include <StdPrs_ShadedShape.hxx>
36#include <StdPrs_ToolShadedShape.hxx>
37#include <StdPrs_WFDeflectionShape.hxx>
38#include <StdPrs_WFShape.hxx>
39#include <TopExp_Explorer.hxx>
40#include <TopoDS.hxx>
41#include <TopoDS_Compound.hxx>
42#include <TopoDS_Iterator.hxx>
43
44IMPLEMENT_STANDARD_HANDLE (AIS_ColoredDrawer, AIS_Drawer)
45IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredDrawer, AIS_Drawer)
46
47IMPLEMENT_STANDARD_HANDLE (AIS_ColoredShape, AIS_Shape)
48IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredShape, AIS_Shape)
49
50//=======================================================================
51//function : AIS_ColoredShape
52//purpose :
53//=======================================================================
54AIS_ColoredShape::AIS_ColoredShape (const TopoDS_Shape& theShape)
55: AIS_Shape (theShape)
56{
57 // disable dedicated line aspects
58 myDrawer->SetFreeBoundaryAspect (myDrawer->LineAspect());
59 myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
60 myDrawer->SetSeenLineAspect (myDrawer->LineAspect());
61}
62
63//=======================================================================
64//function : AIS_ColoredShape
65//purpose :
66//=======================================================================
67AIS_ColoredShape::AIS_ColoredShape (const Handle(AIS_Shape)& theShape)
68: AIS_Shape (theShape->Shape())
69{
70 // disable dedicated line aspects
71 myDrawer->SetFreeBoundaryAspect (myDrawer->LineAspect());
72 myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
73 myDrawer->SetSeenLineAspect (myDrawer->LineAspect());
74 if (theShape->HasMaterial())
75 {
76 SetMaterial (theShape->Material());
77 }
78 if (theShape->HasColor())
79 {
80 SetColor (theShape->Color());
81 }
82 if (theShape->HasWidth())
83 {
84 SetWidth (theShape->Width());
85 }
86 if (theShape->IsTransparent())
87 {
88 SetTransparency (theShape->Transparency());
89 }
90}
91
92//=======================================================================
93//function : CustomAspects
94//purpose :
95//=======================================================================
96Handle(AIS_ColoredDrawer) AIS_ColoredShape::CustomAspects (const TopoDS_Shape& theShape)
97{
98 Handle(AIS_ColoredDrawer) aDrawer;
99 myShapeColors.Find (theShape, aDrawer);
100 if (aDrawer.IsNull())
101 {
102 aDrawer = new AIS_ColoredDrawer (myDrawer);
103 myShapeColors.Bind (theShape, aDrawer);
104 LoadRecomputable (AIS_WireFrame);
105 LoadRecomputable (AIS_Shaded);
106 }
107 return aDrawer;
108}
109
110//=======================================================================
111//function : ClearCustomAspects
112//purpose :
113//=======================================================================
114void AIS_ColoredShape::ClearCustomAspects()
115{
116 if (myShapeColors.IsEmpty())
117 {
118 return;
119 }
120 myShapeColors.Clear();
121 LoadRecomputable (AIS_WireFrame);
122 LoadRecomputable (AIS_Shaded);
123}
124
125//=======================================================================
126//function : UnsetCustomAspects
127//purpose :
128//=======================================================================
129void AIS_ColoredShape::UnsetCustomAspects (const TopoDS_Shape& theShape,
130 const Standard_Boolean theToUnregister)
131{
132 if (!myShapeColors.IsBound (theShape))
133 {
134 return;
135 }
136
137 LoadRecomputable (AIS_WireFrame);
138 LoadRecomputable (AIS_Shaded);
139 if (theToUnregister)
140 {
141 myShapeColors.UnBind (theShape);
142 return;
143 }
144
145 myShapeColors.ChangeFind (theShape) = new AIS_ColoredDrawer (myDrawer);
146}
147
148//=======================================================================
149//function : SetCustomColor
150//purpose :
151//=======================================================================
152void AIS_ColoredShape::SetCustomColor (const TopoDS_Shape& theShape,
153 const Quantity_Color& theColor)
154{
155 if (theShape.IsNull())
156 {
157 return;
158 }
159
160 const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
161 setColor (aDrawer, theColor);
162 aDrawer->SetOwnColor (theColor);
163 LoadRecomputable (AIS_WireFrame);
164 LoadRecomputable (AIS_Shaded);
165}
166
167//=======================================================================
168//function : SetCustomWidth
169//purpose :
170//=======================================================================
171void AIS_ColoredShape::SetCustomWidth (const TopoDS_Shape& theShape,
172 const Standard_Real theLineWidth)
173{
174 if (theShape.IsNull())
175 {
176 return;
177 }
178
179 const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
180 setWidth (CustomAspects (theShape), theLineWidth);
181 aDrawer->SetOwnWidth (theLineWidth);
182 LoadRecomputable (AIS_WireFrame);
183 LoadRecomputable (AIS_Shaded);
184}
185
186//=======================================================================
187//function : SetColor
188//purpose :
189//=======================================================================
190
191void AIS_ColoredShape::SetColor (const Quantity_Color& theColor)
192{
193 setColor (myDrawer, theColor);
194 myOwnColor = theColor;
195 hasOwnColor = Standard_True;
196 LoadRecomputable (AIS_WireFrame);
197 LoadRecomputable (AIS_Shaded);
198 for (DataMapOfShapeColor::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
199 {
200 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
201 if (aDrawer->HasOwnColor())
202 {
203 continue;
204 }
205
206 if (aDrawer->HasShadingAspect())
207 {
208 aDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
209 }
210 if (aDrawer->HasLineAspect())
211 {
212 aDrawer->LineAspect()->SetColor (theColor);
213 }
214 if (aDrawer->HasWireAspect())
215 {
216 aDrawer->WireAspect()->SetColor (theColor);
217 }
218 }
219}
220
221//=======================================================================
222//function : SetWidth
223//purpose :
224//=======================================================================
225
226void AIS_ColoredShape::SetWidth (const Standard_Real theLineWidth)
227{
228 setWidth (myDrawer, theLineWidth);
229 myOwnWidth = theLineWidth;
230 LoadRecomputable (AIS_WireFrame);
231 LoadRecomputable (AIS_Shaded);
232 for (DataMapOfShapeColor::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
233 {
234 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
235 if (aDrawer->HasOwnWidth())
236 {
237 continue;
238 }
239
240 if (aDrawer->HasLineAspect())
241 {
242 aDrawer->LineAspect()->SetWidth (theLineWidth);
243 }
244 if (aDrawer->HasWireAspect())
245 {
246 aDrawer->WireAspect()->SetWidth (theLineWidth);
247 }
248 }
249}
250
251//=======================================================================
252//function : SetTransparency
253//purpose :
254//=======================================================================
255
256void AIS_ColoredShape::SetTransparency (const Standard_Real theValue)
257{
258 setTransparency (myDrawer, theValue);
259 myTransparency = theValue;
260 LoadRecomputable (AIS_WireFrame);
261 LoadRecomputable (AIS_Shaded);
262 for (DataMapOfShapeColor::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
263 {
264 const Handle(AIS_Drawer)& aDrawer = anIter.Value();
265 if (aDrawer->HasShadingAspect())
266 {
267 aDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
268 }
269 }
270}
271
272//=======================================================================
e0608a8d 273//function : SetMaterial
274//purpose :
275//=======================================================================
276
277void AIS_ColoredShape::SetMaterial (const Graphic3d_MaterialAspect& theMaterial)
278{
279 setMaterial (myDrawer, theMaterial, HasColor(), IsTransparent());
280 //myOwnMaterial = theMaterial;
281 hasOwnMaterial = Standard_True;
282 LoadRecomputable (AIS_Shaded);
283 for (DataMapOfShapeColor::Iterator anIter (myShapeColors); anIter.More(); anIter.Next())
284 {
285 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
286 //if (aDrawer->HasOwnMaterial()) continue;
287 if (aDrawer->HasShadingAspect())
288 {
289 setMaterial (aDrawer, theMaterial, aDrawer->HasOwnColor(), Standard_False); // aDrawer->IsTransparent()
290 }
291 }
292}
293
294//=======================================================================
ad3217cd 295//function : Compute
296//purpose :
297//=======================================================================
298void AIS_ColoredShape::Compute (const Handle(PrsMgr_PresentationManager3d)& ,
299 const Handle(Prs3d_Presentation)& thePrs,
300 const Standard_Integer theMode)
301{
302 thePrs->Clear();
303 if (IsInfinite())
304 {
305 thePrs->SetInfiniteState (Standard_True);
306 }
307
308 const Standard_Boolean isClosed = StdPrs_ToolShadedShape::IsClosed (myshape);
309 if (theMode == AIS_Shaded)
310 {
311 // compute mesh for entire shape beforehand to ensure consistency and optimizations (parallelization)
312 Standard_Real anAnglePrev, anAngleNew, aCoeffPrev, aCoeffNew;
313 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle (anAngleNew, anAnglePrev);
314 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(aCoeffNew, aCoeffPrev);
315 if ((isOwnDeviationAngle && Abs (anAngleNew - anAnglePrev) > Precision::Angular())
316 || (isOwnDeviationCoefficient && Abs (aCoeffNew - aCoeffPrev) > Precision::Confusion()))
317 {
318 BRepTools::Clean (myshape);
319 }
320 StdPrs_ShadedShape::Tessellate (myshape, myDrawer);
321 }
322
323 // 1) myShapeColors + myshape --> array[TopAbs_ShapeEnum] of map of color-to-compound
324 DataMapOfShapeCompd aTypeKeyshapeDrawshapeArray[(size_t )TopAbs_SHAPE];
325 dispatchColors (myshape, myShapeColors, aTypeKeyshapeDrawshapeArray);
326
327 // 2) finally add appropriate presentations (1 color -- 1 compound) according to theMode
328 Handle(AIS_ColoredDrawer) aCustomDrawer;
329 for (size_t aShType = 0; aShType < (size_t )TopAbs_SHAPE; ++aShType)
330 {
331 DataMapOfShapeCompd& aKeyshapeDrawshapeMap = aTypeKeyshapeDrawshapeArray[aShType];
332 for (DataMapOfShapeCompd::Iterator aMapIter (aKeyshapeDrawshapeMap);
333 aMapIter.More(); aMapIter.Next())
334 {
335 const TopoDS_Shape& aShapeKey = aMapIter.Key(); // key shape with detailed color or a base shape
336 const TopoDS_Compound& aShapeDraw = aMapIter.Value(); // compound of subshapes with <aShType> type
86766b0e 337 Handle(AIS_Drawer) aDrawer;
338 if (myShapeColors.Find (aShapeKey, aCustomDrawer))
339 {
340 aDrawer = aCustomDrawer;
341 if (aCustomDrawer->IsHidden())
342 {
343 continue;
344 }
345 }
346 else
347 {
348 aDrawer = myDrawer;
349 }
ad3217cd 350
351 // Draw each kind of subshapes and personal-colored shapes in a separate group
352 // since it's necessary to set transparency/material for all subshapes
353 // without affecting their unique colors
354 Handle(Graphic3d_Group) aCurrGroup = Prs3d_Root::NewGroup (thePrs);
355 switch (theMode)
356 {
357 default:
358 case AIS_Shaded:
359 {
360 if ((Standard_Integer )aShapeDraw.ShapeType() <= TopAbs_FACE
361 && !IsInfinite())
362 {
363 StdPrs_ShadedShape::Add (thePrs, aShapeDraw, aDrawer);
364
365 aDrawer->SetShadingAspectGlobal (Standard_False);
366 Handle(Graphic3d_AspectFillArea3d) anAsp = aDrawer->ShadingAspect()->Aspect();
367 isClosed ? anAsp->SuppressBackFace() : anAsp->AllowBackFace();
368 aCurrGroup->SetGroupPrimitivesAspect (anAsp);
369 break;
370 }
371 // compute wire-frame otherwise
372 }
373 case AIS_WireFrame:
374 {
375 StdPrs_WFDeflectionShape::Add (thePrs, aShapeDraw, aDrawer);
376 break;
377 }
378 }
379 }
380 }
381}
382
383//=======================================================================
384//function : dispatchColors
385//purpose :
386//=======================================================================
387Standard_Boolean AIS_ColoredShape::dispatchColors (const TopoDS_Shape& theBaseKey,
388 const TopoDS_Shape& theSubshapeToParse,
389 const DataMapOfShapeShape& theSubshapeKeyshapeMap,
390 const TopAbs_ShapeEnum theParentType,
391 DataMapOfShapeCompd* theTypeKeyshapeDrawshapeArray)
392{
393 TopAbs_ShapeEnum aShType = theSubshapeToParse.ShapeType();
394 if (aShType == TopAbs_SHAPE)
395 {
396 return Standard_False;
397 }
398
399 // check own setting of current shape
400 TopoDS_Shape aKeyShape = theBaseKey;
401 Standard_Boolean isOverriden = theSubshapeKeyshapeMap.Find (theSubshapeToParse, aKeyShape);
402
403 // iterate on sub-shapes
404 BRep_Builder aBBuilder;
405 TopoDS_Shape aShapeCopy = theSubshapeToParse.EmptyCopied();
ab860031 406 aShapeCopy.Closed (theSubshapeToParse.Closed());
ad3217cd 407 Standard_Boolean isSubOverride = Standard_False;
408 Standard_Integer nbDef = 0;
409 for (TopoDS_Iterator it (theSubshapeToParse); it.More(); it.Next())
410 {
411 if (dispatchColors (theBaseKey, it.Value(),
412 theSubshapeKeyshapeMap, aShType,
413 theTypeKeyshapeDrawshapeArray))
414 {
415 isSubOverride = Standard_True;
416 }
417 else
418 {
419 aBBuilder.Add (aShapeCopy, it.Value());
420 ++nbDef;
421 }
422 }
423 if (aShType == TopAbs_FACE || !isSubOverride)
424 {
425 aShapeCopy = theSubshapeToParse;
426 }
427 else if (nbDef == 0)
428 {
429 return isOverriden || isSubOverride; // empty compound
430 }
431
432 // if any of styles is overridden regarding to default one, add rest to map
433 if (isOverriden
434 || (isSubOverride && theParentType != TopAbs_WIRE // avoid drawing edges when vertex color is overridden
435 && theParentType != TopAbs_FACE) // avoid drawing edges of the same color as face
436 || (theParentType == TopAbs_SHAPE && !(isOverriden || isSubOverride))) // bind original shape to default color
437 {
438 TopoDS_Compound aCompound;
439 DataMapOfShapeCompd& aKeyshapeDrawshapeMap = theTypeKeyshapeDrawshapeArray[(size_t )aShType];
440 if (!aKeyshapeDrawshapeMap.FindFromKey (aKeyShape, aCompound))
441 {
442 aBBuilder.MakeCompound (aCompound);
443 aKeyshapeDrawshapeMap.Add (aKeyShape, aCompound);
444 }
445 aBBuilder.Add (aCompound, aShapeCopy);
446 }
447 return isOverriden || isSubOverride;
448}
449
3ea0a91b 450//! Function to check if specified compound is sub-shape of another one
451inline Standard_Boolean isContainCompound (const TopoDS_Shape& theShape,
452 const TopoDS_Compound& theCompound)
453{
454 if (theShape.ShapeType() != TopAbs_COMPOUND)
455 {
456 return Standard_False;
457 }
458
459 for (TopoDS_Iterator aSubShapeIter (theShape); aSubShapeIter.More(); aSubShapeIter.Next())
460 {
461 if (aSubShapeIter.Value().ShapeType() != TopAbs_COMPOUND)
462 {
463 continue;
464 }
465 else if (aSubShapeIter.Value() == theCompound
466 || isContainCompound (aSubShapeIter.Value(), theCompound))
467 {
468 return Standard_True;
469 }
470 }
471 return Standard_False;
472}
473
ad3217cd 474//=======================================================================
475//function : dispatchColors
476//purpose :
477//=======================================================================
478void AIS_ColoredShape::dispatchColors (const TopoDS_Shape& theBaseShape,
479 const DataMapOfShapeColor& theKeyshapeColorMap,
480 DataMapOfShapeCompd* theTypeKeyshapeDrawshapeArray)
481{
482 // Extract <theShapeColors> map (KeyshapeColored -> Color)
483 // to subshapes map (Subshape -> KeyshapeColored).
484 // This needed when colored shape is not part of <theBaseShape>
485 // (but subshapes are) and actually container for subshapes.
486 DataMapOfShapeShape aSubshapeKeyshapeMap;
3ea0a91b 487 for (DataMapOfShapeColor::Iterator aKeyShapeIter (theKeyshapeColorMap);
488 aKeyShapeIter.More(); aKeyShapeIter.Next())
ad3217cd 489 {
3ea0a91b 490 const TopoDS_Shape& aKeySh = aKeyShapeIter.Key();
491 const TopAbs_ShapeEnum aType = aKeySh.ShapeType();
ad3217cd 492 TopAbs_ShapeEnum aSubType = (aType == TopAbs_SOLID || aType == TopAbs_SHELL)
493 ? TopAbs_FACE
494 : (aType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_SHAPE);
495 switch (aSubType)
496 {
497 case TopAbs_SHAPE:
498 {
3ea0a91b 499 if (aType == TopAbs_COMPOUND
500 && !isContainCompound (theBaseShape, TopoDS::Compound (aKeySh)))
501 {
502 for (TopoDS_Iterator aSubShapeIter (aKeySh); aSubShapeIter.More(); aSubShapeIter.Next())
503 {
504 if (!aSubshapeKeyshapeMap.IsBound (aSubShapeIter.Value()))
505 {
506 aSubshapeKeyshapeMap.Bind (aSubShapeIter.Value(), aKeySh);
507 }
508 }
509 }
510 else
511 {
512 aSubshapeKeyshapeMap.Bind (aKeySh, aKeySh);
513 }
ad3217cd 514 break;
515 }
516 default:
517 {
3ea0a91b 518 for (TopExp_Explorer anExp (aKeySh, aSubType); anExp.More(); anExp.Next())
ad3217cd 519 {
520 if (!aSubshapeKeyshapeMap.IsBound (anExp.Current()))
521 {
3ea0a91b 522 aSubshapeKeyshapeMap.Bind (anExp.Current(), aKeySh);
ad3217cd 523 }
524 }
525 }
526 }
527 }
528
529 // Fill the array of maps per shape type
530 dispatchColors (theBaseShape, theBaseShape,
531 aSubshapeKeyshapeMap, TopAbs_SHAPE,
532 theTypeKeyshapeDrawshapeArray);
533}