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