SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Array3.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 ///
32 ///@file Array3.h
33 ///@brief Interface to dynamic 3D array class
34 ///
35 ///@author Steven G. Parker
36 /// Department of Computer Science
37 /// University of Utah
38 ///@date March 1994
39 ///
40 
41 #ifndef CORE_CONAINTERS_ARRAY3_H
42 #define CORE_CONAINTERS_ARRAY3_H 1
43 
44 #include <boost/multi_array.hpp>
45 
46 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
47 #include <sci_defs/bits_defs.h>
48 #include <iostream>
49 #include <stdio.h>
50 #include <errno.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #endif
55 
57 
58 
60 
61 namespace SCIRun {
62 
63 template<class T>
64 class Array3
65 {
66 public:
67  typedef boost::multi_array<T, 3> impl_type;
68  typedef T value_type;
69 
70  Array3() {}
71 
72  Array3(size_t size1, size_t size2, size_t size3)
73  {
74  resize(size1, size2, size3);
75  }
76 
77  void resize(size_t size1, size_t size2, size_t size3)
78  {
79  typename impl_type::extent_gen extents;
80  impl_.resize(extents[size1][size2][size3]);
81  }
82 
83  size_t size() const
84  {
85  return dim1() * dim2() * dim3();
86  }
87 
88  T& operator[](size_t idx)
89  {
90  return impl_.origin()[idx];
91  }
92 
93  const T& operator[](size_t idx) const
94  {
95  return impl_.origin()[idx];
96  }
97 
98  const T& operator()(size_t i1, size_t i2, size_t i3) const
99  {
100  return impl_[i1][i2][i3];
101  }
102 
103  T& operator()(size_t i1, size_t i2, size_t i3)
104  {
105  return impl_[i1][i2][i3];
106  }
107 
108  inline size_t dim1() const {return impl_.shape()[0];}
109  inline size_t dim2() const {return impl_.shape()[1];}
110  inline size_t dim3() const {return impl_.shape()[2];}
111 
112 private:
113  impl_type impl_;
114 };
115 
116 template<class T> void Pio(Piostream& stream, Array3<T>& array);
117 template<class T> void Pio(Piostream& stream, Array3<T>& array, const std::string&);
118 template<class T> void Pio(Piostream& stream, Array3<T>*& array);
119 
120 #define ARRAY3_VERSION 2
121 
122 template<class T>
123 void
125 {
126  int version=stream.begin_class("Array3", ARRAY3_VERSION);
127 
128  if(stream.reading())
129  {
130  if (version < 2)
131  {
132  // Allocate the array
133  int d1, d2, d3;
134  Pio(stream, d1);
135  Pio(stream, d2);
136  Pio(stream, d3);
137  data.resize(d1, d2, d3);
138  }
139  else
140  {
141  // Allocate the array
142  long long d1, d2, d3;
143  Pio(stream, d1);
144  Pio(stream, d2);
145  Pio(stream, d3);
146  data.resize(static_cast<size_type>(d1),
147  static_cast<size_type>(d2),
148  static_cast<size_type>(d3));
149  }
150  }
151  else
152  {
153  long long d1, d2, d3;
154  d1 = static_cast<long long>(data.dim1());
155  d2 = static_cast<long long>(data.dim2());
156  d3 = static_cast<long long>(data.dim3());
157  Pio(stream, d1);
158  Pio(stream, d2);
159  Pio(stream, d3);
160  }
161 
162  if (stream.supports_block_io())
163  {
164  stream.block_io(reinterpret_cast<void*>(&data[0]), sizeof(T), data.size());
165  }
166  else
167  {
168  for(size_t i=0;i<data.dim1();i++)
169  {
170  for(size_t j=0;j<data.dim2();j++)
171  {
172  for(size_t k=0;k<data.dim3();k++)
173  {
174  Pio(stream, data(i,j,k));
175  }
176  }
177  }
178  }
179  stream.end_class();
180 }
181 
182 
183 template<class T>
184 void
186 {
187  if (stream.reading())
188  {
189  data= new Array3<T>;
190  }
191  Pio(stream, *data);
192 }
193 
194 
195 template<class T>
196 void
198  const std::string& filename )
199 {
200  int version=stream.begin_class("Array3", ARRAY3_VERSION);
201 
202  if(stream.reading())
203  {
204  if (version < 2)
205  {
206  // Allocate the array
207  int d1, d2, d3;
208  Pio(stream, d1);
209  Pio(stream, d2);
210  Pio(stream, d3);
211  data.resize(d1, d2, d3);
212  data.input( filename );
213  }
214  else
215  {
216  // Allocate the array
217  long long d1, d2, d3;
218  Pio(stream, d1);
219  Pio(stream, d2);
220  Pio(stream, d3);
221  data.resize(static_cast<size_type>(d1),
222  static_cast<size_type>(d2),
223  static_cast<size_type>(d3));
224  data.input( filename );
225  }
226  }
227  else
228  {
229  long long d1, d2, d3;
230  d1 = static_cast<long long>(data.dm1);
231  d2 = static_cast<long long>(data.dm2);
232  d3 = static_cast<long long>(data.dm3);
233  Pio(stream, d1);
234  Pio(stream, d2);
235  Pio(stream, d3);
236  data.output( filename );
237  }
238 
239  stream.end_class();
240 }
241 #if 0
242 template<class T>
243 int
244 Array3<T>::input( const std::string &filename )
245 {
246  std::cerr << "Array3: Split input\n";
247 
248  // get raw data
249  int file=open( filename.c_str(), O_RDONLY, 0666);
250  if ( file == -1 ) {
251  printf("can not open file %s\n", filename.c_str());
252  return 0;
253  }
254 
255  int maxiosz=1024*1024;
256  size_type size = dm1*dm2*dm3*sizeof(T);
257  int n = int(size / maxiosz);
258  char *p = reinterpret_cast<char *>(objs[0][0]);
259 
260  for ( ; n> 0 ; n--, p+= maxiosz)
261  {
262  int i = read( file, p, maxiosz);
263  if ( i != maxiosz )
264  perror( "io read ");
265  }
266  int i = read( file, p, size % maxiosz);
267  if ( i != (size % maxiosz) )
268  perror("on last io");
269 
270  fsync(file);
271  close(file);
272 
273  return 1;
274 }
275 
276 template<class T>
277 int
278 Array3<T>::output( const std::string &filename )
279 {
280  // get raw data
281  // printf("output [%s] [%s]\n", filename.c_str(), rawfile() );
282  int file=open( filename.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
283  if ( file == -1 ) {
284  perror("open file");
285  return 0;
286  }
287 
288  int maxiosz=1024*1024;
289 
290  size_type size = dm1*dm2*dm3*sizeof(T);
291  int n = size / maxiosz;
292  char *p = reinterpret_cast<char *>(objs[0][0]);
293 
294  printf("Start writing...%d %d %d\n", size, maxiosz, n);
295 
296  for ( ; n> 0 ; n--, p+= maxiosz) {
297  int l = write( file, p, maxiosz);
298  if ( l != maxiosz )
299  perror("write ");
300  }
301  int sz = (size % maxiosz );
302  int l = write( file, p, sz);
303  if ( l != (size % maxiosz ) ) {
304  printf("Error: wrote %d / %d\n", l,(size % maxiosz ));
305  perror("write ");
306  }
307 
308  fsync(file);
309  close(file);
310 
311  return 1;
312 }
313 #endif
314 }
315 
316 #endif
317 
virtual bool block_io(void *, size_t, size_t)
Definition: Persistent.h:174
bool reading() const
Definition: Persistent.h:164
virtual bool supports_block_io()
Definition: Persistent.h:171
Definition: Persistent.h:89
size_t dim2() const
Definition: Array3.h:109
tuple read
Definition: eab.py:11
boost::multi_array< T, 3 > impl_type
Definition: Array3.h:67
Utility for specifying data invariants (Assertions)
const T & operator[](size_t idx) const
Definition: Array3.h:93
size_t size() const
Definition: Array3.h:83
size_t dim3() const
Definition: Array3.h:110
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
Base class for persistent objects...
#define ARRAY3_VERSION
Definition: Array3.h:120
void resize(size_t size1, size_t size2, size_t size3)
Definition: Array3.h:77
long long size_type
Definition: Types.h:40
T value_type
Definition: Array3.h:68
dictionary data
Definition: eabLatVolData.py:11
tuple write
Definition: eab.py:17
T & operator()(size_t i1, size_t i2, size_t i3)
Definition: Array3.h:103
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
size_t dim1() const
Definition: Array3.h:108
virtual void end_class()
Definition: Persistent.cc:178
const T & operator()(size_t i1, size_t i2, size_t i3) const
Definition: Array3.h:98
Array3(size_t size1, size_t size2, size_t size3)
Definition: Array3.h:72
int n
Definition: eab.py:9
tuple file
Definition: eab.py:7
T & operator[](size_t idx)
Definition: Array3.h:88
Definition: Array3.h:64
Array3()
Definition: Array3.h:70
int size
Definition: eabLatVolData.py:2