-
Notifications
You must be signed in to change notification settings - Fork 1
/
PotreeCloud.cs
263 lines (205 loc) · 8.3 KB
/
PotreeCloud.cs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using Fusee.Math.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace PotreeSharp
{
public class PotreeCloud : IDisposable
{
private readonly string path;
private PotreeMetadata metadata;
public PotreeHierarchy Data;
private BinaryReader binaryReader;
public PotreeCloud(string path)
{
this.path = path;
// Loading metadata
metadata = JsonConvert.DeserializeObject<PotreeMetadata>(File.ReadAllText(path + @"//metadata.json"));
int pointSize = 0;
if (metadata != null)
{
foreach (var metaAttributeItem in metadata.Attributes)
{
pointSize += metaAttributeItem.Size;
}
metadata.PointSize = pointSize;
}
// BinaryReader
binaryReader = new BinaryReader(File.OpenRead(path + @"//octree.bin"));
// Loading hierarchy
Data = LoadHierarchy();
}
private PotreeHierarchy LoadHierarchy()
{
var firstChunkSize = metadata.Hierarchy.FirstChunkSize;
var stepSize = metadata.Hierarchy.StepSize;
var depth = metadata.Hierarchy.Depth;
var data = File.ReadAllBytes(path + @"//hierarchy.bin");
PotreeNode root = new PotreeNode()
{
Name = "r",
Aabb = new AABBd(metadata.BoundingBox.Min, metadata.BoundingBox.Max)
};
var hierarchy = new PotreeHierarchy();
long offset = 0;
LoadHierarchyRecursive(ref root, ref data, offset, firstChunkSize);
hierarchy.Nodes = new();
root.Traverse(n => hierarchy.Nodes.Add(n));
hierarchy.TreeRoot = root;
return hierarchy;
}
private void LoadHierarchyRecursive(ref PotreeNode root, ref byte[] data, long offset, long size)
{
int bytesPerNode = 22;
int numNodes = (int)(size / bytesPerNode);
var nodes = new List<PotreeNode>(numNodes)
{
root
};
for (int i = 0; i < numNodes; i++)
{
var currentNode = nodes[i];
if (currentNode == null)
currentNode = new PotreeNode();
ulong offsetNode = (ulong)offset + (ulong)(i * bytesPerNode);
var nodeType = data[offsetNode + 0];
int childMask = BitConverter.ToInt32(data, (int)offsetNode + 1);
var numPoints = BitConverter.ToUInt32(data, (int)offsetNode + 2);
var byteOffset = BitConverter.ToInt64(data, (int)offsetNode + 6);
var byteSize = BitConverter.ToInt64(data, (int)offsetNode + 14);
currentNode.NodeType = (NodeType)nodeType;
currentNode.NumPoints = numPoints;
currentNode.ByteOffset = byteOffset;
currentNode.ByteSize = byteSize;
if (currentNode.NodeType == NodeType.PROXY)
{
LoadHierarchyRecursive(ref currentNode, ref data, byteOffset, byteSize);
}
else
{
for (int childIndex = 0; childIndex < 8; childIndex++)
{
bool childExists = ((1 << childIndex) & childMask) != 0;
if (!childExists)
{
continue;
}
string childName = currentNode.Name + childIndex.ToString();
PotreeNode child = new PotreeNode();
child.Aabb = childAABB(currentNode.Aabb, childIndex);
child.Name = childName;
currentNode.children[childIndex] = child;
child.Parent = currentNode;
nodes.Add(child);
}
}
}
AABBd childAABB(AABBd aabb, int index) {
double3 min = aabb.min;
double3 max = aabb.max;
double3 size = max - min;
if ((index & 0b0001) > 0)
{
min.z += size.z / 2;
}
else
{
max.z -= size.z / 2;
}
if ((index & 0b0010) > 0)
{
min.y += size.y / 2;
}
else
{
max.y -= size.y / 2;
}
if ((index & 0b0100) > 0)
{
min.x += size.x / 2;
}
else
{
max.x -= size.x / 2;
}
return new AABBd(min, max);
}
}
public PotreeNode FindNode(string id)
{
return Data.Nodes.Find(n => n.Name == id);
}
// ID is something like "r64" root->6->4
public PotreeNode LoadNodeData(string id)
{
if (!metadata.Encoding.Contains("DEFAULT"))
throw new NotImplementedException("Non-default encoding is not supported!");
var node = FindNode(id);
if (node == null)
return null;
var points = new PotreePoint[node.NumPoints];
var attributeOffset = 0;
foreach (var metaitem in metadata.Attributes)
{
if (metaitem.Name == "position")
{
for (int i = 0; i < node.NumPoints; i++)
{
binaryReader.BaseStream.Position = node.ByteOffset + attributeOffset + i * metadata.PointSize;
points[i].x = (binaryReader.ReadInt32() * (float)metadata.Scale.x) + (float)metadata.Offset.x;
points[i].y = (binaryReader.ReadInt32() * (float)metadata.Scale.y) + (float)metadata.Offset.y;
points[i].z = (binaryReader.ReadInt32() * (float)metadata.Scale.z) + (float)metadata.Offset.z;
// In js they subtract the min offset for every point,I guess that is just moving the pointcloud to the coordinate origin.
// We should do this in usercode
}
}
else if (metaitem.Name.Contains("rgb"))
{
for (int i = 0; i < node.NumPoints; i++)
{
binaryReader.BaseStream.Position = node.ByteOffset + attributeOffset + i * metadata.PointSize;
UInt16 r = binaryReader.ReadUInt16();
UInt16 g = binaryReader.ReadUInt16();
UInt16 b = binaryReader.ReadUInt16();
points[i].r = (byte)(r > 255 ? r / 256 : r);
points[i].g = (byte)(g > 255 ? g / 256 : g);
points[i].b = (byte)(b > 255 ? b / 256 : b);
if (metaitem.Name.Contains("rgba"))
{
UInt16 a = binaryReader.ReadUInt16();
points[i].a = (byte)(a > 255 ? a / 256 : a);
}
else
{
points[i].a = 255;
}
}
}
else if (metaitem.Name.Equals("classification"))
{
for (int i = 0; i < node.NumPoints; i++)
{
binaryReader.BaseStream.Position = node.ByteOffset + attributeOffset + i + metadata.PointSize;
points[i].classification = binaryReader.ReadSByte();
}
}
attributeOffset += metaitem.Size;
}
node.points = points;
node.IsLoaded = true;
return node;
}
public void UnloadNodeData(string id)
{
var node = FindNode(id);
node.points = null;
node.IsLoaded = false;
GC.Collect();
}
public void Dispose()
{
binaryReader.Dispose();
}
}
}