2 // File : Partition_Loop3d.cxx
3 // Created : Mon Oct 21 09:47:19 2002
4 // Copyright : Open CASCADE
6 #include "Partition_Loop3d.ixx"
8 #include <TopExp_Explorer.hxx>
10 #include <BRep_Builder.hxx>
11 #include <TopTools_MapOfShape.hxx>
12 #include <TopTools_ListIteratorOfListOfShape.hxx>
13 #include <TopoDS_Shell.hxx>
14 #include <TopoDS_Iterator.hxx>
16 #include <TopTools_MapIteratorOfMapOfShape.hxx>
19 #include <Geom2d_Curve.hxx>
20 #include <BRep_Tool.hxx>
21 #include <Geom_Surface.hxx>
22 #include <gp_Pnt2d.hxx>
23 #include <gp_Vec2d.hxx>
24 #include <gp_Dir2d.hxx>
25 #include <Geom_Curve.hxx>
27 //=======================================================================
28 //function : Partition_Loop3d
30 //=======================================================================
32 Partition_Loop3d::Partition_Loop3d()
36 //=======================================================================
37 //function : AddConstFaces
38 //purpose : Add faces of <S> as unique faces in the result.
39 //=======================================================================
41 void Partition_Loop3d::AddConstFaces(const TopoDS_Shape& S)
43 TopExp_Explorer FaceExp(S, TopAbs_FACE);
44 for (; FaceExp.More(); FaceExp.Next())
45 myFaces.Append( FaceExp.Current() );
47 TopExp::MapShapesAndAncestors(S, TopAbs_EDGE, TopAbs_FACE, myEFMap);
50 //=======================================================================
51 //function : AddSectionFaces
52 //purpose : Add faces of <S> as double faces in the result.
53 //=======================================================================
55 void Partition_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 //=======================================================================
68 const TopTools_ListOfShape&
69 Partition_Loop3d::MakeShells (const TopTools_MapOfOrientedShape& AvoidFacesMap)
74 TopTools_MapOfShape CheckedEdgesMap;
75 TopTools_MapOfOrientedShape AddedFacesMap;
77 TopTools_ListIteratorOfListOfShape itF (myFaces);
78 for (; itF.More(); itF.Next())
80 const TopoDS_Shape& FF = itF.Value();
81 if (AvoidFacesMap.Contains( FF ) ||
82 ! AddedFacesMap.Add( FF ) )
87 Builder.MakeShell(Shell);
88 Builder.Add(Shell,FF);
90 // clear the maps from shapes added to previous Shell
91 TopTools_MapIteratorOfMapOfShape itEM (CheckedEdgesMap);
92 for (; itEM.More(); itEM.Next()) {
93 TopTools_ListOfShape& FL = myEFMap.ChangeFromKey( itEM.Key());
94 TopTools_ListIteratorOfListOfShape it (FL);
96 if (AddedFacesMap.Contains( it.Value()))
102 CheckedEdgesMap.Clear();
105 // loop on faces added to Shell; add their neighbor faces to Shell and so on
106 TopoDS_Iterator itAddedF (Shell);
107 for (; itAddedF.More(); itAddedF.Next())
109 const TopoDS_Face& F = TopoDS::Face (itAddedF.Value());
111 // loop on edges of F; find a good neighbor face of F by E
112 TopExp_Explorer EdgeExp(F, TopAbs_EDGE);
113 for (; EdgeExp.More(); EdgeExp.Next())
115 const TopoDS_Edge& E = TopoDS::Edge( EdgeExp.Current());
116 if (! CheckedEdgesMap.Add( E ))
119 // candidate faces list
120 const TopTools_ListOfShape& FL = myEFMap.ChangeFromKey(E);
123 // select one of neighbors
125 if (FL.Extent() == 2) {
126 if (! F.IsSame( FL.First() ))
127 SelF = TopoDS::Face( FL.First() );
128 else if (!F.IsSame( FL.Last() ))
129 SelF = TopoDS::Face( FL.Last() );
132 // check if a face already added to Shell shares E
133 TopTools_ListIteratorOfListOfShape it (FL);
134 Standard_Boolean found = Standard_False;
135 for (; !found && it.More(); it.Next())
137 found = AddedFacesMap.Contains( it.Value() );
140 // select basing on geometrical check
141 Standard_Boolean GoodOri, inside;
142 Standard_Real dot, MaxDot = -100;
143 TopTools_ListOfShape TangFL; // tangent faces
144 for ( it.Initialize( FL ) ; it.More(); it.Next()) {
145 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
146 if (NeighborF.IsSame( F ))
148 inside = Partition_Loop3d::IsInside( E, F, NeighborF, 1, dot, GoodOri);
155 if ( IsEqual( dot, MaxDot))
162 if (!TangFL.IsEmpty()) {
163 for (it.Initialize( TangFL ); it.More(); it.Next()) {
164 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
165 if (Partition_Loop3d:: IsInside( E, SelF , NeighborF, 0, dot, GoodOri))
170 if (!SelF.IsNull() &&
171 AddedFacesMap.Add( SelF ) &&
172 !AvoidFacesMap.Contains( SelF ))
173 Builder.Add( Shell, SelF);
175 } // loop on edges of F
177 } // loop on the faces added to Shell
180 myNewShells.Append( Shell );
185 // prepare to the next call
194 //=======================================================================
197 //=======================================================================
199 gp_Vec Partition_Loop3d::Normal(const TopoDS_Edge& E,
200 const TopoDS_Face& F)
203 Standard_Real First, Last;
206 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
207 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
209 gp_Pnt2d p = C2d->Value( 0.5*(First+Last) );
210 Sf->D1(p.X(), p.Y(), Ps, V1, V2);
211 Norm = V1.Crossed(V2);
213 if (F.Orientation() == TopAbs_REVERSED )
219 //=======================================================================
220 //function : NextNormal
221 //purpose : find normal to F at point a little inside F near the middle of E
222 //warning : E must be properly oriented in F.
223 //=======================================================================
225 static gp_Vec NextNormal(const TopoDS_Edge& E,
226 const TopoDS_Face& F)
228 Standard_Real First, Last;
230 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
231 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
235 C2d->D1( 0.5*(First+Last), p, v);
236 if (E.Orientation() != F.Orientation())
238 gp_Dir2d dir( -v.Y(), v.X() ); // dir inside F
240 Standard_Real duv = 1e-6; // this is not Ok and may give incorrect result if
241 // resolutionUV of compared faces is very different. To have a good result,
242 //it is necessary to get normal to faces at points equidistant from E in 3D
244 p.SetX( p.X() + dir.X()*duv );
245 p.SetY( p.Y() + dir.Y()*duv );
248 gp_Vec Norm, V1, V2, VV1, VV2;
249 Sf->D1( p.X(), p.Y(), Ps, V1, V2);
250 Norm = V1.Crossed(V2);
252 if (F.Orientation() == TopAbs_REVERSED )
259 //=======================================================================
260 //function : FindEinF
261 //purpose : find E in F
262 //=======================================================================
264 static TopoDS_Edge FindEinF(const TopoDS_Edge& E,
265 const TopoDS_Face& F)
267 TopExp_Explorer expl (F, TopAbs_EDGE);
268 for (; expl.More(); expl.Next())
269 if( E.IsSame( expl.Current() ))
270 return TopoDS::Edge(expl.Current());
275 //=======================================================================
276 //function : IsInside
277 //purpose : check if <F2> is inside <F1> by edge <E>.
278 // if <CountDot>, compute <Dot>: scalar production of
279 // normalized vectors pointing inside faces, and
280 // check if faces are oriented well for sewing
281 //=======================================================================
283 Standard_Boolean Partition_Loop3d::IsInside(const TopoDS_Edge& E,
284 const TopoDS_Face& F1,
285 const TopoDS_Face& F2,
286 const Standard_Boolean CountDot,
288 Standard_Boolean& GoodOri)
292 gp_Vec Vc1, Vc2, Vin1, Vin2, Nf1, Nf2;
293 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E,f,l);
294 Curve->D1( 0.5*(f + l), P, Vc2);
295 TopoDS_Edge E1, E2 = FindEinF (E, F2);
296 if (E2.Orientation() == TopAbs_REVERSED ) Vc2.Reverse();
302 Nf1.CrossSquareMagnitude(Nf2) / Nf1.SquareMagnitude() / Nf2.SquareMagnitude();
303 Standard_Boolean tangent = sin < 0.001;
305 Standard_Boolean inside = 0;
307 E1 = FindEinF (E, F1);
308 gp_Vec NNf1 = NextNormal(E1,F1);
309 gp_Vec NNf2 = NextNormal(E2,F2);
310 Vin2 = NNf2.Crossed(Vc2);
311 inside = Vin2 * NNf1 < 0;
314 Vin2 = Nf2.Crossed(Vc2);
315 inside = Vin2 * Nf1 < 0;
318 if (!CountDot) return inside;
321 Vin2 = Nf2.Crossed(Vc2);
323 E1 = FindEinF (E, F1);
326 if (E1.Orientation() != E2.Orientation())
328 Vin1 = Nf1.Crossed(Vc1);
331 Standard_Real N1N2 = Nf1 * Nf2;
332 GoodOri = (Vin2 * Vin1 < 0) ? N1N2 > 0 : N1N2 < 0;
335 Standard_Real V1N2 = Vin1 * Nf2;
336 GoodOri = ( inside ? V1N2 <= 0 : V1N2 >= 0);