1 // Created on: 1993-10-27
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_ToolTriangulatedShape.hxx>
19 #include <BRepMesh_DiscretFactory.hxx>
20 #include <BRepMesh_DiscretRoot.hxx>
21 #include <BRepTools.hxx>
22 #include <BRep_Tool.hxx>
23 #include <GeomAbs_SurfaceType.hxx>
24 #include <GeomLib.hxx>
26 #include <Poly_Connect.hxx>
27 #include <Poly_Triangulation.hxx>
28 #include <Precision.hxx>
30 #include <Prs3d_Drawer.hxx>
31 #include <TColgp_Array1OfPnt.hxx>
32 #include <TColgp_Array1OfPnt2d.hxx>
33 #include <TopAbs_Orientation.hxx>
34 #include <TopLoc_Location.hxx>
35 #include <TShort_HArray1OfShortReal.hxx>
36 #include <TShort_Array1OfShortReal.hxx>
37 #include <TopExp_Explorer.hxx>
39 #include <TopoDS_Face.hxx>
41 //=======================================================================
42 //function : IsTriangulated
44 //=======================================================================
45 Standard_Boolean StdPrs_ToolTriangulatedShape::IsTriangulated (const TopoDS_Shape& theShape)
47 TopLoc_Location aLocDummy;
48 for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
50 const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
51 const Handle(Poly_Triangulation)& aTri = BRep_Tool::Triangulation (aFace, aLocDummy);
54 return Standard_False;
60 //=======================================================================
63 //=======================================================================
64 Standard_Boolean StdPrs_ToolTriangulatedShape::IsClosed (const TopoDS_Shape& theShape)
66 if (theShape.IsNull())
71 switch (theShape.ShapeType())
74 case TopAbs_COMPSOLID:
77 // check that compound consists of closed solids
78 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
80 const TopoDS_Shape& aShape = anIter.Value();
81 if (!IsClosed (aShape))
83 return Standard_False;
90 // Check for non-manifold topology first of all:
91 // have to use BRep_Tool::IsClosed() because it checks the face connectivity
93 if (!BRep_Tool::IsClosed (theShape))
94 return Standard_False;
96 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
98 const TopoDS_Shape& aShape = anIter.Value();
104 if (aShape.ShapeType() == TopAbs_FACE)
107 return Standard_False;
109 else if (!IsTriangulated (aShape))
111 // mesh contains holes
112 return Standard_False;
115 return Standard_True;
120 // free faces / shell are not allowed
121 return Standard_False;
128 return Standard_True;
133 //=======================================================================
136 //=======================================================================
137 void StdPrs_ToolTriangulatedShape::Normal (const TopoDS_Face& theFace,
138 Poly_Connect& thePolyConnect,
139 TColgp_Array1OfDir& theNormals)
141 const Handle(Poly_Triangulation)& aPolyTri = thePolyConnect.Triangulation();
142 const TColgp_Array1OfPnt& aNodes = aPolyTri->Nodes();
143 if (aPolyTri->HasNormals())
145 // normals pre-computed in triangulation structure
146 const TShort_Array1OfShortReal& aNormals = aPolyTri->Normals();
147 const Standard_ShortReal* aNormArr = &(aNormals.Value (aNormals.Lower()));
148 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
150 const Standard_Integer anId = 3 * (aNodeIter - aNodes.Lower());
151 const gp_Dir aNorm (aNormArr[anId + 0],
154 theNormals (aNodeIter) = aNorm;
157 if (theFace.Orientation() == TopAbs_REVERSED)
159 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
161 theNormals.ChangeValue (aNodeIter).Reverse();
167 // take in face the surface location
168 const TopoDS_Face aZeroFace = TopoDS::Face (theFace.Located (TopLoc_Location()));
169 Handle(Geom_Surface) aSurf = BRep_Tool::Surface (aZeroFace);
170 const Standard_Real aTol = Precision::Confusion();
171 Handle(TShort_HArray1OfShortReal) aNormals = new TShort_HArray1OfShortReal (1, aPolyTri->NbNodes() * 3);
172 const Poly_Array1OfTriangle& aTriangles = aPolyTri->Triangles();
173 const TColgp_Array1OfPnt2d* aNodesUV = aPolyTri->HasUVNodes() && !aSurf.IsNull()
174 ? &aPolyTri->UVNodes()
176 Standard_Integer aTri[3];
177 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
179 // try to retrieve normal from real surface first, when UV coordinates are available
181 || GeomLib::NormEstim (aSurf, aNodesUV->Value (aNodeIter), aTol, theNormals (aNodeIter)) > 1)
183 // compute flat normals
184 gp_XYZ eqPlan (0.0, 0.0, 0.0);
185 for (thePolyConnect.Initialize (aNodeIter); thePolyConnect.More(); thePolyConnect.Next())
187 aTriangles (thePolyConnect.Value()).Get (aTri[0], aTri[1], aTri[2]);
188 const gp_XYZ v1 (aNodes (aTri[1]).Coord() - aNodes (aTri[0]).Coord());
189 const gp_XYZ v2 (aNodes (aTri[2]).Coord() - aNodes (aTri[1]).Coord());
190 const gp_XYZ vv = v1 ^ v2;
191 const Standard_Real aMod = vv.Modulus();
197 const Standard_Real aModMax = eqPlan.Modulus();
198 theNormals (aNodeIter) = (aModMax > aTol) ? gp_Dir (eqPlan) : gp::DZ();
201 const Standard_Integer anId = (aNodeIter - aNodes.Lower()) * 3;
202 aNormals->SetValue (anId + 1, (Standard_ShortReal )theNormals (aNodeIter).X());
203 aNormals->SetValue (anId + 2, (Standard_ShortReal )theNormals (aNodeIter).Y());
204 aNormals->SetValue (anId + 3, (Standard_ShortReal )theNormals (aNodeIter).Z());
206 aPolyTri->SetNormals (aNormals);
208 if (theFace.Orientation() == TopAbs_REVERSED)
210 for (Standard_Integer aNodeIter = aNodes.Lower(); aNodeIter <= aNodes.Upper(); ++aNodeIter)
212 theNormals.ChangeValue (aNodeIter).Reverse();
217 //=======================================================================
218 //function : IsTessellated
220 //=======================================================================
221 Standard_Boolean StdPrs_ToolTriangulatedShape::IsTessellated (const TopoDS_Shape& theShape,
222 const Handle(Prs3d_Drawer)& theDrawer)
224 return BRepTools::Triangulation (theShape, Prs3d::GetDeflection (theShape, theDrawer));
227 // =======================================================================
228 // function : Tessellate
230 // =======================================================================
231 Standard_Boolean StdPrs_ToolTriangulatedShape::Tessellate (const TopoDS_Shape& theShape,
232 const Handle(Prs3d_Drawer)& theDrawer)
234 Standard_Boolean wasRecomputed = Standard_False;
235 // Check if it is possible to avoid unnecessary recomputation of shape triangulation
236 if (IsTessellated (theShape, theDrawer))
238 return wasRecomputed;
241 Standard_Real aDeflection = Prs3d::GetDeflection (theShape, theDrawer);
243 // retrieve meshing tool from Factory
244 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (theShape,
246 theDrawer->HLRAngle());
247 if (!aMeshAlgo.IsNull())
249 aMeshAlgo->Perform();
250 wasRecomputed = Standard_True;
253 return wasRecomputed;
256 // =======================================================================
257 // function : ClearOnOwnDeflectionChange
259 // =======================================================================
260 void StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (const TopoDS_Shape& theShape,
261 const Handle(Prs3d_Drawer)& theDrawer,
262 const Standard_Boolean theToResetCoeff)
264 if (!theDrawer->IsAutoTriangulation()
265 || theShape.IsNull())
270 const Standard_Boolean isOwnDeviationAngle = theDrawer->HasOwnDeviationAngle();
271 const Standard_Boolean isOwnDeviationCoefficient = theDrawer->HasOwnDeviationCoefficient();
272 const Standard_Real anAngleNew = theDrawer->DeviationAngle();
273 const Standard_Real anAnglePrev = theDrawer->PreviousDeviationAngle();
274 const Standard_Real aCoeffNew = theDrawer->DeviationCoefficient();
275 const Standard_Real aCoeffPrev = theDrawer->PreviousDeviationCoefficient();
276 if ((!isOwnDeviationAngle || Abs (anAngleNew - anAnglePrev) <= Precision::Angular())
277 && (!isOwnDeviationCoefficient || Abs (aCoeffNew - aCoeffPrev) <= Precision::Confusion()))
282 BRepTools::Clean (theShape);
285 theDrawer->UpdatePreviousDeviationAngle();
286 theDrawer->UpdatePreviousDeviationCoefficient();