1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <BRepMesh_ShapeTool.hxx>
17 #include <Bnd_Box.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <BRepBndLib.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>
28 #include <BRepAdaptor_Curve.hxx>
31 //! Auxilary struct to take a tolerance of edge.
34 static Standard_Real Get(const TopoDS_Shape& theEdge)
36 return BRep_Tool::Tolerance(TopoDS::Edge(theEdge));
40 //! Auxilary struct to take a tolerance of vertex.
41 struct VertexTolerance
43 static Standard_Real Get(const TopoDS_Shape& theVertex)
45 return BRep_Tool::Tolerance(TopoDS::Vertex(theVertex));
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)
53 Standard_Real aMaxTolerance = RealFirst();
54 TopExp_Explorer aExplorer(theFace, ShapeType);
55 for (; aExplorer.More(); aExplorer.Next())
57 Standard_Real aTolerance = ToleranceExtractor::Get(aExplorer.Current());
58 if (aTolerance > aMaxTolerance)
59 aMaxTolerance = aTolerance;
66 //=======================================================================
67 //function : BoxMaxDimension
69 //=======================================================================
70 Standard_Real BRepMesh_ShapeTool::MaxFaceTolerance(const TopoDS_Face& theFace)
72 Standard_Real aMaxTolerance = BRep_Tool::Tolerance(theFace);
74 Standard_Real aTolerance = Max(
75 MaxTolerance<TopAbs_EDGE, EdgeTolerance >(theFace),
76 MaxTolerance<TopAbs_VERTEX, VertexTolerance>(theFace));
78 return Max(aMaxTolerance, aTolerance);
81 //=======================================================================
82 //function : BoxMaxDimension
84 //=======================================================================
85 void BRepMesh_ShapeTool::BoxMaxDimension(const Bnd_Box& theBox,
86 Standard_Real& theMaxDimension)
91 Standard_Real aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
92 theBox.Get(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
94 theMaxDimension = Max(aMaxX - aMinX, Max(aMaxY - aMinY, aMaxZ - aMinZ));
97 //=======================================================================
98 //function : RelativeEdgeDeflection
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)
107 theAdjustmentCoefficient = 1.;
108 Standard_Real aDefEdge = theDeflection;
113 BRepBndLib::Add(theEdge, aBox, Standard_False);
114 BoxMaxDimension(aBox, aDefEdge);
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.;
123 return (theAdjustmentCoefficient * aDefEdge * theDeflection);
126 //=======================================================================
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)
137 const gp_XY& aPnt2d = thePnt2d.Coord();
138 BRepMesh::DMapOfIntegerListOfXY& aLocation2D =
139 theFaceAttribute->ChangeLocation2D();
141 if (!aLocation2D.IsBound(theIndexOfPnt3d))
143 BRepMesh::ListOfXY aPoints2d;
144 aPoints2d.Append(aPnt2d);
145 aLocation2D.Bind(theIndexOfPnt3d, aPoints2d);
149 BRepMesh::ListOfXY& aPoints2d = aLocation2D.ChangeFind(theIndexOfPnt3d);
151 // Find the most closest 2d point to the given one.
153 Standard_Real aMinDist = RealLast();
154 BRepMesh::ListOfXY::Iterator aPoint2dIt(aPoints2d);
155 for (; aPoint2dIt.More(); aPoint2dIt.Next())
157 const gp_XY& aCurPnt2d = aPoint2dIt.Value();
159 Standard_Real aDist = (aPnt2d - aCurPnt2d).Modulus();
160 if (aDist < aMinDist)
167 const Standard_Real aTolerance =
168 Min(2. * BRep_Tool::Tolerance(theVertex), theMinDistance);
171 Standard_Real aDiffU = theFaceAttribute->GetUMax() - theFaceAttribute->GetUMin();
172 Standard_Real aDiffV = theFaceAttribute->GetVMax() - theFaceAttribute->GetVMin();
174 const Standard_Real Utol2d = .5 * aDiffU;
175 const Standard_Real Vtol2d = .5 * aDiffV;
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());
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))
188 aPoints2d.Append(aUV);
194 //=======================================================================
195 //function : AddInFace
197 //=======================================================================
198 void BRepMesh_ShapeTool::AddInFace(
199 const TopoDS_Face& theFace,
200 Handle(Poly_Triangulation)& theTriangulation)
202 const TopLoc_Location& aLoc = theFace.Location();
203 if (!aLoc.IsIdentity())
205 gp_Trsf aTrsf = aLoc.Transformation();
208 TColgp_Array1OfPnt& aNodes = theTriangulation->ChangeNodes();
209 for (Standard_Integer i = aNodes.Lower(); i <= aNodes.Upper(); ++i)
210 aNodes(i).Transform(aTrsf);
213 BRep_Builder aBuilder;
214 aBuilder.UpdateFace(theFace, theTriangulation);
217 //=======================================================================
218 //function : NullifyFace
220 //=======================================================================
221 void BRepMesh_ShapeTool::NullifyFace(const TopoDS_Face& theFace)
223 BRep_Builder aBuilder;
224 aBuilder.UpdateFace(theFace, Handle(Poly_Triangulation)());
227 //=======================================================================
228 //function : NullifyEdge
230 //=======================================================================
231 void BRepMesh_ShapeTool::NullifyEdge(
232 const TopoDS_Edge& theEdge,
233 const Handle(Poly_Triangulation)& theTriangulation,
234 const TopLoc_Location& theLocation)
236 UpdateEdge(theEdge, Handle(Poly_PolygonOnTriangulation)(),
237 theTriangulation, theLocation);
240 //=======================================================================
241 //function : UpdateEdge
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)
250 BRep_Builder aBuilder;
251 aBuilder.UpdateEdge(theEdge, thePolygon, theTriangulation, theLocation);
254 //=======================================================================
255 //function : UpdateEdge
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)
265 BRep_Builder aBuilder;
266 aBuilder.UpdateEdge(theEdge, thePolygon1, thePolygon2,
267 theTriangulation, theLocation);
270 //=======================================================================
271 //function : UseLocation
273 //=======================================================================
274 gp_Pnt BRepMesh_ShapeTool::UseLocation(const gp_Pnt& thePnt,
275 const TopLoc_Location& theLoc)
277 if (theLoc.IsIdentity())
280 return thePnt.Transformed(theLoc.Transformation());
283 //=======================================================================
284 //function : IsDegenerated
286 //=======================================================================
287 Standard_Boolean BRepMesh_ShapeTool::IsDegenerated(
288 const TopoDS_Edge& theEdge,
289 const TopoDS_Face& theFace)
292 TopoDS_Vertex pBegin, pEnd;
293 TopExp::Vertices(theEdge, pBegin, pEnd);
294 if (pBegin.IsNull() || pEnd.IsNull())
295 return Standard_True;
297 if (BRep_Tool::Degenerated(theEdge))
298 return Standard_True;
300 if (!pBegin.IsSame(pEnd))
301 return Standard_False;
303 Standard_Real wFirst, wLast;
304 BRep_Tool::Range(theEdge, theFace, wFirst, wLast);
306 // calculation of the length of the edge in 3D
307 Standard_Real longueur = 0.0;
308 Standard_Real du = (wLast - wFirst) * 0.05;
310 BRepAdaptor_Curve BC(theEdge);
312 Standard_Real tolV = BRep_Tool::Tolerance(pBegin);
313 Standard_Real tolV2 = 1.2 * tolV;
315 for (Standard_Integer l = 1; l <= 20; ++l)
317 BC.D0(wFirst + l * du, P2);
318 longueur += P1.Distance(P2);
320 if (longueur > tolV2)
326 if (longueur < tolV2)
327 return Standard_True;
329 return Standard_False;