geom/ellipse: add naive covering ellipse impl

This commit is contained in:
Danny Robson 2018-04-18 21:47:53 +10:00
parent eeb5215921
commit dcd789a075
3 changed files with 141 additions and 32 deletions

View File

@ -20,65 +20,92 @@
#include "aabb.hpp" #include "aabb.hpp"
#include "ray.hpp" #include "ray.hpp"
#include "sphere.hpp" #include "sphere.hpp"
#include "point.hpp" #include "quaternion.hpp"
#include "../point.hpp"
#include "../matrix.hpp" #include "../matrix.hpp"
#include "../coord/iostream.hpp"
using util::geom::ellipse; using util::geom::ellipse;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <>
static bool
intersects (ellipse<S,T> e, util::point<S,T> p)
{
auto mag = (p - e.origin) * (p - e.origin) / (e.radius * e.radius);
return std::accumulate (mag.begin (), mag.end (), 0) <= 1;
}
//-----------------------------------------------------------------------------
template <size_t S, typename T, template <size_t,typename> class A, template <size_t,typename> class B>
bool bool
util::geom::intersects (A<S,T> a, B<S,T> b) util::geom::intersects (ellipse3f e, util::point3f p)
{ {
return ::intersects (a, b); auto transform = util::quaternionf::from_to (e.up, {0,1,0}).as_matrix () *
} util::translation (0-e.origin);
return all (abs (transform * p) <= e.radius);
/////////////////////////////////////////////////////////////////////////////// //auto mag = (p - e.origin) * (p - e.origin) / (e.radius * e.radius);
template <size_t DimensionV, typename ValueT> //return std::accumulate (mag.begin (), mag.end (), 0) <= 1;
util::point<DimensionV,ValueT>
util::geom::project (util::geom::ray<DimensionV, ValueT> lhs,
util::geom::ellipse<DimensionV, ValueT> rhs)
{
return lhs.origin + lhs.direction * distance (lhs, rhs);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// query a ray-ellipse distance by transforming spaces such that the ellipse is // query a ray-ellipse distance by transforming spaces such that the ellipse is
// a sphere // a sphere
template <size_t DimensionV, typename ValueT> template <>
ValueT float
util::geom::distance (ray<DimensionV,ValueT> r, ellipse<DimensionV,ValueT> e) util::geom::distance (ray3f r, ellipse3f e)
{ {
// find a transform that puts the ellipse at origin and scales it to a // find a transform that puts the ellipse at origin and scales it to a
// unit sphere. // unit sphere.
auto const from_scaled = util::translation (e.origin.template as<vector> ()) * auto const from_scaled = util::translation (e.origin.template as<vector> ()) *
util::quaternionf::from_to ({0,1,0}, e.up) *
util::scale (e.radius); util::scale (e.radius);
auto const to_scaled = inverse (from_scaled); auto const to_scaled = inverse (from_scaled);
// transform the ray into this new space and query against a unit sphere // transform the ray into this new space and query against a unit sphere
auto const scaled_r = to_scaled * r; auto const scaled_r = to_scaled * r;
auto const scaled_d = distance (scaled_r, sphere<DimensionV,ValueT> {0, 1.f}); auto const scaled_d = distance (scaled_r, sphere3f {0, 1.f});
auto const scaled_p = scaled_r.at (scaled_d); auto const scaled_p = scaled_r.at (scaled_d);
// transform the result back into the original space // transform the result back into the original space
return distance (r.origin, (from_scaled * scaled_p.homog ()).template redim<DimensionV> ()); return distance (r.origin, from_scaled * scaled_p);
} }
///////////////////////////////////////////////////////////////////////////////
template <>
util::point3f
util::geom::project (util::geom::ray3f lhs,
util::geom::ellipse3f rhs)
{
return lhs.origin + lhs.direction * distance (lhs, rhs);
}
///////////////////////////////////////////////////////////////////////////////
util::geom::ellipse3f
util::geom::cover (util::view<const point3f*> src)
{
// find our major axis points and vector
const auto [a,b] = furthest (src);
auto const diff = b - a;
auto const dir = normalised (diff);
// find a transform such that we recentre about the origin
auto const transform = quaternionf::from_to (dir, util::vector3f{1,0,0}).as_matrix () *
translation (0 -a -diff*0.5f);
// find the maximum absolute value in each axis
util::point3f hi {0};
for (auto const& p: src)
hi = max (abs (transform * p), hi);
return ellipse3f {
.origin = a + diff * 0.5f,
.radius = hi.as<vector> (),
.up = rotate ({0,1,0}, util::quaternionf::from_to ({1,0,0}, dir))
};
};
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T> template <size_t S, typename T>
static util::geom::aabb<S,T> static util::geom::aabb<S,T>
@ -101,11 +128,24 @@ util::geom::bounds (K<S,T> k)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INSTANTIATE_S_T(S,T) \ template <size_t S, typename T>
template bool util::geom::intersects (ellipse<S,T>, util::point<S,T>); \ std::ostream&
template util::point<S,T> util::geom::project(ray<S,T>, ellipse<S,T>); \ util::geom::operator<< (std::ostream &os, ellipse<S,T> val)
template util::geom::aabb<S,T> util::geom::bounds (ellipse<S,T>); {
return os << "{ origin: " << val.origin
<< ", radius: " << val.radius
<< ", up: " << val.up
<< " }";
}
///////////////////////////////////////////////////////////////////////////////
#define INSTANTIATE_S_T(S,T) \
template util::geom::aabb<S,T> util::geom::bounds (ellipse<S,T>); \
template std::ostream& util::geom::operator<< (std::ostream&, ellipse<S,T>);
//template util::point<S,T> util::geom::project(ray<S,T>, ellipse<S,T>);
//template bool util::geom::intersects (ellipse<S,T>, util::point<S,T>);
INSTANTIATE_S_T(2,float) INSTANTIATE_S_T(2,float)
INSTANTIATE_S_T(3,float) INSTANTIATE_S_T(3,float)

View File

@ -19,11 +19,13 @@
#include "fwd.hpp" #include "fwd.hpp"
#include "../view.hpp"
#include "../point.hpp" #include "../point.hpp"
#include "../vector.hpp" #include "../vector.hpp"
#include <cstdlib> #include <cstdlib>
#include <random> #include <random>
#include <iosfwd>
namespace util::geom { namespace util::geom {
@ -43,6 +45,11 @@ namespace util::geom {
using ellipse3f = ellipse<3,float>; using ellipse3f = ellipse<3,float>;
template <size_t S, typename T>
bool
intersects (ellipse<S,T>, point<S,T>);
/// returns the approximate surface area of the ellipse /// returns the approximate surface area of the ellipse
/// ///
/// the general form involves _substantially_ more expensive and /// the general form involves _substantially_ more expensive and
@ -94,6 +101,14 @@ namespace util::geom {
); );
// generate a covering ellipsoid for an arbitrary set of points
//
// this isn't guaranteed to be optimal in any specific sense. but it
// ought not be outrageously inefficient...
ellipse3f
cover (util::view<const point3f*>);
/// returns a point that is uniformly distributed about the ellipse /// returns a point that is uniformly distributed about the ellipse
/// surface. /// surface.
/// ///
@ -122,6 +137,11 @@ namespace util::geom {
return self.origin + util::vector3f {x,y,z} / d; return self.origin + util::vector3f {x,y,z} / d;
} }
template <size_t S, typename T>
std::ostream&
operator<< (std::ostream&, ellipse<S,T>);
} }

View File

@ -62,6 +62,55 @@ test_area (util::TAP::logger &tap)
} }
void
test_cover (util::TAP::logger &tap)
{
static struct {
std::vector<util::point3f> cloud;
const char *message;
} const TESTS[] = {
{ {
{-1, 0, 0},
{ 0, 0, 0},
{ 1, 0, 0},
}, "three spaced across x"
},
{ {
{-1, 0, 0},
{ 0, 0, 0},
{ 1, 0, 0},
{ 0,-1, 0},
{ 0, 1, 0},
}, "planar circle about origin"
},
{ {
{-1, 0, 0},
{ 0, 0, 0},
{ 1, 0, 0},
{ 0,-1, 0},
{ 0, 1, 0},
{ 0, 0,-1},
{ 0, 0, 1},
}, "sphere about origin"
},
};
for (auto const &t: TESTS) {
auto const shape = util::geom::cover (util::view<const util::point3f*> {t.cloud});
auto const success = std::all_of (
t.cloud.begin (),
t.cloud.end (),
[shape] (auto p) {
return intersects (shape, p);
});
std::cout << shape << '\n';
tap.expect (success, "%!", t.message);
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
int int
main () main ()
@ -70,7 +119,7 @@ main ()
test_intersection (tap); test_intersection (tap);
test_area (tap); test_area (tap);
test_cover (tap);
return tap.status (); return tap.status ();
} }