Loading...
Searching...
No Matches
vec_2d.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022-2023, NVIDIA CORPORATION.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cuproj/detail/utility/cuda.hpp>
20#include <cuproj/detail/utility/floating_point.hpp>
21
22#include <algorithm>
23#include <ostream>
24
25namespace cuproj {
26
41template <typename T>
42class alignas(2 * sizeof(T)) vec_2d {
43 public:
44 using value_type = T;
45 value_type x;
46 value_type y;
47
48 private:
52 friend std::ostream& operator<<(std::ostream& os, vec_2d<T> const& vec)
53 {
54 return os << "(" << vec.x << "," << vec.y << ")";
55 }
56
60 friend bool CUPROJ_HOST_DEVICE operator==(vec_2d<T> const& lhs, vec_2d<T> const& rhs)
61 {
62 return detail::float_equal<T>(lhs.x, rhs.x) && detail::float_equal(lhs.y, rhs.y);
63 }
64
68 friend vec_2d<T> CUPROJ_HOST_DEVICE operator+(vec_2d<T> const& a, vec_2d<T> const& b)
69 {
70 return vec_2d<T>{a.x + b.x, a.y + b.y};
71 }
72
76 friend vec_2d<T> CUPROJ_HOST_DEVICE operator-(vec_2d<T> const& a, vec_2d<T> const& b)
77 {
78 return vec_2d<T>{a.x - b.x, a.y - b.y};
79 }
80
84 friend vec_2d<T> CUPROJ_HOST_DEVICE operator-(vec_2d<T> const& a)
85 {
86 return vec_2d<T>{-a.x, -a.y};
87 }
88
92 friend vec_2d<T> CUPROJ_HOST_DEVICE operator*(vec_2d<T> vec, T const& r)
93 {
94 return vec_2d<T>{vec.x * r, vec.y * r};
95 }
96
100 friend vec_2d<T> CUPROJ_HOST_DEVICE operator*(T const& r, vec_2d<T> vec) { return vec * r; }
101
105 friend vec_2d<T>& CUPROJ_HOST_DEVICE operator+=(vec_2d<T>& a, vec_2d<T> const& b)
106 {
107 a.x += b.x;
108 a.y += b.y;
109 return a;
110 }
111
115 friend vec_2d<T>& CUPROJ_HOST_DEVICE operator-=(vec_2d<T>& a, vec_2d<T> const& b)
116 {
117 return a += -b;
118 }
119
125 friend bool CUPROJ_HOST_DEVICE operator<(vec_2d<T> const& lhs, vec_2d<T> const& rhs)
126 {
127 if (lhs.x < rhs.x)
128 return true;
129 else if (lhs.x == rhs.x)
130 return lhs.y < rhs.y;
131 return false;
132 }
133
137 friend bool CUPROJ_HOST_DEVICE operator>(vec_2d<T> const& lhs, vec_2d<T> const& rhs)
138 {
139 return rhs < lhs;
140 }
141
145 friend bool CUPROJ_HOST_DEVICE operator<=(vec_2d<T> const& lhs, vec_2d<T> const& rhs)
146 {
147 return !(lhs > rhs);
148 }
149
153 friend bool CUPROJ_HOST_DEVICE operator>=(vec_2d<T> const& lhs, vec_2d<T> const& rhs)
154 {
155 return !(lhs < rhs);
156 }
157};
158
159// Deduction guide enables CTAD
160template <typename T>
161vec_2d(T x, T y) -> vec_2d<T>;
162
166template <typename T>
167T CUPROJ_HOST_DEVICE dot(vec_2d<T> const& a, vec_2d<T> const& b)
168{
169 return a.x * b.x + a.y * b.y;
170}
171
175template <typename T>
176T CUPROJ_HOST_DEVICE det(vec_2d<T> const& a, vec_2d<T> const& b)
177{
178 return a.x * b.y - a.y * b.x;
179}
180
184template <typename T>
185vec_2d<T> CUPROJ_HOST_DEVICE box_min(vec_2d<T> const& a, vec_2d<T> const& b)
186{
187#ifdef __CUDA_ARCH__
188 return vec_2d<T>{::min(a.x, b.x), ::min(a.y, b.y)};
189#else
190 return vec_2d<T>{std::min(a.x, b.x), std::min(a.y, b.y)};
191#endif
192}
193
197template <typename T>
198vec_2d<T> CUPROJ_HOST_DEVICE box_max(vec_2d<T> const& a, vec_2d<T> const& b)
199{
200#ifdef __CUDA_ARCH__
201 return vec_2d<T>{::max(a.x, b.x), ::max(a.y, b.y)};
202#else
203 return vec_2d<T>{std::max(a.x, b.x), std::max(a.y, b.y)};
204#endif
205}
206
210template <typename T>
211vec_2d<T> CUPROJ_HOST_DEVICE midpoint(vec_2d<T> const& first, vec_2d<T> const& second)
212{
213 return (first + second) * T{0.5};
214}
215
220} // namespace cuproj
A generic 2D vector type.
Definition vec_2d.hpp:42
friend bool CUPROJ_HOST_DEVICE operator==(vec_2d< T > const &lhs, vec_2d< T > const &rhs)
Compare two 2D vectors for equality.
Definition vec_2d.hpp:60
friend vec_2d< T > CUPROJ_HOST_DEVICE operator-(vec_2d< T > const &a, vec_2d< T > const &b)
Element-wise subtraction of two 2D vectors.
Definition vec_2d.hpp:76
friend vec_2d< T > CUPROJ_HOST_DEVICE operator-(vec_2d< T > const &a)
Invert a 2D vector.
Definition vec_2d.hpp:84
friend vec_2d< T > CUPROJ_HOST_DEVICE operator*(T const &r, vec_2d< T > vec)
Scale a 2d vector by ratio r.
Definition vec_2d.hpp:100
friend bool CUPROJ_HOST_DEVICE operator<(vec_2d< T > const &lhs, vec_2d< T > const &rhs)
Less than operator for two 2D points.
Definition vec_2d.hpp:125
friend std::ostream & operator<<(std::ostream &os, vec_2d< T > const &vec)
Output stream operator for vec_2d<T> for human-readable formatting.
Definition vec_2d.hpp:52
friend vec_2d< T > CUPROJ_HOST_DEVICE operator*(vec_2d< T > vec, T const &r)
Scale a 2D vector by a factor r.
Definition vec_2d.hpp:92
friend bool CUPROJ_HOST_DEVICE operator>=(vec_2d< T > const &lhs, vec_2d< T > const &rhs)
Greater than or equal to operator for two 2D points.
Definition vec_2d.hpp:153
friend vec_2d< T > &CUPROJ_HOST_DEVICE operator+=(vec_2d< T > &a, vec_2d< T > const &b)
Translate a 2D point.
Definition vec_2d.hpp:105
friend bool CUPROJ_HOST_DEVICE operator<=(vec_2d< T > const &lhs, vec_2d< T > const &rhs)
Less than or equal to operator for two 2D points.
Definition vec_2d.hpp:145
friend bool CUPROJ_HOST_DEVICE operator>(vec_2d< T > const &lhs, vec_2d< T > const &rhs)
Greater than operator for two 2D points.
Definition vec_2d.hpp:137
friend vec_2d< T > &CUPROJ_HOST_DEVICE operator-=(vec_2d< T > &a, vec_2d< T > const &b)
Translate a 2D point.
Definition vec_2d.hpp:115
friend vec_2d< T > CUPROJ_HOST_DEVICE operator+(vec_2d< T > const &a, vec_2d< T > const &b)
Element-wise addition of two 2D vectors.
Definition vec_2d.hpp:68
vec_2d< T > CUPROJ_HOST_DEVICE box_min(vec_2d< T > const &a, vec_2d< T > const &b)
Return a new vec_2d made up of the minimum x- and y-components of two input vec_2d values.
Definition vec_2d.hpp:185
vec_2d< T > CUPROJ_HOST_DEVICE midpoint(vec_2d< T > const &first, vec_2d< T > const &second)
Compute the midpoint of first and second.
Definition vec_2d.hpp:211
T CUPROJ_HOST_DEVICE dot(vec_2d< T > const &a, vec_2d< T > const &b)
Compute dot product of two 2D vectors.
Definition vec_2d.hpp:167
T CUPROJ_HOST_DEVICE det(vec_2d< T > const &a, vec_2d< T > const &b)
Compute 2D determinant of a 2x2 matrix with column vectors a and b.
Definition vec_2d.hpp:176
vec_2d< T > CUPROJ_HOST_DEVICE box_max(vec_2d< T > const &a, vec_2d< T > const &b)
Return a new vec_2d made up of the minimum x- and y-components of two input vec_2d values.
Definition vec_2d.hpp:198