Salome HOME
Change comments style in geompy.py for right processing with HappyDoc
[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       GC_MakeCircle MC(PV1,PVM,PV2);
156       Handle(Geom_Circle) C = MC.Value();
157       gp_Pnt P0 = C->Location();
158       gp_Dir D1(gp_Vec(P0,PV1));
159       gp_Dir D2(gp_Vec(P0,PV2));
160       Standard_Real fpar = C->XAxis().Direction().Angle(D1);
161       Standard_Real lpar = C->XAxis().Direction().Angle(D2);
162       Handle(Geom_TrimmedCurve) tc = new Geom_TrimmedCurve(C,fpar,lpar);
163       TopoDS_Edge E;
164       B.MakeEdge (E,tc,Precision::Confusion());
165       B.Add (E,V1);  B.Add (E,V2);
166       B.UpdateVertex(V1, fpar, E, 0.);
167       B.UpdateVertex(V2, lpar, E, 0.);
168       ShapeFix_Edge sfe;
169       sfe.FixAddPCurve(E,aFace,Standard_False);
170       sfe.FixSameParameter(E);
171       aChain.Remove(j);
172       aChain.SetValue(j,E);
173       j--;
174     }
175   }
176   if(j<aChain.Length()) {
177     cout<<"null curve3d in edge..."<<endl;
178     return Standard_False;
179   }
180   if(aChain.Length()>1) {
181     // second step: union edges with various curves
182     cout<<"can not make analitical union => make approximation"<<endl;
183     TopoDS_Wire W;
184     B.MakeWire(W);
185     for(j=1; j<=aChain.Length(); j++) {
186       TopoDS_Edge edge = TopoDS::Edge(aChain.Value(j));
187       B.Add(W,edge);
188     }
189     Handle(BRepAdaptor_HCompCurve) Adapt = new BRepAdaptor_HCompCurve(W);
190     Approx_Curve3d Conv(Adapt,Tol,GeomAbs_C1,9,1000);
191     Handle(Geom_BSplineCurve) bc = Conv.Curve();
192     TopoDS_Edge E;
193     B.MakeEdge (E,bc,Precision::Confusion());
194     B.Add (E,VF);
195     B.Add (E,VL);
196     //TopLoc_Location L;
197     //Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace,L);
198     //ShapeFix_Edge sfe;
199     //if(!L.IsIdentity()) {
200     //  TopoDS_Edge aPCEdge = TopoDS::Edge(E.Moved(L.Inverted()));
201     //  sfe.FixAddPCurve(aPCEdge,aFace,Standard_False);
202     //  Handle(Geom2d_Curve) c2d;
203     //  Standard_Real fp,lp;
204     //  sae.PCurve(aPCEdge,aFace,c2d,fp,lp);
205     //  B.UpdateEdge(E,c2d,aFace,0.);
206     //  B.Range(E,aFace,fp,lp);
207     //  c2d.Nullify();
208     //  B.UpdateEdge(aPCEdge,c2d,aFace,0.);
209     //  E = aPCEdge;
210     //}
211     //else {
212     //  sfe.FixAddPCurve(E,aFace,Standard_False);
213     //}
214     //sfe.FixSameParameter(E);
215     aChain.SetValue(1,E);
216   }
217
218   anEdge = TopoDS::Edge(aChain.Value(1));
219   return Standard_True;
220 }
221
222
223 //=======================================================================
224 //function : Perform
225 //purpose  : 
226 //=======================================================================
227
228 TopoDS_Shape BlockFix_UnionEdges::Perform(const TopoDS_Shape& Shape,
229                                           const Standard_Real Tol)
230 {
231   myContext = new ShapeBuild_ReShape;
232   myTolerance = Tol;
233   TopoDS_Shape aResult = myContext->Apply(Shape);
234   
235   TopTools_IndexedMapOfShape ChangedFaces;
236
237   // processing each solid
238   TopExp_Explorer exps;
239   for(exps.Init(Shape, TopAbs_SOLID); exps.More(); exps.Next()) {
240     TopoDS_Solid aSolid = TopoDS::Solid(exps.Current());
241
242     // creating map of edge faces
243     TopTools_IndexedDataMapOfShapeListOfShape aMapEdgeFaces;
244     TopExp::MapShapesAndAncestors(aSolid, TopAbs_EDGE, TopAbs_FACE, aMapEdgeFaces);
245   
246     // processing each face
247     TopExp_Explorer exp;
248     for(exp.Init(aSolid, TopAbs_FACE); exp.More(); exp.Next()) {
249       TopoDS_Face aFace = TopoDS::Face(myContext->Apply(exp.Current().Oriented(TopAbs_FORWARD)));
250       TopTools_IndexedDataMapOfShapeListOfShape aMapFacesEdges;
251
252       for(TopExp_Explorer expe(aFace,TopAbs_EDGE); expe.More(); expe.Next()) {
253         TopoDS_Edge edge = TopoDS::Edge(expe.Current());
254         if(!aMapEdgeFaces.Contains(edge)) continue;
255         const TopTools_ListOfShape& aList = aMapEdgeFaces.FindFromKey(edge);
256         TopTools_ListIteratorOfListOfShape anIter(aList);
257         for( ; anIter.More(); anIter.Next()) {
258           TopoDS_Face face = TopoDS::Face(anIter.Value());
259           TopoDS_Face face1 = TopoDS::Face(myContext->Apply(anIter.Value()));
260           if(face1.IsSame(aFace)) continue;
261           if(aMapFacesEdges.Contains(face)) {
262             aMapFacesEdges.ChangeFromKey(face).Append(edge);
263           }
264           else {
265             TopTools_ListOfShape ListEdges;
266             ListEdges.Append(edge);
267             aMapFacesEdges.Add(face,ListEdges);
268           }
269         }
270       }
271       
272       for(Standard_Integer i=1; i<=aMapFacesEdges.Extent(); i++) {
273         const TopTools_ListOfShape& ListEdges = aMapFacesEdges.FindFromIndex(i);
274         TopTools_SequenceOfShape SeqEdges;
275         TopTools_ListIteratorOfListOfShape anIter(ListEdges);
276         for( ; anIter.More(); anIter.Next()) {
277           SeqEdges.Append(anIter.Value());
278         }
279         if(SeqEdges.Length()==1) continue;
280         TopoDS_Edge E;
281         if( MergeEdges(SeqEdges,aFace,Tol,E) ) {
282           // now we have only one edge - aChain.Value(1)
283           // we have to replace old ListEdges with this new edge
284           myContext->Replace(SeqEdges(1),E);
285           for(Standard_Integer j=2; j<=SeqEdges.Length(); j++) {
286             myContext->Remove(SeqEdges(j));
287           }
288           TopoDS_Face tmpF = TopoDS::Face(exp.Current());
289           if( !ChangedFaces.Contains(tmpF) )
290             ChangedFaces.Add(tmpF);
291           tmpF = TopoDS::Face(aMapFacesEdges.FindKey(i));
292           if( !ChangedFaces.Contains(tmpF) )
293             ChangedFaces.Add(tmpF);
294         }
295       }
296       
297     } // end processing each face
298     
299   } // end processing each solid
300
301   for(Standard_Integer i=1; i<=ChangedFaces.Extent(); i++) {
302     TopoDS_Face aFace = TopoDS::Face(myContext->Apply(ChangedFaces.FindKey(i)));
303     Handle(ShapeFix_Face) sff = new ShapeFix_Face(aFace);
304     sff->SetContext(myContext);
305     sff->SetPrecision(myTolerance);
306     sff->SetMinTolerance(myTolerance);
307     sff->SetMaxTolerance(Max(1.,myTolerance*1000.));
308     sff->Perform();
309     //Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
310     //sfw->SetContext(myContext);
311     //sfw->SetPrecision(myTolerance);
312     //sfw->SetMinTolerance(myTolerance);
313     //sfw->SetMaxTolerance(Max(1.,myTolerance*1000.));
314     //sfw->SetFace(aFace);
315     //for ( TopoDS_Iterator iter(aFace,Standard_False); iter.More(); iter.Next()) { 
316     //  TopoDS_Wire wire = TopoDS::Wire ( iter.Value() );
317     //  sfw->Load(wire);
318     //  sfw->FixReorder();
319     //  sfw->FixShifted();
320     //}
321   }
322
323   aResult = myContext->Apply(Shape);
324   return aResult;
325 }