Added Kraken Math files, extracted from Kraken Engine

This commit is contained in:
2018-02-26 15:24:21 -08:00
commit 4ceb89c3bd
21 changed files with 3810 additions and 0 deletions

104
include/aabb.h Normal file
View File

@@ -0,0 +1,104 @@
//
// KRAABB.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.
//
// Axis aligned bounding box (AABB)
#ifndef KRAKEN_AABB_H
#define KRAKEN_AABB_H
#include <functional> // for hash<>
#include "vector2.h"
#include "vector3.h"
namespace kraken {
class Matrix4;
class AABB {
public:
Vector3 min;
Vector3 max;
void init(const Vector3 &minPoint, const Vector3 &maxPoint);
void init(const Vector3 &corner1, const Vector3 &corner2, const Matrix4 &modelMatrix);
void init();
static AABB Create(const Vector3 &minPoint, const Vector3 &maxPoint);
static AABB Create(const Vector3 &corner1, const Vector3 &corner2, const Matrix4 &modelMatrix);
static AABB Create();
void scale(const Vector3 &s);
void scale(float s);
Vector3 center() const;
Vector3 size() const;
float volume() const;
bool intersects(const AABB& b) const;
bool contains(const AABB &b) const;
bool contains(const Vector3 &v) const;
bool intersectsLine(const Vector3 &v1, const Vector3 &v2) const;
bool intersectsRay(const Vector3 &v1, const Vector3 &dir) const;
bool intersectsSphere(const Vector3 &center, float radius) const;
void encapsulate(const AABB & b);
bool operator ==(const AABB& b) const;
bool operator !=(const AABB& b) const;
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
bool operator >(const AABB& b) const;
bool operator <(const AABB& b) const;
static AABB Infinite();
static AABB Zero();
float longest_radius() const;
Vector3 nearestPoint(const Vector3 & v) const;
};
static_assert(std::is_pod<AABB>::value, "kraken::AABB must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::AABB> {
public:
size_t operator()(const kraken::AABB &s) const
{
size_t h1 = hash<kraken::Vector3>()(s.min);
size_t h2 = hash<kraken::Vector3>()(s.max);
return h1 ^ ( h2 << 1 );
}
};
} // namespace std
#endif /* defined(KRAKEN_AABB_H) */

65
include/hitinfo.h Normal file
View File

@@ -0,0 +1,65 @@
//
// hitinfo.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_HITINFO_H
#define KRAKEN_HITINFO_H
#include "vector3.h"
class KRNode;
namespace kraken {
class HitInfo {
public:
HitInfo();
HitInfo(const Vector3 &position, const Vector3 &normal, const float distance);
HitInfo(const Vector3 &position, const Vector3 &normal, const float distance, KRNode *node);
~HitInfo();
Vector3 getPosition() const;
Vector3 getNormal() const;
float getDistance() const;
KRNode *getNode() const;
bool didHit() const;
HitInfo& operator =(const HitInfo& b);
private:
KRNode *m_node;
Vector3 m_position;
Vector3 m_normal;
float m_distance;
};
} // namespace kraken
#endif

44
include/kraken-math.h Normal file
View File

@@ -0,0 +1,44 @@
//
// 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_H
#define KRAKEN_H
#include "scalar.h"
#include "vector2.h"
#include "vector3.h"
#include "vector4.h"
#include "matrix4.h"
#include "quaternion.h"
#include "aabb.h"
#include "triangle3.h"
#include "hitinfo.h"
#endif // KRAKEN_H

130
include/matrix4.h Normal file
View File

@@ -0,0 +1,130 @@
//
// Matrix4.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.
//
#include "vector3.h"
#include "vector4.h"
#ifndef KRAKEN_MATRIX4_H
#define KRAKEN_MATRIX4_H
namespace kraken {
typedef enum {
X_AXIS,
Y_AXIS,
Z_AXIS
} AXIS;
class Quaternion;
class Matrix4 {
public:
union {
struct {
Vector4 axis_x, axis_y, axis_z, transform;
};
// Matrix components, in column-major order
float c[16];
};
// Default initializer - Creates an identity matrix
void init();
void init(float *pMat);
void init(const Vector3 &new_axis_x, const Vector3 &new_axis_y, const Vector3 &new_axis_z, const Vector3 &new_transform);
void init(const Matrix4 &m);
// Overload comparison operator
bool operator==(const Matrix4 &m) const;
// Overload compound multiply operator
Matrix4& operator*=(const Matrix4 &m);
float& operator[](unsigned i);
float operator[](unsigned i) const;
// Overload multiply operator
//Matrix4& operator*(const Matrix4 &m);
Matrix4 operator*(const Matrix4 &m) const;
float *getPointer();
void perspective(float fov, float aspect, float nearz, float farz);
void ortho(float left, float right, float top, float bottom, float nearz, float farz);
void translate(float x, float y, float z);
void translate(const Vector3 &v);
void scale(float x, float y, float z);
void scale(const Vector3 &v);
void scale(float s);
void rotate(float angle, AXIS axis);
void rotate(const Quaternion &q);
void bias();
bool invert();
void transpose();
static Vector3 DotNoTranslate(const Matrix4 &m, const Vector3 &v); // Dot product without including translation; useful for transforming normals and tangents
static Matrix4 Invert(const Matrix4 &m);
static Matrix4 Transpose(const Matrix4 &m);
static Vector3 Dot(const Matrix4 &m, const Vector3 &v);
static Vector4 Dot4(const Matrix4 &m, const Vector4 &v);
static float DotW(const Matrix4 &m, const Vector3 &v);
static Vector3 DotWDiv(const Matrix4 &m, const Vector3 &v);
static Matrix4 LookAt(const Vector3 &cameraPos, const Vector3 &lookAtPos, const Vector3 &upDirection);
static Matrix4 Translation(const Vector3 &v);
static Matrix4 Rotation(const Vector3 &v);
static Matrix4 Scaling(const Vector3 &v);
};
static_assert(std::is_pod<Matrix4>::value, "kraken::Matrix4 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Matrix4> {
public:
size_t operator()(const kraken::Matrix4 &s) const
{
size_t h1 = hash<kraken::Vector4>()(s.axis_x);
size_t h2 = hash<kraken::Vector4>()(s.axis_y);
size_t h3 = hash<kraken::Vector4>()(s.axis_z);
size_t h4 = hash<kraken::Vector4>()(s.transform);
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
}
};
} // namespace std
#endif // KRAKEN_MATRIX4_H

114
include/quaternion.h Normal file
View File

@@ -0,0 +1,114 @@
//
// Quaternion.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_QUATERNION_H
#define KRAKEN_QUATERNION_H
#include "vector3.h"
namespace kraken {
class Quaternion {
public:
union {
struct {
float w, x, y, z;
};
float c[4];
};
void init();
void init(float w, float x, float y, float z);
void init(const Quaternion& p);
void init(const Vector3 &euler);
void init(const Vector3 &from_vector, const Vector3 &to_vector);
static Quaternion Create();
static Quaternion Create(float w, float x, float y, float z);
static Quaternion Create(const Quaternion& p);
static Quaternion Create(const Vector3 &euler);
static Quaternion Create(const Vector3 &from_vector, const Vector3 &to_vector);
Quaternion operator +(const Quaternion &v) const;
Quaternion operator -(const Quaternion &v) const;
Quaternion operator +() const;
Quaternion operator -() const;
Quaternion operator *(const Quaternion &v);
Quaternion operator *(float num) const;
Quaternion operator /(float num) const;
Quaternion& operator +=(const Quaternion& v);
Quaternion& operator -=(const Quaternion& v);
Quaternion& operator *=(const Quaternion& v);
Quaternion& operator *=(const float& v);
Quaternion& operator /=(const float& v);
friend bool operator ==(Quaternion &v1, Quaternion &v2);
friend bool operator !=(Quaternion &v1, Quaternion &v2);
float& operator [](unsigned i);
float operator [](unsigned i) const;
void setEulerXYZ(const Vector3 &euler);
void setEulerZYX(const Vector3 &euler);
Vector3 eulerXYZ() const;
Matrix4 rotationMatrix() const;
void normalize();
static Quaternion Normalize(const Quaternion &v1);
void conjugate();
static Quaternion Conjugate(const Quaternion &v1);
static Quaternion FromAngleAxis(const Vector3 &axis, float angle);
static Quaternion Lerp(const Quaternion &a, const Quaternion &b, float t);
static Quaternion Slerp(const Quaternion &a, const Quaternion &b, float t);
static float Dot(const Quaternion &v1, const Quaternion &v2);
};
static_assert(std::is_pod<Quaternion>::value, "kraken::Quaternion must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Quaternion> {
public:
size_t operator()(const kraken::Quaternion &s) const
{
size_t h1 = hash<float>()(s.c[0]);
size_t h2 = hash<float>()(s.c[1]);
size_t h3 = hash<float>()(s.c[2]);
size_t h4 = hash<float>()(s.c[3]);
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
}
};
} // namespace std
#endif // KRAKEN_QUATERNION_H

41
include/scalar.h Normal file
View File

@@ -0,0 +1,41 @@
//
// KRFloat.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_SCALAR_H
#define KRAKEN_SCALAR_H
namespace kraken {
float SmoothStep(float a, float b, float t);
}; // namespace kraken
#endif // KRAKEN_SCALAR_H

78
include/triangle3.h Normal file
View File

@@ -0,0 +1,78 @@
//
// KRTriangle.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_TRIANGLE3_H
#define KRAKEN_TRIANGLE3_H
#include "vector3.h"
namespace kraken {
class Triangle3
{
public:
Vector3 vert[3];
void init(const Triangle3 &tri);
void init(const Vector3 &v1, const Vector3 &v2, const Vector3 &v3);
Vector3 calculateNormal() const;
bool operator ==(const Triangle3& b) const;
bool operator !=(const Triangle3& b) const;
Vector3& operator[](unsigned int i);
Vector3 operator[](unsigned int i) const;
bool rayCast(const Vector3 &start, const Vector3 &dir, Vector3 &hit_point) const;
bool sphereCast(const Vector3 &start, const Vector3 &dir, float radius, Vector3 &hit_point, float &hit_distance) const;
bool containsPoint(const Vector3 &p) const;
Vector3 closestPointOnTriangle(const Vector3 &p) const;
};
static_assert(std::is_pod<Triangle3>::value, "kraken::Triangle3 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Triangle3> {
public:
size_t operator()(const kraken::Triangle3 &s) const
{
size_t h1 = hash<kraken::Vector3>()(s.vert[0]);
size_t h2 = hash<kraken::Vector3>()(s.vert[1]);
size_t h3 = hash<kraken::Vector3>()(s.vert[2]);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
} // namespace std
#endif // KRAKEN_TRIANGLE3_H

121
include/vector2.h Normal file
View File

@@ -0,0 +1,121 @@
//
// vector2.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_VECTOR2_H
#define KRAKEN_VECTOR2_H
#include <functional> // for hash<>
#include <limits> // for std::numeric_limits<>
#include <math.h> // for sqrtf
namespace kraken {
class Vector2 {
public:
union {
struct {
float x, y;
};
float c[2];
};
void init();
void init(float X, float Y);
void init(float v);
void init(float *v);
void init(const Vector2 &v);
static Vector2 Create();
static Vector2 Create(float X, float Y);
static Vector2 Create(float v);
static Vector2 Create(float *v);
static Vector2 Create(const Vector2 &v);
// Vector2 swizzle getters
Vector2 yx() const;
// Vector2 swizzle setters
void yx(const Vector2 &v);
Vector2 operator +(const Vector2& b) const;
Vector2 operator -(const Vector2& b) const;
Vector2 operator +() const;
Vector2 operator -() const;
Vector2 operator *(const float v) const;
Vector2 operator /(const float v) const;
Vector2& operator +=(const Vector2& b);
Vector2& operator -=(const Vector2& b);
Vector2& operator *=(const float v);
Vector2& operator /=(const float v);
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
bool operator >(const Vector2& b) const;
bool operator <(const Vector2& b) const;
bool operator ==(const Vector2& b) const;
bool operator !=(const Vector2& b) const;
float& operator[](unsigned i);
float operator[](unsigned i) const;
float sqrMagnitude() const;
float magnitude() const;
void normalize();
static Vector2 Normalize(const Vector2 &v);
static float Cross(const Vector2 &v1, const Vector2 &v2);
static float Dot(const Vector2 &v1, const Vector2 &v2);
static Vector2 Min();
static Vector2 Max();
static Vector2 Zero();
static Vector2 One();
};
static_assert(std::is_pod<Vector2>::value, "kraken::Vector2 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Vector2> {
public:
size_t operator()(const kraken::Vector2 &s) const
{
size_t h1 = hash<float>()(s.x);
size_t h2 = hash<float>()(s.y);
return h1 ^ (h2 << 1);
}
};
} // namespace std
#endif // KRAKEN_VECTOR2_H

153
include/vector3.h Normal file
View File

@@ -0,0 +1,153 @@
//
// Vector3.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_VECTOR3_H
#define KRAKEN_VECTOR3_H
#include <functional> // for hash<>
#include "vector2.h"
#include "vector4.h"
namespace kraken {
class Vector3 {
public:
union {
struct {
float x, y, z;
};
float c[3];
};
void init();
void init(float X, float Y, float Z);
void init(float v);
void init(float *v);
void init(double *v);
void init(const Vector3 &v);
void init(const Vector4 &v);
static Vector3 Create();
static Vector3 Create(float X, float Y, float Z);
static Vector3 Create(float v);
static Vector3 Create(float *v);
static Vector3 Create(double *v);
static Vector3 Create(const Vector3 &v);
static Vector3 Create(const Vector4 &v);
// Vector2 swizzle getters
Vector2 xx() const;
Vector2 xy() const;
Vector2 xz() const;
Vector2 yx() const;
Vector2 yy() const;
Vector2 yz() const;
Vector2 zx() const;
Vector2 zy() const;
Vector2 zz() const;
// Vector2 swizzle setters
void xy(const Vector2 &v);
void xz(const Vector2 &v);
void yx(const Vector2 &v);
void yz(const Vector2 &v);
void zx(const Vector2 &v);
void zy(const Vector2 &v);
Vector3& operator =(const Vector4& b);
Vector3 operator +(const Vector3& b) const;
Vector3 operator -(const Vector3& b) const;
Vector3 operator +() const;
Vector3 operator -() const;
Vector3 operator *(const float v) const;
Vector3 operator /(const float v) const;
Vector3& operator +=(const Vector3& b);
Vector3& operator -=(const Vector3& b);
Vector3& operator *=(const float v);
Vector3& operator /=(const float v);
bool operator ==(const Vector3& b) const;
bool operator !=(const Vector3& b) const;
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
bool operator >(const Vector3& b) const;
bool operator <(const Vector3& b) const;
float& operator[](unsigned i);
float operator[](unsigned i) const;
float sqrMagnitude() const; // calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
float magnitude() const;
void scale(const Vector3 &v);
void normalize();
static Vector3 Normalize(const Vector3 &v);
static Vector3 Cross(const Vector3 &v1, const Vector3 &v2);
static float Dot(const Vector3 &v1, const Vector3 &v2);
static Vector3 Min();
static Vector3 Max();
static Vector3 Zero();
static Vector3 One();
static Vector3 Forward();
static Vector3 Backward();
static Vector3 Up();
static Vector3 Down();
static Vector3 Left();
static Vector3 Right();
static Vector3 Scale(const Vector3 &v1, const Vector3 &v2);
static Vector3 Lerp(const Vector3 &v1, const Vector3 &v2, float d);
static Vector3 Slerp(const Vector3 &v1, const Vector3 &v2, float d);
static void OrthoNormalize(Vector3 &normal, Vector3 &tangent); // Gram-Schmidt Orthonormalization
};
static_assert(std::is_pod<Vector3>::value, "kraken::Vector3 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Vector3> {
public:
size_t operator()(const kraken::Vector3 &s) const
{
size_t h1 = hash<float>()(s.x);
size_t h2 = hash<float>()(s.y);
size_t h3 = hash<float>()(s.z);
return h1 ^ (h2 << 1) ^ (h3 << 2);
}
};
} // namespace std
#endif // KRAKEN_VECTOR3_H

126
include/vector4.h Normal file
View File

@@ -0,0 +1,126 @@
//
// Vector4.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_VECTOR4_H
#define KRAKEN_VECTOR4_H
#include <functional> // for hash<>
namespace kraken {
class Vector3;
class Vector4 {
public:
union {
struct {
float x, y, z, w;
};
float c[4];
};
void init();
void init(float X, float Y, float Z, float W);
void init(float v);
void init(float *v);
void init(const Vector4 &v);
void init(const Vector3 &v, float W);
static Vector4 Create();
static Vector4 Create(float X, float Y, float Z, float W);
static Vector4 Create(float v);
static Vector4 Create(float *v);
static Vector4 Create(const Vector4 &v);
static Vector4 Create(const Vector3 &v, float W);
Vector4 operator +(const Vector4& b) const;
Vector4 operator -(const Vector4& b) const;
Vector4 operator +() const;
Vector4 operator -() const;
Vector4 operator *(const float v) const;
Vector4 operator /(const float v) const;
Vector4& operator +=(const Vector4& b);
Vector4& operator -=(const Vector4& b);
Vector4& operator *=(const float v);
Vector4& operator /=(const float v);
bool operator ==(const Vector4& b) const;
bool operator !=(const Vector4& b) const;
// Comparison operators are implemented to allow insertion into sorted containers such as std::set
bool operator >(const Vector4& b) const;
bool operator <(const Vector4& b) const;
float& operator[](unsigned i);
float operator[](unsigned i) const;
float sqrMagnitude() const; // calculate the square of the magnitude (useful for comparison of magnitudes without the cost of a sqrt() function)
float magnitude() const;
void normalize();
static Vector4 Normalize(const Vector4 &v);
static float Dot(const Vector4 &v1, const Vector4 &v2);
static Vector4 Min();
static Vector4 Max();
static Vector4 Zero();
static Vector4 One();
static Vector4 Forward();
static Vector4 Backward();
static Vector4 Up();
static Vector4 Down();
static Vector4 Left();
static Vector4 Right();
static Vector4 Lerp(const Vector4 &v1, const Vector4 &v2, float d);
static Vector4 Slerp(const Vector4 &v1, const Vector4 &v2, float d);
static void OrthoNormalize(Vector4 &normal, Vector4 &tangent); // Gram-Schmidt Orthonormalization
};
static_assert(std::is_pod<Vector4>::value, "kraken::Vector4 must be a POD type.");
} // namespace kraken
namespace std {
template<>
struct hash<kraken::Vector4> {
public:
size_t operator()(const kraken::Vector4 &s) const
{
size_t h1 = hash<float>()(s.x);
size_t h2 = hash<float>()(s.y);
size_t h3 = hash<float>()(s.z);
size_t h4 = hash<float>()(s.w);
return h1 ^ (h2 << 1) ^ (h3 << 2) ^ (h4 << 3);
}
};
} // namespace std
#endif // KRAKEN_VECTOR4_H