1 // Copyright (C) 2007-2013 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 <GEOMImpl_SplineDriver.hxx>
25 #include <GEOMImpl_ISpline.hxx>
26 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29 #include <GEOMUtils.hxx>
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeVertex.hxx>
33 #include <BRep_Tool.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Vertex.hxx>
42 #include <Geom_BezierCurve.hxx>
43 //#include <GeomAPI_PointsToBSpline.hxx>
44 #include <GeomAPI_Interpolate.hxx>
48 #include <gp_Circ.hxx>
49 #include <Precision.hxx>
50 #include <TColgp_Array1OfPnt.hxx>
51 #include <TColgp_HArray1OfPnt.hxx>
53 #include <Standard_Stream.hxx>
55 #include <Standard_NullObject.hxx>
57 //=======================================================================
60 //=======================================================================
61 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
63 static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
68 //=======================================================================
69 //function : GEOMImpl_SplineDriver
71 //=======================================================================
72 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
76 //=======================================================================
79 //=======================================================================
80 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
82 if (Label().IsNull()) return 0;
83 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
85 GEOMImpl_ISpline aCI (aFunction);
86 Standard_Integer aType = aFunction->GetType();
90 if (aType == SPLINE_BEZIER ||
91 aType == SPLINE_INTERPOLATION ||
92 aType == SPLINE_INTERPOL_TANGENTS) {
94 bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
96 Handle(TColStd_HArray1OfReal) aCoordsArray; // parametric case
97 Handle(TColStd_HSequenceOfTransient) aPoints; // points case
101 aCoordsArray = aCI.GetCoordinates();
102 aLen = aCoordsArray->Length() / 3;
105 aPoints = aCI.GetPoints();
106 aLen = aPoints->Length();
109 if (aLen < 2) return 0;
111 TColgp_Array1OfPnt points (1, (useCoords ? aLen : 1));
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);
123 if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
126 aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
129 Handle(GEOM_Function) aFPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(1));
130 TopoDS_Shape aFirstPnt = aFPoint->GetValue();
131 aV1 = TopoDS::Vertex(aFirstPnt);
136 aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
139 Handle(GEOM_Function) aLPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(aLen));
140 TopoDS_Shape aLastPnt = aLPoint->GetValue();
141 aV2 = TopoDS::Vertex(aLastPnt);
144 if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
150 Standard_Boolean isSeveral = Standard_False;
153 TColgp_Array1OfPnt CurvePoints (1, aRealLen);
154 for (ind = 1; ind <= aLen; ind++) {
157 aP = points.Value(ind);
158 if (!isSeveral && ind > 1) {
159 if (aP.Distance(aPrevP) > Precision::Confusion()) {
160 isSeveral = Standard_True;
163 CurvePoints.SetValue(ind, aP);
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;
176 CurvePoints.SetValue(ind, aP);
182 if (aType == SPLINE_BEZIER) {
184 Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
186 if (aRealLen > aLen) { // set last point equal to first for the closed curve
187 CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
189 Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve (CurvePoints);
190 aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
193 //GeomAPI_PointsToBSpline GBC (CurvePoints);
194 //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
196 if (aCI.GetDoReordering()) {
197 for (int curInd = 1; curInd < aLen - 1; curInd++) {
198 gp_Pnt curPnt = CurvePoints.Value(curInd);
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()) {
208 if (nearInd > 0 && nearInd != curInd + 1) {
209 // Keep given order of points to use it in case of equidistant candidates
212 // o o o c o->o->o->o->n o o
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));
219 CurvePoints.SetValue(curInd + 1, nearPnt);
224 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
225 for (ind = 1; ind <= aLen; ind++) {
226 aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
229 bool isClosed = Standard_False;
230 if (aType == SPLINE_INTERPOLATION)
231 isClosed = aCI.GetIsClosed();
233 GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
235 if (aType == SPLINE_INTERPOL_TANGENTS) {
236 Handle(GEOM_Function) aVec1Ref = aCI.GetFirstVector();
237 Handle(GEOM_Function) aVec2Ref = aCI.GetLastVector();
239 if (aVec1Ref.IsNull() || aVec2Ref.IsNull())
240 Standard_NullObject::Raise("Null object is given for a vector");
242 TopoDS_Shape aVec1Sh = aVec1Ref->GetValue();
243 TopoDS_Shape aVec2Sh = aVec2Ref->GetValue();
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);
249 GBC.Load(aV1, aV2, /*Scale*/Standard_True);
254 aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
262 if (aShape.IsNull()) return 0;
264 aFunction->SetValue(aShape);
266 log.SetTouched(Label());
272 //=======================================================================
273 //function : GEOMImpl_SplineDriver_Type_
275 //=======================================================================
276 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_Type_()
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);
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),
291 (Standard_Address)_Ancestors,
292 (Standard_Address)NULL);
297 //=======================================================================
298 //function : DownCast
300 //=======================================================================
301 const Handle(GEOMImpl_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
303 Handle(GEOMImpl_SplineDriver) _anOtherObject;
305 if (!AnObject.IsNull()) {
306 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
307 _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
311 return _anOtherObject ;