Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Geom2dAPI / Geom2dAPI_ProjectPointOnCurve.cxx
1 // File:        Geom2dAPI_ProjectPointOnCurve.cxx
2 // Created:     Wed Mar 23 15:16:37 1994
3 // Author:      Bruno DUMORTIER
4 //              <dub@fuegox>
5
6 #include <Geom2dAPI_ProjectPointOnCurve.ixx>
7
8 #include <Geom2dAdaptor_Curve.hxx>
9
10
11 //=======================================================================
12 //function : Geom2dAPI_ProjectPointOnCurve
13 //purpose  : 
14 //=======================================================================
15
16 Geom2dAPI_ProjectPointOnCurve::Geom2dAPI_ProjectPointOnCurve()
17 {
18   myIsDone = Standard_False;
19 }
20
21
22 //=======================================================================
23 //function : Geom2dAPI_ProjectPointOnCurve
24 //purpose  : 
25 //=======================================================================
26
27 Geom2dAPI_ProjectPointOnCurve::Geom2dAPI_ProjectPointOnCurve
28   (const gp_Pnt2d&             P, 
29    const Handle(Geom2d_Curve)& Curve)
30 {
31   Init(P,Curve);
32 }
33
34
35 //=======================================================================
36 //function : Geom2dAPI_ProjectPointOnCurve
37 //purpose  : 
38 //=======================================================================
39
40 Geom2dAPI_ProjectPointOnCurve::Geom2dAPI_ProjectPointOnCurve
41   (const gp_Pnt2d&             P, 
42    const Handle(Geom2d_Curve)& Curve,
43    const Standard_Real         Umin,
44    const Standard_Real         Usup)
45 {
46   Init(P,Curve,Umin,Usup);
47 }
48
49
50 //=======================================================================
51 //function : Init
52 //purpose  : 
53 //=======================================================================
54
55 void Geom2dAPI_ProjectPointOnCurve::Init
56   (const gp_Pnt2d&             P,
57    const Handle(Geom2d_Curve)& Curve)
58 {
59   Init(P,Curve,Curve->FirstParameter(),Curve->LastParameter());
60 }
61
62
63 //=======================================================================
64 //function : Init
65 //purpose  : 
66 //=======================================================================
67
68 void Geom2dAPI_ProjectPointOnCurve::Init
69   (const gp_Pnt2d&             P,
70    const Handle(Geom2d_Curve)& Curve,
71    const Standard_Real         Umin,
72    const Standard_Real         Usup )
73 {
74   myC.Load(Curve,Umin,Usup);
75
76   Extrema_ExtPC2d theExtPC2d(P, myC);
77
78   myExtPC = theExtPC2d;
79   
80   myIsDone = myExtPC.IsDone() && ( myExtPC.NbExt() > 0);
81
82
83   // evaluate the lower distance and its index;
84
85   if ( myIsDone) {
86     Standard_Real Dist2, Dist2Min = myExtPC.SquareDistance(1);
87     myIndex = 1;
88     
89     for ( Standard_Integer i = 2; i <= myExtPC.NbExt(); i++) {
90       Dist2 = myExtPC.SquareDistance(i);
91       if ( Dist2 < Dist2Min) {
92         Dist2Min = Dist2;
93         myIndex = i;
94       }
95     }
96   }
97 }
98
99
100 //=======================================================================
101 //function : NbPoints
102 //purpose  : 
103 //=======================================================================
104
105 Standard_Integer Geom2dAPI_ProjectPointOnCurve::NbPoints() const 
106 {
107   if ( myIsDone)
108     return myExtPC.NbExt();
109   else
110     return 0;
111 }
112
113
114 //=======================================================================
115 //function : Point
116 //purpose  : 
117 //=======================================================================
118
119 gp_Pnt2d Geom2dAPI_ProjectPointOnCurve::Point
120   (const Standard_Integer Index) const 
121 {
122   Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
123                                "Geom2dAPI_ProjectPointOnCurve::Point");
124   return (myExtPC.Point(Index)).Value();
125 }
126
127
128 //=======================================================================
129 //function : Parameter
130 //purpose  : 
131 //=======================================================================
132
133 Standard_Real Geom2dAPI_ProjectPointOnCurve::Parameter
134   (const Standard_Integer Index) const
135 {
136   Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
137                                "Geom2dAPI_ProjectPointOnCurve::Parameter");
138   return (myExtPC.Point(Index)).Parameter();
139 }
140
141
142 //=======================================================================
143 //function : Parameter
144 //purpose  : 
145 //=======================================================================
146
147 void Geom2dAPI_ProjectPointOnCurve::Parameter
148   (const Standard_Integer Index,
149          Standard_Real&   U     ) const
150 {
151   Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
152                                "Geom2dAPI_ProjectPointOnCurve::Parameter");
153   U = (myExtPC.Point(Index)).Parameter();
154 }
155
156
157 //=======================================================================
158 //function : Distance
159 //purpose  : 
160 //=======================================================================
161
162 Standard_Real Geom2dAPI_ProjectPointOnCurve::Distance
163   (const Standard_Integer Index) const
164 {
165   Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
166                                "Geom2dAPI_ProjectPointOnCurve::Distance");
167   return sqrt(myExtPC.SquareDistance(Index));
168 }
169
170
171 //=======================================================================
172 //function : NearestPoint
173 //purpose  : 
174 //=======================================================================
175
176 gp_Pnt2d Geom2dAPI_ProjectPointOnCurve::NearestPoint() const 
177 {
178   StdFail_NotDone_Raise_if
179     (!myIsDone, "Geom2dAPI_ProjectPointOnCurve:NearestPoint");
180
181   return (myExtPC.Point(myIndex)).Value();
182 }
183
184
185 //=======================================================================
186 //function : Standard_Integer
187 //purpose  : 
188 //=======================================================================
189
190 Geom2dAPI_ProjectPointOnCurve::operator Standard_Integer() const
191 {
192   return NbPoints();
193 }
194
195
196 //=======================================================================
197 //function : gp_Pnt2d
198 //purpose  : 
199 //=======================================================================
200
201 Geom2dAPI_ProjectPointOnCurve::operator gp_Pnt2d() const
202 {
203   return NearestPoint();
204 }
205
206
207 //=======================================================================
208 //function : LowerDistanceParameter
209 //purpose  : 
210 //=======================================================================
211
212 Standard_Real Geom2dAPI_ProjectPointOnCurve::LowerDistanceParameter() const
213 {
214   StdFail_NotDone_Raise_if
215     (!myIsDone, "Geom2dAPI_ProjectPointOnCurve:LowerDistanceParameter");
216
217   return (myExtPC.Point(myIndex)).Parameter();
218 }
219
220
221 //=======================================================================
222 //function : LowerDistance
223 //purpose  : 
224 //=======================================================================
225
226 Standard_Real Geom2dAPI_ProjectPointOnCurve::LowerDistance() const
227 {
228   StdFail_NotDone_Raise_if
229     (!myIsDone,"Geom2dAPI_ProjectPointOnCurve:LowerDistance");
230
231   return sqrt (myExtPC.SquareDistance(myIndex));
232 }
233
234
235 //=======================================================================
236 //function : operator
237 //purpose  : 
238 //=======================================================================
239
240 Geom2dAPI_ProjectPointOnCurve::operator Standard_Real() const
241 {
242   return LowerDistance();
243 }