source: src/unittests/LineUnittest.cpp@ 13e3c3

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 13e3c3 was bf3817, checked in by Frederik Heber <heber@…>, 14 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

  • is now topmost in front of MemDebug.hpp (and any other).
  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * LineUnittest.cpp
3 *
4 * Created on: May 27, 2010
5 * Author: crueger
6 */
7
8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include "LineUnittest.hpp"
14
15#include "LinearAlgebra/Vector.hpp"
16#include "Exceptions/LinearDependenceException.hpp"
17#include "Exceptions/SkewException.hpp"
18
19#include <cppunit/CompilerOutputter.h>
20#include <cppunit/extensions/TestFactoryRegistry.h>
21#include <cppunit/ui/text/TestRunner.h>
22
23#include <iostream>
24#include <cmath>
25
26using namespace std;
27
28#ifdef HAVE_TESTRUNNER
29#include "UnitTestMain.hpp"
30#endif /*HAVE_TESTRUNNER*/
31
32CPPUNIT_TEST_SUITE_REGISTRATION( LineUnittest );
33
34void LineUnittest::setUp(){
35 // three lines along the axes
36 la1 = new Line(zeroVec,e1);
37 la2 = new Line(zeroVec,e2);
38 la3 = new Line(zeroVec,e3);
39
40 // the lines along the planes defined by two coordinate axes
41 lp1 = new Line(e1,e1-e2);
42 lp2 = new Line(e2,e2-e3);
43 lp3 = new Line(e3,e3-e1);
44}
45void LineUnittest::tearDown(){
46 delete la1;
47 delete la2;
48 delete la3;
49
50 delete lp1;
51 delete lp2;
52 delete lp3;
53}
54
55void LineUnittest::constructionErrorTest(){
56 // test some constructions
57
58 // direction+origin should never fail
59 CPPUNIT_ASSERT_NO_THROW(Line(zeroVec,e1));
60 CPPUNIT_ASSERT_NO_THROW(Line(zeroVec,e2));
61 CPPUNIT_ASSERT_NO_THROW(Line(zeroVec,e3));
62
63 // two points fails if both points are the same
64 CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e1,e2));
65 CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e2,e3));
66 CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e3,e1));
67 // for zerovectors
68 CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e1,zeroVec));
69 CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e2,zeroVec));
70 CPPUNIT_ASSERT_NO_THROW(makeLineThrough(e3,zeroVec));
71 // now we pass two times the same point
72 CPPUNIT_ASSERT_THROW(makeLineThrough(zeroVec,zeroVec),LinearDependenceException);
73 CPPUNIT_ASSERT_THROW(makeLineThrough(e1,e1),LinearDependenceException);
74 CPPUNIT_ASSERT_THROW(makeLineThrough(e2,e2),LinearDependenceException);
75 CPPUNIT_ASSERT_THROW(makeLineThrough(e3,e3),LinearDependenceException);
76
77}
78
79bool testDirection(const Vector &dir1,const Vector &dir2){
80 return (dir1==dir2) || (dir1==-1*dir2);
81}
82
83void LineUnittest::constructionResultTest(){
84 // test all directions
85 CPPUNIT_ASSERT(testDirection(la1->getDirection(),e1));
86 CPPUNIT_ASSERT(testDirection(la2->getDirection(),e2));
87 CPPUNIT_ASSERT(testDirection(la3->getDirection(),e3));
88
89 // test origins
90 CPPUNIT_ASSERT_EQUAL(la1->getOrigin(),zeroVec);
91 CPPUNIT_ASSERT_EQUAL(la2->getOrigin(),zeroVec);
92 CPPUNIT_ASSERT_EQUAL(la2->getOrigin(),zeroVec);
93
94 // test if desired points are on the lines
95 CPPUNIT_ASSERT(la1->isContained(zeroVec));
96 CPPUNIT_ASSERT(la2->isContained(zeroVec));
97 CPPUNIT_ASSERT(la3->isContained(zeroVec));
98
99 CPPUNIT_ASSERT(la1->isContained(e1));
100 CPPUNIT_ASSERT(la2->isContained(e2));
101 CPPUNIT_ASSERT(la3->isContained(e3));
102
103 CPPUNIT_ASSERT(lp1->isContained(e1));
104 CPPUNIT_ASSERT(lp2->isContained(e2));
105 CPPUNIT_ASSERT(lp3->isContained(e3));
106
107 CPPUNIT_ASSERT(lp1->isContained(e2));
108 CPPUNIT_ASSERT(lp2->isContained(e3));
109 CPPUNIT_ASSERT(lp3->isContained(e1));
110}
111
112void LineUnittest::isContainedTest(){
113 // Zerovector on the axes lines
114 CPPUNIT_ASSERT(la1->isContained(zeroVec));
115 CPPUNIT_ASSERT(la2->isContained(zeroVec));
116 CPPUNIT_ASSERT(la3->isContained(zeroVec));
117
118 // multiples of the second support vector
119 CPPUNIT_ASSERT(la1->isContained(e1));
120 CPPUNIT_ASSERT(la2->isContained(e2));
121 CPPUNIT_ASSERT(la3->isContained(e3));
122
123 CPPUNIT_ASSERT(la1->isContained(2*e1));
124 CPPUNIT_ASSERT(la2->isContained(2*e2));
125 CPPUNIT_ASSERT(la3->isContained(2*e3));
126
127 CPPUNIT_ASSERT(la1->isContained(3*e1));
128 CPPUNIT_ASSERT(la2->isContained(3*e2));
129 CPPUNIT_ASSERT(la3->isContained(3*e3));
130
131 // negative multiples
132 CPPUNIT_ASSERT(la1->isContained(-1*e1));
133 CPPUNIT_ASSERT(la2->isContained(-1*e2));
134 CPPUNIT_ASSERT(la3->isContained(-1*e3));
135
136 CPPUNIT_ASSERT(la1->isContained(-2*e1));
137 CPPUNIT_ASSERT(la2->isContained(-2*e2));
138 CPPUNIT_ASSERT(la3->isContained(-2*e3));
139
140 // points that should not be on the lines
141 CPPUNIT_ASSERT(!la1->isContained(e2));
142 CPPUNIT_ASSERT(!la2->isContained(e3));
143 CPPUNIT_ASSERT(!la3->isContained(e1));
144
145 CPPUNIT_ASSERT(!la1->isContained(2*e2));
146 CPPUNIT_ASSERT(!la2->isContained(2*e3));
147 CPPUNIT_ASSERT(!la3->isContained(2*e1));
148
149 CPPUNIT_ASSERT(!la1->isContained(-1*e2));
150 CPPUNIT_ASSERT(!la2->isContained(-1*e3));
151 CPPUNIT_ASSERT(!la3->isContained(-1*e1));
152
153 // For the plane lines
154 CPPUNIT_ASSERT(lp1->isContained(e1));
155 CPPUNIT_ASSERT(lp2->isContained(e2));
156 CPPUNIT_ASSERT(lp3->isContained(e3));
157
158 CPPUNIT_ASSERT(lp1->isContained(e2));
159 CPPUNIT_ASSERT(lp2->isContained(e3));
160 CPPUNIT_ASSERT(lp3->isContained(e1));
161
162 CPPUNIT_ASSERT(lp1->isContained(e1+2*(e1-e2)));
163 CPPUNIT_ASSERT(lp2->isContained(e2+2*(e2-e3)));
164 CPPUNIT_ASSERT(lp3->isContained(e3+2*(e3-e1)));
165
166 CPPUNIT_ASSERT(lp1->isContained(e1-2*(e1-e2)));
167 CPPUNIT_ASSERT(lp2->isContained(e2-2*(e2-e3)));
168 CPPUNIT_ASSERT(lp3->isContained(e3-2*(e3-e1)));
169}
170
171void LineUnittest::intersectionTest(){
172 Vector fixture;
173
174 // intersection of the axis lines
175 fixture = la1->getIntersection(*la2);
176 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
177 fixture = la2->getIntersection(*la3);
178 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
179 fixture = la3->getIntersection(*la1);
180 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
181
182 // axes and plane lines
183 fixture = la1->getIntersection(*lp1);
184 CPPUNIT_ASSERT_EQUAL(fixture,e1);
185 fixture = la2->getIntersection(*lp2);
186 CPPUNIT_ASSERT_EQUAL(fixture,e2);
187 fixture = la3->getIntersection(*lp3);
188 CPPUNIT_ASSERT_EQUAL(fixture,e3);
189
190 fixture = la1->getIntersection(*lp3);
191 CPPUNIT_ASSERT_EQUAL(fixture,e1);
192 fixture = la2->getIntersection(*lp1);
193 CPPUNIT_ASSERT_EQUAL(fixture,e2);
194 fixture = la3->getIntersection(*lp2);
195 CPPUNIT_ASSERT_EQUAL(fixture,e3);
196
197 // two plane lines
198 fixture = lp1->getIntersection(*lp2);
199 CPPUNIT_ASSERT_EQUAL(fixture,e2);
200 fixture = lp2->getIntersection(*lp3);
201 CPPUNIT_ASSERT_EQUAL(fixture,e3);
202 fixture = lp3->getIntersection(*lp1);
203 CPPUNIT_ASSERT_EQUAL(fixture,e1);
204
205 // When we have two times the same line, we check if the point is on the line
206 fixture = la1->getIntersection(*la1);
207 CPPUNIT_ASSERT(la1->isContained(fixture));
208 fixture = la2->getIntersection(*la2);
209 CPPUNIT_ASSERT(la2->isContained(fixture));
210 fixture = la3->getIntersection(*la3);
211 CPPUNIT_ASSERT(la3->isContained(fixture));
212
213 fixture = lp1->getIntersection(*lp1);
214 CPPUNIT_ASSERT(lp1->isContained(fixture));
215 fixture = lp2->getIntersection(*lp2);
216 CPPUNIT_ASSERT(lp2->isContained(fixture));
217 fixture = lp3->getIntersection(*lp3);
218 CPPUNIT_ASSERT(lp3->isContained(fixture));
219
220 // lines that are askew should produce an Error
221 CPPUNIT_ASSERT_THROW(lp1->getIntersection(*la3),SkewException);
222 CPPUNIT_ASSERT_THROW(lp2->getIntersection(*la1),SkewException);
223 CPPUNIT_ASSERT_THROW(lp3->getIntersection(*la2),SkewException);
224
225 CPPUNIT_ASSERT_THROW(la1->getIntersection(*lp2),SkewException);
226 CPPUNIT_ASSERT_THROW(la2->getIntersection(*lp3),SkewException);
227 CPPUNIT_ASSERT_THROW(la3->getIntersection(*lp1),SkewException);
228}
229
230void LineUnittest::rotationTest(){
231 Vector fixture;
232
233 // rotate zero Vector along the axes lines by various degrees
234 fixture = la1->rotateVector(zeroVec,1.);
235 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
236 fixture = la2->rotateVector(zeroVec,1.);
237 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
238 fixture = la3->rotateVector(zeroVec,1.);
239 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
240
241 fixture = la1->rotateVector(zeroVec,2.);
242 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
243 fixture = la2->rotateVector(zeroVec,2.);
244 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
245 fixture = la3->rotateVector(zeroVec,2.);
246 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
247
248 // rotate vectors on the axis around their lines
249 fixture = la1->rotateVector(e1,1.);
250 CPPUNIT_ASSERT_EQUAL(fixture,e1);
251 fixture = la2->rotateVector(e2,1.);
252 CPPUNIT_ASSERT_EQUAL(fixture,e2);
253 fixture = la3->rotateVector(e3,1.);
254 CPPUNIT_ASSERT_EQUAL(fixture,e3);
255
256 fixture = la1->rotateVector(e1,2.);
257 CPPUNIT_ASSERT_EQUAL(fixture,e1);
258 fixture = la2->rotateVector(e2,2.);
259 CPPUNIT_ASSERT_EQUAL(fixture,e2);
260 fixture = la3->rotateVector(e3,2.);
261 CPPUNIT_ASSERT_EQUAL(fixture,e3);
262
263 // more vectors on the axis
264 fixture = la1->rotateVector(2*e1,1.);
265 CPPUNIT_ASSERT_EQUAL(fixture,2*e1);
266 fixture = la2->rotateVector(2*e2,1.);
267 CPPUNIT_ASSERT_EQUAL(fixture,2*e2);
268 fixture = la3->rotateVector(2*e3,1.);
269 CPPUNIT_ASSERT_EQUAL(fixture,2*e3);
270
271 fixture = la1->rotateVector(2*e1,2.);
272 CPPUNIT_ASSERT_EQUAL(fixture,2*e1);
273 fixture = la2->rotateVector(2*e2,2.);
274 CPPUNIT_ASSERT_EQUAL(fixture,2*e2);
275 fixture = la3->rotateVector(2*e3,2.);
276 CPPUNIT_ASSERT_EQUAL(fixture,2*e3);
277
278 // negative factors
279 fixture = la1->rotateVector(-1*e1,1.);
280 CPPUNIT_ASSERT_EQUAL(fixture,-1*e1);
281 fixture = la2->rotateVector(-1*e2,1.);
282 CPPUNIT_ASSERT_EQUAL(fixture,-1*e2);
283 fixture = la3->rotateVector(-1*e3,1.);
284 CPPUNIT_ASSERT_EQUAL(fixture,-1*e3);
285
286 fixture = la1->rotateVector(-1*e1,2.);
287 CPPUNIT_ASSERT_EQUAL(fixture,-1*e1);
288 fixture = la2->rotateVector(-1*e2,2.);
289 CPPUNIT_ASSERT_EQUAL(fixture,-1*e2);
290 fixture = la3->rotateVector(-1*e3,2.);
291 CPPUNIT_ASSERT_EQUAL(fixture,-1*e3);
292
293
294
295 // now the real rotations
296 // e2 around e1
297 fixture = la1->rotateVector(e2,0);
298 CPPUNIT_ASSERT_EQUAL(fixture,e2);
299 fixture = la1->rotateVector(e2,1./2.*M_PI);
300 CPPUNIT_ASSERT_EQUAL(fixture,-1*e3);
301 fixture = la1->rotateVector(e2,M_PI);
302 CPPUNIT_ASSERT_EQUAL(fixture,-1*e2);
303 fixture = la1->rotateVector(e2,2*M_PI);
304 CPPUNIT_ASSERT_EQUAL(fixture,e2);
305
306 // e3 around e2
307 fixture = la2->rotateVector(e3,0);
308 CPPUNIT_ASSERT_EQUAL(fixture,e3);
309 fixture = la2->rotateVector(e3,1./2.*M_PI);
310 CPPUNIT_ASSERT_EQUAL(fixture,-1*e1);
311 fixture = la2->rotateVector(e3,M_PI);
312 CPPUNIT_ASSERT_EQUAL(fixture,-1*e3);
313 fixture = la2->rotateVector(e3,2*M_PI);
314 CPPUNIT_ASSERT_EQUAL(fixture,e3);
315
316 // e1 around e3
317 fixture = la3->rotateVector(e1,0);
318 CPPUNIT_ASSERT_EQUAL(fixture,e1);
319 fixture = la3->rotateVector(e1,1./2.*M_PI);
320 CPPUNIT_ASSERT_EQUAL(fixture,-1*e2);
321 fixture = la3->rotateVector(e1,M_PI);
322 CPPUNIT_ASSERT_EQUAL(fixture,-1*e1);
323 fixture = la3->rotateVector(e1,2*M_PI);
324 CPPUNIT_ASSERT_EQUAL(fixture,e1);
325
326
327 // and some rotation around the plane lines
328
329 // Vectors on the line
330 fixture = lp1->rotateVector(e1,1.);
331 CPPUNIT_ASSERT_EQUAL(fixture,e1);
332 fixture = lp1->rotateVector(e2,1.);
333 CPPUNIT_ASSERT_EQUAL(fixture,e2);
334
335 fixture = lp2->rotateVector(e2,1.);
336 CPPUNIT_ASSERT_EQUAL(fixture,e2);
337 fixture = lp2->rotateVector(e3,1.);
338 CPPUNIT_ASSERT_EQUAL(fixture,e3);
339
340 fixture = lp3->rotateVector(e3,1.);
341 CPPUNIT_ASSERT_EQUAL(fixture,e3);
342 fixture = lp3->rotateVector(e1,1.);
343 CPPUNIT_ASSERT_EQUAL(fixture,e1);
344
345 // the real stuff
346 fixture = lp1->rotateVector(zeroVec,M_PI);
347 CPPUNIT_ASSERT_EQUAL(fixture,Vector(1,1,0));
348 fixture = lp2->rotateVector(zeroVec,M_PI);
349 CPPUNIT_ASSERT_EQUAL(fixture,Vector(0,1,1));
350 fixture = lp3->rotateVector(zeroVec,M_PI);
351 CPPUNIT_ASSERT_EQUAL(fixture,Vector(1,0,1));
352
353 fixture = lp1->rotateVector(zeroVec,2*M_PI);
354 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
355 fixture = lp2->rotateVector(zeroVec,2*M_PI);
356 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
357 fixture = lp3->rotateVector(zeroVec,2*M_PI);
358 CPPUNIT_ASSERT_EQUAL(fixture,zeroVec);
359}
360
361void LineUnittest::sphereIntersectionTest(){
362 {
363 std::vector<Vector> res = la1->getSphereIntersections();
364 CPPUNIT_ASSERT_EQUAL(res.size(),(size_t)2);
365 CPPUNIT_ASSERT(testDirection(res[0],e1));
366 CPPUNIT_ASSERT(testDirection(res[1],e1));
367 CPPUNIT_ASSERT(res[0]!=res[1]);
368 }
369
370 {
371 std::vector<Vector> res = la2->getSphereIntersections();
372 CPPUNIT_ASSERT_EQUAL(res.size(),(size_t)2);
373 CPPUNIT_ASSERT(testDirection(res[0],e2));
374 CPPUNIT_ASSERT(testDirection(res[1],e2));
375 CPPUNIT_ASSERT(res[0]!=res[1]);
376 }
377
378 {
379 std::vector<Vector> res = la3->getSphereIntersections();
380 CPPUNIT_ASSERT_EQUAL(res.size(),(size_t)2);
381 CPPUNIT_ASSERT(testDirection(res[0],e3));
382 CPPUNIT_ASSERT(testDirection(res[1],e3));
383 CPPUNIT_ASSERT(res[0]!=res[1]);
384 }
385
386 {
387 std::vector<Vector> res = lp1->getSphereIntersections();
388 CPPUNIT_ASSERT_EQUAL(res.size(),(size_t)2);
389 CPPUNIT_ASSERT((res[0]==e1) || (res[0]==e2));
390 CPPUNIT_ASSERT((res[1]==e1) || (res[1]==e2));
391 CPPUNIT_ASSERT(res[0]!=res[1]);
392 }
393
394 {
395 std::vector<Vector> res = lp2->getSphereIntersections();
396 CPPUNIT_ASSERT_EQUAL(res.size(),(size_t)2);
397 CPPUNIT_ASSERT((res[0]==e2) || (res[0]==e3));
398 CPPUNIT_ASSERT((res[1]==e2) || (res[1]==e3));
399 CPPUNIT_ASSERT(res[0]!=res[1]);
400 }
401
402 {
403 std::vector<Vector> res = lp3->getSphereIntersections();
404 CPPUNIT_ASSERT_EQUAL(res.size(),(size_t)2);
405 CPPUNIT_ASSERT((res[0]==e3) || (res[0]==e1));
406 CPPUNIT_ASSERT((res[1]==e3) || (res[1]==e1));
407 CPPUNIT_ASSERT(res[0]!=res[1]);
408 }
409}
Note: See TracBrowser for help on using the repository browser.