[vtkusers] GTS Wrapper?

HIRAKI Hideaki hhiraki at lab.nig.ac.jp
Tue Jul 21 02:21:50 EDT 2009


On Sat, 18 Jul 2009, hardc0rps wrote:
> I was just wondering if anyone has successfully wrapped the Gnu Triangulated
> Surface Library (GTS - http://gts.sourceforge.net) into VTK (with boolean
> operations between vtkpolydata in mind)?

What do you mean by "wrap"? The attached is what I wrote to convert between
vtkPolyData and GTS surface a few years ago. I hope this helps.
-------------- next part --------------
extern "C" {
#include "glib.h"
#include "gts.h"
extern GtsSurface * read_vtk (gchar *);
extern void write_vtk (GtsSurface *, gchar *);
}
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkPolyDataReader.h"
#include "vtkTriangleFilter.h"
#include "vtkPolyDataWriter.h"
#ifndef vtkFloatingPointType
#define vtkFloatingPointType float
#endif

void vtk2gts (vtkPolyData * input, GtsSurface * output)
{
  GtsVertex ** vertices;

  g_return_if_fail(output != NULL);
  if (g_hash_table_size(output->faces) != 0)
    g_warning("overwriting surface");
  input->Update();
  input->BuildLinks();

  vertices = (GtsVertex **)
    g_malloc(input->GetNumberOfPoints() * sizeof(GtsVertex *));
  for (vtkIdType i = 0; i < input->GetNumberOfPoints(); ++i) {
    vtkFloatingPointType x[3];
    input->GetPoint(i, x);
    vertices[i] = gts_vertex_new(output->vertex_class, x[0], x[1], x[2]);
  }
  for (vtkIdType i = 0; i < input->GetNumberOfCells(); ++i) {
    vtkIdType npts, * pts;
    input->GetCellPoints(i, npts, pts);
    if (npts != 3) {
      g_warning("skipping no triangle cell");
      continue;
    }
    GtsEdge * newedges[3];
    for (gint j = 0; j < 3; ++j) {
      vtkIdType v1 = pts[j], v2 = pts[(j+1) % 3];
      newedges[j] = gts_edge_new(output->edge_class,
                                 vertices[v1], vertices[v2]);
      if (gts_segment_is_duplicate(GTS_SEGMENT(newedges[j]))) {
        GtsSegment * tmp = gts_segment_is_duplicate(GTS_SEGMENT(newedges[j]));
        gts_object_destroy(GTS_OBJECT(newedges[j]));
        newedges[j] = GTS_EDGE(tmp);
        g_return_if_fail(gts_segment_is_duplicate(GTS_SEGMENT(newedges[j])) ==
                         NULL);
      }
    }
    gts_surface_add_face(output,
                         gts_face_new(output->face_class,
                                      newedges[0], newedges[1], newedges[2]));
  }
  g_free(vertices);
}

static void gts2vtk_insert_point (GtsPoint * p, vtkPoints * points)
{
  points->InsertNextPoint(p->x, p->y, p->z);
  GTS_OBJECT(p)->reserved = GUINT_TO_POINTER(points->GetNumberOfPoints() - 1);
}
static void gts2vtk_insert_triangle (GtsTriangle * t, vtkCellArray * polys)
{
  GtsVertex * v1, * v2, * v3;
  gts_triangle_vertices(t, &v1, &v2, &v3);
  polys->InsertNextCell(3);
  polys->InsertCellPoint(GPOINTER_TO_UINT(GTS_OBJECT(v1)->reserved));
  polys->InsertCellPoint(GPOINTER_TO_UINT(GTS_OBJECT(v2)->reserved));
  polys->InsertCellPoint(GPOINTER_TO_UINT(GTS_OBJECT(v3)->reserved));
}
void gts2vtk (GtsSurface * input, vtkPolyData * output)
{
  output->UpdateInformation();
  if (output->GetNumberOfPoints() != 0)
    g_warning("overwriting polydata");

  vtkPoints *points = vtkPoints::New();
  vtkCellArray *polys = vtkCellArray::New();
  gts_surface_foreach_vertex(input, (GtsFunc) &gts2vtk_insert_point, points);
  gts_surface_foreach_face(input, (GtsFunc) &gts2vtk_insert_triangle, polys);
  gts_surface_foreach_vertex(input, (GtsFunc) gts_object_reset_reserved, NULL);
  output->SetPoints(points);
  points->Delete();
  output->SetPolys(polys);
  polys->Delete();
}

GtsSurface * read_vtk (gchar * filename)
{
  GtsSurface * s = gts_surface_new(gts_surface_class(), gts_face_class(),
                                   gts_edge_class(), gts_vertex_class());
  vtkPolyDataReader * reader = vtkPolyDataReader::New();
  reader->SetFileName(filename);
  vtkTriangleFilter * triangle = vtkTriangleFilter::New();
  triangle->SetInput(reader->GetOutput());
  vtk2gts(triangle->GetOutput(), s);
  triangle->Delete();
  reader->Delete();
  return s;
}

void write_vtk (GtsSurface * s, gchar * filename)
{
  vtkPolyData * output = vtkPolyData::New();
  gts2vtk(s, output);
  vtkPolyDataWriter * writer = vtkPolyDataWriter::New();
  writer->SetFileTypeToBinary();
  writer->SetFileName(filename);
  writer->SetInput(output);
  writer->Write();
  writer->Delete();
  output->Delete();
}
-------------- next part --------------
extern "C" {
#include <stdio.h>
#include "gts.h"
extern GtsSurface * read_vtk(gchar *);
extern void write_vtk(GtsSurface *, gchar *);
}

int main(int argc, char * argv[]){
  if (argc != 3) {
    fprintf(stderr, "Usage: %s input.vtk output.vtk > output.gts\n", argv[0]);
    return 1;
  }
  GtsSurface * s = read_vtk(argv[1]);
  gts_surface_write(s, stdout);
  write_vtk(s, argv[2]);
  return 0;
}


More information about the vtkusers mailing list