0025773: Visualization - remove AIS_Drawer class and transfer its Link() logic to...
[occt.git] / samples / mfc / occtdemo / Intersections / Intersections_Presentation.cpp
CommitLineData
7fd59977 1// Intersections_Presentation.cpp: implementation of the Intersections_Presentation class.
2// Intersections of curves and surfaces
3//////////////////////////////////////////////////////////////////////
4
5#include "stdafx.h"
6#include "Intersections_Presentation.h"
7
8#include <TColgp_Array1OfPnt2d.hxx>
9#include <TColgp_Array1OfPnt.hxx>
10#include <TColStd_Array2OfReal.hxx>
11
12#include <Geom2d_BSplineCurve.hxx>
13#include <Geom2dAPI_PointsToBSpline.hxx>
14#include <GeomAPI_PointsToBSpline.hxx>
15#include <GeomAPI_PointsToBSplineSurface.hxx>
16#include <GeomAPI_IntCS.hxx>
17#include <GeomAPI_IntSS.hxx>
18#include <Geom2dAPI_InterCurveCurve.hxx>
19
20#include <gp_Pnt.hxx>
21#include <gp_Pnt2d.hxx>
22#include <gp_Dir.hxx>
23#include <gp_Ax2.hxx>
6262338c 24#include <Prs3d_Drawer.hxx>
7fd59977 25#include <Prs3d_LineAspect.hxx>
26
27
28// Initialization of global variable with an instance of this class
29OCCDemo_Presentation* OCCDemo_Presentation::Current = new Intersections_Presentation;
30
31// Initialization of array of samples
32const Intersections_Presentation::PSampleFuncType Intersections_Presentation::SampleFuncs[] =
33{
34 &Intersections_Presentation::InterCurveCurve,
35 &Intersections_Presentation::SelfInterCurveCurve,
36 &Intersections_Presentation::InterCurveSurface,
37 &Intersections_Presentation::InterSurfaceSurface,
38};
39
40#ifdef WNT
41 #define EOL "\r\n"
42#else
43 #define EOL "\n"
44#endif
45
46
47//////////////////////////////////////////////////////////////////////
48// Construction/Destruction
49//////////////////////////////////////////////////////////////////////
50
51Intersections_Presentation::Intersections_Presentation()
52{
53 myNbSamples = sizeof(SampleFuncs)/sizeof(PSampleFuncType);
54 setName ("Intersections of curves and surfaces");
55}
56
57//////////////////////////////////////////////////////////////////////
58// Sample execution
59//////////////////////////////////////////////////////////////////////
60
61void Intersections_Presentation::DoSample()
62{
63 getAISContext()->EraseAll();
64 if (myIndex >=0 && myIndex < myNbSamples)
65 (this->*SampleFuncs[myIndex])();
66}
67
68//////////////////////////////////////////////////////////////////////
69// Functions
70//////////////////////////////////////////////////////////////////////
71
72
73static Handle_Geom2d_BSplineCurve create2dBSplineCurve(const Standard_Integer thePoles,
74 const Standard_Real theCoords[][2])
75{
76 TColgp_Array1OfPnt2d thePoints(1, thePoles);
77
78 for (Standard_Integer i=0; i < thePoles; i++)
79 thePoints(i+1) = gp_Pnt2d (theCoords[i][0]*100, theCoords[i][1]*100);
80
81 Standard_Integer MinDegree = 3;
82 Standard_Integer MaxDegree = 8;
83
84 return Geom2dAPI_PointsToBSpline (
85 thePoints, MinDegree, MaxDegree);
86}
87
88
89static Handle_Geom_BSplineSurface createBSplineSurface(const Standard_Real theZCoords[],
90 const Standard_Real theXStep,
91 const Standard_Real theYStep,
92 const Standard_Real theXBound,
93 const Standard_Real theYBound)
94{
95 TColStd_Array2OfReal aZPoints(1,4,1,4);
96
97 Standard_Integer aColLength = aZPoints.ColLength();
98 Standard_Integer aRowLength = aZPoints.RowLength();
99 Standard_Integer aIndex = 0;
100 Standard_Integer k = 100;
101
102 for(Standard_Integer i = 0 ; i < aRowLength ; i++)
103 for(Standard_Integer j = 0; j < aColLength ; j++)
104 aZPoints(i+1,j+1) = (theZCoords[aIndex++] * k);
105
106 return GeomAPI_PointsToBSplineSurface (
107 aZPoints,theXBound,theXStep,theYBound,theYStep,3,8);
108}
109
110//==========================================================================================
111// Function : Intersections_Presentation::InterCurveCurve
112// Purpose :
113//==========================================================================================
114
115void Intersections_Presentation::InterCurveCurve()
116{
117 setResultTitle("Intersection of 2d curves");
118 TCollection_AsciiString aText(
119 " // define two 2d curves" EOL
120 " Handle(Geom2d_Curve) aCurve1,aCurve2;" EOL
121 " // initializing curves ..." EOL EOL
122
123 " // construct algo" EOL
124 " Standard_Real Tol = 2;" EOL
125 " Geom2dAPI_InterCurveCurve aInterCC(aCurve1,aCurve2,Tol);" EOL EOL
126
127 " // number of intersection points" EOL
128 " Standard_Integer aNumberPoints = aInterCC.NbPoints();" EOL EOL
129
130 " // evaluate solutions" EOL
131 " for(Standard_Integer i = 1; i <= aNumberPoints; i++)" EOL
132 " {" EOL
133 " gp_Pnt2d aPnt = aInterCC.Point(i);" EOL
134 " // use point as you need ..." EOL
135 " }" EOL EOL
136
137 " // number of tangential intersections " EOL
138 " Standard_Integer aNumberSegments = aInterCC.NbSegments();" EOL EOL
139
140 " // evaluate solutions" EOL
141 " for(Standard_Integer j = 1; j <= aNumberSegments; j++)" EOL
142 " {" EOL
143 " Handle(Geom2d_Curve) a2dCurve1,a2dCurve2;" EOL
144 " aInterCC.Segment(j,a2dCurve1,a2dCurve2);" EOL
145 " // use curves as you need ..." EOL
146 " }" EOL EOL
147 );
148 setResultText(aText.ToCString());
149
150
151 // define arrays of points
152 Standard_Real aPolesCoords1[][2] = {
153 {-6,0.5},{-5,1},{-2,0},{-1,-1.01},{0,-3},{2,-4},{4,-1},{5,0}
154 };
155 Standard_Real aPolesCoords2[][2] = {{-6,1},{-5,0},{-4,-1},
156 {-3,-1},{-2,0},{-1,-1},{0,-2.5},{1,-3.4},{2,-2},{3,-2},{4,-2},{5,-3}
157 };
158
159 // define arrays lengths
160 Standard_Integer nPoles1 = sizeof(aPolesCoords1)/(sizeof(Standard_Real)*2);
161 Standard_Integer nPoles2 = sizeof(aPolesCoords2)/(sizeof(Standard_Real)*2);
162
163 // make two bspline curves
164 Handle(Geom2d_BSplineCurve) aCurve1 = create2dBSplineCurve(nPoles1,aPolesCoords1);
165 Handle(Geom2d_BSplineCurve) aCurve2 = create2dBSplineCurve(nPoles2,aPolesCoords2);
166
167 // construct algo
168 Standard_Real Tol = 2;
169 Geom2dAPI_InterCurveCurve aInterCC(aCurve1,aCurve2,Tol);
170
171 // number of intersection points
172 Standard_Integer aNumberPoints = aInterCC.NbPoints();
173 // number of tangential intersections
174 Standard_Integer aNumberSegments = aInterCC.NbSegments();
175
176 // output
177 aText = aText +
178 " //=================================================" EOL EOL
179
180 " Number of intersection points = " + TCollection_AsciiString(aNumberPoints) + EOL
181 " Number of tangential intersections = " + TCollection_AsciiString(aNumberSegments) + EOL;
182 setResultText(aText.ToCString());
183
184 gp_Pnt aPoint(0,0,0);
185 gp_Dir aDir(0,0,1);
186 gp_Ax2 anAx2(aPoint,aDir);
187
188 drawCurve(aCurve1,Quantity_Color(Quantity_NOC_RED),Standard_True,anAx2);
189 drawCurve(aCurve2,Quantity_Color(Quantity_NOC_RED),Standard_True,anAx2);
190
191 if(WAIT_A_SECOND) return;
192 // output intersection points
193 if (aNumberPoints > 0)
194 for(Standard_Integer i = 1; i <= aNumberPoints; i++)
195 {
196 drawPoint(gp_Pnt(aInterCC.Point(i).X(),aInterCC.Point(i).Y(),0));
197 }
198
199 if(WAIT_A_SECOND) return;
200
201 if (aNumberSegments > 0)
202 for(Standard_Integer j = 1; j <= aNumberSegments; j++)
203 {
204 Handle(Geom2d_Curve) a2dCurve1;
205 Handle(Geom2d_Curve) a2dCurve2;
206 aInterCC.Segment(j,a2dCurve1,a2dCurve2);
207 drawCurve(a2dCurve1,Quantity_Color(Quantity_NOC_YELLOW),Standard_True,anAx2);
208 drawCurve(a2dCurve2,Quantity_Color(Quantity_NOC_YELLOW),Standard_True,anAx2);
209 }
210}
211
212//==========================================================================================
213// Function : Intersections_Presentation::SelfInterCurveCurve
214// Purpose :
215//==========================================================================================
216
217void Intersections_Presentation::SelfInterCurveCurve()
218{
219 setResultTitle("Self-intersection of 2d curve");
220 TCollection_AsciiString aText(
221 " // define 2d curve" EOL
222 " Handle(Geom2d_Curve) aCurve1;" EOL
223 " // initializing curve ..." EOL EOL
224
225 " // construct algo" EOL
226 " Standard_Real Tol = 2;" EOL
227 " Geom2dAPI_InterCurveCurve aInterCC(aCurve1,Tol);" EOL EOL
228
229 " // number of intersection points" EOL
230 " Standard_Integer aNumberPoints = aInterCC.NbPoints();" EOL EOL
231
232 " // evaluate solutions" EOL
233 " for(Standard_Integer i = 1; i <= aNumberPoints; i++)" EOL
234 " {" EOL
235 " gp_Pnt2d aPnt = aInterCC.Point(i);" EOL
236 " // use point as you need ..." EOL
237 " }" EOL EOL
238
239 " // number of tangential intersections " EOL
240 " Standard_Integer aNumberSegments = aInterCC.NbSegments();" EOL EOL
241
242 " // evaluate solutions" EOL
243 " for(Standard_Integer j = 1; j <= aNumberSegments; j++)" EOL
244 " {" EOL
245 " Handle(Geom2d_Curve) a2dCurve1;" EOL
246 " aInterCC.Segment(j,a2dCurve1);" EOL
247 " // use curve as you need ..." EOL
248 " }" EOL EOL
249 );
250 setResultText(aText.ToCString());
251
252 // define array of points
253 Standard_Real aPolesCoords1[][2] = {
254 {-5,-2},{-4,-1},{0,2.4},{6,4},{6,-3},
255 {1,-1},{-3,2},{-1,2.2},{3,3.5},{4,4.9}
256 };
257
258 // define array length
259 Standard_Integer nPoles1 = sizeof(aPolesCoords1)/(sizeof(Standard_Real)*2);
260
261 // make bspline curve
262 Handle(Geom2d_BSplineCurve) aCurve1 = create2dBSplineCurve(nPoles1,aPolesCoords1);
263
264 // construct algo
265 Standard_Real Tol = 2;
266 Geom2dAPI_InterCurveCurve aInterCC(aCurve1,Tol);
267
268 aInterCC.Init(aCurve1,Tol);
269
270 // number of intersection points
271 Standard_Integer aNumberPoints = aInterCC.NbPoints();
272 // number of tangential intersections
273 Standard_Integer aNumberSegments = aInterCC.NbSegments();
274
275 // output
276 aText = aText +
277 " //=================================================" EOL EOL
278
279 " Number of intersection points = " + TCollection_AsciiString(aNumberPoints) + EOL
280 " Number of tangential intersections = " + TCollection_AsciiString(aNumberSegments) + EOL;
281 setResultText(aText.ToCString());
282
283 gp_Pnt aPoint(0,0,0);
284 gp_Dir aDir(0,0,1);
285 gp_Ax2 anAx2(aPoint,aDir);
286
287 drawCurve(aCurve1,Quantity_Color(Quantity_NOC_RED),Standard_True,anAx2);
288
289 if(WAIT_A_SECOND) return;
290
291 // output intersection points
292 for(Standard_Integer i = 1; i <= aNumberPoints; i++)
293 drawPoint(gp_Pnt(aInterCC.Point(i).X(),aInterCC.Point(i).Y(),0));
294
295
296 if(WAIT_A_SECOND) return;
297
298 // output tangential intersections
299 for(Standard_Integer j = 1; j <= aNumberSegments; j++)
300 {
301 Handle(Geom2d_Curve) a2dCurve1;
302 aInterCC.Segment(j,a2dCurve1);
303 drawCurve(a2dCurve1,Quantity_Color(Quantity_NOC_YELLOW),Standard_True,anAx2);
304 }
305}
306
307//==========================================================================================
308// Function : Intersections_Presentation::InterCurveSurface
309// Purpose :
310//==========================================================================================
311
312
313void Intersections_Presentation::InterCurveSurface()
314{
315
316 setResultTitle("Intersection of curve and surface");
317 TCollection_AsciiString aText(
318 " // define curve" EOL
319 " Handle(Geom_Curve) aCurve;" EOL
320 " // initializing curve ..." EOL EOL
321
322 " // define surface" EOL
323 " Handle(Geom_Surface) aSurface;" EOL
324 " // initializing surface ..." EOL EOL
325
326 " // construct algo" EOL
327 " GeomAPI_IntCS aInterCS(aCurve,aSurface);" EOL
328 " if(!aInterCS.IsDone()) return;" EOL EOL
329
330 " // number of intersection points" EOL
331 " Standard_Integer aNumberPoints = aInterCS.NbPoints();" EOL EOL
332
333 " // evaluate solutions" EOL
334 " for(Standard_Integer i = 1; i <= aNumberPoints; i++)" EOL
335 " {" EOL
336 " Standard_Real aParamU,aParamV,aParamOnCurve;" EOL
337 " aInterCS.Parameters(i,aParamU,aParamV,aParamOnCurve);" EOL
338 " gp_Pnt aPnt = aInterCS.Point(i);" EOL
339 " // use solution as you need ..." EOL
340 " }" EOL EOL
341
342 " // number of tangential intersections " EOL
343 " Standard_Integer aNumberSegments = aInterCS.NbSegments();" EOL EOL
344
345 " // evaluate solutions" EOL
346 " for(Standard_Integer j = 1; j <= aNumberSegments; j++)" EOL
347 " Handle(Geom_Curve) aCurve1 = aInterCS.Segment(j);" EOL
348 " // use solution as you need ..." EOL EOL
349 );
350 setResultText(aText.ToCString());
351
352 // define bounds surface
353 Standard_Real aXStep = 262.5, aYStep = 262.5;
354 Standard_Real aXBound = -450, aYBound = -300;
355
356 // define array of points
357 Standard_Real aZCoords [] = {{-1},{0},{0},{0.5},{0},{-1},
358 {-1},{0},{-1},{-1.5},{-1},{-1},{1},{-1},{-1},{-1}};
359
360 // creating bspline surface
361 Handle(Geom_BSplineSurface) aSurface = createBSplineSurface(aZCoords,aXStep,aYStep,aXBound,aYBound);
362
363 // make bspline curve
364 // define array of points
365 Standard_Real aCoords[][3] = {{-3,-3,-2},{-2,-2,-1},{-1,-1,-1},
366 {0,0,1},{1,2,3},{2,1,0},{3,1,-3},{4,2,-4},{5,2,-3}};
367 Standard_Integer nPoles = sizeof(aCoords)/(sizeof(Standard_Real)*3);
368
369 TColgp_Array1OfPnt aCurvePoint (1, nPoles);
370
371 for (Standard_Integer n=0; n < nPoles; n++)
372 aCurvePoint(n+1) = gp_Pnt (aCoords[n][0]*100, aCoords[n][1]*100, aCoords[n][2]*100);
373
374 // define parameters
375 Standard_Integer MinDegree = 3;
376 Standard_Integer MaxDegree = 8;
377
378 // make bspline curve
379 GeomAPI_PointsToBSpline aCurve(aCurvePoint, MinDegree, MaxDegree);
380
381
382 // construct algo
383 GeomAPI_IntCS aInterCS(aCurve,aSurface);
384
385 if(!aInterCS.IsDone()) return;
386
387 // number of intersection points
388 Standard_Integer aNumberPoints = aInterCS.NbPoints();
389
390 // number of tangential intersections
391 Standard_Integer aNumberSegments = aInterCS.NbSegments();
392
393 // define and output parameters points
394 aText = aText +
395 " //=================================================" EOL EOL
396 " Number of intersection points = " + TCollection_AsciiString(aNumberPoints) + EOL
397 " Number of tangential intersections = " + TCollection_AsciiString(aNumberSegments) + EOL EOL;
398
399 if(aNumberPoints > 0)
400 {
401 aText = aText +
402 " Parameters (U,V) on the surface of the intersection point." EOL
403 " Parameter (ParamOnCurve) on the curve of the intersection point." EOL EOL;
404
405 for(Standard_Integer j = 1; j <= aNumberPoints; j++)
406 {
407 Standard_Real aParamU,aParamV,aParamOnCurve;
408 aInterCS.Parameters(j,aParamU,aParamV,aParamOnCurve);
409
410 aText = aText +
411 " Intersection point " + j + " : U = " + TCollection_AsciiString(aParamU) + EOL
412 " : V = " + TCollection_AsciiString(aParamV) + EOL
413 " ParamOnCurve = " + TCollection_AsciiString(aParamOnCurve) + EOL EOL;
414 }
415 setResultText(aText.ToCString());
416 }
417
418 drawSurface(aSurface);
419 if(WAIT_A_SECOND) return;
420 drawCurve(aCurve);
421 if(WAIT_A_SECOND) return;
422
423 for(Standard_Integer i = 1; i <= aNumberPoints; i++)
424 {
425 drawPoint(gp_Pnt(aInterCS.Point(i)));
426 }
427
428
429 for(Standard_Integer k = 1; k <= aNumberSegments; k++)
430 {
431 Handle(Geom_Curve) aCurve1 = aInterCS.Segment(k);
432 drawCurve(aCurve1,Quantity_NOC_YELLOW);
433 }
434}
435
436//==========================================================================================
437// Function : Intersections_Presentation::InterSurfaceSurface
438// Purpose :
439//==========================================================================================
440
441void Intersections_Presentation::InterSurfaceSurface()
442{
443 setResultTitle("Intersection of surfaces");
444 TCollection_AsciiString aText(
445 " // define two surfaces" EOL
446 " Handle(Geom_Surface) aSurface1,aSurface2;" EOL
447 " // initializing surfaces ..." EOL EOL
448
449 " // construct algo" EOL
450 " Standard_Real Tol = 0.1e-7;" EOL
451 " GeomAPI_IntSS aInterSS(aSurface1,aSurface2,Tol);" EOL
452 " if(!aInterSS.IsDone()) return;" EOL EOL
453
454 " // number of intersection lines" EOL
455 " Standard_Integer aNumberLines = aInterSS.NbLines();" EOL EOL
456
457 " // evaluate solutions" EOL
458 " for(Standard_Integer i = 1; i <= aNumberLines; i++)" EOL
459 " {" EOL
460 " Handle(Geom_Curve) aCurve = aInterSS.Line(i);" EOL
461 " // use aCurve as you need ..." EOL
462 " }" EOL EOL
463
464 );
465 setResultText(aText.ToCString());
466
467 // define bounds of surfaces
468 Standard_Real aXStep1 = 250, aYStep1 = 250;
469 Standard_Real aXBound1 = -450, aYBound1 = -300;
470 Standard_Real aXStep2 = 250, aYStep2 = 150;
471 Standard_Real aXBound2 = -650, aYBound2 = -200;
472
473 // define arrays of points
474 Standard_Real aZCoords1 [] = {{-1},{0},{0},{-0.5},{0},{-1},
475 {-1},{0},{-1},{-1.5},{-1},{-1},{-2},{-1},{-1},{-1}};
476
477 Standard_Real aZCoords2 [] = {{-2},{-1},{-1},{-1},{-1},{-1.5},
478 {-1.5},{-1},{-1.5},{-1},{0},{0},{-1},{0},{0},{0}};
479
480 // make bspline surface
481 Handle(Geom_BSplineSurface) aSurface1 = createBSplineSurface(aZCoords1,aXStep1,aYStep1,aXBound1,aYBound1);
482 Handle(Geom_BSplineSurface) aSurface2 = createBSplineSurface(aZCoords2,aXStep2,aYStep2,aXBound2,aYBound2);
483
484 // construct algo
485 Standard_Real Tol = 0.1e-7;
486 GeomAPI_IntSS aInterSS(aSurface1,aSurface2,Tol);
487
488 if(!aInterSS.IsDone()) return;
489
490 // number of intersection lines
491 Standard_Integer aNumberLines = aInterSS.NbLines();
492
493 // output
494 aText = aText +
495 " //=================================================" EOL EOL
496 " Number of intersection lines = " + TCollection_AsciiString(aNumberLines) + EOL;
497 setResultText(aText.ToCString());
498
499 drawSurface(aSurface1);
500 if(WAIT_A_SECOND) return;
501 drawSurface(aSurface2,Quantity_NOC_YELLOW);
502 if(WAIT_A_SECOND) return;
503
504 Standard_Real aLineWidth = 3.0;
505
506 for(Standard_Integer i = 1; i <= aNumberLines; i++)
507 {
508 // show intersection line
509 Handle(Geom_Curve) aCurve = aInterSS.Line(i);
510 Handle(AIS_InteractiveObject) anIO = drawCurve(aCurve, Quantity_NOC_RED, Standard_False);
511 Handle(Prs3d_LineAspect) aLA =
512 new Prs3d_LineAspect(Quantity_NOC_RED,Aspect_TOL_SOLID,aLineWidth);
513 anIO->Attributes()->SetLineAspect(aLA);
514 getAISContext()->Display(anIO);
515 }
516}
517
518