Salome HOME
Update copyright
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SplineDriver.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_SplineDriver.hxx>
26 #include <GEOMImpl_ISpline.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <BRep_Tool.hxx>
33
34 #include <TopAbs.hxx>
35 #include <TopExp.hxx>
36 #include <TopoDS.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Vertex.hxx>
40
41 #include <Geom_BezierCurve.hxx>
42 //#include <GeomAPI_PointsToBSpline.hxx>
43 #include <GeomAPI_Interpolate.hxx>
44
45 #include <gp.hxx>
46 #include <gp_Pnt.hxx>
47 #include <gp_Circ.hxx>
48 #include <Precision.hxx>
49 #include <TColgp_Array1OfPnt.hxx>
50 #include <TColgp_HArray1OfPnt.hxx>
51
52 //=======================================================================
53 //function : GetID
54 //purpose  :
55 //=======================================================================
56 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
57 {
58   static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
59   return aSplineDriver;
60 }
61
62
63 //=======================================================================
64 //function : GEOMImpl_SplineDriver
65 //purpose  :
66 //=======================================================================
67 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
68 {
69 }
70
71 //=======================================================================
72 //function : Execute
73 //purpose  :
74 //=======================================================================
75 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
76 {
77   if (Label().IsNull()) return 0;
78   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79
80   GEOMImpl_ISpline aCI (aFunction);
81   Standard_Integer aType = aFunction->GetType();
82
83   TopoDS_Shape aShape;
84
85   if (aType == SPLINE_BEZIER || aType == SPLINE_INTERPOLATION) {
86
87     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
88     TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
89     if(useCoords) {
90       Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
91       int anArrayLength = aCoordsArray->Length();
92       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
93         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
94         points.SetValue(j,aPnt);
95         j++;
96       } 
97     }
98
99     
100     int ind, aLen = aCI.GetLength();
101     if (aLen < 2) return 0;
102     Standard_Boolean isSeveral = Standard_False;
103     gp_Pnt aPrevP;
104     int aRealLen = aLen;
105     if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
106       TopoDS_Vertex aV1;
107       if(useCoords) {
108         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
109       } else {
110         Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
111         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
112         aV1 = TopoDS::Vertex(aFirstPnt);
113       }
114
115       TopoDS_Vertex aV2;
116       if(useCoords) { 
117         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
118       } else {
119         Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
120         TopoDS_Shape aLastPnt = aLPoint->GetValue();
121         aV2 = TopoDS::Vertex(aLastPnt);
122       }
123       
124       if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
125         aRealLen++;
126       }
127     }
128     
129     TColgp_Array1OfPnt CurvePoints (1, aRealLen);
130     for (ind = 1; ind <= aLen; ind++) {
131       gp_Pnt aP;
132       if( useCoords ) { 
133         aP = points.Value(ind);
134         if (!isSeveral && ind > 1) {
135           if (aP.Distance(aPrevP) > Precision::Confusion()) {
136             isSeveral = Standard_True;
137           }
138         }
139         CurvePoints.SetValue(ind, aP);
140         aPrevP = aP;
141       } else {      
142         Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
143         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
144         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
145           aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
146           if (!isSeveral && ind > 1) {
147             if (aP.Distance(aPrevP) > Precision::Confusion()) {
148               isSeveral = Standard_True;
149             }
150           }
151           CurvePoints.SetValue(ind, aP);
152           aPrevP = aP;
153         }
154       }
155     }
156     if (aType == SPLINE_BEZIER) {
157       if (!isSeveral) {
158         Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
159       }
160       if (aRealLen > aLen) { // set last point equal to first for the closed curve
161         CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
162       }
163       Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve(CurvePoints);
164       aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
165     } else {
166       //GeomAPI_PointsToBSpline GBC (CurvePoints);
167       //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
168       
169       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt(1, aLen);
170
171       if (aCI.GetDoReordering()) {
172         for (int curInd = 1; curInd < aLen - 1; curInd++) {
173           gp_Pnt curPnt = CurvePoints.Value(curInd);
174           int nearInd = 0;
175           double nearDist = RealLast();
176           for (ind = curInd + 1; ind <= aLen; ind++) {
177             double dist = curPnt.SquareDistance(CurvePoints.Value(ind));
178             if (dist < nearDist && (nearDist - dist) > Precision::Confusion()) {
179               nearInd = ind;
180               nearDist = dist;
181             }
182           }
183           if (nearInd > 0 && nearInd != curInd + 1) {
184             // Keep given order of points to use it in case of equidistant candidates
185             //               .-<---<-.
186             //              /         \
187             // o  o  o  c  o->o->o->o->n  o  o
188             //          |  |           |
189             //     curInd  curInd+1    nearInd
190             gp_Pnt nearPnt = CurvePoints.Value(nearInd);
191             for (ind = nearInd; ind > curInd + 1; ind--) {
192               CurvePoints.SetValue(ind, CurvePoints.Value(ind - 1));
193             }
194             CurvePoints.SetValue(curInd + 1, nearPnt);
195           }
196         }
197         for (ind = 1; ind <= aLen; ind++) {
198           aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
199         }
200       }
201       else {
202         for (ind = 1; ind <= aLen; ind++) {
203           aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
204         }
205       }
206       
207       bool isClosed = aCI.GetIsClosed();
208       GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
209       GBC.Perform();
210       if (GBC.IsDone())
211         aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
212       else
213         return 0;
214     }
215   }
216   else {
217   }
218   
219   if (aShape.IsNull()) return 0;
220   
221   aFunction->SetValue(aShape);
222   
223   log.SetTouched(Label());
224   
225   return 1;
226 }
227
228
229 //=======================================================================
230 //function :  GEOMImpl_SplineDriver_Type_
231 //purpose  :
232 //=======================================================================
233 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_Type_()
234 {
235
236   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
237   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
238   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
239   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
240   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
241   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
242
243
244   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
245   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_SplineDriver",
246                                                          sizeof(GEOMImpl_SplineDriver),
247                                                          1,
248                                                          (Standard_Address)_Ancestors,
249                                                          (Standard_Address)NULL);
250
251   return _aType;
252 }
253
254 //=======================================================================
255 //function : DownCast
256 //purpose  :
257 //=======================================================================
258 const Handle(GEOMImpl_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
259 {
260   Handle(GEOMImpl_SplineDriver) _anOtherObject;
261
262   if (!AnObject.IsNull()) {
263      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
264        _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
265      }
266   }
267
268   return _anOtherObject ;
269 }