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