forked from arborx/ArborX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArborX_Predicates.hpp
199 lines (168 loc) · 5.09 KB
/
ArborX_Predicates.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/****************************************************************************
* 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_PREDICATE_HPP
#define ARBORX_PREDICATE_HPP
#include <ArborX_DetailsAlgorithms.hpp>
namespace ArborX
{
namespace Details
{
struct NearestPredicateTag
{};
struct SpatialPredicateTag
{};
struct OrderedSpatialPredicateTag
{};
// nvcc has problems with using std::interal_constant here.
template <typename PredicateTag>
struct is_valid_predicate_tag
{
static constexpr bool value =
std::is_same<PredicateTag, SpatialPredicateTag>{} ||
std::is_same<PredicateTag, NearestPredicateTag>{} ||
std::is_same<PredicateTag, OrderedSpatialPredicateTag>{};
};
} // namespace Details
template <typename Geometry>
struct Nearest
{
using Tag = Details::NearestPredicateTag;
KOKKOS_DEFAULTED_FUNCTION
Nearest() = default;
KOKKOS_FUNCTION
Nearest(Geometry const &geometry, int k)
: _geometry(geometry)
, _k(k)
{}
template <class OtherGeometry>
KOKKOS_FUNCTION auto distance(OtherGeometry const &other) const
{
using Details::distance;
return distance(_geometry, other);
}
Geometry _geometry;
int _k = 0;
};
template <typename Geometry>
struct Intersects
{
using Tag = Details::SpatialPredicateTag;
KOKKOS_DEFAULTED_FUNCTION Intersects() = default;
KOKKOS_FUNCTION Intersects(Geometry const &geometry)
: _geometry(geometry)
{}
template <typename OtherGeometry>
KOKKOS_FUNCTION bool operator()(OtherGeometry const &other) const
{
using Details::intersects;
return intersects(_geometry, other);
}
Geometry _geometry;
};
namespace Experimental
{
template <typename Geometry>
struct OrderedSpatial
{
using Tag = Details::OrderedSpatialPredicateTag;
KOKKOS_DEFAULTED_FUNCTION
OrderedSpatial() = default;
KOKKOS_FUNCTION
OrderedSpatial(Geometry const &geometry)
: _geometry(geometry)
{}
template <class OtherGeometry>
KOKKOS_FUNCTION auto distance(OtherGeometry const &other) const
{
using Details::distance;
return distance(_geometry, other);
}
Geometry _geometry;
};
} // namespace Experimental
template <typename Geometry>
KOKKOS_INLINE_FUNCTION Nearest<Geometry> nearest(Geometry const &geometry,
int k = 1)
{
return Nearest<Geometry>(geometry, k);
}
template <typename Geometry>
KOKKOS_INLINE_FUNCTION Intersects<Geometry> intersects(Geometry const &geometry)
{
return Intersects<Geometry>(geometry);
}
template <typename Geometry>
KOKKOS_INLINE_FUNCTION int getK(Nearest<Geometry> const &pred)
{
return pred._k;
}
namespace Experimental
{
template <typename Geometry>
KOKKOS_INLINE_FUNCTION OrderedSpatial<Geometry>
ordered_intersects(Geometry const &geometry)
{
return OrderedSpatial<Geometry>(geometry);
}
} // namespace Experimental
template <typename Geometry>
KOKKOS_INLINE_FUNCTION Geometry const &
getGeometry(Nearest<Geometry> const &pred)
{
return pred._geometry;
}
template <typename Geometry>
KOKKOS_INLINE_FUNCTION Geometry const &
getGeometry(Intersects<Geometry> const &pred)
{
return pred._geometry;
}
template <typename Geometry>
KOKKOS_INLINE_FUNCTION Geometry const &
getGeometry(Experimental::OrderedSpatial<Geometry> const &pred)
{
return pred._geometry;
}
template <typename Predicate, typename Data>
struct PredicateWithAttachment : Predicate
{
KOKKOS_DEFAULTED_FUNCTION PredicateWithAttachment() = default;
KOKKOS_INLINE_FUNCTION PredicateWithAttachment(Predicate const &pred,
Data const &data)
: Predicate{pred}
, _data{data}
{}
KOKKOS_INLINE_FUNCTION PredicateWithAttachment(Predicate &&pred, Data &&data)
: Predicate(std::forward<Predicate>(pred))
, _data(std::forward<Data>(data))
{}
Data _data;
};
template <typename Predicate, typename Data>
KOKKOS_INLINE_FUNCTION Data const &
getData(PredicateWithAttachment<Predicate, Data> const &pred) noexcept
{
return pred._data;
}
template <typename Predicate, typename Data>
KOKKOS_INLINE_FUNCTION Predicate const &
getPredicate(PredicateWithAttachment<Predicate, Data> const &pred) noexcept
{
return static_cast<Predicate const &>(pred); // slicing
}
template <typename Predicate, typename Data>
KOKKOS_INLINE_FUNCTION constexpr auto attach(Predicate &&pred, Data &&data)
{
return PredicateWithAttachment<std::decay_t<Predicate>, std::decay_t<Data>>{
std::forward<Predicate>(pred), std::forward<Data>(data)};
}
} // namespace ArborX
#endif