#
# Distance Tests for curves and surfaces, Gershon Elber, Dec 2003.
#

TestCCDistFunc = function( Crv1, Crv2, Refs ):
    Crv1R: Crv2R: i: bb: Res: All1: All2: All3:

    if ( sizeof( Refs ) > 0,
	Crv1 = crefine( Crv1, false, Refs ):
	Crv2 = crefine( Crv2, false, Refs ) ):

    view( list( axes, Crv1, Crv2 ), 1 ):

    bb = bbox( dist2ff( Crv1, Crv2, 1 ) ):
    if ( nth( bb, 1 ) * nth( bb, 2 ) > 0,
	Res = "Successful",
	Res = "Failed" ):
    All1 = list( nth( bb, 1 ), nth( bb, 2 ), Res ):
    printf( "Distace Square         bound from %8.5lf to %8.5lf  (%s)\\n",
	    All1 ):

    bb = bbox( dist2ff( Crv1, Crv2, 2 ) ):
    if ( nth( bb, 1 ) * nth( bb, 2 ) > 0,
	Res = "Successful",
	Res = "Failed" ):
    All2 = list( nth( bb, 1 ), nth( bb, 2 ), Res ):
    printf( "Projection on Crv1Nrml bound from %8.5lf to %8.5lf  (%s)\\n",
	    All2 ):

    bb = bbox( dist2ff( Crv1, Crv2, 3 ) ):
    if ( nth( bb, 1 ) * nth( bb, 2 ) > 0,
	Res = "Successful",
	Res = "Failed" ):
    All3 = list( nth( bb, 1 ), nth( bb, 2 ), Res ):
    printf( "Projection on Crv2Nrml bound from %8.5lf to %8.5lf  (%s)\\n",
	    All3 ):

    pause():

    return = list( All1, All2, All3 );

TestSSDistFunc = function( Srf1, Srf2, Refs ):
    Srf1R: Srf2R: i: bb: Res: All1: All2: All3:

    if ( sizeof( Refs ) > 0,
	Srf1 = srefine( srefine( Srf1, row, false, Refs ), col, false, Refs ):
	Srf2 = srefine( srefine( Srf2, row, false, Refs ), col, false, Refs ) ):

    view( list( axes, Srf1, Srf2 ), 1 ):

    bb = bbox( dist2ff( Srf1, Srf2, 1 ) ):
    if ( nth( bb, 1 ) * nth( bb, 2 ) > 0,
	Res = "Successful",
	Res = "Failed" ):
    All1 = list( nth( bb, 1 ), nth( bb, 2 ), Res ):
    printf( "Distace Square         bound from %8.5lf to %8.5lf  (%s)\\n",
	    All1 ):

    bb = bbox( dist2ff( Srf1, Srf2, 2 ) ):
    if ( nth( bb, 1 ) * nth( bb, 2 ) > 0,
	Res = "Successful",
	Res = "Failed" ):
    All2 = list( nth( bb, 1 ), nth( bb, 2 ), Res ):
    printf( "Projection on Srf1Nrml bound from %8.5lf to %8.5lf  (%s)\\n",
	    All2 ):

    bb = bbox( dist2ff( Srf1, Srf2, 3 ) ):
    if ( nth( bb, 1 ) * nth( bb, 2 ) > 0,
	Res = "Successful",
	Res = "Failed" ):
    All3 = list( nth( bb, 1 ), nth( bb, 2 ), Res ):
    printf( "Projection on Srf2Nrml bound from %8.5lf to %8.5lf  (%s)\\n",
	    All3 ):

    pause():

    return = list( All1, All2, All3 );

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

Crv1a = cbezier( list( ctlpt( E1, .2 ),
                       ctlpt( E2, 0.5, 4 ),
                       ctlpt( E2, 1.3, 0.05 ) ) ) * sy( 0.2 ):

Crv2a = cbezier( list( ctlpt( E1, -.2 ),
                       ctlpt( E2, 0.25, 1.9 ),
                       ctlpt( E2, 1.3, 0.05 ) ) ) * ty( 0.3 ) * sx( 1.5 ):

Crv1b = cbezier( list( ctlpt( E2, .2, -0.5 ),
                       ctlpt( E2, 0.5, 4 ),
                       ctlpt( E2, 1.3, -0.45 ) ) ) * sy( 0.2 ):

Crv2b = cbezier( list( ctlpt( E2, -.0, -0.5 ),
                       ctlpt( E2, 0.25, 1.09 ),
                       ctlpt( E2, 1.1, -0.5 ) ) ) * ty( 0.3 ) * sx( 1.5 ):

save( "dist2ff1",
      list( TestCCDistFunc( Crv1a, Crv2a, nil() ),
	    TestCCDistFunc( Crv1a, Crv2a, list( 0.5 ) ),
	    TestCCDistFunc( Crv1b, Crv2b, nil() ),
	    TestCCDistFunc( Crv1b, Crv2b, list( 0.5 ) ) ) );

free( Crv1a );
free( Crv2a );
free( Crv1b );
free( Crv2b );

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

Crv1a = cbezier( list( ctlpt( E1, .2 ),
                       ctlpt( E2, 0.95, 3.6 ),
                       ctlpt( E2, 0.35, 3.6 ),
                       ctlpt( E2, 1.3, 0.05 ) ) ) * sy( 0.2 ):

Crv2a = cbezier( list( ctlpt( E1, -.4 ),
                       ctlpt( E2, 0.05, 0.9 ),
                       ctlpt( E2, 0.45, 1.1 ),
                       ctlpt( E2, 0.65, 0.5 ),
                       ctlpt( E2, 1.3, 0.05 ) ) ) * ty( 0.3 ) * sx( 1.5 ):

Crv1b = cbezier( list( ctlpt( E1, .2 ),
                       ctlpt( E2, 0.95, 3.6 ),
                       ctlpt( E2, 0.35, 3.6 ),
                       ctlpt( E2, 1.3, 0.05 ) ) ) * sy( 0.2 ):

Crv2b = cbezier( list( ctlpt( E1, -.4 ),
                       ctlpt( E2, 0.05, 0.9 ),
                       ctlpt( E2, 0.5, 0.7 ),
                       ctlpt( E2, 0.35, 0.5 ),
                       ctlpt( E2, 1.1, -0.5 ) ) ) * ty( 0.3 ) * sx( 1.5 ):

save( "dist2ff2",
      list( TestCCDistFunc( Crv1a, Crv2a, nil() ),
            TestCCDistFunc( Crv1a, Crv2a, list( 0.5 ) ),
            TestCCDistFunc( Crv1b, Crv2b, nil() ),
            TestCCDistFunc( Crv1b, Crv2b, list( 0.5 ) ) ) );

free( Crv1a );
free( Crv2a );
free( Crv1b );
free( Crv2b );

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

Crv = cbezier( list( ctlpt( E1, -.2 ),
                     ctlpt( E2, 0.25, 1.2 ),
                     ctlpt( E2, 0.75, 0.7 ),
                     ctlpt( E2, 1.3, 0.05 ) ) ):

Crv1 = -Crv:
Crv2a = offset( Crv1, -0.8, 1, off ):

Crv2b = offset( Crv1, -0.18, 1, off ):

save( "dist2ff3",
      list( TestCCDistFunc( Crv1, Crv2a, nil() ),
	    TestCCDistFunc( Crv1, Crv2a, list( 0.5 ) ),
	    TestCCDistFunc( Crv1, Crv2b, nil() ),
	    TestCCDistFunc( Crv1, Crv2b, list( 0.5 ) ) ) );
free( Crv );
free( Crv1 );
free( Crv2a );
free( Crv2b );

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

Crv1a = cbezier( list( ctlpt( E1, -.2 ),
                       ctlpt( E2, 0.25, 1.2 ),
                       ctlpt( E2, 0.75, 0.7 ),
                       ctlpt( E2, 1.3, 0.05 ) ) ):

Crv2a = cbezier( list( ctlpt( E1, 0 ),
                       ctlpt( E2, 0.5, -0.1 ),
                       ctlpt( E2, 0.5, 0.45 ) ) ):

Crv1b = cbezier( list( ctlpt( E1, -.2 ),
                       ctlpt( E2, 0.25, 1.0 ),
                       ctlpt( E2, 0.75, 0.3 ),
                       ctlpt( E2, 1.3, -0.05 ) ) ):

Crv2b = cbezier( list( ctlpt( E1, 0 ),
                       ctlpt( E2, 0.5, -0.1 ),
                       ctlpt( E2, 0.5, 0.45 ) ) ):

save( "dist2ff4",
      list( TestCCDistFunc( Crv1a, Crv2a, nil() ),
	    TestCCDistFunc( Crv1a, Crv2a, list( 0.5 ) ),
	    TestCCDistFunc( Crv1b, Crv2b, nil() ),
	    TestCCDistFunc( Crv1b, Crv2b, list( 0.5 ) ) ) );

free( Crv1a );
free( Crv2a );
free( Crv1b );
free( Crv2b );

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

Crv1a = cbezier( list( ctlpt( E1, -.5 ),
                       ctlpt( E2, 0.25, 0.7 ),
                       ctlpt( E2, 0.75, 0.6 ),
                       ctlpt( E2, 1.3, 0.05 ) ) ):

Crv2a = cbezier( list( ctlpt( E1, 0 ),
                       ctlpt( E2, 0.5, -0.1 ),
                       ctlpt( E2, 0.1, 0.3 ),
                       ctlpt( E2, 0.5, 0.05 ) ) ):

Crv1b = cbezier( list( ctlpt( E1, -.5 ),
                       ctlpt( E2, 0.25, 0.35 ),
                       ctlpt( E2, 0.45, 0.2 ),
                       ctlpt( E2, 0.7, -0.15 ) ) ):

Crv2b = cbezier( list( ctlpt( E1, 0 ),
                       ctlpt( E2, 0.5, -0.1 ),
                       ctlpt( E2, 0.1, 0.3 ),
                       ctlpt( E2, 0.5, 0.05 ) ) ):

save( "dist2ff5",
      list( TestCCDistFunc( Crv1a, Crv2a, nil() ),
	    TestCCDistFunc( Crv1a, Crv2a, list( 0.5 ) ),
	    TestCCDistFunc( Crv1b, Crv2b, nil() ),
	    TestCCDistFunc( Crv1b, Crv2b, list( 0.5 ) ) ) );

free( Crv1a );
free( Crv2a );
free( Crv1b );
free( Crv2b );

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

SAD1 = sbezier(
    list( list( ctlpt( E3, 0, 0, 0 ),
                ctlpt( E3, 0.05, 0.2, 0.1 ),
                ctlpt( E3, 0.1, 0.05, 0.2 ) ),
          list( ctlpt( E2, 0.1, -0.2 ),
                ctlpt( E3, 0.15, 0.05, 0.1 ),
                ctlpt( E3, 0.2, -0.1, 0.2 ) ),
          list( ctlpt( E1, 0.2 ),
                ctlpt( E3, 0.25, 0.2, 0.1 ),
                ctlpt( E3, 0.3, 0.05, 0.2 ) ) ) ):

SAD2 = offset( SAD1, 0.2, 1, off ):

save( "dist2ff6",
      list( TestSSDistFunc( SAD1, SAD2, nil() ),
            TestSSDistFunc( SAD1, SAD2, list( 0.25, 0.5, 0.75 ) ) ) );

free( SAD1 );
free( SAD2 );

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

CNVX1 = sbezier(
    list( list( ctlpt( E3, 0,   0,    0.1 ),
                ctlpt( E3, 0.5, 0.1,  0.5 ),
                ctlpt( E3, 1,   0.05, 0.0 ) ),
          list( ctlpt( E3, 0,   0.5,  0.4 ),
                ctlpt( E3, 0.6, 0.5,  0.8 ),
                ctlpt( E3, 0.9, 0.6,  0.3 ) ),
          list( ctlpt( E3, 0,   0.9,  0.1 ),
                ctlpt( E3, 0.4, 1.0,  0.5 ),
                ctlpt( E3, 1,   0.95, 0.0 ) ) ) ) * sz( 2 ):

CNVX2a = offset( CNVX1, 0.3, 1, off ):

CNVX2b = offset( CNVX1, 0.16, 1, off ):

save( "dist2ff7",
      list( TestSSDistFunc( CNVX1, CNVX2a, nil() ),
	    TestSSDistFunc( CNVX1, CNVX2a, list( 0.5 ) ),
	    TestSSDistFunc( CNVX1, CNVX2b, nil() ),
	    TestSSDistFunc( CNVX1, CNVX2b, list( 0.5 ) ),
            TestSSDistFunc( CNVX1, CNVX2b, list( 1/3, 2/3 ) ) ) );

free( CNVX1 );
free( CNVX2a );
free( CNVX2b );

#############################################################################
#
# Almost developable surface.
#

AlmDev1 = sbezier(
    list( list( ctlpt( E3, 0,   0,    0.1 ),
                ctlpt( E3, 0.5, 0.1,  0.5 ),
                ctlpt( E3, 1,   0.05, 0.0 ) ),
          list( ctlpt( E3, 0,   0.9,  0.1 ),
                ctlpt( E3, 0.4, 1.0,  0.5 ),
                ctlpt( E3, 1,   0.95, 0.0 ) ) ) ) * sz( 2 ):

AlmDev2a = offset( AlmDev1, 0.16, 1, off ):


AlmDev2b = offset( AlmDev1, 0.1, 1, off ):

save( "dist2ff8",
      list( TestSSDistFunc( AlmDev1, AlmDev2a, nil() ),
	    TestSSDistFunc( AlmDev1, AlmDev2a, list( 0.5 ) ),
	    TestSSDistFunc( AlmDev1, AlmDev2b, nil() ),
	    TestSSDistFunc( AlmDev1, AlmDev2b, list( 0.5 ) ),
	    TestSSDistFunc( AlmDev1, AlmDev2b, list( 1/3, 2/3 ) ) ) );

free( AlmDev1 );
free( AlmDev2a );
free( AlmDev2b );

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