Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Tensor3D.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Common/W32Compat.hh>
26 #include <math.h>
27 #include "Tensor3D.hh"
28 
29 using namespace BIAS;
30 using namespace std;
31 
32 template <class T>
34  : _Data(NULL), _DataArray(NULL), _Dim1(0), _Dim2(0), _Dim3(0)
35 {}
36 
37 template <class T>
38 Tensor3D<T>::Tensor3D(const int i, const int j, const int k,
39  const T& def)
40  : _Data(NULL), _DataArray(NULL), _Dim1(0), _Dim2(0), _Dim3(0)
41 {
42  NewSize(i, j, k, def);
43 }
44 
45 template <class T>
47 {
48  *this=t;
49 }
50 
51 template <class T>
53 {
54  Destroy();
55 }
56 
57 template <class T>
58 void Tensor3D<T>::NewSize(const int i, const int j, const int k,
59  const T& def)
60 {
61  BIASASSERT(i>=0);
62  BIASASSERT(j>=0);
63  BIASASSERT(k>=0);
64 
65  if (i!=_Dim1 || j!=_Dim2 || k!=_Dim3){
66  Destroy();
67  _Dim1=i; // dimension in x direction
68  _Dim2=j; // dimension in y direction
69  _Dim3=k; // dimension in z direction
70  _Size=i*j*k;
71  // allocate data
72  _Data=new T[_Size];
73  // construct data pointer array
74  int xyPlaneSize=_Dim1*_Dim2;
75  _DataArray=new T**[_Dim3];
76  for (int z=0; z<_Dim3; z++){
77  _DataArray[z]=new T*[_Dim2];
78  for (int y=0; y<_Dim2; y++){
79  _DataArray[z][y]=_Data+z*xyPlaneSize+y*_Dim1;
80  }
81  }
82  }
83  // now fill with default
84  for (int m=0; m<_Size; m++){
85  _Data[m]=def;
86  }
87 }
88 
89 template <class T>
91 {
92  if (_Data) { delete[] _Data; _Data=NULL; }
93  if (_DataArray) {
94  for (int z=0; z<_Dim3; z++){
95  delete[] _DataArray[z]; _DataArray[z]=NULL;
96  }
97  delete[] _DataArray; _DataArray=NULL;
98  }
99  _Dim1=0;
100  _Dim2=0;
101  _Dim3=0;
102  _Size=0;
103 }
104 
105 template <class T>
107 {
108  return _Dim1;
109 }
110 
111 template <class T>
113 {
114  return _Dim2;
115 }
116 
117 template <class T>
119 {
120  return _Dim3;
121 }
122 
123 template <class T>
125 {
126  memset((void *)_Data, 0, sizeof(T)*_Size);
127 }
128 
129 template <class T>
131 {
132  NewSize(t.GetDim1(), t.GetDim2(), t.GetDim3());
133  memcpy((void *)_Data, (void *)t.GetData(), _Size*sizeof(T));
134  return *this;
135 }
136 
137 template <class T>
139 {
140  if ( t.GetDim1() != GetDim1() ||
141  t.GetDim2() != GetDim2() ||
142  t.GetDim3() != GetDim3() ) return false;
143  for(int i=0; i<(GetDim1()*GetDim2()*GetDim3()) ; i++)
144  if ( t.GetData()[i]!=_Data[i] ) return false;
145  return true;
146 }
147 
148 
149 template <class T>
151 {
152  int rows = GetDim1()*GetDim2()*GetDim3();
153  Vector<T> result(rows);
154 
155  for(int i=0; i<GetDim1(); i++)
156  for(int j=0; j<GetDim2(); j++)
157  for(int k=0; k<GetDim3(); k++)
158  result[i*GetDim1()*GetDim2() + j*GetDim2() + k] = _DataArray[i][j][k];
159 
160  return result;
161 }
162 
163 
164 template <class T>
166 {
167  BIASASSERT( (int)v.Size() >= GetDim1()*GetDim2()*GetDim3() );
168 
169  for(int i=0; i<GetDim1(); i++)
170  for(int j=0; j<GetDim2(); j++)
171  for(int k=0; k<GetDim3(); k++)
172  _DataArray[i][j][k] = v[i*GetDim1()*GetDim2() + j*GetDim2() + k];
173 }
174 
175 
176 template <class T>
177 void Tensor3D<T>::Scale(T factor)
178 {
179  for(int i=0; i<_Size; i++)
180  _Data[i] = _Data[i] * factor;
181 }
182 
183 
184 template <class T>
186 {
187  T Length = 0;
188  for(int i=0; i<_Size; i++)
189  Length += _Data[i] * _Data[i];
190  return Length;
191 }
192 
193 
194 
195 #define INST(type) \
196 template class BIASMathBase_EXPORT BIAS::Tensor3D<type>;\
197 
198 INST(short)
199 INST(int)
200 INST(float)
201 INST(double)
bool operator==(const Tensor3D< T > &t)
checks if all tensor entries are equal (up-to-scale-equality is not checked)
Definition: Tensor3D.cpp:138
void Scale(T factor)
Definition: Tensor3D.cpp:177
Tensor3D< T > & operator=(const Tensor3D< T > &t)
Definition: Tensor3D.cpp:130
class for column vectors with arbitrary size
BIAS::Vector< T > ToVector()
Definition: Tensor3D.cpp:150
void SetFromVector(const BIAS::Vector< T > &v)
set the tensor by the rows of the vector.
Definition: Tensor3D.cpp:165
void Destroy()
frees internal data
Definition: Tensor3D.cpp:90
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
T GetLength() const
Definition: Tensor3D.cpp:185
T * GetData()
Definition: Tensor3D.hh:72
INST(unsigned char)
int GetDim3() const
Definition: Tensor3D.cpp:118
void NewSize(const int i, const int j, const int k, const T &def=(T) 0.0)
allocates internal data
Definition: Tensor3D.cpp:58
void SetZero()
set all elements to zero
Definition: Tensor3D.cpp:124
int GetDim1() const
Definition: Tensor3D.cpp:106
int GetDim2() const
Definition: Tensor3D.cpp:112