structgrid.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_STRUCTGRID_H
00022 #define VTKWRAP_STRUCTGRID_H
00023 
00024 #include "numerical/meshgrid.h"
00025 
00026 #include "vtkPoints.h"
00027 #include "vtkFloatArray.h"
00028 #include "vtkStructuredGrid.h"
00029 #include "vtkPointData.h"
00030 #include "vtkStructuredGridWriter.h"
00031 
00032 class StructGrid // Porco-espinho
00033 {
00034     friend std::ostream & operator<< (std::ostream & os, StructGrid const & sg);
00035 public:
00036     struct VectorTuple
00037     {
00038         double vx;
00039         double vy;
00040         double vz;
00041     };
00042     StructGrid(double const X[], int nX,
00043                double const Y[], int nY,
00044                double const Z[], int nZ,
00045                double const F[]=NULL, VectorTuple const V[]=NULL) // arrays size must be equal to nX*nY*nZ
00046     {
00047         // Create VTK points, scalars and vectors
00048         _points  = vtkPoints              ::New();
00049         _scalars = vtkFloatArray          ::New();
00050         _vectors = vtkFloatArray          ::New();
00051         _points  -> Allocate              (nX*nY*nZ);
00052         _scalars -> Allocate              (nX*nY*nZ);
00053         _vectors -> SetNumberOfComponents (3);
00054         _vectors -> SetNumberOfTuples     (nX*nY*nZ);
00055 
00056         // Fill structures
00057         for (int i=0; i<nX*nY*nZ; ++i)
00058         {
00059             _points -> InsertPoint(i, X[i], Y[i], Z[i]);
00060             if (F==NULL) _scalars -> InsertTuple1(i, i);
00061             else         _scalars -> InsertTuple1(i, F[i]);
00062             if (V==NULL) _vectors -> InsertTuple3(i, 1.0, 1.0, 1.0); // point along hydro-axis
00063             else         _vectors -> InsertTuple3(i, V[i].vx, V[i].vy, V[i].vz);
00064         }
00065 
00066         // Create VTK StructuredGrid
00067         _sgrid = vtkStructuredGrid ::New();
00068         _sgrid -> SetDimensions    (nX, nY, nZ);
00069         _sgrid -> SetPoints        (_points);
00070         _sgrid -> GetPointData     () -> SetScalars(_scalars);
00071         _sgrid -> GetPointData     () -> SetVectors(_vectors);
00072     }
00073     StructGrid(MeshGrid const & mg, double const F[]=NULL, VectorTuple const V[]=NULL) // F size must be equal to mg.Length()
00074     {
00075         // Create VTK points, scalars and vectors
00076         _points  = vtkPoints              ::New();
00077         _scalars = vtkFloatArray          ::New();
00078         _vectors = vtkFloatArray          ::New();
00079         _points  -> Allocate              (mg.Length()); // mg.Length() == mg.nX()*mg.nY()*mg.nZ()
00080         _scalars -> Allocate              (mg.Length());
00081         _vectors -> SetNumberOfComponents (3);
00082         _vectors -> SetNumberOfTuples     (mg.Length());
00083 
00084         // Fill structures
00085         for (int i=0; i<mg.Length(); ++i)
00086         {
00087             _points -> InsertPoint(i, mg.X(i), mg.Y(i), mg.Z(i));
00088             if (F==NULL) _scalars -> InsertTuple1(i, i);
00089             else         _scalars -> InsertTuple1(i, F[i]);
00090             if (V==NULL) _vectors -> InsertTuple3(i, 1.0, 1.0, 1.0); // point along hydro-axis
00091             else         _vectors -> InsertTuple3(i, V[i].vx, V[i].vy, V[i].vz);
00092         }
00093 
00094         // Create VTK StructuredGrid
00095         _sgrid = vtkStructuredGrid ::New();
00096         _sgrid -> SetDimensions    (mg.nX(), mg.nY(), mg.nZ());
00097         _sgrid -> SetPoints        (_points);
00098         _sgrid -> GetPointData     () -> SetScalars(_scalars);
00099         _sgrid -> GetPointData     () -> SetVectors(_vectors);
00100     }
00101     ~StructGrid()
00102     {
00103         _points  -> Delete();
00104         _scalars -> Delete();
00105         _vectors -> Delete();
00106         _sgrid   -> Delete();
00107     }
00108     vtkStructuredGrid * GetGrid() { return _sgrid; }
00109     void WriteFile(char const * Filename)
00110     {
00111         vtkStructuredGridWriter * writer = vtkStructuredGridWriter::New();
00112         writer -> SetInput(_sgrid);
00113         writer -> SetFileName(Filename);
00114         writer -> Write();
00115     }
00116 private:
00117     vtkPoints         * _points;
00118     vtkFloatArray     * _scalars;
00119     vtkFloatArray     * _vectors;
00120     vtkStructuredGrid * _sgrid;
00121 }; // class StructGrid
00122 
00123 std::ostream & operator<< (std::ostream & os, StructGrid const & sg)
00124 {
00125     sg._sgrid->Print(os);
00126     return os;
00127 }
00128 
00129 #endif // VTKWRAP_STRUCTGRID_H
00130 
00131 // vim:fdm=marker

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