X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FPartSet%2FPartSet_Tools.cpp;h=2c7a93b069bab6d65eaaab70c0fd1612d4444d42;hb=f336bd42fdbf099f8fed4dc5288f4a2cef3a3dda;hp=04a41572a457de064d2738ef8a746171bcd1f0c9;hpb=507f658f34fc6c42f48fb83a8fa916c6f0e33fd3;p=modules%2Fshaper.git diff --git a/src/PartSet/PartSet_Tools.cpp b/src/PartSet/PartSet_Tools.cpp index 04a41572a..2c7a93b06 100644 --- a/src/PartSet/PartSet_Tools.cpp +++ b/src/PartSet/PartSet_Tools.cpp @@ -4,21 +4,34 @@ #include -#include -#include -#include -#include - #include #include + #include #include +#include + +#include +#include + #include +#include + +#include + +#include +#include +#include +#include +#include +#include #ifdef _DEBUG #include #endif +const double PRECISION_TOLERANCE = 0.000001; + gp_Pnt PartSet_Tools::ConvertClickToPoint(QPoint thePoint, Handle(V3d_View) theView) { if (theView.IsNull()) @@ -80,8 +93,8 @@ void PartSet_Tools::ConvertTo2D(const gp_Pnt& thePoint, boost::shared_ptr(aData->attribute(SKETCH_ATTR_NORM)); gp_Vec aNormalVec(aNormal->x(), aNormal->y(), aNormal->z()); - double aDen = anEyeVec*aNormalVec; - double aLVec = aDen != 0 ? aVec*aNormalVec/aDen : aVec*aNormalVec; + double aDen = anEyeVec * aNormalVec; + double aLVec = aDen != 0 ? aVec * aNormalVec / aDen : DBL_MAX; gp_Vec aDeltaVec = anEyeVec*aLVec; aVec = aVec - aDeltaVec; @@ -89,3 +102,94 @@ void PartSet_Tools::ConvertTo2D(const gp_Pnt& thePoint, boost::shared_ptrx() + aVec.Y() * aX->y() + aVec.Z() * aX->z(); theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z(); } + +void PartSet_Tools::IntersectLines(double theX0, double theY0, double theX1, double theY1, + double theX2, double theY2, double theX3, double theY3, + double& theX, double& theY) +{ + double aV1 = theX1 - theX0, aV2 = theY1 - theY0; + double aW1 = theX3 - theX2, aW2 = theY3 - theY2; + + double aT2 = 0; + if (aV1 != 0 && aV2 != 0) + aT2 = (( theY2 - theY0 )/aV2 - ( theX2 - theX0 )/aV1) / ( aW1/aV1 - aW2/aV2 ); + else + aT2 = DBL_MAX; + + theX = theX2 + aT2*aW1; + theY = theY2 + aT2*aW2; + + // the coordinates of two lines are on the common line + //It is not possible to use Precision::Confusion(), because it is e-0.8, but V is sometimes e-6 + Standard_Real aPrec = PRECISION_TOLERANCE; + if (fabs(theX - theX0) < aPrec && fabs(theY - theY0) < aPrec) { + ProjectPointOnLine(theX2, theY2, theX3, theY3, theX1, theY1, theX, theY); + } +} + +void PartSet_Tools::ProjectPointOnLine(double theX1, double theY1, double theX2, double theY2, + double thePointX, double thePointY, double& theX, double& theY) +{ + theX = theY = 0; + + Handle(Geom_Line) aLine = new Geom_Line(gp_Pnt(theX1, theY1, 0), + gp_Dir(gp_Vec(gp_Pnt(theX1, theY1, 0), gp_Pnt(theX2, theY2, 0)))); + GeomAPI_ProjectPointOnCurve aProj(gp_Pnt(thePointX, thePointY, 0), aLine); + + Standard_Integer aNbPoint = aProj.NbPoints(); + if (aNbPoint > 0) { + gp_Pnt aPoint = aProj.Point(1); + theX = aPoint.X(); + theY = aPoint.Y(); + } +} + +boost::shared_ptr PartSet_Tools::NearestFeature(QPoint thePoint, + Handle_V3d_View theView, + boost::shared_ptr theSketch, + const std::list& theFeatures) +{ + double aX, anY; + gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(thePoint, theView); + PartSet_Tools::ConvertTo2D(aPoint, theSketch, theView, aX, anY); + + boost::shared_ptr aFeature; + std::list::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end(); + + boost::shared_ptr aDeltaFeature; + double aMinDelta = -1; + XGUI_ViewerPrs aPrs; + for (; anIt != aLast; anIt++) { + aPrs = *anIt; + if (!aPrs.feature()) + continue; + double aDelta = DistanceToPoint(aPrs.feature(), aX, anY); + if (aMinDelta < 0 || aMinDelta > aDelta) { + aMinDelta = aDelta; + aDeltaFeature = aPrs.feature(); + } + } + return aDeltaFeature; +} + +double PartSet_Tools::DistanceToPoint(boost::shared_ptr theFeature, + double theX, double theY) +{ + double aDelta = 0; + if (theFeature->getKind() != "SketchLine") + return aDelta; + + boost::shared_ptr aData = theFeature->data(); + + boost::shared_ptr aPoint1 = + boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_START)); + boost::shared_ptr aPoint2 = + boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_END)); + + double aX, anY; + PartSet_Tools::ProjectPointOnLine(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y(), theX, theY, aX, anY); + + aDelta = gp_Pnt(theX, theY, 0).Distance(gp_Pnt(aX, anY, 0)); + + return aDelta; +}