1 // GEOM PARTITION : partition algorithm
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : Partition_Loop3d.cxx
11 #include "Partition_Loop3d.ixx"
13 #include <TopExp_Explorer.hxx>
15 #include <BRep_Builder.hxx>
16 #include <TopTools_MapOfShape.hxx>
17 #include <TopTools_ListIteratorOfListOfShape.hxx>
18 #include <TopoDS_Shell.hxx>
19 #include <TopoDS_Iterator.hxx>
21 #include <TopTools_MapIteratorOfMapOfShape.hxx>
24 #include <Geom2d_Curve.hxx>
25 #include <BRep_Tool.hxx>
26 #include <Geom_Surface.hxx>
27 #include <gp_Pnt2d.hxx>
28 #include <gp_Vec2d.hxx>
29 #include <gp_Dir2d.hxx>
30 #include <Geom_Curve.hxx>
32 //=======================================================================
33 //function : Partition_Loop3d
35 //=======================================================================
37 Partition_Loop3d::Partition_Loop3d()
41 //=======================================================================
42 //function : AddConstFaces
43 //purpose : Add faces of <S> as unique faces in the result.
44 //=======================================================================
46 void Partition_Loop3d::AddConstFaces(const TopoDS_Shape& S)
48 TopExp_Explorer FaceExp(S, TopAbs_FACE);
49 for (; FaceExp.More(); FaceExp.Next())
50 myFaces.Append( FaceExp.Current() );
52 TopExp::MapShapesAndAncestors(S, TopAbs_EDGE, TopAbs_FACE, myEFMap);
55 //=======================================================================
56 //function : AddSectionFaces
57 //purpose : Add faces of <S> as double faces in the result.
58 //=======================================================================
60 void Partition_Loop3d::AddSectionFaces(const TopoDS_Shape& S)
63 AddConstFaces( S.Reversed() );
66 //=======================================================================
67 //function : MakeShells
68 //purpose : Make and return shells.
69 // <AvoidFacesMap> can contain faces that must not be
70 // added to result shells.
71 //=======================================================================
73 const TopTools_ListOfShape&
74 Partition_Loop3d::MakeShells (const TopTools_MapOfOrientedShape& AvoidFacesMap)
79 TopTools_MapOfShape CheckedEdgesMap;
80 TopTools_MapOfOrientedShape AddedFacesMap;
82 TopTools_ListIteratorOfListOfShape itF (myFaces);
83 for (; itF.More(); itF.Next())
85 const TopoDS_Shape& FF = itF.Value();
86 if (AvoidFacesMap.Contains( FF ) ||
87 ! AddedFacesMap.Add( FF ) )
92 Builder.MakeShell(Shell);
93 Builder.Add(Shell,FF);
95 // clear the maps from shapes added to previous Shell
96 TopTools_MapIteratorOfMapOfShape itEM (CheckedEdgesMap);
97 for (; itEM.More(); itEM.Next()) {
98 TopTools_ListOfShape& FL = myEFMap.ChangeFromKey( itEM.Key());
99 TopTools_ListIteratorOfListOfShape it (FL);
101 if (AddedFacesMap.Contains( it.Value()))
107 CheckedEdgesMap.Clear();
110 // loop on faces added to Shell; add their neighbor faces to Shell and so on
111 TopoDS_Iterator itAddedF (Shell);
112 for (; itAddedF.More(); itAddedF.Next())
114 const TopoDS_Face& F = TopoDS::Face (itAddedF.Value());
116 // loop on edges of F; find a good neighbor face of F by E
117 TopExp_Explorer EdgeExp(F, TopAbs_EDGE);
118 for (; EdgeExp.More(); EdgeExp.Next())
120 const TopoDS_Edge& E = TopoDS::Edge( EdgeExp.Current());
121 if (! CheckedEdgesMap.Add( E ))
124 // candidate faces list
125 const TopTools_ListOfShape& FL = myEFMap.ChangeFromKey(E);
128 // select one of neighbors
130 if (FL.Extent() == 2) {
131 if (! F.IsSame( FL.First() ))
132 SelF = TopoDS::Face( FL.First() );
133 else if (!F.IsSame( FL.Last() ))
134 SelF = TopoDS::Face( FL.Last() );
137 // check if a face already added to Shell shares E
138 TopTools_ListIteratorOfListOfShape it (FL);
139 Standard_Boolean found = Standard_False;
140 for (; !found && it.More(); it.Next())
142 found = AddedFacesMap.Contains( it.Value() );
145 // select basing on geometrical check
146 Standard_Boolean GoodOri, inside;
147 Standard_Real dot, MaxDot = -100;
148 TopTools_ListOfShape TangFL; // tangent faces
149 for ( it.Initialize( FL ) ; it.More(); it.Next()) {
150 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
151 if (NeighborF.IsSame( F ))
153 inside = Partition_Loop3d::IsInside( E, F, NeighborF, 1, dot, GoodOri);
160 if ( IsEqual( dot, MaxDot))
167 if (!TangFL.IsEmpty()) {
168 for (it.Initialize( TangFL ); it.More(); it.Next()) {
169 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
170 if (Partition_Loop3d:: IsInside( E, SelF , NeighborF, 0, dot, GoodOri))
175 if (!SelF.IsNull() &&
176 AddedFacesMap.Add( SelF ) &&
177 !AvoidFacesMap.Contains( SelF ))
178 Builder.Add( Shell, SelF);
180 } // loop on edges of F
182 } // loop on the faces added to Shell
185 myNewShells.Append( Shell );
190 // prepare to the next call
199 //=======================================================================
202 //=======================================================================
204 gp_Vec Partition_Loop3d::Normal(const TopoDS_Edge& E,
205 const TopoDS_Face& F)
208 Standard_Real First, Last;
211 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
212 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
214 gp_Pnt2d p = C2d->Value( 0.5*(First+Last) );
215 Sf->D1(p.X(), p.Y(), Ps, V1, V2);
216 Norm = V1.Crossed(V2);
218 if (F.Orientation() == TopAbs_REVERSED )
224 //=======================================================================
225 //function : NextNormal
226 //purpose : find normal to F at point a little inside F near the middle of E
227 //warning : E must be properly oriented in F.
228 //=======================================================================
230 static gp_Vec NextNormal(const TopoDS_Edge& E,
231 const TopoDS_Face& F)
233 Standard_Real First, Last;
235 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
236 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
240 C2d->D1( 0.5*(First+Last), p, v);
241 if (E.Orientation() != F.Orientation())
243 gp_Dir2d dir( -v.Y(), v.X() ); // dir inside F
245 Standard_Real duv = 1e-6; // this is not Ok and may give incorrect result if
246 // resolutionUV of compared faces is very different. To have a good result,
247 //it is necessary to get normal to faces at points equidistant from E in 3D
249 p.SetX( p.X() + dir.X()*duv );
250 p.SetY( p.Y() + dir.Y()*duv );
253 gp_Vec Norm, V1, V2, VV1, VV2;
254 Sf->D1( p.X(), p.Y(), Ps, V1, V2);
255 Norm = V1.Crossed(V2);
257 if (F.Orientation() == TopAbs_REVERSED )
264 //=======================================================================
265 //function : FindEinF
266 //purpose : find E in F
267 //=======================================================================
269 static TopoDS_Edge FindEinF(const TopoDS_Edge& E,
270 const TopoDS_Face& F)
272 TopExp_Explorer expl (F, TopAbs_EDGE);
273 for (; expl.More(); expl.Next())
274 if( E.IsSame( expl.Current() ))
275 return TopoDS::Edge(expl.Current());
280 //=======================================================================
281 //function : IsInside
282 //purpose : check if <F2> is inside <F1> by edge <E>.
283 // if <CountDot>, compute <Dot>: scalar production of
284 // normalized vectors pointing inside faces, and
285 // check if faces are oriented well for sewing
286 //=======================================================================
288 Standard_Boolean Partition_Loop3d::IsInside(const TopoDS_Edge& E,
289 const TopoDS_Face& F1,
290 const TopoDS_Face& F2,
291 const Standard_Boolean CountDot,
293 Standard_Boolean& GoodOri)
297 gp_Vec Vc1, Vc2, Vin1, Vin2, Nf1, Nf2;
298 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E,f,l);
299 Curve->D1( 0.5*(f + l), P, Vc2);
300 TopoDS_Edge E1, E2 = FindEinF (E, F2);
301 if (E2.Orientation() == TopAbs_REVERSED ) Vc2.Reverse();
307 Nf1.CrossSquareMagnitude(Nf2) / Nf1.SquareMagnitude() / Nf2.SquareMagnitude();
308 Standard_Boolean tangent = sin < 0.001;
310 Standard_Boolean inside = 0;
312 E1 = FindEinF (E, F1);
313 gp_Vec NNf1 = NextNormal(E1,F1);
314 gp_Vec NNf2 = NextNormal(E2,F2);
315 Vin2 = NNf2.Crossed(Vc2);
316 inside = Vin2 * NNf1 < 0;
319 Vin2 = Nf2.Crossed(Vc2);
320 inside = Vin2 * Nf1 < 0;
323 if (!CountDot) return inside;
326 Vin2 = Nf2.Crossed(Vc2);
328 E1 = FindEinF (E, F1);
331 if (E1.Orientation() != E2.Orientation())
333 Vin1 = Nf1.Crossed(Vc1);
336 Standard_Real N1N2 = Nf1 * Nf2;
337 GoodOri = (Vin2 * Vin1 < 0) ? N1N2 > 0 : N1N2 < 0;
340 Standard_Real V1N2 = Vin1 * Nf2;
341 GoodOri = ( inside ? V1N2 <= 0 : V1N2 >= 0);