testVector3D.cc

  1 const char *help = "\
  2 progname: testVector3D.cc\n\
  3 code2html: This program tests Vector3D class methods.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Dan Sunday (http:www.softsurfer.com) and Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "general.h"
  8 #include "Point3D.h"
  9 #include "Vector3D.h"
 10 #include "CmdLine.h"
 11 
 12 using namespace Torch;
 13 
 14 int main(int argc, char **argv)
 15 {
 16  	real x1, y1, z1;
 17  	real x2, y2, z2;
 18 	bool verbose;
 19 
 20 	// Construct the command line
 21   	CmdLine cmd;
 22 	cmd.setBOption("write log", false);
 23 	
 24   	// Put the help line at the beginning
 25   	cmd.info(help);
 26 
 27   	cmd.addText("\nArguments:");
 28   	cmd.addRCmdArg("x1", &x1, "x1");
 29   	cmd.addRCmdArg("y1", &y1, "y1");
 30   	cmd.addRCmdArg("z1", &z1, "z1");
 31   	cmd.addRCmdArg("x2", &x2, "x2");
 32   	cmd.addRCmdArg("y2", &y2, "y2");
 33   	cmd.addRCmdArg("z2", &z2, "z2");
 34   	cmd.addText("\nOptions:");
 35   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 36 
 37 	cmd.read(argc, argv);
 38 
 39 	double	 a, c[3];
 40 	Vector3D u, v(1), w(0,1);
 41 	Vector3D vx[3];
 42 
 43 
 44 	// output preset coords of v and w
 45 	print("Initial v = %s\n", v.sprint());
 46 	print("Initial w = %s\n", w.sprint());
 47 
 48 	// input new values 
 49 	v.x = x1;
 50 	v.y = y1;
 51 	v.z = z1;
 52 
 53 	w.x = x2;
 54 	w.y = y2;
 55 	w.z = z2;
 56 
 57 	print("v = %s\n", v.sprint());
 58 	print("w = %s\n", w.sprint());
 59 
 60 	// test operators
 61 	u = -v;			print("-v = %s\n", u.sprint());
 62 	u = ~v;			print("~v = %s\n", u.sprint());
 63 	u = 2*v;		print("2*v = %s\n", u.sprint());
 64 	u = v + w;		print("v + w = %s\n", u.sprint());
 65 	u = (v + w)/2;		print("(v + w)/2 = %s\n", u.sprint());
 66 	u = v - w;		print("v - w = %s\n", u.sprint());
 67 	u = (v - w)*0.5;	print("(v - w)*0.5 = %s\n", u.sprint());
 68 	u = v ^ w;		print("v ^ w = %s\n", u.sprint());
 69 	a = v * w;		print("v * w = %g\n", a);
 70 	a = v | w;		print("v | w = %g\n", a);
 71 	a = ~v * w;		print("~v * w = %g\n", a);
 72 	print("\n");
 73 
 74 	// test functions
 75 	c[0]= 0.5; c[1]= 0.5;	print("c[]= {%g, %g}\n", c[0], c[1]);
 76 	vx[0]= v;  vx[1]= w;	print("vx[]= {%s, %s}\n", vx[0].sprint(), vx[1].sprint());
 77 	u = sum(2,c,vx);	print("sum(2,c,vx) = %s\n", u.sprint());
 78 
 79 	u = v.len2();		print("v.len2() = %s\n", u.sprint());
 80 	u = v.len();		print("v.len()  = %s\n", u.sprint());
 81 	v.normalize();		print("v.normalize() = %s\n", v.sprint());
 82 	w.normalize();		print("w.normalize() = %s\n", w.sprint());
 83 	print("\n");
 84 
 85 	// check that a few identities are always true
 86 	int cnt=0, errs=0;
 87 	Vector3D u0, uinc(2,0,-2);
 88 	Vector3D v0, vinc(1,1,1);
 89 	Vector3D w0, winc(0.2, 0.2, 0.0);
 90 	Vector3D x, x0(-1,-2,-3), xinc(0.5,-0.5,0.1);
 91 	double dl, dr;		// left and right side doubles
 92 	Vector3D vl, vr;		// left and right side vectors
 93 	double er, e=0.000000000001; // allow small float error e
 94 
 95 	for (u=u0; u.x < 7; u+=uinc) 
 96 	for (v=v0; v.x < 11; v+=vinc) 
 97 	for (w=w0; w.x < 1.1; w+=winc) 
 98 	{
 99 		++cnt;
100 		// Triple product
101 		dl = u * (v ^ w);
102 		dr = (u ^ v) * w;
103 		er = abs(dl-dr);
104 		if (er > e) 
105 		{	// identity fails > e
106 			++errs;	// count errors
107 			warning("Error: err=%d\n", er);
108 		}
109 		++cnt;
110 		// Left Association of Cross Product
111 		vl = (u ^ v) ^ w;
112 		vr = (u * w) * v - (v * w) * u;
113 		er = (vl-vr).len();
114 		if (er > e) 
115 		{	// identity fails > e
116 			++errs;	// count errors
117 			warning("Error: err=%d\n", er);
118 		}
119 		++cnt;
120 		// Lagrange Identity
121 		for (x=x0; x.x < 1.1; x+=xinc) 
122 		{
123 			dl = (u ^ v) * (w ^ x);
124 			dr = (u * w) * (v * x) - (v * w) * (u * x);
125 			er = abs(dl-dr);
126 			if (er > e) 
127 			{	// identity fails > e
128 				++errs;	// count errors
129 				warning("Error: err=%d\n", er);
130 			}
131 			++cnt;
132 		}
133 	}
134 	print("Did %d identity chks. Had %d errors\n", cnt, errs);
135 }