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