]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_Fillet1dDriver.cxx
Salome HOME
Merge from V6_main 06/03/2013
[modules/geom.git] / src / GEOMImpl / GEOMImpl_Fillet1dDriver.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 #include <Standard_Stream.hxx>
20
21 #include <GEOMImpl_Fillet1dDriver.hxx>
22
23 #include <GEOMImpl_Fillet1d.hxx>
24 #include <GEOMImpl_IFillet1d.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOMImpl_HealingDriver.hxx>
27
28 #include <GEOM_Function.hxx>
29
30 #include <GEOMUtils.hxx>
31
32 #include <ShapeFix_Wire.hxx>
33 #include <StdFail_NotDone.hxx>
34 #include <Standard_ConstructionError.hxx>
35
36 #include <TopAbs.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Wire.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <TopExp.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_DataMapOfShapeShape.hxx>
44 #include <TopTools_ListOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48
49 #include <BRep_Tool.hxx>
50 #include <BRepTools.hxx>
51 #include <BRepBuilderAPI_MakeWire.hxx>
52 #include <BRepCheck_Analyzer.hxx>
53
54 #include <gp_Pln.hxx>
55 #include <gp_Dir.hxx>
56 #include <gp_XYZ.hxx>
57
58 #include <Precision.hxx>
59
60 //=======================================================================
61 //function : GetID
62 //purpose  :
63 //=======================================================================
64 const Standard_GUID& GEOMImpl_Fillet1dDriver::GetID()
65 {
66   static Standard_GUID aFillet1dDriver("FF60908B-AB2E-4b71-B098-5C256C37D961");
67   return aFillet1dDriver;
68 }
69
70 //=======================================================================
71 //function : GEOMImpl_Fillet1dDriver
72 //purpose  :
73 //=======================================================================
74 GEOMImpl_Fillet1dDriver::GEOMImpl_Fillet1dDriver()
75 {
76 }
77
78 //=======================================================================
79 //function : anotherVertex
80 //purpose  : local function to get vertex from edge
81 //=======================================================================
82 static TopoDS_Vertex anotherVertex( const TopoDS_Edge& theE,
83                                     const TopoDS_Vertex& theV )
84 {
85   // here is an assumption that edge has different vertices
86   TopoDS_Vertex aV;
87   TopExp_Explorer anExp( theE, TopAbs_VERTEX );
88   for ( ; anExp.More(); anExp.Next() )
89   {
90     if ( BRepTools::Compare(theV,TopoDS::Vertex(anExp.Current())) /*theV.IsSame(anExp.Current())*/ )
91       continue;
92     aV = TopoDS::Vertex( anExp.Current() );
93     break;
94   }
95   return aV;
96 }
97
98 //=======================================================================
99 //function : takePlane
100 //purpose  : local function returns plane of given edges
101 //=======================================================================
102 static Standard_Boolean takePlane( const TopoDS_Edge& theE1,
103                                    const TopoDS_Edge& theE2,
104                                    const TopoDS_Vertex& theV,
105                                    gp_Pln& thePlane )
106 {
107   TopoDS_Vertex aV12 = anotherVertex( theE1, theV );
108   TopoDS_Vertex aV22 = anotherVertex( theE2, theV );
109   // check can closed wire be created by two initial edges
110   if ( aV12.IsNull()  || aV22.IsNull() || aV12.IsSame( aV22 ) )
111     return false;
112
113   // create plane by 3 points
114   gp_XYZ aXYZ = BRep_Tool::Pnt( theV ).XYZ();
115   gp_XYZ aXYZ1 = BRep_Tool::Pnt( aV12 ).XYZ();
116   gp_XYZ aXYZ2 = BRep_Tool::Pnt( aV22 ).XYZ();
117   try {
118     gp_Dir aDir1( aXYZ - aXYZ1 );
119     gp_Dir aDir2( aXYZ2 - aXYZ );
120     Standard_Real anAngle = aDir1.Angle(aDir2);
121     if ( fabs(anAngle) <= gp::Resolution() ||
122          fabs(anAngle - M_PI) <= gp::Resolution() )
123       return false;
124     thePlane = gp_Pln( gp_Pnt(aXYZ), aDir1^ aDir2);
125   }
126   catch (Standard_Failure) {
127     return false;
128   }
129   return true;
130 }
131
132 //=======================================================================
133 //function : addEdgeRelation
134 //purpose  : local function to remember relation between initial and modified edge
135 //=======================================================================
136 static void addEdgeRelation(TopTools_DataMapOfShapeShape& theMap,
137                             const TopoDS_Edge& theInitE,
138                             const TopoDS_Edge& theResE)
139 {
140   if ( theMap.IsBound( theInitE ) )
141     theMap.ChangeFind( theInitE ) = theResE;
142   else
143     theMap.Bind( theInitE, theResE );
144 }
145
146 //=======================================================================
147 //function : Execute
148 //purpose  :
149 //=======================================================================
150 Standard_Integer GEOMImpl_Fillet1dDriver::Execute(TFunction_Logbook& log) const
151 {
152   if (Label().IsNull()) return 0;
153   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
154
155   GEOMImpl_IFillet1d aCI (aFunction);
156
157   Handle(GEOM_Function) aRefShape = aCI.GetShape();
158   TopoDS_Shape aShape = aRefShape->GetValue();
159   if (aShape.IsNull())
160     return 0;
161   if (aShape.ShapeType() != TopAbs_WIRE)
162     Standard_ConstructionError::Raise("Wrong arguments: polyline as wire must be given");
163
164   TopoDS_Wire aWire = TopoDS::Wire(aShape);
165
166   bool doIgnoreSecantPoints = aCI.GetFlag();
167
168   double rad = aCI.GetR();
169   if (rad < Precision::Confusion())
170     return 0;
171
172   // collect vertices for make fillet
173   TopTools_ListOfShape aVertexList;
174   TopTools_IndexedMapOfShape anIndices;
175   TopExp::MapShapes(aWire, anIndices);
176   int aLen = aCI.GetLength();
177   if (aLen > 0) {
178     for (int ii = 1; ii <= aLen; ii++) {
179       int ind = aCI.GetVertex(ii);
180       if (1 <= ind && ind <= anIndices.Extent()) {
181         TopoDS_Shape aShapeVertex = anIndices.FindKey(ind);
182         if (aShapeVertex.ShapeType() == TopAbs_VERTEX)
183           aVertexList.Append(aShapeVertex);
184       }
185     }
186   }
187   else { // get all vertices from wire
188     TopTools_MapOfShape mapShape;
189     TopExp_Explorer anExp (aWire, TopAbs_VERTEX);
190     for (; anExp.More(); anExp.Next()) {
191       if (mapShape.Add(anExp.Current()))
192         aVertexList.Append(anExp.Current());
193     }
194   }
195   if (aVertexList.IsEmpty())
196     Standard_ConstructionError::Raise("Invalid input: no vertices to make fillet");
197
198   // at first we try to make fillet on the initial wire (without edges fusing)
199   bool isFinalPass = !doIgnoreSecantPoints;
200   TopoDS_Wire aResult;
201   bool isAllStepsOk = MakeFillet(aWire, aVertexList, rad, isFinalPass, aResult);
202
203   // try to fuse collinear edges to allow bigger radius
204   if (!isFinalPass && !isAllStepsOk) {
205     // 1. Fuse
206     TopoDS_Shape aShapeNew;
207     Handle(TColStd_HSequenceOfTransient) aVerts;
208     GEOMImpl_HealingDriver::FuseCollinearEdges(aWire, aVerts, aShapeNew);
209     TopoDS_Wire aWireNew = TopoDS::Wire(aShapeNew);
210
211     // 2. Rebuild the list of vertices (by coincidence)
212     Standard_Real tol, tolMax = Precision::Confusion();
213     for (TopExp_Explorer ExV (aWireNew, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
214       TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
215       tol = BRep_Tool::Tolerance(Vertex);
216       if (tol > tolMax)
217         tolMax = tol;
218     }
219
220     TopTools_ListOfShape aVertexListNew;
221     TopTools_IndexedMapOfShape anIndicesNew;
222     TopExp::MapShapes(aWireNew, anIndicesNew);
223     TopTools_ListIteratorOfListOfShape anIt (aVertexList);
224     for (; anIt.More(); anIt.Next()) {
225       TopoDS_Vertex aV = TopoDS::Vertex(anIt.Value());
226       if (anIndicesNew.Contains(aV))
227         aVertexListNew.Append(aV);
228       else {
229         // try to find by coords in the new wire
230         gp_Pnt aP = BRep_Tool::Pnt(aV);
231
232         bool isFound = false;
233         TopTools_MapOfShape mapShape;
234         TopExp_Explorer exp (aWireNew, TopAbs_VERTEX);
235         for (; exp.More() && !isFound; exp.Next()) {
236           if (mapShape.Add(exp.Current())) {
237             TopoDS_Vertex aVi = TopoDS::Vertex(exp.Current());
238             gp_Pnt aPi = BRep_Tool::Pnt(aVi);
239             if (aPi.Distance(aP) < tolMax) {
240               aVertexListNew.Append(aVi);
241               isFound = true;
242             }
243           }
244         }
245       }
246     }
247
248     // 3. Repeat the fillet algorithm
249     isFinalPass = true;
250     MakeFillet(aWireNew, aVertexListNew, rad, isFinalPass, aResult);
251   }
252
253   aFunction->SetValue(aResult);
254   log.SetTouched(Label());
255
256   return 1;
257 }
258
259 //=======================================================================
260 //function : MakeFillet
261 //purpose  :
262 //=======================================================================
263 bool GEOMImpl_Fillet1dDriver::MakeFillet(const TopoDS_Wire& aWire,
264                                          const TopTools_ListOfShape& aVertexList,
265                                          const Standard_Real rad,
266                                          bool isFinalPass,
267                                          TopoDS_Wire& aResult) const
268 {
269   // this variable is needed to break execution
270   // in case of fillet failure and try to fuse edges
271   bool isAllStepsOk = true;
272
273   //INFO: this algorithm implemented in assumption that user can select both
274   //  vertices of some edges to make fillet. In this case we should remember
275   //  already modified initial edges to take care in next fillet step
276   TopTools_DataMapOfShapeShape anEdgeToEdgeMap;
277
278   //iterates on vertices, and make fillet on each couple of edges
279   //collect result fillet edges in list
280   TopTools_ListOfShape aListOfNewEdge;
281   // remember relation between initial and modified map
282   TopTools_IndexedDataMapOfShapeListOfShape aMapVToEdges;
283   TopExp::MapShapesAndAncestors( aWire, TopAbs_VERTEX, TopAbs_EDGE, aMapVToEdges );
284   TopTools_ListIteratorOfListOfShape anIt( aVertexList );
285   for ( ; anIt.More(); anIt.Next() ) {
286     TopoDS_Vertex aV = TopoDS::Vertex( anIt.Value() );
287     if ( aV.IsNull() || !aMapVToEdges.Contains( aV ) )
288       continue;
289     const TopTools_ListOfShape& aVertexEdges = aMapVToEdges.FindFromKey( aV );
290     if ( aVertexEdges.Extent() != 2 )
291       continue; // no input data to make fillet
292     TopoDS_Edge anEdge1 = TopoDS::Edge( aVertexEdges.First() );
293     TopoDS_Edge anEdge2 = TopoDS::Edge( aVertexEdges.Last() );
294     // check if initial edges already modified in previous fillet operation
295     if ( anEdgeToEdgeMap.IsBound( anEdge1 ) ) anEdge1 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge1 ));
296     if ( anEdgeToEdgeMap.IsBound( anEdge2 ) ) anEdge2 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge2 ));
297     if ( anEdge1.IsNull() || anEdge2.IsNull() || anEdge1.IsSame( anEdge2 ) )
298       continue; //no input data to make fillet
299
300     // create plane on 2 edges
301     gp_Pln aPlane;
302     if ( !takePlane(anEdge1, anEdge2, aV, aPlane) )
303       continue; // seems edges does not belong to same plane or parallel (fillet can not be build)
304
305     GEOMImpl_Fillet1d aFilletAlgo (anEdge1, anEdge2, aPlane);
306     if (!aFilletAlgo.Perform(rad)) {
307       if (isFinalPass)
308         continue; // can not create fillet with given radius
309       else {
310         isAllStepsOk = false;
311         break; // can not create fillet with given radius
312       }
313     }
314
315     // take fillet result in given vertex
316     TopoDS_Edge aModifE1, aModifE2;
317     TopoDS_Edge aNewE = aFilletAlgo.Result(BRep_Tool::Pnt(aV), aModifE1, aModifE2);
318     if (aNewE.IsNull()) {
319       if (isFinalPass)
320         continue; // no result found
321       else {
322         isAllStepsOk = false;
323         break; // no result found
324       }
325     }
326
327     // add  new created edges and take modified edges
328     aListOfNewEdge.Append(aNewE);
329
330     // check if wire edges modified,
331     // if yes, then map to original edges (from vertex-edges list), because edges can be modified before
332     if (aModifE1.IsNull() || !anEdge1.IsSame( aModifE1 ))
333       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.First()), aModifE1 );
334     if (aModifE2.IsNull() || !anEdge2.IsSame( aModifE2 ))
335       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.Last()), aModifE2 );
336   }
337
338   if (anEdgeToEdgeMap.IsEmpty() && aListOfNewEdge.IsEmpty()) {
339     if (isFinalPass)
340       StdFail_NotDone::Raise("1D Fillet can't be computed on the given shape with the given radius");
341     else
342       isAllStepsOk = false;
343   }
344
345   if (!isAllStepsOk)
346     return false;
347
348   // create new wire instead of original
349   for (TopExp_Explorer anExp (aWire, TopAbs_EDGE); anExp.More(); anExp.Next()) {
350     TopoDS_Shape anEdge = anExp.Current();
351     if (!anEdgeToEdgeMap.IsBound(anEdge))
352       aListOfNewEdge.Append(anEdge);
353     else if (!anEdgeToEdgeMap.Find(anEdge).IsNull())
354       aListOfNewEdge.Append(anEdgeToEdgeMap.Find(anEdge));
355   }
356
357   GEOMUtils::SortShapes(aListOfNewEdge);
358
359   BRepBuilderAPI_MakeWire aWireTool;
360   aWireTool.Add(aListOfNewEdge);
361   aWireTool.Build();
362   if (!aWireTool.IsDone())
363     return 0;
364
365   aResult = aWireTool.Wire();
366
367   return isAllStepsOk;
368 }
369
370
371 //=======================================================================
372 //function :  GEOMImpl_Fillet1dDriver_Type_
373 //purpose  :
374 //=======================================================================
375 Standard_EXPORT Handle_Standard_Type& GEOMImpl_Fillet1dDriver_Type_()
376 {
377   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
378   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
379   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
380   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
381   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
382   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
383
384   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
385   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_Fillet1dDriver",
386                                                          sizeof(GEOMImpl_Fillet1dDriver),
387                                                          1,
388                                                          (Standard_Address)_Ancestors,
389                                                          (Standard_Address)NULL);
390
391   return _aType;
392 }
393
394 //=======================================================================
395 //function : DownCast
396 //purpose  :
397 //=======================================================================
398 const Handle(GEOMImpl_Fillet1dDriver) Handle(GEOMImpl_Fillet1dDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
399 {
400   Handle(GEOMImpl_Fillet1dDriver) _anOtherObject;
401
402   if (!AnObject.IsNull()) {
403      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_Fillet1dDriver))) {
404        _anOtherObject = Handle(GEOMImpl_Fillet1dDriver)((Handle(GEOMImpl_Fillet1dDriver)&)AnObject);
405      }
406   }
407
408   return _anOtherObject;
409 }