X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FGeomAPI%2FGeomAPI_Circ2d.cpp;h=4ac7458640a9e61f1ef6681aa0c3eb6689615353;hb=49c180426b47ea37eee3a779362fef3b78ab7cb9;hp=78c26d69f12049724139dcf49f75b2f26cedbe5d;hpb=142e6a7451245b1c39e5d0afbabb5dc4f678520e;p=modules%2Fshaper.git diff --git a/src/GeomAPI/GeomAPI_Circ2d.cpp b/src/GeomAPI/GeomAPI_Circ2d.cpp index 78c26d69f..4ac745864 100644 --- a/src/GeomAPI/GeomAPI_Circ2d.cpp +++ b/src/GeomAPI/GeomAPI_Circ2d.cpp @@ -1,3 +1,5 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + // File: GeomAPI_Circ2d.cpp // Created: 29 May 2014 // Author: Artem ZHIDKOV @@ -10,23 +12,24 @@ #include #include #include +#include #include #include #include #include -#define MY_CIRC2D static_cast(myImpl) +#define MY_CIRC2D implPtr() -static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, - const gp_Dir2d theDir, const double theRadius) +static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, const gp_Dir2d theDir, + const double theRadius) { gp_Pnt2d aCenter(theCenterX, theCenterY); return new gp_Circ2d(gp_Ax2d(aCenter, theDir), theRadius); } static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, - const double thePointX, const double thePointY) + const double thePointX, const double thePointY) { gp_Pnt2d aCenter(theCenterX, theCenterY); gp_Pnt2d aPoint(thePointX, thePointY); @@ -34,65 +37,135 @@ static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, double aRadius = aCenter.Distance(aPoint); if (aCenter.IsEqual(aPoint, Precision::Confusion())) - return NULL; + return NULL; gp_Dir2d aDir(theCenterX - thePointX, theCenterY - thePointY); - + return newCirc2d(theCenterX, theCenterY, aDir, aRadius); } -GeomAPI_Circ2d::GeomAPI_Circ2d(const boost::shared_ptr& theCenter, - const boost::shared_ptr& theCirclePoint) - : GeomAPI_Interface(newCirc2d(theCenter->x(), theCenter->y(), - theCirclePoint->x(), theCirclePoint->y())) -{} - -GeomAPI_Circ2d::GeomAPI_Circ2d(const boost::shared_ptr& theCenter, - const boost::shared_ptr& theDir, - double theRadius) - : GeomAPI_Interface(newCirc2d(theCenter->x(), theCenter->y(), - theDir->impl(), theRadius)) +static gp_Circ2d* newCirc2d(const std::shared_ptr& theFirstPoint, + const std::shared_ptr& theSecondPoint, + const std::shared_ptr& theThirdPoint) { + gp_XY aFirstPnt(theFirstPoint->x(), theFirstPoint->y()); + gp_XY aSecondPnt(theSecondPoint->x(), theSecondPoint->y()); + gp_XY aThirdPnt(theThirdPoint->x(), theThirdPoint->y()); + + gp_XY aVec12 = aSecondPnt - aFirstPnt; + gp_XY aVec23 = aThirdPnt - aSecondPnt; + gp_XY aVec31 = aFirstPnt - aThirdPnt; + + // coefficients to calculate center + double aCoeff1, aCoeff2, aCoeff3; + + // square of parallelogram + double aSquare2 = aVec12.Crossed(aVec23); + aSquare2 *= aSquare2 * 2.0; + if (aSquare2 < 1.e-20) { + // if two points are equal, build a circle on two different points as on diameter + double aSqLen12 = aVec12.SquareModulus(); + double aSqLen23 = aVec23.SquareModulus(); + double aSqLen31 = aVec31.SquareModulus(); + if (aSqLen12 < Precision::SquareConfusion() && + aSqLen23 < Precision::SquareConfusion() && + aSqLen31 < Precision::SquareConfusion()) + return NULL; + aCoeff1 = aCoeff2 = aCoeff3 = 1.0 / 3.0; + } + else { + aCoeff1 = aVec23.Dot(aVec23) / aSquare2 * aVec12.Dot(aVec31.Reversed()); + aCoeff2 = aVec31.Dot(aVec31) / aSquare2 * aVec23.Dot(aVec12.Reversed()); + aCoeff3 = aVec12.Dot(aVec12) / aSquare2 * aVec31.Dot(aVec23.Reversed()); + } + // center + gp_XY aCenter = aFirstPnt * aCoeff1 + aSecondPnt * aCoeff2 + aThirdPnt * aCoeff3; + // radius + double aRadius = (aFirstPnt - aCenter).Modulus(); + gp_Dir2d aDir(aFirstPnt - aCenter); + return newCirc2d(aCenter.X(), aCenter.Y(), aDir, aRadius); } -const boost::shared_ptr GeomAPI_Circ2d::project(const boost::shared_ptr& thePoint) const +GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr& theCenter, + const std::shared_ptr& theCirclePoint) + : GeomAPI_Interface( + newCirc2d(theCenter->x(), theCenter->y(), theCirclePoint->x(), theCirclePoint->y())) { - boost::shared_ptr aResult; +} + +GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr& theCenter, + const std::shared_ptr& theDir, double theRadius) + : GeomAPI_Interface( + newCirc2d(theCenter->x(), theCenter->y(), theDir->impl(), theRadius)) +{ +} + +GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr& theFirstPoint, + const std::shared_ptr& theSecondPoint, + const std::shared_ptr& theThirdPoint) + : GeomAPI_Interface(newCirc2d(theFirstPoint, theSecondPoint, theThirdPoint)) +{ +} + +const std::shared_ptr GeomAPI_Circ2d::project( + const std::shared_ptr& thePoint) const +{ + std::shared_ptr aResult; if (!MY_CIRC2D) return aResult; - Handle(Geom2d_Circle) aCircle = new Geom2d_Circle(MY_CIRC2D->Axis(), MY_CIRC2D->Radius());//(aCirc); - + const gp_Pnt2d& aCenter = MY_CIRC2D->Location(); const gp_Pnt2d& aPoint = thePoint->impl(); - Geom2dAPI_ProjectPointOnCurve aProj(aPoint, aCircle); - Standard_Integer aNbPoint = aProj.NbPoints(); - double aX, anY; - if (aNbPoint > 0) { - double aMinDistance = 0, aDistance; - for (Standard_Integer j = 1; j <= aNbPoint; j++) { - gp_Pnt2d aNewPoint = aProj.Point(j); - aDistance = aNewPoint.Distance(aPoint); - if (!aMinDistance || aDistance < aMinDistance) { - aX = aNewPoint.X(); - anY = aNewPoint.Y(); - aMinDistance = aDistance; - aResult = boost::shared_ptr(new GeomAPI_Pnt2d(aX, anY)); - } - } + double aDist = aCenter.Distance(aPoint); + if (aDist < Precision::Confusion()) + return aResult; + + if (Abs(aDist - MY_CIRC2D->Radius()) < Precision::Confusion()) { + // Point on the circle + aResult = std::shared_ptr( + new GeomAPI_Pnt2d(thePoint->x(), thePoint->y())); + } else { + gp_Dir2d aDir(aPoint.XY() - aCenter.XY()); + gp_XY aNewPoint = aCenter.XY() + aDir.XY() * MY_CIRC2D->Radius(); + aResult = std::shared_ptr( + new GeomAPI_Pnt2d(aNewPoint.X(), aNewPoint.Y())); } + return aResult; } -const boost::shared_ptr GeomAPI_Circ2d::center() const +const std::shared_ptr GeomAPI_Circ2d::center() const { + if (!MY_CIRC2D) + return std::shared_ptr(); const gp_Pnt2d& aCenter = MY_CIRC2D->Location(); - return boost::shared_ptr(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y())); + return std::shared_ptr(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y())); } double GeomAPI_Circ2d::radius() const { + if (!MY_CIRC2D) + return 0.0; return MY_CIRC2D->Radius(); } +//================================================================================================= +const bool GeomAPI_Circ2d::parameter(const std::shared_ptr thePoint, + const double theTolerance, + double& theParameter) const +{ + Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D); + return GeomLib_Tool::Parameter(aCurve, thePoint->impl(), theTolerance, theParameter) == Standard_True; +} + +//================================================================================================= +void GeomAPI_Circ2d::D0(const double theU, std::shared_ptr& thePoint) +{ + Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D); + gp_Pnt2d aPnt; + aCurve->D0(theU, aPnt); + thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y())); +} +