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 TopExp_Explorer aShapeIter (theShape, TopAbs_FACE);
64 if (!aShapeIter.More())
66 StdPrs_WFShape::Add (thePrs, theShape, theDrawer);
70 const Standard_Boolean aDrawAllVerticesFlag = (theDrawer->VertexDrawMode() == Prs3d_VDM_All);
71 if (!aDrawAllVerticesFlag && theShape.ShapeType() != TopAbs_COMPOUND)
76 // We have to create a compound and collect all subshapes not drawn by the shading algo.
79 // - isolated vertices, if aDrawAllVerticesFlag == Standard_False
80 // - all shape's vertices, if aDrawAllVerticesFlag == Standard_True
81 TopoDS_Compound aCompoundWF;
82 BRep_Builder aBuilder;
83 aBuilder.MakeCompound (aCompoundWF);
84 Standard_Boolean hasElement = Standard_False;
87 for (aShapeIter.Init (theShape, TopAbs_EDGE, TopAbs_FACE); aShapeIter.More(); aShapeIter.Next())
89 hasElement = Standard_True;
90 aBuilder.Add (aCompoundWF, aShapeIter.Current());
92 // isolated or all vertices
93 aShapeIter.Init (theShape, TopAbs_VERTEX, aDrawAllVerticesFlag ? TopAbs_SHAPE : TopAbs_EDGE);
94 for (; aShapeIter.More(); aShapeIter.Next())
96 hasElement = Standard_True;
97 aBuilder.Add (aCompoundWF, aShapeIter.Current());
101 StdPrs_WFShape::Add (thePrs, aCompoundWF, theDrawer);
105 //! Computes special wireframe presentation for faces without triangulation.
106 void wireframeNoTriangFacesFromShape (const Handle(Prs3d_Presentation)& thePrs,
107 const TopoDS_Shape& theShape,
108 const Handle(Prs3d_Drawer)& theDrawer)
110 TopoDS_Compound aCompoundWF;
111 BRep_Builder aBuilder;
112 aBuilder.MakeCompound (aCompoundWF);
113 TopLoc_Location aLoc;
114 Standard_Boolean hasElement = Standard_False;
116 for (TopExp_Explorer aShapeIter(theShape, TopAbs_FACE); aShapeIter.More(); aShapeIter.Next())
118 const TopoDS_Face& aFace = TopoDS::Face (aShapeIter.Current());
119 const Handle(Poly_Triangulation) aTriang = BRep_Tool::Triangulation (aFace, aLoc);
120 if (aTriang.IsNull())
122 hasElement = Standard_True;
123 aBuilder.Add (aCompoundWF, aFace);
129 Standard_Integer aPrevUIsoNb = theDrawer->UIsoAspect()->Number();
130 Standard_Integer aPrevVIsoNb = theDrawer->VIsoAspect()->Number();
131 theDrawer->UIsoAspect()->SetNumber (5);
132 theDrawer->VIsoAspect()->SetNumber (5);
134 StdPrs_WFShape::Add (thePrs, aCompoundWF, theDrawer);
136 theDrawer->UIsoAspect()->SetNumber (aPrevUIsoNb);
137 theDrawer->VIsoAspect()->SetNumber (aPrevVIsoNb);
141 //! Gets triangulation of every face of shape and fills output array of triangles
142 static Handle(Graphic3d_ArrayOfTriangles) fillTriangles (const TopoDS_Shape& theShape,
143 const Standard_Boolean theHasTexels,
144 const gp_Pnt2d& theUVOrigin,
145 const gp_Pnt2d& theUVRepeat,
146 const gp_Pnt2d& theUVScale)
148 Handle(Poly_Triangulation) aT;
149 TopLoc_Location aLoc;
151 Standard_Integer aNbTriangles = 0;
152 Standard_Integer aNbVertices = 0;
154 // Precision for compare square distances
155 const Standard_Real aPreci = Precision::SquareConfusion();
157 TopExp_Explorer aFaceIt(theShape, TopAbs_FACE);
158 for (; aFaceIt.More(); aFaceIt.Next())
160 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
161 aT = BRep_Tool::Triangulation (aFace, aLoc);
164 aNbTriangles += aT->NbTriangles();
165 aNbVertices += aT->NbNodes();
168 if (aNbVertices < 3 || aNbTriangles <= 0)
170 return Handle(Graphic3d_ArrayOfTriangles)();
173 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (aNbVertices, 3 * aNbTriangles,
174 Standard_True, Standard_False, theHasTexels);
175 Standard_Real aUmin (0.0), aUmax (0.0), aVmin (0.0), aVmax (0.0), dUmax (0.0), dVmax (0.0);
176 for (aFaceIt.Init (theShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next())
178 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
179 aT = BRep_Tool::Triangulation (aFace, aLoc);
184 const gp_Trsf& aTrsf = aLoc.Transformation();
186 // Determinant of transform matrix less then 0 means that mirror transform applied.
187 Standard_Boolean isMirrored = aTrsf.VectorialPart().Determinant() < 0;
189 // Extracts vertices & normals from nodes
190 const TColgp_Array1OfPnt& aNodes = aT->Nodes();
191 const TColgp_Array1OfPnt2d& aUVNodes = aT->UVNodes();
192 StdPrs_ToolTriangulatedShape::ComputeNormals (aFace, aT);
193 const TShort_Array1OfShortReal& aNormals = aT->Normals();
194 const Standard_ShortReal* aNormArr = &aNormals.First();
198 BRepTools::UVBounds (aFace, aUmin, aUmax, aVmin, aVmax);
199 dUmax = (aUmax - aUmin);
200 dVmax = (aVmax - aVmin);
203 const Standard_Integer aDecal = anArray->VertexNumber();
204 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
206 aPoint = aNodes (aNodeIter);
207 const Standard_Integer anId = 3 * (aNodeIter - aNodes.Lower());
208 gp_Dir aNorm (aNormArr[anId + 0], aNormArr[anId + 1], aNormArr[anId + 2]);
209 if (aFace.Orientation() == TopAbs_REVERSED)
213 if (!aLoc.IsIdentity())
215 aPoint.Transform (aTrsf);
216 aNorm.Transform (aTrsf);
219 if (theHasTexels && aUVNodes.Upper() == aNodes.Upper())
221 const gp_Pnt2d aTexel = (dUmax == 0.0 || dVmax == 0.0)
222 ? aUVNodes (aNodeIter)
223 : gp_Pnt2d ((-theUVOrigin.X() + (theUVRepeat.X() * (aUVNodes(aNodeIter).X() - aUmin)) / dUmax) / theUVScale.X(),
224 (-theUVOrigin.Y() + (theUVRepeat.Y() * (aUVNodes(aNodeIter).Y() - aVmin)) / dVmax) / theUVScale.Y());
225 anArray->AddVertex (aPoint, aNorm, aTexel);
229 anArray->AddVertex (aPoint, aNorm);
233 // Fill array with vertex and edge visibility info
234 const Poly_Array1OfTriangle& aTriangles = aT->Triangles();
235 Standard_Integer anIndex[3];
236 for (Standard_Integer aTriIter = 1; aTriIter <= aT->NbTriangles(); ++aTriIter)
238 if ((aFace.Orientation() == TopAbs_REVERSED) ^ isMirrored)
240 aTriangles (aTriIter).Get (anIndex[0], anIndex[2], anIndex[1]);
244 aTriangles (aTriIter).Get (anIndex[0], anIndex[1], anIndex[2]);
247 gp_Pnt aP1 = aNodes (anIndex[0]);
248 gp_Pnt aP2 = aNodes (anIndex[1]);
249 gp_Pnt aP3 = aNodes (anIndex[2]);
251 gp_Vec aV1 (aP1, aP2);
252 if (aV1.SquareMagnitude() <= aPreci)
256 gp_Vec aV2 (aP2, aP3);
257 if (aV2.SquareMagnitude() <= aPreci)
261 gp_Vec aV3 (aP3, aP1);
262 if (aV3.SquareMagnitude() <= aPreci)
269 if (aV1.SquareMagnitude() > aPreci)
271 anArray->AddEdges (anIndex[0] + aDecal,
273 anIndex[2] + aDecal);
280 //! Prepare shaded presentation for specified shape
281 static Standard_Boolean shadeFromShape (const TopoDS_Shape& theShape,
282 const Handle(Prs3d_Presentation)& thePrs,
283 const Handle(Prs3d_Drawer)& theDrawer,
284 const Standard_Boolean theHasTexels,
285 const gp_Pnt2d& theUVOrigin,
286 const gp_Pnt2d& theUVRepeat,
287 const gp_Pnt2d& theUVScale,
288 const bool theIsClosed)
290 Handle(Graphic3d_ArrayOfTriangles) aPArray = fillTriangles (theShape, theHasTexels, theUVOrigin, theUVRepeat, theUVScale);
291 if (aPArray.IsNull())
293 return Standard_False;
296 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup (thePrs);
297 aGroup->SetClosed (theIsClosed);
298 aGroup->SetGroupPrimitivesAspect (theDrawer->ShadingAspect()->Aspect());
299 aGroup->AddPrimitiveArray (aPArray);
300 return Standard_True;
303 //! Compute boundary presentation for faces of the shape.
304 static Handle(Graphic3d_ArrayOfSegments) fillFaceBoundaries (const TopoDS_Shape& theShape,
305 GeomAbs_Shape theUpperContinuity)
307 // collection of all triangulation nodes on edges
308 // for computing boundaries presentation
309 Standard_Integer aNodeNumber = 0;
310 Standard_Integer aNbPolylines = 0;
312 TopLoc_Location aTrsf;
313 TColgp_SequenceOfPnt aSeqPntsExtra;
314 for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
316 const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
317 if (aFace.NbChildren() == 0)
319 // handle specifically faces without boundary definition (triangulation-only)
320 StdPrs_WFShape::AddEdgesOnTriangulation (aSeqPntsExtra, aFace, Standard_False);
324 // explore all boundary edges
325 TopTools_IndexedDataMapOfShapeListOfShape anEdgesMap;
326 TopExp::MapShapesAndAncestors (theShape, TopAbs_EDGE, TopAbs_FACE, anEdgesMap);
327 for (TopTools_IndexedDataMapOfShapeListOfShape::Iterator anEdgeIter (anEdgesMap); anEdgeIter.More(); anEdgeIter.Next())
330 if (anEdgeIter.Value().Extent() == 0)
335 // take one of the shared edges and get edge triangulation
336 const TopoDS_Face& aFace = TopoDS::Face (anEdgeIter.Value().First());
337 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (aFace, aTrsf);
338 if (aTriangulation.IsNull())
343 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
344 if (theUpperContinuity < GeomAbs_CN
345 && anEdgeIter.Value().Extent() >= 2
346 && BRep_Tool::MaxContinuity (anEdge) > theUpperContinuity)
351 Handle(Poly_PolygonOnTriangulation) anEdgePoly = BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
352 if (!anEdgePoly.IsNull()
353 && anEdgePoly->Nodes().Length() >= 2)
355 aNodeNumber += anEdgePoly->Nodes().Length();
359 if (aNodeNumber == 0)
361 if (aSeqPntsExtra.Size() < 2)
363 return Handle(Graphic3d_ArrayOfSegments)();
366 Standard_Integer aNbVertices = aSeqPntsExtra.Size();
367 Handle(Graphic3d_ArrayOfSegments) aSegments = new Graphic3d_ArrayOfSegments (aNbVertices);
368 for (Standard_Integer aPntIter = 1; aPntIter <= aNbVertices; aPntIter += 2)
370 aSegments->AddVertex (aSeqPntsExtra.Value (aPntIter));
371 aSegments->AddVertex (aSeqPntsExtra.Value (aPntIter + 1));
376 // create indexed segments array to pack polylines from different edges into single array
377 const Standard_Integer aSegmentEdgeNb = (aNodeNumber - aNbPolylines) * 2;
378 Handle(Graphic3d_ArrayOfSegments) aSegments = new Graphic3d_ArrayOfSegments (aNodeNumber + aSeqPntsExtra.Size(), aSegmentEdgeNb + aSeqPntsExtra.Size());
379 for (TopTools_IndexedDataMapOfShapeListOfShape::Iterator anEdgeIter (anEdgesMap); anEdgeIter.More(); anEdgeIter.Next())
381 if (anEdgeIter.Value().Extent() == 0)
386 const TopoDS_Face& aFace = TopoDS::Face (anEdgeIter.Value().First());
387 Handle(Poly_Triangulation) aTriangulation = BRep_Tool::Triangulation (aFace, aTrsf);
388 if (aTriangulation.IsNull())
393 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgeIter.Key());
394 if (theUpperContinuity < GeomAbs_CN
395 && anEdgeIter.Value().Extent() >= 2
396 && BRep_Tool::MaxContinuity (anEdge) > theUpperContinuity)
401 Handle(Poly_PolygonOnTriangulation) anEdgePoly = BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
402 if (anEdgePoly.IsNull()
403 || anEdgePoly->Nodes().Length () < 2)
408 // get edge nodes indexes from face triangulation
409 const TColgp_Array1OfPnt& aTriNodes = aTriangulation->Nodes();
410 const TColStd_Array1OfInteger& anEdgeNodes = anEdgePoly->Nodes();
412 // collect the edge nodes
413 Standard_Integer aSegmentEdge = aSegments->VertexNumber() + 1;
414 for (Standard_Integer aNodeIdx = anEdgeNodes.Lower(); aNodeIdx <= anEdgeNodes.Upper(); ++aNodeIdx)
416 // node index in face triangulation
417 // get node and apply location transformation to the node
418 const Standard_Integer aTriIndex = anEdgeNodes.Value (aNodeIdx);
419 gp_Pnt aTriNode = aTriNodes.Value (aTriIndex);
420 if (!aTrsf.IsIdentity())
422 aTriNode.Transform (aTrsf);
425 aSegments->AddVertex (aTriNode);
426 if (aNodeIdx != anEdgeNodes.Lower())
428 aSegments->AddEdge ( aSegmentEdge);
429 aSegments->AddEdge (++aSegmentEdge);
435 Standard_Integer aSegmentEdge = aSegments->VertexNumber();
436 const Standard_Integer aNbVertices = aSeqPntsExtra.Size();
437 for (Standard_Integer aPntIter = 1; aPntIter <= aNbVertices; aPntIter += 2)
439 aSegments->AddVertex (aSeqPntsExtra.Value (aPntIter));
440 aSegments->AddEdge (++aSegmentEdge);
441 aSegments->AddVertex (aSeqPntsExtra.Value (aPntIter + 1));
442 aSegments->AddEdge (++aSegmentEdge);
449 } // anonymous namespace
451 // =======================================================================
452 // function : ExploreSolids
454 // =======================================================================
455 void StdPrs_ShadedShape::ExploreSolids (const TopoDS_Shape& theShape,
456 const BRep_Builder& theBuilder,
457 TopoDS_Compound& theClosed,
458 TopoDS_Compound& theOpened,
459 const Standard_Boolean theIgnore1DSubShape)
461 if (theShape.IsNull())
466 switch (theShape.ShapeType())
468 case TopAbs_COMPOUND:
469 case TopAbs_COMPSOLID:
471 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
473 ExploreSolids (anIter.Value(), theBuilder, theClosed, theOpened, theIgnore1DSubShape);
479 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
481 const TopoDS_Shape& aSubShape = anIter.Value();
482 const Standard_Boolean isClosed = aSubShape.ShapeType() == TopAbs_SHELL &&
483 BRep_Tool::IsClosed (aSubShape) &&
484 StdPrs_ToolTriangulatedShape::IsTriangulated (aSubShape);
485 theBuilder.Add (isClosed ? theClosed : theOpened, aSubShape);
492 theBuilder.Add (theOpened, theShape);
499 if (!theIgnore1DSubShape)
501 theBuilder.Add (theOpened, theShape);
511 // =======================================================================
514 // =======================================================================
515 void StdPrs_ShadedShape::Add (const Handle(Prs3d_Presentation)& thePrs,
516 const TopoDS_Shape& theShape,
517 const Handle(Prs3d_Drawer)& theDrawer,
518 const StdPrs_Volume theVolume)
521 StdPrs_ShadedShape::Add (thePrs, theShape, theDrawer,
522 Standard_False, aDummy, aDummy, aDummy, theVolume);
525 // =======================================================================
528 // =======================================================================
529 void StdPrs_ShadedShape::Add (const Handle (Prs3d_Presentation)& thePrs,
530 const TopoDS_Shape& theShape,
531 const Handle (Prs3d_Drawer)& theDrawer,
532 const Standard_Boolean theHasTexels,
533 const gp_Pnt2d& theUVOrigin,
534 const gp_Pnt2d& theUVRepeat,
535 const gp_Pnt2d& theUVScale,
536 const StdPrs_Volume theVolume)
538 if (theShape.IsNull())
543 // add wireframe presentation for isolated edges and vertices
544 wireframeFromShape (thePrs, theShape, theDrawer);
546 // Use automatic re-triangulation with deflection-check logic only if this feature is enable
547 if (theDrawer->IsAutoTriangulation())
549 // Triangulation completeness is important for "open-closed" analysis - perform tessellation beforehand
550 StdPrs_ToolTriangulatedShape::Tessellate (theShape, theDrawer);
553 // add special wireframe presentation for faces without triangulation
554 wireframeNoTriangFacesFromShape (thePrs, theShape, theDrawer);
556 // The shape types listed below need advanced analysis as potentially containing
557 // both closed and open parts. Solids are also included, because they might
558 // contain non-manifold parts inside (internal open shells)
559 if ((theShape.ShapeType() == TopAbs_COMPOUND
560 || theShape.ShapeType() == TopAbs_COMPSOLID
561 || theShape.ShapeType() == TopAbs_SOLID)
562 && theVolume == StdPrs_Volume_Autodetection)
564 // collect two compounds: for opened and closed (solid) sub-shapes
565 TopoDS_Compound anOpened, aClosed;
566 BRep_Builder aBuilder;
567 aBuilder.MakeCompound (aClosed);
568 aBuilder.MakeCompound (anOpened);
569 ExploreSolids (theShape, aBuilder, aClosed, anOpened, Standard_True);
571 if (aClosed.NbChildren() > 0)
573 shadeFromShape (aClosed, thePrs, theDrawer,
574 theHasTexels, theUVOrigin, theUVRepeat, theUVScale, true);
577 if (anOpened.NbChildren() > 0)
579 shadeFromShape (anOpened, thePrs, theDrawer,
580 theHasTexels, theUVOrigin, theUVRepeat, theUVScale, false);
585 // if the shape type is not compound, composolid or solid, use autodetection back-facing filled
586 shadeFromShape (theShape, thePrs, theDrawer,
587 theHasTexels, theUVOrigin, theUVRepeat, theUVScale,
588 theVolume == StdPrs_Volume_Closed);
591 if (theDrawer->FaceBoundaryDraw())
593 if (Handle(Graphic3d_ArrayOfSegments) aBndSegments = fillFaceBoundaries (theShape, theDrawer->FaceBoundaryUpperContinuity()))
595 Handle(Graphic3d_Group) aPrsGrp = thePrs->NewGroup();
596 aPrsGrp->SetGroupPrimitivesAspect (theDrawer->FaceBoundaryAspect()->Aspect());
597 aPrsGrp->AddPrimitiveArray (aBndSegments);
602 // =======================================================================
603 // function : FillTriangles
605 // =======================================================================
606 Handle(Graphic3d_ArrayOfTriangles) StdPrs_ShadedShape::FillTriangles (const TopoDS_Shape& theShape,
607 const Standard_Boolean theHasTexels,
608 const gp_Pnt2d& theUVOrigin,
609 const gp_Pnt2d& theUVRepeat,
610 const gp_Pnt2d& theUVScale)
612 return fillTriangles (theShape, theHasTexels, theUVOrigin, theUVRepeat, theUVScale);
615 // =======================================================================
616 // function : FillFaceBoundaries
618 // =======================================================================
619 Handle(Graphic3d_ArrayOfSegments) StdPrs_ShadedShape::FillFaceBoundaries (const TopoDS_Shape& theShape,
620 GeomAbs_Shape theUpperContinuity)
622 return fillFaceBoundaries (theShape, theUpperContinuity);
625 // =======================================================================
626 // function : AddWireframeForFreeElements
628 // =======================================================================
629 void StdPrs_ShadedShape::AddWireframeForFreeElements (const Handle (Prs3d_Presentation)& thePrs,
630 const TopoDS_Shape& theShape,
631 const Handle (Prs3d_Drawer)& theDrawer)
633 wireframeFromShape (thePrs, theShape, theDrawer);
636 // =======================================================================
637 // function : AddWireframeForFacesWithoutTriangles
639 // =======================================================================
640 void StdPrs_ShadedShape::AddWireframeForFacesWithoutTriangles (const Handle(Prs3d_Presentation)& thePrs,
641 const TopoDS_Shape& theShape,
642 const Handle(Prs3d_Drawer)& theDrawer)
644 wireframeNoTriangFacesFromShape (thePrs, theShape, theDrawer);