//################################################################################################## // // 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::Vector2 /// \ingroup Core /// /// Templated vector class for a 2 component vector. /// /// Three ready-to-use typedefs are defined:\n /// - cvf::Vec2f (Vector2) /// - cvf::Vec2d (Vector2) /// - cvf::Vec2i (Vector2) /// //================================================================================================== template Vector2 const Vector2::X_AXIS(1, 0); template Vector2 const Vector2::Y_AXIS(0, 1); template Vector2 const Vector2::ZERO(0, 0); //-------------------------------------------------------------------------------------------------- /// Set the vector to //-------------------------------------------------------------------------------------------------- template Vector2::Vector2(S x, S y) { m_v[0] = x; m_v[1] = y; } //-------------------------------------------------------------------------------------------------- /// Set the vector to the same as other //-------------------------------------------------------------------------------------------------- template Vector2::Vector2(const Vector2& other) { *this = other; } //-------------------------------------------------------------------------------------------------- /// An explicit cast constructor to convert from one vector type to another. //-------------------------------------------------------------------------------------------------- template template Vector2::Vector2(const T& other) { m_v[0] = static_cast(other.x()); m_v[1] = static_cast(other.y()); } //-------------------------------------------------------------------------------------------------- /// Assign the vector to the contents of other //-------------------------------------------------------------------------------------------------- template Vector2& Vector2::operator=(const Vector2& other) { m_v[0] = other.m_v[0]; m_v[1] = other.m_v[1]; return *this; } //-------------------------------------------------------------------------------------------------- /// Check if two vectors are equal. An exact match is required. //-------------------------------------------------------------------------------------------------- template bool Vector2::equals(const Vector2& other) const { return (*this == other); } //-------------------------------------------------------------------------------------------------- /// Check if two vectors are equal. An exact match is required. //-------------------------------------------------------------------------------------------------- template inline bool Vector2::operator==(const Vector2& rhs) const { return (m_v[0] == rhs.m_v[0]) && (m_v[1] == rhs.m_v[1]); } //-------------------------------------------------------------------------------------------------- /// Check if two vectors are different. Returns true if not an exact match //-------------------------------------------------------------------------------------------------- template inline bool Vector2::operator!=(const Vector2& rhs) const { return !operator==(rhs); } //-------------------------------------------------------------------------------------------------- /// Returns the sum of this vector and the rhs vector //-------------------------------------------------------------------------------------------------- template inline const Vector2 Vector2::operator+(const Vector2& rhs) const { return Vector2(m_v[0]+rhs.m_v[0], m_v[1]+rhs.m_v[1]); } //-------------------------------------------------------------------------------------------------- /// Adds the vector \a other to this vector //-------------------------------------------------------------------------------------------------- template void Vector2::add(const Vector2& other) { (*this) += other; } //-------------------------------------------------------------------------------------------------- /// Subtracts the vector \a other from this vector //-------------------------------------------------------------------------------------------------- template void cvf::Vector2::subtract(const Vector2& other) { (*this) -= other; } //-------------------------------------------------------------------------------------------------- /// Compute this-rhs and return the result. //-------------------------------------------------------------------------------------------------- template inline const Vector2 Vector2::operator-(const Vector2& rhs) const { return Vector2(m_v[0] - rhs.m_v[0], m_v[1] - rhs.m_v[1]); } //-------------------------------------------------------------------------------------------------- /// Scale this vector by the given scalar //-------------------------------------------------------------------------------------------------- template void Vector2::scale(S scalar) { (*this) *= scalar; } //-------------------------------------------------------------------------------------------------- /// Return this vector scaled by the given scalar //-------------------------------------------------------------------------------------------------- template const Vector2 Vector2::operator*(S scalar) const { return Vector2(m_v[0]*scalar, m_v[1]*scalar); } //-------------------------------------------------------------------------------------------------- /// Return vector scaled by the given scalar //-------------------------------------------------------------------------------------------------- template const Vector2 operator*(T scalar, const Vector2& rhs) { // Note that this is a friend function return Vector2(rhs.m_v[0]*scalar, rhs.m_v[1]*scalar); } //-------------------------------------------------------------------------------------------------- /// Return a vector where each component is the corresponding component in this divided by scalar //-------------------------------------------------------------------------------------------------- template const Vector2 Vector2::operator/(S scalar) const { return Vector2(m_v[0]/scalar, m_v[1]/scalar); } //-------------------------------------------------------------------------------------------------- /// Return a vector which is the negation of this //-------------------------------------------------------------------------------------------------- template const Vector2 Vector2::operator-() const { return Vector2(-m_v[0], -m_v[1]); } //-------------------------------------------------------------------------------------------------- /// Add the given vector to this //-------------------------------------------------------------------------------------------------- template inline Vector2& Vector2::operator+=(const Vector2& v) { m_v[0] += v.x(); m_v[1] += v.y(); return *this; } //-------------------------------------------------------------------------------------------------- /// Subtract the given vector from this //-------------------------------------------------------------------------------------------------- template inline Vector2& Vector2::operator-=(const Vector2& v) { m_v[0] -= v.x(); m_v[1] -= v.y(); return *this; } //-------------------------------------------------------------------------------------------------- /// Scale this with the given scalar. Each component is multiplied with the given value //-------------------------------------------------------------------------------------------------- template inline Vector2& Vector2::operator*=(S scalar) { m_v[0] *= scalar; m_v[1] *= scalar; return *this; } //-------------------------------------------------------------------------------------------------- /// Divide this with the given scalar. Each component is divided by the given scalar //-------------------------------------------------------------------------------------------------- template inline Vector2& Vector2::operator/=(S scalar) { m_v[0] /= scalar; m_v[1] /= scalar; return *this; } //-------------------------------------------------------------------------------------------------- /// Get component 0 or 1. E.g. x = v[0]; //-------------------------------------------------------------------------------------------------- template inline const S& Vector2::operator[](int index) const { CVF_TIGHT_ASSERT(index >= 0); CVF_TIGHT_ASSERT(index < 2); return m_v[index]; } //-------------------------------------------------------------------------------------------------- /// Set component 0 or 1. E.g. v[0] = x; //-------------------------------------------------------------------------------------------------- template inline S& Vector2::operator[](int index) { CVF_TIGHT_ASSERT(index >= 0); CVF_TIGHT_ASSERT(index < 2); return m_v[index]; } //-------------------------------------------------------------------------------------------------- /// Compute the dot product of this and \a other //-------------------------------------------------------------------------------------------------- template S Vector2::dot(const Vector2& 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 /// \endcode //-------------------------------------------------------------------------------------------------- template inline S Vector2::operator*(const Vector2& rhs) const { return m_v[0]*rhs.m_v[0] + m_v[1]*rhs.m_v[1]; } //-------------------------------------------------------------------------------------------------- /// Set the vector from the other vector (of different type). Cast each component to convert it. //-------------------------------------------------------------------------------------------------- template template void Vector2::set(const T& other) { m_v[0] = static_cast(other.x()); m_v[1] = static_cast(other.y()); } //-------------------------------------------------------------------------------------------------- /// Get the length of the vector /// /// Formula: /// \code /// len = sqrt(x*x + y*y) /// \endcode //-------------------------------------------------------------------------------------------------- template inline S Vector2::length() const { return Math::sqrt(m_v[0]*m_v[0] + m_v[1]*m_v[1]); } //-------------------------------------------------------------------------------------------------- /// Get the squared length (L2) of the vector /// /// Formula: /// \code /// len = x*x + y*y /// \endcode //-------------------------------------------------------------------------------------------------- template inline S Vector2::lengthSquared() const { return m_v[0]*m_v[0] + m_v[1]*m_v[1]; } //-------------------------------------------------------------------------------------------------- /// Set the length of the vector to \a newLength. /// /// \sa Vector3::setLength() //-------------------------------------------------------------------------------------------------- template bool Vector2::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; return true; } else { setZero(); return (newLength == 0) ? true : false; } } //-------------------------------------------------------------------------------------------------- /// Normalize the vector (make sure the length is 1.0). /// /// \return Returns true if normalization was possible. Returns false if length is zero or a NaN vector. //-------------------------------------------------------------------------------------------------- template bool Vector2::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; return true; } else { // Might be NaN, so set it to zero m_v[0] = 0; m_v[1] = 0; return false; } } //-------------------------------------------------------------------------------------------------- /// Returns a normalized version of the current vector. The vector is unchanged. //-------------------------------------------------------------------------------------------------- template const Vector2 Vector2::getNormalized(bool* normalizationOK) const { S len = length(); if (len > 0.0) { if (normalizationOK) *normalizationOK = true; S oneOverLen = (static_cast(1.0)/len); return Vector2(m_v[0]*oneOverLen, m_v[1]*oneOverLen); } else { if (normalizationOK) *normalizationOK = false; return Vector2::ZERO; } } //-------------------------------------------------------------------------------------------------- /// Set all components to 0 //-------------------------------------------------------------------------------------------------- template inline void Vector2::setZero() { m_v[0] = 0; m_v[1] = 0; } //-------------------------------------------------------------------------------------------------- /// Check if all components are zero (exact match) //-------------------------------------------------------------------------------------------------- template inline bool Vector2::isZero() const { return (m_v[0] == 0) && (m_v[1] == 0); } //-------------------------------------------------------------------------------------------------- /// Check if vector is undefined /// /// \return Returns true if any one of the components is undefined //-------------------------------------------------------------------------------------------------- template inline bool Vector2::isUndefined() const { if (Math::isUndefined(m_v[0]) || Math::isUndefined(m_v[1])) { return true; } else { return false; } } //-------------------------------------------------------------------------------------------------- /// Set the components of the vector //-------------------------------------------------------------------------------------------------- template inline void Vector2::set(S x, S y) { m_v[0] = x; m_v[1] = y; } } // namespace cvf