0025841: Incorrect edge displaying
[occt.git] / src / BRepMesh / BRepMesh_EdgeTessellator.cxx
CommitLineData
ceb418e1 1// Created on: 2014-08-13
2// Created by: Oleg AGASHIN
3// Copyright (c) 2011-2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#include <BRepMesh_EdgeTessellator.hxx>
17#include <Geom_Surface.hxx>
18#include <Geom_Plane.hxx>
19#include <Geom2d_Curve.hxx>
20#include <TopoDS_Edge.hxx>
21#include <TopoDS_Face.hxx>
22#include <BRepAdaptor_HSurface.hxx>
23#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
24#include <TopLoc_Location.hxx>
25#include <BRep_Tool.hxx>
26#include <TColStd_Array1OfReal.hxx>
27#include <TopExp_Explorer.hxx>
28#include <TopoDS_Vertex.hxx>
29#include <TopoDS.hxx>
30#include <TopTools_ListIteratorOfListOfShape.hxx>
31#include <TopTools_ListOfShape.hxx>
32#include <TColStd_Array1OfReal.hxx>
33
34IMPLEMENT_STANDARD_HANDLE (BRepMesh_EdgeTessellator, BRepMesh_IEdgeTool)
35IMPLEMENT_STANDARD_RTTIEXT(BRepMesh_EdgeTessellator, BRepMesh_IEdgeTool)
36
37//=======================================================================
38//function : Constructor
39//purpose :
40//=======================================================================
41BRepMesh_EdgeTessellator::BRepMesh_EdgeTessellator(
42 const TopoDS_Edge& theEdge,
43 const Handle(BRepMesh_FaceAttribute)& theFaceAttribute,
44 const TopTools_IndexedDataMapOfShapeListOfShape& theMapOfSharedFaces,
45 const Standard_Real theLinDeflection,
74da0216 46 const Standard_Real theAngDeflection,
47 const Standard_Real theMinSize)
ceb418e1 48 : mySurface(theFaceAttribute->Surface())
49{
50 Standard_Real aPreciseAngDef = 0.5 * theAngDeflection;
51 Standard_Real aPreciseLinDef = 0.5 * theLinDeflection;
52 if (theEdge.Orientation() == TopAbs_INTERNAL)
53 aPreciseLinDef *= 0.5;
54
55 mySquareEdgeDef = aPreciseLinDef * aPreciseLinDef;
74da0216 56 mySquareMinSize = Max(mySquareEdgeDef, theMinSize * theMinSize);
ceb418e1 57
58 Standard_Boolean isSameParam = BRep_Tool::SameParameter(theEdge);
59 if (isSameParam)
60 myCOnS.Initialize(theEdge);
61 else
62 myCOnS.Initialize(theEdge, theFaceAttribute->Face());
63
64 TopLoc_Location aLoc;
65 const GeomAbs_CurveType aCurveType = myCOnS.GetType();
66 Standard_Integer aMinPntNb = (aCurveType == GeomAbs_Circle) ? 4 : 2; //OCC287
67
68 // Get range on 2d curve
69 Standard_Real aFirstParam, aLastParam;
70 BRep_Tool::Range(theEdge, theFaceAttribute->Face(), aFirstParam, aLastParam);
71 myTool = new BRepMesh_GeomTool(myCOnS, aFirstParam, aLastParam,
74da0216 72 aPreciseLinDef, aPreciseAngDef, aMinPntNb, theMinSize);
ceb418e1 73
74 if (aCurveType == GeomAbs_BSplineCurve)
75 {
77e39787 76 // bug24220
ceb418e1 77 const Standard_Integer aNbInt = myCOnS.NbIntervals(GeomAbs_C1);
78 if ( aNbInt > 0 )
79 {
80 TColStd_Array1OfReal anIntervals( 1, aNbInt + 1 );
81 myCOnS.Intervals(anIntervals, GeomAbs_C1);
82 for (Standard_Integer aIntIt = 1; aIntIt <= aNbInt; ++aIntIt)
83 {
84 const Standard_Real& aStartInt = anIntervals.Value( aIntIt );
85 const Standard_Real& anEndInt = anIntervals.Value( aIntIt + 1 );
86
87 BRepMesh_GeomTool aDetalizator(myCOnS, aStartInt, anEndInt,
74da0216 88 aPreciseLinDef, aPreciseAngDef, aMinPntNb, theMinSize);
ceb418e1 89
90 Standard_Integer aNbAddNodes = aDetalizator.NbPoints();
91 for ( Standard_Integer aNodeIt = 1; aNodeIt <= aNbAddNodes; ++aNodeIt )
92 {
93 Standard_Real aParam;
94 gp_Pnt aPoint3d;
95 gp_Pnt2d aPoint2d;
96 aDetalizator.Value( aNodeIt, mySurface, aParam, aPoint3d, aPoint2d );
97 myTool->AddPoint( aPoint3d, aParam, Standard_False );
98 }
99 }
100 }
101 }
77e39787 102
ceb418e1 103 // PTv, chl/922/G9, Take into account internal vertices
104 // it is necessary for internal edges, which do not split other edges, by their vertex
105 TopExp_Explorer aVertexIt(theEdge, TopAbs_VERTEX);
106 for (; aVertexIt.More(); aVertexIt.Next())
107 {
108 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aVertexIt.Current());
109 if (aVertex.Orientation() != TopAbs_INTERNAL)
110 continue;
111
112 myTool->AddPoint(BRep_Tool::Pnt(aVertex),
113 BRep_Tool::Parameter(aVertex, theEdge), Standard_True);
114 }
115
116 Standard_Integer aNodesNb = myTool->NbPoints();
117 //Check deflection in 2d space for improvement of edge tesselation.
118 if( isSameParam && aNodesNb > 1)
119 {
120 const TopTools_ListOfShape& aSharedFaces = theMapOfSharedFaces.FindFromKey(theEdge);
121 TopTools_ListIteratorOfListOfShape aFaceIt(aSharedFaces);
122 for (; aFaceIt.More(); aFaceIt.Next())
123 {
124 TopLoc_Location aLoc;
125 const TopoDS_Face& aFace = TopoDS::Face(aFaceIt.Value());
126 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace, aLoc);
127
128 if (aSurf->IsInstance(STANDARD_TYPE(Geom_Plane)))
129 continue;
130
131 Standard_Real aF, aL;
132 Handle(Geom2d_Curve) aCurve2d = BRep_Tool::CurveOnSurface(theEdge, aFace, aF, aL);
133 if ( Abs(aF - aFirstParam) > Precision::PConfusion() ||
134 Abs(aL - aLastParam ) > Precision::PConfusion() )
135 {
136 continue;
137 }
138
139 aNodesNb = myTool->NbPoints();
140 TColStd_Array1OfReal aParamArray(1, aNodesNb);
141 for (Standard_Integer i = 1; i <= aNodesNb; ++i)
142 {
143 gp_Pnt2d aTmpUV;
144 gp_Pnt aTmpPnt;
145 Standard_Real aParam;
146 myTool->Value(i, mySurface, aParam, aTmpPnt, aTmpUV);
147 aParamArray.SetValue(i, aParam);
148 }
149
150 for (Standard_Integer i = 1; i < aNodesNb; ++i)
151 splitSegment(aSurf, aCurve2d, aParamArray(i), aParamArray(i + 1), 1);
152 }
153 }
154}
155
156//=======================================================================
157//function : Value
158//purpose :
159//=======================================================================
160void BRepMesh_EdgeTessellator::Value(const Standard_Integer theIndex,
161 Standard_Real& theParameter,
162 gp_Pnt& thePoint,
163 gp_Pnt2d& theUV)
164{
165 myTool->Value(theIndex, mySurface, theParameter, thePoint, theUV);
166}
167
168//=======================================================================
169//function : splitSegment
170//purpose :
171//=======================================================================
172void BRepMesh_EdgeTessellator::splitSegment(
173 const Handle(Geom_Surface)& theSurf,
174 const Handle(Geom2d_Curve)& theCurve2d,
175 const Standard_Real theFirst,
176 const Standard_Real theLast,
177 const Standard_Integer theNbIter)
178{
179 // limit iteration depth
180 if(theNbIter > 10)
181 return;
182
183 gp_Pnt2d uvf, uvl, uvm;
184 gp_Pnt P3dF, P3dL, midP3d, midP3dFromSurf;
185 Standard_Real midpar;
186
187 if(Abs(theLast - theFirst) < 2 * Precision::PConfusion())
188 return;
189
190 theCurve2d->D0(theFirst, uvf);
191 theCurve2d->D0(theLast, uvl);
192
193 P3dF = theSurf->Value(uvf.X(), uvf.Y());
194 P3dL = theSurf->Value(uvl.X(), uvl.Y());
195
74da0216 196 if(P3dF.SquareDistance(P3dL) < mySquareMinSize)
ceb418e1 197 return;
198
199 uvm = gp_Pnt2d((uvf.XY() + uvl.XY())*0.5);
200 midP3dFromSurf = theSurf->Value(uvm.X(), uvm.Y());
201
74da0216 202 gp_XYZ Vec1 = midP3dFromSurf.XYZ() - P3dF.XYZ();
203 if(Vec1.SquareModulus() < mySquareMinSize)
204 return;
205
ceb418e1 206 gp_XYZ aVec = P3dL.XYZ() - P3dF.XYZ();
207 aVec.Normalize();
208
ceb418e1 209 Standard_Real aModulus = Vec1.Dot(aVec);
210 gp_XYZ aProj = aVec * aModulus;
211 gp_XYZ aDist = Vec1 - aProj;
212
213 if(aDist.SquareModulus() < mySquareEdgeDef)
214 return;
215
216 midpar = (theFirst + theLast) * 0.5;
217 myCOnS.D0(midpar, midP3d);
218 myTool->AddPoint(midP3d, midpar, Standard_False);
219
220 splitSegment(theSurf, theCurve2d, theFirst, midpar, theNbIter + 1);
221 splitSegment(theSurf, theCurve2d, midpar, theLast, theNbIter + 1);
222}