summaryrefslogtreecommitdiff
path: root/src/GeometryTest/GeometryTest_APICommands.cxx
blob: e07fe07d583d0b7733a4edd8b9484100e369147f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// File:	GeometryTest_APICommands.cxx
// Created:	Tue Jan 17 18:06:26 1995
// Author:	Remi LEQUETTE
//		<rle@bravox>


#include <Geom_Curve.hxx>
#include <Geom_Surface.hxx>
#include <Draw.hxx>
#include <Draw_Interpretor.hxx>
#include <DrawTrSurf.hxx>
#include <Draw_Appli.hxx>
#include <GeometryTest.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <GeomAPI_ProjectPointOnSurf.hxx>
#include <GeomAPI_ExtremaCurveCurve.hxx>
#include <GeomAPI_ExtremaCurveSurface.hxx>
#include <GeomAPI_ExtremaSurfaceSurface.hxx>
#include <GeomAPI_PointsToBSpline.hxx>
#include <GeomAPI_PointsToBSplineSurface.hxx>
#include <Geom_Line.hxx>
#include <Geom_TrimmedCurve.hxx>
#include <Draw_Segment3D.hxx>
#include <Draw_Marker3D.hxx>
#include <Draw_Color.hxx>
#include <Draw_MarkerShape.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TColgp_Array2OfPnt.hxx>
#include <TColStd_Array2OfReal.hxx>
#include <Precision.hxx>
#include <stdio.h>
#ifdef WNT
Standard_IMPORT Draw_Viewer dout;
#endif

//=======================================================================
//function : proj
//purpose  : 
//=======================================================================

static Standard_Integer proj (Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if ( n < 5) return 1;

  gp_Pnt P(atof(a[2]),atof(a[3]),atof(a[4]));

  char name[100];

  Handle(Geom_Curve) GC = DrawTrSurf::GetCurve(a[1]);
  Handle(Geom_Surface) GS;

  if (GC.IsNull())  {
    GS = DrawTrSurf::GetSurface(a[1]);
    if (GS.IsNull())
      return 1;
    Standard_Real U1, U2, V1, V2;
    GS->Bounds(U1,U2,V1,V2);

    GeomAPI_ProjectPointOnSurf proj(P,GS,U1,U2,V1,V2);
    Standard_Real UU,VV;
    for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) {
      gp_Pnt P1 = proj.Point(i);
      if ( P.Distance(P1) > Precision::Confusion()) {
	Handle(Geom_Line) L = new Geom_Line(P,gp_Vec(P,P1));
	Handle(Geom_TrimmedCurve) CT = 
	  new Geom_TrimmedCurve(L, 0., P.Distance(P1));
	sprintf(name,"%s%d","ext_",i);
	char* temp = name; // portage WNT
	DrawTrSurf::Set(temp, CT);
	di << name << " ";
      }
      else {
	sprintf(name,"%s%d","ext_",i);
	di << name << " ";
	char* temp = name; // portage WNT
	DrawTrSurf::Set(temp, P1);
	proj.Parameters(i,UU,VV);
	di << " Le point est sur la surface." << "\n";
	di << " Ses parametres sont:  UU = " << UU << "\n";
	di << "                       VV = " << VV << "\n";
      }
    }

  }
  else {
    GeomAPI_ProjectPointOnCurve proj(P,GC,GC->FirstParameter(),
				          GC->LastParameter());
//    Standard_Real UU;
    for ( Standard_Integer i = 1; i <= proj.NbPoints(); i++) {
      gp_Pnt P1 = proj.Point(i);
      Standard_Real UU = proj.Parameter(i);
      di << " parameter " << i << " = " << UU << "\n";
      if ( P.Distance(P1) > Precision::Confusion()) {
	Handle(Geom_Line) L = new Geom_Line(P,gp_Vec(P,P1));
	Handle(Geom_TrimmedCurve) CT = 
	  new Geom_TrimmedCurve(L, 0., P.Distance(P1));
	sprintf(name,"%s%d","ext_",i);
	char* temp = name; // portage WNT
	DrawTrSurf::Set(temp, CT);
	di << name << " ";
      }
      else {
	sprintf(name,"%s%d","ext_",i);
	char* temp = name; // portage WNT
	DrawTrSurf::Set(temp, P1);
	di << name << " ";
	UU = proj.Parameter(i);
	di << " Le point est sur la courbe." << "\n";
	di << " Son parametre est U = " << UU << "\n";
      }
    }
  }
  return 0;
}

//=======================================================================
//function : appro
//purpose  : 
//=======================================================================

static Standard_Integer appro(Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if ( n<3) return 1;

  Handle(Geom_Curve) GC;
  Standard_Integer Nb = atoi(a[2]);

  TColgp_Array1OfPnt Points(1, Nb);

  Handle(Draw_Marker3D) mark;

  if ( n == 4) {
    GC = DrawTrSurf::GetCurve(a[3]);
    if ( GC.IsNull()) 
      return 1;

    Standard_Real U, U1, U2;
    U1 = GC->FirstParameter();
    U2 = GC->LastParameter();
    Standard_Real Delta = ( U2 - U1) / (Nb-1);
    for ( Standard_Integer i = 1 ; i <= Nb; i++) {
      U = U1 + (i-1) * Delta;
      Points(i) = GC->Value(U);
      mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert); 
      dout << mark;
    }
  }
  else {
    Standard_Integer id,XX,YY,b;
    dout.Select(id,XX,YY,b);
    Standard_Real zoom = dout.Zoom(id);

    Points(1) = gp_Pnt( ((Standard_Real)XX)/zoom, 
		        ((Standard_Real)YY)/zoom, 
		        0.);
    
    mark = new Draw_Marker3D( Points(1), Draw_X, Draw_vert); 
    
    dout << mark;
    
    for (Standard_Integer i = 2; i<=Nb; i++) {
      dout.Select(id,XX,YY,b);
      Points(i) = gp_Pnt( ((Standard_Real)XX)/zoom, 
			 ((Standard_Real)YY)/zoom, 
			 0.);
      mark = new Draw_Marker3D( Points(i), Draw_X, Draw_vert); 
      dout << mark;
    }
  }    
  dout.Flush();
  Standard_Integer Dmin = 3;
  Standard_Integer Dmax = 8;
  Standard_Real Tol3d = 1.e-3;
  
  Handle(Geom_BSplineCurve) TheCurve;
  GeomAPI_PointsToBSpline aPointToBSpline(Points,Dmin,Dmax,GeomAbs_C2,Tol3d);
  TheCurve = aPointToBSpline.Curve();

  
  DrawTrSurf::Set(a[1], TheCurve);
  di << a[1];

  return 0;

}


//=======================================================================
//function : grilapp
//purpose  : 
//=======================================================================

static Standard_Integer grilapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if ( n < 12) return 1;

  Standard_Integer i,j;
  Standard_Integer Nu = atoi(a[2]);
  Standard_Integer Nv = atoi(a[3]);
  TColStd_Array2OfReal ZPoints (1, Nu, 1, Nv);

  Standard_Real X0 = atof(a[4]);
  Standard_Real dX = atof(a[5]);
  Standard_Real Y0 = atof(a[6]);
  Standard_Real dY = atof(a[7]);

  Standard_Integer Count = 8;
  for ( j = 1; j <= Nv; j++) {
    for ( i = 1; i <= Nu; i++) {
      if ( Count > n) return 1;
      ZPoints(i,j) = atof(a[Count]);
      Count++;
    }
  }
  
  Handle(Geom_BSplineSurface) S 
    = GeomAPI_PointsToBSplineSurface(ZPoints,X0,dX,Y0,dY);
  DrawTrSurf::Set(a[1],S);

  di << a[1];
  
  return 0;
}

//=======================================================================
//function : surfapp
//purpose  : 
//=======================================================================

static Standard_Integer surfapp(Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if ( n < 5 ) return 1;

  Standard_Integer i,j;
  Standard_Integer Nu = atoi(a[2]);
  Standard_Integer Nv = atoi(a[3]);
  TColgp_Array2OfPnt Points (1, Nu, 1, Nv);

  if ( n == 5) {
    Handle(Geom_Surface) Surf = DrawTrSurf::GetSurface(a[4]);
    if ( Surf.IsNull()) return 1;

    Standard_Real U, V, U1, V1, U2, V2;
    Surf->Bounds( U1, U2, V1, V2);
    for ( j = 1; j <= Nv; j++) {
      V = V1 + (j-1) * (V2-V1) / (Nv-1);
      for ( i = 1; i <= Nu; i++) {
	U = U1 + (i-1) * (U2-U1) / (Nu-1);
	Points(i,j) = Surf->Value(U,V);
      }
    } 
  }
  else if ( n >= 16) {
    Standard_Integer Count = 4;
    for ( j = 1; j <= Nv; j++) {
      for ( i = 1; i <= Nu; i++) {
	if ( Count > n) return 1;
	Points(i,j) = gp_Pnt(atof(a[Count]),atof(a[Count+1]),atof(a[Count+2]));
	Count += 3;
      }
    }
  }
  char name[100];
  Standard_Integer Count = 1;
  for ( j = 1; j <= Nv; j++) {
    for ( i = 1; i <= Nu; i++) {
      sprintf(name,"point_%d",Count++);
      char* temp = name; // portage WNT
      DrawTrSurf::Set(temp,Points(i,j));
    }
  } 

  Handle(Geom_BSplineSurface) S = GeomAPI_PointsToBSplineSurface(Points);
  DrawTrSurf::Set(a[1],S);
  di << a[1];

  return 0;
}


//=======================================================================
//function : extrema
//purpose  : 
//=======================================================================

static Standard_Integer extrema(Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if ( n<3) return 1;

  Handle(Geom_Curve)   GC1, GC2;
  Handle(Geom_Surface) GS1, GS2;

  Standard_Boolean C1 = Standard_False;
  Standard_Boolean C2 = Standard_False;
  Standard_Boolean S1 = Standard_False;
  Standard_Boolean S2 = Standard_False;

  Standard_Real U1f,U1l,U2f,U2l,V1f,V1l,V2f,V2l;

  GC1 = DrawTrSurf::GetCurve(a[1]);
  if ( GC1.IsNull()) {
    GS1 = DrawTrSurf::GetSurface(a[1]);
    if ( GS1.IsNull())
      return 1;
    S1 = Standard_True;
    GS1->Bounds(U1f,U1l,V1f,V1l);
  }
  else {
    C1 = Standard_True;
    U1f = GC1->FirstParameter();
    U1l = GC1->LastParameter();
  }

  GC2 = DrawTrSurf::GetCurve(a[2]);
  if ( GC2.IsNull()) {
    GS2 = DrawTrSurf::GetSurface(a[2]);
    if ( GS2.IsNull())
      return 1;
    S2 = Standard_True;
    GS2->Bounds(U2f,U2l,V2f,V2l);
  }
  else {
    C2 = Standard_True;
    U2f = GC2->FirstParameter();
    U2l = GC2->LastParameter();
  }

  char name[100];
  if ( C1 && C2) {
    GeomAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
    if(!Ex.Extrema().IsParallel()) {
      for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
	gp_Pnt P1,P2;
	Ex.Points(i,P1,P2);
	if (P1.Distance(P2) < 1.e-16) {
	  di << "Extrema " << i << " is point : " << P1.X() << " " << P1.Y() << " " << P1.Z() << "\n";
	  continue;
	}
	Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
	Handle(Geom_TrimmedCurve) CT = 
	  new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
	sprintf(name,"%s%d","ext_",i);
	char* temp = name; // portage WNT
	DrawTrSurf::Set(temp, CT);
	di << name << " ";
      }
    }
    else {
      di << "Infinite number of extremas, distance = " << Ex.LowerDistance() << "\n";
    }
  }
  else if ( C1 & S2) {
    GeomAPI_ExtremaCurveSurface Ex(GC1,GS2,U1f,U1l,U2f,U2l,V2f,V2l);
    for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
      gp_Pnt P1,P2;
      Ex.Points(i,P1,P2);
      if (P1.Distance(P2) < 1.e-16) continue;
      Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
      Handle(Geom_TrimmedCurve) CT = 
	new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
      sprintf(name,"%s%d","ext_",i);
      char* temp = name; // portage WNT
      DrawTrSurf::Set(temp, CT);
      di << name << " ";
    }
  }
  else if ( S1 & C2) {
    GeomAPI_ExtremaCurveSurface Ex(GC2,GS1,U2f,U2l,U1f,U1l,V1f,V1l);
    for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
      gp_Pnt P1,P2;
      Ex.Points(i,P1,P2);
      if (P1.Distance(P2) < 1.e-16) continue;
      Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
      Handle(Geom_TrimmedCurve) CT = 
	new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
      sprintf(name,"%s%d","ext_",i);
      char* temp = name; // portage WNT
      DrawTrSurf::Set(temp, CT);
      di << name << " ";
    }
  }
  else if ( S1 & S2) {
    GeomAPI_ExtremaSurfaceSurface Ex(GS1,GS2,U1f,U1l,V1f,V1l,U2f,U2l,V2f,V2l);
    for ( Standard_Integer i = 1; i <= Ex.NbExtrema(); i++) {
      gp_Pnt P1,P2;
      Ex.Points(i,P1,P2);
      if (P1.Distance(P2) < 1.e-16) continue;
      Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
      Handle(Geom_TrimmedCurve) CT = 
	new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
      sprintf(name,"%s%d","ext_",i);
      char* temp = name; // portage WNT
      DrawTrSurf::Set(temp, CT);
      di << name << " ";
    }
  }

  return 0;

}

//=======================================================================
//function : totalextcc
//purpose  : 
//=======================================================================

static Standard_Integer totalextcc(Draw_Interpretor& di, Standard_Integer n, const char** a)
{
  if ( n<3) return 1;

  Handle(Geom_Curve)   GC1, GC2;


  Standard_Real U1f,U1l,U2f,U2l;

  GC1 = DrawTrSurf::GetCurve(a[1]);
  if ( GC1.IsNull()) {
      return 1;
  }
  else {
    U1f = GC1->FirstParameter();
    U1l = GC1->LastParameter();
  }

  GC2 = DrawTrSurf::GetCurve(a[2]);
  if ( GC2.IsNull()) {
      return 1;
  }
  else {
    U2f = GC2->FirstParameter();
    U2l = GC2->LastParameter();
  }

  char name[100];
    GeomAPI_ExtremaCurveCurve Ex(GC1,GC2,U1f,U1l,U2f,U2l);
  gp_Pnt P1,P2;
  if(Ex.TotalNearestPoints(P1,P2)) {
    if (P1.Distance(P2) < 1.e-16) {
      di << "Extrema is point : " << P1.X() << " " << P1.Y() << " " << P1.Z() << "\n";
    }
    else {
      di << "Extrema is segment of line" << "\n"; 
      Handle(Geom_Line) L = new Geom_Line(P1,gp_Vec(P1,P2));
      Handle(Geom_TrimmedCurve) CT = 
	new Geom_TrimmedCurve(L, 0., P1.Distance(P2));
      sprintf(name,"%s%d","ext_",1);
      char* temp = name; // portage WNT
      DrawTrSurf::Set(temp, CT);
      di << name << " ";
    }

    Standard_Real u1, u2;
    Ex.TotalLowerDistanceParameters(u1, u2);

    di << "Parameters on curves : " << u1 << " " << u2 << "\n";

  }
  else {
    di << "Curves are infinite and parallel" << "\n";
  }
  
  di << "Minimal distance : " << Ex.TotalLowerDistance() << "\n";

  return 0;

}


void GeometryTest::APICommands(Draw_Interpretor& theCommands)
{
  static Standard_Boolean done = Standard_False;
  if (done) return;

  done = Standard_True;

  theCommands.Add("proj", "proj curve/surf x y z",__FILE__, proj);

  theCommands.Add("appro", "appro result nbpoint [curve]",__FILE__, appro);
  theCommands.Add("surfapp","surfapp result nbupoint nbvpoint x y z ....",
		  __FILE__,
		  surfapp);
  theCommands.Add("grilapp",
       "grilapp result nbupoint nbvpoint X0 dX Y0 dY z11 z12 .. z1nu ....  ",
        __FILE__,grilapp);

  theCommands.Add("extrema", "extrema curve/surface curve/surface",__FILE__,extrema);
  theCommands.Add("totalextcc", "totalextcc curve curve",__FILE__,totalextcc);
}