OsuTxtObservationIO.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #include <fstream>
8 #include <cassert>
9 #include <algorithm>
10 #include <cmath>
11 #include <boost/tuple/tuple.hpp>
12 
15 #include <nuklei/Common.h>
16 #include <nuklei/Match.h>
17 #include <nuklei/Indenter.h>
18 
19 
20 #ifdef NUKLEI_USE_CIMG
21 #include "CImg.h"
22 #endif
23 
24 namespace nuklei {
25 
26 
27 
28  OsuTxtReader::OsuTxtReader(const std::string &observationArgs) :
29  rows_(0), columns_(0),
30  currentIndex_(0)
31  {
32  size_t pos = observationArgs.find(':');
33  if (pos == std::string::npos)
34  geometryFileName = observationArgs;
35  else
36  {
37  geometryFileName = observationArgs.substr(0, pos);
38  appFileName = observationArgs.substr(pos+1);
39  }
40  }
41 
42  OsuTxtReader::~OsuTxtReader()
43  {
44  }
45 
46 
47 
48  void OsuTxtReader::init_()
49  {
50  NUKLEI_TRACE_BEGIN();
51 
52  NUKLEI_ASSERT(!in_.is_open());
53  in_.open(geometryFileName.c_str(), std::ios::in);
54  if (!in_.is_open())
55  throw ObservationIOError(std::string("Could not open file ") +
56  geometryFileName + " for reading.");
57  try {
58  if ( !(in_ >> rows_ >> Match("rows") >> columns_ >> Match("columns")) )
59  throw ObservationIOError("Non-OsuTxt format.");
60  if ( !(in_ >> Match("pixels") >> Match("(flag") >> Match("X") >>
61  Match("Y") >> Match("Z):")) )
62  throw ObservationIOError("Non-OsuTxt format.");
63  } catch (Error &e) {
64  throw ObservationIOError("Non-OsuTxt format.");
65  }
66  flags_.clear();
67  x_.clear();
68  y_.clear();
69  z_.clear();
70 
71  for (unsigned i = 0; i < rows_*columns_; ++i)
72  {
73  // Why does this not work?:
74  // in_>>flags_[i];
75 
76  bool b;
77  NUKLEI_ASSERT(in_>>b);
78  flags_.push_back(b);
79  }
80  for (unsigned i = 0; i < rows_*columns_; ++i)
81  {
82  coord_t c;
83  NUKLEI_ASSERT(in_>>c);
84  x_.push_back(c);
85  }
86  for (unsigned i = 0; i < rows_*columns_; ++i)
87  {
88  coord_t c;
89  NUKLEI_ASSERT(in_>>c);
90  y_.push_back(c);
91  }
92  for (unsigned i = 0; i < rows_*columns_; ++i)
93  {
94  coord_t c;
95  NUKLEI_ASSERT(in_>>c);
96  z_.push_back(c);
97  }
98 
99  rgb_.clear();
100  if (!appFileName.empty())
101  {
102 #ifdef NUKLEI_USE_CIMG
103  try {
104  cimg_library::CImg<unsigned char> img(appFileName.c_str());
105  for (unsigned r = 0; r < rows_; r++)
106  for (unsigned c = 0; c < columns_; c++)
107  {
108  Vector3 rgb(double(img(c*2,r*2,0,0))/255,
109  double(img(c*2,r*2,0,1))/255,
110  double(img(c*2,r*2,0,2))/255);
111  rgb_.push_back(rgb);
112  }
113  } catch (cimg_library::CImgException &e) {
114  NUKLEI_THROW("CImg error: " << e.what());
115  }
116 #else
117  NUKLEI_THROW("This function requires CIMG.");
118 #endif
119  }
120  else
121  {
122  for (unsigned r = 0; r < rows_; r++)
123  for (unsigned c = 0; c < columns_; c++)
124  {
125  Vector3 rgb(0,0,0);
126  rgb_.push_back(rgb);
127  }
128  }
129  NUKLEI_ASSERT(in_.peek() != EOF);
130  in_.close();
131  currentIndex_ = 0;
132 
133 
134  NUKLEI_TRACE_END();
135  }
136 
137 
138  void OsuTxtReader::reset()
139  {
140  NUKLEI_TRACE_BEGIN();
141  init();
142  NUKLEI_TRACE_END();
143  }
144 
145 
146  NUKLEI_UNIQUE_PTR<Observation> OsuTxtReader::readObservation_()
147  {
148  NUKLEI_TRACE_BEGIN();
149  if (rows_ == 0 || columns_ == 0) NUKLEI_THROW("Reader does not seem inited.");
150 
151  for (;;)
152  {
153  if (currentIndex_ >= rows_*columns_)
154  return NUKLEI_UNIQUE_PTR<Observation>();
155 
156  unsigned index = currentIndex_;
157  currentIndex_++;
158 
159  if (flags_[index] == false) continue;
160 
161  NUKLEI_UNIQUE_PTR<OsuTxtObservation> observation(new OsuTxtObservation);
162 
163  Vector3 loc(x_[index], y_[index], z_[index]);
164  observation->setLoc(loc);
165  RGBColor c(rgb_[index]);
166  observation->setColor(c);
167 
168  return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
169  }
170 
171  return NUKLEI_UNIQUE_PTR<Observation>();
172  NUKLEI_TRACE_END();
173  }
174 
175 }
Public namespace.
Definition: Color.cpp:9
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
double coord_t
Type for point coordinates.
Definition: Definitions.h:25
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.