-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwriter.cpp
147 lines (133 loc) · 3.64 KB
/
writer.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
/*
* 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]
*/
#include "writer.h"
#include <iostream>
#include <fstream>
#include "hierarchy_writer.h"
#include <map>
void populateRec(
const ExplicitTreeNode* treenode,
int id,
const std::vector<Gaussian>& gaussians,
std::vector<Eigen::Vector3f>& positions,
std::vector<Eigen::Vector4f>& rotations,
std::vector<Eigen::Vector3f>& log_scales,
std::vector<float>& opacities,
std::vector<SHs>& shs,
std::vector<Node>& basenodes,
std::vector<Box>& boxes,
std::map<int, const ExplicitTreeNode*>* base2tree = nullptr)
{
if(base2tree)
base2tree->insert(std::make_pair(id, treenode));
boxes[id] = treenode->bounds;
basenodes[id].start = positions.size();
for (auto& i : treenode->leaf_indices)
{
const Gaussian& g = gaussians[i];
positions.push_back(g.position);
rotations.push_back(g.rotation);
log_scales.push_back(g.scale.array().log());
opacities.push_back(g.opacity);
shs.push_back(g.shs);
}
basenodes[id].count_leafs = treenode->leaf_indices.size();
for (auto& g : treenode->merged)
{
positions.push_back(g.position);
rotations.push_back(g.rotation);
log_scales.push_back(g.scale.array().log());
opacities.push_back(g.opacity);
shs.push_back(g.shs);
}
basenodes[id].count_merged = treenode->merged.size();
basenodes[id].start_children = basenodes.size();
for (int n = 0; n < treenode->children.size(); n++)
{
basenodes.push_back(Node());
basenodes.back().parent = id;
boxes.push_back(Box());
}
basenodes[id].count_children = treenode->children.size();
basenodes[id].depth = treenode->depth;
for (int n = 0; n < treenode->children.size(); n++)
{
populateRec(
treenode->children[n],
basenodes[id].start_children + n,
gaussians,
positions,
rotations,
log_scales,
opacities,
shs,
basenodes,
boxes,
base2tree);
}
}
void recTraverse(int id, std::vector<Node>& nodes, int& count)
{
if (nodes[id].depth == 0)
count++;
if (nodes[id].count_children != 0 && nodes[id].depth == 0)
throw std::runtime_error("An error occurred in traversal");
for (int i = 0; i < nodes[id].count_children; i++)
{
recTraverse(nodes[id].start_children + i, nodes, count);
}
}
void Writer::makeHierarchy(
const std::vector<Gaussian>& gaussians,
const ExplicitTreeNode* root,
std::vector<Eigen::Vector3f>& positions,
std::vector<Eigen::Vector4f>& rotations,
std::vector<Eigen::Vector3f>& log_scales,
std::vector<float>& opacities,
std::vector<SHs>& shs,
std::vector<Node>& basenodes,
std::vector<Box>& boxes,
std::map<int, const ExplicitTreeNode*>* base2tree)
{
basenodes.resize(1);
boxes.resize(1);
populateRec(
root,
0,
gaussians,
positions, rotations, log_scales, opacities, shs, basenodes, boxes,
base2tree);
}
void Writer::writeHierarchy(const char* filename, const std::vector<Gaussian>& gaussians, const ExplicitTreeNode* root, bool compressed)
{
std::vector<Eigen::Vector3f> positions;
std::vector<Eigen::Vector4f> rotations;
std::vector<Eigen::Vector3f> log_scales;
std::vector<float> opacities;
std::vector<SHs> shs;
std::vector<Node> basenodes;
std::vector<Box> boxes;
makeHierarchy(gaussians, root, positions, rotations, log_scales, opacities, shs, basenodes, boxes);
HierarchyWriter writer;
writer.write(
filename,
positions.size(),
basenodes.size(),
positions.data(),
shs.data(),
opacities.data(),
log_scales.data(),
rotations.data(),
basenodes.data(),
boxes.data(),
compressed
);
}