//################################################################################################## // // Custom Visualization Core library // Copyright (C) 2011-2013 Ceetron AS // // This library may be used under the terms of either the GNU General Public License or // the GNU Lesser General Public License as follows: // // GNU General Public License Usage // This library 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 3 of the License, or // (at your option) any later version. // // This library 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 at <> // for more details. // // GNU Lesser General Public License Usage // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation; either version 2.1 of the License, or // (at your option) any later version. // // This library 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 Lesser General Public License at <> // for more details. // //################################################################################################## namespace cvf { //================================================================================================== /// /// \class cvf::Vector4 /// \ingroup Core /// /// Templated vector class for a 4 component vector. /// /// Three ready-to-use typedefs are defined:\n /// - cvf::Vec4f (Vector4) /// - cvf::Vec4d (Vector4) /// - cvf::Vec4i (Vector4) /// //================================================================================================== template Vector4 const Vector4::ZERO(0,0,0,0); //-------------------------------------------------------------------------------------------------- /// Set the vector to //-------------------------------------------------------------------------------------------------- template Vector4::Vector4(S x, S y, S z, S w) { m_v[0] = x; m_v[1] = y; m_v[2] = z; m_v[3] = w; } //-------------------------------------------------------------------------------------------------- /// Set the vector to the same as other //-------------------------------------------------------------------------------------------------- template Vector4::Vector4(const Vector4& other) { *this = other; } //-------------------------------------------------------------------------------------------------- /// An explicit cast constructor to convert from one vector type to another. //-------------------------------------------------------------------------------------------------- template template Vector4::Vector4(const T& other) { m_v[0] = static_cast(other.x()); m_v[1] = static_cast(other.y()); m_v[2] = static_cast(other.z()); m_v[3] = static_cast(other.w()); } //-------------------------------------------------------------------------------------------------- /// Assign the vector to the contents of other //-------------------------------------------------------------------------------------------------- template Vector4& Vector4::operator=(const Vector4& other) { m_v[0] = other.m_v[0]; m_v[1] = other.m_v[1]; m_v[2] = other.m_v[2]; m_v[3] = other.m_v[3]; return *this; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- template Vector4::Vector4(const Vector3& other, S w) { m_v[0] = other.x(); m_v[1] = other.y(); m_v[2] = other.z(); m_v[3] = w; } //-------------------------------------------------------------------------------------------------- /// Check if two vectors are equal. An exact match is required. //-------------------------------------------------------------------------------------------------- template bool Vector4::equals(const Vector4& other) const { return (*this == other); } //-------------------------------------------------------------------------------------------------- /// Check if two vectors are equal. An exact match is required. //-------------------------------------------------------------------------------------------------- template inline bool Vector4::operator==(const Vector4& rhs) const { return (m_v[0] == rhs.m_v[0]) && (m_v[1] == rhs.m_v[1]) && (m_v[2] == rhs.m_v[2]) && (m_v[3] == rhs.m_v[3]); } //-------------------------------------------------------------------------------------------------- /// Check if two vectors are different. Returns true if not an exact match //-------------------------------------------------------------------------------------------------- template inline bool Vector4::operator!=(const Vector4& rhs) const { return !operator==(rhs); } //-------------------------------------------------------------------------------------------------- /// Adds the vector \a other to this vector //-------------------------------------------------------------------------------------------------- template void cvf::Vector4::add(const Vector4& other) { (*this) += other; } //-------------------------------------------------------------------------------------------------- /// Subtracts the vector \a other from this vector //-------------------------------------------------------------------------------------------------- template void cvf::Vector4::subtract(const Vector4& other) { (*this) -= other; } //-------------------------------------------------------------------------------------------------- /// Returns the sum of this vector and the rhs vector //-------------------------------------------------------------------------------------------------- template inline const Vector4 Vector4::operator+(const Vector4& rhs) const { return Vector4(m_v[0] + rhs.m_v[0], m_v[1] + rhs.m_v[1], m_v[2] + rhs.m_v[2], m_v[3] + rhs.m_v[3]); } //-------------------------------------------------------------------------------------------------- /// Compute this-rhs and return the result. //-------------------------------------------------------------------------------------------------- template inline const Vector4 Vector4::operator-(const Vector4& rhs) const { return Vector4(m_v[0] - rhs.m_v[0], m_v[1] - rhs.m_v[1], m_v[2] - rhs.m_v[2], m_v[3] - rhs.m_v[3]); } //-------------------------------------------------------------------------------------------------- /// Scale this vector by the given scalar //-------------------------------------------------------------------------------------------------- template void Vector4::scale(S scalar) { (*this) *= scalar; } //-------------------------------------------------------------------------------------------------- /// Return this vector scaled by the given scalar //-------------------------------------------------------------------------------------------------- template const Vector4 Vector4::operator*(S scalar) const { return Vector4(m_v[0]*scalar, m_v[1]*scalar, m_v[2]*scalar, m_v[3]*scalar); } //-------------------------------------------------------------------------------------------------- /// Return a vector where each component is the corresponding component in this divided by scalar //-------------------------------------------------------------------------------------------------- template const Vector4 Vector4::operator/(S scalar) const { return Vector4(m_v[0]/scalar, m_v[1]/scalar, m_v[2]/scalar, m_v[3]/scalar); } //-------------------------------------------------------------------------------------------------- /// Return a vector which is the negation of this //-------------------------------------------------------------------------------------------------- template const Vector4 Vector4::operator-() const { return Vector4(-m_v[0], -m_v[1], -m_v[2], -m_v[3]); } //-------------------------------------------------------------------------------------------------- /// Add the given vector to this //-------------------------------------------------------------------------------------------------- template inline Vector4& Vector4::operator+=(const Vector4& v) { m_v[0] += v.x(); m_v[1] += v.y(); m_v[2] += v.z(); m_v[3] += v.w(); return *this; } //-------------------------------------------------------------------------------------------------- /// Subtract the given vector from this //-------------------------------------------------------------------------------------------------- template inline Vector4& Vector4::operator-=(const Vector4& v) { m_v[0] -= v.x(); m_v[1] -= v.y(); m_v[2] -= v.z(); m_v[3] -= v.w(); return *this; } //-------------------------------------------------------------------------------------------------- /// Scale this with the given scalar. Each component is multiplied with the given value //-------------------------------------------------------------------------------------------------- template inline Vector4& Vector4::operator*=(S scalar) { m_v[0] *= scalar; m_v[1] *= scalar; m_v[2] *= scalar; m_v[3] *= scalar; return *this; } //-------------------------------------------------------------------------------------------------- /// Divide this with the given scalar. Each component is divided by the given scalar //-------------------------------------------------------------------------------------------------- template inline Vector4& Vector4::operator/=(S scalar) { m_v[0] /= scalar; m_v[1] /= scalar; m_v[2] /= scalar; m_v[3] /= scalar; return *this; } //-------------------------------------------------------------------------------------------------- /// Get component 0,1,2,3. E.g. x = v[0]; //-------------------------------------------------------------------------------------------------- template inline const S& Vector4::operator[](int index) const { CVF_TIGHT_ASSERT(index >= 0); CVF_TIGHT_ASSERT(index < 4); return m_v[index]; } //-------------------------------------------------------------------------------------------------- /// Set component 0,1,2,3. E.g. v[0] = x; //-------------------------------------------------------------------------------------------------- template inline S& Vector4::operator[](int index) { CVF_TIGHT_ASSERT(index >= 0); CVF_TIGHT_ASSERT(index < 4); return m_v[index]; } //-------------------------------------------------------------------------------------------------- /// Compute the dot product of this and \a other //-------------------------------------------------------------------------------------------------- template S Vector4::dot(const Vector4& other) const { return (*this)*other; } //-------------------------------------------------------------------------------------------------- /// Compute the dot product of this and rhs and return the result (scalar) /// /// Formula: /// \code /// S = tx*rx + ty*ry + tz*rz + tw*rw /// \endcode //-------------------------------------------------------------------------------------------------- template inline S Vector4::operator*(const Vector4& rhs) const { return m_v[0]*rhs.m_v[0] + m_v[1]*rhs.m_v[1] + m_v[2]*rhs.m_v[2] + m_v[3]*rhs.m_v[3]; } //-------------------------------------------------------------------------------------------------- /// Set the vector from the other vector (of different type). Cast each component to convert it. //-------------------------------------------------------------------------------------------------- template template void Vector4::set(const T& other) { m_v[0] = static_cast(other.x()); m_v[1] = static_cast(other.y()); m_v[2] = static_cast(other.z()); m_v[3] = static_cast(other.w()); } //-------------------------------------------------------------------------------------------------- /// Get the length of the vector /// /// Formula: /// \code /// len = sqrt(x*x + y*y + z*z) /// \endcode //-------------------------------------------------------------------------------------------------- template inline S Vector4::length() const { return Math::sqrt(m_v[0]*m_v[0] + m_v[1]*m_v[1] + m_v[2]*m_v[2] + m_v[3]*m_v[3]); } //-------------------------------------------------------------------------------------------------- /// Get the squared length (L2) of the vector /// /// Formula: /// \code /// len = x*x + y*y + z*z /// \endcode //-------------------------------------------------------------------------------------------------- template inline S Vector4::lengthSquared() const { return m_v[0]*m_v[0] + m_v[1]*m_v[1] + m_v[2]*m_v[2] + m_v[3]*m_v[3]; } //-------------------------------------------------------------------------------------------------- /// Set the length of the vector to \a newLength. /// /// \sa Vector3::setLength() //-------------------------------------------------------------------------------------------------- template bool Vector4::setLength(S newLength) { CVF_ASSERT(newLength >= 0); S currLen = length(); if (currLen > std::numeric_limits::epsilon() && newLength > 0) { S scale = newLength/currLen; m_v[0] *= scale; m_v[1] *= scale; m_v[2] *= scale; m_v[3] *= scale; return true; } else { setZero(); return (newLength == 0) ? true : false; } } //-------------------------------------------------------------------------------------------------- /// Normalize the vector (make sure the length is 1.0). /// Returns true if normalization was possible. Returns false if length is zero or a NaN vector. //-------------------------------------------------------------------------------------------------- template bool Vector4::normalize() { S len = length(); if (len > 0.0) { // Precompute 1/length and do multiplication instead of division S oneOverLen = (static_cast(1.0)/len); m_v[0] *= oneOverLen; m_v[1] *= oneOverLen; m_v[2] *= oneOverLen; m_v[3] *= oneOverLen; return true; } else { // Might be NaN, so set it to zero m_v[0] = 0.0f; m_v[1] = 0.0f; m_v[2] = 0.0f; m_v[3] = 0.0f; return false; } } //-------------------------------------------------------------------------------------------------- /// Returns a normalized version of the current vector. The vector is unchanged. //-------------------------------------------------------------------------------------------------- template const Vector4 Vector4::getNormalized(bool* normalizationOK) const { S len = length(); if (len > 0.0) { if (normalizationOK) *normalizationOK = true; S oneOverLen = (static_cast(1.0)/len); return Vector4(m_v[0]*oneOverLen, m_v[1]*oneOverLen, m_v[2]*oneOverLen, m_v[3]*oneOverLen); } else { if (normalizationOK) *normalizationOK = false; return Vector4::ZERO; } } //-------------------------------------------------------------------------------------------------- /// Set all components to 0 //-------------------------------------------------------------------------------------------------- template inline void Vector4::setZero() { m_v[0] = 0; m_v[1] = 0; m_v[2] = 0; m_v[3] = 0; } //-------------------------------------------------------------------------------------------------- /// Check if all components are zero (exact match) //-------------------------------------------------------------------------------------------------- template inline bool Vector4::isZero() const { return (m_v[0] == 0) && (m_v[1] == 0) && (m_v[2] == 0) && (m_v[2] == 0); } //-------------------------------------------------------------------------------------------------- /// Set the components of the vector //-------------------------------------------------------------------------------------------------- template inline void Vector4::set(S x, S y, S z, S w) { m_v[0] = x; m_v[1] = y; m_v[2] = z; m_v[3] = w; } } // namespace cvf