meshgrid.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_MESHGRID_H
00023 #define MECHSYS_MESHGRID_H
00024 
00025 #ifdef HAVE_CONFIG_H
00026   #include "config.h"
00027 #else
00028   #ifndef REAL
00029     #define REAL double
00030   #endif
00031 #endif
00032 
00033 #include <iostream>
00034 #include <cassert>
00035 
00036 class MeshGrid
00037 {
00038 public:
00039     MeshGrid(REAL StartX, REAL EndX, REAL StepX,
00040              REAL StartY, REAL EndY, REAL StepY);
00041 
00042     MeshGrid(REAL StartX, REAL EndX, int nX,
00043              REAL StartY, REAL EndY, int nY); // nX and nY must be equal to or greater than 1
00044 
00045     MeshGrid(REAL StartX, REAL EndX, int nX,
00046              REAL StartY, REAL EndY, int nY,
00047              REAL StartZ, REAL EndZ, int nZ); // nX and nY must be equal to or greater than 1
00048 
00049     ~MeshGrid()
00050     {
00051         if (_X!=NULL) delete [] _X; 
00052         if (_Y!=NULL) delete [] _Y; 
00053         if (_Z!=NULL) delete [] _Z; 
00054     }
00055 
00056     int      nX() const {                    return _nX;     }
00057     int      nY() const {                    return _nY;     }
00058     int      nZ() const {                    return _nZ;     }
00059     int  Length() const {                    return _length; }
00060     REAL X(int i) const { assert(_X!=NULL);  return _X[i];   }
00061     REAL Y(int i) const { assert(_Y!=NULL);  return _Y[i];   }
00062     REAL Z(int i) const { assert(_Z!=NULL);  return _Z[i];   }
00063 
00064     REAL const * X() const { assert(_X!=NULL);  return _X;   }
00065     REAL const * Y() const { assert(_Y!=NULL);  return _Y;   }
00066     REAL const * Z() const { assert(_Z!=NULL);  return _Z;   }
00067 
00068 private:
00069     int    _nX;
00070     int    _nY;
00071     int    _nZ;
00072     int    _length;
00073     REAL * _X;
00074     REAL * _Y;
00075     REAL * _Z;
00076 }; // class MeshGrid
00077 
00078 std::ostream & operator<< (std::ostream & os, MeshGrid & mg);
00079 
00080 
00082 
00083 
00084 inline MeshGrid::MeshGrid(REAL StartX, REAL EndX, REAL StepX, // {{{
00085                           REAL StartY, REAL EndY, REAL StepY)
00086     : _nX(0), _nY(0), _nZ(0), _length(0), _X(NULL), _Y(NULL), _Z(NULL)
00087 {
00088     REAL NumX = (EndX-StartX+StepX)/StepX;
00089     REAL NumY = (EndY-StartY+StepY)/StepY;
00090     assert(NumX>=1);
00091     assert(NumY>=1);
00092     
00093     int nX = static_cast<int>(NumX);
00094     int nY = static_cast<int>(NumY);
00095 
00096         _nX = nX;
00097         _nY = nY;
00098     _length = nX*nY;
00099     
00100     _X = new REAL [_length];
00101     _Y = new REAL [_length];
00102 
00103     int    k=0;
00104     REAL X;
00105     REAL Y=StartY;
00106     for (int j=0; j<nY; ++j)
00107     {
00108         X=StartX;
00109         for (int i=0; i<nX; ++i)
00110         {
00111             _X[k] = X;
00112             _Y[k] = Y;
00113             X=X+StepX;
00114             k++;
00115         }
00116         Y=Y+StepY;
00117     }
00118 } // }}}
00119 
00120 inline MeshGrid::MeshGrid(REAL StartX, REAL EndX, int nX, // {{{
00121                           REAL StartY, REAL EndY, int nY)
00122     : _nX(0), _nY(0), _nZ(0), _length(0), _X(NULL), _Y(NULL), _Z(NULL)
00123 {
00124     assert(nX>=1);
00125     assert(nY>=1);
00126     REAL StepX = ( nX>1 ? (EndX-StartX)/(nX-1.0) : 0.0 );
00127     REAL StepY = ( nY>1 ? (EndY-StartY)/(nY-1.0) : 0.0 );
00128 
00129         _nX = nX;
00130         _nY = nY;
00131     _length = nX*nY;
00132     
00133     _X = new REAL [_length];
00134     _Y = new REAL [_length];
00135 
00136     int  k=0;
00137     REAL X;
00138     REAL Y=StartY;
00139     for (int j=0; j<nY; ++j)
00140     {
00141         X=StartX;
00142         for (int i=0; i<nX; ++i)
00143         {
00144             _X[k] = X;
00145             _Y[k] = Y;
00146             X=X+StepX;
00147             k++;
00148         }
00149         Y=Y+StepY;
00150     }
00151 } // }}}
00152 
00153 inline MeshGrid::MeshGrid(REAL StartX, REAL EndX, int nX, // {{{
00154                           REAL StartY, REAL EndY, int nY,
00155                           REAL StartZ, REAL EndZ, int nZ)
00156     : _nX(0), _nY(0), _nZ(0), _length(0), _X(NULL), _Y(NULL), _Z(NULL)
00157 {
00158     assert(nX>=1);
00159     assert(nY>=1);
00160     assert(nZ>=1);
00161     REAL StepX = ( nX>1 ? (EndX-StartX)/(nX-1.0) : 0.0 );
00162     REAL StepY = ( nY>1 ? (EndY-StartY)/(nY-1.0) : 0.0 );
00163     REAL StepZ = ( nZ>1 ? (EndZ-StartZ)/(nZ-1.0) : 0.0 );
00164 
00165         _nX = nX;
00166         _nY = nY;
00167         _nZ = nZ;
00168     _length = nX*nY*nZ;
00169     
00170     _X = new REAL [_length];
00171     _Y = new REAL [_length];
00172     _Z = new REAL [_length];
00173 
00174     int  p=0;
00175     REAL X;
00176     REAL Y;
00177     REAL Z=StartZ;
00178     for (int k=0; k<nZ; ++k)
00179     {
00180         Y=StartY;
00181         for (int j=0; j<nY; ++j)
00182         {
00183             X=StartX;
00184             for (int i=0; i<nX; ++i)
00185             {
00186                 _X[p] = X;
00187                 _Y[p] = Y;
00188                 _Z[p] = Z;
00189                 X=X+StepX;
00190                 p++;
00191             }
00192             Y=Y+StepY;
00193         }
00194         Z=Z+StepZ;
00195     }
00196 } // }}}
00197 
00198 std::ostream & operator<< (std::ostream & os, MeshGrid & mg) // {{{
00199 {
00200     for (int i=0; i<mg.Length(); ++i)
00201     {
00202         if (mg.nX()>0) os << mg.X(i) << "   ";
00203         if (mg.nY()>0) os << mg.Y(i) << "   ";
00204         if (mg.nZ()>0) os << mg.Z(i) << "   ";
00205         os << std::endl;
00206     }
00207     return os;
00208 } // }}}
00209 
00210 #endif // MECHSYS_MESHGRID_H
00211 
00212 // vim:fdm=marker

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