Dear vmtk users,

I have put together two pieces of code from other classes in vmtk and created a new class for reading tetgen .node/.ele tetra meshes. However, the resulting vtkUnstructuredGrid is not yet correct: nodes are fine but cells are not

could someone please pay a look to it?
Luca, besides the python script, is there any other way of reading a tetgen mesh into a vtk unstructured grid?

thank you,
sebastian



/*=========================================================================
                                                                                                                                    
Program:   VMTK
Module:    $RCSfile: vtkvmtkTetGenReader.cxx,v $
Language:  C++
Date:      $Date: 2006/04/06 16:47:47 $
Version:   $Revision: 1.8 $
                                                                                                                                    
  Copyright (c) Luca Antiga, David Steinman. All rights reserved.
  See LICENCE file for details.

  Portions of this code are covered under the VTK copyright.
  See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm 
  for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.
                                                                                                                                    
=========================================================================*/

#include "vtkvmtkTetGenReader.h"
#include "vtkDoubleArray.h"
#include "vtkIntArray.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkCellData.h"
#include "vtkCellArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnstructuredGrid.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkvmtkConstants.h"
#include "tetgen.h"

vtkCxxRevisionMacro(vtkvmtkTetGenReader, "$Revision: 1.8 $");
vtkStandardNewMacro(vtkvmtkTetGenReader);

vtkvmtkTetGenReader::vtkvmtkTetGenReader()
{
  this->SingleCellDataEntityArrayName = NULL;
  this->OutputSurfaceElements = 1;
  this->OutputVolumeElements = 1;
  this->Order = 1;
}

vtkvmtkTetGenReader::~vtkvmtkTetGenReader()
{
  if (this->SingleCellDataEntityArrayName)
    {
    delete[] this->SingleCellDataEntityArrayName;
    this->SingleCellDataEntityArrayName = NULL;
    }
}

int vtkvmtkTetGenReader::RequestData(
  vtkInformation *request,
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));

  if (outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) > 0)
  {
  return 1;
  }

  if (!this->FileName)
  {
  vtkErrorMacro(<<"FileName not set.");
  return 1;
  }

  tetgenio in_tetgenio;

  in_tetgenio.load_tetmesh(this->FileName);

  vtkPoints* outputPoints = vtkPoints::New();
  outputPoints->SetNumberOfPoints(in_tetgenio.numberofpoints);
  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): number of points found: " << in_tetgenio.numberofpoints);

  double point[3];
  for (int i=0; i<in_tetgenio.numberofpoints; i++)
    {
    point[0] = in_tetgenio.pointlist[in_tetgenio.mesh_dim * i + 0];
    point[1] = in_tetgenio.pointlist[in_tetgenio.mesh_dim * i + 1];
    point[2] = in_tetgenio.pointlist[in_tetgenio.mesh_dim * i + 2];
    outputPoints->SetPoint(i,point);
    }

  output->SetPoints(outputPoints);

  vtkCellArray* outputCellArray = vtkCellArray::New();

  int numberOfOutputCells = 0;

  if (this->OutputVolumeElements)
    {
    numberOfOutputCells += in_tetgenio.numberoftetrahedra;
    }

  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): number of tetrahedra found: "
      << in_tetgenio.numberoftetrahedra);

  if (this->OutputSurfaceElements)
    {
    numberOfOutputCells += in_tetgenio.numberoftrifaces;
    }

  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): number of trifaces found: "
      << in_tetgenio.numberoftrifaces);

  int* outputCellTypes = new int[numberOfOutputCells];

  vtkIntArray* outputCellMarkerArray = vtkIntArray::New();
  outputCellMarkerArray->SetNumberOfTuples(numberOfOutputCells);
  outputCellMarkerArray->SetName(this->SingleCellDataEntityArrayName);
  outputCellMarkerArray->FillComponent(0,0.0);

  int cellIdOffset = 0;

  #if 1
  if (this->OutputVolumeElements)
    {

    int outputCellType;
    switch (this->Order)
      {
      case 1:
        outputCellType = VTK_TETRA;
        break;
      case 2:
        outputCellType = VTK_QUADRATIC_TETRA;
        break;
      default:
        outputCellType = VTK_TETRA;
      }

    for (int i=0; i<in_tetgenio.numberoftetrahedra; i++)
      {
      outputCellArray->InsertNextCell(in_tetgenio.numberofcorners);
      for (int j=0; j<in_tetgenio.numberofcorners; j++)
        {
        outputCellArray->InsertCellPoint(in_tetgenio.tetrahedronlist[i*in_tetgenio.numberofcorners + j]);
        }
      outputCellTypes[cellIdOffset+i] = outputCellType;
      }

    cellIdOffset = in_tetgenio.numberoftetrahedra;
    }

  if (this->OutputSurfaceElements)
    {
    int outputCellType = VTK_TRIANGLE;
    int numberOfTrifaceCorners = 3;

    for (int i=0; i<in_tetgenio.numberoftrifaces; i++)
      {
      outputCellArray->InsertNextCell(numberOfTrifaceCorners);
      for (int j=0; j<numberOfTrifaceCorners; j++)
        {
        outputCellArray->InsertCellPoint(in_tetgenio.trifacelist[i*numberOfTrifaceCorners + j]);
        }
      outputCellTypes[cellIdOffset+i] = outputCellType;
      outputCellMarkerArray->SetValue(cellIdOffset+i,in_tetgenio.trifacemarkerlist[i]);
      }

    cellIdOffset = in_tetgenio.numberoftrifaces;
    }
  #else
  if (this->OutputSurfaceElements)
    {
    int outputCellType = VTK_TRIANGLE;
    int numberOfTrifaceCorners = 3;

    for (i=0; i<in_tetgenio.numberoftrifaces; i++)
      {
      outputCellArray->InsertNextCell(numberOfTrifaceCorners);
      for (int j=0; j<numberOfTrifaceCorners; j++)
        {
        outputCellArray->InsertCellPoint(in_tetgenio.trifacelist[i*numberOfTrifaceCorners + j]);
        }
      outputCellTypes[cellIdOffset+i] = outputCellType;
      outputCellMarkerArray->SetValue(cellIdOffset+i,in_tetgenio.trifacemarkerlist[i]+1);
      }

    cellIdOffset = in_tetgenio.numberoftrifaces;
    }

  if (this->OutputVolumeElements)
    {
    int outputCellType;
    switch (this->Order)
      {
      case 1:
        outputCellType = VTK_TETRA;
        break;
      case 2:
        outputCellType = VTK_QUADRATIC_TETRA;
        break;
      default:
        outputCellType = VTK_TETRA;
      }

    for (i=0; i<in_tetgenio.numberoftetrahedra; i++)
      {
      outputCellArray->InsertNextCell(in_tetgenio.numberofcorners);
      for (int j=0; j<in_tetgenio.numberofcorners; j++)
        {
        outputCellArray->InsertCellPoint(in_tetgenio.tetrahedronlist[i*in_tetgenio.numberofcorners + j]);
        }
      outputCellTypes[cellIdOffset+i] = outputCellType;
      }

    cellIdOffset = in_tetgenio.numberoftetrahedra;
    }
  #endif

  output->SetCells(outputCellTypes,outputCellArray);

  output->GetCellData()->AddArray(outputCellMarkerArray);

  outputPoints->Delete();
  outputCellArray->Delete();
  delete[] outputCellTypes;

  outputCellMarkerArray->Delete();

  return 1;
}

void vtkvmtkTetGenReader::PrintSelf(ostream& os, vtkIndent indent)
{
  Superclass::PrintSelf(os,indent);
}
/*=========================================================================
                                                                                                                                    
Program:   VMTK
Module:    $RCSfile: vtkvmtkTetGenReader.h,v $
Language:  C++
Date:      $Date: 2006/04/06 16:47:47 $
Version:   $Revision: 1.6 $
                                                                                                                                    
  Copyright (c) Luca Antiga, David Steinman. All rights reserved.
  See LICENCE file for details.

  Portions of this code are covered under the VTK copyright.
  See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm 
  for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.
                                                                                                                                    
=========================================================================*/

// .NAME vtkvmtkTetGenReader -
// .SECTION Description
// vtkvmtkTetGenReader reads unstructured grid data from Fidap FDNEUT format

// .SECTION See Also
// vtkvmtkFDNEUTWriter

#ifndef __vtkvmtkTetGenReader_h
#define __vtkvmtkTetGenReader_h

#include "vtkvmtkWin32Header.h"
#include "vtkUnstructuredGridReader.h"

class VTK_VMTK_IO_EXPORT vtkvmtkTetGenReader : public vtkUnstructuredGridReader
{
  public:
  vtkTypeRevisionMacro(vtkvmtkTetGenReader,vtkUnstructuredGridReader);
  void PrintSelf(ostream& os, vtkIndent indent);

  static vtkvmtkTetGenReader *New();

  vtkSetStringMacro(SingleCellDataEntityArrayName);
  vtkGetStringMacro(SingleCellDataEntityArrayName);

  vtkSetMacro(OutputVolumeElements,int);
  vtkGetMacro(OutputVolumeElements,int);
  vtkBooleanMacro(OutputVolumeElements,int);

  vtkSetMacro(OutputSurfaceElements,int);
  vtkGetMacro(OutputSurfaceElements,int);
  vtkBooleanMacro(OutputSurfaceElements,int);

  protected:
  vtkvmtkTetGenReader();
  ~vtkvmtkTetGenReader();

  virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);

  char* SingleCellDataEntityArrayName;

  int OutputVolumeElements;
  int OutputSurfaceElements;
  int Order;

  private:
  vtkvmtkTetGenReader(const vtkvmtkTetGenReader&);  // Not implemented.
  void operator=(const vtkvmtkTetGenReader&);  // Not implemented.
};

#endif
------------------------------------------------------------------------------
The ultimate all-in-one performance toolkit: Intel(R) Parallel Studio XE:
Pinpoint memory and threading errors before they happen.
Find and fix more than 250 security defects in the development cycle.
Locate bottlenecks in serial and parallel code that limit performance.
http://p.sf.net/sfu/intel-dev2devfeb
_______________________________________________
vmtk-users mailing list
vmtk-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vmtk-users

Reply via email to