File: ccCommandRaster.h

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (742 lines) | stat: -rw-r--r-- 21,297 bytes parent folder | download
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#ifndef COMMAND_LINE_RASTER_HEADER
#define COMMAND_LINE_RASTER_HEADER

#include "ccCommandLineInterface.h"

//local
#include "ccRasterizeTool.h"

//Qt
#include <QString>
#include <QMessageBox>

//qCC_db
#include <ccProgressDialog.h>
#include <ccVolumeCalcTool.h>

//shared commands
static const char COMMAND_GRID_VERT_DIR[]					= "VERT_DIR";
static const char COMMAND_GRID_STEP[]						= "GRID_STEP";
static const char COMMAND_GRID_OUTPUT_CLOUD[]				= "OUTPUT_CLOUD";
static const char COMMAND_GRID_OUTPUT_MESH[]				= "OUTPUT_MESH";
static const char COMMAND_GRID_OUTPUT_RASTER_Z[]			= "OUTPUT_RASTER_Z";
static const char COMMAND_GRID_OUTPUT_RASTER_RGB[]			= "OUTPUT_RASTER_RGB";

//Rasterize specific commands
static const char COMMAND_RASTERIZE[]						= "RASTERIZE";
static const char COMMAND_RASTER_CUSTOM_HEIGHT[]			= "CUSTOM_HEIGHT";
static const char COMMAND_RASTER_FILL_EMPTY_CELLS[]			= "EMPTY_FILL";
static const char COMMAND_RASTER_FILL_MIN_HEIGHT[]			= "MIN_H";
static const char COMMAND_RASTER_FILL_MAX_HEIGHT[]			= "MAX_H";
static const char COMMAND_RASTER_FILL_CUSTOM_HEIGHT[]		= "CUSTOM_H";
static const char COMMAND_RASTER_FILL_INTERPOLATE[]			= "INTERP";
static const char COMMAND_RASTER_PROJ_TYPE[]				= "PROJ";
static const char COMMAND_RASTER_SF_PROJ_TYPE[]				= "SF_PROJ";
static const char COMMAND_RASTER_PROJ_MIN[]					= "MIN";
static const char COMMAND_RASTER_PROJ_MAX[]					= "MAX";
static const char COMMAND_RASTER_PROJ_AVG[]					= "AVG";
static const char COMMAND_RASTER_RESAMPLE[]					= "RESAMPLE";

//2.5D Volume calculation specific commands
static const char COMMAND_VOLUME[] = "VOLUME";
static const char COMMAND_VOLUME_GROUND_IS_FIRST[]			= "GROUND_IS_FIRST";
static const char COMMAND_VOLUME_CONST_HEIGHT[]				= "CONST_HEIGHT";

ccRasterGrid::ProjectionType GetProjectionType(QString option, ccCommandLineInterface& cmd)
{
	if (option == COMMAND_RASTER_PROJ_MIN)
	{
		return ccRasterGrid::PROJ_MINIMUM_VALUE;
	}
	else if (option == COMMAND_RASTER_PROJ_MAX)
	{
		return ccRasterGrid::PROJ_MAXIMUM_VALUE;
	}
	else if (option == COMMAND_RASTER_PROJ_AVG)
	{
		return ccRasterGrid::PROJ_AVERAGE_VALUE;
	}
	else
	{
		assert(false);
		cmd.warning(QString("Unknwon projection type: %1 (defaulting to 'average')").arg(option));
		return ccRasterGrid::PROJ_AVERAGE_VALUE;
	}
}

ccRasterGrid::EmptyCellFillOption GetEmptyCellFillingStrategy(QString option, ccCommandLineInterface& cmd)
{
	if (option == COMMAND_RASTER_FILL_MIN_HEIGHT)
	{
		return ccRasterGrid::FILL_MINIMUM_HEIGHT;
	}
	else if (option == COMMAND_RASTER_FILL_MAX_HEIGHT)
	{
		return ccRasterGrid::FILL_MAXIMUM_HEIGHT;
	}
	else if (option == COMMAND_RASTER_FILL_CUSTOM_HEIGHT)
	{
		return ccRasterGrid::FILL_CUSTOM_HEIGHT;
	}
	else if (option == COMMAND_RASTER_FILL_INTERPOLATE)
	{
		return ccRasterGrid::INTERPOLATE;
	}
	else
	{
		assert(false);
		cmd.warning(QString("Unknwon empty cell filling strategy: %1 (defaulting to 'leave empty')").arg(option));
		return ccRasterGrid::LEAVE_EMPTY;
	}
}

struct CommandRasterize : public ccCommandLineInterface::Command
{
	CommandRasterize() : ccCommandLineInterface::Command("Rasterize", COMMAND_RASTERIZE) {}

	virtual bool process(ccCommandLineInterface& cmd) override
	{
		cmd.print("[RASTERIZE]");

		//look for local options
		double gridStep = 0;
		bool outputCloud = false;
		bool outputRasterZ = false;
		bool outputRasterRGB = false;
		bool outputMesh = false;
		bool resample = false;
		double customHeight = std::numeric_limits<double>::quiet_NaN();
		int vertDir = 2;
		ccRasterGrid::ProjectionType projectionType = ccRasterGrid::PROJ_AVERAGE_VALUE;
		ccRasterGrid::ProjectionType sfProjectionType = ccRasterGrid::PROJ_AVERAGE_VALUE;
		ccRasterGrid::EmptyCellFillOption emptyCellFillStrategy = ccRasterGrid::LEAVE_EMPTY;

		while (!cmd.arguments().empty())
		{
			QString argument = cmd.arguments().front();
			if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_OUTPUT_CLOUD))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				if (outputMesh)
				{
					cmd.warning("Can't output the grid as a mesh AND a cloud at the same time");
				}
				else
				{
					outputCloud = true;
				}
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_OUTPUT_MESH))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				if (outputCloud)
				{
					cmd.warning("Can't output the grid as a mesh AND a cloud at the same time");
				}
				else
				{
					outputMesh = true;
				}
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_OUTPUT_RASTER_Z))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				outputRasterZ = true;
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_OUTPUT_RASTER_RGB))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				outputRasterRGB = true;
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_STEP))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				bool ok;
				gridStep = cmd.arguments().takeFirst().toDouble(&ok);
				if (!ok || gridStep <= 0)
				{
					return cmd.error(QString("Invalid grid step value! (after %1)").arg(COMMAND_GRID_STEP));
				}
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_RASTER_CUSTOM_HEIGHT))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				bool ok;
				customHeight = cmd.arguments().takeFirst().toDouble(&ok);
				if (!ok)
				{
					return cmd.error(QString("Invalid custom height value! (after %1)").arg(COMMAND_RASTER_CUSTOM_HEIGHT));
				}
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_VERT_DIR))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				bool ok;
				vertDir = cmd.arguments().takeFirst().toInt(&ok);
				if (!ok || vertDir < 0 || vertDir > 2)
				{
					return cmd.error(QString("Invalid vert. direction! (after %1)").arg(COMMAND_GRID_VERT_DIR));
				}
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_RASTER_FILL_EMPTY_CELLS))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				emptyCellFillStrategy = GetEmptyCellFillingStrategy(cmd.arguments().takeFirst().toUpper(), cmd);
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_RASTER_PROJ_TYPE))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				projectionType = GetProjectionType(cmd.arguments().takeFirst().toUpper(), cmd);
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_RASTER_SF_PROJ_TYPE))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				sfProjectionType = GetProjectionType(cmd.arguments().takeFirst().toUpper(), cmd);
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_RASTER_RESAMPLE))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				resample = true;
			}
		}

		if (gridStep == 0)
		{
			return cmd.error(QString("Grid step value not defined (use %1)").arg(COMMAND_GRID_STEP));
		}

		if (emptyCellFillStrategy == ccRasterGrid::FILL_CUSTOM_HEIGHT && std::isnan(customHeight))
		{
			cmd.warning("[Rasterize] The filling stragety is set to 'fill with custom height' but no custom height was defined...");
			emptyCellFillStrategy = ccRasterGrid::LEAVE_EMPTY;
		}

		if (!outputCloud && !outputMesh && !outputRasterZ && !outputRasterRGB)
		{
			//if no export target is specified, we chose the cloud by default
			outputCloud = true;
		}
		assert(outputCloud || outputMesh);

		if (resample && !outputCloud && !outputMesh)
		{
			cmd.warning("[Rasterize] The 'resample' option is set while the raster won't be exported as a cloud nor as a mesh");
		}

		//we'll get the first two clouds
		for (CLCloudDesc& cloudDesc : cmd.clouds())
		{
			if (!cloudDesc.pc)
			{
				assert(false);
				continue;
			}
			ccBBox gridBBox = cloudDesc.pc->getOwnBB();

			//compute the grid size
			unsigned gridWidth = 0, gridHeight = 0;
			if (!ccRasterGrid::ComputeGridSize(vertDir, gridBBox, gridStep, gridWidth, gridHeight))
			{
				return cmd.error("Failed to compute the grid dimensions (check input cloud(s) bounding-box)");
			}

			cmd.print(QString("Grid size: %1 x %2").arg(gridWidth).arg(gridHeight));

			if (gridWidth * gridHeight > (1 << 26)) //64 million of cells
			{
				if (cmd.silentMode())
				{
					ccLog::Warning("Huge grid detected!");
				}
				else
				{
					static bool s_firstTime = true;
					if (s_firstTime && QMessageBox::warning(cmd.widgetParent(), "Raster grid", "Grid size is huge. Are you sure you want to proceed?\n(you can avoid this message by running in SILENT mode)", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
					{
						return ccLog::Warning("Process cancelled");
					}
					s_firstTime = false;
				}
			}

			ccRasterGrid grid;
			{
				//memory allocation
				CCVector3d minCorner = CCVector3d::fromArray(gridBBox.minCorner().u);
				if (!grid.init(gridWidth, gridHeight, gridStep, minCorner))
				{
					//not enough memory
					return cmd.error("Not enough memory");
				}

				//progress dialog
				QScopedPointer<ccProgressDialog> pDlg(0);
				if (!cmd.silentMode())
				{
					pDlg.reset(new ccProgressDialog(true, cmd.widgetParent()));
				}

				if (grid.fillWith(cloudDesc.pc,
					vertDir,
					projectionType,
					emptyCellFillStrategy == ccRasterGrid::INTERPOLATE,
					sfProjectionType,
					pDlg.data()))
				{
					grid.fillEmptyCells(emptyCellFillStrategy, customHeight);
					cmd.print(QString("[Rasterize] Raster grid: size: %1 x %2 / heights: [%3 ; %4]").arg(grid.width).arg(grid.height).arg(grid.minHeight).arg(grid.maxHeight));
				}
				else
				{
					return cmd.error("Rasterize process failed");
				}
			}

			//generate the result entity (cloud by default)
			if (outputCloud || outputMesh)
			{
				std::vector<ccRasterGrid::ExportableFields> exportedFields;
				try
				{
					//we always compute the default 'height' layer
					exportedFields.push_back(ccRasterGrid::PER_CELL_HEIGHT);
				}
				catch (const std::bad_alloc&)
				{
					return cmd.error("Not enough memory");
				}

				ccPointCloud* rasterCloud = grid.convertToCloud(
					exportedFields,
					true,
					true,
					resample,
					resample,
					cloudDesc.pc,
					vertDir,
					gridBBox,
					emptyCellFillStrategy == ccRasterGrid::FILL_CUSTOM_HEIGHT,
					customHeight,
					true
					);

				if (!rasterCloud)
				{
					return cmd.error("Failed to output the raster grid as a cloud");
				}

				rasterCloud->showColors(cloudDesc.pc->hasColors());
				if (rasterCloud->hasScalarFields())
				{
					rasterCloud->showSF(!cloudDesc.pc->hasColors());
					rasterCloud->setCurrentDisplayedScalarField(0);
				}

				if (outputCloud)
				{
					assert(!outputMesh);
					//replace current cloud by the restarized version
					delete cloudDesc.pc;
					cloudDesc.pc = rasterCloud;
					cloudDesc.basename += QString("_RASTER");

					rasterCloud = nullptr;

					if (cmd.autoSaveMode())
					{
						QString errorStr = cmd.exportEntity(cloudDesc);
						if (!errorStr.isEmpty())
						{
							return cmd.error(errorStr);
						}
					}
				}
				else if (outputMesh)
				{
					char errorStr[1024];
					CCLib::GenericIndexedMesh* baseMesh = CCLib::PointProjectionTools::computeTriangulation
						(
						rasterCloud,
						DELAUNAY_2D_AXIS_ALIGNED,
						0,
						vertDir,
						errorStr
						);

					if (baseMesh)
					{
						ccMesh* rasterMesh = new ccMesh(baseMesh, rasterCloud);
						delete baseMesh;
						baseMesh = 0;

						rasterCloud->setEnabled(false);
						rasterCloud->setVisible(true);
						rasterMesh->addChild(rasterCloud);
						rasterMesh->setName(rasterCloud->getName());
						//rasterCloud->setName("vertices");
						rasterMesh->showSF(rasterCloud->sfShown());
						rasterMesh->showColors(rasterCloud->colorsShown());
						rasterCloud = 0; //to avoid deleting it later

						cmd.print(QString("[Rasterize] Mesh '%1' successfully generated").arg(rasterMesh->getName()));

						CLMeshDesc meshDesc;
						meshDesc.mesh = rasterMesh;
						meshDesc.basename = cloudDesc.basename + QString("_RASTER_MESH");
						meshDesc.path = cloudDesc.path;

						QString errorStr = cmd.exportEntity(meshDesc);
						if (!errorStr.isEmpty())
						{
							delete rasterMesh;
							return cmd.error(errorStr);
						}

						//we keep the mesh loaded
						cmd.meshes().push_back(meshDesc);
						//delete rasterMesh;
						//rasterMesh = 0;
					}
					else
					{
						cmd.warning(QString("[Rasterize] Failed to create output mesh ('%1')").arg(errorStr));
					}
				}

				if (rasterCloud)
				{
					delete rasterCloud;
					rasterCloud = nullptr;
				}
			}

			if (outputRasterZ)
			{
				ccRasterizeTool::ExportBands bands;
				{
					bands.height = true;
					bands.rgb = false; //not a good idea to mix RGB and height values!
					bands.allSFs = true;
				}
				QString exportFilename = cmd.getExportFilename(cloudDesc, "tif", "RASTER_Z", 0, !cmd.addTimestamp());
				if (exportFilename.isEmpty())
				{
					exportFilename = "rasterZ.tif";
				}

				ccRasterizeTool::ExportGeoTiff(exportFilename, bands, emptyCellFillStrategy, grid, gridBBox, vertDir, customHeight, cloudDesc.pc);
			}

			if (outputRasterRGB)
			{
				ccRasterizeTool::ExportBands bands;
				{
					bands.rgb = true;
					bands.height = false; //not a good idea to mix RGB and height values!
					bands.allSFs = false;
				}
				QString exportFilename = cmd.getExportFilename(cloudDesc, "tif", "RASTER_RGB", 0, !cmd.addTimestamp());
				if (exportFilename.isEmpty())
				{
					exportFilename = "rasterRGB.tif";
				}

				ccRasterizeTool::ExportGeoTiff(exportFilename, bands, emptyCellFillStrategy, grid, gridBBox, vertDir, customHeight, cloudDesc.pc);
			}
		}

		return true;
	}
};

struct CommandVolume25D : public ccCommandLineInterface::Command
{
	CommandVolume25D() : ccCommandLineInterface::Command("2.5D Volume Calculation", COMMAND_VOLUME) {}

	virtual bool process(ccCommandLineInterface& cmd) override
	{
		cmd.print("[2.5D VOLUME]");

		//look for local options
		bool groundIsFirst = false;
		double gridStep = 0;
		double constHeight = std::numeric_limits<double>::quiet_NaN();
		bool outputMesh = false;
		int vertDir = 2;

		while (!cmd.arguments().empty())
		{
			QString argument = cmd.arguments().front();
			if (ccCommandLineInterface::IsCommand(argument, COMMAND_VOLUME_GROUND_IS_FIRST))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				groundIsFirst = true;
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_OUTPUT_MESH))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				outputMesh = true;
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_STEP))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				bool ok;
				gridStep = cmd.arguments().takeFirst().toDouble(&ok);
				if (!ok || gridStep <= 0)
				{
					return cmd.error(QString("Invalid grid step value! (after %1)").arg(COMMAND_GRID_STEP));
				}
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_VOLUME_CONST_HEIGHT))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				bool ok;
				constHeight = cmd.arguments().takeFirst().toDouble(&ok);
				if (!ok)
				{
					return cmd.error(QString("Invalid const. height value! (after %1)").arg(COMMAND_VOLUME_CONST_HEIGHT));
				}
			}
			else if (ccCommandLineInterface::IsCommand(argument, COMMAND_GRID_VERT_DIR))
			{
				//local option confirmed, we can move on
				cmd.arguments().pop_front();

				bool ok;
				vertDir = cmd.arguments().takeFirst().toInt(&ok);
				if (!ok || vertDir < 0 || vertDir > 2)
				{
					return cmd.error(QString("Invalid vert. direction! (after %1)").arg(COMMAND_GRID_VERT_DIR));
				}
			}
			else
			{
				//unrecognized argument (probably another command?)
				break;
			}
		}

		if (gridStep == 0)
		{
			return cmd.error(QString("Grid step value not defined (use %1)").arg(COMMAND_GRID_STEP));
		}

		//we'll get the first two clouds
		CLCloudDesc *ground = 0, *ceil = 0;
		{
			CLCloudDesc* clouds[2] = { 0, 0 };
			int index = 0;
			if (!cmd.clouds().empty())
			{
				clouds[index++] = &cmd.clouds()[0];
				if (std::isnan(constHeight) && cmd.clouds().size() > 1)
				{
					clouds[index++] = &cmd.clouds()[1];
				}
			}

			int expectedCount = std::isnan(constHeight) ? 2 : 1;
			if (index != expectedCount)
			{
				return cmd.error(QString("Not enough loaded entities (%1 found, %2 expected)").arg(index).arg(expectedCount));
			}

			if (index == 2 && groundIsFirst)
			{
				//put them in the right order (ground then ceil)
				std::swap(clouds[0], clouds[1]);
			}

			ceil = clouds[0];
			ground = clouds[1];
		}

		ccBBox gridBBox = ceil ? ceil->pc->getOwnBB() : ccBBox();
		if (ground)
		{
			gridBBox += ground->pc->getOwnBB();
		}

		//compute the grid size
		unsigned gridWidth = 0, gridHeight = 0;
		if (!ccRasterGrid::ComputeGridSize(vertDir, gridBBox, gridStep, gridWidth, gridHeight))
		{
			return cmd.error("Failed to compute the grid dimensions (check input cloud(s) bounding-box)");
		}

		cmd.print(QString("Grid size: %1 x %2").arg(gridWidth).arg(gridHeight));

		if (gridWidth * gridHeight > (1 << 26)) //64 million of cells
		{
			if (cmd.silentMode())
			{
				ccLog::Warning("Huge grid detected!");
			}
			else
			{
				static bool s_firstTime = true;
				if (s_firstTime && QMessageBox::warning(cmd.widgetParent(), "Volume grid", "Grid size is huge. Are you sure you want to proceed?\n(you can avoid this message by running in SILENT mode)", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
				{
					return ccLog::Warning("Process cancelled");
				}
				s_firstTime = false;
			}
		}

		ccRasterGrid grid;
		ccVolumeCalcTool::ReportInfo reportInfo;
		if (ccVolumeCalcTool::ComputeVolume(
			grid,
			ground ? ground->pc : 0,
			ceil ? ceil->pc : 0,
			gridBBox,
			vertDir,
			gridStep,
			gridWidth,
			gridHeight,
			ccRasterGrid::PROJ_AVERAGE_VALUE,
			ccRasterGrid::LEAVE_EMPTY,
			ccRasterGrid::LEAVE_EMPTY,
			reportInfo,
			constHeight,
			constHeight,
			cmd.silentMode() ? 0 : cmd.widgetParent()))
		{
			CLCloudDesc* desc = ceil ? ceil : ground;
			assert(desc);

			//save repot in a separate text file
			{
				QString txtFilename = QString("%1/VolumeCalculationReport").arg(desc->path);
				if (cmd.addTimestamp())
					txtFilename += QString("_%1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd_hh'h'mm"));
				txtFilename += QString(".txt");

				QFile txtFile(txtFilename);
				txtFile.open(QIODevice::WriteOnly | QIODevice::Text);
				QTextStream txtStream(&txtFile);
				txtStream << reportInfo.toText() << endl;
				txtFile.close();
			}

			//generate the result entity (cloud by default)
			{
				ccPointCloud* rasterCloud = ccVolumeCalcTool::ConvertGridToCloud(grid, gridBBox, vertDir, true);
				if (!rasterCloud)
				{
					return cmd.error("Failed to output the volume grid");
				}
				if (rasterCloud->hasScalarFields())
				{
					//convert SF to RGB
					//rasterCloud->setCurrentDisplayedScalarField(0);
					rasterCloud->setRGBColorWithCurrentScalarField(false);
					rasterCloud->showColors(true);
				}

				ccMesh* rasterMesh = 0;
				if (outputMesh)
				{
					char errorStr[1024];
					CCLib::GenericIndexedMesh* baseMesh = CCLib::PointProjectionTools::computeTriangulation(rasterCloud,
						DELAUNAY_2D_AXIS_ALIGNED,
						0,
						vertDir,
						errorStr);

					if (baseMesh)
					{
						rasterMesh = new ccMesh(baseMesh, rasterCloud);
						delete baseMesh;
						baseMesh = 0;
					}

					if (rasterMesh)
					{
						rasterCloud->setEnabled(false);
						rasterCloud->setVisible(true);
						rasterMesh->addChild(rasterCloud);
						rasterMesh->setName(rasterCloud->getName());
						rasterCloud->setName("vertices");
						rasterMesh->showSF(rasterCloud->sfShown());
						rasterMesh->showColors(rasterCloud->colorsShown());

						cmd.print(QString("[Volume] Mesh '%1' successfully generated").arg(rasterMesh->getName()));
					}
					else
					{
						delete rasterCloud;
						return cmd.error(QString("[Voume] Failed to create output mesh ('%1')").arg(errorStr));
					}
				}

				CLEntityDesc* outputDesc = 0;
				if (rasterMesh)
				{
					CLMeshDesc meshDesc;
					meshDesc.mesh = rasterMesh;
					meshDesc.basename = desc->basename;
					meshDesc.path = desc->path;
					cmd.meshes().push_back(meshDesc);
					outputDesc = &cmd.meshes().back();
				}
				else
				{
					CLCloudDesc cloudDesc;
					cloudDesc.pc = rasterCloud;
					cloudDesc.basename = desc->basename;
					cloudDesc.path = desc->path;
					cmd.clouds().push_back(cloudDesc);
					outputDesc = &cmd.clouds().back();
				}

				//save result
				if (outputDesc && cmd.autoSaveMode())
				{
					QString outputFilename;
					QString errorStr = cmd.exportEntity(*outputDesc, "HEIGHT_DIFFERENCE", &outputFilename);
					if (!errorStr.isEmpty())
						cmd.warning(errorStr);
				}
			}
		}
		else
		{
			return cmd.error("Failed to compte the volume");
		}

		return true;
	}
};

#endif //COMMAND_LINE_RASTER_HEADER