1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkProgrammableAttributeDataFilter.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkProgrammableAttributeDataFilter.h"
16 
17 #include "vtkCellData.h"
18 #include "vtkDataSetCollection.h"
19 #include "vtkGarbageCollector.h"
20 #include "vtkInformation.h"
21 #include "vtkInformationVector.h"
22 #include "vtkObjectFactory.h"
23 #include "vtkPointData.h"
24 
25 vtkStandardNewMacro(vtkProgrammableAttributeDataFilter);
26 
vtkProgrammableAttributeDataFilter()27 vtkProgrammableAttributeDataFilter::vtkProgrammableAttributeDataFilter()
28 {
29   this->ExecuteMethod = NULL;
30   this->ExecuteMethodArg = NULL;
31   this->ExecuteMethodArgDelete = NULL;
32   this->InputList = vtkDataSetCollection::New();
33 }
34 
~vtkProgrammableAttributeDataFilter()35 vtkProgrammableAttributeDataFilter::~vtkProgrammableAttributeDataFilter()
36 {
37   // delete the current arg if there is one and a delete meth
38   if ((this->ExecuteMethodArg)&&(this->ExecuteMethodArgDelete))
39     {
40     (*this->ExecuteMethodArgDelete)(this->ExecuteMethodArg);
41     }
42   if (this->InputList)
43     {
44     this->InputList->Delete();
45     this->InputList = NULL;
46     }
47 }
48 
49 // Add a dataset to the list of data to process.
AddInput(vtkDataSet * ds)50 void vtkProgrammableAttributeDataFilter::AddInput(vtkDataSet *ds)
51 {
52   if ( ! this->InputList->IsItemPresent(ds) )
53     {
54     this->Modified();
55     this->InputList->AddItem(ds);
56     }
57 }
58 
59 // Remove a dataset from the list of data to process.
RemoveInput(vtkDataSet * ds)60 void vtkProgrammableAttributeDataFilter::RemoveInput(vtkDataSet *ds)
61 {
62   if ( this->InputList->IsItemPresent(ds) )
63     {
64     this->Modified();
65     this->InputList->RemoveItem(ds);
66     }
67 }
68 
69 // Specify the function to use to operate on the point attribute data. Note
70 // that the function takes a single (void *) argument.
SetExecuteMethod(void (* f)(void *),void * arg)71 void vtkProgrammableAttributeDataFilter::SetExecuteMethod(
72   void (*f)(void *), void *arg)
73 {
74   if ( f != this->ExecuteMethod || arg != this->ExecuteMethodArg )
75     {
76     // delete the current arg if there is one and a delete meth
77     if ((this->ExecuteMethodArg)&&(this->ExecuteMethodArgDelete))
78       {
79       (*this->ExecuteMethodArgDelete)(this->ExecuteMethodArg);
80       }
81     this->ExecuteMethod = f;
82     this->ExecuteMethodArg = arg;
83     this->Modified();
84     }
85 }
86 
87 // Set the arg delete method. This is used to free user memory.
SetExecuteMethodArgDelete(void (* f)(void *))88 void vtkProgrammableAttributeDataFilter::SetExecuteMethodArgDelete(
89   void (*f)(void *))
90 {
91   if ( f != this->ExecuteMethodArgDelete)
92     {
93     this->ExecuteMethodArgDelete = f;
94     this->Modified();
95     }
96 }
97 
RequestData(vtkInformation * vtkNotUsed (request),vtkInformationVector ** inputVector,vtkInformationVector * outputVector)98 int vtkProgrammableAttributeDataFilter::RequestData(
99   vtkInformation *vtkNotUsed(request),
100   vtkInformationVector **inputVector,
101   vtkInformationVector *outputVector)
102 {
103   // get the info objects
104   vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
105   vtkInformation *outInfo = outputVector->GetInformationObject(0);
106 
107   // get the input and output
108   vtkDataSet *input = vtkDataSet::SafeDownCast(
109     inInfo->Get(vtkDataObject::DATA_OBJECT()));
110   vtkDataSet *output = vtkDataSet::SafeDownCast(
111     outInfo->Get(vtkDataObject::DATA_OBJECT()));
112 
113   vtkDebugMacro(<<"Executing programmable point data filter");
114 
115   // First, copy the input to the output as a starting point
116   output->CopyStructure( input );
117 
118   // Output data is the same as input data by default.
119   output->GetCellData()->PassData(input->GetCellData());
120   output->GetPointData()->PassData(input->GetPointData());
121 
122   // Now invoke the procedure, if specified.
123   if ( this->ExecuteMethod != NULL )
124     {
125     (*this->ExecuteMethod)(this->ExecuteMethodArg);
126     }
127 
128   return 1;
129 }
130 
PrintSelf(ostream & os,vtkIndent indent)131 void vtkProgrammableAttributeDataFilter::PrintSelf(ostream& os, vtkIndent indent)
132 {
133   this->Superclass::PrintSelf(os,indent);
134 
135   os << indent << "Input DataSets:\n";
136   this->InputList->PrintSelf(os,indent.GetNextIndent());
137 
138   if ( this->ExecuteMethod )
139     {
140     os << indent << "An ExecuteMethod has been defined\n";
141     }
142   else
143     {
144     os << indent << "An ExecuteMethod has NOT been defined\n";
145     }
146 }
147 
148 //----------------------------------------------------------------------------
149 void
150 vtkProgrammableAttributeDataFilter
ReportReferences(vtkGarbageCollector * collector)151 ::ReportReferences(vtkGarbageCollector* collector)
152 {
153   this->Superclass::ReportReferences(collector);
154   vtkGarbageCollectorReport(collector, this->InputList, "InputList");
155 }
156