#
# Some examples of triangular surfaces.
#
#				Gershon Elber, Aug. 1996
#

b = tsbezier( 3,
	      list( ctlpt( E3, 0.0,  0.0,  0.4 ),
                    ctlpt( E3, 0.3,  0.0,  0.3 ),
                    ctlpt( E3, 0.7,  0.0,  0.8 ),

                    ctlpt( E3, 0.2,  0.4,  1.0 ),
                    ctlpt( E3, 0.4,  0.5,  1.0 ),

                    ctlpt( E3, 0.5,  1.0,  0.7 ) ) );
color( b, green );

bMesh = ffmesh( b );
color( bMesh, red );

interact( list( b, bMesh ) );
free( bMesh );

Isos = nil();
for ( i = 0, 0.1, 1,
    snoc( csurface( b, row, i ), Isos ) );
for ( i = 0, 0.25, 1,
    snoc( csurface( b, col, i ), Isos ) );
for ( i = 0, 1/3, 1,
    snoc( csurface( b, depth, i ), Isos ) );
interact( Isos );
free( Isos );
free( i );

PolyGns = gpolygon( b, true );
color( PolyGns, red );
interact( list( b, PolyGns ) );
free( PolyGns );

PolyLns = gpolyline( b, 15 );
color( PolyLns, magenta );
interact( list( b, PolyLns ) );
free( PolyLns );

fforder( b );
ffmsize( b );
ffctlpts( b );

pdomain( b );
be2 = coerce( b, e2 );
color( b, red );

tseval( b, 0, 0, 1 );
tseval( b, 0, 1, 0 );
tseval( b, 1, 0, 0 );
tseval( b, 1/3, 1/3, 1/3 );
tsnormal( b, 1/3, 1/3, 1/3 );

p = tseval( b, 1/3, 1/3, 1/3 );
n = tsnormal( b, 1/3, 1/3, 1/3 );
Nrml = coerce( p, e3 ) + coerce( coerce( p, point_type ) + n, e3 );

interact( list( b, be2, Nrml, p ) );
free( b );
free( be2 );

c = tsbezier( 4,
	      list( ctlpt( E3, 0.0, -0.3,  0.0 ),
                    ctlpt( E3, 0.2, -0.5, -0.2 ),
                    ctlpt( E3, 0.6, -0.3,  0.2 ),
                    ctlpt( E3, 0.9, -0.4,  0.1 ),

	            ctlpt( E3, 0.0,  0.0,  0.1 ),
                    ctlpt( E3, 0.3,  0.0, -0.1 ),
                    ctlpt( E3, 0.7,  0.0,  0.3 ),

                    ctlpt( E3, 0.2,  0.4,  0.5 ),
                    ctlpt( E3, 0.4,  0.5,  0.5 ),

                    ctlpt( E3, 0.5,  1.0,  0.2 ) ) );
color( c, green );

fforder( c );
ffmsize( c );
ffctlpts( c );

tseval( c, 0, 0, 1 );
tseval( c, 0, 1, 0 );
tseval( c, 1, 0, 0 );
tseval( c, 1/3, 1/3, 1/3 );
tsnormal( c, 1/3, 1/3, 1/3 );

p = tseval( c, 1/3, 1/3, 1/3 );
n = tsnormal( c, 1/3, 1/3, 1/3 );
Nrml = coerce( p, e3 ) + coerce( coerce( p, point_type ) + n, e3 );
free( n );
attrib( Nrml, "dwidth", 8 );
color( Nrml, red );

interact( list( c, Nrml, p ) );



dc1 = tsderive( c, row );
color( dc1, yellow );
dc2 = tsderive( c, col );
color( dc2, magenta );
dc3 = tsderive( c, depth );
color( dc3, cyan );

v1 = coerce( tseval( dc1, 1/3, 1/3, 1/3 ), vector_type );
v2 = coerce( tseval( dc2, 1/3, 1/3, 1/3 ), vector_type );
n2 = normalize( v1 ^ v2 );
Nrml2 = coerce( p, e3 ) + coerce( coerce( p, point_type ) + n2, e3 );
free( v1 );
free( v2 );
free( n2 );
attrib( Nrml2, "dwidth", 4 );
color( Nrml2, blue );

interact( list( axes, c, dc1, dc2, dc3, Nrml, Nrml2, p ) );
free( dc1 );
free( dc2 );
free( dc3 );
free( Nrml );
free( Nrml2 );
free( p );

d = tsbspline( 3, 2,
	       list( ctlpt( E3, 1.7, 0.0, 0 ),
                     ctlpt( E3, 0.7, 0.7, 0 ),
                     ctlpt( E3, 1.7, 0.3, 0 ),

                     ctlpt( E3, 1.5, 0.8, 0 ),
                     ctlpt( E3, 1.4, 0.8, 0 ),

                     ctlpt( E3, 1.0, 1.0, 0 ) ),
	       list( KV_OPEN ) );
color( d, green );
d;

fforder( d );
ffmsize( d );
ffctlpts( d );

save( "trisrf1", list( c, d ) );
free( c );
free( d );



g = tsgregory( 5,
        list( ctlpt( E3, 2, -1, 0 ),
              ctlpt( E3, 2.3, -1, 0.25 ),
              ctlpt( E3, 2.6, -1, 0.25 ),
              ctlpt( E3, 2.8, -1, 0.13 ),
              ctlpt( E3, 3, -1, 0 ),
              ctlpt( E3, 2.25, -0.7, 0.25 ),
              ctlpt( E3, 2.5, -0.7, -0.25 ),
              ctlpt( E3, 2.6, -0.7, -0.15 ),
              ctlpt( E3, 2.75, -0.7, 0.25 ),
              ctlpt( E3, 2.4, -0.4, 0.25 ),
              ctlpt( E3, 2.5, -0.4, 0 ),
              ctlpt( E3, 2.6, -0.4, -0.25 ),
              ctlpt( E3, 2.45, -0.2, 0.12 ),
              ctlpt( E3, 2.55, -0.2, -0.12 ),
              ctlpt( E3, 2.5, 0, 0 ),
              ctlpt( E3, 2.5, -0.7, -0.25 ),
              ctlpt( E3, 2.6, -0.7, -0.15 ),
              ctlpt( E3, 2.5, -0.4, 0 ) ) );
attrib(g, "color", 3);

gb = coerce(g, bezier_type);
color( gb, red );

All = list( gb * tz( 0.01 ), g, axes ) * sc( 0.75 ) * tx( -1 ) * ty( 1 );
interact( All );
save( "trisrf2", All );

free( All );
free( g );
free( gb );
