📄 stlvec.h
字号:
* @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]; z = v[2];
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]; z = v[2];
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 = z = 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; z = v.z;
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]; z = v[2];
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]; z = v[2];
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 = z = v;
return *this;
}
/**
* Set this vector with given values.
*/
Vector &set(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
return *this;
}
/**
* Set the vector as two vectors cross product.
* @param v1 Reference of the first vector.
* @param v2 Reference of the second vector.
* @return Reference of this vector.
*/
Vector &setAsCross(const Vector &v1, const Vector &v2)
{
x = v1.y * v2.z - v1.z * v2.y;
y = v1.z * v2.x - v1.x * v2.z;
z = v1.x * v2.y - v1.y * v2.x;
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 + z * z);
}
/**
* Query the squared norm (magnitude) of this vector.
* @return Squared norm (magnitude) of this vector.
*/
inline double getSquaredNorm() const
{
return x * x + y * y + z * z;
}
/**
* Return the unit vector in the direction of this vector.
* @return The unit vector in the direction of this vector.
* @remarks Attempting to nomalize 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 + z * z;
if (len < 1e-6)
return *this;
len = 1.0 / len;
x *= len; y *= len; z *= len;
return *this;
}
/** @} */
/** \name operators
* @{ */
/**
* Unary + operator
*/
inline Vector operator +() const
{
return *this;
}
/**
* Unary - operator
*/
inline Vector operator -() const
{
return Vector(-x, -y, -z);
}
/**
* 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; z += v.z;
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; z -= v.z;
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; z *= 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; z *= 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, v1.z + v2.z);
}
/**
* 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, v1.z - v2.z);
}
/**
* 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 + v1.z * v2.z;
}
/**
* 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, v1.z * 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, s * v1.z);
}
/**
* 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, v1.z * s);
}
/**
* Take cross product of the two vectors.
*/
friend inline Vector operator %(const Vector &v1, const Vector &v2)
{
return Vector(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
}
/**
* Project one vector onto another.
*/
friend inline Vector operator >>(const Vector &v1, const Vector &v2)
{
return v2 * (v1 * v2) / (v2 * v2);
}
/**
* Project one vector onto another.
*/
friend inline Vector operator <<(const Vector &v1, const Vector &v2)
{
return v1 * (v1 * v2) / (v1 * v1);
}
/**
* 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) && (v1.z == v2.z);
}
/**
* 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) || (v1.z != v2.z);
}
/** @} */
/** \name Rotation
* @{ */
/**
* Rotate the vector around x axis with given angle in radians.
*/
void rotateX(double angle)
{
double c = cos(angle);
double s = sin(angle);
double old_y = y;
y = c * old_y + s * z;
z = -s * old_y + c * z;
}
/**
* Query the rotated vector of this one around x axis with given angle in
* radians.
*/
Vector getRotateX(double angle) const
{
double c = cos(angle);
double s = sin(angle);
return Vector(x, c * y + s * z, -s * y + c * z);
}
/**
* Rotate the vector around y axis with given angle in radians.
*/
void rotateY(double angle)
{
double c = cos(angle);
double s = sin(angle);
double old_x = x;
x = c * old_x - s * z;
z = s * old_x + c * z;
}
/**
* Query the rotated vector of this one around y axis with given angle in
* radians.
*/
Vector getRotateY(double angle) const
{
double c = cos(angle);
double s = sin(angle);
return Vector(c * x - s * z, y, s * x + c * z);
}
/**
* Rotate the vector around z axis with given angle in radians.
*/
void rotateZ(double angle)
{
double c = cos(angle);
double s = sin(angle);
double old_x = x;
x = c * old_x + s * y;
y = -s * old_x + c * y;
}
/**
* Quarey the rotated vector of this one around z axis with given angle in
* radians.
*/
Vector getRotateZ(double angle) const
{
double c = cos(angle);
double s = sin(angle);
return Vector(c * x + s * y, -s * x + c * y, z);
}
/**
* Rotate the vector by given rotation matrix.
*/
Vector &rotate(const Matrix<3, 3> &m);
/**
* Query the rotated vector of this one by the given matrix.
*/
Vector getRotate(const Matrix<3, 3> &m) const;
/**
* Rotate the vector by given quaternion.
*/
Vector &rotate(const Quaternion &q);
/**
* Query the rotated vector of this one by the given quaternion.
*/
Vector getRotate(const Quaternion &q) const;
/** @} */
};
typedef Vector<3> Vector3;
}}
#endif /* __STLMATH_VEC_H__ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -