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
22 #include <GEOMImpl_SplineDriver.hxx>
24 #include <GEOMImpl_ISpline.hxx>
25 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28 #include <GEOMUtils.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 #include <Standard_Stream.hxx>
54 #include <Standard_NullObject.hxx>
56 //=======================================================================
59 //=======================================================================
60 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
62 static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
67 //=======================================================================
68 //function : GEOMImpl_SplineDriver
70 //=======================================================================
71 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
75 //=======================================================================
78 //=======================================================================
79 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
81 if (Label().IsNull()) return 0;
82 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
84 GEOMImpl_ISpline aCI (aFunction);
85 Standard_Integer aType = aFunction->GetType();
89 if (aType == SPLINE_BEZIER ||
90 aType == SPLINE_INTERPOLATION ||
91 aType == SPLINE_INTERPOL_TANGENTS) {
93 bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
95 Handle(TColStd_HArray1OfReal) aCoordsArray; // parametric case
96 Handle(TColStd_HSequenceOfTransient) aPoints; // points case
100 aCoordsArray = aCI.GetCoordinates();
101 aLen = aCoordsArray->Length() / 3;
104 aPoints = aCI.GetPoints();
105 aLen = aPoints->Length();
108 if (aLen < 2) return 0;
110 TColgp_Array1OfPnt points (1, (useCoords ? aLen : 1));
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);
122 if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
125 aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
128 Handle(GEOM_Function) aFPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(1));
129 TopoDS_Shape aFirstPnt = aFPoint->GetValue();
130 aV1 = TopoDS::Vertex(aFirstPnt);
135 aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
138 Handle(GEOM_Function) aLPoint = Handle(GEOM_Function)::DownCast(aPoints->Value(aLen));
139 TopoDS_Shape aLastPnt = aLPoint->GetValue();
140 aV2 = TopoDS::Vertex(aLastPnt);
143 if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
149 Standard_Boolean isSeveral = Standard_False;
152 TColgp_Array1OfPnt CurvePoints (1, aRealLen);
153 for (ind = 1; ind <= aLen; ind++) {
156 aP = points.Value(ind);
157 if (!isSeveral && ind > 1) {
158 if (aP.Distance(aPrevP) > Precision::Confusion()) {
159 isSeveral = Standard_True;
162 CurvePoints.SetValue(ind, aP);
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;
175 CurvePoints.SetValue(ind, aP);
181 if (aType == SPLINE_BEZIER) {
183 Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
185 if (aRealLen > aLen) { // set last point equal to first for the closed curve
186 CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
188 Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve (CurvePoints);
189 aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
192 //GeomAPI_PointsToBSpline GBC (CurvePoints);
193 //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
195 if (aCI.GetDoReordering()) {
196 for (int curInd = 1; curInd < aLen - 1; curInd++) {
197 gp_Pnt curPnt = CurvePoints.Value(curInd);
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()) {
207 if (nearInd > 0 && nearInd != curInd + 1) {
208 // Keep given order of points to use it in case of equidistant candidates
211 // o o o c o->o->o->o->n o o
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));
218 CurvePoints.SetValue(curInd + 1, nearPnt);
223 Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt (1, aLen);
224 for (ind = 1; ind <= aLen; ind++) {
225 aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
228 bool isClosed = Standard_False;
229 if (aType == SPLINE_INTERPOLATION)
230 isClosed = aCI.GetIsClosed();
232 GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
234 if (aType == SPLINE_INTERPOL_TANGENTS) {
235 Handle(GEOM_Function) aVec1Ref = aCI.GetFirstVector();
236 Handle(GEOM_Function) aVec2Ref = aCI.GetLastVector();
238 if (aVec1Ref.IsNull() || aVec2Ref.IsNull())
239 Standard_NullObject::Raise("Null object is given for a vector");
241 TopoDS_Shape aVec1Sh = aVec1Ref->GetValue();
242 TopoDS_Shape aVec2Sh = aVec2Ref->GetValue();
244 gp_Vec aV1 = GEOMUtils::GetVector(aVec1Sh);
245 gp_Vec aV2 = GEOMUtils::GetVector(aVec2Sh);
247 GBC.Load(aV1, aV2, /*Scale*/Standard_True);
252 aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
260 if (aShape.IsNull()) return 0;
262 aFunction->SetValue(aShape);
264 log.SetTouched(Label());
270 //=======================================================================
271 //function : GEOMImpl_SplineDriver_Type_
273 //=======================================================================
274 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_Type_()
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);
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),
289 (Standard_Address)_Ancestors,
290 (Standard_Address)NULL);
295 //=======================================================================
296 //function : DownCast
298 //=======================================================================
299 const Handle(GEOMImpl_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
301 Handle(GEOMImpl_SplineDriver) _anOtherObject;
303 if (!AnObject.IsNull()) {
304 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
305 _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
309 return _anOtherObject ;