]> SALOME platform Git repositories - modules/geom.git/blob - src/PARTITION/Partition_Loop3d.cxx
Salome HOME
NRI : First integration.
[modules/geom.git] / src / PARTITION / Partition_Loop3d.cxx
1 using namespace std;
2 //  File      : Partition_Loop3d.cxx
3 //  Created   : Mon Oct 21 09:47:19 2002
4 //  Copyright : Open CASCADE
5
6 #include "Partition_Loop3d.ixx"
7
8 #include <TopExp_Explorer.hxx>
9 #include <TopExp.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>
15 #include <TopoDS.hxx>
16 #include <TopTools_MapIteratorOfMapOfShape.hxx>
17 #include <gp_Vec.hxx>
18 #include <gp_Pnt.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>
26
27 //=======================================================================
28 //function : Partition_Loop3d
29 //purpose  : 
30 //=======================================================================
31
32 Partition_Loop3d::Partition_Loop3d()
33 {
34 }
35
36 //=======================================================================
37 //function : AddConstFaces
38 //purpose  : Add faces of <S> as unique faces in the result.
39 //=======================================================================
40
41 void Partition_Loop3d::AddConstFaces(const TopoDS_Shape& S) 
42 {
43   TopExp_Explorer FaceExp(S, TopAbs_FACE);
44   for (; FaceExp.More(); FaceExp.Next())
45     myFaces.Append( FaceExp.Current() );
46
47   TopExp::MapShapesAndAncestors(S, TopAbs_EDGE, TopAbs_FACE, myEFMap);
48 }
49
50 //=======================================================================
51 //function : AddSectionFaces
52 //purpose  : Add faces of <S> as double faces in the result.
53 //=======================================================================
54
55 void Partition_Loop3d::AddSectionFaces(const TopoDS_Shape& S) 
56 {
57   AddConstFaces( S );
58   AddConstFaces( S.Reversed() );
59 }
60
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
68 const TopTools_ListOfShape&
69   Partition_Loop3d::MakeShells (const TopTools_MapOfOrientedShape& AvoidFacesMap)
70 {
71   myNewShells.Clear();
72   
73   BRep_Builder Builder;
74   TopTools_MapOfShape CheckedEdgesMap;
75   TopTools_MapOfOrientedShape AddedFacesMap;
76   
77   TopTools_ListIteratorOfListOfShape itF (myFaces);
78   for (; itF.More(); itF.Next())
79   {
80     const TopoDS_Shape& FF = itF.Value();
81     if (AvoidFacesMap.Contains( FF ) ||
82         ! AddedFacesMap.Add( FF ) )
83       continue;
84
85     // make a new shell
86     TopoDS_Shell Shell;
87     Builder.MakeShell(Shell);
88     Builder.Add(Shell,FF);
89
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);
95       while ( it.More()) {
96         if (AddedFacesMap.Contains( it.Value()))
97           FL.Remove( it );
98         else
99           it.Next();
100       }
101     }
102     CheckedEdgesMap.Clear();
103
104     
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())
108     {
109       const TopoDS_Face& F = TopoDS::Face (itAddedF.Value());
110
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())
114       {
115         const TopoDS_Edge& E = TopoDS::Edge( EdgeExp.Current());
116         if (! CheckedEdgesMap.Add( E ))
117           continue;
118
119         // candidate faces list
120         const TopTools_ListOfShape& FL = myEFMap.ChangeFromKey(E);
121         if (FL.IsEmpty())
122           continue;
123         // select one of neighbors
124         TopoDS_Face SelF;
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() );
130         }
131         else {
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())
136             if (F != it.Value())
137               found = AddedFacesMap.Contains( it.Value() );
138           if (found)
139             continue;
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 ))
147               continue;
148             inside = Partition_Loop3d::IsInside( E, F, NeighborF, 1, dot, GoodOri);
149             if (!GoodOri)
150               continue;
151             if (!inside)
152               dot = -dot - 3;
153             if (dot < MaxDot)
154               continue;
155             if ( IsEqual( dot, MaxDot))
156               TangFL.Append(SelF);
157             else
158               TangFL.Clear();
159             MaxDot = dot;
160             SelF = NeighborF;
161           }
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))
166                 SelF = NeighborF;
167             }
168           }
169         }
170         if (!SelF.IsNull() &&
171             AddedFacesMap.Add( SelF ) &&
172             !AvoidFacesMap.Contains( SelF )) 
173           Builder.Add( Shell, SelF);
174
175       } // loop on edges of F
176       
177     } // loop on the faces added to Shell
178
179     // Shell is complete
180     myNewShells.Append( Shell );
181
182   } // loop on myFaces
183
184
185   // prepare to the next call
186   myFaces.Clear();
187   myEFMap.Clear();
188
189   return myNewShells;
190 }
191
192
193
194 //=======================================================================
195 //function : Normal
196 //purpose  : 
197 //=======================================================================
198
199 gp_Vec Partition_Loop3d::Normal(const TopoDS_Edge& E,
200                                 const TopoDS_Face& F)
201 {
202   gp_Vec Norm, V1, V2;
203   Standard_Real First, Last;
204   gp_Pnt Ps;
205
206   Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
207   Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
208
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);
212
213   if (F.Orientation() == TopAbs_REVERSED ) 
214     Norm.Reverse();
215
216   return Norm;
217 }
218
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 //=======================================================================
224
225 static gp_Vec NextNormal(const TopoDS_Edge& E,
226                          const TopoDS_Face& F)
227 {
228   Standard_Real First, Last;
229
230   Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
231   Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
232
233   gp_Pnt2d p;
234   gp_Vec2d v;
235   C2d->D1( 0.5*(First+Last), p, v);
236   if (E.Orientation() != F.Orientation())
237     v.Reverse();
238   gp_Dir2d dir( -v.Y(), v.X() ); // dir inside F
239   
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
243   
244   p.SetX( p.X() + dir.X()*duv );
245   p.SetY( p.Y() + dir.Y()*duv );
246   
247   gp_Pnt Ps;
248   gp_Vec Norm, V1, V2, VV1, VV2;
249   Sf->D1( p.X(), p.Y(), Ps, V1, V2);
250   Norm = V1.Crossed(V2);
251
252   if (F.Orientation() == TopAbs_REVERSED ) 
253     Norm.Reverse();
254
255   return Norm;
256 }
257
258
259 //=======================================================================
260 //function : FindEinF
261 //purpose  : find E in F
262 //=======================================================================
263
264 static TopoDS_Edge FindEinF(const TopoDS_Edge& E,
265                             const TopoDS_Face& F)
266 {
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());
271   TopoDS_Edge nullE;
272   return nullE;
273 }
274
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 //=======================================================================
282
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,
287                                             Standard_Real& Dot,
288                                             Standard_Boolean& GoodOri) 
289 {
290   Standard_Real f, l;
291   gp_Pnt P;
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();
297
298   Nf1 = Normal(E,F1);
299   Nf2 = Normal(E,F2);
300
301   Standard_Real sin =
302     Nf1.CrossSquareMagnitude(Nf2) / Nf1.SquareMagnitude() / Nf2.SquareMagnitude();
303   Standard_Boolean tangent = sin < 0.001;
304
305   Standard_Boolean inside = 0;
306   if (tangent) {
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;
312   }
313   else {
314     Vin2 = Nf2.Crossed(Vc2);
315     inside = Vin2 * Nf1 < 0;
316   }
317   
318   if (!CountDot) return inside;
319
320   if (tangent)
321     Vin2 = Nf2.Crossed(Vc2);
322   else
323     E1 = FindEinF (E, F1);
324     
325   Vc1 = Vc2;
326   if (E1.Orientation() != E2.Orientation()) 
327     Vc1.Reverse();
328   Vin1 = Nf1.Crossed(Vc1);
329
330   if (tangent) {
331     Standard_Real N1N2 = Nf1 * Nf2;
332     GoodOri = (Vin2 * Vin1 < 0) ? N1N2 > 0 : N1N2 < 0;
333   }
334   else {
335     Standard_Real V1N2 = Vin1 * Nf2;
336     GoodOri = ( inside ? V1N2 <= 0 : V1N2 >= 0);
337   }
338
339   Vin1.Normalize();
340   Vin2.Normalize();
341   
342   Dot = Vin2 * Vin1;
343   
344   return inside;
345 }
346