Back to index

salome-geom  6.5.0
GEOMImpl_SplineDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include <Standard_Stream.hxx>
00024 
00025 #include <GEOMImpl_SplineDriver.hxx>
00026 #include <GEOMImpl_ISpline.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <BRepBuilderAPI_MakeEdge.hxx>
00031 #include <BRepBuilderAPI_MakeVertex.hxx>
00032 #include <BRep_Tool.hxx>
00033 
00034 #include <TopAbs.hxx>
00035 #include <TopExp.hxx>
00036 #include <TopoDS.hxx>
00037 #include <TopoDS_Shape.hxx>
00038 #include <TopoDS_Edge.hxx>
00039 #include <TopoDS_Vertex.hxx>
00040 
00041 #include <Geom_BezierCurve.hxx>
00042 //#include <GeomAPI_PointsToBSpline.hxx>
00043 #include <GeomAPI_Interpolate.hxx>
00044 
00045 #include <gp.hxx>
00046 #include <gp_Pnt.hxx>
00047 #include <gp_Circ.hxx>
00048 #include <Precision.hxx>
00049 #include <TColgp_Array1OfPnt.hxx>
00050 #include <TColgp_HArray1OfPnt.hxx>
00051 
00052 //=======================================================================
00053 //function : GetID
00054 //purpose  :
00055 //=======================================================================
00056 const Standard_GUID& GEOMImpl_SplineDriver::GetID()
00057 {
00058   static Standard_GUID aSplineDriver("FF1BBB33-5D14-4df2-980B-3A668264EA16");
00059   return aSplineDriver;
00060 }
00061 
00062 
00063 //=======================================================================
00064 //function : GEOMImpl_SplineDriver
00065 //purpose  :
00066 //=======================================================================
00067 GEOMImpl_SplineDriver::GEOMImpl_SplineDriver()
00068 {
00069 }
00070 
00071 //=======================================================================
00072 //function : Execute
00073 //purpose  :
00074 //=======================================================================
00075 Standard_Integer GEOMImpl_SplineDriver::Execute(TFunction_Logbook& log) const
00076 {
00077   if (Label().IsNull()) return 0;
00078   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00079 
00080   GEOMImpl_ISpline aCI (aFunction);
00081   Standard_Integer aType = aFunction->GetType();
00082 
00083   TopoDS_Shape aShape;
00084 
00085   if (aType == SPLINE_BEZIER || aType == SPLINE_INTERPOLATION) {
00086 
00087     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
00088     TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
00089     if(useCoords) {
00090       Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
00091       int anArrayLength = aCoordsArray->Length();
00092       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
00093        gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
00094        points.SetValue(j,aPnt);
00095        j++;
00096       } 
00097     }
00098 
00099     
00100     int ind, aLen = aCI.GetLength();
00101     if (aLen < 2) return 0;
00102     Standard_Boolean isSeveral = Standard_False;
00103     gp_Pnt aPrevP;
00104     int aRealLen = aLen;
00105     if (aType == SPLINE_BEZIER && aCI.GetIsClosed()) {
00106       TopoDS_Vertex aV1;
00107       if(useCoords) {
00108        aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
00109       } else {
00110        Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
00111        TopoDS_Shape aFirstPnt = aFPoint->GetValue();
00112        aV1 = TopoDS::Vertex(aFirstPnt);
00113       }
00114 
00115       TopoDS_Vertex aV2;
00116       if(useCoords) { 
00117        aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
00118       } else {
00119        Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
00120        TopoDS_Shape aLastPnt = aLPoint->GetValue();
00121        aV2 = TopoDS::Vertex(aLastPnt);
00122       }
00123       
00124       if (!aV1.IsNull() && !aV2.IsNull() && !aV1.IsSame(aV2)) {
00125         aRealLen++;
00126       }
00127     }
00128     
00129     TColgp_Array1OfPnt CurvePoints (1, aRealLen);
00130     for (ind = 1; ind <= aLen; ind++) {
00131       gp_Pnt aP;
00132       if( useCoords ) { 
00133        aP = points.Value(ind);
00134         if (!isSeveral && ind > 1) {
00135           if (aP.Distance(aPrevP) > Precision::Confusion()) {
00136             isSeveral = Standard_True;
00137           }
00138         }
00139         CurvePoints.SetValue(ind, aP);
00140         aPrevP = aP;
00141       } else {      
00142        Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
00143        TopoDS_Shape aShapePnt = aRefPoint->GetValue();
00144        if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
00145          aP = BRep_Tool::Pnt(TopoDS::Vertex(aShapePnt));
00146          if (!isSeveral && ind > 1) {
00147            if (aP.Distance(aPrevP) > Precision::Confusion()) {
00148              isSeveral = Standard_True;
00149            }
00150          }
00151          CurvePoints.SetValue(ind, aP);
00152          aPrevP = aP;
00153        }
00154       }
00155     }
00156     if (aType == SPLINE_BEZIER) {
00157       if (!isSeveral) {
00158         Standard_ConstructionError::Raise("Points for Bezier Curve are too close");
00159       }
00160       if (aRealLen > aLen) { // set last point equal to first for the closed curve
00161         CurvePoints.SetValue(aRealLen, CurvePoints.Value(1));
00162       }
00163       Handle(Geom_BezierCurve) GBC = new Geom_BezierCurve(CurvePoints);
00164       aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
00165     } else {
00166       //GeomAPI_PointsToBSpline GBC (CurvePoints);
00167       //aShape = BRepBuilderAPI_MakeEdge(GBC).Edge();
00168       
00169       Handle(TColgp_HArray1OfPnt) aHCurvePoints = new TColgp_HArray1OfPnt(1, aLen);
00170 
00171       if (aCI.GetDoReordering()) {
00172         for (int curInd = 1; curInd < aLen - 1; curInd++) {
00173           gp_Pnt curPnt = CurvePoints.Value(curInd);
00174           int nearInd = 0;
00175           double nearDist = RealLast();
00176           for (ind = curInd + 1; ind <= aLen; ind++) {
00177             double dist = curPnt.SquareDistance(CurvePoints.Value(ind));
00178             if (dist < nearDist && (nearDist - dist) > Precision::Confusion()) {
00179               nearInd = ind;
00180               nearDist = dist;
00181             }
00182           }
00183           if (nearInd > 0 && nearInd != curInd + 1) {
00184             // Keep given order of points to use it in case of equidistant candidates
00185             //               .-<---<-.
00186             //              /         \
00187             // o  o  o  c  o->o->o->o->n  o  o
00188             //          |  |           |
00189             //     curInd  curInd+1    nearInd
00190             gp_Pnt nearPnt = CurvePoints.Value(nearInd);
00191             for (ind = nearInd; ind > curInd + 1; ind--) {
00192               CurvePoints.SetValue(ind, CurvePoints.Value(ind - 1));
00193             }
00194             CurvePoints.SetValue(curInd + 1, nearPnt);
00195           }
00196         }
00197         for (ind = 1; ind <= aLen; ind++) {
00198           aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
00199         }
00200       }
00201       else {
00202         for (ind = 1; ind <= aLen; ind++) {
00203           aHCurvePoints->SetValue(ind, CurvePoints.Value(ind));
00204         }
00205       }
00206       
00207       bool isClosed = aCI.GetIsClosed();
00208       GeomAPI_Interpolate GBC (aHCurvePoints, isClosed, gp::Resolution());
00209       GBC.Perform();
00210       if (GBC.IsDone())
00211         aShape = BRepBuilderAPI_MakeEdge(GBC.Curve()).Edge();
00212       else
00213         return 0;
00214     }
00215   }
00216   else {
00217   }
00218   
00219   if (aShape.IsNull()) return 0;
00220   
00221   aFunction->SetValue(aShape);
00222   
00223   log.SetTouched(Label());
00224   
00225   return 1;
00226 }
00227 
00228 
00229 //=======================================================================
00230 //function :  GEOMImpl_SplineDriver_Type_
00231 //purpose  :
00232 //=======================================================================
00233 Standard_EXPORT Handle_Standard_Type& GEOMImpl_SplineDriver_Type_()
00234 {
00235 
00236   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00237   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00238   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00239   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00240   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00241   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00242 
00243 
00244   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00245   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_SplineDriver",
00246                                                          sizeof(GEOMImpl_SplineDriver),
00247                                                          1,
00248                                                          (Standard_Address)_Ancestors,
00249                                                          (Standard_Address)NULL);
00250 
00251   return _aType;
00252 }
00253 
00254 //=======================================================================
00255 //function : DownCast
00256 //purpose  :
00257 //=======================================================================
00258 const Handle(GEOMImpl_SplineDriver) Handle(GEOMImpl_SplineDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00259 {
00260   Handle(GEOMImpl_SplineDriver) _anOtherObject;
00261 
00262   if (!AnObject.IsNull()) {
00263      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_SplineDriver))) {
00264        _anOtherObject = Handle(GEOMImpl_SplineDriver)((Handle(GEOMImpl_SplineDriver)&)AnObject);
00265      }
00266   }
00267 
00268   return _anOtherObject ;
00269 }