vtk class for wrapping/handling external command line program

I am looking to wrap some external commands in a C++ vtk class for developer’s convenience.

What class should I look into ?

Some of the external program are generator of information (source), some are filters and some are sink (e.g. send email/update status on web server via http call).

Cheers

Answering my own question, I think the vtkExecutableRunner is what I will start looking into.

It looks promising.

Cheers

1 Like

FYI @Timothee_Chabat :slight_smile:

It was indeed created for that :slight_smile: just for your information @Nicholas_Yue vtkExecutableRunner is not an actual filter (ie it does not inherit vtkAlgorithm) so you will not be able to use it in a pipeline as is, but you can always create your own filters that wraps the class

1 Like

@Timothee_Chabat just clarifying that I should aim towards something like MyRunnerSI rather than MyRunnerMI

class MyRunnerSI : public vtkAlgorithm {

public:
  void Execute(const char* command_string) {

    vtkNew<vtkExecutableRunner> runner;

   runner->SetCommand(command_string);
  }
}
class MyRunnerMI : public vtkAlgorithm, public vtkExecutableRunner {
}

@Timothee_Chabat

Where would be the most appropriate place the call runner->Execute() within the vtkAlgorithm ? I think it should be ProcessRequest()

Here is my initial attempt base on my reading of vtkPolyDataAlgorithm class

#include "vtkRunnerFilter.h"

#include <vtkCommand.h>
#include <vtkInformation.h>
#include <vtkInformationVector.h>
#include <vtkObjectFactory.h>
#include <vtkPolyData.h>
#include <vtkStreamingDemandDrivenPipeline.h>

vtkStandardNewMacro(vtkRunnerFilter);

//------------------------------------------------------------------------------
vtkRunnerFilter::vtkRunnerFilter()
{
	// by default assume filters have one input and one output
	// subclasses that deviate should modify this setting
	this->SetNumberOfInputPorts(1);
	this->SetNumberOfOutputPorts(1);

	_runnerPtr = vtkSmartPointer<vtkExecutableRunner>::New();

}

//------------------------------------------------------------------------------
vtkRunnerFilter::~vtkRunnerFilter() = default;

//------------------------------------------------------------------------------
void vtkRunnerFilter::PrintSelf(ostream& os, vtkIndent indent)
{
	this->Superclass::PrintSelf(os, indent);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetOutput()
{
	return this->GetOutput(0);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetOutput(int port)
{
	return this->GetOutputDataObject(port);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::SetOutput(vtkDataObject* d)
{
	this->GetExecutive()->SetOutputData(0, d);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetInput()
{
	return this->GetInput(0);
}

//------------------------------------------------------------------------------
vtkDataObject* vtkRunnerFilter::GetInput(int port)
{
	return this->GetExecutive()->GetInputData(port, 0);
}

//------------------------------------------------------------------------------
vtkTypeBool vtkRunnerFilter::ProcessRequest(
		vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
	// generate the data
	if (request->Has(vtkDemandDrivenPipeline::REQUEST_DATA()))
	{
		return this->RequestData(request, inputVector, outputVector);
	}

	if (request->Has(vtkStreamingDemandDrivenPipeline::REQUEST_UPDATE_EXTENT()))
	{
		return this->RequestUpdateExtent(request, inputVector, outputVector);
	}

	// execute information
	if (request->Has(vtkDemandDrivenPipeline::REQUEST_INFORMATION()))
	{
		return this->RequestInformation(request, inputVector, outputVector);
	}

	_runnerPtr->Execute();

	return this->Superclass::ProcessRequest(request, inputVector, outputVector);
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::FillOutputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
	// now add our info
	info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkExecutableRunner");
	return 1;
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
	info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkExecutableRunner");
	return 1;
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::RequestInformation(vtkInformation* vtkNotUsed(request),
		vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
{
	// do nothing let subclasses handle it
	return 1;
}

//------------------------------------------------------------------------------
int vtkRunnerFilter::RequestUpdateExtent(vtkInformation* vtkNotUsed(request),
		vtkInformationVector** inputVector, vtkInformationVector* vtkNotUsed(outputVector))
{
	int numInputPorts = this->GetNumberOfInputPorts();
	for (int i = 0; i < numInputPorts; i++)
	{
		int numInputConnections = this->GetNumberOfInputConnections(i);
		for (int j = 0; j < numInputConnections; j++)
		{
			vtkInformation* inputInfo = inputVector[i]->GetInformationObject(j);
			inputInfo->Set(vtkStreamingDemandDrivenPipeline::EXACT_EXTENT(), 1);
		}
	}
	return 1;
}

//------------------------------------------------------------------------------
// This is the superclasses style of Execute method.  Convert it into
// an imaging style Execute method.
int vtkRunnerFilter::RequestData(vtkInformation* vtkNotUsed(request),
		vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* vtkNotUsed(outputVector))
{
	return 1;
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::SetInputData(vtkDataObject* input)
{
	this->SetInputData(0, input);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::SetInputData(int index, vtkDataObject* input)
{
	this->SetInputDataInternal(index, input);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::AddInputData(vtkDataObject* input)
{
	this->AddInputData(0, input);
}

//------------------------------------------------------------------------------
void vtkRunnerFilter::AddInputData(int index, vtkDataObject* input)
{
	this->AddInputDataInternal(index, input);
}

Usually filters are doing the actual work when a REQUEST_DATA request is given so I would put the Execute code in RequestData. Also I don’t think you need to handle REQUEST_DATA_EXTENT or REQUEST_INFORMATION based on what you said, but you know better than me.

If you’re not sure what they are for and their order of executions I recommand you to read the 3 parts of the blog about the VTK pipeline, especially part 2 and 3 (see first part here https://www.kitware.com/a-vtk-pipeline-primer-part-1)