Back to index

salome-geom  6.5.0
GEOMImpl_PointDriver.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_PointDriver.hxx>
00026 #include <GEOMImpl_IPoint.hxx>
00027 #include <GEOMImpl_Types.hxx>
00028 #include <GEOM_Function.hxx>
00029 
00030 #include <ShapeAnalysis.hxx>
00031 
00032 #include <BRep_Builder.hxx>
00033 #include <BRep_Tool.hxx>
00034 #include <BRepAdaptor_Curve.hxx>
00035 #include <BRepBuilderAPI_MakeVertex.hxx>
00036 #include <BRepExtrema_DistShapeShape.hxx>
00037 
00038 #include <TopAbs.hxx>
00039 #include <TopExp.hxx>
00040 #include <TopoDS.hxx>
00041 #include <TopoDS_Edge.hxx>
00042 #include <TopoDS_Face.hxx>
00043 #include <TopoDS_Shape.hxx>
00044 #include <TopoDS_Vertex.hxx>
00045 #include <TopoDS_Compound.hxx>
00046 
00047 #include <GCPnts_AbscissaPoint.hxx>
00048 #include <IntTools.hxx>
00049 
00050 #include <Geom_Curve.hxx>
00051 #include <Geom_Surface.hxx>
00052 
00053 #include <gp_Pnt.hxx>
00054 
00055 #include <Precision.hxx>
00056 
00057 #include <Standard_NullObject.hxx>
00058 
00059 //=======================================================================
00060 //function : GetID
00061 //purpose  :
00062 //=======================================================================
00063 const Standard_GUID& GEOMImpl_PointDriver::GetID()
00064 {
00065   static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
00066   return aPointDriver;
00067 }
00068 
00069 
00070 //=======================================================================
00071 //function : GEOMImpl_PointDriver
00072 //purpose  :
00073 //=======================================================================
00074 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
00075 {
00076 }
00077 
00078 //=======================================================================
00079 //function : getExtremaSolution
00080 //purpose  : local function
00081 //=======================================================================
00082 static Standard_Boolean getExtremaSolution
00083 (const gp_Pnt&       theInitPnt,
00084  const TopoDS_Shape& theRefShape,
00085  gp_Pnt& thePnt)
00086 {
00087   BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
00088   TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
00089   
00090   BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
00091   if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
00092     return Standard_False;
00093   thePnt = anExt.PointOnShape2(1);
00094   Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
00095   for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
00096   {
00097     gp_Pnt aPnt = anExt.PointOnShape2(j);
00098     Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
00099     if ( aDist2 > aMinDist2)
00100       continue;
00101     aMinDist2 = aDist2;
00102     thePnt = aPnt;
00103   }
00104   return Standard_True;
00105 }
00106 
00107 //=======================================================================
00108 //function : Execute
00109 //purpose  :
00110 //=======================================================================
00111 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
00112 {
00113   if (Label().IsNull())  return 0;
00114   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
00115 
00116   GEOMImpl_IPoint aPI (aFunction);
00117   Standard_Integer aType = aFunction->GetType();
00118 
00119   gp_Pnt aPnt;
00120   TopoDS_Compound aCompound;
00121   bool retCompound = false;
00122 
00123   if (aType == POINT_XYZ) {
00124     aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
00125   }
00126   else if (aType == POINT_XYZ_REF) {
00127     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
00128     TopoDS_Shape aRefShape = aRefPoint->GetValue();
00129     if (aRefShape.ShapeType() != TopAbs_VERTEX) {
00130       Standard_TypeMismatch::Raise
00131         ("Point creation aborted : referenced shape is not a vertex");
00132     }
00133     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
00134     aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
00135   }
00136   else if (aType == POINT_CURVE_PAR) {
00137     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
00138     TopoDS_Shape aRefShape = aRefCurve->GetValue();
00139     if (aRefShape.ShapeType() != TopAbs_EDGE) {
00140       Standard_TypeMismatch::Raise
00141         ("Point On Curve creation aborted : curve shape is not an edge");
00142     }
00143     Standard_Real aFP, aLP, aP;
00144     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
00145     aP = aFP + (aLP - aFP) * aPI.GetParameter();
00146     aPnt = aCurve->Value(aP);
00147   }
00148   else if (aType == POINT_CURVE_COORD) {
00149     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
00150     TopoDS_Shape aRefShape = aRefCurve->GetValue();
00151     if (aRefShape.ShapeType() != TopAbs_EDGE) {
00152       Standard_TypeMismatch::Raise
00153         ("Point On Curve creation aborted : curve shape is not an edge");
00154     }
00155     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
00156     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
00157       Standard_ConstructionError::Raise
00158         ("Point On Curve creation aborted : cannot project point");
00159     }
00160   }
00161   else if (aType == POINT_CURVE_LENGTH) {
00162     // RefCurve
00163     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
00164     if (aRefCurve.IsNull()) {
00165       Standard_NullObject::Raise
00166         ("Point On Curve creation aborted : curve object is null");
00167     }
00168     TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
00169     if (aRefShape1.ShapeType() != TopAbs_EDGE) {
00170       Standard_TypeMismatch::Raise
00171         ("Point On Curve creation aborted : curve shape is not an edge");
00172     }
00173     TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
00174     TopoDS_Vertex V1, V2;
00175     TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
00176 
00177     // RefPoint
00178     TopoDS_Vertex aRefVertex;
00179     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
00180     if (aRefPoint.IsNull()) {
00181       aRefVertex = V1;
00182     }
00183     else {
00184       TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
00185       if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
00186         Standard_TypeMismatch::Raise
00187           ("Point On Curve creation aborted : start point shape is not a vertex");
00188       }
00189       aRefVertex = TopoDS::Vertex(aRefShape2);
00190     }
00191     gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
00192 
00193     // Length
00194     Standard_Real aLength = aPI.GetLength();
00195     //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
00196     //if (aLength > theCurveLength) {
00197     //  Standard_ConstructionError::Raise
00198     //    ("Point On Curve creation aborted : given length is greater than edges length");
00199     //}
00200 
00201     // Check orientation
00202     Standard_Real UFirst, ULast;
00203     Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
00204     Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
00205 
00206     Standard_Real dU = ULast - UFirst;
00207     Standard_Real par1 = UFirst + 0.1 * dU;
00208     Standard_Real par2 = ULast  - 0.1 * dU;
00209 
00210     gp_Pnt P1 = EdgeCurve->Value(par1);
00211     gp_Pnt P2 = EdgeCurve->Value(par2);
00212 
00213     if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
00214       ReOrientedCurve = EdgeCurve->Reversed();
00215       UFirst = EdgeCurve->ReversedParameter(ULast);
00216     }
00217 
00218     // Get the point by length
00219     GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
00220     GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst); 
00221     Standard_Real aParam = anAbsPnt.Parameter();
00222     aPnt = AdapCurve.Value(aParam);
00223   }
00224   else if (aType == POINT_SURFACE_PAR) {
00225     Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
00226     TopoDS_Shape aRefShape = aRefCurve->GetValue();
00227     if (aRefShape.ShapeType() != TopAbs_FACE) {
00228       Standard_TypeMismatch::Raise
00229         ("Point On Surface creation aborted : surface shape is not a face");
00230     }
00231     TopoDS_Face F = TopoDS::Face(aRefShape);
00232     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
00233     Standard_Real U1,U2,V1,V2;
00234     //aSurf->Bounds(U1,U2,V1,V2);
00235     ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
00236     Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
00237     Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
00238     aPnt = aSurf->Value(U,V);
00239   }
00240   else if (aType == POINT_SURFACE_COORD) {
00241     Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
00242     TopoDS_Shape aRefShape = aRefCurve->GetValue();
00243     if (aRefShape.ShapeType() != TopAbs_FACE) {
00244       Standard_TypeMismatch::Raise
00245         ("Point On Surface creation aborted : surface shape is not a face");
00246     }
00247     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
00248     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
00249       Standard_ConstructionError::Raise
00250         ("Point On Surface creation aborted : cannot project point");
00251     }
00252   }
00253   else if (aType == POINT_LINES_INTERSECTION) {
00254     Handle(GEOM_Function) aRef1 = aPI.GetLine1();
00255     Handle(GEOM_Function) aRef2 = aPI.GetLine2();
00256 
00257     TopoDS_Shape aRefShape1 = aRef1->GetValue();
00258     TopoDS_Shape aRefShape2 = aRef2->GetValue();
00259 
00260     if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
00261       || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
00262       Standard_TypeMismatch::Raise
00263         ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
00264     }
00265 
00266     if (aRefShape1.IsSame(aRefShape2))
00267       Standard_ConstructionError::Raise("The lines to make intersection must be different");
00268 
00269     //Calculate Lines Intersection Point
00270     BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
00271     if (dst.IsDone()) {
00272       gp_Pnt P1, P2;
00273       BRep_Builder B;
00274       B.MakeCompound( aCompound );
00275       for (int i = 1; i <= dst.NbSolution(); i++) {
00276         P1 = dst.PointOnShape1(i);
00277         P2 = dst.PointOnShape2(i);
00278         Standard_Real Dist = P1.Distance(P2);
00279         if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
00280           BRepBuilderAPI_MakeVertex mkVertex (P1);
00281           B.Add(aCompound, mkVertex.Shape());
00282           retCompound = true;
00283         } else if ( Dist <= Precision::Confusion() ) {
00284           aPnt = P1;
00285         } else {
00286           Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
00287         }
00288       }
00289     }
00290   }
00291   else {
00292     return 0;
00293   }
00294 
00295   TopoDS_Shape aShape;
00296   if ( retCompound ) {
00297     aShape = aCompound;
00298   } else {
00299     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
00300     aShape = mkVertex.Shape();
00301   }
00302 
00303   //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
00304   aFunction->SetValue(aShape);
00305 
00306   log.SetTouched(Label());
00307 
00308   return 1;
00309 }
00310 
00311 
00312 //=======================================================================
00313 //function :  GEOMImpl_PointDriver_Type_
00314 //purpose  :
00315 //=======================================================================
00316 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_Type_()
00317 {
00318 
00319   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
00320   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
00321   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
00322   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
00323   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
00324   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
00325 
00326 
00327   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
00328   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PointDriver",
00329                                                          sizeof(GEOMImpl_PointDriver),
00330                                                          1,
00331                                                          (Standard_Address)_Ancestors,
00332                                                          (Standard_Address)NULL);
00333 
00334   return _aType;
00335 }
00336 
00337 //=======================================================================
00338 //function : DownCast
00339 //purpose  :
00340 //=======================================================================
00341 
00342 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
00343 {
00344   Handle(GEOMImpl_PointDriver) _anOtherObject;
00345 
00346   if (!AnObject.IsNull()) {
00347      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
00348        _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
00349      }
00350   }
00351 
00352   return _anOtherObject ;
00353 }