Last active
January 30, 2019 07:31
-
-
Save pierceh89/6b4816bbfd3d41419337 to your computer and use it in GitHub Desktop.
Very simple converter from Wavefront obj to Point Cloud Data(PCD) format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
#include <string> | |
#include <vector> | |
#include <Eigen/Core> | |
#include <Eigen/Dense> | |
using std::string; | |
using std::vector; | |
using std::fstream; | |
using std::cerr; | |
using std::endl; | |
using std::cout; | |
using std::stringstream; | |
using Eigen::Vector4f; | |
using Eigen::Vector3f; | |
using Eigen::Vector2f; | |
using Eigen::Matrix3f; | |
using Eigen::Matrix4f; | |
using Eigen::MatrixXf; | |
vector<Vector3f > vertData, colData, normData; | |
vector<Vector2f > texData; | |
void objLoader(const string& fname, vector<Vector3f >& vertices, vector<Vector3f >& color, | |
vector<Vector2f >& tex, vector<Vector3f >& normals) | |
{ | |
fstream file; | |
const unsigned int len = 256; | |
char line[len]; | |
file.open(fname.c_str()); | |
if (!file.is_open()) | |
{ | |
cerr << "Cannot open File" << endl; | |
exit(0); | |
} | |
while (file.getline(line, len)) | |
{ | |
if (line[0] == 'v') | |
{ | |
stringstream ss(line + 2); | |
switch (line[1]) | |
{ | |
case 't': | |
{ | |
Vector2f t; | |
ss >> t[0] >> t[1]; | |
tex.push_back(t); | |
} | |
break; | |
case 'n': | |
{ | |
Vector3f n; | |
ss >> n[0] >> n[1] >> n[2]; | |
normals.push_back(n); | |
} | |
break; | |
case ' ': | |
{ | |
Vector3f v; | |
ss >> v[0] >> v[1] >> v[2]; | |
vertices.push_back(v); | |
Vector3f c; | |
ss >> c[0] >> c[1] >> c[2]; | |
color.push_back(c); | |
} | |
break; | |
} | |
} | |
} | |
} | |
void objWriter(const string& fname, const vector<Vector3f >& points) | |
{ | |
std::ofstream file; | |
file.open(fname.c_str()); | |
if (!file.is_open()) | |
{ | |
cerr << "Writing obj: file open error" << endl; | |
return; | |
} | |
// pcd format header | |
// version | |
// fields | |
// size | |
// type | |
// count | |
// width | |
// height | |
// viewpoint | |
// points | |
file << "VERSION " << .7 << endl | |
<< "FIELDS x y z" << endl | |
<< "SIZE 4 4 4" << endl | |
<< "TYPE F F F" << endl | |
<< "COUNT 1 1 1" << endl | |
<< "WIDTH " << points.size() << endl | |
<< "HEIGHT 1" << endl | |
<< "VIEWPOINT 0 0 0 1 0 0 0 " << endl | |
<< "POINTS " << points.size() << endl | |
<< "DATA ascii" << endl; | |
for (size_t i = 0; i < points.size(); i++) | |
file << points[i](0) << " " << points[i](1) << " " << points[i](2) << endl; | |
file.close(); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if (argc != 2) | |
{ | |
cerr << "Invalid arguments" << endl; | |
exit(EXIT_FAILURE); | |
} | |
objLoader(argv[1], vertData, colData, texData, normData); | |
string outFileName(argv[1]); | |
size_t p = outFileName.find_last_of("."); | |
outFileName.replace(p + 1,3, "pcd"); | |
objWriter(outFileName, vertData); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment