diff --git a/examples/animation_2d.cpp b/examples/animation_2d.cpp index 9be116f6..897db894 100644 --- a/examples/animation_2d.cpp +++ b/examples/animation_2d.cpp @@ -3,12 +3,12 @@ #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include #include @@ -35,9 +35,9 @@ namespace struct bone { - geom::point position; - geom::vector direction; - geom::vector velocity; + math::point position; + math::vector direction; + math::vector velocity; float angular_velocity; float length; float width; @@ -51,16 +51,16 @@ struct joint float s0, s1; // angle range max should be in [0, 2*pi] // angle range min should be less or equal to angle range max - std::optional> angle_range = std::nullopt; + std::optional> angle_range = std::nullopt; }; template struct constraint { std::size_t bone[Bones]; - geom::matrix jacobian; - geom::vector bias; - geom::interval range = geom::interval::full(); + math::matrix jacobian; + math::vector bias; + math::interval range = math::interval::full(); }; struct system @@ -74,7 +74,7 @@ struct system { std::size_t index; float pos; - geom::vector delta; + math::vector delta; }; void advance(float time, std::optional sel, util::function(system const &)> torque_provider); @@ -105,7 +105,7 @@ void system::step(std::optional sel, util::function const gravity { 0.f, -10.f }; + math::vector const gravity { 0.f, -10.f }; for (std::size_t i = 0; i < bones.size(); ++i) { @@ -138,7 +138,7 @@ void system::step(std::optional sel, util::function> constraints_1_1; @@ -175,7 +175,7 @@ void system::step(std::optional sel, util::function const & v){ + auto push_1 = [&](float depth, float s, math::vector const & v){ constraint<1, 1> & c = constraints_1_1.emplace_back(); c.bone[0] = i; @@ -209,8 +209,8 @@ void system::step(std::optional sel, util::function sel, util::function sel, util::functioncenter() \in [-pi, 2*pi] // delta \in [-3*pi, 2*pi] float delta = a - j.angle_range->center(); - if (delta > geom::pi) - delta -= 2.f * geom::pi; - if (delta < -geom::pi) - delta += 2.f * geom::pi; + if (delta > math::pi) + delta -= 2.f * math::pi; + if (delta < -math::pi) + delta += 2.f * math::pi; // delta in [-pi, pi] float const half_length = j.angle_range->length() / 2.f; @@ -380,7 +380,7 @@ void system::step(std::optional sel, util::function void system::solve_constraint(constraint const & c) { - geom::vector v; + math::vector v; auto j_inv_mass = c.jacobian; for (std::size_t i = 0; i < B; ++i) @@ -401,11 +401,11 @@ void system::solve_constraint(constraint const & c) } } - geom::vector l = *geom::solve(j_inv_mass * geom::transpose(c.jacobian), - c.jacobian * v - c.bias); + math::vector l = *math::solve(j_inv_mass * math::transpose(c.jacobian), - c.jacobian * v - c.bias); for (std::size_t j = 0; j < C; ++j) - l[j] = geom::clamp(l[j], c.range); + l[j] = math::clamp(l[j], c.range); - auto p = geom::transpose(j_inv_mass) * l; + auto p = math::transpose(j_inv_mass) * l; for (std::size_t i = 0; i < B; ++i) { @@ -413,7 +413,7 @@ void system::solve_constraint(constraint const & c) b.velocity[0] += p[4 * i + 0]; b.velocity[1] += p[4 * i + 1]; - b.angular_velocity += geom::det(b.direction, geom::vector{p[4 * i + 2], p[4 * i + 3]}); + b.angular_velocity += math::det(b.direction, math::vector{p[4 * i + 2], p[4 * i + 3]}); } } @@ -433,15 +433,15 @@ struct controller static constexpr float max_output = 20.f; - geom::matrix m1; - geom::vector t1; + math::matrix m1; + math::vector t1; -// geom::matrix m1; -// geom::vector t1; -// geom::matrix m2; -// geom::vector t2; -// geom::matrix m3; -// geom::vector t3; +// math::matrix m1; +// math::vector t1; +// math::matrix m2; +// math::vector t2; +// math::matrix m3; +// math::vector t3; //Eigen::VectorXf to_eigen() const; //void from_eigen(Eigen::VectorXf const & v); @@ -456,7 +456,7 @@ struct controller void reset(); - geom::vector apply(system const & s) const; + math::vector apply(system const & s) const; void read(std::istream & is); void write(std::ostream & os) const; @@ -464,7 +464,7 @@ struct controller static float activation(float x); template - static geom::vector activation(geom::vector v); + static math::vector activation(math::vector v); template static void read(std::istream & is, T & x); @@ -562,14 +562,14 @@ void controller::mutate(RNG && rng, float amplitude) void controller::reset() {} -geom::vector controller::apply(system const & sys) const +math::vector controller::apply(system const & sys) const { if (sys.bones.size() * 7 != inputs) throw std::runtime_error(util::to_string("Wrong number of inputs: should be ", sys.bones.size() * 7)); if (sys.joints.size() != outputs) throw std::runtime_error(util::to_string("Wrong number of outputs: should be ", sys.joints.size())); - geom::vector input; + math::vector input; for (std::size_t i = 0; i < inputs / 7; ++i) { input[7 * i + 0] = sys.bones[i].position[0] - sys.bones[0].position[0]; @@ -630,7 +630,7 @@ float controller::activation(float x) } template -geom::vector controller::activation(geom::vector v) +math::vector controller::activation(math::vector v) { for (std::size_t i = 0; i < N; ++i) v[i] = activation(v[i]); @@ -640,12 +640,12 @@ geom::vector controller::activation(geom::vector v) controller lerp(controller const & c1, controller const & c2, float t) { controller c; - c.m1 = geom::lerp(c1.m1, c2.m1, t); -// c.m2 = geom::lerp(c1.m2, c2.m2, t); -// c.m3 = geom::lerp(c1.m3, c2.m3, t); - c.t1 = geom::lerp(c1.t1, c2.t1, t); -// c.t2 = geom::lerp(c1.t2, c2.t2, t); -// c.t3 = geom::lerp(c1.t3, c2.t3, t); + c.m1 = math::lerp(c1.m1, c2.m1, t); +// c.m2 = math::lerp(c1.m2, c2.m2, t); +// c.m3 = math::lerp(c1.m3, c2.m3, t); + c.t1 = math::lerp(c1.t1, c2.t1, t); +// c.t2 = math::lerp(c1.t2, c2.t2, t); +// c.t3 = math::lerp(c1.t3, c2.t3, t); c.generation = std::max(c1.generation, c2.generation) + 1; return c; } @@ -665,7 +665,7 @@ struct animation_2d_app void update_camera(); - geom::box view_bbox; + math::box view_bbox; bool centered = false; gfx::painter painter; @@ -700,7 +700,7 @@ struct animation_2d_app std::size_t train_iterations = 0; std::size_t const max_train_iterations = 1024*8; float const initial_variance = 10.f; - static constexpr auto mutation_amplitude = [](float t){ return std::pow(10.f, 1.f + geom::lerp(0.f, -2.f, t)); }; + static constexpr auto mutation_amplitude = [](float t){ return std::pow(10.f, 1.f + math::lerp(0.f, -2.f, t)); }; //Eigen::VectorXf mean; //Eigen::MatrixXf covariance; @@ -765,7 +765,7 @@ void animation_2d_app::update_camera() cx = ratio * view_bbox[1].length() / 2.f; } - view_bbox[0] = geom::expand(geom::interval::singleton(cx), ratio * view_bbox[1].length() / 2.f); + view_bbox[0] = math::expand(math::interval::singleton(cx), ratio * view_bbox[1].length() / 2.f); } void animation_2d_app::on_event(app::key_event const & event) @@ -809,14 +809,14 @@ void animation_2d_app::on_event(app::key_event const & event) float shiftx = random::uniform_distribution{-1.f, 1.f}(rng) * position_variation_amplitude; float shifty = random::uniform_distribution{0.f, 1.f}(rng) * position_variation_amplitude; - float angle = random::uniform_distribution{-1.f, 1.f}(rng) * geom::rad(angle_variation_amplitude); + float angle = random::uniform_distribution{-1.f, 1.f}(rng) * math::rad(angle_variation_amplitude); - geom::plane_rotation rot{0, 1, angle}; + math::plane_rotation rot{0, 1, angle}; float miny = 0.f; for (auto & b : physics.bones) { - b.position = rot(b.position) + geom::vector{shiftx, shifty}; + b.position = rot(b.position) + math::vector{shiftx, shifty}; b.direction = rot(b.direction); miny = std::min(miny, b.position[1] - std::abs(b.direction[1]) * b.length / 2.f - b.width / 2.f); } @@ -871,8 +871,8 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{0.5f, 0.f}, {1.f, 0.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); sys.bones.push_back(bone{{0.25f, 0.5f}, {0.5f, 1.f}, {0.f, 0.f}, 0.f, std::sqrt(1.25f), 0.25f}); sys.bones.push_back(bone{{0.75f, 1.5f}, {0.5f, 1.f}, {0.f, 0.f}, 0.f, std::sqrt(1.25f), 0.25f}); - sys.joints.push_back(joint{0, 1, -1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(-30.f), geom::rad(30.f)}}); + sys.joints.push_back(joint{0, 1, -1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(-30.f), math::rad(30.f)}}); //*/ /* @@ -884,10 +884,10 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{ 0.f, h*2.f }, {1.f, 0.f}, {0.f, 0.f}, 0.f, 2.f, 0.25f}); sys.bones.push_back(bone{{ s, h*1.5f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, h, 0.25f}); sys.bones.push_back(bone{{ s, h*0.5f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, h, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad( 0.f), geom::rad(45.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -s, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{2, 3, s, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{3, 4, 1.f, -1.f, geom::interval{geom::rad( 0.f), geom::rad(45.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad( 0.f), math::rad(45.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -s, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{2, 3, s, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{3, 4, 1.f, -1.f, math::interval{math::rad( 0.f), math::rad(45.f)}}); //*/ /* @@ -896,10 +896,10 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{1.f, 0.5f}, {0.f, 1.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); sys.bones.push_back(bone{{0.5f, 1.f}, {-1.f, 0.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); sys.bones.push_back(bone{{0.f, 0.5f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); - sys.joints.push_back(joint{2, 3, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); - sys.joints.push_back(joint{3, 0, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); + sys.joints.push_back(joint{2, 3, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); + sys.joints.push_back(joint{3, 0, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); //*/ /* @@ -908,10 +908,10 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{1.f, 0.5f}, {0.f, 1.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); sys.bones.push_back(bone{{0.5f, 1.f}, {-1.f, 0.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); sys.bones.push_back(bone{{0.f, 0.5f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); - sys.joints.push_back(joint{2, 3, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); - sys.joints.push_back(joint{3, 0, 1.f, -1.f, geom::interval{geom::rad(60.f), geom::rad(120.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); + sys.joints.push_back(joint{2, 3, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); + sys.joints.push_back(joint{3, 0, 1.f, -1.f, math::interval{math::rad(60.f), math::rad(120.f)}}); //*/ /* @@ -927,7 +927,7 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{-0.25f, std::sqrt(3.f) * 0.25f}, {0.5f, -std::sqrt(3.f) * 0.5}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); for (int i = 0; i < 2 * n + 4; ++i) - sys.joints.push_back(joint{i, (i + 1) % (2 * n + 4), 1.f, -1.f, geom::interval{geom::rad(0.f), geom::rad(60.f)}}); + sys.joints.push_back(joint{i, (i + 1) % (2 * n + 4), 1.f, -1.f, math::interval{math::rad(0.f), math::rad(60.f)}}); //*/ /* @@ -936,7 +936,7 @@ void animation_2d_app::reset_state(system & sys) const for (int i = 0; i < n; ++i) sys.bones.push_back(bone{{i + 0.5f, 0.f}, {1.f, 0.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); for (int i = 0; i + 1 < n; ++i) - sys.joints.push_back(joint{i, i + 1, 1.f, -1.f, geom::interval{geom::rad(-30.f), geom::rad(30.f)}}); + sys.joints.push_back(joint{i, i + 1, 1.f, -1.f, math::interval{math::rad(-30.f), math::rad(30.f)}}); //*/ @@ -951,11 +951,11 @@ void animation_2d_app::reset_state(system & sys) const } for (int i = 0; i + 1 < n; ++i) - sys.joints.push_back(joint{i, i + 1, 1.f, -1.f, geom::interval{geom::rad(-30.f), geom::rad(30.f)}}); + sys.joints.push_back(joint{i, i + 1, 1.f, -1.f, math::interval{math::rad(-30.f), math::rad(30.f)}}); for (int i = 0; i < n; ++i) { - sys.joints.push_back(joint{i, n + 2 * i + 0, -0.5f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{i, n + 2 * i + 1, 0.5f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); + sys.joints.push_back(joint{i, n + 2 * i + 0, -0.5f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{i, n + 2 * i + 1, 0.5f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); } //*/ @@ -965,9 +965,9 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{0.5f, 1.f}, {1.f, 0.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); sys.bones.push_back(bone{{1.5f, 1.f}, {1.f, 0.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); sys.bones.push_back(bone{{2.f, 0.5f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 1.f, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(-15.f), geom::rad( 15.f)}}); - sys.joints.push_back(joint{2, 3, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(-15.f), math::rad( 15.f)}}); + sys.joints.push_back(joint{2, 3, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); //*/ /* @@ -977,10 +977,10 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{ 0.f, 1.f }, {1.f, 0.f}, {0.f, 0.f}, 0.f, 2.f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.75f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.25f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad( 0.f), geom::rad(45.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{2, 3, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{3, 4, 1.f, -1.f, geom::interval{geom::rad( 0.f), geom::rad(45.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad( 0.f), math::rad(45.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{2, 3, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{3, 4, 1.f, -1.f, math::interval{math::rad( 0.f), math::rad(45.f)}}); //*/ /* @@ -990,10 +990,10 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{ 0.f, 1.f }, {1.f, 0.f}, {0.f, 0.f}, 0.f, 2.f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.75f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.25f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad(-45.f), geom::rad( 0.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{2, 3, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{3, 4, 1.f, -1.f, geom::interval{geom::rad(-45.f), geom::rad( 0.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad(-45.f), math::rad( 0.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{2, 3, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{3, 4, 1.f, -1.f, math::interval{math::rad(-45.f), math::rad( 0.f)}}); //*/ /* @@ -1003,10 +1003,10 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{ 0.f, 1.f }, {1.f, 0.f}, {0.f, 0.f}, 0.f, 2.f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.75f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.25f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad(-45.f), geom::rad( 0.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{2, 3, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{3, 4, 1.f, -1.f, geom::interval{geom::rad( 0.f), geom::rad( 45.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad(-45.f), math::rad( 0.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{2, 3, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{3, 4, 1.f, -1.f, math::interval{math::rad( 0.f), math::rad( 45.f)}}); //*/ /* @@ -1016,17 +1016,17 @@ void animation_2d_app::reset_state(system & sys) const sys.bones.push_back(bone{{ 0.f, 1.f }, {1.f, 0.f}, {0.f, 0.f}, 0.f, 2.f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.75f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); sys.bones.push_back(bone{{ 1.f, 0.25f}, {0.f, -1.f}, {0.f, 0.f}, 0.f, 0.5f, 0.25f}); - sys.joints.push_back(joint{0, 1, 1.f, -1.f, geom::interval{geom::rad( 0.f), geom::rad( 45.f)}}); - sys.joints.push_back(joint{1, 2, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{2, 3, 1.f, -1.f, geom::interval{geom::rad(240.f), geom::rad(300.f)}}); - sys.joints.push_back(joint{3, 4, 1.f, -1.f, geom::interval{geom::rad(-45.f), geom::rad( 0.f)}}); + sys.joints.push_back(joint{0, 1, 1.f, -1.f, math::interval{math::rad( 0.f), math::rad( 45.f)}}); + sys.joints.push_back(joint{1, 2, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{2, 3, 1.f, -1.f, math::interval{math::rad(240.f), math::rad(300.f)}}); + sys.joints.push_back(joint{3, 4, 1.f, -1.f, math::interval{math::rad(-45.f), math::rad( 0.f)}}); //*/ float const bone_density = 1.f; for (auto & b : sys.bones) { b.position[1] += 0.125f; - b.direction = geom::normalized(b.direction); + b.direction = math::normalized(b.direction); b.mass = b.length * b.width * bone_density; b.inertia = b.mass * (b.length * b.length + b.width * b.width) / 12.f; } @@ -1045,14 +1045,14 @@ float animation_2d_app::eval_score(controller const & c, random::generator rng) float shiftx = random::uniform_distribution{-1.f, 1.f}(rng) * position_variation_amplitude; float shifty = random::uniform_distribution{0.f, 1.f}(rng) * position_variation_amplitude; - float angle = random::uniform_distribution{-1.f, 1.f}(rng) * geom::rad(angle_variation_amplitude); + float angle = random::uniform_distribution{-1.f, 1.f}(rng) * math::rad(angle_variation_amplitude); - geom::plane_rotation rot{0, 1, angle}; + math::plane_rotation rot{0, 1, angle}; float miny = 0.f; for (auto & b : physics.bones) { - b.position = rot(b.position) + geom::vector{shiftx, shifty}; + b.position = rot(b.position) + math::vector{shiftx, shifty}; b.direction = rot(b.direction); miny = std::min(miny, b.position[1] - std::abs(b.direction[1]) * b.length / 2.f - b.width / 2.f); } @@ -1100,7 +1100,7 @@ float animation_2d_app::eval_score(controller const & c, random::generator rng) auto ctrl = c.apply(physics); for (std::size_t i = 0; i < ctrl.dimension(); ++i) torque[i] = ctrl[i]; - energy += geom::length(ctrl) * dt; + energy += math::length(ctrl) * dt; return torque; }); @@ -1119,12 +1119,12 @@ float animation_2d_app::eval_score(controller const & c, random::generator rng) } } -// penalty += geom::sqr((1.f - physics.bones[2].direction[0]) * dt); +// penalty += math::sqr((1.f - physics.bones[2].direction[0]) * dt); // float const target_speed = 2.f; -// penalty += geom::sqr((physics.bones[2].velocity[0] - target_speed) / target_speed) * dt; +// penalty += math::sqr((physics.bones[2].velocity[0] - target_speed) / target_speed) * dt; // penalty -= physics.bones[2].velocity[0] * dt / physics.bones; - auto cm_vel = geom::vector::zero(); + auto cm_vel = math::vector::zero(); float mass = 0.f; for (auto const & b : physics.bones) { @@ -1360,7 +1360,7 @@ void animation_2d_app::do_train() void animation_2d_app::do_test() { - geom::point mouse = view_bbox.corner(state().mouse[0] * 1.f / state().size[0], 1.f - state().mouse[1] * 1.f / state().size[1]); + math::point mouse = view_bbox.corner(state().mouse[0] * 1.f / state().size[0], 1.f - state().mouse[1] * 1.f / state().size[1]); if (state().mouse_button_down.contains(app::mouse_button::left)) { @@ -1379,18 +1379,18 @@ void animation_2d_app::do_test() auto d = mouse - p0; - float t = geom::dot(d, r) / geom::dot(r, r); + float t = math::dot(d, r) / math::dot(r, r); float distance; if (0.f <= t && t <= 1.f) { - distance = geom::length(d - r * t); + distance = math::length(d - r * t); } else { - float d0 = geom::distance(p0, mouse); - float d1 = geom::distance(p1, mouse); + float d0 = math::distance(p0, mouse); + float d1 = math::distance(p1, mouse); if (d0 < d1) { @@ -1440,7 +1440,7 @@ void animation_2d_app::do_test() } { - auto cm_vel = geom::vector::zero(); + auto cm_vel = math::vector::zero(); float mass = 0.f; for (auto const & b : physics.bones) { @@ -1491,7 +1491,7 @@ void animation_2d_app::present() } } - painter.render(geom::orthographic_camera{view_bbox}.transform()); + painter.render(math::orthographic_camera{view_bbox}.transform()); float avg_speed = 0.f; @@ -1509,8 +1509,8 @@ void animation_2d_app::present() for (std::size_t i = start; i + 1 < test_speeds.size(); ++i) { float const scale = 2.f; - geom::point p0{40.f + (i - start ) * step, 180.f - test_speeds[i ] * scale}; - geom::point p1{40.f + (i - start + 1) * step, 180.f - test_speeds[i + 1] * scale}; + math::point p0{40.f + (i - start ) * step, 180.f - test_speeds[i ] * scale}; + math::point p1{40.f + (i - start + 1) * step, 180.f - test_speeds[i + 1] * scale}; painter.line(p0, p1, 2.f, gfx::red, false); } } @@ -1530,7 +1530,7 @@ void animation_2d_app::present() // if (mode == mode::test && !test_speeds.empty()) painter.text({40.f, 136.f}, util::to_string("Speed: ", test_speeds.back()), opts); } - painter.render(geom::window_camera{state().size[0], state().size[1]}.transform()); + painter.render(math::window_camera{state().size[0], state().size[1]}.transform()); } } diff --git a/examples/audio.cpp b/examples/audio.cpp index ca0c343e..9841ac39 100644 --- a/examples/audio.cpp +++ b/examples/audio.cpp @@ -22,8 +22,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -68,7 +68,7 @@ static std::map const key_to_midi {app::keycode::RIGHTBRACKET, 91}, }; -static geom::interval const key_rows[3] = { +static math::interval const key_rows[3] = { {59, 68}, {69, 79}, {80, 91}, @@ -204,7 +204,7 @@ struct audio_app painter_.rect({{{x - s, x + s}, {r - w, r + w}}}, {0, 0, 255, 255}); } - painter_.render(geom::window_camera{state().size[0], state().size[1]}.transform()); + painter_.render(math::window_camera{state().size[0], state().size[1]}.transform()); } ~audio_app() override diff --git a/examples/cloud.cpp b/examples/cloud.cpp index 7adc2f3d..dec588fe 100644 --- a/examples/cloud.cpp +++ b/examples/cloud.cpp @@ -1,13 +1,13 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -54,16 +54,16 @@ auto barycenter(Container const & c) return barycenter(util::begin(c), util::end(c)); } -std::vector> intersection(geom::vector const & f, std::vector> const & vertices, std::vector> const & edges) +std::vector> intersection(math::vector const & f, std::vector> const & vertices, std::vector> const & edges) { - std::vector> points; + std::vector> points; for (auto e : edges) { auto p0 = vertices[e[0]]; auto p1 = vertices[e[1]]; - auto f0 = dot(f, geom::homogeneous(p0)); - auto f1 = dot(f, geom::homogeneous(p1)); + auto f0 = dot(f, math::homogeneous(p0)); + auto f1 = dot(f, math::homogeneous(p1)); if ((f0 >= 0.f) ^ (f1 >= 0.f)) { @@ -71,14 +71,14 @@ std::vector> intersection(geom::vector const & f } } - geom::vector const n { f[0], f[1], f[2] }; + math::vector const n { f[0], f[1], f[2] }; if (!points.empty()) { auto const & p0 = points[0]; - std::sort(points.begin() + 1, points.end(), [&](geom::point const & p1, geom::point const & p2){ - return geom::dot(geom::cross(p1 - p0, p2 - p0), n) > 0.f; + std::sort(points.begin() + 1, points.end(), [&](math::point const & p1, math::point const & p2){ + return math::dot(math::cross(p1 - p0, p2 - p0), n) > 0.f; }); } @@ -89,9 +89,9 @@ template struct mesh_builder { std::vector vertices; - std::vector> indices; + std::vector> indices; - void add(std::vector const & v, std::vector> const & i) + void add(std::vector const & v, std::vector> const & i) { Index const base = static_cast(vertices.size()); std::size_t begin = indices.size(); @@ -164,25 +164,25 @@ void main() struct cloud_app : app::application_base { - geom::spherical_camera camera; + math::spherical_camera camera; float step; float max_density = 2.f; - geom::interval harmonic_range; + math::interval harmonic_range; util::clock> clock; - geom::box bbox; - std::vector> bbox_vertices; - std::vector> bbox_edges; + math::box bbox; + std::vector> bbox_vertices; + std::vector> bbox_edges; gfx::mesh bbox_mesh; gfx::mesh slice_mesh; gfx::mesh light_mesh; - std::vector> dirs; + std::vector> dirs; gfx::simple_renderer renderer; @@ -193,12 +193,12 @@ struct cloud_app cloud_app(std::size_t size) { - geom::vector cloud_size{2 * size, size, size}; + math::vector cloud_size{2 * size, size, size}; bbox = {{{-2.f, 2.f}, {-1.f, 1.f}, {-1.f, 1.f}}}; step = bbox[0].length() / cloud_size[0]; - camera.fov_y = geom::rad(45.f); + camera.fov_y = math::rad(45.f); camera.near_clip = 0.01f; camera.far_clip = 100.f; @@ -207,15 +207,15 @@ struct cloud_app camera.elevation_angle = 0.f; camera.distance = 10.f; - bbox_vertices = geom::vertices(bbox); - bbox_edges = geom::edges(bbox); + bbox_vertices = math::vertices(bbox); + bbox_edges = math::edges(bbox); - bbox_mesh.setup>(); + bbox_mesh.setup>(); bbox_mesh.load(bbox_vertices, bbox_edges); - slice_mesh.setup, geom::vector>(); + slice_mesh.setup, math::vector>(); - light_mesh.setup>(); + light_mesh.setup>(); { async::threadpool bg("bg"); @@ -223,7 +223,7 @@ struct cloud_app random::generator rng(random::device{}); random::uniform_sphere_vector_distribution d; - std::vector, 3>> grad(4); + std::vector, 3>> grad(4); std::vector weights(grad.size()); float weight_sum = 0.f; @@ -245,7 +245,7 @@ struct cloud_app // can't use template argument deduction for first argument due to gcc bug // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89062 - geom::gradient g(std::make_pair(0.2f, 0.f), geom::easing_type::quadratic_out, std::pair{0.3f, max_density}); + math::gradient g(std::make_pair(0.2f, 0.f), math::easing_type::quadratic_out, std::pair{0.3f, max_density}); util::array cloud_data({cloud_size[0], cloud_size[1], cloud_size[2]}); @@ -256,17 +256,17 @@ struct cloud_app bg.post([&, z, y]{ for (std::size_t x = 0; x < cloud_data.width(); ++x) { - geom::vector p; + math::vector p; p[0] = (x * 1.f) / cloud_data.width(); p[1] = (y * 1.f) / cloud_data.height(); p[2] = (z * 1.f) / cloud_data.depth(); float v = perlin(p); - auto d = p - geom::vector{0.5f, 0.5f, 0.5f}; - v *= std::max(0.f, 1.f - geom::length(d) / 0.5f); + auto d = p - math::vector{0.5f, 0.5f, 0.5f}; + v *= std::max(0.f, 1.f - math::length(d) / 0.5f); - cloud_data(x, y, z) = geom::clamp(g(v) / max_density * 255.f, {0.f, 255.f}); + cloud_data(x, y, z) = math::clamp(g(v) / max_density * 255.f, {0.f, 255.f}); } }); } @@ -280,9 +280,9 @@ struct cloud_app cloud_texture.linear_filter(); cloud_texture.generate_mipmap(); - auto value_at = [this, &cloud_data](geom::point const & p) + auto value_at = [this, &cloud_data](math::point const & p) { - geom::vector d; + math::vector d; d[0] = (p[0] - bbox[0].min) / bbox[0].length() * cloud_data.width(); d[1] = (p[1] - bbox[1].min) / bbox[1].length() * cloud_data.height(); d[2] = (p[2] - bbox[2].min) / bbox[2].length() * cloud_data.depth(); @@ -298,12 +298,12 @@ struct cloud_app if (d[2] < 0.0f) return 0.f; if (d[2] >= cloud_data.depth() - 1) return 0.f; - geom::vector i; + math::vector i; i[0] = std::floor(d[0]); i[1] = std::floor(d[1]); i[2] = std::floor(d[2]); - geom::vector t; + math::vector t; t[0] = d[0] - i[0]; t[1] = d[1] - i[1]; t[2] = d[2] - i[2]; @@ -317,18 +317,18 @@ struct cloud_app float v011 = cloud_data(i[0], i[1] + 1, i[2] + 1); float v111 = cloud_data(i[0] + 1, i[1] + 1, i[2] + 1); - float v00 = geom::lerp(v000, v100, t[0]); - float v10 = geom::lerp(v010, v110, t[0]); - float v01 = geom::lerp(v001, v101, t[0]); - float v11 = geom::lerp(v011, v111, t[0]); + float v00 = math::lerp(v000, v100, t[0]); + float v10 = math::lerp(v010, v110, t[0]); + float v01 = math::lerp(v001, v101, t[0]); + float v11 = math::lerp(v011, v111, t[0]); - float v0 = geom::lerp(v00, v10, t[1]); - float v1 = geom::lerp(v01, v11, t[1]); + float v0 = math::lerp(v00, v10, t[1]); + float v1 = math::lerp(v01, v11, t[1]); - return geom::lerp(v0, v1, t[2]) / 255.f * max_density; + return math::lerp(v0, v1, t[2]) / 255.f * max_density; }; - util::array, 3> cloud_shadow_f(cloud_data.dims()); + util::array, 3> cloud_shadow_f(cloud_data.dims()); dirs.resize(32); @@ -336,7 +336,7 @@ struct cloud_app float const phi = (1.f + std::sqrt(5.f)) / 2.f; for (int i = 0; i < dirs.size(); ++i) { - float a = 2.f * geom::pi * (i / phi); + float a = 2.f * math::pi * (i / phi); float b = std::acos(1.f - (2.f * i) / dirs.size()); dirs[i][0] = std::cos(a) * std::sin(b); @@ -344,14 +344,14 @@ struct cloud_app dirs[i][2] = std::cos(b); } - float diag = std::sqrt(geom::sqr(bbox[0].length()) + geom::sqr(bbox[1].length()) + geom::sqr(bbox[2].length())); + float diag = std::sqrt(math::sqr(bbox[0].length()) + math::sqr(bbox[1].length()) + math::sqr(bbox[2].length())); int steps = diag / step; auto process = [&, this](auto const & idx) { float const step = bbox[0].length() / cloud_size[0]; - geom::point o; + math::point o; for (std::size_t i = 0; i < 3; ++i) o[i] = bbox[i].min + bbox[i].length() * (idx[i] + 0.5f) / cloud_shadow_f.dim(i); @@ -364,7 +364,7 @@ struct cloud_app { float density = 0.f; - geom::point p = o + dir * (steps * step); + math::point p = o + dir * (steps * step); for (int s = steps; s > 0; --s) { @@ -374,10 +374,10 @@ struct cloud_app p -= dir * step; } - sum_0 += density * 2.f * std::sqrt(geom::pi); - sum_x += density * dir[0] * std::sqrt(12.f * geom::pi); - sum_y += density * dir[1] * std::sqrt(12.f * geom::pi); - sum_z += density * dir[2] * std::sqrt(12.f * geom::pi); + sum_0 += density * 2.f * std::sqrt(math::pi); + sum_x += density * dir[0] * std::sqrt(12.f * math::pi); + sum_y += density * dir[1] * std::sqrt(12.f * math::pi); + sum_z += density * dir[2] * std::sqrt(12.f * math::pi); ++count; } @@ -414,8 +414,8 @@ struct cloud_app log::info() << "Finished!"; - geom::interval range_0; - geom::interval range_d; + math::interval range_0; + math::interval range_d; for (auto const & v : cloud_shadow_f) { @@ -448,14 +448,14 @@ struct cloud_app } } - util::array, 3> cloud_shadow(cloud_data.dims()); + util::array, 3> cloud_shadow(cloud_data.dims()); for (auto const & idx : cloud_shadow.indices()) { for (std::size_t i = 0; i < 4; ++i) { float v = (cloud_shadow_f(idx)[i] - harmonic_range.min) / harmonic_range.length(); - cloud_shadow(idx)[i] = geom::clamp(255.f * v, {0.f, 255.f}); + cloud_shadow(idx)[i] = math::clamp(255.f * v, {0.f, 255.f}); } } @@ -500,21 +500,21 @@ struct cloud_app void update_slice_mesh() { auto n = -camera.direction(); - geom::vector f {n[0], n[1], n[2], 0.f}; + math::vector f {n[0], n[1], n[2], 0.f}; - geom::interval range; + math::interval range; for (auto p : bbox_vertices) { - range |= geom::dot(f, geom::homogeneous(p)); + range |= math::dot(f, math::homogeneous(p)); } int count = std::ceil(range.length() / step); struct vertex { - geom::point pos; - geom::vector tc; + math::point pos; + math::vector tc; }; mesh_builder builder; @@ -524,7 +524,7 @@ struct cloud_app f[3] = - (range.max - (range.length() * s) / count); auto slice_vertices = intersection(f, bbox_vertices, bbox_edges); - auto slice_indices = geom::triangulate_convex(slice_vertices); + auto slice_indices = math::triangulate_convex(slice_vertices); std::vector vertices; for (auto p : slice_vertices) @@ -550,11 +550,11 @@ struct cloud_app float t = clock.count(); - geom::vector light_dir = geom::normalized(geom::vector{std::cos(t), std::sin(t), 0.5f}); + math::vector light_dir = math::normalized(math::vector{std::cos(t), std::sin(t), 0.5f}); { - std::vector> light_vertices; - auto o = geom::point::zero() + light_dir * 4.f; + std::vector> light_vertices; + auto o = math::point::zero() + light_dir * 4.f; float s = 0.2f; for (auto d : dirs) { @@ -591,7 +591,7 @@ struct cloud_app cloud_program["u_max_density"] = max_density; cloud_program["u_min_harmonic"] = harmonic_range.min; cloud_program["u_max_harmonic"] = harmonic_range.max; - cloud_program["u_light"] = geom::normalized(light_dir); + cloud_program["u_light"] = math::normalized(light_dir); gl::ActiveTexture(gl::TEXTURE0); cloud_texture.bind(); gl::ActiveTexture(gl::TEXTURE1); diff --git a/examples/deferred.cpp b/examples/deferred.cpp index 1e02c997..1455c059 100644 --- a/examples/deferred.cpp +++ b/examples/deferred.cpp @@ -6,12 +6,12 @@ #include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include #include @@ -44,7 +44,7 @@ struct deferred_app gfx::texture_2d pre_fxaa_texture; gfx::fxaa fxaa; - geom::spherical_camera camera; + math::spherical_camera camera; gfx::mesh plane; gfx::mesh cube; @@ -64,13 +64,13 @@ deferred_app::deferred_app(options const &, context const & context) { context.vsync(false); - camera.fov_y = geom::rad(45.f); + camera.fov_y = math::rad(45.f); camera.near_clip = 0.1f; camera.far_clip = 1000.f; camera.target = {0.f, 0.f, 0.f}; camera.distance = 20.f; camera.azimuthal_angle = 0.f; - camera.elevation_angle = geom::rad(30.f); + camera.elevation_angle = math::rad(30.f); pre_gamma_texture.linear_filter(); pre_fxaa_texture.linear_filter(); @@ -85,10 +85,10 @@ deferred_app::deferred_app(options const &, context const & context) struct vertex { - geom::point position; + math::point position; gfx::color_rgba color; - geom::vector texcoord; - geom::vector normal; + math::vector texcoord; + math::vector normal; static auto attribs() { @@ -98,7 +98,7 @@ deferred_app::deferred_app(options const &, context const & context) struct instance { - geom::matrix transform; + math::matrix transform; static auto attribs() { @@ -115,7 +115,7 @@ deferred_app::deferred_app(options const &, context const & context) vertices.push_back({{-1.f, 1.f, 0.f}, color, {0.f, 0.f}, {0.f, 0.f, 1.f}}); vertices.push_back({{ 1.f, 1.f, 0.f}, color, {0.f, 0.f}, {0.f, 0.f, 1.f}}); - std::vector> indices; + std::vector> indices; indices.push_back({0, 1, 2}); indices.push_back({2, 1, 3}); @@ -124,17 +124,17 @@ deferred_app::deferred_app(options const &, context const & context) } { - auto box = geom::box{{{-1.f, 1.f}, {-1.f, 1.f}, {-1.f, 1.f}}}; + auto box = math::box{{{-1.f, 1.f}, {-1.f, 1.f}, {-1.f, 1.f}}}; - auto triangles = geom::deindex(geom::vertices(box), geom::faces(box)); + auto triangles = math::deindex(math::vertices(box), math::faces(box)); - std::vector> vertices; + std::vector> vertices; for (auto const & t : triangles) { auto & r = vertices.emplace_back(); - auto n = geom::normal(t[0], t[1], t[2]); + auto n = math::normal(t[0], t[1], t[2]); std::size_t tcm = 0; if (std::abs(n[1]) > std::abs(n[tcm])) tcm = 1; @@ -162,7 +162,7 @@ deferred_app::deferred_app(options const &, context const & context) { std::vector vertices; - geom::point const origin {0.f, 0.f, 0.f}; + math::point const origin {0.f, 0.f, 0.f}; float const radius = 1.f; @@ -174,19 +174,19 @@ deferred_app::deferred_app(options const &, context const & context) { for (int i = 0; i < 4 * N; ++i) { - float a = (geom::pi * i) / (2 * N); - float b = (geom::pi * j) / (2 * N); + float a = (math::pi * i) / (2 * N); + float b = (math::pi * j) / (2 * N); - geom::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; + math::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; vertices.push_back({origin + radius * n, c, {0.f, 0.f}, n}); } } - vertices.push_back({origin + geom::vector{0.f, 0.f, -radius}, c, {0.f, 0.f}, {0.f, 0.f, -1.f}}); - vertices.push_back({origin + geom::vector{0.f, 0.f, radius}, c, {0.f, 0.f}, {0.f, 0.f, 1.f}}); + vertices.push_back({origin + math::vector{0.f, 0.f, -radius}, c, {0.f, 0.f}, {0.f, 0.f, -1.f}}); + vertices.push_back({origin + math::vector{0.f, 0.f, radius}, c, {0.f, 0.f}, {0.f, 0.f, 1.f}}); - std::vector> indices; + std::vector> indices; auto idx = [](int i, int j) -> std::uint32_t { return (i % (4 * N)) + 4 * N * (j + N - 1); }; @@ -216,7 +216,7 @@ deferred_app::deferred_app(options const &, context const & context) { std::vector vertices; - geom::point const position = {0.f, 0.f, 0.f}; + math::point const position = {0.f, 0.f, 0.f}; float const radius1 = 0.8f; float const radius2 = 0.2f; @@ -230,18 +230,18 @@ deferred_app::deferred_app(options const &, context const & context) { for (int i = 0; i < N; ++i) { - float a = (2.f * geom::pi * i) / N; - float b = (2.f * geom::pi * j) / M; + float a = (2.f * math::pi * i) / N; + float b = (2.f * math::pi * j) / M; - geom::vector r{std::cos(a), std::sin(a), 0.f}; + math::vector r{std::cos(a), std::sin(a), 0.f}; - geom::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; + math::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; vertices.push_back({position + radius1 * r + radius2 * n, color, {0.f, 0.f}, n}); } } - std::vector> indices; + std::vector> indices; auto idx = [](int i, int j) -> std::uint32_t { return (i % N) + N * (j % M); }; @@ -295,11 +295,11 @@ void deferred_app::on_event(app::resize_event const & event) app::application_base::on_event(event); camera.set_fov(camera.fov_y, static_cast(event.size[0]) / event.size[1]); - pre_gamma_texture.load>(geom::cast(event.size)); + pre_gamma_texture.load>(math::cast(event.size)); pre_gamma_framebuffer.color(pre_gamma_texture); pre_gamma_framebuffer.assert_complete(); - pre_fxaa_texture.load(geom::cast(event.size)); + pre_fxaa_texture.load(math::cast(event.size)); pre_fxaa_framebuffer.color(pre_fxaa_texture); pre_fxaa_framebuffer.assert_complete(); } @@ -343,7 +343,7 @@ void deferred_app::present() { gfx::deferred_renderer::object obj; obj.mesh = &plane; - obj.pre_transform = geom::scale(10.f).affine_matrix(); + obj.pre_transform = math::scale(10.f).affine_matrix(); obj.bbox = {{{-10.f, 10.f}, {-10.f, 10.f}, {0.f, 0.f}}}; obj.mat = &plane_material; objects.push_back(obj); @@ -358,7 +358,7 @@ void deferred_app::present() { gfx::deferred_renderer::object obj; obj.mesh = &cube; - obj.pre_transform = geom::translation{geom::vector{x, y, 3.f}}.affine_matrix(); + obj.pre_transform = math::translation{math::vector{x, y, 3.f}}.affine_matrix(); obj.bbox = {{{x - 1.f, x + 1.f}, {y - 1.f, y + 1.f}, {2.f, 4.f}}}; obj.mat = &cube_material; objects.push_back(obj); @@ -374,7 +374,7 @@ void deferred_app::present() { gfx::deferred_renderer::object obj; obj.mesh = &sphere; - obj.pre_transform = geom::translation{geom::vector{0.f, 0.f, z}}.affine_matrix(); + obj.pre_transform = math::translation{math::vector{0.f, 0.f, z}}.affine_matrix(); obj.bbox = {{{-1.f, 1.f}, {-1.f, 1.f}, {z - 1.f, z + 1.f}}}; obj.mat = &sphere_material; objects.push_back(obj); @@ -383,7 +383,7 @@ void deferred_app::present() { gfx::deferred_renderer::object obj; obj.mesh = &torus; - obj.pre_transform = geom::translation{geom::vector{0.f, 0.f, 4.f}}.affine_matrix(); + obj.pre_transform = math::translation{math::vector{0.f, 0.f, 4.f}}.affine_matrix(); obj.bbox = {{{-1.f, 1.f}, {-1.f, 1.f}, {4 - 0.2f, 4 + 0.2f}}}; obj.mat = &sphere_material; objects.push_back(obj); @@ -392,7 +392,7 @@ void deferred_app::present() gfx::deferred_renderer::options options; options.camera = &camera; - options.clear_color = geom::vector{0.f, 0.f, 0.1f}; + options.clear_color = math::vector{0.f, 0.f, 0.1f}; options.ambient = {1.f, 1.f, 1.f}; options.directional_lights.emplace_back(); @@ -419,7 +419,7 @@ void deferred_app::present() for (int i = 0; i < 24; ++i) { - float a = (i * geom::pi) / 12.f; + float a = (i * math::pi) / 12.f; auto & l = options.point_lights.emplace_back(); l.color = {15.f, 15.f, 15.f}; @@ -437,9 +437,9 @@ void deferred_app::present() float const s = 0.1f; gfx::deferred_renderer::object obj; obj.mesh = &sphere; - obj.pre_transform = (geom::translation{l.position - geom::point::zero()}.transform() * geom::scale(s).transform()).affine_matrix(); + obj.pre_transform = (math::translation{l.position - math::point::zero()}.transform() * math::scale(s).transform()).affine_matrix(); obj.bbox = {{{l.position[0] - s, l.position[0] + s}, {l.position[1] - s, l.position[1] + s}, {l.position[2] - s, l.position[2] + s}}}; - light_materials[i].color = geom::vector{l.color[0], l.color[1], l.color[2], 1.f}; + light_materials[i].color = math::vector{l.color[0], l.color[1], l.color[2], 1.f}; light_materials[i].lit = false; light_materials[i].casts_shadow = false; obj.mat = &light_materials[i]; @@ -495,7 +495,7 @@ void deferred_app::present() gl::Enable(gl::BLEND); gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA); - painter.render(geom::window_camera{state().size[0], state().size[1]}.transform()); + painter.render(math::window_camera{state().size[0], state().size[1]}.transform()); } namespace psemek::app diff --git a/examples/fibonacci_music_box.cpp b/examples/fibonacci_music_box.cpp index f28dd288..176bff5a 100644 --- a/examples/fibonacci_music_box.cpp +++ b/examples/fibonacci_music_box.cpp @@ -30,11 +30,11 @@ using namespace psemek; -std::vector>> fibonacci_cycles(int n) +std::vector>> fibonacci_cycles(int n) { util::array cycle({n, n}, -1); - std::vector>> result; + std::vector>> result; for (int i = 0; i < n; ++i) { @@ -46,14 +46,14 @@ std::vector>> fibonacci_cycles(int n) int cycle_id = result.size(); auto & current = result.emplace_back(); - geom::point p{i, j}; + math::point p{i, j}; while (true) { current.push_back(p); cycle(p[0], p[1]) = cycle_id; p = {p[1], (p[0] + p[1]) % n}; - if (p == geom::point{i, j}) + if (p == math::point{i, j}) break; } } @@ -77,7 +77,7 @@ std::shared_ptr note(int i, float duration) return audio::fade_in(audio::fade_out(audio::karplus_strong(audio::midi_frequency(69 + i)), duration), duration / 16.f); } -std::shared_ptr generate(std::vector const & scale, std::vector> const & cycle, float speed) +std::shared_ptr generate(std::vector const & scale, std::vector> const & cycle, float speed) { auto mixer = audio::make_mixer(); diff --git a/examples/fire.cpp b/examples/fire.cpp index 13c99e9a..db1b350b 100644 --- a/examples/fire.cpp +++ b/examples/fire.cpp @@ -3,13 +3,13 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include #include @@ -81,17 +81,17 @@ struct candle_renderer { candle_renderer(); - void add(geom::point const & pos, geom::vector const & dir, float size); + void add(math::point const & pos, math::vector const & dir, float size); - void render(geom::camera const & camera, float time); + void render(math::camera const & camera, float time); private: struct candle { - geom::point pos; - geom::vector dir; + math::point pos; + math::vector dir; float size; - geom::vector noise_offset; + math::vector noise_offset; }; std::vector candles_; @@ -109,7 +109,7 @@ private: candle_renderer::candle_renderer() { - std::vector> vertices; + std::vector> vertices; vertices.push_back({-1.f, 0.f, -1.f}); vertices.push_back({ 1.f, 0.f, -1.f}); vertices.push_back({ 1.f, 0.f, 1.f}); @@ -117,25 +117,25 @@ candle_renderer::candle_renderer() vertices.push_back({ 1.f, 0.f, 1.f}); vertices.push_back({-1.f, 0.f, 1.f}); - mesh_.setup, gfx::instanced>, gfx::instanced>, gfx::instanced, gfx::instanced>>(); + mesh_.setup, gfx::instanced>, gfx::instanced>, gfx::instanced, gfx::instanced>>(); mesh_.load(vertices, gl::TRIANGLES); - geom::vector c0 {1.f, 0.99f, 0.98f, 1.f}; - geom::vector c1 {1.f, 0.4f, 0.f, 0.75f}; - geom::vector c2 {c1[0], c1[1], c1[2], 0.f}; + math::vector c0 {1.f, 0.99f, 0.98f, 1.f}; + math::vector c1 {1.f, 0.4f, 0.f, 0.75f}; + math::vector c2 {c1[0], c1[1], c1[2], 0.f}; - geom::gradient gy{ + math::gradient gy{ std::make_pair(0.f, 0.f), - geom::easing_type::quadratic_in, + math::easing_type::quadratic_in, std::make_pair(1.f, 2.f), }; - geom::gradient> gc + math::gradient> gc { std::make_pair(0.1f, c0), - geom::easing_type::linear, + math::easing_type::linear, std::pair{0.8f, c1}, - geom::easing_type::cubic, + math::easing_type::cubic, std::pair{1.2f, c2} }; @@ -172,7 +172,7 @@ candle_renderer::candle_renderer() random::generator rng; random::uniform_sphere_vector_distribution d; - util::array, 2> grad({16, 16}); + util::array, 2> grad({16, 16}); for (auto & v : grad) v = d(rng); pcg::perlin perlinx(grad, pcg::seamless); @@ -180,13 +180,13 @@ candle_renderer::candle_renderer() for (auto & v : grad) v = d(rng); pcg::perlin perliny(grad, pcg::seamless); - gfx::basic_pixmap> pm({512, 512}); + gfx::basic_pixmap> pm({512, 512}); for (auto idx : pm.indices()) { float x = (0.5f + idx[0]) / pm.width(); float y = (0.5f + idx[1]) / pm.height(); - pm(idx) = gfx::to_coloru8(geom::vector{perlinx({x, y}), perliny({x, y})}); + pm(idx) = gfx::to_coloru8(math::vector{perlinx({x, y}), perliny({x, y})}); } noise_texture_.load(pm); noise_texture_.linear_filter(); @@ -196,11 +196,11 @@ candle_renderer::candle_renderer() } } -void candle_renderer::add(geom::point const & pos, geom::vector const & dir, float size) +void candle_renderer::add(math::point const & pos, math::vector const & dir, float size) { random::generator rng{random::device{}}; random::uniform_sphere_vector_distribution d; - geom::vector noise_offset; + math::vector noise_offset; while (true) { noise_offset = d(rng); @@ -215,7 +215,7 @@ void candle_renderer::add(geom::point const & pos, geom::vector pos; - geom::vector dir; + math::point pos; + math::vector dir; float size; - geom::vector noise_offset; + math::vector noise_offset; }; std::vector instances; @@ -256,7 +256,7 @@ void candle_renderer::update_instances() struct fire_app : app::application_base { - geom::spherical_camera camera; + math::spherical_camera camera; candle_renderer candles; @@ -265,7 +265,7 @@ struct fire_app fire_app(options const &, context const &) { - camera.fov_y = geom::rad(45.f); + camera.fov_y = math::rad(45.f); camera.near_clip = 0.01f; camera.far_clip = 1000.f; camera.target = {0.f, 0.f, 0.f}; diff --git a/examples/gravity.cpp b/examples/gravity.cpp index 3cbd7b25..a7d3da03 100644 --- a/examples/gravity.cpp +++ b/examples/gravity.cpp @@ -2,9 +2,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -49,8 +49,8 @@ using namespace psemek; struct particle { - geom::point pos; - geom::vector vel; + math::point pos; + math::vector vel; float angle; float angle_vel; @@ -59,12 +59,12 @@ struct particle float mass; float density; - geom::vector delta_pos{0.f, 0.f}; - geom::vector delta_vel{0.f, 0.f}; + math::vector delta_pos{0.f, 0.f}; + math::vector delta_vel{0.f, 0.f}; - geom::point old_pos{0.f, 0.f}; + math::point old_pos{0.f, 0.f}; - geom::vector acc{0.f, 0.f}; + math::vector acc{0.f, 0.f}; float T = 0.f; }; @@ -78,7 +78,7 @@ float const FR = 0.99f; float const dt = 0.01f; float const world_size = 10000000.f; -geom::point world_center{0.f, 0.f}; +math::point world_center{0.f, 0.f}; int const SOLVE_ITERATIONS = 16; float const BIAS = 1.f / 8.f; @@ -96,7 +96,7 @@ struct myapp std::uniform_real_distribution d{-50.f, 50.f}; std::uniform_real_distribution rr{0.5f, 2.f}; std::uniform_real_distribution rden{0.25f, 1.f}; - std::uniform_real_distribution ra{0.f, 2.f * geom::pi}; + std::uniform_real_distribution ra{0.f, 2.f * math::pi}; float min_R = 0.f; float max_R = 100.f; @@ -105,7 +105,7 @@ struct myapp bool star = false; if (star) - particles_.push_back({{0.f, 0.f}, {0.f, 0.f}, 0.f, 0.f, 10.f, geom::pi * 10000.f, 1.f}); + particles_.push_back({{0.f, 0.f}, {0.f, 0.f}, 0.f, 0.f, 10.f, math::pi * 10000.f, 1.f}); // particles_.push_back({{-2.f, 0.f}, {0.f, 0.f}, 0.f, 0.f, 1.f, 1.f, 1.f}); // particles_.push_back({{ 2.f, 0.f}, {0.f, 0.f}, 0.f, 0.f, 1.f, 1.f, 1.f}); @@ -113,14 +113,14 @@ struct myapp // particles_.push_back({{ 0.f, -3.f}, {0.f, 0.f}, 0.f, 0.f, 1.f, 1.f, 1.f}); // float planet_R[] = {200.f, 300.f, 400.f}; -// float planet_a[] = {0.f, geom::rad(120.f), geom::rad(240.f)}; +// float planet_a[] = {0.f, math::rad(120.f), math::rad(240.f)}; // if(false) for (int i = 0; i < 500; ++i) { - geom::vector v{0.f, 0.f}; + math::vector v{0.f, 0.f}; - geom::point p; + math::point p; float m; float r; float den; @@ -130,7 +130,7 @@ struct myapp r = rr(rng_); den = rden(rng_); - m = geom::pi * r * r * den; + m = math::pi * r * r * den; auto a = ra(rng_); float R = std::sqrt(rR(rng_)) * (max_R - min_R) + min_R; @@ -142,18 +142,18 @@ struct myapp // R = planet_R[pl]; // R += std::uniform_real_distribution(-10.f, 10.f)(rng_); -// a += geom::rad(std::uniform_real_distribution(-2.f, 2.f)(rng_)); +// a += math::rad(std::uniform_real_distribution(-2.f, 2.f)(rng_)); p = { R * std::cos(a), R * std::sin(a), }; -// p += geom::vector{((i % 2) ? -1000.f : 1000.f), 0.f}; +// p += math::vector{((i % 2) ? -1000.f : 1000.f), 0.f}; // v[0] = {(i % 2) ? 100.f : -100.f}; - if (std::all_of(particles_.begin(), particles_.end(), [&](particle const & q){ return geom::distance(q.pos, p) > q.radius + r; })) + if (std::all_of(particles_.begin(), particles_.end(), [&](particle const & q){ return math::distance(q.pos, p) > q.radius + r; })) break; } particles_.push_back({p, v, 0.f, 0.f, r, m, den}); @@ -168,10 +168,10 @@ struct myapp if(false) for (std::size_t i = star ? 1 : 0; i < particles_.size(); ++i) { - auto r = particles_[i].pos - geom::point{0.f, 0.f}; - auto R = geom::length(r); + auto r = particles_[i].pos - math::point{0.f, 0.f}; + auto R = math::length(r); float V = std::sqrt(G * total_M / R); - particles_[i].vel = geom::ort(r / R) * V; + particles_[i].vel = math::ort(r / R) * V; (void)V; } @@ -199,24 +199,24 @@ struct myapp { if (event.button == app::mouse_button::left && event.down) { - geom::scale const flip_y({1.f, -1.f}); + math::scale const flip_y({1.f, -1.f}); float const scale = camera_size_ / state().size[1]; - geom::point const screen_center { state().size[0] / 2,state().size[1] / 2 }; - auto target = camera_center_ + flip_y(geom::cast(state().mouse - screen_center) * scale); + math::point const screen_center { state().size[0] / 2,state().size[1] / 2 }; + auto target = camera_center_ + flip_y(math::cast(state().mouse - screen_center) * scale); force_target_ = target; -// geom::point pos{100.f, 0.f}; +// math::point pos{100.f, 0.f}; -// geom::vector vel = geom::normalized(target - pos) * 40.f; +// math::vector vel = math::normalized(target - pos) * 40.f; // particles_.push_back({pos, vel, 1.f, 1.f}); // for (auto & p : particles_) // { // auto r = p.pos - target; -// p.vel += 4000.f * r / geom::length_sqr(r) / p.mass; +// p.vel += 4000.f * r / math::length_sqr(r) / p.mass; // } } @@ -242,20 +242,20 @@ struct myapp if (camera_drag_) { - geom::scale const flip_y({1.f, -1.f}); + math::scale const flip_y({1.f, -1.f}); float const scale = camera_size_ / state().size[1]; - camera_center_ += flip_y(geom::cast(*camera_drag_ - event.position) * scale); + camera_center_ += flip_y(math::cast(*camera_drag_ - event.position) * scale); camera_drag_ = event.position; } if (force_target_) { - geom::scale const flip_y({1.f, -1.f}); + math::scale const flip_y({1.f, -1.f}); float const scale = camera_size_ / state().size[1]; - geom::point const screen_center { state().size[0] / 2, state().size[1] / 2 }; - auto target = camera_center_ + flip_y(geom::cast(state().mouse - screen_center) * scale); + math::point const screen_center { state().size[0] / 2, state().size[1] / 2 }; + auto target = camera_center_ + flip_y(math::cast(state().mouse - screen_center) * scale); force_target_ = target; } } @@ -290,18 +290,18 @@ struct myapp p.acc = {0.f, 0.f}; for (auto & p : particles_) - p.acc += geom::vector{0.f, -GG}; + p.acc += math::vector{0.f, -GG}; for (auto & p : particles_) { auto r = (p.pos - p.pos.zero()); - p.acc -= GC * r / std::pow(1.f + geom::length(r), 3.f); + p.acc -= GC * r / std::pow(1.f + math::length(r), 3.f); } // for (std::size_t i = 0; i < particles_.size(); ++i) // { // log::info() << "Start: #" << i << " pos = " << std::setprecision(10) << particles_[i].pos << ", vel = " << particles_[i].vel -// << ", |vel| = " << geom::length(particles_[i].vel); +// << ", |vel| = " << math::length(particles_[i].vel); // } for (std::size_t i = 0; i < particles_.size(); ++i) @@ -348,7 +348,7 @@ struct myapp // auto vij = particles_[i].vel - particles_[j].vel; -// auto vn = geom::dot(vij, n) * n; +// auto vn = math::dot(vij, n) * n; // particles_[i].acc -= vn * particles_[j].mass / (particles_[i].mass + particles_[j].mass); // particles_[j].acc += vn * particles_[i].mass / (particles_[i].mass + particles_[j].mass); @@ -374,7 +374,7 @@ struct myapp for (auto & p : particles_) { auto r = p.pos - *force_target_; - p.acc += 100000.f * r / geom::length_sqr(r) / p.mass; + p.acc += 100000.f * r / math::length_sqr(r) / p.mass; } } @@ -420,14 +420,14 @@ struct myapp // New iterative algorithm if (false) { - std::vector> old_pos(particles_.size()); + std::vector> old_pos(particles_.size()); for (std::size_t i = 0; i < particles_.size(); ++i) old_pos[i] = particles_[i].pos; struct collision { std::size_t i, j; - geom::vector n; // i -> j + math::vector n; // i -> j }; std::vector collisions; @@ -452,7 +452,7 @@ struct myapp { auto const r = particles_[c.j].pos - particles_[c.i].pos; auto const R = particles_[c.j].radius + particles_[c.i].radius; - auto const l = geom::dot(r, c.n); + auto const l = math::dot(r, c.n); if (l < R) { @@ -488,9 +488,9 @@ struct myapp auto dv = particles_[i].vel - particles_[j].vel; auto R = particles_[i].radius + particles_[j].radius; - float A = geom::dot(dv, dv); - float B = geom::dot(dp, dv); - float C = geom::dot(dp, dp) - geom::sqr(R); + float A = math::dot(dv, dv); + float B = math::dot(dp, dv); + float C = math::dot(dp, dp) - math::sqr(R); if (B >= 0.f) return; @@ -522,7 +522,7 @@ struct myapp // particle_time[i] = e.t; // particle_time[j] = e.t; - auto n = geom::normalized(particles_[i].pos - particles_[j].pos); + auto n = math::normalized(particles_[i].pos - particles_[j].pos); float S = 0.5f * (1.f / particles_[i].mass + 1.f / particles_[j].mass); float T = dot(n, particles_[i].vel - particles_[j].vel); @@ -569,7 +569,7 @@ struct myapp std::vector particle_time(particles_.size(), 0.f); - std::unordered_map, std::vector> cells; + std::unordered_map, std::vector> cells; float time = 0.f; @@ -584,9 +584,9 @@ struct myapp auto dv = particles_[i].vel - particles_[j].vel; auto R = particles_[i].radius + particles_[j].radius; - float A = geom::dot(dv, dv); - float B = geom::dot(dp, dv); - float C = geom::dot(dp, dp) - geom::sqr(R); + float A = math::dot(dv, dv); + float B = math::dot(dp, dv); + float C = math::dot(dp, dp) - math::sqr(R); // log::info() << "DV = " << dv; // log::info() << "DP = " << dp; @@ -597,7 +597,7 @@ struct myapp // if (B > 0.f) return; -// auto result = geom::solve_quadratic(A, B, C); +// auto result = math::solve_quadratic(A, B, C); // if (!result) return; // log::info() << result->first << " " << result->second; @@ -620,7 +620,7 @@ struct myapp // if (!collision_time) return; - if (B >= -0.01f * geom::length(dv) * geom::length(dp)) return; + if (B >= -0.01f * math::length(dv) * math::length(dp)) return; float collision_time; @@ -641,7 +641,7 @@ struct myapp if (collision_time >= dt) return; auto dpn = (particles_[i].pos + particles_[i].vel * (collision_time - particle_time[i])) - (particles_[j].pos + particles_[j].vel * (collision_time - particle_time[j])); - if (geom::dot(dv, dpn) >= -0.01f * geom::length(dv) * geom::length(dpn)) return; + if (math::dot(dv, dpn) >= -0.01f * math::length(dv) * math::length(dpn)) return; auto insert_result = events.insert(collision_event{i, j, collision_time}); if (insert_result.second) @@ -670,15 +670,15 @@ struct myapp auto foreach_cells = [&](std::size_t i, auto && callback) { - geom::box bbox; + math::box bbox; - bbox |= particles_[i].pos - geom::vector{1.f, 1.f} * particles_[i].radius; - bbox |= particles_[i].pos + geom::vector{1.f, 1.f} * particles_[i].radius; + bbox |= particles_[i].pos - math::vector{1.f, 1.f} * particles_[i].radius; + bbox |= particles_[i].pos + math::vector{1.f, 1.f} * particles_[i].radius; auto npos = particles_[i].pos + particles_[i].vel * dt;//(dt - particle_time[i]); - bbox |= npos - geom::vector{1.f, 1.f} * particles_[i].radius; - bbox |= npos + geom::vector{1.f, 1.f} * particles_[i].radius; + bbox |= npos - math::vector{1.f, 1.f} * particles_[i].radius; + bbox |= npos + math::vector{1.f, 1.f} * particles_[i].radius; int xmin = std::floor(bbox[0].min); int xmax = std::floor(bbox[0].max); @@ -724,17 +724,17 @@ struct myapp particles_[e.i0].pos += particles_[e.i0].vel * (e.t - particle_time[e.i0]); particles_[e.i1].pos += particles_[e.i1].vel * (e.t - particle_time[e.i1]); -// log::info() << "Collision " << e.i0 << " " << e.i1 << " " << geom::distance(particles_[e.i0].pos, particles_[e.i1].pos); +// log::info() << "Collision " << e.i0 << " " << e.i1 << " " << math::distance(particles_[e.i0].pos, particles_[e.i1].pos); particle_time[e.i0] = e.t; particle_time[e.i1] = e.t; - auto n = geom::normalized(particles_[e.i0].pos - particles_[e.i1].pos); + auto n = math::normalized(particles_[e.i0].pos - particles_[e.i1].pos); float A = 0.5f * (1.f / particles_[e.i0].mass + 1.f / particles_[e.i1].mass); float B = dot(n, particles_[e.i0].vel - particles_[e.i1].vel); - auto BB = B / geom::length(particles_[e.i0].vel - particles_[e.i1].vel); + auto BB = B / math::length(particles_[e.i0].vel - particles_[e.i1].vel); (void)BB; auto np = n * (- B / A); @@ -769,13 +769,13 @@ struct myapp auto vij = particles_[i].vel - particles_[j].vel; auto R = particles_[i].radius + particles_[j].radius; - float B = geom::dot(rij, vij); + float B = math::dot(rij, vij); if (B >= 0.f) continue; std::optional e; - float C = geom::dot(rij, rij) - geom::sqr(R); + float C = math::dot(rij, rij) - math::sqr(R); if (C <= 0.f) { @@ -783,7 +783,7 @@ struct myapp } else { - float A = geom::dot(vij, vij); + float A = math::dot(vij, vij); float D = B * B - A * C; @@ -868,26 +868,26 @@ struct myapp for (auto & p : particles_) { auto r = p.pos - world_center; - auto l = geom::length(r); + auto l = math::length(r); if (l + p.radius > world_size) { auto n = r / l; - auto t = geom::ort(n); + auto t = math::ort(n); - auto vn = n * geom::dot(p.vel, n); + auto vn = n * math::dot(p.vel, n); - auto vt = t * geom::dot(p.vel + t * p.angle_vel * p.radius, t); + auto vt = t * math::dot(p.vel + t * p.angle_vel * p.radius, t); auto pt = - vt * (1.f - std::exp(- 10.f * dt)); - float I = 0.5f * p.mass * geom::sqr(p.radius); + float I = 0.5f * p.mass * math::sqr(p.radius); p.pos -= n * (l + p.radius - world_size); p.vel -= 1.75f * vn; p.vel += pt / p.mass; - p.angle_vel += geom::det(n * p.radius, pt) / I; + p.angle_vel += math::det(n * p.radius, pt) / I; } } @@ -912,14 +912,14 @@ struct myapp auto const vij = particles_[i].vel - particles_[j].vel; auto dp = - K * dt * n * dot(n, vij) * 2.f / (1.f / particles_[i].mass + 1.f / particles_[j].mass); - float Ei0 = geom::length_sqr(particles_[i].vel) * particles_[i].mass * 0.5f; + float Ei0 = math::length_sqr(particles_[i].vel) * particles_[i].mass * 0.5f; particles_[i].vel += dp / particles_[i].mass; - float Ei1 = geom::length_sqr(particles_[i].vel) * particles_[i].mass * 0.5f; + float Ei1 = math::length_sqr(particles_[i].vel) * particles_[i].mass * 0.5f; particles_[i].T += Ei0 - Ei1; - float Ej0 = geom::length_sqr(particles_[j].vel) * particles_[j].mass * 0.5f; + float Ej0 = math::length_sqr(particles_[j].vel) * particles_[j].mass * 0.5f; particles_[j].vel -= dp / particles_[j].mass; - float Ej1 = geom::length_sqr(particles_[j].vel) * particles_[j].mass * 0.5f; + float Ej1 = math::length_sqr(particles_[j].vel) * particles_[j].mass * 0.5f; particles_[j].T += Ej0 - Ej1; float dT = particles_[i].T - particles_[j].T; @@ -939,8 +939,8 @@ struct myapp p.pos = particles_[i].pos + (particles_[j].pos - particles_[i].pos) * particles_[j].mass / M; p.vel = (particles_[i].vel * particles_[i].mass + particles_[j].vel * particles_[j].mass) / M; p.mass = M; - p.radius = std::sqrt(geom::sqr(particles_[i].radius) + geom::sqr(particles_[j].radius)); - p.density = p.mass / (geom::pi * geom::sqr(p.radius)); + p.radius = std::sqrt(math::sqr(particles_[i].radius) + math::sqr(particles_[j].radius)); + p.density = p.mass / (math::pi * math::sqr(p.radius)); particles_[i] = p; particles_.erase(particles_.begin() + j); @@ -963,10 +963,10 @@ struct myapp auto ri = p - particles_[i].pos; auto rj = p - particles_[j].pos; - auto t = geom::ort(n); + auto t = math::ort(n); - auto vri = geom::dot(t, particles_[i].vel + geom::ort(ri) * particles_[i].angle_vel); - auto vrj = geom::dot(t, particles_[j].vel + geom::ort(rj) * particles_[j].angle_vel); + auto vri = math::dot(t, particles_[i].vel + math::ort(ri) * particles_[i].angle_vel); + auto vrj = math::dot(t, particles_[j].vel + math::ort(rj) * particles_[j].angle_vel); auto vrij = vri - vrj; @@ -975,11 +975,11 @@ struct myapp particles_[i].vel += (np + tp) / particles_[i].mass; particles_[j].vel -= (np + tp) / particles_[j].mass; - float Ii = 0.5f * particles_[i].mass * geom::sqr(particles_[i].radius); - float Ij = 0.5f * particles_[j].mass * geom::sqr(particles_[j].radius); + float Ii = 0.5f * particles_[i].mass * math::sqr(particles_[i].radius); + float Ij = 0.5f * particles_[j].mass * math::sqr(particles_[j].radius); - auto ti = geom::det(ri, tp); - auto tj = geom::det(rj, -tp); + auto ti = math::det(ri, tp); + auto tj = math::det(rj, -tp); particles_[i].angle_vel += ti / Ii; particles_[j].angle_vel += tj / Ij; @@ -1020,7 +1020,7 @@ struct myapp particles_[i].vel += dt * f / particles_[i].mass; particles_[j].vel -= dt * f / particles_[j].mass; - auto vn = geom::dot(vij, n) * n; + auto vn = math::dot(vij, n) * n; // vn *= 1.f - std::exp(- (1.f - l / R)); // vn *= 0.5f; @@ -1048,7 +1048,7 @@ struct myapp float bias = 0.5f; float constraint = l - R; float reduced_mass = 1.f / (1.f / particles_[i].mass + 1.f / particles_[j].mass); - float lambda = (- geom::dot(n, particles_[i].vel - particles_[j].vel) - bias / dt * constraint) * reduced_mass; + float lambda = (- math::dot(n, particles_[i].vel - particles_[j].vel) - bias / dt * constraint) * reduced_mass; auto impulse = lambda * n; particles_[i].vel += impulse / particles_[i].mass; @@ -1080,7 +1080,7 @@ struct myapp struct collision { std::size_t i, j; - geom::vector normal; + math::vector normal; float value; float impulse = 0.f; }; @@ -1114,7 +1114,7 @@ struct myapp float reduced_mass = 1.f / (1.f / particles_[c.i].mass + 1.f / particles_[c.j].mass); - auto dv = geom::dot(c.normal, particles_[c.i].vel - particles_[c.j].vel); + auto dv = math::dot(c.normal, particles_[c.i].vel - particles_[c.j].vel); float elasticity = 0.75f; float lambda = (- dv - bias / dt * c.value - dv * elasticity) * reduced_mass; @@ -1139,7 +1139,7 @@ struct myapp for (std::size_t i = 0; i < particles_.size(); ++i) { - Ek += geom::length_sqr(particles_[i].vel) * particles_[i].mass / 2.f; + Ek += math::length_sqr(particles_[i].vel) * particles_[i].mass / 2.f; for (std::size_t j = i + 1; j < particles_.size(); ++j) { @@ -1150,7 +1150,7 @@ struct myapp float omega = 0.f; for (std::size_t i = 0; i < particles_.size(); ++i) { - omega += geom::det(particles_[i].mass * particles_[i].vel, particles_[i].pos - geom::point{0.f, 0.f}); + omega += math::det(particles_[i].mass * particles_[i].vel, particles_[i].pos - math::point{0.f, 0.f}); } energy_ = Ek + Ep; @@ -1190,11 +1190,11 @@ struct myapp float r = std::max(p.radius * s, 1.5f) / s; painter_.circle(p.pos, r, {x, x, x, 191}); -// painter_.line(p.pos, p.pos + r * geom::direction(p.angle), r / 16.f, {255, 0, 0, 255}, false); +// painter_.line(p.pos, p.pos + r * math::direction(p.angle), r / 16.f, {255, 0, 0, 255}, false); // painter_.circle(p.pos, r * 0.75f, {255, 255, 255, 255}); } - geom::orthographic_camera camera; + math::orthographic_camera camera; camera.box[0].min = camera_center_[0] - camera_size_ * camera_ratio_ / 2.f; camera.box[0].max = camera_center_[0] + camera_size_ * camera_ratio_ / 2.f; camera.box[1].min = camera_center_[1] - camera_size_ / 2.f; @@ -1206,7 +1206,7 @@ struct myapp float rotation = 0.f; for (auto const & p : particles_) - rotation += geom::det(p.pos - p.pos.zero(), p.vel) * p.mass; + rotation += math::det(p.pos - p.pos.zero(), p.vel) * p.mass; rotation_.push(rotation); { @@ -1216,11 +1216,11 @@ struct myapp opts.x = gfx::painter::x_align::center; opts.y = gfx::painter::y_align::top; - auto put = [&](geom::point const & pos, std::string const & str) + auto put = [&](math::point const & pos, std::string const & str) { return; painter_.text(pos, str, opts); - painter_.text(pos + geom::vector{1.f, 0.f}, str, opts); + painter_.text(pos + math::vector{1.f, 0.f}, str, opts); }; put({state().size[0] / 2.f, 30.f}, util::to_string("ITERATIONS: ", SOLVE_ITERATIONS)); @@ -1231,7 +1231,7 @@ struct myapp // painter_.text({10.f, 50.f}, util::to_string("Energy: ", energy_), opts); } - painter_.render(geom::window_camera{state().size[0], state().size[1]}.transform()); + painter_.render(math::window_camera{state().size[0], state().size[1]}.transform()); } ~myapp() @@ -1245,13 +1245,13 @@ struct myapp private: std::default_random_engine rng_; - std::optional> force_target_; + std::optional> force_target_; - geom::point camera_center_ { 0.f, 0.f }; + math::point camera_center_ { 0.f, 0.f }; float camera_size_ = 150.f; float camera_ratio_ = 1.f; - std::optional> camera_drag_; + std::optional> camera_drag_; gfx::painter painter_; diff --git a/examples/physics.cpp b/examples/physics.cpp index cf282188..e58f7ed9 100644 --- a/examples/physics.cpp +++ b/examples/physics.cpp @@ -4,13 +4,13 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -50,11 +50,11 @@ namespace psemek::util struct stick_model { - std::vector> points; - std::vector> vels; + std::vector> points; + std::vector> vels; std::vector movable; - std::vector> sticks; + std::vector> sticks; std::vector stick_length; std::vector stick_solid; @@ -64,7 +64,7 @@ struct stick_model void stick_model::add_stick(std::size_t i, std::size_t j, bool solid) { sticks.push_back({i, j}); - stick_length.push_back(geom::distance(points[i], points[j])); + stick_length.push_back(math::distance(points[i], points[j])); stick_solid.push_back(solid); } @@ -89,8 +89,8 @@ struct physics_demo_app void update() override; void present() override; - geom::point screen_to_world(geom::point const & p) const; - geom::point world_to_screen(geom::point const & p) const; + math::point screen_to_world(math::point const & p) const; + math::point world_to_screen(math::point const & p) const; util::clock> frame_clock; float update_time_left = 0.f; @@ -101,11 +101,11 @@ struct physics_demo_app gfx::painter painter; - geom::box view_region; + math::box view_region; std::optional closest_point; std::optional closest_stick; - std::optional> drag_delta; + std::optional> drag_delta; std::optional new_spring_start; @@ -165,7 +165,7 @@ physics_demo_app::physics_demo_app(options const &, context const &) for (int i = 0; i < N; ++i) { - float a = (2.f * geom::pi * i) / N; + float a = (2.f * math::pi * i) / N; model.points.push_back({std::cos(a) * 0.5f, y + std::sin(a) * 0.5f}); } @@ -193,7 +193,7 @@ physics_demo_app::physics_demo_app(options const &, context const &) model.add_stick(3, 4); } - model.vels.assign(model.points.size(), geom::vector::zero()); + model.vels.assign(model.points.size(), math::vector::zero()); model.movable.assign(model.points.size(), true); } @@ -221,8 +221,8 @@ void physics_demo_app::on_event(app::mouse_button_event const & event) if (event.button == app::mouse_button::left && event.down && closest_point) { - drag_delta = world_to_screen(model.points[*closest_point]) - geom::cast(state().mouse); - model.vels[*closest_point] = geom::vector::zero(); + drag_delta = world_to_screen(model.points[*closest_point]) - math::cast(state().mouse); + model.vels[*closest_point] = math::vector::zero(); } if (event.button == app::mouse_button::left && !event.down) @@ -239,7 +239,7 @@ void physics_demo_app::on_event(app::mouse_button_event const & event) else if (closest_point) { model.movable[*closest_point] = !model.movable[*closest_point]; - model.vels[*closest_point] = geom::vector::zero(); + model.vels[*closest_point] = math::vector::zero(); } } } @@ -254,14 +254,14 @@ void physics_demo_app::on_event(app::mouse_move_event const & event) closest_stick = std::nullopt; { - auto const m = geom::cast(state().mouse); + auto const m = math::cast(state().mouse); std::size_t closest = 0; float distance = std::numeric_limits::infinity(); for (std::size_t i = 0; i < model.points.size(); ++i) { - auto const d = geom::distance(m, world_to_screen(model.points[i])); + auto const d = math::distance(m, world_to_screen(model.points[i])); if (d < distance) { distance = d; @@ -281,7 +281,7 @@ void physics_demo_app::on_event(app::mouse_move_event const & event) for (std::size_t i = 0; i < model.sticks.size(); ++i) { - auto const d = geom::distance(m, geom::simplex{world_to_screen(model.points[model.sticks[i][0]]), world_to_screen(model.points[model.sticks[i][1]])}); + auto const d = math::distance(m, math::simplex{world_to_screen(model.points[model.sticks[i][0]]), world_to_screen(model.points[model.sticks[i][1]])}); if (d < distance) { distance = d; @@ -314,7 +314,7 @@ void physics_demo_app::on_event(app::key_event const & event) model.vels.erase(model.vels.begin() + *closest_point); model.movable.erase(model.movable.begin() + *closest_point); - std::vector> new_sticks; + std::vector> new_sticks; std::vector new_stick_length; std::vector new_stick_solid; @@ -359,8 +359,8 @@ void physics_demo_app::on_event(app::key_event const & event) } else if (new_spring_start) { - model.points.push_back(screen_to_world(geom::cast(state().mouse))); - model.vels.push_back(geom::vector::zero()); + model.points.push_back(screen_to_world(math::cast(state().mouse))); + model.vels.push_back(math::vector::zero()); model.movable.push_back(false); model.add_stick(*new_spring_start, model.points.size() - 1); new_spring_start = model.points.size() - 1; @@ -372,15 +372,15 @@ void physics_demo_app::on_event(app::key_event const & event) } else { - model.points.push_back(screen_to_world(geom::cast(state().mouse))); - model.vels.push_back(geom::vector::zero()); + model.points.push_back(screen_to_world(math::cast(state().mouse))); + model.vels.push_back(math::vector::zero()); model.movable.push_back(false); } } else if (event.down && event.key == app::keycode::N) { - model.points.push_back(screen_to_world(geom::cast(state().mouse))); - model.vels.push_back(geom::vector::zero()); + model.points.push_back(screen_to_world(math::cast(state().mouse))); + model.vels.push_back(math::vector::zero()); model.movable.push_back(true); } else if (event.down && event.key == app::keycode::F) @@ -396,19 +396,19 @@ void physics_demo_app::on_event(app::key_event const & event) int N = 12; - auto o = screen_to_world(geom::cast(state().mouse)); + auto o = screen_to_world(math::cast(state().mouse)); model.points.push_back(o); - model.vels.push_back(geom::vector::zero()); + model.vels.push_back(math::vector::zero()); model.movable.push_back(true); float r = 0.5f; for (int i = 0; i < N; ++i) { - float a = (2.f * geom::pi * i) / N; + float a = (2.f * math::pi * i) / N; model.points.push_back({o[0] + std::cos(a) * r, o[1] + std::sin(a) * r}); - model.vels.push_back(geom::vector::zero()); + model.vels.push_back(math::vector::zero()); model.movable.push_back(true); } @@ -422,7 +422,7 @@ void physics_demo_app::on_event(app::key_event const & event) { std::uint32_t const base = model.points.size(); - auto o = screen_to_world(geom::cast(state().mouse)); + auto o = screen_to_world(math::cast(state().mouse)); float w = 0.25f; @@ -431,10 +431,10 @@ void physics_demo_app::on_event(app::key_event const & event) model.points.push_back({o[0] - w, o[1] + w}); model.points.push_back({o[0] + w, o[1] + w}); - model.vels.push_back(geom::vector::zero()); - model.vels.push_back(geom::vector::zero()); - model.vels.push_back(geom::vector::zero()); - model.vels.push_back(geom::vector::zero()); + model.vels.push_back(math::vector::zero()); + model.vels.push_back(math::vector::zero()); + model.vels.push_back(math::vector::zero()); + model.vels.push_back(math::vector::zero()); model.movable.push_back(true); model.movable.push_back(true); model.movable.push_back(true); @@ -483,7 +483,7 @@ void physics_demo_app::update() float const stick_friction = 500.f; float const stick_bounce = 0.9f; - geom::vector const gravity {0.f, -10.f}; + math::vector const gravity {0.f, -10.f}; update_time_left += frame_clock.restart().count(); @@ -515,10 +515,10 @@ void physics_demo_app::update() for (std::size_t i = 0; i < model.points.size(); ++i) { int x = std::floor(cells.width() * (model.points[i][0] - view_region[0].min) / view_region[0].length()); - x = geom::clamp(x, {0, cells.width() - 1}); + x = math::clamp(x, {0, cells.width() - 1}); int y = std::floor(cells.height() * (model.points[i][1] - view_region[1].min) / view_region[1].length()); - y = geom::clamp(y, {0, cells.height() - 1}); + y = math::clamp(y, {0, cells.height() - 1}); float cx = view_region[0].min + dx * x; float cy = view_region[1].min + dy * y; @@ -579,7 +579,7 @@ void physics_demo_app::update() if (closest_point && drag_delta) { - auto target = screen_to_world(geom::cast(state().mouse) + *drag_delta); + auto target = screen_to_world(math::cast(state().mouse) + *drag_delta); auto & point = model.points[*closest_point]; auto & vel = model.vels[*closest_point]; @@ -591,7 +591,7 @@ void physics_demo_app::update() fixed = closest_point; } - std::vector> force(model.points.size(), geom::vector::zero()); + std::vector> force(model.points.size(), math::vector::zero()); for (std::size_t i = 0; i < model.points.size(); ++i) { @@ -602,7 +602,7 @@ void physics_demo_app::update() { auto const & s = model.sticks[i]; auto d = model.points[s[1]] - model.points[s[0]]; - auto l = geom::length(d); + auto l = math::length(d); auto n = d / l; auto f = - spring_constant * (l - model.stick_length[i]) * n / 2.f; @@ -620,17 +620,17 @@ void physics_demo_app::update() model.vels[i] += dt * force[i]; } - std::vector> impulse(model.points.size(), geom::vector::zero()); - std::vector> offset(model.points.size(), geom::vector::zero()); + std::vector> impulse(model.points.size(), math::vector::zero()); + std::vector> offset(model.points.size(), math::vector::zero()); for (std::size_t i = 0; i < model.sticks.size(); ++i) { auto const & s = model.sticks[i]; auto rel = model.vels[s[1]] - model.vels[s[0]]; - auto n = geom::normalized(model.points[s[1]] - model.points[s[0]]); + auto n = math::normalized(model.points[s[1]] - model.points[s[0]]); - rel = n * geom::dot(rel, n); + rel = n * math::dot(rel, n); impulse[s[0]] += rel / 2.f * spring_damping_constant * dt; impulse[s[1]] -= rel / 2.f * spring_damping_constant * dt; @@ -639,7 +639,7 @@ void physics_demo_app::update() for (std::size_t i = 0; i < model.points.size(); ++i) { bool collision = false; - geom::vector n; + math::vector n; float d; if (model.points[i][0] < view_region[0].min + ball_radius && model.vels[i][0] < 0.f) @@ -675,7 +675,7 @@ void physics_demo_app::update() offset[i] += n * d; (void)d; - auto nv = n * geom::dot(n, model.vels[i]); + auto nv = n * math::dot(n, model.vels[i]); auto tv = model.vels[i] - nv; auto jn = - (1.f + ground_bounce) * nv; @@ -684,9 +684,9 @@ void physics_demo_app::update() unused(ground_mu); -// if (geom::length(jt) > ground_mu * geom::length(jn)) +// if (math::length(jt) > ground_mu * math::length(jn)) // { -// jt = geom::normalized(jt) * ground_mu * geom::length(jn); +// jt = math::normalized(jt) * ground_mu * math::length(jn); // } impulse[i] += jn + jt; @@ -708,14 +708,14 @@ void physics_demo_app::update() checked.push_back(j); - auto r = geom::distance(model.points[i], model.points[j]); + auto r = math::distance(model.points[i], model.points[j]); if (r < ball_radius * 2.f) { auto n = (model.points[j] - model.points[i]) / r; auto rel = model.vels[j] - model.vels[i]; - auto d = geom::dot(rel, n); + auto d = math::dot(rel, n); if (d < 0.f) { @@ -745,23 +745,23 @@ void physics_demo_app::update() if (i == s[0] || i == s[1]) continue; - auto d = geom::normalized(model.points[s[1]] - model.points[s[0]]); + auto d = math::normalized(model.points[s[1]] - model.points[s[0]]); - auto t = geom::dot(d, model.points[i] - model.points[s[0]]) / geom::distance(model.points[s[1]], model.points[s[0]]); + auto t = math::dot(d, model.points[i] - model.points[s[0]]) / math::distance(model.points[s[1]], model.points[s[0]]); if (t < 0.f || t > 1.f) continue; - auto v = geom::lerp(model.vels[s[0]], model.vels[s[1]], t); + auto v = math::lerp(model.vels[s[0]], model.vels[s[1]], t); auto rel = model.vels[i] - v; - auto n = geom::ort(d); + auto n = math::ort(d); - float r = geom::dot(n, model.points[i] - model.points[s[0]]); + float r = math::dot(n, model.points[i] - model.points[s[0]]); - if (geom::dot(rel, n) * r >= 0.f) continue; + if (math::dot(rel, n) * r >= 0.f) continue; - auto nv = n * geom::dot(rel, n); + auto nv = n * math::dot(rel, n); auto tv = rel - nv; unused(stick_friction); @@ -769,10 +769,10 @@ void physics_demo_app::update() if (std::abs(r) >= ball_radius) continue; - geom::vector off = (r < 0.f ? -1.f : 1.f) * n * (ball_radius - std::abs(r)); + math::vector off = (r < 0.f ? -1.f : 1.f) * n * (ball_radius - std::abs(r)); unused(off); - geom::vector imp = - nv * (1.f + stick_bounce) - tv * stick_friction * dt; + math::vector imp = - nv * (1.f + stick_bounce) - tv * stick_friction * dt; offset[i] += off * 2.f / 3.f; offset[s[0]] -= off / 3.f * (1.f - t); @@ -816,7 +816,7 @@ void physics_demo_app::present() if (new_spring_start) { - painter.line(model.points[*new_spring_start], screen_to_world(geom::cast(state().mouse)), ball_radius / 2.f, gfx::red); + painter.line(model.points[*new_spring_start], screen_to_world(math::cast(state().mouse)), ball_radius / 2.f, gfx::red); } if (closest_stick) @@ -834,7 +834,7 @@ void physics_demo_app::present() painter.circle(model.points[*closest_point], ball_radius, gfx::red); } - painter.render(geom::orthographic(geom::box{{view_region[0], view_region[1], {-1.f, 1.f}}}).homogeneous_matrix()); + painter.render(math::orthographic(math::box{{view_region[0], view_region[1], {-1.f, 1.f}}}).homogeneous_matrix()); text = util::to_string("Balls: ", model.points.size(), "\n", text); @@ -865,17 +865,17 @@ void physics_demo_app::present() text.clear(); - painter.render(geom::window_camera{state().size[0], state().size[1]}.transform()); + painter.render(math::window_camera{state().size[0], state().size[1]}.transform()); } -geom::point physics_demo_app::screen_to_world(geom::point const & p) const +math::point physics_demo_app::screen_to_world(math::point const & p) const { - return geom::orthographic(view_region).inverse()(geom::point{2.f * p[0] / state().size[0] - 1.f, 1.f - 2.f * p[1] / state().size[1]}); + return math::orthographic(view_region).inverse()(math::point{2.f * p[0] / state().size[0] - 1.f, 1.f - 2.f * p[1] / state().size[1]}); } -geom::point physics_demo_app::world_to_screen(geom::point const & p) const +math::point physics_demo_app::world_to_screen(math::point const & p) const { - auto q = geom::orthographic(view_region)(p); + auto q = math::orthographic(view_region)(p); return {(q[0] + 1.f) / 2.f * state().size[0], (1.f - q[1]) / 2.f * state().size[1]}; } diff --git a/examples/physics_2d.cpp b/examples/physics_2d.cpp index b188f13f..c8090873 100644 --- a/examples/physics_2d.cpp +++ b/examples/physics_2d.cpp @@ -5,9 +5,9 @@ #include -#include -#include -#include +#include +#include +#include #include #include @@ -30,8 +30,8 @@ struct physics_2d_app { phys2d::engine physics; - geom::box view_box; - geom::box simulation_box; + math::box view_box; + math::box simulation_box; gfx::painter painter; @@ -54,11 +54,11 @@ struct physics_2d_app std::vector radiuses; - std::optional> mouse; + std::optional> mouse; std::optional selected_ball; - std::optional> drag_delta; + std::optional> drag_delta; std::size_t motor_id; @@ -104,7 +104,7 @@ struct physics_2d_app { for (int x = nx / 2 - ky / 2; x < nx / 2 - ky / 2 + (ky - y); ++x) { - geom::point pos{simulation_box.corner((x + 0.5f * y + 0.5f) / nx, (y + 0.5f) / ny)}; + math::point pos{simulation_box.corner((x + 0.5f * y + 0.5f) / nx, (y + 0.5f) / ny)}; physics.add_object(box_group, box_shape, material, {pos, 0.f}, {{0.f, 0.f}, 0.01f}); } } @@ -118,19 +118,19 @@ struct physics_2d_app { if (chess && (y % 2)) { - geom::point pos{simulation_box.corner((0.25f) / nx, (y + 0.5f) / ny)}; + math::point pos{simulation_box.corner((0.25f) / nx, (y + 0.5f) / ny)}; physics.add_object(box_group, small_box_shape, material, {pos, 0.f}, {{0.f, 0.f}, 0.01f}); } for (int x = 0; x < nx - chess * (y % 2); ++x) { - geom::point pos{simulation_box.corner((x + 0.5f + chess * 0.5f * (y % 2)) / nx, (y + 0.5f) / ny)}; + math::point pos{simulation_box.corner((x + 0.5f + chess * 0.5f * (y % 2)) / nx, (y + 0.5f) / ny)}; physics.add_object(box_group, box_shape, material, {pos, 0.f}, {{0.f, 0.f}, 0.01f}); } if (chess && (y % 2)) { - geom::point pos{simulation_box.corner((nx - 0.25f) / nx, (y + 0.5f) / ny)}; + math::point pos{simulation_box.corner((nx - 0.25f) / nx, (y + 0.5f) / ny)}; physics.add_object(box_group, small_box_shape, material, {pos, 0.f}, {{0.f, 0.f}, 0.01f}); } } @@ -146,9 +146,9 @@ struct physics_2d_app if(false) for (int i = 0; i < 15; ++i) { - float a = geom::rad(360 * i / 15.f); - float r = ball_radius * 15.f / geom::pi; - geom::point pos{r * std::cos(a), r * std::sin(a)}; + float a = math::rad(360 * i / 15.f); + float r = ball_radius * 15.f / math::pi; + math::point pos{r * std::cos(a), r * std::sin(a)}; physics.add_object(ball_group, ball_shape, material, {pos, 0.f}, {}); } } @@ -169,12 +169,12 @@ struct physics_2d_app physics.add_object(wall_group, wall_3_shape, wall_material, {}, {}); auto task = util::recursive([this, dx = box_width * 0.4f](auto && self) mutable -> void { - physics.add_object(box_group, box_shape, material, {simulation_box.corner(0.5f, 0.9f) + geom::vector{dx, 0.f}, 0.f}, {}); -// physics.add_object(ball_group, ball_shape, material, {simulation_box.corner(0.5f, 0.9f) + geom::vector{dx, 0.f}, 0.f}, {}); + physics.add_object(box_group, box_shape, material, {simulation_box.corner(0.5f, 0.9f) + math::vector{dx, 0.f}, 0.f}, {}); +// physics.add_object(ball_group, ball_shape, material, {simulation_box.corner(0.5f, 0.9f) + math::vector{dx, 0.f}, 0.f}, {}); // float r = random::uniform_distribution{0.05f, 0.5f}(gen); // auto new_shape = physics.add_shape(phys2d::ball{r}); -// physics.add_object(ball_group, new_shape, material, {simulation_box.corner(0.5f, 0.9f) + geom::vector{dx, 0.f}, 0.f}, {}); +// physics.add_object(ball_group, new_shape, material, {simulation_box.corner(0.5f, 0.9f) + math::vector{dx, 0.f}, 0.f}, {}); // radiuses.push_back(r); dx *= -1.f; @@ -224,7 +224,7 @@ struct physics_2d_app { auto const & s = physics.group_static_state(ball_group)[*selected_ball]; auto r = s.position - *mouse; - r = geom::plane_rotation(0, 1, -s.rotation)(r); + r = math::plane_rotation(0, 1, -s.rotation)(r); drag_delta = r; } else if (mouse) @@ -286,13 +286,13 @@ struct physics_2d_app { auto r = -*drag_delta; - auto R = geom::plane_rotation(0, 1, s.rotation); + auto R = math::plane_rotation(0, 1, s.rotation); - geom::vector f; + math::vector f; f[0] = s.position[0] + R(r)[0] - (*mouse)[0]; f[1] = s.position[1] + R(r)[1] - (*mouse)[1]; - geom::matrix J; + math::matrix J; J[0][0] = 1.f; J[0][1] = 0.f; J[0][2] = - std::sin(s.rotation) * r[0] + std::cos(s.rotation) * r[1]; @@ -300,12 +300,12 @@ struct physics_2d_app J[1][1] = 1.f; J[1][2] = std::cos(s.rotation) * r[0] - std::sin(s.rotation) * r[1]; - geom::vector v; + math::vector v; v[0] = d.velocity[0]; v[1] = d.velocity[1]; v[2] = d.angular_velocity; - auto dv = geom::least_squares(J, -(1.f / 8.f) * f / dt - (J * v)); + auto dv = math::least_squares(J, -(1.f / 8.f) * f / dt - (J * v)); if (dv) { d.velocity[0] += (*dv)[0] * lambda; @@ -323,24 +323,24 @@ struct physics_2d_app auto & d1 = physics.group_dynamic_state(group)[j]; auto r = s1.position - s0.position; - float l = geom::length(r); + float l = math::length(r); - geom::vector f; + math::vector f; f[0] = l - target; - geom::matrix J; + math::matrix J; J[0][0] = -r[0] / l; J[0][1] = -r[1] / l; J[0][2] = r[0] / l; J[0][3] = r[1] / l; - geom::vector v; + math::vector v; v[0] = d0.velocity[0]; v[1] = d0.velocity[1]; v[2] = d1.velocity[0]; v[3] = d1.velocity[1]; - auto dv = geom::least_squares(J, -(1.f / 8.f) * f / dt - (J * v)); + auto dv = math::least_squares(J, -(1.f / 8.f) * f / dt - (J * v)); if (dv) { d0.velocity[0] += (*dv)[0] * lambda; @@ -413,7 +413,7 @@ struct physics_2d_app for (std::size_t i = 0; i < physics.group_size(ball_group); ++i) { auto p = physics.group_static_state(ball_group)[i].position; - float d = geom::distance(p, *mouse); + float d = math::distance(p, *mouse); if (d <= std::min(closest, ball_radius)) { closest = d; @@ -441,8 +441,8 @@ struct physics_2d_app painter.circle(p, r, gfx::black); painter.circle(p, r - line_width, gfx::light(gfx::blue, selected ? 0.8f : 1.f).as_color_rgba()); - geom::vector const n{std::cos(a), std::sin(a)}; - geom::vector const m = geom::ort(n); + math::vector const n{std::cos(a), std::sin(a)}; + math::vector const m = math::ort(n); painter.line(p - n * ball_radius / 2.f, p + n * ball_radius / 2.f, line_width, gfx::black, false); painter.line(p - m * ball_radius / 2.f, p + m * ball_radius / 2.f, line_width, gfx::black, false); @@ -458,13 +458,13 @@ struct physics_2d_app float w = std::get(physics.get_shape(physics.get_object(box_group, i).shape))->width / 2.f; float h = std::get(physics.get_shape(physics.get_object(box_group, i).shape))->height / 2.f; - geom::vector q[4]; + math::vector q[4]; q[0] = {-w, -h}; q[1] = { w, -h}; q[2] = { w, h}; q[3] = {-w, h}; - auto m = geom::plane_rotation(0, 1, a).linear_matrix(); + auto m = math::plane_rotation(0, 1, a).linear_matrix(); for (std::size_t j = 0; j < 4; ++j) q[j] = m * q[j]; @@ -483,7 +483,7 @@ struct physics_2d_app painter.line(simulation_box.corner(1, 1), simulation_box.corner(0, 1), line_width, gfx::black, false); painter.line(simulation_box.corner(0, 1), simulation_box.corner(0, 0), line_width, gfx::black, false); - painter.render(geom::orthographic_camera{view_box}.transform()); + painter.render(math::orthographic_camera{view_box}.transform()); } }; diff --git a/examples/physics_3d.cpp b/examples/physics_3d.cpp index 40ed5928..7b2ff75c 100644 --- a/examples/physics_3d.cpp +++ b/examples/physics_3d.cpp @@ -9,11 +9,11 @@ #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -95,12 +95,12 @@ struct physics_3d_app { camera_.near_clip = 0.1f; camera_.far_clip = 1000.f; - camera_.fov_y = geom::rad(90.f); + camera_.fov_y = math::rad(90.f); camera_.fov_x = camera_.fov_y; camera_.target = {0.f, 0.f, 0.f}; camera_.distance = 10.f; - camera_.elevation_angle = geom::rad(45.f); - camera_.azimuthal_angle = geom::rad(30.f); + camera_.elevation_angle = math::rad(45.f); + camera_.azimuthal_angle = math::rad(30.f); camera_distance_tgt_ = camera_.distance; camera_azimuthal_angle_tgt_ = camera_.azimuthal_angle; @@ -108,12 +108,12 @@ struct physics_3d_app struct vertex { - geom::point position; - geom::vector normal; + math::point position; + math::vector normal; static void setup(gfx::mesh & m) { - m.setup, geom::vector>(); + m.setup, math::vector>(); } }; @@ -136,21 +136,21 @@ struct physics_3d_app auto const & body_vertices = cg::vertices(body); auto const & body_triangles = cg::triangles(body); - std::vector> positions; + std::vector> positions; std::copy(body_vertices.begin(), body_vertices.end(), std::back_inserter(positions)); - std::vector> triangles; + std::vector> triangles; for (auto const & t : body_triangles) triangles.push_back({t[0], t[1], t[2]}); for (int iterations = 0; iterations < 2; ++iterations) { - geom::subdivide(positions, triangles); + math::subdivide(positions, triangles); for (auto & p : positions) - p = p.zero() + geom::normalized(p - p.zero()); + p = p.zero() + math::normalized(p - p.zero()); } - auto normals = geom::smooth_normals(positions, triangles); + auto normals = math::smooth_normals(positions, triangles); std::vector vertices; @@ -167,19 +167,19 @@ struct physics_3d_app auto const & body_vertices = cg::vertices(body); auto const & body_triangles = cg::triangles(body); - std::vector> positions; + std::vector> positions; std::copy(body_vertices.begin(), body_vertices.end(), std::back_inserter(positions)); - std::vector> triangles; + std::vector> triangles; for (auto const & t : body_triangles) triangles.push_back({t[0], t[1], t[2]}); - auto flat_positions = geom::deindex(positions, triangles); + auto flat_positions = math::deindex(positions, triangles); std::vector vertices; for (auto const & t : flat_positions) { - auto n = geom::normal(t[0], t[1], t[2]); + auto n = math::normal(t[0], t[1], t[2]); vertices.push_back({t[0], n}); vertices.push_back({t[1], n}); vertices.push_back({t[2], n}); @@ -286,7 +286,7 @@ struct physics_3d_app auto w = engine_.get_object_state(h).angular_velocity; auto H = engine_.get_object_state(h).position[2]; - E += 0.5f * geom::length_sqr(v) * m + 0.5f * geom::dot(w, I * w) + m * 10.f * H; + E += 0.5f * math::length_sqr(v) * m + 0.5f * math::dot(w, I * w) + m * 10.f * H; } log::info() << "Energy: " << E; } @@ -307,15 +307,15 @@ struct physics_3d_app program_.bind(); program_["u_camera_transform"] = camera_.transform(); - program_["u_light_direction"] = geom::normalized(geom::vector{1.f, 1.f, 1.f}); - program_["u_light_color"] = geom::vector{1.f, 1.f, 1.f}; + program_["u_light_direction"] = math::normalized(math::vector{1.f, 1.f, 1.f}); + program_["u_light_color"] = math::vector{1.f, 1.f, 1.f}; - program_["u_object_transform"] = geom::matrix::identity(); - program_["u_object_color"] = geom::vector{0.5f, 0.5f, 0.5f}; + program_["u_object_transform"] = math::matrix::identity(); + program_["u_object_color"] = math::vector{0.5f, 0.5f, 0.5f}; program_["u_grid"] = 0; plane_mesh_.draw(); - program_["u_object_color"] = geom::vector{0.f, 0.f, 1.f}; + program_["u_object_color"] = math::vector{0.f, 0.f, 1.f}; program_["u_grid"] = 1; for (phys3d::engine::object_handle h = 0; h < engine_.object_count(); ++h) { @@ -326,17 +326,17 @@ struct physics_3d_app if (auto s = std::get_if(&sh)) { program_["u_object_transform"] = - geom::translation(engine_.get_object_state(h).position - geom::point::zero()).homogeneous_matrix() * - geom::quaternion_rotation(engine_.get_object_state(h).rotation).homogeneous_matrix() * - geom::scale((*s)->radius).homogeneous_matrix(); + math::translation(engine_.get_object_state(h).position - math::point::zero()).homogeneous_matrix() * + math::quaternion_rotation(engine_.get_object_state(h).rotation).homogeneous_matrix() * + math::scale((*s)->radius).homogeneous_matrix(); sphere_mesh_.draw(); } else if (auto s = std::get_if(&sh)) { program_["u_object_transform"] = - geom::translation(engine_.get_object_state(h).position - geom::point::zero()).homogeneous_matrix() * - geom::quaternion_rotation(engine_.get_object_state(h).rotation).homogeneous_matrix() * - geom::scale((*s)->dimensions / 2.f).homogeneous_matrix(); + math::translation(engine_.get_object_state(h).position - math::point::zero()).homogeneous_matrix() * + math::quaternion_rotation(engine_.get_object_state(h).rotation).homogeneous_matrix() * + math::scale((*s)->dimensions / 2.f).homogeneous_matrix(); box_mesh_.draw(); } } @@ -345,7 +345,7 @@ struct physics_3d_app private: gfx::program program_; - geom::spherical_camera camera_; + math::spherical_camera camera_; float camera_distance_tgt_; float camera_azimuthal_angle_tgt_; float camera_elevation_angle_tgt_; diff --git a/examples/platformer.cpp b/examples/platformer.cpp index 554f4303..207466fd 100644 --- a/examples/platformer.cpp +++ b/examples/platformer.cpp @@ -2,10 +2,10 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include @@ -15,30 +15,30 @@ using namespace psemek; struct map { - std::vector> platforms; + std::vector> platforms; }; struct player { - geom::vector size; - geom::point position; - geom::vector velocity; + math::vector size; + math::point position; + math::vector velocity; - geom::point ghost_position; - geom::vector ghost_velocity; + math::point ghost_position; + math::vector ghost_velocity; bool grounded = false; - geom::box bbox() const + math::box bbox() const { - return geom::expand(geom::box::singleton(position), size); + return math::expand(math::box::singleton(position), size); } }; struct particle { - geom::point position; - geom::vector velocity; + math::point position; + math::vector velocity; float size; float lifetime; gfx::color_rgba color; @@ -73,8 +73,8 @@ struct platformer_app float const friction = 25.f; float const jump_speed = 15.f; - geom::vector const ghost_spring_force{200.f, 500.f}; - geom::vector const ghost_spring_damping{10.f, 20.f}; + math::vector const ghost_spring_force{200.f, 500.f}; + math::vector const ghost_spring_damping{10.f, 20.f}; float const particle_grow_speed = 0.1f; float const move_particle_spawn_period = 1.f / 32.f; @@ -89,7 +89,7 @@ struct platformer_app { auto v = (player_.position - player_.ghost_position); - player_.ghost_velocity += geom::pointwise_mult(v, ghost_spring_force) * dt; + player_.ghost_velocity += math::pointwise_mult(v, ghost_spring_force) * dt; player_.ghost_velocity[0] *= std::exp(- ghost_spring_damping[0] * dt); player_.ghost_velocity[1] *= std::exp(- ghost_spring_damping[1] * dt); } @@ -142,8 +142,8 @@ struct platformer_app move_particle_spawn_timer_ -= move_particle_spawn_period; float s = state().key_down.contains(app::keycode::A) ? 1.f : -1.f; - auto position = player_.position + geom::vector{s * player_.size[0], -player_.size[1]}; - auto velocity = geom::direction(random::uniform(rng_, geom::rad(60.f), geom::rad(120.f))); + auto position = player_.position + math::vector{s * player_.size[0], -player_.size[1]}; + auto velocity = math::direction(random::uniform(rng_, math::rad(60.f), math::rad(120.f))); auto size = random::uniform(rng_, 0.05f, 0.15f); auto lifetime = random::uniform(rng_, 0.25f, 0.5f); int c = random::uniform(rng_, 63, 191); @@ -161,8 +161,8 @@ struct platformer_app { float s = random::uniform(rng_, -1.f, 1.f); - auto position = player_.position + geom::vector{s * player_.size[0], -player_.size[1]}; - auto velocity = (random::uniform(rng_) ? 1.f : -1.f) * geom::direction(random::uniform(rng_, geom::rad(-30.f), geom::rad(30.f))) * 3.f; + auto position = player_.position + math::vector{s * player_.size[0], -player_.size[1]}; + auto velocity = (random::uniform(rng_) ? 1.f : -1.f) * math::direction(random::uniform(rng_, math::rad(-30.f), math::rad(30.f))) * 3.f; auto size = random::uniform(rng_, 0.05f, 0.15f); auto lifetime = random::uniform(rng_, 0.25f, 0.5f); int c = random::uniform(rng_, 63, 191); @@ -178,8 +178,8 @@ struct platformer_app { float s = random::uniform(rng_, -1.f, 1.f); - auto position = player_.position + geom::vector{s * player_.size[0], -player_.size[1]}; - auto velocity = geom::direction(random::uniform(rng_, geom::rad(0.f), geom::rad(180.f))) * (-velocity_before_collision[1]) * 0.2f; + auto position = player_.position + math::vector{s * player_.size[0], -player_.size[1]}; + auto velocity = math::direction(random::uniform(rng_, math::rad(0.f), math::rad(180.f))) * (-velocity_before_collision[1]) * 0.2f; auto size = random::uniform(rng_, 0.05f, 0.15f); auto lifetime = random::uniform(rng_, 0.25f, 0.5f); int c = random::uniform(rng_, 63, 191); @@ -211,10 +211,10 @@ struct platformer_app painter_.rect(box, {0, 0, 0, 255}); { - geom::point bottom = player_.position - geom::vector{0.f, player_.size[1]}; - geom::point top = player_.ghost_position + geom::vector{0.f, player_.size[1]}; + math::point bottom = player_.position - math::vector{0.f, player_.size[1]}; + math::point top = player_.ghost_position + math::vector{0.f, player_.size[1]}; - auto d = geom::vector{player_.size[0], 0.f}; + auto d = math::vector{player_.size[0], 0.f}; gfx::color_rgba color{255, 0, 0, 255}; painter_.triangle(bottom - d, bottom + d, top - d, color); @@ -230,9 +230,9 @@ struct platformer_app float const aspect_ratio = state().size[0] * 1.f / state().size[1]; float const view_size = 5.f; - geom::box view_box{{{-view_size * aspect_ratio, view_size * aspect_ratio}, {-view_size, view_size}}}; + math::box view_box{{{-view_size * aspect_ratio, view_size * aspect_ratio}, {-view_size, view_size}}}; - painter_.render(geom::orthographic_camera{view_box}.transform()); + painter_.render(math::orthographic_camera{view_box}.transform()); } private: diff --git a/examples/shadow.cpp b/examples/shadow.cpp index 4a3f5867..a769f264 100644 --- a/examples/shadow.cpp +++ b/examples/shadow.cpp @@ -9,11 +9,11 @@ #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include @@ -96,16 +96,16 @@ struct phong_renderer struct light { - geom::vector position; + math::vector position; }; struct render_options { gfx::framebuffer const * framebuffer; GLenum draw_buffer; - geom::box viewport; - geom::matrix transform; - geom::point camera_position; + math::box viewport; + math::matrix transform; + math::point camera_position; struct light light; }; @@ -134,13 +134,13 @@ void phong_renderer::render(render_options const & options) program_.bind(); program_["u_transform"] = options.transform; program_["u_light_position"] = options.light.position; - program_["u_camera_position"] = geom::homogeneous(options.camera_position); + program_["u_camera_position"] = math::homogeneous(options.camera_position); for (auto const & state : render_states_) { if (!state.mesh) continue; - program_["u_material"] = geom::vector{state.material.ambient, state.material.diffuse, state.material.specular, state.material.shininess}; + program_["u_material"] = math::vector{state.material.ambient, state.material.diffuse, state.material.specular, state.material.shininess}; state.mesh->draw(); } @@ -177,12 +177,12 @@ struct shadow_map_builder struct render_state { gfx::mesh const * mesh; - geom::box bbox; + math::box bbox; }; struct render_options { - geom::vector light; + math::vector light; }; shadow_map_builder(std::size_t width, std::size_t height); @@ -191,7 +191,7 @@ struct shadow_map_builder void build(render_options const & options); - geom::matrix const & transform() const; + math::matrix const & transform() const; gfx::texture_2d const & texture() const; @@ -201,7 +201,7 @@ private: gfx::framebuffer framebuffer_; gfx::texture_2d depth_texture_; - geom::matrix transform_; + math::matrix transform_; }; shadow_map_builder::shadow_map_builder(std::size_t width, std::size_t height) @@ -224,7 +224,7 @@ void shadow_map_builder::push(render_state const & state) void shadow_map_builder::build(render_options const & options) { - geom::vector light_axes[3]; + math::vector light_axes[3]; light_axes[2] = -options.light; light_axes[1] = {0.f, 0.f, 1.f}; @@ -232,24 +232,24 @@ void shadow_map_builder::build(render_options const & options) if (light_axes[2][2] > 0.5f) light_axes[1] = {0.f, 1.f, 0.f}; - geom::gram_schmidt(light_axes[2], light_axes[1]); + math::gram_schmidt(light_axes[2], light_axes[1]); - light_axes[0] = geom::cross(light_axes[2], light_axes[1]); + light_axes[0] = math::cross(light_axes[2], light_axes[1]); - geom::box light_bbox; + math::box light_bbox; - geom::point origin = geom::point::zero(); + math::point origin = math::point::zero(); for (auto const & state : render_states_) { - for (auto const & v : geom::vertices(state.bbox)) + for (auto const & v : math::vertices(state.bbox)) { for (std::size_t i = 0; i < 3; ++i) - light_bbox[i] |= geom::dot(light_axes[i], v - origin); + light_bbox[i] |= math::dot(light_axes[i], v - origin); } } - transform_ = geom::orthographic_camera{light_bbox}.projection() * geom::homogeneous(geom::by_rows(light_axes[0], light_axes[1], light_axes[2])); + transform_ = math::orthographic_camera{light_bbox}.projection() * math::homogeneous(math::by_rows(light_axes[0], light_axes[1], light_axes[2])); framebuffer_.bind(); gl::DrawBuffer(gl::NONE); @@ -289,7 +289,7 @@ void shadow_map_builder::build(render_options const & options) */ } -geom::matrix const & shadow_map_builder::transform() const +math::matrix const & shadow_map_builder::transform() const { return transform_; } @@ -399,21 +399,21 @@ struct shadow_renderer struct material material; gfx::mesh const * mesh; bool casts_shadow; - std::optional> bbox; + std::optional> bbox; }; struct light { - geom::vector position; + math::vector position; }; struct render_options { gfx::framebuffer const * framebuffer; GLenum draw_buffer; - geom::box viewport; - geom::matrix transform; - geom::point camera_position; + math::box viewport; + math::matrix transform; + math::point camera_position; struct light light; }; @@ -460,7 +460,7 @@ void shadow_renderer::render(render_options const & options) program_.bind(); program_["u_transform"] = options.transform; program_["u_light_position"] = options.light.position; - program_["u_camera_position"] = geom::homogeneous(options.camera_position); + program_["u_camera_position"] = math::homogeneous(options.camera_position); program_["u_shadow_transform"] = builder_.transform(); program_["u_shadow_map"] = 0; @@ -469,7 +469,7 @@ void shadow_renderer::render(render_options const & options) for (auto const & state : render_states_) { - program_["u_material"] = geom::vector{state.material.ambient, state.material.diffuse, state.material.specular, state.material.shininess}; + program_["u_material"] = math::vector{state.material.ambient, state.material.diffuse, state.material.specular, state.material.shininess}; state.mesh->draw(); } @@ -478,15 +478,15 @@ void shadow_renderer::render(render_options const & options) struct vertex { - geom::point position; - geom::vector normal; + math::point position; + math::vector normal; gfx::color_rgba color; }; struct shadow_app : app::application_base { - geom::spherical_camera camera; + math::spherical_camera camera; shadow_renderer renderer; @@ -495,15 +495,15 @@ struct shadow_app gfx::mesh cube_mesh; shadow_renderer::material cube_material; - geom::box cube_bbox; + math::box cube_bbox; gfx::mesh sphere_mesh; shadow_renderer::material sphere_material; - geom::box sphere_bbox; + math::box sphere_bbox; gfx::mesh torus_mesh; shadow_renderer::material torus_material; - geom::box torus_bbox; + math::box torus_bbox; util::clock> clock; float time = 0.f; @@ -529,10 +529,10 @@ shadow_app::shadow_app(options const &, context const & context) camera.near_clip = 0.1f; camera.far_clip = 1000.f; - camera.fov_y = geom::rad(45.f); + camera.fov_y = math::rad(45.f); camera.azimuthal_angle = 0.f; - camera.elevation_angle = geom::rad(30.f); + camera.elevation_angle = math::rad(30.f); camera.target = {0.f, 0.f, 0.f}; camera.distance = 10.f; @@ -547,7 +547,7 @@ shadow_app::shadow_app(options const &, context const & context) vertices.push_back({{ 10.f, -10.f, 0.f}, {0.f, 0.f, 1.f}, {127, 127, 127, 255}}); vertices.push_back({{ 10.f, 10.f, 0.f}, {0.f, 0.f, 1.f}, {127, 127, 127, 255}}); - plane_mesh.setup, geom::vector, gfx::normalized>(); + plane_mesh.setup, math::vector, gfx::normalized>(); plane_mesh.load(vertices, gl::TRIANGLES, gl::STATIC_DRAW); plane_material.ambient = 0.2f; @@ -557,12 +557,12 @@ shadow_app::shadow_app(options const &, context const & context) } { - auto cube = geom::box{{{-1.f, 1.f}, {-1.f, 1.f}, {0.f, 5.f}}}; + auto cube = math::box{{{-1.f, 1.f}, {-1.f, 1.f}, {0.f, 5.f}}}; - auto vertices = geom::vertices(cube); - auto faces = geom::faces(cube); - auto normals = geom::flat_normals(vertices, faces); - auto flat_vertices = geom::deindex(vertices, faces); + auto vertices = math::vertices(cube); + auto faces = math::faces(cube); + auto normals = math::flat_normals(vertices, faces); + auto flat_vertices = math::deindex(vertices, faces); std::vector mesh_vertices; @@ -573,7 +573,7 @@ shadow_app::shadow_app(options const &, context const & context) mesh_vertices.push_back({flat_vertices[i][2], normals[i], {255, 127, 127, 255}}); } - cube_mesh.setup, geom::vector, gfx::normalized>(); + cube_mesh.setup, math::vector, gfx::normalized>(); cube_mesh.load(mesh_vertices, gl::TRIANGLES, gl::STATIC_DRAW); cube_material.ambient = 0.2f; @@ -587,7 +587,7 @@ shadow_app::shadow_app(options const &, context const & context) { std::vector vertices; - geom::point const position = {3.f, 2.f, 3.f}; + math::point const position = {3.f, 2.f, 3.f}; float const radius = 1.f; @@ -599,19 +599,19 @@ shadow_app::shadow_app(options const &, context const & context) { for (int i = 0; i < 4 * N; ++i) { - float a = (geom::pi * i) / (2 * N); - float b = (geom::pi * j) / (2 * N); + float a = (math::pi * i) / (2 * N); + float b = (math::pi * j) / (2 * N); - geom::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; + math::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; vertices.push_back({position + radius * n, n, color}); } } - vertices.push_back({position + geom::vector{0.f, 0.f, -radius}, {0.f, 0.f, -1.f}, color}); - vertices.push_back({position + geom::vector{0.f, 0.f, radius}, {0.f, 0.f, 1.f}, color}); + vertices.push_back({position + math::vector{0.f, 0.f, -radius}, {0.f, 0.f, -1.f}, color}); + vertices.push_back({position + math::vector{0.f, 0.f, radius}, {0.f, 0.f, 1.f}, color}); - std::vector> indices; + std::vector> indices; auto idx = [](int i, int j) -> std::uint32_t { return (i % (4 * N)) + 4 * N * (j + N - 1); }; @@ -634,7 +634,7 @@ shadow_app::shadow_app(options const &, context const & context) indices.push_back({idx(i, N - 1), idx(i + 1, N - 1), (2 * N - 1) * (4 * N) + 1}); } - sphere_mesh.setup, geom::vector, gfx::normalized>(); + sphere_mesh.setup, math::vector, gfx::normalized>(); sphere_mesh.load(vertices, indices, gl::STATIC_DRAW); sphere_material.ambient = 0.2f; @@ -650,7 +650,7 @@ shadow_app::shadow_app(options const &, context const & context) { std::vector vertices; - geom::point const position = {-3.f, 2.f, 3.f}; + math::point const position = {-3.f, 2.f, 3.f}; float const radius1 = 1.f; float const radius2 = 0.2f; @@ -664,18 +664,18 @@ shadow_app::shadow_app(options const &, context const & context) { for (int i = 0; i < N; ++i) { - float a = (2.f * geom::pi * i) / N; - float b = (2.f * geom::pi * j) / M; + float a = (2.f * math::pi * i) / N; + float b = (2.f * math::pi * j) / M; - geom::vector r{std::cos(a), std::sin(a), 0.f}; + math::vector r{std::cos(a), std::sin(a), 0.f}; - geom::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; + math::vector n{std::cos(a) * std::cos(b), std::sin(a) * std::cos(b), std::sin(b)}; vertices.push_back({position + radius1 * r + radius2 * n, n, color}); } } - std::vector> indices; + std::vector> indices; auto idx = [](int i, int j) -> std::uint32_t { return (i % N) + N * (j % M); }; @@ -688,7 +688,7 @@ shadow_app::shadow_app(options const &, context const & context) } } - torus_mesh.setup, geom::vector, gfx::normalized>(); + torus_mesh.setup, math::vector, gfx::normalized>(); torus_mesh.load(vertices, indices, gl::STATIC_DRAW); torus_material.ambient = 0.2f; @@ -745,7 +745,7 @@ void shadow_app::present() else clock.restart(); - geom::vector light_dir = {std::cos(time), std::sin(time), 0.5f}; + math::vector light_dir = {std::cos(time), std::sin(time), 0.5f}; gfx::framebuffer::null().bind(); gl::DrawBuffer(gl::BACK); @@ -768,7 +768,7 @@ void shadow_app::present() options.viewport = {{{0, state().size[0]}, {0, state().size[1]}}}; options.draw_buffer = gl::BACK; options.transform = camera.transform(); - options.light.position = geom::homogeneous(light_dir); + options.light.position = math::homogeneous(light_dir); options.camera_position = camera.position(); renderer.render(options); diff --git a/examples/soft_creatures_2d.cpp b/examples/soft_creatures_2d.cpp index 2b650cc8..6d0e4125 100644 --- a/examples/soft_creatures_2d.cpp +++ b/examples/soft_creatures_2d.cpp @@ -2,10 +2,10 @@ #include #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include #include @@ -28,8 +28,8 @@ static float const hills_friction = 100.f; static float const spring_damping = 10.f; static float const max_spring_force = 25000.f; -static geom::interval const cell_size_range{1.f, 1.f}; -static geom::interval const cell_period_range{0.25f, 4.f}; +static math::interval const cell_size_range{1.f, 1.f}; +static math::interval const cell_period_range{0.25f, 4.f}; static float const muscle_expand_extent = 0.5f; static int const population_size = 4 * 1024; @@ -70,7 +70,7 @@ float spring_force(cell_type type) return 0.f; } -static geom::vector const neighbours[4] = +static math::vector const neighbours[4] = { {-1, 0}, { 1, 0}, @@ -80,12 +80,12 @@ static geom::vector const neighbours[4] = struct map { - geom::vector ground_normal{0.f, 1.f}; + math::vector ground_normal{0.f, 1.f}; - std::vector> ground_points; + std::vector> ground_points; }; -using creature_blueprint = std::unordered_map, cell_info>; +using creature_blueprint = std::unordered_map, cell_info>; struct creature { @@ -99,16 +99,16 @@ struct creature int generation = 0; std::optional score = std::nullopt; - std::vector> positions; - std::vector> velocities; + std::vector> positions; + std::vector> velocities; std::vector cells; - std::vector> outer_edges; + std::vector> outer_edges; - geom::point center() const; - geom::vector center_velocity() const; - void translate(geom::vector const & delta); - void push(geom::vector const & delta_velocity); + math::point center() const; + math::vector center_velocity() const; + void translate(math::vector const & delta); + void push(math::vector const & delta_velocity); bool dead() const; @@ -118,9 +118,9 @@ struct creature void compute_outer_edges(); }; -geom::point creature::center() const +math::point creature::center() const { - geom::vector sum{0.f, 0.f}; + math::vector sum{0.f, 0.f}; for (int i = 1; i < positions.size(); ++i) { sum += positions[i] - positions[0]; @@ -128,9 +128,9 @@ geom::point creature::center() const return positions[0] + sum / (1.f * positions.size()); } -geom::vector creature::center_velocity() const +math::vector creature::center_velocity() const { - geom::vector sum{0.f, 0.f}; + math::vector sum{0.f, 0.f}; for (int i = 1; i < velocities.size(); ++i) { sum += velocities[i]; @@ -138,13 +138,13 @@ geom::vector creature::center_velocity() const return sum / (1.f * velocities.size()); } -void creature::translate(geom::vector const & delta) +void creature::translate(math::vector const & delta) { for (auto & pos : positions) pos += delta; } -void creature::push(geom::vector const & delta_velocity) +void creature::push(math::vector const & delta_velocity) { for (auto & vel : velocities) vel += delta_velocity; @@ -165,14 +165,14 @@ void creature::update(float dt) for (auto & vel : velocities) { - auto v = geom::length(vel); + auto v = math::length(vel); vel -= air_friction * v * vel * dt; } for (int i = 0; i < positions.size(); ++i) positions[i] += velocities[i] * dt; - static geom::vector const deltas[4] + static math::vector const deltas[4] { {-0.5f, -0.5f}, { 0.5f, -0.5f}, @@ -189,13 +189,13 @@ void creature::update(float dt) cell.info.phase += dt / cell.info.period; while (cell.info.phase >= 1.f) cell.info.phase -= 1.f; - size *= 1.f + muscle_expand_extent * std::sin(cell.info.phase * 2.f * geom::pi); + size *= 1.f + muscle_expand_extent * std::sin(cell.info.phase * 2.f * math::pi); } - geom::point center{0.f, 0.f}; + math::point center{0.f, 0.f}; for (auto idx : cell.indices) { - center += (positions[idx] - geom::point{0.f, 0.f}) * 0.25f; + center += (positions[idx] - math::point{0.f, 0.f}) * 0.25f; } float A = 0.f; @@ -206,33 +206,33 @@ void creature::update(float dt) auto const p = positions[cell.indices[i]] - center; auto const q = deltas[i] * size; - A += geom::dot(p, q); - B += geom::det(p, q); + A += math::dot(p, q); + B += math::det(p, q); } float const angle = - std::atan2(B, A); for (int i = 0; i < 4; ++i) { - auto const target = center + geom::rotate(deltas[i] * size, angle); + auto const target = center + math::rotate(deltas[i] * size, angle); auto force = spring_force(cell.info.type) * (target - positions[cell.indices[i]]); - if (auto f = geom::length(force); f > max_spring_force) + if (auto f = math::length(force); f > max_spring_force) cell.info.type = cell_type::dead; velocities[cell.indices[i]] += force * dt; } - auto cmvel = geom::vector{0.f, 0.f}; + auto cmvel = math::vector{0.f, 0.f}; auto rotation = 0.f; for (auto idx : cell.indices) { cmvel += velocities[idx] * 0.25f; - rotation += geom::det(positions[idx] - center, velocities[idx]) * (0.25f / geom::length_sqr(positions[idx] - center)); + rotation += math::det(positions[idx] - center, velocities[idx]) * (0.25f / math::length_sqr(positions[idx] - center)); } for (auto idx : cell.indices) { - auto target_vel = cmvel + geom::ort(positions[idx] - center) * rotation; + auto target_vel = cmvel + math::ort(positions[idx] - center) * rotation; velocities[idx] += (target_vel - velocities[idx]) * (1.f - std::exp(- spring_damping * dt)); } @@ -243,15 +243,15 @@ void creature::collide(map const & map, float dt) { for (int i = 0; i < positions.size(); ++i) { - auto delta = positions[i] - geom::point{0.f, 0.f}; - auto dist = geom::dot(delta, map.ground_normal); + auto delta = positions[i] - math::point{0.f, 0.f}; + auto dist = math::dot(delta, map.ground_normal); if (dist < 0.f) { positions[i] -= dist * map.ground_normal; - auto tangent = geom::ort(map.ground_normal); - auto vn = geom::dot(velocities[i], map.ground_normal); - auto vt = geom::dot(velocities[i], tangent); + auto tangent = math::ort(map.ground_normal); + auto vn = math::dot(velocities[i], map.ground_normal); + auto vt = math::dot(velocities[i], tangent); vn = std::max(0.f, vn); vt *= std::exp(- dt * ground_friction); @@ -264,16 +264,16 @@ void creature::collide(map const & map, float dt) { auto j = (it - map.ground_points.begin()) - 1; - auto n = geom::normalized(geom::ort(map.ground_points[j + 1] - map.ground_points[j])); - float dist = geom::dot(positions[i] - map.ground_points[j], n); + auto n = math::normalized(math::ort(map.ground_points[j + 1] - map.ground_points[j])); + float dist = math::dot(positions[i] - map.ground_points[j], n); if (dist < 0.f) { positions[i] -= dist * n; - auto tangent = geom::ort(n); - auto vn = geom::dot(velocities[i], n); - auto vt = geom::dot(velocities[i], tangent); + auto tangent = math::ort(n); + auto vn = math::dot(velocities[i], n); + auto vt = math::dot(velocities[i], tangent); vn = std::max(0.f, vn); vt *= std::exp(- dt * hills_friction); @@ -286,13 +286,13 @@ void creature::collide(map const & map, float dt) void creature::compute_outer_edges() { - util::hash_set> edge_set; + util::hash_set> edge_set; for (auto const & cell : cells) { for (int i = 0; i < 4; ++i) { - geom::segment segment; + math::segment segment; segment[0] = cell.indices[i]; segment[1] = cell.indices[(i + 1) % 4]; edge_set.insert(segment); @@ -330,14 +330,14 @@ void draw(gfx::painter & painter, creature const & creature) { for (auto const & cell : creature.cells) { - geom::point center{0.f, 0.f}; + math::point center{0.f, 0.f}; for (auto idx : cell.indices) - center += 0.25f * (creature.positions[idx] - geom::point{0.f, 0.f}); + center += 0.25f * (creature.positions[idx] - math::point{0.f, 0.f}); gfx::color_rgba const color = cell_color(cell.info.type); gfx::color_rgba const bgcolor = gfx::dark(color, 0.25f); - geom::point ps[4]; + math::point ps[4]; for (int i = 0; i < 4; ++i) ps[i] = creature.positions[cell.indices[i]]; @@ -345,7 +345,7 @@ void draw(gfx::painter & painter, creature const & creature) painter.triangle(ps[2], ps[0], ps[3], bgcolor); for (int i = 0; i < 4; ++i) - ps[i] = geom::lerp(ps[i], center, 0.25f); + ps[i] = math::lerp(ps[i], center, 0.25f); painter.triangle(ps[0], ps[1], ps[2], color); painter.triangle(ps[2], ps[0], ps[3], color); @@ -367,19 +367,19 @@ struct creature_builder : blueprint_(std::move(blueprint)) {} - void add(geom::point const position, cell_info const & info) + void add(math::point const position, cell_info const & info) { blueprint_[position] = info; } - bool contains(geom::point const & position) const + bool contains(math::point const & position) const { return blueprint_.contains(position); } creature build(int generation) { - static geom::vector const vertex_delta[4] + static math::vector const vertex_delta[4] { {0, 0}, {1, 0}, @@ -390,7 +390,7 @@ struct creature_builder creature result; result.generation = generation; - util::hash_map, std::uint32_t> vertex_id; + util::hash_map, std::uint32_t> vertex_id; for (auto const & cell : blueprint_) { @@ -398,7 +398,7 @@ struct creature_builder cell_out.info = cell.second; for (int i = 0; i < 4; ++i) { - geom::point vertex = cell.first + vertex_delta[i]; + math::point vertex = cell.first + vertex_delta[i]; if (auto it = vertex_id.find(vertex); it != vertex_id.end()) { cell_out.indices[i] = it->second; @@ -406,8 +406,8 @@ struct creature_builder else { cell_out.indices[i] = (vertex_id[vertex] = result.positions.size()); - result.positions.push_back(geom::cast(vertex)); - result.velocities.push_back(geom::vector::zero()); + result.positions.push_back(math::cast(vertex)); + result.velocities.push_back(math::vector::zero()); } } } @@ -422,9 +422,9 @@ struct creature_builder if (blueprint_.empty()) return false; - util::hash_set> visited; + util::hash_set> visited; - std::deque> queue; + std::deque> queue; queue.push_back(blueprint_.begin()->first); visited.insert(queue.back()); @@ -480,7 +480,7 @@ struct soft_creatures_2d_app // map_.ground_points.push_back({5.f, 0.f}); // for (int x = 10; x <= 200; x += 1) // map_.ground_points.push_back({x / 2.f, random::uniform(rng, 0.f, std::min(x, 200) / 200.f * 4.f)}); -// map_.ground_points.push_back({x, std::min(2.f, geom::sqr(x - 10) * 0.25f)}); +// map_.ground_points.push_back({x, std::min(2.f, math::sqr(x - 10) * 0.25f)}); // map_.ground_points.push_back({x, random::uniform(rng, 0.f, 4.f)}); for (int species = 0; species < population_size; ++species) @@ -560,7 +560,7 @@ struct soft_creatures_2d_app float max_view_x = 0.f; for (int c = 0; c < display_creatures_.size(); ++c) for (auto const & p : display_creatures_[c].positions) - geom::make_max(max_view_x, p[0]); + math::make_max(max_view_x, p[0]); max_view_x = std::max(max_view_x + 10.f, max_view_x_); max_view_x_ += (max_view_x - max_view_x_) * (1.f - std::exp(- 4.f * frame_dt)); @@ -574,14 +574,14 @@ struct soft_creatures_2d_app for (int c = 0; c < display_creatures_.size(); ++c) { float aspect_ratio = state().size[0] * 1.f / state().size[1] * display_creatures_.size(); - geom::box view_box = {{{0.f, 0.f}, {0.f, 0.f}}}; + math::box view_box = {{{0.f, 0.f}, {0.f, 0.f}}}; view_box[0] = {max_view_x_ - display_width, max_view_x_}; view_box[1] = {0.f, view_box[0].length() / aspect_ratio}; view_box[1] -= 2.f; - geom::box ground_box; - ground_box[0] = geom::interval::full(); - ground_box[1] = {geom::limits::min(), 0.f}; + math::box ground_box; + ground_box[0] = math::interval::full(); + ground_box[1] = {math::limits::min(), 0.f}; ground_box = ground_box & view_box; gfx::color_rgba ground_color{127, 91, 65, 255}; painter_.rect(ground_box, ground_color); @@ -611,7 +611,7 @@ struct soft_creatures_2d_app float scale = 2.f * view_box[0].length() / state().size[0]; painter_.line({x, 0.f}, {x, y}, 0.125f, {255, 255, 255, 255}, false); - painter_.text3d({x + 0.1f, -0.5f, 0.f}, util::to_string(r * 5), {.scale = scale, .x = gfx::painter::x_align::left, .c = {255, 255, 255, 127}}, geom::scale({1.f, -1.f, 1.f}).linear_matrix()); + painter_.text3d({x + 0.1f, -0.5f, 0.f}, util::to_string(r * 5), {.scale = scale, .x = gfx::painter::x_align::left, .c = {255, 255, 255, 127}}, math::scale({1.f, -1.f, 1.f}).linear_matrix()); } draw(painter_, display_creatures_[c]); @@ -619,7 +619,7 @@ struct soft_creatures_2d_app float height = view_box[1].length(); view_box[1].max += c * height; view_box[1].min = view_box[1].max - display_creatures_.size() * height; - painter_.render(geom::orthographic_camera(view_box).transform()); + painter_.render(math::orthographic_camera(view_box).transform()); } int text_row = 0; @@ -672,7 +672,7 @@ struct soft_creatures_2d_app painter_.line({0.f, y}, {state().size[0], y}, 4.f, {0, 0, 0, 255}, false); } - painter_.render(geom::window_camera(state().size[0], state().size[1]).transform()); + painter_.render(math::window_camera(state().size[0], state().size[1]).transform()); } private: @@ -726,12 +726,12 @@ private: auto center = creature.center(); float radius = -std::numeric_limits::infinity(); for (auto const & pos : creature.positions) - geom::make_max(radius, geom::distance(pos, center)); + math::make_max(radius, math::distance(pos, center)); float angle = 0.f; for (auto & pos : creature.positions) - pos = geom::vector{0.f, radius} + center + geom::rotate(pos - center, angle); + pos = math::vector{0.f, radius} + center + math::rotate(pos - center, angle); } } } @@ -767,13 +767,13 @@ private: auto center = sim_creature.center(); float radius = -std::numeric_limits::infinity(); for (auto const & pos : sim_creature.positions) - geom::make_max(radius, geom::distance(pos, center)); + math::make_max(radius, math::distance(pos, center)); - auto angle = random::uniform(rng, -1.f, 1.f) * geom::rad(30.f); + auto angle = random::uniform(rng, -1.f, 1.f) * math::rad(30.f); angle = 0.f; for (auto & pos : sim_creature.positions) - pos = geom::vector{0.f, radius} + center + geom::rotate(pos - center, angle); + pos = math::vector{0.f, radius} + center + math::rotate(pos - center, angle); auto start_pos = sim_creature.center(); float max_y = start_pos[1]; @@ -784,7 +784,7 @@ private: { sim_creature.update(sim_dt); sim_creature.collide(map_, sim_dt); - geom::make_max(max_y, sim_creature.center()[1]); + math::make_max(max_y, sim_creature.center()[1]); auto speed = sim_creature.center_velocity(); sum_speed_x += speed[0]; @@ -868,7 +868,7 @@ private: blueprint[cell.first] = cell.second; } - std::vector> erase_cells; + std::vector> erase_cells; for (auto const & cell : blueprint) { if (random::uniform(rng) < erase_probability) @@ -878,7 +878,7 @@ private: for (auto cell : erase_cells) blueprint.erase(cell); - std::vector, cell_info>> grow_cells; + std::vector, cell_info>> grow_cells; for (auto const & cell : blueprint) { for (auto n : neighbours) @@ -921,9 +921,9 @@ private: cell.info.phase += randn(rng) * 0.1f * mutation_boost; cell.info.period += randn(rng) * 0.1f * mutation_boost; - cell.info.size = geom::clamp(cell.info.size, cell_size_range); + cell.info.size = math::clamp(cell.info.size, cell_size_range); cell.info.phase = std::fmod(cell.info.phase, 1.f); - cell.info.period = geom::clamp(cell.info.period, cell_period_range); + cell.info.period = math::clamp(cell.info.period, cell_period_range); new_creature.score = std::nullopt; new_creature.generation = generation; diff --git a/examples/soft_plants_2d.cpp b/examples/soft_plants_2d.cpp index 8751312f..7ff04e87 100644 --- a/examples/soft_plants_2d.cpp +++ b/examples/soft_plants_2d.cpp @@ -2,11 +2,11 @@ #include #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include #include @@ -106,7 +106,7 @@ gfx::color_rgba cell_color(cell_data type) ), type); } -static geom::vector const neighbours[4] = +static math::vector const neighbours[4] = { {-1, 0}, { 1, 0}, @@ -118,23 +118,23 @@ struct map { struct wall { - geom::point origin; - geom::vector normal; + math::point origin; + math::vector normal; - bool contains(geom::point const & p) const + bool contains(math::point const & p) const { - return geom::dot(p - origin, normal) <= 0.f; + return math::dot(p - origin, normal) <= 0.f; } }; std::vector walls; - std::vector> ground_points; + std::vector> ground_points; std::optional radius; }; -using cell_map = std::unordered_map, cell_data>; +using cell_map = std::unordered_map, cell_data>; struct genome { @@ -154,21 +154,21 @@ struct creature struct genome genome; int generation = 0; - std::vector> positions; - std::vector> velocities; + std::vector> positions; + std::vector> velocities; std::vector collided; - std::vector>> fixed; + std::vector>> fixed; std::vector cells; float energy = 0.f; - std::vector> outer_edges; + std::vector> outer_edges; - geom::point center() const; - geom::point cell_center(cell const & cell) const; - geom::vector center_velocity() const; - void translate(geom::vector const & delta); - void push(geom::vector const & delta_velocity); + math::point center() const; + math::point cell_center(cell const & cell) const; + math::vector center_velocity() const; + void translate(math::vector const & delta); + void push(math::vector const & delta_velocity); bool dead() const; @@ -179,9 +179,9 @@ struct creature void finalize_creation(); }; -geom::point creature::center() const +math::point creature::center() const { - geom::vector sum{0.f, 0.f}; + math::vector sum{0.f, 0.f}; for (int i = 1; i < positions.size(); ++i) { sum += positions[i] - positions[0]; @@ -189,17 +189,17 @@ geom::point creature::center() const return positions[0] + sum / (1.f * positions.size()); } -geom::point creature::cell_center(cell const & cell) const +math::point creature::cell_center(cell const & cell) const { - geom::point center{0.f, 0.f}; + math::point center{0.f, 0.f}; for (auto idx : cell.indices) - center += (positions[idx] - geom::point{0.f, 0.f}) * 0.25f; + center += (positions[idx] - math::point{0.f, 0.f}) * 0.25f; return center; } -geom::vector creature::center_velocity() const +math::vector creature::center_velocity() const { - geom::vector sum{0.f, 0.f}; + math::vector sum{0.f, 0.f}; for (int i = 1; i < velocities.size(); ++i) { sum += velocities[i]; @@ -207,13 +207,13 @@ geom::vector creature::center_velocity() const return sum / (1.f * velocities.size()); } -void creature::translate(geom::vector const & delta) +void creature::translate(math::vector const & delta) { for (auto & pos : positions) pos += delta; } -void creature::push(geom::vector const & delta_velocity) +void creature::push(math::vector const & delta_velocity) { for (auto & vel : velocities) vel += delta_velocity; @@ -228,7 +228,7 @@ std::vector creature::update(float dt, random::generator & rng) { for (int i = 0; i < velocities.size(); ++i) { -// velocities[i] -= gravity * dt * geom::normalized(positions[i] - geom::point{0.f, 0.f}); +// velocities[i] -= gravity * dt * math::normalized(positions[i] - math::point{0.f, 0.f}); velocities[i][1] -= gravity * dt; if (fixed[i]) velocities[i] += (*fixed[i] - positions[i]) * fixed_root_force * dt; @@ -236,7 +236,7 @@ std::vector creature::update(float dt, random::generator & rng) for (auto & vel : velocities) { -// auto v = geom::length(vel); +// auto v = math::length(vel); // vel -= air_friction * v * vel * dt; vel *= std::exp(- air_friction * dt); } @@ -244,7 +244,7 @@ std::vector creature::update(float dt, random::generator & rng) for (int i = 0; i < positions.size(); ++i) positions[i] += velocities[i] * dt; - static geom::vector const deltas[4] + static math::vector const deltas[4] { {-0.5f, -0.5f}, { 0.5f, -0.5f}, @@ -329,33 +329,33 @@ std::vector creature::update(float dt, random::generator & rng) auto const p = positions[cell.indices[i]] - center; auto const q = deltas[i] * size; - A += geom::dot(p, q); - B += geom::det(p, q); + A += math::dot(p, q); + B += math::det(p, q); } float const angle = - std::atan2(B, A); for (int i = 0; i < 4; ++i) { - auto const target = center + geom::rotate(deltas[i] * size, angle); + auto const target = center + math::rotate(deltas[i] * size, angle); auto force = spring_force(cell.data) * (target - positions[cell.indices[i]]); -// if (auto f = geom::length(force); f > max_spring_force) +// if (auto f = math::length(force); f > max_spring_force) // ; velocities[cell.indices[i]] += force * dt; } - auto cmvel = geom::vector{0.f, 0.f}; + auto cmvel = math::vector{0.f, 0.f}; auto rotation = 0.f; for (auto idx : cell.indices) { cmvel += velocities[idx] * 0.25f; - rotation += geom::det(positions[idx] - center, velocities[idx]) * (0.25f / geom::length_sqr(positions[idx] - center)); + rotation += math::det(positions[idx] - center, velocities[idx]) * (0.25f / math::length_sqr(positions[idx] - center)); } for (auto idx : cell.indices) { - auto target_vel = cmvel + geom::ort(positions[idx] - center) * rotation; + auto target_vel = cmvel + math::ort(positions[idx] - center) * rotation; velocities[idx] += (target_vel - velocities[idx]) * (1.f - std::exp(- spring_damping * dt)); } @@ -372,8 +372,8 @@ void creature::collide(map const & map, float dt) { if (map.radius) { - auto delta = positions[i] - geom::point{0.f, 0.f}; - auto dist = geom::length(delta); + auto delta = positions[i] - math::point{0.f, 0.f}; + auto dist = math::length(delta); auto normal = delta / dist; dist -= *map.radius; @@ -381,9 +381,9 @@ void creature::collide(map const & map, float dt) { positions[i] -= dist * normal; - auto tangent = geom::ort(normal); - auto vn = geom::dot(velocities[i], normal); - auto vt = geom::dot(velocities[i], tangent); + auto tangent = math::ort(normal); + auto vn = math::dot(velocities[i], normal); + auto vt = math::dot(velocities[i], tangent); vn = std::max(0.f, vn); vt *= std::exp(- dt * ground_friction); @@ -397,14 +397,14 @@ void creature::collide(map const & map, float dt) for (auto const & wall : map.walls) { auto delta = positions[i] - wall.origin; - auto dist = geom::dot(delta, wall.normal); + auto dist = math::dot(delta, wall.normal); if (dist < 0.f) { positions[i] -= dist * wall.normal; - auto tangent = geom::ort(wall.normal); - auto vn = geom::dot(velocities[i], wall.normal); - auto vt = geom::dot(velocities[i], tangent); + auto tangent = math::ort(wall.normal); + auto vn = math::dot(velocities[i], wall.normal); + auto vt = math::dot(velocities[i], tangent); vn = std::max(0.f, vn); vt *= std::exp(- dt * ground_friction); @@ -420,16 +420,16 @@ void creature::collide(map const & map, float dt) { auto j = (it - map.ground_points.begin()) - 1; - auto n = geom::normalized(geom::ort(map.ground_points[j + 1] - map.ground_points[j])); - float dist = geom::dot(positions[i] - map.ground_points[j], n); + auto n = math::normalized(math::ort(map.ground_points[j + 1] - map.ground_points[j])); + float dist = math::dot(positions[i] - map.ground_points[j], n); if (dist < 0.f) { positions[i] -= dist * n; - auto tangent = geom::ort(n); - auto vn = geom::dot(velocities[i], n); - auto vt = geom::dot(velocities[i], tangent); + auto tangent = math::ort(n); + auto vn = math::dot(velocities[i], n); + auto vt = math::dot(velocities[i], tangent); vn = std::max(0.f, vn); vt *= std::exp(- dt * hills_friction); @@ -459,13 +459,13 @@ void creature::finalize_creation() collided.assign(positions.size(), false); fixed.assign(positions.size(), std::nullopt); - util::hash_set> edge_set; + util::hash_set> edge_set; for (auto const & cell : cells) { for (int i = 0; i < 4; ++i) { - geom::segment segment; + math::segment segment; segment[0] = cell.indices[i]; segment[1] = cell.indices[(i + 1) % 4]; edge_set.insert(segment); @@ -488,11 +488,11 @@ void collide(std::vector & creatures, float dt) { int creature; int cell; - geom::point center; + math::point center; float radius; }; - util::hash_map, std::vector> bins; + util::hash_map, std::vector> bins; float bin_size = 1.f; for (int c = 0; c < creatures.size(); ++c) @@ -502,7 +502,7 @@ void collide(std::vector & creatures, float dt) auto const & cell = creatures[c].cells[i]; cell_data data{c, i, creatures[c].cell_center(cell), size(cell.data)}; - geom::point bin_id{std::floor(data.center[0] / bin_size), std::floor(data.center[1] / bin_size)}; + math::point bin_id{std::floor(data.center[0] / bin_size), std::floor(data.center[1] / bin_size)}; bins[bin_id].push_back(data); } } @@ -510,7 +510,7 @@ void collide(std::vector & creatures, float dt) auto collide_cells = [&](cell_data const & data1, cell_data const & data2) { auto delta = data2.center - data1.center; - auto distance = geom::length(delta); + auto distance = math::length(delta); float min_distance = data1.radius + data2.radius; @@ -548,7 +548,7 @@ void collide(std::vector & creatures, float dt) { if (x == 0 && y == 0) continue; - auto nid = bin.first + geom::vector{x, y}; + auto nid = bin.first + math::vector{x, y}; if (bin.first < nid) continue; @@ -567,7 +567,7 @@ void enlighten(std::vector & creatures) { int creature; int cell; - geom::point center; + math::point center; float radius; }; @@ -597,14 +597,14 @@ void enlighten(std::vector & creatures) return std::tie(d1.center[1], d1.center[0]) < std::tie(d2.center[1], d2.center[0]); }); - geom::interval bin_extent{bin.first * bin_size, (bin.first + 1) * bin_size}; + math::interval bin_extent{bin.first * bin_size, (bin.first + 1) * bin_size}; float received_light = 1.f; for (int i = bin.second.size(); i --> 0;) { auto const & data = bin.second[i]; auto & cell = creatures[data.creature].cells[data.cell]; - geom::interval cell_extent{data.center[0] - data.radius, data.center[0] + data.radius}; + math::interval cell_extent{data.center[0] - data.radius, data.center[0] + data.radius}; auto portion = std::min(1.f, (cell_extent & bin_extent).length() / cell_extent.length()); @@ -628,14 +628,14 @@ void draw(gfx::painter & painter, creature const & creature, float lag) for (auto const & cell : creature.cells) { - geom::point center{0.f, 0.f}; + math::point center{0.f, 0.f}; for (auto idx : cell.indices) - center += 0.25f * (creature.positions[idx] - geom::point{0.f, 0.f}); + center += 0.25f * (creature.positions[idx] - math::point{0.f, 0.f}); gfx::color_rgba const color = gfx::dark(cell_color(cell.data), 0.75f * (1.f - cell.received_light)); gfx::color_rgba const bgcolor = gfx::dark(color, 0.25f); - geom::point ps[4]; + math::point ps[4]; for (int i = 0; i < 4; ++i) ps[i] = point(cell.indices[i]); @@ -643,7 +643,7 @@ void draw(gfx::painter & painter, creature const & creature, float lag) painter.triangle(ps[2], ps[0], ps[3], bgcolor); for (int i = 0; i < 4; ++i) - ps[i] = geom::lerp(ps[i], center, 0.25f); + ps[i] = math::lerp(ps[i], center, 0.25f); painter.triangle(ps[0], ps[1], ps[2], color); painter.triangle(ps[2], ps[0], ps[3], color); @@ -665,19 +665,19 @@ struct creature_builder : genome_(std::move(genome)) {} - void add(geom::point const position, cell_data const & data) + void add(math::point const position, cell_data const & data) { genome_.cells[position] = data; } - bool contains(geom::point const & position) const + bool contains(math::point const & position) const { return genome_.cells.contains(position); } creature build(int generation) { - static geom::vector const vertex_delta[4] + static math::vector const vertex_delta[4] { {0, 0}, {1, 0}, @@ -688,7 +688,7 @@ struct creature_builder creature result; result.generation = generation; - util::hash_map, std::uint32_t> vertex_id; + util::hash_map, std::uint32_t> vertex_id; for (auto const & cell : genome_.cells) { @@ -696,7 +696,7 @@ struct creature_builder cell_out.data = cell.second; for (int i = 0; i < 4; ++i) { - geom::point vertex = cell.first + vertex_delta[i]; + math::point vertex = cell.first + vertex_delta[i]; if (auto it = vertex_id.find(vertex); it != vertex_id.end()) { cell_out.indices[i] = it->second; @@ -704,8 +704,8 @@ struct creature_builder else { cell_out.indices[i] = (vertex_id[vertex] = result.positions.size()); - result.positions.push_back(geom::cast(vertex)); - result.velocities.push_back(geom::vector::zero()); + result.positions.push_back(math::cast(vertex)); + result.velocities.push_back(math::vector::zero()); } } } @@ -720,9 +720,9 @@ struct creature_builder if (genome_.cells.empty()) return false; - util::hash_set> visited; + util::hash_set> visited; - std::deque> queue; + std::deque> queue; queue.push_back(genome_.cells.begin()->first); visited.insert(queue.back()); @@ -773,7 +773,7 @@ void mutate(genome & genome, random::generator & rng) if (random::uniform(rng) < change_type_probability) cell.second = random_cell(rng); - std::vector> erase_cells; + std::vector> erase_cells; for (auto const & cell : genome.cells) if (random::uniform(rng) < erase_probability) erase_cells.push_back(cell.first); @@ -781,7 +781,7 @@ void mutate(genome & genome, random::generator & rng) for (auto const & cell : erase_cells) genome.cells.erase(cell); - std::vector, cell_data>> grow_cells; + std::vector, cell_data>> grow_cells; for (auto const & cell : genome.cells) for (auto n : neighbours) { @@ -808,7 +808,7 @@ std::optional creature::create_offspring(cell const & cell, random::ge auto center = result.center(); auto angle = random::uniform_angle(rng); for (auto & position : result.positions) - position = center + geom::rotate(position - center, angle); + position = center + math::rotate(position - center, angle); auto push = random::uniform_hemisphere_vector_distribution({0.f, 1.f})(rng); result.translate(push * 1.f); @@ -826,14 +826,14 @@ struct soft_creatures_2d_app // map_.ground_points.push_back({5.f, 0.f}); // for (int x = 10; x <= 200; x += 1) // map_.ground_points.push_back({x / 2.f, random::uniform(rng, 0.f, std::min(x, 200) / 200.f * 4.f)}); -// map_.ground_points.push_back({x, std::min(2.f, geom::sqr(x - 10) * 0.25f)}); +// map_.ground_points.push_back({x, std::min(2.f, math::sqr(x - 10) * 0.25f)}); // map_.ground_points.push_back({x, random::uniform(rng, 0.f, 4.f)}); map_.walls.push_back({{0.f, 0.f}, {0.f, 1.f}}); // map_.walls.push_back({{-40.f, 0.f}, {1.f, 0.f}}); // map_.walls.push_back({{40.f, 0.f}, {-1.f, 0.f}}); - map_.walls.push_back({{-20.f, 0.f}, geom::normalized(geom::vector{1.f, 1.f})}); - map_.walls.push_back({{20.f, 0.f}, geom::normalized(geom::vector{-1.f, 1.f})}); + map_.walls.push_back({{-20.f, 0.f}, math::normalized(math::vector{1.f, 1.f})}); + map_.walls.push_back({{20.f, 0.f}, math::normalized(math::vector{-1.f, 1.f})}); // map_.radius = 30.f; @@ -946,7 +946,7 @@ struct soft_creatures_2d_app gl::Clear(gl::COLOR_BUFFER_BIT); float aspect_ratio = state().size[0] * 1.f / state().size[1]; - geom::box view_box = {{{0.f, 0.f}, {0.f, 0.f}}}; + math::box view_box = {{{0.f, 0.f}, {0.f, 0.f}}}; view_box[0] = {-50.f, 50.f}; view_box[1] = {0.f, view_box[0].length() / aspect_ratio}; view_box[1] -= view_box[1].length() / 2.f; @@ -960,7 +960,7 @@ struct soft_creatures_2d_app for (auto const & wall : map_.walls) { - std::vector> points; + std::vector> points; for (float x = 0; x <= 1; ++x) { for (float y = 0; y <= 1; ++y) @@ -980,11 +980,11 @@ struct soft_creatures_2d_app // (p0 + t * dp - o) * n = 0 // t (dp*n) + (p0-o)*n = 0 - float a = geom::dot(p1 - p0, wall.normal); + float a = math::dot(p1 - p0, wall.normal); if (std::abs(a) < 1e-4f) continue; - float t = - geom::dot(p0 - wall.origin, wall.normal) / a; + float t = - math::dot(p0 - wall.origin, wall.normal) / a; if (t < 0.f || t > 1.f) continue; @@ -993,7 +993,7 @@ struct soft_creatures_2d_app } } - std::vector>::iterator> hull; + std::vector>::iterator> hull; if (!points.empty()) cg::graham_convex_hull(points.begin(), points.end(), std::back_inserter(hull)); @@ -1019,7 +1019,7 @@ struct soft_creatures_2d_app for (auto const & creature : creatures_) draw(painter_, creature, physics_lag_); - painter_.render(geom::orthographic_camera(view_box).transform()); + painter_.render(math::orthographic_camera(view_box).transform()); int text_row = 0; auto put_line = [&](std::string const & line) @@ -1038,7 +1038,7 @@ struct soft_creatures_2d_app put_line(util::to_string("Cells: ", cells)); put_line(util::to_string("Cell/cr.: ", cells * 1.f / creatures_.size())); - painter_.render(geom::window_camera(state().size[0], state().size[1]).transform()); + painter_.render(math::window_camera(state().size[0], state().size[1]).transform()); } private: @@ -1046,8 +1046,8 @@ private: util::clock<> frame_clock_; - geom::point view_center_{0.f, 20.f}; - geom::point view_center_tgt_ = view_center_; + math::point view_center_{0.f, 20.f}; + math::point view_center_tgt_ = view_center_; random::generator rng_{random::device{}}; diff --git a/examples/srtm.cpp b/examples/srtm.cpp index 1307c83c..0454d87b 100644 --- a/examples/srtm.cpp +++ b/examples/srtm.cpp @@ -8,13 +8,13 @@ #include #include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include @@ -83,7 +83,7 @@ static const float exaggeration = 1.f; struct height_provider { - float height_at(geom::vector const & v); + float height_at(math::vector const & v); struct datum_id_hash { @@ -100,13 +100,13 @@ struct height_provider std::unordered_set, datum_id_hash> no_datums; }; -float height_provider::height_at(geom::vector const & v) +float height_provider::height_at(math::vector const & v) { static std::filesystem::path const data_path = "/home/lisyarus/data/srtm/dem"; - float const lat = geom::deg(std::asin(v[2])); + float const lat = math::deg(std::asin(v[2])); - float const lon = geom::deg(std::atan2(v[0], -v[1])); + float const lon = math::deg(std::atan2(v[0], -v[1])); int ilat = std::floor(lat); int ilon = std::floor(lon); @@ -199,8 +199,8 @@ float height_provider::height_at(geom::vector const & v) return (int)values[(3600 - lat) * 3601 + lon] - 32768; }; - int const tlat = geom::clamp(std::floor((lat - ilat) * 3600), {0, 3600 - 1}); - int const tlon = geom::clamp(std::floor((lon - ilon) * 3600), {0, 3600 - 1}); + int const tlat = math::clamp(std::floor((lat - ilat) * 3600), {0, 3600 - 1}); + int const tlon = math::clamp(std::floor((lon - ilon) * 3600), {0, 3600 - 1}); float const mlat = (lat - ilat) * 3600.f - tlat; float const mlon = (lon - ilon) * 3600.f - tlon; @@ -210,7 +210,7 @@ float height_provider::height_at(geom::vector const & v) float h10 = at(tlat, tlon + 1); float h11 = at(tlat + 1, tlon + 1); - return geom::lerp(geom::lerp(h00, h01, mlat), geom::lerp(h10, h11, mlat), mlon); + return math::lerp(math::lerp(h00, h01, mlat), math::lerp(h10, h11, mlat), mlon); } static constexpr int node_size_log2 = 8; @@ -256,8 +256,8 @@ void node_cache::store(std::size_t uid, std::vector const & data) struct node { - geom::vector v[3]; - geom::interval height_range; + math::vector v[3]; + math::interval height_range; virtual bool draw(int level) = 0; virtual node * child(int id) = 0; @@ -312,7 +312,7 @@ private: }; node_controller::node_controller() - : icosahedron_{geom::point::zero(), 1.f} + : icosahedron_{math::point::zero(), 1.f} { std::vector indices; index_counts_[0] = 0; @@ -356,9 +356,9 @@ node * node_controller::root(int f) n->uid = (1 << 5) | f; auto face = cg::faces(icosahedron_)[f]; - n->v[0] = icosahedron_.vertices[face[0]] - geom::point::zero(); - n->v[1] = icosahedron_.vertices[face[1]] - geom::point::zero(); - n->v[2] = icosahedron_.vertices[face[2]] - geom::point::zero(); + n->v[0] = icosahedron_.vertices[face[0]] - math::point::zero(); + n->v[1] = icosahedron_.vertices[face[1]] - math::point::zero(); + n->v[2] = icosahedron_.vertices[face[2]] - math::point::zero(); roots_[f] = std::move(n); } @@ -450,7 +450,7 @@ node * node_controller::node_impl::child(int id) float t1 = (1.f * j) / (1.f * node_child_size); float t2 = 1.f - t0 - t1; - return geom::normalized(v[0] * t0 + v[1] * t1 + v[2] * t2); + return math::normalized(v[0] * t0 + v[1] * t1 + v[2] * t2); }; n->v[0] = at(i0, j0); @@ -487,7 +487,7 @@ void node_controller::node_impl::load_heights() float t1 = (1.f * j) / (1.f * node_size); float t2 = 1.f - t0 - t1; - return geom::normalized(v[0] * t0 + v[1] * t1 + v[2] * t2); + return math::normalized(v[0] * t0 + v[1] * t1 + v[2] * t2); }; for (int i = 0; i <= node_size; ++i) @@ -612,8 +612,8 @@ struct srtm_app void update() override; void present() override; - geom::free_camera camera; - geom::matrix camera_transform; + math::free_camera camera; + math::matrix camera_transform; bool camera_forward = false; node_controller nodes; @@ -637,15 +637,15 @@ srtm_app::srtm_app(options const &, context const & context) context.vsync(true); context.show_cursor(false); - camera.fov_y = geom::rad(45.f); + camera.fov_y = math::rad(45.f); camera.near_clip = 0.0001f; camera.far_clip = 10.f; camera.pos = {0.f, -10.f, 0.f}; - camera.rotateYZ(geom::rad(-90.f)); + camera.rotateYZ(math::rad(-90.f)); camera_transform = camera.transform(); - selected_mesh.setup>(); + selected_mesh.setup>(); { util::array colors({16}); @@ -721,7 +721,7 @@ void srtm_app::update() camera.rotateXY(4.f * dt); } - float const camera_speed = std::min(5.f, geom::distance(camera.pos, geom::point::zero()) - 1.f); + float const camera_speed = std::min(5.f, math::distance(camera.pos, math::point::zero()) - 1.f); auto const camera_forward = camera.direction(); auto const camera_up = camera.axis_y(); @@ -783,11 +783,11 @@ std::ostream & operator << (std::ostream & os, std::vector const & v) void srtm_app::present() { - cg::icosahedron icosahedron{geom::point::zero(), 1.f}; + cg::icosahedron icosahedron{math::point::zero(), 1.f}; auto const & icosa_vertices = cg::vertices(icosahedron); auto const & icosa_faces = cg::faces(icosahedron); - auto const icosa_side = geom::distance(icosa_vertices[icosa_faces[0][0]], icosa_vertices[icosa_faces[0][1]]); + auto const icosa_side = math::distance(icosa_vertices[icosa_faces[0][0]], icosa_vertices[icosa_faces[0][1]]); std::vector info; @@ -806,7 +806,7 @@ void srtm_app::present() gl::PrimitiveRestartIndex(0xffffu); { - auto d = geom::distance(camera.pos, geom::point::zero()); + auto d = math::distance(camera.pos, math::point::zero()); camera.far_clip = std::sqrt(d * d + 1.f); camera.near_clip = (d > 2.f) ? d - 2.f : 0.0001f; } @@ -815,12 +815,12 @@ void srtm_app::present() auto const camera_pos = camera.position(); auto const camera_direction = camera.direction(); - info.push_back(util::to_string("Camera height: ", (geom::distance(camera_pos, geom::point::zero()) - 1.f) * 6400000.f, " m")); + info.push_back(util::to_string("Camera height: ", (math::distance(camera_pos, math::point::zero()) - 1.f) * 6400000.f, " m")); auto const frustum = cg::frustum(camera_transform); (void)frustum; - auto light = geom::normalized(geom::vector{camera_pos[0]-camera_pos[1], camera_pos[1]+camera_pos[0], 0.f}); + auto light = math::normalized(math::vector{camera_pos[0]-camera_pos[1], camera_pos[1]+camera_pos[0], 0.f}); tile_close_program.bind(); tile_close_program["u_transform"] = camera_transform; @@ -846,20 +846,20 @@ void srtm_app::present() info.push_back(util::to_string("Camera pos: ", camera_pos)); - std::vector> selected_vertices; + std::vector> selected_vertices; std::size_t rendered_tiles = 0; std::vector id; auto visit = util::recursive([&](auto & self, node * n, int level = 0) -> bool { auto const & v = n->v; - auto const o = geom::point::zero(); + auto const o = math::point::zero(); { bool culled = true; for (std::size_t i = 0; i < 3; ++i) { - if (geom::dot(v[i], geom::normalized(camera_pos - o)) >= 0.f) + if (math::dot(v[i], math::normalized(camera_pos - o)) >= 0.f) { culled = false; break; @@ -873,34 +873,34 @@ void srtm_app::present() bool const flat = n->height_range.max == n->height_range.min; auto const m3 = (v[0] + v[1] + v[2]) / 3.f; - auto const m = geom::normalized(m3); + auto const m = math::normalized(m3); auto const m0 = m * (n->height_range.empty() ? 0.f : n->height_range.min) / 6400000.f; auto const m1 = m * (n->height_range.empty() ? 1.f : flat ? n->height_range.min + 1.f : n->height_range.max) / 6400000.f + (m - m3); - geom::triangle> t{o + v[0] + m0, o + v[1] + m0, o + v[2] + m0}; + math::triangle> t{o + v[0] + m0, o + v[1] + m0, o + v[2] + m0}; cg::triangular_prism body{t, m1 - m0}; // if (cg::separation(body, frustum).second > 0.f) // return true; - bool const selected = geom::intersect(geom::ray{camera_pos, camera_direction}, t); + bool const selected = math::intersect(math::ray{camera_pos, camera_direction}, t); float on_screen_unit; { auto edge = [](auto const & v0, auto const & v1, auto const & u) -> std::optional { - auto const n = geom::normalized(geom::cross(v0, v1)); - auto v = geom::normalized(u - n * dot(u, n)); - if (geom::dot(geom::cross(v0, v), geom::cross(v, v1)) >= 0.f) - return geom::length(v - u); + auto const n = math::normalized(math::cross(v0, v1)); + auto v = math::normalized(u - n * dot(u, n)); + if (math::dot(math::cross(v0, v), math::cross(v, v1)) >= 0.f) + return math::length(v - u); return std::nullopt; }; float distance = std::numeric_limits::infinity(); auto c = camera_pos - o; - if (geom::det(v[0], v[1], c) >= 0.f && geom::det(v[1], v[2], c) >= 0.f && geom::det(v[2], v[0], c) >= 0.f) - distance = std::min(distance, geom::length(c) - 1.f); + if (math::det(v[0], v[1], c) >= 0.f && math::det(v[1], v[2], c) >= 0.f && math::det(v[2], v[0], c) >= 0.f) + distance = std::min(distance, math::length(c) - 1.f); else { if (auto d = edge(v[0], v[1], c); d) @@ -910,9 +910,9 @@ void srtm_app::present() if (auto d = edge(v[2], v[0], c); d) distance = std::min(distance, *d); } - distance = std::min(distance, geom::length(c - v[0])); - distance = std::min(distance, geom::length(c - v[1])); - distance = std::min(distance, geom::length(c - v[2])); + distance = std::min(distance, math::length(c - v[0])); + distance = std::min(distance, math::length(c - v[1])); + distance = std::min(distance, math::length(c - v[2])); on_screen_unit = state().size[0] / distance / std::tan(camera.fov_x / 2.f); } @@ -948,7 +948,7 @@ void srtm_app::present() if (!should_draw_children || !all_children_drawn) { - tile_n = geom::clamp(tile_n, {0, node_size_log2}); + tile_n = math::clamp(tile_n, {0, node_size_log2}); ++rendered_tiles; @@ -1026,7 +1026,7 @@ void srtm_app::present() gl::Enable(gl::BLEND); gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA); gl::Disable(gl::DEPTH_TEST); - painter.render(geom::window_camera{width, height}.transform()); + painter.render(math::window_camera{width, height}.transform()); } namespace psemek::app diff --git a/examples/triangulation.cpp b/examples/triangulation.cpp index ad775b0c..195cb69c 100644 --- a/examples/triangulation.cpp +++ b/examples/triangulation.cpp @@ -2,9 +2,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -13,8 +13,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -31,7 +31,7 @@ struct triangulation_app while (true) { - geom::point p; + math::point p; in >> p[0] >> p[1]; if (!in) break; @@ -63,8 +63,8 @@ struct triangulation_app { prof::profiler prof("triangulate"); -// auto dcel = cg::ear_clipping(geom::fast, points_.begin(), points_.end()); - auto dcel = cg::monotone_triangulation(geom::fast, points_.begin(), points_.end()); +// auto dcel = cg::ear_clipping(math::fast, points_.begin(), points_.end()); + auto dcel = cg::monotone_triangulation(math::fast, points_.begin(), points_.end()); edges_ = cg::edge_mesh(dcel); triangles_ = cg::triangle_mesh(dcel); } @@ -103,7 +103,7 @@ struct triangulation_app { auto delta = state().mouse - *drag_start_; delta[1] *= -1; - camera_center_ -= geom::cast(delta) * camera_size_ / (1.f * state().size[1]); + camera_center_ -= math::cast(delta) * camera_size_ / (1.f * state().size[1]); drag_start_ = state().mouse; } @@ -116,7 +116,7 @@ struct triangulation_app gl::Clear(gl::COLOR_BUFFER_BIT); float aspect_ratio = (state().size[0] * 1.f) / state().size[1]; - geom::box view_bbox = geom::expand(geom::box::singleton(camera_center_), geom::vector{camera_size_ * 0.5f * aspect_ratio, camera_size_ * 0.5f}); + math::box view_bbox = math::expand(math::box::singleton(camera_center_), math::vector{camera_size_ * 0.5f * aspect_ratio, camera_size_ * 0.5f}); float line_width = 4.f * camera_size_ / state().size[1]; @@ -144,17 +144,17 @@ struct triangulation_app for (std::size_t i = 0; i < points_.size(); ++i) edge(i, (i + 1) % points_.size(), gfx::black); - auto camera_transform = geom::orthographic_camera{view_bbox}.transform(); + auto camera_transform = math::orthographic_camera{view_bbox}.transform(); painter_.render(camera_transform); { - geom::point m_world; - m_world[0] = geom::lerp(view_bbox[0], state().mouse[0] * 1.f / state().size[0]); - m_world[1] = geom::lerp(view_bbox[1], 1.f - state().mouse[1] * 1.f / state().size[1]); + math::point m_world; + m_world[0] = math::lerp(view_bbox[0], state().mouse[0] * 1.f / state().size[0]); + m_world[1] = math::lerp(view_bbox[1], 1.f - state().mouse[1] * 1.f / state().size[1]); auto compare = [&](auto i, auto j){ - return geom::distance(points_[i], m_world) < geom::distance(points_[j], m_world); + return math::distance(points_[i], m_world) < math::distance(points_[j], m_world); }; std::size_t const n_closest = 8; @@ -167,7 +167,7 @@ struct triangulation_app { auto i = closest_points_[j]; - if (geom::distance(points_[i], m_world) > max_distance) + if (math::distance(points_[i], m_world) > max_distance) break; gfx::painter::text_options opts; @@ -175,26 +175,26 @@ struct triangulation_app opts.x = gfx::painter::x_align::left; opts.y = gfx::painter::y_align::bottom; opts.scale = 2.f; - auto p = geom::swizzle<0, 1>(geom::as_point(camera_transform * geom::homogeneous(geom::swizzle<0, 1, -1>(points_[i])))); + auto p = math::swizzle<0, 1>(math::as_point(camera_transform * math::homogeneous(math::swizzle<0, 1, -1>(points_[i])))); p[0] = std::round((p[0] * 0.5f + 0.5f) * state().size[0]); p[1] = std::round((0.5f - p[1] * 0.5f) * state().size[1]); painter_.text(p, util::to_string(i), opts); } } - painter_.render(geom::window_camera{state().size[0], state().size[1]}.transform()); + painter_.render(math::window_camera{state().size[0], state().size[1]}.transform()); } private: - std::vector> points_; - std::vector> edges_; - std::vector> triangles_; - geom::box bbox_; - geom::point camera_center_; + std::vector> points_; + std::vector> edges_; + std::vector> triangles_; + math::box bbox_; + math::point camera_center_; float camera_size_; float camera_size_tgt_; - std::optional> drag_start_; + std::optional> drag_start_; std::vector closest_points_; gfx::painter painter_; diff --git a/examples/water_1d.cpp b/examples/water_1d.cpp index 7aec8a2e..5798b4ac 100644 --- a/examples/water_1d.cpp +++ b/examples/water_1d.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include using namespace psemek; @@ -16,7 +16,7 @@ struct water_1d_app void on_event(app::resize_event const & event) override; - geom::box simulation_area; + math::box simulation_area; float aspect_ratio; std::size_t const N = 100; @@ -42,7 +42,7 @@ water_1d_app::water_1d_app(options const &, context const &) { bed[i] += (0.5f * (N - i)) / N; bed[i] += (100.f / (N - i)) / N; - bed[i] += 0.125f * std::exp(- 1000.f * geom::sqr(i * 1.f / N - 0.5f)); + bed[i] += 0.125f * std::exp(- 1000.f * math::sqr(i * 1.f / N - 0.5f)); // bed[i] += (0.25f * i) / N; // height[i] += (0.3f * i) / N; @@ -138,7 +138,7 @@ void water_1d_app::present() gl::ClearColor(0.8f, 0.8f, 0.8f, 0.8f); gl::Clear(gl::COLOR_BUFFER_BIT); - geom::box view_area; + math::box view_area; { view_area[0] = simulation_area[0]; view_area[1] = simulation_area[1]; @@ -152,7 +152,7 @@ void water_1d_app::present() view_area[0].max += extra_x / 2.f; } - geom::matrix const transform = geom::orthographic{view_area}.homogeneous_matrix(); + math::matrix const transform = math::orthographic{view_area}.homogeneous_matrix(); painter.rect(simulation_area, {255, 255, 255, 255}); @@ -161,14 +161,14 @@ void water_1d_app::present() for (std::size_t i = 0; i + 1 < N; ++i) { - geom::point p0{x(i + 0) * 0.5f + x(i + 1) * 0.5f, simulation_area[1].min}; - geom::point p1{x(i + 1) * 0.5f + x(i + 2) * 0.5f, simulation_area[1].min}; + math::point p0{x(i + 0) * 0.5f + x(i + 1) * 0.5f, simulation_area[1].min}; + math::point p1{x(i + 1) * 0.5f + x(i + 2) * 0.5f, simulation_area[1].min}; - geom::point q0{p0[0], simulation_area[1].min + bed[i]}; - geom::point q1{p1[0], simulation_area[1].min + bed[i + 1]}; + math::point q0{p0[0], simulation_area[1].min + bed[i]}; + math::point q1{p1[0], simulation_area[1].min + bed[i + 1]}; - geom::point w0{p0[0], simulation_area[1].min + bed[i] + height[i]}; - geom::point w1{p1[0], simulation_area[1].min + bed[i + 1] + height[i + 1]}; + math::point w0{p0[0], simulation_area[1].min + bed[i] + height[i]}; + math::point w1{p1[0], simulation_area[1].min + bed[i + 1] + height[i + 1]}; painter.triangle(p0, p1, q0, earth_color); painter.triangle(q0, p1, q1, earth_color); @@ -200,7 +200,7 @@ void water_1d_app::on_event(app::resize_event const & event) float water_1d_app::x(std::size_t i) const { - return geom::lerp(simulation_area[0], (1.f * i) / N); + return math::lerp(simulation_area[0], (1.f * i) / N); } namespace psemek::app diff --git a/libs/app/include/psemek/app/event_state.hpp b/libs/app/include/psemek/app/event_state.hpp index f4f22b18..62526d71 100644 --- a/libs/app/include/psemek/app/event_state.hpp +++ b/libs/app/include/psemek/app/event_state.hpp @@ -8,9 +8,9 @@ namespace psemek::app struct event_state { - geom::vector size = {0, 0}; + math::vector size = {0, 0}; bool focus = true; - geom::point mouse = {0, 0}; + math::point mouse = {0, 0}; int wheel = 0; util::hash_set mouse_button_down; util::hash_set key_down; diff --git a/libs/app/include/psemek/app/events.hpp b/libs/app/include/psemek/app/events.hpp index 23fd4eee..e3e21ade 100644 --- a/libs/app/include/psemek/app/events.hpp +++ b/libs/app/include/psemek/app/events.hpp @@ -1,13 +1,13 @@ #pragma once -#include +#include namespace psemek::app { struct resize_event { - geom::vector size; + math::vector size; }; struct focus_event @@ -17,8 +17,8 @@ namespace psemek::app struct mouse_move_event { - geom::point position; - geom::vector relative; + math::point position; + math::vector relative; }; struct mouse_wheel_event @@ -41,17 +41,17 @@ namespace psemek::app struct touch_down_event { - geom::point position; + math::point position; }; struct touch_up_event { - geom::point position; + math::point position; }; struct touch_move_event { - geom::point position; + math::point position; }; enum class keycode diff --git a/libs/audio/CMakeLists.txt b/libs/audio/CMakeLists.txt index de06772a..2f1c2291 100644 --- a/libs/audio/CMakeLists.txt +++ b/libs/audio/CMakeLists.txt @@ -3,4 +3,4 @@ file(GLOB_RECURSE PSEMEK_AUDIO_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "s psemek_add_library(psemek-audio ${PSEMEK_AUDIO_HEADERS} ${PSEMEK_AUDIO_SOURCES}) target_include_directories(psemek-audio PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-audio PUBLIC psemek-random psemek-geom psemek-util psemek-log psemek-prof) +target_link_libraries(psemek-audio PUBLIC psemek-random psemek-math psemek-util psemek-log psemek-prof) diff --git a/libs/audio/include/psemek/audio/detail/oscillator.hpp b/libs/audio/include/psemek/audio/detail/oscillator.hpp index 8fda760a..457cf456 100644 --- a/libs/audio/include/psemek/audio/detail/oscillator.hpp +++ b/libs/audio/include/psemek/audio/detail/oscillator.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include @@ -17,7 +17,7 @@ namespace psemek::audio void frequency(float f) { - m_ = std::exp(std::complex{0.f, 2.f * geom::pi * f * inv_frequency}); + m_ = std::exp(std::complex{0.f, 2.f * math::pi * f * inv_frequency}); } std::complex phase() const diff --git a/libs/audio/source/detail/resampler.cpp b/libs/audio/source/detail/resampler.cpp index 8bb4b45e..ed477e2c 100644 --- a/libs/audio/source/detail/resampler.cpp +++ b/libs/audio/source/detail/resampler.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include @@ -47,16 +47,16 @@ namespace psemek::audio while (position_ < 0) { - result_.push_back(geom::lerp(last_sample_[0], samples[0], position_frac_)); - result_.push_back(geom::lerp(last_sample_[1], samples[1], position_frac_)); + result_.push_back(math::lerp(last_sample_[0], samples[0], position_frac_)); + result_.push_back(math::lerp(last_sample_[1], samples[1], position_frac_)); real_ratio_ += (ratio - real_ratio_) * smoothness_multiplier; advance(1.f / real_ratio_); } while (2 * position_ + 3 < samples.size()) { - result_.push_back(geom::lerp(samples[2 * position_ + 0], samples[2 * position_ + 2], position_frac_)); - result_.push_back(geom::lerp(samples[2 * position_ + 1], samples[2 * position_ + 3], position_frac_)); + result_.push_back(math::lerp(samples[2 * position_ + 0], samples[2 * position_ + 2], position_frac_)); + result_.push_back(math::lerp(samples[2 * position_ + 1], samples[2 * position_ + 3], position_frac_)); real_ratio_ += (ratio - real_ratio_) * smoothness_multiplier; advance(1.f / real_ratio_); } diff --git a/libs/audio/source/effect/compressor.cpp b/libs/audio/source/effect/compressor.cpp index de02c96c..43146b8d 100644 --- a/libs/audio/source/effect/compressor.cpp +++ b/libs/audio/source/effect/compressor.cpp @@ -1,8 +1,8 @@ #include #include -#include -#include -#include +#include +#include +#include namespace psemek::audio { @@ -19,7 +19,7 @@ namespace psemek::audio , strength_(strength) , envelope_attack_multiplier_(smoothness_to_multiplier(envelope_attack)) , envelope_release_multiplier_(smoothness_to_multiplier(envelope_release)) - , knee_range_(geom::expand(geom::interval::singleton(volume_threshold_log_), std::log(knee) * 0.5f)) + , knee_range_(math::expand(math::interval::singleton(volume_threshold_log_), std::log(knee) * 0.5f)) {} std::optional length() const override @@ -43,8 +43,8 @@ namespace psemek::audio float envelope_log = std::log(envelope_); - if (geom::contains(knee_range_, envelope_log)) - strength *= geom::unlerp(knee_range_, envelope_log); + if (math::contains(knee_range_, envelope_log)) + strength *= math::unlerp(knee_range_, envelope_log); float log_gain = strength * std::min(0.f, volume_threshold_log_ - envelope_log); @@ -68,7 +68,7 @@ namespace psemek::audio float strength_; float envelope_attack_multiplier_; float envelope_release_multiplier_; - geom::interval knee_range_; + math::interval knee_range_; float envelope_ = 0.f; }; diff --git a/libs/audio/source/wave/sawtooth.cpp b/libs/audio/source/wave/sawtooth.cpp index 885dde51..c0a10d30 100644 --- a/libs/audio/source/wave/sawtooth.cpp +++ b/libs/audio/source/wave/sawtooth.cpp @@ -12,7 +12,7 @@ namespace psemek::audio { auto func = [o = oscillator{frequency}]() mutable { auto z = o.next(); - return (2.f / geom::pi) * (- std::atan2(z.real() + 1.f, z.imag())); + return (2.f / math::pi) * (- std::atan2(z.real() + 1.f, z.imag())); }; return make_generator(func); diff --git a/libs/audio/source/wave/triangle.cpp b/libs/audio/source/wave/triangle.cpp index 7fd997f0..e0740abf 100644 --- a/libs/audio/source/wave/triangle.cpp +++ b/libs/audio/source/wave/triangle.cpp @@ -11,7 +11,7 @@ namespace psemek::audio { auto func = [o = oscillator{frequency}]() mutable { auto const z = o.next(); - float const t = 0.5f * std::atan2(z.imag(), z.real()) / (geom::pi) + 0.5f; + float const t = 0.5f * std::atan2(z.imag(), z.real()) / (math::pi) + 0.5f; return 4.f * std::abs(t - std::floor(t + 0.5f)) - 1.f; }; diff --git a/libs/cg/CMakeLists.txt b/libs/cg/CMakeLists.txt index 0afd48df..a6ae0f1e 100644 --- a/libs/cg/CMakeLists.txt +++ b/libs/cg/CMakeLists.txt @@ -2,5 +2,5 @@ file(GLOB_RECURSE PSEMEK_CG_HEADERS "include/*.hpp") psemek_add_library(psemek-cg ${PSEMEK_CG_HEADERS}) target_include_directories(psemek-cg PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-cg PUBLIC psemek-geom psemek-util) +target_link_libraries(psemek-cg PUBLIC psemek-math psemek-util) set_target_properties(psemek-cg PROPERTIES LINKER_LANGUAGE CXX) diff --git a/libs/cg/include/psemek/cg/area.hpp b/libs/cg/include/psemek/cg/area.hpp index 1b3221d5..aeaa3fa6 100644 --- a/libs/cg/include/psemek/cg/area.hpp +++ b/libs/cg/include/psemek/cg/area.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include @@ -19,7 +19,7 @@ namespace psemek::cg for (auto it = begin, prev = std::ranges::prev(end); it != end; prev = it++) { - result += geom::volume(origin, *prev, *it); + result += math::volume(origin, *prev, *it); } return result / scalar_type{2}; diff --git a/libs/cg/include/psemek/cg/bbox.hpp b/libs/cg/include/psemek/cg/bbox.hpp index 72e2bb4c..90863435 100644 --- a/libs/cg/include/psemek/cg/bbox.hpp +++ b/libs/cg/include/psemek/cg/bbox.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace psemek::cg @@ -11,7 +11,7 @@ namespace psemek::cg { using point_type = std::decay_t; - geom::box result; + math::box result; for (; begin != end; ++begin) result |= *begin; return result; diff --git a/libs/cg/include/psemek/cg/body/body.hpp b/libs/cg/include/psemek/cg/body/body.hpp index 1438600b..d2d3392a 100644 --- a/libs/cg/include/psemek/cg/body/body.hpp +++ b/libs/cg/include/psemek/cg/body/body.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include @@ -95,12 +95,12 @@ namespace psemek::cg using std::begin; using std::end; - std::vector> result; + std::vector> result; for (auto const & f : fs) { for (auto it = begin(f), jt = std::prev(end(f)); it != end(f); jt = it++) { - geom::segment s{*jt, *it}; + math::segment s{*jt, *it}; if (s[0] > s[1]) std::swap(s[0], s[1]); result.push_back(s); } @@ -142,13 +142,13 @@ namespace psemek::cg { constexpr std::size_t faces_count = detail::static_size_v; constexpr std::size_t face_size = detail::static_size_v; - std::array, faces_count * (face_size - 2)> result; + std::array, faces_count * (face_size - 2)> result; impl(result.begin()); return result; } else { - std::vector> result; + std::vector> result; if constexpr (detail::has_static_size_v) { constexpr std::size_t face_size = detail::static_size_v; @@ -176,7 +176,7 @@ namespace psemek::cg { for (auto const & e : es) { - *out++ = geom::normalized(vs[e[1]] - vs[e[0]]); + *out++ = math::normalized(vs[e[1]] - vs[e[0]]); } }; @@ -209,7 +209,7 @@ namespace psemek::cg { for (auto const & f : fs) { - *out++ = geom::normal(vs[f[0]], vs[f[1]], vs[f[2]]); + *out++ = math::normal(vs[f[0]], vs[f[1]], vs[f[2]]); } }; diff --git a/libs/cg/include/psemek/cg/body/box.hpp b/libs/cg/include/psemek/cg/body/box.hpp index be00dc06..a3ae131c 100644 --- a/libs/cg/include/psemek/cg/body/box.hpp +++ b/libs/cg/include/psemek/cg/body/box.hpp @@ -2,7 +2,7 @@ #include -#include +#include namespace psemek::cg { @@ -11,19 +11,19 @@ namespace psemek::cg struct box; template - box(geom::box) -> box; + box(math::box) -> box; template struct box { box() = default; - box(geom::box const & b); + box(math::box const & b); - std::array, 4> vertices; + std::array, 4> vertices; }; template - box::box(geom::box const & b) + box::box(math::box const & b) { for (std::size_t y = 0; y < 2; ++y) { @@ -46,7 +46,7 @@ namespace psemek::cg template auto const & edges(box const &) { - static const std::array, 12> result = + static const std::array, 12> result = {{ { 0b00, 0b01 }, { 0b01, 0b11 }, @@ -61,13 +61,13 @@ namespace psemek::cg struct box { box() = default; - box(geom::box const & b); + box(math::box const & b); - std::array, 8> vertices; + std::array, 8> vertices; }; template - box::box(geom::box const & b) + box::box(math::box const & b) { for (std::size_t z = 0; z < 2; ++z) { @@ -94,7 +94,7 @@ namespace psemek::cg template auto const & edges(box const &) { - static const std::array, 12> result = + static const std::array, 12> result = {{ { 0b000, 0b001 }, { 0b010, 0b011 }, @@ -134,7 +134,7 @@ namespace psemek::cg template auto const & face_normals(box const &) { - static const std::array, 6> result = + static const std::array, 6> result = {{ {-1, 0, 0}, { 1, 0, 0}, @@ -150,7 +150,7 @@ namespace psemek::cg template auto const & edge_directions(box const &) { - static const std::array, 3> result = + static const std::array, 3> result = {{ { 1, 0, 0}, { 0, 1, 0}, diff --git a/libs/cg/include/psemek/cg/body/frustum.hpp b/libs/cg/include/psemek/cg/body/frustum.hpp index 657e17d4..d0cca21a 100644 --- a/libs/cg/include/psemek/cg/body/frustum.hpp +++ b/libs/cg/include/psemek/cg/body/frustum.hpp @@ -3,9 +3,9 @@ #include #include -#include -#include -#include +#include +#include +#include namespace psemek::cg { @@ -14,21 +14,21 @@ namespace psemek::cg struct frustum; template - frustum(geom::matrix) -> frustum; + frustum(math::matrix) -> frustum; template struct frustum { frustum() = default; - frustum(geom::matrix const & m); + frustum(math::matrix const & m); - std::array, 8> vertices; + std::array, 8> vertices; }; template - frustum::frustum(geom::matrix const & m) + frustum::frustum(math::matrix const & m) { - bool flip = (geom::det(m) < 0); + bool flip = (math::det(m) < 0); for (std::size_t z = 0; z < 2; ++z) { @@ -38,15 +38,15 @@ namespace psemek::cg { std::size_t i = z * 4 + y * 2 + (flip ? 1 - x : x); - geom::vector p; + math::vector p; p[0] = (x == 0) ? -1 : 1; p[1] = (y == 0) ? -1 : 1; p[2] = (z == 0) ? -1 : 1; p[3] = 1; - geom::gauss(m, p); + math::gauss(m, p); - vertices[i] = geom::as_point(p); + vertices[i] = math::as_point(p); } } } diff --git a/libs/cg/include/psemek/cg/body/icosahedron.hpp b/libs/cg/include/psemek/cg/body/icosahedron.hpp index a7a17656..7f6de66a 100644 --- a/libs/cg/include/psemek/cg/body/icosahedron.hpp +++ b/libs/cg/include/psemek/cg/body/icosahedron.hpp @@ -11,33 +11,33 @@ namespace psemek::cg struct icosahedron { icosahedron() = default; - icosahedron(geom::point const & origin, T radius); + icosahedron(math::point const & origin, T radius); - std::array, 12> vertices; + std::array, 12> vertices; }; template - icosahedron::icosahedron(geom::point const & origin, T radius) + icosahedron::icosahedron(math::point const & origin, T radius) { static const T icosa_a = radius * 0.850650808; // radius * phi / sqrt(1 + phi^2) static const T icosa_b = radius * 0.525731112; // radius / sqrt(1 + phi^2) vertices = {{ - origin + geom::vector{-icosa_a, -icosa_b, 0}, // 0 - origin + geom::vector{-icosa_a, icosa_b, 0}, // 1 - origin + geom::vector{ icosa_a, -icosa_b, 0}, // 2 - origin + geom::vector{ icosa_a, icosa_b, 0}, // 3 + origin + math::vector{-icosa_a, -icosa_b, 0}, // 0 + origin + math::vector{-icosa_a, icosa_b, 0}, // 1 + origin + math::vector{ icosa_a, -icosa_b, 0}, // 2 + origin + math::vector{ icosa_a, icosa_b, 0}, // 3 - origin + geom::vector{0, -icosa_a, -icosa_b}, // 4 - origin + geom::vector{0, -icosa_a, icosa_b}, // 5 - origin + geom::vector{0, icosa_a, -icosa_b}, // 6 - origin + geom::vector{0, icosa_a, icosa_b}, // 7 + origin + math::vector{0, -icosa_a, -icosa_b}, // 4 + origin + math::vector{0, -icosa_a, icosa_b}, // 5 + origin + math::vector{0, icosa_a, -icosa_b}, // 6 + origin + math::vector{0, icosa_a, icosa_b}, // 7 - origin + geom::vector{-icosa_b, 0, -icosa_a}, // 8 - origin + geom::vector{ icosa_b, 0, -icosa_a}, // 9 - origin + geom::vector{-icosa_b, 0, icosa_a}, // 10 - origin + geom::vector{ icosa_b, 0, icosa_a}, // 11 + origin + math::vector{-icosa_b, 0, -icosa_a}, // 8 + origin + math::vector{ icosa_b, 0, -icosa_a}, // 9 + origin + math::vector{-icosa_b, 0, icosa_a}, // 10 + origin + math::vector{ icosa_b, 0, icosa_a}, // 11 }}; } @@ -50,7 +50,7 @@ namespace psemek::cg template auto const & triangles(icosahedron const &) { - static const std::array, 20> result = + static const std::array, 20> result = {{ {0, 1, 8,}, {0, 10, 1,}, diff --git a/libs/cg/include/psemek/cg/body/polygon.hpp b/libs/cg/include/psemek/cg/body/polygon.hpp index db8676cb..9e5cd0ff 100644 --- a/libs/cg/include/psemek/cg/body/polygon.hpp +++ b/libs/cg/include/psemek/cg/body/polygon.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -11,14 +11,14 @@ namespace psemek::cg template struct polygon { - polygon(std::vector> vertices); + polygon(std::vector> vertices); - std::vector> vertices; - std::vector> edges; + std::vector> vertices; + std::vector> edges; }; template - polygon::polygon(std::vector> vertices) + polygon::polygon(std::vector> vertices) : vertices(std::move(vertices)) { edges.resize(vertices.size()); diff --git a/libs/cg/include/psemek/cg/body/prism.hpp b/libs/cg/include/psemek/cg/body/prism.hpp index 4766cdf4..0aaa54d6 100644 --- a/libs/cg/include/psemek/cg/body/prism.hpp +++ b/libs/cg/include/psemek/cg/body/prism.hpp @@ -11,14 +11,14 @@ namespace psemek::cg struct triangular_prism { triangular_prism() = default; - triangular_prism(geom::triangle> const & t, geom::vector const & d); + triangular_prism(math::triangle> const & t, math::vector const & d); - std::array, 6> vertices; - std::array, 4> edge_directions; + std::array, 6> vertices; + std::array, 4> edge_directions; }; template - triangular_prism::triangular_prism(geom::triangle> const & t, geom::vector const & d) + triangular_prism::triangular_prism(math::triangle> const & t, math::vector const & d) { for (std::size_t i = 0; i < 3; ++i) { @@ -26,7 +26,7 @@ namespace psemek::cg vertices[i + 3] = t[i] + d; } - if (geom::dot(geom::normal(vertices[0], vertices[1], vertices[2]), d) < 0) + if (math::dot(math::normal(vertices[0], vertices[1], vertices[2]), d) < 0) { std::swap(vertices[1], vertices[2]); std::swap(vertices[4], vertices[5]); @@ -34,9 +34,9 @@ namespace psemek::cg for (std::size_t i = 0; i < 3; ++i) { - edge_directions[i] = geom::normalized(vertices[(i + 1) % 3] - vertices[i]); + edge_directions[i] = math::normalized(vertices[(i + 1) % 3] - vertices[i]); } - edge_directions[3] = geom::normalized(d); + edge_directions[3] = math::normalized(d); } template @@ -48,7 +48,7 @@ namespace psemek::cg template auto const & edges(triangular_prism const &) { - static const std::array, 9> result = + static const std::array, 9> result = {{ { 0, 1 }, { 1, 2 }, @@ -90,14 +90,14 @@ namespace psemek::cg struct irregular_triangular_prism { irregular_triangular_prism() = default; - irregular_triangular_prism(geom::triangle> const & low_triangle, geom::triangle> const & high_triangle); + irregular_triangular_prism(math::triangle> const & low_triangle, math::triangle> const & high_triangle); - std::array, 6> vertices; - std::array, 9> edge_directions; + std::array, 6> vertices; + std::array, 9> edge_directions; }; template - irregular_triangular_prism::irregular_triangular_prism(geom::triangle> const & low_triangle, geom::triangle> const & high_triangle) + irregular_triangular_prism::irregular_triangular_prism(math::triangle> const & low_triangle, math::triangle> const & high_triangle) { for (std::size_t i = 0; i < 3; ++i) { @@ -107,9 +107,9 @@ namespace psemek::cg for (std::size_t i = 0; i < 3; ++i) { - edge_directions[i + 0] = geom::normalized(vertices[(i + 1) % 3] - vertices[i]); - edge_directions[i + 3] = geom::normalized(vertices[3 + ((i + 1) % 3)] - vertices[3 + i]); - edge_directions[i + 6] = geom::normalized(vertices[i + 3] - vertices[i]); + edge_directions[i + 0] = math::normalized(vertices[(i + 1) % 3] - vertices[i]); + edge_directions[i + 3] = math::normalized(vertices[3 + ((i + 1) % 3)] - vertices[3 + i]); + edge_directions[i + 6] = math::normalized(vertices[i + 3] - vertices[i]); } } diff --git a/libs/cg/include/psemek/cg/body/triangle_mesh.hpp b/libs/cg/include/psemek/cg/body/triangle_mesh.hpp index c8630f36..71f3ef63 100644 --- a/libs/cg/include/psemek/cg/body/triangle_mesh.hpp +++ b/libs/cg/include/psemek/cg/body/triangle_mesh.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -12,16 +12,16 @@ namespace psemek::cg struct triangle_mesh { triangle_mesh() = default; - triangle_mesh(std::vector> vertices, std::vector> triangles); + triangle_mesh(std::vector> vertices, std::vector> triangles); - std::vector> vertices; - std::vector> triangles; - std::vector> edges; - std::vector> edge_directions; + std::vector> vertices; + std::vector> triangles; + std::vector> edges; + std::vector> edge_directions; }; template - triangle_mesh::triangle_mesh(std::vector> vertices, std::vector> triangles) + triangle_mesh::triangle_mesh(std::vector> vertices, std::vector> triangles) : vertices(std::move(vertices)) , triangles(std::move(triangles)) { @@ -34,7 +34,7 @@ namespace psemek::cg } for (auto const & e : edges) - edge_directions.push_back(geom::normalized(this->vertices[e[1]] - this->vertices[e[0]])); + edge_directions.push_back(math::normalized(this->vertices[e[1]] - this->vertices[e[0]])); } template diff --git a/libs/cg/include/psemek/cg/clip.hpp b/libs/cg/include/psemek/cg/clip.hpp index 84025b5d..a7b07e8e 100644 --- a/libs/cg/include/psemek/cg/clip.hpp +++ b/libs/cg/include/psemek/cg/clip.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include namespace psemek::cg @@ -9,7 +9,7 @@ namespace psemek::cg // TODO: this was written for Voronoi and is known to fail in some cases template - void clip(dcel, Edge, Face, Index> & dcel, geom::vector const & eq) + void clip(dcel, Edge, Face, Index> & dcel, math::vector const & eq) { auto outer_face = dcel.face(0); diff --git a/libs/cg/include/psemek/cg/convex/distance.hpp b/libs/cg/include/psemek/cg/convex/distance.hpp index 2f9b0ddb..3f901445 100644 --- a/libs/cg/include/psemek/cg/convex/distance.hpp +++ b/libs/cg/include/psemek/cg/convex/distance.hpp @@ -1,8 +1,8 @@ #pragma once #include -#include -#include +#include +#include namespace psemek::cg { @@ -11,32 +11,32 @@ namespace psemek::cg { template - T distance_fast_2d(Body const & b, geom::point const & p) + T distance_fast_2d(Body const & b, math::point const & p) { - T result = geom::limits::min(); + T result = math::limits::min(); auto const & vs = vertices(b); for (auto const & e : edges(b)) { - auto n = geom::ort(geom::normalized(vs[e[0]] - vs[e[1]])); - geom::make_max(result, geom::dot(n, p - vs[e[0]])); + auto n = math::ort(math::normalized(vs[e[0]] - vs[e[1]])); + math::make_max(result, math::dot(n, p - vs[e[0]])); } return result; } template - T distance_fast_3d(Body const & b, geom::point const & p) + T distance_fast_3d(Body const & b, math::point const & p) { - T result = geom::limits::min(); + T result = math::limits::min(); auto const & vs = vertices(b); for (auto const & f : faces(b)) { - auto n = geom::normal(vs[f[0]], vs[f[1]], vs[f[2]]); - geom::make_max(result, geom::dot(n, p - vs[f[0]])); + auto n = math::normal(vs[f[0]], vs[f[1]], vs[f[2]]); + math::make_max(result, math::dot(n, p - vs[f[0]])); } return result; diff --git a/libs/cg/include/psemek/cg/convex/inside.hpp b/libs/cg/include/psemek/cg/convex/inside.hpp index 633ae91f..dd83cc67 100644 --- a/libs/cg/include/psemek/cg/convex/inside.hpp +++ b/libs/cg/include/psemek/cg/convex/inside.hpp @@ -2,29 +2,29 @@ #include -#include +#include namespace psemek::cg { template - bool inside(RobustTag robust_tag, geom::point const & p, Body const & body) + bool inside(RobustTag robust_tag, math::point const & p, Body const & body) { auto const & vs = vertices(body); auto const & fs = faces(body); for (auto const & f : fs) { - if (geom::orientation(robust_tag, vs[f[0]], vs[f[1]], vs[f[2]], p) == geom::sign_t::negative) + if (math::orientation(robust_tag, vs[f[0]], vs[f[1]], vs[f[2]], p) == math::sign_t::negative) return false; } return true; } template - bool inside(geom::point const & p, Body const & body) + bool inside(math::point const & p, Body const & body) { - return inside(geom::default_robust_tag, p, body); + return inside(math::default_robust_tag, p, body); } } diff --git a/libs/cg/include/psemek/cg/convex/separation.hpp b/libs/cg/include/psemek/cg/convex/separation.hpp index dbc1b2aa..b74eb318 100644 --- a/libs/cg/include/psemek/cg/convex/separation.hpp +++ b/libs/cg/include/psemek/cg/convex/separation.hpp @@ -2,8 +2,8 @@ #include -#include -#include +#include +#include namespace psemek::cg { @@ -12,8 +12,8 @@ namespace psemek::cg struct separation_info { T distance = -std::numeric_limits::infinity(); - geom::vector direction = geom::vector::zero(); - geom::point contact_point = geom::point::zero(); + math::vector direction = math::vector::zero(); + math::point contact_point = math::point::zero(); int contact_point_body = 0; separation_info & operator |= (separation_info const & i); @@ -57,13 +57,13 @@ namespace psemek::cg for (auto const & e : es1) { result_type edge_result; - edge_result.direction = -geom::ort(geom::normalized(vs1[e[1]] - vs1[e[0]])); + edge_result.direction = -math::ort(math::normalized(vs1[e[1]] - vs1[e[0]])); edge_result.distance = std::numeric_limits::infinity(); for (auto const & v : vs2) { - auto const d = geom::dot(geom::homogeneous(edge_result.direction), geom::homogeneous(v - vs1[e[0]])); - if (geom::make_min(edge_result.distance, d)) + auto const d = math::dot(math::homogeneous(edge_result.direction), math::homogeneous(v - vs1[e[0]])); + if (math::make_min(edge_result.distance, d)) edge_result.contact_point = v; } @@ -113,15 +113,15 @@ namespace psemek::cg for (auto const & f : fs1) { result_type face_result; - face_result.direction = geom::normal(vs1[f[0]], vs1[f[1]], vs1[f[2]]); + face_result.direction = math::normal(vs1[f[0]], vs1[f[1]], vs1[f[2]]); face_result.distance = std::numeric_limits::infinity(); auto const face_p = vs1[f[0]]; for (auto const & v : vs2) { - auto const d = geom::dot(face_result.direction, v - face_p); - if (geom::make_min(face_result.distance, d)) + auto const d = math::dot(face_result.direction, v - face_p); + if (math::make_min(face_result.distance, d)) face_result.contact_point = v; } @@ -145,21 +145,21 @@ namespace psemek::cg for (auto const & ed2 : eds2) { result_type edge_result; - edge_result.direction = geom::cross(ed1, ed2); - auto l = geom::length(edge_result.direction); + edge_result.direction = math::cross(ed1, ed2); + auto l = math::length(edge_result.direction); if (l == 0) continue; edge_result.direction /= l; - geom::interval i1, i2; + math::interval i1, i2; for (auto const & v : vs1) { - i1 |= geom::dot(geom::homogeneous(v), geom::homogeneous(edge_result.direction)); + i1 |= math::dot(math::homogeneous(v), math::homogeneous(edge_result.direction)); } for (auto const & v : vs2) { - i2 |= geom::dot(geom::homogeneous(v), geom::homogeneous(edge_result.direction)); + i2 |= math::dot(math::homogeneous(v), math::homogeneous(edge_result.direction)); } auto edge_d12 = i2.min - i1.max; @@ -194,7 +194,7 @@ namespace psemek::cg return result; auto edge_result = process_edges(vs1, eds1, vs2, eds2); - if (geom::make_max(result.distance, edge_result.distance)) + if (math::make_max(result.distance, edge_result.distance)) { result = edge_result; @@ -202,31 +202,31 @@ namespace psemek::cg auto e2 = es2[0]; { - auto max_d = geom::limits::min(); + auto max_d = math::limits::min(); for (auto const & e : es1) { - auto d = geom::dot(geom::homogeneous(vs1[e[0]]), geom::homogeneous(result.direction)) + geom::dot(geom::homogeneous(vs1[e[1]]), geom::homogeneous(result.direction)); - if (geom::make_max(max_d, d)) + auto d = math::dot(math::homogeneous(vs1[e[0]]), math::homogeneous(result.direction)) + math::dot(math::homogeneous(vs1[e[1]]), math::homogeneous(result.direction)); + if (math::make_max(max_d, d)) e1 = e; } } { - auto min_d = geom::limits::max(); + auto min_d = math::limits::max(); for (auto const & e : es2) { - auto d = geom::dot(geom::homogeneous(vs2[e[0]]), geom::homogeneous(result.direction)) + geom::dot(geom::homogeneous(vs2[e[1]]), geom::homogeneous(result.direction)); - if (geom::make_min(min_d, d)) + auto d = math::dot(math::homogeneous(vs2[e[0]]), math::homogeneous(result.direction)) + math::dot(math::homogeneous(vs2[e[1]]), math::homogeneous(result.direction)); + if (math::make_min(min_d, d)) e2 = e; } } result.contact_point_body = 0; - auto m = geom::cross(result.direction, vs2[e2[1]] - vs2[e2[0]]); - auto v = geom::dot(geom::homogeneous(vs2[e2[0]]), geom::homogeneous(m)); - auto t = (v - geom::dot(geom::homogeneous(vs1[e1[0]]), geom::homogeneous(m))) / geom::dot(geom::homogeneous(vs1[e1[1]] - vs1[e1[0]]), geom::homogeneous(m)); - result.contact_point = geom::lerp(vs1[e1[0]], vs1[e1[1]], geom::clamp(t, geom::interval(scalar_type(0), scalar_type(1)))); + auto m = math::cross(result.direction, vs2[e2[1]] - vs2[e2[0]]); + auto v = math::dot(math::homogeneous(vs2[e2[0]]), math::homogeneous(m)); + auto t = (v - math::dot(math::homogeneous(vs1[e1[0]]), math::homogeneous(m))) / math::dot(math::homogeneous(vs1[e1[1]] - vs1[e1[0]]), math::homogeneous(m)); + result.contact_point = math::lerp(vs1[e1[0]], vs1[e1[1]], math::clamp(t, math::interval(scalar_type(0), scalar_type(1)))); } return result; diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp index b1a53dee..8f303e2e 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/andrew.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -25,7 +25,7 @@ namespace psemek::cg // find lower half of the hull for (auto it = its.begin() + 1; it != its.end(); ++it) { - while (hull.size() >= 2 && orientation(robust_tag, **(hull.end() - 2), **(hull.end() - 1), **it) != geom::sign_t::positive) + while (hull.size() >= 2 && orientation(robust_tag, **(hull.end() - 2), **(hull.end() - 1), **it) != math::sign_t::positive) hull.pop_back(); hull.push_back(*it); @@ -37,7 +37,7 @@ namespace psemek::cg if (*it == *(hull.end() - 2)) continue; - while (hull.size() >= 2 && orientation(robust_tag, **(hull.end() - 2), **(hull.end() - 1), **it) != geom::sign_t::positive) + while (hull.size() >= 2 && orientation(robust_tag, **(hull.end() - 2), **(hull.end() - 1), **it) != math::sign_t::positive) hull.pop_back(); hull.push_back(*it); @@ -53,7 +53,7 @@ namespace psemek::cg template OutIterator andrew_convex_hull(InIterator begin, InIterator end, OutIterator out) { - return andrew_convex_hull(geom::default_robust_tag, begin, end, out); + return andrew_convex_hull(math::default_robust_tag, begin, end, out); } } diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp index db2d38b0..b3666c82 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/graham.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -27,9 +27,9 @@ namespace psemek::cg auto o = orientation(robust_tag, *its.front(), *i1, *i2); // carefully deal with parallel points - if (o == geom::sign_t::positive) + if (o == math::sign_t::positive) return true; - else if (o == geom::sign_t::negative) + else if (o == math::sign_t::negative) return false; return *i1 < *i2; @@ -41,7 +41,7 @@ namespace psemek::cg for (auto it = its.begin() + 1; it != its.end(); ++it) { - while (hull_end - its.begin() >= 2 && orientation(robust_tag, **(hull_end - 2), **(hull_end - 1), **it) != geom::sign_t::positive) + while (hull_end - its.begin() >= 2 && orientation(robust_tag, **(hull_end - 2), **(hull_end - 1), **it) != math::sign_t::positive) --hull_end; *hull_end++ = *it; @@ -54,7 +54,7 @@ namespace psemek::cg template OutIterator graham_convex_hull(InIterator begin, InIterator end, OutIterator out) { - return graham_convex_hull(geom::default_robust_tag, begin, end, out); + return graham_convex_hull(math::default_robust_tag, begin, end, out); } } diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp index a44888dc..74b8a484 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/jarvis.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace psemek::cg { @@ -26,7 +26,7 @@ namespace psemek::cg if (jt == it) continue; if (jt == last_hull_point) continue; - if (orientation(robust_tag, *last_hull_point, *it, *jt) != geom::sign_t::positive) + if (orientation(robust_tag, *last_hull_point, *it, *jt) != math::sign_t::positive) { is_hull_edge = false; break; @@ -51,7 +51,7 @@ namespace psemek::cg template OutIterator jarvis_convex_hull(InIterator begin, InIterator end, OutIterator out) { - return jarvis_convex_hull(geom::default_robust_tag, begin, end, out); + return jarvis_convex_hull(math::default_robust_tag, begin, end, out); } } diff --git a/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp b/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp index 0c08f9ea..ed606f36 100644 --- a/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_2d/quickhull.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -21,11 +21,11 @@ namespace psemek::cg // find point in [begin, end) furthest from segment (p1,p2) auto mid = *std::max_element(begin, end, [&](auto it1, auto it2){ // TODO: design a robust predicate - return orientation(robust_tag, *it2, (*it2) + ((*p2) - (*p1)), *it1) == geom::sign_t::positive; + return orientation(robust_tag, *it2, (*it2) + ((*p2) - (*p1)), *it1) == math::sign_t::positive; }); - auto end1 = std::partition(begin, end, [&](auto it){ return orientation(robust_tag, *p1, *it, *mid) == geom::sign_t::positive; }); - auto end2 = std::partition(end1, end, [&](auto it){ return orientation(robust_tag, *mid, *it, *p2) == geom::sign_t::positive; }); + auto end1 = std::partition(begin, end, [&](auto it){ return orientation(robust_tag, *p1, *it, *mid) == math::sign_t::positive; }); + auto end2 = std::partition(end1, end, [&](auto it){ return orientation(robust_tag, *mid, *it, *p2) == math::sign_t::positive; }); out = quick_hull_recursive_helper(robust_tag, p1, mid, begin, end1, out); out = quick_hull_recursive_helper(robust_tag, mid, p2, end1, end2, out); @@ -52,7 +52,7 @@ namespace psemek::cg { auto p = std::find_if(its.begin() + 1, its.end(), [&](auto it){ return std::all_of(its.begin() + 1, its.end(), [&](auto jt){ - return it == jt || orientation(robust_tag, *its.front(), *it, *jt) == geom::sign_t::negative; + return it == jt || orientation(robust_tag, *its.front(), *it, *jt) == math::sign_t::negative; }); }); std::iter_swap(its.begin() + 1, p); @@ -67,7 +67,7 @@ namespace psemek::cg template OutIterator quick_hull(InIterator begin, InIterator end, OutIterator out) { - return quick_hull(geom::default_robust_tag, begin, end, out); + return quick_hull(math::default_robust_tag, begin, end, out); } } diff --git a/libs/cg/include/psemek/cg/convex_hull_3d/quickhull.hpp b/libs/cg/include/psemek/cg/convex_hull_3d/quickhull.hpp index c85a2742..96ba7565 100644 --- a/libs/cg/include/psemek/cg/convex_hull_3d/quickhull.hpp +++ b/libs/cg/include/psemek/cg/convex_hull_3d/quickhull.hpp @@ -1,10 +1,10 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -16,15 +16,15 @@ namespace psemek::cg { template - std::vector> quickhull(RobustTag robust_tag, std::vector> const & points) + std::vector> quickhull(RobustTag robust_tag, std::vector> const & points) { struct face; using handle = typename std::list::iterator; - using edge = geom::segment; - using triangle = geom::triangle; + using edge = math::segment; + using triangle = math::triangle; - using point = geom::point; + using point = math::point; struct face { @@ -52,10 +52,10 @@ namespace psemek::cg return distance(points[i0], p1) < distance(points[i0], p2); }) - points.begin(); - auto n01 = geom::normalized(points[i1] - points[i0]); + auto n01 = math::normalized(points[i1] - points[i0]); auto dist01 = [&](point const & p){ auto l = p - points[i0]; - return geom::length_sqr(l) - geom::sqr(geom::dot(l, n01)); + return math::length_sqr(l) - math::sqr(math::dot(l, n01)); }; // furthest from i0-i1 line @@ -64,7 +64,7 @@ namespace psemek::cg }) - points.begin(); auto dist012 = [&](point const & p){ - return std::abs(geom::volume(p, points[i0], points[i1], points[i2])); + return std::abs(math::volume(p, points[i0], points[i1], points[i2])); }; // highest 'z' @@ -72,7 +72,7 @@ namespace psemek::cg return dist012(p1) < dist012(p2); }) - points.begin(); - if (geom::orientation(robust_tag, points[i0], points[i1], points[i2], points[i3]) == geom::sign_t::negative) + if (math::orientation(robust_tag, points[i0], points[i1], points[i2], points[i3]) == math::sign_t::negative) std::swap(i2, i3); triangle t0 { i0, i1, i2 }; @@ -89,13 +89,13 @@ namespace psemek::cg if (i == i2) continue; if (i == i3) continue; - if (geom::orientation(robust_tag, points[t0[0]], points[t0[1]], points[t0[2]], points[i]) == geom::sign_t::negative) + if (math::orientation(robust_tag, points[t0[0]], points[t0[1]], points[t0[2]], points[i]) == math::sign_t::negative) out0.push_back(i); - else if (geom::orientation(robust_tag, points[t1[0]], points[t1[1]], points[t1[2]], points[i]) == geom::sign_t::negative) + else if (math::orientation(robust_tag, points[t1[0]], points[t1[1]], points[t1[2]], points[i]) == math::sign_t::negative) out1.push_back(i); - else if (geom::orientation(robust_tag, points[t2[0]], points[t2[1]], points[t2[2]], points[i]) == geom::sign_t::negative) + else if (math::orientation(robust_tag, points[t2[0]], points[t2[1]], points[t2[2]], points[i]) == math::sign_t::negative) out2.push_back(i); - else if (geom::orientation(robust_tag, points[t3[0]], points[t3[1]], points[t3[2]], points[i]) == geom::sign_t::negative) + else if (math::orientation(robust_tag, points[t3[0]], points[t3[1]], points[t3[2]], points[i]) == math::sign_t::negative) out3.push_back(i); } @@ -146,7 +146,7 @@ namespace psemek::cg for (auto i : out_set) { - float const d = geom::volume(points[t[0]], points[t[1]], points[t[2]], points[i]); + float const d = math::volume(points[t[0]], points[t[1]], points[t[2]], points[i]); if (d < dist) { @@ -171,7 +171,7 @@ namespace psemek::cg { handle it = h->neigh[i]; triangle const & t = it->tri; - if (it->valid && (geom::orientation(robust_tag, points[t[0]], points[t[1]], points[t[2]], points[max_i]) == geom::sign_t::negative)) + if (it->valid && (math::orientation(robust_tag, points[t[0]], points[t[1]], points[t[2]], points[max_i]) == math::sign_t::negative)) { it->valid = false; visible_queue.push(it); @@ -222,7 +222,7 @@ namespace psemek::cg triangle const t { h->tri[(i+1)%3], h->tri[i], max_i }; auto it = std::partition(unassigned.begin(), unassigned_end, [&](Index i){ - return geom::orientation(robust_tag, points[t[0]], points[t[1]], points[t[2]], points[i]) == geom::sign_t::positive; + return math::orientation(robust_tag, points[t[0]], points[t[1]], points[t[2]], points[i]) == math::sign_t::positive; }); std::vector out_set(it, unassigned_end); @@ -253,9 +253,9 @@ namespace psemek::cg } template - auto quickhull(std::vector> const & points) + auto quickhull(std::vector> const & points) { - return quickhull(geom::default_robust_tag, points); + return quickhull(math::default_robust_tag, points); } } diff --git a/libs/cg/include/psemek/cg/dcel.hpp b/libs/cg/include/psemek/cg/dcel.hpp index a7ec1b95..1b12680f 100644 --- a/libs/cg/include/psemek/cg/dcel.hpp +++ b/libs/cg/include/psemek/cg/dcel.hpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -661,7 +661,7 @@ namespace psemek::cg auto h = dcel.edge(e); - *out++ = geom::segment{h.origin().index(), h.twin().origin().index()}; + *out++ = math::segment{h.origin().index(), h.twin().origin().index()}; } return out; } @@ -669,7 +669,7 @@ namespace psemek::cg template auto edge_mesh(dcel const & dcel) { - std::vector> result; + std::vector> result; edge_mesh(dcel, std::back_inserter(result)); return result; } @@ -681,7 +681,7 @@ namespace psemek::cg { auto h = dcel.face(f).edge(); - geom::triangle t; + math::triangle t; t[0] = h.origin().index(); h = h.next(); t[1] = h.origin().index(); h = h.next(); t[2] = h.origin().index(); @@ -694,7 +694,7 @@ namespace psemek::cg template auto triangle_mesh(dcel const & dcel) { - std::vector> result; + std::vector> result; triangle_mesh(dcel, std::back_inserter(result)); return result; } diff --git a/libs/cg/include/psemek/cg/ndtree.hpp b/libs/cg/include/psemek/cg/ndtree.hpp index 30dc8f77..5a575e83 100644 --- a/libs/cg/include/psemek/cg/ndtree.hpp +++ b/libs/cg/include/psemek/cg/ndtree.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include @@ -23,8 +23,8 @@ namespace psemek::cg struct ndtree { public: - using point_type = geom::point; - using box_type = geom::box; + using point_type = math::point; + using box_type = math::box; struct value_type { @@ -82,9 +82,9 @@ namespace psemek::cg return {root.get(), true}; } - if (!geom::half_open_contains(root_bbox, point)) + if (!math::half_open_contains(root_bbox, point)) { - while (!geom::half_open_contains(root_bbox, point)) + while (!math::half_open_contains(root_bbox, point)) extend_root(point); } @@ -158,7 +158,7 @@ namespace psemek::cg private: node_ptr root; - geom::box root_bbox; + math::box root_bbox; void extend_root(point_type const & point) { diff --git a/libs/cg/include/psemek/cg/segment_intersection.hpp b/libs/cg/include/psemek/cg/segment_intersection.hpp index 5e3381e5..d8383bae 100644 --- a/libs/cg/include/psemek/cg/segment_intersection.hpp +++ b/libs/cg/include/psemek/cg/segment_intersection.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/libs/cg/include/psemek/cg/triangulation/delaunay.hpp b/libs/cg/include/psemek/cg/triangulation/delaunay.hpp index af204f2d..f875f43d 100644 --- a/libs/cg/include/psemek/cg/triangulation/delaunay.hpp +++ b/libs/cg/include/psemek/cg/triangulation/delaunay.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -62,7 +62,7 @@ namespace psemek::cg auto p3 = e.twin().next().next().origin(); // decide if a flip is needed - if (in_circle(robust_tag, at(p0.index()), at(p1.index()), at(p2.index()), at(p3.index())) != geom::sign_t::positive) continue; + if (in_circle(robust_tag, at(p0.index()), at(p1.index()), at(p2.index()), at(p3.index())) != math::sign_t::positive) continue; flipped_set.insert(std::lower_bound(flipped_set.begin(), flipped_set.end(), e.index()), e.index()); @@ -113,7 +113,7 @@ namespace psemek::cg template auto delaunay(InputIterator begin, InputIterator end) { - return delaunay(geom::default_robust_tag, begin, end); + return delaunay(math::default_robust_tag, begin, end); } } diff --git a/libs/cg/include/psemek/cg/triangulation/ear_clipping.hpp b/libs/cg/include/psemek/cg/triangulation/ear_clipping.hpp index d9b3a408..b8f645da 100644 --- a/libs/cg/include/psemek/cg/triangulation/ear_clipping.hpp +++ b/libs/cg/include/psemek/cg/triangulation/ear_clipping.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include @@ -50,9 +50,9 @@ namespace psemek::cg auto inside = [robust_tag](auto const & t, auto const & p) { return true - && geom::orientation(robust_tag, t[0], t[1], p) == geom::sign_t::positive - && geom::orientation(robust_tag, t[1], t[2], p) == geom::sign_t::positive - && geom::orientation(robust_tag, t[2], t[0], p) == geom::sign_t::positive + && math::orientation(robust_tag, t[0], t[1], p) == math::sign_t::positive + && math::orientation(robust_tag, t[1], t[2], p) == math::sign_t::positive + && math::orientation(robust_tag, t[2], t[0], p) == math::sign_t::positive ; }; @@ -66,9 +66,9 @@ namespace psemek::cg auto next = edge.next(); auto nnext = next.next(); - geom::simplex triangle{at(edge.origin().index()), at(next.origin().index()), at(nnext.origin().index())}; + math::simplex triangle{at(edge.origin().index()), at(next.origin().index()), at(nnext.origin().index())}; - if (geom::orientation(robust_tag, triangle[0], triangle[1], triangle[2]) == geom::sign_t::negative) + if (math::orientation(robust_tag, triangle[0], triangle[1], triangle[2]) == math::sign_t::negative) continue; for (auto k = nnext.next(); k != edge; k = k.next()) @@ -119,7 +119,7 @@ namespace psemek::cg template auto ear_clipping(Iterator begin, Iterator end) { - return ear_clipping(geom::default_robust_tag, begin, end); + return ear_clipping(math::default_robust_tag, begin, end); } } diff --git a/libs/cg/include/psemek/cg/triangulation/monotone.hpp b/libs/cg/include/psemek/cg/triangulation/monotone.hpp index 9d372424..f13cee52 100644 --- a/libs/cg/include/psemek/cg/triangulation/monotone.hpp +++ b/libs/cg/include/psemek/cg/triangulation/monotone.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include @@ -67,23 +67,23 @@ namespace psemek::cg auto const j0 = at(j); auto const j1 = at((j + 1) % count); - auto const oi0 = geom::orientation(RobustTag{}, i0, i1, j0); - auto const oi1 = geom::orientation(RobustTag{}, i0, i1, j1); + auto const oi0 = math::orientation(RobustTag{}, i0, i1, j0); + auto const oi1 = math::orientation(RobustTag{}, i0, i1, j1); if (oi0 == oi1) - return oi0 == geom::sign_t::positive; + return oi0 == math::sign_t::positive; - return geom::orientation(RobustTag{}, j0, j1, i0) == geom::sign_t::negative; + return math::orientation(RobustTag{}, j0, j1, i0) == math::sign_t::negative; } bool operator()(IndexType e, point_handle v) const { - return geom::orientation(RobustTag{}, at(e), at((e + 1) % count), at(v.index())) == geom::sign_t::positive; + return math::orientation(RobustTag{}, at(e), at((e + 1) % count), at(v.index())) == math::sign_t::positive; } bool operator()(point_handle v, IndexType e) const { - return geom::orientation(RobustTag{}, at(e), at((e + 1) % count), at(v.index())) == geom::sign_t::negative; + return math::orientation(RobustTag{}, at(e), at((e + 1) % count), at(v.index())) == math::sign_t::negative; } }; @@ -115,15 +115,15 @@ namespace psemek::cg bool lp = at(p) < at(e); bool ln = at(n) < at(e); - auto s = geom::orientation(robust_tag, at(p), at(e), at(n)); + auto s = math::orientation(robust_tag, at(p), at(e), at(n)); - if (!lp && !ln && s == geom::sign_t::positive) + if (!lp && !ln && s == math::sign_t::positive) return event_type::start; - else if (lp && ln && s == geom::sign_t::positive) + else if (lp && ln && s == math::sign_t::positive) return event_type::end; - else if (!lp && !ln && s == geom::sign_t::negative) + else if (!lp && !ln && s == math::sign_t::negative) return event_type::split; - else if (lp && ln && s == geom::sign_t::negative) + else if (lp && ln && s == math::sign_t::negative) return event_type::merge; else return event_type::regular; @@ -140,10 +140,10 @@ namespace psemek::cg auto f = at(ni.next().origin().index()); auto p = at(ni.prev().origin().index()); bool contains; - if (geom::orientation(robust_tag, f, vi, p) == geom::sign_t::negative) - contains = geom::orientation(robust_tag, f, vi, vj) == geom::sign_t::negative && geom::orientation(robust_tag, vj, vi, p) == geom::sign_t::negative; + if (math::orientation(robust_tag, f, vi, p) == math::sign_t::negative) + contains = math::orientation(robust_tag, f, vi, vj) == math::sign_t::negative && math::orientation(robust_tag, vj, vi, p) == math::sign_t::negative; else - contains = geom::orientation(robust_tag, f, vi, vj) == geom::sign_t::negative || geom::orientation(robust_tag, vj, vi, p) == geom::sign_t::negative; + contains = math::orientation(robust_tag, f, vi, vj) == math::sign_t::negative || math::orientation(robust_tag, vj, vi, p) == math::sign_t::negative; if (contains) break; ni = ni.prev().twin(); @@ -156,10 +156,10 @@ namespace psemek::cg auto f = at(nj.next().origin().index()); auto p = at(nj.prev().origin().index()); bool contains; - if (geom::orientation(robust_tag, f, vj, p) == geom::sign_t::negative) - contains = geom::orientation(robust_tag, f, vj, vi) == geom::sign_t::negative && geom::orientation(robust_tag, vi, vj, p) == geom::sign_t::negative; + if (math::orientation(robust_tag, f, vj, p) == math::sign_t::negative) + contains = math::orientation(robust_tag, f, vj, vi) == math::sign_t::negative && math::orientation(robust_tag, vi, vj, p) == math::sign_t::negative; else - contains = geom::orientation(robust_tag, f, vj, vi) == geom::sign_t::negative || geom::orientation(robust_tag, vi, vj, p) == geom::sign_t::negative; + contains = math::orientation(robust_tag, f, vj, vi) == math::sign_t::negative || math::orientation(robust_tag, vi, vj, p) == math::sign_t::negative; if (contains) break; nj = nj.prev().twin(); @@ -317,12 +317,12 @@ namespace psemek::cg auto orientation = [&](auto e0, auto e1, auto e2) { auto point = [&](auto e){ return at(result.edge(e).origin().index()); }; - return geom::orientation(robust_tag, point(e0), point(e1), point(e2)); + return math::orientation(robust_tag, point(e0), point(e1), point(e2)); }; if (left && stack_left) { - while (stack.size() >= 2 && orientation(ve.index(), stack.back(), stack[stack.size() - 2]) == geom::sign_t::negative) + while (stack.size() >= 2 && orientation(ve.index(), stack.back(), stack[stack.size() - 2]) == math::sign_t::negative) { auto new_edge = emit_triangle(stack[stack.size() - 2], stack.back()); stack.pop_back(); @@ -334,7 +334,7 @@ namespace psemek::cg else if (!left && !stack_left) { auto e = ve.index(); - while (stack.size() >= 2 && orientation(ve.index(), stack.back(), stack[stack.size() - 2]) == geom::sign_t::positive) + while (stack.size() >= 2 && orientation(ve.index(), stack.back(), stack[stack.size() - 2]) == math::sign_t::positive) { e = emit_triangle(e, stack.back()); stack.pop_back(); @@ -389,7 +389,7 @@ namespace psemek::cg template auto monotone_triangulation(Iterator begin, Iterator end) { - return monotone_triangulation(geom::default_robust_tag, begin, end); + return monotone_triangulation(math::default_robust_tag, begin, end); } } diff --git a/libs/cg/include/psemek/cg/triangulation/triangulation.hpp b/libs/cg/include/psemek/cg/triangulation/triangulation.hpp index b0ffcc3b..bf596af7 100644 --- a/libs/cg/include/psemek/cg/triangulation/triangulation.hpp +++ b/libs/cg/include/psemek/cg/triangulation/triangulation.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -100,7 +100,7 @@ namespace psemek::cg bool degenerate = false; // find first hull edge visible from new point - while (orientation(robust_tag, at(*it), at(hp0.index()), at(hp1.index())) != geom::sign_t::positive) + while (orientation(robust_tag, at(*it), at(hp0.index()), at(hp1.index())) != math::sign_t::positive) { move_hull_edge(); if (cur_hull_edge == hull_start) @@ -183,7 +183,7 @@ namespace psemek::cg } // until current edge is visible - while (orientation(robust_tag, at(*it), at(hp0.index()), at(hp1.index())) == geom::sign_t::positive) + while (orientation(robust_tag, at(*it), at(hp0.index()), at(hp1.index())) == math::sign_t::positive) { // fill new triangle @@ -253,7 +253,7 @@ namespace psemek::cg template auto triangulate(InputIterator begin, InputIterator end) { - return triangulate(geom::default_robust_tag, begin, end); + return triangulate(math::default_robust_tag, begin, end); } } diff --git a/libs/cg/include/psemek/cg/voronoi.hpp b/libs/cg/include/psemek/cg/voronoi.hpp index 47d1eb51..e4b2ae46 100644 --- a/libs/cg/include/psemek/cg/voronoi.hpp +++ b/libs/cg/include/psemek/cg/voronoi.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include #include @@ -35,8 +35,8 @@ namespace psemek::cg using T = typename point_type::scalar_type; - geom::matrix m; - geom::vector b; + math::matrix m; + math::vector b; for (std::size_t i = 0; i < 3; ++i) { @@ -47,7 +47,7 @@ namespace psemek::cg b[i] = q[i][0] * q[i][0] + q[i][1] * q[i][1]; } - geom::gauss(m, b); + math::gauss(m, b); auto newp = result.push_point(point_type{b[0] / 2, b[1] / 2}); newp.edge(result.edge(p.edge().index())); diff --git a/libs/fonts/CMakeLists.txt b/libs/fonts/CMakeLists.txt index 3bdb0148..e272fea6 100644 --- a/libs/fonts/CMakeLists.txt +++ b/libs/fonts/CMakeLists.txt @@ -10,7 +10,7 @@ endif() psemek_add_library(psemek-fonts ${PSEMEK_FONTS_HEADERS} ${PSEMEK_FONTS_SOURCES}) target_include_directories(psemek-fonts PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" ${FREETYPE_INCLUDE_DIRS}) -target_link_libraries(psemek-fonts PUBLIC psemek-util psemek-geom psemek-gfx rapidjson ${FREETYPE_LIBRARY}) +target_link_libraries(psemek-fonts PUBLIC psemek-util psemek-math psemek-gfx rapidjson ${FREETYPE_LIBRARY}) if(PSEMEK_GRAPHICS_API STREQUAL WEBGPU) target_link_libraries(psemek-fonts PUBLIC psemek-wgpu) endif() diff --git a/libs/fonts/include/psemek/fonts/bmfont.hpp b/libs/fonts/include/psemek/fonts/bmfont.hpp index 5d013145..2b18455f 100644 --- a/libs/fonts/include/psemek/fonts/bmfont.hpp +++ b/libs/fonts/include/psemek/fonts/bmfont.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -23,7 +23,7 @@ namespace psemek::fonts }; std::string name; - geom::vector size; + math::vector size; int baseline; util::hash_map glyphs; float sdf_scale = 0.f; diff --git a/libs/fonts/include/psemek/fonts/font.hpp b/libs/fonts/include/psemek/fonts/font.hpp index 2ed728a9..a70f77fc 100644 --- a/libs/fonts/include/psemek/fonts/font.hpp +++ b/libs/fonts/include/psemek/fonts/font.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include @@ -28,7 +28,7 @@ namespace psemek::fonts struct glyph { - geom::box position; + math::box position; char32_t character; }; @@ -53,25 +53,25 @@ namespace psemek::fonts virtual std::string_view name() const = 0; - virtual geom::vector size() const = 0; + virtual math::vector size() const = 0; virtual int baseline() const { return 0; } virtual bool supports_character(char32_t c) const = 0; virtual util::span supported_characters() const = 0; - virtual std::vector shape(std::string_view str, shape_options const & options, geom::point & pen) const = 0; - virtual std::vector shape(std::u32string_view str, shape_options const & options, geom::point & pen) const = 0; + virtual std::vector shape(std::string_view str, shape_options const & options, math::point & pen) const = 0; + virtual std::vector shape(std::u32string_view str, shape_options const & options, math::point & pen) const = 0; virtual std::vector shape(std::string_view str, shape_options const & options) const { - geom::point pen{0.f, 0.f}; + math::point pen{0.f, 0.f}; return shape(str, options, pen); } virtual std::vector shape(std::u32string_view str, shape_options const & options) const { - geom::point pen{0.f, 0.f}; + math::point pen{0.f, 0.f}; return shape(str, options, pen); } @@ -79,7 +79,7 @@ namespace psemek::fonts virtual float sdf_scale() const { return 0.f; } - virtual std::optional> texcoords(char32_t character) const = 0; + virtual std::optional> texcoords(char32_t character) const = 0; virtual ~font() {} }; diff --git a/libs/fonts/include/psemek/fonts/font_v2.hpp b/libs/fonts/include/psemek/fonts/font_v2.hpp index 1a570fc9..ed2976b8 100644 --- a/libs/fonts/include/psemek/fonts/font_v2.hpp +++ b/libs/fonts/include/psemek/fonts/font_v2.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #if defined(PSEMEK_GRAPHICS_API_OPENGL) @@ -37,8 +37,8 @@ namespace psemek::fonts struct shaped_glyph { texture_type const * texture; - geom::box position; - geom::box texcoords; + math::box position; + math::box texcoords; }; struct shape_options @@ -60,21 +60,21 @@ namespace psemek::fonts virtual std::string_view name() const = 0; - virtual geom::vector size() const = 0; + virtual math::vector size() const = 0; virtual int xheight() const = 0; - virtual std::vector const & shape(std::string_view str, shape_options const & options, geom::point & pen) = 0; - virtual std::vector const & shape(std::u32string_view str, shape_options const & options, geom::point & pen) = 0; + virtual std::vector const & shape(std::string_view str, shape_options const & options, math::point & pen) = 0; + virtual std::vector const & shape(std::u32string_view str, shape_options const & options, math::point & pen) = 0; virtual std::vector const & shape(std::string_view str, shape_options const & options) { - geom::point pen{0.f, 0.f}; + math::point pen{0.f, 0.f}; return shape(str, options, pen); } virtual std::vector const & shape(std::u32string_view str, shape_options const & options) { - geom::point pen{0.f, 0.f}; + math::point pen{0.f, 0.f}; return shape(str, options, pen); } diff --git a/libs/fonts/include/psemek/fonts/kerned_font.hpp b/libs/fonts/include/psemek/fonts/kerned_font.hpp index 2f572a97..79ce1a47 100644 --- a/libs/fonts/include/psemek/fonts/kerned_font.hpp +++ b/libs/fonts/include/psemek/fonts/kerned_font.hpp @@ -15,19 +15,19 @@ namespace psemek::fonts std::string_view name() const override { return data_.name; } - geom::vector size() const override { return data_.size; } + math::vector size() const override { return data_.size; } int baseline() const override { return data_.baseline; } bool supports_character(char32_t c) const override; util::span supported_characters() const override { return ranges_; } - std::vector shape(std::string_view str, shape_options const & options, geom::point & pen) const override; - std::vector shape(std::u32string_view str, shape_options const & options, geom::point & pen) const override; + std::vector shape(std::string_view str, shape_options const & options, math::point & pen) const override; + std::vector shape(std::u32string_view str, shape_options const & options, math::point & pen) const override; gfx::texture_2d const & atlas() const override { return atlas_; }; - std::optional> texcoords(char32_t character) const override; + std::optional> texcoords(char32_t character) const override; protected: std::vector ranges_; @@ -35,7 +35,7 @@ namespace psemek::fonts gfx::texture_2d atlas_; template - std::vector shape_impl(String const & str, shape_options const & options, geom::point & pen) const; + std::vector shape_impl(String const & str, shape_options const & options, math::point & pen) const; }; } diff --git a/libs/fonts/include/psemek/fonts/monospace_font.hpp b/libs/fonts/include/psemek/fonts/monospace_font.hpp index 6fba3279..046474ce 100644 --- a/libs/fonts/include/psemek/fonts/monospace_font.hpp +++ b/libs/fonts/include/psemek/fonts/monospace_font.hpp @@ -8,33 +8,33 @@ namespace psemek::fonts struct monospace_font : font { - monospace_font(character_range range, std::string_view name, geom::vector size, gfx::texture_2d atlas, std::vector> texcoords); + monospace_font(character_range range, std::string_view name, math::vector size, gfx::texture_2d atlas, std::vector> texcoords); font_type type() const override { return font_type::bitmap; } std::string_view name() const override { return name_; } - geom::vector size() const override { return size_; } + math::vector size() const override { return size_; } bool supports_character(char32_t c) const override; util::span supported_characters() const override { return {&range_, &range_ + 1}; } - std::vector shape(std::string_view str, shape_options const & options, geom::point & pen) const override; - std::vector shape(std::u32string_view str, shape_options const & options, geom::point & pen) const override; + std::vector shape(std::string_view str, shape_options const & options, math::point & pen) const override; + std::vector shape(std::u32string_view str, shape_options const & options, math::point & pen) const override; gfx::texture_2d const & atlas() const override { return atlas_; }; - std::optional> texcoords(char32_t character) const override; + std::optional> texcoords(char32_t character) const override; private: character_range range_; std::string_view name_; - geom::vector size_; + math::vector size_; gfx::texture_2d atlas_; - std::vector> texcoords_; + std::vector> texcoords_; template - std::vector shape_impl(String const & str, shape_options const & options, geom::point & pen) const; + std::vector shape_impl(String const & str, shape_options const & options, math::point & pen) const; }; } diff --git a/libs/fonts/source/freetype.cpp b/libs/fonts/source/freetype.cpp index fd352d80..615c3cde 100644 --- a/libs/fonts/source/freetype.cpp +++ b/libs/fonts/source/freetype.cpp @@ -103,7 +103,7 @@ namespace psemek::fonts return FT_Get_Postscript_Name(state_->face); } - geom::vector size() const override + math::vector size() const override { return {size_, size_}; } @@ -113,12 +113,12 @@ namespace psemek::fonts return xheight_; } - std::vector const & shape(std::string_view str, shape_options const & options, geom::point & pen) override + std::vector const & shape(std::string_view str, shape_options const & options, math::point & pen) override { return shape_impl(util::utf8_range(str), options, pen); } - std::vector const & shape(std::u32string_view str, shape_options const & options, geom::point & pen) override + std::vector const & shape(std::u32string_view str, shape_options const & options, math::point & pen) override { return shape_impl(str, options, pen); } @@ -150,9 +150,9 @@ namespace psemek::fonts struct glyph_data { int page; - geom::box part; - geom::vector offset; - geom::vector advance; + math::box part; + math::vector offset; + math::vector advance; }; util::hash_map glyph_mapping_; @@ -161,7 +161,7 @@ namespace psemek::fonts std::vector shaped_text_; template - std::vector const & shape_impl(String const & string, shape_options const & options, geom::point & pen) + std::vector const & shape_impl(String const & string, shape_options const & options, math::point & pen) { (void)options; // TODO: support options @@ -270,7 +270,7 @@ namespace psemek::fonts } pages_.back().current_row_x += face->glyph->bitmap.width + 2 * padding; - geom::make_max(pages_.back().current_row_height, face->glyph->bitmap.rows + 2 * padding); + math::make_max(pages_.back().current_row_height, face->glyph->bitmap.rows + 2 * padding); pages_.back().needs_update = true; need_update_pages = true; @@ -288,7 +288,7 @@ namespace psemek::fonts result.texcoords[1].min = static_cast(data->part[1].min) / page_size; result.texcoords[1].max = static_cast(data->part[1].max) / page_size; - pen += geom::cast(data->advance); + pen += math::cast(data->advance); } if (need_update_pages) diff --git a/libs/fonts/source/kerned_font.cpp b/libs/fonts/source/kerned_font.cpp index 455946a1..0e8b2846 100644 --- a/libs/fonts/source/kerned_font.cpp +++ b/libs/fonts/source/kerned_font.cpp @@ -29,7 +29,7 @@ namespace psemek::fonts } } - static geom::vector advance_dir(shape_options::direction_t direction) + static math::vector advance_dir(shape_options::direction_t direction) { switch (direction) { @@ -47,21 +47,21 @@ namespace psemek::fonts return data_.glyphs.contains(c); } - std::vector kerned_font::shape(std::string_view str, shape_options const & options, geom::point & pen) const + std::vector kerned_font::shape(std::string_view str, shape_options const & options, math::point & pen) const { return shape_impl(util::utf8_range(str), options, pen); } - std::vector kerned_font::shape(std::u32string_view str, shape_options const & options, geom::point & pen) const + std::vector kerned_font::shape(std::u32string_view str, shape_options const & options, math::point & pen) const { return shape_impl(str, options, pen); } template - std::vector kerned_font::shape_impl(String const & str, shape_options const & options, geom::point & pen) const + std::vector kerned_font::shape_impl(String const & str, shape_options const & options, math::point & pen) const { char32_t const unknown = supports_character(options.unknown_character) ? options.unknown_character : '?'; - geom::vector const advance_mask = advance_dir(options.direction); + math::vector const advance_mask = advance_dir(options.direction); std::vector result; @@ -80,21 +80,21 @@ namespace psemek::fonts g.position[1].max = pen[1] - data.offset_y * options.scale; result.push_back(g); - geom::vector advance{data.advance * options.scale, data_.size[1] * options.scale}; + math::vector advance{data.advance * options.scale, data_.size[1] * options.scale}; - pen += geom::pointwise_mult(advance_mask, advance); + pen += math::pointwise_mult(advance_mask, advance); } return result; } - std::optional> kerned_font::texcoords(char32_t c) const + std::optional> kerned_font::texcoords(char32_t c) const { auto it = data_.glyphs.find(c); if (it == data_.glyphs.end()) return std::nullopt; - geom::box box; + math::box box; box[0].min = it->second.start_x; box[1].min = it->second.start_y; box[0].max = box[0].min + it->second.size_x; diff --git a/libs/fonts/source/monospace_font.cpp b/libs/fonts/source/monospace_font.cpp index 6341042c..920f7b3e 100644 --- a/libs/fonts/source/monospace_font.cpp +++ b/libs/fonts/source/monospace_font.cpp @@ -6,7 +6,7 @@ namespace psemek::fonts { - monospace_font::monospace_font(character_range range, std::string_view name, geom::vector size, gfx::texture_2d atlas, std::vector> texcoords) + monospace_font::monospace_font(character_range range, std::string_view name, math::vector size, gfx::texture_2d atlas, std::vector> texcoords) : range_{range} , name_{name} , size_{size} @@ -20,7 +20,7 @@ namespace psemek::fonts throw util::exception("Monospace font must support '?' character"); } - static geom::vector advance_dir(shape_options::direction_t direction) + static math::vector advance_dir(shape_options::direction_t direction) { switch (direction) { @@ -38,22 +38,22 @@ namespace psemek::fonts return std::isspace(c) || (c >= range_.begin && c < range_.end); } - std::vector monospace_font::shape(std::string_view str, shape_options const & options, geom::point & pen) const + std::vector monospace_font::shape(std::string_view str, shape_options const & options, math::point & pen) const { return shape_impl(util::utf8_range(str), options, pen); } - std::vector monospace_font::shape(std::u32string_view str, shape_options const & options, geom::point & pen) const + std::vector monospace_font::shape(std::u32string_view str, shape_options const & options, math::point & pen) const { return shape_impl(str, options, pen); } template - std::vector monospace_font::shape_impl(String const & str, shape_options const & options, geom::point & pen) const + std::vector monospace_font::shape_impl(String const & str, shape_options const & options, math::point & pen) const { char32_t const unknown = supports_character(options.unknown_character) ? options.unknown_character : '?'; - geom::vector const size = geom::cast(this->size()) * options.scale; - geom::vector const advance = geom::pointwise_mult(advance_dir(options.direction), size); + math::vector const size = math::cast(this->size()) * options.scale; + math::vector const advance = math::pointwise_mult(advance_dir(options.direction), size); std::vector result; @@ -77,7 +77,7 @@ namespace psemek::fonts return result; } - std::optional> monospace_font::texcoords(char32_t c) const + std::optional> monospace_font::texcoords(char32_t c) const { if (!supports_character(c)) return std::nullopt; diff --git a/libs/geom/CMakeLists.txt b/libs/geom/CMakeLists.txt deleted file mode 100644 index ec2122ff..00000000 --- a/libs/geom/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -option(PSEMEK_GEOM_ROBUST_PREDICATES "Use robust geometric predicates" OFF) - -find_package(Boost REQUIRED) -if(PSEMEK_ROBUST_PREDICATES) - find_package(GMP REQUIRED) -endif() - -file(GLOB_RECURSE PSEMEK_GEOM_HEADERS "include/*.hpp") -file(GLOB_RECURSE PSEMEK_GEOM_SOURCES "source/*.cpp") - -psemek_add_library(psemek-geom ${PSEMEK_GEOM_HEADERS} ${PSEMEK_GEOM_SOURCES}) -target_include_directories(psemek-geom PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-geom PUBLIC psemek-util psemek-group Boost::boost) -if(PSEMEK_ROBUST_PREDICATES) - target_link_libraries(psemek-geom PUBLIC gmp) -endif() - -psemek_glob_tests(psemek-geom tests) diff --git a/libs/gfx/CMakeLists.txt b/libs/gfx/CMakeLists.txt index 60d70190..5864a1fd 100644 --- a/libs/gfx/CMakeLists.txt +++ b/libs/gfx/CMakeLists.txt @@ -5,7 +5,7 @@ file(GLOB_RECURSE PSEMEK_GFX_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "sou psemek_add_library(psemek-gfx ${PSEMEK_GFX_HEADERS} ${PSEMEK_GFX_SOURCES} "${CMAKE_CURRENT_SOURCE_DIR}/api/${PSEMEK_GL_API}/source/gl.cpp") target_include_directories(psemek-gfx PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/api/${PSEMEK_GL_API}/include") -target_link_libraries(psemek-gfx PUBLIC psemek-util psemek-geom psemek-cg psemek-random psemek-io psemek-log ${PSEMEK_GL_LIBRARIES} rapidjson) +target_link_libraries(psemek-gfx PUBLIC psemek-util psemek-math psemek-cg psemek-random psemek-io psemek-log ${PSEMEK_GL_LIBRARIES} rapidjson) if(NOT KHR_PLATFORM_FILE) message(STATUS "KHR/khrplatform.h not found, using a substitute header") target_include_directories(psemek-gfx PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/extra/khr/include") diff --git a/libs/gfx/include/psemek/gfx/armature.hpp b/libs/gfx/include/psemek/gfx/armature.hpp index 7a69d21c..bae1da2d 100644 --- a/libs/gfx/include/psemek/gfx/armature.hpp +++ b/libs/gfx/include/psemek/gfx/armature.hpp @@ -1,11 +1,11 @@ #pragma once -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include @@ -17,54 +17,54 @@ namespace psemek::gfx static constexpr std::uint32_t null = static_cast(-1); std::uint32_t parent = null; - geom::vector offset = geom::vector::zero(); - geom::matrix axes = geom::matrix::identity(); + math::vector offset = math::vector::zero(); + math::matrix axes = math::matrix::identity(); }; template struct bone_transform { - geom::quaternion rotation; + math::quaternion rotation; T scale; - geom::vector translation; + math::vector translation; static bone_transform identity(); - static bone_transform from_rotation(geom::quaternion const & rotation); + static bone_transform from_rotation(math::quaternion const & rotation); static bone_transform from_scale(T const & scale); - static bone_transform from_translation(geom::vector const & translation); + static bone_transform from_translation(math::vector const & translation); - geom::matrix matrix() const; + math::matrix matrix() const; }; template bone_transform bone_transform::identity() { - return {geom::quaternion::identity(), static_cast(1), geom::vector::zero()}; + return {math::quaternion::identity(), static_cast(1), math::vector::zero()}; } template - bone_transform bone_transform::from_rotation(geom::quaternion const & rotation) + bone_transform bone_transform::from_rotation(math::quaternion const & rotation) { - return {rotation, static_cast(1), geom::vector::zero()}; + return {rotation, static_cast(1), math::vector::zero()}; } template bone_transform bone_transform::from_scale(T const & scale) { - return {geom::quaternion::identity(), scale, geom::vector::zero()}; + return {math::quaternion::identity(), scale, math::vector::zero()}; } template - bone_transform bone_transform::from_translation(geom::vector const & translation) + bone_transform bone_transform::from_translation(math::vector const & translation) { - return {geom::quaternion::identity(), static_cast(1), translation}; + return {math::quaternion::identity(), static_cast(1), translation}; } template - geom::matrix bone_transform::matrix() const + math::matrix bone_transform::matrix() const { - return geom::affine_transform(scale * geom::quaternion_rotation(geom::quaternion(rotation.coords)).linear_matrix(), translation).affine_matrix(); + return math::affine_transform(scale * math::quaternion_rotation(math::quaternion(rotation.coords)).linear_matrix(), translation).affine_matrix(); } template @@ -76,7 +76,7 @@ namespace psemek::gfx // (1, t1) * (1, S1 * R1 * t2) * (S1, 0) * (S2, 0) * (R1, 0) * (R2, 0) = // (1, t1 + S1 * R1 * t2) * (S1 * S2, 0) * (R1 * R2, 0) - return bone_transform{m1.rotation * m2.rotation, m1.scale * m2.scale, m1.translation + m1.scale * geom::rotate(m1.rotation, m2.translation)}; + return bone_transform{m1.rotation * m2.rotation, m1.scale * m2.scale, m1.translation + m1.scale * math::rotate(m1.rotation, m2.translation)}; } template @@ -85,24 +85,24 @@ namespace psemek::gfx // [(1, T) * (S, 0) * (R, 0)]^-1 = (R^-1, 0) * (S^-1, 0) * (1, -T) = // = (1, - R^-1 S^-1 T) * (S^-1, 0) * (R^-1, 0) - auto ir = geom::inverse(m.rotation); - return bone_transform{ir, T{1} / m.scale, - geom::rotate(ir, m.translation) / m.scale}; + auto ir = math::inverse(m.rotation); + return bone_transform{ir, T{1} / m.scale, - math::rotate(ir, m.translation) / m.scale}; } template bone_transform lerp(bone_transform const & m1, bone_transform const & m2, T t) { - return {geom::slerp(m1.rotation, m2.rotation, t), std::exp(geom::lerp(std::log(m1.scale), std::log(m2.scale), t)), geom::lerp(m1.translation, m2.translation, t)}; + return {math::slerp(m1.rotation, m2.rotation, t), std::exp(math::lerp(std::log(m1.scale), std::log(m2.scale), t)), math::lerp(m1.translation, m2.translation, t)}; } template - geom::vector operator * (bone_transform const & m, geom::vector const & v) + math::vector operator * (bone_transform const & m, math::vector const & v) { - return m.translation + m.scale * geom::rotate(m.rotation, v); + return m.translation + m.scale * math::rotate(m.rotation, v); } template - geom::point operator * (bone_transform const & m, geom::point const & p) + math::point operator * (bone_transform const & m, math::point const & p) { return p.zero() + m * (p - p.zero()); } @@ -130,11 +130,11 @@ namespace psemek::gfx for (std::size_t b = 0; b < bones.size(); ++b) { result[b] = - gfx::bone_transform{geom::quaternion::identity(), 1.f, bones[b].offset} - * gfx::bone_transform{geom::quaternion::rotation(bones[b].axes), 1.f, {0.f, 0.f, 0.f}} + gfx::bone_transform{math::quaternion::identity(), 1.f, bones[b].offset} + * gfx::bone_transform{math::quaternion::rotation(bones[b].axes), 1.f, {0.f, 0.f, 0.f}} * local_pose[b] - * gfx::bone_transform{geom::inverse(geom::quaternion::rotation(bones[b].axes)), 1.f, {0.f, 0.f, 0.f}} - * gfx::bone_transform{geom::quaternion::identity(), 1.f, -bones[b].offset} + * gfx::bone_transform{math::inverse(math::quaternion::rotation(bones[b].axes)), 1.f, {0.f, 0.f, 0.f}} + * gfx::bone_transform{math::quaternion::identity(), 1.f, -bones[b].offset} ; auto p = bones[b].parent; @@ -154,9 +154,9 @@ namespace psemek::gfx for (std::size_t b = 0; b < bones.size(); ++b) { result[b] = - gfx::bone_transform{geom::quaternion::identity(), 1.f, bones[b].offset} + gfx::bone_transform{math::quaternion::identity(), 1.f, bones[b].offset} * local_pose[b] - * gfx::bone_transform{geom::quaternion::identity(), 1.f, -bones[b].offset} + * gfx::bone_transform{math::quaternion::identity(), 1.f, -bones[b].offset} ; auto p = bones[b].parent; diff --git a/libs/gfx/include/psemek/gfx/attribs.hpp b/libs/gfx/include/psemek/gfx/attribs.hpp index bef7f71b..1ec53476 100644 --- a/libs/gfx/include/psemek/gfx/attribs.hpp +++ b/libs/gfx/include/psemek/gfx/attribs.hpp @@ -2,10 +2,10 @@ #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -204,9 +204,9 @@ namespace psemek::gfx }; template - struct attrib_traits> + struct attrib_traits> { - using attrib_type = geom::vector; + using attrib_type = math::vector; static constexpr GLint size = N; static constexpr GLenum type = attrib_traits::type; @@ -215,9 +215,9 @@ namespace psemek::gfx }; template - struct attrib_traits> + struct attrib_traits> { - using attrib_type = geom::point; + using attrib_type = math::point; static constexpr GLint size = N; static constexpr GLenum type = attrib_traits::type; @@ -226,15 +226,15 @@ namespace psemek::gfx }; template - struct attrib_traits> + struct attrib_traits> { - using attrib_type = geom::matrix; + using attrib_type = math::matrix; }; template - struct attrib_traits> + struct attrib_traits> { - using attrib_type = geom::quaternion; + using attrib_type = math::quaternion; static constexpr GLint size = 4; static constexpr GLenum type = attrib_traits::type; @@ -312,7 +312,7 @@ namespace psemek::gfx {}; template - struct is_matrix> : std::true_type + struct is_matrix> : std::true_type {}; template @@ -383,7 +383,7 @@ namespace psemek::gfx if constexpr (is_matrix::value) { using T = typename attr::scalar_type; - using traits = attrib_traits>; + using traits = attrib_traits>; for (std::size_t row = 0; row < attr::static_rows; ++row) { @@ -464,55 +464,55 @@ namespace psemek::gfx inline std::int8_t to_signed_8bit(float x) { - return static_cast(geom::clamp((0.5f * x + 0.5f) * 255.f - 128.f, {-128.f, 127.f})); + return static_cast(math::clamp((0.5f * x + 0.5f) * 255.f - 128.f, {-128.f, 127.f})); } inline std::uint8_t to_unsigned_8bit(float x) { - return static_cast(geom::clamp(x * 255.f, {0.f, 255.f})); + return static_cast(math::clamp(x * 255.f, {0.f, 255.f})); } inline std::int16_t to_signed_16bit(float x) { - return static_cast(geom::clamp((0.5f * x + 0.5f) * 65535.f - 32768.f, {-32768.f, 32767.f})); + return static_cast(math::clamp((0.5f * x + 0.5f) * 65535.f - 32768.f, {-32768.f, 32767.f})); } inline std::uint16_t to_unsigned_16bit(float x) { - return static_cast(geom::clamp(x * 65535.f, {0.f, 65535.f})); + return static_cast(math::clamp(x * 65535.f, {0.f, 65535.f})); } template - geom::vector to_signed_8bit(geom::vector const & v) + math::vector to_signed_8bit(math::vector const & v) { - geom::vector result; + math::vector result; for (std::size_t i = 0; i < D; ++i) result[i] = to_signed_8bit(v[i]); return result; } template - geom::vector to_unsigned_8bit(geom::vector const & v) + math::vector to_unsigned_8bit(math::vector const & v) { - geom::vector result; + math::vector result; for (std::size_t i = 0; i < D; ++i) result[i] = to_unsigned_8bit(v[i]); return result; } template - geom::vector to_signed_16bit(geom::vector const & v) + math::vector to_signed_16bit(math::vector const & v) { - geom::vector result; + math::vector result; for (std::size_t i = 0; i < D; ++i) result[i] = to_signed_16bit(v[i]); return result; } template - geom::vector to_unsigned_16bit(geom::vector const & v) + math::vector to_unsigned_16bit(math::vector const & v) { - geom::vector result; + math::vector result; for (std::size_t i = 0; i < D; ++i) result[i] = to_unsigned_16bit(v[i]); return result; diff --git a/libs/gfx/include/psemek/gfx/color.hpp b/libs/gfx/include/psemek/gfx/color.hpp index 8f74c9e8..2f2e78f6 100644 --- a/libs/gfx/include/psemek/gfx/color.hpp +++ b/libs/gfx/include/psemek/gfx/color.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include @@ -11,111 +11,111 @@ namespace psemek::gfx { - using color_3f = geom::vector; - using color_4f = geom::vector; + using color_3f = math::vector; + using color_4f = math::vector; - using color_rgb = geom::vector; - using color_rgba = geom::vector; + using color_rgb = math::vector; + using color_rgba = math::vector; template - auto to_colorf(geom::vector const & c) + auto to_colorf(math::vector const & c) { - geom::vector r; + math::vector r; for (std::size_t i = 0; i < N; ++i) r[i] = c[i] / 255.f; return r; } template - auto to_colorf(geom::vector const & c) + auto to_colorf(math::vector const & c) { - geom::vector r; + math::vector r; for (std::size_t i = 0; i < N; ++i) r[i] = c[i] / 65535.f; return r; } template - auto to_coloru8(geom::vector const & c) + auto to_coloru8(math::vector const & c) { - geom::vector r; + math::vector r; for (std::size_t i = 0; i < N; ++i) r[i] = (static_cast(c[i]) * 255) / 65535; return r; } template - auto to_coloru8(geom::vector const & c) + auto to_coloru8(math::vector const & c) { - geom::vector r; + math::vector r; for (std::size_t i = 0; i < N; ++i) - r[i] = static_cast(std::round(geom::clamp(c[i] * 255.f, {0.f, 255.f}))); + r[i] = static_cast(std::round(math::clamp(c[i] * 255.f, {0.f, 255.f}))); return r; } template - auto to_coloru16(geom::vector const & c) + auto to_coloru16(math::vector const & c) { - geom::vector r; + math::vector r; for (std::size_t i = 0; i < N; ++i) r[i] = (static_cast(c[i]) * 65535) / 255; return r; } template - auto to_coloru16(geom::vector const & c) + auto to_coloru16(math::vector const & c) { - geom::vector r; + math::vector r; for (std::size_t i = 0; i < N; ++i) - r[i] = static_cast(std::round(geom::clamp(c[i] * 65535.f, {0.f, 65535.f}))); + r[i] = static_cast(std::round(math::clamp(c[i] * 65535.f, {0.f, 65535.f}))); return r; } template - auto lerp(geom::vector const & c0, geom::vector const & c1, float t) + auto lerp(math::vector const & c0, math::vector const & c1, float t) { - return geom::lerp(c0, c1, t); + return math::lerp(c0, c1, t); } template - auto lerp(geom::vector const & c0, geom::vector const & c1, float t) + auto lerp(math::vector const & c0, math::vector const & c1, float t) { return to_coloru8(lerp(to_colorf(c0), to_colorf(c1), t)); } template - auto to_srgb(geom::vector const & c, float g = 1.f / 2.2f) + auto to_srgb(math::vector const & c, float g = 1.f / 2.2f) { - geom::vector r = c; + math::vector r = c; for (std::size_t i = 0; i < std::min(3, N); ++i) r[i] = std::pow(r[i], g); return r; } template - auto to_srgb(geom::vector const & c, float g = 1.f / 2.2f) + auto to_srgb(math::vector const & c, float g = 1.f / 2.2f) { return to_coloru8(to_srgb(to_colorf(c), g)); } template - auto to_srgb(geom::vector const & c, float g = 1.f / 2.2f) + auto to_srgb(math::vector const & c, float g = 1.f / 2.2f) { return to_coloru16(to_srgb(to_colorf(c), g)); } template - auto to_linear(geom::vector const & c, float g = 1.f / 2.2f) + auto to_linear(math::vector const & c, float g = 1.f / 2.2f) { return to_srgb(c, 1.f / g); } - inline geom::vector premult(geom::vector const & c) + inline math::vector premult(math::vector const & c) { return {c[0] * c[3], c[1] * c[3], c[2] * c[3], c[3]}; } - inline geom::vector premult(geom::vector const & c) + inline math::vector premult(math::vector const & c) { return { (static_cast(c[0]) * c[3]) / 255, @@ -125,7 +125,7 @@ namespace psemek::gfx }; } - inline geom::vector premult(geom::vector const & c) + inline math::vector premult(math::vector const & c) { return { (static_cast(c[0]) * c[3]) / 65535, @@ -135,18 +135,18 @@ namespace psemek::gfx }; } - inline geom::vector unpremult(geom::vector const & c) + inline math::vector unpremult(math::vector const & c) { if (c[3] == 0.f) - return geom::vector::zero(); + return math::vector::zero(); return {c[0] / c[3], c[1] / c[3], c[2] / c[3], c[3]}; } - inline geom::vector unpremult(geom::vector const & c) + inline math::vector unpremult(math::vector const & c) { if (c[3] == 0) - return geom::vector::zero(); + return math::vector::zero(); return { (static_cast(c[0]) * 255) / c[3], @@ -156,10 +156,10 @@ namespace psemek::gfx }; } - inline geom::vector unpremult(geom::vector const & c) + inline math::vector unpremult(math::vector const & c) { if (c[3] == 0) - return geom::vector::zero(); + return math::vector::zero(); return { (static_cast(c[0]) * 65535) / c[3], @@ -182,7 +182,7 @@ namespace psemek::gfx } template - auto blend(geom::vector const & c0, geom::vector const & c1, float t) + auto blend(math::vector const & c0, math::vector const & c1, float t) { return to_coloru8(blend(to_colorf(c0), to_colorf(c1), t)); } @@ -190,9 +190,9 @@ namespace psemek::gfx template struct as_hex { - geom::vector color; + math::vector color; - as_hex(geom::vector const & color) + as_hex(math::vector const & color) : color{color} {} }; @@ -220,7 +220,7 @@ namespace psemek::gfx auto as_color_rgb() const { - return to_coloru8(geom::vector{c[0], c[1], c[2]}); + return to_coloru8(math::vector{c[0], c[1], c[2]}); } auto as_color_rgba() const @@ -278,31 +278,31 @@ namespace psemek::gfx static const generic_color gray {{0.50f, 0.50f, 0.50f, 1.f}}; template - geom::vector light(geom::vector c, float lightness = 0.5f) + math::vector light(math::vector c, float lightness = 0.5f) { static_assert(N == 3 || N == 4); for (std::size_t i = 0; i < 3; ++i) - c[i] = geom::lerp(c[i], 1.f, lightness); + c[i] = math::lerp(c[i], 1.f, lightness); return c; } template - geom::vector dark(geom::vector c, float darkness = 0.5f) + math::vector dark(math::vector c, float darkness = 0.5f) { static_assert(N == 3 || N == 4); for (std::size_t i = 0; i < 3; ++i) - c[i] = geom::lerp(c[i], 0.f, darkness); + c[i] = math::lerp(c[i], 0.f, darkness); return c; } template - geom::vector light(geom::vector c, float lightness = 0.5f) + math::vector light(math::vector c, float lightness = 0.5f) { return to_coloru8(light(to_colorf(c), lightness)); } template - geom::vector dark(geom::vector c, float darkness = 0.5f) + math::vector dark(math::vector c, float darkness = 0.5f) { return to_coloru8(dark(to_colorf(c), darkness)); } diff --git a/libs/gfx/include/psemek/gfx/gltf_accessor_iterator.hpp b/libs/gfx/include/psemek/gfx/gltf_accessor_iterator.hpp index 833ca6ea..16af4e84 100644 --- a/libs/gfx/include/psemek/gfx/gltf_accessor_iterator.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_accessor_iterator.hpp @@ -37,70 +37,70 @@ namespace psemek::gfx {}; template - struct accessor_traits> + struct accessor_traits> { static constexpr bool is_floating_point = accessor_traits::is_floating_point; static constexpr std::size_t components = N; using component_type = T; - static auto pointer(geom::vector & value) + static auto pointer(math::vector & value) { return &value[0]; } - static void finalize(geom::vector &) + static void finalize(math::vector &) {} }; template - struct accessor_traits> + struct accessor_traits> { static constexpr bool is_floating_point = accessor_traits::is_floating_point; static constexpr std::size_t components = N; using component_type = T; - static auto pointer(geom::point & value) + static auto pointer(math::point & value) { return &value[0]; } - static void finalize(geom::point &) + static void finalize(math::point &) {} }; template - struct accessor_traits> + struct accessor_traits> { static constexpr bool is_floating_point = accessor_traits::is_floating_point; static constexpr std::size_t components = 4; using component_type = T; - static auto pointer(geom::quaternion & value) + static auto pointer(math::quaternion & value) { return &value[0]; } - static void finalize(geom::quaternion &) + static void finalize(math::quaternion &) {} }; template - struct accessor_traits> + struct accessor_traits> { static constexpr bool is_floating_point = accessor_traits::is_floating_point; static constexpr std::size_t components = R * C; using component_type = T; - static auto pointer(geom::matrix & value) + static auto pointer(math::matrix & value) { return &value[0][0]; } - static void finalize(geom::matrix & value) + static void finalize(math::matrix & value) { - geom::matrix temp; + math::matrix temp; std::copy(value.coords, value.coords + R * C, temp.coords); - value = geom::transpose(temp); + value = math::transpose(temp); } }; diff --git a/libs/gfx/include/psemek/gfx/gltf_animation.hpp b/libs/gfx/include/psemek/gfx/gltf_animation.hpp index bbb60618..ac5f95b7 100644 --- a/libs/gfx/include/psemek/gfx/gltf_animation.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_animation.hpp @@ -1,9 +1,9 @@ #pragma once #include -#include -#include -#include +#include +#include +#include #include #include @@ -20,7 +20,7 @@ namespace psemek::gfx template <> struct gltf_animation_traits { - using output_type = geom::vector; + using output_type = math::vector; static output_type default_value() { @@ -29,7 +29,7 @@ namespace psemek::gfx static output_type lerp(output_type const & v1, output_type const & v2, float t) { - return geom::lerp(v1, v2, t); + return math::lerp(v1, v2, t); } static output_type normalize(output_type const & v) @@ -37,14 +37,14 @@ namespace psemek::gfx return v; } - static void canonicalize(geom::easing_type, std::vector &) + static void canonicalize(math::easing_type, std::vector &) {} }; template <> struct gltf_animation_traits { - using output_type = geom::quaternion; + using output_type = math::quaternion; static output_type default_value() { @@ -53,21 +53,21 @@ namespace psemek::gfx static output_type lerp(output_type const & v1, output_type const & v2, float t) { - return geom::slerp(v1, v2, t); + return math::slerp(v1, v2, t); } static output_type normalize(output_type const & v) { - return geom::normalized(v); + return math::normalized(v); } - static void canonicalize(geom::easing_type interpolation, std::vector & output); + static void canonicalize(math::easing_type interpolation, std::vector & output); }; template <> struct gltf_animation_traits { - using output_type = geom::vector; + using output_type = math::vector; static output_type default_value() { @@ -76,7 +76,7 @@ namespace psemek::gfx static output_type lerp(output_type const & v1, output_type const & v2, float t) { - return geom::lerp(v1, v2, t); + return math::lerp(v1, v2, t); } static output_type normalize(output_type const & v) @@ -84,7 +84,7 @@ namespace psemek::gfx return v; } - static void canonicalize(geom::easing_type, std::vector &) + static void canonicalize(math::easing_type, std::vector &) {} }; @@ -99,10 +99,10 @@ namespace psemek::gfx gltf_animation_channel() : input_{0.f} , output_{traits::default_value()} - , interpolation_{geom::easing_type::constant_left} + , interpolation_{math::easing_type::constant_left} {} - gltf_animation_channel(std::vector input, std::vector output, geom::easing_type interpolation) + gltf_animation_channel(std::vector input, std::vector output, math::easing_type interpolation) : input_(std::move(input)) , output_(std::move(output)) , interpolation_(interpolation) @@ -114,7 +114,7 @@ namespace psemek::gfx gltf_animation_channel & operator = (gltf_animation_channel &&) = default; - geom::interval range() const + math::interval range() const { return {input_.front(), input_.back()}; } @@ -124,7 +124,7 @@ namespace psemek::gfx private: std::vector input_; std::vector output_; - geom::easing_type interpolation_; + math::easing_type interpolation_; }; extern template struct gltf_animation_channel; @@ -145,9 +145,9 @@ namespace psemek::gfx , translation_(std::move(translation)) {} - geom::interval range() const; + math::interval range() const; - geom::affine_transform operator()(float time) const; + math::affine_transform operator()(float time) const; gltf_scale_animation const & scale() const { return scale_; } gltf_rotation_animation const & rotation() const { return rotation_; } diff --git a/libs/gfx/include/psemek/gfx/gltf_mesh.hpp b/libs/gfx/include/psemek/gfx/gltf_mesh.hpp index 5d004f8b..61c79fc9 100644 --- a/libs/gfx/include/psemek/gfx/gltf_mesh.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_mesh.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -19,8 +19,8 @@ namespace psemek::gfx gfx::drawable * drawable; std::optional material; - util::span const> vertices; - util::span const> triangles; + util::span const> vertices; + util::span const> triangles; }; virtual gltf_asset::material const & material(std::size_t index) const = 0; diff --git a/libs/gfx/include/psemek/gfx/gltf_parser.hpp b/libs/gfx/include/psemek/gfx/gltf_parser.hpp index 8bdc37d9..d84573bc 100644 --- a/libs/gfx/include/psemek/gfx/gltf_parser.hpp +++ b/libs/gfx/include/psemek/gfx/gltf_parser.hpp @@ -2,10 +2,10 @@ #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -31,10 +31,10 @@ namespace psemek::gfx std::vector children; std::optional light; // KHR_lights_punctual - geom::vector translation; - geom::quaternion rotation; - geom::vector scale; - geom::affine_transform transform; + math::vector translation; + math::quaternion rotation; + math::vector scale; + math::affine_transform transform; extras_map extras; }; @@ -103,7 +103,7 @@ namespace psemek::gfx { std::size_t input; // keyframes accessor std::size_t output; // values accessor - geom::easing_type interpolation; + math::easing_type interpolation; }; std::string name; @@ -168,7 +168,7 @@ namespace psemek::gfx gfx::color_3f color; float intensity; float range; - geom::interval cone_angle; + math::interval cone_angle; extras_map extras; }; diff --git a/libs/gfx/include/psemek/gfx/mesh.hpp b/libs/gfx/include/psemek/gfx/mesh.hpp index b45e34f1..89d37fc1 100644 --- a/libs/gfx/include/psemek/gfx/mesh.hpp +++ b/libs/gfx/include/psemek/gfx/mesh.hpp @@ -7,10 +7,10 @@ #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include @@ -135,10 +135,10 @@ namespace psemek::gfx void load(std::vector const & vertices, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); template - void load(geom::simplex const * simplices, std::size_t count, GLenum usage = gl::STREAM_DRAW); + void load(math::simplex const * simplices, std::size_t count, GLenum usage = gl::STREAM_DRAW); template - void load(std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); + void load(std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); // Indexed vertex data @@ -153,10 +153,10 @@ namespace psemek::gfx void load(std::vector const & vertices, std::vector const & indices, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); template - void load(Vertex const * vertices, std::size_t vertex_count, geom::simplex const * simplices, std::size_t simplex_count, GLenum usage = gl::STREAM_DRAW); + void load(Vertex const * vertices, std::size_t vertex_count, math::simplex const * simplices, std::size_t simplex_count, GLenum usage = gl::STREAM_DRAW); template - void load(std::vector const & vertices, std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); + void load(std::vector const & vertices, std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); void load_raw(imported_mesh const & m); @@ -169,10 +169,10 @@ namespace psemek::gfx void load_index(std::vector const & indices, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW); template - void load_index(geom::simplex const * simplices, std::size_t simplex_count, GLenum usage = gl::STREAM_DRAW); + void load_index(math::simplex const * simplices, std::size_t simplex_count, GLenum usage = gl::STREAM_DRAW); template - void load_index(std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); + void load_index(std::vector> const & simplices, GLenum usage = gl::STREAM_DRAW); // Instance data @@ -254,14 +254,14 @@ namespace psemek::gfx } template - void mesh::load(geom::simplex const * simplices, std::size_t count, GLenum usage) + void mesh::load(math::simplex const * simplices, std::size_t count, GLenum usage) { static_assert(sizeof(Vertex) * (N + 1) == sizeof(simplices[0])); load(reinterpret_cast(simplices), count * (N + 1), detail::get_primitive_type(), usage); } template - void mesh::load(std::vector> const & simplices, GLenum usage) + void mesh::load(std::vector> const & simplices, GLenum usage) { load(simplices.data(), simplices.size(), usage); } @@ -279,14 +279,14 @@ namespace psemek::gfx } template - void mesh::load(Vertex const * vertices, std::size_t vertex_count, geom::simplex const * simplices, std::size_t simplex_count, GLenum usage) + void mesh::load(Vertex const * vertices, std::size_t vertex_count, math::simplex const * simplices, std::size_t simplex_count, GLenum usage) { static_assert(sizeof(Index) * (N + 1) == sizeof(simplices[0])); load(vertices, vertex_count, reinterpret_cast(simplices), (N + 1) * simplex_count, detail::get_primitive_type(), usage); } template - void mesh::load(std::vector const & vertices, std::vector> const & simplices, GLenum usage) + void mesh::load(std::vector const & vertices, std::vector> const & simplices, GLenum usage) { load(vertices.data(), vertices.size(), simplices.data(), simplices.size(), usage); } @@ -317,14 +317,14 @@ namespace psemek::gfx } template - void mesh::load_index(geom::simplex const * simplices, std::size_t simplex_count, GLenum usage) + void mesh::load_index(math::simplex const * simplices, std::size_t simplex_count, GLenum usage) { static_assert(sizeof(Index) * (N + 1) == sizeof(simplices[0])); load_index(reinterpret_cast(simplices), simplex_count, detail::get_primitive_type(), usage); } template - void mesh::load_index(std::vector> const & simplices, GLenum usage) + void mesh::load_index(std::vector> const & simplices, GLenum usage) { load_index(simplices.data(), simplices.size(), usage); } diff --git a/libs/gfx/include/psemek/gfx/obj_parser.hpp b/libs/gfx/include/psemek/gfx/obj_parser.hpp index 49d185ac..986f48a6 100644 --- a/libs/gfx/include/psemek/gfx/obj_parser.hpp +++ b/libs/gfx/include/psemek/gfx/obj_parser.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include @@ -14,12 +14,12 @@ namespace psemek::gfx { struct vertex { - geom::point position; - geom::vector texcoord; - geom::vector normal; + math::point position; + math::vector texcoord; + math::vector normal; }; - std::vector> triangles; + std::vector> triangles; }; obj_data parse_obj(std::istream & is); diff --git a/libs/gfx/include/psemek/gfx/painter.hpp b/libs/gfx/include/psemek/gfx/painter.hpp index e671685c..f86c44c0 100644 --- a/libs/gfx/include/psemek/gfx/painter.hpp +++ b/libs/gfx/include/psemek/gfx/painter.hpp @@ -3,10 +3,10 @@ #include #include -#include -#include -#include -#include +#include +#include +#include +#include #include @@ -42,7 +42,7 @@ namespace psemek::gfx struct text_options { font f = font::font_9x12; - geom::vector scale = {1.f, 1.f}; + math::vector scale = {1.f, 1.f}; x_align x = x_align::center; y_align y = y_align::center; color c = {255, 255, 255, 255}; @@ -52,29 +52,29 @@ namespace psemek::gfx ~painter(); // 2D - void triangle(geom::point const & p0, geom::point const & p1, geom::point const & p2, color const & c); - void triangle(geom::point const & p0, geom::point const & p1, geom::point const & p2, color const & c0, color const & c1, color const & c2); - void rect(geom::box const & box, color const & c); - void circle(geom::point const & center, float radius, color const & c, int quality = 24); - void line(geom::point const & p0, geom::point const & p1, float width, color const & c, bool smooth = true); - void line(geom::point const & p0, geom::point const & p1, float w0, float w1, color const & c0, color const & c1, bool smooth = true); - void besier(geom::point const & p0, geom::point const & p1, geom::point const & p2, float width, color const & c, int quality = 8, bool smooth = true); - void polygon(util::span const> points, color const & c); + void triangle(math::point const & p0, math::point const & p1, math::point const & p2, color const & c); + void triangle(math::point const & p0, math::point const & p1, math::point const & p2, color const & c0, color const & c1, color const & c2); + void rect(math::box const & box, color const & c); + void circle(math::point const & center, float radius, color const & c, int quality = 24); + void line(math::point const & p0, math::point const & p1, float width, color const & c, bool smooth = true); + void line(math::point const & p0, math::point const & p1, float w0, float w1, color const & c0, color const & c1, bool smooth = true); + void besier(math::point const & p0, math::point const & p1, math::point const & p2, float width, color const & c, int quality = 8, bool smooth = true); + void polygon(util::span const> points, color const & c); // 2D text - geom::vector text_size(std::string_view str, font f = font::font_9x12); - void text(geom::point const & p, std::string_view str, text_options const & opts); + math::vector text_size(std::string_view str, font f = font::font_9x12); + void text(math::point const & p, std::string_view str, text_options const & opts); - void texture(texture_2d const & texture, geom::box const & box, color const & c = {0, 0, 0, 0}); + void texture(texture_2d const & texture, math::box const & box, color const & c = {0, 0, 0, 0}); // 3D - void axes(geom::point const & p, float length, float width); - void sphere(geom::point const & p, float radius, color const & c, int quality = 6); - void line3d(geom::point const & p0, geom::point const & p1, float width, color const & c); - void text3d(geom::point const & p, std::string_view str, text_options const & opts, geom::matrix const & transform); + void axes(math::point const & p, float length, float width); + void sphere(math::point const & p, float radius, color const & c, int quality = 6); + void line3d(math::point const & p0, math::point const & p1, float width, color const & c); + void text3d(math::point const & p, std::string_view str, text_options const & opts, math::matrix const & transform); // Should be called on each frame - void render(geom::matrix const & transform); + void render(math::matrix const & transform); private: psemek_declare_pimpl diff --git a/libs/gfx/include/psemek/gfx/pixel.hpp b/libs/gfx/include/psemek/gfx/pixel.hpp index 7c2c857f..6e50360e 100644 --- a/libs/gfx/include/psemek/gfx/pixel.hpp +++ b/libs/gfx/include/psemek/gfx/pixel.hpp @@ -55,7 +55,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RG8; static constexpr GLenum format = gl::RG; @@ -63,7 +63,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGB8; static constexpr GLenum format = gl::RGB; @@ -71,7 +71,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGBA8; static constexpr GLenum format = gl::RGBA; @@ -81,7 +81,7 @@ namespace psemek::gfx // Normalized 8-bit sRGB template <> - struct pixel_traits>> + struct pixel_traits>> { static constexpr GLenum internal_format = gl::SRGB8; static constexpr GLenum format = gl::RGB; @@ -89,7 +89,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits>> + struct pixel_traits>> { static constexpr GLenum internal_format = gl::SRGB8_ALPHA8; static constexpr GLenum format = gl::RGBA; @@ -100,7 +100,7 @@ namespace psemek::gfx #ifndef PSEMEK_GLES template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGB10; static constexpr GLenum format = gl::RGB; @@ -110,7 +110,7 @@ namespace psemek::gfx // Normalized 12-bit template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGB12; static constexpr GLenum format = gl::RGB; @@ -118,7 +118,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGBA12; static constexpr GLenum format = gl::RGBA; @@ -136,7 +136,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RG16; static constexpr GLenum format = gl::RG; @@ -144,7 +144,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGB16; static constexpr GLenum format = gl::RGB; @@ -152,7 +152,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGBA16; static constexpr GLenum format = gl::RGBA; @@ -182,7 +182,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 2>> + struct pixel_traits, 2>> { static constexpr GLenum internal_format = gl::RG8UI; static constexpr GLenum format = gl::RG_INTEGER; @@ -190,7 +190,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 3>> + struct pixel_traits, 3>> { static constexpr GLenum internal_format = gl::RGB8UI; static constexpr GLenum format = gl::RGB_INTEGER; @@ -198,7 +198,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 4>> + struct pixel_traits, 4>> { static constexpr GLenum internal_format = gl::RGBA8UI; static constexpr GLenum format = gl::RGBA_INTEGER; @@ -216,7 +216,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 2>> + struct pixel_traits, 2>> { static constexpr GLenum internal_format = gl::RG16UI; static constexpr GLenum format = gl::RG_INTEGER; @@ -224,7 +224,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 3>> + struct pixel_traits, 3>> { static constexpr GLenum internal_format = gl::RGB16UI; static constexpr GLenum format = gl::RGB_INTEGER; @@ -232,7 +232,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 4>> + struct pixel_traits, 4>> { static constexpr GLenum internal_format = gl::RGBA16UI; static constexpr GLenum format = gl::RGBA_INTEGER; @@ -250,7 +250,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 2>> + struct pixel_traits, 2>> { static constexpr GLenum internal_format = gl::RG32UI; static constexpr GLenum format = gl::RG_INTEGER; @@ -258,7 +258,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 3>> + struct pixel_traits, 3>> { static constexpr GLenum internal_format = gl::RGB32UI; static constexpr GLenum format = gl::RGB_INTEGER; @@ -266,7 +266,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 4>> + struct pixel_traits, 4>> { static constexpr GLenum internal_format = gl::RGBA32UI; static constexpr GLenum format = gl::RGBA_INTEGER; @@ -284,7 +284,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 2>> + struct pixel_traits, 2>> { static constexpr GLenum internal_format = gl::RG8I; static constexpr GLenum format = gl::RG_INTEGER; @@ -292,7 +292,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 3>> + struct pixel_traits, 3>> { static constexpr GLenum internal_format = gl::RGB8I; static constexpr GLenum format = gl::RGB_INTEGER; @@ -300,7 +300,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 4>> + struct pixel_traits, 4>> { static constexpr GLenum internal_format = gl::RGBA8I; static constexpr GLenum format = gl::RGBA_INTEGER; @@ -318,7 +318,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 2>> + struct pixel_traits, 2>> { static constexpr GLenum internal_format = gl::RG16I; static constexpr GLenum format = gl::RG_INTEGER; @@ -326,7 +326,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 3>> + struct pixel_traits, 3>> { static constexpr GLenum internal_format = gl::RGB16I; static constexpr GLenum format = gl::RGB_INTEGER; @@ -334,7 +334,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 4>> + struct pixel_traits, 4>> { static constexpr GLenum internal_format = gl::RGBA16I; static constexpr GLenum format = gl::RGBA_INTEGER; @@ -352,7 +352,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 2>> + struct pixel_traits, 2>> { static constexpr GLenum internal_format = gl::RG32I; static constexpr GLenum format = gl::RG_INTEGER; @@ -360,7 +360,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 3>> + struct pixel_traits, 3>> { static constexpr GLenum internal_format = gl::RGB32I; static constexpr GLenum format = gl::RGB_INTEGER; @@ -368,7 +368,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits, 4>> + struct pixel_traits, 4>> { static constexpr GLenum internal_format = gl::RGBA32I; static constexpr GLenum format = gl::RGBA_INTEGER; @@ -386,7 +386,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RG16F; static constexpr GLenum format = gl::RG; @@ -394,7 +394,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGB16F; static constexpr GLenum format = gl::RGB; @@ -402,7 +402,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGBA16F; static constexpr GLenum format = gl::RGBA; @@ -420,7 +420,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RG32F; static constexpr GLenum format = gl::RG; @@ -428,7 +428,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGB32F; static constexpr GLenum format = gl::RGB; @@ -436,7 +436,7 @@ namespace psemek::gfx }; template <> - struct pixel_traits> + struct pixel_traits> { static constexpr GLenum internal_format = gl::RGBA32F; static constexpr GLenum format = gl::RGBA; diff --git a/libs/gfx/include/psemek/gfx/program.hpp b/libs/gfx/include/psemek/gfx/program.hpp index 61e08409..eda16191 100644 --- a/libs/gfx/include/psemek/gfx/program.hpp +++ b/libs/gfx/include/psemek/gfx/program.hpp @@ -2,11 +2,11 @@ #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include @@ -53,60 +53,60 @@ namespace psemek::gfx void operator = (unsigned int i); void operator = (float f); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); - void operator = (geom::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); + void operator = (math::vector const & v); template - void operator = (geom::vector const & v); + void operator = (math::vector const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); - void operator = (geom::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); + void operator = (math::point const & v); template - void operator = (geom::point const & v); + void operator = (math::point const & v); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); - void operator = (geom::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); + void operator = (math::matrix const & m); - void operator = (geom::interval const & i); - void operator = (geom::interval const & i); - void operator = (geom::interval const & i); + void operator = (math::interval const & i); + void operator = (math::interval const & i); + void operator = (math::interval const & i); template - void operator = (geom::interval const & i); + void operator = (math::interval const & i); - void operator = (geom::quaternion const & q); + void operator = (math::quaternion const & q); private: GLint location_; @@ -129,21 +129,21 @@ namespace psemek::gfx }; template - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { if constexpr (std::is_floating_point_v) { - (*this) = geom::cast(v); + (*this) = math::cast(v); } else if (std::is_integral_v) { if constexpr (std::is_unsigned_v) { - (*this) = geom::cast(v); + (*this) = math::cast(v); } else { - (*this) = geom::cast(v); + (*this) = math::cast(v); } } else @@ -153,21 +153,21 @@ namespace psemek::gfx } template - void program::uniform_proxy::operator = (geom::point const & p) + void program::uniform_proxy::operator = (math::point const & p) { if constexpr (std::is_floating_point_v) { - (*this) = geom::cast(p); + (*this) = math::cast(p); } else if (std::is_integral_v) { if constexpr (std::is_unsigned_v) { - (*this) = geom::cast(p); + (*this) = math::cast(p); } else { - (*this) = geom::cast(p); + (*this) = math::cast(p); } } else @@ -177,21 +177,21 @@ namespace psemek::gfx } template - void program::uniform_proxy::operator = (geom::interval const & i) + void program::uniform_proxy::operator = (math::interval const & i) { if constexpr (std::is_floating_point_v) { - (*this) = geom::cast(i); + (*this) = math::cast(i); } else if (std::is_integral_v) { if constexpr (std::is_unsigned_v) { - (*this) = geom::cast(i); + (*this) = math::cast(i); } else { - (*this) = geom::cast(i); + (*this) = math::cast(i); } } else diff --git a/libs/gfx/include/psemek/gfx/render_target.hpp b/libs/gfx/include/psemek/gfx/render_target.hpp index fa63c7fe..3ea8a757 100644 --- a/libs/gfx/include/psemek/gfx/render_target.hpp +++ b/libs/gfx/include/psemek/gfx/render_target.hpp @@ -2,7 +2,7 @@ #include -#include +#include #include @@ -13,7 +13,7 @@ namespace psemek::gfx { gfx::framebuffer const * framebuffer; GLenum draw_buffer; - geom::box viewport; + math::box viewport; void bind() const { diff --git a/libs/gfx/include/psemek/gfx/renderbuffer.hpp b/libs/gfx/include/psemek/gfx/renderbuffer.hpp index 7f11bdf2..a592924a 100644 --- a/libs/gfx/include/psemek/gfx/renderbuffer.hpp +++ b/libs/gfx/include/psemek/gfx/renderbuffer.hpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -32,31 +32,31 @@ namespace psemek::gfx void reset(); - geom::vector size() const { return size_; } + math::vector size() const { return size_; } std::size_t width() const { return size_[0]; } std::size_t height() const { return size_[1]; } std::optional samples() const { return samples_; } - void storage(GLenum internal_format, geom::vector const & size); - void storage(GLenum internal_format, geom::vector const & size, int samples); + void storage(GLenum internal_format, math::vector const & size); + void storage(GLenum internal_format, math::vector const & size, int samples); template - void storage(geom::vector const & size) + void storage(math::vector const & size) { storage(pixel_traits::internal_format, size); } template - void storage(geom::vector const & size, int samples) + void storage(math::vector const & size, int samples) { storage(pixel_traits::internal_format, size, samples); } private: GLuint id_ = 0; - geom::vector size_ = {0, 0}; + math::vector size_ = {0, 0}; std::optional samples_ = std::nullopt; explicit renderbuffer(std::nullptr_t); diff --git a/libs/gfx/include/psemek/gfx/renderer/deferred.hpp b/libs/gfx/include/psemek/gfx/renderer/deferred.hpp index 530eb87d..3f0c5023 100644 --- a/libs/gfx/include/psemek/gfx/renderer/deferred.hpp +++ b/libs/gfx/include/psemek/gfx/renderer/deferred.hpp @@ -6,8 +6,8 @@ #include #include -#include -#include +#include +#include #include #include @@ -63,19 +63,19 @@ namespace psemek::gfx gfx::mesh const * mesh = nullptr; material const * mat = nullptr; - geom::box bbox; - std::optional> pre_transform; - std::optional> post_transform; + math::box bbox; + std::optional> pre_transform; + std::optional> post_transform; }; struct directional_light { color_3f color; - geom::vector direction; + math::vector direction; bool shadowed = true; std::size_t shadow_map_size = 1024; std::size_t cascades = 4; - std::vector> cascade_ranges; + std::vector> cascade_ranges; }; struct point_light @@ -86,7 +86,7 @@ namespace psemek::gfx struct { float c0, c1, c2; } attenuation; - geom::point position; + math::point position; bool shadowed = true; float min_shadow_distance; std::size_t shadow_map_size = 1024; @@ -94,7 +94,7 @@ namespace psemek::gfx struct options { - geom::camera const * camera; + math::camera const * camera; std::optional clear_color; util::function background_generator; @@ -125,7 +125,7 @@ namespace psemek::gfx std::optional ssao; - geom::vector grid_size{1, 1, 1}; + math::vector grid_size{1, 1, 1}; }; void render(std::vector const & objects, render_target const & target, options const & opts); diff --git a/libs/gfx/include/psemek/gfx/renderer/simple.hpp b/libs/gfx/include/psemek/gfx/renderer/simple.hpp index 8284293f..2104c3c1 100644 --- a/libs/gfx/include/psemek/gfx/renderer/simple.hpp +++ b/libs/gfx/include/psemek/gfx/renderer/simple.hpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include @@ -37,7 +37,7 @@ namespace psemek::gfx struct render_options { - geom::matrix transform; + math::matrix transform; }; simple_renderer(); diff --git a/libs/gfx/include/psemek/gfx/texture.hpp b/libs/gfx/include/psemek/gfx/texture.hpp index 5e2a698f..88cb4f09 100644 --- a/libs/gfx/include/psemek/gfx/texture.hpp +++ b/libs/gfx/include/psemek/gfx/texture.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include @@ -39,19 +39,19 @@ namespace psemek::gfx GLint internal_format() const { return format_; } - geom::vector size() const { return size_; } + math::vector size() const { return size_; } std::size_t width() const; std::size_t height() const; std::size_t depth() const; - void load(GLint internal_format, geom::vector const & size, GLenum format, GLenum type, const void * data); + void load(GLint internal_format, math::vector const & size, GLenum format, GLenum type, const void * data); template - void load(geom::vector const & size, Pixel const * data = nullptr); + void load(math::vector const & size, Pixel const * data = nullptr); template - void load_srgb(geom::vector const & size, Pixel const * data = nullptr); + void load_srgb(math::vector const & size, Pixel const * data = nullptr); template void load(util::array const & p); @@ -69,7 +69,7 @@ namespace psemek::gfx Pixmap pixels(int layer = 0) const; #endif - static basic_texture from_data(GLint internal_format, geom::vector const & size, GLenum format, GLenum type, const void * data); + static basic_texture from_data(GLint internal_format, math::vector const & size, GLenum format, GLenum type, const void * data); template static basic_texture from_pixmap(Pixmap const & p); @@ -89,7 +89,7 @@ namespace psemek::gfx protected: GLuint id_ = 0; GLint format_ = 0; - geom::vector size_ = geom::vector::zero(); + math::vector size_ = math::vector::zero(); bool uses_mipmaps_ = true; explicit basic_texture(std::nullptr_t); @@ -132,10 +132,10 @@ namespace psemek::gfx static GLenum face_to_gl(int f); - void load(int f, GLint internal_format, geom::vector const & size, GLenum format, GLenum type, const void * data); + void load(int f, GLint internal_format, math::vector const & size, GLenum format, GLenum type, const void * data); template - void load(int f, geom::vector const & size, Pixel const * data = nullptr) + void load(int f, math::vector const & size, Pixel const * data = nullptr) { using traits = pixel_traits; load(f, traits::internal_format, size, traits::format, traits::type, data); @@ -144,7 +144,7 @@ namespace psemek::gfx template void load(int f, util::array const & p) { - geom::vector size; + math::vector size; for (std::size_t i = 0; i < 2; ++i) size[i] = p.dim(i); load(f, size, p.data()); } @@ -194,21 +194,21 @@ namespace psemek::gfx void bind() const; void bind(int texture_unit) const; - geom::vector size() const { return size_; } + math::vector size() const { return size_; } std::size_t width() const { return size_[0]; } std::size_t height() const { return size_[1]; } int samples() const { return samples_; } - void load(GLint internal_format, geom::vector const & size, int samples, bool fixed_sample_locations = true); + void load(GLint internal_format, math::vector const & size, int samples, bool fixed_sample_locations = true); template - void load(geom::vector const & size, int samples, bool fixed_sample_locations = true); + void load(math::vector const & size, int samples, bool fixed_sample_locations = true); protected: GLuint id_ = 0; - geom::vector size_ = {0, 0}; + math::vector size_ = {0, 0}; int samples_ = 0; explicit texture_2d_multisample(std::nullptr_t); @@ -351,7 +351,7 @@ namespace psemek::gfx } template - void basic_texture::load(GLint internal_format, geom::vector const & size, GLenum format, GLenum type, const void * data) + void basic_texture::load(GLint internal_format, math::vector const & size, GLenum format, GLenum type, const void * data) { bind(); @@ -376,7 +376,7 @@ namespace psemek::gfx template template - void basic_texture::load(geom::vector const & size, Pixel const * data) + void basic_texture::load(math::vector const & size, Pixel const * data) { using traits = pixel_traits; load(traits::internal_format, size, traits::format, traits::type, data); @@ -384,7 +384,7 @@ namespace psemek::gfx template template - void basic_texture::load_srgb(geom::vector const & size, Pixel const * data) + void basic_texture::load_srgb(math::vector const & size, Pixel const * data) { using traits = pixel_traits>; load(traits::internal_format, size, traits::format, traits::type, data); @@ -394,7 +394,7 @@ namespace psemek::gfx template void basic_texture::load(util::array const & p) { - geom::vector size; + math::vector size; for (std::size_t i = 0; i < D; ++i) size[i] = p.dim(i); load(size, p.data()); } @@ -403,7 +403,7 @@ namespace psemek::gfx template void basic_texture::load_srgb(util::array const & p) { - geom::vector size; + math::vector size; for (std::size_t i = 0; i < D; ++i) size[i] = p.dim(i); load_srgb(size, p.data()); } @@ -439,7 +439,7 @@ namespace psemek::gfx #endif template - basic_texture basic_texture::from_data(GLint internal_format, geom::vector const & size, GLenum format, GLenum type, const void * data) + basic_texture basic_texture::from_data(GLint internal_format, math::vector const & size, GLenum format, GLenum type, const void * data) { basic_texture result; result.load(internal_format, size, format, type, data); @@ -537,7 +537,7 @@ namespace psemek::gfx #ifndef PSEMEK_GLES template - void texture_2d_multisample::load(geom::vector const & size, int samples, bool fixed_sample_locations) + void texture_2d_multisample::load(math::vector const & size, int samples, bool fixed_sample_locations) { load(pixel_traits::internal_format, size, samples, fixed_sample_locations); } @@ -611,7 +611,7 @@ namespace psemek::gfx {} template - std::size_t mipmap_count(geom::vector const & size) + std::size_t mipmap_count(math::vector const & size) { std::size_t s = 0; for (std::size_t i = 0; i < D; ++i) diff --git a/libs/gfx/include/psemek/gfx/texture_view.hpp b/libs/gfx/include/psemek/gfx/texture_view.hpp index 6b714db6..7f04ca17 100644 --- a/libs/gfx/include/psemek/gfx/texture_view.hpp +++ b/libs/gfx/include/psemek/gfx/texture_view.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include @@ -12,12 +12,12 @@ namespace psemek::gfx struct basic_texture_view { basic_texture const * texture = nullptr; - geom::box part; // in pixels + math::box part; // in pixels basic_texture_view(basic_texture const * texture = nullptr); - basic_texture_view(basic_texture const * texture, geom::box const & part); + basic_texture_view(basic_texture const * texture, math::box const & part); - geom::vector size() const { return part.dimensions(); } + math::vector size() const { return part.dimensions(); } float width() const { @@ -49,13 +49,13 @@ namespace psemek::gfx { if (texture) { - static const auto zero = geom::point::zero(); - part = geom::span(zero, zero + geom::cast(texture->size())); + static const auto zero = math::point::zero(); + part = math::span(zero, zero + math::cast(texture->size())); } } template - basic_texture_view::basic_texture_view(basic_texture const * texture, geom::box const & part) + basic_texture_view::basic_texture_view(basic_texture const * texture, math::box const & part) : texture(texture) , part(part) {} diff --git a/libs/gfx/source/effect/blur.cpp b/libs/gfx/source/effect/blur.cpp index 158ae7c5..ee4fa66d 100644 --- a/libs/gfx/source/effect/blur.cpp +++ b/libs/gfx/source/effect/blur.cpp @@ -136,7 +136,7 @@ void main() src.bind(); array.bind(); program.bind(); - program["u_inv_texture_size"] = geom::vector{1.f / src.width(), 1.f / src.height()}; + program["u_inv_texture_size"] = math::vector{1.f / src.width(), 1.f / src.height()}; gl::DrawArrays(gl::TRIANGLES, 0, 6); } }; diff --git a/libs/gfx/source/effect/fxaa.cpp b/libs/gfx/source/effect/fxaa.cpp index 6cb4355a..8ee24417 100644 --- a/libs/gfx/source/effect/fxaa.cpp +++ b/libs/gfx/source/effect/fxaa.cpp @@ -231,7 +231,7 @@ void main() src.bind(); impl().array.bind(); impl().program.bind(); - impl().program["u_d"] = geom::vector{1.f / src.width(), 1.f / src.height()}; + impl().program["u_d"] = math::vector{1.f / src.width(), 1.f / src.height()}; gl::DrawArrays(gl::TRIANGLES, 0, 6); } diff --git a/libs/gfx/source/fullscreen.cpp b/libs/gfx/source/fullscreen.cpp index 5b7edb17..2f23251b 100644 --- a/libs/gfx/source/fullscreen.cpp +++ b/libs/gfx/source/fullscreen.cpp @@ -7,7 +7,7 @@ namespace psemek::gfx { mesh m; - std::vector> vertices(4); + std::vector> vertices(4); std::vector indices(6); vertices[0] = {-1.f, -1.f}; @@ -22,7 +22,7 @@ namespace psemek::gfx indices[4] = 1; indices[5] = 3; - m.setup>(); + m.setup>(); m.load(vertices, indices, gl::STATIC_DRAW); return m; diff --git a/libs/gfx/source/gltf_animation.cpp b/libs/gfx/source/gltf_animation.cpp index a9c9fbc4..bfd28800 100644 --- a/libs/gfx/source/gltf_animation.cpp +++ b/libs/gfx/source/gltf_animation.cpp @@ -6,23 +6,23 @@ namespace psemek::gfx namespace detail { - void gltf_animation_traits::canonicalize(geom::easing_type interpolation, std::vector & output) + void gltf_animation_traits::canonicalize(math::easing_type interpolation, std::vector & output) { - if (interpolation == geom::easing_type::linear) + if (interpolation == math::easing_type::linear) { for (std::size_t i = 1; i < output.size(); ++i) { - if (geom::dot(output[i - 1].coords, output[i].coords) < 0.f) + if (math::dot(output[i - 1].coords, output[i].coords) < 0.f) { output[i] = - output[i]; } } } - else if (interpolation == geom::easing_type::cubic) + else if (interpolation == math::easing_type::cubic) { for (std::size_t i = 3; i < output.size(); i += 3) { - if (geom::dot(output[(i - 3) + 1].coords, output[i + 1].coords) < 0.f) + if (math::dot(output[(i - 3) + 1].coords, output[i + 1].coords) < 0.f) { output[i + 0] = - output[i + 0]; output[i + 1] = - output[i + 1]; @@ -40,14 +40,14 @@ namespace psemek::gfx auto it = std::lower_bound(input_.begin(), input_.end(), time); if (it == input_.begin()) { - if (interpolation_ == geom::easing_type::cubic) + if (interpolation_ == math::easing_type::cubic) return output_[1]; else return output_.front(); } if (it == input_.end()) { - if (interpolation_ == geom::easing_type::cubic) + if (interpolation_ == math::easing_type::cubic) return output_[output_.size() - 2]; else return output_.back(); @@ -60,11 +60,11 @@ namespace psemek::gfx switch (interpolation_) { - case geom::easing_type::constant_left: + case math::easing_type::constant_left: return output_[i - 1]; - case geom::easing_type::linear: + case math::easing_type::linear: return traits::lerp(output_[i - 1], output_[i], t); - case geom::easing_type::cubic: + case math::easing_type::cubic: { // see https://github.khronos.org/glTF-Tutorials/gltfTutorial/gltfTutorial_007_Animations.html#cubic-spline-interpolation float t2 = t * t; @@ -85,16 +85,16 @@ namespace psemek::gfx template struct gltf_animation_channel; template struct gltf_animation_channel; - geom::interval gltf_animation::range() const + math::interval gltf_animation::range() const { return scale_.range() | rotation_.range() | translation_.range(); } - geom::affine_transform gltf_animation::operator()(float time) const + math::affine_transform gltf_animation::operator()(float time) const { - return geom::translation(translation_(time)).transform() - * geom::quaternion_rotation(rotation_(time)).transform() - * geom::scale(scale_(time)).transform(); + return math::translation(translation_(time)).transform() + * math::quaternion_rotation(rotation_(time)).transform() + * math::scale(scale_(time)).transform(); } } diff --git a/libs/gfx/source/gltf_mesh.cpp b/libs/gfx/source/gltf_mesh.cpp index 035929b9..d155a025 100644 --- a/libs/gfx/source/gltf_mesh.cpp +++ b/libs/gfx/source/gltf_mesh.cpp @@ -20,8 +20,8 @@ namespace psemek::gfx std::size_t index_offset; GLenum index_type; - std::vector> vertices; - std::vector> triangles; + std::vector> vertices; + std::vector> triangles; void draw() const override { @@ -65,8 +65,8 @@ namespace psemek::gfx { std::vector primitives; gltf_asset::extras_map extras; - std::vector> vertices; - std::vector> triangles; + std::vector> vertices; + std::vector> triangles; }; std::vector materials_; @@ -170,7 +170,7 @@ namespace psemek::gfx } if (primitive.position) - for (auto const & p : accessor_range>(asset, *primitive.position)) + for (auto const & p : accessor_range>(asset, *primitive.position)) drawable.vertices.push_back(p); target_mesh.primitives.push_back({&drawable, primitive.material, drawable.vertices, drawable.triangles}); diff --git a/libs/gfx/source/gltf_parser.cpp b/libs/gfx/source/gltf_parser.cpp index 6f233dfe..7130c844 100644 --- a/libs/gfx/source/gltf_parser.cpp +++ b/libs/gfx/source/gltf_parser.cpp @@ -1,7 +1,7 @@ #include -#include -#include -#include +#include +#include +#include #include #include #include @@ -158,7 +158,7 @@ namespace psemek::gfx target.translation[i] = translation[i].GetFloat(); } - target.rotation = geom::quaternion::identity(); + target.rotation = math::quaternion::identity(); if (node.HasMember("rotation")) { auto const & rotation = node["rotation"].GetArray(); @@ -174,9 +174,9 @@ namespace psemek::gfx target.scale[i] = scale[i].GetFloat(); } - target.transform = geom::translation(target.translation).transform() - * geom::quaternion_rotation(target.rotation).transform() - * geom::scale(target.scale).transform(); + target.transform = math::translation(target.translation).transform() + * math::quaternion_rotation(target.rotation).transform() + * math::scale(target.scale).transform(); if (node.HasMember("children")) { @@ -346,11 +346,11 @@ namespace psemek::gfx std::string interpolation{sampler["interpolation"].GetString()}; if (interpolation == "STEP") - sampler_target.interpolation = geom::easing_type::constant_left; + sampler_target.interpolation = math::easing_type::constant_left; else if (interpolation == "LINEAR") - sampler_target.interpolation = geom::easing_type::linear; + sampler_target.interpolation = math::easing_type::linear; else if (interpolation == "CUBICSPLINE") - sampler_target.interpolation = geom::easing_type::cubic; + sampler_target.interpolation = math::easing_type::cubic; else throw util::exception("Unknown glTF animation interpolation type: " + interpolation); } @@ -403,7 +403,7 @@ namespace psemek::gfx target.color = {1.f, 1.f, 1.f}; target.intensity = 1.f; target.range = std::numeric_limits::infinity(); - target.cone_angle = {0.f, geom::pi / 4.f}; + target.cone_angle = {0.f, math::pi / 4.f}; std::string type = light["type"].GetString(); diff --git a/libs/gfx/source/mesh.cpp b/libs/gfx/source/mesh.cpp index faafe4b0..455225b0 100644 --- a/libs/gfx/source/mesh.cpp +++ b/libs/gfx/source/mesh.cpp @@ -243,27 +243,27 @@ namespace psemek::gfx vertex_format = s.read(); if (vertex_format & POSITION_MASK) - result.attribs += make_attribs_description>(); + result.attribs += make_attribs_description>(); else result.attribs += make_attribs_description(); if (vertex_format & NORMALS_MASK) - result.attribs += make_attribs_description>(); + result.attribs += make_attribs_description>(); else result.attribs += make_attribs_description(); if (vertex_format & COLORS_MASK) - result.attribs += make_attribs_description>>(); + result.attribs += make_attribs_description>>(); else result.attribs += make_attribs_description(); if (vertex_format & TEXCOORDS_MASK) - result.attribs += make_attribs_description>(); + result.attribs += make_attribs_description>(); else result.attribs += make_attribs_description(); if (vertex_format & WEIGHTS_MASK) - result.attribs += make_attribs_description>, gfx::normalized>>(); + result.attribs += make_attribs_description>, gfx::normalized>>(); else result.attribs += make_attribs_description(); diff --git a/libs/gfx/source/obj_parser.cpp b/libs/gfx/source/obj_parser.cpp index 7054946e..13161f48 100644 --- a/libs/gfx/source/obj_parser.cpp +++ b/libs/gfx/source/obj_parser.cpp @@ -11,9 +11,9 @@ namespace psemek::gfx obj_data parse_obj(std::istream & is) { - std::vector> positions; - std::vector> texcoords; - std::vector> normals; + std::vector> positions; + std::vector> texcoords; + std::vector> normals; obj_data result; diff --git a/libs/gfx/source/painter.cpp b/libs/gfx/source/painter.cpp index 31a6cac5..051d9db1 100644 --- a/libs/gfx/source/painter.cpp +++ b/libs/gfx/source/painter.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include @@ -113,21 +113,21 @@ namespace psemek::gfx { struct vertex { - geom::point position; + math::point position; color_rgba color; }; struct text_vertex { - geom::point position; + math::point position; color_rgba color; - geom::point texcoord; + math::point texcoord; }; struct texture_vertex { - geom::point position; - geom::point texcoord; + math::point position; + math::point texcoord; }; gfx::program program{shader_prefix + vertex_source, shader_prefix + fragment_source}; @@ -157,9 +157,9 @@ namespace psemek::gfx impl() { - mesh.setup, gfx::normalized>(); - text_mesh.setup, gfx::normalized, geom::point>(); - texture_mesh.setup, gfx::normalized>>(); + mesh.setup, gfx::normalized>(); + text_mesh.setup, gfx::normalized, math::point>(); + texture_mesh.setup, gfx::normalized>>(); font_texture.load(gfx::read_image(io::memory_istream{resource::font_9x12_png.data})); @@ -180,12 +180,12 @@ namespace psemek::gfx painter::~painter() = default; - void painter::triangle(geom::point const & p0, geom::point const & p1, geom::point const & p2, color const & c) + void painter::triangle(math::point const & p0, math::point const & p1, math::point const & p2, color const & c) { triangle(p0, p1, p2, c, c, c); } - void painter::triangle(geom::point const & p0, geom::point const & p1, geom::point const & p2, color const & c0, color const & c1, color const & c2) + void painter::triangle(math::point const & p0, math::point const & p1, math::point const & p2, color const & c0, color const & c1, color const & c2) { std::uint32_t const base = impl().vertices.size(); @@ -198,7 +198,7 @@ namespace psemek::gfx impl().indices.push_back(base + 2); } - void painter::rect(geom::box const & box, color const & c) + void painter::rect(math::box const & box, color const & c) { std::uint32_t const base = impl().vertices.size(); @@ -215,14 +215,14 @@ namespace psemek::gfx impl().indices.push_back(base + 2); } - void painter::circle(geom::point const & p, float r, color const & c, int quality) + void painter::circle(math::point const & p, float r, color const & c, int quality) { std::uint32_t const base = impl().vertices.size(); impl().vertices.push_back({{p[0], p[1], 0.f}, c}); for (int i = 0; i < quality; ++i) { - float const a = (geom::pi * 2.f * i) / quality; + float const a = (math::pi * 2.f * i) / quality; impl().vertices.push_back({{p[0] + r * std::cos(a), p[1] + r * std::sin(a), 0.f}, c}); } @@ -234,19 +234,19 @@ namespace psemek::gfx } } - void painter::line(geom::point const & p0, geom::point const & p1, float width, color const & c, bool smooth) + void painter::line(math::point const & p0, math::point const & p1, float width, color const & c, bool smooth) { line(p0, p1, width, width, c, c, smooth); } - void painter::line(geom::point const & p0, geom::point const & p1, float width0, float width1, color const & c0, color const & c1, bool smooth) + void painter::line(math::point const & p0, math::point const & p1, float width0, float width1, color const & c0, color const & c1, bool smooth) { std::uint32_t const base = impl().vertices.size(); float const r0 = width0 / 2.f; float const r1 = width1 / 2.f; - auto const d = geom::normalized(p1 - p0); - geom::vector const o { -d[1], d[0] }; + auto const d = math::normalized(p1 - p0); + math::vector const o { -d[1], d[0] }; impl().vertices.push_back({{p0[0] + r0 * o[0], p0[1] + r0 * o[1], 0.f}, c0}); impl().vertices.push_back({{p0[0] - r0 * o[0], p0[1] - r0 * o[1], 0.f}, c0}); @@ -267,7 +267,7 @@ namespace psemek::gfx } } - void painter::besier(geom::point const & p0, geom::point const & p1, geom::point const & p2, float width, color const & c, int quality, bool smooth) + void painter::besier(math::point const & p0, math::point const & p1, math::point const & p2, float width, color const & c, int quality, bool smooth) { auto at = [&](float t) { @@ -283,7 +283,7 @@ namespace psemek::gfx } } - void painter::polygon(util::span const> points, color const & c) + void painter::polygon(util::span const> points, color const & c) { auto dcel = cg::ear_clipping(points.begin(), points.end()); @@ -298,10 +298,10 @@ namespace psemek::gfx } } - geom::vector painter::text_size(std::string_view str, font f) + math::vector painter::text_size(std::string_view str, font f) { // TODO: multiline text - geom::vector s; + math::vector s; switch (f) { case font::font_9x12: @@ -315,12 +315,12 @@ namespace psemek::gfx return s; } - void painter::text(geom::point const & p, std::string_view str, text_options const & opts) + void painter::text(math::point const & p, std::string_view str, text_options const & opts) { - text3d(geom::point{p[0], p[1], 0.f}, str, opts, geom::matrix::identity()); + text3d(math::point{p[0], p[1], 0.f}, str, opts, math::matrix::identity()); } - void painter::texture(gfx::texture_2d const & texture, geom::box const & box, color const & c) + void painter::texture(gfx::texture_2d const & texture, math::box const & box, color const & c) { impl::texture_render_data data; data.texture = &texture; @@ -339,9 +339,9 @@ namespace psemek::gfx impl().textures.push_back(data); } - void painter::axes(geom::point const & p, float length, float width) + void painter::axes(math::point const & p, float length, float width) { - geom::vector const d[3] + math::vector const d[3] { { 1.f, 0.f, 0.f }, { 0.f, 1.f, 0.f }, @@ -395,24 +395,24 @@ namespace psemek::gfx impl().indices.push_back(base + 1); } - void painter::sphere(geom::point const & p, float radius, color const & c, int quality) + void painter::sphere(math::point const & p, float radius, color const & c, int quality) { std::uint32_t const base = impl().vertices.size(); - impl().vertices.push_back({p - geom::vector{0.f, 0.f, radius}, c}); - impl().vertices.push_back({p + geom::vector{0.f, 0.f, radius}, c}); + impl().vertices.push_back({p - math::vector{0.f, 0.f, radius}, c}); + impl().vertices.push_back({p + math::vector{0.f, 0.f, radius}, c}); auto const ray = [](float ax, float ay) { - return geom::vector{std::cos(ax) * std::cos(ay), std::sin(ax) * std::cos(ay), std::sin(ay)}; + return math::vector{std::cos(ax) * std::cos(ay), std::sin(ax) * std::cos(ay), std::sin(ay)}; }; for (int y = - quality + 1; y < quality; ++y) { for (int x = 0; x < quality * 4; ++x) { - float ax = (x * geom::pi) / quality / 2.f; - float ay = (y * geom::pi) / quality / 2.f; + float ax = (x * math::pi) / quality / 2.f; + float ay = (y * math::pi) / quality / 2.f; impl().vertices.push_back({p + radius * ray(ax, ay), c}); } @@ -464,13 +464,13 @@ namespace psemek::gfx } } - void painter::line3d(geom::point const & p0, geom::point const & p1, float width, color const & c) + void painter::line3d(math::point const & p0, math::point const & p1, float width, color const & c) { std::uint32_t const base = impl().vertices.size(); float const r = width / 2.f; - auto const d = geom::normalized(p1 - p0); - geom::vector const o = geom::normalized(geom::vector{ -d[1], d[0], 0.f }); + auto const d = math::normalized(p1 - p0); + math::vector const o = math::normalized(math::vector{ -d[1], d[0], 0.f }); impl().vertices.push_back({{p0[0] + r * o[0], p0[1] + r * o[1], p0[2]}, c}); impl().vertices.push_back({{p0[0] - r * o[0], p0[1] - r * o[1], p0[2]}, c}); @@ -485,11 +485,11 @@ namespace psemek::gfx impl().indices.push_back(base + 2); } - void painter::text3d(geom::point const & p, std::string_view str, text_options const & opts, geom::matrix const & t) + void painter::text3d(math::point const & p, std::string_view str, text_options const & opts, math::matrix const & t) { - auto const size = geom::pointwise_mult(text_size(str, opts.f), opts.scale); + auto const size = math::pointwise_mult(text_size(str, opts.f), opts.scale); - geom::vector pen { 0.f, 0.f, 0.f }; + math::vector pen { 0.f, 0.f, 0.f }; switch (opts.x) { @@ -519,12 +519,12 @@ namespace psemek::gfx throw util::unknown_enum_value_exception(opts.y); } - geom::vector const sx = {9.f * opts.scale[0], 0.f, 0.f}; - geom::vector const sy = {0.f, 12.f * opts.scale[1], 0.f}; + math::vector const sx = {9.f * opts.scale[0], 0.f, 0.f}; + math::vector const sy = {0.f, 12.f * opts.scale[1], 0.f}; auto to_texcoord = [](int tx, int ty, int ix, int iy) { - return geom::point{ tx * 11 + (ix == 0 ? 1 : 10), ty * 14 + (iy == 0 ? 1 : 13) }; + return math::point{ tx * 11 + (ix == 0 ? 1 : 10), ty * 14 + (iy == 0 ? 1 : 13) }; }; for (char c : str) @@ -568,7 +568,7 @@ namespace psemek::gfx } } - void painter::render(geom::matrix const & transform) + void painter::render(math::matrix const & transform) { gl::ActiveTexture(gl::TEXTURE0); diff --git a/libs/gfx/source/program.cpp b/libs/gfx/source/program.cpp index 61dd387a..05668990 100644 --- a/libs/gfx/source/program.cpp +++ b/libs/gfx/source/program.cpp @@ -31,187 +31,187 @@ namespace psemek::gfx gl::Uniform1f(location_, f); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform1i(location_, v[0]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform2i(location_, v[0], v[1]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform3i(location_, v[0], v[1], v[2]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform4i(location_, v[0], v[1], v[2], v[3]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform1ui(location_, v[0]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform2ui(location_, v[0], v[1]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform3ui(location_, v[0], v[1], v[2]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform4ui(location_, v[0], v[1], v[2], v[3]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform1f(location_, v[0]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform2f(location_, v[0], v[1]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform3f(location_, v[0], v[1], v[2]); } - void program::uniform_proxy::operator = (geom::vector const & v) + void program::uniform_proxy::operator = (math::vector const & v) { gl::Uniform4f(location_, v[0], v[1], v[2], v[3]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform1i(location_, v[0]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform2i(location_, v[0], v[1]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform3i(location_, v[0], v[1], v[2]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform4i(location_, v[0], v[1], v[2], v[3]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform1ui(location_, v[0]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform2ui(location_, v[0], v[1]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform3ui(location_, v[0], v[1], v[2]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform4ui(location_, v[0], v[1], v[2], v[3]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform1f(location_, v[0]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform2f(location_, v[0], v[1]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform3f(location_, v[0], v[1], v[2]); } - void program::uniform_proxy::operator = (geom::point const & v) + void program::uniform_proxy::operator = (math::point const & v) { gl::Uniform4f(location_, v[0], v[1], v[2], v[3]); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix2fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix3x2fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix4x2fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix2x3fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix3fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix4x3fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix2x4fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix3x4fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::matrix const & m) + void program::uniform_proxy::operator = (math::matrix const & m) { gl::UniformMatrix4fv(location_, 1, gl::TRUE, m.coords); } - void program::uniform_proxy::operator = (geom::interval const & i) + void program::uniform_proxy::operator = (math::interval const & i) { gl::Uniform2i(location_, i.min, i.max); } - void program::uniform_proxy::operator = (geom::interval const & i) + void program::uniform_proxy::operator = (math::interval const & i) { gl::Uniform2ui(location_, i.min, i.max); } - void program::uniform_proxy::operator = (geom::interval const & i) + void program::uniform_proxy::operator = (math::interval const & i) { gl::Uniform2f(location_, i.min, i.max); } - void program::uniform_proxy::operator = (geom::quaternion const & q) + void program::uniform_proxy::operator = (math::quaternion const & q) { (*this) = q.coords; } diff --git a/libs/gfx/source/renderbuffer.cpp b/libs/gfx/source/renderbuffer.cpp index 64f52a37..d19a637b 100644 --- a/libs/gfx/source/renderbuffer.cpp +++ b/libs/gfx/source/renderbuffer.cpp @@ -47,7 +47,7 @@ namespace psemek::gfx id_ = 0; } - void renderbuffer::storage(GLenum internal_format, geom::vector const & size) + void renderbuffer::storage(GLenum internal_format, math::vector const & size) { bind(); gl::RenderbufferStorage(target, internal_format, size[0], size[1]); @@ -55,7 +55,7 @@ namespace psemek::gfx samples_ = std::nullopt; } - void renderbuffer::storage(GLenum internal_format, geom::vector const & size, int samples) + void renderbuffer::storage(GLenum internal_format, math::vector const & size, int samples) { bind(); gl::RenderbufferStorageMultisample(target, samples, internal_format, size[0], size[1]); diff --git a/libs/gfx/source/renderer/deferred.cpp b/libs/gfx/source/renderer/deferred.cpp index d24ac39a..03d6be49 100644 --- a/libs/gfx/source/renderer/deferred.cpp +++ b/libs/gfx/source/renderer/deferred.cpp @@ -14,11 +14,11 @@ #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -758,9 +758,9 @@ R"(#version 330 void main(){} )"; - static std::size_t bbox_to_screen_fan(geom::matrix const & camera_transform, geom::box const & b, geom::point * result) + static std::size_t bbox_to_screen_fan(math::matrix const & camera_transform, math::box const & b, math::point * result) { - geom::point bbox_corners_screen[8]; + math::point bbox_corners_screen[8]; auto bbox_corners_screen_end = bbox_corners_screen; bool need_clipping = false; @@ -770,12 +770,12 @@ void main(){} { for (int x = 0; x < 2; ++x) { - geom::point p; - p[0] = geom::lerp(b[0], x); - p[1] = geom::lerp(b[1], y); - p[2] = geom::lerp(b[2], z); + math::point p; + p[0] = math::lerp(b[0], x); + p[1] = math::lerp(b[1], y); + p[2] = math::lerp(b[2], z); - auto q = camera_transform * geom::homogeneous(p); + auto q = camera_transform * math::homogeneous(p); if (q[2] < -q[3] || q[2] > q[3]) { @@ -797,7 +797,7 @@ void main(){} return 4; } - geom::point * bbox_hull_screen_it[8]; + math::point * bbox_hull_screen_it[8]; auto bbox_hull_size = cg::graham_convex_hull(bbox_corners_screen, bbox_corners_screen_end, bbox_hull_screen_it) - bbox_hull_screen_it; for (std::size_t i = 0; i < bbox_hull_size; ++i) @@ -837,7 +837,7 @@ void main(){} // Only albedo & depth attached gfx::framebuffer transparent_framebuffer; - std::optional> g_buffer_size; + std::optional> g_buffer_size; gfx::framebuffer directional_shadow_framebuffer; gfx::texture_2d_array directional_shadow_texture; @@ -849,7 +849,7 @@ void main(){} std::map, vblur> vblur_container; overlay bloom_overlay; - std::optional> bloom_size; + std::optional> bloom_size; std::optional bloom_downsample; // 0: original, unblurred bloom @@ -860,7 +860,7 @@ void main(){} gfx::texture_2d ssao_rotation_texture; - std::optional> ssao_size; + std::optional> ssao_size; std::optional ssao_downsample; // 0: original, unblurred ssao @@ -876,7 +876,7 @@ void main(){} std::vector queries; gfx::array box_array; - std::optional> prev_transform; + std::optional> prev_transform; }; deferred_renderer::deferred_renderer() @@ -934,8 +934,8 @@ void main(){} random::generator rng{0xaa8e8eabull, 0x44a700e7ull}; { - random::uniform_hemiball_vector_distribution d{geom::vector{0.f, 0.f, 1.f}}; - std::vector> ssao_kernel(32); + random::uniform_hemiball_vector_distribution d{math::vector{0.f, 0.f, 1.f}}; + std::vector> ssao_kernel(32); for (auto & v : ssao_kernel) v = d(rng); @@ -949,7 +949,7 @@ void main(){} { random::uniform_sphere_vector_distribution d; - basic_pixmap> pm({8, 8}); + basic_pixmap> pm({8, 8}); for (auto & v : pm) v = d(rng); @@ -975,7 +975,7 @@ void main(){} impl().ssao_pass_program["u_g3"] = 3; impl().ssao_pass_program["u_ssao_rotation"] = 4; - impl().screen_mesh.setup>(); + impl().screen_mesh.setup>(); } deferred_renderer::~deferred_renderer() = default; @@ -1047,12 +1047,12 @@ void main(){} struct bin { util::hash_map, objects_bucket> buckets; - geom::box bbox; + math::box bbox; bool contains_near_clip = false; float camera_separation; }; - geom::box all_bbox; + math::box all_bbox; std::vector camera_distance(objects.size()); for (std::size_t i = 0; i < objects.size(); ++i) @@ -1071,8 +1071,8 @@ void main(){} util::array bins({opts.grid_size[0], opts.grid_size[1], opts.grid_size[2]}); - geom::box lit_bbox; - geom::box casts_shadow_bbox; + math::box lit_bbox; + math::box casts_shadow_bbox; for (std::size_t i = 0; i < objects.size(); ++i) { @@ -1090,9 +1090,9 @@ void main(){} auto c = o.bbox.center() - all_bbox.corner(0, 0, 0); - int bx = geom::clamp(std::floor(c[0] / all_bbox[0].length() * opts.grid_size[0]), {0, opts.grid_size[0] - 1}); - int by = geom::clamp(std::floor(c[1] / all_bbox[1].length() * opts.grid_size[1]), {0, opts.grid_size[1] - 1}); - int bz = geom::clamp(std::floor(c[2] / all_bbox[2].length() * opts.grid_size[2]), {0, opts.grid_size[2] - 1}); + int bx = math::clamp(std::floor(c[0] / all_bbox[0].length() * opts.grid_size[0]), {0, opts.grid_size[0] - 1}); + int by = math::clamp(std::floor(c[1] / all_bbox[1].length() * opts.grid_size[1]), {0, opts.grid_size[1] - 1}); + int bz = math::clamp(std::floor(c[2] / all_bbox[2].length() * opts.grid_size[2]), {0, opts.grid_size[2] - 1}); bin & b = bins(bx, by, bz); @@ -1105,13 +1105,13 @@ void main(){} for (auto & b : bins) { - b.bbox = geom::expand(b.bbox, b.bbox.dimensions() / 64.f); + b.bbox = math::expand(b.bbox, b.bbox.dimensions() / 64.f); b.camera_separation = cg::separation(camera_frustum, cg::box{b.bbox}).distance; b.contains_near_clip = true; for (int i = 0; i < 4; ++i) - b.contains_near_clip &= geom::contains(b.bbox, camera_frustum.vertices[i]); + b.contains_near_clip &= math::contains(b.bbox, camera_frustum.vertices[i]); for (auto & p : b.buckets) { @@ -1162,7 +1162,7 @@ void main(){} mat->bump_map->bind(); } - program["u_material"] = geom::vector{mat->specular.intensity, mat->specular.shininess}; + program["u_material"] = math::vector{mat->specular.intensity, mat->specular.shininess}; for (std::size_t i = clip_by_camera ? p.second.first_visible : 0; i < p.second.objects.size(); ++i) { @@ -1187,20 +1187,20 @@ void main(){} // Resize g-buffer if needed - auto const buffer_size = geom::cast(target.viewport.dimensions()); + auto const buffer_size = math::cast(target.viewport.dimensions()); bool const buffer_size_changed = !impl().g_buffer_size || *impl().g_buffer_size != buffer_size; if (buffer_size_changed || impl().position_mode_changed) { if (impl().position_mode == position_mode::float16) - impl().g_buffer_texture[0].load>(buffer_size); + impl().g_buffer_texture[0].load>(buffer_size); else if (impl().position_mode == position_mode::float32) - impl().g_buffer_texture[0].load>(buffer_size); + impl().g_buffer_texture[0].load>(buffer_size); if (buffer_size_changed) { - impl().g_buffer_texture[1].load>(buffer_size); + impl().g_buffer_texture[1].load>(buffer_size); impl().g_buffer_texture[2].load>(buffer_size); - impl().g_buffer_texture[3].load>(buffer_size); + impl().g_buffer_texture[3].load>(buffer_size); impl().g_buffer_depth.load(buffer_size); } @@ -1309,8 +1309,8 @@ void main(){} if (b.contains_near_clip) continue; if (b.camera_separation > 0.f) continue; - impl().occlusion_pass_program["u_box_min"] = geom::vector{b.bbox[0].min, b.bbox[1].min, b.bbox[2].min}; - impl().occlusion_pass_program["u_box_max"] = geom::vector{b.bbox[0].max, b.bbox[1].max, b.bbox[2].max}; + impl().occlusion_pass_program["u_box_min"] = math::vector{b.bbox[0].min, b.bbox[1].min, b.bbox[2].min}; + impl().occlusion_pass_program["u_box_max"] = math::vector{b.bbox[0].max, b.bbox[1].max, b.bbox[2].max}; gl::BeginQuery(gl::ANY_SAMPLES_PASSED, impl().queries[bi]); gl::DrawArrays(gl::TRIANGLES, 0, 36); @@ -1484,7 +1484,7 @@ void main(){} impl().ssao_pass_program.bind(); gl::ActiveTexture(gl::TEXTURE4); impl().ssao_rotation_texture.bind(); - impl().ssao_pass_program["u_ssao_rotation_step"] = geom::cast(impl().ssao_texture[0].size()) / (1.f * impl().ssao_rotation_texture.width()); + impl().ssao_pass_program["u_ssao_rotation_step"] = math::cast(impl().ssao_texture[0].size()) / (1.f * impl().ssao_rotation_texture.width()); impl().ssao_pass_program["u_ssao_radius"] = opts.ssao->radius; impl().ssao_pass_program["u_transform"] = camera_transform; impl().ssao_pass_program["u_view"] = opts.camera->view(); @@ -1554,7 +1554,7 @@ void main(){} // Directional lights - geom::point bbox_hull_screen[8]; + math::point bbox_hull_screen[8]; auto lit_bbox_hull_size = bbox_to_screen_fan(camera_transform, lit_bbox, bbox_hull_screen); impl().screen_mesh.load(bbox_hull_screen, lit_bbox_hull_size, gl::TRIANGLE_FAN); @@ -1563,7 +1563,7 @@ void main(){} for (auto const & l : opts.directional_lights) { - std::vector> light_transform; + std::vector> light_transform; if (l.shadowed) { @@ -1576,16 +1576,16 @@ void main(){} // Compute cascade split points - auto dir = geom::as_vector(camera_clip_planes[4]); - std::vector> cascade_splits(l.cascades); + auto dir = math::as_vector(camera_clip_planes[4]); + std::vector> cascade_splits(l.cascades); { float offset; - auto dir_length = geom::length(dir); + auto dir_length = math::length(dir); float near = camera_clip_planes[4][3] / dir_length; dir /= dir_length; - offset = geom::dot(camera_position - geom::point::zero(), dir); + offset = math::dot(camera_position - math::point::zero(), dir); near += offset; - float far = -camera_clip_planes[5][3] / geom::length(geom::as_vector(camera_clip_planes[5])) - offset; + float far = -camera_clip_planes[5][3] / math::length(math::as_vector(camera_clip_planes[5])) - offset; if (l.cascade_ranges.empty()) { @@ -1615,39 +1615,39 @@ void main(){} impl().directional_shadow_texture.load({l.shadow_map_size, l.shadow_map_size, l.cascades}); } - geom::vector light_axes[3]; + math::vector light_axes[3]; - light_axes[2] = -geom::normalized(l.direction); + light_axes[2] = -math::normalized(l.direction); light_axes[1] = {0.f, 0.f, 1.f}; if (light_axes[2][2] > 0.5f) light_axes[1] = {0.f, 1.f, 0.f}; - geom::gram_schmidt(light_axes[2], light_axes[1]); + math::gram_schmidt(light_axes[2], light_axes[1]); - light_axes[0] = geom::cross(light_axes[2], light_axes[1]); + light_axes[0] = math::cross(light_axes[2], light_axes[1]); for (int cascade = 0; cascade < l.cascades; ++cascade) { - geom::box shadowed_bbox; + math::box shadowed_bbox; - geom::matrix cascade_transform = camera_transform; + math::matrix cascade_transform = camera_transform; { float near = cascade_splits[cascade].max; float far = cascade_splits[cascade].min; - geom::vector b; + math::vector b; b[0] = -2.f * cascade_transform[3][dir_max_index]; b[1] = -2.f * cascade_transform[3][3]; - geom::matrix m; + math::matrix m; m[0][0] = - dir[dir_max_index]; m[0][1] = dir[dir_max_index]; m[1][0] = -near; m[1][1] = far; - geom::gauss(m, b); + math::gauss(m, b); for (std::size_t i = 0; i < 3; ++i) cascade_transform[2][i] = dir[i] * (b[0] + b[1]) / 2.f; @@ -1661,20 +1661,20 @@ void main(){} shadowed_bbox &= casts_shadow_bbox; - static auto const origin = geom::point::zero(); + static auto const origin = math::point::zero(); - geom::box light_bbox; + math::box light_bbox; - for (auto const & v : geom::vertices(shadowed_bbox)) + for (auto const & v : math::vertices(shadowed_bbox)) { for (std::size_t i = 0; i < 3; ++i) - light_bbox[i] |= geom::dot(light_axes[i], v - origin); + light_bbox[i] |= math::dot(light_axes[i], v - origin); } - for (auto const & v : geom::vertices(casts_shadow_bbox)) - light_bbox[2] |= geom::dot(light_axes[2], v - origin); + for (auto const & v : math::vertices(casts_shadow_bbox)) + light_bbox[2] |= math::dot(light_axes[2], v - origin); - light_transform[cascade] = geom::orthographic_camera{light_bbox}.projection() * geom::homogeneous(geom::by_rows(light_axes[0], light_axes[1], light_axes[2])); + light_transform[cascade] = math::orthographic_camera{light_bbox}.projection() * math::homogeneous(math::by_rows(light_axes[0], light_axes[1], light_axes[2])); cg::frustum light_frustum{light_transform[cascade]}; @@ -1747,7 +1747,7 @@ void main(){} impl().directional_light_pass_program["u_camera_position"] = camera_position; impl().directional_light_pass_program["u_max_intensity"] = opts.max_intensity; - impl().directional_light_pass_program["u_light_direction"] = geom::normalized(l.direction); + impl().directional_light_pass_program["u_light_direction"] = math::normalized(l.direction); impl().directional_light_pass_program["u_light_color"] = l.color; impl().directional_light_pass_program["u_shadowed"] = l.shadowed; if (l.shadowed) @@ -1772,11 +1772,11 @@ void main(){} for (auto const & l : opts.point_lights) { float I = std::max({l.color[0], l.color[1], l.color[2]}); - auto r = geom::solve_quadratic(l.attenuation.c2, l.attenuation.c1, l.attenuation.c0 - I / min_intensity); + auto r = math::solve_quadratic(l.attenuation.c2, l.attenuation.c1, l.attenuation.c0 - I / min_intensity); float near = l.min_shadow_distance; - geom::vector far_positive; - geom::vector far_negative; + math::vector far_positive; + math::vector far_negative; for (std::size_t i = 0; i < 3; ++i) { @@ -1795,12 +1795,12 @@ void main(){} if (l.shadowed) { - geom::matrix transform[6]; + math::matrix transform[6]; // +X { float far = far_positive[0]; - transform[0] = geom::matrix::zero(); + transform[0] = math::matrix::zero(); transform[0][0][2] = -1.f; transform[0][1][1] = -1.f; @@ -1813,7 +1813,7 @@ void main(){} // -X { float far = far_negative[0]; - transform[1] = geom::matrix::zero(); + transform[1] = math::matrix::zero(); transform[1][0][2] = 1.f; transform[1][1][1] = -1.f; @@ -1826,7 +1826,7 @@ void main(){} // +Y { float far = far_positive[1]; - transform[2] = geom::matrix::zero(); + transform[2] = math::matrix::zero(); transform[2][0][0] = 1.f; transform[2][1][2] = 1.f; @@ -1839,7 +1839,7 @@ void main(){} // -Y { float far = far_negative[1]; - transform[3] = geom::matrix::zero(); + transform[3] = math::matrix::zero(); transform[3][0][0] = 1.f; transform[3][1][2] = -1.f; @@ -1852,7 +1852,7 @@ void main(){} // +Z { float far = far_positive[2]; - transform[4] = geom::matrix::zero(); + transform[4] = math::matrix::zero(); transform[4][0][0] = 1.f; transform[4][1][1] = -1.f; @@ -1865,7 +1865,7 @@ void main(){} // -Z { float far = far_negative[2]; - transform[5] = geom::matrix::zero(); + transform[5] = math::matrix::zero(); transform[5][0][0] = -1.f; transform[5][1][1] = -1.f; @@ -1875,7 +1875,7 @@ void main(){} transform[5][3][2] = - 1.f; } - geom::matrix const translate_by_light = geom::translation(geom::point::zero() - l.position).homogeneous_matrix(); + math::matrix const translate_by_light = math::translation(math::point::zero() - l.position).homogeneous_matrix(); impl().point_shadow_framebuffer.bind(); @@ -1900,7 +1900,7 @@ void main(){} gl::Disable(gl::BLEND); impl().cubemap_shadow_builder_program.bind(); - impl().cubemap_shadow_builder_program["u_light_transform"] = geom::matrix::identity(); + impl().cubemap_shadow_builder_program["u_light_transform"] = math::matrix::identity(); impl().cubemap_shadow_builder_program["u_layer_transform[0]"] = transform[0] * translate_by_light; impl().cubemap_shadow_builder_program["u_layer_transform[1]"] = transform[1] * translate_by_light; @@ -1931,7 +1931,7 @@ void main(){} impl().point_light_pass_program["u_shadow_far_negative"] = far_negative; } - geom::box light_bbox; + math::box light_bbox; if (r) { float light_influence_radius = r->second; @@ -1951,7 +1951,7 @@ void main(){} impl().point_light_pass_program["u_light_position"] = l.position; impl().point_light_pass_program["u_light_color"] = l.color; - impl().point_light_pass_program["u_light_attenuation"] = geom::vector{l.attenuation.c0, l.attenuation.c1, l.attenuation.c2}; + impl().point_light_pass_program["u_light_attenuation"] = math::vector{l.attenuation.c0, l.attenuation.c1, l.attenuation.c2}; impl().screen_mesh.draw(); } diff --git a/libs/gfx/source/texture.cpp b/libs/gfx/source/texture.cpp index bb0c46cd..feac9408 100644 --- a/libs/gfx/source/texture.cpp +++ b/libs/gfx/source/texture.cpp @@ -36,7 +36,7 @@ namespace psemek::gfx } - void texture_cubemap::load(int f, GLint internal_format, geom::vector const & size, GLenum format, GLenum type, const void * data) + void texture_cubemap::load(int f, GLint internal_format, math::vector const & size, GLenum format, GLenum type, const void * data) { bind(); gl::TexImage2D(face_to_gl(f), 0, internal_format, size[0], size[1], 0, format, type, data); @@ -115,7 +115,7 @@ namespace psemek::gfx bind(); } - void texture_2d_multisample::load(GLint internal_format, geom::vector const & size, int samples, bool fixed_sample_locations) + void texture_2d_multisample::load(GLint internal_format, math::vector const & size, int samples, bool fixed_sample_locations) { bind(); gl::TexImage2DMultisample(target, samples, internal_format, size[0], size[1], fixed_sample_locations ? gl::TRUE : gl::FALSE); diff --git a/libs/math/CMakeLists.txt b/libs/math/CMakeLists.txt new file mode 100644 index 00000000..94cb4bcd --- /dev/null +++ b/libs/math/CMakeLists.txt @@ -0,0 +1,18 @@ +option(PSEMEK_ROBUST_PREDICATES "Use robust geometric predicates" OFF) + +find_package(Boost REQUIRED) +if(PSEMEK_ROBUST_PREDICATES) + find_package(GMP REQUIRED) +endif() + +file(GLOB_RECURSE PSEMEK_GEOM_HEADERS "include/*.hpp") +file(GLOB_RECURSE PSEMEK_GEOM_SOURCES "source/*.cpp") + +psemek_add_library(psemek-math ${PSEMEK_GEOM_HEADERS} ${PSEMEK_GEOM_SOURCES}) +target_include_directories(psemek-math PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") +target_link_libraries(psemek-math PUBLIC psemek-util psemek-group Boost::boost) +if(PSEMEK_ROBUST_PREDICATES) + target_link_libraries(psemek-math PUBLIC gmp) +endif() + +psemek_glob_tests(psemek-math tests) diff --git a/libs/geom/include/psemek/geom/affine_transform.hpp b/libs/math/include/psemek/math/affine_transform.hpp similarity index 96% rename from libs/geom/include/psemek/geom/affine_transform.hpp rename to libs/math/include/psemek/math/affine_transform.hpp index 7c5d8751..f19a72d5 100644 --- a/libs/geom/include/psemek/geom/affine_transform.hpp +++ b/libs/math/include/psemek/math/affine_transform.hpp @@ -1,11 +1,11 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include -namespace psemek::geom +namespace psemek::math { // Affine transformation from M-dimensional to N-dimensional affine space diff --git a/libs/geom/include/psemek/geom/bezier.hpp b/libs/math/include/psemek/math/bezier.hpp similarity index 96% rename from libs/geom/include/psemek/geom/bezier.hpp rename to libs/math/include/psemek/math/bezier.hpp index 3c78feec..26c6c6ec 100644 --- a/libs/geom/include/psemek/geom/bezier.hpp +++ b/libs/math/include/psemek/math/bezier.hpp @@ -1,12 +1,12 @@ #pragma once -#include +#include #include #include #include -namespace psemek::geom +namespace psemek::math { // In-place de Casteljau's algorithm diff --git a/libs/geom/include/psemek/geom/box.hpp b/libs/math/include/psemek/math/box.hpp similarity index 97% rename from libs/geom/include/psemek/geom/box.hpp rename to libs/math/include/psemek/math/box.hpp index 6b4b6867..18038144 100644 --- a/libs/geom/include/psemek/geom/box.hpp +++ b/libs/math/include/psemek/math/box.hpp @@ -1,12 +1,12 @@ #pragma once -#include -#include -#include +#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { template @@ -59,9 +59,9 @@ namespace psemek::geom return result; } - geom::vector dimensions() const + math::vector dimensions() const { - geom::vector result; + math::vector result; for (std::size_t i = 0; i < N; ++i) result[i] = axes[i].length(); return result; diff --git a/libs/geom/include/psemek/geom/camera.hpp b/libs/math/include/psemek/math/camera.hpp similarity index 92% rename from libs/geom/include/psemek/geom/camera.hpp rename to libs/math/include/psemek/math/camera.hpp index a723c5de..0a860313 100644 --- a/libs/geom/include/psemek/geom/camera.hpp +++ b/libs/math/include/psemek/math/camera.hpp @@ -1,13 +1,13 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { struct camera @@ -55,18 +55,18 @@ namespace psemek::geom struct orthographic_camera : camera { - geom::box box; + math::box box; orthographic_camera() = default; - orthographic_camera(geom::box const & box) + orthographic_camera(math::box const & box) { this->box[0] = box[0]; this->box[1] = box[1]; this->box[2] = {-1.f, 1.f}; } - orthographic_camera(geom::box const & box) + orthographic_camera(math::box const & box) : box{box} {} diff --git a/libs/geom/include/psemek/geom/cholesky.hpp b/libs/math/include/psemek/math/cholesky.hpp similarity index 95% rename from libs/geom/include/psemek/geom/cholesky.hpp rename to libs/math/include/psemek/math/cholesky.hpp index 015a792e..a93fbd60 100644 --- a/libs/geom/include/psemek/geom/cholesky.hpp +++ b/libs/math/include/psemek/math/cholesky.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/concat.hpp b/libs/math/include/psemek/math/concat.hpp similarity index 85% rename from libs/geom/include/psemek/geom/concat.hpp rename to libs/math/include/psemek/math/concat.hpp index cb5e3080..9da409fc 100644 --- a/libs/geom/include/psemek/geom/concat.hpp +++ b/libs/math/include/psemek/math/concat.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/constants.hpp b/libs/math/include/psemek/math/constants.hpp similarity index 86% rename from libs/geom/include/psemek/geom/constants.hpp rename to libs/math/include/psemek/math/constants.hpp index b97162fc..ed27210b 100644 --- a/libs/geom/include/psemek/geom/constants.hpp +++ b/libs/math/include/psemek/math/constants.hpp @@ -1,6 +1,6 @@ #pragma once -namespace psemek::geom +namespace psemek::math { constexpr long double pi = 3.141592653589793238462643383279502884l; diff --git a/libs/geom/include/psemek/geom/contains.hpp b/libs/math/include/psemek/math/contains.hpp similarity index 91% rename from libs/geom/include/psemek/geom/contains.hpp rename to libs/math/include/psemek/math/contains.hpp index 06e25429..6e6c2513 100644 --- a/libs/geom/include/psemek/geom/contains.hpp +++ b/libs/math/include/psemek/math/contains.hpp @@ -1,13 +1,13 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/cylinder.hpp b/libs/math/include/psemek/math/cylinder.hpp similarity index 72% rename from libs/geom/include/psemek/geom/cylinder.hpp rename to libs/math/include/psemek/math/cylinder.hpp index 432e7647..29dc138e 100644 --- a/libs/geom/include/psemek/geom/cylinder.hpp +++ b/libs/math/include/psemek/math/cylinder.hpp @@ -1,9 +1,9 @@ #pragma once -#include -#include +#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/detail/array.hpp b/libs/math/include/psemek/math/detail/array.hpp similarity index 96% rename from libs/geom/include/psemek/geom/detail/array.hpp rename to libs/math/include/psemek/math/detail/array.hpp index 2532d6b1..2847d8cb 100644 --- a/libs/geom/include/psemek/geom/detail/array.hpp +++ b/libs/math/include/psemek/math/detail/array.hpp @@ -2,7 +2,7 @@ #include -namespace psemek::geom::detail +namespace psemek::math::detail { struct empty_array_exception diff --git a/libs/geom/include/psemek/geom/distance.hpp b/libs/math/include/psemek/math/distance.hpp similarity index 93% rename from libs/geom/include/psemek/geom/distance.hpp rename to libs/math/include/psemek/math/distance.hpp index 1aed86b8..ea03d162 100644 --- a/libs/geom/include/psemek/geom/distance.hpp +++ b/libs/math/include/psemek/math/distance.hpp @@ -1,14 +1,14 @@ #pragma once -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { template @@ -111,7 +111,7 @@ namespace psemek::geom { auto v = p - *prev; auto r = *it - *prev; - auto t = geom::dot(v, r) / geom::dot(r, r); + auto t = math::dot(v, r) / math::dot(r, r); if (0 <= t && t <= 1) { diff --git a/libs/geom/include/psemek/geom/dual.hpp b/libs/math/include/psemek/math/dual.hpp similarity index 98% rename from libs/geom/include/psemek/geom/dual.hpp rename to libs/math/include/psemek/math/dual.hpp index d1b190aa..cca32a95 100644 --- a/libs/geom/include/psemek/geom/dual.hpp +++ b/libs/math/include/psemek/math/dual.hpp @@ -1,10 +1,10 @@ #pragma once -#include +#include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/easing.hpp b/libs/math/include/psemek/math/easing.hpp similarity index 92% rename from libs/geom/include/psemek/geom/easing.hpp rename to libs/math/include/psemek/math/easing.hpp index 4e116a9a..069dc3e8 100644 --- a/libs/geom/include/psemek/geom/easing.hpp +++ b/libs/math/include/psemek/math/easing.hpp @@ -1,10 +1,10 @@ #pragma once -#include +#include #include -namespace psemek::geom +namespace psemek::math { enum class easing_type diff --git a/libs/geom/include/psemek/geom/gauss.hpp b/libs/math/include/psemek/math/gauss.hpp similarity index 99% rename from libs/geom/include/psemek/geom/gauss.hpp rename to libs/math/include/psemek/math/gauss.hpp index 48da6204..dc7a2b15 100644 --- a/libs/geom/include/psemek/geom/gauss.hpp +++ b/libs/math/include/psemek/math/gauss.hpp @@ -1,10 +1,10 @@ #pragma once -#include +#include #include -namespace psemek::geom +namespace psemek::math { namespace detail diff --git a/libs/geom/include/psemek/geom/gradient.hpp b/libs/math/include/psemek/math/gradient.hpp similarity index 93% rename from libs/geom/include/psemek/geom/gradient.hpp rename to libs/math/include/psemek/math/gradient.hpp index 028b9470..7f66a99a 100644 --- a/libs/geom/include/psemek/geom/gradient.hpp +++ b/libs/math/include/psemek/math/gradient.hpp @@ -1,12 +1,12 @@ #pragma once -#include -#include +#include +#include #include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/gram_schmidt.hpp b/libs/math/include/psemek/math/gram_schmidt.hpp similarity index 83% rename from libs/geom/include/psemek/geom/gram_schmidt.hpp rename to libs/math/include/psemek/math/gram_schmidt.hpp index 5dac446f..28898e4e 100644 --- a/libs/geom/include/psemek/geom/gram_schmidt.hpp +++ b/libs/math/include/psemek/math/gram_schmidt.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -namespace psemek::geom +namespace psemek::math { inline bool gram_schmidt() diff --git a/libs/geom/include/psemek/geom/hermite.hpp b/libs/math/include/psemek/math/hermite.hpp similarity index 93% rename from libs/geom/include/psemek/geom/hermite.hpp rename to libs/math/include/psemek/math/hermite.hpp index 5a928e71..5918ad82 100644 --- a/libs/geom/include/psemek/geom/hermite.hpp +++ b/libs/math/include/psemek/math/hermite.hpp @@ -1,6 +1,6 @@ #pragma once -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/homogeneous.hpp b/libs/math/include/psemek/math/homogeneous.hpp similarity index 92% rename from libs/geom/include/psemek/geom/homogeneous.hpp rename to libs/math/include/psemek/math/homogeneous.hpp index 6b828e90..20125f57 100644 --- a/libs/geom/include/psemek/geom/homogeneous.hpp +++ b/libs/math/include/psemek/math/homogeneous.hpp @@ -1,10 +1,10 @@ #pragma once -#include -#include -#include +#include +#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/incircle.hpp b/libs/math/include/psemek/math/incircle.hpp similarity index 95% rename from libs/geom/include/psemek/geom/incircle.hpp rename to libs/math/include/psemek/math/incircle.hpp index 0fee0e11..2324b9c3 100644 --- a/libs/geom/include/psemek/geom/incircle.hpp +++ b/libs/math/include/psemek/math/incircle.hpp @@ -1,9 +1,9 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include #ifdef PSEMEK_ROBUST_PREDICATES #include @@ -11,7 +11,7 @@ #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/intersection.hpp b/libs/math/include/psemek/math/intersection.hpp similarity index 95% rename from libs/geom/include/psemek/geom/intersection.hpp rename to libs/math/include/psemek/math/intersection.hpp index 4c28d24b..ec0f2dbf 100644 --- a/libs/geom/include/psemek/geom/intersection.hpp +++ b/libs/math/include/psemek/math/intersection.hpp @@ -2,20 +2,20 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include -namespace psemek::geom +namespace psemek::math { template @@ -179,8 +179,8 @@ namespace psemek::geom template std::optional intersection(ray const & r, simplex, N - 1> const & s) { - geom::matrix m; - geom::vector b; + math::matrix m; + math::vector b; for (std::size_t i = 0; i < N; ++i) { @@ -193,7 +193,7 @@ namespace psemek::geom } } - if (!geom::gauss(m, b)) + if (!math::gauss(m, b)) return std::nullopt; T sum{}; diff --git a/libs/geom/include/psemek/geom/interval.hpp b/libs/math/include/psemek/math/interval.hpp similarity index 99% rename from libs/geom/include/psemek/geom/interval.hpp rename to libs/math/include/psemek/math/interval.hpp index bf63d079..1cff3ffa 100644 --- a/libs/geom/include/psemek/geom/interval.hpp +++ b/libs/math/include/psemek/math/interval.hpp @@ -5,7 +5,7 @@ #include #include -namespace psemek::geom +namespace psemek::math { // Can be specialized in client code diff --git a/libs/geom/include/psemek/geom/math.hpp b/libs/math/include/psemek/math/math.hpp similarity index 98% rename from libs/geom/include/psemek/geom/math.hpp rename to libs/math/include/psemek/math/math.hpp index f3554977..d6c0c633 100644 --- a/libs/geom/include/psemek/geom/math.hpp +++ b/libs/math/include/psemek/math/math.hpp @@ -1,12 +1,12 @@ #pragma once -#include +#include #include #include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/matrix.hpp b/libs/math/include/psemek/math/matrix.hpp similarity index 98% rename from libs/geom/include/psemek/geom/matrix.hpp rename to libs/math/include/psemek/math/matrix.hpp index a79ac90d..eabfd12f 100644 --- a/libs/geom/include/psemek/geom/matrix.hpp +++ b/libs/math/include/psemek/math/matrix.hpp @@ -1,14 +1,14 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/mesh.hpp b/libs/math/include/psemek/math/mesh.hpp similarity index 98% rename from libs/geom/include/psemek/geom/mesh.hpp rename to libs/math/include/psemek/math/mesh.hpp index 2f5d8ef0..476ba63e 100644 --- a/libs/geom/include/psemek/geom/mesh.hpp +++ b/libs/math/include/psemek/math/mesh.hpp @@ -1,13 +1,13 @@ #pragma once -#include -#include +#include +#include #include #include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/orientation.hpp b/libs/math/include/psemek/math/orientation.hpp similarity index 91% rename from libs/geom/include/psemek/geom/orientation.hpp rename to libs/math/include/psemek/math/orientation.hpp index acc06c73..69095f12 100644 --- a/libs/geom/include/psemek/geom/orientation.hpp +++ b/libs/math/include/psemek/math/orientation.hpp @@ -1,9 +1,9 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include #ifdef PSEMEK_ROBUST_PREDICATES #include @@ -12,7 +12,7 @@ #include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/orthographic.hpp b/libs/math/include/psemek/math/orthographic.hpp similarity index 84% rename from libs/geom/include/psemek/geom/orthographic.hpp rename to libs/math/include/psemek/math/orthographic.hpp index c9d0ecf9..798faefa 100644 --- a/libs/geom/include/psemek/geom/orthographic.hpp +++ b/libs/math/include/psemek/math/orthographic.hpp @@ -1,21 +1,21 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include -namespace psemek::geom +namespace psemek::math { template struct orthographic { - using vector_type = geom::vector; - using point_type = geom::point; - using homogeneous_matrix_type = geom::matrix; - using box_type = geom::box; + using vector_type = math::vector; + using point_type = math::point; + using homogeneous_matrix_type = math::matrix; + using box_type = math::box; orthographic(); orthographic(box_type r); diff --git a/libs/geom/include/psemek/geom/permutation.hpp b/libs/math/include/psemek/math/permutation.hpp similarity index 96% rename from libs/geom/include/psemek/geom/permutation.hpp rename to libs/math/include/psemek/math/permutation.hpp index 4189f765..af503979 100644 --- a/libs/geom/include/psemek/geom/permutation.hpp +++ b/libs/math/include/psemek/math/permutation.hpp @@ -1,10 +1,10 @@ #pragma once -#include +#include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/perspective.hpp b/libs/math/include/psemek/math/perspective.hpp similarity index 95% rename from libs/geom/include/psemek/geom/perspective.hpp rename to libs/math/include/psemek/math/perspective.hpp index 76e51a7f..d664f184 100644 --- a/libs/geom/include/psemek/geom/perspective.hpp +++ b/libs/math/include/psemek/math/perspective.hpp @@ -1,9 +1,9 @@ #pragma once -#include -#include +#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/point.hpp b/libs/math/include/psemek/math/point.hpp similarity index 96% rename from libs/geom/include/psemek/geom/point.hpp rename to libs/math/include/psemek/math/point.hpp index b0aef7b3..aa0722f3 100644 --- a/libs/geom/include/psemek/geom/point.hpp +++ b/libs/math/include/psemek/math/point.hpp @@ -2,12 +2,12 @@ #include -#include -#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { template @@ -220,9 +220,9 @@ namespace std { template - struct hash<::psemek::geom::point> + struct hash<::psemek::math::point> { - std::size_t operator()(::psemek::geom::point const & v) const noexcept + std::size_t operator()(::psemek::math::point const & v) const noexcept { std::hash h; std::size_t r = 0; diff --git a/libs/geom/include/psemek/geom/qr.hpp b/libs/math/include/psemek/math/qr.hpp similarity index 96% rename from libs/geom/include/psemek/geom/qr.hpp rename to libs/math/include/psemek/math/qr.hpp index 2c892793..a8e3d641 100644 --- a/libs/geom/include/psemek/geom/qr.hpp +++ b/libs/math/include/psemek/math/qr.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/quaternion.hpp b/libs/math/include/psemek/math/quaternion.hpp similarity index 92% rename from libs/geom/include/psemek/geom/quaternion.hpp rename to libs/math/include/psemek/math/quaternion.hpp index d9890e2e..a86b7303 100644 --- a/libs/geom/include/psemek/geom/quaternion.hpp +++ b/libs/math/include/psemek/math/quaternion.hpp @@ -1,17 +1,17 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include -namespace psemek::geom +namespace psemek::math { template struct quaternion { - geom::vector coords; + math::vector coords; quaternion() = default; @@ -21,11 +21,11 @@ namespace psemek::geom static quaternion j(); static quaternion k(); static quaternion scalar(T value); - static quaternion vector(geom::vector const & v); - static quaternion rotation(T angle, geom::vector const & axis); + static quaternion vector(math::vector const & v); + static quaternion rotation(T angle, math::vector const & axis); static quaternion rotation(matrix const & m); - quaternion(geom::vector const & v) + quaternion(math::vector const & v) : coords{v} {} @@ -42,7 +42,7 @@ namespace psemek::geom template quaternion quaternion::zero() { - return quaternion{geom::vector::zero()}; + return quaternion{math::vector::zero()}; } template @@ -76,13 +76,13 @@ namespace psemek::geom } template - quaternion quaternion::vector(geom::vector const & v) + quaternion quaternion::vector(math::vector const & v) { return {{v[0], v[1], v[2], T(0)}}; } template - quaternion quaternion::rotation(T angle, geom::vector const & axis) + quaternion quaternion::rotation(T angle, math::vector const & axis) { auto const c = std::cos(angle / 2); auto const s = std::sin(angle / 2); @@ -319,9 +319,9 @@ namespace psemek::geom // Returns a matrix M such that the angular gradient of function f(q) is M * grad(f) template - geom::matrix angular_gradient(quaternion const & q) + math::matrix angular_gradient(quaternion const & q) { - geom::matrix result; + math::matrix result; result[0][0] = q[3]; result[0][1] = - q[2]; result[0][2] = q[1]; diff --git a/libs/geom/include/psemek/geom/ray.hpp b/libs/math/include/psemek/math/ray.hpp similarity index 93% rename from libs/geom/include/psemek/geom/ray.hpp rename to libs/math/include/psemek/math/ray.hpp index a20ce08e..c517d9dd 100644 --- a/libs/geom/include/psemek/geom/ray.hpp +++ b/libs/math/include/psemek/math/ray.hpp @@ -1,11 +1,11 @@ #pragma once -#include -#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/robust.hpp b/libs/math/include/psemek/math/robust.hpp similarity index 87% rename from libs/geom/include/psemek/geom/robust.hpp rename to libs/math/include/psemek/math/robust.hpp index 64fe8a04..1cd23e3f 100644 --- a/libs/geom/include/psemek/geom/robust.hpp +++ b/libs/math/include/psemek/math/robust.hpp @@ -1,6 +1,6 @@ #pragma once -namespace psemek::geom +namespace psemek::math { constexpr struct fast_predicate_tag{} fast; diff --git a/libs/geom/include/psemek/geom/rotation.hpp b/libs/math/include/psemek/math/rotation.hpp similarity index 98% rename from libs/geom/include/psemek/geom/rotation.hpp rename to libs/math/include/psemek/math/rotation.hpp index ea60cb8d..21637f37 100644 --- a/libs/geom/include/psemek/geom/rotation.hpp +++ b/libs/math/include/psemek/math/rotation.hpp @@ -1,11 +1,11 @@ #pragma once -#include -#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { // Rotation in oriented plane (i,j) @@ -234,7 +234,7 @@ namespace psemek::geom template point axis_rotation::operator()(point const & p) const { - auto const o = geom::point::zero(); + auto const o = math::point::zero(); return linear_matrix() * (p - o) + o; } diff --git a/libs/geom/include/psemek/geom/scale.hpp b/libs/math/include/psemek/math/scale.hpp similarity index 97% rename from libs/geom/include/psemek/geom/scale.hpp rename to libs/math/include/psemek/math/scale.hpp index e4a61c54..cd2e647e 100644 --- a/libs/geom/include/psemek/geom/scale.hpp +++ b/libs/math/include/psemek/math/scale.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/sign.hpp b/libs/math/include/psemek/math/sign.hpp similarity index 96% rename from libs/geom/include/psemek/geom/sign.hpp rename to libs/math/include/psemek/math/sign.hpp index d11482d6..43932fa4 100644 --- a/libs/geom/include/psemek/geom/sign.hpp +++ b/libs/math/include/psemek/math/sign.hpp @@ -4,7 +4,7 @@ #include -namespace psemek::geom +namespace psemek::math { enum class sign_t : int diff --git a/libs/geom/include/psemek/geom/simplex.hpp b/libs/math/include/psemek/math/simplex.hpp similarity index 91% rename from libs/geom/include/psemek/geom/simplex.hpp rename to libs/math/include/psemek/math/simplex.hpp index 03d9721d..49853fdc 100644 --- a/libs/geom/include/psemek/geom/simplex.hpp +++ b/libs/math/include/psemek/math/simplex.hpp @@ -1,13 +1,13 @@ #pragma once -#include +#include #include #include #include #include -namespace psemek::geom +namespace psemek::math { template @@ -98,7 +98,7 @@ namespace psemek::geom } template - simplex, K> operator + (simplex, K> s, geom::vector const & v) + simplex, K> operator + (simplex, K> s, math::vector const & v) { for (auto & p : s.points) p += v; @@ -106,7 +106,7 @@ namespace psemek::geom } template - simplex, K> operator + (geom::vector const & v, simplex, K> s) + simplex, K> operator + (math::vector const & v, simplex, K> s) { for (auto & p : s.points) p += v; @@ -114,7 +114,7 @@ namespace psemek::geom } template - simplex, K> operator - (simplex, K> s, geom::vector const & v) + simplex, K> operator - (simplex, K> s, math::vector const & v) { for (auto & p : s.points) p -= v; @@ -137,9 +137,9 @@ namespace std { template - struct hash<::psemek::geom::simplex> + struct hash<::psemek::math::simplex> { - std::size_t operator()(::psemek::geom::simplex const & simplex) const + std::size_t operator()(::psemek::math::simplex const & simplex) const { std::size_t result = 0; std::hash h; diff --git a/libs/geom/include/psemek/geom/sphere.hpp b/libs/math/include/psemek/math/sphere.hpp similarity index 77% rename from libs/geom/include/psemek/geom/sphere.hpp rename to libs/math/include/psemek/math/sphere.hpp index 2db301ea..417490ae 100644 --- a/libs/geom/include/psemek/geom/sphere.hpp +++ b/libs/math/include/psemek/math/sphere.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/swizzle.hpp b/libs/math/include/psemek/math/swizzle.hpp similarity index 97% rename from libs/geom/include/psemek/geom/swizzle.hpp rename to libs/math/include/psemek/math/swizzle.hpp index 20ec6057..a60e5f0e 100644 --- a/libs/geom/include/psemek/geom/swizzle.hpp +++ b/libs/math/include/psemek/math/swizzle.hpp @@ -3,7 +3,7 @@ #include #include -namespace psemek::geom +namespace psemek::math { namespace detail diff --git a/libs/geom/include/psemek/geom/symmetry.hpp b/libs/math/include/psemek/math/symmetry.hpp similarity index 92% rename from libs/geom/include/psemek/geom/symmetry.hpp rename to libs/math/include/psemek/math/symmetry.hpp index 619be3f5..a2b82c20 100644 --- a/libs/geom/include/psemek/geom/symmetry.hpp +++ b/libs/math/include/psemek/math/symmetry.hpp @@ -3,12 +3,12 @@ #include #include -#include -#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { template @@ -60,7 +60,7 @@ namespace psemek::geom { m = m.identity(); - T angle = (T{2} * geom::pi * static_cast(g.value())) / static_cast(D); + T angle = (T{2} * math::pi * static_cast(g.value())) / static_cast(D); m[i][i] = std::cos(angle); m[i][j] = -std::sin(angle); @@ -80,7 +80,7 @@ namespace psemek::geom { m = m.identity(); - T angle = (T{2} * geom::pi * static_cast(g.value())) / static_cast(D); + T angle = (T{2} * math::pi * static_cast(g.value())) / static_cast(D); // See https://en.wikipedia.org/wiki/Dihedral_group#Matrix_representation @@ -137,7 +137,7 @@ namespace psemek::geom template affine_transform symmetry::transform() const { - return {m, geom::vector::zero()}; + return {m, math::vector::zero()}; } template diff --git a/libs/geom/include/psemek/geom/translation.hpp b/libs/math/include/psemek/math/translation.hpp similarity index 96% rename from libs/geom/include/psemek/geom/translation.hpp rename to libs/math/include/psemek/math/translation.hpp index 525f328b..81f664a7 100644 --- a/libs/geom/include/psemek/geom/translation.hpp +++ b/libs/math/include/psemek/math/translation.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -namespace psemek::geom +namespace psemek::math { template diff --git a/libs/geom/include/psemek/geom/vector.hpp b/libs/math/include/psemek/math/vector.hpp similarity index 98% rename from libs/geom/include/psemek/geom/vector.hpp rename to libs/math/include/psemek/math/vector.hpp index 40b2b404..731f53e8 100644 --- a/libs/geom/include/psemek/geom/vector.hpp +++ b/libs/math/include/psemek/math/vector.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -11,7 +11,7 @@ #include #include -namespace psemek::geom +namespace psemek::math { template @@ -470,9 +470,9 @@ namespace std { template - struct hash<::psemek::geom::vector> + struct hash<::psemek::math::vector> { - std::size_t operator()(::psemek::geom::vector const & v) const noexcept + std::size_t operator()(::psemek::math::vector const & v) const noexcept { std::hash h; std::size_t r = 0; diff --git a/libs/geom/source/camera.cpp b/libs/math/source/camera.cpp similarity index 87% rename from libs/geom/source/camera.cpp rename to libs/math/source/camera.cpp index 5bb3710c..8b3a69cf 100644 --- a/libs/geom/source/camera.cpp +++ b/libs/math/source/camera.cpp @@ -1,15 +1,15 @@ -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include -namespace psemek::geom +namespace psemek::math { matrix camera::transform() const @@ -120,7 +120,7 @@ namespace psemek::geom return tr.homogeneous_matrix(); } - static void rotate(geom::vector & a, geom::vector & b, float angle) + static void rotate(math::vector & a, math::vector & b, float angle) { auto s = std::sin(angle); auto c = std::cos(angle); @@ -148,7 +148,7 @@ namespace psemek::geom matrix free_camera::view() const { - return homogeneous(by_rows(axes[0], axes[1], axes[2])) * translation{geom::point::zero() - pos}.homogeneous_matrix(); + return homogeneous(by_rows(axes[0], axes[1], axes[2])) * translation{math::point::zero() - pos}.homogeneous_matrix(); } } diff --git a/libs/geom/tests/matrix.cpp b/libs/math/tests/matrix.cpp similarity index 91% rename from libs/geom/tests/matrix.cpp rename to libs/math/tests/matrix.cpp index 92d73b4b..2b6ad62d 100644 --- a/libs/geom/tests/matrix.cpp +++ b/libs/math/tests/matrix.cpp @@ -1,16 +1,16 @@ #include -#include -#include -#include -#include +#include +#include +#include +#include #include #include using namespace psemek; -using namespace psemek::geom; +using namespace psemek::math; -test_case(geom_matrix_empty) +test_case(math_matrix_empty) { matrix m00; matrix m10; @@ -32,7 +32,7 @@ test_case(geom_matrix_empty) expect_throw(m01[0][0], detail::empty_array_exception); } -test_case(geom_matrix_init) +test_case(math_matrix_init) { matrix m; m[0][0] = 1; @@ -48,7 +48,7 @@ test_case(geom_matrix_init) expect_equal(m[1][1], 4); } -test_case(geom_matrix_by__rows) +test_case(math_matrix_by__rows) { matrix m; m[0][0] = 1; @@ -59,7 +59,7 @@ test_case(geom_matrix_by__rows) expect_equal(m, (by_rows(vector{1, 2}, vector{3, 4}))); } -test_case(geom_matrix_by__columns) +test_case(math_matrix_by__columns) { matrix m; m[0][0] = 1; @@ -70,7 +70,7 @@ test_case(geom_matrix_by__columns) expect_equal(m, (by_columns(vector{1, 3}, vector{2, 4}))); } -test_case(geom_matrix_zero) +test_case(math_matrix_zero) { auto m = matrix::zero(); @@ -83,7 +83,7 @@ test_case(geom_matrix_zero) } } -test_case(geom_matrix_identity) +test_case(math_matrix_identity) { auto m = matrix::identity(); @@ -96,7 +96,7 @@ test_case(geom_matrix_identity) } } -test_case(geom_matrix_scalar) +test_case(math_matrix_scalar) { auto m = matrix::scalar(3.f); @@ -109,7 +109,7 @@ test_case(geom_matrix_scalar) } } -test_case(geom_matrix_arithmetic) +test_case(math_matrix_arithmetic) { vector v{1.f, 2.f}; @@ -140,7 +140,7 @@ test_case(geom_matrix_arithmetic) expect_equal(transpose(transpose(m1)), m1); } -test_case(geom_matrix_trace) +test_case(math_matrix_trace) { matrix m; @@ -152,7 +152,7 @@ test_case(geom_matrix_trace) expect_close(trace(m), 5.f, 1e-6); } -test_case(geom_matrix_det) +test_case(math_matrix_det) { matrix m; @@ -164,7 +164,7 @@ test_case(geom_matrix_det) expect_close(det(m), -2.f, 1e-6); } -test_case(geom_matrix_solve) +test_case(math_matrix_solve) { matrix m; @@ -181,7 +181,7 @@ test_case(geom_matrix_solve) expect_small(length(*s - vector{0.f, 0.5f}), 1e-6); } -test_case(geom_matrix_inverse) +test_case(math_matrix_inverse) { matrix m; @@ -196,7 +196,7 @@ test_case(geom_matrix_inverse) expect_small(frobenius_norm(*i - by_rows(vector{-2.f, 1.f}, vector{1.5f, -0.5f})), 1e-6); } -test_case(geom_matrix_solve__lower__triangular) +test_case(math_matrix_solve__lower__triangular) { random::generator rng; random::normal_distribution normal; @@ -221,7 +221,7 @@ test_case(geom_matrix_solve__lower__triangular) } } -test_case(geom_matrix_solve__upper__triangular) +test_case(math_matrix_solve__upper__triangular) { random::generator rng; random::normal_distribution normal; @@ -246,7 +246,7 @@ test_case(geom_matrix_solve__upper__triangular) } } -test_case(geom_matrix_inverse__lower__triangular) +test_case(math_matrix_inverse__lower__triangular) { random::generator rng; random::normal_distribution normal; @@ -266,7 +266,7 @@ test_case(geom_matrix_inverse__lower__triangular) } } -test_case(geom_matrix_inverse__upper__triangular) +test_case(math_matrix_inverse__upper__triangular) { random::generator rng; random::normal_distribution normal; @@ -286,7 +286,7 @@ test_case(geom_matrix_inverse__upper__triangular) } } -test_case(geom_matrix_qr) +test_case(math_matrix_qr) { random::generator rng; random::normal_distribution normal; @@ -309,7 +309,7 @@ test_case(geom_matrix_qr) } } -test_case(geom_matrix_qr__eig_symmetric) +test_case(math_matrix_qr__eig_symmetric) { random::generator rng; random::normal_distribution normal; @@ -340,7 +340,7 @@ test_case(geom_matrix_qr__eig_symmetric) } } -test_case(geom_matrix_qr__eig_general) +test_case(math_matrix_qr__eig_general) { random::generator rng; random::normal_distribution normal; @@ -374,7 +374,7 @@ test_case(geom_matrix_qr__eig_general) } } -test_case(geom_matrix_cholesky) +test_case(math_matrix_cholesky) { random::generator rng; random::normal_distribution normal; @@ -398,7 +398,7 @@ test_case(geom_matrix_cholesky) } } -test_case(geom_matrix_cholesky__ldl) +test_case(math_matrix_cholesky__ldl) { random::generator rng; random::normal_distribution normal; diff --git a/libs/geom/tests/point.cpp b/libs/math/tests/point.cpp similarity index 89% rename from libs/geom/tests/point.cpp rename to libs/math/tests/point.cpp index fbfd1258..93ff02c9 100644 --- a/libs/geom/tests/point.cpp +++ b/libs/math/tests/point.cpp @@ -1,10 +1,10 @@ #include -#include +#include -using namespace psemek::geom; +using namespace psemek::math; -test_case(geom_point_empty) +test_case(math_point_empty) { point pi; point pf; @@ -18,7 +18,7 @@ test_case(geom_point_empty) expect_throw(pf[0], detail::empty_array_exception); } -test_case(geom_point_init) +test_case(math_point_init) { point pi{1, 2}; point pf{3.f, 4.f, 5.f}; @@ -33,7 +33,7 @@ test_case(geom_point_init) expect_equal(pf[2], 5.f); } -test_case(geom_point_deduce) +test_case(math_point_deduce) { point pi{1, 2}; point pf{3.f, 4.f, 5.f}; @@ -48,7 +48,7 @@ test_case(geom_point_deduce) expect_equal(pf[2], 5.f); } -test_case(geom_point_arithmetic) +test_case(math_point_arithmetic) { point pi1{1, 2}; point pi2{3, 4}; @@ -73,7 +73,7 @@ test_case(geom_point_arithmetic) expect_equal(pf1 - pf2, (vector{-3.f, -3.f, -3.f})); } -test_case(geom_point_compare) +test_case(math_point_compare) { point pi1{1, 2}; point pi2{3, 4}; @@ -88,7 +88,7 @@ test_case(geom_point_compare) expect_gequal(pi2, pi1); } -test_case(geom_point_cast) +test_case(math_point_cast) { point pi{1, 2}; point pf{3.f, 4.f, 5.f}; @@ -97,7 +97,7 @@ test_case(geom_point_cast) expect_equal(cast(pf), (point{3, 4, 5})); } -test_case(geom_point_distance) +test_case(math_point_distance) { point p1{2.f, 5.f}; point p2{7.f, 17.f}; @@ -106,7 +106,7 @@ test_case(geom_point_distance) expect_close(distance(p1, p2), 13.f, 1e-6); } -test_case(geom_point_lerp) +test_case(math_point_lerp) { point p1{1.f, 2.f, 3.f}; point p2{5.f, 6.f, 7.f}; diff --git a/libs/geom/tests/vector.cpp b/libs/math/tests/vector.cpp similarity index 87% rename from libs/geom/tests/vector.cpp rename to libs/math/tests/vector.cpp index 128d64fd..a34d8c30 100644 --- a/libs/geom/tests/vector.cpp +++ b/libs/math/tests/vector.cpp @@ -1,11 +1,11 @@ #include -#include -#include +#include +#include -using namespace psemek::geom; +using namespace psemek::math; -test_case(geom_vector_empty) +test_case(math_vector_empty) { vector pi; vector pf; @@ -19,7 +19,7 @@ test_case(geom_vector_empty) expect_throw(pf[0], detail::empty_array_exception); } -test_case(geom_vector_init) +test_case(math_vector_init) { vector pi{1, 2}; vector pf{3.f, 4.f, 5.f}; @@ -34,7 +34,7 @@ test_case(geom_vector_init) expect_equal(pf[2], 5.f); } -test_case(geom_vector_deduce) +test_case(math_vector_deduce) { vector pi{1, 2}; vector pf{3.f, 4.f, 5.f}; @@ -49,7 +49,7 @@ test_case(geom_vector_deduce) expect_equal(pf[2], 5.f); } -test_case(geom_vector_arithmetic) +test_case(math_vector_arithmetic) { vector vi1{1, 2}; vector vi2{3, 4}; @@ -72,7 +72,7 @@ test_case(geom_vector_arithmetic) expect_equal(2.f * vf1 - 3.f * vf2, (vector{-14.f, -15.f, -16.f})); } -test_case(geom_vector_dot) +test_case(math_vector_dot) { vector vi1{1, 2}; vector vi2{3, 4}; @@ -85,7 +85,7 @@ test_case(geom_vector_dot) expect_equal((dot(vf1, vf2)), 164.f); } -test_case(geom_vector_det) +test_case(math_vector_det) { vector vi1{1, 2}; vector vi2{3, 4}; @@ -99,7 +99,7 @@ test_case(geom_vector_det) expect_equal((det(vf1, vf2, vf3)), -3.f); } -test_case(geom_vector_compare) +test_case(math_vector_compare) { vector vi1{1, 2}; vector vi2{3, 4}; @@ -114,7 +114,7 @@ test_case(geom_vector_compare) expect_gequal(vi2, vi1); } -test_case(geom_vector_cast) +test_case(math_vector_cast) { vector pi{1, 2}; vector pf{3.f, 4.f, 5.f}; @@ -123,7 +123,7 @@ test_case(geom_vector_cast) expect_equal(cast(pf), (vector{3, 4, 5})); } -test_case(geom_vector_ort) +test_case(math_vector_ort) { vector v1{1.f, 2.f}; vector v2{3.f, 4.f, 5.f}; @@ -132,7 +132,7 @@ test_case(geom_vector_ort) expect_small((dot(v2, ort(v2))), 1e-6); } -test_case(geom_vector_cross) +test_case(math_vector_cross) { vector v1{1.f, 2.f, 3.f}; vector v2{4.f, 5.f, 6.f}; @@ -141,7 +141,7 @@ test_case(geom_vector_cross) expect_small((dot(v2, cross(v1, v2))), 1e-6); } -test_case(geom_vector_length) +test_case(math_vector_length) { vector v{5.f, 12.f}; @@ -149,7 +149,7 @@ test_case(geom_vector_length) expect_close(length(v), 13.f, 1e-6); } -test_case(geom_vector_lerp) +test_case(math_vector_lerp) { vector v1{1.f, 2.f, 3.f}; vector v2{5.f, 6.f, 7.f}; @@ -159,7 +159,7 @@ test_case(geom_vector_lerp) expect_equal((lerp(v1, v2, 0.75f)), (vector{4.f, 5.f, 6.f})); } -test_case(geom_vector_slerp) +test_case(math_vector_slerp) { vector v1{1.f, 0.f}; vector v2{0.f, 1.f}; diff --git a/libs/ml/CMakeLists.txt b/libs/ml/CMakeLists.txt index 216f7f82..31de92ca 100644 --- a/libs/ml/CMakeLists.txt +++ b/libs/ml/CMakeLists.txt @@ -3,6 +3,6 @@ file(GLOB_RECURSE PSEMEK_ML_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "sour psemek_add_library(psemek-ml ${PSEMEK_ML_HEADERS} ${PSEMEK_ML_SOURCES}) target_include_directories(psemek-ml PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-ml PUBLIC psemek-util psemek-geom psemek-random) +target_link_libraries(psemek-ml PUBLIC psemek-util psemek-math psemek-random) psemek_glob_tests(psemek-ml tests) diff --git a/libs/ml/include/psemek/ml/neural_net/learner.hpp b/libs/ml/include/psemek/ml/neural_net/learner.hpp index fbbe865d..6f10b1f4 100644 --- a/libs/ml/include/psemek/ml/neural_net/learner.hpp +++ b/libs/ml/include/psemek/ml/neural_net/learner.hpp @@ -2,7 +2,7 @@ #include #include -#include +#include namespace psemek::ml { @@ -161,7 +161,7 @@ namespace psemek::ml { T value = T{0}; for (auto g : gradient_) - value += geom::sqr(g); + value += math::sqr(g); return std::sqrt(value); } diff --git a/libs/ml/include/psemek/ml/neural_net/loss.hpp b/libs/ml/include/psemek/ml/neural_net/loss.hpp index 31f242dd..9f0801f5 100644 --- a/libs/ml/include/psemek/ml/neural_net/loss.hpp +++ b/libs/ml/include/psemek/ml/neural_net/loss.hpp @@ -2,7 +2,7 @@ #include -#include +#include namespace psemek::ml { @@ -12,7 +12,7 @@ namespace psemek::ml { T value = T{0}; for (std::size_t i = 0; i < x1.size(); ++i) - value += geom::sqr(x1[i] - x2[i]) / T{2}; + value += math::sqr(x1[i] - x2[i]) / T{2}; return value; } diff --git a/libs/ml/tests/neural_net/gradient.cpp b/libs/ml/tests/neural_net/gradient.cpp index 3ff9c218..e289aff9 100644 --- a/libs/ml/tests/neural_net/gradient.cpp +++ b/libs/ml/tests/neural_net/gradient.cpp @@ -6,11 +6,11 @@ #include #include #include -#include +#include using namespace psemek::ml; using namespace psemek::random; -using namespace psemek::geom; +using namespace psemek::math; test_case(ml_neural__net_gradient) { diff --git a/libs/ml/tests/neural_net/learn.cpp b/libs/ml/tests/neural_net/learn.cpp index 20f997ad..151a423c 100644 --- a/libs/ml/tests/neural_net/learn.cpp +++ b/libs/ml/tests/neural_net/learn.cpp @@ -6,12 +6,12 @@ #include #include #include -#include +#include #include using namespace psemek::ml; using namespace psemek::random; -using namespace psemek::geom; +using namespace psemek::math; using namespace psemek::log; namespace diff --git a/libs/pcg/CMakeLists.txt b/libs/pcg/CMakeLists.txt index 3bcbaeb2..6d062da9 100644 --- a/libs/pcg/CMakeLists.txt +++ b/libs/pcg/CMakeLists.txt @@ -5,4 +5,4 @@ file(GLOB_RECURSE PSEMEK_PCG_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "sou psemek_add_library(psemek-pcg ${PSEMEK_PCG_HEADERS} ${PSEMEK_PCG_SOURCES}) target_include_directories(psemek-pcg PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-pcg PUBLIC psemek-util psemek-geom psemek-gfx psemek-random) +target_link_libraries(psemek-pcg PUBLIC psemek-util psemek-math psemek-gfx psemek-random) diff --git a/libs/pcg/include/psemek/pcg/bump.hpp b/libs/pcg/include/psemek/pcg/bump.hpp index 0b931c03..3cc7d1d3 100644 --- a/libs/pcg/include/psemek/pcg/bump.hpp +++ b/libs/pcg/include/psemek/pcg/bump.hpp @@ -7,9 +7,9 @@ namespace psemek::pcg { - inline gfx::basic_pixmap> bump(gfx::pixmap_float const & height_map, seamless_tag) + inline gfx::basic_pixmap> bump(gfx::pixmap_float const & height_map, seamless_tag) { - gfx::basic_pixmap> result(height_map.dims()); + gfx::basic_pixmap> result(height_map.dims()); float const dx = 1.f / result.width(); float const dy = 1.f / result.height(); @@ -34,14 +34,14 @@ namespace psemek::pcg float hy0 = height_map(iy0); float hy1 = height_map(iy1); - auto n = geom::vector::zero(); + auto n = math::vector::zero(); - n += geom::normalized(geom::vector{hx0 - h, 0.f, dx}); - n += geom::normalized(geom::vector{h - hx1, 0.f, dx}); - n += geom::normalized(geom::vector{0.f, hy0 - h, dy}); - n += geom::normalized(geom::vector{0.f, h - hy1, dy}); + n += math::normalized(math::vector{hx0 - h, 0.f, dx}); + n += math::normalized(math::vector{h - hx1, 0.f, dx}); + n += math::normalized(math::vector{0.f, hy0 - h, dy}); + n += math::normalized(math::vector{0.f, h - hy1, dy}); - result(i) = geom::normalized(n); + result(i) = math::normalized(n); } return result; diff --git a/libs/pcg/include/psemek/pcg/chunked_map.hpp b/libs/pcg/include/psemek/pcg/chunked_map.hpp index b4bf598b..a20a6e16 100644 --- a/libs/pcg/include/psemek/pcg/chunked_map.hpp +++ b/libs/pcg/include/psemek/pcg/chunked_map.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include @@ -13,16 +13,16 @@ namespace psemek::pcg template struct chunked_map { - using generator_func = util::function(geom::box const &)>; + using generator_func = util::function(math::box const &)>; chunked_map(std::size_t chunk_size, generator_func generator) : chunk_size_(chunk_size) , generator_(std::move(generator)) {} - T & operator()(geom::point p) + T & operator()(math::point p) { - geom::vector ip; + math::vector ip; for (std::size_t i = 0; i < N; ++i) { ip[i] = (p[i] >= 0) ? (p[i] / chunk_size_) : -((- p[i] + chunk_size_ - 1) / chunk_size_); @@ -31,7 +31,7 @@ namespace psemek::pcg auto it = chunks_.find(ip); if (it == chunks_.end()) { - geom::box box; + math::box box; for (std::size_t i = 0; i < N; ++i) { box[i] = {ip[i] * chunk_size_, (ip[i] + 1) * chunk_size_}; @@ -46,9 +46,9 @@ namespace psemek::pcg return it->second(t); } - std::optional at(geom::point p) const + std::optional at(math::point p) const { - geom::vector ip; + math::vector ip; for (std::size_t i = 0; i < N; ++i) { ip[i] = (p[i] >= 0) ? (p[i] / chunk_size_) : -((- p[i] + chunk_size_ - 1) / chunk_size_); @@ -68,7 +68,7 @@ namespace psemek::pcg private: int const chunk_size_; generator_func generator_; - mutable util::hash_map, util::array> chunks_; + mutable util::hash_map, util::array> chunks_; }; diff --git a/libs/pcg/include/psemek/pcg/fractal.hpp b/libs/pcg/include/psemek/pcg/fractal.hpp index b112880c..ac3ef759 100644 --- a/libs/pcg/include/psemek/pcg/fractal.hpp +++ b/libs/pcg/include/psemek/pcg/fractal.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include #include @@ -25,12 +25,12 @@ namespace psemek::pcg template value_type operator ()(Args const & ... args) const { - return (*this)(geom::vector{args...}); + return (*this)(math::vector{args...}); } - value_type operator()(geom::vector const & p) const; + value_type operator()(math::vector const & p) const; - value_type operator()(geom::point const & p) const + value_type operator()(math::point const & p) const { return (*this)(p - p.zero()); } @@ -80,7 +80,7 @@ namespace psemek::pcg } template - typename fractal::value_type fractal::operator()(geom::vector const & p) const + typename fractal::value_type fractal::operator()(math::vector const & p) const { value_type result = value_type{}; for (std::size_t i = 0; i < octaves.size(); ++i) diff --git a/libs/pcg/include/psemek/pcg/lazy_perlin.hpp b/libs/pcg/include/psemek/pcg/lazy_perlin.hpp index 58367c1a..ec97b83a 100644 --- a/libs/pcg/include/psemek/pcg/lazy_perlin.hpp +++ b/libs/pcg/include/psemek/pcg/lazy_perlin.hpp @@ -4,8 +4,8 @@ #include #include -#include -#include +#include +#include #include @@ -16,30 +16,30 @@ namespace psemek::pcg { template - T perlin_impl(geom::point p, int grid_size, F && grid_at, G && bound) + T perlin_impl(math::point p, int grid_size, F && grid_at, G && bound) { - geom::vector ip; + math::vector ip; for (std::size_t i = 0; i < N; ++i) { ip[i] = (p[i] >= 0) ? (p[i] / grid_size) : -((- p[i] + grid_size - 1) / grid_size); } ip = bound(ip); - geom::vector t = (p - p.zero() - geom::cast(ip) * T(grid_size)) / T(grid_size); + math::vector t = (p - p.zero() - math::cast(ip) * T(grid_size)) / T(grid_size); T values[1 << N]; for (std::size_t mask = 0; mask < (1 << N); ++mask) { - geom::vector tt; - geom::vector ii; + math::vector tt; + math::vector ii; for (std::size_t i = 0; i < N; ++i) { bool m = (mask & (1 << i)); tt[i] = m ? t[i] - 1 : t[i]; ii[i] = m ? ip[i] + 1 : ip[i]; } - values[mask] = geom::dot(grid_at(ii), tt); + values[mask] = math::dot(grid_at(ii), tt); } auto smootherstep = [](T x0, T x1, T t) @@ -68,24 +68,24 @@ namespace psemek::pcg lazy_perlin_view() = default; - lazy_perlin_view(std::size_t grid_size, geom::vector const & view_origin, util::array, N> subgrid) + lazy_perlin_view(std::size_t grid_size, math::vector const & view_origin, util::array, N> subgrid) : grid_size_(grid_size) , origin_(view_origin) , subgrid_(std::move(subgrid)) {} - T operator()(geom::point p) const; + T operator()(math::point p) const; private: int grid_size_; - geom::vector origin_; - util::array, N> subgrid_; + math::vector origin_; + util::array, N> subgrid_; }; template struct lazy_perlin { - using generator_func = util::function(geom::vector const &)>; + using generator_func = util::function(math::vector const &)>; using value_type = T; static constexpr auto dimension = N; @@ -95,16 +95,16 @@ namespace psemek::pcg , gen_(std::move(generator)) {} - T operator()(geom::point p) const; + T operator()(math::point p) const; - auto subview(geom::box const & box) const; + auto subview(math::box const & box) const; private: int const grid_size_; - mutable util::hash_map, geom::vector> grid_; + mutable util::hash_map, math::vector> grid_; generator_func gen_; - geom::vector grid_at(geom::vector const & c) const + math::vector grid_at(math::vector const & c) const { auto it = grid_.find(c); if (it == grid_.end()) @@ -116,12 +116,12 @@ namespace psemek::pcg template struct bounded_lazy_perlin { - using generator_func = util::function(geom::vector const &)>; + using generator_func = util::function(math::vector const &)>; using value_type = T; static constexpr auto dimension = N; - bounded_lazy_perlin(std::size_t grid_size, generator_func generator, geom::box const & bounds) + bounded_lazy_perlin(std::size_t grid_size, generator_func generator, math::box const & bounds) : grid_size_(grid_size) , gen_(std::move(generator)) { @@ -137,16 +137,16 @@ namespace psemek::pcg grid_.resize(dims); } - T operator()(geom::point p) const; + T operator()(math::point p) const; private: int const grid_size_; - geom::vector origin_; - geom::vector corner_; - mutable util::array>, N> grid_; + math::vector origin_; + math::vector corner_; + mutable util::array>, N> grid_; generator_func gen_; - geom::vector grid_at(geom::vector const & c) const + math::vector grid_at(math::vector const & c) const { std::array idx; for (std::size_t i = 0; i < N; ++i) @@ -159,7 +159,7 @@ namespace psemek::pcg }; template - T lazy_perlin_view::operator()(geom::point p) const + T lazy_perlin_view::operator()(math::point p) const { return detail::perlin_impl(p, grid_size_, [this](auto const & c){ return subgrid_(c - origin_); @@ -174,15 +174,15 @@ namespace psemek::pcg } template - T lazy_perlin::operator()(geom::point p) const + T lazy_perlin::operator()(math::point p) const { return detail::perlin_impl(p, grid_size_, [this](auto const & c){ return grid_at(c); }, [](auto const & p){ return p; }); } template - auto lazy_perlin::subview(geom::box const & box) const + auto lazy_perlin::subview(math::box const & box) const { - geom::vector subgrid_origin; + math::vector subgrid_origin; std::array subgrid_dimensions; for (std::size_t i = 0; i < N; ++i) { @@ -191,10 +191,10 @@ namespace psemek::pcg subgrid_dimensions[i] = subgrid_max - subgrid_origin[i] + 1; } - util::array, N> subgrid(subgrid_dimensions); + util::array, N> subgrid(subgrid_dimensions); for (auto const idx : subgrid.indices()) { - geom::vector id; + math::vector id; for (std::size_t i = 0; i < N; ++i) id[i] = subgrid_origin[i] + idx[i]; subgrid(idx) = grid_at(id); @@ -204,7 +204,7 @@ namespace psemek::pcg } template - T bounded_lazy_perlin::operator()(geom::point p) const + T bounded_lazy_perlin::operator()(math::point p) const { return detail::perlin_impl(p, grid_size_, [this](auto const & c){ return grid_at(c); }, [this](auto p){ for (std::size_t i = 0; i < N; ++i) diff --git a/libs/pcg/include/psemek/pcg/perlin.hpp b/libs/pcg/include/psemek/pcg/perlin.hpp index babc663d..374b6bd5 100644 --- a/libs/pcg/include/psemek/pcg/perlin.hpp +++ b/libs/pcg/include/psemek/pcg/perlin.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -16,8 +16,8 @@ namespace psemek::pcg static constexpr std::size_t dimension = N; perlin() = default; - perlin(util::array, N> grad_map); - perlin(util::array, N> const & grad_map, seamless_tag); + perlin(util::array, N> grad_map); + perlin(util::array, N> const & grad_map, seamless_tag); perlin(perlin &&) = default; perlin & operator = (perlin &&) = default; @@ -41,26 +41,26 @@ namespace psemek::pcg template T operator()(Args const & ... args) const { - return (*this)(geom::vector{args...}); + return (*this)(math::vector{args...}); } // Coords \in [0.0 .. 1.0] - T operator()(geom::vector p) const; + T operator()(math::vector p) const; auto & grad() { return grad_map_; } auto const & grad() const { return grad_map_; } private: - util::array, N> grad_map_; + util::array, N> grad_map_; }; template - perlin::perlin(util::array, N> grad_map) + perlin::perlin(util::array, N> grad_map) : grad_map_{std::move(grad_map)} {} template - perlin::perlin(util::array, N> const & grad_map, seamless_tag) + perlin::perlin(util::array, N> const & grad_map, seamless_tag) { auto dims = grad_map.dims(); for (std::size_t i = 0; i < N; ++i) @@ -78,7 +78,7 @@ namespace psemek::pcg } template - T perlin::operator()(geom::vector p) const + T perlin::operator()(math::vector p) const { for (std::size_t i = 0; i < N; ++i) { @@ -87,19 +87,19 @@ namespace psemek::pcg p[i] *= grad_map_.dim(i) - 1; } - geom::vector ip; + math::vector ip; for (std::size_t i = 0; i < N; ++i) { - ip[i] = geom::clamp(std::floor(p[i]), {0, static_cast(grad_map_.dim(i)) - 2}); + ip[i] = math::clamp(std::floor(p[i]), {0, static_cast(grad_map_.dim(i)) - 2}); } - geom::vector t = p - geom::cast(ip); + math::vector t = p - math::cast(ip); T values[1 << N]; for (std::size_t mask = 0; mask < (1 << N); ++mask) { - geom::vector tt; + math::vector tt; std::array ii; for (std::size_t i = 0; i < N; ++i) { @@ -107,7 +107,7 @@ namespace psemek::pcg tt[i] = m ? t[i] - 1 : t[i]; ii[i] = m ? ip[i] + 1 : ip[i]; } - values[mask] = geom::dot(grad_map_(ii), tt); + values[mask] = math::dot(grad_map_(ii), tt); } auto smoothstep = [](auto x0, auto x1, auto t) diff --git a/libs/pcg/include/psemek/pcg/voronoi.hpp b/libs/pcg/include/psemek/pcg/voronoi.hpp index 6a57cc37..f96d2547 100644 --- a/libs/pcg/include/psemek/pcg/voronoi.hpp +++ b/libs/pcg/include/psemek/pcg/voronoi.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -10,8 +10,8 @@ namespace psemek::pcg struct voronoi { - voronoi(std::vector> points); - voronoi(std::vector> points, seamless_tag); + voronoi(std::vector> points); + voronoi(std::vector> points, seamless_tag); struct result_type { @@ -22,7 +22,7 @@ namespace psemek::pcg result_type operator()(float x, float y) const; private: - std::vector> points_; + std::vector> points_; bool seamless_ = false; }; diff --git a/libs/pcg/include/psemek/pcg/white.hpp b/libs/pcg/include/psemek/pcg/white.hpp index fbfa6285..410b7ba8 100644 --- a/libs/pcg/include/psemek/pcg/white.hpp +++ b/libs/pcg/include/psemek/pcg/white.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -10,7 +10,7 @@ namespace psemek::pcg { template - gfx::basic_pixmap white(std::size_t width, std::size_t height, RNG && rng, geom::interval const & range) + gfx::basic_pixmap white(std::size_t width, std::size_t height, RNG && rng, math::interval const & range) { using dist = std::conditional_t, uniform_real_distribution, uniform_int_distribution>; diff --git a/libs/pcg/source/voronoi.cpp b/libs/pcg/source/voronoi.cpp index 42553c19..89ed3997 100644 --- a/libs/pcg/source/voronoi.cpp +++ b/libs/pcg/source/voronoi.cpp @@ -3,12 +3,12 @@ namespace psemek::pcg { - voronoi::voronoi(std::vector> points) + voronoi::voronoi(std::vector> points) : points_(std::move(points)) , seamless_{false} {} - voronoi::voronoi(std::vector> points, seamless_tag) + voronoi::voronoi(std::vector> points, seamless_tag) : points_(std::move(points)) , seamless_{true} {} diff --git a/libs/phys/CMakeLists.txt b/libs/phys/CMakeLists.txt index 253ce54b..55659f3f 100644 --- a/libs/phys/CMakeLists.txt +++ b/libs/phys/CMakeLists.txt @@ -3,4 +3,4 @@ file(GLOB_RECURSE PSEMEK_PHYS_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "so psemek_add_library(psemek-phys ${PSEMEK_PHYS_HEADERS} ${PSEMEK_PHYS_SOURCES}) target_include_directories(psemek-phys PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-phys PUBLIC psemek-util psemek-geom psemek-cg psemek-prof) +target_link_libraries(psemek-phys PUBLIC psemek-util psemek-math psemek-cg psemek-prof) diff --git a/libs/phys/include/psemek/phys/common_2d.hpp b/libs/phys/include/psemek/phys/common_2d.hpp index 4aa49401..1b296609 100644 --- a/libs/phys/include/psemek/phys/common_2d.hpp +++ b/libs/phys/include/psemek/phys/common_2d.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include @@ -15,7 +15,7 @@ namespace psemek::phys2d struct half_space { // defined by normal * p <= value - geom::vector normal; + math::vector normal; float value; }; @@ -27,8 +27,8 @@ namespace psemek::phys2d struct convex_polygon { - std::vector> points; - geom::point anchor; + std::vector> points; + math::point anchor; }; struct material @@ -40,24 +40,24 @@ namespace psemek::phys2d struct static_state { - geom::point position = {0.f, 0.f}; + math::point position = {0.f, 0.f}; float rotation = 0.f; // in radians }; struct dynamic_state { - geom::vector velocity = {0.f, 0.f}; + math::vector velocity = {0.f, 0.f}; float angular_velocity = 0.f; }; struct fixed_constraint { - geom::point point, target; + math::point point, target; }; struct joint_constraint { - geom::point point1, point2; + math::point point1, point2; }; } diff --git a/libs/phys/include/psemek/phys/engine_2d.hpp b/libs/phys/include/psemek/phys/engine_2d.hpp index 4cc26247..4b824169 100644 --- a/libs/phys/include/psemek/phys/engine_2d.hpp +++ b/libs/phys/include/psemek/phys/engine_2d.hpp @@ -4,7 +4,7 @@ #include -#include +#include #include #include @@ -78,12 +78,12 @@ namespace psemek::phys2d // Global system properties - void set_gravity(geom::vector const & g); + void set_gravity(math::vector const & g); void set_air_friction(float friction); // Effects - void explode(geom::point const & center, float strength, float attenuation); + void explode(math::point const & center, float strength, float attenuation); // Main update diff --git a/libs/phys/include/psemek/phys/engine_3d.hpp b/libs/phys/include/psemek/phys/engine_3d.hpp index 06e66c72..6c683c6a 100644 --- a/libs/phys/include/psemek/phys/engine_3d.hpp +++ b/libs/phys/include/psemek/phys/engine_3d.hpp @@ -1,9 +1,9 @@ #pragma once -#include -#include -#include -#include +#include +#include +#include +#include #include @@ -20,13 +20,13 @@ namespace psemek::phys3d struct box { - geom::vector dimensions; + math::vector dimensions; }; struct half_space { // p * normal >= value - geom::vector normal; + math::vector normal; float value; }; @@ -39,11 +39,11 @@ namespace psemek::phys3d struct object_state { - geom::point position; - geom::quaternion rotation = geom::quaternion::identity(); + math::point position; + math::quaternion rotation = math::quaternion::identity(); - geom::vector velocity = {0.f, 0.f, 0.f}; - geom::vector angular_velocity = {0.f, 0.f, 0.f}; + math::vector velocity = {0.f, 0.f, 0.f}; + math::vector angular_velocity = {0.f, 0.f, 0.f}; }; struct engine @@ -91,18 +91,18 @@ namespace psemek::phys3d object_state & get_object_state(object_handle h); float get_object_mass(object_handle h) const; - geom::matrix get_object_inertia(object_handle h) const; + math::matrix get_object_inertia(object_handle h) const; void remove_object(object_handle h); object_handle object_count() const; bool is_object(object_handle h) const; - void add_impulse(object_handle h, geom::vector const & impulse); + void add_impulse(object_handle h, math::vector const & impulse); // Force management - void set_gravity(geom::vector const & g); + void set_gravity(math::vector const & g); // Update loop diff --git a/libs/phys/include/psemek/phys/shallow_water.hpp b/libs/phys/include/psemek/phys/shallow_water.hpp index 693b7dd7..ff457891 100644 --- a/libs/phys/include/psemek/phys/shallow_water.hpp +++ b/libs/phys/include/psemek/phys/shallow_water.hpp @@ -1,8 +1,8 @@ #pragma once #include -#include -#include +#include +#include namespace psemek::phys { @@ -19,13 +19,13 @@ namespace psemek::phys util::array delta_x_velocity({n + 1, m + 2}, 0.f); util::array delta_y_velocity({n + 2, m + 1}, 0.f); - auto h_dual = [&](auto i, auto j) { return geom::dual{water_height(i, j), {delta_height(i, j), 0.f}}; }; - auto ux_dual = [&](auto i, auto j) { return geom::dual{x_velocity(i, j + 1), {delta_x_velocity(i, j + 1), 0.f}}; }; - auto uy_dual = [&](auto i, auto j) { return geom::dual{y_velocity(i + 1, j), {delta_y_velocity(i + 1, j), 0.f}}; }; + auto h_dual = [&](auto i, auto j) { return math::dual{water_height(i, j), {delta_height(i, j), 0.f}}; }; + auto ux_dual = [&](auto i, auto j) { return math::dual{x_velocity(i, j + 1), {delta_x_velocity(i, j + 1), 0.f}}; }; + auto uy_dual = [&](auto i, auto j) { return math::dual{y_velocity(i + 1, j), {delta_y_velocity(i + 1, j), 0.f}}; }; - auto h_diag = [&](auto i, auto j) { return geom::dual{water_height(i, j), {0.f, 1.f}}; }; - auto ux_diag = [&](auto i, auto j) { return geom::dual{x_velocity(i, j + 1), {0.f, 1.f}}; }; - auto uy_diag = [&](auto i, auto j) { return geom::dual{y_velocity(i + 1, j), {0.f, 1.f}}; }; + auto h_diag = [&](auto i, auto j) { return math::dual{water_height(i, j), {0.f, 1.f}}; }; + auto ux_diag = [&](auto i, auto j) { return math::dual{x_velocity(i, j + 1), {0.f, 1.f}}; }; + auto uy_diag = [&](auto i, auto j) { return math::dual{y_velocity(i + 1, j), {0.f, 1.f}}; }; T error = 0; @@ -42,18 +42,18 @@ namespace psemek::phys { for (std::size_t i = 1; i < n + 1; ++i) { - geom::dual value = + math::dual value = + (h_dual(i + 1, j ) + h_diag(i, j)) * ux_dual(i , j - 1) / 2.f - (h_dual(i - 1, j ) + h_diag(i, j)) * ux_dual(i - 1, j - 1) / 2.f + (h_dual(i , j + 1) + h_diag(i, j)) * uy_dual(i - 1, j ) / 2.f - (h_dual(i , j - 1) + h_diag(i, j)) * uy_dual(i - 1, j - 1) / 2.f ; -// error += geom::sqr(value.scalar - value.delta[0] - value.delta[1] * delta_height(i, j)); - error += geom::sqr(value.scalar); +// error += math::sqr(value.scalar - value.delta[0] - value.delta[1] * delta_height(i, j)); + error += math::sqr(value.scalar); if (value.delta[1] != 0.f) - delta_height(i, j) = geom::lerp(delta_height(i, j), (-value.scalar - value.delta[0]) / value.delta[1], omega); + delta_height(i, j) = math::lerp(delta_height(i, j), (-value.scalar - value.delta[0]) / value.delta[1], omega); } } @@ -62,9 +62,9 @@ namespace psemek::phys { for (std::size_t i = 1; i < n; ++i) { - geom::dual value = - + h_dual(i + 1, j + 1) * (geom::sqr(ux_dual(i + 1, j ) + geom::sqr(ux_diag(i, j)))) / 2.f - - h_dual(i , j + 1) * (geom::sqr(ux_dual(i - 1, j ) + geom::sqr(ux_diag(i, j)))) / 2.f + math::dual value = + + h_dual(i + 1, j + 1) * (math::sqr(ux_dual(i + 1, j ) + math::sqr(ux_diag(i, j)))) / 2.f + - h_dual(i , j + 1) * (math::sqr(ux_dual(i - 1, j ) + math::sqr(ux_diag(i, j)))) / 2.f + (h_dual(i, j + 1) + h_dual(i + 1, j + 1) + h_dual(i, j + 2) + h_dual(i + 1, j + 2)) * (ux_diag(i, j) + ux_dual(i, j + 1)) * (uy_dual(i - 1, j + 1) + uy_dual(i, j + 1)) / 16.f @@ -72,18 +72,18 @@ namespace psemek::phys - (h_dual(i, j + 1) + h_dual(i + 1, j + 1) + h_dual(i, j) + h_dual(i - 1, j)) * (ux_diag(i, j) + ux_dual(i, j - 1)) * (uy_dual(i - 1, j) + uy_dual(i, j)) / 16.f - + gravity * (geom::sqr(h_dual(i + 1, j + 1)) - geom::sqr(h_dual(i, j + 1))) / 2.f + + gravity * (math::sqr(h_dual(i + 1, j + 1)) - math::sqr(h_dual(i, j + 1))) / 2.f - gravity * (h_dual(i + 1, j + 1) + h_dual(i, j + 1)) * (depth(i + 1, j + 1) - depth(i, j + 1)) / 2.f ; value = 0; -// error += geom::sqr(value.scalar - value.delta[0] - value.delta[1] * delta_x_velocity(i, j + 1)); - error += geom::sqr(value.scalar); +// error += math::sqr(value.scalar - value.delta[0] - value.delta[1] * delta_x_velocity(i, j + 1)); + error += math::sqr(value.scalar); if (value.delta[1] != 0.f) - delta_x_velocity(i, j + 1) = geom::lerp(delta_x_velocity(i, j + 1), (value.scalar - value.delta[0]) / value.delta[1], omega); + delta_x_velocity(i, j + 1) = math::lerp(delta_x_velocity(i, j + 1), (value.scalar - value.delta[0]) / value.delta[1], omega); } } @@ -92,9 +92,9 @@ namespace psemek::phys { for (std::size_t i = 0; i < n; ++i) { - geom::dual value = - + h_dual(i + 1, j + 1) * (geom::sqr(uy_dual(i , j + 1) + geom::sqr(uy_diag(i, j)))) / 2.f - - h_dual(i + 1, j ) * (geom::sqr(uy_dual(i , j - 1) + geom::sqr(uy_diag(i, j)))) / 2.f + math::dual value = + + h_dual(i + 1, j + 1) * (math::sqr(uy_dual(i , j + 1) + math::sqr(uy_diag(i, j)))) / 2.f + - h_dual(i + 1, j ) * (math::sqr(uy_dual(i , j - 1) + math::sqr(uy_diag(i, j)))) / 2.f + (h_dual(i + 1, j) + h_dual(i + 1, j + 1) + h_dual(i + 2, j) + h_dual(i + 2, j + 1)) * (uy_diag(i, j) + uy_dual(i + 1, j)) * (ux_dual(i + 1, j - 1) + ux_dual(i + 1, j)) / 16.f @@ -102,18 +102,18 @@ namespace psemek::phys - (h_dual(i + 1, j) + h_dual(i + 1, j + 1) + h_dual(i, j) + h_dual(i, j - 1)) * (uy_diag(i, j) + uy_dual(i - 1, j)) * (ux_dual(i, j - 1) + ux_dual(i, j)) / 16.f - + gravity * (geom::sqr(h_dual(i + 1, j + 1)) - geom::sqr(h_dual(i + 1, j))) / 2.f + + gravity * (math::sqr(h_dual(i + 1, j + 1)) - math::sqr(h_dual(i + 1, j))) / 2.f - gravity * (h_dual(i + 1, j + 1) + h_dual(i + 1, j)) * (depth(i + 1, j + 1) - depth(i + 1, j)) / 2.f ; value = 0; -// error += geom::sqr(value.scalar - value.delta[0] - value.delta[1] * delta_y_velocity(i + 1, j)); - error += geom::sqr(value.scalar); +// error += math::sqr(value.scalar - value.delta[0] - value.delta[1] * delta_y_velocity(i + 1, j)); + error += math::sqr(value.scalar); if (value.delta[1] != 0.f) - delta_y_velocity(i + 1, j) = geom::lerp(delta_y_velocity(i + 1, j), (-value.scalar - value.delta[0]) / value.delta[1], omega); + delta_y_velocity(i + 1, j) = math::lerp(delta_y_velocity(i + 1, j), (-value.scalar - value.delta[0]) / value.delta[1], omega); } } diff --git a/libs/phys/source/engine_2d.cpp b/libs/phys/source/engine_2d.cpp index 42f4f88e..141ed35d 100644 --- a/libs/phys/source/engine_2d.cpp +++ b/libs/phys/source/engine_2d.cpp @@ -3,12 +3,12 @@ #include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include @@ -23,7 +23,7 @@ namespace psemek::phys2d float shape_area(ball const & b) { - return geom::pi * b.radius * b.radius; + return math::pi * b.radius * b.radius; } float shape_inertia(ball const & b) @@ -51,48 +51,48 @@ namespace psemek::phys2d return shape_area(b) * (b.width * b.width + b.height * b.height) / 12.f; } - geom::point shape_closest(ball const & b, static_state const & s, geom::point const & p) + math::point shape_closest(ball const & b, static_state const & s, math::point const & p) { auto d = p - s.position; - auto l = geom::length(d); + auto l = math::length(d); if (l <= b.radius) return p; return s.position + (d / l) * b.radius; } - geom::point shape_closest(half_space const & b, static_state const &, geom::point const & p) + math::point shape_closest(half_space const & b, static_state const &, math::point const & p) { - auto v = geom::dot(b.normal, p - geom::point::zero()) - b.value; + auto v = math::dot(b.normal, p - math::point::zero()) - b.value; if (v <= 0.f) return p; return p - b.normal * v; } - geom::point shape_closest(box const & b, static_state const & s, geom::point const & p) + math::point shape_closest(box const & b, static_state const & s, math::point const & p) { - geom::vector ex{std::cos(s.rotation), std::sin(s.rotation)}; - geom::vector ey{-std::sin(s.rotation), std::cos(s.rotation)}; + math::vector ex{std::cos(s.rotation), std::sin(s.rotation)}; + math::vector ey{-std::sin(s.rotation), std::cos(s.rotation)}; float w = b.width / 2.f; float h = b.height / 2.f; auto d = p - s.position; - float x = geom::dot(d, ex); - float y = geom::dot(d, ey); + float x = math::dot(d, ex); + float y = math::dot(d, ey); - x = geom::clamp(x, {-w, w}); - y = geom::clamp(y, {-h, h}); + x = math::clamp(x, {-w, w}); + y = math::clamp(y, {-h, h}); return s.position + ex * x + ey * y; } - geom::box shape_bbox(ball const & b, static_state const & s) + math::box shape_bbox(ball const & b, static_state const & s) { return {{{s.position[0] - b.radius, s.position[0] + b.radius}, {s.position[1] - b.radius, s.position[1] + b.radius}}}; } - geom::box shape_bbox(box const & b, static_state const & s) + math::box shape_bbox(box const & b, static_state const & s) { float cc = std::abs(std::cos(s.rotation)); float ss = std::abs(std::sin(s.rotation)); @@ -106,9 +106,9 @@ namespace psemek::phys2d return {{{s.position[0] - tx, s.position[0] + tx}, {s.position[1] - ty, s.position[1] + ty}}}; } - geom::box shape_bbox(half_space const &, static_state const &) + math::box shape_bbox(half_space const &, static_state const &) { - return geom::box::full(); + return math::box::full(); } template @@ -134,8 +134,8 @@ namespace psemek::phys2d // penetration points from object 1 to object 2 struct collision { - geom::point position; - geom::vector penetration; + math::point position; + math::vector penetration; }; std::optional invert(std::optional c) @@ -151,9 +151,9 @@ namespace psemek::phys2d static constexpr float inf = std::numeric_limits::infinity(); bool invert = false; - geom::point point{0.f, 0.f}; + math::point point{0.f, 0.f}; float penetration = -inf; - geom::vector normal{0.f, 0.f}; + math::vector normal{0.f, 0.f}; // Normals of 1st body against points of 2nd body { @@ -163,10 +163,10 @@ namespace psemek::phys2d for (; n != std::end(normals1); ++p, ++n) { float min = inf; - geom::point minp{0.f, 0.f}; + math::point minp{0.f, 0.f}; for (auto const & p2 : points2) { - float v = geom::dot(p2 - *p, *n); + float v = math::dot(p2 - *p, *n); if (v < min) { min = v; @@ -194,10 +194,10 @@ namespace psemek::phys2d for (; n != std::end(normals2); ++p, ++n) { float min = inf; - geom::point minp{0.f, 0.f}; + math::point minp{0.f, 0.f}; for (auto const & p1 : points1) { - float v = geom::dot(p1 - *p, *n); + float v = math::dot(p1 - *p, *n); if (v < min) { min = v; @@ -224,11 +224,11 @@ namespace psemek::phys2d std::optional shape_collision(ball const & b1, static_state const & s1, ball const & b2, static_state const & s2) { auto const d = s2.position - s1.position; - float const l = geom::length(d); + float const l = math::length(d); if (l > b1.radius + b2.radius) return std::nullopt; - auto const p = geom::lerp(s1.position, s2.position, (b1.radius + l - b2.radius) / 2.f / l); + auto const p = math::lerp(s1.position, s2.position, (b1.radius + l - b2.radius) / 2.f / l); return collision{p, d * (b1.radius + b2.radius - l) / l}; } @@ -243,16 +243,16 @@ namespace psemek::phys2d std::optional shape_collision(ball const & b1, static_state const & s1, box const & b2, static_state const & s2) { - geom::vector ex{std::cos(s2.rotation), std::sin(s2.rotation)}; - geom::vector ey{-std::sin(s2.rotation), std::cos(s2.rotation)}; + math::vector ex{std::cos(s2.rotation), std::sin(s2.rotation)}; + math::vector ey{-std::sin(s2.rotation), std::cos(s2.rotation)}; float w = b2.width / 2.f; float h = b2.height / 2.f; auto r = s1.position - s2.position; - float x = geom::dot(r, ex); - float y = geom::dot(r, ey); + float x = math::dot(r, ex); + float y = math::dot(r, ey); if (std::abs(x) < w && std::abs(y) < h) { @@ -265,33 +265,33 @@ namespace psemek::phys2d { if (x > 0.f) { - return collision{s1.position, geom::vector{-vx - b1.radius, 0.f}}; + return collision{s1.position, math::vector{-vx - b1.radius, 0.f}}; } else { - return collision{s1.position, geom::vector{vx + b1.radius, 0.f}}; + return collision{s1.position, math::vector{vx + b1.radius, 0.f}}; } } else { if (y > 0.f) { - return collision{s1.position, geom::vector{0.f, -vy - b1.radius}}; + return collision{s1.position, math::vector{0.f, -vy - b1.radius}}; } else { - return collision{s1.position, geom::vector{0.f, vy + b1.radius}}; + return collision{s1.position, math::vector{0.f, vy + b1.radius}}; } } } else { - float cx = geom::clamp(x, {-w, w}); - float cy = geom::clamp(y, {-h, h}); + float cx = math::clamp(x, {-w, w}); + float cy = math::clamp(y, {-h, h}); - geom::vector n = ex * (cx - x) + ey * (cy - y); + math::vector n = ex * (cx - x) + ey * (cy - y); - float l = geom::length(n); + float l = math::length(n); if (l < b1.radius) { @@ -317,19 +317,19 @@ namespace psemek::phys2d std::optional shape_collision(half_space const & b1, static_state const &, box const & b2, static_state const & s2) { - geom::vector a{ b2.width / 2.f, b2.height / 2.f}; - geom::vector b{-b2.width / 2.f, b2.height / 2.f}; + math::vector a{ b2.width / 2.f, b2.height / 2.f}; + math::vector b{-b2.width / 2.f, b2.height / 2.f}; - auto const rot = geom::plane_rotation(0, 1, s2.rotation); + auto const rot = math::plane_rotation(0, 1, s2.rotation); a = rot(a); b = rot(b); float v = b1.normal[0] * s2.position[0] + b1.normal[1] * s2.position[1] - b1.value; - geom::vector d; + math::vector d; - float va = geom::dot(a, b1.normal); - float vb = geom::dot(b, b1.normal); + float va = math::dot(a, b1.normal); + float vb = math::dot(b, b1.normal); v -= std::max(std::abs(va), std::abs(vb)); if (std::abs(va) > std::abs(vb)) { @@ -370,18 +370,18 @@ namespace psemek::phys2d std::optional shape_collision(box const & b1, static_state const & s1, box const & b2, static_state const & s2) { - geom::vector ex1{ std::cos(s1.rotation), std::sin(s1.rotation)}; - geom::vector ey1{-std::sin(s1.rotation), std::cos(s1.rotation)}; + math::vector ex1{ std::cos(s1.rotation), std::sin(s1.rotation)}; + math::vector ey1{-std::sin(s1.rotation), std::cos(s1.rotation)}; - geom::vector ex2{ std::cos(s2.rotation), std::sin(s2.rotation)}; - geom::vector ey2{-std::sin(s2.rotation), std::cos(s2.rotation)}; + math::vector ex2{ std::cos(s2.rotation), std::sin(s2.rotation)}; + math::vector ey2{-std::sin(s2.rotation), std::cos(s2.rotation)}; float w1 = b1.width / 2.f; float h1 = b1.height / 2.f; float w2 = b2.width / 2.f; float h2 = b2.height / 2.f; - geom::point points1[4] = + math::point points1[4] = { s1.position - ex1 * w1 - ey1 * h1, s1.position + ex1 * w1 - ey1 * h1, @@ -389,7 +389,7 @@ namespace psemek::phys2d s1.position - ex1 * w1 + ey1 * h1, }; - geom::vector normals1[4] = + math::vector normals1[4] = { -ey1, ex1, @@ -397,7 +397,7 @@ namespace psemek::phys2d -ex1 }; - geom::point points2[4] = + math::point points2[4] = { s2.position - ex2 * w2 - ey2 * h2, s2.position + ex2 * w2 - ey2 * h2, @@ -405,7 +405,7 @@ namespace psemek::phys2d s2.position - ex2 * w2 + ey2 * h2, }; - geom::vector normals2[4] = + math::vector normals2[4] = { -ey2, ex2, @@ -419,11 +419,11 @@ namespace psemek::phys2d struct contact { std::size_t gi, i, gj, j; - geom::vector ri, rj; - geom::vector penetration; - geom::vector normal; + math::vector ri, rj; + math::vector penetration; + math::vector normal; float penetration_depth; - geom::vector velocity; + math::vector velocity; float velocity_projection; }; @@ -483,13 +483,13 @@ namespace psemek::phys2d std::vector groups; - geom::vector gravity{0.f, 0.f}; + math::vector gravity{0.f, 0.f}; float air_friction = 0.f; std::set contacts_by_velocity; std::set contacts_by_penetration; - void explode(geom::point const & center, float strength, float attenuation); + void explode(math::point const & center, float strength, float attenuation); void update(float dt); @@ -505,7 +505,7 @@ namespace psemek::phys2d float energy(); }; - void engine::impl::explode(geom::point const & center, float strength, float attenuation) + void engine::impl::explode(math::point const & center, float strength, float attenuation) { for (auto & g : groups) { @@ -513,15 +513,15 @@ namespace psemek::phys2d { if (g.infos[i].inv_mass == 0.f) continue; - auto const closest = shapes.visit([&](auto const & s) -> geom::point { return shape_closest(s.shape, g.static_states[i], center); }, g.infos[i].shape); + auto const closest = shapes.visit([&](auto const & s) -> math::point { return shape_closest(s.shape, g.static_states[i], center); }, g.infos[i].shape); auto r = closest - center; - float l = geom::length(r); + float l = math::length(r); auto J = (r / l) * strength / (1.f + l * l * attenuation); g.dynamic_states[i].velocity += J * g.infos[i].inv_mass; - g.dynamic_states[i].angular_velocity += geom::det(closest - g.static_states[i].position, J) * g.infos[i].inv_inertia; + g.dynamic_states[i].angular_velocity += math::det(closest - g.static_states[i].position, J) * g.infos[i].inv_inertia; } } } @@ -541,7 +541,7 @@ namespace psemek::phys2d void engine::impl::apply_gravity(float dt) { - if (gravity != geom::vector{0.f, 0.f}) + if (gravity != math::vector{0.f, 0.f}) { for (auto & g : groups) { @@ -624,7 +624,7 @@ namespace psemek::phys2d if (!c) return; - if (c->penetration == geom::vector::zero()) return; + if (c->penetration == math::vector::zero()) return; auto ri = c->position - sti.position; auto rj = c->position - stj.position; @@ -632,8 +632,8 @@ namespace psemek::phys2d auto & dsi = groups[gi].dynamic_states[i]; auto & dsj = groups[gj].dynamic_states[j]; - auto ui = dsi.velocity + geom::ort(ri) * dsi.angular_velocity; - auto uj = dsj.velocity + geom::ort(rj) * dsj.angular_velocity; + auto ui = dsi.velocity + math::ort(ri) * dsi.angular_velocity; + auto uj = dsj.velocity + math::ort(rj) * dsj.angular_velocity; auto const u = uj - ui; @@ -645,10 +645,10 @@ namespace psemek::phys2d ct.ri = ri; ct.rj = rj; ct.penetration = c->penetration; - ct.normal = geom::normalized(ct.penetration); - ct.penetration_depth = geom::length(ct.penetration); + ct.normal = math::normalized(ct.penetration); + ct.penetration_depth = math::length(ct.penetration); ct.velocity = u; - ct.velocity_projection = geom::dot(u, ct.normal); + ct.velocity_projection = math::dot(u, ct.normal); auto res = contacts_by_velocity.insert(ct); assert(res.second); @@ -658,7 +658,7 @@ namespace psemek::phys2d struct bbox_data { - geom::box box; + math::box box; std::size_t g, i; }; @@ -733,7 +733,7 @@ namespace psemek::phys2d auto const & mati = materials[infoi.material]; auto const & matj = materials[infoj.material]; - geom::matrix K; + math::matrix K; K[0][0] = infoi.inv_mass + infoj.inv_mass; K[1][1] = K[0][0]; K[0][1] = 0.f; @@ -754,11 +754,11 @@ namespace psemek::phys2d // Plastic sliding impulse // Normal relative velocity -> 0 // Tangential relative velocity -> unchanged - auto const J1 = - n * c.velocity_projection / geom::dot(n, K * n); + auto const J1 = - n * c.velocity_projection / math::dot(n, K * n); // Plastic sticking impulse // Normal relative velocity -> 0 // Tangential relative velocity -> 0 - auto const J2 = - *geom::solve(K, c.velocity); + auto const J2 = - *math::solve(K, c.velocity); float const e = std::sqrt(mati.elasticity * matj.elasticity); float const f = std::sqrt(mati.friction * matj.friction); @@ -767,18 +767,18 @@ namespace psemek::phys2d auto J = (1.f + e) * J1 + f * (J2 - J1); // Test for friction cone - float q = geom::length(J - n * geom::dot(J, n)); - if (q > mu * geom::dot(J, n)) + float q = math::length(J - n * math::dot(J, n)); + if (q > mu * math::dot(J, n)) { - float k = (mu * (1.f + e) * geom::dot(J1, n)) / (geom::length(J2 - n * geom::dot(J2, n)) - mu * geom::dot(n, J2 - J1)); + float k = (mu * (1.f + e) * math::dot(J1, n)) / (math::length(J2 - n * math::dot(J2, n)) - mu * math::dot(n, J2 - J1)); J = (1.f + e) * J1 + k * (J2 - J1); } auto dvi = -J * infoi.inv_mass; auto dvj = J * infoj.inv_mass; - auto dai = -geom::det(c.ri, J) * infoi.inv_inertia; - auto daj = geom::det(c.rj, J) * infoj.inv_inertia; + auto dai = -math::det(c.ri, J) * infoi.inv_inertia; + auto daj = math::det(c.rj, J) * infoj.inv_inertia; groups[c.gi].dynamic_states[c.i].velocity += dvi; groups[c.gj].dynamic_states[c.j].velocity += dvj; @@ -790,7 +790,7 @@ namespace psemek::phys2d std::size_t cti = -1, ctj = -1; - auto update_contact = [&](auto old_it, std::size_t G, std::size_t I, std::size_t i, geom::vector const & dv, float da) + auto update_contact = [&](auto old_it, std::size_t G, std::size_t I, std::size_t i, math::vector const & dv, float da) { contact const & c = *old_it; @@ -819,8 +819,8 @@ namespace psemek::phys2d auto node = contacts_by_velocity.extract(old_it); contact & cc = node.value(); - cc.velocity += (flip ? 1.f : -1.f) * (dv + geom::ort(flip ? cc.rj : cc.ri) * da); - cc.velocity_projection = geom::dot(cc.velocity, cc.normal); + cc.velocity += (flip ? 1.f : -1.f) * (dv + math::ort(flip ? cc.rj : cc.ri) * da); + cc.velocity_projection = math::dot(cc.velocity, cc.normal); auto res = contacts_by_velocity.insert(std::move(node)); assert(res.inserted); @@ -857,9 +857,9 @@ namespace psemek::phys2d { auto node = contacts_by_velocity.extract(ct); contact & cc = node.value(); - cc.velocity -= dvi + geom::ort(cc.ri) * dai; - cc.velocity += dvj + geom::ort(cc.rj) * daj; - cc.velocity_projection = geom::dot(cc.velocity, cc.normal); + cc.velocity -= dvi + math::ort(cc.ri) * dai; + cc.velocity += dvj + math::ort(cc.rj) * daj; + cc.velocity_projection = math::dot(cc.velocity, cc.normal); auto res = contacts_by_velocity.insert(std::move(node)); assert(res.inserted); @@ -911,7 +911,7 @@ namespace psemek::phys2d std::size_t cti = -1, ctj = -1; - auto update_contact = [&](auto old_it, std::size_t G, std::size_t I, std::size_t i, geom::vector const & d) + auto update_contact = [&](auto old_it, std::size_t G, std::size_t I, std::size_t i, math::vector const & d) { contact const & c = *old_it; @@ -941,7 +941,7 @@ namespace psemek::phys2d auto node = contacts_by_penetration.extract(old_it); contact & cc = node.value(); cc.penetration += (flip ? -1.f : 1.f) * d; - cc.penetration_depth = geom::dot(cc.penetration, cc.normal); + cc.penetration_depth = math::dot(cc.penetration, cc.normal); auto res = contacts_by_penetration.insert(std::move(node)); assert(res.inserted); @@ -980,7 +980,7 @@ namespace psemek::phys2d contact & cc = node.value(); cc.penetration += di; cc.penetration -= dj; - cc.penetration_depth = geom::dot(cc.penetration, cc.normal); + cc.penetration_depth = math::dot(cc.penetration, cc.normal); auto res = contacts_by_penetration.insert(std::move(node)); assert(res.inserted); @@ -1025,10 +1025,10 @@ namespace psemek::phys2d { if (g.infos[i].inv_mass == 0.f) continue; - Eg -= geom::dot(gravity, g.static_states[i].position - geom::point::zero()) / g.infos[i].inv_mass; + Eg -= math::dot(gravity, g.static_states[i].position - math::point::zero()) / g.infos[i].inv_mass; - Ek += geom::length_sqr(g.dynamic_states[i].velocity) / g.infos[i].inv_mass / 2.f; - Er += geom::sqr(g.dynamic_states[i].angular_velocity) / g.infos[i].inv_inertia / 2.f; + Ek += math::length_sqr(g.dynamic_states[i].velocity) / g.infos[i].inv_mass / 2.f; + Er += math::sqr(g.dynamic_states[i].angular_velocity) / g.infos[i].inv_inertia / 2.f; } } @@ -1171,11 +1171,11 @@ namespace psemek::phys2d constraint.resolve = [=, this]{ auto & ss = impl().groups[g].static_states[index]; - geom::vector t; + math::vector t; t[0] = c.target[0] - ss.position[0] - c.point[0] * std::cos(ss.rotation) + c.point[1] * std::sin(ss.rotation); t[1] = c.target[1] - ss.position[1] - c.point[0] * std::sin(ss.rotation) - c.point[1] * std::cos(ss.rotation); - geom::matrix J; + math::matrix J; J[0][0] = -1.f; J[0][1] = 0.f; J[0][2] = c.point[0] * std::sin(ss.rotation) + c.point[1] * std::cos(ss.rotation); @@ -1183,7 +1183,7 @@ namespace psemek::phys2d J[1][1] = -1.f; J[1][2] = - c.point[0] * std::cos(ss.rotation) + c.point[1] * std::sin(ss.rotation); - auto delta = geom::least_squares(J, -t); + auto delta = math::least_squares(J, -t); if (delta) { @@ -1208,11 +1208,11 @@ namespace psemek::phys2d auto & ss1 = impl().groups[g1].static_states[index1]; auto & ss2 = impl().groups[g2].static_states[index2]; - geom::vector t; + math::vector t; t[0] = ss2.position[0] + c.point2[0] * std::cos(ss2.rotation) - c.point2[1] * std::sin(ss2.rotation) - ss1.position[0] - c.point1[0] * std::cos(ss1.rotation) + c.point1[1] * std::sin(ss1.rotation); t[1] = ss2.position[1] + c.point2[0] * std::sin(ss2.rotation) + c.point2[1] * std::cos(ss2.rotation) - ss1.position[1] - c.point1[0] * std::sin(ss1.rotation) - c.point1[1] * std::cos(ss1.rotation); - geom::matrix J; + math::matrix J; J[0][0] = -1.f; J[0][1] = 0.f; J[0][2] = c.point1[0] * std::sin(ss1.rotation) + c.point1[1] * std::cos(ss1.rotation); @@ -1226,7 +1226,7 @@ namespace psemek::phys2d J[1][4] = 1.f; J[1][5] = c.point2[0] * std::cos(ss2.rotation) - c.point2[1] * std::sin(ss2.rotation); - auto delta = geom::least_squares(J, -t); + auto delta = math::least_squares(J, -t); if (delta) { @@ -1259,7 +1259,7 @@ namespace psemek::phys2d impl().constraint_handles.erase(std::find(impl().constraint_handles.begin(), impl().constraint_handles.end(), handle)); } - void engine::set_gravity(geom::vector const & g) + void engine::set_gravity(math::vector const & g) { impl().gravity = g; } @@ -1269,7 +1269,7 @@ namespace psemek::phys2d impl().air_friction = friction; } - void engine::explode(geom::point const & center, float strength, float attenuation) + void engine::explode(math::point const & center, float strength, float attenuation) { impl().explode(center, strength, attenuation); } diff --git a/libs/phys/source/engine_3d.cpp b/libs/phys/source/engine_3d.cpp index c3fa17e8..037a7795 100644 --- a/libs/phys/source/engine_3d.cpp +++ b/libs/phys/source/engine_3d.cpp @@ -1,8 +1,8 @@ #include -#include -#include -#include +#include +#include +#include #include #include @@ -24,7 +24,7 @@ namespace psemek::phys3d { engine::any_shape shape; float volume; - geom::vector inertia; + math::vector inertia; }; struct object @@ -34,14 +34,14 @@ namespace psemek::phys3d object_state state; float inv_mass; - geom::vector inertia; + math::vector inertia; - geom::matrix global_inv_inertia = {}; + math::matrix global_inv_inertia = {}; }; float volume(ball const & s) { - return 4.f / 3.f * geom::pi * s.radius * s.radius * s.radius; + return 4.f / 3.f * math::pi * s.radius * s.radius * s.radius; } float volume(box const & s) @@ -54,13 +54,13 @@ namespace psemek::phys3d return inf; } - geom::vector inertia(ball const & s) + math::vector inertia(ball const & s) { float const I = 0.4f * s.radius * s.radius; return {I, I, I}; } - geom::vector inertia(box const & s) + math::vector inertia(box const & s) { float const x = s.dimensions[0] * s.dimensions[0] / 12.f; float const y = s.dimensions[1] * s.dimensions[1] / 12.f; @@ -69,15 +69,15 @@ namespace psemek::phys3d return {y + z, x + z, x + y}; } - geom::vector inertia(half_space const &) + math::vector inertia(half_space const &) { return {inf, inf, inf}; } struct collision_data { - geom::vector point1, point2; - geom::vector normal; + math::vector point1, point2; + math::vector normal; // depth should be equal to dot(position 2 + point 2 - position1 - point1, normal) float depth; }; @@ -99,7 +99,7 @@ namespace psemek::phys3d std::optional collision(object_state const & state1, ball const & shape1, object_state const & state2, ball const & shape2) { auto r = state2.position - state1.position; - auto l = geom::length(r); + auto l = math::length(r); if (l >= shape1.radius + shape2.radius) return std::nullopt; @@ -121,7 +121,7 @@ namespace psemek::phys3d std::optional collision(object_state const & state1, ball const & shape1, object_state const & /* state2 */, half_space const & shape2) { - auto v = geom::dot(shape2.normal, state1.position - state1.position.zero()) - shape2.value; + auto v = math::dot(shape2.normal, state1.position - state1.position.zero()) - shape2.value; if (v >= shape1.radius) return std::nullopt; @@ -140,25 +140,25 @@ namespace psemek::phys3d float const slop = -0.001f; (void)C; - geom::vector normals[15]; + math::vector normals[15]; - normals[0] = geom::rotate(state1.rotation, {1.f, 0.f, 0.f}); - normals[1] = geom::rotate(state1.rotation, {0.f, 1.f, 0.f}); - normals[2] = geom::rotate(state1.rotation, {0.f, 0.f, 1.f}); + normals[0] = math::rotate(state1.rotation, {1.f, 0.f, 0.f}); + normals[1] = math::rotate(state1.rotation, {0.f, 1.f, 0.f}); + normals[2] = math::rotate(state1.rotation, {0.f, 0.f, 1.f}); - normals[3] = geom::rotate(state2.rotation, {1.f, 0.f, 0.f}); - normals[4] = geom::rotate(state2.rotation, {0.f, 1.f, 0.f}); - normals[5] = geom::rotate(state2.rotation, {0.f, 0.f, 1.f}); + normals[3] = math::rotate(state2.rotation, {1.f, 0.f, 0.f}); + normals[4] = math::rotate(state2.rotation, {0.f, 1.f, 0.f}); + normals[5] = math::rotate(state2.rotation, {0.f, 0.f, 1.f}); - normals[ 6] = geom::cross(normals[0], normals[3]); - normals[ 7] = geom::cross(normals[0], normals[4]); - normals[ 8] = geom::cross(normals[0], normals[5]); - normals[ 9] = geom::cross(normals[1], normals[3]); - normals[10] = geom::cross(normals[1], normals[4]); - normals[11] = geom::cross(normals[1], normals[5]); - normals[12] = geom::cross(normals[2], normals[3]); - normals[13] = geom::cross(normals[2], normals[4]); - normals[14] = geom::cross(normals[2], normals[5]); + normals[ 6] = math::cross(normals[0], normals[3]); + normals[ 7] = math::cross(normals[0], normals[4]); + normals[ 8] = math::cross(normals[0], normals[5]); + normals[ 9] = math::cross(normals[1], normals[3]); + normals[10] = math::cross(normals[1], normals[4]); + normals[11] = math::cross(normals[1], normals[5]); + normals[12] = math::cross(normals[2], normals[3]); + normals[13] = math::cross(normals[2], normals[4]); + normals[14] = math::cross(normals[2], normals[5]); auto tx1 = normals[0] * shape1.dimensions[0] / 2.f; auto ty1 = normals[1] * shape1.dimensions[1] / 2.f; @@ -168,23 +168,23 @@ namespace psemek::phys3d auto ty2 = normals[4] * shape2.dimensions[1] / 2.f; auto tz2 = normals[5] * shape2.dimensions[2] / 2.f; - geom::vector max_n{0.f, 0.f, 0.f}; + math::vector max_n{0.f, 0.f, 0.f}; float max_v = -inf; - geom::vector max_p1{0.f, 0.f, 0.f}; - geom::vector max_p2{0.f, 0.f, 0.f}; + math::vector max_p1{0.f, 0.f, 0.f}; + math::vector max_p2{0.f, 0.f, 0.f}; - static const auto zero = geom::point::zero(); + static const auto zero = math::point::zero(); for (auto & n : normals) { - auto nl = geom::length(n); + auto nl = math::length(n); if (nl == 0.f) continue; n /= nl; - geom::interval i1, i2; + math::interval i1, i2; - geom::vector min_pp1, max_pp1; - geom::vector min_pp2, max_pp2; + math::vector min_pp1, max_pp1; + math::vector min_pp2, max_pp2; for (float x : {-1.f, 1.f}) { @@ -193,7 +193,7 @@ namespace psemek::phys3d for (float z : {-1.f, 1.f}) { auto p = tx1 * x + ty1 * y + tz1 * z; - float v = geom::dot(n, state1.position + p - zero); + float v = math::dot(n, state1.position + p - zero); if (v < i1.min) { i1.min = v; @@ -215,7 +215,7 @@ namespace psemek::phys3d for (float z : {-1.f, 1.f}) { auto p = tx2 * x + ty2 * y + tz2 * z; - float v = geom::dot(n, state2.position + p - zero); + float v = math::dot(n, state2.position + p - zero); if (v < i2.min) { i2.min = v; @@ -272,12 +272,12 @@ namespace psemek::phys3d { for (float tz : {-1.f, 1.f}) { - geom::vector t{tx, ty, tz}; - t = geom::rotate(state1.rotation, geom::pointwise_mult(t, shape1.dimensions / 2.f)); + math::vector t{tx, ty, tz}; + t = math::rotate(state1.rotation, math::pointwise_mult(t, shape1.dimensions / 2.f)); auto p = state1.position + t; - auto v = geom::dot(shape2.normal, p - p.zero()) - shape2.value; + auto v = math::dot(shape2.normal, p - p.zero()) - shape2.value; if (v < min_v) min_v = v; @@ -288,7 +288,7 @@ namespace psemek::phys3d float const C = 1000.f; float const slop = -0.001f; - geom::vector sum_r{0.f, 0.f, 0.f}; + math::vector sum_r{0.f, 0.f, 0.f}; float sum_w = 0.f; for (float tx : {-1.f, 1.f}) @@ -297,12 +297,12 @@ namespace psemek::phys3d { for (float tz : {-1.f, 1.f}) { - geom::vector t{tx, ty, tz}; - t = geom::rotate(state1.rotation, geom::pointwise_mult(t, shape1.dimensions / 2.f)); + math::vector t{tx, ty, tz}; + t = math::rotate(state1.rotation, math::pointwise_mult(t, shape1.dimensions / 2.f)); auto p = state1.position + t; - auto v = geom::dot(shape2.normal, p - p.zero()) - shape2.value; + auto v = math::dot(shape2.normal, p - p.zero()) - shape2.value; float w = std::exp(- C * (v - min_v)); @@ -347,7 +347,7 @@ namespace psemek::phys3d util::flat_list objects; std::vector object_mask; - geom::vector gravity{0.f, 0.f, 0.f}; + math::vector gravity{0.f, 0.f, 0.f}; float collision_bias = 0.25f; template @@ -385,9 +385,9 @@ namespace psemek::phys3d auto J = jacobian_fn(); auto limits = limits_fn(); - auto M_inv_J_T = geom::transpose(J); + auto M_inv_J_T = math::transpose(J); - geom::vector v; + math::vector v; for (std::size_t i = 0; i < N; ++i) { object const & obj = objects[handles[i]]; @@ -404,7 +404,7 @@ namespace psemek::phys3d M_inv_J_T[6 * i + 1][j] *= obj.inv_mass; M_inv_J_T[6 * i + 2][j] *= obj.inv_mass; - geom::vector r; + math::vector r; r[0] = M_inv_J_T[6 * i + 3][j]; r[1] = M_inv_J_T[6 * i + 4][j]; r[2] = M_inv_J_T[6 * i + 5][j]; @@ -417,13 +417,13 @@ namespace psemek::phys3d } } - auto lambda = geom::solve(J * M_inv_J_T, - J * v - f); + auto lambda = math::solve(J * M_inv_J_T, - J * v - f); if (!lambda) return; for (std::size_t i = 0; i < lambda->dimension(); ++i) - (*lambda)[i] = geom::clamp((*lambda)[i], limits[i]); + (*lambda)[i] = math::clamp((*lambda)[i], limits[i]); auto dv = M_inv_J_T * (*lambda); @@ -442,16 +442,16 @@ namespace psemek::phys3d engine::impl::solver engine::impl::make_collision_solver(object_handle h1, object_handle h2, collision_data const & c, float dt) { - auto tx = geom::ort(c.normal); - auto ty = geom::cross(c.normal, tx); + auto tx = math::ort(c.normal); + auto ty = math::cross(c.normal, tx); auto value_fn = [this, c, dt, h1, h2]{ auto const & s1 = objects[h1].state; auto const & s2 = objects[h2].state; - auto v1 = s1.velocity + geom::cross(s1.angular_velocity, c.point1); - auto v2 = s2.velocity + geom::cross(s2.angular_velocity, c.point2); + auto v1 = s1.velocity + math::cross(s1.angular_velocity, c.point1); + auto v2 = s2.velocity + math::cross(s2.angular_velocity, c.point2); auto dv = v2 - v1; @@ -463,12 +463,12 @@ namespace psemek::phys3d // float f = std::sqrt(m1.friction * m2.friction); // float fc = std::exp(- f * dt); - return geom::vector{ - std::min(c.depth * collision_bias / dt, geom::dot(c.normal, dv) * b), + return math::vector{ + std::min(c.depth * collision_bias / dt, math::dot(c.normal, dv) * b), 0.f, 0.f, -// - fc * geom::dot(tx, dv), -// - fc * geom::dot(ty, dv), +// - fc * math::dot(tx, dv), +// - fc * math::dot(ty, dv), }; }; @@ -476,16 +476,16 @@ namespace psemek::phys3d // f = dot(n, p2 - p1) // df = dot(n, v2 + cross(omega2, r2) - v1 - cross(omega1, r1)) - auto t1 = geom::cross(c.point1, c.normal); - auto t2 = geom::cross(c.point2, c.normal); + auto t1 = math::cross(c.point1, c.normal); + auto t2 = math::cross(c.point2, c.normal); - auto tx1 = geom::cross(c.point1, tx); - auto tx2 = geom::cross(c.point2, tx); + auto tx1 = math::cross(c.point1, tx); + auto tx2 = math::cross(c.point2, tx); - auto ty1 = geom::cross(c.point1, ty); - auto ty2 = geom::cross(c.point2, ty); + auto ty1 = math::cross(c.point1, ty); + auto ty2 = math::cross(c.point2, ty); - geom::matrix J; + math::matrix J; J[0][0] = -c.normal[0]; J[0][1] = -c.normal[1]; @@ -546,7 +546,7 @@ namespace psemek::phys3d float f = std::sqrt(m1.friction * m2.friction) * dt * 0; // float fc = std::exp(- f * dt); - return geom::box{{ + return math::box{{ {0.f, inf}, // {-inf, inf}, // {-inf, inf}, @@ -568,8 +568,8 @@ namespace psemek::phys3d if (!is_object(h)) continue; auto & o = objects[h]; - auto R = geom::quaternion_rotation(o.state.rotation).linear_matrix(); - if (auto i = geom::inverse(R * geom::matrix::diagonal(o.inertia) * geom::transpose(R)); i && o.inv_mass != 0.f) + auto R = math::quaternion_rotation(o.state.rotation).linear_matrix(); + if (auto i = math::inverse(R * math::matrix::diagonal(o.inertia) * math::transpose(R)); i && o.inv_mass != 0.f) o.global_inv_inertia = *i; else o.global_inv_inertia = o.global_inv_inertia.zero(); @@ -606,7 +606,7 @@ namespace psemek::phys3d for (auto & o : objects) { o.state.position += o.state.velocity * dt; - o.state.rotation = geom::normalized(geom::exp(geom::quaternion::vector(o.state.angular_velocity * dt)) * o.state.rotation); + o.state.rotation = math::normalized(math::exp(math::quaternion::vector(o.state.angular_velocity * dt)) * o.state.rotation); } } @@ -693,9 +693,9 @@ namespace psemek::phys3d return 1.f / impl().objects[h].inv_mass; } - geom::matrix engine::get_object_inertia(object_handle h) const + math::matrix engine::get_object_inertia(object_handle h) const { - return *geom::inverse(impl().objects[h].global_inv_inertia); + return *math::inverse(impl().objects[h].global_inv_inertia); } void engine::remove_object(object_handle h) @@ -714,12 +714,12 @@ namespace psemek::phys3d return impl().is_object(h); } - void engine::add_impulse(object_handle h, geom::vector const & impulse) + void engine::add_impulse(object_handle h, math::vector const & impulse) { impl().objects[h].state.velocity += impulse * impl().objects[h].inv_mass; } - void engine::set_gravity(geom::vector const & g) + void engine::set_gravity(math::vector const & g) { impl().gravity = g; } diff --git a/libs/random/CMakeLists.txt b/libs/random/CMakeLists.txt index 7eae7e9b..bd004d25 100644 --- a/libs/random/CMakeLists.txt +++ b/libs/random/CMakeLists.txt @@ -5,4 +5,4 @@ file(GLOB_RECURSE PSEMEK_RANDOM_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" " psemek_add_library(psemek-random ${PSEMEK_RANDOM_HEADERS} ${PSEMEK_RANDOM_SOURCES}) target_include_directories(psemek-random PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-random PUBLIC psemek-util psemek-geom Boost::random) +target_link_libraries(psemek-random PUBLIC psemek-util psemek-math Boost::random) diff --git a/libs/random/include/psemek/random/normal.hpp b/libs/random/include/psemek/random/normal.hpp index cb761fb1..061559b9 100644 --- a/libs/random/include/psemek/random/normal.hpp +++ b/libs/random/include/psemek/random/normal.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include @@ -62,7 +62,7 @@ namespace psemek::random T const u2 = d(rng); T const r = std::sqrt(- 2 * std::log(u1)); - T const th = 2 * geom::pi * u2; + T const th = 2 * math::pi * u2; T const z1 = mean_ + stddev_ * r * std::cos(th); T const z2 = mean_ + stddev_ * r * std::sin(th); diff --git a/libs/random/include/psemek/random/uniform.hpp b/libs/random/include/psemek/random/uniform.hpp index ec277514..777acf06 100644 --- a/libs/random/include/psemek/random/uniform.hpp +++ b/libs/random/include/psemek/random/uniform.hpp @@ -3,8 +3,8 @@ #include #include -#include -#include +#include +#include #include @@ -18,7 +18,7 @@ namespace psemek::random using uniform_distribution = std::conditional_t, uniform_real_distribution, uniform_int_distribution>; template - T uniform(RNG && rng, geom::interval const & range) + T uniform(RNG && rng, math::interval const & range) { return uniform_distribution{range}(rng); } @@ -47,9 +47,9 @@ namespace psemek::random } template - geom::point uniform(RNG && rng, geom::box const & box) + math::point uniform(RNG && rng, math::box const & box) { - geom::point result; + math::point result; for (std::size_t i = 0; i < N; ++i) result[i] = uniform(rng, box[i]); return result; @@ -58,7 +58,7 @@ namespace psemek::random template T uniform_angle(RNG && rng) { - return uniform(rng, T{0}, T{2 * geom::pi}); + return uniform(rng, T{0}, T{2 * math::pi}); } template diff --git a/libs/random/include/psemek/random/uniform_ball.hpp b/libs/random/include/psemek/random/uniform_ball.hpp index c2a3f65c..aeda3741 100644 --- a/libs/random/include/psemek/random/uniform_ball.hpp +++ b/libs/random/include/psemek/random/uniform_ball.hpp @@ -9,7 +9,7 @@ namespace psemek::random template struct uniform_ball_vector_distribution { - using result_type = geom::vector; + using result_type = math::vector; uniform_ball_vector_distribution(T r = T{1}) : sphere_d_{r} @@ -34,7 +34,7 @@ namespace psemek::random template struct uniform_ball_point_distribution { - using result_type = geom::point; + using result_type = math::point; uniform_ball_point_distribution(result_type origin, T r = T{1}) : origin_{origin} diff --git a/libs/random/include/psemek/random/uniform_box.hpp b/libs/random/include/psemek/random/uniform_box.hpp index 89f63938..5675a134 100644 --- a/libs/random/include/psemek/random/uniform_box.hpp +++ b/libs/random/include/psemek/random/uniform_box.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include namespace psemek::random @@ -10,9 +10,9 @@ namespace psemek::random template struct uniform_box_point_distribution { - using result_type = geom::point; + using result_type = math::point; - uniform_box_point_distribution(geom::box const & b) + uniform_box_point_distribution(math::box const & b) { for (std::size_t i = 0; i < N; ++i) { @@ -25,15 +25,15 @@ namespace psemek::random template auto operator()(RNG && rng) { - geom::point p; + math::point p; for (std::size_t i = 0; i < N; ++i) p[i] = d_[i](rng); return p; } - geom::box box() const + math::box box() const { - geom::box result; + math::box result; for (std::size_t i = 0; i < N; ++i) result[i] = d_[i].range(); return result; diff --git a/libs/random/include/psemek/random/uniform_hemiball.hpp b/libs/random/include/psemek/random/uniform_hemiball.hpp index 25baadd7..427d868d 100644 --- a/libs/random/include/psemek/random/uniform_hemiball.hpp +++ b/libs/random/include/psemek/random/uniform_hemiball.hpp @@ -8,9 +8,9 @@ namespace psemek::random template struct uniform_hemiball_vector_distribution { - using result_type = geom::vector; + using result_type = math::vector; - uniform_hemiball_vector_distribution(geom::vector const & n, T r = T{1}) + uniform_hemiball_vector_distribution(math::vector const & n, T r = T{1}) : sphere_d_{n, r} {} @@ -33,9 +33,9 @@ namespace psemek::random template struct uniform_hemiball_point_distribution { - using result_type = geom::point; + using result_type = math::point; - uniform_hemiball_point_distribution(result_type origin, geom::vector const & n, T r = T{1}) + uniform_hemiball_point_distribution(result_type origin, math::vector const & n, T r = T{1}) : origin_{origin} , vector_d_{n, r} {} diff --git a/libs/random/include/psemek/random/uniform_hemisphere.hpp b/libs/random/include/psemek/random/uniform_hemisphere.hpp index 83963917..a3eeb3e7 100644 --- a/libs/random/include/psemek/random/uniform_hemisphere.hpp +++ b/libs/random/include/psemek/random/uniform_hemisphere.hpp @@ -8,9 +8,9 @@ namespace psemek::random template struct uniform_hemisphere_vector_distribution { - using result_type = geom::vector; + using result_type = math::vector; - uniform_hemisphere_vector_distribution(geom::vector const & n, T r = T{1}) + uniform_hemisphere_vector_distribution(math::vector const & n, T r = T{1}) : d_{r} , n_{n} {} @@ -30,15 +30,15 @@ namespace psemek::random private: uniform_sphere_vector_distribution d_; - geom::vector n_; + math::vector n_; }; template struct uniform_hemisphere_point_distribution { - using result_type = geom::point; + using result_type = math::point; - uniform_hemisphere_point_distribution(result_type origin, geom::vector const & n, T r = T{1}) + uniform_hemisphere_point_distribution(result_type origin, math::vector const & n, T r = T{1}) : origin_{origin} , vector_d_{n, r} {} diff --git a/libs/random/include/psemek/random/uniform_int.hpp b/libs/random/include/psemek/random/uniform_int.hpp index 18add863..1eebd18f 100644 --- a/libs/random/include/psemek/random/uniform_int.hpp +++ b/libs/random/include/psemek/random/uniform_int.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -23,7 +23,7 @@ namespace psemek::random : range_{min, max} {} - uniform_int_distribution(geom::interval range) + uniform_int_distribution(math::interval range) : range_(range) {} @@ -32,13 +32,13 @@ namespace psemek::random template T operator()(RNG && rng); - geom::interval range() const { return range_; } + math::interval range() const { return range_; } private: - geom::interval range_; + math::interval range_; template - static T generate(RNG & rng, geom::interval const & range); + static T generate(RNG & rng, math::interval const & range); }; template @@ -50,7 +50,7 @@ namespace psemek::random template template - T uniform_int_distribution::generate(RNG & rng, geom::interval const & range) + T uniform_int_distribution::generate(RNG & rng, math::interval const & range) { using ctype = std::common_type_t>; diff --git a/libs/random/include/psemek/random/uniform_real.hpp b/libs/random/include/psemek/random/uniform_real.hpp index 7209ef96..377197dc 100644 --- a/libs/random/include/psemek/random/uniform_real.hpp +++ b/libs/random/include/psemek/random/uniform_real.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -23,7 +23,7 @@ namespace psemek::random : range_{min, max} {} - uniform_real_distribution(geom::interval range) + uniform_real_distribution(math::interval range) : range_(range) {} @@ -32,10 +32,10 @@ namespace psemek::random template T operator()(RNG && rng); - geom::interval range() const { return range_; } + math::interval range() const { return range_; } private: - geom::interval range_; + math::interval range_; template T generate(RNG & rng); diff --git a/libs/random/include/psemek/random/uniform_sphere.hpp b/libs/random/include/psemek/random/uniform_sphere.hpp index 69bc1dbb..86963477 100644 --- a/libs/random/include/psemek/random/uniform_sphere.hpp +++ b/libs/random/include/psemek/random/uniform_sphere.hpp @@ -1,8 +1,8 @@ #pragma once -#include -#include -#include +#include +#include +#include #include #include @@ -12,7 +12,7 @@ namespace psemek::random template struct uniform_sphere_vector_distribution { - using result_type = geom::vector; + using result_type = math::vector; uniform_sphere_vector_distribution(T r = T{1}) : r_{r} @@ -45,7 +45,7 @@ namespace psemek::random template struct uniform_sphere_vector_distribution { - using result_type = geom::vector; + using result_type = math::vector; uniform_sphere_vector_distribution(T r = T{1}) : r_{r} @@ -54,7 +54,7 @@ namespace psemek::random uniform_sphere_vector_distribution(uniform_sphere_vector_distribution const &) = default; template - geom::vector operator()(RNG && rng) + math::vector operator()(RNG && rng) { uniform_int_distribution d{0, 1}; if (d(rng) == 0) @@ -71,7 +71,7 @@ namespace psemek::random template struct uniform_sphere_vector_distribution { - using result_type = geom::vector; + using result_type = math::vector; uniform_sphere_vector_distribution(T r = T{1}) : r_{r} @@ -82,7 +82,7 @@ namespace psemek::random template result_type operator()(RNG && rng) { - uniform_real_distribution d{0, 2 * geom::pi}; + uniform_real_distribution d{0, 2 * math::pi}; T a = d(rng); return r_ * result_type{std::cos(a), std::sin(a)}; } @@ -96,7 +96,7 @@ namespace psemek::random template struct uniform_sphere_vector_distribution { - using result_type = geom::vector; + using result_type = math::vector; uniform_sphere_vector_distribution(T r = T{1}) : r_{r} @@ -131,7 +131,7 @@ namespace psemek::random template struct uniform_sphere_point_distribution { - using result_type = geom::point; + using result_type = math::point; uniform_sphere_point_distribution(result_type origin, T r = T{1}) : origin_{origin} diff --git a/libs/sdl2/include/psemek/sdl2/cursor.hpp b/libs/sdl2/include/psemek/sdl2/cursor.hpp index 7f08ddb9..d7dfd8b1 100644 --- a/libs/sdl2/include/psemek/sdl2/cursor.hpp +++ b/libs/sdl2/include/psemek/sdl2/cursor.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include @@ -27,7 +27,7 @@ namespace psemek::sdl2 struct cursor; std::shared_ptr get_default_cursor(default_cursor_type type); - std::shared_ptr make_cursor(gfx::pixmap_rgba const & image, geom::point const & pivot); + std::shared_ptr make_cursor(gfx::pixmap_rgba const & image, math::point const & pivot); void set_cursor(cursor const & cursor); diff --git a/libs/sdl2/include/psemek/sdl2/window.hpp b/libs/sdl2/include/psemek/sdl2/window.hpp index 4afaed52..dc344505 100644 --- a/libs/sdl2/include/psemek/sdl2/window.hpp +++ b/libs/sdl2/include/psemek/sdl2/window.hpp @@ -18,7 +18,7 @@ namespace psemek::sdl2 window(psemek::app::application::options const & options); ~window(); - geom::vector size() const; + math::vector size() const; void show(); void swap(); diff --git a/libs/sdl2/source/cursor.cpp b/libs/sdl2/source/cursor.cpp index 65e3accd..20914214 100644 --- a/libs/sdl2/source/cursor.cpp +++ b/libs/sdl2/source/cursor.cpp @@ -71,7 +71,7 @@ namespace psemek::sdl2 return std::make_shared(std::move(cursor_ptr)); } - std::shared_ptr make_cursor(gfx::pixmap_rgba const & image, geom::point const & pivot) + std::shared_ptr make_cursor(gfx::pixmap_rgba const & image, math::point const & pivot) { sdl_surface_ptr surface_ptr{SDL_CreateRGBSurfaceFrom((void *)image.data(), image.width(), image.height(), 32, 4 * image.width(), 0x000000ffu, 0x0000ff00u, 0x00ff0000u, 0xff000000u)}; if (!surface_ptr) diff --git a/libs/sdl2/source/window.cpp b/libs/sdl2/source/window.cpp index 6b57a07b..b673046a 100644 --- a/libs/sdl2/source/window.cpp +++ b/libs/sdl2/source/window.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #if defined(PSEMEK_GRAPHICS_API_OPENGL) @@ -163,9 +163,9 @@ namespace psemek::sdl2 #endif } - geom::vector window::size() const + math::vector window::size() const { - geom::vector result; + math::vector result; SDL_GL_GetDrawableSize(window_, &result[0], &result[1]); return result; } @@ -224,14 +224,14 @@ namespace psemek::sdl2 int top_border, left_border, bottom_border, right_border; SDL_GetWindowBordersSize(window_, &top_border, &left_border, &bottom_border, &right_border); - geom::box bounds_rect; + math::box bounds_rect; bounds_rect[0] = {display_usable_bounds.x, display_usable_bounds.x + display_usable_bounds.w}; bounds_rect[1] = {display_usable_bounds.y, display_usable_bounds.y + display_usable_bounds.h}; log::info() << "Display usable bounds: " << bounds_rect; log::info() << "Window borders: left " << left_border << ", right " << right_border << ", top " << top_border << ", bottom " << bottom_border; - geom::box window_rect = bounds_rect; + math::box window_rect = bounds_rect; window_rect[0].min += left_border; window_rect[0].max -= right_border; window_rect[1].min += top_border; diff --git a/libs/ui_legacy/CMakeLists.txt b/libs/ui_legacy/CMakeLists.txt index 289a4fbf..109252e6 100644 --- a/libs/ui_legacy/CMakeLists.txt +++ b/libs/ui_legacy/CMakeLists.txt @@ -3,7 +3,7 @@ file(GLOB_RECURSE PSEMEK_UI_SOURCES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "sour psemek_add_library(psemek-ui ${PSEMEK_UI_HEADERS} ${PSEMEK_UI_SOURCES}) target_include_directories(psemek-ui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") -target_link_libraries(psemek-ui PUBLIC psemek-util psemek-log psemek-geom psemek-cg psemek-gfx psemek-async psemek-sdl2 rapidjson) +target_link_libraries(psemek-ui PUBLIC psemek-util psemek-log psemek-math psemek-cg psemek-gfx psemek-async psemek-sdl2 rapidjson) psemek_glob_resources(psemek-ui resources psemek/ui/resources) diff --git a/libs/ui_legacy/include/psemek/ui/box_shape.hpp b/libs/ui_legacy/include/psemek/ui/box_shape.hpp index 03438506..5f415b5e 100644 --- a/libs/ui_legacy/include/psemek/ui/box_shape.hpp +++ b/libs/ui_legacy/include/psemek/ui/box_shape.hpp @@ -8,16 +8,16 @@ namespace psemek::ui struct box_shape : shape { - geom::box box{{{0.f, 0.f}, {0.f, 0.f}}}; + math::box box{{{0.f, 0.f}, {0.f, 0.f}}}; box_shape() = default; - box_shape(geom::box box) + box_shape(math::box box) : box{box} {} - bool contains(geom::point const & point) const override; - geom::box bbox() const override { return box; } + bool contains(math::point const & point) const override; + math::box bbox() const override { return box; } }; } diff --git a/libs/ui_legacy/include/psemek/ui/color_view.hpp b/libs/ui_legacy/include/psemek/ui/color_view.hpp index 37399cd1..3ef942c5 100644 --- a/libs/ui_legacy/include/psemek/ui/color_view.hpp +++ b/libs/ui_legacy/include/psemek/ui/color_view.hpp @@ -16,10 +16,10 @@ namespace psemek::ui virtual void set_square(bool value) { square_ = value; } struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & box) override { shape_.box = box; } + void reshape(math::box const & box) override { shape_.box = box; } - geom::interval width_constraints(float height) const override; - geom::interval height_constraints(float width) const override; + math::interval width_constraints(float height) const override; + math::interval height_constraints(float width) const override; void draw(painter & p) const override; diff --git a/libs/ui_legacy/include/psemek/ui/controller.hpp b/libs/ui_legacy/include/psemek/ui/controller.hpp index ec7141e9..e155ac25 100644 --- a/libs/ui_legacy/include/psemek/ui/controller.hpp +++ b/libs/ui_legacy/include/psemek/ui/controller.hpp @@ -24,7 +24,7 @@ namespace psemek::ui void set_post_draw_effect(util::function effect); - void reshape(geom::box const & shape); + void reshape(math::box const & shape); bool event(mouse_move const & e); bool event(mouse_click const & e); diff --git a/libs/ui_legacy/include/psemek/ui/edit.hpp b/libs/ui_legacy/include/psemek/ui/edit.hpp index 4374dd37..be512c56 100644 --- a/libs/ui_legacy/include/psemek/ui/edit.hpp +++ b/libs/ui_legacy/include/psemek/ui/edit.hpp @@ -64,9 +64,9 @@ namespace psemek::ui virtual bool editing() const; struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override; + void reshape(math::box const & bbox) override; - geom::box size_constraints() const override; + math::box size_constraints() const override; void update(float dt) override; @@ -74,7 +74,7 @@ namespace psemek::ui protected: - void set_text_shape(geom::box const & box); + void set_text_shape(math::box const & box); private: std::u32string text_; @@ -94,7 +94,7 @@ namespace psemek::ui bool caret_visible_ = true; box_shape shape_; - geom::box text_box_; + math::box text_box_; validator_type validator_; @@ -106,13 +106,13 @@ namespace psemek::ui { struct image { - geom::box texcoords; - geom::box position; + math::box texcoords; + math::box position; }; gfx::texture_2d const * texture = nullptr; std::vector images; - geom::vector size{0.f, 0.f}; + math::vector size{0.f, 0.f}; }; mutable std::optional cached_state_; diff --git a/libs/ui_legacy/include/psemek/ui/element.hpp b/libs/ui_legacy/include/psemek/ui/element.hpp index 7faafb96..1245a28c 100644 --- a/libs/ui_legacy/include/psemek/ui/element.hpp +++ b/libs/ui_legacy/include/psemek/ui/element.hpp @@ -33,7 +33,7 @@ namespace psemek::ui virtual async::event_loop * loop() const; virtual void set_loop(async::event_loop * loop); - virtual geom::box events_bbox() const; + virtual math::box events_bbox() const; virtual bool on_event(mouse_move const &) { return false; } virtual bool on_event(mouse_click const &) { return false; } @@ -44,17 +44,17 @@ namespace psemek::ui virtual bool focused() const { return false; } virtual struct shape const & shape() const = 0; - virtual void reshape(geom::box const & bbox) = 0; + virtual void reshape(math::box const & bbox) = 0; virtual void reshape(); // Constraints on the element's width & height, assuming unlimited available space - virtual geom::box size_constraints() const; + virtual math::box size_constraints() const; // Constraints on the element's width, assuming fixed height - virtual geom::interval width_constraints(float height) const; + virtual math::interval width_constraints(float height) const; // Constraints on the element's height, assuming fixed width - virtual geom::interval height_constraints(float width) const; + virtual math::interval height_constraints(float width) const; virtual bool enabled() const { return enabled_; } virtual void set_enabled(bool value) { enabled_ = value; } diff --git a/libs/ui_legacy/include/psemek/ui/event.hpp b/libs/ui_legacy/include/psemek/ui/event.hpp index 49844f22..b7184735 100644 --- a/libs/ui_legacy/include/psemek/ui/event.hpp +++ b/libs/ui_legacy/include/psemek/ui/event.hpp @@ -2,7 +2,7 @@ #include -#include +#include #include @@ -11,7 +11,7 @@ namespace psemek::ui struct mouse_move { - geom::point position; + math::point position; }; enum class mouse_button diff --git a/libs/ui_legacy/include/psemek/ui/event_interceptor.hpp b/libs/ui_legacy/include/psemek/ui/event_interceptor.hpp index f1648e9d..ba95e5c9 100644 --- a/libs/ui_legacy/include/psemek/ui/event_interceptor.hpp +++ b/libs/ui_legacy/include/psemek/ui/event_interceptor.hpp @@ -10,12 +10,12 @@ namespace psemek::ui struct event_interceptor : single_container { - geom::box size_constraints() const override; - geom::interval width_constraints(float height) const override; - geom::interval height_constraints(float width) const override; + math::box size_constraints() const override; + math::interval width_constraints(float height) const override; + math::interval height_constraints(float width) const override; struct shape const & shape() const override; - void reshape(geom::box const & bbox) override; + void reshape(math::box const & bbox) override; void on_mouse_move(std::function callback); void on_mouse_click(std::function callback); diff --git a/libs/ui_legacy/include/psemek/ui/frame.hpp b/libs/ui_legacy/include/psemek/ui/frame.hpp index 83a7f238..f4ba5001 100644 --- a/libs/ui_legacy/include/psemek/ui/frame.hpp +++ b/libs/ui_legacy/include/psemek/ui/frame.hpp @@ -8,24 +8,24 @@ namespace psemek::ui struct frame : single_container { - virtual void set_min_size(std::optional> const & size); - virtual void set_max_size(std::optional> const & size); - virtual void set_fixed_size(geom::vector const & size); + virtual void set_min_size(std::optional> const & size); + virtual void set_max_size(std::optional> const & size); + virtual void set_fixed_size(math::vector const & size); - virtual std::optional> min_size() const { return min_size_; } - virtual std::optional> max_size() const { return max_size_; } + virtual std::optional> min_size() const { return min_size_; } + virtual std::optional> max_size() const { return max_size_; } struct shape const & shape() const override; - void reshape(geom::box const & box) override; - geom::box size_constraints() const override; - geom::interval width_constraints(float height) const override; - geom::interval height_constraints(float width) const override; + void reshape(math::box const & box) override; + math::box size_constraints() const override; + math::interval width_constraints(float height) const override; + math::interval height_constraints(float width) const override; void draw(painter &) const override {} private: - std::optional> min_size_; - std::optional> max_size_; + std::optional> min_size_; + std::optional> max_size_; }; } diff --git a/libs/ui_legacy/include/psemek/ui/grid_layout.hpp b/libs/ui_legacy/include/psemek/ui/grid_layout.hpp index 347521b2..f7b7c4b7 100644 --- a/libs/ui_legacy/include/psemek/ui/grid_layout.hpp +++ b/libs/ui_legacy/include/psemek/ui/grid_layout.hpp @@ -40,14 +40,14 @@ namespace psemek::ui virtual void set_outer_margin(bool value); virtual bool outer_margin() const { return outer_margin_; } - geom::box size_constraints() const override; + math::box size_constraints() const override; - geom::interval width_constraints(float height) const override; + math::interval width_constraints(float height) const override; - geom::interval height_constraints(float width) const override; + math::interval height_constraints(float width) const override; struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override; + void reshape(math::box const & bbox) override; bool transparent() const override { return !hint(); } @@ -56,8 +56,8 @@ namespace psemek::ui ~grid_layout() override; protected: - std::vector> row_shape_; - std::vector> column_shape_; + std::vector> row_shape_; + std::vector> column_shape_; private: util::array, 2> children_; diff --git a/libs/ui_legacy/include/psemek/ui/image_view.hpp b/libs/ui_legacy/include/psemek/ui/image_view.hpp index 49064f04..a108a0f0 100644 --- a/libs/ui_legacy/include/psemek/ui/image_view.hpp +++ b/libs/ui_legacy/include/psemek/ui/image_view.hpp @@ -27,9 +27,9 @@ namespace psemek::ui virtual void set_color(gfx::color_rgba const & c) { color_ = c; } struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override { shape_.box = bbox; } + void reshape(math::box const & bbox) override { shape_.box = bbox; } - geom::box size_constraints() const override; + math::box size_constraints() const override; bool transparent() const override { return !hint(); } diff --git a/libs/ui_legacy/include/psemek/ui/label.hpp b/libs/ui_legacy/include/psemek/ui/label.hpp index 7dccb510..4b9573ba 100644 --- a/libs/ui_legacy/include/psemek/ui/label.hpp +++ b/libs/ui_legacy/include/psemek/ui/label.hpp @@ -74,15 +74,15 @@ namespace psemek::ui virtual void on_link_click(link_callback callback); struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override; + void reshape(math::box const & bbox) override; bool on_event(ui::mouse_move const & e) override; bool on_event(ui::mouse_click const & e) override; - geom::box size_constraints() const override; + math::box size_constraints() const override; - geom::interval width_constraints(float height) const override; - geom::interval height_constraints(float width) const override; + math::interval width_constraints(float height) const override; + math::interval height_constraints(float width) const override; void style_updated() const override; void own_style_updated() const override; @@ -135,8 +135,8 @@ namespace psemek::ui { struct image { - geom::box texcoords; - geom::box position; + math::box texcoords; + math::box position; }; struct batch @@ -148,12 +148,12 @@ namespace psemek::ui }; std::vector batches; - geom::vector size{0.f, 0.f}; + math::vector size{0.f, 0.f}; ui::font_type font_type = ui::font_type::bitmap; float sdf_scale = 0.f; - std::vector, std::string>> link_bboxes; + std::vector, std::string>> link_bboxes; }; mutable std::optional cached_state_; @@ -162,7 +162,7 @@ namespace psemek::ui bool mouse_down_ = false; void update_cached_state() const; - cached_state cached_state_for(geom::box const & bbox) const; + cached_state cached_state_for(math::box const & bbox) const; mutable std::shared_ptr single_white_pixel_texture_; std::shared_ptr single_white_pixel_texture() const; diff --git a/libs/ui_legacy/include/psemek/ui/null_shape.hpp b/libs/ui_legacy/include/psemek/ui/null_shape.hpp index def76767..35c13354 100644 --- a/libs/ui_legacy/include/psemek/ui/null_shape.hpp +++ b/libs/ui_legacy/include/psemek/ui/null_shape.hpp @@ -8,8 +8,8 @@ namespace psemek::ui struct null_shape : shape { - bool contains(geom::point const & point) const override; - geom::box bbox() const override; + bool contains(math::point const & point) const override; + math::box bbox() const override; }; } diff --git a/libs/ui_legacy/include/psemek/ui/painter.hpp b/libs/ui_legacy/include/psemek/ui/painter.hpp index 919ced85..7051e010 100644 --- a/libs/ui_legacy/include/psemek/ui/painter.hpp +++ b/libs/ui_legacy/include/psemek/ui/painter.hpp @@ -4,8 +4,8 @@ #include #include #include -#include -#include +#include +#include namespace psemek::ui { @@ -32,20 +32,20 @@ namespace psemek::ui struct painter { - virtual void draw_rect(geom::box const & rect, gfx::color_rgba const & color) = 0; + virtual void draw_rect(math::box const & rect, gfx::color_rgba const & color) = 0; - virtual void draw_triangle(geom::triangle> const & tri, gfx::color_rgba const & color) + virtual void draw_triangle(math::triangle> const & tri, gfx::color_rgba const & color) { draw_triangle(tri, color, color, color); } - virtual void draw_triangle(geom::triangle> const & tri, gfx::color_rgba const & c0, gfx::color_rgba const & c1, gfx::color_rgba const & c2) = 0; + virtual void draw_triangle(math::triangle> const & tri, gfx::color_rgba const & c0, gfx::color_rgba const & c1, gfx::color_rgba const & c2) = 0; using color_mode = detail::color_mode; using image_options = detail::image_options; - virtual void draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, image_options const & opts = image_options{}) = 0; - virtual void draw_msdf_glyph(geom::box const & rect, gfx::texture_view_2d const & tex, float sdf_scale, image_options const & opts = image_options{}) = 0; + virtual void draw_image(math::box const & rect, gfx::texture_view_2d const & tex, image_options const & opts = image_options{}) = 0; + virtual void draw_msdf_glyph(math::box const & rect, gfx::texture_view_2d const & tex, float sdf_scale, image_options const & opts = image_options{}) = 0; virtual void begin_stencil() = 0; virtual void commit_stencil() = 0; diff --git a/libs/ui_legacy/include/psemek/ui/painter_impl.hpp b/libs/ui_legacy/include/psemek/ui/painter_impl.hpp index b260b208..49f50776 100644 --- a/libs/ui_legacy/include/psemek/ui/painter_impl.hpp +++ b/libs/ui_legacy/include/psemek/ui/painter_impl.hpp @@ -4,7 +4,7 @@ #include -#include +#include namespace psemek::ui { @@ -15,19 +15,19 @@ namespace psemek::ui painter_impl(); ~painter_impl(); - void draw_rect(geom::box const & rect, gfx::color_rgba const & color) override; - void draw_triangle(geom::triangle> const & tri, gfx::color_rgba const & c0, gfx::color_rgba const & c1, gfx::color_rgba const & c2) override; + void draw_rect(math::box const & rect, gfx::color_rgba const & color) override; + void draw_triangle(math::triangle> const & tri, gfx::color_rgba const & c0, gfx::color_rgba const & c1, gfx::color_rgba const & c2) override; - void draw_image(geom::box const & rect, gfx::texture_view_2d const & tex, image_options const & opts) override; - void draw_msdf_glyph(geom::box const & rect, gfx::texture_view_2d const & tex, float sdf_scale, image_options const & opts) override; + void draw_image(math::box const & rect, gfx::texture_view_2d const & tex, image_options const & opts) override; + void draw_msdf_glyph(math::box const & rect, gfx::texture_view_2d const & tex, float sdf_scale, image_options const & opts) override; void begin_stencil() override; void commit_stencil() override; void end_stencil() override; - void start_frame(geom::box const & bbox); - void render(geom::matrix const & transform); - geom::box current_bbox() const; + void start_frame(math::box const & bbox); + void render(math::matrix const & transform); + math::box current_bbox() const; private: psemek_declare_pimpl diff --git a/libs/ui_legacy/include/psemek/ui/positioner.hpp b/libs/ui_legacy/include/psemek/ui/positioner.hpp index 08a5c71c..2f17e742 100644 --- a/libs/ui_legacy/include/psemek/ui/positioner.hpp +++ b/libs/ui_legacy/include/psemek/ui/positioner.hpp @@ -24,9 +24,9 @@ namespace psemek::ui }; struct shape const & shape() const override; - void reshape(geom::box const & box) override; + void reshape(math::box const & box) override; - virtual void set_position(geom::point const & p, x_align x, y_align y, bool clamp = true); + virtual void set_position(math::point const & p, x_align x, y_align y, bool clamp = true); bool transparent() const override { return true; } @@ -35,7 +35,7 @@ namespace psemek::ui private: box_shape shape_; - geom::point position_{0.f, 0.f}; + math::point position_{0.f, 0.f}; x_align x_; y_align y_; bool clamp_; diff --git a/libs/ui_legacy/include/psemek/ui/rich_image_view.hpp b/libs/ui_legacy/include/psemek/ui/rich_image_view.hpp index cf96bf3a..6b78fba5 100644 --- a/libs/ui_legacy/include/psemek/ui/rich_image_view.hpp +++ b/libs/ui_legacy/include/psemek/ui/rich_image_view.hpp @@ -14,16 +14,16 @@ namespace psemek::ui void set_image(std::shared_ptr image); // Zoom is defined as (size of painted pixels)/(size of texture pixels) - geom::interval zoom_range() const { return zoom_range_; } - void set_zoom_range(geom::interval range); + math::interval zoom_range() const { return zoom_range_; } + void set_zoom_range(math::interval range); - geom::point center() const { return center_; } + math::point center() const { return center_; } float zoom() const { return zoom_; } - void set_center(geom::point const & center); + void set_center(math::point const & center); void set_zoom(float zoom); - geom::box region() const; + math::box region() const; bool allow_overflow() const { return allow_overflow_; } void set_allow_overflow(bool value); @@ -34,7 +34,7 @@ namespace psemek::ui bool on_event(key_press const & e) override; struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override; + void reshape(math::box const & bbox) override; void update(float dt) override; @@ -45,17 +45,17 @@ namespace psemek::ui private: std::shared_ptr image_; - geom::interval zoom_range_ = {0.f, std::numeric_limits::infinity()}; + math::interval zoom_range_ = {0.f, std::numeric_limits::infinity()}; float zoom_ = 1.f; float zoom_tgt_ = 1.f; - geom::point center_{0.f, 0.f}; + math::point center_{0.f, 0.f}; bool allow_overflow_ = false; gfx::color_rgba color_{0, 0, 0, 0}; box_shape shape_; bool mouseover_ = false; - std::optional> mouse_; - std::optional> drag_; + std::optional> mouse_; + std::optional> drag_; void post_region_changed(); }; diff --git a/libs/ui_legacy/include/psemek/ui/screen.hpp b/libs/ui_legacy/include/psemek/ui/screen.hpp index 60124c3d..6f51d209 100644 --- a/libs/ui_legacy/include/psemek/ui/screen.hpp +++ b/libs/ui_legacy/include/psemek/ui/screen.hpp @@ -39,9 +39,9 @@ namespace psemek::ui bool move_to_top(element * c); struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override; + void reshape(math::box const & bbox) override; - geom::box size_constraints() const override; + math::box size_constraints() const override; bool transparent() const override { return !hint(); } diff --git a/libs/ui_legacy/include/psemek/ui/scroller.hpp b/libs/ui_legacy/include/psemek/ui/scroller.hpp index 6e5c5237..89413892 100644 --- a/libs/ui_legacy/include/psemek/ui/scroller.hpp +++ b/libs/ui_legacy/include/psemek/ui/scroller.hpp @@ -24,18 +24,18 @@ namespace psemek::ui virtual bool horizontal_scroll() const { return horizontal_; } virtual bool vertical_scroll() const { return vertical_; } - virtual geom::box events_bbox() const override; + virtual math::box events_bbox() const override; virtual bool on_event(mouse_move const & e) override; virtual bool on_event(mouse_click const & e) override; virtual bool on_event(mouse_wheel const & e) override; struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override; + void reshape(math::box const & bbox) override; - geom::box size_constraints() const override; - geom::interval width_constraints(float height) const override; - geom::interval height_constraints(float width) const override; + math::box size_constraints() const override; + math::interval width_constraints(float height) const override; + math::interval height_constraints(float width) const override; virtual float position(direction dir) const; virtual void set_position(direction dir, float position, bool animate); @@ -47,13 +47,13 @@ namespace psemek::ui protected: - virtual void on_scroll(geom::vector const & delta); + virtual void on_scroll(math::vector const & delta); virtual float width() const; - virtual geom::box visible_range() const; + virtual math::box visible_range() const; - virtual geom::box horizontal_box() const; - virtual geom::box vertical_box() const; + virtual math::box horizontal_box() const; + virtual math::box vertical_box() const; enum state_t { @@ -67,15 +67,15 @@ namespace psemek::ui private: box_shape shape_; - std::optional> mouse_; + std::optional> mouse_; direction preferred_direction_ = direction::vertical; bool horizontal_ = false; bool vertical_ = true; - geom::vector shift_{0.f, 0.f}; - geom::vector shift_tgt_{0.f, 0.f}; + math::vector shift_{0.f, 0.f}; + math::vector shift_tgt_{0.f, 0.f}; void clamp_shift(); }; diff --git a/libs/ui_legacy/include/psemek/ui/selector.hpp b/libs/ui_legacy/include/psemek/ui/selector.hpp index 2a7b9c26..0ccbafd6 100644 --- a/libs/ui_legacy/include/psemek/ui/selector.hpp +++ b/libs/ui_legacy/include/psemek/ui/selector.hpp @@ -20,9 +20,9 @@ namespace psemek::ui struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & box) override; + void reshape(math::box const & box) override; - geom::box size_constraints() const override; + math::box size_constraints() const override; bool on_event(mouse_move const & e) override; bool on_event(mouse_click const & e) override; @@ -35,7 +35,7 @@ namespace psemek::ui virtual std::shared_ptr submenu(std::size_t index) const; virtual void clear(); - virtual geom::interval y_range(std::size_t index) const; + virtual math::interval y_range(std::size_t index) const; using callback_type = std::function; @@ -45,7 +45,7 @@ namespace psemek::ui virtual callback_type on_selected() const; - virtual geom::box const & item_box(std::size_t index) const { return child_boxes_[index]; } + virtual math::box const & item_box(std::size_t index) const { return child_boxes_[index]; } protected: @@ -68,7 +68,7 @@ namespace psemek::ui std::optional active_submenu_; float active_submenu_y_ = 0.f; - std::vector> child_boxes_; + std::vector> child_boxes_; std::optional selected_; @@ -76,6 +76,6 @@ namespace psemek::ui std::function)> mouseover_callback_; }; - bool spawn_selector(element * root, std::shared_ptr selector, geom::point const & position, std::function on_canceled = nullptr); + bool spawn_selector(element * root, std::shared_ptr selector, math::point const & position, std::function on_canceled = nullptr); } diff --git a/libs/ui_legacy/include/psemek/ui/shape.hpp b/libs/ui_legacy/include/psemek/ui/shape.hpp index 69a0f853..8fa341c2 100644 --- a/libs/ui_legacy/include/psemek/ui/shape.hpp +++ b/libs/ui_legacy/include/psemek/ui/shape.hpp @@ -1,15 +1,15 @@ #pragma once -#include -#include +#include +#include namespace psemek::ui { struct shape { - virtual bool contains(geom::point const & point) const = 0; - virtual geom::box bbox() const = 0; + virtual bool contains(math::point const & point) const = 0; + virtual math::box bbox() const = 0; virtual ~shape() {} }; diff --git a/libs/ui_legacy/include/psemek/ui/slider.hpp b/libs/ui_legacy/include/psemek/ui/slider.hpp index 3992aa0c..9f10d47e 100644 --- a/libs/ui_legacy/include/psemek/ui/slider.hpp +++ b/libs/ui_legacy/include/psemek/ui/slider.hpp @@ -14,8 +14,8 @@ namespace psemek::ui bool on_event(mouse_click const & e) override; bool on_event(mouse_wheel const & e) override; - virtual geom::interval value_range() const { return value_range_; } - virtual void set_value_range(geom::interval i, bool notify = true); + virtual math::interval value_range() const { return value_range_; } + virtual void set_value_range(math::interval i, bool notify = true); virtual int value() const { return value_; } virtual void set_value(int v, bool notify = true); @@ -39,24 +39,24 @@ namespace psemek::ui virtual void on_state_changed(state_t /* old */) {} - virtual geom::interval slider_range() const; + virtual math::interval slider_range() const; private: - geom::interval value_range_{0, 100}; + math::interval value_range_{0, 100}; int value_ = 0; bool cyclic_ = false; state_t state_ = state_t::normal; - std::optional> mouse_; - std::optional> drag_range_; + std::optional> mouse_; + std::optional> drag_range_; on_value_changed_callback callback_; void post_value_changed(); int compute_value(int x) const; - int compute_value(int x, geom::interval const & range) const; + int compute_value(int x, math::interval const & range) const; }; } diff --git a/libs/ui_legacy/include/psemek/ui/spawn.hpp b/libs/ui_legacy/include/psemek/ui/spawn.hpp index 6fa26440..8133f6e5 100644 --- a/libs/ui_legacy/include/psemek/ui/spawn.hpp +++ b/libs/ui_legacy/include/psemek/ui/spawn.hpp @@ -5,6 +5,6 @@ namespace psemek::ui { - bool spawn(element * root, std::shared_ptr element, geom::point const & position); + bool spawn(element * root, std::shared_ptr element, math::point const & position); } diff --git a/libs/ui_legacy/include/psemek/ui/spinbox.hpp b/libs/ui_legacy/include/psemek/ui/spinbox.hpp index 6e4db3f6..e91b3fc7 100644 --- a/libs/ui_legacy/include/psemek/ui/spinbox.hpp +++ b/libs/ui_legacy/include/psemek/ui/spinbox.hpp @@ -16,8 +16,8 @@ namespace psemek::ui virtual int value() const { return value_; } virtual void set_value(int v, bool notify = true); - virtual geom::interval value_range() const { return value_range_; } - virtual void set_value_range(geom::interval i); + virtual math::interval value_range() const { return value_range_; } + virtual void set_value_range(math::interval i); virtual bool wrap() const { return wrap_; } virtual void set_wrap(bool w); @@ -38,7 +38,7 @@ namespace psemek::ui private: int value_ = 0; - geom::interval value_range_ = geom::interval::full(); + math::interval value_range_ = math::interval::full(); bool wrap_ = false; on_value_changed_callback on_value_changed_; }; diff --git a/libs/ui_legacy/include/psemek/ui/style.hpp b/libs/ui_legacy/include/psemek/ui/style.hpp index 3f5e4a61..d47e48ad 100644 --- a/libs/ui_legacy/include/psemek/ui/style.hpp +++ b/libs/ui_legacy/include/psemek/ui/style.hpp @@ -36,7 +36,7 @@ namespace psemek::ui std::optional highlight_color; std::optional action_color; - std::optional> action_offset; + std::optional> action_offset; std::optional border_color; std::optional border_width; @@ -50,16 +50,16 @@ namespace psemek::ui std::optional ref_height; - std::optional> shadow_offset; + std::optional> shadow_offset; std::optional shadow_color; - std::optional> inner_margin; + std::optional> inner_margin; std::optional outer_margin; std::optional text_color; std::optional text_scale; std::optional text_style; - std::optional> text_shadow_offset; + std::optional> text_shadow_offset; std::optional link_color; std::optional link_style; diff --git a/libs/ui_legacy/include/psemek/ui/table.hpp b/libs/ui_legacy/include/psemek/ui/table.hpp index 36b4d616..84cdca8f 100644 --- a/libs/ui_legacy/include/psemek/ui/table.hpp +++ b/libs/ui_legacy/include/psemek/ui/table.hpp @@ -10,7 +10,7 @@ namespace psemek::ui { table(); - void reshape(geom::box const & box) override; + void reshape(math::box const & box) override; void set_size(std::size_t rows, std::size_t columns) override; diff --git a/libs/ui_legacy/include/psemek/ui/triangle_shape.hpp b/libs/ui_legacy/include/psemek/ui/triangle_shape.hpp index 7a5253df..a176e0c3 100644 --- a/libs/ui_legacy/include/psemek/ui/triangle_shape.hpp +++ b/libs/ui_legacy/include/psemek/ui/triangle_shape.hpp @@ -2,7 +2,7 @@ #include -#include +#include namespace psemek::ui { @@ -10,16 +10,16 @@ namespace psemek::ui struct triangle_shape : shape { - geom::triangle> triangle{{{0.f, 0.f}, {0.f, 0.f}, {0.f, 0.f}}}; + math::triangle> triangle{{{0.f, 0.f}, {0.f, 0.f}, {0.f, 0.f}}}; triangle_shape() = default; - triangle_shape(geom::triangle> triangle) + triangle_shape(math::triangle> triangle) : triangle{triangle} {} - bool contains(geom::point const & point) const override; - geom::box bbox() const override; + bool contains(math::point const & point) const override; + math::box bbox() const override; }; } diff --git a/libs/ui_legacy/source/box_shape.cpp b/libs/ui_legacy/source/box_shape.cpp index e0815683..974caf47 100644 --- a/libs/ui_legacy/source/box_shape.cpp +++ b/libs/ui_legacy/source/box_shape.cpp @@ -1,13 +1,13 @@ #include -#include +#include namespace psemek::ui { - bool box_shape::contains(geom::point const & point) const + bool box_shape::contains(math::point const & point) const { - return geom::contains(box, point); + return math::contains(box, point); } } diff --git a/libs/ui_legacy/source/button.cpp b/libs/ui_legacy/source/button.cpp index 505bab14..98596e5f 100644 --- a/libs/ui_legacy/source/button.cpp +++ b/libs/ui_legacy/source/button.cpp @@ -15,7 +15,7 @@ namespace psemek::ui bool button::on_event(mouse_move const & e) { - bool const over = shape().contains(geom::cast(e.position)); + bool const over = shape().contains(math::cast(e.position)); switch (state_) { case state_t::normal: diff --git a/libs/ui_legacy/source/checkbox.cpp b/libs/ui_legacy/source/checkbox.cpp index c6c4a9da..f2a777f9 100644 --- a/libs/ui_legacy/source/checkbox.cpp +++ b/libs/ui_legacy/source/checkbox.cpp @@ -17,7 +17,7 @@ namespace psemek::ui bool checkbox::on_event(mouse_move const & e) { - bool const over = shape().contains(geom::cast(e.position)); + bool const over = shape().contains(math::cast(e.position)); switch (state_) { case state_t::normal: diff --git a/libs/ui_legacy/source/color_picker.cpp b/libs/ui_legacy/source/color_picker.cpp index 390b37b4..1e2f3b39 100644 --- a/libs/ui_legacy/source/color_picker.cpp +++ b/libs/ui_legacy/source/color_picker.cpp @@ -21,7 +21,7 @@ namespace psemek::ui color_picker_impl(element_factory & factory, bool alpha) { auto frame = std::make_shared(); - frame->set_min_size(geom::vector{250.f, 0.f}); + frame->set_min_size(math::vector{250.f, 0.f}); auto main_layout = factory.make_grid_layout(); main_layout->set_size(1, 2); @@ -107,7 +107,7 @@ namespace psemek::ui return child_->shape(); } - void reshape(geom::box const & box) override + void reshape(math::box const & box) override { child_->reshape(box); } @@ -115,17 +115,17 @@ namespace psemek::ui void draw(painter &) const override {} - geom::box size_constraints() const override + math::box size_constraints() const override { return child_->size_constraints(); } - geom::interval width_constraints(float height) const override + math::interval width_constraints(float height) const override { return child_->width_constraints(height); } - geom::interval height_constraints(float width) const override + math::interval height_constraints(float width) const override { return child_->height_constraints(width); } @@ -170,7 +170,7 @@ namespace psemek::ui float max_width = 0.f; char max_width_char = '0'; for (auto const & g : glyphs) - if (geom::make_max(max_width, g.position[0].length())) + if (math::make_max(max_width, g.position[0].length())) max_width_char = g.character; hidden_label_->set_text(std::string(3, max_width_char)); diff --git a/libs/ui_legacy/source/color_view.cpp b/libs/ui_legacy/source/color_view.cpp index 5ece909b..2606afa1 100644 --- a/libs/ui_legacy/source/color_view.cpp +++ b/libs/ui_legacy/source/color_view.cpp @@ -4,14 +4,14 @@ namespace psemek::ui { - geom::interval color_view::width_constraints(float height) const + math::interval color_view::width_constraints(float height) const { if (square_) return {height, height}; return element::width_constraints(height); } - geom::interval color_view::height_constraints(float width) const + math::interval color_view::height_constraints(float width) const { if (square_) return {width, width}; @@ -20,13 +20,13 @@ namespace psemek::ui void color_view::draw(painter & p) const { - geom::box box = shape_.box; + math::box box = shape_.box; if (square_) { if (box[0].length() > box[1].length()) - box[0] = geom::expand(geom::interval::singleton(box[0].center()), box[1].length() / 2.f); + box[0] = math::expand(math::interval::singleton(box[0].center()), box[1].length() / 2.f); else - box[1] = geom::expand(geom::interval::singleton(box[1].center()), box[0].length() / 2.f); + box[1] = math::expand(math::interval::singleton(box[1].center()), box[0].length() / 2.f); } if (color_[3] != 255) @@ -34,7 +34,7 @@ namespace psemek::ui p.draw_rect(box, {255, 255, 255, 255}); int const n = 4; - geom::vector d = box.dimensions() / (n * 1.f); + math::vector d = box.dimensions() / (n * 1.f); for (int x = 0; x < n; ++x) { @@ -42,9 +42,9 @@ namespace psemek::ui { if ((x + y) % 2 == 0) continue; - auto origin = box.corner(0.f, 0.f) + geom::pointwise_mult(d, {x * 1.f, y}); + auto origin = box.corner(0.f, 0.f) + math::pointwise_mult(d, {x * 1.f, y}); - p.draw_rect(geom::span(origin, origin + d), {191, 191, 191, 255}); + p.draw_rect(math::span(origin, origin + d), {191, 191, 191, 255}); } } } diff --git a/libs/ui_legacy/source/controller.cpp b/libs/ui_legacy/source/controller.cpp index a72f2edd..4fc222c2 100644 --- a/libs/ui_legacy/source/controller.cpp +++ b/libs/ui_legacy/source/controller.cpp @@ -4,8 +4,8 @@ #include #include #include -#include -#include +#include +#include namespace psemek::ui { @@ -28,7 +28,7 @@ namespace psemek::ui using element::reshape; - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { shape_.box = bbox; if (root) @@ -37,7 +37,7 @@ namespace psemek::ui post(on_reshape); } - geom::box size_constraints() const override + math::box size_constraints() const override { return root->size_constraints(); } @@ -63,7 +63,7 @@ namespace psemek::ui painter_impl painter; std::shared_ptr root; std::weak_ptr focused; - std::optional> mouse; + std::optional> mouse; float hint_delay = 0.f; float hint_timer = 0.f; util::function on_hint; @@ -98,7 +98,7 @@ namespace psemek::ui auto visitor = util::recursive([&](auto && self, element * elem) -> element * { if (elem->hidden()) return nullptr; - if (!use_event_bbox || geom::contains(elem->events_bbox(), geom::cast(*mouse))) + if (!use_event_bbox || math::contains(elem->events_bbox(), math::cast(*mouse))) { auto cs = elem->children(); for (auto it = cs.rbegin(); it != cs.rend(); ++it) @@ -140,7 +140,7 @@ namespace psemek::ui if (!mouse) return nullptr; - auto m = geom::cast(*mouse); + auto m = math::cast(*mouse); auto visitor = util::recursive([&](auto && self, element * elem) -> element * { if (elem->hidden()) return nullptr; @@ -222,7 +222,7 @@ namespace psemek::ui impl().post_draw_effect = std::move(effect); } - void controller::reshape(geom::box const & shape) + void controller::reshape(math::box const & shape) { impl().root->reshape(shape); } @@ -321,7 +321,7 @@ namespace psemek::ui void controller::render(gfx::render_target const & rt) { - impl().painter.start_frame(geom::cast(rt.viewport)); + impl().painter.start_frame(math::cast(rt.viewport)); auto visitor = util::recursive([&](auto && self, element * elem) -> void { if (elem->hidden()) return; @@ -350,7 +350,7 @@ namespace psemek::ui gl::Disable(gl::CULL_FACE); - impl().painter.render(geom::window_camera{rt.viewport[0].length(), rt.viewport[1].length()}.transform()); + impl().painter.render(math::window_camera{rt.viewport[0].length(), rt.viewport[1].length()}.transform()); } } diff --git a/libs/ui_legacy/source/default_element_factory.cpp b/libs/ui_legacy/source/default_element_factory.cpp index 0e94e7b7..05ac9d06 100644 --- a/libs/ui_legacy/source/default_element_factory.cpp +++ b/libs/ui_legacy/source/default_element_factory.cpp @@ -31,17 +31,17 @@ namespace psemek::ui return shape_; } - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { shape_.box = bbox; auto st = merged_own_style(); - geom::vector offset{0.f, 0.f}; + math::vector offset{0.f, 0.f}; if (state() == state_t::mousedown) - offset = geom::cast(*st->action_offset); + offset = math::cast(*st->action_offset); auto c = child_.get(); - if (c) c->reshape(geom::shrink(bbox, geom::vector{*st->border_width + (*st->inner_margin)[0], *st->border_width + (*st->inner_margin)[1]}) + offset); + if (c) c->reshape(math::shrink(bbox, math::vector{*st->border_width + (*st->inner_margin)[0], *st->border_width + (*st->inner_margin)[1]}) + offset); } void on_state_changed(state_t old) override @@ -70,12 +70,12 @@ namespace psemek::ui auto st = merged_own_style(); if (!st) return; - if (st->shadow_offset != geom::vector{0, 0}) - p.draw_rect(shape_.box + geom::cast(*st->shadow_offset), *st->shadow_color); + if (st->shadow_offset != math::vector{0, 0}) + p.draw_rect(shape_.box + math::cast(*st->shadow_offset), *st->shadow_color); - auto offset = geom::vector{0.f, 0.f}; + auto offset = math::vector{0.f, 0.f}; if (state() == state_t::mousedown) - offset = geom::cast(*st->action_offset); + offset = math::cast(*st->action_offset); if (st->border_width > 0) p.draw_rect(shape_.box + offset, *st->border_color); @@ -85,10 +85,10 @@ namespace psemek::ui color = *st->highlight_color; else if (state() == state_t::mousedown) color = *st->action_color; - p.draw_rect(geom::shrink(shape_.box, 1.f * (*st->border_width)) + offset, color); + p.draw_rect(math::shrink(shape_.box, 1.f * (*st->border_width)) + offset, color); } - geom::box size_constraints() const override + math::box size_constraints() const override { auto sc = element::size_constraints(); @@ -104,19 +104,19 @@ namespace psemek::ui sc[0] += 2.f * (*st->border_width); sc[1] += 2.f * (*st->border_width); - sc += 2.f * geom::cast(*st->inner_margin); + sc += 2.f * math::cast(*st->inner_margin); } return sc; } - geom::interval width_constraints(float height) const override + math::interval width_constraints(float height) const override { auto wc = element::width_constraints(height); auto st = merged_own_style(); - float extra = 2.f * (*st->border_width) + 2.f * geom::cast(*st->inner_margin)[0]; + float extra = 2.f * (*st->border_width) + 2.f * math::cast(*st->inner_margin)[0]; if (child_) { @@ -129,13 +129,13 @@ namespace psemek::ui return wc; } - geom::interval height_constraints(float width) const override + math::interval height_constraints(float width) const override { auto hc = element::height_constraints(width); auto st = merged_own_style(); - float extra = 2.f * (*st->border_width) + 2.f * geom::cast(*st->inner_margin)[1]; + float extra = 2.f * (*st->border_width) + 2.f * math::cast(*st->inner_margin)[1]; if (child_) { @@ -157,18 +157,18 @@ namespace psemek::ui { struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { shape_.box = bbox; auto st = merged_own_style(); if (!st) return; for (auto c : children()) - if (c) c->reshape(geom::shrink(bbox, 1.f * (*st->border_width + *st->bevel_width + *st->outer_margin))); + if (c) c->reshape(math::shrink(bbox, 1.f * (*st->border_width + *st->bevel_width + *st->outer_margin))); } - geom::box size_constraints() const override + math::box size_constraints() const override { - geom::box sc = frame::size_constraints(); + math::box sc = frame::size_constraints(); auto st = merged_own_style(); if (st) @@ -181,7 +181,7 @@ namespace psemek::ui return sc; } - geom::interval width_constraints(float height) const override + math::interval width_constraints(float height) const override { auto wc = frame::width_constraints(height); @@ -195,7 +195,7 @@ namespace psemek::ui return wc; } - geom::interval height_constraints(float width) const override + math::interval height_constraints(float width) const override { auto hc = frame::height_constraints(width); @@ -214,25 +214,25 @@ namespace psemek::ui auto st = merged_own_style(); if (!st) return; - if (st->shadow_offset != geom::vector{0, 0}) - p.draw_rect(shape_.box + geom::cast(*st->shadow_offset), *st->shadow_color); + if (st->shadow_offset != math::vector{0, 0}) + p.draw_rect(shape_.box + math::cast(*st->shadow_offset), *st->shadow_color); if (st->border_width > 0) p.draw_rect(shape_.box, *st->border_color); if (st->bevel_width > 0) { - geom::point corners[4]; - geom::point corners_in[4]; + math::point corners[4]; + math::point corners_in[4]; - auto box = geom::shrink(shape_.box, *st->border_width); + auto box = math::shrink(shape_.box, *st->border_width); corners[0] = box.corner(0, 0); corners[1] = box.corner(1, 0); corners[2] = box.corner(1, 1); corners[3] = box.corner(0, 1); - auto box_in = geom::shrink(box, *st->bevel_width); + auto box_in = math::shrink(box, *st->bevel_width); corners_in[0] = box_in.corner(0, 0); corners_in[1] = box_in.corner(1, 0); @@ -256,7 +256,7 @@ namespace psemek::ui p.draw_triangle({corners[0], corners_in[3], corners_in[0]}, c2); } - p.draw_rect(geom::shrink(shape_.box, 1.f * (*st->border_width + *st->bevel_width)), *st->bg_color); + p.draw_rect(math::shrink(shape_.box, 1.f * (*st->border_width + *st->bevel_width)), *st->bg_color); } private: @@ -353,12 +353,12 @@ namespace psemek::ui if (drag_ && *drag_ != e.position) { - shape_.box += geom::cast(e.position - *drag_); + shape_.box += math::cast(e.position - *drag_); drag_ = e.position; post_reshape(); } - return shape().contains(geom::cast(*mouse_)); + return shape().contains(math::cast(*mouse_)); } bool on_event(mouse_click const & e) override @@ -368,7 +368,7 @@ namespace psemek::ui if (e.button == mouse_button::left && mouse_) { - if (e.down && shape().contains(geom::cast(*mouse_))) + if (e.down && shape().contains(math::cast(*mouse_))) { drag_ = *mouse_; return true; @@ -383,7 +383,7 @@ namespace psemek::ui if (e.button == mouse_button::left && mouse_) { - if (shape().contains(geom::cast(*mouse_))) + if (shape().contains(math::cast(*mouse_))) return true; } @@ -392,7 +392,7 @@ namespace psemek::ui bool on_event(mouse_wheel const &) override { - return mouse_ && shape().contains(geom::cast(*mouse_)); + return mouse_ && shape().contains(math::cast(*mouse_)); } struct shape const & shape() const override @@ -400,7 +400,7 @@ namespace psemek::ui return shape_; } - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { shape_.box = bbox; @@ -412,7 +412,7 @@ namespace psemek::ui float const header_height = std::max(lc[1].min + 2.f * (*st->inner_margin)[1] + 2.f * (*st->border_width), bc[1].min); { - geom::box b; + math::box b; b[0].min = bbox[0].max - bc[0].min; b[0].max = bbox[0].max; b[1].min = bbox[1].min; @@ -421,7 +421,7 @@ namespace psemek::ui } { - geom::box b; + math::box b; b[0].min = bbox[0].min + (*st->inner_margin)[0] + (*st->border_width); b[0].max = bbox[0].max - (*st->inner_margin)[0] - bc[0].min; b[1].min = bbox[1].min + (*st->inner_margin)[1] + (*st->border_width); @@ -431,7 +431,7 @@ namespace psemek::ui if (child_) { - geom::box b; + math::box b; b[0].min = bbox[0].min + (*st->border_width); b[0].max = bbox[0].max - (*st->border_width); b[1].min = bbox[1].min + header_height; @@ -440,9 +440,9 @@ namespace psemek::ui } } - geom::box size_constraints() const override + math::box size_constraints() const override { - geom::box r = element::size_constraints(); + math::box r = element::size_constraints(); if (child_) r = child_->size_constraints(); auto lc = caption_->size_constraints(); @@ -467,8 +467,8 @@ namespace psemek::ui auto const & bb = shape_.box; - if (*st->shadow_offset != geom::vector{0, 0}) - p.draw_rect(bb + geom::cast(*st->shadow_offset), *st->shadow_color); + if (*st->shadow_offset != math::vector{0, 0}) + p.draw_rect(bb + math::cast(*st->shadow_offset), *st->shadow_color); p.draw_rect(bb, *st->border_color); float const header_height = std::max(lc[1].min + 2.f * (*st->inner_margin)[1] + 2.f * (*st->border_width), bc[1].min); @@ -486,8 +486,8 @@ namespace psemek::ui on_close_callback on_close_; - std::optional> mouse_; - std::optional> drag_; + std::optional> mouse_; + std::optional> drag_; box_shape shape_; @@ -507,9 +507,9 @@ namespace psemek::ui return shape_; } - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { - shape_.box = geom::expand(geom::box::singleton(bbox.center()), size() / 2.f); + shape_.box = math::expand(math::box::singleton(bbox.center()), size() / 2.f); } void draw(painter & p) const override @@ -517,26 +517,26 @@ namespace psemek::ui auto st = merged_own_style(); if (!st) return; - geom::box boxes[5]; + math::box boxes[5]; boxes[0] = {{shape_.box[0], {shape_.box[1].min, shape_.box[1].min + outer_width()}}}; boxes[1] = {{shape_.box[0], {shape_.box[1].max - outer_width(), shape_.box[1].max}}}; boxes[2] = {{{shape_.box[0].min, shape_.box[0].min + outer_width()}, {shape_.box[1].min + outer_width(), shape_.box[1].max - outer_width()}}}; boxes[3] = {{{shape_.box[0].max - outer_width(), shape_.box[0].max}, {shape_.box[1].min + outer_width(), shape_.box[1].max - outer_width()}}}; - boxes[4] = geom::shrink(shape_.box, 0.f + outer_width() + outer_margin()); + boxes[4] = math::shrink(shape_.box, 0.f + outer_width() + outer_margin()); int const box_count = value() ? 5 : 4; - if (st->shadow_offset != geom::vector{0, 0}) + if (st->shadow_offset != math::vector{0, 0}) { - auto const offset = geom::cast(*st->shadow_offset); + auto const offset = math::cast(*st->shadow_offset); for (int b = 0; b < box_count; ++b) p.draw_rect(boxes[b] + offset, *st->shadow_color); } - auto offset = geom::vector{0.f, 0.f}; + auto offset = math::vector{0.f, 0.f}; if (state() == state_t::mousedown) - offset = geom::cast(*st->action_offset); + offset = math::cast(*st->action_offset); gfx::color_rgba color = *st->fg_color; if (state() == state_t::mouseover) @@ -548,12 +548,12 @@ namespace psemek::ui p.draw_rect(boxes[b] + offset, color); } - geom::box size_constraints() const override + math::box size_constraints() const override { auto sc = element::size_constraints(); - sc[0] &= geom::interval{size(), std::numeric_limits::infinity()}; - sc[1] &= geom::interval{size(), std::numeric_limits::infinity()}; + sc[0] &= math::interval{size(), std::numeric_limits::infinity()}; + sc[1] &= math::interval{size(), std::numeric_limits::infinity()}; return sc; } @@ -585,17 +585,17 @@ namespace psemek::ui return shape_; } - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { shape_.box = bbox; auto st = merged_own_style(); - geom::vector offset{0.f, 0.f}; + math::vector offset{0.f, 0.f}; if (state() == state_t::mousedown || value()) - offset = geom::cast(*st->action_offset); + offset = math::cast(*st->action_offset); auto c = child_.get(); - if (c) c->reshape(geom::shrink(bbox, geom::vector{*st->border_width + (*st->inner_margin)[0], *st->border_width + (*st->inner_margin)[1]}) + offset); + if (c) c->reshape(math::shrink(bbox, math::vector{*st->border_width + (*st->inner_margin)[0], *st->border_width + (*st->inner_margin)[1]}) + offset); } void on_state_changed(state_t old) override @@ -627,12 +627,12 @@ namespace psemek::ui auto st = merged_own_style(); if (!st) return; - if (st->shadow_offset != geom::vector{0, 0}) - p.draw_rect(shape_.box + geom::cast(*st->shadow_offset), *st->shadow_color); + if (st->shadow_offset != math::vector{0, 0}) + p.draw_rect(shape_.box + math::cast(*st->shadow_offset), *st->shadow_color); - auto offset = geom::vector{0.f, 0.f}; + auto offset = math::vector{0.f, 0.f}; if (state() == state_t::mousedown || value()) - offset = geom::cast(*st->action_offset); + offset = math::cast(*st->action_offset); if (st->border_width > 0) p.draw_rect(shape_.box + offset, *st->border_color); @@ -642,10 +642,10 @@ namespace psemek::ui color = *st->highlight_color; else if (state() == state_t::mousedown || value()) color = *st->action_color; - p.draw_rect(geom::shrink(shape_.box, 1.f * (*st->border_width)) + offset, color); + p.draw_rect(math::shrink(shape_.box, 1.f * (*st->border_width)) + offset, color); } - geom::box size_constraints() const override + math::box size_constraints() const override { auto sc = element::size_constraints(); @@ -661,19 +661,19 @@ namespace psemek::ui sc[0] += 2.f * (*st->border_width); sc[1] += 2.f * (*st->border_width); - sc += 2.f * geom::cast(*st->inner_margin); + sc += 2.f * math::cast(*st->inner_margin); } return sc; } - geom::interval width_constraints(float height) const override + math::interval width_constraints(float height) const override { auto wc = element::width_constraints(height); auto st = merged_own_style(); - float extra = 2.f * (*st->border_width) + 2.f * geom::cast(*st->inner_margin)[0]; + float extra = 2.f * (*st->border_width) + 2.f * math::cast(*st->inner_margin)[0]; if (child_) { @@ -686,13 +686,13 @@ namespace psemek::ui return wc; } - geom::interval height_constraints(float width) const override + math::interval height_constraints(float width) const override { auto hc = element::height_constraints(width); auto st = merged_own_style(); - float extra = 2.f * (*st->border_width) + 2.f * geom::cast(*st->inner_margin)[1]; + float extra = 2.f * (*st->border_width) + 2.f * math::cast(*st->inner_margin)[1]; if (child_) { @@ -714,12 +714,12 @@ namespace psemek::ui { struct shape const & shape() const override { return shape_; } - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { shape_.box = bbox; } - geom::box size_constraints() const override + math::box size_constraints() const override { static float const inf = std::numeric_limits::infinity(); return {{{button_width(), inf}, {button_height(), inf}}}; @@ -729,9 +729,9 @@ namespace psemek::ui { auto st = merged_own_style(); - geom::box ab; + math::box ab; ab[0] = shape_.box[0]; - ab[1] = geom::expand(geom::interval::singleton(shape_.box[1].center()), axis_width() / 2.f); + ab[1] = math::expand(math::interval::singleton(shape_.box[1].center()), axis_width() / 2.f); gfx::color_rgba c = *st->fg_color; if (state() == state_t::mouseover) @@ -741,15 +741,15 @@ namespace psemek::ui auto const r = slider_range(); - float x = geom::lerp(r, geom::unlerp(geom::cast(value_range()), value())); + float x = math::lerp(r, math::unlerp(math::cast(value_range()), value())); - geom::box sb; - sb[0] = geom::expand(geom::interval::singleton(x), button_width() / 2.f); - sb[1] = geom::expand(geom::interval::singleton(shape_.box[1].center()), button_height() / 2.f); + math::box sb; + sb[0] = math::expand(math::interval::singleton(x), button_width() / 2.f); + sb[1] = math::expand(math::interval::singleton(shape_.box[1].center()), button_height() / 2.f); - if (*st->shadow_offset != geom::vector{0, 0}) + if (*st->shadow_offset != math::vector{0, 0}) { - auto off = geom::cast(*st->shadow_offset); + auto off = math::cast(*st->shadow_offset); p.draw_rect(ab + off, *st->shadow_color); p.draw_rect(sb + off, *st->shadow_color); } @@ -757,14 +757,14 @@ namespace psemek::ui p.draw_rect(ab, *st->axis_color); if (*st->border_width != 0) p.draw_rect(sb, *st->border_color); - p.draw_rect(geom::shrink(sb, 1.f * (*st->border_width)), c); + p.draw_rect(math::shrink(sb, 1.f * (*st->border_width)), c); } protected: - geom::interval slider_range() const override + math::interval slider_range() const override { - return geom::shrink(shape_.box[0], button_width() / 2.f); + return math::shrink(shape_.box[0], button_width() / 2.f); } private: @@ -801,7 +801,7 @@ namespace psemek::ui throw std::runtime_error("default arrow button doesn't support children"); } - geom::box size_constraints() const override + math::box size_constraints() const override { auto b = button::size_constraints(); @@ -817,7 +817,7 @@ namespace psemek::ui return shape_; } - void reshape(geom::box const & box) override + void reshape(math::box const & box) override { shape_.box = box; @@ -847,7 +847,7 @@ namespace psemek::ui throw std::runtime_error("Unknown arrow button direction"); } - if (geom::volume(triangle_[0], triangle_[1], triangle_[2]) < 0.f) + if (math::volume(triangle_[0], triangle_[1], triangle_[2]) < 0.f) std::swap(triangle_[1], triangle_[2]); } @@ -855,12 +855,12 @@ namespace psemek::ui { auto st = merged_own_style(); - if (*st->shadow_offset != geom::vector{0, 0}) - p.draw_triangle(triangle_ + geom::cast(*st->shadow_offset), *st->shadow_color); + if (*st->shadow_offset != math::vector{0, 0}) + p.draw_triangle(triangle_ + math::cast(*st->shadow_offset), *st->shadow_color); - geom::vector off{0.f, 0.f}; + math::vector off{0.f, 0.f}; if (state() == state_t::mousedown) - off = geom::cast(*st->action_offset); + off = math::cast(*st->action_offset); gfx::color_rgba color = *st->fg_color; if (state() == state_t::mouseover) @@ -873,7 +873,7 @@ namespace psemek::ui private: int const direction_; box_shape shape_; - geom::triangle> triangle_; + math::triangle> triangle_; }; struct spinbox_impl @@ -907,7 +907,7 @@ namespace psemek::ui dec_button_ = dec_button.get(); } - geom::box size_constraints() const override + math::box size_constraints() const override { return child_->size_constraints(); } @@ -922,7 +922,7 @@ namespace psemek::ui return child_->shape(); } - void reshape(geom::box const & box) override + void reshape(math::box const & box) override { child_->reshape(box); } @@ -953,12 +953,12 @@ namespace psemek::ui { auto st = merged_own_style(); - auto draw_box = [&](geom::box box, state_t state, float position, int dimension) + auto draw_box = [&](math::box box, state_t state, float position, int dimension) { float h = height(); box[dimension].max -= h; - float origin = geom::lerp(box[dimension], position); + float origin = math::lerp(box[dimension], position); box[dimension] = {origin, origin + h}; gfx::color_rgba color; @@ -969,11 +969,11 @@ namespace psemek::ui else { color = *st->action_color; - box += geom::cast(*st->action_offset); + box += math::cast(*st->action_offset); } - if (*st->shadow_offset != geom::vector{0, 0}) - painter.draw_rect(box + geom::cast(*st->shadow_offset), *st->shadow_color); + if (*st->shadow_offset != math::vector{0, 0}) + painter.draw_rect(box + math::cast(*st->shadow_offset), *st->shadow_color); painter.draw_rect(box, color); }; @@ -1008,7 +1008,7 @@ namespace psemek::ui auto bbox = shape().bbox(); if (st->shadow_offset) - p.draw_rect(bbox + geom::cast(*st->shadow_offset), *st->shadow_color); + p.draw_rect(bbox + math::cast(*st->shadow_offset), *st->shadow_color); p.draw_rect(bbox, *st->fg_color); @@ -1034,7 +1034,7 @@ namespace psemek::ui p1[0] -= w / 2.f; p1[1] += w / 2.f; - auto sh = geom::cast(*st->shadow_offset); + auto sh = math::cast(*st->shadow_offset); p.draw_triangle({p0 + sh, p1 + sh, c + sh}, *st->shadow_color); p.draw_triangle({p0, p1, c}, *st->text_color); diff --git a/libs/ui_legacy/source/default_fonts.cpp b/libs/ui_legacy/source/default_fonts.cpp index b74e470e..b4305c4b 100644 --- a/libs/ui_legacy/source/default_fonts.cpp +++ b/libs/ui_legacy/source/default_fonts.cpp @@ -18,7 +18,7 @@ namespace psemek::ui { character_range range{32, 128}; std::string_view name = "default_monospace_9x12"; - geom::vector size{9, 12}; + math::vector size{9, 12}; gfx::texture_2d atlas = gfx::texture_2d::from_pixmap(gfx::read_png_monochrome(io::memory_istream{gfx::resource::font_9x12_png.data})); atlas.nearest_filter(); @@ -27,7 +27,7 @@ namespace psemek::ui gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_B, gl::RED); gl::TexParameteri(atlas.target, gl::TEXTURE_SWIZZLE_A, gl::RED); - std::vector> texcoords(range.end - range.begin); + std::vector> texcoords(range.end - range.begin); for (char32_t c = range.begin; c < range.end; ++c) { @@ -35,7 +35,7 @@ namespace psemek::ui int x = (c - range.begin) % row; int y = (c - range.begin) / row; - geom::box b; + math::box b; b[0].min = x * 11 + 1; b[0].max = b[0].min + 9; b[1].min = y * 14 + 1; @@ -46,7 +46,7 @@ namespace psemek::ui return std::make_unique(range, name, size, std::move(atlas), std::move(texcoords)); } - static std::unique_ptr make_default_font(std::string_view name, geom::vector const & size, rs::resource const & atlas_resource, rs::resource const & glyphs_resource) + static std::unique_ptr make_default_font(std::string_view name, math::vector const & size, rs::resource const & atlas_resource, rs::resource const & glyphs_resource) { gfx::texture_2d atlas = gfx::texture_2d::from_pixmap(gfx::read_png_monochrome(io::memory_istream{atlas_resource.data})); atlas.nearest_filter(); diff --git a/libs/ui_legacy/source/edit.cpp b/libs/ui_legacy/source/edit.cpp index 9ce87857..beb83673 100644 --- a/libs/ui_legacy/source/edit.cpp +++ b/libs/ui_legacy/source/edit.cpp @@ -101,7 +101,7 @@ namespace psemek::ui bool edit::on_event(mouse_move const & e) { - auto m = geom::cast(e.position); + auto m = math::cast(e.position); mouse_x_ = m[0]; bool new_mouseover = shape_.contains(m); @@ -354,13 +354,13 @@ namespace psemek::ui return editing_; } - void edit::reshape(geom::box const & bbox) + void edit::reshape(math::box const & bbox) { shape_.box = bbox; text_box_ = bbox; } - geom::box edit::size_constraints() const + math::box edit::size_constraints() const { // TODO: min x-size static float const inf = std::numeric_limits::infinity(); @@ -397,7 +397,7 @@ namespace psemek::ui options.scale = *st->text_scale; auto glyphs = font->shape(text_, options); - geom::box bbox; + math::box bbox; for (auto const & g : glyphs) bbox |= g.position; @@ -414,7 +414,7 @@ namespace psemek::ui cached_state_ = std::move(state); } - geom::vector offset{0.f, 0.f}; + math::vector offset{0.f, 0.f}; switch (halign_) { @@ -449,9 +449,9 @@ namespace psemek::ui p.draw_rect(text_box_, {0, 0, 0, 255}); p.commit_stencil(); - if (*st->text_shadow_offset != geom::vector{0, 0} && (*st->shadow_color)[3] != 0) + if (*st->text_shadow_offset != math::vector{0, 0} && (*st->shadow_color)[3] != 0) { - auto shoffset = offset + geom::cast(*st->text_shadow_offset); + auto shoffset = offset + math::cast(*st->text_shadow_offset); for (auto const & i : cached_state_->images) p.draw_image(i.position + shoffset, {cached_state_->texture, i.texcoords}, {*st->shadow_color, painter::color_mode::multiply}); } @@ -481,7 +481,7 @@ namespace psemek::ui } } - void edit::set_text_shape(geom::box const & box) + void edit::set_text_shape(math::box const & box) { text_box_ = box; } diff --git a/libs/ui_legacy/source/element.cpp b/libs/ui_legacy/source/element.cpp index d6c65b81..5921c1e8 100644 --- a/libs/ui_legacy/source/element.cpp +++ b/libs/ui_legacy/source/element.cpp @@ -48,9 +48,9 @@ namespace psemek::ui post_delayed_callbacks(); } - geom::box element::events_bbox() const + math::box element::events_bbox() const { - return geom::box::full(); + return math::box::full(); } void element::reshape() @@ -58,18 +58,18 @@ namespace psemek::ui reshape(shape().bbox()); } - geom::box element::size_constraints() const + math::box element::size_constraints() const { static float const inf = std::numeric_limits::infinity(); return {{{0.f, inf}, {0.f, inf}}}; } - geom::interval element::width_constraints(float) const + math::interval element::width_constraints(float) const { return size_constraints()[0]; } - geom::interval element::height_constraints(float) const + math::interval element::height_constraints(float) const { return size_constraints()[1]; } @@ -266,9 +266,9 @@ namespace psemek::ui { auto const box = e->shape().bbox(); if (mouseover) - e->on_event(mouse_move{geom::cast(box.center())}); + e->on_event(mouse_move{math::cast(box.center())}); else - e->on_event(mouse_move{geom::cast(box.corner(0.f, 0.f) - geom::vector{1000.f, 1000.f})}); + e->on_event(mouse_move{math::cast(box.corner(0.f, 0.f) - math::vector{1000.f, 1000.f})}); } } diff --git a/libs/ui_legacy/source/event_interceptor.cpp b/libs/ui_legacy/source/event_interceptor.cpp index 8163e2ef..d9e78f4a 100644 --- a/libs/ui_legacy/source/event_interceptor.cpp +++ b/libs/ui_legacy/source/event_interceptor.cpp @@ -4,21 +4,21 @@ namespace psemek::ui { - geom::box event_interceptor::size_constraints() const + math::box event_interceptor::size_constraints() const { if (child_) return child_->size_constraints(); return element::size_constraints(); } - geom::interval event_interceptor::width_constraints(float height) const + math::interval event_interceptor::width_constraints(float height) const { if (child_) return child_->width_constraints(height); return element::width_constraints(height); } - geom::interval event_interceptor::height_constraints(float width) const + math::interval event_interceptor::height_constraints(float width) const { if (child_) return child_->height_constraints(width); @@ -33,7 +33,7 @@ namespace psemek::ui return fallback_shape; } - void event_interceptor::reshape(geom::box const & bbox) + void event_interceptor::reshape(math::box const & bbox) { if (child_) child_->reshape(bbox); @@ -67,7 +67,7 @@ namespace psemek::ui bool event_interceptor::on_event(mouse_move const & event) { if (child_) - mouseover_ = child_->shape().contains(geom::cast(event.position)); + mouseover_ = child_->shape().contains(math::cast(event.position)); else mouseover_ = false; diff --git a/libs/ui_legacy/source/file_dialog.cpp b/libs/ui_legacy/source/file_dialog.cpp index df52ec10..ac3e12dc 100644 --- a/libs/ui_legacy/source/file_dialog.cpp +++ b/libs/ui_legacy/source/file_dialog.cpp @@ -33,22 +33,22 @@ namespace psemek::ui return child()->shape(); } - void reshape(geom::box const & bbox) override + void reshape(math::box const & bbox) override { child()->reshape(bbox); } - geom::box size_constraints() const override + math::box size_constraints() const override { return child()->size_constraints(); } - geom::interval width_constraints(float height) const override + math::interval width_constraints(float height) const override { return child()->width_constraints(height); } - geom::interval height_constraints(float width) const override + math::interval height_constraints(float width) const override { return child()->height_constraints(width); } @@ -142,7 +142,7 @@ namespace psemek::ui back_button->icon()->set_downscale(false); auto back_button_style = std::make_shared