00001 //$Header: /home/ben/Mapper/c++/test/RCS/test_curve.cpp,v 6.8 2002/07/13 17:37:20 ben Exp $
00002 //Unit tests of Mapper curve classes.
00003 // Copyright Benedict Adamson 2002.
00004 // This file is part of Mapper.
00005
00006 // Mapper is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010
00011 // Mapper is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014 // GNU General Public License for more details.
00015
00016 // You should have received a copy of the GNU General Public License
00017 // along with Mapper; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019
00030 #include <cmath>
00031
00032 #include "curve.h"
00033 #include "vector2.h"
00034 #include "test/test.h"
00035
00036
00037 namespace test{
00038
00039 static void test_nearest_point(
00040 const curve &c,
00041 const vector2 &p,
00042 const double t_expect
00043 )
00044 {
00045 double t;
00046 double d2;
00047 unsigned iter;
00048 static const unsigned max_iter = 100;
00049 nearest_point( c, p, test::tol, max_iter, t, d2, iter );
00050 require( 0 <= t && t <= 1 );
00051 require( 0 <= d2 );
00052 require( 0 < iter && iter <= max_iter );
00053 require( test::approx_equal( t, t_expect ) );
00054 };
00055
00056 }
00057
00058
00059
00060 void test::test_curve(void)
00061 {
00062 test_start_section( "test/test_curve.cpp" );
00063 {
00064 test_start_section("curve::position, straight line");
00065 {
00066 curve c;
00067 vector2 p0( 00, 00 );
00068 vector2 p1( 01, 00 );
00069 c.param.push_back( &p0 );
00070 c.param.push_back( &p1 );
00071 require( approx_equal( c.position( 0.0 ), p0 ) );
00072 require( approx_equal( c.position( 1.0 ), p1 ) );
00073 require( approx_equal( c.position( 0.2 ), vector2( 0.2, 00 ) ) );
00074 };
00075 {
00076 curve c;
00077 vector2 p0( 00, 00 );
00078 vector2 p1( 00, 01 );
00079 c.param.push_back( &p0 );
00080 c.param.push_back( &p1 );
00081 require( approx_equal( c.position( 0.0 ), p0 ) );
00082 require( approx_equal( c.position( 1.0 ), p1 ) );
00083 require( approx_equal( c.position( 0.2 ), vector2( 00, 0.2 ) ) );
00084 };
00085 {
00086 curve c;
00087 vector2 p0( 00, 00 );
00088 vector2 p1( -1, 00 );
00089 c.param.push_back( &p0 );
00090 c.param.push_back( &p1 );
00091 require( approx_equal( c.position( 0.0 ), p0 ) );
00092 require( approx_equal( c.position( 1.0 ), p1 ) );
00093 require( approx_equal( c.position( 0.2 ), vector2( -0.2, 00 ) ) );
00094 };
00095 };
00096 {
00097 test_start_section("curve::position, curved line");
00098 curve c;
00099 vector2 p0( 00, 00 );
00100 vector2 p1( 01, 00 );
00101 vector2 p2( 00, 01 );
00102 c.param.push_back( &p0 );
00103 c.param.push_back( &p1 );
00104 c.param.push_back( &p2 );
00105 require( approx_equal( c.position( 0.0 ), p0 ) );
00106 require( approx_equal( c.position( 1.0 ), p1 ) );
00107 require( approx_equal( c.position( 0.5 ), vector2( 0.5, 1.0 ) ) );
00108 };
00109 {
00110 test_start_section("curve, nearest_point");
00111 {
00112 test_start_section("curve, nearest, on straignt line");
00113 curve c;
00114 vector2 p0( 00, 00 );
00115 vector2 p1( 01, 00 );
00116 c.param.push_back( &p0 );
00117 c.param.push_back( &p1 );
00118 test_nearest_point( c, p0, 0 );
00119 test_nearest_point( c, p1, 1 );
00120 test_nearest_point( c, vector2( 0.5, 0 ), 0.5 );
00121 };
00122 {
00123 test_start_section("curve, nearest, near straignt line");
00124 curve c;
00125 vector2 p0( 00, 00 );
00126 vector2 p1( 01, 00 );
00127 c.param.push_back( &p0 );
00128 c.param.push_back( &p1 );
00129 test_nearest_point( c, vector2( -1, 0 ), 0 );
00130 test_nearest_point( c, vector2( 2, 0 ), 1 );
00131 test_nearest_point( c, vector2( 0, 1 ), 0 );
00132 test_nearest_point( c, vector2( 1, 1 ), 1 );
00133 test_nearest_point( c, vector2( 0.5, 1 ), 0.5 );
00134 };
00135 {
00136 test_start_section("curve, nearest, on curved line");
00137 curve c;
00138 vector2 p0( 00, 00 );
00139 vector2 p1( 00, 02 );
00140 vector2 p2( 01, 00 );
00141 c.param.push_back( &p0 );
00142 c.param.push_back( &p1 );
00143 c.param.push_back( &p2 );
00144 test_nearest_point( c, p0, 0 );
00145 test_nearest_point( c, p1, 1 );
00146 test_nearest_point( c, vector2( 01, 01 ), 0.5 );
00147 };
00148 {
00149 test_start_section("curve, nearest, near curved line");
00150 curve c;
00151 vector2 p0( 00, 00 );
00152 vector2 p1( 00, 02 );
00153 vector2 p2( 01, 00 );
00154 c.param.push_back( &p0 );
00155 c.param.push_back( &p1 );
00156 c.param.push_back( &p2 );
00157 test_nearest_point( c, vector2( -1, 0 ), 0 );
00158 test_nearest_point( c, vector2( 0, 3 ), 1 );
00159 test_nearest_point( c, vector2( 0, -3 ), 0 );
00160 test_nearest_point( c, vector2( 2, 1 ), 0.5 );
00161 };
00162 {
00163 test_start_section("curve, nearest, only 1 iteration");
00164 curve c;
00165 vector2 p0( 00, 00 );
00166 vector2 p1( 01, 00 );
00167 c.param.push_back( &p0 );
00168 c.param.push_back( &p1 );
00169 const vector2 p( 0.5, 0 );
00170 double t;
00171 double d2;
00172 unsigned iter;
00173 static const unsigned max_iter = 1u;
00174 nearest_point( c, p, tol, max_iter, t, d2, iter );
00175 require( 0 <= t && t <= 1 );
00176 require( 0 <= d2 );
00177 require( 0 < iter && iter <= max_iter );
00178 };
00179 };
00180 }