#
# Test cases for SSI.
#
#				Gershon Elber, Feb 1995
#
# In order to be able to appreciate the complexity of some of the test cases,
# it is suggested to view this file through IRIT with a display device that
# is able to render the surfaces shaded, such as xgldrvs.
#

#
# 1. Simple close loop intersection. Both srfs are bi-quadratic polynomials.
#

s1 = sbezier( list( list( ctlpt( E3, 0.1, 0.0, 1.0 ),
                          ctlpt( E3, 0.3, 1.0, 0.5 ),
                          ctlpt( E3, 0.0, 2.0, 1.0 ) ),
                    list( ctlpt( E3, 1.1, 0.0, 0.5 ),
                          ctlpt( E3, 1.3, 1.0, 0.0 ),
                          ctlpt( E3, 1.0, 2.0, 0.5 ) ),
                    list( ctlpt( E3, 2.1, 0.0, 1.1 ),
                          ctlpt( E3, 2.3, 1.0, 0.4 ),
                          ctlpt( E3, 2.0, 2.0, 1.2 ) ) ) );
s2 = s1 * scale( vector( 1, 1, -1 ) ) * tz( 1.2 );
color( s1, red );
color( s2, green );
All = list( s1, s2 );
interact( All );

save( "ssi1", All );

#
# 2 and 3. Same as 1 but both surfaces are degree raised. How much does it
# slows down ssi computation!?
#

s1a = sraise( sraise( s1, ROW, 4 ), COL, 4 );
s2a = sraise( sraise( s2, ROW, 4 ), COL, 4 );
color( s1a, red );
color( s2a, green );
All = list( s1a, s2a );
interact( All );

save( "ssi2", All );

s1b = sraise( sraise( s1a, ROW, 5 ), COL, 5 );
s2b = sraise( sraise( s2a, ROW, 5 ), COL, 5 );
color( s1b, red );
color( s2b, green );
All = list( s1b, s2b );
interact( All );

save( "ssi3", All );

#
# 4. Two biquadratic polynomial Bspline surfaces. Intersection is open.
#

s1 = sbspline( 3, 3,
	       list( list( ctlpt( E3, 0.1, 0.0, 1.0 ),
                           ctlpt( E3, 0.3, 1.0, 0.5 ),
                           ctlpt( E3, 0.0, 2.0, 1.0 ) ),
                     list( ctlpt( E3, 1.1, 0.0, 0.5 ),
                           ctlpt( E3, 1.3, 1.0, 0.0 ),
                           ctlpt( E3, 1.0, 2.0, 0.5 ) ),
                     list( ctlpt( E3, 2.1, 0.0, 1.1 ),
                           ctlpt( E3, 2.3, 1.0, 0.4 ),
                           ctlpt( E3, 2.0, 2.0, 1.2 ) ),
                     list( ctlpt( E3, 3.1, 0.0, 1.9 ),
                           ctlpt( E3, 3.3, 1.1, 1.4 ),
                           ctlpt( E3, 3.0, 2.0, 1.9 ) ),
                     list( ctlpt( E3, 4.1, 0.0, 1.1 ),
                           ctlpt( E3, 4.3, 1.0,-0.4 ),
                           ctlpt( E3, 4.0, 2.2, 1.2 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) );
s2 = sbspline( 3, 3,
	       list( list( ctlpt( E3, 0.1, 0.0, 1.0 ),
                           ctlpt( E3, 0.3, 0.7, 0.5 ),
                           ctlpt( E3, 0.1, 1.2, 1.0 ),
                           ctlpt( E3, 0.0, 2.0, 0.5 ) ),
                     list( ctlpt( E3, 1.1, 0.0, 0.5 ),
                           ctlpt( E3, 1.3, 1.0, 0.0 ),
                           ctlpt( E3, 1.1, 1.3, 0.5 ),
                           ctlpt( E3, 1.0, 2.0, 0.5 ) ),
                     list( ctlpt( E3, 2.1, 0.0, 1.1 ),
                           ctlpt( E3, 2.3, 0.5, 0.4 ),
                           ctlpt( E3, 2.0, 1.0, 1.3 ),
                           ctlpt( E3, 2.0, 2.0, 0.4 ) ),
                     list( ctlpt( E3, 3.1, 0.0, 1.9 ),
                           ctlpt( E3, 3.3, 0.7, 1.4 ),
                           ctlpt( E3, 3.1, 1.1, 1.5 ),
                           ctlpt( E3, 3.1, 2.0, 1.9 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) ) *
	rotx( 90 ) * trans( vector( 1.5, 1.5, 0 ) );
color( s1, red );
color( s2, green );
All = list( s1, s2 );
interact( All );

save( "ssi4", All );

#
# Two biquadratic rational surface intersection - a cone and a sphere.
#

s1 = coneSrf( 3.0, 0.7 );
s2 = sphereSrf( 0.8 ) * trans( vector( 0.35, 0.65, 1.3 ) );
color( s1, red );
color( s2, green );
All = list( s1, s2 );
interact( All );

save( "ssi5", All );

#
# Same as 5, but the poles of the sphere are on the cone's surface.
#

s1 = coneSrf( 3.0, 0.7 );
s2 = sphereSrf( 0.8 ) * roty( -atan2( 0.7, 3.0 ) * 180 / Pi )
		      * trans( vector( 0.35, 0.0, 1.5 ) );
color( s1, red );
color( s2, green );
All = list( s1, s2 );
interact( All );

save( "ssi6", All );

#
# Four different and isolated intersection loops between two bicubic
# Bspline surfaces.
#

s1 = sbspline( 4, 4,
	       list( list( ctlpt( E3, 0.1, 0.0, 0.51 ),
                           ctlpt( E3, 0.4, 1.0, 0.52 ),
                           ctlpt( E3, 0.2, 2.2, 0.5 ),
                           ctlpt( E3, 0.4, 3.5, 0.49 ),
                           ctlpt( E3, 0.0, 4.3, 0.52 ) ),
                     list( ctlpt( E3, 1.1, 0.3, 0.53 ),
                           ctlpt( E3, 1.3, 1.3, 2.7 ),
                           ctlpt( E3, 1.1, 2.2,-1.4 ),
                           ctlpt( E3, 1.1, 3.3, 3.1 ),
                           ctlpt( E3, 1.2, 4.2, 0.48 ) ),
                     list( ctlpt( E3, 2.1, 0.1, 0.47 ),
                           ctlpt( E3, 2.4, 1.1, 0.52 ),
                           ctlpt( E3, 2.3, 2.0, 0.51 ),
                           ctlpt( E3, 2.4, 3.3, 0.52 ),
                           ctlpt( E3, 2.0, 4.0, 0.53 ) ),
                     list( ctlpt( E3, 3.1, 0.4, 0.49 ),
                           ctlpt( E3, 3.3, 1.3, 2.6 ),
                           ctlpt( E3, 2.9, 2.1,-1.9 ),
                           ctlpt( E3, 2.9, 3.5, 2.0 ),
                           ctlpt( E3, 3.0, 4.6, 0.51 ) ),
                     list( ctlpt( E3, 4.1, 0.1, 0.53 ),
                           ctlpt( E3, 4.0, 1.2, 0.45 ),
                           ctlpt( E3, 4.3, 2.0, 0.51 ),
                           ctlpt( E3, 3.9, 3.4, 0.55 ),
                           ctlpt( E3, 4.0, 4.2, 0.51 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) );
s2 = sbspline( 4, 4,
	       list( list( ctlpt( E3, 0.1, 0.0, 1.85 ),
                           ctlpt( E3, 0.4, 1.0, 1.9 ),
                           ctlpt( E3, 0.2, 2.2, 1.95 ),
                           ctlpt( E3, 0.4, 3.5, 1.7 ),
                           ctlpt( E3, 0.0, 4.3, 1.8 ) ),
                     list( ctlpt( E3, 1.1, 0.3, 1.88 ),
                           ctlpt( E3, 1.3, 1.3,-1.1 ),
                           ctlpt( E3, 1.1, 2.2, 2.85 ),
                           ctlpt( E3, 1.1, 3.3,-0.95 ),
                           ctlpt( E3, 1.2, 4.2, 1.7 ) ),
                     list( ctlpt( E3, 2.1, 0.1, 1.9 ),
                           ctlpt( E3, 2.4, 1.1, 1.8 ),
                           ctlpt( E3, 2.3, 2.0, 1.85 ),
                           ctlpt( E3, 2.4, 3.3, 1.65 ),
                           ctlpt( E3, 2.0, 4.0, 1.75 ) ),
                     list( ctlpt( E3, 3.1, 0.4, 1.85 ),
                           ctlpt( E3, 3.3, 1.3,-0.9 ),
                           ctlpt( E3, 2.9, 2.1, 2.4 ),
                           ctlpt( E3, 2.9, 3.5,-0.9 ),
                           Ctlpt( E3, 3.0, 4.6, 1.8 ) ),
                     list( ctlpt( E3, 4.1, 0.1, 1.85 ),
                           ctlpt( E3, 4.0, 1.2, 1.75 ),
                           ctlpt( E3, 4.3, 2.0, 1.65 ),
                           ctlpt( E3, 3.9, 3.4, 1.95 ),
                           ctlpt( E3, 4.0, 4.2, 1.85 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) );
color( s1, red );
color( s2, green );
All = list( s1, s2 );
interact( All );

save( "ssi7", All );


#
# Same as 7 but we scale the Z axis by 0.1 and by 0.01 to create almost
# tangent surfaces.
#

s1a = s1 * sz( 0.1 );
s2a = s2 * sz( 0.1 );
color( s1a, red );
color( s2a, green );
All = list( s1a, s2a );
interact( All );

save( "ssi8", All  );

s1b = s1 * sz( 0.01 );
s2b = s2 * sz( 0.01 );
color( s1b, red );
color( s2b, green );
All = list( s1b, s2b );
interact( All );

save( "ssi9", All  );

#
# Two different intersection curves that are very close to each other.
# Intersection between two biquadratic Bezier saddle like surfaces.
# In the last example of this sequence, the surfaces are tangent at
# the center point, s1( 0.5, 0.5 ) = s2( 0.5, 0.5 ) ~= (1.175, 1.13, 1.49 )
#

s1 = sbezier( list( list( ctlpt( E3, 0.1, 0.0, 1.6 ),
                          ctlpt( E3, 0.3, 1.1, 0.4 ),
                          ctlpt( E3, 0.0, 2.2, 1.5 ) ),
                    list( ctlpt( E3, 1.1, 0.2, 3.0 ),
                          ctlpt( E3, 1.3, 1.0, 1.4 ),
                          ctlpt( E3, 1.0, 2.2, 2.7 ) ),
                    list( ctlpt( E3, 2.1, 0.1, 1.4 ),
                          ctlpt( E3, 2.3, 1.3, 0.2 ),
                          ctlpt( E3, 2.0, 2.2, 1.2 ) ) ) );
p = seval( s1, 0.5, 0.5 );

s2a = s1 * trans( -coerce( p, vector_type ) )
         * scale( vector( 1.2, 1.1, -0.5 ) )
         * rotz( 15 )
	 * trans( vector( coord( p, 1 ), coord( p, 2 ), coord( p, 3 ) + 0.1 ) );

color( s1, red );
color( s2a, green );
All = list( s1, s2a );
interact( All );

save( "ssi10", All );

s2b = s1 * trans( -coerce( p, vector_type ) )
         * scale( vector( 1.2, 1.1, -0.5 ) )
         * rotz( 15 )
	 * trans( vector( coord( p, 1 ), coord( p, 2 ), coord( p, 3 ) + 0.01 ) );

color( s1, red );
color( s2b, green );
All = list( s1, s2b );
interact( All );

save( "ssi11", All );

s2c = s1 * trans( -coerce( p, vector_type ) )
         * scale( vector( 1.2, 1.1, -0.5 ) )
         * rotz( 15 )
	 * trans( vector( coord( p, 1 ), coord( p, 2 ), coord( p, 3 ) + 0.001 ) );

color( s1, red );
color( s2c, green );
All = list( s1, s2c );
interact( All );

save( "ssi12", All );

s2d = s1 * trans( -coerce( p, vector_type ) )
         * scale( vector( 1.2, 1.1, -0.5 ) )
         * rotz( 15 )
	 * trans( vector( coord( p, 1 ), coord( p, 2 ), coord( p, 3 ) ) );

color( s1, red );
color( s2d, green );
All = list( s1, s2d );
interact( All );

save( "ssi13", All );

free( p );
#
# Another case of almost tangency. Here we have a fairly flat surface and
# an elliptic surface almots tangent (first case), tangent at a point (second
# case) and intersects in a tiny loop (third case). Both happens at the
# centers of the surfaces
#     s1( 1.5, 1.0 ) ~= s2( 1.0, 1.5 ) ~= ( 2.28, 2.14, 0.48 )
# Both surfaces are cubic by quadratic Bspline surfaces.
#

s1 = sbspline( 3, 4,
	       list( list( ctlpt( E3, 0.1, 0.0, 0.3 ),
                           ctlpt( E3, 0.4, 1.0, 0.1 ),
                           ctlpt( E3, 0.2, 2.2, 0.5 ),
                           ctlpt( E3, 0.4, 3.5, 0.1 ),
                           ctlpt( E3, 0.0, 4.3, 0.2 ) ),
                     list( ctlpt( E3, 1.1, 0.3, 0.15 ),
                           ctlpt( E3, 1.3, 1.3, 0.3 ),
                           ctlpt( E3, 1.1, 2.2, 0.2 ),
                           ctlpt( E3, 1.1, 3.3, 0.25 ),
                           ctlpt( E3, 1.2, 4.2, 0.0 ) ),
                     list( ctlpt( E3, 2.1, 0.1, 0.4 ),
                           ctlpt( E3, 2.4, 1.1, 0.7 ),
                           ctlpt( E3, 2.3, 2.0, 0.35 ),
                           ctlpt( E3, 2.4, 3.3, 0.22 ),
                           ctlpt( E3, 2.0, 4.0, 0.35 ) ),
                     list( ctlpt( E3, 3.1, 0.4, 0.11 ),
                           ctlpt( E3, 3.3, 1.3, 0.1 ),
                           ctlpt( E3, 2.9, 2.1, 0.2 ),
                           ctlpt( E3, 2.9, 3.5, 0.3 ),
                           Ctlpt( E3, 3.0, 4.6, 0.3 ) ),
                     list( ctlpt( E3, 4.1, 0.1, 0.12 ),
                           ctlpt( E3, 4.0, 1.2, 0.05 ),
                           ctlpt( E3, 4.3, 2.0, 0.33 ),
                           ctlpt( E3, 3.9, 3.4, 0.13 ),
                           ctlpt( E3, 4.0, 4.2, 0.27 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) );
s2 = sbspline( 4, 3,
	       list( list( ctlpt( E3, 0.1, 0.0, 1.85 ),
                           ctlpt( E3, 0.4, 1.0, 1.9 ),
                           ctlpt( E3, 0.2, 2.2, 1.95 ),
                           ctlpt( E3, 0.4, 3.5, 1.7 ),
                           ctlpt( E3, 0.0, 4.3, 1.8 ) ),
                     list( ctlpt( E3, 1.1, 0.3, 1.88 ),
                           ctlpt( E3, 1.3, 1.3, 1.6 ),
                           ctlpt( E3, 1.1, 2.2, 0.7 ),
                           ctlpt( E3, 1.1, 3.3, 1.5 ),
                           ctlpt( E3, 1.2, 4.2, 1.7 ) ),
                     list( ctlpt( E3, 2.1, 0.1, 1.9 ),
                           ctlpt( E3, 2.4, 1.1, 0.5 ),
                           ctlpt( E3, 2.3, 2.0, 0.1 ),
                           ctlpt( E3, 2.4, 3.3, 0.5 ),
                           ctlpt( E3, 2.0, 4.0, 1.75 ) ),
                     list( ctlpt( E3, 3.1, 0.4, 1.85 ),
                           ctlpt( E3, 3.3, 1.3, 1.3 ),
                           ctlpt( E3, 2.9, 2.1, 0.5 ),
                           ctlpt( E3, 2.9, 3.5, 1.4 ),
                           Ctlpt( E3, 3.0, 4.6, 1.8 ) ),
                     list( ctlpt( E3, 4.1, 0.1, 1.85 ),
                           ctlpt( E3, 4.0, 1.2, 1.75 ),
                           ctlpt( E3, 4.3, 2.0, 1.65 ),
                           ctlpt( E3, 3.9, 3.4, 1.95 ),
                           ctlpt( E3, 4.0, 4.2, 1.85 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) );

#
# Compute a rotation matrix that rotates s2 so that its tangent plane at
# (0.5, 0.5) is the same as the tangent plane of s1 at (0.5, 0.5).
#
nrml1 = normalize( coerce( seval( snrmlsrf( s1 ), 1.5, 1.0 ), vector_type ) );
tan1a = normalize( coerce( seval( sderive( s1, ROW ), 1.5, 1.0 ),
			   vector_type ) );
tan1b = tan1a ^ nrml1;
rot1 = homomat( list(
	list( coord( tan1a, 0 ), coord( tan1a, 1 ), coord( tan1a, 2 ), 0 ),
	list( coord( tan1b, 0 ), coord( tan1b, 1 ), coord( tan1b, 2 ), 0 ),
	list( coord( nrml1, 0 ), coord( nrml1, 1 ), coord( nrml1, 2 ), 0 ),
	list( 0, 0, 0, 1 ) ) );

free( tan1a );
free( tan1b );
free( nrml1 );

nrml2 = normalize( coerce( seval( snrmlsrf( s2 ), 1.0, 1.5 ), vector_type ) );
tan2a = normalize( coerce( seval( sderive( s2, ROW ), 1.0, 1.5 ),
			   vector_type ) );
tan2b = tan2a ^ nrml2;
rot2 = homomat( list(
	list( coord( tan2a, 0 ), coord( tan2a, 1 ), coord( tan2a, 2 ), 0 ),
	list( coord( tan2b, 0 ), coord( tan2b, 1 ), coord( tan2b, 2 ), 0 ),
	list( coord( nrml2, 0 ), coord( nrml2, 1 ), coord( nrml2, 2 ), 0 ),
	list( 0, 0, 0, 1 ) ) );

free( tan2a );
free( tan2b );
free( nrml2 );

RotMat = rot2^-1 * rot1;
free( rot1 );
free( rot2 );

#
# Apply the rotation matrix.
#
s2r = s2 * rotmat;
free( RotMat );

#
# Prove it: here are the normals of both surfaces at (0.5, 0.5).
#
normalize( coerce( seval( snrmlsrf( s1 ), 1.5, 1.0 ), vector_type ) );
normalize( coerce( seval( snrmlsrf( s2r ), 1.0, 1.5 ), vector_type ) );

pt1 = seval( s1, 1.5, 1.0 );
pt2 = seval( s2r, 1.0, 1.5 );

s2a = s2r * trans( vector( coord( pt1, 1 ) - coord( pt2, 1 ),
		           coord( pt1, 2 ) - coord( pt2, 2 ),
		           coord( pt1, 3 ) - coord( pt2, 3 ) + 0.01 ) );
color( s1, red );
color( s2a, green );
All = list( s1, s2a );
interact( All );

save( "ssi14", All );

s2b = s2r * trans( vector( coord( pt1, 1 ) - coord( pt2, 1 ),
		           coord( pt1, 2 ) - coord( pt2, 2 ),
		           coord( pt1, 3 ) - coord( pt2, 3 ) ) );
color( s1, red );
color( s2b, green );
All = list( s1, s2b );
interact( All );

save( "ssi15", All );

s2c = s2r * trans( vector( coord( pt1, 1 ) - coord( pt2, 1 ),
		           coord( pt1, 2 ) - coord( pt2, 2 ),
		           coord( pt1, 3 ) - coord( pt2, 3 ) - 0.01 ) );
color( s1, red );
color( s2c, green );
All = list( s1, s2c );
interact( All );

save( "ssi16", All );

free( s2r );
free( pt1 );
free( pt2 );

#
# A complex but single intersection curve. This is between two quadratic
# Bspline surfaces.
# 

s1 = sbspline( 3, 3,
	       list( list( ctlpt( E3, 0.1, 0.0, 0.51 ),
                           ctlpt( E3, 0.4, 1.0, 0.52 ),
                           ctlpt( E3, 0.2, 2.2, 0.5 ),
                           ctlpt( E3, 0.4, 3.5, 0.49 ),
                           ctlpt( E3, 0.0, 4.3, 0.52 ) ),
                     list( ctlpt( E3, 1.1, 0.3, 0.53 ),
                           ctlpt( E3, 1.3, 1.3, 1.7 ),
                           ctlpt( E3, 1.1, 2.2,-0.4 ),
                           ctlpt( E3, 1.1, 3.3, 1.1 ),
                           ctlpt( E3, 1.2, 4.2, 0.48 ) ),
                     list( ctlpt( E3, 2.1, 0.1, 0.47 ),
                           ctlpt( E3, 2.4, 1.1, 1.52 ),
                           ctlpt( E3, 2.3, 2.0, 0.51 ),
                           ctlpt( E3, 2.4, 3.3, 1.52 ),
                           ctlpt( E3, 2.0, 4.0, 0.53 ) ),
                     list( ctlpt( E3, 3.1, 0.4, 0.49 ),
                           ctlpt( E3, 3.3, 1.3, 1.6 ),
                           ctlpt( E3, 2.9, 2.1,-0.9 ),
                           ctlpt( E3, 2.9, 3.5, 1.0 ),
                           ctlpt( E3, 3.0, 4.6, 0.51 ) ),
                     list( ctlpt( E3, 4.1, 0.1, 0.53 ),
                           ctlpt( E3, 4.0, 1.2, 0.45 ),
                           ctlpt( E3, 4.3, 2.0, 0.51 ),
                           ctlpt( E3, 3.9, 3.4, 0.55 ),
                           ctlpt( E3, 4.0, 4.2, 0.51 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) );

s2 = sbspline( 3, 3,
	       list( list( ctlpt( E3, 0.1, 0.0, 1.45 ),
                           ctlpt( E3, 0.4, 1.0, 1.5 ),
                           ctlpt( E3, 0.2, 2.2, 1.55 ),
                           ctlpt( E3, 0.4, 3.5, 1.3 ),
                           ctlpt( E3, 0.0, 4.3, 1.4 ) ),
                     list( ctlpt( E3, 1.1, 0.3, 1.48 ),
                           ctlpt( E3, 1.3, 1.3,-0.5 ),
                           ctlpt( E3, 1.1, 2.2, 1.45 ),
                           ctlpt( E3, 1.1, 3.3,-0.5 ),
                           ctlpt( E3, 1.2, 4.2, 1.3 ) ),
                     list( ctlpt( E3, 2.1, 0.1, 2.5 ),
                           ctlpt( E3, 2.4, 1.1, 2.4 ),
                           ctlpt( E3, 2.3, 2.0, 0.1 ),
                           ctlpt( E3, 2.4, 3.3, 2.4 ),
                           ctlpt( E3, 2.0, 4.0, 2.25 ) ),
                     list( ctlpt( E3, 3.1, 0.4, 1.45 ),
                           ctlpt( E3, 3.3, 1.3,-0.5 ),
                           ctlpt( E3, 2.9, 2.1, 1.0 ),
                           ctlpt( E3, 2.9, 3.5,-0.3 ),
                           Ctlpt( E3, 3.0, 4.6, 1.4 ) ),
                     list( ctlpt( E3, 4.1, 0.1, 1.45 ),
                           ctlpt( E3, 4.0, 1.2, 1.35 ),
                           ctlpt( E3, 4.3, 2.0, 1.25 ),
                           ctlpt( E3, 3.9, 3.4, 1.55 ),
                           ctlpt( E3, 4.0, 4.2, 1.45 ) ) ),
	       list( list( KV_OPEN ),
		     list( KV_OPEN ) ) ) * tz( -0.26 );
color( s1, red );
color( s2, green );
All = list( s1, s2 );

interact( All );

save( "ssi17", All );

free( All );
free( s1 );
free( s2 );
free( s1a );
free( s1b );
free( s2a );
free( s2b );
free( s2c );
free( s2d );
