1 | /*
|
---|
2 | * PlaneUnittest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Apr 30, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "PlaneUnittest.hpp"
|
---|
9 |
|
---|
10 | #include <cppunit/CompilerOutputter.h>
|
---|
11 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
12 | #include <cppunit/ui/text/TestRunner.h>
|
---|
13 |
|
---|
14 | #include <cmath>
|
---|
15 |
|
---|
16 | #ifdef HAVE_TESTRUNNER
|
---|
17 | #include "UnitTestMain.hpp"
|
---|
18 | #endif /*HAVE_TESTRUNNER*/
|
---|
19 |
|
---|
20 | #include "vector.hpp"
|
---|
21 | #include "Line.hpp"
|
---|
22 |
|
---|
23 | CPPUNIT_TEST_SUITE_REGISTRATION( PlaneUnittest );
|
---|
24 |
|
---|
25 | void PlaneUnittest::setUp(){
|
---|
26 | p1 = new Plane(e1,e2,e3);
|
---|
27 | p2 = new Plane(e1,e2,zeroVec);
|
---|
28 | p3 = new Plane(e1,zeroVec,e3);
|
---|
29 | p4 = new Plane(zeroVec,e2,e3);
|
---|
30 | }
|
---|
31 |
|
---|
32 | void PlaneUnittest::tearDown(){
|
---|
33 | delete p1;
|
---|
34 | delete p2;
|
---|
35 | delete p3;
|
---|
36 | delete p4;
|
---|
37 | }
|
---|
38 |
|
---|
39 | void PlaneUnittest::constructionErrorTest(){
|
---|
40 | // try several method of construction..
|
---|
41 | // see if error checking works
|
---|
42 |
|
---|
43 | // three points
|
---|
44 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,e2,e3));
|
---|
45 | // when only two points are differnt this gives an error
|
---|
46 | CPPUNIT_ASSERT_THROW(Plane(e1,e2,e2),LinearDependenceException);
|
---|
47 | // same with only one point
|
---|
48 | CPPUNIT_ASSERT_THROW(Plane(e1,e1,e1),LinearDependenceException);
|
---|
49 |
|
---|
50 | // use two vector giving two directions
|
---|
51 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,e2,0));
|
---|
52 | // and again this is actually only one vector
|
---|
53 | CPPUNIT_ASSERT_THROW(Plane(e1,e1,0),LinearDependenceException);
|
---|
54 | // Zero vector does not give a good direction
|
---|
55 | CPPUNIT_ASSERT_THROW(Plane(e1,zeroVec,0),ZeroVectorException);
|
---|
56 |
|
---|
57 | // use a normalvector and an scalar offset
|
---|
58 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,0));
|
---|
59 | // The zero vector is no good as a normalvector
|
---|
60 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,0),ZeroVectorException);
|
---|
61 |
|
---|
62 | // use a normalvector and an offset vector
|
---|
63 | CPPUNIT_ASSERT_NO_THROW(Plane(e1,zeroVec));
|
---|
64 | // and the bad zeroVector again
|
---|
65 | CPPUNIT_ASSERT_THROW(Plane(zeroVec,zeroVec),ZeroVectorException);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | // we need to test normals independent of the direction
|
---|
70 | bool testNormal(const Vector &normal1, const Vector &normal2){
|
---|
71 | return (normal1==normal2) || (normal1==-1*normal2);
|
---|
72 | }
|
---|
73 |
|
---|
74 | void PlaneUnittest::constructionResultTest(){
|
---|
75 | {
|
---|
76 | // construct with three points on plane
|
---|
77 | Plane p1(e1,e2,zeroVec);
|
---|
78 | CPPUNIT_ASSERT(testNormal(e3,p1.getNormal()));
|
---|
79 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset());
|
---|
80 |
|
---|
81 | Plane p2(e1,e3,zeroVec);
|
---|
82 | CPPUNIT_ASSERT(testNormal(e2,p2.getNormal()));
|
---|
83 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset());
|
---|
84 |
|
---|
85 | Plane p3(e2,e3,zeroVec);
|
---|
86 | CPPUNIT_ASSERT(testNormal(e1,p3.getNormal()));
|
---|
87 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset());
|
---|
88 | }
|
---|
89 | {
|
---|
90 | // construct with two directions + offset
|
---|
91 | Plane p1(e1,e2,0);
|
---|
92 | CPPUNIT_ASSERT(testNormal(e3,p1.getNormal()));
|
---|
93 | CPPUNIT_ASSERT_EQUAL(0.,p1.getOffset());
|
---|
94 |
|
---|
95 | Plane p2(e1,e3,0);
|
---|
96 | CPPUNIT_ASSERT(testNormal(e2,p2.getNormal()));
|
---|
97 | CPPUNIT_ASSERT_EQUAL(0.,p2.getOffset());
|
---|
98 |
|
---|
99 | Plane p3(e2,e3,0);
|
---|
100 | CPPUNIT_ASSERT(testNormal(e1,p3.getNormal()));
|
---|
101 | CPPUNIT_ASSERT_EQUAL(0.,p3.getOffset());
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | void PlaneUnittest::pointsTest(){
|
---|
106 | std::vector<Vector> points1 = p1->getPointsOnPlane();
|
---|
107 | CPPUNIT_ASSERT(p1->isContained(points1[0]));
|
---|
108 | CPPUNIT_ASSERT(p1->isContained(points1[1]));
|
---|
109 | CPPUNIT_ASSERT(p1->isContained(points1[2]));
|
---|
110 | // check that the three points differ
|
---|
111 | CPPUNIT_ASSERT(points1[0]!=points1[1]);
|
---|
112 | CPPUNIT_ASSERT(points1[0]!=points1[2]);
|
---|
113 | CPPUNIT_ASSERT(points1[1]!=points1[2]);
|
---|
114 |
|
---|
115 |
|
---|
116 | std::vector<Vector> points2 = p2->getPointsOnPlane();
|
---|
117 | CPPUNIT_ASSERT(p2->isContained(points2[0]));
|
---|
118 | CPPUNIT_ASSERT(p2->isContained(points2[1]));
|
---|
119 | CPPUNIT_ASSERT(p2->isContained(points2[2]));
|
---|
120 | // check that the three points differ
|
---|
121 | CPPUNIT_ASSERT(points2[0]!=points2[1]);
|
---|
122 | CPPUNIT_ASSERT(points2[0]!=points2[2]);
|
---|
123 | CPPUNIT_ASSERT(points2[1]!=points2[2]);
|
---|
124 |
|
---|
125 | std::vector<Vector> points3 = p3->getPointsOnPlane();
|
---|
126 | CPPUNIT_ASSERT(p3->isContained(points3[0]));
|
---|
127 | CPPUNIT_ASSERT(p3->isContained(points3[1]));
|
---|
128 | CPPUNIT_ASSERT(p3->isContained(points3[2]));
|
---|
129 | // check that the three points differ
|
---|
130 | CPPUNIT_ASSERT(points3[0]!=points3[1]);
|
---|
131 | CPPUNIT_ASSERT(points3[0]!=points3[2]);
|
---|
132 | CPPUNIT_ASSERT(points3[1]!=points3[2]);
|
---|
133 |
|
---|
134 | std::vector<Vector> points4 = p4->getPointsOnPlane();
|
---|
135 | CPPUNIT_ASSERT(p4->isContained(points4[0]));
|
---|
136 | CPPUNIT_ASSERT(p4->isContained(points4[1]));
|
---|
137 | CPPUNIT_ASSERT(p4->isContained(points4[2]));
|
---|
138 | // check that the three points differ
|
---|
139 | CPPUNIT_ASSERT(points4[0]!=points4[1]);
|
---|
140 | CPPUNIT_ASSERT(points4[0]!=points4[2]);
|
---|
141 | CPPUNIT_ASSERT(points4[1]!=points4[2]);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | void PlaneUnittest::operationsTest(){
|
---|
146 | {
|
---|
147 | Vector t = (1./3.)*(e1+e2+e3);
|
---|
148 | CPPUNIT_ASSERT(fabs(p1->distance(zeroVec)-t.Norm()) < MYEPSILON);
|
---|
149 | CPPUNIT_ASSERT_EQUAL(t,p1->getClosestPoint(zeroVec));
|
---|
150 | }
|
---|
151 |
|
---|
152 | CPPUNIT_ASSERT(fabs(p2->distance(e3)-1) < MYEPSILON);
|
---|
153 | CPPUNIT_ASSERT_EQUAL(zeroVec,p2->getClosestPoint(e3));
|
---|
154 | CPPUNIT_ASSERT(fabs(p3->distance(e2)-1) < MYEPSILON);
|
---|
155 | CPPUNIT_ASSERT_EQUAL(zeroVec,p3->getClosestPoint(e2));
|
---|
156 | CPPUNIT_ASSERT(fabs(p4->distance(e1)-1) < MYEPSILON);
|
---|
157 | CPPUNIT_ASSERT_EQUAL(zeroVec,p4->getClosestPoint(e1));
|
---|
158 | }
|
---|
159 |
|
---|
160 | void PlaneUnittest::mirrorTest(){
|
---|
161 | Vector fixture;
|
---|
162 |
|
---|
163 | // some Vectors that lie on the planes
|
---|
164 | fixture = p1->mirrorVector(e1);
|
---|
165 | CPPUNIT_ASSERT_EQUAL(fixture,e1);
|
---|
166 | fixture = p1->mirrorVector(e2);
|
---|
167 | CPPUNIT_ASSERT_EQUAL(fixture,e2);
|
---|
168 | fixture = p1->mirrorVector(e3);
|
---|
169 | CPPUNIT_ASSERT_EQUAL(fixture,e3);
|
---|
170 |
|
---|
171 | fixture = p2->mirrorVector(zeroVec);
|
---|
172 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
|
---|
173 | fixture = p2->mirrorVector(e1);
|
---|
174 | CPPUNIT_ASSERT_EQUAL(fixture,e1);
|
---|
175 | fixture = p2->mirrorVector(e2);
|
---|
176 | CPPUNIT_ASSERT_EQUAL(fixture,e2);
|
---|
177 |
|
---|
178 | fixture = p3->mirrorVector(zeroVec);
|
---|
179 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
|
---|
180 | fixture = p3->mirrorVector(e1);
|
---|
181 | CPPUNIT_ASSERT_EQUAL(fixture,e1);
|
---|
182 | fixture = p3->mirrorVector(e3);
|
---|
183 | CPPUNIT_ASSERT_EQUAL(fixture,e3);
|
---|
184 |
|
---|
185 | fixture = p4->mirrorVector(zeroVec);
|
---|
186 | CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
|
---|
187 | fixture = p4->mirrorVector(e2);
|
---|
188 | CPPUNIT_ASSERT_EQUAL(fixture,e2);
|
---|
189 | fixture = p4->mirrorVector(e3);
|
---|
190 | CPPUNIT_ASSERT_EQUAL(fixture,e3);
|
---|
191 |
|
---|
192 | // some Vectors outside of the planes
|
---|
193 | {
|
---|
194 | Vector t = (2./3.)*(e1+e2+e3);
|
---|
195 | fixture = p1->mirrorVector(zeroVec);
|
---|
196 | CPPUNIT_ASSERT_EQUAL(fixture,t);
|
---|
197 | }
|
---|
198 |
|
---|
199 | fixture = p2->mirrorVector(e3);
|
---|
200 | CPPUNIT_ASSERT_EQUAL(fixture,-1*e3);
|
---|
201 | fixture = p3->mirrorVector(e2);
|
---|
202 | CPPUNIT_ASSERT_EQUAL(fixture,-1*e2);
|
---|
203 | fixture = p4->mirrorVector(e1);
|
---|
204 | CPPUNIT_ASSERT_EQUAL(fixture,-1*e1);
|
---|
205 | }
|
---|
206 |
|
---|
207 | void PlaneUnittest::LineIntersectionTest(){
|
---|
208 | Vector fixture;
|
---|
209 | // plane at (0,0,0) normal to (1,0,0) cuts line from (0,0,0) to (2,1,0) at ???
|
---|
210 | Line l1 = makeLineThrough(zeroVec,Vector(2,1,0));
|
---|
211 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(e1, zeroVec).GetIntersection(l1) );
|
---|
212 | CPPUNIT_ASSERT_EQUAL( zeroVec, fixture );
|
---|
213 |
|
---|
214 | // plane at (2,1,0) normal to (0,1,0) cuts line from (1,0,0) to (0,1,1) at ???
|
---|
215 | Line l2 = makeLineThrough(e1,Vector(0,1,1));
|
---|
216 | CPPUNIT_ASSERT_NO_THROW(fixture = Plane(e2, Vector(2,1,0)).GetIntersection(l2) );
|
---|
217 | CPPUNIT_ASSERT_EQUAL( Vector(0., 1., 1.), fixture );
|
---|
218 | }
|
---|