-
Notifications
You must be signed in to change notification settings - Fork 5
/
LasDataManager.cs
528 lines (414 loc) · 15.7 KB
/
LasDataManager.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
using System;
using System.Collections.Generic;
using System.Text;
using laslib;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using las.datamanager.structures;
using System.Drawing;
namespace las.datamanager
{
public delegate void LasFileLoadingStatus(String status, float progress);
public delegate void LasFileLoadingEnd(bool success);
/// <summary>
/// Manages data dynamic loading and unloading of data from las file to a quadtree hierarchy.
/// </summary>
public partial class LasDataManager
{
public static bool CALCULATE_NORMALS = true;
public static float BufferedFOV = 30;
public static float BufferedDistance = 100;
public static float BufferedPositionRadius = 50; //circle around current position that is buffered
public static string serializiationExtension = "srl";
private Queue<QTreeLeaf> leafLoadRequestQueue;
private Thread leafLoadingThread;
private bool runLeafLoader;
private int leafLoaderMSsleepInterval = 10;
public event LasFileLoadingStatus loadingStatusEvent;
public event LasFileLoadingEnd loadingEndedEvent;
private float SelectionLOD; //for loaded points. NOT for displayed points!!!
public static int dedicatedPointMemory = 262144000; //in bytes
public static ColorPalette ColorPallette;
private static LasDataManager instance = null;
#region Public properties
public int LeafLoaderMSsleepInterval
{
get { return leafLoaderMSsleepInterval; }
set { leafLoaderMSsleepInterval = value; }
}
/// <summary>
/// Amount of memory dedicated to loaded LAS points. In bytes.
/// Points are stored in las.dataManager.Point3D structure
/// </summary>
public int DedicatedPointMemory
{
get { return dedicatedPointMemory; }
set { dedicatedPointMemory = value; }
}
#endregion
#region Constructors
public static LasDataManager GetInstance()
{
if (instance == null)
{
instance = new LasDataManager();
}
return instance;
}
static LasDataManager()
{
}
private LasDataManager()
{
//init leaf loading thread
leafLoadRequestQueue = new Queue<QTreeLeaf>(500);
runLeafLoader = true;
leafLoadingThread = new Thread(new ThreadStart(LeafLoader));
leafLoadingThread.Start();
//set event handler
QTreeLeaf.LeafLoadRequestEvent += new LeafLoadRequest(RequestLeafLoad);
GlobalBoundingCube = new BoundingCube();
}
~LasDataManager() // destructor
{
Close();
}
#endregion
/// <summary>
/// loads and initializes the file
/// </summary>
/// <param name="filename"></param>
public void Load(string filename, bool forcePreprocessing )
{
if (loadingStatusEvent != null)
{
loadingStatusEvent("Opening LAS file", 0.01f);
}
QTreeWrapper tree = AddQtreeFromFile(filename);
Initialize(tree, forcePreprocessing);
}
/// <summary>
/// serializes qtree to file.
/// </summary>
/// <param name="rewriteIfExists"></param>
private void SerializeQtreeToFile(QTreeWrapper qtree, bool rewriteIfExists)
{
Console.WriteLine("Serializing qtree to file");
string srlFilename = qtree.filename + "." + serializiationExtension;
FileStream fileStreamObject = null;
if (File.Exists(srlFilename) && !rewriteIfExists)
{
Console.WriteLine("File already exists and rewrite flag is set to false. Returning");
return;
}
try
{
fileStreamObject = new FileStream(srlFilename, FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStreamObject, qtree.qtree);
}
finally
{
if (fileStreamObject != null)
{
fileStreamObject.Close();
}
}
Console.WriteLine("Serialization finished");
}
/// <summary>
/// deserializes the tree from the file
/// </summary>
/// <returns>true if successful, false otherwise</returns>
private QTree DeserializeQtreeFromFile(string filename)
{
Console.WriteLine("Deserilaizing qtree from file");
string srlFilename = filename+"."+serializiationExtension;
if (!File.Exists(srlFilename))
{
Console.WriteLine("Deserialization failed. The file does not exist.");
return null;
}
FileStream fileStreamObject = null;
QTree tree = null;
try
{
fileStreamObject = new FileStream(srlFilename, FileMode.Open);
BinaryFormatter binaryFormatter = new BinaryFormatter();
tree = (binaryFormatter.Deserialize(fileStreamObject)) as QTree;
}
catch (Exception e)
{
Console.WriteLine("Deserialization failed: {0}"+e.ToString());
}
finally
{
if (fileStreamObject != null)
{
fileStreamObject.Close();
}
}
Console.WriteLine("Deserialization finished");
return tree;
}
/// <summary>
/// initializes data manager with data from las file and fills QTree structure with preliminary data
/// </summary>
public void Initialize(QTreeWrapper qtree, bool forcePreprocessing)
{
if (forcePreprocessing)
{
Console.WriteLine("Preprocessing forced. Not using serialized even if available");
PreprocessDataFromFile(qtree);
SerializeQtreeToFile(qtree, true);
}
else
{
QTree des = DeserializeQtreeFromFile(qtree.filename);
if (des == null)
{
Console.WriteLine("Deserialization failed. Preprocessing.");
PreprocessDataFromFile(qtree);
SerializeQtreeToFile(qtree, true);
}
else
{
qtree.qtree = des;
}
}
if (loadingEndedEvent != null)
{
loadingEndedEvent(true);
}
}
private void PreprocessDataFromFile(QTreeWrapper qtreeWrapper)
{
Stopwatch initTimer = new Stopwatch();
initTimer.Start();
qtreeWrapper.lasFile.PositionAtStartOfPointData();
uint index = 0;
uint numAllPoints = qtreeWrapper.lasFile.header.NumberOfPointRecords;
Point3D[] lineOfPoints = null;
int step = 100000;
determineInitialSelectionLOD();
TimeSpan accReadTime = new TimeSpan();
while (index < numAllPoints)
{
if (loadingStatusEvent != null)
loadingStatusEvent("Indexing points..." + index + "/" + numAllPoints, (float)index / (float)numAllPoints);
TimeSpan before = initTimer.Elapsed;
lineOfPoints = GetPoints(qtreeWrapper, index, step);
accReadTime += (initTimer.Elapsed - before);
if (lineOfPoints != null)
{
qtreeWrapper.qtree.Initialize(index, lineOfPoints, SelectionLOD);
}
index += (uint)lineOfPoints.Length;
lineOfPoints = null;
}
initTimer.Stop();
LasMetrics.GetInstance().indexing = initTimer.ElapsedMilliseconds;
LasMetrics.GetInstance().indexingNoDisk = (initTimer.Elapsed - accReadTime).TotalMilliseconds;
LasMetrics.GetInstance().avgPointsPerLeafActual = (double)numAllPoints / LasMetrics.GetInstance().numberOfNonEmptyLeafs;
LasMetrics.GetInstance().numberOfPoints += numAllPoints;
Console.WriteLine("lasDataManager.Initialize() took {0} ms for {1} points", initTimer.ElapsedMilliseconds, numAllPoints);
Console.WriteLine("reading from disk took {0} ms, indexing without reading from disk took {1} ms", accReadTime.TotalMilliseconds, LasMetrics.GetInstance().indexingNoDisk);
Console.WriteLine("nr. of all points: {0}, avg. points per leaf: {1}, without empty: {2}", numAllPoints, numAllPoints / LasMetrics.GetInstance().numberOfLeafs, LasMetrics.GetInstance().avgPointsPerLeafActual);
Console.WriteLine("QTreeLeaf.findEnd() took {0} ms", QTreeLeaf.findStepStopWatch.ElapsedMilliseconds);
Console.WriteLine("array copying() took {0} ms", QTreeLeaf.arrayCompyTimer.ElapsedMilliseconds);
Console.WriteLine("mem alocated: {0}kb", GC.GetTotalMemory(false) / 1024);
}
/// <summary>
/// calculates LOD based on dedicated amount of memory. Based on ability of video memory being able to hold
/// 1000000 points
/// </summary>
/// <param name="amountOfMemory"></param>
private void determineInitialSelectionLOD()
{
SelectionLOD = 1f;
Point3D tempPoint = new Point3D();
int pointSize = Marshal.SizeOf(tempPoint);
//calculate selection LOD based on the video memory being able to hold 1miomemory
SelectionLOD = ((float)dedicatedPointMemory / (float)VBOUtils.PointInformationSize) / 1000000;
if (SelectionLOD > 1.0f)
{
SelectionLOD = 1.0f;
}
}
public Point3D[] GetPoints(QTreeWrapper qtree, uint index, int numberOfPoints)
{
//Console.WriteLine("getPoints entry. mem alocated: {0}", GC.GetTotalMemory(false));
Point3D[] pts = null;
float minX = (float)((qtree.lasFile.header.MinX - qtree.lasFile.header.OffsetX));
float minY = (float)((qtree.lasFile.header.MinY - qtree.lasFile.header.OffsetY));
float minZ = (float)((qtree.lasFile.header.MinZ - qtree.lasFile.header.OffsetZ));
float scaleX = (float)qtree.lasFile.header.ScaleFactorX;
float scaleY = (float)qtree.lasFile.header.ScaleFactorY;
float scaleZ = (float)qtree.lasFile.header.ScaleFactorZ;
//Console.WriteLine("loading point at index={0}, number to load={1}", index, numberOfPoints);
if (qtree.lasFile.header.PointDataFormat == 0)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
LASPointFormat0[] pointArray = (LASPointFormat0[])qtree.lasFile.GetPoints(index, numberOfPoints);
stopwatch.Stop();
//Console.WriteLine("loading took: {0} ms", stopwatch.ElapsedMilliseconds);
long loading = stopwatch.ElapsedMilliseconds;
stopwatch.Reset();
stopwatch.Start();
if (pointArray != null)
{
int count = pointArray.Length;
index += (uint)count;
pts = new Point3D[numberOfPoints];
for (int i = 0; i < count; i++)
{
pts[i].x = pointArray[i].X * scaleX * LasDataManager.GlobalPointsScaleFactor.x - minX;
pts[i].y = (pointArray[i].Y) * scaleY * LasDataManager.GlobalPointsScaleFactor.y - minY;
pts[i].z = (pointArray[i].Z) * scaleZ * LasDataManager.GlobalPointsScaleFactor.z - minZ;
pts[i].colorIndex = pointArray[i].Classification;
if (pts[i].z < GlobalBoundingCube.minZ)
{
GlobalBoundingCube.minZ = pts[i].z;
}
if (pts[i].y > GlobalBoundingCube.maxZ)
{
GlobalBoundingCube.maxZ = pts[i].z;
}
}
}
pointArray = null;
stopwatch.Stop();
long transforming = stopwatch.ElapsedMilliseconds;
//Console.WriteLine("transforming points: {0} ms", stopwatch.ElapsedMilliseconds);
}
else
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
LASPointFormat1[] pointArray = (LASPointFormat1[])qtree.lasFile.GetPoints(index, numberOfPoints);
stopwatch.Stop();
//Console.WriteLine("loading poins: {0} ms", stopwatch.ElapsedMilliseconds);
long loading = stopwatch.ElapsedMilliseconds;
stopwatch.Reset();
stopwatch.Start();
if (pointArray != null)
{
int count = pointArray.Length;
index += (uint)count;
pts = new Point3D[numberOfPoints];
for (int i = 0; i < count; i++)
{
pts[i].x = pointArray[i].X * scaleX * LasDataManager.GlobalPointsScaleFactor.x - minX;
pts[i].y = (pointArray[i].Y) * scaleY * LasDataManager.GlobalPointsScaleFactor.y - minY;
pts[i].z = (pointArray[i].Z) * scaleZ * LasDataManager.GlobalPointsScaleFactor.z - minZ;
pts[i].colorIndex = pointArray[i].Classification;
if (pts[i].z < GlobalBoundingCube.minZ)
{
GlobalBoundingCube.minZ = pts[i].z;
}
if (pts[i].y > GlobalBoundingCube.maxZ)
{
GlobalBoundingCube.maxZ = pts[i].z;
}
}
}
pointArray = null;
stopwatch.Stop();
long transforming = stopwatch.ElapsedMilliseconds;
//Console.WriteLine("transforming poins: {0} ms", stopwatch.ElapsedMilliseconds);
}
//Console.WriteLine("getPoints exit. mem alocated: {0} ", GC.GetTotalMemory(false));
return pts;
}
public void RequestLeafLoad(QTreeLeaf leaf)
{
if (leaf.State == LoadedState.UNLOADED && leaf.ListInfos.Count > 0 && leafLoadRequestQueue.Count < 1000)
{
leaf.State = LoadedState.REQUEST_LOAD;
if (leaf.ListInfos.Count > 0)
{
leafLoadRequestQueue.Enqueue(leaf);
}
}
}
/// <summary>
/// leads leafs in a separate thread
/// </summary>
private void LeafLoader()
{
TimeSpan accLoadTime = new TimeSpan();
long loadCount = 0;
TimeSpan accReadTime = new TimeSpan();
TimeSpan readTemp = new TimeSpan();
Stopwatch stopwatch = new Stopwatch();
long accPointsPerLeaf = 0;
QTreeLeaf leaf = null;
while (runLeafLoader)
{
if ( LasMetrics.GetInstance().leafLoadToggle && leafLoadRequestQueue.Count > 0)
{
leaf = leafLoadRequestQueue.Dequeue();
if (leaf == null)
{
continue;
}
if (leaf.State == LoadedState.REQUEST_LOAD) //if leaf still wants to be loaded
{
stopwatch.Start();
long readTicks = 0;
for (int i = 0; i < leaf.ListInfos.Count; i++)
{
readTemp = stopwatch.Elapsed;
Point3D[] pts = GetPoints(GetQtreeByGuid(leaf.ParentTreeID), leaf.ListInfos[i].startingPointIndex, leaf.ListInfos[i].numberOfPoints);
readTicks = stopwatch.ElapsedTicks;
accReadTime += (stopwatch.Elapsed-readTemp);
leaf.InsertPoints(i, pts);
}
if (CALCULATE_NORMALS == true)
{
leaf.CalculateNormals();
}
long loadTicks = stopwatch.ElapsedTicks;
accLoadTime += stopwatch.Elapsed;
stopwatch.Stop();
loadCount++;
stopwatch.Reset();
leaf.State = LoadedState.BUFFERED_IN_RAM;
if (loadCount > 0)
{
double avgLoadTime = accLoadTime.TotalMilliseconds / loadCount;
double avgLoadTimeNoRead = (accLoadTime - accReadTime).TotalMilliseconds / loadCount;
//Console.WriteLine("Avg. time from disk to GPU: {0} ms, without disk reads: {1} ms", avgLoadTime, avgLoadTimeNoRead);
LasMetrics.GetInstance().avgLoad = avgLoadTime;
LasMetrics.GetInstance().avgLoadNoDisk = avgLoadTimeNoRead;
accPointsPerLeaf += leaf.NumberOfPoints;
LasMetrics.GetInstance().avgPointsPerLeaf = (double)accPointsPerLeaf / loadCount;
}
}
leaf = null;
}
else
{
Thread.Sleep(leafLoaderMSsleepInterval);
}
}
}
public void Close()
{
if (leafLoadingThread != null)
{
runLeafLoader = false;
leafLoadingThread.Join();
}
for (int i = 0; i < QTrees.Count; i++)
{
QTrees[i].lasFile.Close();
}
}
}
}