Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
vec.h
Go to the documentation of this file.
1#ifndef BVH_V2_VEC_H
2#define BVH_V2_VEC_H
3
4#include "bvh/v2/utils.h"
5
6#include <cstddef>
7#include <numeric>
8#include <cmath>
9#include <algorithm>
10
11namespace bvh::v2 {
12
13template <typename T, size_t N>
14struct Vec {
16
17 Vec() = default;
18 template <typename... Args>
19 BVH_ALWAYS_INLINE Vec(T x, T y, Args&&... args) : values { x, y, static_cast<T>(std::forward<Args>(args))... } {}
20 BVH_ALWAYS_INLINE explicit Vec(T x) { std::fill(values, values + N, x); }
21
22 template <typename Compare>
23 BVH_ALWAYS_INLINE size_t get_best_axis(Compare&& compare) const {
24 size_t axis = 0;
25 static_for<1, N>([&] (size_t i) {
26 if (compare(values[i], values[axis]))
27 axis = i;
28 });
29 return axis;
30 }
31
32 // Note: These functions are designed to be robust to NaNs
33 BVH_ALWAYS_INLINE size_t get_largest_axis() const { return get_best_axis(std::greater<T>()); }
34 BVH_ALWAYS_INLINE size_t get_smallest_axis() const { return get_best_axis(std::less<T>()); }
35
36 BVH_ALWAYS_INLINE T& operator [] (size_t i) { return values[i]; }
37 BVH_ALWAYS_INLINE T operator [] (size_t i) const { return values[i]; }
38
39 template <typename F>
42 static_for<0, N>([&] (size_t i) { v[i] = f(i); });
43 return v;
44 }
45};
46
47template <typename T, size_t N>
49 return Vec<T, N>::generate([&] (size_t i) { return a[i] + b[i]; });
50}
51
52template <typename T, size_t N>
54 return Vec<T, N>::generate([&] (size_t i) { return a[i] - b[i]; });
55}
56
57template <typename T, size_t N>
59 return Vec<T, N>::generate([&] (size_t i) { return -a[i]; });
60}
61
62template <typename T, size_t N>
64 return Vec<T, N>::generate([&] (size_t i) { return a[i] * b[i]; });
65}
66
67template <typename T, size_t N>
69 return Vec<T, N>::generate([&] (size_t i) { return a[i] / b[i]; });
70}
71
72template <typename T, size_t N>
74 return Vec<T, N>::generate([&] (size_t i) { return a[i] * b; });
75}
76
77template <typename T, size_t N>
79 return b * a;
80}
81
82template <typename T, size_t N>
84 return Vec<T, N>::generate([&] (size_t i) { return a / b[i]; });
85}
86
87template <typename T, size_t N>
89 return Vec<T, N>::generate([&] (size_t i) { return robust_min(a[i], b[i]); });
90}
91
92template <typename T, size_t N>
94 return Vec<T, N>::generate([&] (size_t i) { return robust_max(a[i], b[i]); });
95}
96
97template <typename T, size_t N>
99 // return std::transform_reduce(a.values, a.values + N, b.values, T(0));
100 return std::inner_product(a.values, a.values + N, b.values, T(0));
101}
102
103template <typename T>
105 return Vec<T, 3>(
106 a[1] * b[2] - a[2] * b[1],
107 a[2] * b[0] - a[0] * b[2],
108 a[0] * b[1] - a[1] * b[0]);
109}
110
111template <typename T, size_t N>
113 return Vec<T, N>::generate([&] (size_t i) { return fast_mul_add(a[i], b[i], c[i]); });
114}
115
116template <typename T, size_t N>
118 return Vec<T, N>::generate([&] (size_t i) { return safe_inverse(v[i]); });
119}
120
121template <typename T, size_t N>
123 return std::sqrt(dot(v, v));
124}
125
126template <typename T, size_t N>
128 return v * (static_cast<T>(1.) / length(v));
129}
130
131} // namespace bvh::v2
132
133#endif
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define N
Int_t Compare(const void *item1, const void *item2)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
#define F(x, y, z)
Definition bbox.h:9
BVH_ALWAYS_INLINE Vec< T, N > operator-(const Vec< T, N > &a, const Vec< T, N > &b)
Definition vec.h:53
BVH_ALWAYS_INLINE Vec< T, N > operator*(const Vec< T, N > &a, const Vec< T, N > &b)
Definition vec.h:63
BVH_ALWAYS_INLINE T safe_inverse(T x)
Computes the inverse of the given value, always returning a finite value.
Definition utils.h:59
BVH_ALWAYS_INLINE Vec< T, N > normalize(const Vec< T, N > &v)
Definition vec.h:127
BVH_ALWAYS_INLINE Vec< T, N > operator/(const Vec< T, N > &a, const Vec< T, N > &b)
Definition vec.h:68
BVH_ALWAYS_INLINE Vec< T, 3 > cross(const Vec< T, 3 > &a, const Vec< T, 3 > &b)
Definition vec.h:104
BVH_ALWAYS_INLINE T robust_max(T a, T b)
Definition utils.h:43
BVH_ALWAYS_INLINE T fast_mul_add(T a, T b, T c)
Fast multiply-add operation.
Definition utils.h:74
BVH_ALWAYS_INLINE T robust_min(T a, T b)
Definition utils.h:41
BVH_ALWAYS_INLINE T dot(const Vec< T, N > &a, const Vec< T, N > &b)
Definition vec.h:98
BVH_ALWAYS_INLINE Vec< T, N > operator+(const Vec< T, N > &a, const Vec< T, N > &b)
Definition vec.h:48
#define BVH_ALWAYS_INLINE
Definition platform.h:19
BVH_ALWAYS_INLINE Vec(T x, T y, Args &&... args)
Definition vec.h:19
BVH_ALWAYS_INLINE Vec(T x)
Definition vec.h:20
T values[N]
Definition vec.h:15
BVH_ALWAYS_INLINE size_t get_smallest_axis() const
Definition vec.h:34
BVH_ALWAYS_INLINE size_t get_largest_axis() const
Definition vec.h:33
Vec()=default
static BVH_ALWAYS_INLINE Vec< T, N > generate(F &&f)
Definition vec.h:40
BVH_ALWAYS_INLINE size_t get_best_axis(Compare &&compare) const
Definition vec.h:23
BVH_ALWAYS_INLINE T & operator[](size_t i)
Definition vec.h:36