1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // GEOM PARTITION : partition algorithm
22 // File : Partition_Loop3d.cxx
26 #include "Partition_Loop3d.ixx"
28 #include <TopExp_Explorer.hxx>
30 #include <BRep_Builder.hxx>
31 #include <TopTools_MapOfShape.hxx>
32 #include <TopTools_ListIteratorOfListOfShape.hxx>
33 #include <TopoDS_Shell.hxx>
34 #include <TopoDS_Iterator.hxx>
36 #include <TopTools_MapIteratorOfMapOfShape.hxx>
39 #include <Geom2d_Curve.hxx>
40 #include <BRep_Tool.hxx>
41 #include <Geom_Surface.hxx>
42 #include <gp_Pnt2d.hxx>
43 #include <gp_Vec2d.hxx>
44 #include <gp_Dir2d.hxx>
45 #include <Geom_Curve.hxx>
47 //=======================================================================
48 //function : Partition_Loop3d
50 //=======================================================================
52 Partition_Loop3d::Partition_Loop3d()
56 //=======================================================================
57 //function : AddConstFaces
58 //purpose : Add faces of <S> as unique faces in the result.
59 //=======================================================================
61 void Partition_Loop3d::AddConstFaces(const TopoDS_Shape& S)
63 TopExp_Explorer FaceExp(S, TopAbs_FACE);
64 for (; FaceExp.More(); FaceExp.Next())
65 myFaces.Append( FaceExp.Current() );
67 TopExp::MapShapesAndAncestors(S, TopAbs_EDGE, TopAbs_FACE, myEFMap);
70 //=======================================================================
71 //function : AddSectionFaces
72 //purpose : Add faces of <S> as double faces in the result.
73 //=======================================================================
75 void Partition_Loop3d::AddSectionFaces(const TopoDS_Shape& S)
78 AddConstFaces( S.Reversed() );
81 //=======================================================================
82 //function : MakeShells
83 //purpose : Make and return shells.
84 // <AvoidFacesMap> can contain faces that must not be
85 // added to result shells.
86 //=======================================================================
88 const TopTools_ListOfShape&
89 Partition_Loop3d::MakeShells (const TopTools_MapOfOrientedShape& AvoidFacesMap)
94 TopTools_MapOfShape CheckedEdgesMap;
95 TopTools_MapOfOrientedShape AddedFacesMap;
97 TopTools_ListIteratorOfListOfShape itF (myFaces);
98 for (; itF.More(); itF.Next())
100 const TopoDS_Shape& FF = itF.Value();
101 if (AvoidFacesMap.Contains( FF ) ||
102 ! AddedFacesMap.Add( FF ) )
107 Builder.MakeShell(Shell);
108 Builder.Add(Shell,FF);
110 // clear the maps from shapes added to previous Shell
111 TopTools_MapIteratorOfMapOfShape itEM (CheckedEdgesMap);
112 for (; itEM.More(); itEM.Next()) {
113 TopTools_ListOfShape& FL = myEFMap.ChangeFromKey( itEM.Key());
114 TopTools_ListIteratorOfListOfShape it (FL);
116 if (AddedFacesMap.Contains( it.Value()))
122 CheckedEdgesMap.Clear();
125 // loop on faces added to Shell; add their neighbor faces to Shell and so on
126 TopoDS_Iterator itAddedF (Shell);
127 for (; itAddedF.More(); itAddedF.Next())
129 const TopoDS_Face& F = TopoDS::Face (itAddedF.Value());
131 // loop on edges of F; find a good neighbor face of F by E
132 TopExp_Explorer EdgeExp(F, TopAbs_EDGE);
133 for (; EdgeExp.More(); EdgeExp.Next())
135 const TopoDS_Edge& E = TopoDS::Edge( EdgeExp.Current());
136 if (! CheckedEdgesMap.Add( E ))
139 // candidate faces list
140 const TopTools_ListOfShape& FL = myEFMap.ChangeFromKey(E);
143 // select one of neighbors
145 if (FL.Extent() == 2) {
146 if (! F.IsSame( FL.First() ))
147 SelF = TopoDS::Face( FL.First() );
148 else if (!F.IsSame( FL.Last() ))
149 SelF = TopoDS::Face( FL.Last() );
152 // check if a face already added to Shell shares E
153 TopTools_ListIteratorOfListOfShape it (FL);
154 Standard_Boolean found = Standard_False;
155 for (; !found && it.More(); it.Next())
157 found = AddedFacesMap.Contains( it.Value() );
160 // select basing on geometrical check
161 Standard_Boolean GoodOri, inside;
162 Standard_Real dot, MaxDot = -100;
163 TopTools_ListOfShape TangFL; // tangent faces
164 for ( it.Initialize( FL ) ; it.More(); it.Next()) {
165 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
166 if (NeighborF.IsSame( F ))
168 inside = Partition_Loop3d::IsInside( E, F, NeighborF, 1, dot, GoodOri);
175 if ( IsEqual( dot, MaxDot))
182 if (!TangFL.IsEmpty()) {
183 for (it.Initialize( TangFL ); it.More(); it.Next()) {
184 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
185 if (Partition_Loop3d:: IsInside( E, SelF , NeighborF, 0, dot, GoodOri))
190 if (!SelF.IsNull() &&
191 AddedFacesMap.Add( SelF ) &&
192 !AvoidFacesMap.Contains( SelF ))
193 Builder.Add( Shell, SelF);
195 } // loop on edges of F
197 } // loop on the faces added to Shell
200 myNewShells.Append( Shell );
205 // prepare to the next call
214 //=======================================================================
217 //=======================================================================
219 gp_Vec Partition_Loop3d::Normal(const TopoDS_Edge& E,
220 const TopoDS_Face& F)
223 Standard_Real First, Last;
226 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
227 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
229 gp_Pnt2d p = C2d->Value( 0.5*(First+Last) );
230 Sf->D1(p.X(), p.Y(), Ps, V1, V2);
231 Norm = V1.Crossed(V2);
233 if (F.Orientation() == TopAbs_REVERSED )
239 //=======================================================================
240 //function : NextNormal
241 //purpose : find normal to F at point a little inside F near the middle of E
242 //warning : E must be properly oriented in F.
243 //=======================================================================
245 static gp_Vec NextNormal(const TopoDS_Edge& E,
246 const TopoDS_Face& F)
248 Standard_Real First, Last;
250 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
251 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
255 C2d->D1( 0.5*(First+Last), p, v);
256 if (E.Orientation() != F.Orientation())
258 gp_Dir2d dir( -v.Y(), v.X() ); // dir inside F
260 Standard_Real duv = 1e-6; // this is not Ok and may give incorrect result if
261 // resolutionUV of compared faces is very different. To have a good result,
262 //it is necessary to get normal to faces at points equidistant from E in 3D
264 p.SetX( p.X() + dir.X()*duv );
265 p.SetY( p.Y() + dir.Y()*duv );
268 gp_Vec Norm, V1, V2, VV1, VV2;
269 Sf->D1( p.X(), p.Y(), Ps, V1, V2);
270 Norm = V1.Crossed(V2);
272 if (F.Orientation() == TopAbs_REVERSED )
279 //=======================================================================
280 //function : FindEinF
281 //purpose : find E in F
282 //=======================================================================
284 static TopoDS_Edge FindEinF(const TopoDS_Edge& E,
285 const TopoDS_Face& F)
287 TopExp_Explorer expl (F, TopAbs_EDGE);
288 for (; expl.More(); expl.Next())
289 if( E.IsSame( expl.Current() ))
290 return TopoDS::Edge(expl.Current());
295 //=======================================================================
296 //function : IsInside
297 //purpose : check if <F2> is inside <F1> by edge <E>.
298 // if <CountDot>, compute <Dot>: scalar production of
299 // normalized vectors pointing inside faces, and
300 // check if faces are oriented well for sewing
301 //=======================================================================
303 Standard_Boolean Partition_Loop3d::IsInside(const TopoDS_Edge& E,
304 const TopoDS_Face& F1,
305 const TopoDS_Face& F2,
306 const Standard_Boolean CountDot,
308 Standard_Boolean& GoodOri)
312 gp_Vec Vc1, Vc2, Vin1, Vin2, Nf1, Nf2;
313 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E,f,l);
314 Curve->D1( 0.5*(f + l), P, Vc2);
315 TopoDS_Edge E1, E2 = FindEinF (E, F2);
316 if (E2.Orientation() == TopAbs_REVERSED ) Vc2.Reverse();
322 Nf1.CrossSquareMagnitude(Nf2) / Nf1.SquareMagnitude() / Nf2.SquareMagnitude();
323 Standard_Boolean tangent = sin < 0.001;
325 Standard_Boolean inside = 0;
327 E1 = FindEinF (E, F1);
328 gp_Vec NNf1 = NextNormal(E1,F1);
329 gp_Vec NNf2 = NextNormal(E2,F2);
330 Vin2 = NNf2.Crossed(Vc2);
331 inside = Vin2 * NNf1 < 0;
334 Vin2 = Nf2.Crossed(Vc2);
335 inside = Vin2 * Nf1 < 0;
338 if (!CountDot) return inside;
341 Vin2 = Nf2.Crossed(Vc2);
343 E1 = FindEinF (E, F1);
346 if (E1.Orientation() != E2.Orientation())
348 Vin1 = Nf1.Crossed(Vc1);
351 Standard_Real N1N2 = Nf1 * Nf2;
352 GoodOri = (Vin2 * Vin1 < 0) ? N1N2 > 0 : N1N2 < 0;
355 Standard_Real V1N2 = Vin1 * Nf2;
356 GoodOri = ( inside ? V1N2 <= 0 : V1N2 >= 0);