1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Standard_Stream.hxx>
25 #include <GEOMImpl_SplineDriver.hxx>
26 #include <GEOMImpl_ISpline.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
30 #include <BRepBuilderAPI_MakeEdge.hxx>
31 #include <BRepBuilderAPI_MakeVertex.hxx>
32 #include <BRep_Tool.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Vertex.hxx>
41 #include <Geom_BezierCurve.hxx>
42 //#include <GeomAPI_PointsToBSpline.hxx>
43 #include <GeomAPI_Interpolate.hxx>
47 #include <gp_Circ.hxx>
48 #include <Precision.hxx>
49 #include <TColgp_Array1OfPnt.hxx>
50 #include <TColgp_HArray1OfPnt.hxx>
52 //=======================================================================
55 //=======================================================================
56 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
58 static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
63 //=======================================================================
64 //function : GEOMImpl_SplineDriver
66 //=======================================================================
67 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
71 //=======================================================================
74 //=======================================================================
75 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
77 if (Label().IsNull()) return 0;
78 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
80 GEOMImpl_ISpline aCI (aFunction);
81 Standard_Integer aType = aFunction->GetType();
85 if (aType == SPLINE_BEZIER || aType == SPLINE_INTERPOLATION) {
87 bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
88 TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
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);
100 int ind, aLen = aCI.GetLength();
101 if (aLen < 2) return 0;
102 Standard_Boolean isSeveral = Standard_False;
105 if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
108 aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
110 Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
111 TopoDS_Shape aFirstPnt = aFPoint->GetValue();
112 aV1 = TopoDS::Vertex(aFirstPnt);
117 aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
119 Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
120 TopoDS_Shape aLastPnt = aLPoint->GetValue();
121 aV2 = TopoDS::Vertex(aLastPnt);
124 if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
129 TColgp_Array1OfPnt CurvePoints (1, aRealLen);
130 for (ind = 1; ind <= aLen; ind++) {
133 aP = points.Value(ind);
134 if (!isSeveral && ind > 1) {
135 if (aP.Distance(aPrevP) > Precision::Confusion()) {
136 isSeveral = Standard_True;
139 CurvePoints.SetValue(ind, aP);
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;
151 CurvePoints.SetValue(ind, aP);
156 if (aType == SPLINE_BEZIER) {
158 Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
160 if (aRealLen > aLen) { // set last point equal to first for the closed curve
161 CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
163 Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve(CurvePoints);
164 aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
166 //GeomAPI_PointsToBSpline GBC (CurvePoints);
167 //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
169 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt(1, aLen);
171 if (aCI.GetDoReordering()) {
172 for (int curInd = 1; curInd < aLen - 1; curInd++) {
173 gp_Pnt curPnt = CurvePoints.Value(curInd);
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()) {
183 if (nearInd > 0 && nearInd != curInd + 1) {
184 // Keep given order of points to use it in case of equidistant candidates
187 // o o o c o->o->o->o->n o o
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));
194 CurvePoints.SetValue(curInd + 1, nearPnt);
197 for (ind = 1; ind <= aLen; ind++) {
198 aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
202 for (ind = 1; ind <= aLen; ind++) {
203 aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
207 bool isClosed = aCI.GetIsClosed();
208 GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
211 aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
219 if (aShape.IsNull()) return 0;
221 aFunction->SetValue(aShape);
223 log.SetTouched(Label());
229 //=======================================================================
230 //function : GEOMImpl_SplineDriver_Type_
232 //=======================================================================
233 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_Type_()
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);
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),
248 (Standard_Address)_Ancestors,
249 (Standard_Address)NULL);
254 //=======================================================================
255 //function : DownCast
257 //=======================================================================
258 const Handle(GEOMImpl_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
260 Handle(GEOMImpl_SplineDriver) _anOtherObject;
262 if (!AnObject.IsNull()) {
263 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
264 _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
268 return _anOtherObject ;