45fd7d72adf3ca8c3447e9f8681d146486367039
[occt.git] / src / GeometryTest / GeometryTest_API2dCommands.cxx
1 // Created on: 1995-01-11
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <GeometryTest.hxx>
18 #include <Geom2d_BSplineCurve.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 _WIN32
40 Standard_IMPORT Draw_Viewer dout;
41 #endif
42
43 //=======================================================================
44 //function : proj
45 //purpose  : 
46 //=======================================================================
47
48 static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
49 {
50   if ( n < 4) return 1;
51
52   gp_Pnt2d P(Draw::Atof(a[2]),Draw::Atof(a[3]));
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));
69     Sprintf(name,"%s%d","ext_",i);
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
83 static 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) 
105       Tol2d = Draw::Atof(a[1]);
106     
107     di << "Tolerance for 2d approx : "<< Tol2d << "\n";
108     return 0;
109   }
110
111
112   Standard_Integer i, Nb = Draw::Atoi(a[2]);
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 j = 2; j<=Nb; j++) {
135       dout.Select(id,XX,YY,b);
136       Points(j) = gp_Pnt2d( ((Standard_Real)XX)/zoom, 
137                             ((Standard_Real)YY)/zoom );
138       mark = new Draw_Marker2D( Points(j), 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++) {
167           Points(i).SetCoord(Draw::Atof(a[nc]),Draw::Atof(a[nc+1]));
168           nc += 2;
169         }
170       }
171       else if (nc - 2 == Nb) {
172         // YValues
173         nc = 5;
174         X0 = Draw::Atof(a[3]);
175         DX = Draw::Atof(a[4]);
176         for (i = 1; i <= Nb; i++) {
177           YValues(i) = Draw::Atof(a[nc]);
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 : intersect
210 //purpose  : 
211 //=======================================================================
212
213 static Standard_Integer intersect(Draw_Interpretor& /*di*/, Standard_Integer n, const char** a)
214 {
215   if( n < 2) 
216     return 1;
217
218   Handle(Geom2d_Curve) C1 = DrawTrSurf::GetCurve2d(a[1]);
219   if ( C1.IsNull()) 
220     return 1;
221
222   Standard_Real Tol = 0.001;
223   Geom2dAPI_InterCurveCurve Intersector;
224
225   Handle(Geom2d_Curve) C2;
226   if ( n == 3) {
227     C2 = DrawTrSurf::GetCurve2d(a[2]);
228     if ( C2.IsNull())
229       return 1;
230     Intersector.Init(C1,C2,Tol);
231   }
232   else {
233     Intersector.Init(C1, Tol);
234   }
235
236   Standard_Integer i;
237
238   for ( i = 1; i <= Intersector.NbPoints(); i++) {
239     gp_Pnt2d P = Intersector.Point(i);
240     Handle(Draw_Marker2D) mark = new Draw_Marker2D( P, Draw_X, Draw_vert); 
241     dout << mark;
242   }
243   dout.Flush();
244
245   Handle(Geom2d_Curve) S1,S2;
246   Handle(DrawTrSurf_Curve2d) CD;
247   if ( n == 3) {
248     for ( i = 1; i <= Intersector.NbSegments(); i++) {
249       Intersector.Segment(i,S1,S2);
250       CD = new DrawTrSurf_Curve2d(S1, Draw_bleu, 30);
251       dout << CD;
252       CD = new DrawTrSurf_Curve2d(S2, Draw_violet, 30);
253       dout << CD;
254     }
255   }
256   dout.Flush();
257
258   return 0;
259 }
260
261
262 void GeometryTest::API2dCommands(Draw_Interpretor& theCommands)
263 {
264   static Standard_Boolean done = Standard_False;
265   if (done) return;
266
267   const char *g;
268
269   done = Standard_True;
270   g = "GEOMETRY curves and surfaces analysis";
271
272   theCommands.Add("2dproj", "proj curve x y",__FILE__, proj,g);
273
274   g = "GEOMETRY approximations";
275
276   theCommands.Add("2dapprox", "2dapprox result nbpoint [curve] [[x] y [x] y...]",__FILE__, 
277                   appro,g);
278   theCommands.Add("2dinterpole", "2dinterpole result nbpoint [curve] [[x] y [x] y ...]",__FILE__, 
279                   appro,g);
280
281   g = "GEOMETRY curves and surfaces analysis";
282
283   g = "GEOMETRY intersections";
284
285   theCommands.Add("2dintersect", "intersect curve curve",__FILE__,
286                   intersect,g);
287 }