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