Apply formating to all files. CLeanup
This commit is contained in:
parent
43e374f269
commit
744c01c5d0
928 changed files with 14515 additions and 21480 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
using namespace tp;
|
||||
|
||||
Camera::Camera() { lookAtPoint({0, 0, 0}, {2, 0, 0}, {0, 0, 1}); }
|
||||
Camera::Camera() { lookAtPoint({ 0, 0, 0 }, { 2, 0, 0 }, { 0, 0, 1 }); }
|
||||
|
||||
const Vec3F& Camera::getTarget() const { return mTarget; }
|
||||
|
||||
|
|
@ -31,102 +31,102 @@ halnf Camera::getFOV() const { return mFOV; }
|
|||
Mat4F Camera::calculateTransformationMatrix() { return calculateProjectionMatrix() * calculateViewMatrix(); }
|
||||
|
||||
Mat4F Camera::calculateViewMatrix() {
|
||||
const Vec3F& F = (mPos - mTarget).unitV();
|
||||
const Vec3F& S = mUp * F;
|
||||
const Vec3F& U = F * S;
|
||||
const Vec3F& P = mPos;
|
||||
const Vec3F& F = (mPos - mTarget).unitV();
|
||||
const Vec3F& S = mUp * F;
|
||||
const Vec3F& U = F * S;
|
||||
const Vec3F& P = mPos;
|
||||
|
||||
Mat4F out;
|
||||
out[0] = Vec4F(S.x, S.y, S.z, -P.dot(S));
|
||||
out[1] = Vec4F(U.x, U.y, U.z, -P.dot(U));
|
||||
out[2] = Vec4F(F.x, F.y, F.z, -P.dot(F));
|
||||
out[3] = Vec4F(0, 0, 0, 1);
|
||||
return out;
|
||||
Mat4F out;
|
||||
out[0] = Vec4F(S.x, S.y, S.z, -P.dot(S));
|
||||
out[1] = Vec4F(U.x, U.y, U.z, -P.dot(U));
|
||||
out[2] = Vec4F(F.x, F.y, F.z, -P.dot(F));
|
||||
out[3] = Vec4F(0, 0, 0, 1);
|
||||
return out;
|
||||
}
|
||||
|
||||
Mat4F Camera::calculateProjectionMatrix() const {
|
||||
auto r = (halnf) sqrt(mRatio);
|
||||
halnf c = 1 / r;
|
||||
auto s = halnf(1.f / tan(mFOV / 2.f));
|
||||
auto r = (halnf) sqrt(mRatio);
|
||||
halnf c = 1 / r;
|
||||
auto s = halnf(1.f / tan(mFOV / 2.f));
|
||||
|
||||
Mat4F out;
|
||||
out[0] = Vec4F(s * r, 0, 0, 0);
|
||||
out[1] = Vec4F(0, s * c, 0, 0);
|
||||
out[2] = Vec4F(0, 0, -2.f / (mFar - mNear), -(mFar + mNear) / (mFar - mNear));
|
||||
out[3] = Vec4F(0, 0, -1, 0);
|
||||
return out;
|
||||
Mat4F out;
|
||||
out[0] = Vec4F(s * r, 0, 0, 0);
|
||||
out[1] = Vec4F(0, s * c, 0, 0);
|
||||
out[2] = Vec4F(0, 0, -2.f / (mFar - mNear), -(mFar + mNear) / (mFar - mNear));
|
||||
out[3] = Vec4F(0, 0, -1, 0);
|
||||
return out;
|
||||
}
|
||||
|
||||
Vec3F Camera::project(Vec2F normalized) {
|
||||
auto camMat = calculateTransformationMatrix();
|
||||
auto inv = camMat.inv();
|
||||
auto camMat = calculateTransformationMatrix();
|
||||
auto inv = camMat.inv();
|
||||
|
||||
halnf z = halnf((((mTarget - mPos).length() - mNear) / (mFar - mNear) - 1.f / 2) * 2.f);
|
||||
halnf w = halnf((mTarget - mPos).length());
|
||||
Vec4<halnf> world_pos4(normalized.x * w, normalized.y * w, z, w);
|
||||
halnf z = halnf((((mTarget - mPos).length() - mNear) / (mFar - mNear) - 1.f / 2) * 2.f);
|
||||
halnf w = halnf((mTarget - mPos).length());
|
||||
Vec4<halnf> world_pos4(normalized.x * w, normalized.y * w, z, w);
|
||||
|
||||
return Vec3F(inv * world_pos4);
|
||||
return Vec3F(inv * world_pos4);
|
||||
}
|
||||
|
||||
Vec2F Camera::project(const Vec3F& world) {
|
||||
Vec4F world_pos4(world.x, world.y, world.z, 1);
|
||||
Vec4F transformed = calculateViewMatrix() * world_pos4;
|
||||
transformed = calculateProjectionMatrix() * transformed;
|
||||
return {transformed[0] / transformed[3], transformed[1] / transformed[3]};
|
||||
Vec4F world_pos4(world.x, world.y, world.z, 1);
|
||||
Vec4F transformed = calculateViewMatrix() * world_pos4;
|
||||
transformed = calculateProjectionMatrix() * transformed;
|
||||
return { transformed[0] / transformed[3], transformed[1] / transformed[3] };
|
||||
}
|
||||
|
||||
Vec2F Camera::project(const tp::Vec3F& world, const tp::Mat4F& viewMat, const tp::Mat4F& projMat) {
|
||||
Vec4F world_pos4(world.x, world.y, world.z, 1);
|
||||
Vec4F transformed = viewMat * world_pos4;
|
||||
transformed = projMat * transformed;
|
||||
return {transformed[0] / transformed[3], transformed[1] / transformed[3]};
|
||||
Vec4F world_pos4(world.x, world.y, world.z, 1);
|
||||
Vec4F transformed = viewMat * world_pos4;
|
||||
transformed = projMat * transformed;
|
||||
return { transformed[0] / transformed[3], transformed[1] / transformed[3] };
|
||||
}
|
||||
|
||||
void Camera::lookAtPoint(const Vec3F& aTarget, const Vec3F& aPos, Vec3F aUp) {
|
||||
if (aTarget == aPos) {
|
||||
return;
|
||||
}
|
||||
mPos = aPos;
|
||||
mTarget = aTarget;
|
||||
Vec3F f = (mPos - mTarget).normalize();
|
||||
mUp = f * (aUp.normalize() * f);
|
||||
if (aTarget == aPos) {
|
||||
return;
|
||||
}
|
||||
mPos = aPos;
|
||||
mTarget = aTarget;
|
||||
Vec3F f = (mPos - mTarget).normalize();
|
||||
mUp = f * (aUp.normalize() * f);
|
||||
}
|
||||
|
||||
void Camera::zoom(halnf ratio) {
|
||||
ratio = abs(ratio);
|
||||
if (ratio < 0.1f) {
|
||||
return;
|
||||
}
|
||||
if (abs((mPos - mTarget).length2()) < 0.05f && ratio < 1.f) {
|
||||
return;
|
||||
}
|
||||
mPos = mTarget + (mPos - mTarget) * ratio;
|
||||
lookAtPoint(mTarget, mPos, mUp);
|
||||
ratio = abs(ratio);
|
||||
if (ratio < 0.1f) {
|
||||
return;
|
||||
}
|
||||
if (abs((mPos - mTarget).length2()) < 0.05f && ratio < 1.f) {
|
||||
return;
|
||||
}
|
||||
mPos = mTarget + (mPos - mTarget) * ratio;
|
||||
lookAtPoint(mTarget, mPos, mUp);
|
||||
}
|
||||
|
||||
void Camera::move(Vec2F aPos, Vec2F aPrevPos) {
|
||||
Vec3F p1 = project(aPrevPos);
|
||||
Vec3F p2 = project(aPos);
|
||||
Vec3F move = p1 - p2;
|
||||
mPos += move;
|
||||
mTarget += move;
|
||||
lookAtPoint(mTarget, mPos, mUp);
|
||||
Vec3F p1 = project(aPrevPos);
|
||||
Vec3F p2 = project(aPos);
|
||||
Vec3F move = p1 - p2;
|
||||
mPos += move;
|
||||
mTarget += move;
|
||||
lookAtPoint(mTarget, mPos, mUp);
|
||||
}
|
||||
|
||||
void Camera::rotate(halnf angleX, halnf angleY) {
|
||||
Vec3F wup(0, 0, 1);
|
||||
mPos -= mTarget;
|
||||
Vec3F wup(0, 0, 1);
|
||||
mPos -= mTarget;
|
||||
|
||||
mat3f rotZ = mat3f::rotatorDir(wup, angleX);
|
||||
mPos = rotZ * mPos;
|
||||
mUp = rotZ * mUp;
|
||||
mat3f rotZ = mat3f::rotatorDir(wup, angleX);
|
||||
mPos = rotZ * mPos;
|
||||
mUp = rotZ * mUp;
|
||||
|
||||
Vec3F f = mPos.unitV();
|
||||
Vec3F s = mUp * f;
|
||||
Vec3F f = mPos.unitV();
|
||||
Vec3F s = mUp * f;
|
||||
|
||||
mPos = mat3f::rotatorDir(s, -angleY) * mPos;
|
||||
mPos = mat3f::rotatorDir(s, -angleY) * mPos;
|
||||
|
||||
mPos += mTarget;
|
||||
mPos += mTarget;
|
||||
|
||||
lookAtPoint(mTarget, mPos, mUp);
|
||||
lookAtPoint(mTarget, mPos, mUp);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,169 +1,169 @@
|
|||
|
||||
#include "Color.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
RGB::RGB() { r = g = b = 1.f; }
|
||||
RGB::RGB(flt4 pr, flt4 pg, flt4 pb) { set(pr, pg, pb); }
|
||||
RGB::RGB(flt4 val) { set(val, val, val); }
|
||||
|
||||
void RGB::set(flt4 pr, flt4 pg, flt4 pb) {
|
||||
r = pr;
|
||||
b = pb;
|
||||
g = pg;
|
||||
}
|
||||
|
||||
RGB::operator HSV() const {
|
||||
|
||||
HSV out;
|
||||
alnf min, max, delta;
|
||||
|
||||
min = r < g ? r : g;
|
||||
min = min < b ? min : b;
|
||||
max = r > g ? r : g;
|
||||
max = max > b ? max : b;
|
||||
|
||||
out.v = (halnf) max;
|
||||
delta = max - min;
|
||||
|
||||
if (delta < 0.00001) {
|
||||
out.s = 0;
|
||||
// undefined, maybe nan?
|
||||
out.h = 0;
|
||||
return out;
|
||||
}
|
||||
if (max > 0.f) {
|
||||
// NOTE: if Max is == 0, this divide would cause a crash
|
||||
out.s = (halnf) (delta / max);
|
||||
|
||||
} else {
|
||||
// if max is 0, then r = g = b = 0
|
||||
// s = 0, h is undefined
|
||||
out.s = 0.f;
|
||||
out.h = 0.f;
|
||||
return out;
|
||||
}
|
||||
if (r >= max) {
|
||||
// between yellow & magenta
|
||||
out.h = (halnf) ((g - b) / delta);
|
||||
} else {
|
||||
if (g >= max) {
|
||||
// between cyan & yellow
|
||||
out.h = (halnf) (2.0 + (b - r) / delta);
|
||||
} else {
|
||||
// between magenta & cyan
|
||||
out.h = (halnf) (4.0 + (r - g) / delta);
|
||||
}
|
||||
}
|
||||
|
||||
out.h *= 60.f;
|
||||
|
||||
if (out.h < 0.0) {
|
||||
out.h += 360.f;
|
||||
}
|
||||
|
||||
out.h = (halnf) (out.h / 360.f * (PI2));
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
HSV::HSV() { h = s = v = 0.f; }
|
||||
|
||||
HSV::HSV(flt4 ph, flt4 ps, flt4 pv) { set(ph, ps, pv); }
|
||||
|
||||
void HSV::set(flt4 ph, flt4 ps, flt4 pv) {
|
||||
h = ph;
|
||||
s = ps;
|
||||
v = pv;
|
||||
}
|
||||
|
||||
HSV::operator RGB() const {
|
||||
alnf hh, p, q, t, ff;
|
||||
alni i;
|
||||
RGB out;
|
||||
|
||||
if (s <= 0.0) { // < is bogus, just shuts up warnings
|
||||
out.r = v;
|
||||
out.g = v;
|
||||
out.b = v;
|
||||
return out;
|
||||
}
|
||||
|
||||
hh = h / (PI2) * 360;
|
||||
|
||||
if (hh >= 360.0) {
|
||||
hh = 0.0;
|
||||
}
|
||||
|
||||
hh /= 60.0;
|
||||
i = (long) hh;
|
||||
ff = hh - i;
|
||||
p = v * (1.0 - s);
|
||||
q = v * (1.0 - (s * ff));
|
||||
t = v * (1.0 - (s * (1.0 - ff)));
|
||||
|
||||
switch (i) {
|
||||
case 0:
|
||||
out.r = (halnf) v;
|
||||
out.g = (halnf) t;
|
||||
out.b = (halnf) p;
|
||||
break;
|
||||
case 1:
|
||||
out.r = (halnf) q;
|
||||
out.g = (halnf) v;
|
||||
out.b = (halnf) p;
|
||||
break;
|
||||
case 2:
|
||||
out.r = (halnf) p;
|
||||
out.g = (halnf) v;
|
||||
out.b = (halnf) t;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
out.r = (halnf) p;
|
||||
out.g = (halnf) q;
|
||||
out.b = (halnf) v;
|
||||
break;
|
||||
case 4:
|
||||
out.r = (halnf) t;
|
||||
out.g = (halnf) p;
|
||||
out.b = (halnf) v;
|
||||
break;
|
||||
case 5:
|
||||
default:
|
||||
out.r = (halnf) v;
|
||||
out.g = (halnf) p;
|
||||
out.b = (halnf) q;
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/*
|
||||
struct rgbab {
|
||||
|
||||
uint4 RGBA;
|
||||
|
||||
rgbab() { RGBA = 0xFFFFFFFF; }
|
||||
rgbab(uint4 color) { RGBA = color; }
|
||||
rgbab(uint1 r, uint1 b, uint1 g, uint1 a) { set(r, b, g, a); }
|
||||
rgbab(const rgbab& in) { RGBA = in.RGBA; }
|
||||
|
||||
void set(uint4 color) { RGBA = color; }
|
||||
void set(uint1 r, uint1 b, uint1 g, uint1 a) {
|
||||
RGBA = (uint1)(a);
|
||||
RGBA <<= 8; RGBA |= (uint1)(r);
|
||||
RGBA <<= 8; RGBA |= (uint1)(g);
|
||||
RGBA <<= 8; RGBA |= (uint1)(b);
|
||||
}
|
||||
|
||||
operator rgbaf() {
|
||||
rgbaf out;
|
||||
out.r = uint1((RGBA & 0x00FF0000) >> 16) / 255.f;
|
||||
out.g = uint1((RGBA & 0x0000FF00) >> 8) / 255.f;
|
||||
out.b = uint1(RGBA & 0x000000FF) / 255.f;
|
||||
out.a = uint1((RGBA & 0xFF000000) >> 24) / 255.f;
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
#include "Color.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
RGB::RGB() { r = g = b = 1.f; }
|
||||
RGB::RGB(flt4 pr, flt4 pg, flt4 pb) { set(pr, pg, pb); }
|
||||
RGB::RGB(flt4 val) { set(val, val, val); }
|
||||
|
||||
void RGB::set(flt4 pr, flt4 pg, flt4 pb) {
|
||||
r = pr;
|
||||
b = pb;
|
||||
g = pg;
|
||||
}
|
||||
|
||||
RGB::operator HSV() const {
|
||||
|
||||
HSV out;
|
||||
alnf min, max, delta;
|
||||
|
||||
min = r < g ? r : g;
|
||||
min = min < b ? min : b;
|
||||
max = r > g ? r : g;
|
||||
max = max > b ? max : b;
|
||||
|
||||
out.v = (halnf) max;
|
||||
delta = max - min;
|
||||
|
||||
if (delta < 0.00001) {
|
||||
out.s = 0;
|
||||
// undefined, maybe nan?
|
||||
out.h = 0;
|
||||
return out;
|
||||
}
|
||||
if (max > 0.f) {
|
||||
// NOTE: if Max is == 0, this divide would cause a crash
|
||||
out.s = (halnf) (delta / max);
|
||||
|
||||
} else {
|
||||
// if max is 0, then r = g = b = 0
|
||||
// s = 0, h is undefined
|
||||
out.s = 0.f;
|
||||
out.h = 0.f;
|
||||
return out;
|
||||
}
|
||||
if (r >= max) {
|
||||
// between yellow & magenta
|
||||
out.h = (halnf) ((g - b) / delta);
|
||||
} else {
|
||||
if (g >= max) {
|
||||
// between cyan & yellow
|
||||
out.h = (halnf) (2.0 + (b - r) / delta);
|
||||
} else {
|
||||
// between magenta & cyan
|
||||
out.h = (halnf) (4.0 + (r - g) / delta);
|
||||
}
|
||||
}
|
||||
|
||||
out.h *= 60.f;
|
||||
|
||||
if (out.h < 0.0) {
|
||||
out.h += 360.f;
|
||||
}
|
||||
|
||||
out.h = (halnf) (out.h / 360.f * (PI2));
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
HSV::HSV() { h = s = v = 0.f; }
|
||||
|
||||
HSV::HSV(flt4 ph, flt4 ps, flt4 pv) { set(ph, ps, pv); }
|
||||
|
||||
void HSV::set(flt4 ph, flt4 ps, flt4 pv) {
|
||||
h = ph;
|
||||
s = ps;
|
||||
v = pv;
|
||||
}
|
||||
|
||||
HSV::operator RGB() const {
|
||||
alnf hh, p, q, t, ff;
|
||||
alni i;
|
||||
RGB out;
|
||||
|
||||
if (s <= 0.0) { // < is bogus, just shuts up warnings
|
||||
out.r = v;
|
||||
out.g = v;
|
||||
out.b = v;
|
||||
return out;
|
||||
}
|
||||
|
||||
hh = h / (PI2) *360;
|
||||
|
||||
if (hh >= 360.0) {
|
||||
hh = 0.0;
|
||||
}
|
||||
|
||||
hh /= 60.0;
|
||||
i = (long) hh;
|
||||
ff = hh - i;
|
||||
p = v * (1.0 - s);
|
||||
q = v * (1.0 - (s * ff));
|
||||
t = v * (1.0 - (s * (1.0 - ff)));
|
||||
|
||||
switch (i) {
|
||||
case 0:
|
||||
out.r = (halnf) v;
|
||||
out.g = (halnf) t;
|
||||
out.b = (halnf) p;
|
||||
break;
|
||||
case 1:
|
||||
out.r = (halnf) q;
|
||||
out.g = (halnf) v;
|
||||
out.b = (halnf) p;
|
||||
break;
|
||||
case 2:
|
||||
out.r = (halnf) p;
|
||||
out.g = (halnf) v;
|
||||
out.b = (halnf) t;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
out.r = (halnf) p;
|
||||
out.g = (halnf) q;
|
||||
out.b = (halnf) v;
|
||||
break;
|
||||
case 4:
|
||||
out.r = (halnf) t;
|
||||
out.g = (halnf) p;
|
||||
out.b = (halnf) v;
|
||||
break;
|
||||
case 5:
|
||||
default:
|
||||
out.r = (halnf) v;
|
||||
out.g = (halnf) p;
|
||||
out.b = (halnf) q;
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/*
|
||||
struct rgbab {
|
||||
|
||||
uint4 RGBA;
|
||||
|
||||
rgbab() { RGBA = 0xFFFFFFFF; }
|
||||
rgbab(uint4 color) { RGBA = color; }
|
||||
rgbab(uint1 r, uint1 b, uint1 g, uint1 a) { set(r, b, g, a); }
|
||||
rgbab(const rgbab& in) { RGBA = in.RGBA; }
|
||||
|
||||
void set(uint4 color) { RGBA = color; }
|
||||
void set(uint1 r, uint1 b, uint1 g, uint1 a) {
|
||||
RGBA = (uint1)(a);
|
||||
RGBA <<= 8; RGBA |= (uint1)(r);
|
||||
RGBA <<= 8; RGBA |= (uint1)(g);
|
||||
RGBA <<= 8; RGBA |= (uint1)(b);
|
||||
}
|
||||
|
||||
operator rgbaf() {
|
||||
rgbaf out;
|
||||
out.r = uint1((RGBA & 0x00FF0000) >> 16) / 255.f;
|
||||
out.g = uint1((RGBA & 0x0000FF00) >> 8) / 255.f;
|
||||
out.b = uint1(RGBA & 0x000000FF) / 255.f;
|
||||
out.a = uint1((RGBA & 0xFF000000) >> 24) / 255.f;
|
||||
return out;
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
|
@ -1,43 +1,40 @@
|
|||
|
||||
#include "MathCommon.hpp"
|
||||
|
||||
#include "ContainersCommon.hpp"
|
||||
|
||||
static tp::ModuleManifest* sModuleDependencies[] = {
|
||||
&tp::gModuleContainers,
|
||||
nullptr
|
||||
};
|
||||
|
||||
tp::ModuleManifest tp::gModuleMath = ModuleManifest("Math", nullptr, nullptr, sModuleDependencies);
|
||||
|
||||
tp::alnf std_sin(tp::alnf radians);
|
||||
tp::alnf std_tan(tp::alnf radians);
|
||||
tp::alnf std_cos(tp::alnf radians);
|
||||
tp::alnf std_acos(tp::alnf val);
|
||||
tp::alnf std_sqrt(tp::alnf val);
|
||||
tp::alnf std_rad(tp::alnf val);
|
||||
tp::alnf std_deg(tp::alnf val);
|
||||
tp::alnf std_atan2(tp::alnf X, tp::alnf Y);
|
||||
tp::alnf std_atan(tp::alnf val);
|
||||
|
||||
tp::alnf tp::sin(const tp::alnf radians) { return std_sin((halnf) radians); }
|
||||
tp::alnf tp::tan(const tp::alnf radians) { return std_tan((halnf) radians); }
|
||||
tp::alnf tp::cos(const tp::alnf radians) { return std_cos((halnf) radians); }
|
||||
tp::alnf tp::acos(const tp::alnf val) { return std_acos((halnf) val); }
|
||||
tp::alnf tp::sqrt(const tp::alnf val) { return std_sqrt((halnf) val); }
|
||||
tp::alnf tp::rad(const tp::alnf val) { return val * (PI / 180.f); }
|
||||
tp::alnf tp::deg(const tp::alnf val) { return val * (180.f / PI); }
|
||||
tp::alnf tp::atan2(const tp::alnf X, const tp::alnf Y) { return std_atan2((halnf) X, (halnf) Y); }
|
||||
tp::alnf tp::atan(const tp::alnf val) { return std_atan((halnf) val); }
|
||||
|
||||
#include <cmath>
|
||||
|
||||
tp::alnf std_sin(const tp::alnf radians) { return sinf((tp::halnf)radians); }
|
||||
tp::alnf std_tan(const tp::alnf radians) { return tanf((tp::halnf)radians); }
|
||||
tp::alnf std_cos(const tp::alnf radians) { return cosf((tp::halnf)radians); }
|
||||
tp::alnf std_acos(const tp::alnf val) { return acos((tp::halnf)val); }
|
||||
tp::alnf std_sqrt(const tp::alnf val) { return sqrt((tp::halnf)val); }
|
||||
tp::alnf std_rad(const tp::alnf val) { return val * (PI / 180.f); }
|
||||
tp::alnf std_deg(const tp::alnf val) { return val * (180.f / PI); }
|
||||
tp::alnf std_atan2(const tp::alnf X, const tp::alnf Y) { return atan2((tp::halnf)X, (tp::halnf)Y); }
|
||||
tp::alnf std_atan(const tp::alnf val) { return atan((tp::halnf)val); }
|
||||
|
||||
#include "MathCommon.hpp"
|
||||
|
||||
#include "ContainersCommon.hpp"
|
||||
|
||||
static tp::ModuleManifest* sModuleDependencies[] = { &tp::gModuleContainers, nullptr };
|
||||
|
||||
tp::ModuleManifest tp::gModuleMath = ModuleManifest("Math", nullptr, nullptr, sModuleDependencies);
|
||||
|
||||
tp::alnf std_sin(tp::alnf radians);
|
||||
tp::alnf std_tan(tp::alnf radians);
|
||||
tp::alnf std_cos(tp::alnf radians);
|
||||
tp::alnf std_acos(tp::alnf val);
|
||||
tp::alnf std_sqrt(tp::alnf val);
|
||||
tp::alnf std_rad(tp::alnf val);
|
||||
tp::alnf std_deg(tp::alnf val);
|
||||
tp::alnf std_atan2(tp::alnf X, tp::alnf Y);
|
||||
tp::alnf std_atan(tp::alnf val);
|
||||
|
||||
tp::alnf tp::sin(const tp::alnf radians) { return std_sin((halnf) radians); }
|
||||
tp::alnf tp::tan(const tp::alnf radians) { return std_tan((halnf) radians); }
|
||||
tp::alnf tp::cos(const tp::alnf radians) { return std_cos((halnf) radians); }
|
||||
tp::alnf tp::acos(const tp::alnf val) { return std_acos((halnf) val); }
|
||||
tp::alnf tp::sqrt(const tp::alnf val) { return std_sqrt((halnf) val); }
|
||||
tp::alnf tp::rad(const tp::alnf val) { return val * (PI / 180.f); }
|
||||
tp::alnf tp::deg(const tp::alnf val) { return val * (180.f / PI); }
|
||||
tp::alnf tp::atan2(const tp::alnf X, const tp::alnf Y) { return std_atan2((halnf) X, (halnf) Y); }
|
||||
tp::alnf tp::atan(const tp::alnf val) { return std_atan((halnf) val); }
|
||||
|
||||
#include <cmath>
|
||||
|
||||
tp::alnf std_sin(const tp::alnf radians) { return sinf((tp::halnf) radians); }
|
||||
tp::alnf std_tan(const tp::alnf radians) { return tanf((tp::halnf) radians); }
|
||||
tp::alnf std_cos(const tp::alnf radians) { return cosf((tp::halnf) radians); }
|
||||
tp::alnf std_acos(const tp::alnf val) { return acos((tp::halnf) val); }
|
||||
tp::alnf std_sqrt(const tp::alnf val) { return sqrt((tp::halnf) val); }
|
||||
tp::alnf std_rad(const tp::alnf val) { return val * (PI / 180.f); }
|
||||
tp::alnf std_deg(const tp::alnf val) { return val * (180.f / PI); }
|
||||
tp::alnf std_atan2(const tp::alnf X, const tp::alnf Y) { return atan2((tp::halnf) X, (tp::halnf) Y); }
|
||||
tp::alnf std_atan(const tp::alnf val) { return atan((tp::halnf) val); }
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
|
||||
#include "Ray.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
Ray::Ray(const Vec3F& aDir, const Vec3F& aPos) {
|
||||
this->dir = aDir.unitV();
|
||||
this->pos = aPos;
|
||||
|
||||
#include "Ray.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
Ray::Ray(const Vec3F& aDir, const Vec3F& aPos) {
|
||||
this->dir = aDir.unitV();
|
||||
this->pos = aPos;
|
||||
}
|
||||
|
|
@ -1,104 +1,110 @@
|
|||
|
||||
#include "Topology.hpp"
|
||||
|
||||
void* operator new(std::size_t, void* in) { return in; }
|
||||
|
||||
using namespace tp;
|
||||
|
||||
Vec3F TrigCache::gHitPos;
|
||||
|
||||
TrigCache::TrigCache() : mP1(0), mP2(0), mP3(0) {}
|
||||
|
||||
TrigCache::TrigCache(ualni v1, ualni v2, ualni v3) : mP1(v1), mP2(v2), mP3(v3) {}
|
||||
|
||||
TrigCache::TrigCache(const TrigCache& in) {
|
||||
mP1 = in.mP1;
|
||||
mP2 = in.mP2;
|
||||
mP3 = in.mP3;
|
||||
|
||||
mEdgeP1P2 = in.mEdgeP1P2;
|
||||
mEdgeP1P3 = in.mEdgeP1P3;
|
||||
|
||||
mOrigin = in.mOrigin;
|
||||
mNormal = in.mNormal;
|
||||
}
|
||||
|
||||
void TrigCache::updateCache(const Buffer<Vec3F>& points) {
|
||||
mOrigin = points[mP1];
|
||||
mEdgeP1P2 = points[mP2] - points[mP1];
|
||||
mEdgeP1P3 = points[mP3] - points[mP1];
|
||||
mNormal = (points[mP2] - points[mP1]).cross(points[mP3] - points[mP1]).unitV();
|
||||
}
|
||||
|
||||
bool TrigCache::castRay(const Ray& ray) const {
|
||||
static Vec3F h, s, q;
|
||||
static halnf a, f, u, v;
|
||||
static halnf t;
|
||||
|
||||
if (ray.dir.dot(mNormal) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
h = ray.dir.cross(mEdgeP1P3);
|
||||
a = mEdgeP1P2.dot(h);
|
||||
|
||||
if (a > -EPSILON && a < EPSILON) {
|
||||
return false;
|
||||
}
|
||||
|
||||
f = 1.f / a;
|
||||
s = ray.pos - mOrigin;
|
||||
u = f * s.dot(h);
|
||||
|
||||
if (u < 0.0 || u > 1.0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
q = s.cross(mEdgeP1P2);
|
||||
v = f * ray.dir.dot(q);
|
||||
|
||||
if (v < 0.f || u + v > 1.f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
t = f * mEdgeP1P3.dot(q);
|
||||
if (t > EPSILON) {
|
||||
gHitPos = ray.pos + ray.dir * t;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const Vec3F& TrigCache::getHitPos() { return gHitPos; }
|
||||
|
||||
const Vec3F& TrigCache::getNormal() const { return mNormal; }
|
||||
|
||||
void TopologyCache::updateCache() {
|
||||
TransformedNormals.clear();
|
||||
TransformedPoints.clear();
|
||||
TrigCaches.clear();
|
||||
|
||||
if (!Source) {
|
||||
return;
|
||||
}
|
||||
|
||||
TransformedPoints.reserve(Source->Points.size());
|
||||
for (auto idx : Range(TransformedPoints.size())) {
|
||||
TransformedPoints[idx] = Source->Basis.transform(Source->Points[idx]);
|
||||
TransformedPoints[idx] += Source->Origin;
|
||||
}
|
||||
|
||||
TransformedNormals.reserve(Source->Normals.size());
|
||||
for (auto idx : Range(TransformedNormals.size())) {
|
||||
TransformedNormals[idx] = Source->Basis.transform(Source->Normals[idx]);
|
||||
}
|
||||
|
||||
TrigCaches.reserve(Source->Indexes.size());
|
||||
for (auto idx : Range(TrigCaches.size())) {
|
||||
TrigCaches[idx].mP1 = Source->Indexes[idx].x;
|
||||
TrigCaches[idx].mP2 = Source->Indexes[idx].y;
|
||||
TrigCaches[idx].mP3 = Source->Indexes[idx].z;
|
||||
TrigCaches[idx].updateCache(TransformedPoints);
|
||||
}
|
||||
}
|
||||
|
||||
#include "Topology.hpp"
|
||||
|
||||
void* operator new(std::size_t, void* in) { return in; }
|
||||
|
||||
using namespace tp;
|
||||
|
||||
Vec3F TrigCache::gHitPos;
|
||||
|
||||
TrigCache::TrigCache() :
|
||||
mP1(0),
|
||||
mP2(0),
|
||||
mP3(0) {}
|
||||
|
||||
TrigCache::TrigCache(ualni v1, ualni v2, ualni v3) :
|
||||
mP1(v1),
|
||||
mP2(v2),
|
||||
mP3(v3) {}
|
||||
|
||||
TrigCache::TrigCache(const TrigCache& in) {
|
||||
mP1 = in.mP1;
|
||||
mP2 = in.mP2;
|
||||
mP3 = in.mP3;
|
||||
|
||||
mEdgeP1P2 = in.mEdgeP1P2;
|
||||
mEdgeP1P3 = in.mEdgeP1P3;
|
||||
|
||||
mOrigin = in.mOrigin;
|
||||
mNormal = in.mNormal;
|
||||
}
|
||||
|
||||
void TrigCache::updateCache(const Buffer<Vec3F>& points) {
|
||||
mOrigin = points[mP1];
|
||||
mEdgeP1P2 = points[mP2] - points[mP1];
|
||||
mEdgeP1P3 = points[mP3] - points[mP1];
|
||||
mNormal = (points[mP2] - points[mP1]).cross(points[mP3] - points[mP1]).unitV();
|
||||
}
|
||||
|
||||
bool TrigCache::castRay(const Ray& ray) const {
|
||||
static Vec3F h, s, q;
|
||||
static halnf a, f, u, v;
|
||||
static halnf t;
|
||||
|
||||
if (ray.dir.dot(mNormal) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
h = ray.dir.cross(mEdgeP1P3);
|
||||
a = mEdgeP1P2.dot(h);
|
||||
|
||||
if (a > -EPSILON && a < EPSILON) {
|
||||
return false;
|
||||
}
|
||||
|
||||
f = 1.f / a;
|
||||
s = ray.pos - mOrigin;
|
||||
u = f * s.dot(h);
|
||||
|
||||
if (u < 0.0 || u > 1.0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
q = s.cross(mEdgeP1P2);
|
||||
v = f * ray.dir.dot(q);
|
||||
|
||||
if (v < 0.f || u + v > 1.f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
t = f * mEdgeP1P3.dot(q);
|
||||
if (t > EPSILON) {
|
||||
gHitPos = ray.pos + ray.dir * t;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const Vec3F& TrigCache::getHitPos() { return gHitPos; }
|
||||
|
||||
const Vec3F& TrigCache::getNormal() const { return mNormal; }
|
||||
|
||||
void TopologyCache::updateCache() {
|
||||
TransformedNormals.clear();
|
||||
TransformedPoints.clear();
|
||||
TrigCaches.clear();
|
||||
|
||||
if (!Source) {
|
||||
return;
|
||||
}
|
||||
|
||||
TransformedPoints.reserve(Source->Points.size());
|
||||
for (auto idx : Range(TransformedPoints.size())) {
|
||||
TransformedPoints[idx] = Source->Basis.transform(Source->Points[idx]);
|
||||
TransformedPoints[idx] += Source->Origin;
|
||||
}
|
||||
|
||||
TransformedNormals.reserve(Source->Normals.size());
|
||||
for (auto idx : Range(TransformedNormals.size())) {
|
||||
TransformedNormals[idx] = Source->Basis.transform(Source->Normals[idx]);
|
||||
}
|
||||
|
||||
TrigCaches.reserve(Source->Indexes.size());
|
||||
for (auto idx : Range(TrigCaches.size())) {
|
||||
TrigCaches[idx].mP1 = Source->Indexes[idx].x;
|
||||
TrigCaches[idx].mP2 = Source->Indexes[idx].y;
|
||||
TrigCaches[idx].mP3 = Source->Indexes[idx].z;
|
||||
TrigCaches[idx].updateCache(TransformedPoints);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,72 +1,72 @@
|
|||
|
||||
#include "Trig.hpp"
|
||||
|
||||
#include "Ray.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
Trig::Trig() {
|
||||
MODULE_SANITY_CHECK(gModuleMath)
|
||||
p1.assign(0.f, 0.f, 0.f);
|
||||
p2.assign(0.f, 0.f, 0.f);
|
||||
p3.assign(0.f, 0.f, 0.f);
|
||||
}
|
||||
|
||||
Trig::Trig(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2) {
|
||||
p1 = v0;
|
||||
p2 = v1;
|
||||
p3 = v2;
|
||||
}
|
||||
|
||||
void Trig::assign(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2) {
|
||||
p1 = v0;
|
||||
p2 = v1;
|
||||
p3 = v2;
|
||||
}
|
||||
|
||||
Trig::~Trig() = default;
|
||||
|
||||
void Trig::normal(Vec3F& dir) const {
|
||||
dir = (p2 - p1).cross(p3 - p1);
|
||||
dir.normalize();
|
||||
}
|
||||
|
||||
Vec3F edge1, edge2, h, s, q;
|
||||
halnf a, f, u, v;
|
||||
halnf t;
|
||||
|
||||
bool Trig::rayHit(class Ray& ray, Vec3F& HitPos) const {
|
||||
|
||||
edge1 = p2 - p1;
|
||||
edge2 = p3 - p1;
|
||||
|
||||
h = ray.dir.cross(edge2);
|
||||
a = edge1.dot(h);
|
||||
|
||||
if (a > -EPSILON && a < EPSILON) {
|
||||
return false;
|
||||
}
|
||||
|
||||
f = 1.f / a;
|
||||
s = ray.pos - p1;
|
||||
u = f * s.dot(h);
|
||||
|
||||
if (u < 0.0 || u > 1.0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
q = s.cross(edge1);
|
||||
v = f * ray.dir.dot(q);
|
||||
|
||||
if (v < 0.f || u + v > 1.f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
t = f * edge2.dot(q);
|
||||
if (t > EPSILON) {
|
||||
HitPos = ray.pos + ray.dir * t;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#include "Trig.hpp"
|
||||
|
||||
#include "Ray.hpp"
|
||||
|
||||
using namespace tp;
|
||||
|
||||
Trig::Trig() {
|
||||
MODULE_SANITY_CHECK(gModuleMath)
|
||||
p1.assign(0.f, 0.f, 0.f);
|
||||
p2.assign(0.f, 0.f, 0.f);
|
||||
p3.assign(0.f, 0.f, 0.f);
|
||||
}
|
||||
|
||||
Trig::Trig(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2) {
|
||||
p1 = v0;
|
||||
p2 = v1;
|
||||
p3 = v2;
|
||||
}
|
||||
|
||||
void Trig::assign(const Vec3F& v0, const Vec3F& v1, const Vec3F& v2) {
|
||||
p1 = v0;
|
||||
p2 = v1;
|
||||
p3 = v2;
|
||||
}
|
||||
|
||||
Trig::~Trig() = default;
|
||||
|
||||
void Trig::normal(Vec3F& dir) const {
|
||||
dir = (p2 - p1).cross(p3 - p1);
|
||||
dir.normalize();
|
||||
}
|
||||
|
||||
Vec3F edge1, edge2, h, s, q;
|
||||
halnf a, f, u, v;
|
||||
halnf t;
|
||||
|
||||
bool Trig::rayHit(class Ray& ray, Vec3F& HitPos) const {
|
||||
|
||||
edge1 = p2 - p1;
|
||||
edge2 = p3 - p1;
|
||||
|
||||
h = ray.dir.cross(edge2);
|
||||
a = edge1.dot(h);
|
||||
|
||||
if (a > -EPSILON && a < EPSILON) {
|
||||
return false;
|
||||
}
|
||||
|
||||
f = 1.f / a;
|
||||
s = ray.pos - p1;
|
||||
u = f * s.dot(h);
|
||||
|
||||
if (u < 0.0 || u > 1.0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
q = s.cross(edge1);
|
||||
v = f * ray.dir.dot(q);
|
||||
|
||||
if (v < 0.f || u + v > 1.f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
t = f * edge2.dot(q);
|
||||
if (t > EPSILON) {
|
||||
HitPos = ray.pos + ray.dir * t;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
|
@ -5,37 +5,37 @@
|
|||
using namespace tp;
|
||||
|
||||
TEST_DEF(ComplexNumber) {
|
||||
TEST((ComplexCart {3, 4}.mod() == 5));
|
||||
TEST((ComplexCart {1, 1}.arg() == 0.7853981633974483));
|
||||
TEST((ComplexCart {3, 4}.norm2() == 25));
|
||||
TEST((ComplexCart{ 3, 4 }.mod() == 5));
|
||||
TEST((ComplexCart{ 1, 1 }.arg() == 0.7853981633974483));
|
||||
TEST((ComplexCart{ 3, 4 }.norm2() == 25));
|
||||
|
||||
{
|
||||
ComplexCart cn {3, 4};
|
||||
ComplexCart expected {0.12, -0.16};
|
||||
auto val = cn.reciprocal();
|
||||
TEST((val == expected));
|
||||
}
|
||||
{
|
||||
ComplexCart cn{ 3, 4 };
|
||||
ComplexCart expected{ 0.12, -0.16 };
|
||||
auto val = cn.reciprocal();
|
||||
TEST((val == expected));
|
||||
}
|
||||
|
||||
{
|
||||
ComplexCart cn {3, 4};
|
||||
ComplexCart expected {3, -4};
|
||||
TEST((cn.conjugate() == expected));
|
||||
}
|
||||
{
|
||||
ComplexCart cn{ 3, 4 };
|
||||
ComplexCart expected{ 3, -4 };
|
||||
TEST((cn.conjugate() == expected));
|
||||
}
|
||||
|
||||
{
|
||||
ComplexCart cn1 {3, 4};
|
||||
ComplexCart cn2 {1, 2};
|
||||
ComplexCart expected {-5, 10};
|
||||
TEST((cn1 * cn2 == expected));
|
||||
}
|
||||
{
|
||||
ComplexCart cn1{ 3, 4 };
|
||||
ComplexCart cn2{ 1, 2 };
|
||||
ComplexCart expected{ -5, 10 };
|
||||
TEST((cn1 * cn2 == expected));
|
||||
}
|
||||
|
||||
{
|
||||
ComplexRad c1 {5, 7};
|
||||
ComplexRad c2 {10, 3};
|
||||
ComplexCart tmp1 = (c1 * c2).cart();
|
||||
ComplexCart tmp2 = (c1.cart() * c2.cart());
|
||||
TEST((tmp1 - tmp2).mod() < 0.001);
|
||||
}
|
||||
{
|
||||
ComplexRad c1{ 5, 7 };
|
||||
ComplexRad c2{ 10, 3 };
|
||||
ComplexCart tmp1 = (c1 * c2).cart();
|
||||
ComplexCart tmp2 = (c1.cart() * c2.cart());
|
||||
TEST((tmp1 - tmp2).mod() < 0.001);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_DEF(Math) { testComplexNumber(); }
|
||||
|
|
|
|||
|
|
@ -1,23 +1,23 @@
|
|||
|
||||
#include "MathCommon.hpp"
|
||||
#include "Testing.hpp"
|
||||
|
||||
static bool init(const tp::ModuleManifest* self) {
|
||||
tp::gTesting.setRootName(self->getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
void testMath();
|
||||
|
||||
int main() {
|
||||
tp::ModuleManifest* deps[] = {&tp::gModuleMath, &tp::gModuleUtils, nullptr};
|
||||
tp::ModuleManifest testModule("MathTest", init, nullptr, deps);
|
||||
|
||||
if (!testModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
testMath();
|
||||
|
||||
testModule.deinitialize();
|
||||
}
|
||||
|
||||
#include "MathCommon.hpp"
|
||||
#include "Testing.hpp"
|
||||
|
||||
static bool init(const tp::ModuleManifest* self) {
|
||||
tp::gTesting.setRootName(self->getName());
|
||||
return true;
|
||||
}
|
||||
|
||||
void testMath();
|
||||
|
||||
int main() {
|
||||
tp::ModuleManifest* deps[] = { &tp::gModuleMath, &tp::gModuleUtils, nullptr };
|
||||
tp::ModuleManifest testModule("MathTest", init, nullptr, deps);
|
||||
|
||||
if (!testModule.initialize()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
testMath();
|
||||
|
||||
testModule.deinitialize();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue