Add segment-box intersection
This commit is contained in:
parent
dbeb613740
commit
db2fb3095d
1 changed files with 24 additions and 0 deletions
|
|
@ -130,6 +130,30 @@ namespace psemek::geom
|
||||||
return !intersection(r, b).empty();
|
return !intersection(r, b).empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T, std::size_t N>
|
||||||
|
interval<T> intersection(segment<point<T, N>> const & s, box<T, N> const & b)
|
||||||
|
{
|
||||||
|
auto t = interval<T>{T(0), T(1)};
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < N; ++i)
|
||||||
|
{
|
||||||
|
T tmin = (b[i].min - s[0][i]) / (s[1][i] - s[0][i]);
|
||||||
|
T tmax = (b[i].max - s[0][i]) / (s[1][i] - s[0][i]);
|
||||||
|
|
||||||
|
if (tmin > tmax) std::swap(tmin, tmax);
|
||||||
|
|
||||||
|
t &= interval<T>{tmin, tmax};
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, std::size_t N>
|
||||||
|
bool intersect(segment<point<T, N>> const & s, box<T, N> const & b)
|
||||||
|
{
|
||||||
|
return !intersection(s, b).empty();
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T, std::size_t N>
|
template <typename T, std::size_t N>
|
||||||
std::optional<T> intersection(ray<T, N> const & r, simplex<point<T, N>, N - 1> const & s)
|
std::optional<T> intersection(ray<T, N> const & r, simplex<point<T, N>, N - 1> const & s)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue