0025154: Collections in BRepMesh package are named in non-conformant manner
[occt.git] / src / BRepMesh / BRepMesh_ShapeTool.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <BRepMesh_ShapeTool.hxx>
16
17 #include <Bnd_Box.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <BRepBndLib.hxx>
20 #include <TopoDS.hxx>
21 #include <BRep_Tool.hxx>
22 #include <TopExp_Explorer.hxx>
23 #include <BRepAdaptor_HSurface.hxx>
24 #include <TColgp_Array1OfPnt.hxx>
25 #include <Poly_Triangulation.hxx>
26 #include <BRep_Builder.hxx>
27 #include <TopExp.hxx>
28 #include <BRepAdaptor_Curve.hxx>
29
30 namespace {
31   //! Auxilary struct to take a tolerance of edge.
32   struct EdgeTolerance
33   {
34     static Standard_Real Get(const TopoDS_Shape& theEdge)
35     {
36       return BRep_Tool::Tolerance(TopoDS::Edge(theEdge));
37     }
38   };
39
40   //! Auxilary struct to take a tolerance of vertex.
41   struct VertexTolerance
42   {
43     static Standard_Real Get(const TopoDS_Shape& theVertex)
44     {
45       return BRep_Tool::Tolerance(TopoDS::Vertex(theVertex));
46     }
47   };
48
49   //! Returns maximum tolerance of face element of the specified type.
50   template<TopAbs_ShapeEnum ShapeType, class ToleranceExtractor>
51   Standard_Real MaxTolerance(const TopoDS_Face& theFace)
52   {
53     Standard_Real aMaxTolerance = RealFirst();
54     TopExp_Explorer aExplorer(theFace, ShapeType);
55     for (; aExplorer.More(); aExplorer.Next())
56     {
57       Standard_Real aTolerance = ToleranceExtractor::Get(aExplorer.Current());
58       if (aTolerance > aMaxTolerance)
59         aMaxTolerance = aTolerance;
60     }
61
62     return aMaxTolerance;
63   }
64 }
65
66 //=======================================================================
67 //function : BoxMaxDimension
68 //purpose  : 
69 //=======================================================================
70 Standard_Real BRepMesh_ShapeTool::MaxFaceTolerance(const TopoDS_Face& theFace)
71 {
72   Standard_Real aMaxTolerance = BRep_Tool::Tolerance(theFace);
73
74   Standard_Real aTolerance = Max(
75     MaxTolerance<TopAbs_EDGE,   EdgeTolerance  >(theFace),
76     MaxTolerance<TopAbs_VERTEX, VertexTolerance>(theFace));
77
78   return Max(aMaxTolerance, aTolerance);
79 }
80
81 //=======================================================================
82 //function : BoxMaxDimension
83 //purpose  : 
84 //=======================================================================
85 void BRepMesh_ShapeTool::BoxMaxDimension(const Bnd_Box& theBox,
86                                          Standard_Real& theMaxDimension)
87 {
88   if(theBox.IsVoid())
89     return;
90
91   Standard_Real aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
92   theBox.Get(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
93
94   theMaxDimension = Max(aMaxX - aMinX, Max(aMaxY - aMinY, aMaxZ - aMinZ));
95 }
96
97 //=======================================================================
98 //function : RelativeEdgeDeflection
99 //purpose  : 
100 //=======================================================================
101 Standard_Real BRepMesh_ShapeTool::RelativeEdgeDeflection(
102   const TopoDS_Edge&  theEdge,
103   const Standard_Real theDeflection,
104   const Standard_Real theMaxShapeSize,
105   Standard_Real&      theAdjustmentCoefficient)
106 {
107   theAdjustmentCoefficient = 1.;
108   Standard_Real aDefEdge = theDeflection;
109   if(theEdge.IsNull())
110     return aDefEdge;
111
112   Bnd_Box aBox;
113   BRepBndLib::Add(theEdge, aBox, Standard_False);
114   BoxMaxDimension(aBox, aDefEdge);
115             
116   // Adjust resulting value in relation to the total size
117   theAdjustmentCoefficient = theMaxShapeSize / (2 * aDefEdge);
118   if (theAdjustmentCoefficient < 0.5)
119     theAdjustmentCoefficient = 0.5;
120   else if (theAdjustmentCoefficient > 2.)
121     theAdjustmentCoefficient = 2.;
122
123   return (theAdjustmentCoefficient * aDefEdge * theDeflection);
124 }
125
126 //=======================================================================
127 //function : FindUV
128 //purpose  : 
129 //=======================================================================
130 gp_XY BRepMesh_ShapeTool::FindUV(
131   const Standard_Integer                theIndexOfPnt3d,
132   const gp_Pnt2d&                       thePnt2d,
133   const TopoDS_Vertex&                  theVertex,
134   const Standard_Real                   theMinDistance,
135   const Handle(BRepMesh_FaceAttribute)& theFaceAttribute)
136 {
137   const gp_XY& aPnt2d = thePnt2d.Coord();
138   BRepMesh::DMapOfIntegerListOfXY& aLocation2D =
139     theFaceAttribute->ChangeLocation2D();
140
141   if (!aLocation2D.IsBound(theIndexOfPnt3d))
142   {
143     BRepMesh::ListOfXY aPoints2d;
144     aPoints2d.Append(aPnt2d);
145     aLocation2D.Bind(theIndexOfPnt3d, aPoints2d);
146     return aPnt2d;
147   }
148
149   BRepMesh::ListOfXY& aPoints2d = aLocation2D.ChangeFind(theIndexOfPnt3d);
150
151   // Find the most closest 2d point to the given one.
152   gp_XY aUV;
153   Standard_Real aMinDist = RealLast();
154   BRepMesh::ListOfXY::Iterator aPoint2dIt(aPoints2d);
155   for (; aPoint2dIt.More(); aPoint2dIt.Next())
156   {
157     const gp_XY& aCurPnt2d = aPoint2dIt.Value();
158
159     Standard_Real aDist = (aPnt2d - aCurPnt2d).Modulus();
160     if (aDist < aMinDist)
161     {
162       aUV      = aCurPnt2d;
163       aMinDist = aDist;
164     }
165   }
166
167   const Standard_Real aTolerance = 
168     Min(2. * BRep_Tool::Tolerance(theVertex), theMinDistance);
169
170   // Get face limits
171   Standard_Real aDiffU = theFaceAttribute->GetUMax() - theFaceAttribute->GetUMin();
172   Standard_Real aDiffV = theFaceAttribute->GetVMax() - theFaceAttribute->GetVMin();
173
174   const Standard_Real Utol2d = .5 * aDiffU;
175   const Standard_Real Vtol2d = .5 * aDiffV;
176
177   const Handle(BRepAdaptor_HSurface)& aSurface = theFaceAttribute->Surface();
178   const gp_Pnt aPnt1 = aSurface->Value(aUV.X(), aUV.Y());
179   const gp_Pnt aPnt2 = aSurface->Value(aPnt2d.X(), aPnt2d.Y());
180
181   //! If selected point is too far from the given one in parametric space
182   //! or their positions in 3d are different, add the given point as unique.
183   if (Abs(aUV.X() - aPnt2d.X()) > Utol2d ||
184       Abs(aUV.Y() - aPnt2d.Y()) > Vtol2d ||
185       !aPnt1.IsEqual(aPnt2, aTolerance))
186   {
187     aUV = aPnt2d;
188     aPoints2d.Append(aUV);
189   }
190
191   return aUV;
192 }
193
194 //=======================================================================
195 //function : AddInFace
196 //purpose  : 
197 //=======================================================================
198 void BRepMesh_ShapeTool::AddInFace(
199   const TopoDS_Face&          theFace,
200   Handle(Poly_Triangulation)& theTriangulation)
201 {
202   const TopLoc_Location& aLoc = theFace.Location();
203   if (!aLoc.IsIdentity())
204   {
205     gp_Trsf aTrsf = aLoc.Transformation();
206     aTrsf.Invert();
207
208     TColgp_Array1OfPnt& aNodes = theTriangulation->ChangeNodes();
209     for (Standard_Integer i = aNodes.Lower(); i <= aNodes.Upper(); ++i) 
210       aNodes(i).Transform(aTrsf);
211   }
212
213   BRep_Builder aBuilder;
214   aBuilder.UpdateFace(theFace, theTriangulation);
215 }
216
217 //=======================================================================
218 //function : NullifyFace
219 //purpose  : 
220 //=======================================================================
221 void BRepMesh_ShapeTool::NullifyFace(const TopoDS_Face& theFace)
222 {
223   BRep_Builder aBuilder;
224   aBuilder.UpdateFace(theFace, Handle(Poly_Triangulation)());
225 }
226
227 //=======================================================================
228 //function : NullifyEdge
229 //purpose  : 
230 //=======================================================================
231 void BRepMesh_ShapeTool::NullifyEdge(
232   const TopoDS_Edge&                theEdge,
233   const Handle(Poly_Triangulation)& theTriangulation,
234   const TopLoc_Location&            theLocation)
235 {
236   UpdateEdge(theEdge, Handle(Poly_PolygonOnTriangulation)(),
237     theTriangulation, theLocation);
238 }
239
240 //=======================================================================
241 //function : UpdateEdge
242 //purpose  : 
243 //=======================================================================
244 void BRepMesh_ShapeTool::UpdateEdge(
245   const TopoDS_Edge&                         theEdge,
246   const Handle(Poly_PolygonOnTriangulation)& thePolygon,
247   const Handle(Poly_Triangulation)&          theTriangulation,
248   const TopLoc_Location&                     theLocation)
249 {
250   BRep_Builder aBuilder;
251   aBuilder.UpdateEdge(theEdge, thePolygon, theTriangulation, theLocation);
252 }
253
254 //=======================================================================
255 //function : UpdateEdge
256 //purpose  : 
257 //=======================================================================
258 void BRepMesh_ShapeTool::UpdateEdge(
259   const TopoDS_Edge&                         theEdge,
260   const Handle(Poly_PolygonOnTriangulation)& thePolygon1,
261   const Handle(Poly_PolygonOnTriangulation)& thePolygon2,
262   const Handle(Poly_Triangulation)&          theTriangulation,
263   const TopLoc_Location&                     theLocation)
264 {
265   BRep_Builder aBuilder;
266   aBuilder.UpdateEdge(theEdge, thePolygon1, thePolygon2, 
267     theTriangulation, theLocation);
268 }
269
270 //=======================================================================
271 //function : UseLocation
272 //purpose  : 
273 //=======================================================================
274 gp_Pnt BRepMesh_ShapeTool::UseLocation(const gp_Pnt&          thePnt,
275                                        const TopLoc_Location& theLoc)
276 {
277   if (theLoc.IsIdentity())
278     return thePnt;
279
280   return thePnt.Transformed(theLoc.Transformation());
281 }
282
283 //=======================================================================
284 //function : IsDegenerated
285 //purpose  : 
286 //=======================================================================
287 Standard_Boolean BRepMesh_ShapeTool::IsDegenerated(
288   const TopoDS_Edge& theEdge,
289   const TopoDS_Face& theFace)
290 {
291   // Get vertices
292   TopoDS_Vertex pBegin, pEnd;
293   TopExp::Vertices(theEdge, pBegin, pEnd);
294   if (pBegin.IsNull() || pEnd.IsNull())
295     return Standard_True;
296
297   if (BRep_Tool::Degenerated(theEdge))
298     return Standard_True;
299
300   if (!pBegin.IsSame(pEnd))
301     return Standard_False;
302
303   Standard_Real wFirst, wLast;
304   BRep_Tool::Range(theEdge, theFace, wFirst, wLast);
305
306   // calculation of the length of the edge in 3D
307   Standard_Real longueur = 0.0;
308   Standard_Real du = (wLast - wFirst) * 0.05;
309   gp_Pnt P1, P2;
310   BRepAdaptor_Curve BC(theEdge);
311   BC.D0(wFirst, P1);
312   Standard_Real tolV = BRep_Tool::Tolerance(pBegin);
313   Standard_Real tolV2 = 1.2 * tolV;
314
315   for (Standard_Integer l = 1; l <= 20; ++l)
316   {
317     BC.D0(wFirst + l * du, P2);
318     longueur += P1.Distance(P2);
319
320     if (longueur > tolV2)
321       break;
322
323     P1 = P2;
324   }
325
326   if (longueur < tolV2)
327     return Standard_True;
328
329   return Standard_False;
330 }