Skip to content

Commit d682035

Browse files
committed
fix auto-offset always enabled bug, select folder from input/output field if available (when click browse ...), set automatic file extension on savedialog filter, PCROOT: testing bytepacking (not enabled yet)
1 parent 935ef5a commit d682035

File tree

3 files changed

+158
-19
lines changed

3 files changed

+158
-19
lines changed

MainWindow.xaml.cs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,13 @@ void StartProcess(bool doProcess = true)
264264
}
265265
args.Add("-output=" + txtOutput.Text);
266266

267-
if ((bool)chkAutoOffset.IsChecked) args.Add("-offset=" + (bool)chkAutoOffset.IsChecked);
267+
args.Add("-offset=" + (bool)chkAutoOffset.IsChecked);
268268

269269
if (cmbExportFormat.SelectedItem.ToString().ToUpper().Contains("PCROOT")) args.Add("-gridsize=" + txtGridSize.Text);
270270

271271
if ((bool)chkUseMinPointCount.IsChecked) args.Add("-minpoints=" + txtMinPointCount.Text);
272272
if ((bool)chkUseScale.IsChecked) args.Add("-scale=" + txtScale.Text);
273-
if ((bool)chkSwapYZ.IsChecked) args.Add("-swap=" + (bool)chkSwapYZ.IsChecked);
273+
args.Add("-swap=" + (bool)chkSwapYZ.IsChecked);
274274
if ((bool)chkPackColors.IsChecked) args.Add("-pack=" + (bool)chkPackColors.IsChecked);
275275
if ((bool)chkUsePackMagic.IsChecked) args.Add("-packmagic=" + txtPackMagic.Text);
276276
if ((bool)chkUseMaxImportPointCount.IsChecked) args.Add("-limit=" + txtMaxImportPointCount.Text);
@@ -326,7 +326,39 @@ private void btnBrowseInput_Click(object sender, RoutedEventArgs e)
326326
var dialog = new OpenFileDialog();
327327
dialog.Title = "Select file to import";
328328
dialog.Filter = "LAS|*.las;*.laz";
329-
dialog.InitialDirectory = Properties.Settings.Default.lastImportFolder;
329+
330+
// take folder from field
331+
if (string.IsNullOrEmpty(txtInputFile.Text) == false)
332+
{
333+
// check if folder exists, if not take parent folder
334+
if (Directory.Exists(Path.GetDirectoryName(txtInputFile.Text)) == true)
335+
{
336+
dialog.InitialDirectory = Path.GetDirectoryName(txtInputFile.Text);
337+
}
338+
else // take parent
339+
{
340+
var folder = Path.GetDirectoryName(txtInputFile.Text);
341+
// fix slashes
342+
folder = folder.Replace("\\", "/");
343+
for (int i = folder.Length - 1; i > -1; i--)
344+
{
345+
if (folder[i] == '/')
346+
{
347+
if (Directory.Exists(folder.Substring(0, i)))
348+
{
349+
dialog.InitialDirectory = folder.Substring(0, i).Replace("/", "\\");
350+
break;
351+
}
352+
}
353+
}
354+
}
355+
}
356+
else // no path given
357+
{
358+
dialog.InitialDirectory = Properties.Settings.Default.lastImportFolder;
359+
}
360+
361+
330362
if (dialog.ShowDialog() == true)
331363
{
332364
txtInputFile.Text = dialog.FileName;
@@ -343,15 +375,17 @@ private void btnBrowseOutput_Click(object sender, RoutedEventArgs e)
343375
dialog.Title = "Set output folder and filename";
344376
dialog.Filter = "UCPC (V2)|*.ucpc|PCROOT (V3)|*.pcroot";
345377

378+
dialog.FilterIndex = cmbExportFormat.SelectedIndex + 1;
379+
346380
// take folder from field
347381
if (string.IsNullOrEmpty(txtOutput.Text) == false)
348382
{
349383
// check if folder exists, if not take parent folder
350384
if (Directory.Exists(Path.GetDirectoryName(txtOutput.Text)) == true)
351385
{
352-
dialog.InitialDirectory = txtOutput.Text;
386+
dialog.InitialDirectory = Path.GetDirectoryName(txtOutput.Text);
353387
}
354-
else
388+
else // take parent
355389
{
356390
var folder = Path.GetDirectoryName(txtOutput.Text);
357391
// fix slashes
@@ -369,13 +403,11 @@ private void btnBrowseOutput_Click(object sender, RoutedEventArgs e)
369403
}
370404
}
371405
}
372-
else // no path
406+
else // no path given
373407
{
374408
dialog.InitialDirectory = Properties.Settings.Default.lastExportFolder;
375409
}
376410

377-
Console.WriteLine(dialog.InitialDirectory);
378-
379411
if (dialog.ShowDialog() == true)
380412
{
381413
txtOutput.Text = dialog.FileName;

Structs/ImportSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ImportSettings
3939
public int limit = 0;
4040
public bool randomize = false;
4141
public float gridSize = 25;
42-
public int minimumPointCount = 500;
42+
public int minimumPointCount = 0;
4343
public bool packColors = false;
4444
public int packMagicValue = 64; // use lower value if your gridsize is very large, if gridsize=500 then try value 2
4545
public bool skipPoints = false;

Writers/PCROOT.cs

Lines changed: 117 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ void IWriter.AddPoint(int index, float x, float y, float z, float r, float g, fl
139139

140140
void IWriter.Save(int fileIndex)
141141
{
142-
int skippedCounter = 0;
142+
// TEST
143+
bool useLossyFiltering = false;
144+
int skippedNodesCounter = 0;
145+
int skippedPointsCounter = 0;
143146

144147
string fileOnly = Path.GetFileNameWithoutExtension(importSettings.outputFile);
145148
string baseFolder = Path.GetDirectoryName(importSettings.outputFile);
@@ -159,7 +162,7 @@ void IWriter.Save(int fileIndex)
159162
{
160163
if (nodeData.Value.Count < importSettings.minimumPointCount)
161164
{
162-
skippedCounter++;
165+
skippedNodesCounter++;
163166
continue;
164167
}
165168

@@ -208,6 +211,13 @@ void IWriter.Save(int fileIndex)
208211
// FIXME this is wrong value, if file is appended.. but for now append is disabled
209212
int totalPointsWritten = 0;
210213

214+
// TESTING
215+
int cells = 32;
216+
float center = (1f / (float)cells) / 2f;
217+
bool[] usedGrid = null;
218+
219+
if (useLossyFiltering == true) usedGrid = new bool[cells * cells * cells];
220+
211221
// output all points within that node tile
212222
for (int i = 0, len = nodeTempX.Count; i < len; i++)
213223
{
@@ -217,6 +227,7 @@ void IWriter.Save(int fileIndex)
217227
// keep points
218228
if (importSettings.keepPoints == true && (i % importSettings.keepEveryN != 0)) continue;
219229

230+
// original world positions
220231
float px = nodeTempX[i];
221232
float py = nodeTempY[i];
222233
float pz = nodeTempZ[i];
@@ -250,10 +261,71 @@ void IWriter.Save(int fileIndex)
250261
// pack blue and z
251262
pz = Tools.SuperPacker(nodeTempB[i] * 0.98f, pz, importSettings.gridSize * importSettings.packMagicValue);
252263
}
264+
else if (useLossyFiltering == true)
265+
{
266+
// get local coords within tile
267+
var keys = nodeData.Key.Split('_');
268+
// TODO no need to parse, we should know these values?
269+
cellX = int.Parse(keys[0]);
270+
cellY = int.Parse(keys[1]);
271+
cellZ = int.Parse(keys[2]);
272+
px -= (cellX * importSettings.gridSize);
273+
py -= (cellY * importSettings.gridSize);
274+
pz -= (cellZ * importSettings.gridSize);
275+
276+
byte packx = (byte)(px * cells);
277+
byte packy = (byte)(py * cells);
278+
byte packz = (byte)(pz * cells);
279+
var index = packx + cells * (packy + cells * packz);
280+
281+
// TODO could decide which point is more important or stronger color?
282+
if (usedGrid[index] == true)
283+
{
284+
skippedPointsCounter++;
285+
continue;
286+
}
253287

254-
writerPoints.Write(px);
255-
writerPoints.Write(py);
256-
writerPoints.Write(pz);
288+
usedGrid[index] = true;
289+
290+
//if (i < 3) Console.WriteLine("px: " + px + " py: " + py + " pz: " + pz + " index: " + index + " packx: " + packx + " packy: " + packy + " packz: " + packz);
291+
292+
}
293+
294+
if (useLossyFiltering == true)
295+
{
296+
byte bx = (byte)(px * cells);
297+
byte by = (byte)(py * cells);
298+
byte bz = (byte)(pz * cells);
299+
300+
float h = 0f;
301+
float s = 0f;
302+
float v = 0f;
303+
RGBtoHSV(nodeTempR[i], nodeTempG[i], nodeTempB[i], out h, out s, out v);
304+
305+
if (i < 3) Console.WriteLine("h: " + h + " s: " + s + " v: " + v);
306+
307+
// fix values
308+
h = h / 360f;
309+
310+
byte bh = (byte)(h * 255f);
311+
byte bs = (byte)(s * 255f);
312+
byte bv = (byte)(v * 255f);
313+
byte huepacked = (byte)(bh >> 2);
314+
// cut off 3 bits, then move in the middle bits
315+
byte satpacked = (byte)(bs >> 3);
316+
// cut off 3 bits
317+
byte valpacked = (byte)(bv >> 3);
318+
uint hsv655 = (uint)((huepacked << 10) + (satpacked << 5) + valpacked);
319+
320+
uint combinedXYZHSV = (uint)(((bz | by << 5 | bx << 10)) << 16) + hsv655;
321+
writerPoints.Write((uint)combinedXYZHSV);
322+
}
323+
else
324+
{
325+
writerPoints.Write(px);
326+
writerPoints.Write(py);
327+
writerPoints.Write(pz);
328+
}
257329

258330
totalPointsWritten++;
259331
} // loop all points in tile (node)
@@ -262,7 +334,7 @@ void IWriter.Save(int fileIndex)
262334
writerPoints.Close();
263335
bsPoints.Dispose();
264336

265-
if (importSettings.packColors == false)
337+
if (importSettings.packColors == false || useLossyFiltering == false)
266338
{
267339
// save separate RGB
268340
BufferedStream bsColors;
@@ -312,7 +384,7 @@ void IWriter.Save(int fileIndex)
312384
cb.cellZ = cellZ;
313385

314386
nodeBounds.Add(cb);
315-
} // loop all nodes
387+
} // loop all nodes foreach
316388

317389
// save rootfile
318390
// only save after last file, TODO should save this if process fails or user cancels, so no need to start from 0 again.. but then needs some merge or continue from index n feature
@@ -334,6 +406,7 @@ void IWriter.Save(int fileIndex)
334406

335407
int versionID = importSettings.packColors ? 2 : 1; // (1 = original, 2 = packed v3 format)
336408
if (importSettings.packColors == true) versionID = 2;
409+
if (useLossyFiltering == true) versionID = 3;
337410

338411
// add global header settings to first row
339412
// version, gridsize, pointcount, boundsMinX, boundsMinY, boundsMinZ, boundsMaxX, boundsMaxY, boundsMaxZ
@@ -348,7 +421,8 @@ void IWriter.Save(int fileIndex)
348421
Console.ForegroundColor = ConsoleColor.Green;
349422
Console.WriteLine("Done saving v3 : " + outputFileRoot);
350423
Console.ForegroundColor = ConsoleColor.White;
351-
if (skippedCounter > 0) Console.WriteLine("*Skipped " + skippedCounter + " nodes with less than " + importSettings.minimumPointCount + " points)");
424+
if (skippedNodesCounter > 0) Console.WriteLine("*Skipped " + skippedNodesCounter + " nodes with less than " + importSettings.minimumPointCount + " points)");
425+
if (useLossyFiltering == true && skippedPointsCounter > 0) Console.WriteLine("*Skipped " + skippedPointsCounter + " points due to bytepacked grid filtering");
352426

353427
if ((tilerootdata.Count - 1) <= 0)
354428
{
@@ -357,8 +431,41 @@ void IWriter.Save(int fileIndex)
357431
Console.ForegroundColor = ConsoleColor.White;
358432
}
359433
}
434+
} // Save()
360435

436+
void RGBtoHSV(float r, float g, float b, out float h, out float s, out float v)
437+
{
438+
float min, max, delta;
439+
440+
min = Math.Min(Math.Min(r, g), b);
441+
max = Math.Max(Math.Max(r, g), b);
442+
v = max; // v
443+
444+
delta = max - min;
445+
446+
if (max != 0)
447+
s = delta / max; // s
448+
else
449+
{
450+
// r = g = b = 0 // s = 0, v is undefined
451+
s = 0;
452+
h = -1;
453+
return;
454+
}
455+
456+
if (r == max)
457+
h = (g - b) / delta; // between yellow & magenta
458+
else if (g == max)
459+
h = 2 + (b - r) / delta; // between cyan & yellow
460+
else
461+
h = 4 + (r - g) / delta; // between magenta & cyan
462+
463+
h *= 60; // degrees
464+
465+
if (h < 0) h += 360;
361466
}
362-
}
363467

364-
}
468+
469+
470+
} // class
471+
} // namespace

0 commit comments

Comments
 (0)