Salome HOME
Fix compilation errors (part 2)
[modules/shaper.git] / src / GeomAPI / GeomAPI_Circ2d.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Circ2d.cpp
4 // Created:     29 May 2014
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAPI_Circ2d.h>
8 #include <GeomAPI_Pnt2d.h>
9 #include <GeomAPI_Dir2d.h>
10
11 #include <gp_Dir2d.hxx>
12 #include <gp_Circ2d.hxx>
13 #include <gp_Pnt2d.hxx>
14 #include <gp_Ax2d.hxx>
15 #include <GeomLib_Tool.hxx>
16 #include <Geom2d_Circle.hxx>
17 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
18 #include <Precision.hxx>
19
20 #include <IntAna2d_AnaIntersection.hxx>
21
22 #define MY_CIRC2D implPtr<gp_Circ2d>()
23
24 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY, const gp_Dir2d theDir,
25                             const double theRadius)
26 {
27   gp_Pnt2d aCenter(theCenterX, theCenterY);
28   return new gp_Circ2d(gp_Ax2d(aCenter, theDir), theRadius);
29 }
30
31 static gp_Circ2d* newCirc2d(const double theCenterX, const double theCenterY,
32                             const double thePointX, const double thePointY)
33 {
34   gp_Pnt2d aCenter(theCenterX, theCenterY);
35   gp_Pnt2d aPoint(thePointX, thePointY);
36
37   double aRadius = aCenter.Distance(aPoint);
38
39   if (aCenter.IsEqual(aPoint, Precision::Confusion()))
40     return NULL;
41
42   gp_Dir2d aDir(thePointX - theCenterX, thePointY - theCenterY);
43
44   return newCirc2d(theCenterX, theCenterY, aDir, aRadius);
45 }
46
47 static gp_Circ2d* newCirc2d(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
48                             const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint,
49                             const std::shared_ptr<GeomAPI_Pnt2d>& theThirdPoint)
50 {
51   gp_XY aFirstPnt(theFirstPoint->x(), theFirstPoint->y());
52   gp_XY aSecondPnt(theSecondPoint->x(), theSecondPoint->y());
53   gp_XY aThirdPnt(theThirdPoint->x(), theThirdPoint->y());
54
55   gp_XY aVec12 = aSecondPnt - aFirstPnt;
56   gp_XY aVec23 = aThirdPnt - aSecondPnt;
57   gp_XY aVec31 = aFirstPnt - aThirdPnt;
58
59   // coefficients to calculate center
60   double aCoeff1, aCoeff2, aCoeff3;
61
62   // square of parallelogram
63   double aSquare2 = aVec12.Crossed(aVec23);
64   aSquare2 *= aSquare2 * 2.0;
65   if (aSquare2 < 1.e-20) {
66     // if two points are equal, build a circle on two different points as on diameter
67     double aSqLen12 = aVec12.SquareModulus();
68     double aSqLen23 = aVec23.SquareModulus();
69     double aSqLen31 = aVec31.SquareModulus();
70     if (aSqLen12 < Precision::SquareConfusion() &&
71         aSqLen23 < Precision::SquareConfusion() &&
72         aSqLen31 < Precision::SquareConfusion())
73       return NULL;
74     aCoeff1 = aCoeff2 = aCoeff3 = 1.0 / 3.0;
75   }
76   else {
77     aCoeff1 = aVec23.Dot(aVec23) / aSquare2 * aVec12.Dot(aVec31.Reversed());
78     aCoeff2 = aVec31.Dot(aVec31) / aSquare2 * aVec23.Dot(aVec12.Reversed());
79     aCoeff3 = aVec12.Dot(aVec12) / aSquare2 * aVec31.Dot(aVec23.Reversed());
80   }
81   // center
82   gp_XY aCenter = aFirstPnt * aCoeff1 + aSecondPnt * aCoeff2 + aThirdPnt * aCoeff3;
83   // radius
84   double aRadius = (aFirstPnt - aCenter).Modulus();
85
86   gp_Dir2d aDir(aFirstPnt - aCenter);
87   return newCirc2d(aCenter.X(), aCenter.Y(), aDir, aRadius);
88 }
89
90 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
91                                const std::shared_ptr<GeomAPI_Pnt2d>& theCirclePoint)
92     : GeomAPI_Interface(
93         newCirc2d(theCenter->x(), theCenter->y(), theCirclePoint->x(), theCirclePoint->y()))
94 {
95 }
96
97 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theCenter,
98                                const std::shared_ptr<GeomAPI_Dir2d>& theDir, double theRadius)
99     : GeomAPI_Interface(
100         newCirc2d(theCenter->x(), theCenter->y(), theDir->impl<gp_Dir2d>(), theRadius))
101 {
102 }
103
104 GeomAPI_Circ2d::GeomAPI_Circ2d(const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
105                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint,
106                                const std::shared_ptr<GeomAPI_Pnt2d>& theThirdPoint)
107     : GeomAPI_Interface(newCirc2d(theFirstPoint, theSecondPoint, theThirdPoint))
108 {
109 }
110
111 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::project(
112     const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) const
113 {
114   std::shared_ptr<GeomAPI_Pnt2d> aResult;
115   if (!MY_CIRC2D)
116     return aResult;
117
118   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
119   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
120
121   double aDist = aCenter.Distance(aPoint);
122   if (aDist < Precision::Confusion())
123     return aResult;
124
125   if (Abs(aDist - MY_CIRC2D->Radius()) < Precision::Confusion()) {
126     // Point on the circle
127     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
128         new GeomAPI_Pnt2d(thePoint->x(), thePoint->y()));
129   } else {
130     gp_Dir2d aDir(aPoint.XY() - aCenter.XY());
131     gp_XY aNewPoint = aCenter.XY() + aDir.XY() * MY_CIRC2D->Radius();
132     aResult = std::shared_ptr<GeomAPI_Pnt2d>(
133         new GeomAPI_Pnt2d(aNewPoint.X(), aNewPoint.Y()));
134   }
135
136   return aResult;
137 }
138
139 const std::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Circ2d::center() const
140 {
141   if (!MY_CIRC2D)
142     return std::shared_ptr<GeomAPI_Pnt2d>();
143   const gp_Pnt2d& aCenter = MY_CIRC2D->Location();
144   return std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aCenter.X(), aCenter.Y()));
145 }
146
147 double GeomAPI_Circ2d::radius() const
148 {
149   if (!MY_CIRC2D)
150     return 0.0;
151   return MY_CIRC2D->Radius();
152 }
153
154 //=================================================================================================
155 const bool GeomAPI_Circ2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> thePoint,
156                                    const double theTolerance,
157                                    double& theParameter) const
158 {
159   Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D);
160   return GeomLib_Tool::Parameter(aCurve, thePoint->impl<gp_Pnt2d>(),
161                                  theTolerance, theParameter) == Standard_True;
162 }
163
164 //=================================================================================================
165 void GeomAPI_Circ2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
166 {
167   Handle(Geom2d_Circle) aCurve = new Geom2d_Circle(*MY_CIRC2D);
168   gp_Pnt2d aPnt;
169   aCurve->D0(theU, aPnt);
170   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
171 }
172