2001-12-12 22:19:17 +00:00
|
|
|
// vector_test.cpp (Vector<> test functions)
|
|
|
|
|
//
|
|
|
|
|
// The WorldForge Project
|
|
|
|
|
// Copyright (C) 2001 The WorldForge Project
|
|
|
|
|
//
|
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-05-16 14:12:35 +02:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-12-12 22:19:17 +00:00
|
|
|
//
|
|
|
|
|
// For information about WorldForge and its authors, please contact
|
|
|
|
|
// the Worldforge Web Site at http://www.worldforge.org.
|
|
|
|
|
|
|
|
|
|
// Author: Ron Steinke
|
|
|
|
|
// Created: 2001-12-7
|
|
|
|
|
|
2008-11-10 17:16:23 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-10-02 16:32:47 +02:00
|
|
|
#include "wfmath/const.h"
|
|
|
|
|
#include "wfmath/vector.h"
|
|
|
|
|
#include "wfmath/rotmatrix.h"
|
|
|
|
|
#include "wfmath/stream.h"
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2002-01-09 20:38:53 +00:00
|
|
|
#include "general_test.h"
|
|
|
|
|
|
2011-01-29 12:07:57 -08:00
|
|
|
#include <cmath>
|
|
|
|
|
|
2002-01-23 01:38:16 +00:00
|
|
|
using namespace WFMath;
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2011-04-11 21:48:42 +01:00
|
|
|
template<int dim>
|
2001-12-29 23:10:55 +00:00
|
|
|
void test_vector(const Vector<dim>& v)
|
2001-12-12 22:19:17 +00:00
|
|
|
{
|
2002-03-12 22:16:46 +00:00
|
|
|
std::cout << "Testing vector: " << v << std::endl;
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2002-01-09 20:38:53 +00:00
|
|
|
test_general(v);
|
|
|
|
|
|
2002-01-01 02:08:41 +00:00
|
|
|
CoordType sqr_mag = v.sqrMag();
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2011-05-01 21:02:47 +01:00
|
|
|
assert(Equal(std::sqrt(sqr_mag), v.mag()));
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2002-02-01 20:43:41 +00:00
|
|
|
assert(Equal(sqr_mag, Dot(v, v)));
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2001-12-29 23:10:55 +00:00
|
|
|
Vector<dim> v1, v2;
|
2001-12-12 22:19:17 +00:00
|
|
|
|
|
|
|
|
v1.zero();
|
|
|
|
|
v1[0] = 1;
|
2001-12-29 23:10:55 +00:00
|
|
|
for(int i = 0; i < dim; ++i)
|
2001-12-12 22:19:17 +00:00
|
|
|
v2[i] = 1;
|
|
|
|
|
|
|
|
|
|
const int steps = 8;
|
|
|
|
|
|
2001-12-29 23:10:55 +00:00
|
|
|
Vector<dim> vcopy = v;
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2001-12-29 23:10:55 +00:00
|
|
|
for(int j = 0; j < dim; ++j) {
|
2001-12-12 22:19:17 +00:00
|
|
|
for(int i = 0; i < steps; ++i) {
|
2012-02-09 10:09:59 +00:00
|
|
|
vcopy.rotate(v1, v2, 2 * numeric_constants<CoordType>::pi() / steps);
|
2002-03-12 22:16:46 +00:00
|
|
|
// std::cout << vcopy << std::endl;
|
2002-02-01 20:43:41 +00:00
|
|
|
assert(Equal(sqr_mag, vcopy.sqrMag()));
|
2001-12-12 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
2001-12-29 23:10:55 +00:00
|
|
|
for(int i = 0; i < dim; ++i)
|
2002-02-01 20:43:41 +00:00
|
|
|
assert(Equal(v[i], vcopy[i]));
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2002-01-09 20:38:53 +00:00
|
|
|
v2 -= v1 / 2; // operator-=(), operator/()
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2001-12-29 23:10:55 +00:00
|
|
|
int k = (j < dim - 1) ? j + 1 : 0;
|
2012-02-09 10:09:59 +00:00
|
|
|
v1.rotate(j, k, numeric_constants<CoordType>::pi() / 2);
|
2001-12-12 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
2002-01-09 20:38:53 +00:00
|
|
|
v2 *= 2; // operator*=()
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2001-12-29 23:10:55 +00:00
|
|
|
for(int i = 0; i < dim; ++i)
|
2019-12-16 22:58:33 +01:00
|
|
|
assert(Equal(v2[i], 1.0));
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2002-01-09 20:38:53 +00:00
|
|
|
// operator+(), operator-(), operator*() (pre and post), operator/()
|
2002-01-01 02:08:41 +00:00
|
|
|
CoordType check = Dot((v1 + v2) * 5 - v2 / 4, 2 * v2);
|
2019-12-16 22:58:33 +01:00
|
|
|
assert(Equal((10.0 + dim * 38.0 / 4.0), check));
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2002-01-09 20:38:53 +00:00
|
|
|
Vector<dim> v3 = v;
|
|
|
|
|
v3 += v;
|
|
|
|
|
v3 *= 2;
|
|
|
|
|
v3 -= 2 * v;
|
|
|
|
|
v3 /= 2;
|
|
|
|
|
assert(v == v3);
|
|
|
|
|
for(int i = 0; i < dim; ++i)
|
|
|
|
|
assert(v[i] == v3[i]); // const and non-const operator[]()
|
|
|
|
|
|
2002-01-01 02:08:41 +00:00
|
|
|
CoordType check_mag = v.sloppyMag() / v.mag();
|
2001-12-12 22:19:17 +00:00
|
|
|
|
2012-02-27 19:07:51 +00:00
|
|
|
assert(1 - numeric_constants<CoordType>::epsilon() < check_mag);
|
|
|
|
|
assert(check_mag < Vector<dim>::sloppyMagMax() + numeric_constants<CoordType>::epsilon());
|
2002-01-09 20:38:53 +00:00
|
|
|
|
2015-09-26 14:39:07 +02:00
|
|
|
//Check that an invalid vector isn't equal to a valid vector, even if the values are equal
|
|
|
|
|
Vector<dim> invalid_1;
|
|
|
|
|
Vector<dim> invalid_2;
|
|
|
|
|
for (size_t i = 0; i < dim; ++i) {
|
2017-12-21 10:02:02 +01:00
|
|
|
invalid_1[i] = 0.0;
|
|
|
|
|
invalid_2[i] = 0.0;
|
2015-09-26 14:39:07 +02:00
|
|
|
}
|
|
|
|
|
assert(invalid_1 != Vector<dim>::ZERO());
|
|
|
|
|
|
|
|
|
|
//Two invalid points are never equal
|
|
|
|
|
assert(invalid_1 != invalid_2);
|
|
|
|
|
|
2002-01-09 20:38:53 +00:00
|
|
|
// Still need Dot(), Angle(), normalize(), mirror()
|
2001-12-12 22:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
2002-01-09 20:38:53 +00:00
|
|
|
Vector<2> v2(1, -1);
|
2012-02-09 10:09:59 +00:00
|
|
|
Vector<3> v3(1, -1, numeric_constants<CoordType>::sqrt2());
|
2002-01-09 20:38:53 +00:00
|
|
|
|
|
|
|
|
test_vector(v2);
|
|
|
|
|
test_vector(v3);
|
2009-01-03 16:12:30 +01:00
|
|
|
|
|
|
|
|
Vector<2> zero2 = Vector<2>::ZERO();
|
|
|
|
|
assert(zero2.x() == 0 && zero2.y() == 0);
|
|
|
|
|
Vector<3> zero3 = Vector<3>::ZERO();
|
|
|
|
|
assert(zero3.x() == 0 && zero3.y() == 0 && zero3.z() == 0);
|
2002-01-09 20:38:53 +00:00
|
|
|
|
|
|
|
|
assert(v2.sloppyMag() / v2.mag() < Vector<2>::sloppyMagMax());
|
|
|
|
|
assert(v3.sloppyMag() / v3.mag() < Vector<3>::sloppyMagMax());
|
|
|
|
|
|
|
|
|
|
v2.sloppyNorm(1);
|
|
|
|
|
v3.sloppyNorm(1);
|
2001-12-12 22:19:17 +00:00
|
|
|
|
|
|
|
|
assert((Vector<3>(1, 0, 0).rotate(Cross(Vector<3>(1, 0, 0), Vector<3>(0, 1, 0)),
|
2012-02-09 10:09:59 +00:00
|
|
|
numeric_constants<CoordType>::pi() / 2) - Vector<3>(0, 1, 0)).sqrMag()
|
2012-02-27 19:07:51 +00:00
|
|
|
< numeric_constants<CoordType>::epsilon() * numeric_constants<CoordType>::epsilon());
|
2002-01-09 20:38:53 +00:00
|
|
|
|
|
|
|
|
// Need 2D+3D stuff
|
2001-12-12 22:19:17 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|