COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
objlib.h
Go to the documentation of this file.
1 #ifndef OBJLIB_H
2 #define OBJLIB_H
3 
4 #include "support/configCosmos.h"
5 #include "math/mathlib.h"
6 #include "math/vector.h"
7 using namespace Cosmos::Math::Vectors;
8 using namespace Cosmos::Math::Quaternions;
9 
10 
11 namespace Cosmos
12 {
13  class wavefront
14  {
15  public:
16  wavefront();
17 
18  struct vertex
19  {
20  size_t v=0;
21  size_t vt=0;
22  size_t vn=0;
23 
25 
29  return json11::Json::object {
30  { "v" , static_cast<double>(v) },
31  { "vt" , static_cast<double>(vt) },
32  { "vn" , static_cast<double>(vn) }
33  };
34  }
35 
37 
42  void from_json(const string& s) {
43  string error;
45  if(error.empty()) {
46  if(!p["v"].is_null()) v = static_cast<size_t>(p["v"].number_value());
47  if(!p["vt"].is_null()) vt = static_cast<size_t>(p["vt"].number_value());
48  if(!p["vn"].is_null()) vn = static_cast<size_t>(p["vn"].number_value());
49  } else {
50  cerr<<"ERROR: <"<<error<<">"<<endl;
51  }
52  return;
53  }
54  };
55 
56  struct point
57  {
58  vector <size_t> groups;
59  size_t vertex = 0;
60 
62 
66  vector<double> d_groups(groups.begin(), groups.end());
67  return json11::Json::object {
68  { "groups" , d_groups },
69  { "vertex" , static_cast<double>(vertex) }
70  };
71  }
72 
74 
79  void from_json(const string& s) {
80  string error;
82  if(error.empty()) {
83  for(size_t i = 0; i < groups.size(); ++i) {
84  if(!p["groups"][i].is_null())
85  groups[i] = static_cast<size_t>(p["groups"][i].number_value());
86  }
87  if(!p["vertex"].is_null()) vertex = static_cast<size_t>(p["vertex"].number_value());
88  } else {
89  cerr<<"ERROR: <"<<error<<">"<<endl;
90  }
91  return;
92  }
93  };
94 
95  struct line
96  {
97  vector <size_t> groups;
98  vector <vertex> vertices;
100  double length = 0.;
101 
103 
107  vector<double> d_groups(groups.begin(), groups.end());
108  return json11::Json::object {
109  { "groups" , d_groups },
110  { "vertices" , vertices },
111  { "centroid" , centroid },
112  { "length" , length }
113  };
114  }
115 
117 
122  void from_json(const string& s) {
123  string error;
125  if(error.empty()) {
126  for(size_t i = 0; i < groups.size(); ++i) {
127  if(!p["groups"][i].is_null())
128  groups[i] = static_cast<size_t>(p["groups"][i].number_value());
129  }
130  for(size_t i = 0; i < vertices.size(); ++i) {
131  if(!p["vertices"][i].is_null()) vertices[i].from_json(p["vertices"][i].dump());
132  }
133  if(!p["centroid"].is_null()) centroid.from_json(p["centroid"].dump());
134  if(!p["length"].is_null()) length = p["length"].number_value();
135  } else {
136  cerr<<"ERROR: <"<<error<<">"<<endl;
137  }
138  return;
139  }
140  };
141 
142  struct face
143  {
144  vector <size_t> groups;
145  vector <vertex> vertices;
148  double area = 0.;
149 
151 
155  vector<double> d_groups(groups.begin(), groups.end());
156  return json11::Json::object {
157  { "groups" , d_groups },
158  { "vertices" , vertices },
159  { "com" , com },
160  { "normal" , normal },
161  { "area" , area }
162  };
163  }
164 
166 
171  void from_json(const string& s) {
172  string error;
174  if(error.empty()) {
175  for(size_t i = 0; i < groups.size(); ++i) {
176  if(!p["groups"][i].is_null())
177  groups[i] = static_cast<size_t>(p["groups"][i].number_value());
178  }
179  for(size_t i = 0; i < vertices.size(); ++i) {
180  if(!p["vertices"][i].is_null()) vertices[i].from_json(p["vertices"][i].dump());
181  }
182  if(!p["com"].is_null()) com.from_json(p["com"].dump());
183  if(!p["normal"].is_null()) normal.from_json(p["normal"].dump());
184  if(!p["area"].is_null()) area = p["area"].number_value();
185  } else {
186  cerr<<"ERROR: <"<<error<<">"<<endl;
187  }
188  return;
189  }
190  };
191 
192  struct group
193  {
194  string name = "";
195  size_t materialidx = 0;
196  vector <size_t> pointidx;
197  vector <size_t> lineidx;
198  vector <size_t> faceidx;
200  double volume = 0;
201 
203 
207  vector<double> d_pointidx(pointidx.begin(), pointidx.end());
208  vector<double> d_lineidx(lineidx.begin(), lineidx.end());
209  vector<double> d_faceidx(faceidx.begin(), faceidx.end());
210  return json11::Json::object {
211  { "name" , name },
212  { "materialidx" , static_cast<double>(materialidx) },
213  { "pointidx" , d_pointidx },
214  { "lineidx" , d_lineidx },
215  { "faceidx" , d_faceidx },
216  { "com" , com },
217  { "volume", volume }
218  };
219  }
220 
222 
227  void from_json(const string& s) {
228  string error;
230  if(error.empty()) {
231  if(!p["name"].is_null()) name = p["name"].string_value();
232  if(!p["materialidx"].is_null()) materialidx = static_cast<size_t>(p["materialidx"].number_value());
233  for(size_t i = 0; i < pointidx.size(); ++i) {
234  if(!p["pointidx"][i].is_null())
235  pointidx[i] = static_cast<size_t>(p["pointidx"][i].number_value());
236  }
237  for(size_t i = 0; i < lineidx.size(); ++i) {
238  if(!p["lineidx"][i].is_null())
239  lineidx[i] = static_cast<size_t>(p["lineidx"][i].number_value());
240  }
241  for(size_t i = 0; i < faceidx.size(); ++i) {
242  if(!p["faceidx"][i].is_null())
243  faceidx[i] = static_cast<size_t>(p["faceidx"][i].number_value());
244  }
245  if(!p["com"].is_null()) com.from_json(p["com"].dump());
246  if(!p["volume"].is_null()) volume = p["volume"].number_value();
247  } else {
248  cerr<<"ERROR: <"<<error<<">"<<endl;
249  }
250  return;
251  }
252  };
253 
254  struct material
255  {
256  string name = "";
257  float density = 0.f;
261 
263 
267  return json11::Json::object {
268  { "name" , name },
269  { "density" , density },
270  { "ambient" , ambient },
271  { "diffuse" , diffuse },
272  { "specular", specular }
273  };
274  }
275 
277 
282  void from_json(const string& s) {
283  string error;
285  if(error.empty()) {
286  if(!p["name"].is_null()) name = p["name"].string_value();
287  if(!p["density"].is_null()) density = p["density"].number_value();
288  if(!p["ambient"].is_null()) ambient.from_json(p["ambient"].dump());
289  if(!p["diffuse"].is_null()) diffuse.from_json(p["diffuse"].dump());
290  if(!p["specular"].is_null()) specular.from_json(p["specular"].dump());
291  } else {
292  cerr<<"ERROR: <"<<error<<">"<<endl;
293  }
294  return;
295  }
296  };
297 
298  size_t add_geometric_vertex(Vector v);
299  size_t add_texture_vertex(Vector v);
300  size_t add_normal_vertex(Vector v);
301  size_t add_parameter_vertex(Vector v);
302  void add_material(material material);
303  void add_point(point point);
304  void add_line(line line);
305  void add_face(face face);
306  void update_line(line &cline);
307  void update_face(face &cface);
308  void update_group(group &cgroup);
309  void modify_groups(vector <string> groups);
310  void modify_groups(string group);
311  void add_cuboid(string name, Vector size, Quaternion orientation, Vector offset);
312  void add_1u(string basename, Vector offset);
313  void add_3u(string basename, Vector offset);
314  int32_t load_file(string loc, string name);
315  int32_t save_file(string loc, string name);
316  void rebase();
317  vector<string> split(string str, char c = ' ');
318 
319  vector <Vector> Vg;
320  vector <Vector> Vt;
321  vector <Vector> Vn;
322  vector <Vector> Vp;
323 
324  vector <material> Materials;
325  vector <point> Points;
326  vector <line> Lines;
327  vector <face> Faces;
328  vector <group> Groups;
329  string name = "";
330 
332 
336  return json11::Json::object {
337  { "Vg" , Vg },
338  { "Vt" , Vt },
339  { "Vn" , Vn },
340  { "Vp" , Vp },
341  { "Materials" , Materials },
342  { "Points" , Points },
343  { "Lines" , Lines },
344  { "Faces" , Faces },
345  { "Groups" , Groups },
346  { "name" , name }
347  };
348  }
349 
351 
356  void from_json(const string& s) {
357  string error;
359  if(error.empty()) {
360  for(size_t i = 0; i < Vg.size(); ++i) {
361  if(!p["Vg"][i].is_null())
362  Vg[i].from_json(p["Vg"][i].dump());
363  }
364  for(size_t i = 0; i < Vt.size(); ++i) {
365  if(!p["Vt"][i].is_null())
366  Vt[i].from_json(p["Vt"][i].dump());
367  }
368  for(size_t i = 0; i < Vn.size(); ++i) {
369  if(!p["Vn"][i].is_null())
370  Vn[i].from_json(p["Vn"][i].dump());
371  }
372  for(size_t i = 0; i < Materials.size(); ++i) {
373  if(!p["Materials"][i].is_null())
374  Materials[i].from_json(p["Materials"][i].dump());
375  }
376 
377  for(size_t i = 0; i < Points.size(); ++i) {
378  if(!p["Points"][i].is_null())
379  Points[i].from_json(p["Points"][i].dump());
380  }
381  for(size_t i = 0; i < Lines.size(); ++i) {
382  if(!p["Lines"][i].is_null())
383  Lines[i].from_json(p["Lines"][i].dump());
384  }
385  for(size_t i = 0; i < Faces.size(); ++i) {
386  if(!p["Faces"][i].is_null())
387  Faces[i].from_json(p["Faces"][i].dump());
388  }
389  for(size_t i = 0; i < Groups.size(); ++i) {
390  if(!p["Groups"][i].is_null())
391  Groups[i].from_json(p["Groups"][i].dump());
392  }
393  if(!p["name"].is_null()) name = p["name"].string_value();
394  } else {
395  cerr<<"ERROR: <"<<error<<">"<<endl;
396  }
397  return;
398  }
399 
400  private:
401  vector <size_t> cGroups;
402  void parseobj(string input);
403  void parsemtl(string input);
404  string location = "";
405  };
406 }
407 
408 #endif // OBJLIB_H
Definition: agentclass.cpp:54
vector< group > Groups
Definition: objlib.h:328
std::map< std::string, Json > object
Definition: json11.hpp:88
Definition: objlib.h:95
Definition: json11.hpp:79
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:122
Definition: objlib.h:18
vector< material > Materials
Definition: objlib.h:324
int i
Definition: rw_test.cpp:37
Vector specular
Definition: objlib.h:260
Vector normal
Definition: objlib.h:147
Definition: vector.cpp:1578
vector< Vector > Vt
Definition: objlib.h:320
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:171
json11::Json to_json() const
Convert class contents to JSON object.
Definition: objlib.h:106
const string & str
Definition: json11.cpp:360
vector< point > Points
Definition: objlib.h:325
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:42
vector< Vector > Vg
Definition: objlib.h:319
static double * p
Definition: gauss_jackson_test.cpp:42
void dump(std::string &out) const
void from_json(const string &s)
Set class contents from JSON string.
Definition: vector.h:783
Definition: vector.cpp:2097
vector< face > Faces
Definition: objlib.h:327
static Json parse(const std::string &in, std::string &err, JsonParse strategy=JsonParse::STANDARD)
static void dump(NullStruct, string &out)
Definition: json11.cpp:53
Definition: objlib.h:254
json11::Json to_json() const
Convert class contents to JSON object.
Definition: objlib.h:266
Vector centroid
Definition: objlib.h:99
vector< size_t > pointidx
Definition: objlib.h:196
Definition: objlib.h:56
Vector diffuse
Definition: objlib.h:259
json11::Json to_json() const
Convert class contents to JSON object.
Definition: objlib.h:335
vector< size_t > groups
Definition: objlib.h:144
Headers and definitions common to all COSMOS.
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:282
vector< size_t > lineidx
Definition: objlib.h:197
string name
Definition: cubesat2obj.cpp:6
Vector ambient
Definition: objlib.h:258
json11::Json to_json() const
Convert class contents to JSON object.
Definition: objlib.h:206
vector< Vector > Vn
Definition: objlib.h:321
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:356
vector< vertex > vertices
Definition: objlib.h:98
json11::Json to_json() const
Convert class contents to JSON object.
Definition: objlib.h:28
mathlib include file
vector< size_t > groups
Definition: objlib.h:97
Definition: objlib.h:13
png_uint_32 length
Definition: png.c:2173
vector< line > Lines
Definition: objlib.h:326
Vector com
Definition: objlib.h:199
vector< size_t > faceidx
Definition: objlib.h:198
vector< size_t > groups
Definition: objlib.h:58
vector< vertex > vertices
Definition: objlib.h:145
json11::Json to_json() const
Convert class contents to JSON object.
Definition: objlib.h:154
Definition: objlib.h:192
json11::Json to_json() const
Convert class contents to JSON object.
Definition: objlib.h:65
Definition: objlib.h:142
vector< Vector > Vp
Definition: objlib.h:322
Vector Class.
Definition: vector.h:672
Vector com
Definition: objlib.h:146
vector< size_t > cGroups
Definition: objlib.h:401
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:227
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:79