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. |
b311480e |
16 | |
7fd59977 |
17 | // modified : pmn 11/04/97 : mis dans GeomliteTest |
18 | |
19 | |
20 | #include <GeomliteTest.hxx> |
21 | #include <Geom2d_Curve.hxx> |
22 | #include <Draw.hxx> |
23 | #include <Draw_Interpretor.hxx> |
24 | #include <DrawTrSurf.hxx> |
25 | #include <Draw_Appli.hxx> |
26 | #include <DrawTrSurf_Curve2d.hxx> |
27 | #include <Geom2dAPI_ProjectPointOnCurve.hxx> |
28 | #include <Geom2dAPI_ExtremaCurveCurve.hxx> |
29 | #include <Geom2dAPI_PointsToBSpline.hxx> |
30 | #include <Geom2dAPI_InterCurveCurve.hxx> |
31 | #include <Geom2d_Line.hxx> |
32 | #include <Geom2d_TrimmedCurve.hxx> |
33 | #include <TColgp_Array1OfPnt2d.hxx> |
34 | #include <gp_Pnt.hxx> |
35 | #include <Draw_Marker2D.hxx> |
36 | #include <Draw_Color.hxx> |
37 | #include <Draw_MarkerShape.hxx> |
38 | #include <TColStd_Array1OfReal.hxx> |
39 | #include <GeomAbs_Shape.hxx> |
40 | #include <Precision.hxx> |
3f16d970 |
41 | #include <Geom2d_Circle.hxx> |
42 | #include <IntAna2d_AnaIntersection.hxx> |
43 | #include <IntAna2d_IntPoint.hxx> |
7fd59977 |
44 | |
45 | #include <stdio.h> |
46 | #ifdef WNT |
47 | Standard_IMPORT Draw_Viewer dout; |
48 | #endif |
49 | |
50 | //======================================================================= |
51 | //function : proj |
52 | //purpose : |
53 | //======================================================================= |
54 | |
55 | static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a) |
56 | { |
57 | if ( n < 4) return 1; |
58 | |
91322f44 |
59 | gp_Pnt2d P(Draw::Atof(a[2]),Draw::Atof(a[3])); |
7fd59977 |
60 | |
61 | char name[100]; |
62 | |
63 | Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[1]); |
64 | |
65 | if (GC.IsNull()) |
66 | return 1; |
67 | |
68 | Geom2dAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(), |
69 | GC->LastParameter()); |
70 | |
71 | for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) { |
72 | gp_Pnt2d P1 = proj.Point(i); |
73 | Handle(Geom2d_Line) L = new Geom2d_Line(P,gp_Vec2d(P,P1)); |
74 | Handle(Geom2d_TrimmedCurve) CT = |
75 | new Geom2d_TrimmedCurve(L, 0., P.Distance(P1)); |
91322f44 |
76 | Sprintf(name,"%s%d","ext_",i); |
7fd59977 |
77 | char* temp = name; // portage WNT |
78 | DrawTrSurf::Set(temp, CT); |
79 | di << name << " "; |
80 | } |
81 | |
82 | return 0; |
83 | } |
84 | |
85 | //======================================================================= |
86 | //function : appro |
87 | //purpose : |
88 | //======================================================================= |
89 | |
90 | static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a) |
91 | { |
92 | // Approximation et interpolation 2d |
93 | |
94 | // 2dappro |
95 | // - affiche la tolerance |
96 | // 2dappro tol |
97 | // - change la tolerance |
98 | // 2dappro result nbpoint |
99 | // - saisie interactive |
100 | // 2dappro result nbpoint curve |
101 | // - calcule des points sur la courbe |
102 | // 2dappro result nbpoint x1 y1 x2 y2 .. |
103 | // - tableau de points |
104 | // 2dappro result nbpoint x1 dx y1 y2 .. |
105 | // - tableau de points (x1,y1) (x1+dx,y2) ... avec x = t |
106 | |
107 | |
108 | static Standard_Real Tol2d = 1.e-6; |
109 | |
110 | if (n < 3) { |
111 | if (n == 2) |
91322f44 |
112 | Tol2d = Draw::Atof(a[1]); |
7fd59977 |
113 | |
114 | di << "Tolerance for 2d approx : "<< Tol2d << "\n"; |
115 | return 0; |
116 | } |
117 | |
118 | |
91322f44 |
119 | Standard_Integer i, Nb = Draw::Atoi(a[2]); |
7fd59977 |
120 | |
121 | Standard_Boolean hasPoints = Standard_True; |
122 | TColgp_Array1OfPnt2d Points(1, Nb); |
123 | TColStd_Array1OfReal YValues(1,Nb); |
124 | Standard_Real X0=0,DX=0; |
125 | |
126 | Handle(Draw_Marker2D) mark; |
127 | |
128 | if (n == 3) { |
129 | // saisie interactive |
130 | Standard_Integer id,XX,YY,b; |
131 | dout.Select(id,XX,YY,b); |
132 | Standard_Real zoom = dout.Zoom(id); |
133 | |
134 | Points(1) = gp_Pnt2d( ((Standard_Real)XX)/zoom, |
135 | ((Standard_Real)YY)/zoom ); |
136 | |
137 | mark = new Draw_Marker2D( Points(1), Draw_X, Draw_vert); |
138 | |
139 | dout << mark; |
140 | |
141 | for (i = 2; i<=Nb; i++) { |
142 | dout.Select(id,XX,YY,b); |
143 | Points(i) = gp_Pnt2d( ((Standard_Real)XX)/zoom, |
144 | ((Standard_Real)YY)/zoom ); |
145 | mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert); |
146 | dout << mark; |
147 | } |
148 | } |
149 | else { |
150 | if ( n == 4) { |
151 | // points sur courbe |
152 | Handle(Geom2d_Curve) GC = DrawTrSurf::GetCurve2d(a[3]); |
153 | if ( GC.IsNull()) |
154 | return 1; |
155 | |
156 | Standard_Real U, U1, U2; |
157 | U1 = GC->FirstParameter(); |
158 | U2 = GC->LastParameter(); |
159 | Standard_Real Delta = ( U2 - U1) / (Nb-1); |
160 | for ( i = 1 ; i <= Nb; i++) { |
161 | U = U1 + (i-1) * Delta; |
162 | Points(i) = GC->Value(U); |
163 | } |
164 | } |
165 | |
166 | else { |
167 | // test points ou ordonnees |
168 | hasPoints = Standard_False; |
169 | Standard_Integer nc = n - 3; |
170 | if (nc == 2 * Nb) { |
171 | // points |
172 | nc = 3; |
173 | for (i = 1; i <= Nb; i++) { |
91322f44 |
174 | Points(i).SetCoord(Draw::Atof(a[nc]),Draw::Atof(a[nc+1])); |
7fd59977 |
175 | nc += 2; |
176 | } |
177 | } |
178 | else if (nc - 2 == Nb) { |
179 | // YValues |
180 | nc = 5; |
91322f44 |
181 | X0 = Draw::Atof(a[3]); |
182 | DX = Draw::Atof(a[4]); |
7fd59977 |
183 | for (i = 1; i <= Nb; i++) { |
91322f44 |
184 | YValues(i) = Draw::Atof(a[nc]); |
7fd59977 |
185 | Points(i).SetCoord(X0+(i-1)*DX,YValues(i)); |
186 | nc++; |
187 | } |
188 | } |
189 | else |
190 | return 1; |
191 | } |
192 | // display the points |
193 | for ( i = 1 ; i <= Nb; i++) { |
194 | mark = new Draw_Marker2D( Points(i), Draw_X, Draw_vert); |
195 | dout << mark; |
196 | } |
197 | } |
198 | dout.Flush(); |
199 | Standard_Integer Dmin = 3; |
200 | Standard_Integer Dmax = 8; |
201 | |
202 | Handle(Geom2d_BSplineCurve) TheCurve; |
203 | if (hasPoints) |
204 | TheCurve = Geom2dAPI_PointsToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol2d); |
205 | else |
206 | TheCurve = Geom2dAPI_PointsToBSpline(YValues,X0,DX,Dmin,Dmax,GeomAbs_C2,Tol2d); |
207 | |
208 | DrawTrSurf::Set(a[1], TheCurve); |
209 | di << a[1]; |
210 | |
211 | return 0; |
212 | |
213 | } |
214 | |
215 | //======================================================================= |
216 | //function : extrema |
217 | //purpose : |
218 | //======================================================================= |
219 | |
220 | static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a) |
221 | { |
222 | if ( n<3) return 1; |
223 | |
224 | Handle(Geom2d_Curve) GC1, GC2; |
225 | |
226 | Standard_Real U1f,U1l,U2f,U2l; |
227 | |
228 | GC1 = DrawTrSurf::GetCurve2d(a[1]); |
229 | if ( GC1.IsNull()) |
230 | return 1; |
231 | U1f = GC1->FirstParameter(); |
232 | U1l = GC1->LastParameter(); |
233 | |
234 | GC2 = DrawTrSurf::GetCurve2d(a[2]); |
235 | if ( GC2.IsNull()) |
236 | return 1; |
237 | U2f = GC2->FirstParameter(); |
238 | U2l = GC2->LastParameter(); |
239 | |
240 | char name[100]; |
241 | |
242 | Geom2dAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l); |
243 | |
244 | // modified by APV (compilation error - LINUX) |
245 | // for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) { |
246 | Standard_Integer i; |
32ca7a51 |
247 | const Standard_Integer aNExtr = Ex.NbExtrema(); |
248 | for ( i = 1; i <= aNExtr; i++) { |
7fd59977 |
249 | // modified by APV (compilation error - LINUX) |
250 | |
251 | gp_Pnt2d P1,P2; |
252 | Ex.Points(i,P1,P2); |
e8746a26 |
253 | di << "dist " << i << ": " << Ex.Distance(i) << " "; |
7fd59977 |
254 | if (Ex.Distance(i) <= Precision::PConfusion()) { |
255 | Handle(Draw_Marker2D) mark = new Draw_Marker2D( P1, Draw_X, Draw_vert); |
256 | dout << mark; |
257 | dout.Flush(); |
e8746a26 |
258 | Sprintf(name,"%s%d","ext_",i); |
259 | char* temp = name; |
260 | DrawTrSurf::Set(temp, P1); |
261 | di << name << "\n"; |
7fd59977 |
262 | } |
263 | else { |
264 | Handle(Geom2d_Line) L = new Geom2d_Line(P1,gp_Vec2d(P1,P2)); |
e8746a26 |
265 | Handle(Geom2d_TrimmedCurve) CT = new Geom2d_TrimmedCurve(L, 0., P1.Distance(P2)); |
91322f44 |
266 | Sprintf(name,"%s%d","ext_",i); |
7fd59977 |
267 | char* temp = name; // portage WNT |
268 | DrawTrSurf::Set(temp, CT); |
e8746a26 |
269 | di << name << "\n"; |
7fd59977 |
270 | } |
271 | } |
272 | if (i==1) |
32ca7a51 |
273 | di << "No solutions!\n"; |
7fd59977 |
274 | |
275 | return 0; |
276 | } |
277 | |
278 | |
279 | //======================================================================= |
280 | //function : intersect |
281 | //purpose : |
282 | //======================================================================= |
283 | |
305cc3f8 |
284 | static Standard_Integer intersect(Draw_Interpretor& di, Standard_Integer n, const char** a) |
7fd59977 |
285 | { |
286 | if( n < 2) |
305cc3f8 |
287 | { |
0797d9d3 |
288 | #ifdef OCCT_DEBUG |
305cc3f8 |
289 | cout<< "2dintersect curve curve [Tol]"<<endl; |
63c629aa |
290 | #endif |
7fd59977 |
291 | return 1; |
305cc3f8 |
292 | } |
293 | Standard_Integer k = 1; |
294 | Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[k++]); |
7fd59977 |
295 | if ( C1.IsNull()) |
296 | return 1; |
297 | |
298 | Standard_Real Tol = 0.001; |
299 | Geom2dAPI_InterCurveCurve Intersector; |
300 | |
301 | Handle(Geom2d_Curve) C2; |
305cc3f8 |
302 | if ( k < n ) { |
303 | C2 = DrawTrSurf::GetCurve2d(a[k++]); |
7fd59977 |
304 | if ( C2.IsNull()) |
305 | return 1; |
305cc3f8 |
306 | } |
307 | if(k < n) |
91322f44 |
308 | Tol = Draw::Atof(a[k]); |
305cc3f8 |
309 | |
310 | if(!C2.IsNull()) |
311 | { |
7fd59977 |
312 | Intersector.Init(C1,C2,Tol); |
313 | } |
314 | else { |
315 | Intersector.Init(C1, Tol); |
316 | } |
317 | |
318 | Standard_Integer i; |
319 | |
320 | for ( i = 1; i <= Intersector.NbPoints(); i++) { |
321 | gp_Pnt2d P = Intersector.Point(i); |
305cc3f8 |
322 | di<<"Intersection point "<<i<<" : "<<P.X()<<" "<<P.Y()<<"\n"; |
7fd59977 |
323 | Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert); |
324 | dout << mark; |
325 | } |
326 | dout.Flush(); |
327 | |
328 | Handle(Geom2d_Curve) S1,S2; |
329 | Handle(DrawTrSurf_Curve2d) CD; |
305cc3f8 |
330 | for ( i = 1; i <= Intersector.NbSegments(); i++) { |
331 | Intersector.Segment(i,S1,S2); |
332 | CD = new DrawTrSurf_Curve2d(S1, Draw_bleu, 30); |
333 | dout << CD; |
334 | CD = new DrawTrSurf_Curve2d(S2, Draw_violet, 30); |
335 | dout << CD; |
7fd59977 |
336 | } |
305cc3f8 |
337 | |
7fd59977 |
338 | dout.Flush(); |
339 | |
340 | return 0; |
341 | } |
342 | |
3f16d970 |
343 | //======================================================================= |
344 | //function : intersect |
345 | //purpose : |
346 | //======================================================================= |
347 | |
348 | static Standard_Integer intersect_ana(Draw_Interpretor& di, Standard_Integer n, const char** a) |
349 | { |
350 | if( n < 2) |
351 | { |
352 | cout<< "2dintana circle circle "<<endl; |
353 | return 1; |
354 | } |
355 | |
356 | Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[1]); |
357 | if ( C1.IsNull() && !C1->IsKind(STANDARD_TYPE(Geom2d_Circle))) |
358 | return 1; |
359 | |
360 | Handle(Geom2d_Curve) C2 = DrawTrSurf::GetCurve2d(a[2]); |
361 | if ( C2.IsNull() && !C2->IsKind(STANDARD_TYPE(Geom2d_Circle))) |
362 | return 1; |
363 | |
364 | Handle(Geom2d_Circle) aCir1 = Handle(Geom2d_Circle)::DownCast(C1); |
365 | Handle(Geom2d_Circle) aCir2 = Handle(Geom2d_Circle)::DownCast(C2); |
366 | |
367 | IntAna2d_AnaIntersection Intersector(aCir1->Circ2d(), aCir2->Circ2d()); |
368 | |
369 | Standard_Integer i; |
370 | |
371 | for ( i = 1; i <= Intersector.NbPoints(); i++) { |
372 | gp_Pnt2d P = Intersector.Point(i).Value(); |
373 | di<<"Intersection point "<<i<<" : "<<P.X()<<" "<<P.Y()<<"\n"; |
374 | di<<"parameter on the fist: "<<Intersector.Point(i).ParamOnFirst(); |
375 | di<<" parameter on the second: "<<Intersector.Point(i).ParamOnSecond()<<"\n"; |
376 | Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert); |
377 | dout << mark; |
378 | } |
379 | dout.Flush(); |
380 | |
381 | return 0; |
382 | } |
383 | |
384 | |
7fd59977 |
385 | |
386 | void GeomliteTest::API2dCommands(Draw_Interpretor& theCommands) |
387 | { |
388 | static Standard_Boolean done = Standard_False; |
389 | if (done) return; |
390 | |
391 | const char *g; |
392 | |
393 | done = Standard_True; |
394 | g = "GEOMETRY curves and surfaces analysis"; |
395 | |
396 | theCommands.Add("2dproj", "proj curve x y",__FILE__, proj,g); |
397 | |
398 | g = "GEOMETRY approximations"; |
399 | |
400 | theCommands.Add("2dapprox", "2dapprox result nbpoint [curve] [[x] y [x] y...]",__FILE__, |
401 | appro,g); |
402 | theCommands.Add("2dinterpole", "2dinterpole result nbpoint [curve] [[x] y [x] y ...]",__FILE__, |
403 | appro,g); |
404 | |
405 | g = "GEOMETRY curves and surfaces analysis"; |
406 | |
407 | theCommands.Add("2dextrema", "extrema curve curve",__FILE__, |
408 | extrema,g); |
409 | |
410 | g = "GEOMETRY intersections"; |
411 | |
305cc3f8 |
412 | theCommands.Add("2dintersect", "intersect curve curve [Tol]",__FILE__, |
7fd59977 |
413 | intersect,g); |
3f16d970 |
414 | |
415 | theCommands.Add("2dintanalytical", "intersect curve curve using IntAna",__FILE__, |
416 | intersect_ana,g); |
7fd59977 |
417 | } |