Skip to content
Snippets Groups Projects
Commit 4a6765cc authored by Tom Fischer's avatar Tom Fischer
Browse files

[MaL] Rm MaL::Vector3.

parent 74b0bf8e
No related branches found
No related tags found
No related merge requests found
/**
* \file
* \author Lars Bilke
* \date 2009-10-27
* \brief Definition of the Vector3 class.
* From: http://www.strout.net
* with modifications to derive from TemplatePoint
*
* \copyright
* Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
#pragma once
#include <cmath>
#include "MathTools.h"
#include "TemplatePoint.h"
namespace MathLib
{
/**
* The Vector3 class defines a three-dimensional vector, with appropriate
* operators.
*/
template <class T>
class TemplateVector3 : public MathLib::TemplatePoint<T>
{
public:
/**
* Default constructor. All coordinates are set to zero.
*/
TemplateVector3() = default;
TemplateVector3(T x0, T x1, T x2)
{
this->_x[0] = x0;
this->_x[1] = x1;
this->_x[2] = x2;
}
/**
* Copy constructor.
*/
TemplateVector3(TemplateVector3<T> const& /* v */) = default;
TemplateVector3<T>& operator=(TemplateVector3<T> const& /* v */) = default;
/**
* Construct Vector3 from TemplatePoint.
*/
explicit TemplateVector3(TemplatePoint<T,3> const& p) :
TemplatePoint<T>(p)
{}
/** Constructs the vector \f$v=(b-a)\f$ from the given points,
* which starts in point \f$a\f$ and ends in point \f$b\f$
*/
TemplateVector3(const MathLib::TemplatePoint<T> &a, const MathLib::TemplatePoint<T> &b) :
MathLib::TemplatePoint<T>()
{
this->_x[0] = b[0] - a[0];
this->_x[1] = b[1] - a[1];
this->_x[2] = b[2] - a[2];
}
// vector arithmetic
TemplateVector3 operator+(TemplateVector3 const& v) const
{
return TemplateVector3(this->_x[0]+v[0], this->_x[1]+v[1], this->_x[2]+v[2]);
}
TemplateVector3 operator-(TemplateVector3 const& v) const
{
return TemplateVector3(this->_x[0]-v[0], this->_x[1]-v[1], this->_x[2]-v[2]);
}
TemplateVector3& operator+=(TemplateVector3 const& v)
{
for (std::size_t i(0); i < 3; i++)
{
this->_x[i] += v[i];
}
return *this;
}
TemplateVector3& operator-=(const TemplateVector3 & pV)
{
for (std::size_t i(0); i < 3; i++)
{
this->_x[i] -= pV[i];
}
return *this;
}
TemplateVector3& operator*=(double s)
{
for (std::size_t i(0); i < 3; i++)
{
this->_x[i] *= s;
}
return *this;
}
/**
* After applying the normalize operator to the vector its length is 1.0.
*/
void normalize()
{
const double s(1/getLength());
for (std::size_t i(0); i < 3; i++)
{
this->_x[i] *= s;
}
}
/// Returns a normalized version of this vector
TemplateVector3<double> getNormalizedVector() const
{
if (getSqrLength() == 0)
{
return TemplateVector3<double>(0, 0, 0);
}
TemplateVector3<double> norm_vec (this->_x[0], this->_x[1], this->_x[2]);
norm_vec.normalize();
return norm_vec;
}
/// Returns the squared length
double getSqrLength() const
{
return this->_x[0]*this->_x[0] + this->_x[1]*this->_x[1] + this->_x[2]*this->_x[2];
}
/// Returns the length
double getLength() const { return sqrt(getSqrLength()); }
/** scalarProduct, implementation of scalar product,
* sometimes called dot or inner product.
*/
template <typename T1>
friend T1 scalarProduct(TemplateVector3<T1> const& v, TemplateVector3<T1> const& w);
/** crossProduct: implementation of cross product,
* sometimes called outer product.
*/
template <typename T1>
friend TemplateVector3<T1> crossProduct(
TemplateVector3<T1> const& v,
TemplateVector3<T1> const& w);
/** multiplication with a scalar s */
template <typename T1>
friend TemplateVector3<T1> operator*(
TemplateVector3<T1> const& v,
double s);
template <typename T1>
friend TemplateVector3<T1> operator*(
double s,
TemplateVector3<T1> const& v);
};
template <typename T>
T scalarProduct(TemplateVector3<T> const& v, TemplateVector3<T> const& w)
{
return v._x[0] * w._x[0] + v._x[1] * w._x[1] + v._x[2] * w._x[2];
}
template <typename T1>
TemplateVector3<T1> crossProduct(
TemplateVector3<T1> const& v,
TemplateVector3<T1> const& w)
{
return TemplateVector3<T1>(
v._x[1] * w._x[2] - v._x[2] * w._x[1],
v._x[2] * w._x[0] - v._x[0] * w._x[2],
v._x[0] * w._x[1] - v._x[1] * w._x[0]);
}
template <typename T1> TemplateVector3<T1> operator*(
TemplateVector3<T1> const& v,
double s)
{
return TemplateVector3<T1>(v[0] * s, v[1] * s, v[2] * s);
}
template <typename T1> TemplateVector3<T1> operator*(
double s,
TemplateVector3<T1> const& v)
{
return v * s;
}
using Vector3 = TemplateVector3<double>;
} // namespace MathLib
/**
* @file TestVector3.cpp
* @date Feb 28, 2014
*
* \copyright
* Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*/
#include <array>
#include "gtest/gtest.h"
#include "MathLib/Vector3.h"
#include "GeoLib/Point.h"
using namespace MathLib;
TEST(MathLib, TestVector3Constructor)
{
// *** test default constructor
Vector3 u;
// test coordinates of default constructed vec
ASSERT_NEAR(0.0, u[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(0.0, u[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(0.0, u[2], std::numeric_limits<double>::epsilon());
// *** test constructor taking 3 double values
Vector3 v(1.0, 3.0, 5.0);
ASSERT_NEAR(1.0, v[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(3.0, v[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(5.0, v[2], std::numeric_limits<double>::epsilon());
// *** test copy constructor
Vector3 v_copy(v);
// test equality of coordinates
ASSERT_NEAR(v[0], v_copy[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(v[1], v_copy[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(v[2], v_copy[2], std::numeric_limits<double>::epsilon());
// *** test constructor taking TemplatePoint
std::array<double,3> ap = {{0, 1, 2}};
TemplatePoint<double> p(ap);
Vector3 vp(p);
ASSERT_NEAR(0.0, vp[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(1.0, vp[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(2.0, vp[2], std::numeric_limits<double>::epsilon());
// *** test constructing Vector from two TemplatePoints
std::array<double,3> aa = {{1, 2, 3}}; // necessary for old compilers
std::array<double,3> ab = {{6, 5, 4}}; // necessary for old compilers
TemplatePoint<double,3> a(aa);
TemplatePoint<double,3> b(ab);
Vector3 w(a,b);
// test coordinates of constructed Vector3 w = (b-a)
ASSERT_NEAR(5.0, w[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(3.0, w[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(1.0, w[2], std::numeric_limits<double>::epsilon());
}
TEST(MathLib, TestVector3Operators)
{
Vector3 v;
// access operator
v[0] = 1.0;
v[1] = 3.0;
v[2] = 5.0;
ASSERT_NEAR(1.0, v[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(3.0, v[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(5.0, v[2], std::numeric_limits<double>::epsilon());
Vector3 w(5.0, 3.0, 1.0);
// operator+
Vector3 res(v+w);
ASSERT_NEAR(6.0, res[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(6.0, res[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(6.0, res[2], std::numeric_limits<double>::epsilon());
// operator-
res = v-w;
ASSERT_NEAR(-4.0, res[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR( 0.0, res[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR( 4.0, res[2], std::numeric_limits<double>::epsilon());
// test operator*=
v *= 2.0;
ASSERT_NEAR(2.0, v[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(6.0, v[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(10.0, v[2], std::numeric_limits<double>::epsilon());
// test operator+=
v += w;
ASSERT_NEAR(7.0, v[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(9.0, v[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(11.0, v[2], std::numeric_limits<double>::epsilon());
// test operator-=
v -= w;
ASSERT_NEAR(2.0, v[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(6.0, v[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(10.0, v[2], std::numeric_limits<double>::epsilon());
}
TEST(MathLib, TestVector3Multiplications)
{
// test scalar product
Vector3 v(1.0, 3.0, 5.0);
Vector3 w(3.0, -2.0, 1.0);
ASSERT_NEAR(2.0, scalarProduct(v,w), std::numeric_limits<double>::epsilon());
// test cross product
Vector3 e1(1.0, 0.0, 0.0);
Vector3 e2(0.0, 1.0, 0.0);
Vector3 e3(0.0, 0.0, 1.0);
Vector3 res_e1e2(crossProduct(e1, e2)); // should be e3
ASSERT_NEAR(e3[0], res_e1e2[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(e3[1], res_e1e2[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(e3[2], res_e1e2[2], std::numeric_limits<double>::epsilon());
Vector3 res_e2e3(crossProduct(e2, e3)); // should be e1
ASSERT_NEAR(e1[0], res_e2e3[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(e1[1], res_e2e3[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(e1[2], res_e2e3[2], std::numeric_limits<double>::epsilon());
Vector3 res_e3e1(crossProduct(e3, e1)); // should be e2
ASSERT_NEAR(e2[0], res_e3e1[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(e2[1], res_e3e1[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(e2[2], res_e3e1[2], std::numeric_limits<double>::epsilon());
Vector3 res_e2e1(crossProduct(e2, e1)); // should be -e3
ASSERT_NEAR(-e3[0], res_e2e1[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-e3[1], res_e2e1[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-e3[2], res_e2e1[2], std::numeric_limits<double>::epsilon());
Vector3 res_e3e2(crossProduct(e3, e2)); // should be -e1
ASSERT_NEAR(-e1[0], res_e3e2[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-e1[1], res_e3e2[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-e1[2], res_e3e2[2], std::numeric_limits<double>::epsilon());
Vector3 res_e1e3(crossProduct(e1, e3)); // should be -e2
ASSERT_NEAR(-e2[0], res_e1e3[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-e2[1], res_e1e3[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-e2[2], res_e1e3[2], std::numeric_limits<double>::epsilon());
// test multplication with scalar
v = -1.0 * v;
ASSERT_NEAR(-1.0, v[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-3.0, v[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(-5.0, v[2], std::numeric_limits<double>::epsilon());
v = v * -1.0;
ASSERT_NEAR(1.0, v[0], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(3.0, v[1], std::numeric_limits<double>::epsilon());
ASSERT_NEAR(5.0, v[2], std::numeric_limits<double>::epsilon());
// test normalisation
v.normalize();
ASSERT_NEAR(1.0, v.getLength(), std::numeric_limits<double>::epsilon());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment