Rename vertex_index struct.

This commit is contained in:
Syoyo Fujita
2016-08-13 15:27:35 +09:00
parent 319746d3db
commit 110b49a0c8
2 changed files with 46 additions and 46 deletions

View File

@@ -301,19 +301,19 @@ typedef struct {
unsigned int length; unsigned int length;
} shape_t; } shape_t;
struct vertex_index { struct index_t {
int v_idx, vt_idx, vn_idx; int vertex_index, texcoord_index, normal_index;
vertex_index() : v_idx(-1), vt_idx(-1), vn_idx(-1) {} index_t() : vertex_index(-1), texcoord_index(-1), normal_index(-1) {}
explicit vertex_index(int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx) {} explicit index_t(int idx) : vertex_index(idx), texcoord_index(idx), normal_index(idx) {}
vertex_index(int vidx, int vtidx, int vnidx) index_t(int vidx, int vtidx, int vnidx)
: v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx) {} : vertex_index(vidx), texcoord_index(vtidx), normal_index(vnidx) {}
}; };
typedef struct { typedef struct {
std::vector<float, lt::allocator<float> > vertices; std::vector<float, lt::allocator<float> > vertices;
std::vector<float, lt::allocator<float> > normals; std::vector<float, lt::allocator<float> > normals;
std::vector<float, lt::allocator<float> > texcoords; std::vector<float, lt::allocator<float> > texcoords;
std::vector<vertex_index, lt::allocator<vertex_index> > faces; std::vector<index_t, lt::allocator<index_t> > indices;
std::vector<int, lt::allocator<int> > face_num_verts; std::vector<int, lt::allocator<int> > face_num_verts;
std::vector<int, lt::allocator<int> > material_ids; std::vector<int, lt::allocator<int> > material_ids;
} attrib_t; } attrib_t;
@@ -386,11 +386,11 @@ static inline int fixIndex(int idx, int n) {
} }
// Parse raw triples: i, i/j/k, i//k, i/j // Parse raw triples: i, i/j/k, i//k, i/j
static vertex_index parseRawTriple(const char **token) { static index_t parseRawTriple(const char **token) {
vertex_index vi( index_t vi(
static_cast<int>(0x80000000)); // 0x80000000 = -2147483648 = invalid static_cast<int>(0x80000000)); // 0x80000000 = -2147483648 = invalid
vi.v_idx = my_atoi((*token)); vi.vertex_index = my_atoi((*token));
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -403,7 +403,7 @@ static vertex_index parseRawTriple(const char **token) {
// i//k // i//k
if ((*token)[0] == '/') { if ((*token)[0] == '/') {
(*token)++; (*token)++;
vi.vn_idx = my_atoi((*token)); vi.normal_index = my_atoi((*token));
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -412,7 +412,7 @@ static vertex_index parseRawTriple(const char **token) {
} }
// i/j/k or i/j // i/j/k or i/j
vi.vt_idx = my_atoi((*token)); vi.texcoord_index = my_atoi((*token));
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -423,7 +423,7 @@ static vertex_index parseRawTriple(const char **token) {
// i/j/k // i/j/k
(*token)++; // skip '/' (*token)++; // skip '/'
vi.vn_idx = my_atoi((*token)); vi.normal_index = my_atoi((*token));
while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' && while ((*token)[0] != '\0' && (*token)[0] != '/' && (*token)[0] != ' ' &&
(*token)[0] != '\t' && (*token)[0] != '\r') { (*token)[0] != '\t' && (*token)[0] != '\r') {
(*token)++; (*token)++;
@@ -891,8 +891,8 @@ typedef struct {
float tx, ty; float tx, ty;
// for f // for f
std::vector<vertex_index, lt::allocator<vertex_index> > f; std::vector<index_t, lt::allocator<index_t> > f;
// std::vector<vertex_index> f; // std::vector<index_t> f;
std::vector<int, lt::allocator<int> > f_num_verts; std::vector<int, lt::allocator<int> > f_num_verts;
const char *group_name; const char *group_name;
@@ -913,13 +913,13 @@ struct CommandCount {
size_t num_vn; size_t num_vn;
size_t num_vt; size_t num_vt;
size_t num_f; size_t num_f;
size_t num_faces; size_t num_indices;
CommandCount() { CommandCount() {
num_v = 0; num_v = 0;
num_vn = 0; num_vn = 0;
num_vt = 0; num_vt = 0;
num_f = 0; num_f = 0;
num_faces = 0; num_indices = 0;
} }
}; };
@@ -1006,10 +1006,10 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
token += 2; token += 2;
skip_space(&token); skip_space(&token);
StackVector<vertex_index, 8> f; StackVector<index_t, 8> f;
while (!IS_NEW_LINE(token[0])) { while (!IS_NEW_LINE(token[0])) {
vertex_index vi = parseRawTriple(&token); index_t vi = parseRawTriple(&token);
skip_space_and_cr(&token); skip_space_and_cr(&token);
f->push_back(vi); f->push_back(vi);
@@ -1018,9 +1018,9 @@ static bool parseLine(Command *command, const char *p, size_t p_len,
command->type = COMMAND_F; command->type = COMMAND_F;
if (triangulate) { if (triangulate) {
vertex_index i0 = f[0]; index_t i0 = f[0];
vertex_index i1(-1); index_t i1(-1);
vertex_index i2 = f[1]; index_t i2 = f[1];
for (size_t k = 2; k < f->size(); k++) { for (size_t k = 2; k < f->size(); k++) {
i1 = i2; i1 = i2;
@@ -1142,7 +1142,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
attrib->vertices.clear(); attrib->vertices.clear();
attrib->normals.clear(); attrib->normals.clear();
attrib->texcoords.clear(); attrib->texcoords.clear();
attrib->faces.clear(); attrib->indices.clear();
attrib->face_num_verts.clear(); attrib->face_num_verts.clear();
attrib->material_ids.clear(); attrib->material_ids.clear();
shapes->clear(); shapes->clear();
@@ -1288,7 +1288,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
command_count[t].num_vt++; command_count[t].num_vt++;
} else if (command.type == COMMAND_F) { } else if (command.type == COMMAND_F) {
command_count[t].num_f += command.f.size(); command_count[t].num_f += command.f.size();
command_count[t].num_faces++; command_count[t].num_indices++;
} }
if (command.type == COMMAND_MTLLIB) { if (command.type == COMMAND_MTLLIB) {
@@ -1352,13 +1352,13 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
size_t num_vn = 0; size_t num_vn = 0;
size_t num_vt = 0; size_t num_vt = 0;
size_t num_f = 0; size_t num_f = 0;
size_t num_faces = 0; size_t num_indices = 0;
for (size_t t = 0; t < num_threads; t++) { for (size_t t = 0; t < num_threads; t++) {
num_v += command_count[t].num_v; num_v += command_count[t].num_v;
num_vn += command_count[t].num_vn; num_vn += command_count[t].num_vn;
num_vt += command_count[t].num_vt; num_vt += command_count[t].num_vt;
num_f += command_count[t].num_f; num_f += command_count[t].num_f;
num_faces += command_count[t].num_faces; num_indices += command_count[t].num_indices;
} }
//std::cout << "# v " << num_v << std::endl; //std::cout << "# v " << num_v << std::endl;
@@ -1374,9 +1374,9 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
attrib->vertices.resize(num_v * 3); attrib->vertices.resize(num_v * 3);
attrib->normals.resize(num_vn * 3); attrib->normals.resize(num_vn * 3);
attrib->texcoords.resize(num_vt * 2); attrib->texcoords.resize(num_vt * 2);
attrib->faces.resize(num_f); attrib->indices.resize(num_f);
attrib->face_num_verts.resize(num_faces); attrib->face_num_verts.resize(num_indices);
attrib->material_ids.resize(num_faces); attrib->material_ids.resize(num_indices);
size_t v_offsets[kMaxThreads]; size_t v_offsets[kMaxThreads];
size_t n_offsets[kMaxThreads]; size_t n_offsets[kMaxThreads];
@@ -1395,7 +1395,7 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
n_offsets[t] = n_offsets[t - 1] + command_count[t - 1].num_vn; n_offsets[t] = n_offsets[t - 1] + command_count[t - 1].num_vn;
t_offsets[t] = t_offsets[t - 1] + command_count[t - 1].num_vt; t_offsets[t] = t_offsets[t - 1] + command_count[t - 1].num_vt;
f_offsets[t] = f_offsets[t - 1] + command_count[t - 1].num_f; f_offsets[t] = f_offsets[t - 1] + command_count[t - 1].num_f;
face_offsets[t] = face_offsets[t - 1] + command_count[t - 1].num_faces; face_offsets[t] = face_offsets[t - 1] + command_count[t - 1].num_indices;
} }
StackVector<std::thread, 16> workers; StackVector<std::thread, 16> workers;
@@ -1441,11 +1441,11 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
t_count++; t_count++;
} else if (commands[t][i].type == COMMAND_F) { } else if (commands[t][i].type == COMMAND_F) {
for (size_t k = 0; k < commands[t][i].f.size(); k++) { for (size_t k = 0; k < commands[t][i].f.size(); k++) {
vertex_index &vi = commands[t][i].f[k]; index_t &vi = commands[t][i].f[k];
int v_idx = fixIndex(vi.v_idx, v_count); int vertex_index = fixIndex(vi.vertex_index, v_count);
int vt_idx = fixIndex(vi.vt_idx, t_count); int texcoord_index = fixIndex(vi.texcoord_index, t_count);
int vn_idx = fixIndex(vi.vn_idx, n_count); int normal_index = fixIndex(vi.normal_index, n_count);
attrib->faces[f_count + k] = vertex_index(v_idx, vt_idx, vn_idx); attrib->indices[f_count + k] = index_t(vertex_index, texcoord_index, normal_index);
} }
attrib->material_ids[face_count] = material_id; attrib->material_ids[face_count] = material_id;
attrib->face_num_verts[face_count] = commands[t][i].f.size(); attrib->face_num_verts[face_count] = commands[t][i].f.size();
@@ -1552,8 +1552,8 @@ bool parseObj(attrib_t *attrib, std::vector<shape_t> *shapes, const char *buf,
std::cout << "# of vertices = " << attrib->vertices.size() << std::endl; std::cout << "# of vertices = " << attrib->vertices.size() << std::endl;
std::cout << "# of normals = " << attrib->normals.size() << std::endl; std::cout << "# of normals = " << attrib->normals.size() << std::endl;
std::cout << "# of texcoords = " << attrib->texcoords.size() << std::endl; std::cout << "# of texcoords = " << attrib->texcoords.size() << std::endl;
std::cout << "# of face indices = " << attrib->faces.size() << std::endl; std::cout << "# of face indices = " << attrib->indices.size() << std::endl;
std::cout << "# of faces = " << attrib->material_ids.size() << std::endl; std::cout << "# of indices = " << attrib->material_ids.size() << std::endl;
std::cout << "# of shapes = " << shapes->size() << std::endl; std::cout << "# of shapes = " << shapes->size() << std::endl;
} }

View File

@@ -244,15 +244,15 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
for (size_t v = 0; v < attrib.face_num_verts.size(); v++) { for (size_t v = 0; v < attrib.face_num_verts.size(); v++) {
assert(attrib.face_num_verts[v] % 3 == 0); // assume all triangle face. assert(attrib.face_num_verts[v] % 3 == 0); // assume all triangle face.
for (size_t f = 0; f < attrib.face_num_verts[v] / 3; f++) { for (size_t f = 0; f < attrib.face_num_verts[v] / 3; f++) {
tinyobj_opt::vertex_index idx0 = attrib.faces[face_offset+3*f+0]; tinyobj_opt::index_t idx0 = attrib.indices[face_offset+3*f+0];
tinyobj_opt::vertex_index idx1 = attrib.faces[face_offset+3*f+1]; tinyobj_opt::index_t idx1 = attrib.indices[face_offset+3*f+1];
tinyobj_opt::vertex_index idx2 = attrib.faces[face_offset+3*f+2]; tinyobj_opt::index_t idx2 = attrib.indices[face_offset+3*f+2];
float v[3][3]; float v[3][3];
for (int k = 0; k < 3; k++) { for (int k = 0; k < 3; k++) {
int f0 = idx0.v_idx; int f0 = idx0.vertex_index;
int f1 = idx1.v_idx; int f1 = idx1.vertex_index;
int f2 = idx2.v_idx; int f2 = idx2.vertex_index;
assert(f0 >= 0); assert(f0 >= 0);
assert(f1 >= 0); assert(f1 >= 0);
assert(f2 >= 0); assert(f2 >= 0);
@@ -271,9 +271,9 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3], const char* filename, int n
float n[3][3]; float n[3][3];
if (attrib.normals.size() > 0) { if (attrib.normals.size() > 0) {
int nf0 = idx0.vn_idx; int nf0 = idx0.normal_index;
int nf1 = idx1.vn_idx; int nf1 = idx1.normal_index;
int nf2 = idx2.vn_idx; int nf2 = idx2.normal_index;
if (nf0 >= 0 && nf1 >= 0 && nf2 >= 0) { if (nf0 >= 0 && nf1 >= 0 && nf2 >= 0) {
assert(3*nf0+2 < attrib.normals.size()); assert(3*nf0+2 < attrib.normals.size());