Apply formating to all files. CLeanup
This commit is contained in:
parent
43e374f269
commit
744c01c5d0
928 changed files with 14515 additions and 21480 deletions
|
|
@ -4,43 +4,43 @@
|
|||
#include "Ray.hpp"
|
||||
|
||||
namespace tp {
|
||||
class Camera {
|
||||
Vec3F mPos, mTarget, mUp;
|
||||
halnf mFOV = (halnf) (PI) / 4;
|
||||
halnf mNear = 0.001f;
|
||||
halnf mFar = 100.f;
|
||||
halnf mRatio = 1.f;
|
||||
class Camera {
|
||||
Vec3F mPos, mTarget, mUp;
|
||||
halnf mFOV = (halnf) (PI) / 4;
|
||||
halnf mNear = 0.001f;
|
||||
halnf mFar = 100.f;
|
||||
halnf mRatio = 1.f;
|
||||
|
||||
public:
|
||||
Camera();
|
||||
~Camera() = default;
|
||||
public:
|
||||
Camera();
|
||||
~Camera() = default;
|
||||
|
||||
void setRatio(halnf ratio);
|
||||
void setFOV(halnf fov);
|
||||
void setFar(halnf far);
|
||||
void setRatio(halnf ratio);
|
||||
void setFOV(halnf fov);
|
||||
void setFar(halnf far);
|
||||
|
||||
[[nodiscard]] const Vec3F& getPos() const;
|
||||
[[nodiscard]] const Vec3F& getTarget() const;
|
||||
[[nodiscard]] Vec3F getForward() const;
|
||||
[[nodiscard]] const Vec3F& getUp() const;
|
||||
[[nodiscard]] halnf getRatio() const;
|
||||
[[nodiscard]] halnf getFOV() const;
|
||||
[[nodiscard]] halnf getFar() const;
|
||||
[[nodiscard]] halnf getNear() const;
|
||||
[[nodiscard]] const Vec3F& getPos() const;
|
||||
[[nodiscard]] const Vec3F& getTarget() const;
|
||||
[[nodiscard]] Vec3F getForward() const;
|
||||
[[nodiscard]] const Vec3F& getUp() const;
|
||||
[[nodiscard]] halnf getRatio() const;
|
||||
[[nodiscard]] halnf getFOV() const;
|
||||
[[nodiscard]] halnf getFar() const;
|
||||
[[nodiscard]] halnf getNear() const;
|
||||
|
||||
public:
|
||||
void lookAtPoint(const Vec3F& aTarget, const Vec3F& aPos, Vec3F aUp);
|
||||
void rotate(halnf anglex, halnf angleY);
|
||||
void move(Vec2F aPos, Vec2F aPrevPos);
|
||||
void zoom(halnf ratio);
|
||||
void offset_target(halnf val);
|
||||
public:
|
||||
void lookAtPoint(const Vec3F& aTarget, const Vec3F& aPos, Vec3F aUp);
|
||||
void rotate(halnf anglex, halnf angleY);
|
||||
void move(Vec2F aPos, Vec2F aPrevPos);
|
||||
void zoom(halnf ratio);
|
||||
void offset_target(halnf val);
|
||||
|
||||
public:
|
||||
[[nodiscard]] Mat4F calculateTransformationMatrix();
|
||||
[[nodiscard]] Mat<halnf, 4, 4> calculateProjectionMatrix() const;
|
||||
[[nodiscard]] Mat<halnf, 4, 4> calculateViewMatrix();
|
||||
[[nodiscard]] Vec3F project(Vec2F normalized);
|
||||
[[nodiscard]] Vec2F project(const Vec3F& world);
|
||||
[[nodiscard]] static Vec2F project(const tp::Vec3F& world, const tp::Mat4F& viewMat, const tp::Mat4F& projMat);
|
||||
};
|
||||
public:
|
||||
[[nodiscard]] Mat4F calculateTransformationMatrix();
|
||||
[[nodiscard]] Mat<halnf, 4, 4> calculateProjectionMatrix() const;
|
||||
[[nodiscard]] Mat<halnf, 4, 4> calculateViewMatrix();
|
||||
[[nodiscard]] Vec3F project(Vec2F normalized);
|
||||
[[nodiscard]] Vec2F project(const Vec3F& world);
|
||||
[[nodiscard]] static Vec2F project(const tp::Vec3F& world, const tp::Mat4F& viewMat, const tp::Mat4F& projMat);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,90 +1,100 @@
|
|||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class RGB;
|
||||
class HSV;
|
||||
|
||||
class RGB {
|
||||
public:
|
||||
RGB();
|
||||
RGB(flt4 pr, flt4 pg, flt4 pb);
|
||||
RGB(flt4 val);
|
||||
|
||||
public:
|
||||
void set(flt4 pr, flt4 pg, flt4 pb);
|
||||
operator HSV() const;
|
||||
|
||||
public:
|
||||
halnf r, g, b;
|
||||
};
|
||||
|
||||
class HSV {
|
||||
public:
|
||||
HSV();
|
||||
HSV(flt4 ph, flt4 ps, flt4 pv);
|
||||
|
||||
public:
|
||||
void set(flt4 ph, flt4 ps, flt4 pv);
|
||||
operator RGB() const;
|
||||
|
||||
public:
|
||||
halnf h, s, v;
|
||||
};
|
||||
|
||||
class RGBA {
|
||||
public:
|
||||
RGBA() : r(0), g(0), b(0), a(0) {}
|
||||
|
||||
RGBA(flt4 val) : rgbs(val), a(val) {}
|
||||
|
||||
RGBA(const RGB& RGBs, flt4 val) : rgbs(RGBs), a(val) {}
|
||||
|
||||
RGBA(flt4 pr, flt4 pg, flt4 pb, flt4 pa) : rgbs(pr, pg, pb), a(pa) {}
|
||||
|
||||
public:
|
||||
RGBA& operator=(const HSV& in) {
|
||||
rgbs = in;
|
||||
a = 1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
RGBA operator-(const RGBA& in) const {
|
||||
const auto nr = tp::clamp(r - in.r, 0.f, 1.f);
|
||||
const auto ng = tp::clamp(g - in.g, 0.f, 1.f);
|
||||
const auto nb = tp::clamp(b - in.b, 0.f, 1.f);
|
||||
const auto na = tp::clamp(a - in.a, 0.f, 1.f);
|
||||
return {nr, ng, nb, na};
|
||||
}
|
||||
|
||||
RGBA operator+(const RGBA& in) const {
|
||||
const auto nr = tp::clamp(r + in.r, 0.f, 1.f);
|
||||
const auto ng = tp::clamp(g + in.g, 0.f, 1.f);
|
||||
const auto nb = tp::clamp(b + in.b, 0.f, 1.f);
|
||||
const auto na = tp::clamp(a + in.a, 0.f, 1.f);
|
||||
return {nr, ng, nb, na};
|
||||
}
|
||||
|
||||
bool operator==(const RGBA& in) const { return r == in.r && g == in.g && b == in.b && a == in.a; }
|
||||
|
||||
public:
|
||||
flt4 a;
|
||||
|
||||
union {
|
||||
RGB rgbs;
|
||||
|
||||
struct {
|
||||
flt4 r;
|
||||
flt4 g;
|
||||
flt4 b;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
class HSVA {
|
||||
HSV rgbs;
|
||||
flt4 a = 1.f;
|
||||
};
|
||||
}
|
||||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class RGB;
|
||||
class HSV;
|
||||
|
||||
class RGB {
|
||||
public:
|
||||
RGB();
|
||||
RGB(flt4 pr, flt4 pg, flt4 pb);
|
||||
RGB(flt4 val);
|
||||
|
||||
public:
|
||||
void set(flt4 pr, flt4 pg, flt4 pb);
|
||||
operator HSV() const;
|
||||
|
||||
public:
|
||||
halnf r, g, b;
|
||||
};
|
||||
|
||||
class HSV {
|
||||
public:
|
||||
HSV();
|
||||
HSV(flt4 ph, flt4 ps, flt4 pv);
|
||||
|
||||
public:
|
||||
void set(flt4 ph, flt4 ps, flt4 pv);
|
||||
operator RGB() const;
|
||||
|
||||
public:
|
||||
halnf h, s, v;
|
||||
};
|
||||
|
||||
class RGBA {
|
||||
public:
|
||||
RGBA() :
|
||||
r(0),
|
||||
g(0),
|
||||
b(0),
|
||||
a(0) {}
|
||||
|
||||
RGBA(flt4 val) :
|
||||
rgbs(val),
|
||||
a(val) {}
|
||||
|
||||
RGBA(const RGB& RGBs, flt4 val) :
|
||||
rgbs(RGBs),
|
||||
a(val) {}
|
||||
|
||||
RGBA(flt4 pr, flt4 pg, flt4 pb, flt4 pa) :
|
||||
rgbs(pr, pg, pb),
|
||||
a(pa) {}
|
||||
|
||||
public:
|
||||
RGBA& operator=(const HSV& in) {
|
||||
rgbs = in;
|
||||
a = 1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
RGBA operator-(const RGBA& in) const {
|
||||
const auto nr = tp::clamp(r - in.r, 0.f, 1.f);
|
||||
const auto ng = tp::clamp(g - in.g, 0.f, 1.f);
|
||||
const auto nb = tp::clamp(b - in.b, 0.f, 1.f);
|
||||
const auto na = tp::clamp(a - in.a, 0.f, 1.f);
|
||||
return { nr, ng, nb, na };
|
||||
}
|
||||
|
||||
RGBA operator+(const RGBA& in) const {
|
||||
const auto nr = tp::clamp(r + in.r, 0.f, 1.f);
|
||||
const auto ng = tp::clamp(g + in.g, 0.f, 1.f);
|
||||
const auto nb = tp::clamp(b + in.b, 0.f, 1.f);
|
||||
const auto na = tp::clamp(a + in.a, 0.f, 1.f);
|
||||
return { nr, ng, nb, na };
|
||||
}
|
||||
|
||||
bool operator==(const RGBA& in) const { return r == in.r && g == in.g && b == in.b && a == in.a; }
|
||||
|
||||
public:
|
||||
flt4 a;
|
||||
|
||||
union {
|
||||
RGB rgbs;
|
||||
|
||||
struct {
|
||||
flt4 r;
|
||||
flt4 g;
|
||||
flt4 b;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
class HSVA {
|
||||
HSV rgbs;
|
||||
flt4 a = 1.f;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,57 +1,57 @@
|
|||
#pragma once
|
||||
|
||||
#include "MathCommon.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
struct ComplexCart {
|
||||
typedef alnf Number;
|
||||
Number r = 0;
|
||||
Number i = 0;
|
||||
|
||||
public:
|
||||
[[nodiscard]] ComplexCart operator*(const ComplexCart& in) const { return {in.r * r - in.i * i, in.r * i + in.i * r}; }
|
||||
|
||||
[[nodiscard]] ComplexCart operator*(Number in) const { return {in * r, in * i}; }
|
||||
|
||||
[[nodiscard]] ComplexCart operator+(const ComplexCart& in) const { return {in.r + r, in.i + i}; }
|
||||
|
||||
[[nodiscard]] ComplexCart operator-(const ComplexCart& in) const { return {in.r - r, in.i - i}; }
|
||||
|
||||
[[nodiscard]] Number mod() const { return tp::sqrt(i * i + r * r); }
|
||||
|
||||
[[nodiscard]] Number arg() const { return atan2(r, i); }
|
||||
|
||||
[[nodiscard]] Number norm2() const { return this->operator*(conjugate()).r; }
|
||||
|
||||
[[nodiscard]] ComplexCart reciprocal() const { return {r / norm2(), -i / norm2()}; }
|
||||
|
||||
void set(Number mod, Number arg) {
|
||||
r = mod * cos(arg);
|
||||
i = mod * sin(arg);
|
||||
}
|
||||
|
||||
bool operator==(const ComplexCart& in) const { return in.r == r && in.i == i; }
|
||||
|
||||
[[nodiscard]] ComplexCart conjugate() const { return {r, -i}; }
|
||||
};
|
||||
|
||||
struct ComplexRad {
|
||||
typedef alnf Number;
|
||||
Number r = 0;
|
||||
Number a = 0;
|
||||
|
||||
public:
|
||||
[[nodiscard]] ComplexRad operator*(const ComplexRad& in) const { return {r * in.r, a + in.a}; }
|
||||
|
||||
[[nodiscard]] ComplexRad operator*(Number in) const { return {r * in, a}; }
|
||||
|
||||
[[nodiscard]] ComplexCart cart() const {
|
||||
ComplexCart out;
|
||||
out.set(r, a);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool operator==(const ComplexRad& in) const { return in.r == r && in.a == a; }
|
||||
};
|
||||
}
|
||||
#pragma once
|
||||
|
||||
#include "MathCommon.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
struct ComplexCart {
|
||||
typedef alnf Number;
|
||||
Number r = 0;
|
||||
Number i = 0;
|
||||
|
||||
public:
|
||||
[[nodiscard]] ComplexCart operator*(const ComplexCart& in) const { return { in.r * r - in.i * i, in.r * i + in.i * r }; }
|
||||
|
||||
[[nodiscard]] ComplexCart operator*(Number in) const { return { in * r, in * i }; }
|
||||
|
||||
[[nodiscard]] ComplexCart operator+(const ComplexCart& in) const { return { in.r + r, in.i + i }; }
|
||||
|
||||
[[nodiscard]] ComplexCart operator-(const ComplexCart& in) const { return { in.r - r, in.i - i }; }
|
||||
|
||||
[[nodiscard]] Number mod() const { return tp::sqrt(i * i + r * r); }
|
||||
|
||||
[[nodiscard]] Number arg() const { return atan2(r, i); }
|
||||
|
||||
[[nodiscard]] Number norm2() const { return this->operator*(conjugate()).r; }
|
||||
|
||||
[[nodiscard]] ComplexCart reciprocal() const { return { r / norm2(), -i / norm2() }; }
|
||||
|
||||
void set(Number mod, Number arg) {
|
||||
r = mod * cos(arg);
|
||||
i = mod * sin(arg);
|
||||
}
|
||||
|
||||
bool operator==(const ComplexCart& in) const { return in.r == r && in.i == i; }
|
||||
|
||||
[[nodiscard]] ComplexCart conjugate() const { return { r, -i }; }
|
||||
};
|
||||
|
||||
struct ComplexRad {
|
||||
typedef alnf Number;
|
||||
Number r = 0;
|
||||
Number a = 0;
|
||||
|
||||
public:
|
||||
[[nodiscard]] ComplexRad operator*(const ComplexRad& in) const { return { r * in.r, a + in.a }; }
|
||||
|
||||
[[nodiscard]] ComplexRad operator*(Number in) const { return { r * in, a }; }
|
||||
|
||||
[[nodiscard]] ComplexCart cart() const {
|
||||
ComplexCart out;
|
||||
out.set(r, a);
|
||||
return out;
|
||||
}
|
||||
|
||||
bool operator==(const ComplexRad& in) const { return in.r == r && in.a == a; }
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,34 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template <typename Type>
|
||||
bool intersectLines2D(const Vec2<Type>& p1, const Vec2<Type>& p2, const Vec2<Type>& v1, const Vec2<Type>& v2, Vec2<Type>* out) {
|
||||
auto a1 = p2.x - p1.x;
|
||||
auto a2 = p2.y - p1.y;
|
||||
|
||||
auto b1 = v2.x - v1.x;
|
||||
auto b2 = v2.y - v1.y;
|
||||
|
||||
auto c1 = v1.x - p1.x;
|
||||
auto c2 = v1.y - p1.y;
|
||||
|
||||
auto det = a2 * b1 - a1 * b2;
|
||||
|
||||
auto t1 = ( -b2 * c1 + b1 * c2 ) / det;
|
||||
auto t2 = ( -a2 * c1 + a1 * c2 ) / det;
|
||||
|
||||
if (t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) {
|
||||
if (out != nullptr) {
|
||||
out->x = p1.x + (a1 * t1);
|
||||
out->y = p1.y + (a2 * t1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template <typename Type>
|
||||
bool intersectLines2D(const Vec2<Type>& p1, const Vec2<Type>& p2, const Vec2<Type>& v1, const Vec2<Type>& v2, Vec2<Type>* out) {
|
||||
auto a1 = p2.x - p1.x;
|
||||
auto a2 = p2.y - p1.y;
|
||||
|
||||
auto b1 = v2.x - v1.x;
|
||||
auto b2 = v2.y - v1.y;
|
||||
|
||||
auto c1 = v1.x - p1.x;
|
||||
auto c2 = v1.y - p1.y;
|
||||
|
||||
auto det = a2 * b1 - a1 * b2;
|
||||
|
||||
auto t1 = (-b2 * c1 + b1 * c2) / det;
|
||||
auto t2 = (-a2 * c1 + a1 * c2) / det;
|
||||
|
||||
if (t1 >= 0 && t1 <= 1 && t2 >= 0 && t2 <= 1) {
|
||||
if (out != nullptr) {
|
||||
out->x = p1.x + (a1 * t1);
|
||||
out->y = p1.y + (a2 * t1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
1434
Math/public/Mat.hpp
1434
Math/public/Mat.hpp
File diff suppressed because it is too large
Load diff
|
|
@ -1,20 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class Ray {
|
||||
public:
|
||||
Ray(const Vec3F& Dir, const Vec3F& Pos);
|
||||
Ray() = default;
|
||||
|
||||
public:
|
||||
Vec3F dir;
|
||||
Vec3F pos;
|
||||
|
||||
public:
|
||||
~Ray() = default;
|
||||
};
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class Ray {
|
||||
public:
|
||||
Ray(const Vec3F& Dir, const Vec3F& Pos);
|
||||
Ray() = default;
|
||||
|
||||
public:
|
||||
Vec3F dir;
|
||||
Vec3F pos;
|
||||
|
||||
public:
|
||||
~Ray() = default;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -1,274 +1,240 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
#include "Intersections.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template <typename Type> class Rect;
|
||||
using RectF = Rect<halnf>;
|
||||
using RectI = Rect<halni>;
|
||||
|
||||
template <typename Type>
|
||||
class Rect {
|
||||
public:
|
||||
Rect() {}
|
||||
|
||||
explicit Rect(Type val) {
|
||||
this->pos = val;
|
||||
this->size = val;
|
||||
}
|
||||
|
||||
template <typename ConversionType>
|
||||
explicit Rect(const Rect<ConversionType>& rec) {
|
||||
this->pos = rec.pos;
|
||||
this->size = rec.size;
|
||||
}
|
||||
|
||||
Rect(const Vec2<Type>& pos, const Vec2<Type>& size) {
|
||||
this->pos = pos;
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
Rect(Type aPosX, Type posy, Type aSizeX, Type aSizeY) {
|
||||
pos.assign(aPosX, posy);
|
||||
size.assign(aSizeX, aSizeY);
|
||||
}
|
||||
|
||||
// assign
|
||||
template <typename InType>
|
||||
Rect<Type>& assign(InType p1x, InType p1y, InType p2x, InType p2y) {
|
||||
pos.assign(p1x, p1y);
|
||||
size.assign(p2x, p2y);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// assign
|
||||
template <typename InType>
|
||||
Rect<Type>& assign(const Vec2<InType>& pos, const Vec2<InType>& size) {
|
||||
this->pos.assign(pos.x, pos.y);
|
||||
this->size.assign(size.x, size.y);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// conversion
|
||||
template <typename ConversionType>
|
||||
Rect<Type>& operator=(const Rect<ConversionType>& rect) {
|
||||
pos = rect.pos;
|
||||
size = rect.size;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(Rect<Type>& rect) const {
|
||||
return (pos == rect.pos && size == rect.size);
|
||||
}
|
||||
|
||||
bool isEnclosedIn(const Rect<Type>& rect, bool aParent = false) const {
|
||||
if (aParent) {
|
||||
return (pos.x + size.x <= rect.size.x && pos.y + size.y <= rect.size.y &&
|
||||
pos.x >= 0 && pos.y >= 0);
|
||||
}
|
||||
*(Vec2<Type>*)(&pos) -= rect.pos;
|
||||
bool ret = this->isEnclosedIn(rect, true);
|
||||
*(Vec2<Type>*)(&pos) += rect.pos;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void calcIntersection(Rect<Type>& in, Rect<Type>& out) const {
|
||||
if (isOverlap(in)) {
|
||||
out = *this;
|
||||
for (char i = 0; i < 2; i++) {
|
||||
clamp(out.pos[i], in.pos[i], in.pos[i] + in.size[i]);
|
||||
Type p2 = pos[i] + size[i];
|
||||
clamp(p2, in.pos[i], in.pos[i] + in.size[i]);
|
||||
out.size[i] = p2 - out.pos[i];
|
||||
}
|
||||
}
|
||||
else {
|
||||
out.size.assign(0, 0);
|
||||
out.pos.assign(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// argument isInside
|
||||
bool isInside(const Vec2<Type>& p) const {
|
||||
return isInside(p.x, p.y);
|
||||
}
|
||||
|
||||
bool isInside(Type x, Type y) const {
|
||||
return (pos.x < x&& pos.y < y&& pos.x + size.x > x&& pos.y + size.y > y);
|
||||
}
|
||||
|
||||
inline Vec2<Type> sizeVec() const {
|
||||
return Vec2<Type>(size.x, size.y);
|
||||
}
|
||||
|
||||
inline Vec2<Type> sizeVecW() const {
|
||||
return Vec2<Type>(size.x + pos.x, size.y + pos.y);
|
||||
}
|
||||
|
||||
void invertY(Type scr_y) {
|
||||
pos.y = scr_y - pos.y - size.y;
|
||||
}
|
||||
|
||||
void move(Type dx, Type dy) {
|
||||
pos.x += dx;
|
||||
pos.y += dy;
|
||||
}
|
||||
|
||||
Rect<Type>& scaleFromCenter(tp::halnf fac, bool add = false) {
|
||||
if (add) {
|
||||
pos += fac;
|
||||
size -= fac * 2;
|
||||
}
|
||||
else {
|
||||
auto new_size = size * fac;
|
||||
pos = pos - (new_size - size) / 2;
|
||||
size = new_size;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2<Type> p1() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
Vec2<Type> p3() {
|
||||
return pos + size;
|
||||
}
|
||||
|
||||
Vec2<Type> p2() {
|
||||
return { pos.x, pos.y + size.y };
|
||||
}
|
||||
|
||||
Vec2<Type> p4() {
|
||||
return { pos.x + size.x, pos.y };
|
||||
}
|
||||
|
||||
inline bool isAbove(const Rect<Type>& rect) const {
|
||||
return (pos.y + size.y < rect.pos.y);
|
||||
}
|
||||
|
||||
inline bool isBellow(const Rect<Type>& rect) const {
|
||||
return (rect.pos.y + rect.size.y < pos.y);
|
||||
}
|
||||
|
||||
inline bool isRight(const Rect<Type>& rect) const {
|
||||
return (pos.x + size.x < rect.pos.x);
|
||||
}
|
||||
|
||||
inline bool isLeft(const Rect<Type>& rect) const {
|
||||
return (rect.pos.x + rect.size.x < pos.x);
|
||||
}
|
||||
|
||||
inline bool isIntersectsY(const Rect<Type>& in) const {
|
||||
if (INRANGE(in.pos.x, pos.x, pos.x + size.x)) return true;
|
||||
if (INRANGE(pos.x, in.pos.x, in.pos.x + in.size.x)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isIntersectX(const Rect<Type>& rect) const {
|
||||
if (INRANGE(rect.pos.y, pos.y, pos.y + size.y)) return true;
|
||||
if (INRANGE(pos.y, rect.pos.y, rect.pos.y + rect.size.y)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isOverlap(const Rect<Type>& rect) const {
|
||||
return (isIntersectX(rect) && isIntersectsY(rect));
|
||||
}
|
||||
|
||||
void clamp(const Rect<Type>& bounds) {
|
||||
Vec2<Type> p3(pos + size);
|
||||
Vec2<Type> max = bounds.pos + bounds.size;
|
||||
|
||||
pos.clamp(bounds.pos, max);
|
||||
p3.clamp(bounds.pos, max);
|
||||
|
||||
size = p3 - pos;
|
||||
}
|
||||
|
||||
// if only one point isInside
|
||||
bool clampOutside(Vec2<Type>& v1, Vec2<Type>& v2) {
|
||||
bool const in1 = isInside(v1);
|
||||
bool const in2 = isInside(v2);
|
||||
if (!in1 && !in2) return false;
|
||||
|
||||
if (in1) {
|
||||
if (!intersectLines2D(p2(), p3(), v1, v2, &v1)) {
|
||||
if (!intersectLines2D(p1(), p4(), v1, v2, &v1)) {
|
||||
if (!intersectLines2D(p1(), p2(), v1, v2, &v1)) {
|
||||
intersectLines2D(p3(), p4(), v1, v2, &v1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!intersectLines2D(p2(), p3(), v1, v2, &v2)) {
|
||||
if (!intersectLines2D(p1(), p4(), v1, v2, &v2)) {
|
||||
if (!intersectLines2D(p1(), p2(), v1, v2, &v2)) {
|
||||
intersectLines2D(p3(), p4(), v1, v2, &v2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uhalni clampInside(Vec2<Type>& v1, Vec2<Type>& v2) {
|
||||
bool const in1 = isInside(v1);
|
||||
bool const in2 = isInside(v2);
|
||||
|
||||
if (in1 && in2) return 2;
|
||||
|
||||
Vec2<Type> v1copy = v1;
|
||||
Vec2<Type> v2copy = v2;
|
||||
|
||||
if (in1 || in2) {
|
||||
if (!intersectLines2D(p2(), p3(), v1copy, v2copy, &v2)) {
|
||||
if (!intersectLines2D(p1(), p4(), v1copy, v2copy, &v2)) {
|
||||
if (!intersectLines2D(p1(), p2(), v1copy, v2copy, &v2)) {
|
||||
intersectLines2D(p3(), p4(), v1copy, v2copy, &v2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEBUG_ASSERT(0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename ConversionType>
|
||||
Rect<Type> operator*(ConversionType val) {
|
||||
Rect<Type> out;
|
||||
out.pos = pos * val;
|
||||
out.size = size * val;
|
||||
return out;
|
||||
}
|
||||
|
||||
Vec2<Type> center() {
|
||||
return pos + size / 2.f;
|
||||
}
|
||||
|
||||
public:
|
||||
union {
|
||||
Vec2<Type> v1;
|
||||
Vec2<Type> pos;
|
||||
struct {
|
||||
Type x;
|
||||
Type y;
|
||||
};
|
||||
};
|
||||
|
||||
union {
|
||||
Vec2<Type> v2;
|
||||
Vec2<Type> size;
|
||||
struct {
|
||||
Type z;
|
||||
Type w;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
#include "Intersections.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
template <typename Type>
|
||||
class Rect;
|
||||
using RectF = Rect<halnf>;
|
||||
using RectI = Rect<halni>;
|
||||
|
||||
template <typename Type>
|
||||
class Rect {
|
||||
public:
|
||||
Rect() {}
|
||||
|
||||
explicit Rect(Type val) {
|
||||
this->pos = val;
|
||||
this->size = val;
|
||||
}
|
||||
|
||||
template <typename ConversionType>
|
||||
explicit Rect(const Rect<ConversionType>& rec) {
|
||||
this->pos = rec.pos;
|
||||
this->size = rec.size;
|
||||
}
|
||||
|
||||
Rect(const Vec2<Type>& pos, const Vec2<Type>& size) {
|
||||
this->pos = pos;
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
Rect(Type aPosX, Type posy, Type aSizeX, Type aSizeY) {
|
||||
pos.assign(aPosX, posy);
|
||||
size.assign(aSizeX, aSizeY);
|
||||
}
|
||||
|
||||
// assign
|
||||
template <typename InType>
|
||||
Rect<Type>& assign(InType p1x, InType p1y, InType p2x, InType p2y) {
|
||||
pos.assign(p1x, p1y);
|
||||
size.assign(p2x, p2y);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// assign
|
||||
template <typename InType>
|
||||
Rect<Type>& assign(const Vec2<InType>& pos, const Vec2<InType>& size) {
|
||||
this->pos.assign(pos.x, pos.y);
|
||||
this->size.assign(size.x, size.y);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// conversion
|
||||
template <typename ConversionType>
|
||||
Rect<Type>& operator=(const Rect<ConversionType>& rect) {
|
||||
pos = rect.pos;
|
||||
size = rect.size;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(Rect<Type>& rect) const { return (pos == rect.pos && size == rect.size); }
|
||||
|
||||
bool isEnclosedIn(const Rect<Type>& rect, bool aParent = false) const {
|
||||
if (aParent) {
|
||||
return (pos.x + size.x <= rect.size.x && pos.y + size.y <= rect.size.y && pos.x >= 0 && pos.y >= 0);
|
||||
}
|
||||
*(Vec2<Type>*) (&pos) -= rect.pos;
|
||||
bool ret = this->isEnclosedIn(rect, true);
|
||||
*(Vec2<Type>*) (&pos) += rect.pos;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void calcIntersection(Rect<Type>& in, Rect<Type>& out) const {
|
||||
if (isOverlap(in)) {
|
||||
out = *this;
|
||||
for (char i = 0; i < 2; i++) {
|
||||
clamp(out.pos[i], in.pos[i], in.pos[i] + in.size[i]);
|
||||
Type p2 = pos[i] + size[i];
|
||||
clamp(p2, in.pos[i], in.pos[i] + in.size[i]);
|
||||
out.size[i] = p2 - out.pos[i];
|
||||
}
|
||||
} else {
|
||||
out.size.assign(0, 0);
|
||||
out.pos.assign(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// argument isInside
|
||||
bool isInside(const Vec2<Type>& p) const { return isInside(p.x, p.y); }
|
||||
|
||||
bool isInside(Type x, Type y) const { return (pos.x < x && pos.y < y && pos.x + size.x > x && pos.y + size.y > y); }
|
||||
|
||||
inline Vec2<Type> sizeVec() const { return Vec2<Type>(size.x, size.y); }
|
||||
|
||||
inline Vec2<Type> sizeVecW() const { return Vec2<Type>(size.x + pos.x, size.y + pos.y); }
|
||||
|
||||
void invertY(Type scr_y) { pos.y = scr_y - pos.y - size.y; }
|
||||
|
||||
void move(Type dx, Type dy) {
|
||||
pos.x += dx;
|
||||
pos.y += dy;
|
||||
}
|
||||
|
||||
Rect<Type>& scaleFromCenter(tp::halnf fac, bool add = false) {
|
||||
if (add) {
|
||||
pos += fac;
|
||||
size -= fac * 2;
|
||||
} else {
|
||||
auto new_size = size * fac;
|
||||
pos = pos - (new_size - size) / 2;
|
||||
size = new_size;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2<Type> p1() { return pos; }
|
||||
|
||||
Vec2<Type> p3() { return pos + size; }
|
||||
|
||||
Vec2<Type> p2() { return { pos.x, pos.y + size.y }; }
|
||||
|
||||
Vec2<Type> p4() { return { pos.x + size.x, pos.y }; }
|
||||
|
||||
inline bool isAbove(const Rect<Type>& rect) const { return (pos.y + size.y < rect.pos.y); }
|
||||
|
||||
inline bool isBellow(const Rect<Type>& rect) const { return (rect.pos.y + rect.size.y < pos.y); }
|
||||
|
||||
inline bool isRight(const Rect<Type>& rect) const { return (pos.x + size.x < rect.pos.x); }
|
||||
|
||||
inline bool isLeft(const Rect<Type>& rect) const { return (rect.pos.x + rect.size.x < pos.x); }
|
||||
|
||||
inline bool isIntersectsY(const Rect<Type>& in) const {
|
||||
if (INRANGE(in.pos.x, pos.x, pos.x + size.x)) return true;
|
||||
if (INRANGE(pos.x, in.pos.x, in.pos.x + in.size.x)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool isIntersectX(const Rect<Type>& rect) const {
|
||||
if (INRANGE(rect.pos.y, pos.y, pos.y + size.y)) return true;
|
||||
if (INRANGE(pos.y, rect.pos.y, rect.pos.y + rect.size.y)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isOverlap(const Rect<Type>& rect) const { return (isIntersectX(rect) && isIntersectsY(rect)); }
|
||||
|
||||
void clamp(const Rect<Type>& bounds) {
|
||||
Vec2<Type> p3(pos + size);
|
||||
Vec2<Type> max = bounds.pos + bounds.size;
|
||||
|
||||
pos.clamp(bounds.pos, max);
|
||||
p3.clamp(bounds.pos, max);
|
||||
|
||||
size = p3 - pos;
|
||||
}
|
||||
|
||||
// if only one point isInside
|
||||
bool clampOutside(Vec2<Type>& v1, Vec2<Type>& v2) {
|
||||
bool const in1 = isInside(v1);
|
||||
bool const in2 = isInside(v2);
|
||||
if (!in1 && !in2) return false;
|
||||
|
||||
if (in1) {
|
||||
if (!intersectLines2D(p2(), p3(), v1, v2, &v1)) {
|
||||
if (!intersectLines2D(p1(), p4(), v1, v2, &v1)) {
|
||||
if (!intersectLines2D(p1(), p2(), v1, v2, &v1)) {
|
||||
intersectLines2D(p3(), p4(), v1, v2, &v1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!intersectLines2D(p2(), p3(), v1, v2, &v2)) {
|
||||
if (!intersectLines2D(p1(), p4(), v1, v2, &v2)) {
|
||||
if (!intersectLines2D(p1(), p2(), v1, v2, &v2)) {
|
||||
intersectLines2D(p3(), p4(), v1, v2, &v2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uhalni clampInside(Vec2<Type>& v1, Vec2<Type>& v2) {
|
||||
bool const in1 = isInside(v1);
|
||||
bool const in2 = isInside(v2);
|
||||
|
||||
if (in1 && in2) return 2;
|
||||
|
||||
Vec2<Type> v1copy = v1;
|
||||
Vec2<Type> v2copy = v2;
|
||||
|
||||
if (in1 || in2) {
|
||||
if (!intersectLines2D(p2(), p3(), v1copy, v2copy, &v2)) {
|
||||
if (!intersectLines2D(p1(), p4(), v1copy, v2copy, &v2)) {
|
||||
if (!intersectLines2D(p1(), p2(), v1copy, v2copy, &v2)) {
|
||||
intersectLines2D(p3(), p4(), v1copy, v2copy, &v2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEBUG_ASSERT(0)
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename ConversionType>
|
||||
Rect<Type> operator*(ConversionType val) {
|
||||
Rect<Type> out;
|
||||
out.pos = pos * val;
|
||||
out.size = size * val;
|
||||
return out;
|
||||
}
|
||||
|
||||
Vec2<Type> center() { return pos + size / 2.f; }
|
||||
|
||||
public:
|
||||
union {
|
||||
Vec2<Type> v1;
|
||||
Vec2<Type> pos;
|
||||
struct {
|
||||
Type x;
|
||||
Type y;
|
||||
};
|
||||
};
|
||||
|
||||
union {
|
||||
Vec2<Type> v2;
|
||||
Vec2<Type> size;
|
||||
struct {
|
||||
Type z;
|
||||
Type w;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -1,53 +1,49 @@
|
|||
#pragma once
|
||||
|
||||
#include "Buffer.hpp"
|
||||
#include "Camera.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class TrigCache {
|
||||
static Vec3F gHitPos;
|
||||
|
||||
public:
|
||||
ualni mP1, mP2, mP3;
|
||||
Vec3F mEdgeP1P2, mEdgeP1P3;
|
||||
Vec3F mNormal;
|
||||
Vec3F mOrigin;
|
||||
|
||||
public:
|
||||
TrigCache();
|
||||
TrigCache(const TrigCache& in);
|
||||
TrigCache(ualni v1, ualni v2, ualni v3);
|
||||
|
||||
public:
|
||||
static const Vec3F& getHitPos();
|
||||
[[nodiscard]] const Vec3F& getNormal() const;
|
||||
|
||||
public:
|
||||
void updateCache(const Buffer<Vec3F>& points);
|
||||
[[nodiscard]] bool castRay(const Ray& ray) const;
|
||||
};
|
||||
|
||||
struct Topology {
|
||||
Vec3F Origin = {0, 0, 0};
|
||||
mat3f Basis = {
|
||||
{1, 0, 0},
|
||||
{0, 1, 0},
|
||||
{0, 0, 1}
|
||||
};
|
||||
|
||||
Buffer<Vec3F> Points;
|
||||
Buffer<Vec3F> Normals;
|
||||
Buffer<Vec3I> Indexes;
|
||||
};
|
||||
|
||||
struct TopologyCache {
|
||||
const Topology* Source = nullptr;
|
||||
|
||||
Buffer<Vec3F> TransformedPoints;
|
||||
Buffer<Vec3F> TransformedNormals;
|
||||
Buffer<TrigCache> TrigCaches;
|
||||
|
||||
void updateCache();
|
||||
};
|
||||
}
|
||||
#pragma once
|
||||
|
||||
#include "Buffer.hpp"
|
||||
#include "Camera.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class TrigCache {
|
||||
static Vec3F gHitPos;
|
||||
|
||||
public:
|
||||
ualni mP1, mP2, mP3;
|
||||
Vec3F mEdgeP1P2, mEdgeP1P3;
|
||||
Vec3F mNormal;
|
||||
Vec3F mOrigin;
|
||||
|
||||
public:
|
||||
TrigCache();
|
||||
TrigCache(const TrigCache& in);
|
||||
TrigCache(ualni v1, ualni v2, ualni v3);
|
||||
|
||||
public:
|
||||
static const Vec3F& getHitPos();
|
||||
[[nodiscard]] const Vec3F& getNormal() const;
|
||||
|
||||
public:
|
||||
void updateCache(const Buffer<Vec3F>& points);
|
||||
[[nodiscard]] bool castRay(const Ray& ray) const;
|
||||
};
|
||||
|
||||
struct Topology {
|
||||
Vec3F Origin = { 0, 0, 0 };
|
||||
mat3f Basis = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
|
||||
|
||||
Buffer<Vec3F> Points;
|
||||
Buffer<Vec3F> Normals;
|
||||
Buffer<Vec3I> Indexes;
|
||||
};
|
||||
|
||||
struct TopologyCache {
|
||||
const Topology* Source = nullptr;
|
||||
|
||||
Buffer<Vec3F> TransformedPoints;
|
||||
Buffer<Vec3F> TransformedNormals;
|
||||
Buffer<TrigCache> TrigCaches;
|
||||
|
||||
void updateCache();
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,25 @@
|
|||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class Trig {
|
||||
public:
|
||||
Trig();
|
||||
Trig(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2);
|
||||
|
||||
public:
|
||||
Vec3F p1;
|
||||
Vec3F p2;
|
||||
Vec3F p3;
|
||||
|
||||
public:
|
||||
void assign(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2);
|
||||
void normal(Vec3F& dir) const;
|
||||
bool rayHit(class Ray& ray, Vec3F& HitPos) const;
|
||||
|
||||
public:
|
||||
~Trig();
|
||||
};
|
||||
#pragma once
|
||||
|
||||
#include "Vec.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
class Trig {
|
||||
public:
|
||||
Trig();
|
||||
Trig(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2);
|
||||
|
||||
public:
|
||||
Vec3F p1;
|
||||
Vec3F p2;
|
||||
Vec3F p3;
|
||||
|
||||
public:
|
||||
void assign(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2);
|
||||
void normal(Vec3F& dir) const;
|
||||
bool rayHit(class Ray& ray, Vec3F& HitPos) const;
|
||||
|
||||
public:
|
||||
~Trig();
|
||||
};
|
||||
}
|
||||
1223
Math/public/Vec.hpp
1223
Math/public/Vec.hpp
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue