/* * 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 */ #include "ellipse.hpp" #include "ops.hpp" #include "aabb.hpp" #include "ray.hpp" #include "sphere.hpp" #include "point.hpp" #include "../matrix.hpp" using util::geom::ellipse; /////////////////////////////////////////////////////////////////////////////// template static bool intersects (ellipse e, util::point p) { auto mag = (p - e.origin) * (p - e.origin) / (e.radius * e.radius); return std::accumulate (mag.begin (), mag.end (), 0) <= 1; } //----------------------------------------------------------------------------- template class A, template class B> bool util::geom::intersects (A a, B b) { return ::intersects (a, b); } /////////////////////////////////////////////////////////////////////////////// template util::point util::geom::project (util::geom::ray lhs, util::geom::ellipse rhs) { return lhs.origin + lhs.direction * distance (lhs, rhs); } /////////////////////////////////////////////////////////////////////////////// // query a ray-ellipse distance by transforming spaces such that the ellipse is // a sphere template ValueT util::geom::distance (ray r, ellipse 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 ()) * 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, sphere {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.homog ()).template redim ()); } /////////////////////////////////////////////////////////////////////////////// template static util::geom::aabb bounds (ellipse e) { return { e.origin - e.radius, e.origin + e.radius }; } //----------------------------------------------------------------------------- template class K> util::geom::aabb util::geom::bounds (K k) { return ::bounds (k); } /////////////////////////////////////////////////////////////////////////////// #define INSTANTIATE_S_T(S,T) \ template bool util::geom::intersects (ellipse, util::point); \ template util::point util::geom::project(ray, ellipse); \ template util::geom::aabb util::geom::bounds (ellipse); INSTANTIATE_S_T(2,float) INSTANTIATE_S_T(3,float)