void ccVolumeCalcTool::updateGridAndDisplay()
{
	bool success = updateGrid();
	if (success && m_glWindow)
	{
		//convert grid to point cloud
		if (m_rasterCloud)
		{
			m_glWindow->removeFromOwnDB(m_rasterCloud);
			delete m_rasterCloud;
			m_rasterCloud = 0;
		}

		m_rasterCloud = convertGridToCloud(false);
		if (m_rasterCloud)
		{
			m_glWindow->addToOwnDB(m_rasterCloud);
			ccBBox box = m_rasterCloud->getDisplayBB_recursive(false, m_glWindow);
			update2DDisplayZoom(box);
		}
		else
		{
			ccLog::Error("Not enough memory!");
			m_glWindow->redraw();
		}
	}

	gridIsUpToDate(success);
}
ccPointCloud* ccRasterizeTool::generateCloud(bool autoExport/*=true*/) const
{
	if (!m_cloud || !m_grid.isValid())
		return 0;

	//look for fields to be exported
	std::vector<ExportableFields> exportedFields;
	try
	{
		exportedFields.push_back(PER_CELL_HEIGHT);
		if (exportAsSF(PER_CELL_COUNT))
			exportedFields.push_back(PER_CELL_COUNT);
		if (exportAsSF(PER_CELL_MIN_HEIGHT))
			exportedFields.push_back(PER_CELL_MIN_HEIGHT);
		if (exportAsSF(PER_CELL_MAX_HEIGHT))
			exportedFields.push_back(PER_CELL_MAX_HEIGHT);
		if (exportAsSF(PER_CELL_AVG_HEIGHT))
			exportedFields.push_back(PER_CELL_AVG_HEIGHT);
		if (exportAsSF(PER_CELL_HEIGHT_STD_DEV))
			exportedFields.push_back(PER_CELL_HEIGHT_STD_DEV);
		if (exportAsSF(PER_CELL_HEIGHT_RANGE))
			exportedFields.push_back(PER_CELL_HEIGHT_RANGE);
	}
	catch (const std::bad_alloc&)
	{
		ccLog::Error("Not enough memory!");
		return 0;
	}
	QString activeLayerName = activeLayerComboBox->currentText();
	bool activeLayerIsSF = activeLayerComboBox->currentIndex() != 0;
	ccPointCloud* rasterCloud = convertGridToCloud(	exportedFields,
													getTypeOfSFInterpolation() != INVALID_PROJECTION_TYPE || activeLayerIsSF,
													activeLayerName);

	if (rasterCloud && autoExport)
	{
		if (m_cloud->getParent())
			m_cloud->getParent()->addChild(rasterCloud);
		rasterCloud->setDisplay(m_cloud->getDisplay());
		
		if (m_cloud->isEnabled())
		{
			m_cloud->setEnabled(false);
			ccLog::Warning("[Rasterize] Previously selected entity (source cloud) has been hidden!");
		}

		MainWindow* mainWindow = MainWindow::TheInstance();
		if (mainWindow)
			MainWindow::TheInstance()->addToDB(rasterCloud);
		ccLog::Print(QString("[Rasterize] Cloud '%1' successfully exported").arg(rasterCloud->getName()));
	}

	return rasterCloud;
}
void ccRasterizeTool::updateGridAndDisplay()
{
	bool activeLayerIsSF = activeLayerComboBox->currentIndex() != 0;
	bool interpolateSF = activeLayerIsSF || (getTypeOfSFInterpolation() != INVALID_PROJECTION_TYPE);
	bool success = updateGrid(interpolateSF);

	if (success && m_window)
	{
		//convert grid to point cloud
		if (m_rasterCloud)
		{
			m_window->removeFromOwnDB(m_rasterCloud);
			delete m_rasterCloud;
			m_rasterCloud = 0;
		}

		std::vector<ExportableFields> exportedFields;
		try
		{
			//we always compute the default 'height' layer
			exportedFields.push_back(PER_CELL_HEIGHT);
			//but we may also have to compute the 'original SF(s)' layer(s)
			QString activeLayerName = activeLayerComboBox->currentText();
			m_rasterCloud = convertGridToCloud(exportedFields,activeLayerIsSF,activeLayerName);
		}
		catch (const std::bad_alloc&)
		{
			//see below
		}

		if (m_rasterCloud)
		{
			m_window->addToOwnDB(m_rasterCloud);
			ccBBox box = m_rasterCloud->getDisplayBB_recursive(false,m_window);
			update2DDisplayZoom(box);

			//update 
			activeLayerChanged(activeLayerComboBox->currentIndex(),false);
		}
		else
		{
			ccLog::Error("Not enough memory!");
			m_window->redraw();
		}
	}

	gridIsUpToDate(success);
}
void ccVolumeCalcTool::exportGridAsCloud() const
{
	if (!m_grid.isValid())
	{
		assert(false);
	}

	ccPointCloud* rasterCloud = convertGridToCloud(true);
	if (!rasterCloud)
	{
		//error message should have already been issued
		return;
	}
	
	rasterCloud->setName("Height difference " + rasterCloud->getName());
	ccGenericPointCloud* originCloud = (m_cloud1 ? m_cloud1 : m_cloud2);
	assert(originCloud);
	if (originCloud)
	{
		if (originCloud->getParent())
		{
			originCloud->getParent()->addChild(rasterCloud);
		}
		rasterCloud->setDisplay(originCloud->getDisplay());
	}

	MainWindow* mainWindow = MainWindow::TheInstance();
	if (mainWindow)
	{
		mainWindow->addToDB(rasterCloud);
		ccLog::Print(QString("[Volume] Cloud '%1' successfully exported").arg(rasterCloud->getName()));
	}
	else
	{
		assert(false);
		delete rasterCloud;
	}
}