Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

gfx/GfxTest.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: GfxTest.cpp 1030 2004-02-11 20:46:17Z jungd $
00019   $Revision: 1.2 $
00020   $Date: 2004-02-11 15:46:17 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <gfx/GfxTest>
00026 
00027 using gfx::GfxTest;
00028 
00029 using gfx::Line3;
00030 using gfx::Segment3;
00031 using gfx::Triangle3;
00032 using gfx::Plane;
00033 using gfx::Quad3;
00034 using gfx::Disc3;
00035 
00036 
00037 
00038 GfxTest::GfxTest()
00039 {
00040 }
00041 
00042 
00043 void GfxTest::setUp() 
00044 { 
00045   ul = Line3(Point3(0,0,0), Vector3(1,1,1));
00046   l = Line3(Point3(0,0,0), Vector3(1,2,3));
00047   us = Segment3(Point3(0,0,0), Point3(1,1,1).normalize());
00048   s = Segment3(Point3(0,0,0), Point3(1,1,1));
00049   s2 = Segment3(Point3(1,1,1), Point3(2,2,2));
00050   s3 = Segment3(Point3(1,2,3), Point3(4,6,7));
00051   
00052   q1 = Quad3(Point3(0,0,0),Point3(1,0,0),Point3(1,1,0), Point3(0,1,0));
00053   q2 = Quad3(Point3(2,0,0),Point3(3,0,0),Point3(3,1,0), Point3(2,1,0));
00054 }
00055   
00056 
00057 void GfxTest::tearDown()
00058 { 
00059 }
00060 
00061  
00062 void GfxTest::testLine()
00063 {
00064   CPPUNIT_ASSERT( ul.contains(Point3(0,0,0)) );
00065   CPPUNIT_ASSERT( ul.contains(Point3(0.5,0.5,0.5)) );
00066   CPPUNIT_ASSERT( ul.contains(Point3(2,2,2)) );
00067   CPPUNIT_ASSERT( ul.contains(Point3(-2,-2,-2)) );
00068   
00069   CPPUNIT_ASSERT( l.contains(Point3(1,2,3)) );
00070   CPPUNIT_ASSERT( l.contains(Point3(2,4,6)) );
00071   
00072   CPPUNIT_ASSERT( Line3(Point3(0,0,0), Vector3(1,0,0)).pointClosestTo(Point3(0,1,0)).equals(Point3(0,0,0)) );
00073   
00074   CPPUNIT_ASSERT( Math::equals( l.distanceTo(Point3(2,4,6)), 0 ) ); 
00075   CPPUNIT_ASSERT( Math::equals( Line3(Point3(0,0,0), Vector3(1,0,0)).distanceTo(Point3(0.5,0,0)), 0 ) ); 
00076   CPPUNIT_ASSERT( Math::equals( Line3(Point3(0,0,0), Vector3(1,0,0)).distanceTo(Point3(0,2,0)), 2 ) ); 
00077   
00078   Line3 xl(Point3(0,0,0), Vector3(1,0,0));
00079   Line3 yl(Point3(0,-1,1), Vector3(0,1,0));
00080   Segment3 bxy(Point3(0,0,0), Point3(0,0,1));
00081   CPPUNIT_ASSERT( xl.shortestSegmentBetween(yl).equals(bxy) );
00082   CPPUNIT_ASSERT( Math::equals(xl.distanceTo(yl),1) );
00083 }
00084 
00085 
00086 void GfxTest::testSegment()
00087 {
00088   CPPUNIT_ASSERT( s.contains(Point3(0,0,0)) );
00089   CPPUNIT_ASSERT( s.contains(Point3(0.5,0.5,0.5)) );
00090   CPPUNIT_ASSERT( s.contains(Point3(1,1,1)) );
00091   CPPUNIT_ASSERT( !s.contains(Point3(2,2,2)) );
00092   CPPUNIT_ASSERT( !s.contains(Point3(-2,-2,-2)) );
00093   CPPUNIT_ASSERT( !s.contains(Point3(1,1,1.5)) );
00094   CPPUNIT_ASSERT( !s.contains(Point3(2,2,2.5)) );
00095   CPPUNIT_ASSERT( !s.contains(Point3(-1,-1,-0.5)) );
00096 
00097   CPPUNIT_ASSERT( s.pointClosestTo(Point3(2,2,2)).equals(Point3(1,1,1)) );
00098   CPPUNIT_ASSERT( s.pointClosestTo(Point3(2,3,4)).equals(Point3(1,1,1)) );
00099   CPPUNIT_ASSERT( s.pointClosestTo(Point3(-1,-1,-1)).equals(Point3(0,0,0)) );
00100   CPPUNIT_ASSERT( s.pointClosestTo(Point3(-2,-1,-1)).equals(Point3(0,0,0)) );
00101   
00102   CPPUNIT_ASSERT( Segment3(Point3(0,0,0),Point3(1,0,0)).pointClosestTo(Point3(0.5,2,1)).equals(Point3(0.5,0,0)) );
00103   CPPUNIT_ASSERT( Math::equals(Segment3(Point3(0,0,0),Point3(1,0,0)).distanceTo(Point3(0.5,2,0)),2) );
00104   
00105   CPPUNIT_ASSERT( Math::equals(s.distanceTo(s2), 0) );
00106   Segment3 s4(Point3(3,3,3),Point3(4,4,4));
00107   CPPUNIT_ASSERT( Math::equals(s.distanceTo(s4), Math::sqrt(12)) );
00108   
00109   Segment3 xs(Point3(-0.5,0,0), Point3(1,0,0));
00110   Segment3 ys(Point3(0,-1,1), Point3(0,1,1));
00111   CPPUNIT_ASSERT( Math::equals( xs.distanceTo(ys), 1) );
00112   CPPUNIT_ASSERT( xs.shortestSegmentBetween(ys).equals( Segment3(Point3(0,0,0),Point3(0,0,1)) ) );
00113 }
00114 
00115 
00116 void GfxTest::testPlane()
00117 {
00118   Plane pl1(Vector3(0,0,1),0);
00119   
00120   CPPUNIT_ASSERT( Math::equals( pl1.distanceTo(Point3(0,0,1)), 1) );
00121   CPPUNIT_ASSERT( pl1.contains(Point3(0,0,0)) );
00122 
00123   Plane pl2(Vector3(0,0,1),-1);
00124 
00125   CPPUNIT_ASSERT( pl2.contains(Point3(0,0,1)) );
00126   
00127   Plane pl3(Vector3(1,1,1),-1);
00128   Point3 p(1,1,1); p.normalize();
00129   CPPUNIT_ASSERT( pl3.contains(p) );
00130   CPPUNIT_ASSERT( !pl3.contains(Point3(2,2,2)) );
00131 }
00132 
00133   
00134 void GfxTest::testTriangle()
00135 {
00136   Triangle3 txy(Point3(1,0,0), Point3(0,0,0), Point3(0,1,0));
00137 
00138   CPPUNIT_ASSERT( txy.pointClosestTo(Point3(20,-1,1)).equals(Point3(1,0,0)) );
00139   CPPUNIT_ASSERT( txy.pointClosestTo(Point3(0.3,0.3,1)).equals(Point3(0.3,0.3,0)) );
00140   CPPUNIT_ASSERT( txy.pointClosestTo(Point3(0.3,0.3,0)).equals(Point3(0.3,0.3,0)) );
00141 
00142   Segment3 ts(Point3(0.3,0.3,1),Point3(0.4,0.4,0.2));
00143   CPPUNIT_ASSERT( txy.shortestSegmentBetween(ts).equals(Segment3(Point3(0.4,0.4,0),Point3(0.4,0.4,0.2))) );
00144   
00145   Segment3 ts2(Point3(2,2,2), Point3(3,3,3));
00146   CPPUNIT_ASSERT( txy.shortestSegmentBetween(ts2).equals(Segment3(Point3(0.5,0.5,0), Point3(2,2,2))) );
00147 }
00148 
00149 
00150 void GfxTest::testQuad()
00151 {
00152   CPPUNIT_ASSERT( Math::equals( q1.distanceTo(q2), 1) );
00153 }
00154 
00155 
00156 
00157 void GfxTest::testDisc()
00158 {
00159   Disc3 d1(Point3(0,0,0), Vector3(0,0,1), 1);
00160   CPPUNIT_ASSERT( Math::equals( d1.distanceTo(Point3(0,0,1)), 1) );
00161   CPPUNIT_ASSERT( Math::equals( d1.distanceTo(Point3(0.5,0.5,1)), 1) );
00162   CPPUNIT_ASSERT( Math::equals( d1.distanceTo(Point3(0,2,0)), 1) );
00163 }
00164 
00165 
00166 
00167 
00168 #ifdef DEBUG
00169 CPPUNIT_TEST_SUITE_REGISTRATION( GfxTest );
00170 #endif
00171 

Generated on Thu Jul 29 15:56:19 2004 for OpenSim by doxygen 1.3.6