From 2706d780799ec3e44ebb459a1e44ad0f4e4e6ffc Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Mon, 13 Aug 2012 17:52:27 +0200
Subject: [PATCH] Fixed VtkColorByHeightFilter.

---
 VtkVis/VtkColorByHeightFilter.cpp          | 67 +++-------------------
 VtkVis/VtkColorByHeightFilter.h            |  8 +--
 VtkVis/VtkColorLookupTable.cpp             |  6 +-
 VtkVis/VtkCompositeColorByHeightFilter.cpp | 19 +++---
 4 files changed, 23 insertions(+), 77 deletions(-)

diff --git a/VtkVis/VtkColorByHeightFilter.cpp b/VtkVis/VtkColorByHeightFilter.cpp
index 95519d88cb3..8f48c6d0c37 100644
--- a/VtkVis/VtkColorByHeightFilter.cpp
+++ b/VtkVis/VtkColorByHeightFilter.cpp
@@ -17,22 +17,18 @@
 #include <vtkPolyData.h>
 #include <vtkSmartPointer.h>
 #include <vtkStreamingDemandDrivenPipeline.h>
+#include <vtkFloatArray.h>
 
 vtkStandardNewMacro(VtkColorByHeightFilter);
 vtkCxxRevisionMacro(VtkColorByHeightFilter, "$Revision$");
 
 VtkColorByHeightFilter::VtkColorByHeightFilter()
+:  ColorLookupTable(VtkColorLookupTable::New()), _tableRangeScaling(1.0)
 {
-	ColorLookupTable = VtkColorLookupTable::New();
-	ColorLookupTable->GetTableRange(this->_tableRange);
-	ColorLookupTable->setInterpolationType(VtkColorLookupTable::LINEAR);
-	_tableRangeScaling = 1.0;
-	_activeAttributeName = "P-Colors";
 }
 
 VtkColorByHeightFilter::~VtkColorByHeightFilter()
 {
-	ColorLookupTable->Delete();
 }
 
 void VtkColorByHeightFilter::PrintSelf( ostream& os, vtkIndent indent )
@@ -66,23 +62,17 @@ int VtkColorByHeightFilter::RequestData( vtkInformation*,
 	vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
 	vtkPolyData* input = vtkPolyData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
 
-	ColorLookupTable->SetTableRange(getMinHeight(input), getMaxHeight(input));
-	ColorLookupTable->Build();
-
-	vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New();
-	colors->SetNumberOfComponents(3);
+	vtkSmartPointer<vtkFloatArray> colors = vtkSmartPointer<vtkFloatArray>::New();
+	colors->SetNumberOfComponents(1);
 	colors->SetName("Colors");
 
-	// Generate the colors for each point based on the color map
+	// Inserts height values as a new scalar array
 	size_t nPoints = input->GetNumberOfPoints();
 	for (size_t i = 0; i < nPoints; i++)
 	{
 		double p[3];
 		input->GetPoint(i,p);
-
-		unsigned char lutColor[4];
-		ColorLookupTable->getColor((int)p[2], lutColor);
-		colors->InsertNextTupleValue(lutColor);
+		colors->InsertNextValue(p[2]);
 	}
 
 	vtkInformation* outInfo = outputVector->GetInformationObject(0);
@@ -96,50 +86,6 @@ int VtkColorByHeightFilter::RequestData( vtkInformation*,
 	return 1;
 }
 
-double VtkColorByHeightFilter::getMinHeight(vtkPolyData* data)
-{
-	double range[2];
-	ColorLookupTable->GetTableRange(range);
-	double min = range[0];
-	size_t nPoints = data->GetNumberOfPoints();
-	if (min == ColorLookupTable->DEFAULTMINVALUE && nPoints > 0)
-	{
-		double p[3];
-		data->GetPoint(0,p);
-		min = p[2];
-
-		for (size_t i = 1; i < nPoints; i++)
-		{
-			data->GetPoint(i,p);
-			if (p[2] < min)
-				min = p[2];
-		}
-	}
-	return min;
-}
-
-double VtkColorByHeightFilter::getMaxHeight(vtkPolyData* data)
-{
-	double range[2];
-	ColorLookupTable->GetTableRange(range);
-	double max = range[1];
-	size_t nPoints = data->GetNumberOfPoints();
-	if (max == ColorLookupTable->DEFAULTMAXVALUE && nPoints > 0)
-	{
-		double p[3];
-		data->GetPoint(0,p);
-		max = p[2];
-
-		for (size_t i = 1; i < nPoints; i++)
-		{
-			data->GetPoint(i,p);
-			if (p[2] > max)
-				max = p[2];
-		}
-	}
-	return max;
-}
-
 void VtkColorByHeightFilter::SetTableRange(double min, double max)
 {
 	if (min < max)
@@ -160,3 +106,4 @@ void VtkColorByHeightFilter::SetTableRangeScaling( double scale )
 	this->ColorLookupTable->SetTableRange(
 	        this->_tableRange[0] * scale, this->_tableRange[1] * scale);
 }
+
diff --git a/VtkVis/VtkColorByHeightFilter.h b/VtkVis/VtkColorByHeightFilter.h
index 64458b9a35b..f896939a16c 100644
--- a/VtkVis/VtkColorByHeightFilter.h
+++ b/VtkVis/VtkColorByHeightFilter.h
@@ -22,6 +22,8 @@ class VtkColorLookupTable;
  * table is calculated and so on.
  * If no range boundaries are explicitly set, the minimum and maximum height value will be calculated from
  * the data and set as minimum and maximum value for the lookup table.
+ * ColorLookupTable must be deleted manually.
+ * \see VtkCompositeColorByHeightFilter::init() for example usage.
  */
 class VtkColorByHeightFilter : public vtkPolyDataAlgorithm, public VtkAlgorithmProperties
 {
@@ -66,12 +68,6 @@ protected:
 	/// @brief Calculates the color lookup table based on set parameters.
 	VtkColorLookupTable* BuildColorTable();
 
-	/// @brief Returns the value of the smallest z-coordinate in the data set.
-	double getMinHeight(vtkPolyData* data);
-
-	/// @brief Returns the value of the largest z-coordinate in the data set.
-	double getMaxHeight(vtkPolyData* data);
-
 	VtkColorLookupTable* ColorLookupTable;
 
 	double _tableRange[2];
diff --git a/VtkVis/VtkColorLookupTable.cpp b/VtkVis/VtkColorLookupTable.cpp
index 6ec612ca6c9..9bb936b0287 100644
--- a/VtkVis/VtkColorLookupTable.cpp
+++ b/VtkVis/VtkColorLookupTable.cpp
@@ -68,13 +68,13 @@ void VtkColorLookupTable::Build()
 					double pos = (i - lastValue.first) / (static_cast<double>(nextIndex - lastValue.first));
 
 					if (_type == VtkColorLookupTable::LINEAR)
-						for (size_t j = 0; j < 3; j++)
+						for (size_t j = 0; j < 4; j++)
 							int_rgba[j] = linInterpolation( (lastValue.second)[j], (it->second)[j], pos);
 					else if (_type == VtkColorLookupTable::EXPONENTIAL)
-						for (size_t j = 0; j < 3; j++)
+						for (size_t j = 0; j < 4; j++)
 							int_rgba[j] = expInterpolation((lastValue.second)[j], (it->second)[j], 0.2, pos);
 					else // no interpolation
-						for (size_t j = 0; j < 3; j++)
+						for (size_t j = 0; j < 4; j++)
 							int_rgba[j] = (lastValue.second)[j];
 
 					this->SetTableValue(i, int_rgba);
diff --git a/VtkVis/VtkCompositeColorByHeightFilter.cpp b/VtkVis/VtkCompositeColorByHeightFilter.cpp
index bde566997cc..d6d35ffce9c 100644
--- a/VtkVis/VtkCompositeColorByHeightFilter.cpp
+++ b/VtkVis/VtkCompositeColorByHeightFilter.cpp
@@ -37,18 +37,21 @@ void VtkCompositeColorByHeightFilter::init()
 	else
 		heightFilter->SetInputConnection(_inputAlgorithm->GetOutputPort());
 
-	heightFilter->SetTableRange(-35, 800); // default min- and max-height (default: blue to red)
-	//heightFilter->GetColorLookupTable()->setInterpolationType(ColorLookupTable::EXPONENTIAL);
 	unsigned char a[4] = { 0, 0, 255, 255 }; // blue
 	unsigned char b[4] = { 0, 255, 0, 255 }; // green
 	unsigned char c[4] = { 255, 255, 0, 255 }; // yellow
 	unsigned char d[4] = { 255, 0, 0, 255 }; // red
-	//unsigned char e[4] = { 255, 255, 255, 255 }; // white
-	heightFilter->GetColorLookupTable()->setColor(-35, a);
-	heightFilter->GetColorLookupTable()->setColor(150, b); // green at about 150m
-	heightFilter->GetColorLookupTable()->setColor(450, c); // yellow at about 450m and changing to red from then on
-	heightFilter->GetColorLookupTable()->setColor(800, d);
-	//heightFilter->GetColorLookupTable()->setColor(1.0, e);
+	VtkColorLookupTable* ColorLookupTable = heightFilter->GetColorLookupTable();
+	ColorLookupTable->setInterpolationType(VtkColorLookupTable::LINEAR);
+	ColorLookupTable->setColor(-35, a);
+	ColorLookupTable->setColor(150, b); // green at about 150m
+	ColorLookupTable->setColor(450, c); // yellow at about 450m and changing to red from then on
+	ColorLookupTable->setColor(800, d);
+	ColorLookupTable->SetTableRange(-35, 800);
+	ColorLookupTable->Build();
+
+	// This passes ownership of the ColorLookupTable to VtkVisPointSetItem
+	heightFilter->SetLookUpTable("P-Colors", ColorLookupTable);
 	heightFilter->Update();
 
 	_outputAlgorithm = heightFilter;
-- 
GitLab