#
# Self and partial intersections' examples
#
#					Gershon Elber, October 1995.
#

save_mat = view_mat;
save_res = resolution;

#
# A non complete intersection between polyhedra.
#
v1  = vector( 0.0, 0.0, 0.0);
v2  = vector( 1.0, 0.0, 0.0);
v3  = vector( 1.0, 1.0, 0.0);
v4  = vector( 0.0, 1.0, 0.0);

P = poly( list( v1, v2, v3, v4 ), false );
free( v1 );
free( v2 );
free( v3 );
free( v4 );

C = cylin( vector( 0.1, 0.5, -0.5 ), vector( 0, 0, 1 ), 0.3, 3 );

#
# Boolean operation will fail here since the intersection is NOT CLOSED.
# One is only able to extract the itersection curves then.
#
# Cntrs = P + C;
#

IntrCrv = iritstate( "InterCrv", true );
Cntrs = P + C;
attrib( Cntrs, "dwidth", 3 );
color( Cntrs, red );
interact( list( C, P, Cntrs ) );

free( C );
free( P );
free( Cntrs );

#
# Simple self intersecting surface.
#
c1 = cbspline( 3,
	       list( ctlpt( E3,  0.0, -1.0, 0.0 ),
		     ctlpt( E3,  0.0,  1.0, 0.2 ),
		     ctlpt( E3,  1.0,  0.0, 0.4 ),
		     ctlpt( E3, -1.0,  0.0, 0.6 ) ),
	       list( KV_OPEN ) );
s1 = ruledSrf( c1, c1 * tz( 1.0 ) * rz( 10 ) );
color( s1, green );

resolution = 30;

#
# Computing self intersection by a Boolean operation with itself - Parametric.
#
dummy = iritstate( "InterCrv", true );
UVBool = iritstate( "UVBoolean", true );
s1Inter = s1 + s1;
color( s1Inter, red );
attrib( s1Inter, "dwidth", 3 );

ParamDomain = poly( list( vector( 0, 0, 0 ),
			  vector( 0, 1, 0 ),
			  vector( 2, 1, 0 ),
			  vector( 2, 0, 0 ) ), false );
color( ParamDomain, green );

view_mat = tx( -1.0 ) * sc( 0.6 );
interact( list( view_mat, ParamDomain, s1Inter ) );

#
# Computing self intersection by a Boolean operation with itself - Euclidean.
#
dummy = iritstate( "InterCrv", true );
dummy = iritstate( "UVBoolean", false );
s1Inter = s1 + s1;
color( s1Inter, red );
attrib( s1Inter, "dwidth", 5 );

view_mat = save_mat * sc( 0.7 ) * ty( -0.3 );
interact( list( view_mat, s1, s1Inter ) );
save( "selfint1", list( s1, s1Inter ) );

free( s1 );
free( s1Inter );

#
# A self intersecting offset.
#
c1 = cbspline( 4,
	       list( ctlpt( E3, -1.1, -1,    0 ),
		     ctlpt( E3, -1,   -0.5,  0.1 ),
		     ctlpt( E3, -1,   -0.1,  0.1 ),
		     ctlpt( E3, -1.2,  0.2,  0.1 ),
		     ctlpt( E3, -1,    0.5,  0.1 ),
		     ctlpt( E3, -0.9,  1,    0 ) ),
	       list( KV_OPEN ) );
c2 = cbspline( 4,
	       list( ctlpt( E3, -0.7, -0.9,  0 ),
		     ctlpt( E3, -0.6, -0.4,  0.1 ),
		     ctlpt( E3, -0.5, -0.0,  0.1 ),
		     ctlpt( E3, -0.5,  0.3,  0.6 ),
		     ctlpt( E3, -0.7,  0.7,  0.2 ),
		     ctlpt( E3, -0.7,  1.1,  0 ) ),
	       list( KV_OPEN ) );
c3 = cbspline( 4,
	       list( ctlpt( E3, -0.1, -1,    0 ),
		     ctlpt( E3, -0.2, -0.6,  0.1 ),
		     ctlpt( E3, -0.4,  0.1,  0.1 ),
		     ctlpt( E3, -0.1,  0.6, -2.0 ),
		     ctlpt( E3, -0.4,  0.7,  0.2 ),
		     ctlpt( E3, -0.1,  1.1,  0 ) ),
	       list( KV_OPEN ) );
c4 = cbspline( 4,
	       list( ctlpt( E3,  0.5, -1,    0 ),
		     ctlpt( E3,  0.4, -0.7,  0.1 ),
		     ctlpt( E3,  0.6, -0.3,  0.2 ),
		     ctlpt( E3,  0.4,  0.1, -0.4 ),
		     ctlpt( E3,  0.5,  0.7,  0.2 ),
		     ctlpt( E3,  0.7,  1,    0.2 ) ),
	       list( KV_OPEN ) );
c5 = cbspline( 4,
	       list( ctlpt( E3,  1.2, -1,    0.1 ),
		     ctlpt( E3,  1,   -0.5,  0.1 ),
		     ctlpt( E3,  1,   -0.2,  0.2 ),
		     ctlpt( E3,  1.1,  0.3,  0.0 ),
		     ctlpt( E3,  1,    0.6,  0.1 ),
		     ctlpt( E3,  0.9,  1,    0 ) ),
	       list( KV_OPEN ) );

s2 = sFromCrvs( list( c1, c2, c3, c4, c5 ), 3, KV_OPEN );

ref_knots = list( 0.2, 0.2667, 0.4, 0.4667, 0.5333, 0.6, 0.7333, 0.8 );
s2r = srefine( srefine( s2, row, false, ref_knots ), col, false, ref_knots );
free( ref_knots );

s2o = offset( s2r, 0.3, 10.0, false );
color( s2o, green );

resolution = 20;

#
# Computing self intersection by a Boolean operation with itself - Parametric.
#
dummy = iritstate( "InterCrv", true );
dummy = iritstate( "UVBoolean", true );
s2oInter = s2o + s2o;
color( s2oInter, red );
attrib( s2oInter, "dwidth", 3 );

ParamDomain = poly( list( vector( 0, 0, 0 ),
			  vector( 0, 1, 0 ),
			  vector( 1, 1, 0 ),
			  vector( 1, 0, 0 ) ), false );
color( ParamDomain, green );

view_mat = tx( -0.5 ) * ty( -0.5 ) * sc( 1 );
interact( list( view_mat, ParamDomain, s2oInter ) );

#
# Computing self intersection by a Boolean operation with itself - Euclidean.
#
dummy = iritstate( "InterCrv", true );
dumm = iritstate( "UVBoolean", false );
s2oInter = s2o + s2o;

color( s2oInter, red );
attrib( s2oInter, "dwidth", 3 );

view_mat = save_mat;
interact( list( view_mat, s2o, s2oInter ) );
save( "selfint2", list( s2o, s2oInter ) );

free( c1 );
free( c2 );
free( c3 );
free( c4 );
free( c5 );
free( s2 );
free( s2oInter );
free( s2o );
free( s2r );
free( ParamDomain );

resolution = save_res;
view_mat = save_mat;

dummy = iritstate( "InterCrv", IntrCrv );
free( IntrCrv );
dummy = iritstate( "UVBoolean", UVBool );
free( UVBool );
