[84721b] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[84721b] | 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * ShapeOpsUnitTest.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Jun 18, 2010
|
---|
| 27 | * Author: crueger
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #include <cppunit/CompilerOutputter.h>
|
---|
| 36 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
| 37 | #include <cppunit/ui/text/TestRunner.h>
|
---|
| 38 |
|
---|
| 39 | #include <cmath>
|
---|
[6acc2f3] | 40 | #include <limits>
|
---|
[84721b] | 41 |
|
---|
| 42 | #ifdef HAVE_TESTRUNNER
|
---|
| 43 | #include "UnitTestMain.hpp"
|
---|
| 44 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 45 |
|
---|
| 46 | #include "CodePatterns/Assert.hpp"
|
---|
| 47 |
|
---|
| 48 | #include "Helpers/defs.hpp"
|
---|
| 49 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
| 50 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 51 | #include "Shapes/BaseShapes.hpp"
|
---|
| 52 | #include "Shapes/Shape.hpp"
|
---|
| 53 | #include "Shapes/ShapeOps.hpp"
|
---|
| 54 |
|
---|
| 55 | #include "ShapeOpsUnitTest.hpp"
|
---|
| 56 |
|
---|
| 57 | // Registers the fixture into the 'registry'
|
---|
| 58 | CPPUNIT_TEST_SUITE_REGISTRATION( ShapeOpsTest );
|
---|
| 59 |
|
---|
| 60 | void ShapeOpsTest::setUp()
|
---|
| 61 | {
|
---|
| 62 | // failing asserts should be thrown
|
---|
| 63 | ASSERT_DO(Assert::Throw);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void ShapeOpsTest::tearDown()
|
---|
| 67 | {}
|
---|
| 68 |
|
---|
| 69 | void ShapeOpsTest::resizeTest()
|
---|
| 70 | {
|
---|
| 71 | Shape s = resize(Sphere(), 2.);
|
---|
| 72 |
|
---|
| 73 | CPPUNIT_ASSERT( s.isInside( Vector(1.5,0.,0.) ) );
|
---|
| 74 | CPPUNIT_ASSERT( s.isInside( Vector(0.,1.5,0.) ) );
|
---|
| 75 | CPPUNIT_ASSERT( s.isInside( Vector(0.,0.,1.5) ) );
|
---|
| 76 | CPPUNIT_ASSERT( s.isOnSurface( Vector(2.,0.,0.) ) );
|
---|
| 77 | CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,2.,0.) ) );
|
---|
| 78 | CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,0.,2.) ) );
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void ShapeOpsTest::translateTest()
|
---|
| 82 | {
|
---|
| 83 | Shape s = translate(Sphere(), Vector(1.,0.,0.));
|
---|
| 84 |
|
---|
| 85 | CPPUNIT_ASSERT( s.isInside( Vector(1.5,0.,0.) ) );
|
---|
| 86 | CPPUNIT_ASSERT( s.isInside( Vector(1.,.5,0.) ) );
|
---|
| 87 | CPPUNIT_ASSERT( s.isInside( Vector(1.,0.,.5) ) );
|
---|
| 88 | CPPUNIT_ASSERT( s.isOnSurface( Vector(2.,0.,0.) ) );
|
---|
| 89 | CPPUNIT_ASSERT( s.isOnSurface( Vector(1.,1.,0.) ) );
|
---|
| 90 | CPPUNIT_ASSERT( s.isOnSurface( Vector(1.,0.,1.) ) );
|
---|
| 91 | CPPUNIT_ASSERT( !s.isInside( Vector(-.5,0.,0.) ) );
|
---|
| 92 | CPPUNIT_ASSERT( !s.isInside( Vector(0.,-.5,0.) ) );
|
---|
| 93 | CPPUNIT_ASSERT( !s.isInside( Vector(0.,0.,-.5) ) );
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void ShapeOpsTest::stretchTest()
|
---|
| 97 | {
|
---|
| 98 | #ifndef NDEBUG
|
---|
| 99 | CPPUNIT_ASSERT_THROW( Shape test = stretch(Sphere(), Vector(-2.,0.,0.)), Assert::AssertionFailure );
|
---|
| 100 | #endif
|
---|
| 101 | Shape s = stretch(Sphere(), Vector(2.,1.,1.));
|
---|
| 102 |
|
---|
| 103 | CPPUNIT_ASSERT( s.isInside( Vector(1.5,0.,0.) ) );
|
---|
| 104 | CPPUNIT_ASSERT( !s.isInside( Vector(0.,1.5,0.) ) );
|
---|
| 105 | CPPUNIT_ASSERT( !s.isInside( Vector(0.,0.,1.5) ) );
|
---|
| 106 | CPPUNIT_ASSERT( s.isOnSurface( Vector(2.,0.,0.) ) );
|
---|
| 107 | CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,1.,0.) ) );
|
---|
| 108 | CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,1.,0.) ) );
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void ShapeOpsTest::transformTest()
|
---|
| 112 | {
|
---|
| 113 | RealSpaceMatrix M;
|
---|
| 114 | M.setRotation(45., 0., 0.);
|
---|
| 115 | Shape s = transform(Sphere(), M);
|
---|
| 116 |
|
---|
| 117 | CPPUNIT_ASSERT( s.isInside( Vector(.5,0.,0.) ) );
|
---|
| 118 | CPPUNIT_ASSERT( s.isInside( Vector(0.,.5,0.) ) );
|
---|
| 119 | CPPUNIT_ASSERT( s.isInside( Vector(0.,0.,.5) ) );
|
---|
| 120 | CPPUNIT_ASSERT( s.isOnSurface( Vector(1.,0.,0.) ) );
|
---|
| 121 | CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,1.,0.) ) );
|
---|
| 122 | CPPUNIT_ASSERT( s.isOnSurface( Vector(0.,0.,1.) ) );
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[6acc2f3] | 125 | void ShapeOpsTest::getCenterTest()
|
---|
| 126 | {
|
---|
| 127 | Shape s = Sphere();
|
---|
| 128 | Shape t = Sphere(Vector(2.,0.,0.),1.);
|
---|
| 129 | Vector offset(1.,0.,0);
|
---|
| 130 | RealSpaceMatrix M;
|
---|
| 131 | M.setRotation(45.,0.,0.);
|
---|
| 132 |
|
---|
| 133 | // Sphere
|
---|
| 134 | {
|
---|
| 135 | // sphere at origin
|
---|
| 136 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), s.getCenter() );
|
---|
| 137 |
|
---|
| 138 | // translated sphere
|
---|
| 139 | CPPUNIT_ASSERT_EQUAL( offset, translate(s, offset).getCenter() );
|
---|
| 140 |
|
---|
| 141 | // resized sphere
|
---|
| 142 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), resize(s, 2.).getCenter() );
|
---|
| 143 |
|
---|
| 144 | // stretched sphere
|
---|
| 145 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), stretch(s, Vector(2.,1.,1.)).getCenter() );
|
---|
| 146 |
|
---|
| 147 | // transformed sphere
|
---|
| 148 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), transform(s, M).getCenter() );
|
---|
| 149 |
|
---|
| 150 | // resized and translated sphere
|
---|
| 151 | CPPUNIT_ASSERT_EQUAL( offset, translate(resize(s, 2.), offset).getCenter() );
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | // AND spheres
|
---|
| 155 | {
|
---|
| 156 | // sphere at origin
|
---|
| 157 | CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (s && t).getCenter() );
|
---|
| 158 |
|
---|
| 159 | // translated sphere
|
---|
| 160 | CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate((s && t), offset).getCenter() );
|
---|
| 161 |
|
---|
| 162 | // resized sphere
|
---|
| 163 | CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), resize((s && t), 2.).getCenter() );
|
---|
| 164 |
|
---|
| 165 | // stretched sphere
|
---|
| 166 | CPPUNIT_ASSERT_EQUAL( Vector(1.5,0.,0), (stretch(s, Vector(2.,1.,1.)) && t).getCenter() );
|
---|
| 167 |
|
---|
| 168 | // transformed sphere
|
---|
| 169 | CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (transform(s, M) && t).getCenter() );
|
---|
| 170 |
|
---|
| 171 | // resized and translated sphere
|
---|
| 172 | CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate(resize((s && t), 2.), offset).getCenter() );
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | // OR spheres
|
---|
| 176 | {
|
---|
| 177 | // sphere at origin
|
---|
| 178 | CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (s || t).getCenter() );
|
---|
| 179 |
|
---|
| 180 | // translated sphere
|
---|
| 181 | CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate((s || t), offset).getCenter() );
|
---|
| 182 |
|
---|
| 183 | // resized sphere
|
---|
| 184 | CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), resize((s || t), 2.).getCenter() );
|
---|
| 185 |
|
---|
| 186 | // stretched sphere
|
---|
| 187 | CPPUNIT_ASSERT_EQUAL( Vector(0.5,0.,0), (stretch(s, Vector(2.,1.,1.)) || t).getCenter() );
|
---|
| 188 |
|
---|
| 189 | // transformed sphere
|
---|
| 190 | CPPUNIT_ASSERT_EQUAL( Vector(1.,0.,0), (transform(s, M) || t).getCenter() );
|
---|
| 191 |
|
---|
| 192 | // resized and translated sphere
|
---|
| 193 | CPPUNIT_ASSERT_EQUAL( Vector(2.,0.,0), translate(resize((s || t), 2.), offset).getCenter() );
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | // NOT spheres
|
---|
| 197 | {
|
---|
| 198 | // sphere at origin
|
---|
| 199 | CPPUNIT_ASSERT_EQUAL( Vector(0.,0.,0), (!s).getCenter() );
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | void ShapeOpsTest::getRadiusTest()
|
---|
| 204 | {
|
---|
| 205 | Shape s = Sphere();
|
---|
| 206 | Shape t = Sphere(Vector(2.,0.,0.),1.);
|
---|
| 207 | Vector offset(1.,0.,0);
|
---|
| 208 | RealSpaceMatrix M;
|
---|
| 209 | M.setRotation(45.,0.,0.);
|
---|
| 210 |
|
---|
| 211 | // Sphere
|
---|
| 212 | {
|
---|
| 213 | // sphere at origin
|
---|
| 214 | CPPUNIT_ASSERT_EQUAL( 1., s.getRadius() );
|
---|
| 215 |
|
---|
| 216 | // translated sphere
|
---|
| 217 | CPPUNIT_ASSERT_EQUAL( 1., translate(s, offset).getRadius() );
|
---|
| 218 |
|
---|
| 219 | // resized sphere
|
---|
| 220 | CPPUNIT_ASSERT_EQUAL( 2., resize(s, 2.).getRadius() );
|
---|
| 221 |
|
---|
| 222 | // stretched sphere
|
---|
| 223 | CPPUNIT_ASSERT_EQUAL( 2., stretch(s, Vector(2.,1.,1.)).getRadius() );
|
---|
| 224 |
|
---|
| 225 | // transformed sphere
|
---|
| 226 | CPPUNIT_ASSERT_EQUAL( 1., transform(s, M).getRadius() );
|
---|
| 227 |
|
---|
| 228 | // resized and translated sphere
|
---|
| 229 | CPPUNIT_ASSERT_EQUAL( 2., translate(resize(s, 2.), offset).getRadius() );
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | // AND spheres
|
---|
| 233 | {
|
---|
| 234 | // sphere at origin
|
---|
| 235 | CPPUNIT_ASSERT_EQUAL( 1., (s && t).getRadius() );
|
---|
| 236 |
|
---|
| 237 | // translated sphere
|
---|
| 238 | CPPUNIT_ASSERT_EQUAL( 1., translate((s && t), offset).getRadius() );
|
---|
| 239 |
|
---|
| 240 | // resized sphere
|
---|
| 241 | CPPUNIT_ASSERT_EQUAL( 2., resize((s && t), 2.).getRadius() );
|
---|
| 242 |
|
---|
| 243 | // stretched sphere
|
---|
| 244 | CPPUNIT_ASSERT_EQUAL( 1., (stretch(s, Vector(2.,1.,1.)) && t).getRadius() );
|
---|
| 245 |
|
---|
| 246 | // transformed sphere
|
---|
| 247 | CPPUNIT_ASSERT_EQUAL( 1., (transform(s, M) && t).getRadius() );
|
---|
| 248 |
|
---|
| 249 | // resized and translated sphere
|
---|
| 250 | CPPUNIT_ASSERT_EQUAL( 2., translate(resize((s && t), 2.), offset).getRadius() );
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | // OR spheres
|
---|
| 254 | {
|
---|
| 255 | // sphere at origin
|
---|
| 256 | CPPUNIT_ASSERT_EQUAL( 2., (s || t).getRadius() );
|
---|
| 257 |
|
---|
| 258 | // translated sphere
|
---|
| 259 | CPPUNIT_ASSERT_EQUAL( 2., translate((s || t), offset).getRadius() );
|
---|
| 260 |
|
---|
| 261 | // resized sphere
|
---|
| 262 | CPPUNIT_ASSERT_EQUAL( 4., resize((s || t), 2.).getRadius() );
|
---|
| 263 |
|
---|
| 264 | // stretched sphere
|
---|
| 265 | CPPUNIT_ASSERT_EQUAL( 2.5, (stretch(s, Vector(2.,1.,1.)) || t).getRadius() );
|
---|
| 266 |
|
---|
| 267 | // transformed sphere
|
---|
| 268 | CPPUNIT_ASSERT_EQUAL( 2., (transform(s, M) || t).getRadius() );
|
---|
| 269 |
|
---|
| 270 | // resized and translated sphere
|
---|
| 271 | CPPUNIT_ASSERT_EQUAL( 4., translate(resize((s || t), 2.), offset).getRadius() );
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | // NOT spheres
|
---|
| 275 | {
|
---|
| 276 | // sphere at origin
|
---|
| 277 | CPPUNIT_ASSERT_EQUAL( std::numeric_limits<double>::infinity(), (!s).getRadius() );
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[13202c] | 281 | void ShapeOpsTest::PointsOnSurfaceTest(){
|
---|
| 282 |
|
---|
| 283 | RealSpaceMatrix M;
|
---|
| 284 | M.setRotation(32., 5., 5.);
|
---|
| 285 | Shape s = transform(Cuboid(), M);
|
---|
| 286 | size_t N = 200;
|
---|
| 287 | std::vector<Vector> PointsOnSurface = s.getHomogeneousPointsOnSurface(N);
|
---|
| 288 | for (std::vector<Vector>::const_iterator iter = PointsOnSurface.begin();
|
---|
| 289 | iter != PointsOnSurface.end();
|
---|
| 290 | ++iter) {
|
---|
| 291 | CPPUNIT_ASSERT(s.isOnSurface(*iter));
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|