Commit e18781e9 authored by Utkarsh Ayachit's avatar Utkarsh Ayachit Committed by Kyle Lutz

WIP: Adding a filter demonstrating extracting using Python

parent 818a8e98
......@@ -140,6 +140,7 @@
<Proxy group="filters" name="ProgrammableFilter" />
<Proxy group="filters" name="PVConnectivityFilter" />
<Proxy group="filters" name="PythonCalculator" />
<Proxy group="filters" name="PythonExtractSelection" />
<Proxy group="filters" name="QuadraturePointInterpolator" />
<Proxy group="filters" name="QuadraturePointsGenerator" />
<Proxy group="filters" name="QuadraturePointStatistics" />
......
......@@ -139,6 +139,7 @@ IF(PARAVIEW_ENABLE_PYTHON)
SET(Kit_SRCS ${Kit_SRCS}
vtkPythonAnimationCue.cxx
vtkPythonCalculator.cxx
vtkPythonExtractSelection.cxx
vtkPythonProgrammableFilter.cxx
)
SET (KIT_LIBS ${KIT_LIBS} ${PYTHON_LIBRARIES} vtkPVPythonInterpretor)
......
This diff is collapsed.
/*=========================================================================
Program: ParaView
Module: $RCSfile$
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkPythonExtractSelection
// .SECTION Description
//
#ifndef __vtkPythonExtractSelection_h
#define __vtkPythonExtractSelection_h
#include "vtkProgrammableFilter.h"
class vtkCharArray;
class vtkDataSet;
class vtkTable;
class vtkUnstructuredGrid;
class VTK_EXPORT vtkPythonExtractSelection : public vtkProgrammableFilter
{
public:
static vtkPythonExtractSelection* New();
vtkTypeMacro(vtkPythonExtractSelection, vtkProgrammableFilter);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Which field data to get the arrays from. See
// vtkDataObject::FieldAssociations for choices. The default
// is FIELD_ASSOCIATION_POINTS.
vtkSetMacro(ArrayAssociation, int);
vtkGetMacro(ArrayAssociation, int);
// Description:
// Set the text of the python expression to execute. This expression
// must return a scalar value (which is converted to an array) or a
// numpy array.
vtkSetStringMacro(Expression)
vtkGetStringMacro(Expression)
// Description:
// Internal method.
vtkDataObject* ExtractElements(vtkDataObject* data, vtkCharArray* mask);
//BTX
protected:
vtkPythonExtractSelection();
~vtkPythonExtractSelection();
// Description:
// For internal use only.
void Exec();
vtkUnstructuredGrid* ExtractPoints(vtkDataSet* data, vtkCharArray* mask);
vtkUnstructuredGrid* ExtractCells(vtkDataSet* data, vtkCharArray* mask);
vtkTable* ExtractElements(vtkTable* data, vtkCharArray* mask);
virtual int FillInputPortInformation(int port, vtkInformation *info);
// Description:
// Creates whatever output data set type is selected.
virtual int RequestDataObject(vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector);
int ArrayAssociation;
char* Expression;
private:
vtkPythonExtractSelection(const vtkPythonExtractSelection&); // Not implemented
void operator=(const vtkPythonExtractSelection&); // Not implemented
// Description:
// For internal use only.
static void ExecuteScript(void *);
//ETX
};
#endif
<ServerManagerConfiguration>
<ProxyGroup name="filters">
<SourceProxy name="PythonExtractSelection"
class="vtkPythonExtractSelection"
label="Extract Selection (Python)">
<InputProperty
name="Input"
command="AddInputConnection"
clean_command="RemoveAllInputs"
multiple_input="1">
<ProxyGroupDomain name="groups">
<Group name="sources"/>
<Group name="filters"/>
</ProxyGroupDomain>
<DataTypeDomain name="input_type">
<DataType value="vtkDataSet"/>
</DataTypeDomain>
<Documentation>
Set the input of the filter.
</Documentation>
</InputProperty>
<StringVectorProperty
name="Expression"
command="SetExpression"
number_of_elements="1">
<Documentation>
The Python expression evaluated during execution.
</Documentation>
</StringVectorProperty>
<IntVectorProperty
name="ArrayAssociation"
command="SetArrayAssociation"
number_of_elements="1"
default_values="0">
<EnumerationDomain name="enum">
<Entry value="0" text="Point Data"/>
<Entry value="1" text="Cell Data"/>
</EnumerationDomain>
<Documentation>
This property controls the association of the output array as well as
which arrays are defined as variables.
</Documentation>
</IntVectorProperty>
</SourceProxy>
<SourceProxy name="PythonCalculator"
class="vtkPythonCalculator"
......
......@@ -101,6 +101,7 @@ IF (PARAVIEW_ENABLE_PYTHON)
smstate
pvfilters
pvvtkextensions
extract_selection
)
# Now copy these files if necessary.
......
#==============================================================================
#
# Program: ParaView
# Module: extract_selection.py
#
# Copyright (c) Kitware, Inc.
# All rights reserved.
# See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
#==============================================================================
r"""
This module is used by vtkPythonExtractSelection filter.
"""
import paraview
from paraview.vtk import dataset_adapter
from numpy import *
from paraview.vtk.algorithms import *
from paraview import servermanager
if servermanager.progressObserverTag:
servermanager.ToggleProgressPrinting()
def PassBlock(self, iterCD):
"""Test if the block passes the block-criteria, if any"""
return True
def ExtractElements(self, inputDS, mask):
if type(mask) == bool:
if mask:
# FIXME: We need to add the "vtkOriginalIds" array.
return inputDS
else:
# nothing was extracted.
return None
else:
# mask must be an array. Process it.
mask_array = dataset_adapter.numpyTovtkDataArray(int8(mask), "_mask_array")
retVal = self.ExtractElements(inputDS, mask_array)
if retVal:
retVal.UnRegister(None)
return retVal
return None
def ExecData(self, inputDS):
"""inputDS is either a non-composite data object"""
# wrap the data objects. makes them easier to use.
do = dataset_adapter.WrapDataObject(inputDS)
dsa = dataset_adapter.DataSetAttributes(
inputDS.GetAttributes(self.GetArrayAssociation()),
do, self.GetArrayAssociation())
new_locals = {}
# define global variables for all the arrays.
for arrayname in dsa.keys():
name = paraview.make_name_valid(arrayname)
new_locals[name] = dsa[arrayname]
new_locals["cell"] = do
new_locals["dataset"] = do
new_locals["input"] = do
new_locals["element"] = do
new_locals["id"] = arange(inputDS.GetNumberOfElements(
self.GetArrayAssociation()))
# evaluate the query expression. The expression should return a mask which
# is either an array or a boolean value.
mask = eval(self.GetExpression(), globals(), new_locals)
print mask
# extract the elements from the input dataset using the mask.
extracted_ds = ExtractElements(self, inputDS, mask)
del mask
del new_locals
del do
del dsa
return extracted_ds
def Exec(self, inputDO, outputDO):
if inputDO.IsA("vtkCompositeDataSet"):
outputDO.CopyStructure(inputDO)
# For composite datasets, iterate over the tree and call ExecData() only
# for this nodes that pass the block-criteria, if any.
iterCD = inputDO.NewIterator()
iterCD.UnRegister(None)
while not iterCD.IsDoneWithTraversal():
if PassBlock(self, iterCD):
ds = ExecData(self, iterCD.GetCurrentDataObject())
outputDO.SetDataSet(iterCD, ds)
del ds
iterCD.GoToNextItem()
del iterCD
else:
ds = ExecData(self, inputDO)
if ds:
outputDO.ShallowCopy(ds)
del ds
return True
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment