Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SplineDriver.cxx
1 // Copyright (C) 2007-2013  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 <GEOMImpl_SplineDriver.hxx>
24
25 #include <GEOMImpl_ISpline.hxx>
26 #include <GEOMImpl_Types.hxx>
27
28 #include <GEOM_Function.hxx>
29 #include <GEOMUtils.hxx>
30
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <BRep_Tool.hxx>
34
35 #include <TopAbs.hxx>
36 #include <TopExp.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
41
42 #include <Geom_BezierCurve.hxx>
43 //#include <GeomAPI_PointsToBSpline.hxx>
44 #include <GeomAPI_Interpolate.hxx>
45
46 #include <gp.hxx>
47 #include <gp_Pnt.hxx>
48 #include <gp_Circ.hxx>
49 #include <Precision.hxx>
50 #include <TColgp_Array1OfPnt.hxx>
51 #include <TColgp_HArray1OfPnt.hxx>
52
53 #include <Standard_Stream.hxx>
54
55 #include <Standard_NullObject.hxx>
56
57 //=======================================================================
58 //function : GetID
59 //purpose  :
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
62 {
63   static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
64   return aSplineDriver;
65 }
66
67
68 //=======================================================================
69 //function : GEOMImpl_SplineDriver
70 //purpose  :
71 //=======================================================================
72 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
73 {
74 }
75
76 //=======================================================================
77 //function : Execute
78 //purpose  :
79 //=======================================================================
80 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
81 {
82   if (Label().IsNull()) return 0;
83   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
84
85   GEOMImpl_ISpline aCI (aFunction);
86   Standard_Integer aType = aFunction->GetType();
87
88   TopoDS_Shape aShape;
89
90   if (aType == SPLINE_BEZIER ||
91       aType == SPLINE_INTERPOLATION ||
92       aType == SPLINE_INTERPOL_TANGENTS) {
93
94     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
95
96     Handle(TColStd_HArray1OfReal) aCoordsArray; // parametric case
97     Handle(TColStd_HSequenceOfTransient) aPoints; // points case
98
99     int aLen = 0;
100     if (useCoords) {
101       aCoordsArray = aCI.GetCoordinates();
102       aLen = aCoordsArray->Length() / 3;
103     }
104     else {
105       aPoints = aCI.GetPoints();
106       aLen = aPoints->Length();
107     }
108
109     if (aLen < 2) return 0;
110
111     TColgp_Array1OfPnt points (1, (useCoords ? aLen : 1));
112     if (useCoords) {
113       int anArrayLength = aCoordsArray->Length();
114       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
115         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
116         points.SetValue(j, aPnt);
117         j++;
118       }
119     }
120
121     int aRealLen = aLen;
122
123     if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
124       TopoDS_Vertex aV1;
125       if (useCoords) {
126         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
127       }
128       else {
129         Handle(GEOM_Function) aFPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(1));
130         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
131         aV1 = TopoDS::Vertex(aFirstPnt);
132       }
133
134       TopoDS_Vertex aV2;
135       if (useCoords) {
136         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
137       }
138       else {
139         Handle(GEOM_Function) aLPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(aLen));
140         TopoDS_Shape aLastPnt = aLPoint->GetValue();
141         aV2 = TopoDS::Vertex(aLastPnt);
142       }
143
144       if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
145         aRealLen++;
146       }
147     }
148
149     int ind;
150     Standard_Boolean isSeveral = Standard_False;
151     gp_Pnt aPrevP;
152
153     TColgp_Array1OfPnt CurvePoints (1, aRealLen);
154     for (ind = 1; ind <= aLen; ind++) {
155       gp_Pnt aP;
156       if (useCoords) {
157         aP = points.Value(ind);
158         if (!isSeveral && ind > 1) {
159           if (aP.Distance(aPrevP) > Precision::Confusion()) {
160             isSeveral = Standard_True;
161           }
162         }
163         CurvePoints.SetValue(ind, aP);
164         aPrevP = aP;
165       }
166       else {
167         Handle(GEOM_Function) aRefPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(ind));
168         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
169         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
170           aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
171           if (!isSeveral && ind > 1) {
172             if (aP.Distance(aPrevP) > Precision::Confusion()) {
173               isSeveral = Standard_True;
174             }
175           }
176           CurvePoints.SetValue(ind, aP);
177           aPrevP = aP;
178         }
179       }
180     }
181
182     if (aType == SPLINE_BEZIER) {
183       if (!isSeveral) {
184         Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
185       }
186       if (aRealLen > aLen) { // set last point equal to first for the closed curve
187         CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
188       }
189       Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve (CurvePoints);
190       aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
191     }
192     else {
193       //GeomAPI_PointsToBSpline GBC (CurvePoints);
194       //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
195
196       if (aCI.GetDoReordering()) {
197         for (int curInd = 1; curInd < aLen - 1; curInd++) {
198           gp_Pnt curPnt = CurvePoints.Value(curInd);
199           int nearInd = 0;
200           double nearDist = RealLast();
201           for (ind = curInd + 1; ind <= aLen; ind++) {
202             double dist = curPnt.SquareDistance(CurvePoints.Value(ind));
203             if (dist < nearDist && (nearDist - dist) > Precision::Confusion()) {
204               nearInd = ind;
205               nearDist = dist;
206             }
207           }
208           if (nearInd > 0 && nearInd != curInd + 1) {
209             // Keep given order of points to use it in case of equidistant candidates
210             //               .-<---<-.
211             //              /         \
212             // o  o  o  c  o->o->o->o->n  o  o
213             //          |  |           |
214             //     curInd  curInd+1    nearInd
215             gp_Pnt nearPnt = CurvePoints.Value(nearInd);
216             for (ind = nearInd; ind > curInd + 1; ind--) {
217               CurvePoints.SetValue(ind, CurvePoints.Value(ind - 1));
218             }
219             CurvePoints.SetValue(curInd + 1, nearPnt);
220           }
221         }
222       }
223
224       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
225       for (ind = 1; ind <= aLen; ind++) {
226         aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
227       }
228
229       bool isClosed = Standard_False;
230       if (aType == SPLINE_INTERPOLATION)
231         isClosed = aCI.GetIsClosed();
232
233       GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
234
235       if (aType == SPLINE_INTERPOL_TANGENTS) {
236         Handle(GEOM_Function) aVec1Ref  = aCI.GetFirstVector();
237         Handle(GEOM_Function) aVec2Ref  = aCI.GetLastVector();
238
239         if (aVec1Ref.IsNull() || aVec2Ref.IsNull())
240           Standard_NullObject::Raise("Null object is given for a vector");
241
242         TopoDS_Shape aVec1Sh = aVec1Ref->GetValue();
243         TopoDS_Shape aVec2Sh = aVec2Ref->GetValue();
244
245         // take orientation of edge into account to avoid regressions, as it was implemented so
246         gp_Vec aV1 = GEOMUtils::GetVector(aVec1Sh, Standard_True);
247         gp_Vec aV2 = GEOMUtils::GetVector(aVec2Sh, Standard_True);
248
249         GBC.Load(aV1, aV2, /*Scale*/Standard_True);
250       }
251
252       GBC.Perform();
253       if (GBC.IsDone())
254         aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
255       else
256         return 0;
257     }
258   }
259   else {
260   }
261
262   if (aShape.IsNull()) return 0;
263
264   aFunction->SetValue(aShape);
265
266   log.SetTouched(Label());
267
268   return 1;
269 }
270
271
272 //=======================================================================
273 //function :  GEOMImpl_SplineDriver_Type_
274 //purpose  :
275 //=======================================================================
276 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_Type_()
277 {
278
279   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
280   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
281   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
282   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
283   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
284   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
285
286
287   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
288   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_SplineDriver",
289                                                          sizeof(GEOMImpl_SplineDriver),
290                                                          1,
291                                                          (Standard_Address)_Ancestors,
292                                                          (Standard_Address)NULL);
293
294   return _aType;
295 }
296
297 //=======================================================================
298 //function : DownCast
299 //purpose  :
300 //=======================================================================
301 const Handle(GEOMImpl_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
302 {
303   Handle(GEOMImpl_SplineDriver) _anOtherObject;
304
305   if (!AnObject.IsNull()) {
306      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
307        _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
308      }
309   }
310
311   return _anOtherObject ;
312 }