-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhierarchy_explicit_loader.cpp
149 lines (139 loc) · 4.06 KB
/
hierarchy_explicit_loader.cpp
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
/*
* 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 george.drettakis@inria.fr
*/
#include "hierarchy_loader.h"
#include "hierarchy_explicit_loader.h"
#include <vector>
#include <Eigen/Dense>
#include "common.h"
#include <iostream>
#include <fstream>
#include "half.hpp"
float getWeight(Eigen::Vector3f& pos, int chunk_id,
std::vector<Eigen::Vector3f>& chunk_centers)
{
float dist_to_current_center = (pos - chunk_centers[chunk_id]).norm();
float min_dist_to_other_center = 1e12f;
for (int other_chunk_id(0); other_chunk_id < chunk_centers.size(); other_chunk_id++)
{
if (other_chunk_id != chunk_id)
{
float dist_to_other_center = (pos - chunk_centers[other_chunk_id]).norm();
if (min_dist_to_other_center > dist_to_other_center)
{
min_dist_to_other_center = dist_to_other_center;
}
}
}
float falloff = 0.05f;
if (dist_to_current_center <= (1.f - falloff) * min_dist_to_other_center)
{
return 1.f;
}
else if (dist_to_current_center > (1.f + falloff) * min_dist_to_other_center)
{
return 0.f;
}
else
{
float a = -1.f / (2 * falloff * min_dist_to_other_center);
float b = (1.f + falloff) / (2 * falloff);
return a * dist_to_current_center + b;
}
}
std::vector<ExplicitTreeNode*> buildTreeRec(ExplicitTreeNode* expliciteNode,
std::vector<Gaussian>& gaussians,
int chunk_id,
std::vector<Eigen::Vector3f>& chunk_centers,
Node& node,
int node_id,
std::vector<Eigen::Vector3f>& pos,
std::vector<SHs>& shs,
std::vector<float>& alphas,
std::vector<Eigen::Vector3f>& scales,
std::vector<Eigen::Vector4f>& rot,
std::vector<Node>& nodes,
std::vector<Box>& boxes)
{
expliciteNode->depth = node.depth;
expliciteNode->bounds = boxes[node_id];
int n_valid_gaussians = 0;
if (node.depth > 0)
{
for (int n(0); n < node.count_merged; n++)
{
float weigth = getWeight(pos[node.start + n], chunk_id, chunk_centers);
if (weigth > 0.f)
{
n_valid_gaussians++;
Gaussian g;
g.position = pos[node.start + n];
g.rotation = rot[node.start + n];
g.opacity = alphas[node.start + n] * weigth;
g.scale = scales[node.start + n].array().exp();
g.shs = shs[node.start + n];
expliciteNode->merged.push_back(g);
}
}
}
else
{
for (int n(0); n < node.count_leafs; n++)
{
float weigth = getWeight(pos[node.start + n], chunk_id, chunk_centers);
if (weigth > 0.f)
{
n_valid_gaussians++;
Gaussian g;
g.position = pos[node.start + n];
g.rotation = rot[node.start + n];
g.opacity = alphas[node.start + n] * weigth;
g.scale = scales[node.start + n].array().exp();
g.shs = shs[node.start + n];
expliciteNode->leaf_indices.push_back(gaussians.size());
gaussians.push_back(g);
}
}
}
std::vector<ExplicitTreeNode*> children;
for (int i = 0; i < node.count_children; i++)
{
ExplicitTreeNode* newNode = new ExplicitTreeNode;
std::vector<ExplicitTreeNode*> newChildren = buildTreeRec(newNode, gaussians, chunk_id, chunk_centers,
nodes[node.start_children + i], node.start_children + i,
pos, shs, alphas, scales, rot, nodes, boxes
);
children.insert(children.end(), newChildren.begin(), newChildren.end());
}
if (n_valid_gaussians > 0)
{
expliciteNode->children = children;
return std::vector<ExplicitTreeNode*>(1, expliciteNode);
}
else
{
return children;
}
}
void HierarchyExplicitLoader::loadExplicit(
const char* filename, std::vector<Gaussian>& gaussians, ExplicitTreeNode* root,
int chunk_id, std::vector<Eigen::Vector3f>& chunk_centers)
{
std::vector<Eigen::Vector3f> pos;
std::vector<SHs> shs;
std::vector<float> alphas;
std::vector<Eigen::Vector3f> scales;
std::vector<Eigen::Vector4f> rot;
std::vector<Node> nodes;
std::vector<Box> boxes;
HierarchyLoader::load(filename, pos, shs, alphas, scales, rot, nodes, boxes);
pos[0] = chunk_centers[chunk_id];
buildTreeRec(root, gaussians, chunk_id, chunk_centers, nodes[0], 0, pos, shs, alphas, scales, rot, nodes, boxes);
}