fw4spl
tests/fwTest/src/fwTest/generator/Image.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwTest/generator/Image.hpp"
8 
9 #include <fwDataTools/helper/Array.hpp>
10 
11 #include <fwTools/NumericRoundCast.hxx>
12 #include <fwTools/Type.hpp>
13 
14 #include <ctime>
15 
16 
17 namespace fwTest
18 {
19 namespace generator
20 {
21 
22 //------------------------------------------------------------------------------
23 
25 {
26  std::srand(::fwTools::numericRoundCast< unsigned int >(std::time(NULL)));
27 }
28 
29 //------------------------------------------------------------------------------
30 
31 void Image::generateImage(::fwData::Image::sptr image,
33  std::vector<double> spacing,
34  std::vector<double> origin,
35  ::fwTools::Type type)
36 {
37  image->setSpacing(spacing);
38  image->setOrigin(origin);
39  image->setSize(size);
40  image->setType(type);
41 
42  image->allocate();
43 
44  ::fwData::Array::sptr array = image->getDataArray();
45  ::fwDataTools::helper::Array helper(array);
46  std::fill(helper.begin(), helper.end(), 0);
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 void Image::generateRandomImage(::fwData::Image::sptr image, ::fwTools::Type type)
52 {
53  image->setType(type);
54 
56  size[0] = rand()%100 + 2;
57  size[1] = rand()%100 + 2;
58  size[2] = rand()%100 + 2;
59  image->setSize(size);
60 
61  std::vector< double > spacing(3);
62  spacing[0] = (rand()%200 +1) / 100.;
63  spacing[1] = (rand()%200 +1) / 100.;
64  spacing[2] = (rand()%200 +1) / 100.;
65  image->setSpacing(spacing);
66 
67  std::vector< double > origin(3);
68  origin[0] = (rand()%200 - 100) / 10.;
69  origin[1] = (rand()%200 - 100) / 10.;
70  origin[2] = (rand()%200 - 100) / 10.;
71  image->setOrigin(origin);
72 
73  image->allocate();
74 
75  ::fwData::Array::sptr array = image->getDataArray();
76  randomizeArray(array);
77 
78  image->setWindowWidth( (rand()%200) / 10. + 1);
79  image->setWindowCenter((rand()%200 - 100) / 10.);
80 }
81 
82 //------------------------------------------------------------------------------
83 
84 void Image::randomizeArray(::fwData::Array::sptr array)
85 {
86  ::fwDataTools::helper::Array helper(array);
87  char* iter = helper.begin< char >();
88 
89  for (; iter != helper.end< char >(); ++iter)
90  {
91  *iter = rand()%256;
92  }
93 }
94 
95 //------------------------------------------------------------------------------
96 
97 ::fwData::Array::sptr Image::createRandomizedArray(const std::string& type, ::fwData::Array::SizeType sizes)
98 {
99  ::fwData::Array::sptr array = ::fwData::Array::New();
100 
101  array->resize( ::fwTools::Type::create(type), sizes, 1, true );
102 
103  Image::randomizeArray( array );
104 
105  return array;
106 }
107 
108 //------------------------------------------------------------------------------
109 
110 } // namespace generator
111 } // namespace fwTest
std::vector< size_t > SizeType
Array size type.
static FWTEST_API void generateRandomImage(::fwData::Image::sptr image,::fwTools::Type type)
Generate an image with random information (size, spacing, ...). Buffer is filled with random values...
static FWTEST_API void randomizeArray(::fwData::Array::sptr array)
Fill array with random value.
static FWTEST_API void initRand()
Initialize &#39;rand&#39; seed.
static FWTEST_API void generateImage(::fwData::Image::sptr image,::fwData::Image::SizeType size, std::vector< double > spacing, std::vector< double > origin,::fwTools::Type type)
Generate an image with the given informations. Buffer is filled with 0.
virtual FWDATATOOLS_API char * begin()
Returns the begining/end of the buffer interpreted as a char buffer.
Definition: Data.hpp:15
Class describing an elementary C++ type aka unsigned char, signed char, .... int, float...
Definition: Type.hpp:32
static FWTEST_API::fwData::Array::sptr createRandomizedArray(const std::string &type,::fwData::Array::SizeType sizes)
Creates an Array with the given type and size and fills buffer with random values.
Helper to manage array buffer. Lock the buffer before to modify it.
::fwData::Array::SizeType SizeType
Image size type.