18e20bb3bd66ec6eadabcd725b567b5ddfeeb84b
[occt.git] / samples / mfc / standard / 10_Convert / src / Convert_Presentation.cpp
1 // Convert_Presentation.cpp: implementation of the Convert_Presentation class.
2 // Conversion of elementary geometry to BSpline curves and surfaces
3 //////////////////////////////////////////////////////////////////////
4
5 #include "stdafx.h"
6 #include "Convert_Presentation.h"
7
8 #include <Quantity_Color.hxx>
9
10 #include <gp_Dir.hxx>
11 #include <gp_Ax2.hxx>
12
13 #include <TColgp_Array1OfPnt.hxx>
14 #include <TColStd_Array1OfReal.hxx>
15 #include <TColgp_Array2OfPnt.hxx>
16 #include <TColStd_Array2OfReal.hxx>
17
18 #include <GeomConvert.hxx>
19
20 #include <Geom_BezierSurface.hxx>
21 #include <Geom_Circle.hxx>
22 #include <Geom_Ellipse.hxx>
23 #include <Geom_BezierCurve.hxx>
24 #include <Geom_BSplineCurve.hxx>
25 #include <Geom_SphericalSurface.hxx>
26 #include <Geom_CylindricalSurface.hxx>
27 #include <Geom_RectangularTrimmedSurface.hxx>
28 #include <Geom_SurfaceOfRevolution.hxx>
29 #include <Geom_ToroidalSurface.hxx>
30 #include <Geom_ConicalSurface.hxx>
31
32
33 // Initialization of global variable with an instance of this class
34 OCCDemo_Presentation* OCCDemo_Presentation::Current = new Convert_Presentation;
35
36 // Initialization of array of samples
37 const Convert_Presentation::PSampleFuncType Convert_Presentation::SampleFuncs[] =
38 {
39   &Convert_Presentation::sampleCircle,
40   &Convert_Presentation::sampleEllipse,
41   &Convert_Presentation::sampleBezier,
42   &Convert_Presentation::sampleBezierSurface,
43   &Convert_Presentation::sampleCylindricalSurface,
44   &Convert_Presentation::sampleRevolSurface,
45   &Convert_Presentation::sampleToroidalSurface,
46   &Convert_Presentation::sampleConicalSurface,
47   &Convert_Presentation::sampleSphericalSurface
48 };
49
50 // Colors of objects
51 static const Quantity_Color CurveColor       (1,1,0, Quantity_TOC_RGB);      // yellow
52 static const Quantity_Color SurfaceColor     (1,1,0, Quantity_TOC_RGB);      // yellow
53 static const Quantity_Color BSplineColor     (1,0.647,0, Quantity_TOC_RGB);  // orange
54 static const Quantity_Color BSplineSurfaceColor (0,0,1, Quantity_TOC_RGB);   // blue
55
56 #ifdef WNT
57  #define EOL "\r\n"
58 #else
59  #define EOL "\n"
60 #endif
61
62 //////////////////////////////////////////////////////////////////////
63 // Construction/Destruction
64 //////////////////////////////////////////////////////////////////////
65
66 Convert_Presentation::Convert_Presentation()
67 {
68   setName ("Conversion to BSpline curves and surfaces");
69   myIndex = 0;
70   myNbFuncs = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
71   myNbSamples = myNbFuncs;
72   FitMode = true;
73 }
74
75 //////////////////////////////////////////////////////////////////////
76 // Sample execution
77 //////////////////////////////////////////////////////////////////////
78
79 void Convert_Presentation::DoSample()
80 {
81         ((COCCDemoApp*) AfxGetApp())->SetSampleName (L"Convert");
82   ((COCCDemoApp*) AfxGetApp())->SetSamplePath (L"..\\..\\10_Convert");
83         getAISContext()->EraseAll();
84         if (myIndex >=0 && myIndex < myNbFuncs)
85     (this->*SampleFuncs[myIndex])();
86 }
87
88 //================================================================
89 // Function : Convert_Presentation::drawSurfaceAndItsBSpline
90 // Purpose  : 
91 //================================================================
92 void Convert_Presentation::drawSurfaceAndItsBSpline(Handle_Geom_Surface theSurface, 
93                                                     const Standard_CString theName, 
94                                                     TCollection_AsciiString& theText)
95 {
96   TCollection_AsciiString aTitle ("Converting ");
97   aTitle += theName;
98   aTitle += " to BSpline surface";
99
100   theText += EOL
101     "  Handle_Geom_BSplineSurface aBSplineSurface = " EOL
102     "    GeomConvert::SurfaceToBSplineSurface(aSurface);" EOL;
103
104   setResultTitle (aTitle.ToCString());
105   setResultText (theText.ToCString());
106
107   drawSurface (theSurface, SurfaceColor);
108
109   if (WAIT_A_LITTLE) return;
110
111   Handle_Geom_BSplineSurface aBSplineSurface = GeomConvert::SurfaceToBSplineSurface(theSurface);
112
113   _ASSERTE(!aBSplineSurface.IsNull());
114
115   drawSurface (aBSplineSurface, BSplineSurfaceColor);
116 }
117
118 //================================================================
119 // Function : Convert_Presentation::drawCurveAndItsBSpline
120 // Purpose  : 
121 //================================================================
122 void Convert_Presentation::drawCurveAndItsBSpline(Handle_Geom_Curve theCurve, 
123                                                   const Standard_CString theName, 
124                                                   TCollection_AsciiString& theText)
125 {
126   TCollection_AsciiString aTitle ("Converting ");
127   aTitle += theName;
128   aTitle += " to BSpline curve";
129
130   theText += EOL
131     "  Handle_Geom_BSplineCurve aBSpline = " EOL
132     "    GeomConvert::CurveToBSplineCurve(aCurve);" EOL;
133
134   setResultTitle (aTitle.ToCString());
135   setResultText (theText.ToCString());
136   
137   drawCurve (theCurve, CurveColor);
138   
139   if (WAIT_A_LITTLE) return;
140
141   Handle_Geom_BSplineCurve aBSpline = GeomConvert::CurveToBSplineCurve(theCurve);
142
143   drawCurve (aBSpline, BSplineColor);
144 }
145
146
147 //////////////////////////////////////////////////////////////////////
148 // Sample functions
149 //////////////////////////////////////////////////////////////////////
150
151 //================================================================
152 // Function : Convert_Presentation::sampleCircle
153 // Purpose  : 
154 //================================================================
155 void Convert_Presentation::sampleCircle()
156 {
157   gp_Pnt aOrigin (0,0,0);
158   gp_Dir aDir (1,0,0);
159   gp_Ax2 aAxis (aOrigin, aDir);
160   Standard_Real aRadius = 300;
161   Handle_Geom_Circle aCurve = new Geom_Circle (aAxis, aRadius);
162
163   TCollection_AsciiString aText (
164     "  gp_Pnt aOrigin (0,0,0);" EOL
165     "  gp_Dir aDir (1,0,0);" EOL
166     "  gp_Ax2 aAxis (aOrigin, aDir);" EOL
167     "  Standard_Real aRadius = 300;" EOL
168     "  Handle(Geom_Circle) aCurve = new Geom_Circle (aAxis, aRadius);" EOL
169     );
170   drawCurveAndItsBSpline (aCurve, "Circle", aText);
171 }
172
173 //================================================================
174 // Function : Convert_Presentation::sampleEllipse
175 // Purpose  : 
176 //================================================================
177 void Convert_Presentation::sampleEllipse()
178 {
179   gp_Pnt aOrigin (0,0,0);
180   gp_Dir aDir (1,0,0);
181   gp_Ax2 aMajorAxis (aOrigin, aDir);
182   Standard_Real aMajorRadius = 300;
183   Standard_Real aMinorRadius = 150;
184   Handle(Geom_Ellipse) aCurve = 
185     new Geom_Ellipse (aMajorAxis, aMajorRadius, aMinorRadius);
186
187   TCollection_AsciiString aText (
188     "  gp_Pnt aOrigin (0,0,0);" EOL
189     "  gp_Dir aDir (1,0,0);" EOL
190     "  gp_Ax2 aAxis (aOrigin, aDir);" EOL
191     "  Standard_Real aMajorRadius = 300;" EOL
192     "  Standard_Real aMinorRadius = 150;" EOL
193     "  Handle(Geom_Ellipse) aCurve = " EOL
194     "    new Geom_Ellipse (aAxis, aMajorRadius, aMinorRadius);" EOL
195     );
196   drawCurveAndItsBSpline (aCurve, "Ellipse", aText);
197 }
198
199 //================================================================
200 // Function : Convert_Presentation::sampleBezier
201 // Purpose  : 
202 //================================================================
203 void Convert_Presentation::sampleBezier()
204 {
205   TCollection_AsciiString aText (
206     "  Standard_Real aPolesCoords[][3] = {" EOL
207     "    {0,0,0},{0,1,0},{1,1,0},{1,2,0},{2,2,0},{2,1,0},{3,1,0},{3,0,0},{2,0,0},{2,-1,0}," EOL
208     "    {3,-1,0},{3,-2,0},{4,-2,0},{4,-1,0},{5,-1,0},{5,0,0},{6,0,0},{6,-1,0},{7,-1,0}," EOL
209     "    {7,0,0},{8,0,0},{8,1,0}" EOL
210     "  };" EOL
211     "  TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*2));" EOL
212     " " EOL
213     "  for (Standard_Integer i=1; i <= aPoles.Upper(); i++)" EOL
214     "    aPoles(i) = gp_Pnt (aPolesCoords[i-1][0]*100, " EOL
215     "                        aPolesCoords[i-1][1]*100, " EOL
216     "                        aPolesCoords[i-1][2]*100);" EOL
217     "  " EOL
218     "  Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);" EOL
219     );
220
221   Standard_Real aPolesCoords[][3] = {
222     {0,0,0},{0,1,0},{1,1,0},{1,2,0},{2,2,0},{2,1,0},{3,1,0},{3,0,0},{2,0,0},{2,-1,0},
223     {3,-1,0},{3,-2,0},{4,-2,0},{4,-1,0},{5,-1,0},{5,0,0},{6,0,0},{6,-1,0},{7,-1,0},
224     {7,0,0},{8,0,0},{8,1,0}
225   };
226   TColgp_Array1OfPnt aPoles (1, sizeof(aPolesCoords)/(sizeof(Standard_Real)*3));
227  
228   for (Standard_Integer i=1; i <= aPoles.Upper(); i++)
229     aPoles(i) = gp_Pnt (aPolesCoords[i-1][0]*150-500, 
230                         aPolesCoords[i-1][1]*150, 
231                         aPolesCoords[i-1][2]*150);
232   
233   Handle(Geom_BezierCurve) aCurve = new Geom_BezierCurve (aPoles);
234
235   drawCurveAndItsBSpline (aCurve, "BezierCurve", aText);
236 }
237
238 //================================================================
239 // Function : Convert_Presentation::sampleBezierSurface
240 // Purpose  : 
241 //================================================================
242 void Convert_Presentation::sampleBezierSurface()
243 {
244   getAISContext()->EraseAll();
245
246   Standard_CString aName = "BezierSurface";
247   // Create a BezierSurface
248   TColgp_Array2OfPnt aPoles(1,2,1,4); // 8 points
249   TColStd_Array2OfReal aWeights(1,2,1,4);
250   // initializing array of points
251   aPoles.SetValue(1,1,gp_Pnt(0,10,0));     aPoles.SetValue(1,2,gp_Pnt(3.3,6.6,3));
252   aPoles.SetValue(1,3,gp_Pnt(6.6,6.6,-3)); aPoles.SetValue(1,4,gp_Pnt(10,10,0));
253   aPoles.SetValue(2,1,gp_Pnt(0,0,0));      aPoles.SetValue(2,2,gp_Pnt(3.3,3.3,-3));
254   aPoles.SetValue(2,3,gp_Pnt(6.6,3.3,3));  aPoles.SetValue(2,4,gp_Pnt(10,0,0));  
255   // scaling poles
256   for (Standard_Integer i=1; i <= aPoles.ColLength(); i++)
257     for (Standard_Integer j=1; j <= aPoles.RowLength(); j++)
258       aPoles(i,j).ChangeCoord() = aPoles(i,j).Coord() * 100 + gp_XYZ(-500,-500,0);
259   //initializing array of weights
260   aWeights.SetValue(1,1,1); aWeights.SetValue(1,2,3);
261   aWeights.SetValue(1,3,9); aWeights.SetValue(1,4,1);
262   aWeights.SetValue(2,1,1); aWeights.SetValue(2,2,2);
263   aWeights.SetValue(2,3,5); aWeights.SetValue(2,4,1);
264   Handle(Geom_BezierSurface) aSurface =
265     new Geom_BezierSurface(aPoles, aWeights);
266
267   TCollection_AsciiString aText (
268     "  // Create a BezierSurface" EOL
269     "  TColgp_Array2OfPnt aPoles(1,2,1,4); // 8 points" EOL
270     "  TColStd_Array2OfReal aWeights(1,2,1,4);" EOL
271     "  // initializing array of points" EOL
272     "  aPoles.SetValue(1,1,gp_Pnt(0,10,0));     aPoles.SetValue(1,2,gp_Pnt(3.3,6.6,3));" EOL
273     "  aPoles.SetValue(1,3,gp_Pnt(6.6,6.6,-3)); aPoles.SetValue(1,4,gp_Pnt(10,10,0));" EOL
274     "  aPoles.SetValue(2,1,gp_Pnt(0,0,0));      aPoles.SetValue(2,2,gp_Pnt(3.3,3.3,-3));" EOL
275     "  aPoles.SetValue(2,3,gp_Pnt(6.6,3.3,3));  aPoles.SetValue(2,4,gp_Pnt(10,0,0));  " EOL
276     "  // scaling poles" EOL
277     "  for (Standard_Integer i=1; i <= aPoles.ColLength(); i++)" EOL
278     "    for (Standard_Integer j=1; j <= aPoles.RowLength(); j++)" EOL
279     "      aPoles(i,j).ChangeCoord() = aPoles(i,j).Coord() * 100 + gp_XYZ(-500,-500,0);" EOL
280     "  //initializing array of weights" EOL
281     "  aWeights.SetValue(1,1,1); aWeights.SetValue(1,2,3);" EOL
282     "  aWeights.SetValue(1,3,9); aWeights.SetValue(1,4,1);" EOL
283     "  aWeights.SetValue(2,1,1); aWeights.SetValue(2,2,2);" EOL
284     "  aWeights.SetValue(2,3,5); aWeights.SetValue(2,4,1);" EOL
285     "  Handle(Geom_BezierSurface) aSurface =" EOL
286     "    new Geom_BezierSurface(aPoles, aWeights);" EOL
287     );
288
289   drawSurfaceAndItsBSpline (aSurface, aName, aText);
290 }
291
292 //================================================================
293 // Function : OCCDemo_Presentation::sampleCylindricalSurface
294 // Purpose  : 
295 //================================================================
296 void Convert_Presentation::sampleCylindricalSurface()
297 {
298   getAISContext()->EraseAll();
299
300   Standard_CString aName = "Cylindrical Surface";
301   TCollection_AsciiString aText (
302     "  // creating an axis parallel to Y axis" EOL
303     "  gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));" EOL EOL
304
305     "  // creating a cylindrical surface along anAx with radius = 100" EOL
306     "  Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);" EOL EOL
307
308     "  // only finit surfaces can be converted to BSpline surfaces, " EOL
309     "  // cylindrical surface is infinite, it must be trimmed" EOL
310     "  Handle(Geom_RectangularTrimmedSurface) aSurface = " EOL
311     "    new Geom_RectangularTrimmedSurface(aCylSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);" EOL);
312
313   // creating an axis parallel to Y axis
314   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
315
316   // creating a cylindrical surface along anAx with radius = 4
317   Handle(Geom_CylindricalSurface) aCylSurface = new Geom_CylindricalSurface(anAx, 100);
318
319   // only finit surfaces can be converted to BSpline surfaces, 
320   // cylindrical surface is infinite, it must be trimmed
321   Handle(Geom_RectangularTrimmedSurface) aSurface = 
322     new Geom_RectangularTrimmedSurface(aCylSurface, 0, 2*M_PI, -1000, 1000, Standard_True, Standard_True);
323   
324   drawSurfaceAndItsBSpline(aSurface, aName, aText);
325 }
326
327 //================================================================
328 // Function : OCCDemo_Presentation::sampleRevolSurface
329 // Purpose  : 
330 //================================================================
331 void Convert_Presentation::sampleRevolSurface()
332 {
333   FitMode=false;
334   ResetView();
335   TranslateView(-176.84682, -102.12892);
336   SetViewScale(0.69326);
337
338   getAISContext()->EraseAll();
339
340   Standard_CString aName = "Surface of revolution";
341   TCollection_AsciiString aText (
342     "  // creating a curve for revolution.  Let it be a Bezier curve." EOL
343     "  Handle(Geom_BezierCurve) aBezierCurve;" EOL EOL
344
345     "  // array of the bezier curve poles" EOL
346     "  TColgp_Array1OfPnt aPoles(1,4);" EOL
347     "  // array of the poles' weights" EOL
348     "  TColStd_Array1OfReal aWeights(1,4);" EOL EOL
349
350     "  aPoles(1) = gp_Pnt(0, 0, 0);      aWeights(1) = 1;" EOL
351     "  aPoles(2) = gp_Pnt(150, 250, 0);  aWeights(2) =75;" EOL
352     "  aPoles(3) = gp_Pnt(350, 150, 0);  aWeights(3) =120;" EOL
353     "  aPoles(4) = gp_Pnt(500, 500, 0);  aWeights(4) = 1;" EOL EOL
354
355     "  // creating a bezier curve" EOL
356     "  aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);" EOL EOL
357
358     "  // creating a surface of revolution of the bezier curve around Y axis" EOL
359     "  gp_Ax1 anAx(gp_Pnt(0, 0, 0), gp_Dir(0,1,0));" EOL
360     "  Handle(Geom_SurfaceOfRevolution) aSurface = new Geom_SurfaceOfRevolution(aBezierCurve, anAx);" EOL
361     );
362   
363   // array of the bezier curve poles
364   TColgp_Array1OfPnt aPoles(1,4);
365   // array of the poles' weights
366   TColStd_Array1OfReal aWeights(1,4);
367
368   aPoles(1) = gp_Pnt(0, 0, 0);      aWeights(1) = 1;
369   aPoles(2) = gp_Pnt(150, 250, 0);  aWeights(2) =75;
370   aPoles(3) = gp_Pnt(350, 150, 0);  aWeights(3) =120;
371   aPoles(4) = gp_Pnt(500, 500, 0);  aWeights(4) = 1;
372
373   Handle(Geom_BezierCurve) aBezierCurve = new Geom_BezierCurve(aPoles, aWeights);
374   drawCurve(aBezierCurve);
375
376   // creating a surface of revolution of the bezier curve around Y axis
377   gp_Ax1 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));
378   Handle(Geom_SurfaceOfRevolution) aSurface = new Geom_SurfaceOfRevolution(aBezierCurve, anAx);
379
380   drawSurfaceAndItsBSpline (aSurface, aName, aText);
381   FitMode=true;
382 }
383
384 //================================================================
385 // Function : Convert_Presentation::sampleToroidalSurface
386 // Purpose  : 
387 //================================================================
388 void Convert_Presentation::sampleToroidalSurface()
389 {
390   getAISContext()->EraseAll();
391
392   Standard_CString aName = "Toroidal surface";
393   TCollection_AsciiString aText (
394     "  // creating an axis parallel to Y axis" EOL
395     "  gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0));" EOL
396     "  // creating a toroidal surface with major radius = 240 and minor radius = 120" EOL
397     "  Handle(Geom_ToroidalSurface) aSurface = new Geom_ToroidalSurface(anAx, 240, 120);" EOL);
398
399   // creating an axis parallel to Y axis 
400   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,1,0)); 
401   // creating a toroidal surface with major radius = 240 and minor radius = 120
402   Handle(Geom_ToroidalSurface) aSurface = new Geom_ToroidalSurface(anAx, 240, 120);
403
404   drawSurfaceAndItsBSpline(aSurface, aName, aText);
405 }
406
407 //================================================================
408 // Function : Convert_Presentation::sampleConicalSurface
409 // Purpose  : 
410 //================================================================
411 void Convert_Presentation::sampleConicalSurface()
412 {
413   getAISContext()->EraseAll();
414
415   Standard_CString aName = "Conical surface";
416   TCollection_AsciiString aText (
417     "  // creating an axis parallel to Z axis" EOL
418     "  gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1)); " EOL
419     "  // creating a conical surface with base radius = 10 and angle = 20 deg" EOL
420     "  Handle(Geom_ConicalSurface) aConicalSurface = new Geom_ConicalSurface(anAx,PI/9., 10);" EOL EOL
421
422     "  // only finit surfaces can be converted to BSpline surfaces, " EOL
423     "  // conical surface is infinite, it must be trimmed" EOL
424     "  Handle(Geom_RectangularTrimmedSurface) aSurface = " EOL
425     "    new Geom_RectangularTrimmedSurface(aConicalSurface, 0, 2*PI, -1000, 1000, Standard_True, Standard_True);" EOL);
426
427   // creating an axis parallel to Z axis 
428   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1)); 
429   // creating a conical surface with base radius = 10 and angle = 20 deg
430   Handle(Geom_ConicalSurface) aConicalSurface = new Geom_ConicalSurface(anAx,M_PI/9., 10);
431
432   // only finit surfaces can be converted to BSpline surfaces, 
433   // conical surface is infinite, it must be trimmed
434   Handle(Geom_RectangularTrimmedSurface) aSurface = 
435     new Geom_RectangularTrimmedSurface(aConicalSurface, 0, 2*M_PI, -1000, 1000, Standard_True, Standard_True);
436
437   drawSurfaceAndItsBSpline(aSurface, aName, aText);
438 }
439
440 //================================================================
441 // Function : Convert_Presentation::sampleSphericalSurface
442 // Purpose  : 
443 //================================================================
444 void Convert_Presentation::sampleSphericalSurface()
445 {
446   getAISContext()->EraseAll();
447
448   Standard_CString aName = "Spherical surface";
449   TCollection_AsciiString aText (
450     "// creating an axis parallel to Z axis" EOL
451     "gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1));" EOL
452     "// creating a spherical surface with radius = 300" EOL
453     "Handle(Geom_SphericalSurface) aSurface = new Geom_SphericalSurface(anAx,300);" EOL);
454
455   // creating an axis parallel to Z axis 
456   gp_Ax3 anAx(gp_Pnt(0,0,0), gp_Dir(0,0,1)); 
457   // creating a spherical surface with radius = 300
458   Handle(Geom_SphericalSurface) aSurface = new Geom_SphericalSurface(anAx,300);
459
460   drawSurfaceAndItsBSpline(aSurface, aName, aText);
461 }
462