#!/usr/local/bin/perl -w
#
use Graphics::VTK;



use Tk;
use Graphics::VTK::Tk;
$MW = Tk::MainWindow->new;

$VTK_DATA = 0;
$VTK_DATA = $ENV{VTK_DATA};
# include get the vtk interactor ui
use Graphics::VTK::Tk::vtkInt;
$points = Graphics::VTK::Points->new;
$points->InsertNextPoint(2,4,0);
$points->InsertNextPoint(2.6,2.6,0);
$points->InsertNextPoint(4,2,0);
$points->InsertNextPoint(1.4,4,1.4);
$points->InsertNextPoint(2,3,1);
$points->InsertNextPoint(3,2,1);
$points->InsertNextPoint(4,1.4,1.4);
$points->InsertNextPoint(0,4,2);
$points->InsertNextPoint(1,3,2);
$points->InsertNextPoint(2,2,2);
$points->InsertNextPoint(3,1,2);
$points->InsertNextPoint(4,0,2);
$points->InsertNextPoint(0,2.6,2.6);
$points->InsertNextPoint(1,2,3);
$points->InsertNextPoint(2,1,3);
$points->InsertNextPoint(2.6,0,2.6);
$points->InsertNextPoint(0,2,4);
$points->InsertNextPoint(1.4,1.4,4);
$points->InsertNextPoint(2,0,4);
$faces = Graphics::VTK::CellArray->new;
$faces->InsertNextCell(3);
$faces->InsertCellPoint(0);
$faces->InsertCellPoint(3);
$faces->InsertCellPoint(4);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(0);
$faces->InsertCellPoint(4);
$faces->InsertCellPoint(1);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(1);
$faces->InsertCellPoint(4);
$faces->InsertCellPoint(5);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(1);
$faces->InsertCellPoint(5);
$faces->InsertCellPoint(2);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(2);
$faces->InsertCellPoint(5);
$faces->InsertCellPoint(6);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(3);
$faces->InsertCellPoint(7);
$faces->InsertCellPoint(8);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(3);
$faces->InsertCellPoint(8);
$faces->InsertCellPoint(4);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(4);
$faces->InsertCellPoint(8);
$faces->InsertCellPoint(9);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(4);
$faces->InsertCellPoint(9);
$faces->InsertCellPoint(5);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(5);
$faces->InsertCellPoint(9);
$faces->InsertCellPoint(10);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(5);
$faces->InsertCellPoint(10);
$faces->InsertCellPoint(6);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(6);
$faces->InsertCellPoint(10);
$faces->InsertCellPoint(11);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(7);
$faces->InsertCellPoint(12);
$faces->InsertCellPoint(8);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(8);
$faces->InsertCellPoint(12);
$faces->InsertCellPoint(13);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(8);
$faces->InsertCellPoint(13);
$faces->InsertCellPoint(9);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(9);
$faces->InsertCellPoint(13);
$faces->InsertCellPoint(14);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(9);
$faces->InsertCellPoint(14);
$faces->InsertCellPoint(10);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(10);
$faces->InsertCellPoint(14);
$faces->InsertCellPoint(15);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(10);
$faces->InsertCellPoint(15);
$faces->InsertCellPoint(11);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(12);
$faces->InsertCellPoint(16);
$faces->InsertCellPoint(13);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(13);
$faces->InsertCellPoint(16);
$faces->InsertCellPoint(17);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(13);
$faces->InsertCellPoint(17);
$faces->InsertCellPoint(14);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(14);
$faces->InsertCellPoint(17);
$faces->InsertCellPoint(18);
$faces->InsertNextCell(3);
$faces->InsertCellPoint(14);
$faces->InsertCellPoint(18);
$faces->InsertCellPoint(15);
$model = Graphics::VTK::PolyData->new;
$model->SetPolys($faces);
$model->SetPoints($points);
$rn = Graphics::VTK::Math->new;
$cellColors = Graphics::VTK::UnsignedCharArray->new;
$cellColors->SetNumberOfComponents(3);
$cellColors->SetNumberOfTuples($model->GetNumberOfCells);
for ($i = 0; $i < $model->GetNumberOfCells; $i += 1)
 {
  $cellColors->InsertComponent($i,0,$rn->Random(100,255));
  $cellColors->InsertComponent($i,1,$rn->Random(100,255));
  $cellColors->InsertComponent($i,2,$rn->Random(100,255));
 }
$cellScalars = Graphics::VTK::Scalars->new;
$cellScalars->SetData($cellColors);
$model->GetCellData->SetScalars($cellScalars);
$t0 = Graphics::VTK::Transform->new;
$t0->Identity;
$tf0 = Graphics::VTK::TransformPolyDataFilter->new;
$tf0->SetTransform($t0);
$tf0->SetInput($model);
$t1 = Graphics::VTK::Transform->new;
$t1->Identity;
$t1->RotateZ(90);
$tf1 = Graphics::VTK::TransformPolyDataFilter->new;
$tf1->SetTransform($t1);
$tf1->SetInput($model);
$t2 = Graphics::VTK::Transform->new;
$t2->Identity;
$t2->RotateZ(180);
$tf2 = Graphics::VTK::TransformPolyDataFilter->new;
$tf2->SetTransform($t2);
$tf2->SetInput($model);
$t3 = Graphics::VTK::Transform->new;
$t3->Identity;
$t3->RotateZ(270);
$tf3 = Graphics::VTK::TransformPolyDataFilter->new;
$tf3->SetTransform($t3);
$tf3->SetInput($model);
$af = Graphics::VTK::AppendPolyData->new;
$af->AddInput($tf0->GetOutput);
$af->AddInput($tf1->GetOutput);
$af->AddInput($tf2->GetOutput);
$af->AddInput($tf3->GetOutput);
$t4 = Graphics::VTK::Transform->new;
$t4->Identity;
$t4->RotateX(180);
$tf4 = Graphics::VTK::TransformPolyDataFilter->new;
$tf4->SetTransform($t4);
$tf4->SetInput($af->GetOutput);
$af2 = Graphics::VTK::AppendPolyData->new;
$af2->AddInput($af->GetOutput);
$af2->AddInput($tf4->GetOutput);
$t5 = Graphics::VTK::Transform->new;
$t5->Identity;
$t5->Translate(0,0,-8);
$tf5 = Graphics::VTK::TransformPolyDataFilter->new;
$tf5->SetTransform($t5);
$tf5->SetInput($af2->GetOutput);
$af3 = Graphics::VTK::AppendPolyData->new;
$af3->AddInput($af2->GetOutput);
$af3->AddInput($tf5->GetOutput);
$t6 = Graphics::VTK::Transform->new;
$t6->Identity;
$t6->Translate(0,-8,0);
$tf6 = Graphics::VTK::TransformPolyDataFilter->new;
$tf6->SetTransform($t6);
$tf6->SetInput($af3->GetOutput);
$af4 = Graphics::VTK::AppendPolyData->new;
$af4->AddInput($af3->GetOutput);
$af4->AddInput($tf6->GetOutput);
# Create the RenderWindow, Renderer and both Actors
$ren1 = Graphics::VTK::Renderer->new;
$renWin = Graphics::VTK::RenderWindow->new;
$renWin->AddRenderer($ren1);
$iren = Graphics::VTK::RenderWindowInteractor->new;
$iren->SetRenderWindow($renWin);
$clean = Graphics::VTK::CleanPolyData->new;
$clean->SetTolerance('.001');
$clean->SetInput($model);
$clean->SetInput($af2->GetOutput);
$clean->SetInput($af3->GetOutput);
$clean->SetInput($af4->GetOutput);
$subdivide = Graphics::VTK::ButterflySubdivisionFilter->new;
$subdivide->SetInput($clean->GetOutput);
$subdivide->SetNumberOfSubdivisions(3);
$mapper = Graphics::VTK::DataSetMapper->new;
$mapper->SetInput($subdivide->GetOutput);
$surface = Graphics::VTK::Actor->new;
$surface->SetMapper($mapper);
$fe = Graphics::VTK::FeatureEdges->new;
$fe->SetInput($subdivide->GetOutput);
$fe->SetFeatureAngle(100);
$feStripper = Graphics::VTK::Stripper->new;
$feStripper->SetInput($fe->GetOutput);
$feTubes = Graphics::VTK::TubeFilter->new;
$feTubes->SetInput($feStripper->GetOutput);
$feTubes->SetRadius('.1');
$feMapper = Graphics::VTK::PolyDataMapper->new;
$feMapper->SetInput($feTubes->GetOutput);
$edges = Graphics::VTK::Actor->new;
$edges->SetMapper($feMapper);
# Add the actors to the renderer, set the background and size
$ren1->AddActor($surface);
$ren1->AddActor($edges);
$backP = Graphics::VTK::Property->new;
$backP->SetDiffuseColor(1,1,'.3');
$surface->SetBackfaceProperty($backP);
$edges->GetProperty->SetDiffuseColor('.2','.2','.2');
$surface->GetProperty->SetDiffuseColor(1,'.4','.3');
$surface->GetProperty->SetSpecular('.4');
$surface->GetProperty->SetDiffuse('.8');
$surface->GetProperty->SetSpecularPower(40);
$ren1->SetBackground(0.1,0.2,0.4);
$renWin->SetSize(300,300);
# render the image
$iren->SetUserMethod(
 sub
  {
   $MW->{'.vtkInteract'}->deiconify;
  }
);
$cam1 = $ren1->GetActiveCamera;
$cam1->Azimuth(90);
$ren1->ResetCamera;
$cam1->Zoom(1.5);
$iren->Initialize;
$renWin->SetFileName("schwarz.tcl.ppm");
#renWin SaveImageAsPPM
# prevent the tk window from showing up then start the event loop
$MW->withdraw;
Graphics::VTK::Tk::vtkInt::vtkInteract($MW);

Tk->MainLoop;
