Commit 4cb1fd64 authored by Philip Trettner's avatar Philip Trettner
Browse files

renamed distance2 and length2 to distance_sqr and length_sqr

parent 195b5f58
......@@ -48,7 +48,7 @@ TG_NODISCARD constexpr ScalarT area(aabb<3, ScalarT> const& b)
template <class ScalarT>
TG_NODISCARD constexpr ScalarT area(box<2, ScalarT> const& b)
{
return 4 * tg::sqrt(length2(b.half_extents[0]) * length2(b.half_extents[1]));
return 4 * tg::sqrt(length_sqr(b.half_extents[0]) * length_sqr(b.half_extents[1]));
}
template <class ScalarT>
TG_NODISCARD constexpr ScalarT area(box<3, ScalarT> const& b)
......
......@@ -39,7 +39,7 @@ template <int D, class ScalarT>
TG_NODISCARD constexpr bool contains(pos<D, ScalarT> const& b, pos<D, ScalarT> const& o, ScalarT eps = ScalarT(0))
{
if (eps > 0)
return distance2(b, o) < eps * eps;
return distance_sqr(b, o) < eps * eps;
return b == o;
}
......@@ -81,7 +81,7 @@ TG_NODISCARD constexpr bool contains(box<D, ScalarT> const& b, pos<D, ScalarT> c
auto r = o - b.center;
// TODO: unroll
for (auto i = 0; i < D; ++i)
if (tg::abs(dot(b.half_extents[i], r)) > length2(b.half_extents[i]) + eps)
if (tg::abs(dot(b.half_extents[i], r)) > length_sqr(b.half_extents[i]) + eps)
return false;
return true;
}
......@@ -96,13 +96,13 @@ template <int D, class ScalarT>
TG_NODISCARD constexpr bool contains(ball<D, ScalarT> const& s, pos<D, ScalarT> const& p, ScalarT eps = ScalarT(0))
{
auto r = s.radius + eps;
return distance2(s.center, p) <= r * r;
return distance_sqr(s.center, p) <= r * r;
}
template <int D, class ScalarT>
TG_NODISCARD constexpr bool contains(sphere<D, ScalarT> const& s, pos<D, ScalarT> const& p, ScalarT eps = ScalarT(0))
{
return tg::abs(distance2(s.center, p) - s.radius * s.radius) <= eps;
return tg::abs(distance_sqr(s.center, p) - s.radius * s.radius) <= eps;
}
// Note that eps is used to compare 2D areas, not 1D lengths
......@@ -162,7 +162,7 @@ TG_NODISCARD constexpr bool contains(cylinder<3, ScalarT> const& c, pos<3, Scala
auto ad = c.axis.pos1 - c.axis.pos0;
auto d0 = dot(pd, ad);
auto hsqd = length2(ad);
auto hsqd = length_sqr(ad);
auto rsqd = pow2(c.radius);
if (d0 < 0 || d0 > hsqd) // behind a cap
......
......@@ -16,26 +16,26 @@
namespace tg
{
// Base case for distance2 of point/point
// Base case for distance_sqr of point/point
template <int D, class ScalarA, class ScalarB>
TG_NODISCARD constexpr auto distance2(pos<D, ScalarA> const& a, pos<D, ScalarB> const& b) -> decltype(length2(a - b))
TG_NODISCARD constexpr auto distance_sqr(pos<D, ScalarA> const& a, pos<D, ScalarB> const& b) -> decltype(length_sqr(a - b))
{
return length2(a - b);
return length_sqr(a - b);
}
// Default implementation of distance as sqrt(distance2)
// Default implementation of distance as sqrt(distance_sqr)
template <class A, class B>
TG_NODISCARD constexpr auto distance(A const& a, B const& b) -> decltype(sqrt(distance2(a, b)))
TG_NODISCARD constexpr auto distance(A const& a, B const& b) -> decltype(sqrt(distance_sqr(a, b)))
{
return sqrt(distance2(a, b));
return sqrt(distance_sqr(a, b));
}
// Default implementation of distance2 as distance2(ca, cb) for closest points ca and cb
// Default implementation of distance_sqr as distance_sqr(ca, cb) for closest points ca and cb
template <class A, class B>
TG_NODISCARD constexpr auto distance2(A const& a, B const& b) -> decltype(length2(closest_points(a, b).first - closest_points(a, b).second))
TG_NODISCARD constexpr auto distance_sqr(A const& a, B const& b) -> decltype(length_sqr(closest_points(a, b).first - closest_points(a, b).second))
{
auto cp = closest_points(a, b);
return length2(cp.first - cp.second);
return length_sqr(cp.first - cp.second);
}
// Convenience for distance to (0,0,0)
......@@ -45,9 +45,9 @@ TG_NODISCARD constexpr auto distance_to_origin(Obj const& o) -> decltype(distanc
return distance(o, pos_type_for<Obj>::zero);
}
template <class Obj>
TG_NODISCARD constexpr auto distance2_to_origin(Obj const& o) -> decltype(distance(o, pos_type_for<Obj>::zero))
TG_NODISCARD constexpr auto distance_sqr_to_origin(Obj const& o) -> decltype(distance(o, pos_type_for<Obj>::zero))
{
return distance2(o, pos_type_for<Obj>::zero);
return distance_sqr(o, pos_type_for<Obj>::zero);
}
......@@ -70,7 +70,7 @@ TG_NODISCARD constexpr fractional_result<ScalarT> distance(pos<3, ScalarT> const
// =========== Other Implementations ===========
template <class ScalarT>
TG_NODISCARD constexpr ScalarT distance2(pos<2, ScalarT> const& p, quadric<2, ScalarT> const& q)
TG_NODISCARD constexpr ScalarT distance_sqr(pos<2, ScalarT> const& p, quadric<2, ScalarT> const& q)
{
/// Residual L2 error as given by x^T A x - 2 r^T x + c
......@@ -84,7 +84,7 @@ TG_NODISCARD constexpr ScalarT distance2(pos<2, ScalarT> const& p, quadric<2, Sc
+ q.d_sqr; // + c
}
template <class ScalarT>
TG_NODISCARD constexpr ScalarT distance2(pos<3, ScalarT> const& p, quadric<3, ScalarT> const& q)
TG_NODISCARD constexpr ScalarT distance_sqr(pos<3, ScalarT> const& p, quadric<3, ScalarT> const& q)
{
/// Residual L2 error as given by x^T A x - 2 r^T x + c
......@@ -99,7 +99,7 @@ TG_NODISCARD constexpr ScalarT distance2(pos<3, ScalarT> const& p, quadric<3, Sc
+ q.d_sqr; // + c
}
template <int D, class ScalarT>
TG_NODISCARD constexpr ScalarT distance2(quadric<D, ScalarT> const& q, pos<D, ScalarT> const& p)
TG_NODISCARD constexpr ScalarT distance_sqr(quadric<D, ScalarT> const& q, pos<D, ScalarT> const& p)
{
return distance(p, q);
}
......
......@@ -182,7 +182,7 @@ template <class ScalarT>
TG_NODISCARD constexpr auto intersection(sphere<3, ScalarT> const& a, sphere<3, ScalarT> const& b)
-> intersection_result<sphere<3, ScalarT>, sphere<3, ScalarT>>
{
auto d2 = distance2(a.center, b.center);
auto d2 = distance_sqr(a.center, b.center);
auto const empty_circle = circle<3, ScalarT>(tg::pos<3, ScalarT>::zero, ScalarT(0), tg::dir<3, ScalarT>::pos_x);
......@@ -243,7 +243,7 @@ TG_NODISCARD constexpr auto intersection(circle<2, ScalarT> const& a, circle<2,
if (a.center == b.center && a.radius == b.radius)
return {true, {}, {}};
auto d2 = distance2(a.center, b.center);
auto d2 = distance_sqr(a.center, b.center);
auto d = tg::sqrt(d2);
auto ar = a.radius;
auto br = b.radius;
......@@ -399,7 +399,7 @@ TG_NODISCARD constexpr optional<ScalarT> intersection_coordinate(ray<D, ScalarT>
{
auto const t = dot(s.center - r.origin, r.dir);
auto const p = r[t];
auto const dt = tg::sqrt(s.radius * s.radius - distance2(p, s.center));
auto const dt = tg::sqrt(s.radius * s.radius - distance_sqr(p, s.center));
if (t - dt >= 0)
return t - dt;
......@@ -471,7 +471,7 @@ TG_NODISCARD constexpr optional<ScalarT> intersection_coordinate(ray<3, ScalarT>
return t;
auto const p = r[t.value()];
if (distance2(p, c.center) > c.radius * c.radius)
if (distance_sqr(p, c.center) > c.radius * c.radius)
return {};
return t;
......
......@@ -9,7 +9,7 @@
namespace tg
{
template <int D, class ScalarT>
TG_NODISCARD constexpr ScalarT length2(vec<D, ScalarT> const& v)
TG_NODISCARD constexpr ScalarT length_sqr(vec<D, ScalarT> const& v)
{
auto a = vec<D, ScalarT>(v);
return dot(a, a);
......@@ -18,18 +18,18 @@ TG_NODISCARD constexpr ScalarT length2(vec<D, ScalarT> const& v)
template <int D, class ScalarT>
TG_NODISCARD constexpr fractional_result<ScalarT> length(vec<D, ScalarT> const& v)
{
return tg::sqrt(length2(vec<D, fractional_result<ScalarT>>(v)));
return tg::sqrt(length_sqr(vec<D, fractional_result<ScalarT>>(v)));
}
template <int D, class ScalarT>
TG_NODISCARD constexpr fractional_result<ScalarT> length2(segment<D, ScalarT> const& s)
TG_NODISCARD constexpr fractional_result<ScalarT> length_sqr(segment<D, ScalarT> const& s)
{
return length2(s.pos0 - s.pos1);
return length_sqr(s.pos0 - s.pos1);
}
template <int D, class ScalarT>
TG_NODISCARD constexpr fractional_result<ScalarT> length(segment<D, ScalarT> const& s)
{
return tg::sqrt(length2(segment<D, fractional_result<ScalarT>>(s)));
return tg::sqrt(length_sqr(segment<D, fractional_result<ScalarT>>(s)));
}
} // namespace tg
......@@ -169,7 +169,7 @@ TG_NODISCARD constexpr pos<D, ScalarT> uniform(Rng& rng, sphere<D, ScalarT> cons
while (true)
{
auto p = uniform_vec(rng, ub);
auto l = length2(p);
auto l = length_sqr(p);
if (l > ScalarT(0) && l <= ScalarT(1))
return s.center + p * (s.radius / tg::sqrt(l));
}
......@@ -182,7 +182,7 @@ TG_NODISCARD constexpr pos<D, ScalarT> uniform(Rng& rng, ball<D, ScalarT> const&
while (true)
{
auto p = uniform_vec(rng, ub);
auto l = length2(p);
auto l = length_sqr(p);
if (l <= ScalarT(1))
return b.center + p * b.radius;
}
......@@ -251,7 +251,7 @@ struct sampler<dir<D, ScalarT>>
while (true)
{
auto p = uniform_vec(rng, ub);
auto l = length2(p);
auto l = length_sqr(p);
if (l > ScalarT(0) && l <= ScalarT(1))
return tg::dir<D, ScalarT>(p / tg::sqrt(l));
}
......
......@@ -31,7 +31,7 @@ TG_NODISCARD constexpr ScalarT volume(aabb<3, ScalarT> const& b)
template <class ScalarT>
TG_NODISCARD constexpr ScalarT volume(box<3, ScalarT> const& b)
{
return 8 * tg::sqrt(length2(b.half_extents[0]) * length2(b.half_extents[1]) * length2(b.half_extents[2]));
return 8 * tg::sqrt(length_sqr(b.half_extents[0]) * length_sqr(b.half_extents[1]) * length_sqr(b.half_extents[2]));
}
template <class ScalarT>
......
......@@ -13,13 +13,13 @@ namespace tg
template <int D, class ScalarT>
TG_NODISCARD constexpr bool is_normalized(vec<D, ScalarT> const& v, ScalarT eps = 2 * D * tg::epsilon<ScalarT>)
{
return tg::abs(ScalarT(1) - length2(v)) < eps;
return tg::abs(ScalarT(1) - length_sqr(v)) < eps;
}
template <int D, class ScalarT>
TG_NODISCARD constexpr bool is_zero(vec<D, ScalarT> const& v, ScalarT eps = 2 * tg::epsilon<ScalarT>)
{
return length2(v) < eps * eps;
return length_sqr(v) < eps * eps;
}
template <int D, class ScalarT>
......
......@@ -12,7 +12,7 @@ namespace tg
*
* Notable functions:
* - distance(pos, quadric) for residual L2 error
* - distance2(pos, quadric) for residual squared L2 error
* - distance_sqr(pos, quadric) for residual squared L2 error
* - closest_point(quadric) for the position minimizing the distance
* - operator + * /
*/
......@@ -73,12 +73,12 @@ public:
r += normal * d + vec2_t(pos) * s2;
// TODO: maybe positional uncertainty
d_sqr += d * d + distance2_to_origin(pos) * s2;
d_sqr += d * d + distance_sqr_to_origin(pos) * s2;
}
// TODO: fix me
template<class T>
friend constexpr T distance2(pos<2, T> const& p, quadric<2, T> const& q);
friend constexpr T distance_sqr(pos<2, T> const& p, quadric<2, T> const& q);
template<class T>
friend constexpr pos<2, T> closest_point(quadric<2, T> const& q);
};
......@@ -130,7 +130,7 @@ public:
r += normal * d + vec3_t(pos) * s2;
// TODO: maybe positional uncertainty
d_sqr += d * d + distance2_to_origin(pos) * s2;
d_sqr += d * d + distance_sqr_to_origin(pos) * s2;
}
/// Adds two quadrics
......@@ -150,7 +150,7 @@ public:
// TODO: fix me
template<class T>
friend constexpr T distance2(pos<3, T> const& p, quadric<3, T> const& q);
friend constexpr T distance_sqr(pos<3, T> const& p, quadric<3, T> const& q);
template<class T>
friend constexpr pos<3, T> closest_point(quadric<3, T> const& q);
};
......
Supports Markdown
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