1 Commits
warn ... curve

Author SHA1 Message Date
Syoyo Fujita
98da6829ac Initial support of parsing curve primitive('curv')
Add clang-format style file.
2015-08-20 18:17:13 +09:00
4 changed files with 776 additions and 621 deletions

7
.clang-format Normal file
View File

@@ -0,0 +1,7 @@
---
BasedOnStyle: LLVM
IndentWidth: 2
TabWidth: 2
UseTab: Always
BreakBeforeBraces: Attach
Standard: Cpp03

36
test.cc
View File

@@ -7,9 +7,10 @@
#include <sstream>
#include <fstream>
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials)
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::curve_t>& curves, const std::vector<tinyobj::material_t>& materials)
{
std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of curves : " << curves.size() << std::endl;
std::cout << "# of materials : " << materials.size() << std::endl;
for (size_t i = 0; i < shapes.size(); i++) {
@@ -31,6 +32,29 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
}
}
for (size_t i = 0; i < curves.size(); i++) {
printf("curve[%ld].name = %s\n", i, curves[i].name.c_str());
printf("Size of curve[%ld].indices: %ld\n", i, curves[i].indices.size());
printf("curves[%ld].vertices: %ld\n", i, curves[i].positions.size());
assert((curves[i].positions.size() % 3) == 0);
for (size_t v = 0; v < curves[i].positions.size() / 3; v++) {
printf(" v[%ld] = (%f, %f, %f)\n", v,
curves[i].positions[3*v+0],
curves[i].positions[3*v+1],
curves[i].positions[3*v+2]);
}
for (size_t v = 0; v < curves[i].u_params.size(); v++) {
printf(" u[%ld] = %f\n", v, curves[i].u_params[v]);
}
for (size_t v = 0; v < curves[i].v_params.size(); v++) {
printf(" u[%ld] = %f\n", v, curves[i].v_params[v]);
}
}
for (size_t i = 0; i < materials.size(); i++) {
printf("material[%ld].name = %s\n", i, materials[i].name.c_str());
printf(" material.Ka = (%f, %f ,%f)\n", materials[i].ambient[0], materials[i].ambient[1], materials[i].ambient[2]);
@@ -63,15 +87,16 @@ TestLoadObj(
std::cout << "Loading " << filename << std::endl;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::curve_t> curves;
std::vector<tinyobj::material_t> materials;
std::string err = tinyobj::LoadObj(shapes, materials, filename, basepath);
std::string err = tinyobj::LoadObj(shapes, curves, materials, filename, basepath);
if (!err.empty()) {
std::cerr << err << std::endl;
return false;
}
PrintInfo(shapes, materials);
PrintInfo(shapes, curves, materials);
return true;
}
@@ -163,15 +188,16 @@ std::string matStream(
MaterialStringStreamReader matSSReader(matStream);
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::curve_t> curves;
std::vector<tinyobj::material_t> materials;
std::string err = tinyobj::LoadObj(shapes, materials, objStream, matSSReader);
std::string err = tinyobj::LoadObj(shapes, curves, materials, objStream, matSSReader);
if (!err.empty()) {
std::cerr << err << std::endl;
return false;
}
PrintInfo(shapes, materials);
PrintInfo(shapes, curves, materials);
return true;
}

View File

@@ -5,8 +5,10 @@
//
//
// version 0.9.14: Initial support of parsing curve primitive.
// version 0.9.13: Report "Material file not found message" in `err`(#46)
// version 0.9.12: Fix groups being ignored if they have 'usemtl' just before 'g' (#44)
// version 0.9.12: Fix groups being ignored if they have 'usemtl' just before
// 'g' (#44)
// version 0.9.11: Invert `Tr` parameter(#43)
// version 0.9.10: Fix seg fault on windows.
// version 0.9.9 : Replace atof() with custom parser.
@@ -16,7 +18,8 @@
// version 0.9.6 : Support Ni(index of refraction) mtl parameter.
// Parse transmittance material parameter correctly.
// version 0.9.5 : Parse multiple group name.
// Add support of specifying the base path to load material file.
// Add support of specifying the base path to load material
// file.
// version 0.9.4 : Initial suupport of group tag(g)
// version 0.9.3 : Fix parsing triple 'x/y/z'
// version 0.9.2 : Add more .mtl load support
@@ -75,8 +78,10 @@ static inline bool isNewLine(const char c) {
// Make index zero-base, and also support relative index.
static inline int fixIndex(int idx, int n) {
if (idx > 0) return idx - 1;
if (idx == 0) return 0;
if (idx > 0)
return idx - 1;
if (idx == 0)
return 0;
return n + idx; // negative value = relative
}
@@ -96,7 +101,6 @@ static inline int parseInt(const char *&token) {
return i;
}
// Tries to parse a floating point number located at s.
//
// s_end should be a location in the string where reading should absolutely
@@ -124,10 +128,8 @@ static inline int parseInt(const char *&token) {
// - s >= s_end.
// - parse failure.
//
static bool tryParseDouble(const char *s, const char *s_end, double *result)
{
if (s >= s_end)
{
static bool tryParseDouble(const char *s, const char *s_end, double *result) {
if (s >= s_end) {
return false;
}
@@ -157,23 +159,20 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result)
*/
// Find out what sign we've got.
if (*curr == '+' || *curr == '-')
{
if (*curr == '+' || *curr == '-') {
sign = *curr;
curr++;
}
else if (isdigit(*curr)) { /* Pass through. */ }
else
{
} else if (isdigit(*curr)) { /* Pass through. */
} else {
goto fail;
}
// Read the integer part.
while ((end_not_reached = (curr != s_end)) && isdigit(*curr))
{
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
mantissa *= 10;
mantissa += static_cast<int>(*curr - 0x30);
curr++; read++;
curr++;
read++;
}
// We must make sure we actually got something.
@@ -184,20 +183,17 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result)
goto assemble;
// Read the decimal part.
if (*curr == '.')
{
if (*curr == '.') {
curr++;
read = 1;
while ((end_not_reached = (curr != s_end)) && isdigit(*curr))
{
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
// NOTE: Don't use powf here, it will absolutely murder precision.
mantissa += static_cast<int>(*curr - 0x30) * pow(10.0, -read);
read++; curr++;
read++;
curr++;
}
}
else if (*curr == 'e' || *curr == 'E') {}
else
{
} else if (*curr == 'e' || *curr == 'E') {
} else {
goto assemble;
}
@@ -205,36 +201,33 @@ static bool tryParseDouble(const char *s, const char *s_end, double *result)
goto assemble;
// Read the exponent part.
if (*curr == 'e' || *curr == 'E')
{
if (*curr == 'e' || *curr == 'E') {
curr++;
// Figure out if a sign is present and if it is.
if ((end_not_reached = (curr != s_end)) && (*curr == '+' || *curr == '-'))
{
if ((end_not_reached = (curr != s_end)) && (*curr == '+' || *curr == '-')) {
exp_sign = *curr;
curr++;
}
else if (isdigit(*curr)) { /* Pass through. */ }
else
{
} else if (isdigit(*curr)) { /* Pass through. */
} else {
// Empty E is not allowed.
goto fail;
}
read = 0;
while ((end_not_reached = (curr != s_end)) && isdigit(*curr))
{
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
exponent *= 10;
exponent += static_cast<int>(*curr - 0x30);
curr++; read++;
curr++;
read++;
}
exponent *= (exp_sign == '+'? 1 : -1);
exponent *= (exp_sign == '+' ? 1 : -1);
if (read == 0)
goto fail;
}
assemble:
*result = (sign == '+'? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
*result =
(sign == '+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
return true;
fail:
return false;
@@ -254,7 +247,6 @@ static inline float parseFloat(const char *&token) {
return f;
}
static inline void parseFloat2(float &x, float &y, const char *&token) {
x = parseFloat(token);
y = parseFloat(token);
@@ -456,8 +448,8 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
if ((0 == strncmp(token, "newmtl", 6)) && isSpace((token[6]))) {
// flush previous material.
if (!material.name.empty()) {
material_map.insert(
std::pair<std::string, int>(material.name, static_cast<int>(materials.size())));
material_map.insert(std::pair<std::string, int>(
material.name, static_cast<int>(materials.size())));
materials.push_back(material);
}
@@ -607,8 +599,8 @@ std::string LoadMtl(std::map<std::string, int> &material_map,
}
}
// flush last material.
material_map.insert(
std::pair<std::string, int>(material.name, static_cast<int>(materials.size())));
material_map.insert(std::pair<std::string, int>(
material.name, static_cast<int>(materials.size())));
materials.push_back(material);
return err.str();
@@ -629,13 +621,14 @@ std::string MaterialFileReader::operator()(const std::string &matId,
std::string err = LoadMtl(matMap, materials, matIStream);
if (!matIStream) {
std::stringstream ss;
ss << "WARN: Material file [ " << filepath << " ] not found. Created a default material.";
ss << "WARN: Material file [ " << filepath
<< " ] not found. Created a default material.";
err += ss.str();
}
return err;
}
std::string LoadObj(std::vector<shape_t> &shapes,
std::string LoadObj(std::vector<shape_t> &shapes, std::vector<curve_t> &curves,
std::vector<material_t> &materials, // [output]
const char *filename, const char *mtl_basepath) {
@@ -655,10 +648,11 @@ std::string LoadObj(std::vector<shape_t> &shapes,
}
MaterialFileReader matFileReader(basePath);
return LoadObj(shapes, materials, ifs, matFileReader);
return LoadObj(shapes, curves, materials, ifs, matFileReader);
}
std::string LoadObj(std::vector<shape_t> &shapes,
std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<curve_t> &curves, // [output]
std::vector<material_t> &materials, // [output]
std::istream &inStream, MaterialReader &readMatFn) {
std::stringstream err;
@@ -669,6 +663,13 @@ std::string LoadObj(std::vector<shape_t> &shapes,
std::vector<std::vector<vertex_index> > faceGroup;
std::string name;
// curve
int curveType = -1;
int curveDegree = -1;
std::vector<unsigned int> curve_indices;
std::vector<float> u_params;
std::vector<float> v_params;
// material
std::map<std::string, int> material_map;
std::map<vertex_index, unsigned int> vertexCache;
@@ -748,8 +749,9 @@ std::string LoadObj(std::vector<shape_t> &shapes,
std::vector<vertex_index> face;
while (!isNewLine(token[0])) {
vertex_index vi =
parseTriple(token, static_cast<int>(v.size() / 3), static_cast<int>(vn.size() / 3), static_cast<int>(vt.size() / 2));
vertex_index vi = parseTriple(token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2));
face.push_back(vi);
size_t n = strspn(token, " \t\r");
token += n;
@@ -870,6 +872,107 @@ std::string LoadObj(std::vector<shape_t> &shapes,
continue;
}
// curve type
if ((0 == strncmp(token, "cstype", 6)) && isSpace((token[6]))) {
token += 7;
std::string type = parseString(token);
if (type == "bspline") { // 0
curveType = 0;
} else if (type == "bezier") { // 1
curveType = 1;
} else if (type == "cardinal") {
curveType = 2;
}
}
// degree(curve)
if (token[0] == 'd' && token[1] == 'e' && token[2] == 'g' &&
isSpace((token[3]))) {
token += 4;
curveDegree = parseInt(token);
continue;
}
// curve
if (token[0] == 'c' && token[1] == 'u' && token[2] == 'r' &&
token[3] == 'v' && isSpace((token[4]))) {
token += 5;
// parse u0 and u1
float u0, u1;
parseFloat2(u0, u1, token);
size_t n = strspn(token, " \t\r");
token += n;
// parse vertex indices.
curve_indices.clear();
while (!isNewLine(token[0])) {
vertex_index vi = parseTriple(token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2));
curve_indices.push_back(vi.v_idx);
size_t n = strspn(token, " \t\r");
token += n;
}
continue;
}
// curve param
if ((0 == strncmp(token, "parm", 4)) && isSpace((token[4]))) {
token += 5;
std::string name = parseString(token);
if (name == "u") {
u_params.clear();
} else if (name == "v") {
v_params.clear();
}
// parse parameter values.
while (!isNewLine(token[0])) {
float value = parseFloat(token);
if (name == "u") {
u_params.push_back(value);
} else if (name == "v") {
v_params.push_back(value);
}
}
continue;
}
// end(curve)
if (token[0] == 'e' && token[1] == 'n' && token[2] == 'd') {
// size_t n = strspn(token, " \t\r");
// token += n;
// @todo { Consider 'end' at the end of line: parm u 0 0 0 0 ... end }
if ((curveType > -1) && (curveDegree > 0) && (curve_indices.size() > 1)) {
curve_t curve;
curve.name = name;
curve.type = curveType;
curve.degree = curveDegree;
curve.positions = v;
curve.indices = curve_indices;
curve.u_params = u_params;
curve.v_params = v_params;
curves.push_back(curve);
}
u_params.clear();
v_params.clear();
curve_indices.clear();
continue;
}
// Ignore unknown command.
}
@@ -882,4 +985,5 @@ std::string LoadObj(std::vector<shape_t> &shapes,
return err.str();
}
}
} // namespace

View File

@@ -41,6 +41,21 @@ typedef struct {
std::vector<int> material_ids; // per-mesh material ID
} mesh_t;
typedef struct {
std::vector<float> positions;
std::vector<unsigned int> indices;
} line_t;
typedef struct {
std::string name;
std::vector<float> positions; // control points. xyz
std::vector<unsigned int> indices; // index to control point
std::vector<float> u_params;
std::vector<float> v_params;
int degree;
int type; // 0: bspline, 1: bezier, 2: cardinal
} curve_t;
typedef struct {
std::string name;
mesh_t mesh;
@@ -71,10 +86,12 @@ private:
/// Loads .obj from a file.
/// 'shapes' will be filled with parsed shape data
/// 'curves' will be filled with parsed curve data(NURBS, Bezier, etc)
/// The function returns error string.
/// Returns empty string when loading .obj success.
/// 'mtl_basepath' is optional, and used for base path for .mtl file.
std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<curve_t> &curves, // [output]
std::vector<material_t> &materials, // [output]
const char *filename, const char *mtl_basepath = NULL);
@@ -82,6 +99,7 @@ std::string LoadObj(std::vector<shape_t> &shapes, // [output]
/// std::istream for materials.
/// Returns empty string when loading .obj success.
std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<curve_t> &curves, // [output]
std::vector<material_t> &materials, // [output]
std::istream &inStream, MaterialReader &readMatFn);