Salome HOME
3865908ee541575d650cccbc9d28c480a7321497
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SplineDriver.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 #include "GEOMImpl_ICurveParametric.hxx"
28
29 #include "GEOM_Function.hxx"
30 #include "GEOMUtils.hxx"
31
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRepBuilderAPI_MakeVertex.hxx>
34 #include <BRep_Tool.hxx>
35
36 #include <TopAbs.hxx>
37 #include <TopExp.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Vertex.hxx>
42
43 #include <Geom_BezierCurve.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_NullObject.hxx>
54
55 //=======================================================================
56 //function : GetID
57 //purpose  :
58 //=======================================================================
59 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
60 {
61   static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
62   return aSplineDriver;
63 }
64
65
66 //=======================================================================
67 //function : GEOMImpl_SplineDriver
68 //purpose  :
69 //=======================================================================
70 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
71 {
72 }
73
74 //=======================================================================
75 //function : Execute
76 //purpose  :
77 //=======================================================================
78 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
79 {
80   if (Label().IsNull()) return 0;
81   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
82
83   GEOMImpl_ISpline aCI (aFunction);
84   Standard_Integer aType = aFunction->GetType();
85
86   TopoDS_Shape aShape;
87
88   if (aType == SPLINE_BEZIER ||
89       aType == SPLINE_INTERPOLATION ||
90       aType == SPLINE_INTERPOL_TANGENTS) {
91
92     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
93
94     Handle(TColStd_HArray1OfReal) aCoordsArray; // parametric case
95     Handle(TColStd_HSequenceOfTransient) aPoints; // points case
96
97     int aLen = 0;
98     if (useCoords) {
99       aCoordsArray = aCI.GetCoordinates();
100       aLen = aCoordsArray->Length() / 3;
101     }
102     else {
103       aPoints = aCI.GetPoints();
104       aLen = aPoints->Length();
105     }
106
107     if (aLen < 2) return 0;
108
109     TColgp_Array1OfPnt points (1, (useCoords ? aLen : 1));
110     if (useCoords) {
111       int anArrayLength = aCoordsArray->Length();
112       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
113         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
114         points.SetValue(j, aPnt);
115         j++;
116       }
117     }
118
119     int aRealLen = aLen;
120
121     if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
122       TopoDS_Vertex aV1;
123       if (useCoords) {
124         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
125       }
126       else {
127         Handle(GEOM_Function) aFPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(1));
128         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
129         aV1 = TopoDS::Vertex(aFirstPnt);
130       }
131
132       TopoDS_Vertex aV2;
133       if (useCoords) {
134         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
135       }
136       else {
137         Handle(GEOM_Function) aLPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(aLen));
138         TopoDS_Shape aLastPnt = aLPoint->GetValue();
139         aV2 = TopoDS::Vertex(aLastPnt);
140       }
141
142       if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
143         aRealLen++;
144       }
145     }
146
147     int ind;
148     Standard_Boolean isSeveral = Standard_False;
149     gp_Pnt aPrevP;
150
151     TColgp_Array1OfPnt CurvePoints (1, aRealLen);
152     for (ind = 1; ind <= aLen; ind++) {
153       gp_Pnt aP;
154       if (useCoords) {
155         aP = points.Value(ind);
156         if (!isSeveral && ind > 1) {
157           if (aP.Distance(aPrevP) > Precision::Confusion()) {
158             isSeveral = Standard_True;
159           }
160         }
161         CurvePoints.SetValue(ind, aP);
162         aPrevP = aP;
163       }
164       else {
165         Handle(GEOM_Function) aRefPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(ind));
166         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
167         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
168           aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
169           if (!isSeveral && ind > 1) {
170             if (aP.Distance(aPrevP) > Precision::Confusion()) {
171               isSeveral = Standard_True;
172             }
173           }
174           CurvePoints.SetValue(ind, aP);
175           aPrevP = aP;
176         }
177       }
178     }
179
180     if (aType == SPLINE_BEZIER) {
181       if (!isSeveral) {
182         Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
183       }
184       if (aRealLen > aLen) { // set last point equal to first for the closed curve
185         CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
186       }
187       Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve (CurvePoints);
188       aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
189     }
190     else {
191       //GeomAPI_PointsToBSpline GBC (CurvePoints);
192       //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
193
194       if (aCI.GetDoReordering()) {
195         for (int curInd = 1; curInd < aLen - 1; curInd++) {
196           gp_Pnt curPnt = CurvePoints.Value(curInd);
197           int nearInd = 0;
198           double nearDist = RealLast();
199           for (ind = curInd + 1; ind <= aLen; ind++) {
200             double dist = curPnt.SquareDistance(CurvePoints.Value(ind));
201             if (dist < nearDist && (nearDist - dist) > Precision::Confusion()) {
202               nearInd = ind;
203               nearDist = dist;
204             }
205           }
206           if (nearInd > 0 && nearInd != curInd + 1) {
207             // Keep given order of points to use it in case of equidistant candidates
208             //               .-<---<-.
209             //              /         \
210             // o  o  o  c  o->o->o->o->n  o  o
211             //          |  |           |
212             //     curInd  curInd+1    nearInd
213             gp_Pnt nearPnt = CurvePoints.Value(nearInd);
214             for (ind = nearInd; ind > curInd + 1; ind--) {
215               CurvePoints.SetValue(ind, CurvePoints.Value(ind - 1));
216             }
217             CurvePoints.SetValue(curInd + 1, nearPnt);
218           }
219         }
220       }
221
222       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
223       for (ind = 1; ind <= aLen; ind++) {
224         aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
225       }
226
227       bool isClosed = Standard_False;
228       if (aType == SPLINE_INTERPOLATION)
229         isClosed = aCI.GetIsClosed();
230
231       GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
232
233       if (aType == SPLINE_INTERPOL_TANGENTS) {
234         Handle(GEOM_Function) aVec1Ref  = aCI.GetFirstVector();
235         Handle(GEOM_Function) aVec2Ref  = aCI.GetLastVector();
236
237         if (aVec1Ref.IsNull() || aVec2Ref.IsNull())
238           Standard_NullObject::Raise("Null object is given for a vector");
239
240         TopoDS_Shape aVec1Sh = aVec1Ref->GetValue();
241         TopoDS_Shape aVec2Sh = aVec2Ref->GetValue();
242
243         // take orientation of edge into account to avoid regressions, as it was implemented so
244         gp_Vec aV1 = GEOMUtils::GetVector(aVec1Sh, Standard_True);
245         gp_Vec aV2 = GEOMUtils::GetVector(aVec2Sh, Standard_True);
246
247         GBC.Load(aV1, aV2, /*Scale*/Standard_True);
248       }
249
250       GBC.Perform();
251       if (GBC.IsDone())
252         aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
253       else
254         return 0;
255     }
256   }
257   else {
258   }
259
260   if (aShape.IsNull()) return 0;
261
262   aFunction->SetValue(aShape);
263
264   log.SetTouched(Label());
265
266   return 1;
267 }
268
269 //================================================================================
270 /*!
271  * \brief Returns a name of creation operation and names and values of creation parameters
272  */
273 //================================================================================
274
275 bool GEOMImpl_SplineDriver::
276 GetCreationInformation(std::string&             theOperationName,
277                        std::vector<GEOM_Param>& theParams)
278 {
279   if (Label().IsNull()) return 0;
280   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
281
282   GEOMImpl_ISpline          aCI( function );
283   GEOMImpl_ICurveParametric aPI( function );
284   Standard_Integer aType = function->GetType();
285
286   theOperationName = "CURVE";
287
288   switch ( aType ) {
289   case SPLINE_BEZIER:
290   case SPLINE_INTERPOLATION:
291   case SPLINE_INTERPOL_TANGENTS:
292
293     AddParam( theParams, "Type", ( aType == SPLINE_BEZIER ? "Bezier" : "Interpolation"));
294
295     if ( aPI.HasData() )
296     {
297       AddParam( theParams, "X(t) equation", aPI.GetExprX() );
298       AddParam( theParams, "Y(t) equation", aPI.GetExprY() );
299       AddParam( theParams, "Z(t) equation", aPI.GetExprZ() );
300       AddParam( theParams, "Min t", aPI.GetParamMin() );
301       AddParam( theParams, "Max t", aPI.GetParamMax() );
302       if ( aPI.GetParamNbStep() )
303         AddParam( theParams, "Number of steps", aPI.GetParamNbStep() );
304       else
305         AddParam( theParams, "t step", aPI.GetParamStep() );
306     }
307     else
308     {
309       if ( aCI.GetConstructorType() == COORD_CONSTRUCTOR )
310       {
311         Handle(TColStd_HArray1OfReal) coords = aCI.GetCoordinates();
312         GEOM_Param& pntParam = AddParam( theParams, "Points");
313         pntParam << ( coords->Length() ) / 3 << " points: ";
314         for ( int i = coords->Lower(), nb = coords->Upper(); i <= nb; )
315           pntParam << "( " << coords->Value( i++ )
316                    << ", " << coords->Value( i++ )
317                    << ", " << coords->Value( i++ ) << " ) ";
318       }
319       else
320       {
321         AddParam( theParams, "Points", aCI.GetPoints() );
322       }
323       Handle(GEOM_Function) v1 = aCI.GetFirstVector();
324       Handle(GEOM_Function) v2 = aCI.GetLastVector();
325       if ( !v1.IsNull() ) AddParam( theParams, "First tangent vector", v1 );
326       if ( !v2.IsNull() ) AddParam( theParams, "Last tangent vector", v2 );
327     }
328     break;
329   default:
330     return false;
331   }
332
333   return true;
334 }
335
336 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_SplineDriver,GEOM_BaseDriver);
337 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_SplineDriver,GEOM_BaseDriver);