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