Loading...
Searching...
No Matches
vec_3d.hpp
1/*
2 * Copyright (c) 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 <cuspatial/cuda_utils.hpp>
20#include <cuspatial/detail/utility/floating_point.cuh>
21
22#include <algorithm>
23#include <ostream>
24
25namespace cuspatial {
26
39template <typename T>
40class alignas(4 * sizeof(T)) vec_3d {
41 public:
42 using value_type = T;
43 value_type x;
44 value_type y;
45 value_type z;
46
47 private:
51 friend std::ostream& operator<<(std::ostream& os, cuspatial::vec_3d<T> const& vec)
52 {
53 return os << "(" << vec.x << "," << vec.y << "," << vec.z << ")";
54 }
55
59 friend bool CUSPATIAL_HOST_DEVICE operator==(vec_3d<T> const& lhs, vec_3d<T> const& rhs)
60 {
61 return detail::float_equal<T>(lhs.x, rhs.x) && detail::float_equal(lhs.y, rhs.y) &&
62 detail::float_equal(lhs.z, rhs.z);
63 }
64
68 friend vec_3d<T> CUSPATIAL_HOST_DEVICE operator+(vec_3d<T> const& a, vec_3d<T> const& b)
69 {
70 return vec_3d<T>{a.x + b.x, a.y + b.y, a.z + b.z};
71 }
72
76 friend vec_3d<T> CUSPATIAL_HOST_DEVICE operator-(vec_3d<T> const& a, vec_3d<T> const& b)
77 {
78 return vec_3d<T>{a.x - b.x, a.y - b.y, a.z - b.z};
79 }
80
84 friend vec_3d<T> CUSPATIAL_HOST_DEVICE operator-(vec_3d<T> const& a)
85 {
86 return vec_3d<T>{-a.x, -a.y, -a.z};
87 }
88
92 friend vec_3d<T> CUSPATIAL_HOST_DEVICE operator*(vec_3d<T> vec, T const& r)
93 {
94 return vec_3d<T>{vec.x * r, vec.y * r, vec.z * r};
95 }
96
100 friend vec_3d<T> CUSPATIAL_HOST_DEVICE operator*(T const& r, vec_3d<T> vec) { return vec * r; }
101
105 friend vec_3d<T>& CUSPATIAL_HOST_DEVICE operator+=(vec_3d<T>& a, vec_3d<T> const& b)
106 {
107 a.x += b.x;
108 a.y += b.y;
109 a.z += b.z;
110 return a;
111 }
112
116 friend vec_3d<T>& CUSPATIAL_HOST_DEVICE operator-=(vec_3d<T>& a, vec_3d<T> const& b)
117 {
118 return a += -b;
119 }
120
126 friend bool CUSPATIAL_HOST_DEVICE operator<(vec_3d<T> const& lhs, vec_3d<T> const& rhs)
127 {
128 if (lhs.x < rhs.x) return true;
129 if (lhs.x > rhs.x) return false;
130 if (lhs.y < rhs.y) return true;
131 if (lhs.y > rhs.y) return false;
132 return lhs.z < rhs.z;
133 }
134
138 friend bool CUSPATIAL_HOST_DEVICE operator>(vec_3d<T> const& lhs, vec_3d<T> const& rhs)
139 {
140 return rhs < lhs;
141 }
142
146 friend bool CUSPATIAL_HOST_DEVICE operator<=(vec_3d<T> const& lhs, vec_3d<T> const& rhs)
147 {
148 return !(lhs > rhs);
149 }
150
154 friend bool CUSPATIAL_HOST_DEVICE operator>=(vec_3d<T> const& lhs, vec_3d<T> const& rhs)
155 {
156 return !(lhs < rhs);
157 }
158};
159
160// Deduction guide enables CTAD
161template <typename T>
162vec_3d(T x, T y, T z) -> vec_3d<T>;
163
167template <typename T>
168T CUSPATIAL_HOST_DEVICE dot(vec_3d<T> const& a, vec_3d<T> const& b)
169{
170 return a.x * b.x + a.y * b.y + a.z * b.z;
171}
172
178template <typename T>
179vec_3d<T> CUSPATIAL_HOST_DEVICE cross(vec_3d<T> const& a, vec_3d<T> const& b)
180{
181 return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
182}
183
184} // namespace cuspatial
A generic 3D vector type.
Definition vec_3d.hpp:40
friend vec_3d< T > &CUSPATIAL_HOST_DEVICE operator-=(vec_3d< T > &a, vec_3d< T > const &b)
Translate a 3D point.
Definition vec_3d.hpp:116
friend vec_3d< T > CUSPATIAL_HOST_DEVICE operator-(vec_3d< T > const &a)
Invert a 3D vector.
Definition vec_3d.hpp:84
friend bool CUSPATIAL_HOST_DEVICE operator==(vec_3d< T > const &lhs, vec_3d< T > const &rhs)
Compare two 3D vectors for equality.
Definition vec_3d.hpp:59
friend std::ostream & operator<<(std::ostream &os, cuspatial::vec_3d< T > const &vec)
Output stream operator for vec_3d<T> for human-readable formatting.
Definition vec_3d.hpp:51
friend vec_3d< T > CUSPATIAL_HOST_DEVICE operator-(vec_3d< T > const &a, vec_3d< T > const &b)
Element-wise subtraction of two 3D vectors.
Definition vec_3d.hpp:76
friend bool CUSPATIAL_HOST_DEVICE operator>=(vec_3d< T > const &lhs, vec_3d< T > const &rhs)
Greater than or equal to operator for two 3D points.
Definition vec_3d.hpp:154
friend vec_3d< T > &CUSPATIAL_HOST_DEVICE operator+=(vec_3d< T > &a, vec_3d< T > const &b)
Translate a 3D point.
Definition vec_3d.hpp:105
friend bool CUSPATIAL_HOST_DEVICE operator<=(vec_3d< T > const &lhs, vec_3d< T > const &rhs)
Less than or equal to operator for two 3D points.
Definition vec_3d.hpp:146
friend bool CUSPATIAL_HOST_DEVICE operator<(vec_3d< T > const &lhs, vec_3d< T > const &rhs)
Less than operator for two 3D points.
Definition vec_3d.hpp:126
T CUSPATIAL_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
friend vec_3d< T > CUSPATIAL_HOST_DEVICE operator+(vec_3d< T > const &a, vec_3d< T > const &b)
Element-wise addition of two 3D vectors.
Definition vec_3d.hpp:68
vec_3d< T > CUSPATIAL_HOST_DEVICE cross(vec_3d< T > const &a, vec_3d< T > const &b)
Compute cross product of two 3D vectors.
Definition vec_3d.hpp:179
friend vec_3d< T > CUSPATIAL_HOST_DEVICE operator*(vec_3d< T > vec, T const &r)
Scale a 3D vector by a factor r.
Definition vec_3d.hpp:92
friend bool CUSPATIAL_HOST_DEVICE operator>(vec_3d< T > const &lhs, vec_3d< T > const &rhs)
Greater than operator for two 3D points.
Definition vec_3d.hpp:138
friend vec_3d< T > CUSPATIAL_HOST_DEVICE operator*(T const &r, vec_3d< T > vec)
Scale a 3d vector by ratio r.
Definition vec_3d.hpp:100