-
Notifications
You must be signed in to change notification settings - Fork 34
/
ArborX_DetailsVector.hpp
94 lines (78 loc) · 3.02 KB
/
ArborX_DetailsVector.hpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/****************************************************************************
* Copyright (c) 2017-2022 by the ArborX authors *
* All rights reserved. *
* *
* This file is part of the ArborX library. ArborX is *
* distributed under a BSD 3-clause license. For the licensing terms see *
* the LICENSE file in the top-level directory. *
* *
* SPDX-License-Identifier: BSD-3-Clause *
****************************************************************************/
#ifndef ARBORX_DETAILS_VECTOR_HPP
#define ARBORX_DETAILS_VECTOR_HPP
#include <ArborX_GeometryTraits.hpp>
#include <ArborX_HyperPoint.hpp>
#include <Kokkos_MathematicalFunctions.hpp>
namespace ArborX::Details
{
// Same as Point, but a different type (can't inherit from Point as it would
// run into constexpr and inability to aggregate initialize)
template <int DIM, typename Coordinate = float>
struct Vector
{
static_assert(DIM > 0);
// Initialization is needed to be able to use Vector in constexpr
// TODO: do we want to actually want to zero initialize it? Seems like
// unnecessary work.
Coordinate _coords[DIM] = {};
KOKKOS_FUNCTION
constexpr auto &operator[](unsigned int i) { return _coords[i]; }
KOKKOS_FUNCTION
constexpr auto const &operator[](unsigned int i) const { return _coords[i]; }
KOKKOS_FUNCTION
constexpr auto dot(Vector const &w) const
{
auto const &v = *this;
Coordinate r = 0;
for (int d = 0; d < DIM; ++d)
r += v[d] * w[d];
return r;
}
KOKKOS_FUNCTION
auto norm() const { return Kokkos::sqrt(dot(*this)); }
KOKKOS_FUNCTION
constexpr auto cross(Vector const &w) const
{
static_assert(DIM == 3);
auto const &v = *this;
return Vector{v[1] * w[2] - v[2] * w[1], v[2] * w[0] - v[0] * w[2],
v[0] * w[1] - v[1] * w[0]};
}
friend KOKKOS_FUNCTION constexpr bool operator==(Vector const &v,
Vector const &w)
{
bool match = true;
for (int d = 0; d < DIM; ++d)
match &= (v[d] == w[d]);
return match;
}
};
template <typename... T>
Vector(T...) -> Vector<
sizeof...(T),
std::conditional_t<(... || std::is_same_v<std::decay_t<T>, double>), double,
float>>;
} // namespace ArborX::Details
template <typename Point, typename Enable = std::enable_if_t<
ArborX::GeometryTraits::is_point_v<Point>>>
KOKKOS_INLINE_FUNCTION constexpr auto operator-(Point const &end,
Point const &begin)
{
namespace GT = ArborX::GeometryTraits;
constexpr int DIM = GT::dimension_v<Point>;
ArborX::Details::Vector<DIM, GT::coordinate_type_t<Point>> v;
for (int d = 0; d < DIM; ++d)
v[d] = end[d] - begin[d];
return v;
}
#endif