MechSys  1.0
Computing library for simulations in continuum and discrete mechanics
/home/dorival/mechsys/lib/vtk/triangulate.h
Go to the documentation of this file.
00001 /************************************************************************
00002  * MechSys - Open Library for Mechanical Systems                        *
00003  * Copyright (C) 2005 Dorival M. Pedroso, Raúl D. D. Farfan             *
00004  *                                                                      *
00005  * This program is free software: you can redistribute it and/or modify *
00006  * it under the terms of the GNU General Public License as published by *
00007  * the Free Software Foundation, either version 3 of the License, or    *
00008  * any later version.                                                   *
00009  *                                                                      *
00010  * This program is distributed in the hope that it will be useful,      *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
00013  * GNU General Public License for more details.                         *
00014  *                                                                      *
00015  * You should have received a copy of the GNU General Public License    *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>  *
00017  ************************************************************************/
00018 
00019 #ifndef MECHSYS_TRIANGULATE_H
00020 #define MECHSYS_TRIANGULATE_H
00021 
00022 // VTK
00023 #include <vtkPoints.h>
00024 #include <vtkPolyData.h>
00025 #include <vtkDelaunay3D.h>
00026 #include <vtkUnstructuredGridWriter.h>
00027 
00028 // MechSys
00029 #include <mechsys/vtk/sgrid.h>
00030 #include <mechsys/util/array.h>
00031 
00032 namespace VTK
00033 {
00034 
00035 void Triangulate (double const * X, double const * Y, double const * Z, size_t Size, char const * Filename, double Tol=1.0e-3)
00036 {
00037     // check
00038     if (Size<1) throw new Fatal("VTK::Triangulate: Size of arrays must be greater than zero");
00039 
00040     // create VTK points
00041     vtkPoints * points = vtkPoints::New();
00042     points->Allocate (Size);
00043     for (size_t i=0; i<Size; ++i) points->InsertPoint (i, X[i], Y[i], Z[i]);
00044 
00045     // Create a 3D triangulation
00046     //   - The Tolerance is the distance that nearly coincident points are merged together.
00047     //   - Delaunay does better if points are well spaced.
00048     //   - The alpha value is the radius of circumcircles, circumspheres.
00049     //     Any mesh entity whose circumcircle is smaller than this value is output.
00050     vtkPolyData   * vertices = vtkPolyData   ::New();
00051     vtkDelaunay3D * delaunay = vtkDelaunay3D ::New();
00052     vertices -> SetPoints    (points);
00053     delaunay -> SetInput     (vertices);
00054     delaunay -> SetTolerance (Tol);
00055     delaunay -> SetAlpha     (0.2);
00056     delaunay -> BoundingTriangulationOff();
00057 
00058     // Write file
00059     vtkUnstructuredGridWriter * writer = vtkUnstructuredGridWriter ::New();
00060     writer -> SetInputConnection (delaunay->GetOutputPort());
00061     writer -> SetFileName        (Filename);
00062     writer -> Write              ();
00063 
00064     // Clean up
00065     points   -> Delete();
00066     vertices -> Delete();
00067     delaunay -> Delete();
00068     writer   -> Delete();
00069 }
00070 
00071 void Triangulate (Array<double> const & X, Array<double> const & Y, Array<double> const & Z, char const * Filename, double Tol=1.0e-3)
00072 {
00073     if (X.Size()<1)         throw new Fatal("VTK::Triangulate: Size of arrays must be greater than zero");
00074     if (X.Size()!=Y.Size()) throw new Fatal("VTK::Triangulate: X, Y, and Z arrays must have the same size");
00075     if (X.Size()!=Z.Size()) throw new Fatal("VTK::Triangulate: X, Y, and Z arrays must have the same size");
00076     Triangulate (X.GetPtr(), Y.GetPtr(), Z.GetPtr(), X.Size(), Filename, Tol);
00077 }
00078 
00079 void Triangulate (VTK::SGrid & G, char const * Filename, double Tol=1.0e-3)
00080 {
00081     // Create a 3D triangulation
00082     //   - The Tolerance is the distance that nearly coincident points are merged together.
00083     //   - Delaunay does better if points are well spaced.
00084     //   - The alpha value is the radius of circumcircles, circumspheres.
00085     //     Any mesh entity whose circumcircle is smaller than this value is output.
00086     vtkPolyData   * vertices = vtkPolyData   ::New();
00087     vtkDelaunay3D * delaunay = vtkDelaunay3D ::New();
00088     vertices -> SetPoints    (G.GetPoints());
00089     delaunay -> SetInput     (vertices);
00090     delaunay -> SetTolerance (Tol);
00091     delaunay -> SetAlpha     (0.2);
00092     delaunay -> BoundingTriangulationOff();
00093 
00094     // Write file
00095     vtkUnstructuredGridWriter * writer = vtkUnstructuredGridWriter ::New();
00096     writer -> SetInputConnection (delaunay->GetOutputPort());
00097     writer -> SetFileName        (Filename);
00098     writer -> Write              ();
00099 
00100     // Clean up
00101     vertices -> Delete();
00102     delaunay -> Delete();
00103     writer   -> Delete();
00104 }
00105 
00106 }; // namespace VTK
00107 
00108 #endif // MECHSYS_TRIANGULATE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines