triangulate.h

00001 /***********************************************************************************
00002  * VTKwrap - Simple VTK wrappers                                                   *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>     *
00004  *                                                                                 *
00005  * This file is part of VTKwrap.                                                   *
00006  *                                                                                 *
00007  * VTKwrap is free software; you can redistribute it and/or modify it under        *
00008  * the  terms of the GNU General Public License as published by the Free Software  *
00009  * Foundation; either version 2 of the License, or (at your option) any later      *
00010  * version.                                                                        *
00011  *                                                                                 *
00012  * VTKwrap is distributed in the hope that it will be useful, but WITHOUT ANY      *
00013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
00014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.        *
00015  *                                                                                 *
00016  * You should have received a copy of the GNU General Public License along with    *
00017  * VTKwrap; if not, write to the Free Software Foundation, Inc., 51 Franklin       *
00018  * Street, Fifth Floor, Boston, MA 02110-1301, USA                                 *
00019  ***********************************************************************************/
00020 
00021 #ifndef VTKWRAP_TRIANGULATE_H
00022 #define VTKWRAP_TRIANGULATE_H
00023 
00024 #include <cassert>
00025 #include <vector>
00026 
00027 #include "numerical/meshgrid.h"
00028 
00029 #include "vtkPoints.h"
00030 #include "vtkPolyData.h"
00031 #include "vtkDelaunay3D.h"
00032 #include "vtkUnstructuredGridWriter.h"
00033 
00034 class Triangulate
00035 {
00036 public:
00037     Triangulate(double const * X,
00038                 double const * Y,
00039                 double const * Z, int Size, char const * Filename)
00040         : _use_vector(false), _npoints(Size), _filename(Filename), _X(X), _Y(Y), _Z(Z)
00041     {
00042         assert(Size>0);
00043     }
00044     Triangulate(std::vector<double> & X,
00045                 std::vector<double> & Y,
00046                 std::vector<double> & Z, char const * Filename)
00047         : _use_vector(true), _npoints(X.size()), _filename(Filename), _X(NULL), _Y(NULL), _Z(NULL), _vX(X), _vY(Y), _vZ(Z)
00048     {
00049         assert(X.size()>0);
00050         assert(X.size()==Y.size());
00051         assert(Y.size()==Z.size());
00052     }
00053     void Start();
00054 private:
00055     bool                  _use_vector;
00056     int                   _npoints;
00057     char const          * _filename;
00058     double const        * _X; 
00059     double const        * _Y; 
00060     double const        * _Z; 
00061     std::vector<double>   _vX;
00062     std::vector<double>   _vY;
00063     std::vector<double>   _vZ;
00064 }; // class Triangulate
00065 
00066 
00068 
00069 
00070 inline void Triangulate::Start() // {{{
00071 {
00072     // Create VTK points
00073     vtkPoints * points = vtkPoints::New();
00074     points->Allocate(_npoints);
00075     if (_use_vector)
00076     {
00077         for (int i=0; i<_npoints; ++i)
00078         {
00079             double P[3] = {_vX[i], _vY[i], _vZ[i]};
00080             points->InsertPoint(i,P);
00081         }
00082     }
00083     else 
00084     {
00085         for (int i=0; i<_npoints; ++i)
00086         {
00087             double P[3] = {_X[i], _Y[i], _Z[i]};
00088             points->InsertPoint(i,P);
00089         }
00090     }
00091 
00092     // Create a 3D triangulation
00093     //   - The Tolerance is the distance that nearly coincident points are merged together.
00094     //   - Delaunay does better if points are well spaced.
00095     //   - The alpha value is the radius of circumcircles, circumspheres.
00096     //     Any mesh entity whose circumcircle is smaller than this value is output.
00097     vtkPolyData   * vertices = vtkPolyData::New();
00098     vtkDelaunay3D * delaunay = vtkDelaunay3D::New();
00099     vertices -> SetPoints(points);
00100     delaunay -> SetInput(vertices);
00101     delaunay -> SetTolerance(0.01);
00102     //delaunay -> SetAlpha(0.2);
00103     delaunay -> BoundingTriangulationOff();
00104 
00105     // Write file
00106     vtkUnstructuredGridWriter * writer = vtkUnstructuredGridWriter::New();
00107     writer -> SetInputConnection(delaunay->GetOutputPort());
00108     writer -> SetFileName(_filename);
00109     writer -> Write();
00110 
00111     // Clean up
00112     points   -> Delete();
00113     vertices -> Delete();
00114     delaunay -> Delete();
00115     writer   -> Delete();
00116 } // }}}
00117 
00118 #endif // VTKWRAP_TRIANGULATE_H
00119 
00120 // vim:fdm=marker

Generated on Wed Jan 24 15:56:28 2007 for MechSys by  doxygen 1.4.7