1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // GEOM PARTITION : partition algorithm
23 // 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>
49 //=======================================================================
50 //function : Partition_Loop3d
52 //=======================================================================
54 Partition_Loop3d::Partition_Loop3d()
58 //=======================================================================
59 //function : AddConstFaces
60 //purpose : Add faces of <S> as unique faces in the result.
61 //=======================================================================
63 void Partition_Loop3d::AddConstFaces(const TopoDS_Shape& S)
65 TopExp_Explorer FaceExp(S, TopAbs_FACE);
66 for (; FaceExp.More(); FaceExp.Next())
67 myFaces.Append( FaceExp.Current() );
69 TopExp::MapShapesAndAncestors(S, TopAbs_EDGE, TopAbs_FACE, myEFMap);
72 //=======================================================================
73 //function : AddSectionFaces
74 //purpose : Add faces of <S> as double faces in the result.
75 //=======================================================================
77 void Partition_Loop3d::AddSectionFaces(const TopoDS_Shape& S)
80 AddConstFaces( S.Reversed() );
83 //=======================================================================
84 //function : MakeShells
85 //purpose : Make and return shells.
86 // <AvoidFacesMap> can contain faces that must not be
87 // added to result shells.
88 //=======================================================================
90 const TopTools_ListOfShape&
91 Partition_Loop3d::MakeShells (const TopTools_MapOfOrientedShape& AvoidFacesMap)
96 TopTools_MapOfShape CheckedEdgesMap;
97 TopTools_MapOfOrientedShape AddedFacesMap;
99 TopTools_ListIteratorOfListOfShape itF (myFaces);
100 for (; itF.More(); itF.Next())
102 const TopoDS_Shape& FF = itF.Value();
103 if (AvoidFacesMap.Contains( FF ) ||
104 ! AddedFacesMap.Add( FF ) )
109 Builder.MakeShell(Shell);
110 Builder.Add(Shell,FF);
112 // clear the maps from shapes added to previous Shell
113 TopTools_MapIteratorOfMapOfShape itEM (CheckedEdgesMap);
114 for (; itEM.More(); itEM.Next()) {
115 TopTools_ListOfShape& FL = myEFMap.ChangeFromKey( itEM.Key());
116 TopTools_ListIteratorOfListOfShape it (FL);
118 if (AddedFacesMap.Contains( it.Value()))
124 CheckedEdgesMap.Clear();
127 // loop on faces added to Shell; add their neighbor faces to Shell and so on
128 TopoDS_Iterator itAddedF (Shell);
129 for (; itAddedF.More(); itAddedF.Next())
131 const TopoDS_Face& F = TopoDS::Face (itAddedF.Value());
133 // loop on edges of F; find a good neighbor face of F by E
134 TopExp_Explorer EdgeExp(F, TopAbs_EDGE);
135 for (; EdgeExp.More(); EdgeExp.Next())
137 const TopoDS_Edge& E = TopoDS::Edge( EdgeExp.Current());
138 if (! CheckedEdgesMap.Add( E ))
141 // candidate faces list
142 const TopTools_ListOfShape& FL = myEFMap.ChangeFromKey(E);
145 // select one of neighbors
147 if (FL.Extent() == 2) {
148 if (! F.IsSame( FL.First() ))
149 SelF = TopoDS::Face( FL.First() );
150 else if (!F.IsSame( FL.Last() ))
151 SelF = TopoDS::Face( FL.Last() );
154 // check if a face already added to Shell shares E
155 TopTools_ListIteratorOfListOfShape it (FL);
156 Standard_Boolean found = Standard_False;
157 for (; !found && it.More(); it.Next())
159 found = AddedFacesMap.Contains( it.Value() );
162 // select basing on geometrical check
163 Standard_Boolean GoodOri, inside;
164 Standard_Real dot, MaxDot = -100;
165 TopTools_ListOfShape TangFL; // tangent faces
166 for ( it.Initialize( FL ) ; it.More(); it.Next()) {
167 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
168 if (NeighborF.IsSame( F ))
170 inside = Partition_Loop3d::IsInside( E, F, NeighborF, 1, dot, GoodOri);
177 if ( IsEqual( dot, MaxDot))
184 if (!TangFL.IsEmpty()) {
185 for (it.Initialize( TangFL ); it.More(); it.Next()) {
186 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
187 if (Partition_Loop3d:: IsInside( E, SelF , NeighborF, 0, dot, GoodOri))
192 if (!SelF.IsNull() &&
193 AddedFacesMap.Add( SelF ) &&
194 !AvoidFacesMap.Contains( SelF ))
195 Builder.Add( Shell, SelF);
197 } // loop on edges of F
199 } // loop on the faces added to Shell
202 myNewShells.Append( Shell );
207 // prepare to the next call
216 //=======================================================================
219 //=======================================================================
221 gp_Vec Partition_Loop3d::Normal(const TopoDS_Edge& E,
222 const TopoDS_Face& F)
225 Standard_Real First, Last;
228 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
229 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
231 gp_Pnt2d p = C2d->Value( 0.5*(First+Last) );
232 Sf->D1(p.X(), p.Y(), Ps, V1, V2);
233 Norm = V1.Crossed(V2);
235 if (F.Orientation() == TopAbs_REVERSED )
241 //=======================================================================
242 //function : NextNormal
243 //purpose : find normal to F at point a little inside F near the middle of E
244 //warning : E must be properly oriented in F.
245 //=======================================================================
247 static gp_Vec NextNormal(const TopoDS_Edge& E,
248 const TopoDS_Face& F)
250 Standard_Real First, Last;
252 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
253 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
257 C2d->D1( 0.5*(First+Last), p, v);
258 if (E.Orientation() != F.Orientation())
260 gp_Dir2d dir( -v.Y(), v.X() ); // dir inside F
262 Standard_Real duv = 1e-6; // this is not Ok and may give incorrect result if
263 // resolutionUV of compared faces is very different. To have a good result,
264 //it is necessary to get normal to faces at points equidistant from E in 3D
266 p.SetX( p.X() + dir.X()*duv );
267 p.SetY( p.Y() + dir.Y()*duv );
270 gp_Vec Norm, V1, V2, VV1, VV2;
271 Sf->D1( p.X(), p.Y(), Ps, V1, V2);
272 Norm = V1.Crossed(V2);
274 if (F.Orientation() == TopAbs_REVERSED )
281 //=======================================================================
282 //function : FindEinF
283 //purpose : find E in F
284 //=======================================================================
286 static TopoDS_Edge FindEinF(const TopoDS_Edge& E,
287 const TopoDS_Face& F)
289 TopExp_Explorer expl (F, TopAbs_EDGE);
290 for (; expl.More(); expl.Next())
291 if( E.IsSame( expl.Current() ))
292 return TopoDS::Edge(expl.Current());
297 //=======================================================================
298 //function : IsInside
299 //purpose : check if <F2> is inside <F1> by edge <E>.
300 // if <CountDot>, compute <Dot>: scalar production of
301 // normalized vectors pointing inside faces, and
302 // check if faces are oriented well for sewing
303 //=======================================================================
305 Standard_Boolean Partition_Loop3d::IsInside(const TopoDS_Edge& E,
306 const TopoDS_Face& F1,
307 const TopoDS_Face& F2,
308 const Standard_Boolean CountDot,
310 Standard_Boolean& GoodOri)
314 gp_Vec Vc1, Vc2, Vin1, Vin2, Nf1, Nf2;
315 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E,f,l);
316 Curve->D1( 0.5*(f + l), P, Vc2);
317 TopoDS_Edge E1, E2 = FindEinF (E, F2);
318 if (E2.Orientation() == TopAbs_REVERSED ) Vc2.Reverse();
324 Nf1.CrossSquareMagnitude(Nf2) / Nf1.SquareMagnitude() / Nf2.SquareMagnitude();
325 Standard_Boolean tangent = sin < 0.001;
327 Standard_Boolean inside = 0;
329 E1 = FindEinF (E, F1);
330 gp_Vec NNf1 = NextNormal(E1,F1);
331 gp_Vec NNf2 = NextNormal(E2,F2);
332 Vin2 = NNf2.Crossed(Vc2);
333 inside = Vin2 * NNf1 < 0;
336 Vin2 = Nf2.Crossed(Vc2);
337 inside = Vin2 * Nf1 < 0;
340 if (!CountDot) return inside;
343 Vin2 = Nf2.Crossed(Vc2);
345 E1 = FindEinF (E, F1);
348 if (E1.Orientation() != E2.Orientation())
350 Vin1 = Nf1.Crossed(Vc1);
353 Standard_Real N1N2 = Nf1 * Nf2;
354 GoodOri = (Vin2 * Vin1 < 0) ? N1N2 > 0 : N1N2 < 0;
357 Standard_Real V1N2 = Vin1 * Nf2;
358 GoodOri = ( inside ? V1N2 <= 0 : V1N2 >= 0);