1 // Created on: 1993-09-23
2 // Created by: Jean-Louis FRENKEL
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <StdPrs_ShadedShape.hxx>
19 #include <Bnd_Box.hxx>
20 #include <BRepTools.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRep_Builder.hxx>
23 #include <BRep_Tool.hxx>
24 #include <Graphic3d_ArrayOfSegments.hxx>
25 #include <Graphic3d_ArrayOfTriangles.hxx>
26 #include <Graphic3d_AspectFillArea3d.hxx>
27 #include <Graphic3d_Group.hxx>
31 #include <NCollection_List.hxx>
32 #include <Precision.hxx>
33 #include <Prs3d_Drawer.hxx>
34 #include <Prs3d_IsoAspect.hxx>
35 #include <Prs3d_LineAspect.hxx>
36 #include <Prs3d_Presentation.hxx>
37 #include <Prs3d_ShadingAspect.hxx>
38 #include <Poly_Connect.hxx>
39 #include <Poly_PolygonOnTriangulation.hxx>
40 #include <Poly_Triangulation.hxx>
41 #include <StdPrs_ToolTriangulatedShape.hxx>
42 #include <StdPrs_WFShape.hxx>
44 #include <TopExp_Explorer.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TColgp_Array1OfDir.hxx>
50 #include <TColgp_Array1OfPnt2d.hxx>
51 #include <TColgp_HArray1OfPnt.hxx>
52 #include <TopTools_ListOfShape.hxx>
53 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
58 //! Computes wireframe presentation for free wires and vertices
59 void wireframeFromShape (const Handle (Prs3d_Presentation)& thePrs,
60 const TopoDS_Shape& theShape,
61 const Handle (Prs3d_Drawer)& theDrawer)
63 Standard_Boolean aDrawAllVerticesFlag = (theDrawer->VertexDrawMode() == Prs3d_VDM_All);
65 if (!aDrawAllVerticesFlag && theShape.ShapeType() != TopAbs_COMPOUND)
70 TopExp_Explorer aShapeIter (theShape, TopAbs_FACE);
71 if (!aShapeIter.More())
73 // compound contains no shaded elements at all
74 StdPrs_WFShape::Add (thePrs, theShape, theDrawer);
78 // We have to create a compound and collect all subshapes not drawn by the shading algo.
81 // - isolated vertices, if aDrawAllVerticesFlag == Standard_False
82 // - all shape's vertices, if aDrawAllVerticesFlag == Standard_True
83 TopoDS_Compound aCompoundWF;
84 BRep_Builder aBuilder;
85 aBuilder.MakeCompound (aCompoundWF);
86 Standard_Boolean hasElement = Standard_False;
89 for (aShapeIter.Init (theShape, TopAbs_EDGE, TopAbs_FACE); aShapeIter.More(); aShapeIter.Next())
91 hasElement = Standard_True;
92 aBuilder.Add (aCompoundWF, aShapeIter.Current());
94 // isolated or all vertices
95 aShapeIter.Init (theShape, TopAbs_VERTEX, aDrawAllVerticesFlag ? TopAbs_SHAPE : TopAbs_EDGE);
96 for (; aShapeIter.More(); aShapeIter.Next())
98 hasElement = Standard_True;
99 aBuilder.Add (aCompoundWF, aShapeIter.Current());
103 StdPrs_WFShape::Add (thePrs, aCompoundWF, theDrawer);
107 //! Computes special wireframe presentation for faces without triangulation.
108 void wireframeNoTriangFacesFromShape (const Handle(Prs3d_Presentation)& thePrs,
109 const TopoDS_Shape& theShape,
110 const Handle(Prs3d_Drawer)& theDrawer)
112 TopoDS_Compound aCompoundWF;
113 BRep_Builder aBuilder;
114 aBuilder.MakeCompound (aCompoundWF);
115 TopLoc_Location aLoc;
116 Standard_Boolean hasElement = Standard_False;
118 for (TopExp_Explorer aShapeIter(theShape, TopAbs_FACE); aShapeIter.More(); aShapeIter.Next())
120 const TopoDS_Face& aFace = TopoDS::Face (aShapeIter.Current());
121 const Handle(Poly_Triangulation) aTriang = BRep_Tool::Triangulation (aFace, aLoc);
122 if (aTriang.IsNull())
124 hasElement = Standard_True;
125 aBuilder.Add (aCompoundWF, aFace);
131 Standard_Integer aPrevUIsoNb = theDrawer->UIsoAspect()->Number();
132 Standard_Integer aPrevVIsoNb = theDrawer->VIsoAspect()->Number();
133 theDrawer->UIsoAspect()->SetNumber (5);
134 theDrawer->VIsoAspect()->SetNumber (5);
136 StdPrs_WFShape::Add (thePrs, aCompoundWF, theDrawer);
138 theDrawer->UIsoAspect()->SetNumber (aPrevUIsoNb);
139 theDrawer->VIsoAspect()->SetNumber (aPrevVIsoNb);
143 //! Gets triangulation of every face of shape and fills output array of triangles
144 static Handle(Graphic3d_ArrayOfTriangles) fillTriangles (const TopoDS_Shape& theShape,
145 const Standard_Boolean theHasTexels,
146 const gp_Pnt2d& theUVOrigin,
147 const gp_Pnt2d& theUVRepeat,
148 const gp_Pnt2d& theUVScale)
150 Handle(Poly_Triangulation) aT;
151 TopLoc_Location aLoc;
153 Standard_Integer aNbTriangles = 0;
154 Standard_Integer aNbVertices = 0;
156 // Precision for compare square distances
157 const Standard_Real aPreci = Precision::SquareConfusion();
159 TopExp_Explorer aFaceIt(theShape, TopAbs_FACE);
160 for (; aFaceIt.More(); aFaceIt.Next())
162 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
163 aT = BRep_Tool::Triangulation (aFace, aLoc);
166 aNbTriangles += aT->NbTriangles();
167 aNbVertices += aT->NbNodes();
170 if (aNbVertices < 3 || aNbTriangles <= 0)
172 return Handle(Graphic3d_ArrayOfTriangles)();
175 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (aNbVertices, 3 * aNbTriangles,
176 Standard_True, Standard_False, theHasTexels);
177 Standard_Real aUmin (0.0), aUmax (0.0), aVmin (0.0), aVmax (0.0), dUmax (0.0), dVmax (0.0);
178 for (aFaceIt.Init (theShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next())
180 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
181 aT = BRep_Tool::Triangulation (aFace, aLoc);
186 const gp_Trsf& aTrsf = aLoc.Transformation();
188 // Determinant of transform matrix less then 0 means that mirror transform applied.
189 Standard_Boolean isMirrored = aTrsf.VectorialPart().Determinant() < 0;
191 Poly_Connect aPolyConnect (aT);
192 // Extracts vertices & normals from nodes
193 const TColgp_Array1OfPnt& aNodes = aT->Nodes();
194 const TColgp_Array1OfPnt2d& aUVNodes = aT->UVNodes();
195 TColgp_Array1OfDir aNormals (aNodes.Lower(), aNodes.Upper());
196 StdPrs_ToolTriangulatedShape::Normal (aFace, aPolyConnect, aNormals);
200 BRepTools::UVBounds (aFace, aUmin, aUmax, aVmin, aVmax);
201 dUmax = (aUmax - aUmin);
202 dVmax = (aVmax - aVmin);
205 const Standard_Integer aDecal = anArray->VertexNumber();
206 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
208 aPoint = aNodes (aNodeIter);
209 if (!aLoc.IsIdentity())
211 aPoint.Transform (aTrsf);
213 aNormals (aNodeIter) = aNormals (aNodeIter).Transformed (aTrsf);
216 if (theHasTexels && aUVNodes.Upper() == aNodes.Upper())
218 const gp_Pnt2d aTexel = gp_Pnt2d ((-theUVOrigin.X() + (theUVRepeat.X() * (aUVNodes (aNodeIter).X() - aUmin)) / dUmax) / theUVScale.X(),
219 (-theUVOrigin.Y() + (theUVRepeat.Y() * (aUVNodes (aNodeIter).Y() - aVmin)) / dVmax) / theUVScale.Y());
220 anArray->AddVertex (aPoint, aNormals (aNodeIter), aTexel);
224 anArray->AddVertex (aPoint, aNormals (aNodeIter));
228 // Fill array with vertex and edge visibility info
229 const Poly_Array1OfTriangle& aTriangles = aT->Triangles();
230 Standard_Integer anIndex[3];
231 for (Standard_Integer aTriIter = 1; aTriIter <= aT->NbTriangles(); ++aTriIter)
233 if ((aFace.Orientation() == TopAbs_REVERSED) ^ isMirrored)
235 aTriangles (aTriIter).Get (anIndex[0], anIndex[2], anIndex[1]);
239 aTriangles (aTriIter).Get (anIndex[0], anIndex[1], anIndex[2]);
242 gp_Pnt aP1 = aNodes (anIndex[0]);
243 gp_Pnt aP2 = aNodes (anIndex[1]);
244 gp_Pnt aP3 = aNodes (anIndex[2]);
246 gp_Vec aV1 (aP1, aP2);
247 if (aV1.SquareMagnitude() <= aPreci)
251 gp_Vec aV2 (aP2, aP3);
252 if (aV2.SquareMagnitude() <= aPreci)
256 gp_Vec aV3 (aP3, aP1);
257 if (aV3.SquareMagnitude() <= aPreci)
264 if (aV1.SquareMagnitude() > aPreci)
266 anArray->AddEdge (anIndex[0] + aDecal);
267 anArray->AddEdge (anIndex[1] + aDecal);
268 anArray->AddEdge (anIndex[2] + aDecal);
275 //! Prepare shaded presentation for specified shape
276 static Standard_Boolean shadeFromShape (const TopoDS_Shape& theShape,
277 const Handle(Prs3d_Presentation)& thePrs,
278 const Handle(Prs3d_Drawer)& theDrawer,
279 const Standard_Boolean theHasTexels,
280 const gp_Pnt2d& theUVOrigin,
281 const gp_Pnt2d& theUVRepeat,
282 const gp_Pnt2d& theUVScale,
283 const bool theIsClosed)
285 Handle(Graphic3d_ArrayOfTriangles) aPArray = fillTriangles (theShape, theHasTexels, theUVOrigin, theUVRepeat, theUVScale);
286 if (aPArray.IsNull())
288 return Standard_False;
291 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup (thePrs);
292 aGroup->SetClosed (theIsClosed);
293 aGroup->SetGroupPrimitivesAspect (theDrawer->ShadingAspect()->Aspect());
294 aGroup->AddPrimitiveArray (aPArray);
295 return Standard_True;
298 //! Compute boundary presentation for faces of the shape.
299 static void computeFaceBoundaries (const TopoDS_Shape& theShape,
300 const Handle(Prs3d_Presentation)& thePrs,
301 const Handle(Prs3d_Drawer)& theDrawer)
303 // collection of all triangulation nodes on edges
304 // for computing boundaries presentation
305 NCollection_List<Handle(TColgp_HArray1OfPnt)> aNodeCollection;
306 Standard_Integer aNodeNumber = 0;
307 Standard_Integer aNbPolylines = 0;
309 TopLoc_Location aTrsf;
311 // explore all boundary edges
312 TopTools_IndexedDataMapOfShapeListOfShape anEdgesMap;
313 TopExp::MapShapesAndAncestors (theShape, TopAbs_EDGE, TopAbs_FACE, anEdgesMap);
314 for (TopTools_IndexedDataMapOfShapeListOfShape::Iterator anEdgeIter (anEdgesMap); anEdgeIter.More(); anEdgeIter.Next())
317 if (anEdgeIter.Value().Extent() == 0)
322 // take one of the shared edges and get edge triangulation
323 const TopoDS_Face& aFace = TopoDS::Face (anEdgeIter.Value().First());
324 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (aFace, aTrsf);
325 if (aTriangulation.IsNull())
330 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
331 Handle(Poly_PolygonOnTriangulation) anEdgePoly = BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
332 if (!anEdgePoly.IsNull()
333 && anEdgePoly->Nodes().Length() >= 2)
335 aNodeNumber += anEdgePoly->Nodes().Length();
339 if (aNodeNumber == 0)
344 // create indexed segments array to pack polylines from different edges into single array
345 const Standard_Integer aSegmentEdgeNb = (aNodeNumber - aNbPolylines) * 2;
346 Handle(Graphic3d_ArrayOfSegments) aSegments = new Graphic3d_ArrayOfSegments (aNodeNumber, aSegmentEdgeNb);
347 for (TopTools_IndexedDataMapOfShapeListOfShape::Iterator anEdgeIter (anEdgesMap); anEdgeIter.More(); anEdgeIter.Next())
349 if (anEdgeIter.Value().Extent() == 0)
354 const TopoDS_Face& aFace = TopoDS::Face (anEdgeIter.Value().First());
355 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (aFace, aTrsf);
356 if (aTriangulation.IsNull())
361 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
362 Handle(Poly_PolygonOnTriangulation) anEdgePoly = BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
363 if (anEdgePoly.IsNull()
364 || anEdgePoly->Nodes().Length () < 2)
369 // get edge nodes indexes from face triangulation
370 const TColgp_Array1OfPnt& aTriNodes = aTriangulation->Nodes();
371 const TColStd_Array1OfInteger& anEdgeNodes = anEdgePoly->Nodes();
373 // collect the edge nodes
374 Standard_Integer aSegmentEdge = aSegments->VertexNumber() + 1;
375 for (Standard_Integer aNodeIdx = anEdgeNodes.Lower(); aNodeIdx <= anEdgeNodes.Upper(); ++aNodeIdx)
377 // node index in face triangulation
378 // get node and apply location transformation to the node
379 const Standard_Integer aTriIndex = anEdgeNodes.Value (aNodeIdx);
380 gp_Pnt aTriNode = aTriNodes.Value (aTriIndex);
381 if (!aTrsf.IsIdentity())
383 aTriNode.Transform (aTrsf);
386 aSegments->AddVertex (aTriNode);
387 if (aNodeIdx != anEdgeNodes.Lower())
389 aSegments->AddEdge ( aSegmentEdge);
390 aSegments->AddEdge (++aSegmentEdge);
395 // set up aspect and add polyline data
396 Handle(Graphic3d_AspectLine3d) aBoundaryAspect = theDrawer->FaceBoundaryAspect()->Aspect();
398 Handle(Graphic3d_Group) aPrsGrp = Prs3d_Root::CurrentGroup (thePrs);
399 aPrsGrp->SetGroupPrimitivesAspect (aBoundaryAspect);
400 aPrsGrp->AddPrimitiveArray (aSegments);
403 } // anonymous namespace
405 // =======================================================================
406 // function : ExploreSolids
408 // =======================================================================
409 void StdPrs_ShadedShape::ExploreSolids (const TopoDS_Shape& theShape,
410 const BRep_Builder& theBuilder,
411 TopoDS_Compound& theClosed,
412 TopoDS_Compound& theOpened,
413 const Standard_Boolean theIgnore1DSubShape)
415 if (theShape.IsNull())
420 switch (theShape.ShapeType())
422 case TopAbs_COMPOUND:
423 case TopAbs_COMPSOLID:
425 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
427 ExploreSolids (anIter.Value(), theBuilder, theClosed, theOpened, theIgnore1DSubShape);
433 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
435 const TopoDS_Shape& aSubShape = anIter.Value();
436 const Standard_Boolean isClosed = aSubShape.ShapeType() == TopAbs_SHELL &&
437 BRep_Tool::IsClosed (aSubShape) &&
438 StdPrs_ToolTriangulatedShape::IsTriangulated (aSubShape);
439 theBuilder.Add (isClosed ? theClosed : theOpened, aSubShape);
446 theBuilder.Add (theOpened, theShape);
453 if (!theIgnore1DSubShape)
455 theBuilder.Add (theOpened, theShape);
465 // =======================================================================
468 // =======================================================================
469 void StdPrs_ShadedShape::Add (const Handle(Prs3d_Presentation)& thePrs,
470 const TopoDS_Shape& theShape,
471 const Handle(Prs3d_Drawer)& theDrawer,
472 const StdPrs_Volume theVolume)
475 StdPrs_ShadedShape::Add (thePrs, theShape, theDrawer,
476 Standard_False, aDummy, aDummy, aDummy, theVolume);
479 // =======================================================================
482 // =======================================================================
483 void StdPrs_ShadedShape::Add (const Handle (Prs3d_Presentation)& thePrs,
484 const TopoDS_Shape& theShape,
485 const Handle (Prs3d_Drawer)& theDrawer,
486 const Standard_Boolean theHasTexels,
487 const gp_Pnt2d& theUVOrigin,
488 const gp_Pnt2d& theUVRepeat,
489 const gp_Pnt2d& theUVScale,
490 const StdPrs_Volume theVolume)
492 if (theShape.IsNull())
497 // add wireframe presentation for isolated edges and vertices
498 wireframeFromShape (thePrs, theShape, theDrawer);
500 // Use automatic re-triangulation with deflection-check logic only if this feature is enable
501 if (theDrawer->IsAutoTriangulation())
503 // Triangulation completeness is important for "open-closed" analysis - perform tessellation beforehand
504 StdPrs_ToolTriangulatedShape::Tessellate (theShape, theDrawer);
507 // add special wireframe presentation for faces without triangulation
508 wireframeNoTriangFacesFromShape (thePrs, theShape, theDrawer);
510 // The shape types listed below need advanced analysis as potentially containing
511 // both closed and open parts. Solids are also included, because they might
512 // contain non-manifold parts inside (internal open shells)
513 if ((theShape.ShapeType() == TopAbs_COMPOUND
514 || theShape.ShapeType() == TopAbs_COMPSOLID
515 || theShape.ShapeType() == TopAbs_SOLID)
516 && theVolume == StdPrs_Volume_Autodetection)
518 // collect two compounds: for opened and closed (solid) sub-shapes
519 TopoDS_Compound anOpened, aClosed;
520 BRep_Builder aBuilder;
521 aBuilder.MakeCompound (aClosed);
522 aBuilder.MakeCompound (anOpened);
523 ExploreSolids (theShape, aBuilder, aClosed, anOpened, Standard_True);
525 TopoDS_Iterator aShapeIter (aClosed);
526 if (aShapeIter.More())
528 shadeFromShape (aClosed, thePrs, theDrawer,
529 theHasTexels, theUVOrigin, theUVRepeat, theUVScale, true);
532 aShapeIter.Initialize (anOpened);
533 if (aShapeIter.More())
535 shadeFromShape (anOpened, thePrs, theDrawer,
536 theHasTexels, theUVOrigin, theUVRepeat, theUVScale, false);
541 // if the shape type is not compound, composolid or solid, use autodetection back-facing filled
542 shadeFromShape (theShape, thePrs, theDrawer,
543 theHasTexels, theUVOrigin, theUVRepeat, theUVScale,
544 theVolume == StdPrs_Volume_Closed);
547 if (theDrawer->FaceBoundaryDraw())
549 computeFaceBoundaries (theShape, thePrs, theDrawer);