Salome HOME
0020731: EDF 1302 GEOM: Fillet 1D anomaly
[modules/geom.git] / src / GEOMImpl / GEOMImpl_Fillet1dDriver.cxx
1 // Copyright (C) 2009  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
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_Fillet1dDriver.hxx>
24
25 #include <GEOMImpl_Fillet1d.hxx>
26 #include <GEOMImpl_IFillet1d.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOMImpl_ILocalOperations.hxx>
29 #include <GEOMImpl_IShapesOperations.hxx>
30 #include <GEOM_Function.hxx>
31
32 #include <gp_Pln.hxx>
33 #include <gp_Dir.hxx>
34 #include <gp_XYZ.hxx>
35
36 #include <BRep_Tool.hxx>
37 #include <BRepTools.hxx>
38 #include <BRepBuilderAPI_MakeWire.hxx>
39 #include <BRepCheck_Analyzer.hxx>
40
41 #include <Precision.hxx>
42
43 #include <ShapeFix_Wire.hxx>
44 #include <StdFail_NotDone.hxx>
45 #include <Standard_ConstructionError.hxx>
46
47 #include <TopAbs.hxx>
48 #include <TopoDS.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Wire.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopExp.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopTools_DataMapOfShapeShape.hxx>
55 #include <TopTools_ListOfShape.hxx>
56 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
58
59 //=======================================================================
60 //function : GetID
61 //purpose  :
62 //=======================================================================
63 const Standard_GUID& GEOMImpl_Fillet1dDriver::GetID()
64 {
65   static Standard_GUID aFillet1dDriver("FF60908B-AB2E-4b71-B098-5C256C37D961");
66   return aFillet1dDriver;
67 }
68
69 //=======================================================================
70 //function : GEOMImpl_Fillet1dDriver
71 //purpose  :
72 //=======================================================================
73 GEOMImpl_Fillet1dDriver::GEOMImpl_Fillet1dDriver()
74 {
75 }
76
77 //=======================================================================
78 //function : anotherVertex
79 //purpose  : local function to get vertex from edge
80 //=======================================================================
81 static TopoDS_Vertex anotherVertex( const TopoDS_Edge& theE,
82                                     const TopoDS_Vertex& theV )
83 {
84   // here is an assumption that edge has different vertices
85   TopoDS_Vertex aV;
86   TopExp_Explorer anExp( theE, TopAbs_VERTEX );
87   for ( ; anExp.More(); anExp.Next() )
88   {
89     if ( BRepTools::Compare(theV,TopoDS::Vertex(anExp.Current())) /*theV.IsSame(anExp.Current())*/ )
90       continue;
91     aV = TopoDS::Vertex( anExp.Current() );
92     break;
93   }
94   return aV;
95 }
96            
97 //=======================================================================
98 //function : takePlane
99 //purpose  : local function returns plane of given edges
100 //=======================================================================
101 static Standard_Boolean takePlane( const TopoDS_Edge& theE1,
102                                    const TopoDS_Edge& theE2,
103                                    const TopoDS_Vertex& theV,
104                                    gp_Pln& thePlane )
105 {
106   TopoDS_Vertex aV12 = anotherVertex( theE1, theV );
107   TopoDS_Vertex aV22 = anotherVertex( theE2, theV );
108   // check can closed wire be created by two initial edges
109   if ( aV12.IsNull()  || aV22.IsNull() || aV12.IsSame( aV22 ) )
110     return false;
111
112   // create plane by 3 points
113   gp_XYZ aXYZ = BRep_Tool::Pnt( theV ).XYZ();
114   gp_XYZ aXYZ1 = BRep_Tool::Pnt( aV12 ).XYZ();
115   gp_XYZ aXYZ2 = BRep_Tool::Pnt( aV22 ).XYZ();
116   try {
117     gp_Dir aDir1( aXYZ - aXYZ1 );
118     gp_Dir aDir2( aXYZ2 - aXYZ );
119     Standard_Real anAngle = aDir1.Angle(aDir2);
120     if ( fabs(anAngle) <= gp::Resolution() || 
121          fabs(anAngle - PI) <= gp::Resolution() )
122       return false;
123     thePlane = gp_Pln( gp_Pnt(aXYZ), aDir1^ aDir2);
124   }
125   catch (Standard_Failure) {
126     return false;
127   }
128   return true;
129 }
130
131 //=======================================================================
132 //function : addEdgeRelation
133 //purpose  : local function to remember relation between initial and modified edge
134 //=======================================================================
135 static void addEdgeRelation(TopTools_DataMapOfShapeShape& theMap,
136                             const TopoDS_Edge& theInitE,
137                             const TopoDS_Edge& theResE)
138 {
139   if ( theMap.IsBound( theInitE ) )
140     theMap.ChangeFind( theInitE ) = theResE;
141   else
142     theMap.Bind( theInitE, theResE );
143 }
144
145 //=======================================================================
146 //function : Execute
147 //purpose  :
148 //=======================================================================
149 Standard_Integer GEOMImpl_Fillet1dDriver::Execute(TFunction_Logbook& log) const
150 {
151   if (Label().IsNull()) return 0;
152   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
153
154   GEOMImpl_IFillet1d aCI (aFunction);
155
156   Handle(GEOM_Function) aRefShape = aCI.GetShape();
157   TopoDS_Shape aShape = aRefShape->GetValue();
158   if (aShape.ShapeType() != TopAbs_WIRE)
159     Standard_ConstructionError::Raise("Wrong arguments: polyline as wire must be given");
160
161   TopoDS_Wire aWire = TopoDS::Wire(aShape);
162
163   double rad = aCI.GetR();
164
165   // collect vertices for make fillet
166   TopTools_ListOfShape aVertexList;
167   TopTools_MapOfShape mapShape;
168   int aLen = aCI.GetLength();
169   if ( aLen > 0 ) {
170     for (int ind = 1; ind <= aLen; ind++) {
171       TopoDS_Shape aShapeVertex;
172       if (GEOMImpl_ILocalOperations::GetSubShape
173           (aWire, aCI.GetVertex(ind), aShapeVertex))
174         if (mapShape.Add(aShapeVertex))
175           aVertexList.Append( aShapeVertex );
176     }
177   } else { // get all vertices from wire
178     TopExp_Explorer anExp( aWire, TopAbs_VERTEX );
179     for ( ; anExp.More(); anExp.Next() ) {
180       if (mapShape.Add(anExp.Current()))
181         aVertexList.Append( anExp.Current() );
182     }
183   }
184   if (aVertexList.IsEmpty())
185     Standard_ConstructionError::Raise("Invalid input no vertices to make fillet");
186
187   //INFO: this algorithm implemented in assumption that user can select both
188   //  vertices of some edges to make fillet. In this case we should remember
189   //  already modified initial edges to take care in next fillet step
190   TopTools_DataMapOfShapeShape anEdgeToEdgeMap;
191
192   //iterates on vertices, and make fillet on each couple of edges
193   //collect result fillet edges in list
194   TopTools_ListOfShape aListOfNewEdge;
195   // remember relation between initial and modified map
196   TopTools_IndexedDataMapOfShapeListOfShape aMapVToEdges;
197   TopExp::MapShapesAndAncestors( aWire, TopAbs_VERTEX, TopAbs_EDGE, aMapVToEdges );
198   TopTools_ListIteratorOfListOfShape anIt( aVertexList );
199   for ( ; anIt.More(); anIt.Next() ) {
200     TopoDS_Vertex aV = TopoDS::Vertex( anIt.Value() );
201     if ( aV.IsNull() || !aMapVToEdges.Contains( aV ) )
202       continue;
203     const TopTools_ListOfShape& aVertexEdges = aMapVToEdges.FindFromKey( aV );
204     if ( aVertexEdges.Extent() != 2 )
205       continue; // no input data to make fillet
206     TopoDS_Edge anEdge1 = TopoDS::Edge( aVertexEdges.First() );
207     TopoDS_Edge anEdge2 = TopoDS::Edge( aVertexEdges.Last() );
208     // check if initial edges already modified in previous fillet operation
209     if ( anEdgeToEdgeMap.IsBound( anEdge1 ) ) anEdge1 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge1 ));
210     if ( anEdgeToEdgeMap.IsBound( anEdge2 ) ) anEdge2 = TopoDS::Edge(anEdgeToEdgeMap.Find( anEdge2 ));
211     if ( anEdge1.IsNull() || anEdge2.IsNull() || anEdge1.IsSame( anEdge2 ) )
212       continue; //no input data to make fillet
213     
214     // create plane on 2 edges
215     gp_Pln aPlane;
216     if ( !takePlane(anEdge1, anEdge2, aV, aPlane) )
217       continue; // seems edges does not belong to same plane or parallel (fillet can not be build)
218     
219     GEOMImpl_Fillet1d aFilletAlgo(anEdge1, anEdge2, aPlane);
220     if ( !aFilletAlgo.Perform(rad) )
221       continue; // can not create fillet with given radius
222     
223     // take fillet result in given vertex
224     TopoDS_Edge aModifE1, aModifE2;
225     TopoDS_Edge aNewE = aFilletAlgo.Result(BRep_Tool::Pnt(aV), aModifE1, aModifE2);
226     if (aNewE.IsNull())
227       continue; // no result found
228     
229     // add  new created edges and take modified edges
230     aListOfNewEdge.Append( aNewE );
231     
232     // check if face edges modified,
233     // if yes, than map to original edges (from vertex-edges list), because edges can be modified before
234     if (!aModifE1.IsNull() && !aModifE1.IsSame( anEdge1 ))
235       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.First()), aModifE1 );
236     if (!aModifE2.IsNull() && !aModifE2.IsSame( anEdge2 ))
237       addEdgeRelation( anEdgeToEdgeMap, TopoDS::Edge(aVertexEdges.Last()), aModifE2 );
238   }
239
240   if ( anEdgeToEdgeMap.IsEmpty() && aListOfNewEdge.IsEmpty() ) {
241     StdFail_NotDone::Raise("1D Fillet can't be computed on the given shape with the given radius");
242     return 0;
243   }
244   
245   // create new wire instead of original
246   for ( TopExp_Explorer anExp( aWire, TopAbs_EDGE ); anExp.More(); anExp.Next() ) {
247     TopoDS_Shape anEdge = anExp.Current(); 
248     if ( !anEdgeToEdgeMap.IsBound( anEdge ) )
249       aListOfNewEdge.Append( anEdge );
250     else
251       aListOfNewEdge.Append( anEdgeToEdgeMap.Find( anEdge ) );
252   }
253
254   GEOMImpl_IShapesOperations::SortShapes( aListOfNewEdge );
255
256   BRepBuilderAPI_MakeWire aWireTool;
257   aWireTool.Add( aListOfNewEdge );
258   aWireTool.Build();
259   if (!aWireTool.IsDone())
260     return 0;
261
262   aWire = aWireTool.Wire();
263   aFunction->SetValue(aWire);
264   log.SetTouched(Label());
265   
266   return 1;
267 }
268
269
270 //=======================================================================
271 //function :  GEOMImpl_Fillet1dDriver_Type_
272 //purpose  :
273 //=======================================================================
274 Standard_EXPORT Handle_Standard_Type& GEOMImpl_Fillet1dDriver_Type_()
275 {
276
277   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
278   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
279   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
280   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
281   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
282   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
283
284
285   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
286   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_Fillet1dDriver",
287                                                          sizeof(GEOMImpl_Fillet1dDriver),
288                                                          1,
289                                                          (Standard_Address)_Ancestors,
290                                                          (Standard_Address)NULL);
291
292   return _aType;
293 }
294
295 //=======================================================================
296 //function : DownCast
297 //purpose  :
298 //=======================================================================
299 const Handle(GEOMImpl_Fillet1dDriver) Handle(GEOMImpl_Fillet1dDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
300 {
301   Handle(GEOMImpl_Fillet1dDriver) _anOtherObject;
302
303   if (!AnObject.IsNull()) {
304      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_Fillet1dDriver))) {
305        _anOtherObject = Handle(GEOMImpl_Fillet1dDriver)((Handle(GEOMImpl_Fillet1dDriver)&)AnObject);
306      }
307   }
308
309   return _anOtherObject ;
310 }