Commit 216565a3 authored by Philip Trettner's avatar Philip Trettner
Browse files

Merge branch 'agrabowy' into 'develop'

Intersections with ray/line for all objects + various smaller additions

See merge request !100
parents 0dad6e4b 866b0b6f
......@@ -45,7 +45,7 @@ public:
};
template <class T, class Stream>
auto operator<<(Stream& os, optional<T> const& o) -> decltype(os << o.value(), os << "", os)
auto operator<<(Stream& os, optional<T> const& o) -> decltype(os << o.value(), os << std::declval<char const*>(), os)
{
return o.has_value() ? (os << o.value()) : (os << "[empty]");
}
......
......@@ -35,7 +35,19 @@ template <>
struct priority_tag<0>
{
};
// see https://stackoverflow.com/questions/44395169/why-is-sfinae-on-if-constexpr-not-allowed
template <template <class...> class, class, class...>
struct can_apply : std::false_type
{
};
template <template <class...> class Z, class... Ts>
struct can_apply<Z, std::void_t<Z<Ts...>>, Ts...> : std::true_type
{
};
} // namespace detail
template <template <class...> class Z, class... Ts>
constexpr bool can_apply = detail::can_apply<Z, void, Ts...>::value;
template <class...>
constexpr bool always_false = false;
......
......@@ -144,6 +144,14 @@ template <class A, class B>
return a < b ? b : a;
}
template <class T>
[[nodiscard]] constexpr pair<T, T> minmax(T const& a, T const& b)
{
if (b < a)
return {b, a};
return {a, b};
}
template <class A, class B, class C>
[[nodiscard]] constexpr auto clamp(A const& a, B const& min_value, C const& max_value) -> decltype(min(max(a, min_value), max_value))
{
......
......@@ -98,7 +98,12 @@ template <int D, class ScalarT, class TraitsT>
if constexpr (std::is_same_v<TraitsT, default_object_tag>)
return c.axis.pos;
else
return c.axis.pos + c.radius * any_normal(c.axis.dir);
{
if constexpr (D == 3)
return c.axis.pos + c.radius * any_normal(c.axis.dir);
else
return c.axis.pos + c.radius * perpendicular(c.axis.dir);
}
}
template <int D, class ScalarT>
......
......@@ -4,6 +4,7 @@
#include <typed-geometry/types/objects/aabb.hh>
#include <typed-geometry/types/objects/box.hh>
#include <typed-geometry/types/objects/capsule.hh>
#include <typed-geometry/types/objects/cone.hh>
#include <typed-geometry/types/objects/cylinder.hh>
#include <typed-geometry/types/objects/ellipse.hh>
......@@ -221,6 +222,19 @@ template <class ScalarT, class TraitsT>
return x2 / pow2(a + eps) + y2 / pow2(b + eps) + z2 / pow2(c + eps) <= ScalarT(1);
}
template <class ScalarT>
[[nodiscard]] constexpr bool contains(capsule<3, ScalarT> const& c, pos<3, ScalarT> const& p, dont_deduce<ScalarT> eps = ScalarT(0))
{
auto r = c.radius + eps;
return distance_sqr(c.axis, p) <= pow2(r);
}
template <class ScalarT>
[[nodiscard]] constexpr bool contains(capsule_boundary<3, ScalarT> const& c, pos<3, ScalarT> const& p, dont_deduce<ScalarT> eps = ScalarT(0))
{
auto d2 = distance_sqr(c.axis, p);
return pow2(max(ScalarT(0), c.radius - eps)) <= d2 && d2 <= pow2(c.radius + eps);
}
template <int D, class ScalarT>
[[nodiscard]] constexpr bool contains(sphere<D, ScalarT> const& s, pos<D, ScalarT> const& p, dont_deduce<ScalarT> eps = ScalarT(0))
{
......@@ -260,7 +274,7 @@ template <int D, class ScalarT>
}
template <int D, class ScalarT>
[[nodiscard]] constexpr bool contains(halfspace<D, ScalarT> const& h, pos<2, ScalarT> const& p, dont_deduce<ScalarT> eps = ScalarT(0))
[[nodiscard]] constexpr bool contains(halfspace<D, ScalarT> const& h, pos<D, ScalarT> const& p, dont_deduce<ScalarT> eps = ScalarT(0))
{
return signed_distance(p, h) <= eps;
}
......
......@@ -44,6 +44,11 @@ template <int D, class ScalarT, class TraitsT>
return direction(c.axis);
}
template <int D, class ScalarT, class TraitsT>
[[nodiscard]] constexpr dir<D, ScalarT> direction(inf_cylinder<D, ScalarT, TraitsT> const& c)
{
return direction(c.axis);
}
template <int D, class ScalarT, class TraitsT>
[[nodiscard]] constexpr dir<D, ScalarT> direction(capsule<D, ScalarT, TraitsT> const& c)
{
return direction(c.axis);
......
#pragma once
#include <typed-geometry/types/objects/box.hh>
#include <typed-geometry/types/objects/ellipse.hh>
#include <typed-geometry/types/objects/halfspace.hh>
#include <typed-geometry/types/objects/line.hh>
#include <typed-geometry/types/objects/plane.hh>
......@@ -78,6 +79,12 @@ template <class ScalarT, class TraitsT>
return normalize(cross(b.half_extents[0], b.half_extents[1]));
}
template <class ScalarT, class TraitsT>
[[nodiscard]] constexpr dir<3, fractional_result<ScalarT>> normal_of(ellipse<2, ScalarT, 3, TraitsT> const& e)
{
return normalize(cross(e.semi_axes[0], e.semi_axes[1]));
}
template <class ScalarT>
[[nodiscard]] constexpr dir<3, ScalarT> any_normal(vec<3, ScalarT> const& v)
{
......
......@@ -30,12 +30,6 @@
namespace tg
{
template <int D, class ScalarT>
[[nodiscard]] constexpr pos<D, ScalarT> project(pos<D, ScalarT> const&, pos<D, ScalarT> const& target_p)
{
return target_p;
}
// ============== project to plane ==============
template <int D, class ScalarT>
......@@ -66,6 +60,15 @@ template <int D, class ScalarT>
}
// ============== project to point ==============
template <int D, class ScalarT>
[[nodiscard]] constexpr pos<D, ScalarT> project([[maybe_unused]] pos<D, ScalarT> const& p, pos<D, ScalarT> const& q)
{
return q;
}
// ============== project to line / ray / segment ==============
template <int D, class ScalarT>
......
......@@ -48,6 +48,7 @@ using aabb_boundary = aabb<D, ScalarT, boundary_tag>;
template <int D, class ScalarT, class TraitsT>
struct aabb
{
using scalar_t = ScalarT;
using vec_t = vec<D, ScalarT>;
using pos_t = pos<D, ScalarT>;
......
......@@ -16,6 +16,12 @@ template <int D, class ScalarT, class TraitsT = default_object_tag>
struct inf_cone;
// Common cone types
using inf_cone2 = inf_cone<2, f32>;
using finf_cone2 = inf_cone<2, f32>;
using dinf_cone2 = inf_cone<2, f64>;
using iinf_cone2 = inf_cone<2, i32>;
using uinf_cone2 = inf_cone<2, u32>;
using inf_cone3 = inf_cone<3, f32>;
using finf_cone3 = inf_cone<3, f32>;
using dinf_cone3 = inf_cone<3, f64>;
......
......@@ -8,16 +8,22 @@
namespace tg
{
/**
* infinite tube
* infinite cylinder
*
* An infinite tube is defined by a position and an axis
* An infinite cylinder is defined by an axis and a radius
*/
template <int D, class ScalarT, class TraitsT = default_object_tag>
struct inf_cylinder;
// Common infinte tube types
// Common infinite cylinder types
using inf_cylinder2 = inf_cylinder<2, f32>;
using finf_cylinder2 = inf_cylinder<2, f32>;
using dinf_cylinder2 = inf_cylinder<2, f64>;
using iinf_cylinder2 = inf_cylinder<2, i32>;
using uinf_cylinder2 = inf_cylinder<2, u32>;
using inf_cylinder3 = inf_cylinder<3, f32>;
using finttube3 = inf_cylinder<3, f32>;
using finf_cylinder3 = inf_cylinder<3, f32>;
using dinf_cylinder3 = inf_cylinder<3, f64>;
using iinf_cylinder3 = inf_cylinder<3, i32>;
using uinf_cylinder3 = inf_cylinder<3, u32>;
......@@ -29,6 +35,7 @@ using inf_cylinder_boundary = inf_cylinder<D, ScalarT, boundary_tag>;
template <int D, class ScalarT, class TraitsT>
struct inf_cylinder
{
using scalar_t = ScalarT;
using pos_t = pos<D, ScalarT>;
using dir_t = dir<D, ScalarT>;
using line_t = line<D, ScalarT>;
......
......@@ -3,7 +3,6 @@
#include <typed-geometry/types/scalars/default.hh>
#include "../dir.hh"
#include "../pos.hh"
#include "../size.hh"
#include "traits.hh"
// A line has a direction and a point lying on it
......@@ -46,7 +45,8 @@ using uline4 = line<4, u32>;
template <int D, class ScalarT>
struct line
{
using dir_t = tg::dir<D, ScalarT>;
using scalar_t = ScalarT;
using dir_t = tg::dir<D, ScalarT>; // Note: gcc requires the tg:: prefix to distinguish from member variable
using pos_t = tg::pos<D, ScalarT>;
pos_t pos;
......
......@@ -3,7 +3,6 @@
#include <typed-geometry/types/scalars/default.hh>
#include "../dir.hh"
#include "../pos.hh"
#include "../size.hh"
#include "traits.hh"
namespace tg
......@@ -44,7 +43,8 @@ using uray4 = ray<4, u32>;
template <int D, class ScalarT>
struct ray
{
using dir_t = tg::dir<D, ScalarT>;
using scalar_t = ScalarT;
using dir_t = tg::dir<D, ScalarT>; // Note: gcc requires the tg:: prefix to distinguish from member variable
using pos_t = tg::pos<D, ScalarT>;
pos_t origin;
......
......@@ -44,6 +44,7 @@ using usegment4 = segment<4, u32>;
template <int D, class ScalarT>
struct segment
{
using scalar_t = ScalarT;
using pos_t = pos<D, ScalarT>;
pos_t pos0;
......
#pragma once
#include "cylinder.hh"
#include "inf_cylinder.hh"
namespace tg
{
......@@ -13,4 +14,15 @@ using ftube3 = tube<3, f32>;
using dtube3 = tube<3, f64>;
using itube3 = tube<3, i32>;
using utube3 = tube<3, u32>;
template <int D, class ScalarT>
using inf_tube = inf_cylinder_boundary<D, ScalarT>;
// Common inf_tube types
using inf_tube3 = inf_tube<3, f32>;
using finf_tube3 = inf_tube<3, f32>;
using dinf_tube3 = inf_tube<3, f64>;
using iinf_tube3 = inf_tube<3, i32>;
using uinf_tube3 = inf_tube<3, u32>;
}
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