Salome HOME
Fix problem with partition (merge fix of PKV from BR_QT4_Dev branch)
[modules/geom.git] / src / GEOMAlgo / BlockFix_UnionEdges.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:      BlockFix_UnionEdges.cxx
21 // Created:   07.12.04 15:27:30
22 // Author:    Sergey KUUL
23
24
25 #include <BlockFix_UnionEdges.ixx>
26
27 #include <Approx_Curve3d.hxx>
28 #include <BRepAdaptor_HCompCurve.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRep_Tool.hxx>
31 #include <GC_MakeCircle.hxx>
32 #include <Geom_BSplineCurve.hxx>
33 #include <Geom_Circle.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_Line.hxx>
36 #include <Geom_TrimmedCurve.hxx>
37 #include <ShapeAnalysis_Edge.hxx>
38 #include <ShapeFix_Edge.hxx>
39 #include <ShapeFix_Face.hxx>
40 #include <ShapeFix_Shell.hxx>
41 #include <TColgp_SequenceOfPnt.hxx>
42 #include <TColStd_MapOfInteger.hxx>
43 #include <TopExp.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopTools_MapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_SequenceOfShape.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Shell.hxx>
55 #include <TopoDS_Solid.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <TopoDS_Iterator.hxx>
58
59 #include "utilities.h"
60
61 //=======================================================================
62 //function : BlockFix_UnionEdges()
63 //purpose  : Constructor
64 //=======================================================================
65
66 BlockFix_UnionEdges::BlockFix_UnionEdges (  )
67 {
68 }
69
70
71 //=======================================================================
72 //function : MergeEdges
73 //purpose  : auxilary
74 //=======================================================================
75 static Standard_Boolean MergeEdges(const TopTools_SequenceOfShape& SeqEdges,
76                                    const TopoDS_Face& aFace,
77                                    const Standard_Real Tol,
78                                    TopoDS_Edge& anEdge)
79 {
80   // make chain for union
81   BRep_Builder B;
82   ShapeAnalysis_Edge sae;
83   TopoDS_Edge FirstE = TopoDS::Edge(SeqEdges.Value(1));
84   TopoDS_Edge LastE = FirstE;
85   TopoDS_Vertex VF = sae.FirstVertex(FirstE);
86   TopoDS_Vertex VL = sae.LastVertex(LastE);
87   TopTools_SequenceOfShape aChain;
88   aChain.Append(FirstE);
89   TColStd_MapOfInteger IndUsedEdges;
90   IndUsedEdges.Add(1);
91   Standard_Integer j;
92   for(j=2; j<=SeqEdges.Length(); j++) {
93     for(Standard_Integer k=2; k<=SeqEdges.Length(); k++) {
94       if(IndUsedEdges.Contains(k)) continue;
95       TopoDS_Edge edge = TopoDS::Edge(SeqEdges.Value(k));
96       TopoDS_Vertex VF2 = sae.FirstVertex(edge);
97       TopoDS_Vertex VL2 = sae.LastVertex(edge);
98       if(sae.FirstVertex(edge).IsSame(VL)) {
99         aChain.Append(edge);
100         LastE = edge;
101         VL = sae.LastVertex(LastE);
102         IndUsedEdges.Add(k);
103       }
104       else if(sae.LastVertex(edge).IsSame(VF)) {
105         aChain.Prepend(edge);
106         FirstE = edge;
107         VF = sae.FirstVertex(FirstE);
108         IndUsedEdges.Add(k);
109       }
110     }
111   }
112   if(aChain.Length()<SeqEdges.Length()) {
113     MESSAGE ("can not create correct chain...");
114     return Standard_False;
115   }
116   // union edges in chain
117   // first step: union lines and circles
118   TopLoc_Location Loc;
119   Standard_Real fp1,lp1,fp2,lp2;
120   for(j=1; j<aChain.Length(); j++) {
121     TopoDS_Edge edge1 = TopoDS::Edge(aChain.Value(j));
122     Handle(Geom_Curve) c3d1 = BRep_Tool::Curve(edge1,Loc,fp1,lp1);
123     if(c3d1.IsNull()) break;
124     while(c3d1->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
125       Handle(Geom_TrimmedCurve) tc =
126         Handle(Geom_TrimmedCurve)::DownCast(c3d1);
127       c3d1 = tc->BasisCurve();
128     }
129     TopoDS_Edge edge2 = TopoDS::Edge(aChain.Value(j+1));
130     Handle(Geom_Curve) c3d2 = BRep_Tool::Curve(edge2,Loc,fp2,lp2);
131     if(c3d2.IsNull()) break;
132     while(c3d2->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
133       Handle(Geom_TrimmedCurve) tc =
134         Handle(Geom_TrimmedCurve)::DownCast(c3d2);
135       c3d2 = tc->BasisCurve();
136     }
137     if( c3d1->IsKind(STANDARD_TYPE(Geom_Line)) && c3d2->IsKind(STANDARD_TYPE(Geom_Line)) ) {
138       // union lines
139       Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(c3d1);
140       Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(c3d2);
141       gp_Dir Dir1 = L1->Position().Direction();
142       gp_Dir Dir2 = L2->Position().Direction();
143       if(!Dir1.IsEqual(Dir2,Precision::Angular())) continue;
144       // can union lines => create new edge
145       TopoDS_Vertex V1 = sae.FirstVertex(edge1);
146       gp_Pnt PV1 = BRep_Tool::Pnt(V1);
147       TopoDS_Vertex V2 = sae.LastVertex(edge2);
148       gp_Pnt PV2 = BRep_Tool::Pnt(V2);
149       gp_Vec Vec(PV1,PV2);
150       Handle(Geom_Line) L = new Geom_Line(gp_Ax1(PV1,Vec));
151       Standard_Real dist = PV1.Distance(PV2);
152       Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(L,0.0,dist);
153       TopoDS_Edge E;
154       B.MakeEdge (E,tc,Precision::Confusion());
155       B.Add (E,V1);  B.Add (E,V2);
156       B.UpdateVertex(V1, 0., E, 0.);
157       B.UpdateVertex(V2, dist, E, 0.);
158       ShapeFix_Edge sfe;
159       sfe.FixAddPCurve(E,aFace,Standard_False);
160       sfe.FixSameParameter(E);
161       aChain.Remove(j);
162       aChain.SetValue(j,E);
163       j--;
164     }
165     if( c3d1->IsKind(STANDARD_TYPE(Geom_Circle)) && c3d2->IsKind(STANDARD_TYPE(Geom_Circle)) ) {
166       // union circles
167       Handle(Geom_Circle) C1 = Handle(Geom_Circle)::DownCast(c3d1);
168       Handle(Geom_Circle) C2 = Handle(Geom_Circle)::DownCast(c3d2);
169       gp_Pnt P01 = C1->Location();
170       gp_Pnt P02 = C2->Location();
171       if(P01.Distance(P02)>Precision::Confusion()) continue;
172       // can union circles => create new edge
173       TopoDS_Vertex V1 = sae.FirstVertex(edge1);
174       gp_Pnt PV1 = BRep_Tool::Pnt(V1);
175       TopoDS_Vertex V2 = sae.LastVertex(edge2);
176       gp_Pnt PV2 = BRep_Tool::Pnt(V2);
177       TopoDS_Vertex VM = sae.LastVertex(edge1);
178       gp_Pnt PVM = BRep_Tool::Pnt(VM);
179       GC_MakeCircle MC(PV1,PVM,PV2);
180       Handle(Geom_Circle) C = MC.Value();
181       gp_Pnt P0 = C->Location();
182       gp_Dir D1(gp_Vec(P0,PV1));
183       gp_Dir D2(gp_Vec(P0,PV2));
184       Standard_Real fpar = C->XAxis().Direction().Angle(D1);
185       Standard_Real lpar = C->XAxis().Direction().Angle(D2);
186       Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
187       TopoDS_Edge E;
188       B.MakeEdge (E,tc,Precision::Confusion());
189       B.Add (E,V1);  B.Add (E,V2);
190       B.UpdateVertex(V1, fpar, E, 0.);
191       B.UpdateVertex(V2, lpar, E, 0.);
192       ShapeFix_Edge sfe;
193       sfe.FixAddPCurve(E,aFace,Standard_False);
194       sfe.FixSameParameter(E);
195       aChain.Remove(j);
196       aChain.SetValue(j,E);
197       j--;
198     }
199   }
200   if(j<aChain.Length()) {
201     MESSAGE ("null curve3d in edge...");
202     return Standard_False;
203   }
204   if(aChain.Length()>1) {
205     // second step: union edges with various curves
206     MESSAGE ("can not make analitical union => make approximation");
207     TopoDS_Wire W;
208     B.MakeWire(W);
209     for(j=1; j<=aChain.Length(); j++) {
210       TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
211       B.Add(W,edge);
212     }
213     Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
214     Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
215     Handle(Geom_BSplineCurve) bc = Conv.Curve();
216     TopoDS_Edge E;
217     B.MakeEdge (E,bc,Precision::Confusion());
218     B.Add (E,VF);
219     B.Add (E,VL);
220     aChain.SetValue(1,E);
221   }
222
223   anEdge = TopoDS::Edge(aChain.Value(1));
224   return Standard_True;
225 }
226
227
228 //=======================================================================
229 //function : Perform
230 //purpose  : 
231 //=======================================================================
232
233 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
234                                           const Standard_Real Tol)
235 {
236   myContext = new ShapeBuild_ReShape;
237   myTolerance = Tol;
238   TopoDS_Shape aResult = myContext->Apply(Shape);
239   
240   // processing each solid
241   TopExp_Explorer exps;
242   for(exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
243     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
244
245     TopTools_IndexedMapOfShape ChangedFaces;
246     
247     // creating map of edge faces
248     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
249     TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
250   
251     Handle(ShapeBuild_ReShape) aContext = new ShapeBuild_ReShape;
252     TopoDS_Shape aRes = aSolid;
253     aRes = aContext->Apply(aSolid);
254
255     // processing each face
256     TopExp_Explorer exp;
257     for(exp.Init(aRes, TopAbs_FACE); exp.More(); exp.Next()) {
258       TopoDS_Face aFace =
259         TopoDS::Face(aContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
260       TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
261
262       for(TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
263         TopoDS_Edge edge = TopoDS::Edge(expe.Current());
264         if(!aMapEdgeFaces.Contains(edge)) continue;
265         const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
266         TopTools_ListIteratorOfListOfShape anIter(aList);
267         for( ; anIter.More(); anIter.Next()) {
268           TopoDS_Face face = TopoDS::Face(anIter.Value());
269           TopoDS_Face face1 = TopoDS::Face(aContext->Apply(anIter.Value()));
270           if(face1.IsSame(aFace)) continue;
271           if(aMapFacesEdges.Contains(face)) {
272             aMapFacesEdges.ChangeFromKey(face).Append(edge);
273           }
274           else {
275             TopTools_ListOfShape ListEdges;
276             ListEdges.Append(edge);
277             aMapFacesEdges.Add(face,ListEdges);
278           }
279         }
280       }
281       
282       for(Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
283         const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
284         TopTools_SequenceOfShape SeqEdges;
285         TopTools_ListIteratorOfListOfShape anIter(ListEdges);
286         for( ; anIter.More(); anIter.Next()) {
287           SeqEdges.Append(anIter.Value());
288         }
289         if(SeqEdges.Length()==1) continue;
290         TopoDS_Edge E;
291         if( MergeEdges(SeqEdges,aFace,Tol,E) ) {
292           // now we have only one edge - aChain.Value(1)
293           // we have to replace old ListEdges with this new edge
294           aContext->Replace(SeqEdges(1),E);
295           for(Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
296             aContext->Remove(SeqEdges(j));
297           }
298           TopoDS_Face tmpF = TopoDS::Face(exp.Current());
299           if( !ChangedFaces.Contains(tmpF) )
300             ChangedFaces.Add(tmpF);
301           tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
302           if( !ChangedFaces.Contains(tmpF) )
303             ChangedFaces.Add(tmpF);
304         }
305       }
306       
307     } // end processing each face
308     
309     // fix changed faces and replace them in the local context
310     for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
311       TopoDS_Face aFace = TopoDS::Face(aContext->Apply(ChangedFaces.FindKey(i)));
312       Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
313       sff->SetContext(myContext);
314       sff->SetPrecision(myTolerance);
315       sff->SetMinTolerance(myTolerance);
316       sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
317       sff->Perform();
318       aContext->Replace(aFace,sff->Face());
319     }
320
321     if(ChangedFaces.Extent()>0) {
322       // fix changed shell and replace it in the local context
323       TopoDS_Shape aRes1 = aContext->Apply(aRes);
324       TopExp_Explorer expsh;
325       for(expsh.Init(aRes1, TopAbs_SHELL); expsh.More(); expsh.Next()) {
326         TopoDS_Shell aShell = TopoDS::Shell(expsh.Current());
327         Handle(ShapeFix_Shell) sfsh = new ShapeFix_Shell;
328         sfsh->FixFaceOrientation(aShell);
329         aContext->Replace(aShell,sfsh->Shell());
330       }
331       TopoDS_Shape aRes2 = aContext->Apply(aRes1);
332       // put new solid into global context
333       myContext->Replace(aSolid,aRes2);
334     }
335
336   } // end processing each solid
337
338   aResult = myContext->Apply(Shape);
339   return aResult;
340 }