0022627: Change OCCT memory management defaults
[occt.git] / src / GeometryTest / GeometryTest_API2dCommands.cxx
CommitLineData
7fd59977 1// File: GeometryTest_API2dCommands.cxx
2// Created: Wed Jan 11 10:33:13 1995
3// Author: Remi LEQUETTE
4// <rle@bravox>
5
6
7#include <GeometryTest.hxx>
8#include <Geom2d_Curve.hxx>
9#include <Draw.hxx>
10#include <Draw_Interpretor.hxx>
11#include <DrawTrSurf.hxx>
12#include <Draw_Appli.hxx>
13#include <DrawTrSurf_Curve2d.hxx>
14#include <Geom2dAPI_ProjectPointOnCurve.hxx>
15#include <Geom2dAPI_ExtremaCurveCurve.hxx>
16#include <Geom2dAPI_PointsToBSpline.hxx>
17#include <Geom2dAPI_InterCurveCurve.hxx>
18#include <Geom2d_Line.hxx>
19#include <Geom2d_TrimmedCurve.hxx>
20#include <TColgp_Array1OfPnt2d.hxx>
21#include <gp_Pnt.hxx>
22#include <Draw_Marker2D.hxx>
23#include <Draw_Color.hxx>
24#include <Draw_MarkerShape.hxx>
25#include <TColStd_Array1OfReal.hxx>
26#include <GeomAbs_Shape.hxx>
27
28#include <stdio.h>
29#ifdef WNT
30Standard_IMPORT Draw_Viewer dout;
31#endif
32
33//=======================================================================
34//function : proj
35//purpose :
36//=======================================================================
37
38static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
39{
40 if ( n < 4) return 1;
41
42 gp_Pnt2d P(atof(a[2]),atof(a[3]));
43
44 char name[100];
45
46 Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[1]);
47
48 if (GC.IsNull())
49 return 1;
50
51 Geom2dAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
52 GC->LastParameter());
53
54 for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) {
55 gp_Pnt2d P1 = proj.Point(i);
56 Handle(Geom2d_Line) L = new Geom2d_Line(P,gp_Vec2d(P,P1));
57 Handle(Geom2d_TrimmedCurve) CT =
58 new Geom2d_TrimmedCurve(L, 0., P.Distance(P1));
59 sprintf(name,"%s%d","ext_",i);
60 char* temp = name; // portage WNT
61 DrawTrSurf::Set(temp, CT);
62 di << name << " ";
63 }
64
65 return 0;
66}
67
68//=======================================================================
69//function : appro
70//purpose :
71//=======================================================================
72
73static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
74{
75 // Approximation et interpolation 2d
76
77 // 2dappro
78 // - affiche la tolerance
79 // 2dappro tol
80 // - change la tolerance
81 // 2dappro result nbpoint
82 // - saisie interactive
83 // 2dappro result nbpoint curve
84 // - calcule des points sur la courbe
85 // 2dappro result nbpoint x1 y1 x2 y2 ..
86 // - tableau de points
87 // 2dappro result nbpoint x1 dx y1 y2 ..
88 // - tableau de points (x1,y1) (x1+dx,y2) ... avec x = t
89
90
91 static Standard_Real Tol2d = 1.e-6;
92
93 if (n < 3) {
94 if (n == 2)
95 Tol2d = atof(a[1]);
96
97 di << "Tolerance for 2d approx : "<< Tol2d << "\n";
98 return 0;
99 }
100
101
102 Standard_Integer i, Nb = atoi(a[2]);
103
104 Standard_Boolean hasPoints = Standard_True;
105 TColgp_Array1OfPnt2d Points(1, Nb);
106 TColStd_Array1OfReal YValues(1,Nb);
107 Standard_Real X0=0,DX=0;
108
109 Handle(Draw_Marker2D) mark;
110
111 if (n == 3) {
112 // saisie interactive
113 Standard_Integer id,XX,YY,b;
114 dout.Select(id,XX,YY,b);
115 Standard_Real zoom = dout.Zoom(id);
116
117 Points(1) = gp_Pnt2d( ((Standard_Real)XX)/zoom,
118 ((Standard_Real)YY)/zoom );
119
120 mark = new Draw_Marker2D( Points(1), Draw_X, Draw_vert);
121
122 dout << mark;
123
124 for (Standard_Integer i = 2; i<=Nb; i++) {
125 dout.Select(id,XX,YY,b);
126 Points(i) = gp_Pnt2d( ((Standard_Real)XX)/zoom,
127 ((Standard_Real)YY)/zoom );
128 mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert);
129 dout << mark;
130 }
131 }
132 else {
133 if ( n == 4) {
134 // points sur courbe
135 Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[3]);
136 if ( GC.IsNull())
137 return 1;
138
139 Standard_Real U, U1, U2;
140 U1 = GC->FirstParameter();
141 U2 = GC->LastParameter();
142 Standard_Real Delta = ( U2 - U1) / (Nb-1);
143 for ( i = 1 ; i <= Nb; i++) {
144 U = U1 + (i-1) * Delta;
145 Points(i) = GC->Value(U);
146 }
147 }
148
149 else {
150 // test points ou ordonnees
151 hasPoints = Standard_False;
152 Standard_Integer nc = n - 3;
153 if (nc == 2 * Nb) {
154 // points
155 nc = 3;
156 for (i = 1; i <= Nb; i++) {
157 Points(i).SetCoord(atof(a[nc]),atof(a[nc+1]));
158 nc += 2;
159 }
160 }
161 else if (nc - 2 == Nb) {
162 // YValues
163 nc = 5;
164 X0 = atof(a[3]);
165 DX = atof(a[4]);
166 for (i = 1; i <= Nb; i++) {
167 YValues(i) = atof(a[nc]);
168 Points(i).SetCoord(X0+(i-1)*DX,YValues(i));
169 nc++;
170 }
171 }
172 else
173 return 1;
174 }
175 // display the points
176 for ( i = 1 ; i <= Nb; i++) {
177 mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert);
178 dout << mark;
179 }
180 }
181 dout.Flush();
182 Standard_Integer Dmin = 3;
183 Standard_Integer Dmax = 8;
184
185 Handle(Geom2d_BSplineCurve) TheCurve;
186 if (hasPoints)
187 TheCurve = Geom2dAPI_PointsToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol2d);
188 else
189 TheCurve = Geom2dAPI_PointsToBSpline(YValues,X0,DX,Dmin,Dmax,GeomAbs_C2,Tol2d);
190
191 DrawTrSurf::Set(a[1], TheCurve);
192 di << a[1];
193
194 return 0;
195
196}
197
198//=======================================================================
199//function : extrema
200//purpose :
201//=======================================================================
202
203static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a)
204{
205 if ( n<3) return 1;
206
207 Handle(Geom2d_Curve) GC1, GC2;
208
209 Standard_Real U1f,U1l,U2f,U2l;
210
211 GC1 = DrawTrSurf::GetCurve2d(a[1]);
212 if ( GC1.IsNull())
213 return 1;
214 U1f = GC1->FirstParameter();
215 U1l = GC1->LastParameter();
216
217 GC2 = DrawTrSurf::GetCurve2d(a[2]);
218 if ( GC2.IsNull())
219 return 1;
220 U2f = GC2->FirstParameter();
221 U2l = GC2->LastParameter();
222
223 char name[100];
224
225 Geom2dAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
226 for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
227 gp_Pnt2d P1,P2;
228 Ex.Points(i,P1,P2);
229 Handle(Geom2d_Line) L = new Geom2d_Line(P1,gp_Vec2d(P1,P2));
230 Handle(Geom2d_TrimmedCurve) CT =
231 new Geom2d_TrimmedCurve(L, 0., P1.Distance(P2));
232 sprintf(name,"%s%d","ext_",i);
233 char* temp = name; // portage WNT
234 DrawTrSurf::Set(temp, CT);
235 di << name << " ";
236 }
237
238 return 0;
239}
240
241
242//=======================================================================
243//function : intersect
244//purpose :
245//=======================================================================
246
247static Standard_Integer intersect(Draw_Interpretor& di, Standard_Integer n, const char** a)
248{
249 if( n < 2)
250 return 1;
251
252 Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[1]);
253 if ( C1.IsNull())
254 return 1;
255
256 Standard_Real Tol = 0.001;
257 Geom2dAPI_InterCurveCurve Intersector;
258
259 Handle(Geom2d_Curve) C2;
260 if ( n == 3) {
261 C2 = DrawTrSurf::GetCurve2d(a[2]);
262 if ( C2.IsNull())
263 return 1;
264 Intersector.Init(C1,C2,Tol);
265 }
266 else {
267 Intersector.Init(C1, Tol);
268 }
269
270 Standard_Integer i;
271
272 for ( i = 1; i <= Intersector.NbPoints(); i++) {
273 gp_Pnt2d P = Intersector.Point(i);
274 Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert);
275 dout << mark;
276 }
277 dout.Flush();
278
279 Handle(Geom2d_Curve) S1,S2;
280 Handle(DrawTrSurf_Curve2d) CD;
281 if ( n == 3) {
282 for ( i = 1; i <= Intersector.NbSegments(); i++) {
283 Intersector.Segment(i,S1,S2);
284 CD = new DrawTrSurf_Curve2d(S1, Draw_bleu, 30);
285 dout << CD;
286 CD = new DrawTrSurf_Curve2d(S2, Draw_violet, 30);
287 dout << CD;
288 }
289 }
290 dout.Flush();
291
292 return 0;
293}
294
295
296void GeometryTest::API2dCommands(Draw_Interpretor& theCommands)
297{
298 static Standard_Boolean done = Standard_False;
299 if (done) return;
300
301 const char *g;
302
303 done = Standard_True;
304 g = "GEOMETRY curves and surfaces analysis";
305
306 theCommands.Add("2dproj", "proj curve x y",__FILE__, proj,g);
307
308 g = "GEOMETRY approximations";
309
310 theCommands.Add("2dapprox", "2dapprox result nbpoint [curve] [[x] y [x] y...]",__FILE__,
311 appro,g);
312 theCommands.Add("2dinterpole", "2dinterpole result nbpoint [curve] [[x] y [x] y ...]",__FILE__,
313 appro,g);
314
315 g = "GEOMETRY curves and surfaces analysis";
316
317 theCommands.Add("2dextrema", "extrema curve curve",__FILE__,
318 extrema,g);
319
320 g = "GEOMETRY intersections";
321
322 theCommands.Add("2dintersect", "intersect curve curve",__FILE__,
323 intersect,g);
324}