1 #include "nurbsSub.h"
2
3 namespace PLib {
4
5 template <class T, int N>
generateTorus(T majorRadius,T minorRadius)6 NurbsSurface<T,N>* generateTorus(T majorRadius, T minorRadius)
7 {
8 // These define the shape of a unit torus centered about the origin.
9 T xvalues[] = { 0.0, -1.0, -1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 0.0 };
10 T yvalues[] = { 1.0, 1.0, 0.0, -1.0, -1.0, -1.0, 0.0, 1.0, 1.0 };
11 T zvalues[] = { 0.0, 1.0, 1.0, 1.0, 0.0, -1.0, -1.0, -1.0, 0.0 };
12 T offsets[] = { -1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 0.0, -1.0, -1.0 };
13
14 // Piecewise Bezier knot vector for a quadratic curve with four segments
15 T knotsMem[] = { 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4 };
16 Vector<T> knots(knotsMem,12) ;
17
18 int i, j;
19
20 double r2over2 = sqrt( 2.0 ) / 2.0;
21 double weight;
22
23 NurbsSurface<T,N> *torus = new NurbsSurface<T,N> ;
24
25 torus->resize(9,9,2,2) ;
26
27 for (i = 0; i < 9; i++){
28 for (j = 0; j < 9; j++) {
29 HPoint_nD<T,N> hp ;
30 weight = ((j & 1) ? r2over2 : 1.0) * ((i & 1) ? r2over2 : 1.0);
31 // Notice how the weights are pre-multiplied with the x, y and z values
32 hp.x() = xvalues[j]* (majorRadius + offsets[i] * minorRadius) * weight;
33 hp.y() = yvalues[j]* (majorRadius + offsets[i] * minorRadius) * weight;
34 hp.z() = (zvalues[i] * minorRadius) * weight;
35 hp.w() = weight;
36 torus->modCP(i,j,hp) ;
37 }
38 }
39
40 // The knot vectors define piecewise Bezier segments
41 // (the same in both U and V).
42
43 torus->modKnotU(knots) ;
44 torus->modKnotV(knots) ;
45
46 return torus;
47 }
48
49 template NurbsSurface<float,3>* generateTorus<float,3>(float majorRadius, float minorRadius);
50
51 }
52
main()53 main()
54 {
55 using namespace PLib ;
56 NurbsSurface<float,3> *torus;
57
58 float tolerance = 2.0;
59
60 torus = generateTorus<float,3>( 1.3, 0.3 );
61
62 NurbsSubSurface<float> surf(*torus) ;
63
64 surf.drawSubdivisionPS("tnurbsSub.ps", tolerance );
65 surf.drawSubdivisionVRML("tnurbsSub.wrl",0.1) ;
66 surf.drawSubdivisionVRML97("tnurbsSub97.wrl",0.1) ;
67 }
68