source: src/unittests/LineUnittest.cpp@ fd4905

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 v1.0.3
Last change on this file since fd4905 was bcf653, checked in by Frederik Heber <heber@…>, 14 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

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