#
# testing functions for the point fitting functions, for primitives.
#

save_res = resolution;
iritstate( "RandomInit", 1964 );  # Seed-initiate the randomizer.

#
# A Sphere: returned is (Error, Xcntr, Ycntr, Zcntr, Radius)
#
resolution = 20;
x1 = triangl( sphere( vector( 1, 2, 3 ), 4 ), 1 );

resolution = 5;
x2 = triangl( sphere( vector( -1.4, 2.2, 5.3 ), 1.4 ), 1 );

SpFit = list( FitPModel(x1, 1, 0.01, 100 ),
              FitPModel(x1, 1, 1e-5, 100 ),
	      FitPModel(x2, 1, 0.01, 100 ),
	      FitPModel(x2, 1, 1e-6, 100 ) );

#
# A cylinder: returned is (Error, Xcntr, Ycntr, Zcntr, 
#                                 Xdir, Ydir, Zdir, Radius)
#

resolution = 20;
x1 = triangl( cylin( vector( 1, 2, 3 ), vector( 0, 0, 1 ), 0.3, 0 ), 1 );
x1 = MaxEdgeLen( x1, 0.3 );

resolution = 50;
x2 = triangl( cylin( vector( -1.1, 3.7, 0.1 ), vector( 1, 2, 1 ), 0.23, 0 ), 1 );
x2 = MaxEdgeLen( x2, 0.3 );

CylFit = list( FitPModel( x1, 2, 1e-6, 100 ),
	       FitPModel( x2, 2, 0.001, 100 ) );

#
# A cone: returned is (Error, Xapex, Yapex, Zapex, angle,
#                                    Xdir, Ydir, Zdir)
#
x1 = MaxEdgeLen( triangl( CON2( vector( 0, 0, 0 ), vector( 0, 0, 1 ),
				1.0, 2.0, 0 ),
		          1 ),
		 0.3 );
x2 = MaxEdgeLen( triangl( CON2( vector( -1, 1.2, -0.5 ), vector( 2, 1, 1 ),
				0.5, 0.0, 0 ),
		          1 ),
		 0.3 );
ConeFit = list( FitPModel( x1, 4, 0.001, 30 ),
                FitPModel( x2, 4, 1e-5, 30 ) );

#
# Planar fit: return is (Error, A, B, C, D)
#

x1 = triangl( gpolygon( planeSrf( 0, 0, 1, 1 ), 1 ), 1 );
x1 = MaxEdgeLen( x1, 0.2 );

x2 = x1 * rx( 45 );

view( list( axes, x2 ), 1 );

PlaneFit = list( FitPModel( x1, 0, 1e-5, 100 ),
		 fitpmodel( x2, 0, 1e-5, 100 ) );

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

save("prim_fit", list( PlaneFit, SpFit, CylFit, ConeFit ) );

free( x1 );
free( x2 );
free( PlaneFit );
free( SpFit );
free( CylFit );
free( ConeFit );

resolution = save_res;
