Apply formating to all files. CLeanup

This commit is contained in:
IlyaShurupov 2023-10-22 17:07:28 +03:00 committed by Ilya Shurupov
parent b4ae5dde77
commit 91fb72bd49
928 changed files with 14515 additions and 21479 deletions

View file

@ -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);
}

View file

@ -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;
}
};
*/

View file

@ -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); }

View file

@ -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;
}

View file

@ -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);
}
}

View file

@ -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;
}
}