00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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)
00046 {
00047
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
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);
00063 else _vectors -> InsertTuple3(i, V[i].vx, V[i].vy, V[i].vz);
00064 }
00065
00066
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)
00074 {
00075
00076 _points = vtkPoints ::New();
00077 _scalars = vtkFloatArray ::New();
00078 _vectors = vtkFloatArray ::New();
00079 _points -> Allocate (mg.Length());
00080 _scalars -> Allocate (mg.Length());
00081 _vectors -> SetNumberOfComponents (3);
00082 _vectors -> SetNumberOfTuples (mg.Length());
00083
00084
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);
00091 else _vectors -> InsertTuple3(i, V[i].vx, V[i].vy, V[i].vz);
00092 }
00093
00094
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 };
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