⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stlvec.h

📁 矩阵、向量以及四元数的封装类
💻 H
📖 第 1 页 / 共 3 页
字号:
	 * @remarks The vector is not initialized in this case.
	 */
	Vector() {}

	/**
	 * Construct a vector with given values.
	 * @param x The x value want to set.
	 * @param y The y value want to set.
	 */
	Vector(double x, double y) :
		x(x), y(y)
	{}

	/**
	 * Construct a vector with given values array.
	 * @param v Pointer of the values array used to set the vector.
	 */
	Vector(const double *v) :
		x(v[0]), y(v[1])
	{}

	/**
	 * Construct a vector with given type compatible values array.
	 * @param v Pointer of the values array used to set the vector.
	 */
	template <typename T>
	Vector(const T *v) :
		x(v[0]), y(v[1])
	{}

	/**
	 * Copy constructor. Construct a vector with another one.
	 * @param v A vector used to copy size_to new vector.
	 */
	Vector(const Vector &v) :
		x(v.x), y(v.y)
	{}
	/** @} */

	/** \name Assignment
	 * @{ */
	/**
	 * Assignment operator, assign another vector's values size_to this one.
	 * @param v Another vector used to set this one.
	 * @return Reference of this vector.
	 */
	inline Vector &operator =(const Vector &v)
	{
		x = v.x; y = v.y;
		return *this;
	}

	/**
	 * Assignment operator, set the vector's values using a values array.
	 * @param v Posize_ter of the values array used to set this vector.
	 * @return Reference of this vector.
	 */
	inline Vector &operator =(const double *v)
	{
		x = v[0]; y = v[1];
		return *this;
	}

	/**
	 * Assignment operator, set this vector's values using a type compatible values array.
	 * @param v Posize_ter of the type compatible values array used to set this vector.
	 * @return Reference of this vector.
	 */
	template <typename T>
	inline Vector &operator =(const T *v)
	{
		x = v[0]; y = v[1];
		return *this;
	}

	/**
	 * Assignment operator, set all values of this vector equal to the given value.
	 * @param v Value use to set the vector.
	 * @return Reference of this vector.
	 */
	inline Vector &operator =(double v)
	{
		x = y = v;
		return *this;
	}

	/**
	 * Set the values of this vector with another one.
	 * @param v Reference of vector used to set this one.
	 * @return Reference of this vector.
	 */
	inline Vector &set(const Vector &v)
	{
		x = v.x; y = v.y;
		return *this;
	}

	/**
	 * Set the values of this vector with a values array.
	 * @param v Pointer of the values array used to set the vector.
	 * @return Reference of this vector.
	 */
	inline Vector &set(const double *v)
	{
		x = v[0]; y = v[1];
		return *this;
	}

	/**
	 * Set the values of this vector with a type compatible values array.
	 * @param v Pointer of the type compatible values array used to set the vector.
	 * @return Reference of this vector.
	 */
	template <typename T>
	inline Vector &set(const T *v)
	{
		x = v[0]; y = v[1];
		return *this;
	}

	/**
	 * Set all the components of this vector equal to the given value.
	 * @param v Value used to set the vector.
	 * @return Reference of this vector.
	 */
	inline Vector &set(double v)
	{
		x = y = v;
		return *this;
	}
	/** @} */

	/** \name Components visit
	 * @{ */
	/**
	 * Visit the n'th component of this vector.
	 * @param n Zero based index of the component wanted.
	 * @return Reference of the queried component.
	 */
	inline double &operator [](size_t n)
	{
		return vec[n];
	}

	/**
	 * Query the n'th component's value of this vector.
	 * @param n Zero based index of the component wanted.
	 * @return Value of queried component.
	 */
	inline double operator [](size_t n) const
	{
		return vec[n];
	}

	/**
	 * Visit the n'th component of this vector.
	 * @param n Zero based index of the component wanted.
	 * @return Reference of the queried component.
	 */
	inline double &get(size_t n)
	{
		return vec[n];
	}

	/**
	 * Query the n'th component's value of this vector.
	 * @param n Zero based index of the component wanted.
	 * @return Value of queried component.
	 */
	inline double get(size_t n) const
	{
		return vec[n];
	}

	/**
	 * Query the vector as a pointer of the components.
	 * @return The address of the first component in the vector.
	 */
	inline const double * const get() const
	{
		return vec;
	}
	/** @} */

	/** \name Vector operation
	 * @{ */
	/**
	 * Query the norm (magnitude) of this vector.
	 * @return Norm (magnitude) of this vector.
	 */
	inline double getNorm() const
	{
		return sqrt(x * x + y * y);
	}

	/**
	 * Query the squared norm (magnitude) of this vector.
	 * @return Squared norm (magnitude) of this vector.
	 */
	inline double getSquaredNorm() const
	{
		return x * x + y * y;
	}

	/**
	 * Return the unit vector in the direction of this vector.
	 * @return The unit vector in the direction of this vector.
	 * @remarks Attempting to normalize a zero-vector will result in a divided by
   * zero error. This is as it should be... fix the calling code.
	 */
	inline Vector getUnit() const
	{
		return (*this) / getNorm();
	}

	/**
	 * Normalize this vector.
	 * @return Reference of this vector.
	 */
	inline Vector &normalize()
	{
		double len = x * x + y * y;
		if (len < 1e-6)
			return *this;
		len = 1.0 / len;
		x *= len; y *= len;
		return *this;
	}

  /**
   * Rotate the vector around the origin by a given angle.
   * @param angle The angle will rotate in radians.
   * @return Reference of this vector.
   */
  Vector &rotate(double angle)
  {
    double s = sin(angle);
    double c = cos(angle);
    double nx = x * c + y * s;
    y = -x * s + y * c;
    x = nx;
    return *this;
  }
	/** @} */

	/** \name operators
	 * @{ */
	/**
	 * Unary + operator
	 */
	inline Vector operator +() const
	{
		return *this;
	}

	/**
	 * Unary - operator
	 */
	inline Vector operator -() const
	{
		return Vector(-x, -y);
	}

	/**
	 * Add another vector onto this one.
	 * @param v Reference of the vector will be added onto this one.
	 * @return Reference of this vector.
	 */
	inline Vector &operator +=(const Vector &v)
	{
		x += v.x; y += v.y;
		return *this;
	}

	/**
	 * Subtract another vector from this one.
	 * @param v Reference of the vector will be subtracted from this one.
	 * @return Reference of this vector.
	 */
	inline Vector &operator -=(const Vector &v)
	{
		x -= v.x; y -= v.y;
		return *this;
	}

	/**
	 * Multiply this vector with a scalar.
	 * @param s The scalar.
	 * @return Reference of this vector.
	 */
	inline Vector &operator *=(double s)
	{
		x *= s; y *= s;
		return *this;
	}

	/**
	 * Divide this vector by a scalar.
	 * @param s The scalar.
	 * @return Reference of this vector.
	 */
	inline Vector &operator /=(double s)
	{
		s = 1.0 / s;
		x *= s; y *= s;
		return *this;
	}

	/**
	 * Add two vectors.
	 * @param v1 Reference of the first vector.
	 * @param v2 Reference of the second vector.
	 * @return Vector hold on the result.
	 */
	friend inline Vector operator +(const Vector &v1, const Vector &v2)
	{
		return Vector(v1.x + v2.x, v1.y + v2.y);
	}

	/**
	 * Subtract two vectors.
	 * @param v1 Reference of the first vector.
	 * @param v2 Reference of the second vector.
	 * @return Vector hold on the result.
	 */
	friend inline Vector operator -(const Vector &v1, const Vector &v2)
	{
		return Vector(v1.x - v2.x, v1.y - v2.y);
	}

	/**
	 * Dot multiply two vectors.
	 * @param v1 Reference of the first vector.
	 * @param v2 Reference of the second vector.
	 * @return Dot product of the two vectors.
	 */
	friend inline double operator *(const Vector &v1, const Vector &v2)
	{
		return v1.x * v2.x + v1.y * v2.y;
	}

	/**
	 * Multiply a vector with a scalar.
	 * @param v1 Reference of the vector.
	 * @param s Reference of the scalar.
	 * @return Vector hold on the result.
	 */
	friend inline Vector operator *(const Vector &v1, double s)
	{
		return Vector(v1.x * s, v1.y * s);
	}

	/**
	 * Multiply a vector with a scalar.
	 * @param s Reference of the scalar.
	 * @param v1 Reference of the vector.
	 * @return Vector hold on the result.
	 */
	friend inline Vector operator *(double s, const Vector &v1)
	{
		return Vector(s * v1.x, s * v1.y);
	}

	/**
	 * Divide a vector by a scalar.
	 * @param v1 Reference of the vector.
	 * @param s Reference of the scalar.
	 * @return Vector hold on the result.
	 */
	friend inline Vector operator /(const Vector &v1, double s)
	{
		s = 1.0 / s;
		return Vector(v1.x * s, v1.y * s);
	}

	/**
	 * Check if the two vectors are equal.
	 * @param v1 Reference of the first vector.
	 * @param v2 Reference of the second vector.
	 * @return True if the two vector are equal, else return false.
	 */
	friend inline bool operator ==(const Vector &v1, const Vector &v2)
	{
		return (v1.x == v2.x) && (v1.y == v2.y);
	}

	/**
	 * Check if the two vectors are not equal.
	 * @param v1 Reference of the first vector.
	 * @param v2 Reference of the second vector.
	 * @return True if the two vectors are not equal, else return false.
	 */
	friend inline bool operator !=(const Vector &v1, const Vector &v2)
	{
		return (v1.x != v2.x) || (v1.y != v2.y);
	}
	/** @} */
};

typedef Vector<2>		Vector2;

template <size_t R, size_t C>
struct Matrix;
struct Quaternion;

/** \class Vector
 * A specified template class for 3D vector.
 */
template <>
struct Vector<3>
{
public:
	/// Components
	union
	{
		struct
		{
			/// The x component
			double				x;
			/// The y component
			double				y;
			/// The z component
			double				z;
		};
		/// Components array
		double					vec[3];
	};

	/** \name Construct
	 * @{ */
	/**
	 * Default constructure.
	 * @remarks The vector is not initialized in this case.
	 */
	Vector() {}

	/**
	 * Construct a vector with given values.
	 * @param x The x value want to set.
	 * @param y The y value want to set.
	 * @param z The z value want to set.
	 */
	Vector(double x, double y, double z) :
		x(x), y(y), z(z)
	{}

	/**
	 * Construct a vector with given values array.
	 * @param v Pointer of the values array used to set the vector.
	 */
	Vector(const double *v) :
		x(v[0]), y(v[1]), z(v[2])
	{}

	/**
	 * Construct a vector with given type compatible values array.
	 * @param v Pointer of the values array used to set the vector.
	 */
	template <typename T>
	Vector(const T *v) :
		x(v[0]), y(v[1]), z(v[2])
	{}

	/**
	 * Copy constructure. Construct a vector with another one.
	 * @param v A vector used to copy size_to new vector.
	 */
	Vector(const Vector &v) :
		x(v.x), y(v.y), z(v.z)
	{}
	/** @} */

	/** \name Assignment
	 * @{ */
	/**
	 * Assignment operator, assign another vector's values size_to this one.
	 * @param v Another vector used to set this one.
	 * @return Reference of this vector.
	 */
	inline Vector &operator =(const Vector &v)
	{
		x = v.x; y = v.y; z = v.z;
		return *this;
	}

	/**
	 * Assignment operator, set the vector's values using a values array.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -