Commit 2321fd17 authored by Will Schroeder's avatar Will Schroeder Committed by Kitware Robot

Merge topic 'ImplicitFunctionSampling'

e5425ed8 Regression test image
a13950ca New regression test images
7a31d165 Renamed filter for consistency
bf20a5aa Pass input attribute data to output
bdc02994 Cleaned up documentation
707da941 New class generates data from implicit function
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !1259
parents db3c55e9 e5425ed8
......@@ -61,6 +61,7 @@ set(Module_SRCS
vtkRecursiveDividingCubes.cxx
vtkReflectionFilter.cxx
vtkRotationFilter.cxx
vtkSampleImplicitFunctionFilter.cxx
vtkShrinkFilter.cxx
vtkShrinkPolyData.cxx
vtkSpatialRepresentationFilter.cxx
......
......@@ -10,6 +10,7 @@ vtk_add_test_python(
TestGraphLayoutFilter.py
TestMultiBlockStreamer.py
TestRectilinearGridToTetrahedra.py
TestSampleImplicitFunctionFilter.py
TestSplineFilter.py
WarpToImage.py
blankGrid.py
......
#!/usr/bin/env python
import vtk
from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
# Parameters for debugging
NPts = 100000
# create pipeline
#
points = vtk.vtkPointSource()
points.SetNumberOfPoints(NPts)
points.SetRadius(5)
# create some scalars based on implicit function
# Create a cylinder
cyl = vtk.vtkCylinder()
cyl.SetCenter(0,0,0)
cyl.SetRadius(0.1)
# Generate scalars and vector
sample = vtk.vtkSampleImplicitFunctionFilter()
sample.SetInputConnection(points.GetOutputPort())
sample.SetImplicitFunction(cyl)
sample.Update()
print(sample.GetOutput().GetScalarRange())
# Draw the points
sampleMapper = vtk.vtkPointGaussianMapper()
sampleMapper.SetInputConnection(sample.GetOutputPort(0))
sampleMapper.EmissiveOff()
sampleMapper.SetScaleFactor(0.0)
sampleMapper.SetScalarRange(0,20)
sampleActor = vtk.vtkActor()
sampleActor.SetMapper(sampleMapper)
# Create an outline
outline = vtk.vtkOutlineFilter()
outline.SetInputConnection(sample.GetOutputPort())
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)
# Create the RenderWindow, Renderer and both Actors
#
ren0 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren0)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors to the renderer, set the background and size
#
ren0.AddActor(sampleActor)
ren0.AddActor(outlineActor)
ren0.SetBackground(0.1, 0.2, 0.4)
renWin.SetSize(250,250)
cam = ren0.GetActiveCamera()
cam.SetFocalPoint(0,0,0)
cam.SetPosition(1,1,1)
ren0.ResetCamera()
iren.Initialize()
# render the image
#
renWin.Render()
#iren.Start()
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSampleImplicitFunctionFilter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkSampleImplicitFunctionFilter.h"
#include "vtkFloatArray.h"
#include "vtkGarbageCollector.h"
#include "vtkImplicitFunction.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkDataSet.h"
#include "vtkCellData.h"
#include "vtkPointData.h"
#include "vtkSMPTools.h"
vtkStandardNewMacro(vtkSampleImplicitFunctionFilter);
vtkCxxSetObjectMacro(vtkSampleImplicitFunctionFilter,ImplicitFunction,vtkImplicitFunction);
// Interface between vtkSMPTools and the VTK pipeline
namespace {
struct SampleDataSet
{
vtkDataSet *Input;
vtkImplicitFunction *Function;
float *Scalars;
// Contructor
SampleDataSet(vtkDataSet *input, vtkImplicitFunction *imp, float *s) :
Input(input), Function(imp), Scalars(s)
{
}
void operator() (vtkIdType ptId, vtkIdType endPtId)
{
double x[3];
float *n = this->Scalars + ptId;
for ( ; ptId < endPtId; ++ptId )
{
this->Input->GetPoint(ptId, x);
*n++ = this->Function->FunctionValue(x);
}
}
};
struct SampleDataSetWithGradients
{
vtkDataSet *Input;
vtkImplicitFunction *Function;
float *Scalars;
float *Gradients;
// Contructor
SampleDataSetWithGradients(vtkDataSet *input, vtkImplicitFunction *imp, float *s, float *g) :
Input(input), Function(imp), Scalars(s), Gradients(g)
{
}
void operator() (vtkIdType ptId, vtkIdType endPtId)
{
double x[3], g[3];
float *n = this->Scalars + ptId;
float *v = this->Gradients + 3*ptId;
for ( ; ptId < endPtId; ++ptId )
{
this->Input->GetPoint(ptId, x);
*n++ = this->Function->FunctionValue(x);
this->Function->FunctionGradient(x,g);
*v++ = g[0];
*v++ = g[1];
*v++ = g[2];
}
}
};
} //anonymous namespace
//----------------------------------------------------------------------------
// Okay define the VTK class proper
vtkSampleImplicitFunctionFilter::vtkSampleImplicitFunctionFilter()
{
this->ImplicitFunction = NULL;
this->ComputeGradients = 1;
this->ScalarArrayName = 0;
this->SetScalarArrayName("Implicit scalars");
this->GradientArrayName = 0;
this->SetGradientArrayName("Implicit gradients");
}
//----------------------------------------------------------------------------
vtkSampleImplicitFunctionFilter::~vtkSampleImplicitFunctionFilter()
{
this->SetImplicitFunction(NULL);
this->SetScalarArrayName(NULL);
this->SetGradientArrayName(NULL);
}
//----------------------------------------------------------------------------
// Produce the output data
int vtkSampleImplicitFunctionFilter::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkDebugMacro(<< "Generating implicit data");
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkDataSet *input = vtkDataSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataSet *output = vtkDataSet::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// Check the input
if ( !input || !output )
{
return 1;
}
vtkIdType numPts = input->GetNumberOfPoints();
if ( numPts < 1 )
{
return 1;
}
// Ensure implicit function is specified
//
if ( !this->ImplicitFunction )
{
vtkErrorMacro(<<"No implicit function specified");
return 1;
}
// The output geometic structure is the same as the input
output->CopyStructure(input);
// Pass the output attribute data
output->GetPointData()->PassData(input->GetPointData());
output->GetCellData()->PassData(input->GetCellData());
// Set up for execution
vtkFloatArray *newScalars = vtkFloatArray::New();
newScalars->SetNumberOfTuples(numPts);
float *scalars = newScalars->WritePointer(0,numPts);
vtkFloatArray *newGradients=NULL;
float *gradients=NULL;
if ( this->ComputeGradients )
{
newGradients = vtkFloatArray::New();
newGradients->SetNumberOfComponents(3);
newGradients->SetNumberOfTuples(numPts);
gradients = newGradients->WritePointer(0,numPts);
}
// Threaded execute
if ( this->ComputeGradients )
{
SampleDataSetWithGradients
sample(input,this->ImplicitFunction,scalars,gradients);
vtkSMPTools::For(0,numPts, sample);
}
else
{
SampleDataSet sample(input,this->ImplicitFunction,scalars);
vtkSMPTools::For(0,numPts, sample);
}
// Update self
newScalars->SetName(this->ScalarArrayName);
output->GetPointData()->AddArray(newScalars);
output->GetPointData()->SetActiveScalars(this->ScalarArrayName);
newScalars->Delete();
if ( this->ComputeGradients )
{
newGradients->SetName(this->GradientArrayName);
output->GetPointData()->AddArray(newGradients);
output->GetPointData()->SetActiveVectors(this->GradientArrayName);
newGradients->Delete();
}
return 1;
}
//----------------------------------------------------------------------------
int vtkSampleImplicitFunctionFilter::
FillInputPortInformation(int, vtkInformation *info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
return 1;
}
//----------------------------------------------------------------------------
unsigned long vtkSampleImplicitFunctionFilter::GetMTime()
{
unsigned long mTime=this->Superclass::GetMTime();
unsigned long impFuncMTime;
if ( this->ImplicitFunction != NULL )
{
impFuncMTime = this->ImplicitFunction->GetMTime();
mTime = ( impFuncMTime > mTime ? impFuncMTime : mTime );
}
return mTime;
}
//----------------------------------------------------------------------------
void vtkSampleImplicitFunctionFilter::
ReportReferences(vtkGarbageCollector* collector)
{
this->Superclass::ReportReferences(collector);
vtkGarbageCollectorReport(collector, this->ImplicitFunction,
"ImplicitFunction");
}
//----------------------------------------------------------------------------
void vtkSampleImplicitFunctionFilter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if ( this->ImplicitFunction )
{
os << indent << "Implicit Function: " << this->ImplicitFunction << "\n";
}
else
{
os << indent << "No Implicit function defined\n";
}
os << indent << "Compute Gradients: " << (this->ComputeGradients ? "On\n" : "Off\n");
os << indent << "Scalar Array Name: ";
if(this->ScalarArrayName != 0)
{
os << this->ScalarArrayName << endl;
}
else
{
os << "(none)" << endl;
}
os << indent << "Gradient Array Name: ";
if(this->GradientArrayName != 0)
{
os << this->GradientArrayName << endl;
}
else
{
os << "(none)" << endl;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSampleImplicitFunctionFilter.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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 vtkSampleImplicitFunctionFilter - sample an implicit function over a dataset,
// generating scalar values and optional gradient vectors
// .SECTION Description
// vtkSampleImplicitFunctionFilter is a filter that evaluates an implicit function and
// (optional) gradients at each point in an input vtkDataSet. The output
// of the filter are new scalar values (the function values) and the
// optional vector (function gradient) array.
// .SECTION Caveats
// This class has been threaded with vtkSMPTools. Using TBB or other
// non-sequential type (set in the CMake variable
// VTK_SMP_IMPLEMENTATION_TYPE) may improve performance significantly.
// .SECTION See Also
// vtkSampleFunction vtkImplicitModeller
#ifndef vtkSampleImplicitFunctionFilter_h
#define vtkSampleImplicitFunctionFilter_h
#include "vtkFiltersGeneralModule.h" // For export macro
#include "vtkDataSetAlgorithm.h"
class vtkImplicitFunction;
class vtkDataArray;
class VTKFILTERSGENERAL_EXPORT vtkSampleImplicitFunctionFilter : public vtkDataSetAlgorithm
{
public:
// Description:
// Standard instantiation, type information, and print methods.
static vtkSampleImplicitFunctionFilter *New();
vtkTypeMacro(vtkSampleImplicitFunctionFilter,vtkDataSetAlgorithm);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Specify the implicit function to use to generate data.
virtual void SetImplicitFunction(vtkImplicitFunction*);
vtkGetObjectMacro(ImplicitFunction,vtkImplicitFunction);
// Description:
// Turn on/off the computation of gradients.
vtkSetMacro(ComputeGradients,int);
vtkGetMacro(ComputeGradients,int);
vtkBooleanMacro(ComputeGradients,int);
// Description:
// Set/get the scalar array name for this data set. The initial value is
// "Implicit scalars".
vtkSetStringMacro(ScalarArrayName);
vtkGetStringMacro(ScalarArrayName);
// Description:
// Set/get the gradient array name for this data set. The initial value is
// "Implicit gradients".
vtkSetStringMacro(GradientArrayName);
vtkGetStringMacro(GradientArrayName);
// Description:
// Return the MTime also taking into account the implicit function.
unsigned long GetMTime();
protected:
vtkSampleImplicitFunctionFilter();
~vtkSampleImplicitFunctionFilter();
vtkImplicitFunction *ImplicitFunction;
int ComputeGradients;
char *ScalarArrayName;
char *GradientArrayName;
virtual void ReportReferences(vtkGarbageCollector*);
virtual int RequestData(vtkInformation *, vtkInformationVector **,
vtkInformationVector *);
virtual int FillInputPortInformation(int port, vtkInformation *info);
private:
vtkSampleImplicitFunctionFilter(const vtkSampleImplicitFunctionFilter&); // Not implemented.
void operator=(const vtkSampleImplicitFunctionFilter&); // Not implemented.
};
#endif
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