EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Vec.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2022 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
12#ifndef EBGEOMETRY_VEC_HPP
13#define EBGEOMETRY_VEC_HPP
14
15// Std includes
16#include <algorithm>
17#include <cstddef>
18#include <type_traits>
19
20// Our includes
21#include "EBGeometry_Array.hpp"
22#include "EBGeometry_GPU.hpp"
23#include "EBGeometry_Macros.hpp"
24#include "EBGeometry_Math.hpp"
25
26namespace EBGeometry {
27
35template <typename T>
36class Vec2T
37{
38public:
39 static_assert(std::is_floating_point_v<T>, "Vec2T<T>: T must be a floating-point type");
47 Vec2T() noexcept = default;
48
54 constexpr Vec2T(const Vec2T& u) noexcept = default;
55
63 constexpr Vec2T(const T& a_x, const T& a_y) noexcept;
64
68 ~Vec2T() = default;
69
76 inline constexpr T&
77 operator[](size_t i) noexcept;
78
84 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
85 inline constexpr const T&
86 operator[](size_t i) const noexcept;
87
92 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
93 inline static constexpr Vec2T<T>
94 zeros() noexcept;
95
100 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
101 inline static constexpr Vec2T<T>
102 ones() noexcept;
103
108 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
109 inline static constexpr Vec2T<T>
110 min() noexcept;
111
116 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
117 inline static constexpr Vec2T<T>
118 max() noexcept;
119
124 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
125 inline static constexpr Vec2T<T>
126 infinity() noexcept;
127
133 constexpr Vec2T<T>&
134 operator=(const Vec2T& a_other) noexcept = default;
135
141 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
142 inline constexpr Vec2T<T>
143 operator+(const Vec2T& a_other) const noexcept;
144
150 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
151 inline constexpr Vec2T<T>
152 operator-(const Vec2T& a_other) const noexcept;
153
158 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
159 inline constexpr Vec2T<T>
160 operator-() const noexcept;
161
167 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
168 inline constexpr Vec2T<T>
169 operator*(const T& s) const noexcept;
170
176 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
177 inline constexpr Vec2T<T>
178 operator/(const T& s) const noexcept;
179
186 inline constexpr Vec2T<T>&
187 operator+=(const Vec2T& a_other) noexcept;
188
195 inline constexpr Vec2T<T>&
196 operator-=(const Vec2T& a_other) noexcept;
197
204 inline constexpr Vec2T<T>&
205 operator*=(const T& s) noexcept;
206
213 inline constexpr Vec2T<T>&
214 operator/=(const T& s) noexcept;
215
221 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
222 inline constexpr T
223 dot(const Vec2T& a_other) const noexcept;
224
230 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
231 inline T
232 length() const noexcept;
233
238 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
239 inline constexpr T
240 length2() const noexcept;
241
242protected:
246 Array<T, 2> m_X;
247};
248
256template <typename T>
257class Vec3T
258{
259public:
260 static_assert(std::is_floating_point_v<T>, "Vec3T<T>: T must be a floating-point type");
261
269 Vec3T() noexcept = default;
270
276 constexpr Vec3T(const Vec3T<T>& a_u) noexcept = default;
277
286 constexpr Vec3T(const T& a_x, const T& a_y, const T& a_z) noexcept;
287
291 ~Vec3T() noexcept = default;
292
297 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
298 inline static constexpr Vec3T<T>
299 zeros() noexcept;
300
305 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
306 inline static constexpr Vec3T<T>
307 ones() noexcept;
308
314 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
315 inline static constexpr Vec3T<T>
316 unit(const size_t a_dir) noexcept;
317
322 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
323 inline static constexpr Vec3T<T>
324 min() noexcept;
325
330 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
331 inline static constexpr Vec3T<T>
332 max() noexcept;
333
338 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
339 inline static constexpr Vec3T<T>
340 infinity() noexcept;
341
350 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
351 inline constexpr bool
352 lessLX(const Vec3T<T>& u) const noexcept;
353
360 inline constexpr T&
361 operator[](size_t i) noexcept;
362
368 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
369 inline constexpr const T&
370 operator[](size_t i) const noexcept;
371
377 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
378 inline constexpr bool
379 operator==(const Vec3T<T>& u) const noexcept;
380
386 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
387 inline constexpr bool
388 operator!=(const Vec3T<T>& u) const noexcept;
389
399 bool
400 operator<(const Vec3T<T>& u) const noexcept = delete;
401 bool
402 operator>(const Vec3T<T>& u) const noexcept = delete;
403 bool
404 operator<=(const Vec3T<T>& u) const noexcept = delete;
405 bool
406 operator>=(const Vec3T<T>& u) const noexcept = delete;
408
414 constexpr Vec3T<T>&
415 operator=(const Vec3T<T>& u) noexcept = default;
416
422 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
423 inline constexpr Vec3T<T>
424 operator+(const Vec3T<T>& u) const noexcept;
425
431 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
432 inline constexpr Vec3T<T>
433 operator-(const Vec3T<T>& u) const noexcept;
434
439 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
440 inline constexpr Vec3T<T>
441 operator+() const noexcept;
442
447 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
448 inline constexpr Vec3T<T>
449 operator-() const noexcept;
450
457 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
458 inline constexpr Vec3T<T>
459 operator*(const T& s) const noexcept;
460
467 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
468 inline constexpr Vec3T<T>
469 operator*(const Vec3T<T>& s) const noexcept;
470
476 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
477 inline constexpr Vec3T<T>
478 operator/(const T& s) const noexcept;
479
485 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
486 inline constexpr Vec3T<T>
487 operator/(const Vec3T<T>& v) const noexcept;
488
495 inline constexpr Vec3T<T>&
496 operator+=(const Vec3T<T>& u) noexcept;
497
504 inline constexpr Vec3T<T>&
505 operator-=(const Vec3T<T>& u) noexcept;
506
513 inline constexpr Vec3T<T>&
514 operator*=(const T& s) noexcept;
515
522 inline constexpr Vec3T<T>&
523 operator/=(const T& s) noexcept;
524
530 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
531 inline constexpr Vec3T<T>
532 cross(const Vec3T<T>& u) const noexcept;
533
539 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
540 inline constexpr T
541 dot(const Vec3T<T>& u) const noexcept;
542
548 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
549 inline size_t
550 minDir(const bool a_doAbs) const noexcept;
551
559 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
560 inline size_t
561 maxDir(const bool a_doAbs) const noexcept;
562
568 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
569 inline T
570 length() const noexcept;
571
576 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
577 inline constexpr T
578 length2() const noexcept;
579
580protected:
584 Array<T, 3> m_X;
585};
586
595template <class R, typename T, std::enable_if_t<std::is_arithmetic_v<R>, int> = 0>
596[[nodiscard]] EBGEOMETRY_HOST_DEVICE
597inline constexpr Vec2T<T>
598operator*(const R& s, const Vec2T<T>& a_other) noexcept;
599
609template <class R, typename T, std::enable_if_t<std::is_arithmetic_v<R>, int> = 0>
610[[nodiscard]] EBGEOMETRY_HOST_DEVICE
611inline constexpr Vec2T<T>
612operator/(const R& s, const Vec2T<T>& a_other) noexcept;
613
623template <typename T>
624[[nodiscard]] EBGEOMETRY_HOST_DEVICE
625inline Vec2T<T>
626min(const Vec2T<T>& u, const Vec2T<T>& v) noexcept;
627
637template <typename T>
638[[nodiscard]] EBGEOMETRY_HOST_DEVICE
639inline Vec2T<T>
640max(const Vec2T<T>& u, const Vec2T<T>& v) noexcept;
641
649template <typename T>
650[[nodiscard]] EBGEOMETRY_HOST_DEVICE
651inline T
652dot(const Vec2T<T>& u, const Vec2T<T>& v) noexcept;
653
660template <typename T>
661[[nodiscard]] EBGEOMETRY_HOST_DEVICE
662inline T
663length(const Vec2T<T>& v) noexcept;
664
673template <class R, typename T, std::enable_if_t<std::is_arithmetic_v<R>, int> = 0>
674[[nodiscard]] EBGEOMETRY_HOST_DEVICE
675inline constexpr Vec3T<T>
676operator*(const R& s, const Vec3T<T>& u) noexcept;
677
687template <class R, typename T, std::enable_if_t<std::is_arithmetic_v<R>, int> = 0>
688[[nodiscard]] EBGEOMETRY_HOST_DEVICE
689inline constexpr Vec3T<T>
690operator/(const R& s, const Vec3T<T>& u) noexcept;
691
701template <typename T>
702[[nodiscard]] EBGEOMETRY_HOST_DEVICE
703inline constexpr Vec3T<T>
704min(const Vec3T<T>& u, const Vec3T<T>& v) noexcept;
705
715template <typename T>
716[[nodiscard]] EBGEOMETRY_HOST_DEVICE
717inline constexpr Vec3T<T>
718max(const Vec3T<T>& u, const Vec3T<T>& v) noexcept;
719
728template <typename T>
729[[nodiscard]] EBGEOMETRY_HOST_DEVICE
730inline constexpr Vec3T<T>
731clamp(const Vec3T<T>& v, const Vec3T<T>& lo, const Vec3T<T>& hi) noexcept;
732
740template <typename T>
741[[nodiscard]] EBGEOMETRY_HOST_DEVICE
742inline constexpr T
743dot(const Vec3T<T>& u, const Vec3T<T>& v) noexcept;
744
752template <typename T>
753[[nodiscard]] EBGEOMETRY_HOST_DEVICE
754inline constexpr Vec3T<T>
755cross(const Vec3T<T>& u, const Vec3T<T>& v) noexcept;
756
763template <typename T>
764[[nodiscard]] EBGEOMETRY_HOST_DEVICE
765inline T
766length(const Vec3T<T>& v) noexcept;
767
768static_assert(std::is_trivially_copyable_v<Vec2T<float>>, "Vec2T<float> must be trivially copyable");
769static_assert(std::is_trivially_copyable_v<Vec2T<double>>, "Vec2T<double> must be trivially copyable");
770static_assert(std::is_trivially_copyable_v<Vec3T<float>>, "Vec3T<float> must be trivially copyable");
771static_assert(std::is_trivially_copyable_v<Vec3T<double>>, "Vec3T<double> must be trivially copyable");
772static_assert(std::is_trivially_default_constructible_v<Vec2T<float>>,
773 "Vec2T<float> must be trivially default constructible");
774static_assert(std::is_trivially_default_constructible_v<Vec2T<double>>,
775 "Vec2T<double> must be trivially default constructible");
776static_assert(std::is_trivially_default_constructible_v<Vec3T<float>>,
777 "Vec3T<float> must be trivially default constructible");
778static_assert(std::is_trivially_default_constructible_v<Vec3T<double>>,
779 "Vec3T<double> must be trivially default constructible");
780
781} // namespace EBGeometry
782
783#include "EBGeometry_VecImplem.hpp"
784
785#endif
A fixed-size array that compiles for both host and device.
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
Utility macros for EBGeometry.
Scalar helpers and numeric limits that compile for both host and device.
Two-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:37
constexpr T dot(const Vec2T &a_other) const noexcept
Dot product.
static constexpr Vec2T< T > infinity() noexcept
Return a vector with infinite components.
static constexpr Vec2T< T > zeros() noexcept
Return a vector with x = y = 0.
constexpr T length2() const noexcept
Compute squared vector length.
static constexpr Vec2T< T > min() noexcept
Return the most-negative representable vector.
Array< T, 2 > m_X
Vector components.
Definition EBGeometry_Vec.hpp:246
Vec2T() noexcept=default
Default constructor. Leaves the components uninitialized.
T length() const noexcept
Compute length of vector.
static constexpr Vec2T< T > max() noexcept
Return the most-positive representable vector.
static constexpr Vec2T< T > ones() noexcept
Return a vector with x = y = 1.
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
Vec3T() noexcept=default
Default constructor. Leaves the components uninitialized.
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
constexpr Vec3T< T > clamp(const Vec3T< T > &v, const Vec3T< T > &lo, const Vec3T< T > &hi) noexcept
Component-wise clamp of a 3D vector.
constexpr Vec3T< T > cross(const Vec3T< T > &u, const Vec3T< T > &v) noexcept
Cross product of two 3D vectors.
Fixed-size array with host- and device-callable members.
Definition EBGeometry_Array.hpp:33