[ create a new paste ] login | about

Link: http://codepad.org/OxnvsZe6    [ raw code | fork ]

C++, pasted on Jul 13:
#include <vtkPolyData.h>
#include <vtkLine.h>
#include <vtkSmartPointer.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolygon.h>
#include <vtkCellArray.h>

void PolygonFromContourClockwise(vtkPolyData* contour, vtkPolyData* polygonPolyData);
void PolygonFromContourCounterClockwise(vtkPolyData* contour, vtkPolyData* polygonPolyData);
void MakeContour(vtkPolyData*);

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkPolyData> polydataContour = vtkSmartPointer<vtkPolyData>::New();
  MakeContour(polydataContour);

  vtkSmartPointer<vtkPolyData> polydataPolygonClockwise = vtkSmartPointer<vtkPolyData>::New();
  PolygonFromContourClockwise(polydataContour, polydataPolygonClockwise);
  
  {
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("polygonClockwise.vtp");
  writer->SetInputConnection(polydataPolygonClockwise->GetProducerPort());
  writer->Write();
  }
  
  vtkSmartPointer<vtkPolyData> polydataPolygonCounterClockwise = vtkSmartPointer<vtkPolyData>::New();
  PolygonFromContourCounterClockwise(polydataContour, polydataPolygonCounterClockwise);
  
  {
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("polygonCounterClockwise.vtp");
  writer->SetInputConnection(polydataPolygonCounterClockwise->GetProducerPort());
  writer->Write();
  }
  
  return EXIT_SUCCESS;
}


void PolygonFromContourClockwise(vtkPolyData* contour, vtkPolyData* polygonPolyData)
{
  // Create a polygon from the contour
  vtkSmartPointer<vtkPolygon> polygon =
    vtkSmartPointer<vtkPolygon>::New();
  polygon->GetPointIds()->SetNumberOfIds(contour->GetNumberOfPoints());

  for(unsigned int i = 0; i < contour->GetNumberOfPoints(); ++i)
    {
    polygon->GetPointIds()->SetId(i, i);
    }
    
  // Add the polygon to a list of polygons
  vtkSmartPointer<vtkCellArray> polygons =
    vtkSmartPointer<vtkCellArray>::New();
  polygons->InsertNextCell(polygon);
 
  // Create a PolyData
  polygonPolyData->SetPoints(contour->GetPoints());
  polygonPolyData->SetPolys(polygons);    
}

void PolygonFromContourCounterClockwise(vtkPolyData* contour, vtkPolyData* polygonPolyData)
{
  // Create a polygon from the contour
  vtkSmartPointer<vtkPolygon> polygon =
    vtkSmartPointer<vtkPolygon>::New();
  polygon->GetPointIds()->SetNumberOfIds(contour->GetNumberOfPoints());

  for(int i = contour->GetNumberOfPoints() - 1; i >= 0; --i)
    {
    std::cout << "Adding point " << i << std::endl;
    polygon->GetPointIds()->SetId(i, i);
    }
    
  // Add the polygon to a list of polygons
  vtkSmartPointer<vtkCellArray> polygons =
    vtkSmartPointer<vtkCellArray>::New();
  polygons->InsertNextCell(polygon);
 
  // Create a PolyData
  polygonPolyData->SetPoints(contour->GetPoints());
  polygonPolyData->SetPolys(polygons);    
}

void MakeContour(vtkPolyData* polydata)
{
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(14, 7, 0);
  points->InsertNextPoint(15, 7, 0);
  points->InsertNextPoint(16, 8, 0);
  points->InsertNextPoint(16, 9, 0);
  points->InsertNextPoint(16, 10, 0);
  points->InsertNextPoint(15, 11, 0);
  points->InsertNextPoint(15, 12, 0);
  points->InsertNextPoint(15, 13, 0);
  points->InsertNextPoint(15, 14, 0);
  points->InsertNextPoint(15, 15, 0);
  points->InsertNextPoint(15, 16, 0);
  points->InsertNextPoint(15, 17, 0);
  points->InsertNextPoint(15, 18, 0);
  points->InsertNextPoint(14, 19, 0);
  points->InsertNextPoint(14, 20, 0);
  points->InsertNextPoint(15, 21, 0);
  points->InsertNextPoint(14, 22, 0);
  points->InsertNextPoint(13, 21, 0);
  points->InsertNextPoint(12, 21, 0);
  points->InsertNextPoint(11, 22, 0);
  points->InsertNextPoint(10, 21, 0);
  points->InsertNextPoint(10, 20, 0);
  points->InsertNextPoint(10, 19, 0);
  points->InsertNextPoint(10, 18, 0);
  points->InsertNextPoint(11, 17, 0);
  points->InsertNextPoint(11, 16, 0);
  points->InsertNextPoint(11, 15, 0);
  points->InsertNextPoint(11, 14, 0);
  points->InsertNextPoint(10, 13, 0);
  points->InsertNextPoint(9, 12, 0);
  points->InsertNextPoint(9, 11, 0);
  points->InsertNextPoint(9, 10, 0);
  points->InsertNextPoint(10, 9, 0);
  points->InsertNextPoint(9, 8, 0);
  points->InsertNextPoint(10, 8, 0);
  points->InsertNextPoint(11, 8, 0);
  points->InsertNextPoint(12, 8, 0);
  points->InsertNextPoint(13, 7, 0);
  
  // Create a polydata contour of the approximate outline
  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
  for(unsigned int i = 0; i < points->GetNumberOfPoints() - 1; ++i) // We use the i+1 element, so we must stop 1 before the end
    {
    vtkSmartPointer<vtkLine> line = vtkSmartPointer<vtkLine>::New();
    line->GetPointIds()->SetId(0,i);
    line->GetPointIds()->SetId(1,i+1);
    lines->InsertNextCell(line);
    
    std::cout << "Adding line between " << i << " and " << i+1 << std::endl;
    }
  
  // Close the loop
  vtkSmartPointer<vtkLine> line = vtkSmartPointer<vtkLine>::New();
  line->GetPointIds()->SetId(0, points->GetNumberOfPoints()-1);
  line->GetPointIds()->SetId(1,0);
  lines->InsertNextCell(line);

  polydata->SetPoints(points);
  polydata->SetLines(lines);
    
  vtkSmartPointer<vtkXMLPolyDataWriter> writer =
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("contour.vtp");
  writer->SetInputConnection(polydata->GetProducerPort());
  writer->Write();
}


Create a new paste based on this one


Comments: