Commit e208eb52 authored by Philip Trettner's avatar Philip Trettner
Browse files

segmentize

parent 9b48a947
......@@ -22,6 +22,7 @@
#include <typed-geometry/functions/objects/plane.hh>
#include <typed-geometry/functions/objects/project.hh>
#include <typed-geometry/functions/objects/rasterize.hh>
#include <typed-geometry/functions/objects/segmentize.hh>
#include <typed-geometry/functions/objects/size.hh>
#include <typed-geometry/functions/objects/tangent.hh>
#include <typed-geometry/functions/objects/triangle.hh>
......
#pragma once
#include <typed-geometry/feature/basic.hh>
#include <typed-geometry/functions/objects/edges.hh>
namespace tg
{
/// calls on_segment for each edge of the object
/// on_segment: (tg::segment) -> void
template <class Obj, class OnSegment, std::enable_if_t<has_edges_of<Obj>, int> = 0>
constexpr void segmentize(Obj const& obj, OnSegment&& on_segment)
{
for (auto&& s : edges_of(obj))
on_segment(s);
}
/// calls on_segment for a discretized version of the sphere
template <class ScalarT, class TraitsT, class OnSegment>
void segmentize(sphere<2, ScalarT, 2, TraitsT> const& s, int segs, OnSegment&& on_segment)
{
TG_ASSERT(segs >= 3);
using dir_t = dir<2, ScalarT>;
auto const dir_of = [&](int i) -> dir_t {
if (i == 0 || i == segs)
return dir_t(1, 0);
auto [sa, ca] = sin_cos(tau<ScalarT> * i / ScalarT(segs));
return dir_t(ca, sa);
};
auto const pos_of = [&](int i) {
auto d = dir_of(i);
return s.center + d * s.radius;
};
auto prev = pos_of(0);
for (auto i = 1; i <= segs; ++i)
{
auto curr = pos_of(i);
on_segment(tg::segment<2, ScalarT>(prev, curr));
prev = curr;
}
}
template <class ScalarT, class TraitsT, class OnSegment>
void segmentize(sphere<2, ScalarT, 3, TraitsT> const& s, int segs, OnSegment&& on_segment)
{
TG_ASSERT(segs >= 3);
auto const dx = any_normal(s.normal);
auto const dy = cross(s.normal, dx);
using dir_t = dir<2, ScalarT>;
auto const dir_of = [&](int i) -> dir_t {
if (i == 0 || i == segs)
return dir_t(1, 0);
auto [sa, ca] = sin_cos(tau<ScalarT> * i / ScalarT(segs));
return dir_t(ca, sa);
};
auto const pos_of = [&](int i) {
auto d = dir_of(i) * s.radius;
return s.center + dx * d.x + dy * d.y;
};
auto prev = pos_of(0);
for (auto i = 1; i <= segs; ++i)
{
auto curr = pos_of(i);
on_segment(tg::segment<3, ScalarT>(prev, curr));
prev = curr;
}
}
}
......@@ -34,8 +34,8 @@ void triangulate_uv(sphere<3, ScalarT, 3, TraitsT> const& s, int segs_u, int seg
else if (v == segs_v)
return dir_t(0, -1, 0);
auto [su, cu] = tg::sin_cos(tau<ScalarT> * (u == segs_u ? 0 : u) / ScalarT(segs_u));
auto [sv, cv] = tg::sin_cos(pi<ScalarT> * v / ScalarT(segs_v));
auto [su, cu] = sin_cos(tau<ScalarT> * (u == segs_u ? 0 : u) / ScalarT(segs_u));
auto [sv, cv] = sin_cos(pi<ScalarT> * v / ScalarT(segs_v));
return dir_t(sv * su, cv, sv * cu);
};
......@@ -52,7 +52,7 @@ void triangulate_uv(sphere<3, ScalarT, 3, TraitsT> const& s, int segs_u, int seg
auto p1 = pos_of(i, 1);
auto p2 = pos_of(i + 1, 1);
on_triangle(tg::triangle3(p0, p1, p2));
on_triangle(tg::triangle<3, ScalarT>(p0, p1, p2));
}
}
......@@ -66,8 +66,8 @@ void triangulate_uv(sphere<3, ScalarT, 3, TraitsT> const& s, int segs_u, int seg
auto p10 = pos_of(i + 1, j + 0);
auto p11 = pos_of(i + 1, j + 1);
on_triangle(tg::triangle3(p00, p01, p11));
on_triangle(tg::triangle3(p00, p11, p10));
on_triangle(tg::triangle<3, ScalarT>(p00, p01, p11));
on_triangle(tg::triangle<3, ScalarT>(p00, p11, p10));
}
}
......@@ -79,7 +79,7 @@ void triangulate_uv(sphere<3, ScalarT, 3, TraitsT> const& s, int segs_u, int seg
auto p1 = pos_of(i, segs_v - 1);
auto p2 = pos_of(i + 1, segs_v - 1);
on_triangle(tg::triangle3(p0, p2, p1));
on_triangle(tg::triangle<3, ScalarT>(p0, p2, p1));
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment