0031445: Advanced wrappers, C# wrapper - provide device info in About dialog of WPF...
[occt.git] / samples / mfc / occtdemo / Approx / Approx_Presentation.cpp
CommitLineData
7fd59977 1// Approx_Presentation.cpp: implementation of the Approx_Presentation class.
2// Approximation of curves and surfaces from points.
3////////////////////////////////////////////////////////////////////////////
4
5#include "stdafx.h"
6#include "Approx_Presentation.h"
7
8#include <GeomAPI_PointsToBSpline.hxx>
9#include <GeomAPI_PointsToBSplineSurface.hxx>
10#include <TColgp_Array1OfPnt.hxx>
11#include <TColgp_Array2OfPnt.hxx>
12#include <TColStd_Array2OfReal.hxx>
13#include <gp_Pnt.hxx>
14
15#ifdef WNT
16 #define EOL "\r\n"
17#else
18 #define EOL "\n"
19#endif
20
21#define SCALE 100
22
23
24// Initialization of global variable with an instance of this class
25OCCDemo_Presentation* OCCDemo_Presentation::Current = new Approx_Presentation;
26
27// Initialization of array of samples
28const Approx_Presentation::PSampleFuncType Approx_Presentation::SampleFuncs[] =
29{
30 &Approx_Presentation::sample1,
31 &Approx_Presentation::sample2,
32 &Approx_Presentation::sample3,
33 &Approx_Presentation::sample4
34};
35
36//////////////////////////////////////////////////////////////////////
37// Construction/Destruction
38//////////////////////////////////////////////////////////////////////
39
40Approx_Presentation::Approx_Presentation()
41{
42 myIndex = 0;
43 myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
44 setName ("Approximation of curves and surfaces.");
45}
46
47//////////////////////////////////////////////////////////////////////
48// Sample execution
49//////////////////////////////////////////////////////////////////////
50
51void Approx_Presentation::DoSample()
52{
53 getAISContext()->EraseAll();
54 if (myIndex >=0 && myIndex < myNbSamples)
55 (this->*SampleFuncs[myIndex])();
56}
57
58//////////////////////////////////////////////////////////////////////
59// Sample functions
60//////////////////////////////////////////////////////////////////////
61//================================================================
62
63//defining the data of BSpline curves and surfaces:
64
65static Standard_Real Tol [] =
66{
67 0.5*SCALE,
68 0.7*SCALE,
69 0.4*SCALE,
70 1*SCALE
71};
72
73static Standard_Integer DegMin [] =
74{
75 2,3,5,7
76};
77
78static Standard_Integer DegMax [] =
79{
80 7,9,10,12
81};
82
83static GeomAbs_Shape Continuity [] =
84{
85 GeomAbs_C2, GeomAbs_C1, GeomAbs_C2, GeomAbs_C3
86};
87
88
89//================================================================
90// Function : Comment
91// Purpose :
92//================================================================
93
94static TCollection_AsciiString Comment(Standard_Real Step,
95 Standard_Integer Upper,
96 Standard_Integer DegMin,
97 Standard_Integer DegMax,
98 Standard_Integer Indicator,
99 Standard_Real Tol)
100{
101 TCollection_AsciiString aText;
102 aText = (
103 "/////////////////////////////////////////////////////////////////" EOL
104 "// Approximation of surface." EOL
105 "// Building a BSpline surface which approximates a set of points." EOL
106 "/////////////////////////////////////////////////////////////////" EOL EOL
107
108 "// creating a set of points:" EOL
109 );
110
111 aText += "Standard_Real Step = ";
112 aText += TCollection_AsciiString(Step);
113 aText += ";" EOL;
114 aText += "Standard_Integer Upper = ";
115 aText += TCollection_AsciiString(Upper);
116 aText += ";" EOL EOL;
117 aText += (
118 " //a set of X and Y coordinates:" EOL
119 " Standard_Real aXStep = Step , aYStep = Step ;" EOL
120 " Standard_Real aX0 = -300, aY0 = -200;" EOL
121 " //Z coordinates:" EOL
122 " TColStd_Array2OfReal aZPoints( 1, Upper , 1, Upper );" EOL EOL
123
124 "// initializing array of Z coordinates:" EOL
125 "// aZPoints(1,1) = -2;" EOL
126 "// aZPoints(1,2) = 3;" EOL
127 "// ..." EOL EOL
128
129 "//creating a approximate BSpline surface:" EOL
130 );
131
132 aText += "Parameters of surface:" EOL ;
133 aText += "DegMin = ";
134 aText += TCollection_AsciiString(DegMin);
135 aText += ";" EOL;
136 aText += "DegMax = ";
137 aText += TCollection_AsciiString(DegMax);
138 aText += ";" EOL;
139 aText += "Continuity = " ;
140
141 if( Indicator == 2 )
142 aText += "GeomAbs_C1";
143 if( Indicator == 3 )
144 aText += "GeomAbs_C2";
145 if( Indicator == 4 )
146 aText += "GeomAbs_C3";
147 aText += ";" EOL;
148 aText += "Tolerance = ";
149 aText += TCollection_AsciiString(Tol/SCALE);
150 aText += ";" EOL EOL ;
151
152 aText += (
153 "GeomAPI_PointsToBSplineSurface aPTBS;" EOL
154 "aPTBS.Init(aZPoints,aX0,aXStep,aY0,aYStep," EOL
155 " DegMin,DegMax,Continuity,Tolerance);" EOL
92efcf78 156 "Handle(Geom_BSplineSurface) aSurface = aPTBS.Surface();" EOL EOL EOL
7fd59977 157 );
158
159 return aText;
160
161}
162
163//================================================================
164// Function : Approx_Presentation::CreateBSplineSurface
165// Purpose :
166//================================================================
167
92efcf78 168Handle(Geom_BSplineSurface) Approx_Presentation::CreateBSplineSurface(TColStd_Array2OfReal& aZPoints,
7fd59977 169 Standard_Real theXStep,
170 Standard_Real theYStep,
171 Standard_Integer Count)
172{
173 Standard_Real aX0 = -300, aY0 = -200;
174
175 GeomAPI_PointsToBSplineSurface aPTBS;
176 aPTBS.Init(aZPoints,aX0,theXStep,aY0,theYStep,
177 DegMin[Count],DegMax[Count],Continuity[Count],Tol[Count]);
92efcf78 178 Handle(Geom_BSplineSurface) aSurface = aPTBS.Surface();
7fd59977 179
180 return aSurface;
181}
182
183
184//================================================================
185// Function : Approx_Presentation::DrawModifyBSplineSurface
186// Purpose :
187//================================================================
188
189Standard_Boolean Approx_Presentation::DrawModifyBSplineSurface(TColStd_Array2OfReal& aZPoints,
190 Standard_Real theXStep,
191 Standard_Real theYStep,
192 Standard_Integer theIndexX,
193 Standard_Integer theIndexY,
194 Standard_Real theDeflection,
92efcf78 195 Handle(AIS_InteractiveObject)& aMovePnt,
196 Handle(AIS_InteractiveObject)& aObj,
7fd59977 197 Standard_Integer Count)
198
199{
92efcf78 200 Handle(AIS_InteractiveObject) auxObj;
7fd59977 201 Standard_Real aX0 = -300, aY0 = -200;
202 Standard_Real aLastZ = aZPoints(theIndexX,theIndexY);
203 aZPoints(theIndexX,theIndexY) += 100*theDeflection;
204 Standard_Real aCurrentX = aX0 + theXStep*(theIndexX-1),
205 aCurrentY = aY0 + theYStep*(theIndexY-1);
206
207 getAISContext()->Erase(aMovePnt);
208 aMovePnt = drawPoint(gp_Pnt(aCurrentX,aCurrentY,aZPoints(theIndexX,theIndexY)));
209
210 if(WAIT_A_LITTLE) return Standard_False;
211
212
213 GeomAPI_PointsToBSplineSurface aPTBS;
214 aPTBS.Init(aZPoints,aX0,theXStep,aY0,theYStep,
215 DegMin[Count],DegMax[Count],Continuity[Count],Tol[Count]);
216
217 aZPoints(theIndexX,theIndexY) = aLastZ;
218
92efcf78 219 Handle(Geom_BSplineSurface) aSurface = aPTBS.Surface();
7fd59977 220
221 auxObj = drawSurface(aSurface);
222 getAISContext()->Erase(aObj);
223 aObj = auxObj;
224 if(WAIT_A_SECOND) return Standard_False;
225
226 return Standard_True;
227}
228
229
230
231
232//================================================================
233// Function : Approx_Presentation::sample1
234// Purpose :
235//================================================================
236
237void Approx_Presentation::sample1()
238{
239 Standard_Integer Count = 0;
240
241 TCollection_AsciiString aText = (
242 "//////////////////////////////////////////////////////////////" EOL
243 "// Approximation of curve." EOL
244 "// Building a BSpline curve which approximates a set of points." EOL
245 "//////////////////////////////////////////////////////////////" EOL EOL
246
247 "// creating a set of points to approximate," EOL
248 "// nPoint is the number of points:" EOL
249 "Standard_Integer nPoint = 20;" EOL
250 "TColgp_Array1OfPnt aCurvePoint (1, nPoint);" EOL EOL
251
252 "// initializing this array of points:" EOL
253 "// aCurvePoint(1,1) = gp_Pnt(-6,1,0);" EOL
254 "// aCurvePoint(1,2) = gp_Pnt(-5,1.5,0);" EOL
255 "// ..." EOL EOL
256
257 "//creating an empty approximation algorithm:" EOL
258 "GeomAPI_PointsToBSpline aPTB;" EOL EOL
259
260 "//creating a approximate BSpline curve:" EOL
261 );
262
263 aText += "//parameters of curve:" EOL ;
264 aText += "DegMin = ";
265 aText += TCollection_AsciiString(DegMin[Count]);
266 aText += ";" EOL;
267 aText += "DegMax = ";
268 aText += TCollection_AsciiString(DegMax[Count]);
269 aText += ";" EOL;
270 aText += "Continuity = GeomAbs_C2" ;
271 aText += ";" EOL;
272 aText += "Tolerance = ";
273 aText += TCollection_AsciiString(Tol[Count]/SCALE);
274 aText += ";" EOL EOL ;
275
276 aText += (
277 "aPTB.Init(aCurvePoint,DegMin,DegMax,Continuity,Tolerance);" EOL
92efcf78 278 "Handle(Geom_BSplineCurve) aCurve = aPTB.Curve();" EOL
7fd59977 279 );
280
281 setResultTitle("Creating approximations of curves");
282 setResultText(aText.ToCString());
283
284 getAISContext()->EraseAll();
285
286 Standard_Real aCoords[][3] = {
287 {-6,1,0},{-5,1.5,0},{-4,2,0},{-3.5,3,0},{-3,2.7,0},{-2,2.5,0},{-1.5,1,0},{-1,0.5,0},
288 {0,0,0},{1,0.3,0},{2,1,0},{3,1.5,0},{4,2.3,0},{5,2.7,0},{5.5,3.2,0},{6,2.5,0},
289 {7,2,0},{7.5,1,0},{8,0,0},{8.5,-1,0}
290 };
291
292 Standard_Integer nPoint = sizeof(aCoords)/(sizeof(Standard_Real)*3);
293
294 TColgp_Array1OfPnt aCurvePoint (1, nPoint);
295
296 GeomAPI_PointsToBSpline aPTB;
297
92efcf78 298 Handle(AIS_InteractiveObject) aIndexPnt [2],aObj;
7fd59977 299 Standard_Integer aIndex[2] = {9,13};
300 Standard_Real aDeflection[2] = {-1.5,2};
301
302 for (Standard_Integer i=0; i < nPoint; i++)
303 {
304 aCurvePoint(i+1) = gp_Pnt (aCoords[i][0]*SCALE-100, aCoords[i][1]*SCALE-100, aCoords[i][2]*SCALE);
305 if( i+1 == aIndex[0])
306 aIndexPnt[0] = drawPoint(aCurvePoint(aIndex[0]));
307 if( i+1 == aIndex[1])
308 aIndexPnt[1] = drawPoint(aCurvePoint(aIndex[1]));
309 if( i+1 != aIndex[0] && i+1 != aIndex[1])
310 drawPoint(aCurvePoint(i+1));
311 }
312
313 if(WAIT_A_LITTLE) return;
314
315 aPTB.Init(aCurvePoint,DegMin[Count],DegMax[Count],Continuity[Count],Tol[Count]);
92efcf78 316 Handle(Geom_BSplineCurve) aCurve = aPTB.Curve();
7fd59977 317 aObj = drawCurve(aCurve);
318
319 for( i = 0 ; i < 2 ; i++)
320 {
321 if (WAIT_A_SECOND) return;
322 getAISContext()->Erase(aIndexPnt[i]);
323
324 aCurvePoint(aIndex[i]) = gp_Pnt(aCurvePoint(aIndex[i]).X()-SCALE*aDeflection[i],
325 aCurvePoint(aIndex[i]).Y()+SCALE*aDeflection[i],
326 aCurvePoint(aIndex[i]).Z());
327
328
329 aIndexPnt[i] = drawPoint(aCurvePoint(aIndex[i]));
330 aPTB.Init(aCurvePoint,DegMin[Count],DegMax[Count],Continuity[Count],Tol[Count]);
331 aCurve = aPTB.Curve();
332
333 if (WAIT_A_LITTLE) return;
334 getAISContext()->Erase(aObj);
335 aObj = drawCurve(aCurve);
336 }
337
338}
339
340
341//================================================================
342// Function : Approx_Presentation::sample2
343// Purpose :
344//================================================================
345
346void Approx_Presentation::sample2()
347{
348 setResultTitle("Creating approximations of surfaces");
349
350 Standard_Integer Count = 1;
351 Standard_Real aZCoords [] =
352 {
353 {-1},{0},{0},{1},{0},{-1},{-1},{0},{0},{-1.5},{-2.5},{0},{1},{-2},{-3},{0}
354 };
355
356 Standard_Real aXStep = 175, aYStep = 175;
357 Standard_Real aX0 = -300, aY0 = -200;
358
359 Standard_Integer anUpper = 4;
360 TColStd_Array2OfReal aZPoints(1,anUpper,1,anUpper);
361
362 Standard_Integer aIndexX[] = { 4, 3, 2, 2, 1 };
363 Standard_Integer aIndexY[] = { 4, 3, 2, 3, 4 };
364 Standard_Real aDeflection[] = { 1.5, 2.5, 1.5, 1.5, -1 };
365
366 Standard_Integer aNumOfIndexPnt = sizeof(aIndexX)/sizeof(Standard_Integer);
367
368
369 TColgp_Array2OfPnt aPnt(1,4,1,4);
92efcf78 370 Handle(AIS_InteractiveObject) aShowPnt[4][4],aObj,aMovePnt;
7fd59977 371
372 Standard_Integer aColLength = aZPoints.ColLength();
373 Standard_Integer aRowLength = aZPoints.RowLength();
374 Standard_Integer aIndex = -1;
375
376 for(Standard_Integer i = 0 ; i < aRowLength ; i++)
377 {
378 for(Standard_Integer j = 0; j < aColLength ; j++)
379 {
380 aIndex++;
381 aZPoints(i+1,j+1) = aZCoords[aIndex];
382 }
383 }
384
385 Standard_Real auxY0,auxX0 = aX0 - aXStep;
386
387 for( i = 0 ; i < aColLength ; i++)
388 {
389 auxX0 += aXStep;
390 auxY0 = aY0 - aYStep;
391
392 for(Standard_Integer j = 0 ; j < aRowLength ; j++)
393 {
394 aZPoints(i+1,j+1) *=SCALE;
395 auxY0 += aYStep;
396 aPnt(i+1,j+1) = gp_Pnt (auxX0,auxY0,aZPoints(i+1,j+1));
397 aShowPnt[i][j] = drawPoint(aPnt(i+1,j+1));
398 }
399 }
400
401 if(WAIT_A_LITTLE) return ;
402
403 TCollection_AsciiString aText;
404 aText = Comment(aXStep,anUpper,DegMin[Count],DegMax[Count],Count+1,Tol[Count]);
405 setResultText(aText.ToCString());
406
92efcf78 407 Handle(Geom_BSplineSurface) aSurface = CreateBSplineSurface(aZPoints,aXStep,aYStep,Count);
7fd59977 408 aObj = drawSurface(aSurface);
409 Standard_Boolean aBool;
410 if(WAIT_A_LITTLE) return ;
411
412 for( i = 0 ; i < aNumOfIndexPnt ; i++)
413 {
414 aMovePnt = aShowPnt[aIndexX[i]-1][aIndexY[i]-1];
415 aBool = DrawModifyBSplineSurface(aZPoints,aXStep,aYStep,aIndexX[i],aIndexY[i],
416 aDeflection[i],aMovePnt,aObj,Count);
417 if(!aBool) return;
418
419 if( i < aNumOfIndexPnt - 1)
420 {
421 drawPoint(aPnt(aIndexX[i],aIndexY[i]));
422 getAISContext()->Erase(aMovePnt);
423 }
424 }
425
426}
427
428
429//================================================================
430// Function : Approx_Presentation::sample3
431// Purpose :
432//================================================================
433
434void Approx_Presentation::sample3()
435{
436 setResultTitle("Creating approximations of surfaces");
437
438 Standard_Integer Count = 2;
439 Standard_Real aZCoords [] =
440 {
441 {-3},{-2.3},{-3},{-0.5},{-1},{-1},{-1},{-1},{0},{0},{0},{0},{1},{-1},{-1},{0}
442 };
443
444 Standard_Integer anUpper = 4;
445 TColStd_Array2OfReal aZPoints(1,anUpper,1,anUpper);
446
447 Standard_Integer aIndexX[] = {1, 2, 3, 4};
448 Standard_Integer aIndexY[] = {1, 2, 3, 4};
449 Standard_Real aDeflection[] = {1.5, 2, 1, 1.5};
450
451 Standard_Integer aNumOfIndexPnt = sizeof(aIndexX)/sizeof(Standard_Integer);
452
453
454 TColgp_Array2OfPnt aPnt(1,4,1,4);
92efcf78 455 Handle(AIS_InteractiveObject) aShowPnt[4][4],aObj,aMovePnt;
7fd59977 456
457 Standard_Integer aColLength = aZPoints.ColLength();
458 Standard_Integer aRowLength = aZPoints.RowLength();
459 Standard_Integer aIndex = -1;
460
461 for(Standard_Integer i = 0 ; i < aRowLength ; i++)
462 {
463 for(Standard_Integer j = 0; j < aColLength ; j++)
464 {
465 aIndex++;
466 aZPoints(i+1,j+1) = aZCoords[aIndex];
467 }
468 }
469
470 Standard_Real aXStep = 175, aYStep = 175;
471 Standard_Real aX0 = -300, aY0 = -200;
472 Standard_Real auxY0,auxX0 = aX0 - aXStep;
473
474 for( i = 0 ; i < aColLength ; i++)
475 {
476 auxX0 += aXStep;
477 auxY0 = aY0 - aYStep;
478
479 for(Standard_Integer j = 0 ; j < aRowLength ; j++)
480 {
481 aZPoints(i+1,j+1) *=SCALE;
482 auxY0 += aYStep;
483 aPnt(i+1,j+1) = gp_Pnt (auxX0,auxY0,aZPoints(i+1,j+1));
484 aShowPnt[i][j] = drawPoint(aPnt(i+1,j+1));
485 }
486 }
487
488 if(WAIT_A_LITTLE) return ;
489
490 TCollection_AsciiString aText;
491 aText += Comment(aXStep,anUpper,DegMin[Count],DegMax[Count],Count+1,Tol[Count]);
492 setResultText(aText.ToCString());
493
92efcf78 494 Handle(Geom_BSplineSurface) aSurface = CreateBSplineSurface(aZPoints,aXStep,aYStep,Count);
7fd59977 495 aObj = drawSurface(aSurface);
496 Standard_Boolean aBool;
497 if(WAIT_A_LITTLE) return ;
498
499 for( i = 0 ; i < aNumOfIndexPnt ; i++)
500 {
501 aMovePnt = aShowPnt[aIndexX[i]-1][aIndexY[i]-1];
502 aBool = DrawModifyBSplineSurface(aZPoints,aXStep,aYStep,aIndexX[i],aIndexY[i],
503 aDeflection[i],aMovePnt,aObj,Count);
504 if(!aBool) return;
505
506 if( i < aNumOfIndexPnt - 1)
507 {
508 drawPoint(aPnt(aIndexX[i],aIndexY[i]));
509 getAISContext()->Erase(aMovePnt);
510 }
511 }
512
513}
514
515
516//================================================================
517// Function : Approx_Presentation::sample4
518// Purpose :
519//================================================================
520
521void Approx_Presentation::sample4()
522{
523 setResultTitle("Creating approximations of surfaces");
524
525 Standard_Integer Count = 3;
526 Standard_Real aZCoords [] =
527 {
528 {-1.5},{0.5},{1},{0.5},{-1.5},{0},{-0.5},{0},{-0.5},{0},{1},{-0.5},{0},{-0.5},{1},{0},{-0.5},
529 {0},{-0.5},{0},{-1.5},{0.5},{1},{0.5},{-1.5}
530 };
531
532 Standard_Integer anUpper = 5;
533 TColStd_Array2OfReal aZPoints(1,anUpper,1,anUpper);
534
535 Standard_Integer aIndexX[] = { 1, 3, 5 };
536 Standard_Integer aIndexY[] = { 3, 3, 3 };
537 Standard_Real aDeflection[] = {-2, 1, -2 };
538
539 Standard_Integer aNumOfIndexPnt = sizeof(aIndexX)/sizeof(Standard_Integer);
540
541
542 TColgp_Array2OfPnt aPnt(1,5,1,5);
92efcf78 543 Handle(AIS_InteractiveObject) aShowPnt[5][5],aObj,aMovePnt;
7fd59977 544
545 Standard_Integer aColLength = aZPoints.ColLength();
546 Standard_Integer aRowLength = aZPoints.RowLength();
547 Standard_Integer aIndex = -1;
548
549 for(Standard_Integer i = 0 ; i < aRowLength ; i++)
550 {
551 for(Standard_Integer j = 0; j < aColLength ; j++)
552 {
553 aIndex++;
554 aZPoints(i+1,j+1) = aZCoords[aIndex];
555 }
556 }
557
558 Standard_Real aXStep = 140, aYStep = 140;
559 Standard_Real aX0 = -300, aY0 = -200;
560 Standard_Real auxY0,auxX0 = aX0 - aXStep;
561
562 for( i = 0 ; i < aColLength ; i++)
563 {
564 auxX0 += aXStep;
565 auxY0 = aY0 - aYStep;
566
567 for(Standard_Integer j = 0 ; j < aRowLength ; j++)
568 {
569 aZPoints(i+1,j+1) *=SCALE;
570 auxY0 += aYStep;
571 aPnt(i+1,j+1) = gp_Pnt (auxX0,auxY0,aZPoints(i+1,j+1));
572 aShowPnt[i][j] = drawPoint(aPnt(i+1,j+1));
573 }
574 }
575
576 if(WAIT_A_LITTLE) return ;
577
578 TCollection_AsciiString aText;
579 aText += Comment(aXStep,anUpper,DegMin[Count],DegMax[Count],Count+1,Tol[Count]);
580 setResultText(aText.ToCString());
581
92efcf78 582 Handle(Geom_BSplineSurface) aSurface = CreateBSplineSurface(aZPoints,aXStep,aYStep,Count);
7fd59977 583 aObj = drawSurface(aSurface);
584 Standard_Boolean aBool;
585 if(WAIT_A_LITTLE) return ;
586
587 for( i = 0 ; i < aNumOfIndexPnt ; i++)
588 {
589 aMovePnt = aShowPnt[aIndexX[i]-1][aIndexY[i]-1];
590 aBool = DrawModifyBSplineSurface(aZPoints,aXStep,aYStep,aIndexX[i],aIndexY[i],
591 aDeflection[i],aMovePnt,aObj,Count);
592 if(!aBool) return;
593
594 if( i < aNumOfIndexPnt - 1)
595 {
596 drawPoint(aPnt(aIndexX[i],aIndexY[i]));
597 getAISContext()->Erase(aMovePnt);
598 }
599 }
600
601}
602
603