summaryrefslogtreecommitdiffhomepage
path: root/VectorMath.h
blob: 72eab4d243f8996481be83ee6fa778d15f4d7d94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once

#include <cmath>
#include <type_traits>


template <typename T> static auto test_z(int) -> decltype(decltype(T::z){}, std::true_type{});
template <typename> static auto test_z(...) -> std::false_type;
template <typename T> struct has_z : decltype(test_z<T>(0)) {};


template <typename V, typename T=decltype(V::x), typename std::enable_if<has_z<V>::value, bool>::type=true>
T
dist(const V& lhs, const V& rhs)
{
    return std::sqrt(
        std::pow(lhs.x - rhs.x, 2) +
        std::pow(lhs.y - rhs.y, 2) +
        std::pow(lhs.z - rhs.z, 2));
}


template <typename V, typename T=decltype(V::x), typename std::enable_if<!has_z<V>::value, bool>::type=true>
T
dist(const V& lhs, const V& rhs)
{
    return std::sqrt(
        std::pow(lhs.x - rhs.x, 2) +
        std::pow(lhs.y - rhs.y, 2));
}