Created
November 6, 2018 10:16
-
-
Save 1f0/11b29e05923bb93741acd9f52d7ffbbd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//!Makefile, replace $USER with your name | |
//INC=-I/home/$USER/libs/libigl/include -I/home/$USER/libs/eigen3 | |
//all: | |
// g++ -std=c++11 $(INC) voxel.cpp -o voxel.out | |
#include <igl/bounding_box.h> | |
#include <igl/readOBJ.h> | |
#include <igl/writeOFF.h> | |
#include <string> | |
#include <vector> | |
using namespace Eigen; | |
using namespace std; | |
void push_vertex(double x, double y, double z, double len, vector<Vector3d> &list) { | |
for(size_t i=0;i<2;++i) | |
for(size_t j=0;j<2;++j) | |
for(size_t k=0;k<2;++k){ | |
list.emplace_back(x + i*len, y + j*len, z + k*len); | |
} | |
} | |
void push_face(size_t cnt, vector<Vector3i> &list) { | |
size_t begin = list.size(); | |
list.emplace_back(0, 1, 2); | |
list.emplace_back(1, 3, 2); | |
list.emplace_back(0, 4, 1); | |
list.emplace_back(1, 4, 5); | |
list.emplace_back(4, 7, 5); | |
list.emplace_back(7, 4, 6); | |
list.emplace_back(1, 5, 7); | |
list.emplace_back(1, 7, 3); | |
list.emplace_back(6, 3, 7); | |
list.emplace_back(6, 2, 3); | |
list.emplace_back(6, 4, 0); | |
list.emplace_back(6, 0, 2); | |
for(size_t i=begin;i<begin+12;++i){ | |
list[i][0] += 8 *cnt; | |
list[i][1] += 8 *cnt; | |
list[i][2] += 8 *cnt; | |
} | |
} | |
template<class S, class T> | |
void stdvec_to_eigen(const S& src, T& dst){ | |
for(size_t i=0;i < src.size(); ++i){ | |
dst(i, 0) = src[i][0]; | |
dst(i, 1) = src[i][1]; | |
dst(i, 2) = src[i][2]; | |
} | |
} | |
int main(int argc, char *argv[]){ | |
MatrixXd V, F; | |
igl::readOBJ(argv[1], V, F); | |
MatrixXd BV, BF; | |
igl::bounding_box(V, BV, BF); | |
double len = stod(argv[2]); | |
vector<Vector3d> vlist; | |
vector<Vector3i> flist; | |
size_t cnt = 0; | |
for(double x = BV(7, 0); x + len <= BV(0, 0); x += len) | |
for(double y = BV(7, 1); y + len <= BV(0, 1); y += len) | |
for(double z = BV(7, 2); z + len <= BV(0, 2); z += len, ++cnt) { | |
push_vertex(x, y, z, len, vlist); | |
push_face(cnt, flist); | |
} | |
MatrixXd OV(vlist.size(), 3); | |
MatrixXi OF(flist.size(), 3); | |
stdvec_to_eigen(vlist, OV); | |
stdvec_to_eigen(flist, OF); | |
igl::writeOFF(argv[3], OV, OF); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment