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