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_ArrayOfTriangles.hxx>
26 #include <Graphic3d_AspectFillArea3d.hxx>
27 #include <Graphic3d_Group.hxx>
31 #include <Precision.hxx>
32 #include <Prs3d_Drawer.hxx>
33 #include <Prs3d_Presentation.hxx>
34 #include <Prs3d_ShadingAspect.hxx>
35 #include <Poly_Connect.hxx>
36 #include <Poly_Triangulation.hxx>
37 #include <StdPrs_ToolShadedShape.hxx>
38 #include <StdPrs_WFShape.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TColgp_Array1OfDir.hxx>
42 #include <TColgp_Array1OfPnt2d.hxx>
43 #include <TopoDS_Compound.hxx>
44 #include <Poly_PolygonOnTriangulation.hxx>
46 #include <TopTools_ListOfShape.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <NCollection_List.hxx>
49 #include <Graphic3d_ArrayOfSegments.hxx>
50 #include <Prs3d_LineAspect.hxx>
51 #include <TColgp_HArray1OfPnt.hxx>
52 #include <Aspect_PolygonOffsetMode.hxx>
54 #define MAX2(X, Y) (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
55 #define MAX3(X, Y, Z) (MAX2 (MAX2 (X, Y), Z))
59 // =======================================================================
60 // function : GetDeflection
62 // =======================================================================
63 static Standard_Real GetDeflection (const TopoDS_Shape& theShape,
64 const Handle(Prs3d_Drawer)& theDrawer)
66 Standard_Real aDeflection = theDrawer->MaximalChordialDeviation();
67 if (theDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE)
70 BRepBndLib::Add (theShape, aBndBox, Standard_False);
71 if (!aBndBox.IsVoid())
73 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
74 aBndBox.Get (aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
75 aDeflection = MAX3 (aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * theDrawer->DeviationCoefficient() * 4.0;
81 // =======================================================================
82 // function : ShadeFromShape
84 // =======================================================================
85 static Standard_Boolean ShadeFromShape (const TopoDS_Shape& theShape,
86 const Handle (Prs3d_Presentation)& thePresentation,
87 const Handle (Prs3d_Drawer)& theDrawer,
88 const Standard_Boolean theHasTexels,
89 const gp_Pnt2d& theUVOrigin,
90 const gp_Pnt2d& theUVRepeat,
91 const gp_Pnt2d& theUVScale)
93 StdPrs_ToolShadedShape SST;
94 Handle(Poly_Triangulation) T;
97 Standard_Integer decal;
98 Standard_Integer t[3], n[3];
99 Standard_Integer nbTriangles = 0, nbVertices = 0;
100 Standard_Real aUmin (0.0), aUmax (0.0), aVmin (0.0), aVmax (0.0), dUmax (0.0), dVmax (0.0);
102 // precision for compare square distances
103 const Standard_Real aPreci = Precision::SquareConfusion();
105 if (!theDrawer->ShadingAspectGlobal())
107 Handle(Graphic3d_AspectFillArea3d) anAsp = theDrawer->ShadingAspect()->Aspect();
108 if (StdPrs_ToolShadedShape::IsClosed (theShape))
110 anAsp->SuppressBackFace();
114 anAsp->AllowBackFace();
116 Prs3d_Root::CurrentGroup (thePresentation)->SetGroupPrimitivesAspect (anAsp);
119 for (SST.Init (theShape); SST.MoreFace(); SST.NextFace())
121 const TopoDS_Face& aFace = SST.CurrentFace();
122 T = SST.Triangulation (aFace, aLoc);
125 nbTriangles += T->NbTriangles();
126 nbVertices += T->NbNodes();
132 return Standard_False;
135 Handle(Graphic3d_ArrayOfTriangles) aPArray
136 = new Graphic3d_ArrayOfTriangles (nbVertices, 3 * nbTriangles,
137 Standard_True, Standard_False, theHasTexels, Standard_True);
138 for (SST.Init (theShape); SST.MoreFace(); SST.NextFace())
140 const TopoDS_Face& aFace = SST.CurrentFace();
141 T = SST.Triangulation (aFace, aLoc);
146 const gp_Trsf& aTrsf = aLoc.Transformation();
148 // Extracts vertices & normals from nodes
149 const TColgp_Array1OfPnt& aNodes = T->Nodes();
150 const TColgp_Array1OfPnt2d& aUVNodes = T->UVNodes();
151 TColgp_Array1OfDir aNormals (aNodes.Lower(), aNodes.Upper());
152 SST.Normal (aFace, pc, aNormals);
156 BRepTools::UVBounds (aFace, aUmin, aUmax, aVmin, aVmax);
157 dUmax = (aUmax - aUmin);
158 dVmax = (aVmax - aVmin);
161 decal = aPArray->VertexNumber();
162 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
164 p = aNodes (aNodeIter);
165 if (!aLoc.IsIdentity())
168 aNormals (aNodeIter).Transform (aTrsf);
171 if (theHasTexels && aUVNodes.Upper() == aNodes.Upper())
173 const gp_Pnt2d aTexel = gp_Pnt2d ((-theUVOrigin.X() + (theUVRepeat.X() * (aUVNodes (aNodeIter).X() - aUmin)) / dUmax) / theUVScale.X(),
174 (-theUVOrigin.Y() + (theUVRepeat.Y() * (aUVNodes (aNodeIter).Y() - aVmin)) / dVmax) / theUVScale.Y());
175 aPArray->AddVertex (p, aNormals (aNodeIter), aTexel);
179 aPArray->AddVertex (p, aNormals (aNodeIter));
183 // Fill array with vertex and edge visibility info
184 const Poly_Array1OfTriangle& aTriangles = T->Triangles();
185 for (Standard_Integer aTriIter = 1; aTriIter <= T->NbTriangles(); ++aTriIter)
187 pc.Triangles (aTriIter, t[0], t[1], t[2]);
188 if (SST.Orientation (aFace) == TopAbs_REVERSED)
189 aTriangles (aTriIter).Get (n[0], n[2], n[1]);
191 aTriangles (aTriIter).Get (n[0], n[1], n[2]);
193 gp_Pnt P1 = aNodes (n[0]);
194 gp_Pnt P2 = aNodes (n[1]);
195 gp_Pnt P3 = aNodes (n[2]);
198 if (V1.SquareMagnitude() <= aPreci)
203 if (V2.SquareMagnitude() <= aPreci)
208 if (V3.SquareMagnitude() <= aPreci)
215 if (V1.SquareMagnitude() > aPreci)
217 aPArray->AddEdge (n[0] + decal, t[0] == 0);
218 aPArray->AddEdge (n[1] + decal, t[1] == 0);
219 aPArray->AddEdge (n[2] + decal, t[2] == 0);
223 Prs3d_Root::CurrentGroup (thePresentation)->AddPrimitiveArray (aPArray);
225 return Standard_True;
228 // =======================================================================
229 // function : ComputeFaceBoundaries
230 // purpose : Compute boundary presentation for faces of the shape.
231 // =======================================================================
232 static void ComputeFaceBoundaries (const TopoDS_Shape& theShape,
233 const Handle (Prs3d_Presentation)& thePresentation,
234 const Handle (Prs3d_Drawer)& theDrawer)
236 // collection of all triangulation nodes on edges
237 // for computing boundaries presentation
238 NCollection_List<Handle(TColgp_HArray1OfPnt)> aNodeCollection;
239 Standard_Integer aNodeNumber = 0;
241 TopLoc_Location aTrsf;
243 // explore all boundary edges
244 TopTools_IndexedDataMapOfShapeListOfShape anEdgesMap;
245 TopExp::MapShapesAndAncestors (
246 theShape, TopAbs_EDGE, TopAbs_FACE, anEdgesMap);
248 Standard_Integer anEdgeIdx = 1;
249 for ( ; anEdgeIdx <= anEdgesMap.Extent (); anEdgeIdx++)
252 const TopTools_ListOfShape& aFaceList = anEdgesMap.FindFromIndex (anEdgeIdx);
253 if (aFaceList.Extent() == 0)
256 // take one of the shared edges and get edge triangulation
257 const TopoDS_Face& aFace = TopoDS::Face (aFaceList.First ());
258 const TopoDS_Edge& anEdge = TopoDS::Edge (anEdgesMap.FindKey (anEdgeIdx));
260 Handle(Poly_Triangulation) aTriangulation =
261 BRep_Tool::Triangulation (aFace, aTrsf);
263 if (aTriangulation.IsNull ())
266 Handle(Poly_PolygonOnTriangulation) anEdgePoly =
267 BRep_Tool::PolygonOnTriangulation (anEdge, aTriangulation, aTrsf);
269 if (anEdgePoly.IsNull ())
272 // get edge nodes indexes from face triangulation
273 const TColgp_Array1OfPnt& aTriNodes = aTriangulation->Nodes ();
274 const TColStd_Array1OfInteger& anEdgeNodes = anEdgePoly->Nodes ();
276 if (anEdgeNodes.Length () < 2)
279 // collect the edge nodes
280 Handle(TColgp_HArray1OfPnt) aCollected =
281 new TColgp_HArray1OfPnt (anEdgeNodes.Lower (), anEdgeNodes.Upper ());
283 Standard_Integer aNodeIdx = anEdgeNodes.Lower ();
284 for ( ; aNodeIdx <= anEdgeNodes.Upper (); aNodeIdx++)
286 // node index in face triangulation
287 Standard_Integer aTriIndex = anEdgeNodes.Value (aNodeIdx);
289 // get node and apply location transformation to the node
290 gp_Pnt aTriNode = aTriNodes.Value (aTriIndex);
291 if (!aTrsf.IsIdentity ())
292 aTriNode.Transform (aTrsf);
294 // add node to the boundary array
295 aCollected->SetValue (aNodeIdx, aTriNode);
298 aNodeNumber += anEdgeNodes.Length ();
299 aNodeCollection.Append (aCollected);
302 // check if it possible to continue building the presentation
303 if (aNodeNumber == 0)
306 // allocate polyline array for presentation
307 Standard_Integer aSegmentEdgeNb =
308 (aNodeNumber - aNodeCollection.Extent()) * 2;
310 Handle(Graphic3d_ArrayOfSegments) aSegments =
311 new Graphic3d_ArrayOfSegments (aNodeNumber, aSegmentEdgeNb);
313 // build presentation for edge bondaries
314 NCollection_List<Handle(TColgp_HArray1OfPnt)>::Iterator
315 aCollIt (aNodeCollection);
317 // the edge index is increased in each iteration step to
318 // avoid contiguous segments between different face edges.
319 for ( ; aCollIt.More(); aCollIt.Next () )
321 const Handle(TColgp_HArray1OfPnt)& aNodeArray = aCollIt.Value ();
323 Standard_Integer aNodeIdx = aNodeArray->Lower ();
325 // add first node (this node is not shared with previous segment).
326 // for each face edge, indices for sharing nodes
327 // between segments begin from the first added node.
328 Standard_Integer aSegmentEdge =
329 aSegments->AddVertex (aNodeArray->Value (aNodeIdx));
331 // add subsequent nodes and provide edge indexes for sharing
332 // the nodes between the sequential segments.
333 for ( aNodeIdx++; aNodeIdx <= aNodeArray->Upper (); aNodeIdx++ )
335 aSegments->AddVertex (aNodeArray->Value (aNodeIdx));
336 aSegments->AddEdge ( aSegmentEdge);
337 aSegments->AddEdge (++aSegmentEdge);
341 // set up aspect and add polyline data
342 Handle(Graphic3d_AspectLine3d) aBoundaryAspect =
343 theDrawer->FaceBoundaryAspect ()->Aspect ();
345 Handle(Graphic3d_Group) aPrsGrp = Prs3d_Root::CurrentGroup (thePresentation);
346 aPrsGrp->SetGroupPrimitivesAspect (aBoundaryAspect);
347 aPrsGrp->AddPrimitiveArray (aSegments);
351 // =======================================================================
354 // =======================================================================
355 void StdPrs_ShadedShape::Add (const Handle(Prs3d_Presentation)& thePresentation,
356 const TopoDS_Shape& theShape,
357 const Handle(Prs3d_Drawer)& theDrawer)
360 StdPrs_ShadedShape::Add (thePresentation, theShape, theDrawer,
361 Standard_False, aDummy, aDummy, aDummy);
364 // =======================================================================
365 // function : Tessellate
367 // =======================================================================
368 void StdPrs_ShadedShape::Tessellate (const TopoDS_Shape& theShape,
369 const Handle (Prs3d_Drawer)& theDrawer)
371 // Check if it is possible to avoid unnecessary recomputation
372 // of shape triangulation
373 Standard_Real aDeflection = GetDeflection (theShape, theDrawer);
374 if (BRepTools::Triangulation (theShape, aDeflection))
379 // retrieve meshing tool from Factory
380 BRepTools::Clean (theShape);
381 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (theShape,
383 theDrawer->HLRAngle());
384 if (!aMeshAlgo.IsNull())
386 aMeshAlgo->Perform();
390 // =======================================================================
393 // =======================================================================
394 void StdPrs_ShadedShape::Add (const Handle (Prs3d_Presentation)& thePresentation,
395 const TopoDS_Shape& theShape,
396 const Handle (Prs3d_Drawer)& theDrawer,
397 const Standard_Boolean theHasTexels,
398 const gp_Pnt2d& theUVOrigin,
399 const gp_Pnt2d& theUVRepeat,
400 const gp_Pnt2d& theUVScale)
402 if (theShape.IsNull())
407 if (theShape.ShapeType() == TopAbs_COMPOUND)
410 ex.Init (theShape, TopAbs_FACE);
414 BRep_Builder aBuilder;
415 aBuilder.MakeCompound (CO);
416 Standard_Boolean hasElement = Standard_False;
418 // il faut presenter les edges isoles.
419 for (ex.Init (theShape, TopAbs_EDGE, TopAbs_FACE); ex.More(); ex.Next())
421 hasElement = Standard_True;
422 aBuilder.Add (CO, ex.Current());
424 // il faut presenter les vertex isoles.
425 for (ex.Init (theShape, TopAbs_VERTEX, TopAbs_EDGE); ex.More(); ex.Next())
427 hasElement = Standard_True;
428 aBuilder.Add (CO, ex.Current());
432 StdPrs_WFShape::Add (thePresentation, CO, theDrawer);
437 StdPrs_WFShape::Add (thePresentation, theShape, theDrawer);
441 Tessellate (theShape, theDrawer);
442 ShadeFromShape (theShape, thePresentation, theDrawer,
443 theHasTexels, theUVOrigin, theUVRepeat, theUVScale);
445 if (theDrawer->IsFaceBoundaryDraw ())
447 ComputeFaceBoundaries (theShape, thePresentation, theDrawer);