0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / Geom2dAPI / Geom2dAPI_ProjectPointOnCurve.cxx
CommitLineData
b311480e 1// Created on: 1994-03-23
2// Created by: Bruno DUMORTIER
3// Copyright (c) 1994-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <Extrema_ExtPC2d.hxx>
19#include <Geom2d_Curve.hxx>
7fd59977 20#include <Geom2dAdaptor_Curve.hxx>
42cf5bc1 21#include <Geom2dAPI_ProjectPointOnCurve.hxx>
22#include <gp_Pnt2d.hxx>
23#include <Standard_OutOfRange.hxx>
24#include <StdFail_NotDone.hxx>
7fd59977 25
26//=======================================================================
27//function : Geom2dAPI_ProjectPointOnCurve
28//purpose :
29//=======================================================================
7fd59977 30Geom2dAPI_ProjectPointOnCurve::Geom2dAPI_ProjectPointOnCurve()
31{
32 myIsDone = Standard_False;
33}
34
35
36//=======================================================================
37//function : Geom2dAPI_ProjectPointOnCurve
38//purpose :
39//=======================================================================
40
41Geom2dAPI_ProjectPointOnCurve::Geom2dAPI_ProjectPointOnCurve
42 (const gp_Pnt2d& P,
43 const Handle(Geom2d_Curve)& Curve)
44{
45 Init(P,Curve);
46}
47
48
49//=======================================================================
50//function : Geom2dAPI_ProjectPointOnCurve
51//purpose :
52//=======================================================================
53
54Geom2dAPI_ProjectPointOnCurve::Geom2dAPI_ProjectPointOnCurve
55 (const gp_Pnt2d& P,
56 const Handle(Geom2d_Curve)& Curve,
57 const Standard_Real Umin,
58 const Standard_Real Usup)
59{
60 Init(P,Curve,Umin,Usup);
61}
62
63
64//=======================================================================
65//function : Init
66//purpose :
67//=======================================================================
68
69void Geom2dAPI_ProjectPointOnCurve::Init
70 (const gp_Pnt2d& P,
71 const Handle(Geom2d_Curve)& Curve)
72{
73 Init(P,Curve,Curve->FirstParameter(),Curve->LastParameter());
74}
75
76
77//=======================================================================
78//function : Init
79//purpose :
80//=======================================================================
81
82void Geom2dAPI_ProjectPointOnCurve::Init
83 (const gp_Pnt2d& P,
84 const Handle(Geom2d_Curve)& Curve,
85 const Standard_Real Umin,
86 const Standard_Real Usup )
87{
88 myC.Load(Curve,Umin,Usup);
89
90 Extrema_ExtPC2d theExtPC2d(P, myC);
91
92 myExtPC = theExtPC2d;
93
94 myIsDone = myExtPC.IsDone() && ( myExtPC.NbExt() > 0);
95
96
97 // evaluate the lower distance and its index;
98
99 if ( myIsDone) {
100 Standard_Real Dist2, Dist2Min = myExtPC.SquareDistance(1);
101 myIndex = 1;
102
103 for ( Standard_Integer i = 2; i <= myExtPC.NbExt(); i++) {
104 Dist2 = myExtPC.SquareDistance(i);
105 if ( Dist2 < Dist2Min) {
106 Dist2Min = Dist2;
107 myIndex = i;
108 }
109 }
110 }
111}
112
113
114//=======================================================================
115//function : NbPoints
116//purpose :
117//=======================================================================
118
119Standard_Integer Geom2dAPI_ProjectPointOnCurve::NbPoints() const
120{
121 if ( myIsDone)
122 return myExtPC.NbExt();
123 else
124 return 0;
125}
126
127
128//=======================================================================
129//function : Point
130//purpose :
131//=======================================================================
132
133gp_Pnt2d Geom2dAPI_ProjectPointOnCurve::Point
134 (const Standard_Integer Index) const
135{
136 Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
137 "Geom2dAPI_ProjectPointOnCurve::Point");
138 return (myExtPC.Point(Index)).Value();
139}
140
141
142//=======================================================================
143//function : Parameter
144//purpose :
145//=======================================================================
146
147Standard_Real Geom2dAPI_ProjectPointOnCurve::Parameter
148 (const Standard_Integer Index) const
149{
150 Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
151 "Geom2dAPI_ProjectPointOnCurve::Parameter");
152 return (myExtPC.Point(Index)).Parameter();
153}
154
155
156//=======================================================================
157//function : Parameter
158//purpose :
159//=======================================================================
160
161void Geom2dAPI_ProjectPointOnCurve::Parameter
162 (const Standard_Integer Index,
163 Standard_Real& U ) const
164{
165 Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
166 "Geom2dAPI_ProjectPointOnCurve::Parameter");
167 U = (myExtPC.Point(Index)).Parameter();
168}
169
170
171//=======================================================================
172//function : Distance
173//purpose :
174//=======================================================================
175
176Standard_Real Geom2dAPI_ProjectPointOnCurve::Distance
177 (const Standard_Integer Index) const
178{
179 Standard_OutOfRange_Raise_if( Index < 1 || Index > NbPoints(),
180 "Geom2dAPI_ProjectPointOnCurve::Distance");
181 return sqrt(myExtPC.SquareDistance(Index));
182}
183
184
185//=======================================================================
186//function : NearestPoint
187//purpose :
188//=======================================================================
189
190gp_Pnt2d Geom2dAPI_ProjectPointOnCurve::NearestPoint() const
191{
192 StdFail_NotDone_Raise_if
193 (!myIsDone, "Geom2dAPI_ProjectPointOnCurve:NearestPoint");
194
195 return (myExtPC.Point(myIndex)).Value();
196}
197
198
199//=======================================================================
200//function : Standard_Integer
201//purpose :
202//=======================================================================
203
204Geom2dAPI_ProjectPointOnCurve::operator Standard_Integer() const
205{
206 return NbPoints();
207}
208
209
210//=======================================================================
211//function : gp_Pnt2d
212//purpose :
213//=======================================================================
214
215Geom2dAPI_ProjectPointOnCurve::operator gp_Pnt2d() const
216{
217 return NearestPoint();
218}
219
220
221//=======================================================================
222//function : LowerDistanceParameter
223//purpose :
224//=======================================================================
225
226Standard_Real Geom2dAPI_ProjectPointOnCurve::LowerDistanceParameter() const
227{
228 StdFail_NotDone_Raise_if
229 (!myIsDone, "Geom2dAPI_ProjectPointOnCurve:LowerDistanceParameter");
230
231 return (myExtPC.Point(myIndex)).Parameter();
232}
233
234
235//=======================================================================
236//function : LowerDistance
237//purpose :
238//=======================================================================
239
240Standard_Real Geom2dAPI_ProjectPointOnCurve::LowerDistance() const
241{
242 StdFail_NotDone_Raise_if
243 (!myIsDone,"Geom2dAPI_ProjectPointOnCurve:LowerDistance");
244
245 return sqrt (myExtPC.SquareDistance(myIndex));
246}
247
248
249//=======================================================================
250//function : operator
251//purpose :
252//=======================================================================
253
254Geom2dAPI_ProjectPointOnCurve::operator Standard_Real() const
255{
256 return LowerDistance();
257}