Blender V2.61 - r43446

tnt_array3d_utils.h

Go to the documentation of this file.
00001 
00006 #ifndef TNT_ARRAY3D_UTILS_H
00007 #define TNT_ARRAY3D_UTILS_H
00008 
00009 #include <cstdlib>
00010 #include <cassert>
00011 
00012 namespace TNT
00013 {
00014 
00015 
00016 template <class T>
00017 std::ostream& operator<<(std::ostream &s, const Array3D<T> &A)
00018 {
00019     int M=A.dim1();
00020     int N=A.dim2();
00021     int K=A.dim3();
00022 
00023     s << M << " " << N << " " << K << "\n";
00024 
00025     for (int i=0; i<M; i++)
00026     {
00027         for (int j=0; j<N; j++)
00028         {
00029             for (int k=0; k<K; k++)
00030                 s << A[i][j][k] << " ";
00031             s << "\n";
00032         }
00033         s << "\n";
00034     }
00035 
00036 
00037     return s;
00038 }
00039 
00040 template <class T>
00041 std::istream& operator>>(std::istream &s, Array3D<T> &A)
00042 {
00043 
00044     int M, N, K;
00045 
00046     s >> M >> N >> K;
00047 
00048     Array3D<T> B(M,N,K);
00049 
00050     for (int i=0; i<M; i++)
00051         for (int j=0; j<N; j++)
00052             for (int k=0; k<K; k++)
00053                 s >>  B[i][j][k];
00054 
00055     A = B;
00056     return s;
00057 }
00058 
00059 
00060 
00061 template <class T>
00062 Array3D<T> operator+(const Array3D<T> &A, const Array3D<T> &B)
00063 {
00064     int m = A.dim1();
00065     int n = A.dim2();
00066     int p = A.dim3();
00067 
00068     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00069         return Array3D<T>();
00070 
00071     else
00072     {
00073         Array3D<T> C(m,n,p);
00074 
00075         for (int i=0; i<m; i++)
00076             for (int j=0; j<n; j++)
00077                 for (int k=0; k<p; k++)
00078                 C[i][j][k] = A[i][j][k] + B[i][j][k];
00079 
00080         return C;
00081     }
00082 }
00083 
00084 
00085 template <class T>
00086 Array3D<T> operator-(const Array3D<T> &A, const Array3D<T> &B)
00087 {
00088     int m = A.dim1();
00089     int n = A.dim2();
00090     int p = A.dim3();
00091 
00092     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00093         return Array3D<T>();
00094 
00095     else
00096     {
00097         Array3D<T> C(m,n,p);
00098 
00099         for (int i=0; i<m; i++)
00100             for (int j=0; j<n; j++)
00101                 for (int k=0; k<p; k++)
00102                 C[i][j][k] = A[i][j][k] - B[i][j][k];
00103 
00104         return C;
00105     }
00106 }
00107 
00108 
00109 
00110 
00111 template <class T>
00112 Array3D<T> operator*(const Array3D<T> &A, const Array3D<T> &B)
00113 {
00114     int m = A.dim1();
00115     int n = A.dim2();
00116     int p = A.dim3();
00117 
00118     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00119         return Array3D<T>();
00120 
00121     else
00122     {
00123         Array3D<T> C(m,n,p);
00124 
00125         for (int i=0; i<m; i++)
00126             for (int j=0; j<n; j++)
00127                 for (int k=0; k<p; k++)
00128                 C[i][j][k] = A[i][j][k] * B[i][j][k];
00129 
00130         return C;
00131     }
00132 }
00133 
00134 
00135 template <class T>
00136 Array3D<T> operator/(const Array3D<T> &A, const Array3D<T> &B)
00137 {
00138     int m = A.dim1();
00139     int n = A.dim2();
00140     int p = A.dim3();
00141 
00142     if (B.dim1() != m ||  B.dim2() != n || B.dim3() != p )
00143         return Array3D<T>();
00144 
00145     else
00146     {
00147         Array3D<T> C(m,n,p);
00148 
00149         for (int i=0; i<m; i++)
00150             for (int j=0; j<n; j++)
00151                 for (int k=0; k<p; k++)
00152                 C[i][j][k] = A[i][j][k] / B[i][j][k];
00153 
00154         return C;
00155     }
00156 }
00157 
00158 
00159 
00160 template <class T>
00161 Array3D<T>& operator+=(Array3D<T> &A, const Array3D<T> &B)
00162 {
00163     int m = A.dim1();
00164     int n = A.dim2();
00165     int p = A.dim3();
00166 
00167     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00168     {
00169         for (int i=0; i<m; i++)
00170             for (int j=0; j<n; j++)
00171                 for (int k=0; k<p; k++)
00172                     A[i][j][k] += B[i][j][k];
00173     }
00174 
00175     return A;
00176 }
00177 
00178 template <class T>
00179 Array3D<T>& operator-=(Array3D<T> &A, const Array3D<T> &B)
00180 {
00181     int m = A.dim1();
00182     int n = A.dim2();
00183     int p = A.dim3();
00184 
00185     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00186     {
00187         for (int i=0; i<m; i++)
00188             for (int j=0; j<n; j++)
00189                 for (int k=0; k<p; k++)
00190                     A[i][j][k] -= B[i][j][k];
00191     }
00192 
00193     return A;
00194 }
00195 
00196 template <class T>
00197 Array3D<T>& operator*=(Array3D<T> &A, const Array3D<T> &B)
00198 {
00199     int m = A.dim1();
00200     int n = A.dim2();
00201     int p = A.dim3();
00202 
00203     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00204     {
00205         for (int i=0; i<m; i++)
00206             for (int j=0; j<n; j++)
00207                 for (int k=0; k<p; k++)
00208                     A[i][j][k] *= B[i][j][k];
00209     }
00210 
00211     return A;
00212 }
00213 
00214 
00215 template <class T>
00216 Array3D<T>& operator/=(Array3D<T> &A, const Array3D<T> &B)
00217 {
00218     int m = A.dim1();
00219     int n = A.dim2();
00220     int p = A.dim3();
00221 
00222     if (B.dim1() == m &&  B.dim2() == n && B.dim3() == p )
00223     {
00224         for (int i=0; i<m; i++)
00225             for (int j=0; j<n; j++)
00226                 for (int k=0; k<p; k++)
00227                     A[i][j][k] /= B[i][j][k];
00228     }
00229 
00230     return A;
00231 }
00232 
00233 
00234 
00235 
00236 
00237 } // namespace TNT
00238 
00239 #endif