2014-04-30 08:50:12 +08:00
|
|
|
/**
|
|
|
|
Copyright 2013 BlackBerry Inc.
|
2015-03-23 21:49:36 +08:00
|
|
|
Copyright (c) 2014-2015 Chukong Technologies
|
|
|
|
|
2014-04-30 08:50:12 +08:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
Original file from GamePlay3D: http://gameplay3d.org
|
|
|
|
|
|
|
|
This file was modified to fit the cocos2d-x project
|
|
|
|
*/
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
#ifndef MATH_VEC4_H
|
|
|
|
#define MATH_VEC4_H
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
#include <xmmintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
#include "math/CCMathBase.h"
|
2014-04-02 12:09:51 +08:00
|
|
|
|
|
|
|
NS_CC_MATH_BEGIN
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
class Mat4;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines 4-element floating point vector.
|
|
|
|
*/
|
2014-07-14 16:40:54 +08:00
|
|
|
class CC_DLL Vec4
|
2014-04-02 10:33:07 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-09-09 23:01:37 +08:00
|
|
|
#ifdef __SSE__
|
2014-09-08 20:58:59 +08:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
float z;
|
|
|
|
float w;
|
|
|
|
};
|
|
|
|
__m128 v;
|
|
|
|
};
|
|
|
|
#else
|
2014-04-02 10:33:07 +08:00
|
|
|
/**
|
|
|
|
* The x-coordinate.
|
|
|
|
*/
|
|
|
|
float x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The y-coordinate.
|
|
|
|
*/
|
|
|
|
float y;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The z-coordinate.
|
|
|
|
*/
|
|
|
|
float z;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The w-coordinate.
|
|
|
|
*/
|
|
|
|
float w;
|
2014-09-08 20:58:59 +08:00
|
|
|
#endif
|
2014-04-02 10:33:07 +08:00
|
|
|
/**
|
|
|
|
* Constructs a new vector initialized to all zeros.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec4();
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new vector initialized to the specified values.
|
|
|
|
*
|
2014-04-16 10:58:34 +08:00
|
|
|
* @param xx The x coordinate.
|
|
|
|
* @param yy The y coordinate.
|
|
|
|
* @param zz The z coordinate.
|
|
|
|
* @param ww The w coordinate.
|
2014-04-02 10:33:07 +08:00
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec4(float xx, float yy, float zz, float ww);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new vector from the values in the specified array.
|
|
|
|
*
|
|
|
|
* @param array An array containing the elements of the vector in the order x, y, z, w.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec4(const float* array);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a vector that describes the direction between the specified points.
|
|
|
|
*
|
|
|
|
* @param p1 The first point.
|
|
|
|
* @param p2 The second point.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec4(const Vec4& p1, const Vec4& p2);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* Creates a new vector that is a copy of the specified vector.
|
|
|
|
*
|
|
|
|
* @param copy The vector to copy.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec4(const Vec4& copy);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new vector from an integer interpreted as an RGBA value.
|
|
|
|
* E.g. 0xff0000ff represents opaque red or the vector (1, 0, 0, 1).
|
|
|
|
*
|
|
|
|
* @param color The integer to interpret as an RGBA value.
|
|
|
|
*
|
|
|
|
* @return A vector corresponding to the interpreted RGBA color.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
static Vec4 fromColor(unsigned int color);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
~Vec4();
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this vector contains all zeros.
|
|
|
|
*
|
|
|
|
* @return true if this vector contains all zeros, false otherwise.
|
|
|
|
*/
|
|
|
|
bool isZero() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this vector contains all ones.
|
|
|
|
*
|
|
|
|
* @return true if this vector contains all ones, false otherwise.
|
|
|
|
*/
|
|
|
|
bool isOne() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the angle (in radians) between the specified vectors.
|
|
|
|
*
|
|
|
|
* @param v1 The first vector.
|
|
|
|
* @param v2 The second vector.
|
|
|
|
*
|
|
|
|
* @return The angle between the two vectors (in radians).
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
static float angle(const Vec4& v1, const Vec4& v2);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the elements of the specified vector to this one.
|
|
|
|
*
|
|
|
|
* @param v The vector to add.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void add(const Vec4& v);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the specified vectors and stores the result in dst.
|
|
|
|
*
|
|
|
|
* @param v1 The first vector.
|
|
|
|
* @param v2 The second vector.
|
|
|
|
* @param dst A vector to store the result in.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
static void add(const Vec4& v1, const Vec4& v2, Vec4* dst);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clamps this vector within the specified range.
|
|
|
|
*
|
|
|
|
* @param min The minimum value.
|
|
|
|
* @param max The maximum value.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void clamp(const Vec4& min, const Vec4& max);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clamps the specified vector within the specified range and returns it in dst.
|
|
|
|
*
|
|
|
|
* @param v The vector to clamp.
|
|
|
|
* @param min The minimum value.
|
|
|
|
* @param max The maximum value.
|
|
|
|
* @param dst A vector to store the result in.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
static void clamp(const Vec4& v, const Vec4& min, const Vec4& max, Vec4* dst);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the distance between this vector and v.
|
|
|
|
*
|
|
|
|
* @param v The other vector.
|
|
|
|
*
|
|
|
|
* @return The distance between this vector and v.
|
|
|
|
*
|
|
|
|
* @see distanceSquared
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
float distance(const Vec4& v) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the squared distance between this vector and v.
|
|
|
|
*
|
|
|
|
* When it is not necessary to get the exact distance between
|
|
|
|
* two vectors (for example, when simply comparing the
|
|
|
|
* distance between different vectors), it is advised to use
|
|
|
|
* this method instead of distance.
|
|
|
|
*
|
|
|
|
* @param v The other vector.
|
|
|
|
*
|
|
|
|
* @return The squared distance between this vector and v.
|
|
|
|
*
|
|
|
|
* @see distance
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
float distanceSquared(const Vec4& v) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the dot product of this vector and the specified vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to compute the dot product with.
|
|
|
|
*
|
|
|
|
* @return The dot product.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
float dot(const Vec4& v) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the dot product between the specified vectors.
|
|
|
|
*
|
|
|
|
* @param v1 The first vector.
|
|
|
|
* @param v2 The second vector.
|
|
|
|
*
|
|
|
|
* @return The dot product between the vectors.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
static float dot(const Vec4& v1, const Vec4& v2);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the length of this vector.
|
|
|
|
*
|
|
|
|
* @return The length of the vector.
|
|
|
|
*
|
|
|
|
* @see lengthSquared
|
|
|
|
*/
|
|
|
|
float length() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the squared length of this vector.
|
|
|
|
*
|
|
|
|
* When it is not necessary to get the exact length of a
|
|
|
|
* vector (for example, when simply comparing the lengths of
|
|
|
|
* different vectors), it is advised to use this method
|
|
|
|
* instead of length.
|
|
|
|
*
|
|
|
|
* @return The squared length of the vector.
|
|
|
|
*
|
|
|
|
* @see length
|
|
|
|
*/
|
|
|
|
float lengthSquared() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Negates this vector.
|
|
|
|
*/
|
|
|
|
void negate();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes this vector.
|
|
|
|
*
|
2014-05-15 01:07:09 +08:00
|
|
|
* This method normalizes this Vec4 so that it is of
|
2014-04-02 10:33:07 +08:00
|
|
|
* unit length (in other words, the length of the vector
|
|
|
|
* after calling this method will be 1.0f). If the vector
|
|
|
|
* already has unit length or if the length of the vector
|
|
|
|
* is zero, this method does nothing.
|
|
|
|
*
|
|
|
|
* @return This vector, after the normalization occurs.
|
|
|
|
*/
|
2014-04-30 19:59:06 +08:00
|
|
|
void normalize();
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-27 11:20:50 +08:00
|
|
|
* Get the normalized vector.
|
2014-04-02 10:33:07 +08:00
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec4 getNormalized() const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scales all elements of this vector by the specified value.
|
|
|
|
*
|
|
|
|
* @param scalar The scalar value.
|
|
|
|
*/
|
|
|
|
void scale(float scalar);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the elements of this vector to the specified values.
|
|
|
|
*
|
2014-04-16 10:58:34 +08:00
|
|
|
* @param xx The new x coordinate.
|
|
|
|
* @param yy The new y coordinate.
|
|
|
|
* @param zz The new z coordinate.
|
|
|
|
* @param ww The new w coordinate.
|
2014-04-02 10:33:07 +08:00
|
|
|
*/
|
2014-04-16 10:58:34 +08:00
|
|
|
void set(float xx, float yy, float zz, float ww);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the elements of this vector from the values in the specified array.
|
|
|
|
*
|
|
|
|
* @param array An array containing the elements of the vector in the order x, y, z, w.
|
|
|
|
*/
|
|
|
|
void set(const float* array);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the elements of this vector to those in the specified vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to copy.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void set(const Vec4& v);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets this vector to the directional vector between the specified points.
|
|
|
|
*
|
|
|
|
* @param p1 The first point.
|
|
|
|
* @param p2 The second point.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void set(const Vec4& p1, const Vec4& p2);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtracts this vector and the specified vector as (this - v)
|
|
|
|
* and stores the result in this vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to subtract.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void subtract(const Vec4& v);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtracts the specified vectors and stores the result in dst.
|
|
|
|
* The resulting vector is computed as (v1 - v2).
|
|
|
|
*
|
|
|
|
* @param v1 The first vector.
|
|
|
|
* @param v2 The second vector.
|
|
|
|
* @param dst The destination vector.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
static void subtract(const Vec4& v1, const Vec4& v2, Vec4* dst);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the sum of this vector with the given vector.
|
|
|
|
*
|
|
|
|
* Note: this does not modify this vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to add.
|
|
|
|
* @return The vector sum.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline const Vec4 operator+(const Vec4& v) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the given vector to this vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to add.
|
|
|
|
* @return This vector, after the addition occurs.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline Vec4& operator+=(const Vec4& v);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the sum of this vector with the given vector.
|
|
|
|
*
|
|
|
|
* Note: this does not modify this vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to add.
|
|
|
|
* @return The vector sum.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline const Vec4 operator-(const Vec4& v) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtracts the given vector from this vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to subtract.
|
|
|
|
* @return This vector, after the subtraction occurs.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline Vec4& operator-=(const Vec4& v);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the negation of this vector.
|
|
|
|
*
|
|
|
|
* Note: this does not modify this vector.
|
|
|
|
*
|
|
|
|
* @return The negation of this vector.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline const Vec4 operator-() const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the scalar product of this vector with the given value.
|
|
|
|
*
|
|
|
|
* Note: this does not modify this vector.
|
|
|
|
*
|
2014-04-16 10:27:17 +08:00
|
|
|
* @param s The value to scale by.
|
2014-04-02 10:33:07 +08:00
|
|
|
* @return The scaled vector.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline const Vec4 operator*(float s) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scales this vector by the given value.
|
|
|
|
*
|
2014-04-16 10:27:17 +08:00
|
|
|
* @param s The value to scale by.
|
2014-04-02 10:33:07 +08:00
|
|
|
* @return This vector, after the scale occurs.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline Vec4& operator*=(float s);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the components of this vector divided by the given constant
|
|
|
|
*
|
|
|
|
* Note: this does not modify this vector.
|
|
|
|
*
|
2014-04-16 10:27:17 +08:00
|
|
|
* @param s the constant to divide this vector with
|
2014-04-02 10:33:07 +08:00
|
|
|
* @return a smaller vector
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline const Vec4 operator/(float s) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if this vector is less than the given vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to compare against.
|
|
|
|
*
|
|
|
|
* @return True if this vector is less than the given vector, false otherwise.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline bool operator<(const Vec4& v) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if this vector is equal to the given vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to compare against.
|
|
|
|
*
|
|
|
|
* @return True if this vector is equal to the given vector, false otherwise.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline bool operator==(const Vec4& v) const;
|
2014-04-02 10:33:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if this vector is not equal to the given vector.
|
|
|
|
*
|
|
|
|
* @param v The vector to compare against.
|
|
|
|
*
|
|
|
|
* @return True if this vector is not equal to the given vector, false otherwise.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline bool operator!=(const Vec4& v) const;
|
2014-05-06 09:07:58 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
/** equals to Vec4(0,0,0,0) */
|
|
|
|
static const Vec4 ZERO;
|
|
|
|
/** equals to Vec4(1,1,1,1) */
|
|
|
|
static const Vec4 ONE;
|
|
|
|
/** equals to Vec4(1,0,0,0) */
|
|
|
|
static const Vec4 UNIT_X;
|
|
|
|
/** equals to Vec4(0,1,0,0) */
|
|
|
|
static const Vec4 UNIT_Y;
|
|
|
|
/** equals to Vec4(0,0,1,0) */
|
|
|
|
static const Vec4 UNIT_Z;
|
|
|
|
/** equals to Vec4(0,0,0,1) */
|
|
|
|
static const Vec4 UNIT_W;
|
2014-04-02 10:33:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the scalar product of the given vector with the given value.
|
|
|
|
*
|
|
|
|
* @param x The value to scale by.
|
|
|
|
* @param v The vector to scale.
|
|
|
|
* @return The scaled vector.
|
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
inline const Vec4 operator*(float x, const Vec4& v);
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-04-02 12:09:51 +08:00
|
|
|
NS_CC_MATH_END
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "math/Vec4.inl"
|
2014-04-02 10:33:07 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
#endif // MATH_VEC4_H
|