1 // Created on: 2014-04-24
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <AIS_ColoredShape.hxx>
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>
41 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_Iterator.hxx>
44 IMPLEMENT_STANDARD_HANDLE (AIS_ColoredDrawer, AIS_Drawer)
45 IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredDrawer, AIS_Drawer)
47 IMPLEMENT_STANDARD_HANDLE (AIS_ColoredShape, AIS_Shape)
48 IMPLEMENT_STANDARD_RTTIEXT(AIS_ColoredShape, AIS_Shape)
50 //=======================================================================
51 //function : AIS_ColoredShape
53 //=======================================================================
54 AIS_ColoredShape::AIS_ColoredShape (const TopoDS_Shape& theShape)
55 : AIS_Shape (theShape)
57 // disable dedicated line aspects
58 myDrawer->SetFreeBoundaryAspect (myDrawer->LineAspect());
59 myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
60 myDrawer->SetSeenLineAspect (myDrawer->LineAspect());
63 //=======================================================================
64 //function : AIS_ColoredShape
66 //=======================================================================
67 AIS_ColoredShape::AIS_ColoredShape (const Handle(AIS_Shape)& theShape)
68 : AIS_Shape (theShape->Shape())
70 // disable dedicated line aspects
71 myDrawer->SetFreeBoundaryAspect (myDrawer->LineAspect());
72 myDrawer->SetUnFreeBoundaryAspect(myDrawer->LineAspect());
73 myDrawer->SetSeenLineAspect (myDrawer->LineAspect());
74 if (theShape->HasMaterial())
76 SetMaterial (theShape->Material());
78 if (theShape->HasColor())
80 SetColor (theShape->Color());
82 if (theShape->HasWidth())
84 SetWidth (theShape->Width());
86 if (theShape->IsTransparent())
88 SetTransparency (theShape->Transparency());
92 //=======================================================================
93 //function : CustomAspects
95 //=======================================================================
96 Handle(AIS_ColoredDrawer) AIS_ColoredShape::CustomAspects (const TopoDS_Shape& theShape)
98 Handle(AIS_ColoredDrawer) aDrawer;
99 myShapeColors.Find (theShape, aDrawer);
100 if (aDrawer.IsNull())
102 aDrawer = new AIS_ColoredDrawer (myDrawer);
103 myShapeColors.Bind (theShape, aDrawer);
104 LoadRecomputable (AIS_WireFrame);
105 LoadRecomputable (AIS_Shaded);
110 //=======================================================================
111 //function : ClearCustomAspects
113 //=======================================================================
114 void AIS_ColoredShape::ClearCustomAspects()
116 if (myShapeColors.IsEmpty())
120 myShapeColors.Clear();
121 LoadRecomputable (AIS_WireFrame);
122 LoadRecomputable (AIS_Shaded);
125 //=======================================================================
126 //function : UnsetCustomAspects
128 //=======================================================================
129 void AIS_ColoredShape::UnsetCustomAspects (const TopoDS_Shape& theShape,
130 const Standard_Boolean theToUnregister)
132 if (!myShapeColors.IsBound (theShape))
137 LoadRecomputable (AIS_WireFrame);
138 LoadRecomputable (AIS_Shaded);
141 myShapeColors.UnBind (theShape);
145 myShapeColors.ChangeFind (theShape) = new AIS_ColoredDrawer (myDrawer);
148 //=======================================================================
149 //function : SetCustomColor
151 //=======================================================================
152 void AIS_ColoredShape::SetCustomColor (const TopoDS_Shape& theShape,
153 const Quantity_Color& theColor)
155 if (theShape.IsNull())
160 const Handle(AIS_ColoredDrawer)& aDrawer = CustomAspects (theShape);
161 setColor (aDrawer, theColor);
162 aDrawer->SetOwnColor (theColor);
163 LoadRecomputable (AIS_WireFrame);
164 LoadRecomputable (AIS_Shaded);
167 //=======================================================================
168 //function : SetCustomWidth
170 //=======================================================================
171 void AIS_ColoredShape::SetCustomWidth (const TopoDS_Shape& theShape,
172 const Standard_Real theLineWidth)
174 if (theShape.IsNull())
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);
186 //=======================================================================
187 //function : SetColor
189 //=======================================================================
191 void AIS_ColoredShape::SetColor (const Quantity_Color& theColor)
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())
200 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
201 if (aDrawer->HasOwnColor())
206 if (aDrawer->HasShadingAspect())
208 aDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
210 if (aDrawer->HasLineAspect())
212 aDrawer->LineAspect()->SetColor (theColor);
214 if (aDrawer->HasWireAspect())
216 aDrawer->WireAspect()->SetColor (theColor);
221 //=======================================================================
222 //function : SetWidth
224 //=======================================================================
226 void AIS_ColoredShape::SetWidth (const Standard_Real theLineWidth)
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())
234 const Handle(AIS_ColoredDrawer)& aDrawer = anIter.Value();
235 if (aDrawer->HasOwnWidth())
240 if (aDrawer->HasLineAspect())
242 aDrawer->LineAspect()->SetWidth (theLineWidth);
244 if (aDrawer->HasWireAspect())
246 aDrawer->WireAspect()->SetWidth (theLineWidth);
251 //=======================================================================
252 //function : SetTransparency
254 //=======================================================================
256 void AIS_ColoredShape::SetTransparency (const Standard_Real theValue)
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())
264 const Handle(AIS_Drawer)& aDrawer = anIter.Value();
265 if (aDrawer->HasShadingAspect())
267 aDrawer->ShadingAspect()->SetTransparency (theValue, myCurrentFacingModel);
272 //=======================================================================
275 //=======================================================================
276 void AIS_ColoredShape::Compute (const Handle(PrsMgr_PresentationManager3d)& ,
277 const Handle(Prs3d_Presentation)& thePrs,
278 const Standard_Integer theMode)
283 thePrs->SetInfiniteState (Standard_True);
286 const Standard_Boolean isClosed = StdPrs_ToolShadedShape::IsClosed (myshape);
287 if (theMode == AIS_Shaded)
289 // compute mesh for entire shape beforehand to ensure consistency and optimizations (parallelization)
290 Standard_Real anAnglePrev, anAngleNew, aCoeffPrev, aCoeffNew;
291 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle (anAngleNew, anAnglePrev);
292 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(aCoeffNew, aCoeffPrev);
293 if ((isOwnDeviationAngle && Abs (anAngleNew - anAnglePrev) > Precision::Angular())
294 || (isOwnDeviationCoefficient && Abs (aCoeffNew - aCoeffPrev) > Precision::Confusion()))
296 BRepTools::Clean (myshape);
298 StdPrs_ShadedShape::Tessellate (myshape, myDrawer);
301 // 1) myShapeColors + myshape --> array[TopAbs_ShapeEnum] of map of color-to-compound
302 DataMapOfShapeCompd aTypeKeyshapeDrawshapeArray[(size_t )TopAbs_SHAPE];
303 dispatchColors (myshape, myShapeColors, aTypeKeyshapeDrawshapeArray);
305 // 2) finally add appropriate presentations (1 color -- 1 compound) according to theMode
306 Handle(AIS_ColoredDrawer) aCustomDrawer;
307 for (size_t aShType = 0; aShType < (size_t )TopAbs_SHAPE; ++aShType)
309 DataMapOfShapeCompd& aKeyshapeDrawshapeMap = aTypeKeyshapeDrawshapeArray[aShType];
310 for (DataMapOfShapeCompd::Iterator aMapIter (aKeyshapeDrawshapeMap);
311 aMapIter.More(); aMapIter.Next())
313 const TopoDS_Shape& aShapeKey = aMapIter.Key(); // key shape with detailed color or a base shape
314 const TopoDS_Compound& aShapeDraw = aMapIter.Value(); // compound of subshapes with <aShType> type
315 Handle(AIS_Drawer) aDrawer;
316 if (myShapeColors.Find (aShapeKey, aCustomDrawer))
318 aDrawer = aCustomDrawer;
319 if (aCustomDrawer->IsHidden())
329 // Draw each kind of subshapes and personal-colored shapes in a separate group
330 // since it's necessary to set transparency/material for all subshapes
331 // without affecting their unique colors
332 Handle(Graphic3d_Group) aCurrGroup = Prs3d_Root::NewGroup (thePrs);
338 if ((Standard_Integer )aShapeDraw.ShapeType() <= TopAbs_FACE
341 StdPrs_ShadedShape::Add (thePrs, aShapeDraw, aDrawer);
343 aDrawer->SetShadingAspectGlobal (Standard_False);
344 Handle(Graphic3d_AspectFillArea3d) anAsp = aDrawer->ShadingAspect()->Aspect();
345 isClosed ? anAsp->SuppressBackFace() : anAsp->AllowBackFace();
346 aCurrGroup->SetGroupPrimitivesAspect (anAsp);
349 // compute wire-frame otherwise
353 StdPrs_WFDeflectionShape::Add (thePrs, aShapeDraw, aDrawer);
361 //=======================================================================
362 //function : dispatchColors
364 //=======================================================================
365 Standard_Boolean AIS_ColoredShape::dispatchColors (const TopoDS_Shape& theBaseKey,
366 const TopoDS_Shape& theSubshapeToParse,
367 const DataMapOfShapeShape& theSubshapeKeyshapeMap,
368 const TopAbs_ShapeEnum theParentType,
369 DataMapOfShapeCompd* theTypeKeyshapeDrawshapeArray)
371 TopAbs_ShapeEnum aShType = theSubshapeToParse.ShapeType();
372 if (aShType == TopAbs_SHAPE)
374 return Standard_False;
377 // check own setting of current shape
378 TopoDS_Shape aKeyShape = theBaseKey;
379 Standard_Boolean isOverriden = theSubshapeKeyshapeMap.Find (theSubshapeToParse, aKeyShape);
381 // iterate on sub-shapes
382 BRep_Builder aBBuilder;
383 TopoDS_Shape aShapeCopy = theSubshapeToParse.EmptyCopied();
384 aShapeCopy.Closed (theSubshapeToParse.Closed());
385 Standard_Boolean isSubOverride = Standard_False;
386 Standard_Integer nbDef = 0;
387 for (TopoDS_Iterator it (theSubshapeToParse); it.More(); it.Next())
389 if (dispatchColors (theBaseKey, it.Value(),
390 theSubshapeKeyshapeMap, aShType,
391 theTypeKeyshapeDrawshapeArray))
393 isSubOverride = Standard_True;
397 aBBuilder.Add (aShapeCopy, it.Value());
401 if (aShType == TopAbs_FACE || !isSubOverride)
403 aShapeCopy = theSubshapeToParse;
407 return isOverriden || isSubOverride; // empty compound
410 // if any of styles is overridden regarding to default one, add rest to map
412 || (isSubOverride && theParentType != TopAbs_WIRE // avoid drawing edges when vertex color is overridden
413 && theParentType != TopAbs_FACE) // avoid drawing edges of the same color as face
414 || (theParentType == TopAbs_SHAPE && !(isOverriden || isSubOverride))) // bind original shape to default color
416 TopoDS_Compound aCompound;
417 DataMapOfShapeCompd& aKeyshapeDrawshapeMap = theTypeKeyshapeDrawshapeArray[(size_t )aShType];
418 if (!aKeyshapeDrawshapeMap.FindFromKey (aKeyShape, aCompound))
420 aBBuilder.MakeCompound (aCompound);
421 aKeyshapeDrawshapeMap.Add (aKeyShape, aCompound);
423 aBBuilder.Add (aCompound, aShapeCopy);
425 return isOverriden || isSubOverride;
428 //=======================================================================
429 //function : dispatchColors
431 //=======================================================================
432 void AIS_ColoredShape::dispatchColors (const TopoDS_Shape& theBaseShape,
433 const DataMapOfShapeColor& theKeyshapeColorMap,
434 DataMapOfShapeCompd* theTypeKeyshapeDrawshapeArray)
436 // Extract <theShapeColors> map (KeyshapeColored -> Color)
437 // to subshapes map (Subshape -> KeyshapeColored).
438 // This needed when colored shape is not part of <theBaseShape>
439 // (but subshapes are) and actually container for subshapes.
440 DataMapOfShapeShape aSubshapeKeyshapeMap;
441 for (DataMapOfShapeColor::Iterator anIt (theKeyshapeColorMap);
442 anIt.More(); anIt.Next())
444 const TopoDS_Shape& aSh = anIt.Key();
445 TopAbs_ShapeEnum aType = aSh.ShapeType();
446 TopAbs_ShapeEnum aSubType = (aType == TopAbs_SOLID || aType == TopAbs_SHELL)
448 : (aType == TopAbs_WIRE ? TopAbs_EDGE : TopAbs_SHAPE);
453 aSubshapeKeyshapeMap.Bind (aSh, aSh);
458 for (TopExp_Explorer anExp (aSh, aSubType); anExp.More(); anExp.Next())
460 if (!aSubshapeKeyshapeMap.IsBound (anExp.Current()))
462 aSubshapeKeyshapeMap.Bind (anExp.Current(), aSh);
469 // Fill the array of maps per shape type
470 dispatchColors (theBaseShape, theBaseShape,
471 aSubshapeKeyshapeMap, TopAbs_SHAPE,
472 theTypeKeyshapeDrawshapeArray);