1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
#pragma once
#include <sstream>
/* greebo: This file contains the templated class definition of the three-component vector
*
* BasicVector4: A vector with three components of type <T>
*
* The BasicVector4 is equipped with the most important operators like *, *= and so on.
*
* Note: The most commonly used Vector4 is a BasicVector4<float>, this is also defined in this file
*
* Note: that the multiplication of a Vector4 with another one (Vector4*Vector4) does NOT
* result in an inner product but in a component-wise scaling. Use the .dot() method to
* execute an inner product of two vectors.
*/
#include "lrint.h"
#include "FloatTools.h"
#include "Vector3.h"
/// A 4-element vector of type <T>
template<typename T>
class BasicVector4
{
public:
/// Eigen vector type to store data
using Eigen_T = Eigen::Matrix<T, 4, 1>;
/// Public typedef to read the type of our elements
using ElementType = T;
private:
// Eigen vector for storage and calculations
Eigen_T _v;
public:
/// Default construct a zero vector
BasicVector4(): _v(0, 0, 0, 0)
{}
/**
* \brief Construct a BasicVector4 out of 4 explicit values.
*
* If the W coordinate is unspecified it will default to 1.
*/
BasicVector4(T x_, T y_, T z_, T w_ = 1)
: _v(x_, y_, z_, w_)
{}
/// Construct from another BasicVector4 with a compatible element type
template<typename U> BasicVector4(const BasicVector4<U>& other)
: BasicVector4(static_cast<T>(other.x()), static_cast<T>(other.y()), static_cast<T>(other.z()), static_cast<T>(other.w()))
{}
/// Construct from a BasicVector3 of compatible element type, plus an optional W value
template <typename U, typename W = float>
BasicVector4(const BasicVector3<U>& other, W w_ = 1.0f)
{
_v[0] = static_cast<T>(other.x());
_v[1] = static_cast<T>(other.y());
_v[2] = static_cast<T>(other.z());
_v[3] = static_cast<T>(w_);
}
/// Construct directly from Eigen type
BasicVector4(const Eigen_T& vec): _v(vec)
{}
/// Return the underlying Eigen vector
const Eigen_T& eigen() const { return _v; }
// Return non-constant references to the components
T& x() { return _v[0]; }
T& y() { return _v[1]; }
T& z() { return _v[2]; }
T& w() { return _v[3]; }
// Return constant references to the components
const T& x() const { return _v[0]; }
const T& y() const { return _v[1]; }
const T& z() const { return _v[2]; }
const T& w() const { return _v[3]; }
/// Dot product this BasicVector4 with another vector
T dot(const BasicVector4<T>& other) const {
return x() * other.x()
+ y() * other.y()
+ z() * other.z()
+ w() * other.w();
}
/// Truncate this Vector4 into a Vector3 (no division by W)
BasicVector3<T> getVector3() const {
return BasicVector3<T>(x(), y(), z());
}
/// Project homogeneous BasicVector4 into 3 dimensions by dividing by W
BasicVector3<T> getProjected() const
{
return getVector3() / w();
}
/// Cast to const raw array
operator const T* () const { return _v.data(); }
/// Cast to non-const raw array
operator T* () { return _v.data(); }
};
/// Equality comparison for BasicVector4
template<typename T>
bool operator== (const BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
return (v1.x() == v2.x()
&& v1.y() == v2.y()
&& v1.z() == v2.z()
&& v1.w() == v2.w());
}
/// Inequality comparison for BasicVector4
template<typename T>
bool operator!= (const BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
return !(v1 == v2);
}
/// Componentwise addition of two vectors
template <typename T>
BasicVector4<T> operator+(const BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
return BasicVector4<T>(v1.x() + v2.x(),
v1.y() + v2.y(),
v1.z() + v2.z(),
v1.w() + v2.w());
}
template <typename T>
BasicVector4<T>& operator+=(BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
v1.x() += v2.x();
v1.y() += v2.y();
v1.z() += v2.z();
v1.w() += v2.w();
return v1;
}
/// Componentwise subtraction of two vectors
template<typename T>
BasicVector4<T> operator- (const BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
return BasicVector4<T>(v1.x() - v2.x(),
v1.y() - v2.y(),
v1.z() - v2.z(),
v1.w() - v2.w());
}
template<typename T>
BasicVector4<T>& operator-= (BasicVector4<T>& v1, const BasicVector4<T>& v2)
{
v1.x() -= v2.x();
v1.y() -= v2.y();
v1.z() -= v2.z();
v1.w() -= v2.w();
return v1;
}
/// Multiply BasicVector4 with a scalar
template <
typename T, typename S,
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
BasicVector4<T> operator*(const BasicVector4<T>& v, S s)
{
return BasicVector4<T>(v.x() * s, v.y() * s, v.z() * s, v.w() * s);
}
/// Multiply BasicVector4 with a scalar
template <
typename T, typename S,
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
BasicVector4<T> operator*(S s, const BasicVector4<T>& v)
{
return v * s;
}
/// Multiply BasicVector4 with a scalar and modify in place
template<typename T, typename S>
BasicVector4<T>& operator*= (BasicVector4<T>& v, S s)
{
v.x() *= s;
v.y() *= s;
v.z() *= s;
v.w() *= s;
return v;
}
/// Divide and assign BasicVector4 by a scalar
template<typename T, typename S>
BasicVector4<T>& operator/= (BasicVector4<T>& v, S s)
{
v.x() /= s;
v.y() /= s;
v.z() /= s;
v.w() /= s;
return v;
}
/// Divide a BasicVector4 by a scalar
template<typename T, typename S>
BasicVector4<T> operator/ (const BasicVector4<T>& v, S s)
{
auto result = v;
result /= s;
return result;
}
/// Stream insertion for BasicVector4
template<typename T>
inline std::ostream& operator<<(std::ostream& st, BasicVector4<T> vec)
{
return st << vec.x() << " " << vec.y() << " " << vec.z() << " " << vec.w();
}
/// Stream extraction for BasicVector4
template<typename T>
inline std::istream& operator>>(std::istream& st, BasicVector4<T>& vec)
{
return st >> std::skipws >> vec.x() >> vec.y() >> vec.z() >> vec.w();
}
// A 4-element vector stored in double-precision floating-point.
typedef BasicVector4<double> Vector4;
// A 4-element vector stored in single-precision floating-point.
typedef BasicVector4<float> Vector4f;
namespace math
{
/// Epsilon equality test for BasicVector4
template <typename T>
inline bool isNear(const BasicVector4<T>& v1, const BasicVector4<T>& v2, double epsilon)
{
BasicVector4<T> diff = v1 - v2;
return std::abs(diff.x()) < epsilon && std::abs(diff.y()) < epsilon
&& std::abs(diff.z()) < epsilon && std::abs(diff.w()) < epsilon;
}
/**
* \brief Return a readable (pretty-printed) string representation of a
* BasicVector4.
*
* We need a dedicated function for this because the standard operator<< is
* already used for serialisation to the less readable space-separated text
* format.
*/
template<typename T> std::string pp(const BasicVector4<T>& v)
{
std::stringstream ss;
ss << "(" << v.x() << ", " << v.y() << ", " << v.z() << ", " << v.w() << ")";
return ss.str();
}
}
|