COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Cosmos::wavefront Class Reference

#include <objlib.h>

Collaboration diagram for Cosmos::wavefront:

Classes

struct  face
 
struct  group
 
struct  line
 
struct  material
 
struct  point
 
struct  vertex
 

Public Member Functions

 wavefront ()
 
size_t add_geometric_vertex (Vector v)
 
size_t add_texture_vertex (Vector v)
 
size_t add_normal_vertex (Vector v)
 
size_t add_parameter_vertex (Vector v)
 
void add_material (material material)
 
void add_point (point point)
 
void add_line (line line)
 
void add_face (face face)
 
void update_line (line &cline)
 
void update_face (face &cface)
 
void update_group (group &cgroup)
 
void modify_groups (vector< string > groups)
 
void modify_groups (string group)
 
void add_cuboid (string name, Vector size, Quaternion orientation, Vector offset)
 
void add_1u (string basename, Vector offset)
 
void add_3u (string basename, Vector offset)
 
int32_t load_file (string loc, string name)
 
int32_t save_file (string loc, string name)
 
void rebase ()
 
vector< string > split (string str, char c= ' ')
 
json11::Json to_json () const
 Convert class contents to JSON object. More...
 
void from_json (const string &s)
 Set class contents from JSON string. More...
 

Public Attributes

vector< VectorVg
 
vector< VectorVt
 
vector< VectorVn
 
vector< VectorVp
 
vector< materialMaterials
 
vector< pointPoints
 
vector< lineLines
 
vector< faceFaces
 
vector< groupGroups
 
string name = ""
 

Private Member Functions

void parseobj (string input)
 
void parsemtl (string input)
 

Private Attributes

vector< size_t > cGroups
 
string location = ""
 

Constructor & Destructor Documentation

Cosmos::wavefront::wavefront ( )
7  {
8  Vg.clear();
9  Vg.push_back(Vector());
10  Vt.clear();
11  Vt.push_back(Vector());
12  Vn.clear();
13  Vn.push_back(Vector());
14  Vp.clear();
15  Vp.push_back(Vector());
16  Points.clear();
17  Lines.clear();
18  Faces.clear();
19 
20  Materials.clear();
21  material tmaterial;
22  tmaterial.name = "Any";
23  tmaterial.density = 1.;
24  tmaterial.ambient = Vector(.5, .5, .5, 1.);
25  tmaterial.diffuse = Vector(.5, .5, .5, 1.);
26  Materials.push_back(tmaterial);
27 
28  Groups.clear();
29  group tgroup;
30  tgroup.name = "All";
31  Groups.push_back(tgroup);
32  }
vector< group > Groups
Definition: objlib.h:328
vector< material > Materials
Definition: objlib.h:324
vector< Vector > Vt
Definition: objlib.h:320
vector< point > Points
Definition: objlib.h:325
vector< Vector > Vg
Definition: objlib.h:319
vector< face > Faces
Definition: objlib.h:327
vector< Vector > Vn
Definition: objlib.h:321
struct ip_mreq group
Definition: mcastclient.c:39
vector< line > Lines
Definition: objlib.h:326
vector< Vector > Vp
Definition: objlib.h:322
Vector Class.
Definition: vector.h:672

Member Function Documentation

size_t Cosmos::wavefront::add_geometric_vertex ( Vector  v)
35  {
36  Vg.push_back(v);
37  return (Vg.size()-1);
38  }
vector< Vector > Vg
Definition: objlib.h:319
size_t Cosmos::wavefront::add_texture_vertex ( Vector  v)
41  {
42  Vt.push_back(v);
43  return (Vt.size()-1);
44  }
vector< Vector > Vt
Definition: objlib.h:320
size_t Cosmos::wavefront::add_normal_vertex ( Vector  v)
47  {
48  Vn.push_back(v);
49  return (Vn.size()-1);
50  }
vector< Vector > Vn
Definition: objlib.h:321
size_t Cosmos::wavefront::add_parameter_vertex ( Vector  v)
53  {
54  Vp.push_back(v);
55  return (Vp.size()-1);
56  }
vector< Vector > Vp
Definition: objlib.h:322
void Cosmos::wavefront::add_material ( material  material)
59  {
60  Materials.push_back(m);
61  }
vector< material > Materials
Definition: objlib.h:324
void Cosmos::wavefront::add_point ( point  point)
64  {
65  // Add pointidx to Groups
66  for (size_t i=0; i<cGroups.size(); ++i)
67  {
68  Groups[cGroups[i]].pointidx.push_back(Points.size());
69  }
70 
71  point.groups = cGroups;
72  Points.push_back(point);
73  }
vector< group > Groups
Definition: objlib.h:328
int i
Definition: rw_test.cpp:37
vector< point > Points
Definition: objlib.h:325
vector< size_t > cGroups
Definition: objlib.h:401
void Cosmos::wavefront::add_line ( line  line)
76  {
77  // Add lineidx to Groups
78  for (size_t i=0; i<cGroups.size(); ++i)
79  {
80  Groups[cGroups[i]].lineidx.push_back(Lines.size());
81  }
82 
83  line.groups = cGroups;
84  Lines.push_back(line);
85  }
vector< group > Groups
Definition: objlib.h:328
int i
Definition: rw_test.cpp:37
vector< line > Lines
Definition: objlib.h:326
vector< size_t > cGroups
Definition: objlib.h:401
void Cosmos::wavefront::add_face ( face  face)
88  {
89  // Add faceidx to Groups
90  for (size_t i=0; i<cGroups.size(); ++i)
91  {
92  Groups[cGroups[i]].faceidx.push_back(Faces.size());
93  }
94 
95  face.groups = cGroups;
96  update_face(face);
97  Faces.push_back(face);
98  }
vector< group > Groups
Definition: objlib.h:328
int i
Definition: rw_test.cpp:37
vector< face > Faces
Definition: objlib.h:327
void update_face(face &cface)
Definition: objlib.cpp:872
vector< size_t > cGroups
Definition: objlib.h:401
void Cosmos::wavefront::update_line ( line cline)
855  {
856  cline.centroid = Vector();
857  cline.length = 0.;
858  if (cline.vertices.size() > 0)
859  {
860  for (size_t j=0; j<cline.vertices.size(); ++j)
861  {
862  cline.centroid += Vg[cline.vertices[j].v];
863  if (j>1)
864  {
865  cline.length += (Vg[cline.vertices[j].v] - Vg[cline.vertices[j-1].v]).norm();
866  }
867  }
868  cline.centroid /= cline.vertices.size();
869  }
870  }
vector< Vector > Vg
Definition: objlib.h:319
Vector Class.
Definition: vector.h:672
void Cosmos::wavefront::update_face ( face cface)
873  {
874  if (cface.vertices.size() < 2)
875  {
876  return;
877  }
878 
879  Vector fcentroid = Vg[cface.vertices[0].v];
880  fcentroid += Vg[cface.vertices[1].v];
881  Vector v1 = Vg[cface.vertices[0].v] - Vg[cface.vertices[cface.vertices.size()-1].v];
882  Vector v2 = Vg[cface.vertices[1].v] - Vg[cface.vertices[0].v];
883  Vector fnormal = v1.cross(v2);
884  for (size_t j=2; j<cface.vertices.size(); ++j)
885  {
886  fcentroid += Vg[cface.vertices[j].v];
887  v1 = v2;
888  v2 = Vg[cface.vertices[j].v] - Vg[cface.vertices[j-1].v];
889  fnormal += v1.cross(v2);
890  }
891  fcentroid /= cface.vertices.size();
892  v1 = v2;
893  v2 = Vg[cface.vertices[0].v] - Vg[cface.vertices[cface.vertices.size()-1].v];
894  fnormal += v1.cross(v2);
895  fnormal.normalize();
896  cface.normal = fnormal;
897 
898  cface.com = Vector ();
899  cface.area = 0.;
900  v1 = Vg[cface.vertices[cface.vertices.size()-1].v] - fcentroid;
901  for (size_t j=0; j<cface.vertices.size(); ++j)
902  {
903  v2 = Vg[cface.vertices[j].v] - fcentroid;
904  // Area of triangle made by v1, v2 and Face centroid
905  double tarea = v1.area(v2);
906  // Sum
907  cface.area += tarea;
908  // Centroid of triangle made by v1, v2 amd Face centroid
909  Vector tcentroid = (v1 + v2) / 3. + fcentroid;
910  // Weighted sum
911  // Vector test = tarea * tcentroid;
912  cface.com += tcentroid * tarea;
913  v1 = v2;
914  }
915  // Divide by summed weights
916  if (cface.area)
917  {
918  cface.com /= cface.area;
919  }
920  }
Vector cross(Vector b)
Cross product.
Definition: vector.cpp:1667
vector< Vector > Vg
Definition: objlib.h:319
double area(Vector b)
Area subtended by two ::Vector.
Definition: vector.cpp:2018
Vector & normalize(double size=1.)
Normalize.
Definition: vector.cpp:1706
Vector Class.
Definition: vector.h:672
void Cosmos::wavefront::update_group ( group cgroup)
923  {
924  // Calculate Center of Mass
925  cgroup.com = Vector();
926  for (size_t j=0; j<cgroup.faceidx.size(); ++j)
927  {
928  cgroup.com += Faces[cgroup.faceidx[j]].com;
929  }
930  if (cgroup.faceidx.size())
931  {
932  cgroup.com /= cgroup.faceidx.size();
933  }
934 
935  // Calculate volume
936  cgroup.volume = 0.;
937  for (size_t j=0; j<cgroup.faceidx.size(); ++j)
938  {
939  Vector dv = Faces[(cgroup.faceidx[j])].com - cgroup.com;
940  if (dv.norm() != 0.)
941  {
942  cgroup.volume += Faces[(cgroup.faceidx[j])].area * dv.norm() / 3.;
943  }
944  }
945  }
double norm()
Norm.
Definition: vector.cpp:1735
vector< face > Faces
Definition: objlib.h:327
Vector Class.
Definition: vector.h:672
void Cosmos::wavefront::modify_groups ( vector< string >  groups)
108  {
109  cGroups.clear();
110  cGroups.push_back(0);
111 
112  for (size_t k=0; k<groups.size(); ++k)
113  {
114  // Only add groups with COSMOS name format
115  if (groups[k].find("cosmos.") == string::npos)
116  {
117  continue;
118  }
119  // Add group if not already present
120  bool newgroup = true;
121  for (size_t i=0; i<Groups.size(); ++i)
122  {
123  if (groups[k] == Groups[i].name)
124  {
125  newgroup = false;
126  cGroups.push_back(i);
127  break;
128  }
129  }
130  if (newgroup)
131  {
132  cGroups.push_back(Groups.size());
133  group tgroup;
134  tgroup.name = groups[k].substr(7);
135  tgroup.materialidx = 0;
136  Groups.push_back(tgroup);
137  }
138  }
139  }
vector< group > Groups
Definition: objlib.h:328
int i
Definition: rw_test.cpp:37
struct ip_mreq group
Definition: mcastclient.c:39
vector< size_t > cGroups
Definition: objlib.h:401
string name
Definition: objlib.h:329
void Cosmos::wavefront::modify_groups ( string  group)
101  {
102  vector <string> groups;
103  groups.push_back(group);
104  modify_groups(groups);
105  }
void modify_groups(vector< string > groups)
Definition: objlib.cpp:107
struct ip_mreq group
Definition: mcastclient.c:39
void Cosmos::wavefront::add_cuboid ( string  name,
Vector  size,
Quaternion  orientation,
Vector  offset 
)
142  {
144  face tface;
145  vertex tvertex;
146  Vector tvector;
147  for (int16_t i=-1; i<2; i+=2)
148  {
149  // x
150  tface.vertices.clear();
151  tvector = unitxV(i);
152  tvector = orientation.irotate(tvector);
153  tvector += offset;
154  tvertex.vn = add_normal_vertex(tvector);
155 
156  tvector = Vector(i*size.x/2., size.y/2., -i*size.z/2.);
157  tvector = orientation.irotate(tvector);
158  tvector += offset;
159  tvertex.v = add_geometric_vertex(tvector);
160  tface.vertices.push_back(tvertex);
161 
162  tvector = Vector(i*size.x/2., size.y/2., i*size.z/2.);
163  tvector = orientation.irotate(tvector);
164  tvector += offset;
165  tvertex.v = add_geometric_vertex(tvector);
166  tface.vertices.push_back(tvertex);
167 
168  tvector = Vector(i*size.x/2., -size.y/2., i*size.z/2.);
169  tvector = orientation.irotate(tvector);
170  tvector += offset;
171  tvertex.v = add_geometric_vertex(tvector);
172  tface.vertices.push_back(tvertex);
173 
174  tvector = Vector(i*size.x/2., -size.y/2., -i*size.z/2.);
175  tvector = orientation.irotate(tvector);
176  tvector += offset;
177  tvertex.v = add_geometric_vertex(tvector);
178  tface.vertices.push_back(tvertex);
179 
180  add_face(tface);
181 
182  // y
183  tface.vertices.clear();
184  tvector = unityV(i);
185  tvector = orientation.irotate(tvector);
186  tvector += offset;
187  tvertex.vn = add_normal_vertex(tvector);
188 
189  tvector = Vector(size.x/2., i*size.y/2., i*size.z/2.);
190  tvector = orientation.irotate(tvector);
191  tvector += offset;
192  tvertex.v = add_geometric_vertex(tvector);
193  tface.vertices.push_back(tvertex);
194 
195  tvector = Vector(size.x/2., i*size.y/2., -i*size.z/2.);
196  tvector = orientation.irotate(tvector);
197  tvector += offset;
198  tvertex.v = add_geometric_vertex(tvector);
199  tface.vertices.push_back(tvertex);
200 
201  tvector = Vector(-size.x/2., i*size.y/2., -i*size.z/2.);
202  tvector = orientation.irotate(tvector);
203  tvector += offset;
204  tvertex.v = add_geometric_vertex(tvector);
205  tface.vertices.push_back(tvertex);
206 
207  tvector = Vector(-size.x/2., i*size.y/2., i*size.z/2.);
208  tvector = orientation.irotate(tvector);
209  tvector += offset;
210  tvertex.v = add_geometric_vertex(tvector);
211  tface.vertices.push_back(tvertex);
212 
213  add_face(tface);
214 
215  // z
216  tface.vertices.clear();
217  tvector = unitzV(i);
218  tvector = orientation.irotate(tvector);
219  tvector += offset;
220  tvertex.vn = add_normal_vertex(tvector);
221 
222  tvector = Vector(size.x/2., size.y/2., i*size.z/2.);
223  tvector = orientation.irotate(tvector);
224  tvector += offset;
225  tvertex.v = add_geometric_vertex(tvector);
226  tface.vertices.push_back(tvertex);
227 
228  tvector = Vector(-i*size.x/2., i*size.y/2., i*size.z/2.);
229  tvector = orientation.irotate(tvector);
230  tvector += offset;
231  tvertex.v = add_geometric_vertex(tvector);
232  tface.vertices.push_back(tvertex);
233 
234  tvector = Vector(-size.x/2., -size.y/2., i*size.z/2.);
235  tvector = orientation.irotate(tvector);
236  tvector += offset;
237  tvertex.v = add_geometric_vertex(tvector);
238  tface.vertices.push_back(tvertex);
239 
240  tvector = Vector(i*size.x/2., -i*size.y/2., i*size.z/2.);
241  tvector = orientation.irotate(tvector);
242  tvector += offset;
243  tvertex.v = add_geometric_vertex(tvector);
244  tface.vertices.push_back(tvertex);
245 
246  add_face(tface);
247  }
248  }
Vector & clear(double x0=0., double y0=0., double z0=0., double w0=0.)
Clear.
Definition: vector.cpp:1693
int i
Definition: rw_test.cpp:37
void add_face(face face)
Definition: objlib.cpp:87
double y
Definition: vector.h:713
size_t add_normal_vertex(Vector v)
Definition: objlib.cpp:46
void modify_groups(vector< string > groups)
Definition: objlib.cpp:107
size_t add_geometric_vertex(Vector v)
Definition: objlib.cpp:34
double z
Definition: vector.h:714
Vector unitxV(double scale)
Definition: vector.cpp:2075
double x
Definition: vector.h:712
Vector unityV(double scale)
Definition: vector.cpp:2082
Vector Class.
Definition: vector.h:672
Vector irotate(const Vector &v)
Indirectly rotate a ::Vector using a ::Quaternion.
Definition: vector.cpp:2473
Vector unitzV(double scale)
Definition: vector.cpp:2089
string name
Definition: objlib.h:329
void Cosmos::wavefront::add_1u ( string  basename,
Vector  offset 
)
251  {
252  string name;
253 
254  // Verticals
255  name = "cosmos.none." + basename + "_z+x+y";
256  add_cuboid(name, Vector(.003, .003, .1), Math::Quaternions::eye(), offset + Vector(.0485, .0485, 0.));
257  name = "cosmos.none." + basename + "_z+x-y";
258  add_cuboid(name, Vector(.003, .003, .1), Math::Quaternions::eye(), offset + Vector(.0485, -.0485, 0.));
259  name = "cosmos.none." + basename + "_z-x-y";
260  add_cuboid(name, Vector(.003, .003, .1), Math::Quaternions::eye(), offset + Vector(-.0485, -.0485, 0.));
261  name = "cosmos.none." + basename + "_z-x+y";
262  add_cuboid(name, Vector(.003, .003, .1), Math::Quaternions::eye(), offset + Vector(-.0485, .0485, 0.));
263 
264  // Horizontals
265  name = "cosmos.none." + basename + "_+y+z";
266  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., .0485, .0485));
267  name = "cosmos.none." + basename + "_-y+z";
268  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., -.0485, .0485));
269  name = "cosmos.none." + basename + "_+x+z";
270  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(.0485, 0., .0485));
271  name = "cosmos.none." + basename + "_-x+z";
272  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(-.0485, 0., .0485));
273 
274  name = "cosmos.none." + basename + "_+y-z";
275  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., .0485, -.0485));
276  name = "cosmos.none." + basename + "_-y-z";
277  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., -.0485, -.0485));
278  name = "cosmos.none." + basename + "_+x-z";
279  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(.0485, 0., -.0485));
280  name = "cosmos.none." + basename + "_-x-z";
281  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(-.0485, 0., -.0485));
282 
283  }
void add_cuboid(string name, Vector size, Quaternion orientation, Vector offset)
Definition: objlib.cpp:141
Quaternion eye(double scale)
Definition: vector.cpp:2661
Vector Class.
Definition: vector.h:672
string name
Definition: objlib.h:329
void Cosmos::wavefront::add_3u ( string  basename,
Vector  offset 
)
286  {
287  string name;
288 
289  // Verticals
290  name = "cosmos.none." + basename + "_z+x+y";
291  add_cuboid(name, Vector(.003, .003, .3), Math::Quaternions::eye(), offset + Vector(.0485, .0485, 0.));
292  name = "cosmos.none." + basename + "_z+x-y";
293  add_cuboid(name, Vector(.003, .003, .3), Math::Quaternions::eye(), offset + Vector(.0485, -.0485, 0.));
294  name = "cosmos.none." + basename + "_z-x-y";
295  add_cuboid(name, Vector(.003, .003, .3), Math::Quaternions::eye(), offset + Vector(-.0485, -.0485, 0.));
296  name = "cosmos.none." + basename + "_z-x+y";
297  add_cuboid(name, Vector(.003, .003, .3), Math::Quaternions::eye(), offset + Vector(-.0485, .0485, 0.));
298 
299  // Horizontals
300  name = "cosmos.none." + basename + "_+y+z";
301  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., .0485, .1485));
302  name = "cosmos.none." + basename + "_-y+z";
303  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., -.0485, .1485));
304  name = "cosmos.none." + basename + "_+x+z";
305  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(.0485, 0., .1485));
306  name = "cosmos.none." + basename + "_-x+z";
307  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(-.0485, 0., .1485));
308 
309  name = "cosmos.none." + basename + "_+y-z";
310  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., .0485, -.1485));
311  name = "cosmos.none." + basename + "_-y-z";
312  add_cuboid(name, Vector(.1, .003, .003), Math::Quaternions::eye(), offset + Vector(0., -.0485, -.1485));
313  name = "cosmos.none." + basename + "_+x-z";
314  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(.0485, 0., -.1485));
315  name = "cosmos.none." + basename + "_-x-z";
316  add_cuboid(name, Vector(.003, .1, .003), Math::Quaternions::eye(), offset + Vector(-.0485, 0., -.1485));
317 
318  }
void add_cuboid(string name, Vector size, Quaternion orientation, Vector offset)
Definition: objlib.cpp:141
Quaternion eye(double scale)
Definition: vector.cpp:2661
Vector Class.
Definition: vector.h:672
string name
Definition: objlib.h:329
int32_t Cosmos::wavefront::load_file ( string  loc,
string  name 
)
702  {
703 
704  if (!data_isfile(loc+"/"+name))
705  {
706  return -errno;
707  }
708  location = loc;
709 
710  Vg.clear();
711  Vg.push_back(Vector());
712  Vt.clear();
713  Vt.push_back(Vector());
714  Vn.clear();
715  Vn.push_back(Vector());
716  Vp.clear();
717  Vp.push_back(Vector());
718  Points.clear();
719  Lines.clear();
720  Faces.clear();
721 
722  Materials.clear();
723  material tmaterial;
724  tmaterial.name = "Any";
725  tmaterial.density = 1.;
726  tmaterial.ambient = Vector(.5, .5, .5, 1.);
727  tmaterial.diffuse = Vector(.5, .5, .5, 1.);
728  Materials.push_back(tmaterial);
729 
730  Groups.clear();
731  group tgroup;
732  tgroup.name = "All";
733  Groups.push_back(tgroup);
734 
735  ifstream infile(loc+"/"+name);
736  string sinput;
737  while (std::getline(infile, sinput))
738  {
739  parseobj(sinput);
740  }
741  infile.close();
742 
743  // Calculate centroid and length of each line
744  for (line &nextl : Lines)
745  {
746  update_line(nextl);
747  }
748 
749  // Calculate centroid and area for each face
750  for (face &nextf : Faces)
751  {
752  update_face(nextf);
753  }
754 
755  // Calculate center of mass and volume for each Group
756  for (group &nextg : Groups)
757  {
758  update_group(nextg);
759  }
760 
761  rebase();
762 
763  return 0;
764  }
vector< group > Groups
Definition: objlib.h:328
vector< material > Materials
Definition: objlib.h:324
vector< Vector > Vt
Definition: objlib.h:320
vector< point > Points
Definition: objlib.h:325
vector< Vector > Vg
Definition: objlib.h:319
void update_line(line &cline)
Definition: objlib.cpp:854
vector< face > Faces
Definition: objlib.h:327
bool data_isfile(string path, off_t size)
Definition: datalib.cpp:1895
vector< Vector > Vn
Definition: objlib.h:321
struct ip_mreq group
Definition: mcastclient.c:39
void parseobj(string input)
Definition: objlib.cpp:320
vector< line > Lines
Definition: objlib.h:326
void update_group(group &cgroup)
Definition: objlib.cpp:922
void rebase()
Definition: objlib.cpp:654
void update_face(face &cface)
Definition: objlib.cpp:872
vector< Vector > Vp
Definition: objlib.h:322
Vector Class.
Definition: vector.h:672
string location
Definition: objlib.h:404
string name
Definition: objlib.h:329
int32_t Cosmos::wavefront::save_file ( string  loc,
string  name 
)
767  {
768  char outbuf[300];
769 
770  ofstream outfile(loc+"/"+name+".obj");
771  if (!outfile.is_open())
772  {
773  return -errno;
774  }
775 
776  rebase();
777 
778  outfile << "# Node Object Model" << endl;
779  outfile << "# File units = meters" << endl;
780  outfile << "mtllib material.mtl" << endl;
781  outfile << endl;
782 
783  for (size_t i=1; i<Vg.size(); ++i)
784  {
785  sprintf(outbuf, "v %f %f %f", Vg[i].x, Vg[i].y, Vg[i].z);
786  outfile << outbuf << endl;
787  }
788  outfile << endl;
789 
790  for (size_t i=1; i<Vt.size(); ++i)
791  {
792  sprintf(outbuf, "vt %f %f", Vt[i].x, Vt[i].y);
793  outfile << outbuf << endl;
794  }
795  outfile << endl;
796 
797  for (size_t i=1; i<Vn.size(); ++i)
798  {
799  sprintf(outbuf, "vn %f %f %f", Vn[i].x, Vn[i].y, Vn[i].z);
800  outfile << outbuf << endl;
801  }
802  outfile << endl;
803 
804  for (group &nextg : Groups)
805  {
806  if (nextg.name == "All")
807  {
808  continue;
809  }
810  sprintf(outbuf, "g cosmos.%s", nextg.name.c_str());
811  outfile << outbuf << endl;
812  for (size_t fidx : nextg.faceidx)
813  {
814  outfile << "f ";
815  for (vertex nextv : Faces[fidx].vertices)
816  {
817  if (nextv.vt == 0)
818  {
819  sprintf(outbuf, " %lu//%lu", nextv.v, nextv.vn);
820  }
821  else
822  {
823  sprintf(outbuf, " %lu/%lu/%lu", nextv.v, nextv.vt, nextv.vn);
824  }
825  outfile << outbuf;
826  }
827  outfile << endl;
828  }
829  outfile << endl;
830  }
831  return 0;
832  }
vector< group > Groups
Definition: objlib.h:328
y
Definition: inputfile.py:6
int i
Definition: rw_test.cpp:37
vector< Vector > Vt
Definition: objlib.h:320
vector< Vector > Vg
Definition: objlib.h:319
vector< face > Faces
Definition: objlib.h:327
vector< Vector > Vn
Definition: objlib.h:321
x
Definition: inputfile.py:6
struct ip_mreq group
Definition: mcastclient.c:39
void rebase()
Definition: objlib.cpp:654
string name
Definition: objlib.h:329
void Cosmos::wavefront::rebase ( )
655  {
656  for (face &nextf : Faces)
657  {
658  update_face(nextf);
659  }
660 
661  Vector tcom = Vector();
662  double tvolume = 0.;
663  for (group &nextg : Groups)
664  {
665  if (nextg.name == "All")
666  {
667  continue;
668  }
669  update_group(nextg);
670  tvolume += nextg.volume;
671  tcom += nextg.com * nextg.volume;
672  }
673  if (tvolume)
674  {
675  tcom /= tvolume;
676  }
677 
678  // Recenter all vectors to total Center of Mass
679  for (face &nextf : Faces)
680  {
681  nextf.com -= tcom;
682  nextf.normal -= tcom;
683  }
684 
685  for (group &nextg : Groups)
686  {
687  nextg.com -= tcom;
688  }
689 
690  for (Vector &nextv : Vg)
691  {
692  nextv -= tcom;
693  }
694 
695  for (Vector &nextv : Vn)
696  {
697  nextv -= tcom;
698  }
699  }
vector< group > Groups
Definition: objlib.h:328
vector< Vector > Vg
Definition: objlib.h:319
vector< face > Faces
Definition: objlib.h:327
vector< Vector > Vn
Definition: objlib.h:321
struct ip_mreq group
Definition: mcastclient.c:39
void update_group(group &cgroup)
Definition: objlib.cpp:922
void update_face(face &cface)
Definition: objlib.cpp:872
Vector Class.
Definition: vector.h:672
vector< string > Cosmos::wavefront::split ( string  str,
char  c = ' ' 
)
835  {
836  vector <string> result;
837  const char *strptr = str.data();
838 
839  do
840  {
841  const char *begin = strptr;
842 
843  while(*strptr != c && *strptr)
844  {
845  strptr++;
846  }
847 
848  result.push_back(string(begin, strptr));
849  } while (0 != *strptr++);
850 
851  return result;
852  }
const string & str
Definition: json11.cpp:360
json11::Json Cosmos::wavefront::to_json ( ) const
inline

Convert class contents to JSON object.

Returns a json11 JSON object of the class

Returns
A json11 JSON object containing every member variable within the class
335  {
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  }
vector< group > Groups
Definition: objlib.h:328
std::map< std::string, Json > object
Definition: json11.hpp:88
vector< material > Materials
Definition: objlib.h:324
vector< Vector > Vt
Definition: objlib.h:320
vector< point > Points
Definition: objlib.h:325
vector< Vector > Vg
Definition: objlib.h:319
vector< face > Faces
Definition: objlib.h:327
vector< Vector > Vn
Definition: objlib.h:321
vector< line > Lines
Definition: objlib.h:326
vector< Vector > Vp
Definition: objlib.h:322
string name
Definition: objlib.h:329
void Cosmos::wavefront::from_json ( const string &  s)
inline

Set class contents from JSON string.

Parses the provided JSON-formatted string and sets the class data. String should be formatted like the string returned from to_json()

Parameters
sJSON-formatted string to set class contents to
Returns
n/a
356  {
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  }
vector< group > Groups
Definition: objlib.h:328
Definition: json11.hpp:79
vector< material > Materials
Definition: objlib.h:324
int i
Definition: rw_test.cpp:37
vector< Vector > Vt
Definition: objlib.h:320
vector< point > Points
Definition: objlib.h:325
vector< Vector > Vg
Definition: objlib.h:319
static double * p
Definition: gauss_jackson_test.cpp:42
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
vector< Vector > Vn
Definition: objlib.h:321
void from_json(const string &s)
Set class contents from JSON string.
Definition: objlib.h:356
vector< line > Lines
Definition: objlib.h:326
string name
Definition: objlib.h:329
void Cosmos::wavefront::parseobj ( string  input)
private
321  {
322  size_t index = 0;
323  // Find command
324  while (isspace(input[index]))
325  {
326  if (++index == input.size())
327  {
328  return;
329  }
330  }
331  switch (input[index++])
332  {
333  case 'b':
334  // bmat, bevel
335  break;
336  case 'c':
337  // cstype, curv, curv2, con, c_interp, ctech
338  break;
339  case 'd':
340  // deg, d_interp
341  break;
342  case 'e':
343  // end
344  break;
345  case 'f':
346  // f
347  {
348  face tface;
349  do
350  {
351  while (index < input.size() && isspace(input[index]))
352  {
353  ++index;
354  }
355  string tval;
356  while (index < input.size() && !isspace(input[index]))
357  {
358  tval.push_back(input[index++]);
359  }
360  vertex tvertex;
361  if (tval.size())
362  {
363  if (tval.find("///") != string::npos)
364  {
365  sscanf(tval.data(), "%lu///", &tvertex.v);
366  }
367  else if (tval.find("//") != string::npos)
368  {
369  sscanf(tval.data(), "%lu//%lu", &tvertex.v, &tvertex.vn);
370  }
371  else
372  {
373  sscanf(tval.data(), "%lu/%lu/%lu", &tvertex.v, &tvertex.vt, &tvertex.vn);
374  }
375 
376  tface.vertices.push_back(tvertex);
377  }
378  } while (index < input.size());
379  add_face(tface);
380  }
381  break;
382  case 'g':
383  // g
384  {
385  vector <string> tgroups;
386  do
387  {
388  while (index < input.size() && isspace(input[index]))
389  {
390  ++index;
391  }
392  string tgroup;
393  while (index < input.size() && !isspace(input[index]))
394  {
395  tgroup.push_back(input[index++]);
396  }
397  if (tgroup.size())
398  {
399  tgroups.push_back(tgroup);
400  }
401  } while (index < input.size());
402  modify_groups(tgroups);
403  }
404  break;
405  case 'h':
406  //hole
407  break;
408  case 'l':
409  // l, lod
410  switch (input[index+1])
411  {
412  case 'o':
413  // lod
414  break;
415  default:
416  // l
417  {
418  line tline;
419  do
420  {
421  while (index < input.size() && isspace(input[index]))
422  {
423  ++index;
424  }
425  string tval;
426  while (index < input.size() && !isspace(input[index]))
427  {
428  tval.push_back(input[index++]);
429  }
430  vertex tvertex;
431  if (tval.size())
432  {
433  sscanf(tval.data(), "%lu/%lu", &tvertex.v, &tvertex.vt);
434  tline.vertices.push_back(tvertex);
435  }
436  } while (index < input.size());
437  add_line(tline);
438  }
439  break;
440  }
441  case 'm':
442  // mg, mtllib
443  switch (input[index])
444  {
445  case 't':
446  // mtllib
447  string mtlname = input.substr(input.find(" ")+1);
448  if (!data_isfile(location+"/"+mtlname))
449  {
450  break;
451  }
452  ifstream mtlfile(location+"/"+mtlname);
453  string sinput;
454  while (std::getline(mtlfile, sinput))
455  {
456  parsemtl(sinput);
457  }
458  mtlfile.close();
459  }
460  break;
461  case 'o':
462  // o
463  while (index < input.size() && isspace(input[index]))
464  {
465  ++index;
466  }
467  name = input.substr(index, input.size());
468  break;
469  case 'p':
470  // p, parm
471  switch (input[index+1])
472  {
473  case 'a':
474  // parm
475  break;
476  default:
477  // p
478  do
479  {
480  while (index < input.size() && isspace(input[index]))
481  {
482  ++index;
483  }
484  string tval;
485  while (index < input.size() && !isspace(input[index]))
486  {
487  tval.push_back(input[index++]);
488  }
489  if (tval.size())
490  {
491  point tpoint;
492  sscanf(&input[index], "%lu", &tpoint.vertex);
493  add_point(tpoint);
494  }
495  } while (index < input.size());
496  }
497  break;
498  case 's':
499  // step, surf, scrv, sp, s, shadow_obj, stech
500  break;
501  case 't':
502  // trim, trace_obj
503  break;
504  case 'u':
505  // usemtl
506  {
507  string mtlname = input.substr(input.find(" ")+1);
508  // Add materialidx to Groups
509  for (size_t j=0; j<Materials.size(); ++j)
510  {
511  if (Materials[j].name == mtlname)
512  {
513  for (size_t i=0; i<cGroups.size(); ++i)
514  {
515  Groups[cGroups[i]].materialidx = j;
516  }
517  }
518  }
519  }
520  break;
521  case 'v':
522  // v, vt, vn, vp
523  Vector tvec;
524  switch (input[index])
525  {
526  case 't':
527  {
528  ++index;
529  int i = sscanf(&input[index], "%lf %lf %lf", &tvec.x, &tvec.y, &tvec.z);
530  if (i == 1 || i == 2 || i == 3)
531  {
532  add_texture_vertex(tvec);
533  }
534  }
535  break;
536  case 'n':
537  {
538  ++index;
539  int i = sscanf(&input[index], "%lf %lf %lf", &tvec.x, &tvec.y, &tvec.z);
540  if (i == 1 || i == 2 || i == 3)
541  {
542  add_normal_vertex(tvec);
543  }
544  }
545  break;
546  case 'p':
547  {
548  ++index;
549  int i = sscanf(&input[index], "%lf %lf %lf", &tvec.x, &tvec.y, &tvec.z);
550  if (i == 1 || i == 2 || i == 3)
551  {
552  add_parameter_vertex(tvec);
553  }
554  }
555  break;
556  default:
557  {
558  int i = sscanf(&input[index], "%lf %lf %lf %lf", &tvec.x, &tvec.y, &tvec.z, &tvec.w);
559  if (i == 3 || i == 4)
560  {
561  // tvec /= 1000.;
562  add_geometric_vertex(tvec);
563  }
564  }
565  break;
566  }
567  break;
568  }
569  }
size_t add_texture_vertex(Vector v)
Definition: objlib.cpp:40
vector< group > Groups
Definition: objlib.h:328
vector< material > Materials
Definition: objlib.h:324
int i
Definition: rw_test.cpp:37
void add_face(face face)
Definition: objlib.cpp:87
void add_line(line line)
Definition: objlib.cpp:75
double y
Definition: vector.h:713
size_t add_normal_vertex(Vector v)
Definition: objlib.cpp:46
void modify_groups(vector< string > groups)
Definition: objlib.cpp:107
size_t add_parameter_vertex(Vector v)
Definition: objlib.cpp:52
size_t add_geometric_vertex(Vector v)
Definition: objlib.cpp:34
double z
Definition: vector.h:714
void add_point(point point)
Definition: objlib.cpp:63
bool data_isfile(string path, off_t size)
Definition: datalib.cpp:1895
double x
Definition: vector.h:712
Vector Class.
Definition: vector.h:672
string location
Definition: objlib.h:404
double w
Definition: vector.h:715
vector< size_t > cGroups
Definition: objlib.h:401
void parsemtl(string input)
Definition: objlib.cpp:571
string name
Definition: objlib.h:329
void Cosmos::wavefront::parsemtl ( string  input)
private
572  {
573  size_t index = 0;
574  // Find command
575  while (isspace(input[index]))
576  {
577  if (++index == input.size())
578  {
579  return;
580  }
581  }
582 
583  switch (input[index++])
584  {
585  case 'd':
586  {
587  sscanf(&input[index], "%f", &Materials.back().density);
588  }
589  break;
590  case 'n':
591  {
592  struct material tmaterial;
593  tmaterial.name = input.substr(input.find(" ")+1);
594  tmaterial.density = 1.;
595  tmaterial.ambient = Vector(.1,.1,.1);
596  tmaterial.diffuse = Vector(.9, .9, .9);
597  add_material(tmaterial);
598  }
599  break;
600  case 'K':
601  Vector tval;
602  switch (input[index])
603  {
604  case 'a':
605  {
606  ++index;
607  int i = sscanf(&input[index], "%lf %lf %lf", &tval[0], &tval[1], &tval[2]);
608  if (i == 1)
609  {
610  tval[1] = tval[0];
611  tval[2] = tval[0];
612  }
613  if (i)
614  {
615  Materials.back().ambient = tval;
616  }
617  }
618  break;
619  case 'd':
620  {
621  ++index;
622  int i = sscanf(&input[index], "%lf %lf %lf", &tval[0], &tval[1], &tval[2]);
623  if (i == 1)
624  {
625  tval[1] = tval[0];
626  tval[2] = tval[0];
627  }
628  if (i)
629  {
630  Materials.back().diffuse = tval;
631  }
632  }
633  break;
634  case 's':
635  {
636  ++index;
637  int i = sscanf(&input[index], "%lf %lf %lf", &tval[0], &tval[1], &tval[2]);
638  if (i == 1)
639  {
640  tval[1] = tval[0];
641  tval[2] = tval[0];
642  }
643  if (i)
644  {
645  Materials.back().specular = tval;
646  }
647  }
648  break;
649  }
650  break;
651  }
652  }
vector< material > Materials
Definition: objlib.h:324
int i
Definition: rw_test.cpp:37
void add_material(material material)
Definition: objlib.cpp:58
Vector Class.
Definition: vector.h:672

Member Data Documentation

vector<Vector> Cosmos::wavefront::Vg
vector<Vector> Cosmos::wavefront::Vt
vector<Vector> Cosmos::wavefront::Vn
vector<Vector> Cosmos::wavefront::Vp
vector<material> Cosmos::wavefront::Materials
vector<point> Cosmos::wavefront::Points
vector<line> Cosmos::wavefront::Lines
vector<face> Cosmos::wavefront::Faces
vector<group> Cosmos::wavefront::Groups
string Cosmos::wavefront::name = ""
vector<size_t> Cosmos::wavefront::cGroups
private
string Cosmos::wavefront::location = ""
private

The documentation for this class was generated from the following files: