From 99739491a7fc5de51a0b4c2a7340ca2258ec37bc Mon Sep 17 00:00:00 2001 From: kearwood Date: Mon, 3 Oct 2022 23:03:43 -0700 Subject: [PATCH] Implemented Vector3i --- CMakeLists.txt | 2 + include/hydra.h | 1 + include/vector3i.h | 145 ++++++++++++++++ src/vector3i.cpp | 420 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 568 insertions(+) create mode 100644 include/vector3i.h create mode 100644 src/vector3i.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 49b2bd2..327fc49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(PUBLIC_HEADERS include/vector3.h include/vector4.h include/vector2i.h + include/vector3i.h ) set(SRCS @@ -34,6 +35,7 @@ set(SRCS src/vector3.cpp src/vector4.cpp src/vector2i.cpp + src/vector3i.cpp ) add_library(hydra ${SRCS} ${PUBLIC_HEADERS}) diff --git a/include/hydra.h b/include/hydra.h index d52c205..1926617 100644 --- a/include/hydra.h +++ b/include/hydra.h @@ -37,6 +37,7 @@ #include "vector3.h" #include "vector4.h" #include "vector2i.h" +#include "vector3i.h" #include "matrix2.h" #include "matrix2x3.h" #include "matrix4.h" diff --git a/include/vector3i.h b/include/vector3i.h new file mode 100644 index 0000000..a594235 --- /dev/null +++ b/include/vector3i.h @@ -0,0 +1,145 @@ +// +// Vector3i.h +// Kraken Engine / Hydra +// +// Copyright 2022 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. +// + +#pragma once + +#include // for hash<> + +#include "vector2i.h" + +namespace kraken { + +class Vector3i +{ + +public: + union + { + struct + { + int x, y, z; + }; + int c[3]; + }; + + void init(); + void init(int X, int Y, int Z); + void init(int v); + void init(int* v); + void init(const Vector3i& v); + static Vector3i Create(); + static Vector3i Create(int X, int Y, int Z); + static Vector3i Create(int v); + static Vector3i Create(int* v); + static Vector3i Create(const Vector3i& v); + + + // Vector2 swizzle getters + Vector2i xx() const; + Vector2i xy() const; + Vector2i xz() const; + Vector2i yx() const; + Vector2i yy() const; + Vector2i yz() const; + Vector2i zx() const; + Vector2i zy() const; + Vector2i zz() const; + + // Vector2 swizzle setters + void xy(const Vector2i& v); + void xz(const Vector2i& v); + void yx(const Vector2i& v); + void yz(const Vector2i& v); + void zx(const Vector2i& v); + void zy(const Vector2i& v); + + Vector3i operator +(const Vector3i& b) const; + Vector3i operator -(const Vector3i& b) const; + Vector3i operator +() const; + Vector3i operator -() const; + Vector3i operator *(const int v) const; + Vector3i operator /(const int v) const; + + Vector3i& operator +=(const Vector3i& b); + Vector3i& operator -=(const Vector3i& b); + Vector3i& operator *=(const int v); + Vector3i& operator /=(const int v); + + bool operator ==(const Vector3i& b) const; + bool operator !=(const Vector3i& b) const; + + // Comparison operators are implemented to allow insertion into sorted containers such as std::set + bool operator >(const Vector3i& b) const; + bool operator <(const Vector3i& b) const; + + int& operator[](unsigned i); + int operator[](unsigned i) const; + + int sqrMagnitude() const; // calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function) + int magnitude() const; + + void scale(const Vector3i& v); + + static Vector3i Cross(const Vector3i& v1, const Vector3i& v2); + static int Dot(const Vector3i& v1, const Vector3i& v2); + static Vector3i Min(const Vector3i& v1, const Vector3i& v2); + static Vector3i Max(const Vector3i& v1, const Vector3i& v2); + + static Vector3i Min(); + static Vector3i Max(); + static Vector3i Zero(); + static Vector3i One(); + static Vector3i Forward(); + static Vector3i Backward(); + static Vector3i Up(); + static Vector3i Down(); + static Vector3i Left(); + static Vector3i Right(); + static Vector3i Scale(const Vector3i& v1, const Vector3i& v2); +}; +static_assert(std::is_pod::value, "kraken::Vector3i must be a POD type."); + +} // namespace kraken + +namespace std { +template<> +struct hash +{ +public: + size_t operator()(const kraken::Vector3i& s) const + { + size_t h1 = hash()(s.x); + size_t h2 = hash()(s.y); + size_t h3 = hash()(s.z); + return h1 ^ (h2 << 1) ^ (h3 << 2); + } +}; +} // namespace std diff --git a/src/vector3i.cpp b/src/vector3i.cpp new file mode 100644 index 0000000..7085ff0 --- /dev/null +++ b/src/vector3i.cpp @@ -0,0 +1,420 @@ +// +// Vector3i.cpp +// Kraken Engine / Hydra +// +// Copyright 2022 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/hydra.h" +#include "krhelpers.h" + +namespace kraken { + +//default constructor +void Vector3i::init() +{ + x = 0; + y = 0; + z = 0; +} + +Vector3i Vector3i::Create() +{ + Vector3i r; + r.init(); + return r; +} + +void Vector3i::init(const Vector3i& v) +{ + x = v.x; + y = v.y; + z = v.z; +} + +Vector3i Vector3i::Create(const Vector3i& v) +{ + Vector3i r; + r.init(v); + return r; +} + +void Vector3i::init(int* v) +{ + x = v[0]; + y = v[1]; + z = v[2]; +} + +Vector3i Vector3i::Create(int* v) +{ + Vector3i r; + r.init(v); + return r; +} + +void Vector3i::init(int v) +{ + x = v; + y = v; + z = v; +} + +Vector3i Vector3i::Create(int v) +{ + Vector3i r; + r.init(v); + return r; +} + +void Vector3i::init(int X, int Y, int Z) +{ + x = X; + y = Y; + z = Z; +} + +Vector3i Vector3i::Create(int X, int Y, int Z) +{ + Vector3i r; + r.init(X, Y, Z); + return r; +} + +Vector2i Vector3i::xx() const +{ + return Vector2i::Create(x, x); +} + +Vector2i Vector3i::xy() const +{ + return Vector2i::Create(x, y); +} + +Vector2i Vector3i::xz() const +{ + return Vector2i::Create(x, z); +} + +Vector2i Vector3i::yx() const +{ + return Vector2i::Create(y, x); +} + +Vector2i Vector3i::yy() const +{ + return Vector2i::Create(y, y); +} + +Vector2i Vector3i::yz() const +{ + return Vector2i::Create(y, z); +} + +Vector2i Vector3i::zx() const +{ + return Vector2i::Create(z, x); +} + +Vector2i Vector3i::zy() const +{ + return Vector2i::Create(z, y); +} + +Vector2i Vector3i::zz() const +{ + return Vector2i::Create(z, z); +} + +void Vector3i::xy(const Vector2i& v) +{ + x = v.x; + y = v.y; +} + +void Vector3i::xz(const Vector2i& v) +{ + x = v.x; + z = v.y; +} + +void Vector3i::yx(const Vector2i& v) +{ + y = v.x; + x = v.y; +} + +void Vector3i::yz(const Vector2i& v) +{ + y = v.x; + z = v.y; +} + +void Vector3i::zx(const Vector2i& v) +{ + z = v.x; + x = v.y; +} + +void Vector3i::zy(const Vector2i& v) +{ + z = v.x; + y = v.y; +} + +Vector3i Vector3i::Min() +{ + return Vector3i::Create(-std::numeric_limits::max()); +} + +Vector3i Vector3i::Max() +{ + return Vector3i::Create(std::numeric_limits::max()); +} + +Vector3i Vector3i::Zero() +{ + return Vector3i::Create(); +} + +Vector3i Vector3i::One() +{ + return Vector3i::Create(1,1,1); +} + +Vector3i Vector3i::Forward() +{ + return Vector3i::Create(0, 0, 1); +} + +Vector3i Vector3i::Backward() +{ + return Vector3i::Create(0, 0, -1); +} + +Vector3i Vector3i::Up() +{ + return Vector3i::Create(0,1,0); +} + +Vector3i Vector3i::Down() +{ + return Vector3i::Create(0, -1, 0); +} + +Vector3i Vector3i::Left() +{ + return Vector3i::Create(-1, 0, 0); +} + +Vector3i Vector3i::Right() +{ + return Vector3i::Create(1,0,0); +} + + +void Vector3i::scale(const Vector3i& v) +{ + x *= v.x; + y *= v.y; + z *= v.z; +} + +Vector3i Vector3i::Scale(const Vector3i& v1, const Vector3i& v2) +{ + return Vector3i::Create(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); +} + +Vector3i Vector3i::operator +(const Vector3i& b) const +{ + return Vector3i::Create(x + b.x, y + b.y, z + b.z); +} +Vector3i Vector3i::operator -(const Vector3i& b) const +{ + return Vector3i::Create(x - b.x, y - b.y, z - b.z); +} +Vector3i Vector3i::operator +() const +{ + return *this; +} +Vector3i Vector3i::operator -() const +{ + return Vector3i::Create(-x, -y, -z); +} + +Vector3i Vector3i::operator *(const int v) const +{ + return Vector3i::Create(x * v, y * v, z * v); +} + +Vector3i Vector3i::operator /(const int v) const +{ + return Vector3i::Create(x / v, y / v, z / v); +} + +Vector3i& Vector3i::operator +=(const Vector3i& b) +{ + x += b.x; + y += b.y; + z += b.z; + + return *this; +} + +Vector3i& Vector3i::operator -=(const Vector3i& b) +{ + x -= b.x; + y -= b.y; + z -= b.z; + + return *this; +} + +Vector3i& Vector3i::operator *=(const int v) +{ + x *= v; + y *= v; + z *= v; + + return *this; +} + +Vector3i& Vector3i::operator /=(const int v) +{ + x /= v; + y /= v; + z /= v; + + return *this; +} + +bool Vector3i::operator ==(const Vector3i& b) const +{ + return x == b.x && y == b.y && z == b.z; + +} +bool Vector3i::operator !=(const Vector3i& b) const +{ + return x != b.x || y != b.y || z != b.z; +} + +int& Vector3i::operator[](unsigned i) +{ + switch (i) { + case 0: + return x; + case 1: + return y; + default: + case 2: + return z; + } +} + +int Vector3i::operator[](unsigned i) const +{ + switch (i) { + case 0: + return x; + case 1: + return y; + case 2: + default: + return z; + } +} + +int Vector3i::sqrMagnitude() const +{ + // calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function) + return x * x + y * y + z * z; +} + +int Vector3i::magnitude() const +{ + return sqrt(sqrMagnitude()); +} + +Vector3i Vector3i::Cross(const Vector3i& v1, const Vector3i& v2) +{ + return Vector3i::Create(v1.y * v2.z - v1.z * v2.y, + v1.z * v2.x - v1.x * v2.z, + v1.x * v2.y - v1.y * v2.x); +} + +int Vector3i::Dot(const Vector3i& v1, const Vector3i& v2) +{ + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; +} + +Vector3i Vector3i::Min(const Vector3i& v1, const Vector3i& v2) +{ + return Vector3i::Create(KRMIN(v1.x, v2.x), KRMIN(v1.y, v2.y), KRMIN(v1.z, v2.z)); +} + +Vector3i Vector3i::Max(const Vector3i& v1, const Vector3i& v2) +{ + return Vector3i::Create(KRMAX(v1.x, v2.x), KRMAX(v1.y, v2.y), KRMAX(v1.z, v2.z)); +} + +bool Vector3i::operator >(const Vector3i& 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 if (y < b.y) { + return false; + } else if (z > b.z) { + return true; + } else { + return false; + } +} + +bool Vector3i::operator <(const Vector3i& 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 if (y > b.y) { + return false; + } else if (z < b.z) { + return true; + } else { + return false; + } +} + +} // namespace kraken