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 <BRep_Builder.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRepMesh_DiscretFactory.hxx>
23 #include <BRepMesh_DiscretRoot.hxx>
24 #include <BRepTools.hxx>
25 #include <Graphic3d_ArrayOfSegments.hxx>
26 #include <Graphic3d_ArrayOfTriangles.hxx>
27 #include <Graphic3d_AspectFillArea3d.hxx>
28 #include <Graphic3d_Group.hxx>
32 #include <NCollection_List.hxx>
33 #include <Precision.hxx>
35 #include <Prs3d_Drawer.hxx>
36 #include <Prs3d_IsoAspect.hxx>
37 #include <Prs3d_LineAspect.hxx>
38 #include <Prs3d_Presentation.hxx>
39 #include <Prs3d_ShadingAspect.hxx>
40 #include <Poly_Connect.hxx>
41 #include <Poly_PolygonOnTriangulation.hxx>
42 #include <Poly_Triangulation.hxx>
43 #include <StdPrs_ToolShadedShape.hxx>
44 #include <StdPrs_WFDeflectionShape.hxx>
45 #include <StdPrs_WFShape.hxx>
47 #include <TopExp_Explorer.hxx>
49 #include <TopoDS_Compound.hxx>
50 #include <TopoDS_Face.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TColgp_Array1OfDir.hxx>
53 #include <TColgp_Array1OfPnt2d.hxx>
54 #include <TColgp_HArray1OfPnt.hxx>
55 #include <TopTools_ListOfShape.hxx>
56 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
61 //! Computes wireframe presentation for free wires and vertices
62 void wireframeFromShape (const Handle (Prs3d_Presentation)& thePrs,
63 const TopoDS_Shape& theShape,
64 const Handle (Prs3d_Drawer)& theDrawer)
66 Standard_Boolean aDrawAllVerticesFlag = (theDrawer->VertexDrawMode() == Prs3d_VDM_All);
68 if (!aDrawAllVerticesFlag && theShape.ShapeType() != TopAbs_COMPOUND)
73 TopExp_Explorer aShapeIter (theShape, TopAbs_FACE);
74 if (!aShapeIter.More())
76 // compound contains no shaded elements at all
77 StdPrs_WFShape::Add (thePrs, theShape, theDrawer);
81 // We have to create a compound and collect all subshapes not drawn by the shading algo.
84 // - isolated vertices, if aDrawAllVerticesFlag == Standard_False
85 // - all shape's vertices, if aDrawAllVerticesFlag == Standard_True
86 TopoDS_Compound aCompoundWF;
87 BRep_Builder aBuilder;
88 aBuilder.MakeCompound (aCompoundWF);
89 Standard_Boolean hasElement = Standard_False;
92 for (aShapeIter.Init (theShape, TopAbs_EDGE, TopAbs_FACE); aShapeIter.More(); aShapeIter.Next())
94 hasElement = Standard_True;
95 aBuilder.Add (aCompoundWF, aShapeIter.Current());
97 // isolated or all vertices
98 aShapeIter.Init (theShape, TopAbs_VERTEX, aDrawAllVerticesFlag ? TopAbs_SHAPE : TopAbs_EDGE);
99 for (; aShapeIter.More(); aShapeIter.Next())
101 hasElement = Standard_True;
102 aBuilder.Add (aCompoundWF, aShapeIter.Current());
106 StdPrs_WFShape::Add (thePrs, aCompoundWF, theDrawer);
110 //! Computes special wireframe presentation for faces without triangulation.
111 void wireframeNoTriangFacesFromShape (const Handle(Prs3d_Presentation)& thePrs,
112 const TopoDS_Shape& theShape,
113 const Handle(Prs3d_Drawer)& theDrawer)
115 TopoDS_Compound aCompoundWF;
116 BRep_Builder aBuilder;
117 aBuilder.MakeCompound (aCompoundWF);
118 TopLoc_Location aLoc;
119 Standard_Boolean hasElement = Standard_False;
121 for (TopExp_Explorer aShapeIter(theShape, TopAbs_FACE); aShapeIter.More(); aShapeIter.Next())
123 const TopoDS_Face& aFace = TopoDS::Face (aShapeIter.Current());
124 const Handle(Poly_Triangulation) aTriang = BRep_Tool::Triangulation (aFace, aLoc);
125 if (aTriang.IsNull())
127 hasElement = Standard_True;
128 aBuilder.Add (aCompoundWF, aFace);
134 Standard_Integer aPrevUIsoNb = theDrawer->UIsoAspect()->Number();
135 Standard_Integer aPrevVIsoNb = theDrawer->VIsoAspect()->Number();
136 theDrawer->UIsoAspect()->SetNumber (5);
137 theDrawer->VIsoAspect()->SetNumber (5);
139 StdPrs_WFDeflectionShape::Add (thePrs, aCompoundWF, theDrawer);
141 theDrawer->UIsoAspect()->SetNumber (aPrevUIsoNb);
142 theDrawer->VIsoAspect()->SetNumber (aPrevVIsoNb);
146 //! Gets triangulation of every face of shape and fills output array of triangles
147 static Handle(Graphic3d_ArrayOfTriangles) fillTriangles (const TopoDS_Shape& theShape,
148 const Standard_Boolean theHasTexels,
149 const gp_Pnt2d& theUVOrigin,
150 const gp_Pnt2d& theUVRepeat,
151 const gp_Pnt2d& theUVScale)
153 Handle(Poly_Triangulation) aT;
154 TopLoc_Location aLoc;
156 Standard_Integer aNbTriangles = 0;
157 Standard_Integer aNbVertices = 0;
159 // Precision for compare square distances
160 const Standard_Real aPreci = Precision::SquareConfusion();
162 TopExp_Explorer aFaceIt(theShape, TopAbs_FACE);
163 for (; aFaceIt.More(); aFaceIt.Next())
165 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
166 aT = StdPrs_ToolShadedShape::Triangulation (aFace, aLoc);
169 aNbTriangles += aT->NbTriangles();
170 aNbVertices += aT->NbNodes();
173 if (aNbVertices < 3 || aNbTriangles <= 0)
175 return Handle(Graphic3d_ArrayOfTriangles)();
178 Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (aNbVertices, 3 * aNbTriangles,
179 Standard_True, Standard_False, theHasTexels);
180 Standard_Real aUmin (0.0), aUmax (0.0), aVmin (0.0), aVmax (0.0), dUmax (0.0), dVmax (0.0);
181 for (aFaceIt.Init (theShape, TopAbs_FACE); aFaceIt.More(); aFaceIt.Next())
183 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Current());
184 aT = StdPrs_ToolShadedShape::Triangulation (aFace, aLoc);
189 const gp_Trsf& aTrsf = aLoc.Transformation();
191 // Determinant of transform matrix less then 0 means that mirror transform applied.
192 Standard_Boolean isMirrored = aTrsf.VectorialPart().Determinant() < 0;
194 Poly_Connect aPolyConnect (aT);
195 // Extracts vertices & normals from nodes
196 const TColgp_Array1OfPnt& aNodes = aT->Nodes();
197 const TColgp_Array1OfPnt2d& aUVNodes = aT->UVNodes();
198 TColgp_Array1OfDir aNormals (aNodes.Lower(), aNodes.Upper());
199 StdPrs_ToolShadedShape::Normal (aFace, aPolyConnect, aNormals);
203 BRepTools::UVBounds (aFace, aUmin, aUmax, aVmin, aVmax);
204 dUmax = (aUmax - aUmin);
205 dVmax = (aVmax - aVmin);
208 const Standard_Integer aDecal = anArray->VertexNumber();
209 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
211 aPoint = aNodes (aNodeIter);
212 if (!aLoc.IsIdentity())
214 aPoint.Transform (aTrsf);
216 aNormals (aNodeIter) = aNormals (aNodeIter).Transformed (aTrsf);
219 if (theHasTexels && aUVNodes.Upper() == aNodes.Upper())
221 const gp_Pnt2d aTexel = gp_Pnt2d ((-theUVOrigin.X() + (theUVRepeat.X() * (aUVNodes (aNodeIter).X() - aUmin)) / dUmax) / theUVScale.X(),
222 (-theUVOrigin.Y() + (theUVRepeat.Y() * (aUVNodes (aNodeIter).Y() - aVmin)) / dVmax) / theUVScale.Y());
223 anArray->AddVertex (aPoint, aNormals (aNodeIter), aTexel);
227 anArray->AddVertex (aPoint, aNormals (aNodeIter));
231 // Fill array with vertex and edge visibility info
232 const Poly_Array1OfTriangle& aTriangles = aT->Triangles();
233 Standard_Integer anIndex[3];
234 for (Standard_Integer aTriIter = 1; aTriIter <= aT->NbTriangles(); ++aTriIter)
236 if ((aFace.Orientation() == TopAbs_REVERSED) ^ isMirrored)
238 aTriangles (aTriIter).Get (anIndex[0], anIndex[2], anIndex[1]);
242 aTriangles (aTriIter).Get (anIndex[0], anIndex[1], anIndex[2]);
245 gp_Pnt aP1 = aNodes (anIndex[0]);
246 gp_Pnt aP2 = aNodes (anIndex[1]);
247 gp_Pnt aP3 = aNodes (anIndex[2]);
249 gp_Vec aV1 (aP1, aP2);
250 if (aV1.SquareMagnitude() <= aPreci)
254 gp_Vec aV2 (aP2, aP3);
255 if (aV2.SquareMagnitude() <= aPreci)
259 gp_Vec aV3 (aP3, aP1);
260 if (aV3.SquareMagnitude() <= aPreci)
267 if (aV1.SquareMagnitude() > aPreci)
269 anArray->AddEdge (anIndex[0] + aDecal);
270 anArray->AddEdge (anIndex[1] + aDecal);
271 anArray->AddEdge (anIndex[2] + aDecal);
278 //! Prepare shaded presentation for specified shape
279 static Standard_Boolean shadeFromShape (const TopoDS_Shape& theShape,
280 const Handle(Prs3d_Presentation)& thePrs,
281 const Handle(Prs3d_Drawer)& theDrawer,
282 const Standard_Boolean theHasTexels,
283 const gp_Pnt2d& theUVOrigin,
284 const gp_Pnt2d& theUVRepeat,
285 const gp_Pnt2d& theUVScale,
286 const Standard_Boolean theIsClosed)
288 Handle(Graphic3d_ArrayOfTriangles) aPArray = fillTriangles (theShape, theHasTexels, theUVOrigin, theUVRepeat, theUVScale);
289 if (aPArray.IsNull())
291 return Standard_False;
294 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup (thePrs);
295 aGroup->SetClosed (theIsClosed);
296 if (!theDrawer->ShadingAspectGlobal())
298 Handle(Graphic3d_AspectFillArea3d) anAsp = theDrawer->ShadingAspect()->Aspect();
299 theIsClosed ? anAsp->SuppressBackFace() : anAsp->AllowBackFace();
300 aGroup->SetGroupPrimitivesAspect (anAsp);
302 aGroup->AddPrimitiveArray (aPArray);
303 return Standard_True;
306 //! Compute boundary presentation for faces of the shape.
307 static void computeFaceBoundaries (const TopoDS_Shape& theShape,
308 const Handle(Prs3d_Presentation)& thePrs,
309 const Handle(Prs3d_Drawer)& theDrawer)
311 // collection of all triangulation nodes on edges
312 // for computing boundaries presentation
313 NCollection_List<Handle(TColgp_HArray1OfPnt)> aNodeCollection;
314 Standard_Integer aNodeNumber = 0;
316 TopLoc_Location aTrsf;
318 // explore all boundary edges
319 TopTools_IndexedDataMapOfShapeListOfShape anEdgesMap;
320 TopExp::MapShapesAndAncestors (
321 theShape, TopAbs_EDGE, TopAbs_FACE, anEdgesMap);
323 Standard_Integer anEdgeIdx = 1;
324 for ( ; anEdgeIdx <= anEdgesMap.Extent (); anEdgeIdx++)
327 const TopTools_ListOfShape& aFaceList = anEdgesMap.FindFromIndex (anEdgeIdx);
328 if (aFaceList.Extent() == 0)
331 // take one of the shared edges and get edge triangulation
332 const TopoDS_Face& aFace = TopoDS::Face (aFaceList.First ());
333 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgesMap.FindKey (anEdgeIdx));
335 Handle(Poly_Triangulation) aTriangulation =
336 BRep_Tool::Triangulation (aFace, aTrsf);
338 if (aTriangulation.IsNull ())
341 Handle(Poly_PolygonOnTriangulation) anEdgePoly =
342 BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
344 if (anEdgePoly.IsNull ())
347 // get edge nodes indexes from face triangulation
348 const TColgp_Array1OfPnt& aTriNodes = aTriangulation->Nodes ();
349 const TColStd_Array1OfInteger& anEdgeNodes = anEdgePoly->Nodes ();
351 if (anEdgeNodes.Length () < 2)
354 // collect the edge nodes
355 Handle(TColgp_HArray1OfPnt) aCollected =
356 new TColgp_HArray1OfPnt (anEdgeNodes.Lower (), anEdgeNodes.Upper ());
358 Standard_Integer aNodeIdx = anEdgeNodes.Lower ();
359 for ( ; aNodeIdx <= anEdgeNodes.Upper (); aNodeIdx++)
361 // node index in face triangulation
362 Standard_Integer aTriIndex = anEdgeNodes.Value (aNodeIdx);
364 // get node and apply location transformation to the node
365 gp_Pnt aTriNode = aTriNodes.Value (aTriIndex);
366 if (!aTrsf.IsIdentity ())
367 aTriNode.Transform (aTrsf);
369 // add node to the boundary array
370 aCollected->SetValue (aNodeIdx, aTriNode);
373 aNodeNumber += anEdgeNodes.Length ();
374 aNodeCollection.Append (aCollected);
377 // check if it possible to continue building the presentation
378 if (aNodeNumber == 0)
381 // allocate polyline array for presentation
382 Standard_Integer aSegmentEdgeNb =
383 (aNodeNumber - aNodeCollection.Extent()) * 2;
385 Handle(Graphic3d_ArrayOfSegments) aSegments =
386 new Graphic3d_ArrayOfSegments (aNodeNumber, aSegmentEdgeNb);
388 // build presentation for edge bondaries
389 NCollection_List<Handle(TColgp_HArray1OfPnt)>::Iterator
390 aCollIt (aNodeCollection);
392 // the edge index is increased in each iteration step to
393 // avoid contiguous segments between different face edges.
394 for ( ; aCollIt.More(); aCollIt.Next () )
396 const Handle(TColgp_HArray1OfPnt)& aNodeArray = aCollIt.Value ();
398 Standard_Integer aNodeIdx = aNodeArray->Lower ();
400 // add first node (this node is not shared with previous segment).
401 // for each face edge, indices for sharing nodes
402 // between segments begin from the first added node.
403 Standard_Integer aSegmentEdge =
404 aSegments->AddVertex (aNodeArray->Value (aNodeIdx));
406 // add subsequent nodes and provide edge indexes for sharing
407 // the nodes between the sequential segments.
408 for ( aNodeIdx++; aNodeIdx <= aNodeArray->Upper (); aNodeIdx++ )
410 aSegments->AddVertex (aNodeArray->Value (aNodeIdx));
411 aSegments->AddEdge ( aSegmentEdge);
412 aSegments->AddEdge (++aSegmentEdge);
416 // set up aspect and add polyline data
417 Handle(Graphic3d_AspectLine3d) aBoundaryAspect =
418 theDrawer->FaceBoundaryAspect ()->Aspect ();
420 Handle(Graphic3d_Group) aPrsGrp = Prs3d_Root::CurrentGroup (thePrs);
421 aPrsGrp->SetGroupPrimitivesAspect (aBoundaryAspect);
422 aPrsGrp->AddPrimitiveArray (aSegments);
426 // =======================================================================
427 // function : ExploreSolids
429 // =======================================================================
430 void StdPrs_ShadedShape::ExploreSolids (const TopoDS_Shape& theShape,
431 const BRep_Builder& theBuilder,
432 TopoDS_Compound& theClosed,
433 TopoDS_Compound& theOpened,
434 const Standard_Boolean theIgnore1DSubShape)
436 if (theShape.IsNull())
441 switch (theShape.ShapeType())
443 case TopAbs_COMPOUND:
444 case TopAbs_COMPSOLID:
446 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
448 ExploreSolids (anIter.Value(), theBuilder, theClosed, theOpened, theIgnore1DSubShape);
454 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
456 const TopoDS_Shape& aSubShape = anIter.Value();
457 const Standard_Boolean isClosed = aSubShape.ShapeType() == TopAbs_SHELL &&
458 BRep_Tool::IsClosed (aSubShape) &&
459 StdPrs_ToolShadedShape::IsTriangulated (aSubShape);
460 theBuilder.Add (isClosed ? theClosed : theOpened, aSubShape);
467 theBuilder.Add (theOpened, theShape);
474 if (!theIgnore1DSubShape)
476 theBuilder.Add (theOpened, theShape);
486 // =======================================================================
489 // =======================================================================
490 void StdPrs_ShadedShape::Add (const Handle(Prs3d_Presentation)& thePrs,
491 const TopoDS_Shape& theShape,
492 const Handle(Prs3d_Drawer)& theDrawer,
493 const StdPrs_Volume theVolume)
496 StdPrs_ShadedShape::Add (thePrs, theShape, theDrawer,
497 Standard_False, aDummy, aDummy, aDummy, theVolume);
500 // =======================================================================
501 // function : Tessellate
503 // =======================================================================
504 void StdPrs_ShadedShape::Tessellate (const TopoDS_Shape& theShape,
505 const Handle (Prs3d_Drawer)& theDrawer)
507 // Check if it is possible to avoid unnecessary recomputation of shape triangulation
508 Standard_Real aDeflection = Prs3d::GetDeflection (theShape, theDrawer);
509 if (BRepTools::Triangulation (theShape, aDeflection))
514 // retrieve meshing tool from Factory
515 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (theShape,
517 theDrawer->HLRAngle());
518 if (!aMeshAlgo.IsNull())
520 aMeshAlgo->Perform();
524 // =======================================================================
527 // =======================================================================
528 void StdPrs_ShadedShape::Add (const Handle (Prs3d_Presentation)& thePrs,
529 const TopoDS_Shape& theShape,
530 const Handle (Prs3d_Drawer)& theDrawer,
531 const Standard_Boolean theHasTexels,
532 const gp_Pnt2d& theUVOrigin,
533 const gp_Pnt2d& theUVRepeat,
534 const gp_Pnt2d& theUVScale,
535 const StdPrs_Volume theVolume)
537 if (theShape.IsNull())
542 // add wireframe presentation for isolated edges and vertices
543 wireframeFromShape (thePrs, theShape, theDrawer);
545 // Use automatic re-triangulation with deflection-check logic only if this feature is enable
546 if (theDrawer->IsAutoTriangulation())
548 // Triangulation completeness is important for "open-closed" analysis - perform tessellation beforehand
549 Tessellate (theShape, theDrawer);
552 // add special wireframe presentation for faces without triangulation
553 wireframeNoTriangFacesFromShape (thePrs, theShape, theDrawer);
555 // The shape types listed below need advanced analysis as potentially containing
556 // both closed and open parts. Solids are also included, because they might
557 // contain non-manifold parts inside (internal open shells)
558 if ((theShape.ShapeType() == TopAbs_COMPOUND
559 || theShape.ShapeType() == TopAbs_COMPSOLID
560 || theShape.ShapeType() == TopAbs_SOLID)
561 && theVolume == StdPrs_Volume_Autodetection)
563 // collect two compounds: for opened and closed (solid) sub-shapes
564 TopoDS_Compound anOpened, aClosed;
565 BRep_Builder aBuilder;
566 aBuilder.MakeCompound (aClosed);
567 aBuilder.MakeCompound (anOpened);
568 ExploreSolids (theShape, aBuilder, aClosed, anOpened, Standard_True);
570 TopoDS_Iterator aShapeIter (aClosed);
571 if (aShapeIter.More())
573 shadeFromShape (aClosed, thePrs, theDrawer,
574 theHasTexels, theUVOrigin, theUVRepeat, theUVScale, Standard_True);
577 aShapeIter.Initialize (anOpened);
578 if (aShapeIter.More())
580 shadeFromShape (anOpened, thePrs, theDrawer,
581 theHasTexels, theUVOrigin, theUVRepeat, theUVScale, Standard_False);
586 // if the shape type is not compound, composolid or solid, use autodetection back-facing filled
587 shadeFromShape (theShape, thePrs, theDrawer,
588 theHasTexels, theUVOrigin, theUVRepeat, theUVScale,
589 (theVolume == StdPrs_Volume_Closed ? Standard_True : Standard_False));
592 if (theDrawer->FaceBoundaryDraw())
594 computeFaceBoundaries (theShape, thePrs, theDrawer);