diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f2ff7c..43fea47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ set(PUBLIC_HEADERS include/vector2.h include/vector3.h include/vector4.h + include/vector2i.h ) set(SRCS @@ -29,6 +30,7 @@ set(SRCS src/vector2.cpp src/vector3.cpp src/vector4.cpp + src/vector2i.cpp ) add_library(krakenmath STATIC ${SRCS} ${PUBLIC_HEADERS}) diff --git a/include/kraken-math.h b/include/kraken-math.h index 5223841..bb9cb78 100644 --- a/include/kraken-math.h +++ b/include/kraken-math.h @@ -35,6 +35,7 @@ #include "vector2.h" #include "vector3.h" #include "vector4.h" +#include "vector2i.h" #include "matrix4.h" #include "quaternion.h" #include "aabb.h" diff --git a/include/vector2i.h b/include/vector2i.h new file mode 100644 index 0000000..12882a9 --- /dev/null +++ b/include/vector2i.h @@ -0,0 +1,122 @@ +// +// vector2i.h +// Kraken +// +// Copyright 2018 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + + +#ifndef KRAKEN_VECTOR2I_H +#define KRAKEN_VECTOR2I_H + +#include // for hash<> +#include // for std::numeric_limits<> +#include // for sqrtf + +namespace kraken { + +class Vector2i { + +public: + union { + struct { + int x, y; + }; + int c[2]; + }; + + void init(); + void init(int X, int Y); + void init(int v); + void init(int *v); + void init(const Vector2i &v); + static Vector2i Create(); + static Vector2i Create(int X, int Y); + static Vector2i Create(int v); + static Vector2i Create(int *v); + static Vector2i Create(const Vector2i &v); + + // Vector2 swizzle getters + Vector2i yx() const; + + // Vector2 swizzle setters + void yx(const Vector2i &v); + + Vector2i operator +(const Vector2i& b) const; + Vector2i operator -(const Vector2i& b) const; + Vector2i operator +() const; + Vector2i operator -() const; + Vector2i operator *(const int v) const; + Vector2i operator /(const int v) const; + + Vector2i& operator +=(const Vector2i& b); + Vector2i& operator -=(const Vector2i& b); + Vector2i& operator *=(const int v); + Vector2i& operator /=(const int v); + + // Comparison operators are implemented to allow insertion into sorted containers such as std::set + bool operator >(const Vector2i& b) const; + bool operator <(const Vector2i& b) const; + + bool operator ==(const Vector2i& b) const; + bool operator !=(const Vector2i& b) const; + + int& operator[](unsigned i); + int operator[](unsigned i) const; + + int sqrMagnitude() const; + int magnitude() const; + + void normalize(); + static Vector2i Normalize(const Vector2i &v); + + static int Cross(const Vector2i &v1, const Vector2i &v2); + + static int Dot(const Vector2i &v1, const Vector2i &v2); + static Vector2i Min(); + static Vector2i Max(); + static Vector2i Zero(); + static Vector2i One(); +}; // class Vector2i +static_assert(std::is_pod::value, "kraken::Vector2i must be a POD type."); + +} // namespace kraken + +namespace std { + template<> + struct hash { + public: + size_t operator()(const kraken::Vector2i &s) const + { + size_t h1 = hash()(s.x); + size_t h2 = hash()(s.y); + return h1 ^ (h2 << 1); + } + }; +} // namespace std + +#endif // KRAKEN_VECTOR2I_H diff --git a/src/vector2i.cpp b/src/vector2i.cpp new file mode 100644 index 0000000..244da22 --- /dev/null +++ b/src/vector2i.cpp @@ -0,0 +1,256 @@ +// +// Vector2i.cpp +// Kraken +// +// Copyright 2018 Kearwood Gilbert. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other materials +// provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// The views and conclusions contained in the software and documentation are those of the +// authors and should not be interpreted as representing official policies, either expressed +// or implied, of Kearwood Gilbert. +// + +#include "../include/kraken-math.h" + +namespace kraken { + +void Vector2i::init() { + x = 0; + y = 0; +} + +Vector2i Vector2i::Create() +{ + Vector2i r; + r.init(); + return r; +} + +void Vector2i::init(int X, int Y) { + x = X; + y = Y; +} + +Vector2i Vector2i::Create(int X, int Y) +{ + Vector2i r; + r.init(X,Y); + return r; +} + +void Vector2i::init(int v) { + x = v; + y = v; +} + +Vector2i Vector2i::Create(int v) +{ + Vector2i r; + r.init(v); + return r; +} + +void Vector2i::init(int *v) { + x = v[0]; + y = v[1]; +} + +Vector2i Vector2i::Create(int *v) +{ + Vector2i r; + r.init(v); + return r; +} + +void Vector2i::init(const Vector2i &v) { + x = v.x; + y = v.y; +} + +Vector2i Vector2i::Create(const Vector2i &v) +{ + Vector2i r; + r.init(v); + return r; +} + +// Vector2 swizzle getters +Vector2i Vector2i::yx() const +{ + return Vector2i::Create(y,x); +} + +// Vector2 swizzle setters +void Vector2i::yx(const Vector2i &v) +{ + y = v.x; + x = v.y; +} + +Vector2i Vector2i::Min() { + return Vector2i::Create(-std::numeric_limits::max()); +} + +Vector2i Vector2i::Max() { + return Vector2i::Create(std::numeric_limits::max()); +} + +Vector2i Vector2i::Zero() { + return Vector2i::Create(0); +} + +Vector2i Vector2i::One() { + return Vector2i::Create(1); +} + +Vector2i Vector2i::operator +(const Vector2i& b) const { + return Vector2i::Create(x + b.x, y + b.y); +} + +Vector2i Vector2i::operator -(const Vector2i& b) const { + return Vector2i::Create(x - b.x, y - b.y); +} + +Vector2i Vector2i::operator +() const { + return *this; +} + +Vector2i Vector2i::operator -() const { + return Vector2i::Create(-x, -y); +} + +Vector2i Vector2i::operator *(const int v) const { + return Vector2i::Create(x * v, y * v); +} + +Vector2i Vector2i::operator /(const int v) const { + return Vector2i::Create(x / v, y / v); +} + +Vector2i& Vector2i::operator +=(const Vector2i& b) { + x += b.x; + y += b.y; + return *this; +} + +Vector2i& Vector2i::operator -=(const Vector2i& b) { + x -= b.x; + y -= b.y; + return *this; +} + +Vector2i& Vector2i::operator *=(const int v) { + x *= v; + y *= v; + return *this; +} + +Vector2i& Vector2i::operator /=(const int v) { + x /= v; + y /= v; + return *this; +} + +bool Vector2i::operator ==(const Vector2i& b) const { + return x == b.x && y == b.y; +} + +bool Vector2i::operator !=(const Vector2i& b) const { + return x != b.x || y != b.y; +} + +bool Vector2i::operator >(const Vector2i& b) const +{ + // Comparison operators are implemented to allow insertion into sorted containers such as std::set + if(x > b.x) { + return true; + } else if(x < b.x) { + return false; + } else if(y > b.y) { + return true; + } else { + return false; + } +} + +bool Vector2i::operator <(const Vector2i& b) const +{ + // Comparison operators are implemented to allow insertion into sorted containers such as std::set + if(x < b.x) { + return true; + } else if(x > b.x) { + return false; + } else if(y < b.y) { + return true; + } else { + return false; + } +} + +int& Vector2i::operator[] (unsigned i) { + switch(i) { + case 0: + return x; + case 1: + default: + return y; + } +} + +int Vector2i::operator[](unsigned i) const { + switch(i) { + case 0: + return x; + case 1: + default: + return y; + } +} + +void Vector2i::normalize() { + int m = magnitude(); + x /= m; + y /= m; +} + +int Vector2i::sqrMagnitude() const { + return x * x + y * y; +} + +int Vector2i::magnitude() const { + return static_cast(sqrt(x * x + y * y)); +} + +Vector2i Vector2i::Normalize(const Vector2i &v) { + int m = v.magnitude(); + return Vector2i::Create(v.x / m, v.y / m); +} + +int Vector2i::Cross(const Vector2i &v1, const Vector2i &v2) { + return v1.x * v2.y - v1.y * v2.x; +} + +int Vector2i::Dot(const Vector2i &v1, const Vector2i &v2) { + return v1.x * v2.x + v1.y * v2.y; +} + +} // namepsace kraken