+#include "surfacerenderingimagestencilexport.h"
+
+#include "vtkObjectFactory.h"
+#include "vtkMetaImageWriter.h"
+
+#include "math.h"
+
+vtkStandardNewMacro(SurfaceRenderingImageStencilExport)
+
+SurfaceRenderingImageStencilExport::SurfaceRenderingImageStencilExport()
+{
+ ImageOutput = 0;
+}
+
+SurfaceRenderingImageStencilExport::~SurfaceRenderingImageStencilExport(){
+
+ if(ImageOutput){
+ ImageOutput->Delete();
+ }
+}
+
+void SurfaceRenderingImageStencilExport::Update(){
+ this->Execute(0);
+}
+
+void SurfaceRenderingImageStencilExport::Execute(vtkImageData *data){
+
+ vtkPolyData* polydata = (vtkPolyData*)this->GetInput();
+
+ vtkSmartPointer<vtkPolyDataToImageStencil> polytostencil = vtkSmartPointer<vtkPolyDataToImageStencil>::New();
+ polytostencil->SetInput(polydata);
+ polytostencil->Update();
+
+ double *bounds = polydata->GetBounds();
+ vtkSmartPointer<vtkImageData> imagein = vtkSmartPointer<vtkImageData>::New();
+
+ imagein->SetExtent(bounds[0] - 1, bounds[1] + 1, bounds[2] - 1, bounds[3] + 1, bounds[4] - 1, bounds[5] + 1);
+ imagein->SetScalarTypeToUnsignedShort();
+ imagein->AllocateScalars();
+
+
+ int* extent = imagein->GetExtent();
+
+ for (int x = extent[0]; x <= extent[1]; x++){
+ for (int y = extent[2]; y <= extent[3]; y++){
+ for (int z =extent[4]; z <= extent[5]; z++){
+ unsigned short* pixel = static_cast<unsigned short*>(imagein->GetScalarPointer(x,y,z));
+ *pixel = 0;
+ }
+ }
+ }
+
+ vtkSmartPointer<vtkImageStencil> stencil = vtkSmartPointer<vtkImageStencil>::New();
+ stencil->SetInput(imagein);
+ stencil->SetStencil(polytostencil->GetOutput());
+ stencil->ReverseStencilOn();
+ stencil->SetBackgroundValue(128);
+ stencil->Update();
+
+ if(ImageOutput){
+ ImageOutput->Delete();
+ }
+ ImageOutput = vtkImageData::New();
+ ImageOutput->DeepCopy(stencil->GetOutput());
+
+}
+
+vtkImageData* SurfaceRenderingImageStencilExport::GetOutput(){
+ return ImageOutput;
+}