4 #include <NMTAlgo_Loop3d.ixx>
6 #include <TopExp_Explorer.hxx>
8 #include <BRep_Builder.hxx>
9 #include <TopTools_MapOfShape.hxx>
10 #include <TopTools_ListIteratorOfListOfShape.hxx>
11 #include <TopoDS_Shell.hxx>
12 #include <TopoDS_Iterator.hxx>
14 #include <TopTools_MapIteratorOfMapOfShape.hxx>
17 #include <Geom2d_Curve.hxx>
18 #include <BRep_Tool.hxx>
19 #include <Geom_Surface.hxx>
20 #include <gp_Pnt2d.hxx>
21 #include <gp_Vec2d.hxx>
22 #include <gp_Dir2d.hxx>
23 #include <Geom_Curve.hxx>
26 TopoDS_Edge FindEinF(const TopoDS_Edge&, const TopoDS_Face&);
28 gp_Vec NextNormal(const TopoDS_Edge&, const TopoDS_Face&);
30 //=======================================================================
33 //=======================================================================
34 NMTAlgo_Loop3d::NMTAlgo_Loop3d()
38 //=======================================================================
39 //function : AddConstFaces
40 //purpose : Add faces of <S> as unique faces in the result.
41 //=======================================================================
42 void NMTAlgo_Loop3d::AddConstFaces(const TopoDS_Shape& S)
44 TopExp_Explorer FaceExp(S, TopAbs_FACE);
45 for (; FaceExp.More(); FaceExp.Next()){
46 myFaces.Append( FaceExp.Current() );
48 TopExp::MapShapesAndAncestors(S, TopAbs_EDGE, TopAbs_FACE, myEFMap);
51 //=======================================================================
52 //function : AddSectionFaces
53 //purpose : Add faces of <S> as double faces in the result.
54 //=======================================================================
55 void NMTAlgo_Loop3d::AddSectionFaces(const TopoDS_Shape& S)
58 AddConstFaces( S.Reversed() );
61 //=======================================================================
62 //function : MakeShells
63 //purpose : Make and return shells.
64 // <AvoidFacesMap> can contain faces that must not be
65 // added to result shells.
66 //=======================================================================
67 const TopTools_ListOfShape& NMTAlgo_Loop3d::MakeShells
68 (const TopTools_MapOfOrientedShape& AvoidFacesMap)
73 TopTools_MapOfShape CheckedEdgesMap;
74 TopTools_MapOfOrientedShape AddedFacesMap;
76 TopTools_ListIteratorOfListOfShape itF (myFaces);
77 for (; itF.More(); itF.Next()) {
78 const TopoDS_Shape& FF = itF.Value();
79 if (AvoidFacesMap.Contains( FF ) ||
80 ! AddedFacesMap.Add( FF ) )
85 Builder.MakeShell(Shell);
86 Builder.Add(Shell,FF);
88 // clear the maps from shapes added to previous Shell
89 TopTools_MapIteratorOfMapOfShape itEM (CheckedEdgesMap);
90 for (; itEM.More(); itEM.Next()) {
91 TopTools_ListOfShape& FL = myEFMap.ChangeFromKey( itEM.Key());
92 TopTools_ListIteratorOfListOfShape it (FL);
94 if (AddedFacesMap.Contains( it.Value()))
100 CheckedEdgesMap.Clear();
103 // loop on faces added to Shell; add their neighbor faces to Shell and so on
104 TopoDS_Iterator itAddedF (Shell);
105 for (; itAddedF.More(); itAddedF.Next()){
106 const TopoDS_Face& F = TopoDS::Face (itAddedF.Value());
108 // loop on edges of F; find a good neighbor face of F by E
109 TopExp_Explorer EdgeExp(F, TopAbs_EDGE);
110 for (; EdgeExp.More(); EdgeExp.Next()){
111 const TopoDS_Edge& E = TopoDS::Edge( EdgeExp.Current());
112 if (! CheckedEdgesMap.Add( E ))
115 // candidate faces list
116 const TopTools_ListOfShape& FL = myEFMap.ChangeFromKey(E);
119 // select one of neighbors
121 if (FL.Extent() == 2) {
122 if (! F.IsSame( FL.First() ))
123 SelF = TopoDS::Face( FL.First() );
124 else if (!F.IsSame( FL.Last() ))
125 SelF = TopoDS::Face( FL.Last() );
128 // check if a face already added to Shell shares E
129 TopTools_ListIteratorOfListOfShape it (FL);
130 Standard_Boolean found = Standard_False;
131 for (; !found && it.More(); it.Next())
133 found = AddedFacesMap.Contains( it.Value() );
136 // select basing on geometrical check
137 Standard_Boolean GoodOri, inside;
138 Standard_Real dot, MaxDot = -100;
139 TopTools_ListOfShape TangFL; // tangent faces
140 for ( it.Initialize( FL ) ; it.More(); it.Next()) {
141 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
142 if (NeighborF.IsSame( F ))
144 inside = NMTAlgo_Loop3d::IsInside( E, F, NeighborF, 1, dot, GoodOri);
151 if ( IsEqual( dot, MaxDot))
158 if (!TangFL.IsEmpty()) {
159 for (it.Initialize( TangFL ); it.More(); it.Next()) {
160 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
161 if (NMTAlgo_Loop3d:: IsInside( E, SelF , NeighborF, 0, dot, GoodOri))
166 if (!SelF.IsNull() &&
167 AddedFacesMap.Add( SelF ) &&
168 !AvoidFacesMap.Contains( SelF ))
169 Builder.Add( Shell, SelF);
171 } // loop on edges of F
173 } // loop on the faces added to Shell
176 myNewShells.Append( Shell );
181 // prepare to the next call
187 //=======================================================================
190 //=======================================================================
191 gp_Vec NMTAlgo_Loop3d::Normal(const TopoDS_Edge& E,
192 const TopoDS_Face& F)
195 Standard_Real First, Last;
198 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
199 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
201 gp_Pnt2d p = C2d->Value( 0.5*(First+Last) );
202 Sf->D1(p.X(), p.Y(), Ps, V1, V2);
203 Norm = V1.Crossed(V2);
205 if (F.Orientation() == TopAbs_REVERSED )
210 //=======================================================================
211 //function : IsInside
212 //purpose : check if <F2> is inside <F1> by edge <E>.
213 // if <CountDot>, compute <Dot>: scalar production of
214 // normalized vectors pointing inside faces, and
215 // check if faces are oriented well for sewing
216 //=======================================================================
217 Standard_Boolean NMTAlgo_Loop3d::IsInside(const TopoDS_Edge& E,
218 const TopoDS_Face& F1,
219 const TopoDS_Face& F2,
220 const Standard_Boolean CountDot,
222 Standard_Boolean& GoodOri)
226 gp_Vec Vc1, Vc2, Vin1, Vin2, Nf1, Nf2;
227 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E,f,l);
228 Curve->D1( 0.5*(f + l), P, Vc2);
229 TopoDS_Edge E1, E2 = FindEinF (E, F2);
230 if (E2.Orientation() == TopAbs_REVERSED ) Vc2.Reverse();
236 Nf1.CrossSquareMagnitude(Nf2) / Nf1.SquareMagnitude() / Nf2.SquareMagnitude();
237 Standard_Boolean tangent = sin < 0.001;
239 Standard_Boolean inside = 0;
241 E1 = FindEinF (E, F1);
242 gp_Vec NNf1 = NextNormal(E1,F1);
243 gp_Vec NNf2 = NextNormal(E2,F2);
244 Vin2 = NNf2.Crossed(Vc2);
245 inside = Vin2 * NNf1 < 0;
248 Vin2 = Nf2.Crossed(Vc2);
249 inside = Vin2 * Nf1 < 0;
252 if (!CountDot) return inside;
255 Vin2 = Nf2.Crossed(Vc2);
257 E1 = FindEinF (E, F1);
260 if (E1.Orientation() != E2.Orientation())
262 Vin1 = Nf1.Crossed(Vc1);
265 Standard_Real N1N2 = Nf1 * Nf2;
266 GoodOri = (Vin2 * Vin1 < 0) ? N1N2 > 0 : N1N2 < 0;
269 Standard_Real V1N2 = Vin1 * Nf2;
270 GoodOri = ( inside ? V1N2 <= 0 : V1N2 >= 0);
280 //=======================================================================
281 //function : NextNormal
282 //purpose : find normal to F at point a little inside F near the middle of E
283 //warning : E must be properly oriented in F.
284 //=======================================================================
285 gp_Vec NextNormal(const TopoDS_Edge& E,
286 const TopoDS_Face& F)
288 Standard_Real First, Last;
290 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
291 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
295 C2d->D1( 0.5*(First+Last), p, v);
296 if (E.Orientation() != F.Orientation())
298 gp_Dir2d dir( -v.Y(), v.X() ); // dir inside F
300 Standard_Real duv = 1e-6; // this is not Ok and may give incorrect result if
301 // resolutionUV of compared faces is very different. To have a good result,
302 //it is necessary to get normal to faces at points equidistant from E in 3D
304 p.SetX( p.X() + dir.X()*duv );
305 p.SetY( p.Y() + dir.Y()*duv );
309 Sf->D1( p.X(), p.Y(), Ps, V1, V2);
310 Norm = V1.Crossed(V2);
312 if (F.Orientation() == TopAbs_REVERSED )
317 //=======================================================================
318 //function : FindEinF
319 //purpose : find E in F
320 //=======================================================================
321 TopoDS_Edge FindEinF(const TopoDS_Edge& E,
322 const TopoDS_Face& F)
324 TopExp_Explorer expl (F, TopAbs_EDGE);
325 for (; expl.More(); expl.Next())
326 if( E.IsSame( expl.Current() ))
327 return TopoDS::Edge(expl.Current());