-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommon.h
176 lines (153 loc) · 3.89 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (C) 2024, Inria
* GRAPHDECO research group, https://team.inria.fr/graphdeco
* All rights reserved.
*
* This software is free for non-commercial, research and evaluation use
* under the terms of the LICENSE.md file.
*
* For inquiries contact [email protected]
*/
#pragma once
#include <Eigen/Dense>
#include <iostream>
#include "types.h"
#include <float.h>
#include <memory>
static float sigmoid(const float m1)
{
return 1.0f / (1.0f + exp(-m1));
}
typedef Eigen::Matrix<float, 6, 1> Cov;
typedef Eigen::Vector3f Point;
struct RichPoint
{
Eigen::Vector3f position;
Eigen::Vector3f normal;
float shs[48];
float opacity;
Eigen::Vector3f scale;
float rotation[4];
};
struct LessRichPoint
{
Eigen::Vector3f position;
Eigen::Vector3f normal;
float shs[12];
float opacity;
Eigen::Vector3f scale;
float rotation[4];
};
struct Gaussian
{
Eigen::Vector3f position;
SHs shs;
float opacity;
Eigen::Vector3f scale;
Eigen::Vector4f rotation;
Cov covariance;
Box bounds99(Eigen::Vector3f& minn, Eigen::Vector3f& maxx) const
{
Eigen::Vector3f off(3.f * sqrt(covariance[0]), 3.f * sqrt(covariance[3]), 3.f * sqrt(covariance[5]));
return Box(position - off, position + off);
}
};
struct ExplicitTreeNode
{
int depth = -1;
Box bounds = Box({FLT_MAX, FLT_MAX, FLT_MAX}, {-FLT_MAX, -FLT_MAX, -FLT_MAX});
std::vector<ExplicitTreeNode*> children;
std::vector<int> leaf_indices;
std::vector<Gaussian> merged;
};
static Box getBounds(const std::vector<Gaussian>& gaussians)
{
Eigen::Vector3f minn(FLT_MAX, FLT_MAX, FLT_MAX);
Eigen::Vector3f maxx = -minn;
Eigen::Vector3f gMax, gMin;
for (int i = 0; i < gaussians.size(); i++)
{
gaussians[i].bounds99(gMin, gMax);
maxx = maxx.cwiseMax(gMax);
minn = minn.cwiseMin(gMin);
}
return Box(minn, maxx);
}
static Eigen::Matrix3f matrixFromQuat(Eigen::Vector4f rot)
{
float s = rot.x();
float x = rot.y();
float y = rot.z();
float z = rot.w();
Eigen::Matrix3f R;
R <<
1.f - 2.f * (y * y + z * z), 2.f * (x * y - s * z), 2.f * (x * z + s * y),
2.f * (x * y + s * z), 1.f - 2.f * (x * x + z * z), 2.f * (y * z - s * x),
2.f * (x * z - s * y), 2.f * (y * z + s * x), 1.f - 2.f * (x * x + y * y);
return R;
}
static void computeCovariance(const Eigen::Vector3f& scale, const Eigen::Vector4f& rot, Cov& covariance, bool debug = false)
{
Eigen::Matrix3f L = Eigen::Matrix3f::Identity();
L(0, 0) = scale.x();
L(1, 1) = scale.y();
L(2, 2) = scale.z();
auto R = matrixFromQuat(rot);
Eigen::Matrix3f T = R * L;
Eigen::Matrix3f Tf;
Tf <<
T(0, 0), T(1, 0), T(2, 0),
T(0, 1), T(1, 1), T(2, 1),
T(0, 2), T(1, 2), T(2, 2);
Eigen::Matrix3f T2 = T * Tf;
covariance[0] = T2(0, 0);
covariance[1] = T2(0, 1);
covariance[2] = T2(0, 2);
covariance[3] = T2(1, 1);
covariance[4] = T2(1, 2);
covariance[5] = T2(2, 2);
if (debug)
std::cout << covariance << std::endl;
}
template <typename T, int Options>
Eigen::Quaternion<T, 0> quatFromMatrix(const Eigen::Matrix<T, 3, 3, Options, 3, 3>& m)
{
Eigen::Quaternion<T, 0> q;
float trace = m(0, 0) + m(1, 1) + m(2, 2) + 1.f;
if (trace > 0)
{
float s = 0.5f / sqrtf(trace);
q.x() = (m(1, 2) - m(2, 1)) * s;
q.y() = (m(2, 0) - m(0, 2)) * s;
q.z() = (m(0, 1) - m(1, 0)) * s;
q.w() = 0.25f / s;
}
else
{
if ((m(0, 0) > m(1, 1)) && (m(0, 0) > m(2, 2)))
{
float s = sqrtf(1.f + m(0, 0) - m(1, 1) - m(2, 2)) * 2.f;
q.x() = 0.5f / s;
q.y() = (m(1, 0) + m(0, 1)) / s;
q.z() = (m(2, 0) + m(0, 2)) / s;
q.w() = (m(2, 1) + m(1, 2)) / s;
}
else if (m(1, 1) > m(2, 2))
{
float s = sqrtf(1.f - m(0, 0) + m(1, 1) - m(2, 2)) * 2.f;
q.x() = (m(1, 0) + m(0, 1)) / s;
q.y() = 0.5f / s;
q.z() = (m(2, 1) + m(1, 2)) / s;
q.w() = (m(2, 0) + m(0, 2)) / s;
}
else
{
float s = sqrtf(1.f - m(0, 0) - m(1, 1) + m(2, 2)) * 2.f;
q.x() = (m(2, 0) + m(0, 2)) / s;
q.y() = (m(2, 1) + m(1, 2)) / s;
q.z() = 0.5f / s;
q.w() = (m(1, 0) + m(0, 1)) / s;
}
}
return q;
}