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
25 #include "Partition_Loop3d.ixx"
27 #include <TopExp_Explorer.hxx>
29 #include <BRep_Builder.hxx>
30 #include <TopTools_MapOfShape.hxx>
31 #include <TopTools_ListIteratorOfListOfShape.hxx>
32 #include <TopoDS_Shell.hxx>
33 #include <TopoDS_Iterator.hxx>
35 #include <TopTools_MapIteratorOfMapOfShape.hxx>
38 #include <Geom2d_Curve.hxx>
39 #include <BRep_Tool.hxx>
40 #include <Geom_Surface.hxx>
41 #include <gp_Pnt2d.hxx>
42 #include <gp_Vec2d.hxx>
43 #include <gp_Dir2d.hxx>
44 #include <Geom_Curve.hxx>
48 //=======================================================================
49 //function : Partition_Loop3d
51 //=======================================================================
53 Partition_Loop3d::Partition_Loop3d()
57 //=======================================================================
58 //function : AddConstFaces
59 //purpose : Add faces of <S> as unique faces in the result.
60 //=======================================================================
62 void Partition_Loop3d::AddConstFaces(const TopoDS_Shape& S)
64 TopExp_Explorer FaceExp(S, TopAbs_FACE);
65 for (; FaceExp.More(); FaceExp.Next())
66 myFaces.Append( FaceExp.Current() );
68 TopExp::MapShapesAndAncestors(S, TopAbs_EDGE, TopAbs_FACE, myEFMap);
71 //=======================================================================
72 //function : AddSectionFaces
73 //purpose : Add faces of <S> as double faces in the result.
74 //=======================================================================
76 void Partition_Loop3d::AddSectionFaces(const TopoDS_Shape& S)
79 AddConstFaces( S.Reversed() );
82 //=======================================================================
83 //function : MakeShells
84 //purpose : Make and return shells.
85 // <AvoidFacesMap> can contain faces that must not be
86 // added to result shells.
87 //=======================================================================
89 const TopTools_ListOfShape&
90 Partition_Loop3d::MakeShells (const TopTools_MapOfOrientedShape& AvoidFacesMap)
95 TopTools_MapOfShape CheckedEdgesMap;
96 TopTools_MapOfOrientedShape AddedFacesMap;
98 TopTools_ListIteratorOfListOfShape itF (myFaces);
99 for (; itF.More(); itF.Next())
101 const TopoDS_Shape& FF = itF.Value();
102 if (AvoidFacesMap.Contains( FF ) ||
103 ! AddedFacesMap.Add( FF ) )
108 Builder.MakeShell(Shell);
109 Builder.Add(Shell,FF);
111 // clear the maps from shapes added to previous Shell
112 TopTools_MapIteratorOfMapOfShape itEM (CheckedEdgesMap);
113 for (; itEM.More(); itEM.Next()) {
114 TopTools_ListOfShape& FL = myEFMap.ChangeFromKey( itEM.Key());
115 TopTools_ListIteratorOfListOfShape it (FL);
117 if (AddedFacesMap.Contains( it.Value()))
123 CheckedEdgesMap.Clear();
126 // loop on faces added to Shell; add their neighbor faces to Shell and so on
127 TopoDS_Iterator itAddedF (Shell);
128 for (; itAddedF.More(); itAddedF.Next())
130 const TopoDS_Face& F = TopoDS::Face (itAddedF.Value());
132 // loop on edges of F; find a good neighbor face of F by E
133 TopExp_Explorer EdgeExp(F, TopAbs_EDGE);
134 for (; EdgeExp.More(); EdgeExp.Next())
136 const TopoDS_Edge& E = TopoDS::Edge( EdgeExp.Current());
137 if (! CheckedEdgesMap.Add( E ))
140 // candidate faces list
141 const TopTools_ListOfShape& FL = myEFMap.ChangeFromKey(E);
144 // select one of neighbors
146 if (FL.Extent() == 2) {
147 if (! F.IsSame( FL.First() ))
148 SelF = TopoDS::Face( FL.First() );
149 else if (!F.IsSame( FL.Last() ))
150 SelF = TopoDS::Face( FL.Last() );
153 // check if a face already added to Shell shares E
154 TopTools_ListIteratorOfListOfShape it (FL);
155 Standard_Boolean found = Standard_False;
156 for (; !found && it.More(); it.Next())
158 found = AddedFacesMap.Contains( it.Value() );
161 // select basing on geometrical check
162 Standard_Boolean GoodOri, inside;
163 Standard_Real dot, MaxDot = -100;
164 TopTools_ListOfShape TangFL; // tangent faces
165 for ( it.Initialize( FL ) ; it.More(); it.Next()) {
166 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
167 if (NeighborF.IsSame( F ))
169 inside = Partition_Loop3d::IsInside( E, F, NeighborF, 1, dot, GoodOri);
176 if ( IsEqual( dot, MaxDot))
183 if (!TangFL.IsEmpty()) {
184 for (it.Initialize( TangFL ); it.More(); it.Next()) {
185 const TopoDS_Face& NeighborF = TopoDS::Face( it.Value());
186 if (Partition_Loop3d:: IsInside( E, SelF , NeighborF, 0, dot, GoodOri))
191 if (!SelF.IsNull() &&
192 AddedFacesMap.Add( SelF ) &&
193 !AvoidFacesMap.Contains( SelF ))
194 Builder.Add( Shell, SelF);
196 } // loop on edges of F
198 } // loop on the faces added to Shell
201 myNewShells.Append( Shell );
206 // prepare to the next call
215 //=======================================================================
218 //=======================================================================
220 gp_Vec Partition_Loop3d::Normal(const TopoDS_Edge& E,
221 const TopoDS_Face& F)
224 Standard_Real First, Last;
227 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
228 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
230 gp_Pnt2d p = C2d->Value( 0.5*(First+Last) );
231 Sf->D1(p.X(), p.Y(), Ps, V1, V2);
232 Norm = V1.Crossed(V2);
234 if (F.Orientation() == TopAbs_REVERSED )
240 //=======================================================================
241 //function : NextNormal
242 //purpose : find normal to F at point a little inside F near the middle of E
243 //warning : E must be properly oriented in F.
244 //=======================================================================
246 static gp_Vec NextNormal(const TopoDS_Edge& E,
247 const TopoDS_Face& F)
249 Standard_Real First, Last;
251 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface (E, F, First, Last);
252 Handle(Geom_Surface) Sf = BRep_Tool::Surface(F);
256 C2d->D1( 0.5*(First+Last), p, v);
257 if (E.Orientation() != F.Orientation())
259 gp_Dir2d dir( -v.Y(), v.X() ); // dir inside F
261 Standard_Real duv = 1e-6; // this is not Ok and may give incorrect result if
262 // resolutionUV of compared faces is very different. To have a good result,
263 //it is necessary to get normal to faces at points equidistant from E in 3D
265 p.SetX( p.X() + dir.X()*duv );
266 p.SetY( p.Y() + dir.Y()*duv );
269 gp_Vec Norm, V1, V2, VV1, VV2;
270 Sf->D1( p.X(), p.Y(), Ps, V1, V2);
271 Norm = V1.Crossed(V2);
273 if (F.Orientation() == TopAbs_REVERSED )
280 //=======================================================================
281 //function : FindEinF
282 //purpose : find E in F
283 //=======================================================================
285 static TopoDS_Edge FindEinF(const TopoDS_Edge& E,
286 const TopoDS_Face& F)
288 TopExp_Explorer expl (F, TopAbs_EDGE);
289 for (; expl.More(); expl.Next())
290 if( E.IsSame( expl.Current() ))
291 return TopoDS::Edge(expl.Current());
296 //=======================================================================
297 //function : IsInside
298 //purpose : check if <F2> is inside <F1> by edge <E>.
299 // if <CountDot>, compute <Dot>: scalar production of
300 // normalized vectors pointing inside faces, and
301 // check if faces are oriented well for sewing
302 //=======================================================================
304 Standard_Boolean Partition_Loop3d::IsInside(const TopoDS_Edge& E,
305 const TopoDS_Face& F1,
306 const TopoDS_Face& F2,
307 const Standard_Boolean CountDot,
309 Standard_Boolean& GoodOri)
313 gp_Vec Vc1, Vc2, Vin1, Vin2, Nf1, Nf2;
314 Handle(Geom_Curve) Curve = BRep_Tool::Curve(E,f,l);
315 Curve->D1( 0.5*(f + l), P, Vc2);
316 TopoDS_Edge E1, E2 = FindEinF (E, F2);
317 if (E2.Orientation() == TopAbs_REVERSED ) Vc2.Reverse();
323 Nf1.CrossSquareMagnitude(Nf2) / Nf1.SquareMagnitude() / Nf2.SquareMagnitude();
324 Standard_Boolean tangent = sin < 0.001;
326 Standard_Boolean inside = 0;
328 E1 = FindEinF (E, F1);
329 gp_Vec NNf1 = NextNormal(E1,F1);
330 gp_Vec NNf2 = NextNormal(E2,F2);
331 Vin2 = NNf2.Crossed(Vc2);
332 inside = Vin2 * NNf1 < 0;
335 Vin2 = Nf2.Crossed(Vc2);
336 inside = Vin2 * Nf1 < 0;
339 if (!CountDot) return inside;
342 Vin2 = Nf2.Crossed(Vc2);
344 E1 = FindEinF (E, F1);
347 if (E1.Orientation() != E2.Orientation())
349 Vin1 = Nf1.Crossed(Vc1);
352 Standard_Real N1N2 = Nf1 * Nf2;
353 GoodOri = (Vin2 * Vin1 < 0) ? N1N2 > 0 : N1N2 < 0;
356 Standard_Real V1N2 = Vin1 * Nf2;
357 GoodOri = ( inside ? V1N2 <= 0 : V1N2 >= 0);