Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/geom.git] / src / ShHealOper / ShHealOper_CloseContour.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:      ShHealOper_CloseContour.cxx
21 // Created:   20.04.04 11:36:01
22 // Author:    Galina KULIKOVA
23
24
25 #include <ShHealOper_CloseContour.hxx>
26 #include <ShapeExtend_WireData.hxx>
27 #include <ShapeFix_Wire.hxx>
28 #include <TopoDS_Face.hxx>
29 #include <ShapeFix_Edge.hxx>
30 #include <TopoDS_Iterator.hxx>
31 #include <ShapeAnalysis_Wire.hxx>
32 #include <TColStd_SequenceOfInteger.hxx>
33 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopTools_ListOfShape.hxx>
36 #include <TopTools_ListIteratorOfListOfShape.hxx>
37 #include <TColStd_MapOfInteger.hxx>
38 #include <TopExp.hxx>
39 #include <TopoDS.hxx>
40 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
41 #include <ShapeAnalysis_Edge.hxx>
42 #include <BRep_Builder.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <BRepBuilderAPI_MakeVertex.hxx>
46 #include <ShapeAnalysis_Edge.hxx>
47 #include <gp_Pnt.hxx>
48 #include <gp_Vec2d.hxx>
49 #include <gp_Vec.hxx>
50 #include <Geom_Line.hxx>
51 #include <Geom2d_Line.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <BRep_Tool.hxx>
54 #include <ShapeBuild_Edge.hxx>
55 #include <TopExp_Explorer.hxx>
56
57 //=======================================================================
58 //function : ShHealOper_CloseContour()
59 //purpose  : Constructor
60 //=======================================================================
61 ShHealOper_CloseContour::ShHealOper_CloseContour (const TopoDS_Shape& theShape  ) 
62 {
63   Init(theShape);
64 }
65
66 //=======================================================================
67 //function : Init
68 //purpose  : 
69 //=======================================================================
70 void ShHealOper_CloseContour::Init(const TopoDS_Shape& theShape)
71 {
72   ShHealOper_Tool::Init(theShape);
73   myTolerance = Precision::Confusion();
74   myMaxTolerance = 1.0;
75   TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,myMapEdgesFace);
76 }
77
78 //=======================================================================
79 //function : Perform
80 //purpose  : 
81 //=======================================================================
82 Standard_Boolean ShHealOper_CloseContour::Perform(const TopTools_SequenceOfShape& theSeqEdges,
83                                                   const Standard_Boolean theModeVertex,
84                                                   const Standard_Boolean theModeFixGapsCurves)
85 {
86   myFreeEdges.Clear();
87   myDone = Standard_False;
88   myErrorStatus =ShHealOper_NotError;
89   if(myInitShape.IsNull()) {
90     myErrorStatus = ShHealOper_InvalidParameters;
91     return myDone;
92   }
93   Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData;
94   Standard_Integer i =1;
95   for ( ; i <= theSeqEdges.Length(); i++)
96     asewd->Add(theSeqEdges.Value(i));
97
98   myModeVertex = theModeVertex;
99   myModeFixGapsCurves = theModeFixGapsCurves;
100
101   build(asewd);
102   
103   return myDone;
104 }
105
106 //=======================================================================
107 //function : Perform
108 //purpose  : 
109 //=======================================================================
110 Standard_Boolean ShHealOper_CloseContour::Perform(const TopoDS_Wire& theWire,
111                                                   const Standard_Boolean theModeVertex,
112                                                   const Standard_Boolean theModeFixGapsCurves)
113 {
114   myFreeEdges.Clear();
115   myDone = Standard_False;
116   if(myInitShape.IsNull()) {
117     myErrorStatus = ShHealOper_InvalidParameters;
118     return myDone;
119   }
120   Handle(ShapeExtend_WireData) asewd = new ShapeExtend_WireData(theWire);
121   myModeVertex = theModeVertex;
122   myModeFixGapsCurves = theModeFixGapsCurves;
123   build(asewd);
124   return myDone;
125 }
126
127 //=======================================================================
128 //function : Build
129 //purpose  : 
130 //=======================================================================
131 void ShHealOper_CloseContour::build(Handle(ShapeExtend_WireData)& theSewd)
132 {
133   TopTools_SequenceOfShape aCommonFaces;
134   //checks that all specified edges belong the one face or not.
135   Standard_Boolean isOneFace = checkOneFace(theSewd,aCommonFaces);
136
137   //add edge or increase max tolerance in dependance on specified VertexMode.
138   //if all edges belong the one face that gap will be closed in the 2D by line
139   //else gap will be closed in the 3D by line.
140   myDone = fixGaps(theSewd,aCommonFaces);
141   updateWire(theSewd);
142   Handle(ShapeFix_Wire) sfw = new ShapeFix_Wire;
143   sfw->SetContext(myContext);
144   sfw->Load(theSewd);
145
146   sfw->ClosedWireMode() = Standard_True;
147   sfw->SetPrecision(myTolerance);
148   sfw->SetMaxTolerance(myMaxTolerance);
149   
150   if(isOneFace) {
151     Standard_Integer ii =1;
152     for( ; ii <= aCommonFaces.Length(); ii++) {
153       TopoDS_Face aFace = TopoDS::Face(aCommonFaces.Value(ii));
154       sfw->SetFace(aFace);
155       myDone = (sfw->Perform() || myDone);
156       //if Mode for fix gaps is equal to true that
157       //curve 3D and curve 2D will be pull to each other.
158       if(myModeFixGapsCurves) {
159         Standard_Boolean isFixgaps = Standard_False;
160         if(ii ==1) {
161           sfw->FixGaps3d();
162           isFixgaps = sfw->StatusGaps3d(ShapeExtend_DONE);
163           if(!isFixgaps && sfw->StatusGaps3d(ShapeExtend_FAIL))
164              myErrorStatus = ShHealOper_ErrorExecution;
165         }
166         if(sfw->FixGaps2d())
167           sfw->FixSelfIntersection();
168         else if(sfw->StatusGaps2d(ShapeExtend_FAIL))
169           myErrorStatus = ShHealOper_ErrorExecution;
170
171         isFixgaps = (isFixgaps || sfw->StatusGaps2d(ShapeExtend_DONE));
172         myDone = (myDone || isFixgaps);
173         if(isFixgaps) {
174           Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
175           Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
176           for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
177             TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
178             sfe->FixVertexTolerance(aEdge,aFace);
179             sfe->FixSameParameter(aEdge);
180           }
181         }
182       }
183     }
184   }
185   else {
186     myDone = (sfw->Perform() || myDone);
187     if(myModeFixGapsCurves) {
188       //if Mode for fix gaps is equal to true that
189       //curve 3D will be pull to each other.
190       if(sfw->FixGaps3d()) {
191         Handle(ShapeExtend_WireData) sbwd = sfw->WireData();
192         Handle(ShapeFix_Edge) sfe = new ShapeFix_Edge;
193         for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
194           TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
195           sfe->FixVertexTolerance(aEdge);
196           sfe->FixSameParameter(aEdge);
197         }
198       }
199       else if(sfw->StatusGaps3d(ShapeExtend_FAIL))
200         myErrorStatus = ShHealOper_ErrorExecution;
201       myDone = (sfw->StatusGaps3d(ShapeExtend_DONE) || myDone);
202     }
203   }
204
205   if(myDone) {
206     TopoDS_Shape aoldShape = myInitShape;
207     //if free edges were added they will be added to the result shape
208     //and type of result shape should be change if initial shape is not COMPOUND.
209     if( myFreeEdges.Length()) {
210       BRep_Builder aB;
211       TopoDS_Compound aComp;
212       aB.MakeCompound(aComp);
213       if(aoldShape.ShapeType() == TopAbs_COMPOUND) {
214         TopoDS_Iterator aIt(aoldShape);
215         for( ;aIt.More();aIt.Next() )
216            aB.Add(aComp,aIt.Value());
217       }
218       else
219         aB.Add(aComp,aoldShape);
220
221       Standard_Integer i=1;
222       for ( ; i <= myFreeEdges.Length(); i++ ) 
223         aB.Add(aComp,myFreeEdges.Value(i));
224       aoldShape = aComp;
225       
226     }
227     myResultShape = myContext->Apply(aoldShape);
228   }
229   return;
230 }
231
232 //=======================================================================
233 //function : checkGaps
234 //purpose  : 
235 //=======================================================================
236 Standard_Boolean ShHealOper_CloseContour::fixGaps(const Handle(ShapeExtend_WireData)& theWire,
237                                                   const TopTools_SequenceOfShape& theCommonFaces) 
238 {
239   Handle(ShapeAnalysis_Wire) asaw = new ShapeAnalysis_Wire;
240   asaw->Load(theWire);
241   
242   Standard_Integer i =1;
243   Standard_Boolean hasGaps = Standard_False;
244   for ( ; i <= asaw->NbEdges(); i++) {
245     if(asaw->CheckGap3d(i)) {
246       Standard_Real dist = asaw->MinDistance3d();
247       if(dist > myMaxTolerance) {
248         hasGaps = Standard_True;
249         Standard_Integer ind2 = (i ==0 ? theWire->NbEdges() :i);
250         Standard_Integer ind1 = (ind2 >1 ? ind2 -1 : theWire->NbEdges());
251         TopoDS_Edge aE1= theWire->Edge(ind1);
252         TopoDS_Edge aE2= theWire->Edge(ind2);
253         if(!myModeVertex)
254           buildEdge(aE1,aE2,theCommonFaces);
255         else
256           myMaxTolerance = RealLast();
257         if(ind2 == ind1) break;
258       }
259     }
260     
261   }
262   return  hasGaps;
263 }
264
265 //=======================================================================
266 //function : checkOneFace
267 //purpose  : 
268 //=======================================================================
269 Standard_Boolean ShHealOper_CloseContour::checkOneFace(const Handle(ShapeExtend_WireData)& theSewd,
270                                                        TopTools_SequenceOfShape& theCommonFaces) const
271 {
272   TopTools_IndexedMapOfShape amapfaces;
273   TopoDS_Edge aEdge1 = theSewd->Edge(1);
274   Standard_Boolean isOneFace = myMapEdgesFace.Contains(aEdge1 );
275   if(!isOneFace)
276     return isOneFace;
277
278   //check that all specified edges belong to one face. 
279   const TopTools_ListOfShape& alfaces = myMapEdgesFace.FindFromKey(aEdge1);
280   isOneFace = isOneFace && (!alfaces.IsEmpty());
281   if(!isOneFace)
282     return Standard_False;
283
284   TopTools_ListIteratorOfListOfShape litr(alfaces);
285   for( ; litr.More();litr.Next() ) 
286     amapfaces.Add(litr.Value());
287   
288   TColStd_MapOfInteger amapIndex;
289   Standard_Integer ind  =0;
290   Standard_Integer i =2;
291   for( ; i <= theSewd->NbEdges() && isOneFace; i++) {
292     isOneFace = myMapEdgesFace.Contains(theSewd->Edge(i));
293     if(isOneFace) {
294       const TopTools_ListOfShape& alfaces1 = myMapEdgesFace.FindFromKey(theSewd->Edge(i));
295       
296         TColStd_MapOfInteger aTmpInd;
297         for(litr.Initialize(alfaces1) ; litr.More() ;litr.Next() ) {
298           if(amapfaces.Contains(litr.Value())) {
299             ind = amapfaces.FindIndex(litr.Value());
300             if( i == 2) 
301               amapIndex.Add(ind);
302             else 
303               aTmpInd.Add(ind);
304           }
305         }
306       if(i ==2) {
307         isOneFace = (amapIndex.Extent());
308         continue;
309       }
310       else {
311         TColStd_MapIteratorOfMapOfInteger aMi(amapIndex);
312         for( ;  aMi.More();aMi.Next()) {
313           if(!aTmpInd.Contains(aMi.Key()))
314             amapIndex.Remove(aMi.Key());
315         }
316         isOneFace = (amapIndex.Extent()); 
317         
318       }
319     }
320   }
321   if(theSewd->NbEdges() >1) {
322     Standard_Integer j =1;
323     for( ; j <= amapfaces.Extent(); j++) {
324       if(!amapIndex.Contains(j)) continue;
325       TopoDS_Shape aF = amapfaces.FindKey(j);
326       theCommonFaces.Append(aF);
327     }
328   }
329   return isOneFace;
330 }
331
332 //=======================================================================
333 //function : buildEdge
334 //purpose  : 
335 //=======================================================================
336 void ShHealOper_CloseContour::buildEdge(const TopoDS_Edge& aE1, 
337                                         const TopoDS_Edge& aE2,
338                                         const TopTools_SequenceOfShape& theCommonFaces) 
339 {
340   ShapeAnalysis_Edge asae;
341   TopoDS_Vertex aV1 = asae.LastVertex(aE1);
342   TopoDS_Vertex aV2 = asae.FirstVertex(aE2);
343   gp_Pnt p1 = BRep_Tool::Pnt(aV1);
344   gp_Pnt p2 = BRep_Tool::Pnt(aV2);
345   BRepBuilderAPI_MakeVertex mkver1( p1 );
346   TopoDS_Vertex newV1 = mkver1.Vertex();
347   
348   BRepBuilderAPI_MakeVertex mkver2( p2 );
349   TopoDS_Vertex newV2 = mkver2.Vertex();
350   BRep_Builder B;
351   B.UpdateVertex ( newV1,Precision::Confusion());
352   B.UpdateVertex ( newV2, Precision::Confusion());
353   Standard_Boolean isBuild = Standard_False;
354   TopoDS_Edge edge;
355   B.MakeEdge ( edge );
356   ShapeBuild_Edge sbe;
357   //if all edges belong to one face that gap will be closed in the 2D by line
358   //than 3D curve will be built by 2D curve
359   if(theCommonFaces.Length()) {
360     
361     TopoDS_Face aF1 = TopoDS::Face(theCommonFaces.Value(1));
362     Handle(Geom2d_Curve) c2d1,c2d2;
363     gp_Pnt2d p2d1,p2d2;
364     Standard_Real a1, b1,a2, b2;
365     if (  asae.PCurve ( aE1, aF1, c2d1, a1, b1, Standard_True )  && 
366         asae.PCurve ( aE2, aF1, c2d2, a2, b2, Standard_True )) {
367       
368       c2d1->D0 ( b1, p2d1);
369       c2d2->D0 ( a2, p2d2);
370       gp_Vec2d v12 ( p2d1, p2d2 );
371       
372       Handle(Geom2d_Line) aLine2d = new Geom2d_Line ( p2d1, gp_Dir2d ( v12 ) );
373       B.UpdateEdge ( edge, aLine2d, aF1, ::Precision::Confusion() );
374       B.Range ( edge, aF1, 0.0, v12.Magnitude() );
375       
376       isBuild = sbe.BuildCurve3d ( edge );
377     }
378   }
379   //else gap will be closed in the 3D by line.
380   if(!isBuild) {
381     gp_Vec v1 ( p1, p2 );
382     Handle(Geom_Line) aLine = new Geom_Line ( p1, gp_Dir ( v1 ) );
383     B.UpdateEdge ( edge, aLine, ::Precision::Confusion() );
384     B.Range ( edge,0.0, v1.Magnitude());
385   }
386   B.Add ( edge, newV1.Oriented ( TopAbs_FORWARD ) );
387   B.Add ( edge, newV2.Oriented ( TopAbs_REVERSED ) );
388   myContext->Replace(aV1, newV1.Oriented (aV1.Orientation()));
389   myContext->Replace(aV2, newV2.Oriented (aV2.Orientation()));
390   if(isBuild) {
391     newV1.Orientation(aV1.Orientation());
392     TopoDS_Edge newEdge = sbe.CopyReplaceVertices ( aE1,TopoDS_Vertex(), newV1);
393     TopoDS_Wire aw;
394     B.MakeWire(aw);
395     B.Add(aw,newEdge);
396     B.Add(aw,edge);
397     TopoDS_Shape anE1 = myContext->Apply(aE1);
398     myContext->Replace(anE1,aw);
399   }
400   else {
401     myFreeEdges.Append(edge);
402   }
403 }
404
405 //=======================================================================
406 //function : UpdateWire
407 //purpose  : 
408 //=======================================================================
409 void ShHealOper_CloseContour::updateWire (Handle(ShapeExtend_WireData)& sbwd) 
410 {
411   Standard_Integer i=1;
412   for ( ; i <= sbwd->NbEdges(); i++ ) {
413     TopoDS_Edge E = sbwd->Edge(i);
414     TopoDS_Shape S = myContext->Apply ( E );
415     if ( S == E ) continue;
416     for ( TopExp_Explorer exp(S,TopAbs_EDGE); exp.More(); exp.Next() )
417       sbwd->Add ( exp.Current(), i++ );
418     sbwd->Remove ( i-- );
419   }
420   for ( i =1; i <= myFreeEdges.Length(); i++ ) {
421     sbwd->Add(TopoDS::Edge(myFreeEdges.Value(i)));
422   }
423 }