STLIO.cpp
Go to the documentation of this file.
1 
28 /*
29  * STLIO.cpp
30  *
31  * Created on: Dec 9, 2016
32  * Author: robot
33  */
34 
35 
36 #include "lvr2/io/STLIO.hpp"
37 #include "lvr2/io/Timestamp.hpp"
39 #include "lvr2/geometry/Normal.hpp"
40 
41 #include <iostream>
42 #include <cstdio>
43 #include <cstring>
44 #include <stdint.h>
45 #include <fstream>
46 
47 using std::cout;
48 using std::endl;
49 
50 namespace lvr2
51 {
52 
54  // TODO Auto-generated constructor stub
55 
56 }
57 
59  // TODO Auto-generated destructor stub
60 }
61 
63 {
64  return ModelPtr(new Model);
65 }
66 
67 void STLIO::save( string filename )
68 {
69  save(this->m_model, filename);
70 }
71 
72 void STLIO::save( ModelPtr model, string filename )
73 {
74 
75  MeshBufferPtr mesh = model->m_mesh;
76  size_t n_vert = mesh->numVertices();
77  size_t n_faces = mesh->numFaces();
78  floatArr vertices = mesh->getVertices();
79  indexArray indices = mesh->getFaceIndices();
80 
81  std::string header_info = "Created by LVR";
82  char head[80];
83  std::strncpy(head,header_info.c_str(),sizeof(head)-1);
84  char attribute[2] = "0";
85 
86  std::ofstream myfile(filename.c_str());
87 
88  myfile.write(head,sizeof(head));
89  myfile.write((char*)&n_faces,4);
90 
91  if(myfile.good())
92  {
93  for(int i = 0; i < n_faces; i++)
94  {
95  int a = (int)indices[3 * i];
96  int b = (int)indices[3 * i + 1];
97  int c = (int)indices[3 * i + 2];
98 
99  using Vec = BaseVector<float>;
100  Vec v1;
101  Vec v2;
102  Vec v3;
103 
104  v1.x = vertices[3 * a];
105  v1.y = vertices[3 * a + 1];
106  v1.z = vertices[3 * a + 2];
107 
108  v2.x = vertices[3 * b];
109  v2.y = vertices[3 * b + 1];
110  v2.z = vertices[3 * b + 2];
111 
112  v3.x = vertices[3 * c];
113  v3.y = vertices[3 * c + 1];
114  v3.z = vertices[3 * c + 2];
115 
116  Normal<float> normal( (v1 - v2).cross(v1 - v3));
117 
118  myfile.write( (char*)&normal.x, 4);
119  myfile.write( (char*)&normal.y, 4);
120  myfile.write( (char*)&normal.z, 4);
121 
122  myfile.write( (char*)&v1.x, 4);
123  myfile.write( (char*)&v1.y, 4);
124  myfile.write( (char*)&v1.z, 4);
125 
126  myfile.write( (char*)&v2.x, 4);
127  myfile.write( (char*)&v2.y, 4);
128  myfile.write( (char*)&v2.z, 4);
129 
130  myfile.write( (char*)&v3.x, 4);
131  myfile.write( (char*)&v3.y, 4);
132  myfile.write( (char*)&v3.z, 4);
133 
134  uint16_t u = 0;
135  myfile.write( (char*)attribute, 2 );
136  }
137  }
138  else
139  {
140  cout << timestamp << "Could not open file " << filename << " for writing." << endl;
141  }
142 }
143 
144 } /* namespace lvr2 */
virtual void save(string filename)
Definition: STLIO.cpp:67
HalfEdgeMesh< Vec > mesh
std::shared_ptr< MeshBuffer > MeshBufferPtr
Definition: MeshBuffer.hpp:217
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
ModelPtr m_model
Definition: BaseIO.hpp:104
boost::shared_array< unsigned int > indexArray
Definition: DataStruct.hpp:128
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
virtual ~STLIO()
Definition: STLIO.cpp:58
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:80
virtual ModelPtr read(string filename)
Parse the given file and load supported elements.
Definition: STLIO.cpp:62
__kf_hdevice__ float3 cross(const float3 &v1, const float3 &v2)
Definition: temp_utils.hpp:102


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:09