Jpp  18.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditTransmitter.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <set>
5 
7 
8 #include "JSupport/JMeta.hh"
9 
10 #include "Jeep/JContainer.hh"
11 #include "Jeep/JPrint.hh"
12 #include "Jeep/JParser.hh"
13 #include "Jeep/JMessage.hh"
14 
15 
16 namespace {
17 
18  using namespace JPP;
19 
20  static const std::string set_t = "set"; //!< Set position
21  static const std::string add_t = "add"; //!< Add position
22  static const std::string sub_t = "sub"; //!< Subtract position
23  static const std::string rot_t = "rot"; //!< Rotate around z-axis by given value [rad]
24 
25  /**
26  * Auxiliary class to apply transmitter modifications.
27  */
28  struct JModifier {
29  /**
30  * Apply modification to given transmitter.
31  *
32  * \param transmitter transmitter
33  * \return true if valid action; else false
34  */
35  bool apply(JTransmitter& transmitter) const
36  {
37  switch (data.size()) {
38 
39  case 1:
40  return apply(transmitter, action, data[0]); // orientation calibration
41 
42  case 3:
43  return apply(transmitter, action, JVector3D(data[0], data[1], data[2])); // 3D position calibration
44 
45  default:
46  return false;
47  }
48  }
49 
50 
51  /**
52  * Read modifier from input.
53  *
54  * \param in input stream
55  * \param modifier modifier
56  * \return input stream
57  */
58  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
59  {
60  using namespace std;
61 
62  if (in >> modifier.id >> modifier.action) {
63 
64  modifier.data.clear();
65 
66  for (double x; in >> x; ) {
67  modifier.data.push_back(x);
68  }
69 
70  in.clear(ios_base::eofbit);
71  }
72 
73  return in;
74  }
75 
76 
77  /**
78  * Write modifier to output.
79  *
80  * \param out output stream
81  * \param modifier modifier
82  * \return output stream
83  */
84  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
85  {
86  out << modifier.id;
87  out << ' ';
88  out << modifier.action;
89 
90  for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
91  out << ' ' << *i;
92  }
93 
94  return out;
95  }
96 
97 
98  int id;
99  std::string action;
101 
102 
103  private:
104  /**
105  * Apply orientation calibration to given transmitter.
106  *
107  * \param transmitter transmitter
108  * \param action action
109  * \param value value
110  * \return true if valid action; else false
111  */
112  static bool apply(JTransmitter& transmitter, const std::string& action, const double value)
113  {
114  if (action == rot_t)
115  transmitter.rotate(JRotation3Z(value));
116  else
117  return false;
118 
119  return true;
120  }
121 
122 
123  /**
124  * Apply position calibration to given transmitter.
125  *
126  * \param transmitter transmitter
127  * \param action action
128  * \param pos pos
129  * \return true if valid action; else false
130  */
131  static bool apply(JTransmitter& transmitter, const std::string& action, const JVector3D& pos)
132  {
133  if (action == set_t)
134  transmitter.setPosition(pos);
135  else if (action == add_t)
136  transmitter.add(pos);
137  else if (action == sub_t)
138  transmitter.sub(pos);
139  else
140  return false;
141 
142  return true;
143  }
144  };
145 }
146 
147 /**
148  * \file
149  *
150  * Auxiliary program to modify transmitter configuration.
151  *
152  * Syntax:
153  * <pre>
154  * -S "<string number> (set|add|sub) x y z"
155  * -S "<string number> (rot) phi"
156  * </pre>
157  *
158  * For option <tt>rot</tt>,
159  * the angle <tt>phi</tt> refers to an anti-clockwise rotation around the z-axis.\n
160  * The rotation angle is defined in radians.
161  *
162  * The options
163  * <pre>
164  * -A "<identifier> <string number> <floor> x y z"
165  * -r "<string number>"
166  * </pre>
167  * can be used to add and remove a transmitter, respectively.
168  *
169  * \author mdejong
170  */
171 int main(int argc, char **argv)
172 {
173  using namespace std;
174  using namespace JPP;
175 
176  typedef JContainer< vector<JTransmitter> > container_type;
177 
178  string inputFile;
179  string outputFile;
180  vector<JModifier> mod;
182  set<int> rm;
183  bool squash;
184  int debug;
185 
186  try {
187 
188  JParser<> zap("Auxiliary program to modify transmitter configuration.");
189 
190  zap['f'] = make_field(inputFile, "transmitter input file");
191  zap['o'] = make_field(outputFile, "transmitter output file");
192  zap['S'] = make_field(mod, "transmitter modifier") = JPARSER::initialised();
193  zap['A'] = make_field(add, "add transmitter") = JPARSER::initialised();
194  zap['r'] = make_field(rm, "remove transmitter[s]") = JPARSER::initialised();
195  zap['q'] = make_field(squash, "squash meta data");
196  zap['d'] = make_field(debug, "debug level") = 2;
197 
198  zap(argc, argv);
199  }
200  catch(const exception &error) {
201  FATAL(error.what() << endl);
202  }
203 
204  container_type data;
205 
206  try {
207  data.load(inputFile.c_str());
208  }
209  catch(const exception&) {}
210 
211  if (squash) {
212  data.comment.clear();
213  }
214 
215  data.comment.add(JMeta(argc, argv));
216 
217  for (vector<JTransmitter>::const_iterator i = add.begin(); i != add.end(); ++i) {
218  data.push_back(*i);
219  }
220 
221  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
222 
223  for (container_type::iterator target = data.begin(); target != data.end(); ++target) {
224 
225  if (target->getString() == i->id) {
226 
227  DEBUG("Modifier" << ' '
228  << "(" << FILL(4,'0') << target->getString() << "," << FILL(2,'0') << target->getFloor() << FILL() << ")" << ' '
229  << "action" << ' ' << i->action << JEEPZ() << i->data << endl);
230 
231  if (!i->apply(*target)) {
232  ERROR("No valid action: " << *i << endl);
233  }
234  }
235  }
236  }
237 
238  for (set<int>::const_iterator i = rm.begin(); i != rm.end(); ++i) {
239  for (container_type::iterator target = data.begin(); target != data.end(); ) {
240  if (target->getString() == *i)
241  target = data.erase(target);
242  else
243  ++target;
244  }
245  }
246 
247  data.store(outputFile.c_str());
248 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1514
int main(int argc, char *argv[])
Definition: Main.cc:15
Target.
Definition: JHead.hh:298
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
string outputFile
Rotation around Z-axis.
Definition: JRotation3D.hh:85
then fatal Missing detector file $DETECTOR fi eval JPrintDetector a $DETECTOR O IDENTIFIER for KEY in tripod hydrophone transmitter
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
then rm
Definition: sftpput.zsh:30
I/O formatting auxiliaries.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
Data structure for transmitter.
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition: JContainer.hh:39
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:65
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:328
#define FATAL(A)
Definition: JMessage.hh:67
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1829
Utility class to parse command line options.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Type definition of transmitter.
Definition: JTransmitter.hh:34
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:186
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:142
Container I/O.
int debug
debug level
void setPosition(const JVector3D &pos)
Set position.
Definition: JPosition3D.hh:152
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62