DNA Calib 1.1
Project brief
Computations.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "tdm/Types.h"
6#include "tdm/Mat.h"
7#include "tdm/Vec.h"
8
9#ifdef _MSC_VER
10 #pragma warning(push)
11 #pragma warning(disable : 4365 4987)
12#endif
13#include <cmath>
14#include <utility>
15#ifdef _MSC_VER
16 #pragma warning(pop)
17#endif
18
19namespace tdm {
20
21template<typename T>
22inline vec3<T> cross(const vec3<T>& lhs, const vec3<T>& rhs) {
23 return vec3<T>{
24 lhs[1] * rhs[2] - lhs[2] * rhs[1],
25 lhs[2] * rhs[0] - lhs[0] * rhs[2],
26 lhs[0] * rhs[1] - lhs[1] * rhs[0]
27 };
28}
29
30template<dim_t L, typename T>
31inline T dot(const vec<L, T>& lhs, const vec<L, T>& rhs) {
32 return (lhs * rhs).sum();
33}
34
35template<dim_t L, typename T>
37 return v.negate();
38}
39
40template<dim_t R, dim_t C, typename T>
42 return m.negate();
43}
44
45template<dim_t L, typename T>
46inline typename std::enable_if<std::is_floating_point<T>::value, T>::type length(const vec<L, T>& v) {
47 return v.length();
48}
49
50template<dim_t L, typename T>
51inline typename std::enable_if<std::is_floating_point<T>::value, vec<L, T> >::type normalize(vec<L, T> v) {
52 v.normalize();
53 return v;
54}
55
56template<dim_t R, dim_t C, typename T>
58 using row_type = typename mat<C, R, T>::row_type;
59 mat<C, R, T> ret;
60 ret.apply([&m](row_type& row, dim_t i) {
61 row = m.column(i);
62 });
63 return ret;
64}
65
66namespace impl {
67
68#pragma push_macro("minor")
69#undef minor
70
71template<dim_t N, typename T>
72inline void minor(const mat<N, N, T>& input, dim_t dimensions, dim_t i, dim_t j, mat<N, N, T>& output) {
73 for (dim_t outRow{}, inRow{}; inRow < dimensions; ++inRow) {
74 for (dim_t outCol{}, inCol{}; inCol < dimensions; ++inCol) {
75 if ((inRow != i) && (inCol != j)) {
76 output(outRow, outCol) = input(inRow, inCol);
77 ++outCol;
78 if (outCol == (dimensions - static_cast<dim_t>(1))) {
79 outCol = {};
80 ++outRow;
81 }
82 }
83 }
84 }
85}
86
87template<dim_t N, typename T>
88inline T determinant(const mat<N, N, T>& m, dim_t dimensions) {
89 if (dimensions == static_cast<dim_t>(1)) {
90 return m(0, 0);
91 }
92
93 T result{};
94 mat<N, N, T> temp;
95 auto sign = static_cast<T>(1);
96 const dim_t i{};
97 for (dim_t j{}; j < dimensions; ++j) {
98 minor(m, dimensions, i, j, temp);
99 result += (sign * m(i, j) * determinant(temp, dimensions - 1));
100 sign = -sign;
101 }
102
103 return result;
104}
105
106template<dim_t N, typename T>
108 if (m.rows() == static_cast<dim_t>(1)) {
109 return mat<N, N, T>{static_cast<T>(1)};
110 }
111
112 mat<N, N, T> result;
113 mat<N, N, T> temp;
114 for (dim_t row{}; row < m.rows(); ++row) {
115 for (dim_t col{}; col < m.columns(); ++col) {
116 minor(m, N, row, col, temp);
117 const T sign = static_cast<T>((row + col) % 2u == 0u ? 1 : -1);
118 result(col, row) = (sign * determinant(temp, N - 1));
119 }
120 }
121 return result;
122}
123
124#pragma pop_macro("minor")
125
126} // namespace impl
127
128template<dim_t N, typename T>
129inline T determinant(const mat<N, N, T>& m) {
130 return impl::determinant(m, N);
131}
132
133template<dim_t N, typename T>
135 T det = determinant(m);
136 if (det == T{}) {
137 return {};
138 }
139
141 mat<N, N, T> inv;
142 for (dim_t row{}; row < m.rows(); ++row) {
143 for (dim_t col{}; col < m.columns(); ++col) {
144 inv(row, col) = adj(row, col) / det;
145 }
146 }
147 return inv;
148}
149
150template<dim_t N, typename T>
151inline T trace(const mat<N, N, T>& m) {
152 T trace{0};
153 for (dim_t row{}; row < m.rows(); ++row) {
154 trace += m(row, row);
155 }
156 return trace;
157}
158
159} // namespace tdm
void minor(const mat< N, N, T > &input, dim_t dimensions, dim_t i, dim_t j, mat< N, N, T > &output)
Definition: Computations.h:72
mat< N, N, T > adjoint(const mat< N, N, T > &m)
Definition: Computations.h:107
T determinant(const mat< N, N, T > &m, dim_t dimensions)
Definition: Computations.h:88
Definition: Computations.h:19
std::enable_if< std::is_floating_point< T >::value, vec< L, T > >::type normalize(vec< L, T > v)
Definition: Computations.h:51
std::size_t dim_t
Definition: Types.h:22
T dot(const vec< L, T > &lhs, const vec< L, T > &rhs)
Definition: Computations.h:31
vec< L, T > negate(vec< L, T > v)
Definition: Computations.h:36
T determinant(const mat< N, N, T > &m)
Definition: Computations.h:129
std::enable_if< std::is_floating_point< T >::value, T >::type length(const vec< L, T > &v)
Definition: Computations.h:46
vec3< T > cross(const vec3< T > &lhs, const vec3< T > &rhs)
Definition: Computations.h:22
mat< C, R, T > transpose(const mat< R, C, T > &m)
Definition: Computations.h:57
T trace(const mat< N, N, T > &m)
Definition: Computations.h:151
mat< N, N, T > inverse(const mat< N, N, T > &m)
Definition: Computations.h:134
Definition: Mat.h:17
mat & apply(F func)
Definition: Mat.h:140
Definition: Vec.h:10
std::enable_if< std::is_floating_point< V >::value, V >::type length() const
Definition: Vec.h:168
vec & negate()
Definition: Vec.h:178
std::enable_if< std::is_floating_point< V >::value, vec & >::type normalize()
Definition: Vec.h:174