Skip to content

Commit

Permalink
basic readers.obj implementation complete
Browse files Browse the repository at this point in the history
  • Loading branch information
hobu committed Jul 7, 2020
1 parent 0664761 commit 83ff488
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 135 deletions.
209 changes: 133 additions & 76 deletions io/ObjReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ std::string ObjReader::getName() const { return s_info.name; }
void ObjReader::addArgs(ProgramArgs& args)
{
args.add("material", "Material filename", m_materialFileName);
args.add("triangulate", "Triangulate", m_triangulate);
args.add("triangulate", "Triangulate", m_triangulate, false);
args.add("color_scale_factor", "Color scale factor", m_color_scale_factor, 255.0);
}

void ObjReader::addDimensions(PointLayoutPtr layout)
Expand All @@ -81,18 +82,17 @@ void ObjReader::initialize()
{
bool exists = FileUtils::fileExists(m_filename);
if (!exists)
{
throwError("Given filename '" + m_filename + "' does not exist");
}
throwError("Given filename '"
+ m_filename +
"' does not exist");

if (m_materialFileName.size())
{
exists = FileUtils::fileExists(m_materialFileName);
if (!exists)
{
throwError("Given material filename '" + m_materialFileName + "' does not exist");
}

throwError("Given material filename '"
+ m_materialFileName +
"' does not exist");
}
}
void ObjReader::ready(PointTableRef)
Expand All @@ -102,7 +102,11 @@ void ObjReader::ready(PointTableRef)
m_materialFileName = FileUtils::stem(m_filename) + ".obj.mtl";
bool haveMat = FileUtils::fileExists(m_materialFileName);

log()->get(LogLevel::Debug) << "Mat file name: " << m_materialFileName << std::endl;
log()->get(LogLevel::Debug) << "Mat file exists?: "
<< haveMat << std::endl;

log()->get(LogLevel::Debug) << "Mat file name: "
<< m_materialFileName << std::endl;


std::string basepath;
Expand All @@ -123,90 +127,146 @@ void ObjReader::ready(PointTableRef)
if (m_errors.size())
throwError(m_errors);

// size_t fileSize = FileUtils::fileSize(m_filename);
// size_t pointSize = sbet::fileDimensions().size() * sizeof(double);
// if (fileSize % pointSize != 0)
// throwError("Invalid file size.");
// m_numPts = fileSize / pointSize;
// m_index = 0;
// m_stream.reset(new ILeStream(m_filename));
// m_dims = sbet::fileDimensions();
// seek(m_index);
// reset point count
m_numPts = 0;

if (m_shapes.size())
{
for (size_t s = 0; s < m_shapes.size(); s++)
m_numPts += m_shapes[s].mesh.num_face_vertices.size();
}
else
{
m_numPts = m_attributes.vertices.size() / 3.0;
}

m_index = 0;

log()->get(LogLevel::Debug) << "Number of shapes "
<< m_shapes.size() << std::endl;
log()->get(LogLevel::Debug) << "Number of attributes "
<< m_attributes.vertices.size() << std::endl;
log()->get(LogLevel::Debug) << "Number of vertices "
<< m_numPts << std::endl;

}

bool ObjReader::processOne(PointRef& point)
{

if (m_index < m_numPts )
{
fillPoint(point);
m_index++;
updateIndexes();
return true;
}
return false;

point_count_t ObjReader::read(PointViewPtr view, point_count_t count)
}


void ObjReader::fillPoint(PointRef& point)
{
tinyobj::index_t idx;
if (m_shapes.size() )
idx = m_shapes[m_shape_idx].mesh.indices[m_face_offset + m_vert_idx];
else
{
idx.vertex_index = m_vert_idx;
idx.texcoord_index = m_vert_idx;
idx.normal_index = m_vert_idx;
}

// reset point count
m_numPts = 0;
for (size_t s = 0; s < m_shapes.size(); s++) {
m_numPts += m_shapes[s].mesh.num_face_vertices.size();
tinyobj::real_t vx = m_attributes.vertices[3*idx.vertex_index+0];
tinyobj::real_t vy = m_attributes.vertices[3*idx.vertex_index+1];
tinyobj::real_t vz = m_attributes.vertices[3*idx.vertex_index+2];

point.setField(Dimension::Id::X, vx);

tinyobj::real_t nx(0);
tinyobj::real_t ny(0);
tinyobj::real_t nz(0);
if (m_attributes.normals.size())
{
nx = m_attributes.normals[3 * idx.normal_index + 0];
ny = m_attributes.normals[3 * idx.normal_index + 1];
nz = m_attributes.normals[3 * idx.normal_index + 2];
}

log()->get(LogLevel::Debug) << "Number of shapes " << m_shapes.size() << std::endl;
log()->get(LogLevel::Debug) << "Number of vertices " << m_numPts << std::endl;
tinyobj::real_t tx(0);
tinyobj::real_t ty(0);
if (m_attributes.texcoords.size())
{
tx = m_attributes.texcoords[2 * idx.texcoord_index + 0];
ty = m_attributes.texcoords[2 * idx.texcoord_index + 1];
}

// Loop over shapes
for (;m_shape_idx < m_shapes.size(); m_shape_idx++)
tinyobj::real_t red(0);
tinyobj::real_t green(0);
tinyobj::real_t blue(0);
if (m_attributes.colors.size())
{
// Loop over faces(polygon)
size_t index_offset = 0;
for (; m_face_idx < m_shapes[m_shape_idx].mesh.num_face_vertices.size(); m_face_idx++)
red = m_attributes.colors[ 3 * idx.vertex_index + 0];
green = m_attributes.colors[ 3 * idx.vertex_index + 1];
blue = m_attributes.colors[ 3 * idx.vertex_index + 2];
}

point.setField(Dimension::Id::X, vx);
point.setField(Dimension::Id::Y, vy);
point.setField(Dimension::Id::Z, vz);

point.setField(Dimension::Id::NormalX, nx);
point.setField(Dimension::Id::NormalY, ny);
point.setField(Dimension::Id::NormalZ, nz);

point.setField(Dimension::Id::TextureX, tx);
point.setField(Dimension::Id::TextureY, ty);

point.setField(Dimension::Id::Red, red * m_color_scale_factor);
point.setField(Dimension::Id::Green, green * m_color_scale_factor);
point.setField(Dimension::Id::Blue, blue * m_color_scale_factor);

}

void ObjReader::updateIndexes()
{

// If there's no shapes, we just increment our vertex id.
// If there are shapes, we have to walk the faces
if (m_shapes.size() < m_shape_idx)
{
if (m_face_idx == m_shapes[m_shape_idx].mesh.num_face_vertices.size())
{
size_t fv = m_shapes[m_shape_idx].mesh.num_face_vertices[m_face_idx];

// Loop over vertices in the face.
for (; m_vert_idx < fv; m_vert_idx++)
{
// access to vertex
tinyobj::index_t idx = m_shapes[m_shape_idx].mesh.indices[index_offset + m_vert_idx];
tinyobj::real_t vx = m_attributes.vertices[3*idx.vertex_index+0];
tinyobj::real_t vy = m_attributes.vertices[3*idx.vertex_index+1];
tinyobj::real_t vz = m_attributes.vertices[3*idx.vertex_index+2];

tinyobj::real_t nx(0);
tinyobj::real_t ny(0);
tinyobj::real_t nz(0);
if (m_attributes.normals.size())
{
nx = m_attributes.normals[3*idx.normal_index+0];
ny = m_attributes.normals[3*idx.normal_index+1];
nz = m_attributes.normals[3*idx.normal_index+2];
}

tinyobj::real_t tx(0);
tinyobj::real_t ty(0);
if (m_attributes.texcoords.size())
{
tx = m_attributes.texcoords[2*idx.texcoord_index+0];
ty = m_attributes.texcoords[2*idx.texcoord_index+1];
}

tinyobj::real_t red(0);
tinyobj::real_t green(0);
tinyobj::real_t blue(0);
if (m_attributes.colors.size())
{
red = m_attributes.colors[3*idx.vertex_index+0];
green = m_attributes.colors[3*idx.vertex_index+1];
blue = m_attributes.colors[3*idx.vertex_index+2];
}
}
index_offset += fv;

// per-face material
m_shapes[m_shape_idx].mesh.material_ids[m_face_idx];
m_face_idx = 0;
m_face_offset = 0;
m_shape_idx++;
}
}

if (m_vert_idx == m_face_vertex_id && m_shapes.size())
{
m_face_offset += m_face_vertex_id;
m_vert_idx = 0;
m_face_idx++;
m_face_vertex_id = m_shapes[m_shape_idx].mesh.num_face_vertices[m_face_idx];
} else
{
m_vert_idx++;
}

}

point_count_t ObjReader::read(PointViewPtr view, point_count_t count)
{

PointId nextId = view->size();
PointId idx = m_index;
point_count_t numRead = 0;
while (numRead < count && idx < m_numPts)
{
PointRef point = view->point(nextId);
processOne(point);
if (m_cb)
m_cb(*view, nextId);

Expand All @@ -218,7 +278,4 @@ point_count_t ObjReader::read(PointViewPtr view, point_count_t count)
return numRead;
}




} // namespace pdal
13 changes: 12 additions & 1 deletion io/ObjReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <pdal/Reader.hpp>
#include <pdal/util/IStream.hpp>
#include <pdal/util/ProgramArgs.hpp>
#include <pdal/Streamable.hpp>



#ifndef TINYOBJLOADER_USE_DOUBLE
Expand All @@ -52,7 +54,7 @@
namespace pdal
{

class PDAL_DLL ObjReader : public Reader
class PDAL_DLL ObjReader : public Reader, public Streamable
{
public:
ObjReader()
Expand All @@ -61,7 +63,9 @@ class PDAL_DLL ObjReader : public Reader
, m_index(0)
, m_shape_idx(0)
, m_face_idx(0)
, m_face_offset(0)
, m_vert_idx(0)
, m_face_vertex_id(0)
{}

std::string getName() const override;
Expand All @@ -84,12 +88,19 @@ class PDAL_DLL ObjReader : public Reader

size_t m_shape_idx;
size_t m_face_idx;
size_t m_face_offset;
size_t m_vert_idx;
size_t m_face_vertex_id;

double m_color_scale_factor;

void fillPoint(PointRef& point);
void updateIndexes();

virtual void initialize() override;
virtual void addDimensions(PointLayoutPtr layout) override;
virtual void ready(PointTableRef table) override;
virtual bool processOne(PointRef& point) override;
virtual point_count_t read(PointViewPtr view, point_count_t count) override;
virtual void addArgs(ProgramArgs& args) override;
virtual bool eof()
Expand Down

0 comments on commit 83ff488

Please sign in to comment.