]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_SplineDriver.cxx
Salome HOME
Merge from V6_main 06/03/2013
[modules/geom.git] / src / GEOMImpl / GEOMImpl_SplineDriver.cxx
1 // Copyright (C) 2007-2012  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 #include <GEOMImpl_SplineDriver.hxx>
23
24 #include <GEOMImpl_ISpline.hxx>
25 #include <GEOMImpl_Types.hxx>
26
27 #include <GEOM_Function.hxx>
28 #include <GEOMUtils.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 #include <Standard_Stream.hxx>
53
54 #include <Standard_NullObject.hxx>
55
56 //=======================================================================
57 //function : GetID
58 //purpose  :
59 //=======================================================================
60 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
61 {
62   static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
63   return aSplineDriver;
64 }
65
66
67 //=======================================================================
68 //function : GEOMImpl_SplineDriver
69 //purpose  :
70 //=======================================================================
71 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
72 {
73 }
74
75 //=======================================================================
76 //function : Execute
77 //purpose  :
78 //=======================================================================
79 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
80 {
81   if (Label().IsNull()) return 0;
82   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
83
84   GEOMImpl_ISpline aCI (aFunction);
85   Standard_Integer aType = aFunction->GetType();
86
87   TopoDS_Shape aShape;
88
89   if (aType == SPLINE_BEZIER ||
90       aType == SPLINE_INTERPOLATION ||
91       aType == SPLINE_INTERPOL_TANGENTS) {
92
93     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
94
95     Handle(TColStd_HArray1OfReal) aCoordsArray; // parametric case
96     Handle(TColStd_HSequenceOfTransient) aPoints; // points case
97
98     int aLen = 0;
99     if (useCoords) {
100       aCoordsArray = aCI.GetCoordinates();
101       aLen = aCoordsArray->Length() / 3;
102     }
103     else {
104       aPoints = aCI.GetPoints();
105       aLen = aPoints->Length();
106     }
107
108     if (aLen < 2) return 0;
109
110     TColgp_Array1OfPnt points (1, (useCoords ? aLen : 1));
111     if (useCoords) {
112       int anArrayLength = aCoordsArray->Length();
113       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
114         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
115         points.SetValue(j, aPnt);
116         j++;
117       }
118     }
119
120     int aRealLen = aLen;
121
122     if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
123       TopoDS_Vertex aV1;
124       if (useCoords) {
125         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
126       }
127       else {
128         Handle(GEOM_Function) aFPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(1));
129         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
130         aV1 = TopoDS::Vertex(aFirstPnt);
131       }
132
133       TopoDS_Vertex aV2;
134       if (useCoords) {
135         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
136       }
137       else {
138         Handle(GEOM_Function) aLPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(aLen));
139         TopoDS_Shape aLastPnt = aLPoint->GetValue();
140         aV2 = TopoDS::Vertex(aLastPnt);
141       }
142
143       if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
144         aRealLen++;
145       }
146     }
147
148     int ind;
149     Standard_Boolean isSeveral = Standard_False;
150     gp_Pnt aPrevP;
151
152     TColgp_Array1OfPnt CurvePoints (1, aRealLen);
153     for (ind = 1; ind <= aLen; ind++) {
154       gp_Pnt aP;
155       if (useCoords) {
156         aP = points.Value(ind);
157         if (!isSeveral && ind > 1) {
158           if (aP.Distance(aPrevP) > Precision::Confusion()) {
159             isSeveral = Standard_True;
160           }
161         }
162         CurvePoints.SetValue(ind, aP);
163         aPrevP = aP;
164       }
165       else {
166         Handle(GEOM_Function) aRefPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(ind));
167         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
168         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
169           aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
170           if (!isSeveral && ind > 1) {
171             if (aP.Distance(aPrevP) > Precision::Confusion()) {
172               isSeveral = Standard_True;
173             }
174           }
175           CurvePoints.SetValue(ind, aP);
176           aPrevP = aP;
177         }
178       }
179     }
180
181     if (aType == SPLINE_BEZIER) {
182       if (!isSeveral) {
183         Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
184       }
185       if (aRealLen > aLen) { // set last point equal to first for the closed curve
186         CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
187       }
188       Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve (CurvePoints);
189       aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
190     }
191     else {
192       //GeomAPI_PointsToBSpline GBC (CurvePoints);
193       //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
194
195       if (aCI.GetDoReordering()) {
196         for (int curInd = 1; curInd < aLen - 1; curInd++) {
197           gp_Pnt curPnt = CurvePoints.Value(curInd);
198           int nearInd = 0;
199           double nearDist = RealLast();
200           for (ind = curInd + 1; ind <= aLen; ind++) {
201             double dist = curPnt.SquareDistance(CurvePoints.Value(ind));
202             if (dist < nearDist && (nearDist - dist) > Precision::Confusion()) {
203               nearInd = ind;
204               nearDist = dist;
205             }
206           }
207           if (nearInd > 0 && nearInd != curInd + 1) {
208             // Keep given order of points to use it in case of equidistant candidates
209             //               .-<---<-.
210             //              /         \
211             // o  o  o  c  o->o->o->o->n  o  o
212             //          |  |           |
213             //     curInd  curInd+1    nearInd
214             gp_Pnt nearPnt = CurvePoints.Value(nearInd);
215             for (ind = nearInd; ind > curInd + 1; ind--) {
216               CurvePoints.SetValue(ind, CurvePoints.Value(ind - 1));
217             }
218             CurvePoints.SetValue(curInd + 1, nearPnt);
219           }
220         }
221       }
222
223       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
224       for (ind = 1; ind <= aLen; ind++) {
225         aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
226       }
227
228       bool isClosed = Standard_False;
229       if (aType == SPLINE_INTERPOLATION)
230         isClosed = aCI.GetIsClosed();
231
232       GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
233
234       if (aType == SPLINE_INTERPOL_TANGENTS) {
235         Handle(GEOM_Function) aVec1Ref  = aCI.GetFirstVector();
236         Handle(GEOM_Function) aVec2Ref  = aCI.GetLastVector();
237
238         if (aVec1Ref.IsNull() || aVec2Ref.IsNull())
239           Standard_NullObject::Raise("Null object is given for a vector");
240
241         TopoDS_Shape aVec1Sh = aVec1Ref->GetValue();
242         TopoDS_Shape aVec2Sh = aVec2Ref->GetValue();
243
244         gp_Vec aV1 = GEOMUtils::GetVector(aVec1Sh);
245         gp_Vec aV2 = GEOMUtils::GetVector(aVec2Sh);
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 //function :  GEOMImpl_SplineDriver_Type_
272 //purpose  :
273 //=======================================================================
274 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_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_SplineDriver",
287                                                          sizeof(GEOMImpl_SplineDriver),
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_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
300 {
301   Handle(GEOMImpl_SplineDriver) _anOtherObject;
302
303   if (!AnObject.IsNull()) {
304      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
305        _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
306      }
307   }
308
309   return _anOtherObject ;
310 }