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
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <StdPrs_ToolTriangulatedShape.hxx>
18
19 #include <BRepBndLib.hxx>
20 #include <BRepMesh_DiscretFactory.hxx>
21 #include <BRepMesh_DiscretRoot.hxx>
22 #include <BRepTools.hxx>
23 #include <BRep_Tool.hxx>
24 #include <GeomAbs_SurfaceType.hxx>
25 #include <GeomLib.hxx>
26 #include <gp_XYZ.hxx>
27 #include <Poly.hxx>
28 #include <Poly_Connect.hxx>
29 #include <Poly_Triangulation.hxx>
30 #include <Precision.hxx>
31 #include <Prs3d.hxx>
32 #include <Prs3d_Drawer.hxx>
33 #include <TColgp_Array1OfPnt.hxx>
34 #include <TColgp_Array1OfPnt2d.hxx>
35 #include <TopAbs_Orientation.hxx>
36 #include <TopLoc_Location.hxx>
37 #include <TShort_HArray1OfShortReal.hxx>
38 #include <TShort_Array1OfShortReal.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Face.hxx>
42
43 //=======================================================================
44 //function : IsTriangulated
45 //purpose :
46 //=======================================================================
IsTriangulated(const TopoDS_Shape & theShape)47 Standard_Boolean StdPrs_ToolTriangulatedShape::IsTriangulated (const TopoDS_Shape& theShape)
48 {
49 TopLoc_Location aLocDummy;
50 for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
51 {
52 const TopoDS_Face& aFace = TopoDS::Face (aFaceIter.Current());
53 const Handle(Poly_Triangulation)& aTri = BRep_Tool::Triangulation (aFace, aLocDummy);
54 if (aTri.IsNull())
55 {
56 return Standard_False;
57 }
58 }
59 return Standard_True;
60 }
61
62 //=======================================================================
63 //function : IsClosed
64 //purpose :
65 //=======================================================================
IsClosed(const TopoDS_Shape & theShape)66 Standard_Boolean StdPrs_ToolTriangulatedShape::IsClosed (const TopoDS_Shape& theShape)
67 {
68 if (theShape.IsNull())
69 {
70 return Standard_True;
71 }
72
73 switch (theShape.ShapeType())
74 {
75 case TopAbs_COMPOUND:
76 case TopAbs_COMPSOLID:
77 default:
78 {
79 // check that compound consists of closed solids
80 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
81 {
82 const TopoDS_Shape& aShape = anIter.Value();
83 if (!IsClosed (aShape))
84 {
85 return Standard_False;
86 }
87 }
88 return Standard_True;
89 }
90 case TopAbs_SOLID:
91 {
92 // Check for non-manifold topology first of all:
93 // have to use BRep_Tool::IsClosed() because it checks the face connectivity
94 // inside the shape
95 if (!BRep_Tool::IsClosed (theShape))
96 return Standard_False;
97
98 for (TopoDS_Iterator anIter (theShape); anIter.More(); anIter.Next())
99 {
100 const TopoDS_Shape& aShape = anIter.Value();
101 if (aShape.IsNull())
102 {
103 continue;
104 }
105
106 if (aShape.ShapeType() == TopAbs_FACE)
107 {
108 // invalid solid
109 return Standard_False;
110 }
111 else if (!IsTriangulated (aShape))
112 {
113 // mesh contains holes
114 return Standard_False;
115 }
116 }
117 return Standard_True;
118 }
119 case TopAbs_SHELL:
120 case TopAbs_FACE:
121 {
122 // free faces / shell are not allowed
123 return Standard_False;
124 }
125 case TopAbs_WIRE:
126 case TopAbs_EDGE:
127 case TopAbs_VERTEX:
128 {
129 // ignore
130 return Standard_True;
131 }
132 }
133 }
134
135 //=======================================================================
136 //function : ComputeNormals
137 //purpose :
138 //=======================================================================
ComputeNormals(const TopoDS_Face & theFace,const Handle (Poly_Triangulation)& theTris,Poly_Connect & thePolyConnect)139 void StdPrs_ToolTriangulatedShape::ComputeNormals (const TopoDS_Face& theFace,
140 const Handle(Poly_Triangulation)& theTris,
141 Poly_Connect& thePolyConnect)
142 {
143 if (theTris.IsNull()
144 || theTris->HasNormals())
145 {
146 return;
147 }
148
149 // take in face the surface location
150 const TopoDS_Face aZeroFace = TopoDS::Face (theFace.Located (TopLoc_Location()));
151 Handle(Geom_Surface) aSurf = BRep_Tool::Surface (aZeroFace);
152 if (!theTris->HasUVNodes() || aSurf.IsNull())
153 {
154 // compute normals by averaging triangulation normals sharing the same vertex
155 Poly::ComputeNormals (theTris);
156 return;
157 }
158
159 const Standard_Real aTol = Precision::Confusion();
160 Standard_Integer aTri[3];
161 gp_Dir aNorm;
162 theTris->AddNormals();
163 for (Standard_Integer aNodeIter = 1; aNodeIter <= theTris->NbNodes(); ++aNodeIter)
164 {
165 // try to retrieve normal from real surface first, when UV coordinates are available
166 if (GeomLib::NormEstim (aSurf, theTris->UVNode (aNodeIter), aTol, aNorm) > 1)
167 {
168 if (thePolyConnect.Triangulation() != theTris)
169 {
170 thePolyConnect.Load (theTris);
171 }
172
173 // compute flat normals
174 gp_XYZ eqPlan (0.0, 0.0, 0.0);
175 for (thePolyConnect.Initialize (aNodeIter); thePolyConnect.More(); thePolyConnect.Next())
176 {
177 theTris->Triangle (thePolyConnect.Value()).Get (aTri[0], aTri[1], aTri[2]);
178 const gp_XYZ v1 (theTris->Node (aTri[1]).Coord() - theTris->Node (aTri[0]).Coord());
179 const gp_XYZ v2 (theTris->Node (aTri[2]).Coord() - theTris->Node (aTri[1]).Coord());
180 const gp_XYZ vv = v1 ^ v2;
181 const Standard_Real aMod = vv.Modulus();
182 if (aMod >= aTol)
183 {
184 eqPlan += vv / aMod;
185 }
186 }
187 const Standard_Real aModMax = eqPlan.Modulus();
188 aNorm = (aModMax > aTol) ? gp_Dir (eqPlan) : gp::DZ();
189 }
190
191 theTris->SetNormal (aNodeIter, aNorm);
192 }
193 }
194
195 //=======================================================================
196 //function : Normal
197 //purpose :
198 //=======================================================================
Normal(const TopoDS_Face & theFace,Poly_Connect & thePolyConnect,TColgp_Array1OfDir & theNormals)199 void StdPrs_ToolTriangulatedShape::Normal (const TopoDS_Face& theFace,
200 Poly_Connect& thePolyConnect,
201 TColgp_Array1OfDir& theNormals)
202 {
203 const Handle(Poly_Triangulation)& aPolyTri = thePolyConnect.Triangulation();
204 if (!aPolyTri->HasNormals())
205 {
206 ComputeNormals (theFace, aPolyTri, thePolyConnect);
207 }
208
209 gp_Vec3f aNormal;
210 for (Standard_Integer aNodeIter = 1; aNodeIter <= aPolyTri->NbNodes(); ++aNodeIter)
211 {
212 aPolyTri->Normal (aNodeIter, aNormal);
213 theNormals.ChangeValue (aNodeIter).SetCoord (aNormal.x(), aNormal.y(), aNormal.z());
214 }
215
216 if (theFace.Orientation() == TopAbs_REVERSED)
217 {
218 for (Standard_Integer aNodeIter = 1; aNodeIter <= aPolyTri->NbNodes(); ++aNodeIter)
219 {
220 theNormals.ChangeValue (aNodeIter).Reverse();
221 }
222 }
223 }
224
225 //=======================================================================
226 //function : GetDeflection
227 //purpose :
228 //=======================================================================
GetDeflection(const TopoDS_Shape & theShape,const Handle (Prs3d_Drawer)& theDrawer)229 Standard_Real StdPrs_ToolTriangulatedShape::GetDeflection (const TopoDS_Shape& theShape,
230 const Handle(Prs3d_Drawer)& theDrawer)
231 {
232 if (theDrawer->TypeOfDeflection() != Aspect_TOD_RELATIVE)
233 {
234 return theDrawer->MaximalChordialDeviation();
235 }
236
237 Bnd_Box aBndBox;
238 BRepBndLib::Add (theShape, aBndBox, Standard_False);
239 if (aBndBox.IsVoid())
240 {
241 return theDrawer->MaximalChordialDeviation();
242 }
243 else if (aBndBox.IsOpen())
244 {
245 if (!aBndBox.HasFinitePart())
246 {
247 return theDrawer->MaximalChordialDeviation();
248 }
249 aBndBox = aBndBox.FinitePart();
250 }
251
252 // store computed relative deflection of shape as absolute deviation coefficient in case relative type to use it later on for sub-shapes
253 const Standard_Real aDeflection = Prs3d::GetDeflection (aBndBox, theDrawer->DeviationCoefficient(), theDrawer->MaximalChordialDeviation());
254 theDrawer->SetMaximalChordialDeviation (aDeflection);
255 return aDeflection;
256 }
257
258 //=======================================================================
259 //function : IsTessellated
260 //purpose :
261 //=======================================================================
IsTessellated(const TopoDS_Shape & theShape,const Handle (Prs3d_Drawer)& theDrawer)262 Standard_Boolean StdPrs_ToolTriangulatedShape::IsTessellated (const TopoDS_Shape& theShape,
263 const Handle(Prs3d_Drawer)& theDrawer)
264 {
265 return BRepTools::Triangulation (theShape, GetDeflection (theShape, theDrawer), true);
266 }
267
268 // =======================================================================
269 // function : Tessellate
270 // purpose :
271 // =======================================================================
Tessellate(const TopoDS_Shape & theShape,const Handle (Prs3d_Drawer)& theDrawer)272 Standard_Boolean StdPrs_ToolTriangulatedShape::Tessellate (const TopoDS_Shape& theShape,
273 const Handle(Prs3d_Drawer)& theDrawer)
274 {
275 Standard_Boolean wasRecomputed = Standard_False;
276 // Check if it is possible to avoid unnecessary recomputation of shape triangulation
277 if (IsTessellated (theShape, theDrawer))
278 {
279 return wasRecomputed;
280 }
281
282 const Standard_Real aDeflection = GetDeflection (theShape, theDrawer);
283
284 // retrieve meshing tool from Factory
285 Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (theShape,
286 aDeflection,
287 theDrawer->DeviationAngle());
288 if (!aMeshAlgo.IsNull())
289 {
290 aMeshAlgo->Perform();
291 wasRecomputed = Standard_True;
292 }
293
294 return wasRecomputed;
295 }
296
297 // =======================================================================
298 // function : ClearOnOwnDeflectionChange
299 // purpose :
300 // =======================================================================
ClearOnOwnDeflectionChange(const TopoDS_Shape & theShape,const Handle (Prs3d_Drawer)& theDrawer,const Standard_Boolean theToResetCoeff)301 void StdPrs_ToolTriangulatedShape::ClearOnOwnDeflectionChange (const TopoDS_Shape& theShape,
302 const Handle(Prs3d_Drawer)& theDrawer,
303 const Standard_Boolean theToResetCoeff)
304 {
305 if (!theDrawer->IsAutoTriangulation()
306 || theShape.IsNull())
307 {
308 return;
309 }
310
311 const Standard_Boolean isOwnDeviationAngle = theDrawer->HasOwnDeviationAngle();
312 const Standard_Boolean isOwnDeviationCoefficient = theDrawer->HasOwnDeviationCoefficient();
313 const Standard_Real anAngleNew = theDrawer->DeviationAngle();
314 const Standard_Real anAnglePrev = theDrawer->PreviousDeviationAngle();
315 const Standard_Real aCoeffNew = theDrawer->DeviationCoefficient();
316 const Standard_Real aCoeffPrev = theDrawer->PreviousDeviationCoefficient();
317 if ((!isOwnDeviationAngle || Abs (anAngleNew - anAnglePrev) <= Precision::Angular())
318 && (!isOwnDeviationCoefficient || Abs (aCoeffNew - aCoeffPrev) <= Precision::Confusion()))
319 {
320 return;
321 }
322
323 BRepTools::Clean (theShape);
324 if (theToResetCoeff)
325 {
326 theDrawer->UpdatePreviousDeviationAngle();
327 theDrawer->UpdatePreviousDeviationCoefficient();
328 }
329 }
330