source: src/Shapes/unittests/ShapeOpsUnitTest.cpp@ 6f0507e

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 6f0507e was 6acc2f3, checked in by Frederik Heber <heber@…>, 13 years ago

Added getCenter() and getRadius to all Shapes.

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * ShapeOpsUnitTest.cpp
10 *
11 * Created on: Jun 18, 2010
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include <cppunit/CompilerOutputter.h>
21#include <cppunit/extensions/TestFactoryRegistry.h>
22#include <cppunit/ui/text/TestRunner.h>
23
24#include <cmath>
25#include <limits>
26
27#ifdef HAVE_TESTRUNNER
28#include "UnitTestMain.hpp"
29#endif /*HAVE_TESTRUNNER*/
30
31#include "CodePatterns/Assert.hpp"
32
33#include "Helpers/defs.hpp"
34#include "LinearAlgebra/RealSpaceMatrix.hpp"
35#include "LinearAlgebra/Vector.hpp"
36#include "Shapes/BaseShapes.hpp"
37#include "Shapes/Shape.hpp"
38#include "Shapes/ShapeOps.hpp"
39
40#include "ShapeOpsUnitTest.hpp"
41
42// Registers the fixture into the 'registry'
43CPPUNIT_TEST_SUITE_REGISTRATION( ShapeOpsTest );
44
45void ShapeOpsTest::setUp()
46{
47 // failing asserts should be thrown
48 ASSERT_DO(Assert::Throw);
49}
50
51void ShapeOpsTest::tearDown()
52{}
53
54void ShapeOpsTest::resizeTest()
55{
56 Shape s = resize(Sphere(), 2.);
57
58 CPPUNIT_ASSERT( s.isInside( Vector(1.5,0.,0.) ) );
59 CPPUNIT_ASSERT( s.isInside( Vector(0.,1.5,0.) ) );
60 CPPUNIT_ASSERT( s.isInside( Vector(0.,0.,1.5) ) );
61 CPPUNIT_ASSERT( s.isOnSurface( Vector(2.,0.,0.) ) );
62 CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,2.,0.) ) );
63 CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,0.,2.) ) );
64}
65
66void ShapeOpsTest::translateTest()
67{
68 Shape s = translate(Sphere(), Vector(1.,0.,0.));
69
70 CPPUNIT_ASSERT( s.isInside( Vector(1.5,0.,0.) ) );
71 CPPUNIT_ASSERT( s.isInside( Vector(1.,.5,0.) ) );
72 CPPUNIT_ASSERT( s.isInside( Vector(1.,0.,.5) ) );
73 CPPUNIT_ASSERT( s.isOnSurface( Vector(2.,0.,0.) ) );
74 CPPUNIT_ASSERT( s.isOnSurface( Vector(1.,1.,0.) ) );
75 CPPUNIT_ASSERT( s.isOnSurface( Vector(1.,0.,1.) ) );
76 CPPUNIT_ASSERT( !s.isInside( Vector(-.5,0.,0.) ) );
77 CPPUNIT_ASSERT( !s.isInside( Vector(0.,-.5,0.) ) );
78 CPPUNIT_ASSERT( !s.isInside( Vector(0.,0.,-.5) ) );
79}
80
81void ShapeOpsTest::stretchTest()
82{
83#ifndef NDEBUG
84 CPPUNIT_ASSERT_THROW( Shape test = stretch(Sphere(), Vector(-2.,0.,0.)), Assert::AssertionFailure );
85#endif
86 Shape s = stretch(Sphere(), Vector(2.,1.,1.));
87
88 CPPUNIT_ASSERT( s.isInside( Vector(1.5,0.,0.) ) );
89 CPPUNIT_ASSERT( !s.isInside( Vector(0.,1.5,0.) ) );
90 CPPUNIT_ASSERT( !s.isInside( Vector(0.,0.,1.5) ) );
91 CPPUNIT_ASSERT( s.isOnSurface( Vector(2.,0.,0.) ) );
92 CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,1.,0.) ) );
93 CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,1.,0.) ) );
94}
95
96void ShapeOpsTest::transformTest()
97{
98 RealSpaceMatrix M;
99 M.setRotation(45., 0., 0.);
100 Shape s = transform(Sphere(), M);
101
102 CPPUNIT_ASSERT( s.isInside( Vector(.5,0.,0.) ) );
103 CPPUNIT_ASSERT( s.isInside( Vector(0.,.5,0.) ) );
104 CPPUNIT_ASSERT( s.isInside( Vector(0.,0.,.5) ) );
105 CPPUNIT_ASSERT( s.isOnSurface( Vector(1.,0.,0.) ) );
106 CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,1.,0.) ) );
107 CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,0.,1.) ) );
108}
109
110void ShapeOpsTest::getCenterTest()
111{
112 Shape s = Sphere();
113 Shape t = Sphere(Vector(2.,0.,0.),1.);
114 Vector offset(1.,0.,0);
115 RealSpaceMatrix M;
116 M.setRotation(45.,0.,0.);
117
118 // Sphere
119 {
120 // sphere at origin
121 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), s.getCenter() );
122
123 // translated sphere
124 CPPUNIT_ASSERT_EQUAL( offset, translate(s, offset).getCenter() );
125
126 // resized sphere
127 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), resize(s, 2.).getCenter() );
128
129 // stretched sphere
130 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), stretch(s, Vector(2.,1.,1.)).getCenter() );
131
132 // transformed sphere
133 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), transform(s, M).getCenter() );
134
135 // resized and translated sphere
136 CPPUNIT_ASSERT_EQUAL( offset, translate(resize(s, 2.), offset).getCenter() );
137 }
138
139 // AND spheres
140 {
141 // sphere at origin
142 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (s && t).getCenter() );
143
144 // translated sphere
145 CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate((s && t), offset).getCenter() );
146
147 // resized sphere
148 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), resize((s && t), 2.).getCenter() );
149
150 // stretched sphere
151 CPPUNIT_ASSERT_EQUAL( Vector(1.5,0.,0), (stretch(s, Vector(2.,1.,1.)) && t).getCenter() );
152
153 // transformed sphere
154 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (transform(s, M) && t).getCenter() );
155
156 // resized and translated sphere
157 CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate(resize((s && t), 2.), offset).getCenter() );
158 }
159
160 // OR spheres
161 {
162 // sphere at origin
163 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (s || t).getCenter() );
164
165 // translated sphere
166 CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate((s || t), offset).getCenter() );
167
168 // resized sphere
169 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), resize((s || t), 2.).getCenter() );
170
171 // stretched sphere
172 CPPUNIT_ASSERT_EQUAL( Vector(0.5,0.,0), (stretch(s, Vector(2.,1.,1.)) || t).getCenter() );
173
174 // transformed sphere
175 CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (transform(s, M) || t).getCenter() );
176
177 // resized and translated sphere
178 CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate(resize((s || t), 2.), offset).getCenter() );
179 }
180
181 // NOT spheres
182 {
183 // sphere at origin
184 CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), (!s).getCenter() );
185 }
186}
187
188void ShapeOpsTest::getRadiusTest()
189{
190 Shape s = Sphere();
191 Shape t = Sphere(Vector(2.,0.,0.),1.);
192 Vector offset(1.,0.,0);
193 RealSpaceMatrix M;
194 M.setRotation(45.,0.,0.);
195
196 // Sphere
197 {
198 // sphere at origin
199 CPPUNIT_ASSERT_EQUAL( 1., s.getRadius() );
200
201 // translated sphere
202 CPPUNIT_ASSERT_EQUAL( 1., translate(s, offset).getRadius() );
203
204 // resized sphere
205 CPPUNIT_ASSERT_EQUAL( 2., resize(s, 2.).getRadius() );
206
207 // stretched sphere
208 CPPUNIT_ASSERT_EQUAL( 2., stretch(s, Vector(2.,1.,1.)).getRadius() );
209
210 // transformed sphere
211 CPPUNIT_ASSERT_EQUAL( 1., transform(s, M).getRadius() );
212
213 // resized and translated sphere
214 CPPUNIT_ASSERT_EQUAL( 2., translate(resize(s, 2.), offset).getRadius() );
215 }
216
217 // AND spheres
218 {
219 // sphere at origin
220 CPPUNIT_ASSERT_EQUAL( 1., (s && t).getRadius() );
221
222 // translated sphere
223 CPPUNIT_ASSERT_EQUAL( 1., translate((s && t), offset).getRadius() );
224
225 // resized sphere
226 CPPUNIT_ASSERT_EQUAL( 2., resize((s && t), 2.).getRadius() );
227
228 // stretched sphere
229 CPPUNIT_ASSERT_EQUAL( 1., (stretch(s, Vector(2.,1.,1.)) && t).getRadius() );
230
231 // transformed sphere
232 CPPUNIT_ASSERT_EQUAL( 1., (transform(s, M) && t).getRadius() );
233
234 // resized and translated sphere
235 CPPUNIT_ASSERT_EQUAL( 2., translate(resize((s && t), 2.), offset).getRadius() );
236 }
237
238 // OR spheres
239 {
240 // sphere at origin
241 CPPUNIT_ASSERT_EQUAL( 2., (s || t).getRadius() );
242
243 // translated sphere
244 CPPUNIT_ASSERT_EQUAL( 2., translate((s || t), offset).getRadius() );
245
246 // resized sphere
247 CPPUNIT_ASSERT_EQUAL( 4., resize((s || t), 2.).getRadius() );
248
249 // stretched sphere
250 CPPUNIT_ASSERT_EQUAL( 2.5, (stretch(s, Vector(2.,1.,1.)) || t).getRadius() );
251
252 // transformed sphere
253 CPPUNIT_ASSERT_EQUAL( 2., (transform(s, M) || t).getRadius() );
254
255 // resized and translated sphere
256 CPPUNIT_ASSERT_EQUAL( 4., translate(resize((s || t), 2.), offset).getRadius() );
257 }
258
259 // NOT spheres
260 {
261 // sphere at origin
262 CPPUNIT_ASSERT_EQUAL( std::numeric_limits<double>::infinity(), (!s).getRadius() );
263 }
264}
265
Note: See TracBrowser for help on using the repository browser.