From 4fc8b62d1a4f7e7c4440096e947f78df8cf3729a Mon Sep 17 00:00:00 2001
From: Owen Arnold <owen.arnold@stfc.ac.uk>
Date: Wed, 2 Sep 2015 10:14:38 +0100
Subject: [PATCH] refs #13523. Eliminate all member variables.

Using and storing pointers in the way this was done before is fundamentally unecessary and unsafe.
---
 .../ScaleWorkspace/vtkScaleWorkspace.cxx      |  7 +-
 .../vtkDataSetToScaledDataSet.h               | 15 ++--
 .../src/vtkDataSetToScaledDataSet.cpp         | 82 +++++++++++++------
 .../test/vtkDataSetToScaledDataSetTest.h      | 23 ++----
 4 files changed, 76 insertions(+), 51 deletions(-)

diff --git a/Code/Mantid/Vates/ParaviewPlugins/ParaViewFilters/ScaleWorkspace/vtkScaleWorkspace.cxx b/Code/Mantid/Vates/ParaviewPlugins/ParaViewFilters/ScaleWorkspace/vtkScaleWorkspace.cxx
index 8568ccf5a9e..101257eeb6f 100644
--- a/Code/Mantid/Vates/ParaviewPlugins/ParaViewFilters/ScaleWorkspace/vtkScaleWorkspace.cxx
+++ b/Code/Mantid/Vates/ParaviewPlugins/ParaViewFilters/ScaleWorkspace/vtkScaleWorkspace.cxx
@@ -56,11 +56,8 @@ int vtkScaleWorkspace::RequestData(vtkInformation*, vtkInformationVector **input
   }
 
   vtkInformation *outInfo = outputVector->GetInformationObject(0);
-  vtkUnstructuredGrid *outputDataSet = vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
-
-  vtkDataSetToScaledDataSet scaler(inputDataSet, outputDataSet);
-  scaler.initialize(m_xScaling, m_yScaling, m_zScaling);
-  scaler.execute();
+  vtkDataSetToScaledDataSet scaler;
+  scaler.execute(m_xScaling, m_yScaling, m_zScaling, inputDataSet, outInfo);
 
   // Need to call an update on the meta data, as it is not guaranteed that RequestInformation will be called
   // before we access the metadata.
diff --git a/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/vtkDataSetToScaledDataSet.h b/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/vtkDataSetToScaledDataSet.h
index d7d9e205f66..5a99ba72873 100644
--- a/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/vtkDataSetToScaledDataSet.h
+++ b/Code/Mantid/Vates/VatesAPI/inc/MantidVatesAPI/vtkDataSetToScaledDataSet.h
@@ -4,13 +4,14 @@
 #include "MantidKernel/System.h"
 
 class vtkUnstructuredGrid;
+class vtkInformation;
 namespace Mantid
 {
 namespace VATES
 {
 
   /**
-   *Class that handles scaling a given vtkDataSet and setting appropriate
+   *Functor class that handles scaling a given vtkDataSet and setting appropriate
    *metadata on output vtkDataSet so that original extents will be shown.
     
     @date 22/02/2013
@@ -39,20 +40,18 @@ namespace VATES
   {
   public:
     /// Constructor
-    vtkDataSetToScaledDataSet(vtkUnstructuredGrid *input,
-                              vtkUnstructuredGrid *output);
+    vtkDataSetToScaledDataSet();
     /// Destructor
     virtual ~vtkDataSetToScaledDataSet();
     /// Apply the scaling and add metadata
-    void execute(double xScale, double yScale, double zScale);
+    vtkUnstructuredGrid* execute(double xScale, double yScale, double zScale, vtkUnstructuredGrid * inputData, vtkInformation* info);
+    /// Apply the scaling and add metadata
+    vtkUnstructuredGrid* execute(double xScale, double yScale, double zScale, vtkUnstructuredGrid * inputData, vtkUnstructuredGrid * outputData = NULL);
   private:
     vtkDataSetToScaledDataSet& operator=(const vtkDataSetToScaledDataSet& other);
     /// Set metadata on the dataset to handle scaling
     void updateMetaData(double xScale, double yScale,
-                        double zScale);
-
-    vtkUnstructuredGrid *m_inputData; ///< Data to scale
-    vtkUnstructuredGrid *m_outputData; ///< Scaled data
+                        double zScale, vtkUnstructuredGrid* inputData, vtkUnstructuredGrid * outputData);
   };
 
 } // namespace VATES
diff --git a/Code/Mantid/Vates/VatesAPI/src/vtkDataSetToScaledDataSet.cpp b/Code/Mantid/Vates/VatesAPI/src/vtkDataSetToScaledDataSet.cpp
index d52ea8da772..a5335067f20 100644
--- a/Code/Mantid/Vates/VatesAPI/src/vtkDataSetToScaledDataSet.cpp
+++ b/Code/Mantid/Vates/VatesAPI/src/vtkDataSetToScaledDataSet.cpp
@@ -10,10 +10,10 @@
 #include <vtkSmartPointer.h>
 #include <vtkMatrix4x4.h>
 #include <vtkPVChangeOfBasisHelper.h>
+#include <vtkInformation.h>
 
 #include <stdexcept>
 
-
 namespace {
 Mantid::Kernel::Logger g_log("vtkDataSetTOScaledDataSet");
 }
@@ -22,20 +22,8 @@ namespace Mantid {
 namespace VATES {
 /**
  * Standard constructor for object.
- * @param input : The dataset to scale
- * @param output : The resulting scaled dataset
  */
-vtkDataSetToScaledDataSet::vtkDataSetToScaledDataSet(
-    vtkUnstructuredGrid *input, vtkUnstructuredGrid *output)
-    : m_inputData(input), m_outputData(output){
-  if (NULL == m_inputData) {
-    throw std::runtime_error("Cannot construct vtkDataSetToScaledDataSet with "
-                             "NULL input vtkUnstructuredGrid");
-  }
-  if (NULL == m_outputData) {
-    throw std::runtime_error("Cannot construct vtkDataSetToScaledDataSet with "
-                             "NULL output vtkUnstructuredGrid");
-  }
+vtkDataSetToScaledDataSet::vtkDataSetToScaledDataSet() {
 }
 
 vtkDataSetToScaledDataSet::~vtkDataSetToScaledDataSet() {}
@@ -43,14 +31,56 @@ vtkDataSetToScaledDataSet::~vtkDataSetToScaledDataSet() {}
 /**
  * Process the input data. First, scale a copy of the points and apply
  * that to the output data. Next, update the metadata for range information.
+ *
+ * This is a data source method.
+ *
+ * @param xScale : Scale factor for the x direction
+ * @param yScale : Scale factor for the y direction
+ * @param zScale : Scale factor for the z direction
+ * @param info : The dataset to scale
+ * @return The resulting scaled dataset
+ */
+vtkUnstructuredGrid *
+vtkDataSetToScaledDataSet::execute(double xScale, double yScale, double zScale,
+                                   vtkUnstructuredGrid *inputData, vtkInformation* info) {
+
+  // Extract output dataset from information.
+  vtkUnstructuredGrid *outputData = vtkUnstructuredGrid::SafeDownCast(info->Get(vtkDataObject::DATA_OBJECT()));
+
+  return execute(xScale, yScale, zScale, inputData, outputData);
+
+}
+
+
+
+
+/**
+ * Process the input data. First, scale a copy of the points and apply
+ * that to the output data. Next, update the metadata for range information.
+ *
+ * This is a data source method.
+ *
  * @param xScale : Scale factor for the x direction
  * @param yScale : Scale factor for the y direction
  * @param zScale : Scale factor for the z direction
+ * @param inputData : The dataset to scale
+ * @param outputData : The output dataset. Optional. If not specified or null, new one created.
+ * @return The resulting scaled dataset
  */
-void vtkDataSetToScaledDataSet::execute(double xScale, double yScale,
-                                        double zScale) {
+vtkUnstructuredGrid *
+vtkDataSetToScaledDataSet::execute(double xScale, double yScale, double zScale,
+                                   vtkUnstructuredGrid *inputData, vtkUnstructuredGrid* outputData) {
 
-  vtkPoints *points = m_inputData->GetPoints();
+  if (NULL == inputData) {
+    throw std::runtime_error("Cannot construct vtkDataSetToScaledDataSet with "
+                             "NULL input vtkUnstructuredGrid");
+  }
+
+  if(outputData == NULL){
+       outputData = vtkUnstructuredGrid::New();
+  }
+
+  vtkPoints *points = inputData->GetPoints();
 
   double *point;
   vtkPoints *newPoints = vtkPoints::New();
@@ -63,10 +93,12 @@ void vtkDataSetToScaledDataSet::execute(double xScale, double yScale,
     newPoints->InsertNextPoint(point);
   }
   // Shallow copy the input.
-  m_outputData->ShallowCopy(m_inputData);
+  outputData->ShallowCopy(inputData);
   // Give the output dataset the scaled set of points.
-  m_outputData->SetPoints(newPoints);
-  this->updateMetaData(xScale, yScale, zScale);
+  outputData->SetPoints(newPoints);
+
+  this->updateMetaData(xScale, yScale, zScale, inputData, outputData);
+  return outputData;
 }
 
 /**
@@ -81,9 +113,11 @@ void vtkDataSetToScaledDataSet::execute(double xScale, double yScale,
  * @param xScale : Scale factor for the x direction
  * @param yScale : Scale factor for the y direction
  * @param zScale : Scale factor for the z direction
+ * @param inputData : Input dataset
+ * @param outputData : Output dataset
  */
 void vtkDataSetToScaledDataSet::updateMetaData(double xScale, double yScale,
-                                               double zScale) {
+                                               double zScale, vtkUnstructuredGrid *inputData, vtkUnstructuredGrid *outputData) {
   // We need to put the scaling on the diagonal elements of the ChangeOfBasis
   // (COB) Matrix.
   vtkSmartPointer<vtkMatrix4x4> cobMatrix =
@@ -93,7 +127,7 @@ void vtkDataSetToScaledDataSet::updateMetaData(double xScale, double yScale,
   cobMatrix->Element[1][1] *= yScale;
   cobMatrix->Element[2][2] *= zScale;
 
-  if (!vtkPVChangeOfBasisHelper::AddChangeOfBasisMatrixToFieldData(m_outputData,
+  if (!vtkPVChangeOfBasisHelper::AddChangeOfBasisMatrixToFieldData(outputData,
                                                                    cobMatrix)) {
     g_log.warning("The Change-of-Basis-Matrix could not be added to the field "
                   "data of the scaled data set.\n");
@@ -101,8 +135,8 @@ void vtkDataSetToScaledDataSet::updateMetaData(double xScale, double yScale,
 
   // We also need to update the bounding box for the COB Matrix
   double boundingBox[6];
-  m_inputData->GetBounds(boundingBox);
-  if (!vtkPVChangeOfBasisHelper::AddBoundingBoxInBasis(m_outputData,
+  inputData->GetBounds(boundingBox);
+  if (!vtkPVChangeOfBasisHelper::AddBoundingBoxInBasis(outputData,
                                                        boundingBox)) {
     g_log.warning("The bounding box could not be added to the field data of "
                   "the scaled data set.\n");
diff --git a/Code/Mantid/Vates/VatesAPI/test/vtkDataSetToScaledDataSetTest.h b/Code/Mantid/Vates/VatesAPI/test/vtkDataSetToScaledDataSetTest.h
index ff26639200b..b7caf816535 100644
--- a/Code/Mantid/Vates/VatesAPI/test/vtkDataSetToScaledDataSetTest.h
+++ b/Code/Mantid/Vates/VatesAPI/test/vtkDataSetToScaledDataSetTest.h
@@ -68,23 +68,19 @@ public:
 
   void testThrowIfInputNull() {
     vtkUnstructuredGrid *in = NULL;
-    vtkUnstructuredGrid *out = vtkUnstructuredGrid::New();
-    TS_ASSERT_THROWS(vtkDataSetToScaledDataSet scaler(in, out),
-                     std::runtime_error);
-  }
 
-  void testThrowIfOutputNull() {
-    vtkUnstructuredGrid *in = vtkUnstructuredGrid::New();
-    vtkUnstructuredGrid *out = NULL;
-    TS_ASSERT_THROWS(vtkDataSetToScaledDataSet scaler(in, out),
+    vtkDataSetToScaledDataSet scaler;
+
+    TS_ASSERT_THROWS(scaler.execute(1, 1, 1, in),
                      std::runtime_error);
   }
 
+
   void testExecution() {
+
+    vtkDataSetToScaledDataSet scaler;
     vtkUnstructuredGrid *in = makeDataSet();
-    vtkUnstructuredGrid *out = vtkUnstructuredGrid::New();
-    vtkDataSetToScaledDataSet scaler(in, out);
-    TS_ASSERT_THROWS_NOTHING(scaler.execute(0.1, 0.5, 0.2));
+    vtkUnstructuredGrid* out = scaler.execute(0.1, 0.5, 0.2, in);
 
     // Check bounds are scaled
     double *bb = out->GetBounds();
@@ -136,11 +132,10 @@ public:
   void testJsonMetadataExtractionFromScaledDataSet() {
     // Arrange
     vtkUnstructuredGrid *in = makeDataSetWithJsonMetadata();
-    vtkUnstructuredGrid *out = vtkUnstructuredGrid::New();
 
     // Act
-    vtkDataSetToScaledDataSet scaler(in, out);
-    TS_ASSERT_THROWS_NOTHING(scaler.execute(0.1, 0.5, 0.2));
+    vtkDataSetToScaledDataSet scaler;
+    vtkUnstructuredGrid* out = scaler.execute(0.1, 0.5, 0.2, in);
 
     vtkFieldData *fieldData = out->GetFieldData();
     MetadataJsonManager manager;
-- 
GitLab