| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /*
- Copyright (c) 2013 Daniele Bartolini, Michele Rossi
- Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
- Permission is hereby granted, free of charge, to any person
- obtaining a copy of this software and associated documentation
- files (the "Software"), to deal in the Software without
- restriction, including without limitation the rights to use,
- copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following
- conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- */
- #pragma once
- #include "assert.h"
- #include "types.h"
- #include "math_utils.h"
- #include "vector3.h"
- namespace crown
- {
- /// Negates @a a and returns the result.
- Vector4 operator-(const Vector4& a);
- /// Adds the vector @a a to @a b and returns the result.
- Vector4 operator+(Vector4 a, const Vector4& b);
- /// Subtracts the vector @a b from @a a and returns the result.
- Vector4 operator-(Vector4 a, const Vector4& b);
- /// Multiplies the vector @a a by the scalar @a k and returns the result.
- Vector4 operator*(Vector4 a, float k);
- /// @copydoc operator*(Vector4, float)
- Vector4 operator*(float k, Vector4 a);
- /// Divides the vector @a a by the scalar @a k and returns the result.
- Vector4 operator/(Vector4 a, float k);
- /// Returns true whether the vectors @a a and @a b are equal.
- bool operator==(const Vector4& a, const Vector4& b);
- /// Functions to manipulate Vector4.
- ///
- /// @ingroup Math
- namespace vector4
- {
- const Vector4 ZERO = Vector4(0, 0, 0, 0);
- const Vector4 XAXIS = Vector4(1, 0, 0, 0);
- const Vector4 YAXIS = Vector4(0, 1, 0, 0);
- const Vector4 ZAXIS = Vector4(0, 0, 1, 0);
- const Vector4 WAXIS = Vector4(0, 0, 0, 1);
- /// Returns the dot product between the vectors @a a and @a b.
- float dot(const Vector4& a, const Vector4& b);
- /// Returns the lenght of @a a.
- float length(const Vector4& a);
- /// Returns the squared length of @a a.
- float squared_length(const Vector4& a);
- /// Sets the lenght of @a a to @a len.
- void set_length(Vector4& a, float len);
- /// Normalizes @a a and returns the result.
- Vector4 normalize(Vector4& a);
- /// Returns the distance between the points @a a and @a b.
- float distance(const Vector4& a, const Vector4& b);
- /// Returns the angle between the vectors @a a and @a b.
- float angle(const Vector4& a, const Vector4& b);
- /// Returns the pointer to the data of @a a.
- float* to_float_ptr(Vector4& a);
- /// @copydoc to_float_ptr(Vector4&)
- const float* to_float_ptr(const Vector4& a);
- /// Returns the Vector3 portion of @a a. (i.e. truncates w)
- Vector3 to_vector3(const Vector4& a);
- } // namespace vector4
- inline Vector4 operator-(const Vector4& a)
- {
- return Vector4(-a.x, -a.y, -a.z, -a.w);
- }
- inline Vector4 operator+(Vector4 a, const Vector4& b)
- {
- a += b;
- return a;
- }
- inline Vector4 operator-(Vector4 a, const Vector4& b)
- {
- a -= b;
- return a;
- }
- inline Vector4 operator*(Vector4 a, float k)
- {
- a *= k;
- return a;
- }
- inline Vector4 operator*(float k, Vector4 a)
- {
- a *= k;
- return a;
- }
- inline Vector4 operator/(Vector4 a, float k)
- {
- a /= k;
- return a;
- }
- inline bool operator==(const Vector4& a, const Vector4& b)
- {
- return math::equals(a.x, b.x) && math::equals(a.y, b.y) && math::equals(a.z, b.z) && math::equals(a.w, b.w);
- }
- namespace vector4
- {
- inline float dot(const Vector4& a, const Vector4& b)
- {
- return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
- }
- inline float length(const Vector4& a)
- {
- return math::sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w);
- }
- inline float squared_length(const Vector4& a)
- {
- return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
- }
- inline void set_length(Vector4& a, float len)
- {
- normalize(a);
- a.x *= len;
- a.y *= len;
- a.z *= len;
- a.w *= len;
- }
- inline Vector4 normalize(Vector4& a)
- {
- float inv_len = 1.0f / length(a);
- a.x *= inv_len;
- a.y *= inv_len;
- a.z *= inv_len;
- a.w *= inv_len;
- return a;
- }
- inline float distance(const Vector4& a, const Vector4& b)
- {
- return length(b - a);
- }
- inline float angle(const Vector4& a, const Vector4& b)
- {
- return math::acos(dot(a, b) / (length(a) * length(b)));
- }
- inline float* to_float_ptr(Vector4& a)
- {
- return &a.x;
- }
- inline const float* to_float_ptr(const Vector4& a)
- {
- return &a.x;
- }
- inline Vector3 to_vector3(const Vector4& a)
- {
- return Vector3(a.x, a.y, a.z);
- }
- }
- inline Vector4::Vector4()
- {
- // Do not initialize
- }
- inline Vector4::Vector4(const Vector3& a, float w) : x(a.x), y(a.y), z(a.z), w(w)
- {
- }
- inline Vector4::Vector4(float val) : x(val), y(val), z(val), w(val)
- {
- }
- inline Vector4::Vector4(float nx, float ny, float nz, float nw) : x(nx), y(ny), z(nz), w(nw)
- {
- }
- inline Vector4::Vector4(const float a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3])
- {
- }
- inline const float& Vector4::operator[](uint32_t i) const
- {
- CE_ASSERT(i < 4, "Index out of bounds");
- return (&x)[i];
- }
- inline float& Vector4::operator[](uint32_t i)
- {
- CE_ASSERT(i < 4, "Index out of bounds");
- return (&x)[i];
- }
- inline Vector4& Vector4::operator+=(const Vector4& a)
- {
- x += a.x;
- y += a.y;
- z += a.z;
- w += a.w;
- return *this;
- }
- inline Vector4& Vector4::operator-=(const Vector4& a)
- {
- x -= a.x;
- y -= a.y;
- z -= a.z;
- w -= a.w;
- return *this;
- }
- inline Vector4& Vector4::operator*=(float k)
- {
- x *= k;
- y *= k;
- z *= k;
- w *= k;
- return *this;
- }
- inline Vector4& Vector4::operator/=(float k)
- {
- CE_ASSERT(k != (float)0.0, "Division by zero");
- float inv = (float)(1.0 / k);
- x *= inv;
- y *= inv;
- z *= inv;
- w *= inv;
- return *this;
- }
- } // namespace crown
|