libcruft-util/geom/ellipse.cpp

152 lines
4.6 KiB
C++

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015-2018 Danny Robson <danny@nerdcruft.net>
*/
#include "ellipse.hpp"
#include "ops.hpp"
#include "aabb.hpp"
#include "ray.hpp"
#include "sphere.hpp"
#include "quaternion.hpp"
#include "../point.hpp"
#include "../matrix.hpp"
#include "../coord/iostream.hpp"
using util::geom::ellipse;
///////////////////////////////////////////////////////////////////////////////
template <>
bool
util::geom::intersects (ellipse3f e, util::point3f p)
{
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);
//return std::accumulate (mag.begin (), mag.end (), 0) <= 1;
}
///////////////////////////////////////////////////////////////////////////////
// query a ray-ellipse distance by transforming spaces such that the ellipse is
// a sphere
template <>
float
util::geom::distance (ray3f r, ellipse3f e)
{
// find a transform that puts the ellipse at origin and scales it to a
// unit sphere.
auto const from_scaled = util::translation (e.origin.template as<vector> ()) *
util::quaternionf::from_to ({0,1,0}, e.up) *
util::scale (e.radius);
auto const to_scaled = inverse (from_scaled);
// transform the ray into this new space and query against a unit sphere
auto const scaled_r = to_scaled * r;
auto const scaled_d = distance (scaled_r, sphere3f {0, 1.f});
auto const scaled_p = scaled_r.at (scaled_d);
// transform the result back into the original space
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>
static util::geom::aabb<S,T>
bounds (ellipse<S,T> e)
{
return {
e.origin - e.radius,
e.origin + e.radius
};
}
//-----------------------------------------------------------------------------
template <size_t S, typename T, template <size_t,typename> class K>
util::geom::aabb<S,T>
util::geom::bounds (K<S,T> k)
{
return ::bounds (k);
}
///////////////////////////////////////////////////////////////////////////////
template <size_t S, typename T>
std::ostream&
util::geom::operator<< (std::ostream &os, ellipse<S,T> val)
{
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(3,float)