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