RIFObservationIO.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 
14 #include <nuklei/RIFObservation.h>
15 #include <nuklei/Common.h>
16 #include <nuklei/Match.h>
17 #include <nuklei/Indenter.h>
18 
19 namespace nuklei {
20 
21 
22 
23  RIFReader::RIFReader(const std::string &geometryFileName) :
24  geometryFileName_(geometryFileName), rows_(0), columns_(0),
25  currentIndex_(0)
26  {
27  }
28 
29  RIFReader::~RIFReader()
30  {
31  }
32 
33 
34 
35  void RIFReader::init_()
36  {
37  NUKLEI_TRACE_BEGIN();
38  NUKLEI_ASSERT(!in_.is_open());
39  in_.open(geometryFileName_.c_str(), std::ios::in);
40  if (!in_.is_open())
41  throw ObservationIOError(std::string("Could not open file ") +
42  geometryFileName_ + " for reading.");
43  try {
44  std::string line;
45  while (std::getline(in_, line))
46  if (cleanLine(line) == "|") break;
47  NUKLEI_ASSERT(line == "|");
48  rows_ = columns_ = -1;
49  if ( !(in_ >> Match("float") >> Match("ascii") >> rows_ >> columns_) )
50  throw ObservationIOError("Non-RIF format.");
51  NUKLEI_ASSERT(rows_ >= 0 && columns_ >= 0);
52  } catch (Error &e) {
53  throw ObservationIOError("Non-RIF format.");
54  }
55  flags_.clear();
56  x_.clear();
57  y_.clear();
58  z_.clear();
59 
60  for (unsigned i = 0; i < rows_*columns_; ++i)
61  {
62  coord_t c;
63  NUKLEI_ASSERT(in_>>c);
64  x_.push_back(c);
65  }
66  for (unsigned i = 0; i < rows_*columns_; ++i)
67  {
68  coord_t c;
69  NUKLEI_ASSERT(in_>>c);
70  y_.push_back(c);
71  }
72  for (unsigned i = 0; i < rows_*columns_; ++i)
73  {
74  coord_t c;
75  NUKLEI_ASSERT(in_>>c);
76  z_.push_back(c);
77  }
78  for (unsigned i = 0; i < rows_*columns_; ++i)
79  {
80  // Why does this not work?:
81  // in_>>flags_[i];
82 
83  bool b;
84  NUKLEI_ASSERT(in_>>b);
85  flags_.push_back(b);
86  }
87 
88  rgb_.clear();
89  {
90  for (unsigned r = 0; r < rows_; r++)
91  for (unsigned c = 0; c < columns_; c++)
92  {
93  Vector3 rgb(.5,.5,.5);
94  rgb_.push_back(rgb);
95  }
96  }
97  in_.close();
98  currentIndex_ = 0;
99  NUKLEI_TRACE_END();
100  }
101 
102 
103  void RIFReader::reset()
104  {
105  NUKLEI_TRACE_BEGIN();
106  init();
107  NUKLEI_TRACE_END();
108  }
109 
110 
111  NUKLEI_UNIQUE_PTR<Observation> RIFReader::readObservation_()
112  {
113  NUKLEI_TRACE_BEGIN();
114  if (rows_ == 0 || columns_ == 0) NUKLEI_THROW("Reader does not seem inited.");
115 
116  for (;;)
117  {
118  if (currentIndex_ >= rows_*columns_)
119  return NUKLEI_UNIQUE_PTR<Observation>();
120 
121  unsigned index = currentIndex_;
122  currentIndex_++;
123 
124  if (flags_[index] == true) continue;
125 
126  NUKLEI_UNIQUE_PTR<RIFObservation> observation(new RIFObservation);
127 
128  Vector3 loc(x_[index], y_[index], z_[index]);
129  observation->setLoc(loc);
130  RGBColor c(rgb_[index]);
131  observation->setColor(c);
132 
133  return NUKLEI_UNIQUE_PTR<Observation>(NUKLEI_MOVE(observation));
134  }
135 
136  return NUKLEI_UNIQUE_PTR<Observation>();
137  NUKLEI_TRACE_END();
138  }
139 
140 }
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.