#
# Some examples of 3d bisector computations of 3-space freeform curves.
#
#			Gershon Elber, August 1996.
#

#
# Set states.
#
save_mat = view_mat;
view_mat = view_mat * sc( 0.35 );
viewobj( view_mat );

IProd = iritstate( "InterpProd", off );

#############################################################################
#
# Two linear curves
#
c1 = cbezier( list( ctlpt( E3,  0.0,  0.1, -1.0 ),
		    ctlpt( E3,  0.0,  0.1,  1.0 ) ) );
c2 = cbezier( list( ctlpt( E3, -1.0, -0.1,  0.0 ),
		    ctlpt( E3,  1.0, -0.1,  0.0 ) ) );
color( c1, red );
adwidth( c1, 3 );
color( c2, red );
adwidth( c2, 3 );

BisectSrf = cbisector( list( c1, c2 ), -1, 0.5, true, false );
color( BisectSrf, green );

interact( list( c1, c2, BisectSrf ) );

#############################################################################
#
# Two linear curves animated
#

for ( b = 2.0, -0.02, 0.0,
    c1 = cbezier( list( ctlpt( E3,  0.0, -0.1 - b, -1.0 ),
		        ctlpt( E3,  0.0, -0.1 - b,  1.0 ) ) ):
    c2 = cbezier( list( ctlpt( E3, -1.0,  0.1 + b,  0.0 ),
		        ctlpt( E3,  1.0,  0.1 + b,  0.0 ) ) ):
    color( c1, red ):
    adwidth( c1, 3 ):
    color( c2, red ):
    adwidth( c2, 3 ):
    BisectSrf = cbisector( list( c1, c2 ), -1, 0.5, true, false ):
    color( BisectSrf, green ):
    view( list( c1, c2, BisectSrf ), true ) );

pause();

#############################################################################
#
# Two quadratic curves
#
c1 = cbezier( list( ctlpt( E3,  0.0,  0.5, -1.0 ),
		    ctlpt( E3,  0.0,  0.1,  0.0 ),
		    ctlpt( E3,  0.0,  0.5,  1.0 ) ) );
c2 = cbezier( list( ctlpt( E3, -1.0,  1.0,  0.0 ),
		    ctlpt( E3,  0.0, -1.5,  0.0 ),
		    ctlpt( E3,  1.0,  1.0,  0.0 ) ) );
color( c1, red );
adwidth( c1, 3 );
color( c2, red );
adwidth( c2, 3 );

BisectSrf = cbisector( list( c1, c2 ), -1, 0.5, true, false );
color( BisectSrf, green );

interact( list( c1, c2, BisectSrf ) );

#############################################################################
#
# A line and a (approximation of a) circle.
#
c1 = cbezier( list( ctlpt( E3,  0.0,  0.0, -1.0 ),
		    ctlpt( E3,  0.0,  0.0,  1.0 ) ) );
c2 = pcircle( vector( 0.0, 0.0, 0.0 ), 1 );
color( c1, red );
adwidth( c1, 3 );
color( c2, red );
adwidth( c2, 3 );

BisectSrf = cbisector( list( c1, c2 ), -1, 0.5, true, false );
color( BisectSrf, green );

interact( list( c1, c2, BisectSrf ) );

#############################################################################
#
# A line and a (approximation of a) circle.
#
c1 = pcircle( vector( 0.0, 0.0, 0.0 ), 1 );
c2 = cbezier( list( ctlpt( E3, -1.0, 0.0,  1.0 ),
		    ctlpt( E3,  1.0, 0.0, -1.0 ) ) );
color( c1, red );
adwidth( c1, 3 );
color( c2, red );
adwidth( c2, 3 );

BisectSrf = cbisector( list( c1, c2 ), -1, 0.5, true, false );
color( BisectSrf, green );

interact( list( c1, c2, BisectSrf ) );

#############################################################################
#
# A line and a (approximation of a) circle.
#
c1 = creparam( pcircle( vector( 0.0, 0.0, 0.0 ), 1 ), 0, 1 );
c2 = cbezier( list( ctlpt( E3, -2.0, 0.0,  1.0 ),
		    ctlpt( E3, -2.0, 0.0, -1.0 ) ) );
color( c1, red );
adwidth( c1, 3 );
color( c2, red );
adwidth( c2, 3 );

BisectSrf = cbisector( list( c1, c2 ), -1, 0.5, true, false );
BisectSrf1 = sregion( BisectSrf, col, 0.0, 0.3 );
BisectSrf2 = sregion( BisectSrf, col, 0.7, 1.0 );
BisectSrf3 = sregion( BisectSrf, col, 0.36, 0.64 );
color( BisectSrf1, green );
color( BisectSrf2, green );
color( BisectSrf3, green );

interact( list( c1, c2, BisectSrf1, BisectSrf2, BisectSrf3 ) );

#############################################################################
#
# A helix and a point
#

Helix = pcircle( vector( 0.0, 0.0, 0.0 ), 1 ) * rz( 90 ) * ry( -90 );
for ( i = 0, 1, sizeof( Helix ) - 1,
    PtH = coord( Helix, i ):
    Helix = cEditPt( Helix,
		     ctlpt( E3, i / 4, coord( PtH, 2 ), coord( PtH, 3 ) ),
		     i ) );
Helix = ( Helix + Helix * tx( 9 / 4 ) ) * sx( 0.5 );
color( Helix, red );
adwidth( Helix, 3 );

pt = point( 1.2, 0, 0 );
color( pt, yellow );
adwidth( Pt, 3 );

BisectSrf = cbisector( list( Helix, pt ), -1, -0.2, true, false );
color( BisectSrf, green );

interact( list( Helix, pt, BisectSrf ) );

#############################################################################
#
# A bilinear surface: sphere--plane bisector
#
s1 = ruledSrf( ctlpt( E3, -1.0, -1.0, 0.0 ) + ctlpt( E3,  1.0, -1.0, 0.0 ),
	       ctlpt( E3, -1.0,  1.0, 0.0 ) + ctlpt( E3,  1.0,  1.0, 0.0 ) );
color( s1, red );

pt = point( 0, 0, 1 );
adwidth( pt, 3 );
color( pt, yellow );

BisectSrf = sbisector( s1, pt );
color( BisectSrf, green );

interact( list( s1, pt, BisectSrf ) );

for ( z = 1.0, -0.01, -1.0,
    pt = point( 0, 0, z ):
    adwidth( pt, 3 ):
    color( pt, yellow ):
    BisectSrf = sbisector( s1, pt ):
    color( BisectSrf, green ):
    view( list( s1, pt, BisectSrf ), 1 ) );
pause();

#############################################################################
#
# A sphere--sphere/sphere-pt bisector
#

s = sphereSrf( 1 );
color( s, red );

s1 = sRegion( sRegion( s, row, 0.5, 1.5 ), col, 2.5, 3.5 );
s2 = sRegion( sRegion( s, row, 0.001, 1.999 ), col, 0.001, 1.999 );

pt = point( 0, 2, 0 );
adwidth( pt, 3 );
color( pt, yellow );

BisectSrf1 = sbisector( s1, pt );
BisectSrf2 = sbisector( s2, pt );
color( BisectSrf1, green );
color( BisectSrf2, green );

interact( list( s, pt, BisectSrf1, BisectSrf2 ) );

pt = point( 0, 0.6, 0 );
adwidth( pt, 3 );
color( pt, yellow );

BisectSrf = sbisector( s, pt );
color( BisectSrf, green );

interact( list( s, pt, BisectSrf ) );

#############################################################################

IProd = iritstate( "InterpProd", IProd );
free( IProd );

view_mat = save_mat;

free( s );
free( s1 );
free( s2 );
free( Helix );
free( c1 );
free( c2 );
free( BisectSrf );
free( BisectSrf1 );
free( BisectSrf2 );
free( BisectSrf3 );
free( Pt );
free( PtH );
free( i );
free( z );
free( b );
