/* Read node from memory and continue with remaining nodes. */ void VertexBufferNode::fromMemory( char** addr, VertexBufferData& globalData ) { // read node itself Type nodeType; memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( nodeType )); if( nodeType != Type::node ) throw MeshException( "Error reading binary file. Expected a node, got "+ std::to_string( unsigned( nodeType ))); VertexBufferBase::fromMemory( addr, globalData ); // read left child (peek ahead) memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( nodeType )); if( nodeType != Type::node && nodeType != Type::leaf ) throw MeshException( "Error reading binary file. Expected either a " "regular or a leaf node, but found neither." ); *addr -= sizeof( nodeType ); if( nodeType == Type::node ) _left.reset( new VertexBufferNode ); else _left.reset( new VertexBufferLeaf( globalData )); _left->fromMemory( addr, globalData ); // read right child (peek ahead) memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( nodeType )); if( nodeType != Type::node && nodeType != Type::leaf ) throw MeshException( "Error reading binary file. Expected either a " "regular or a leaf node, but found neither." ); *addr -= sizeof( nodeType ); if( nodeType == Type::node ) _right.reset( new VertexBufferNode ); else _right.reset( new VertexBufferLeaf( globalData )); _right->fromMemory( addr, globalData ); }
void MeshBase::loadDataFromPly( const std::string& filename ) { p_ply ply = ply_open( filename.c_str(), 0); if( !ply ) { throw MeshException( "Error opening ply file during second pass (" + filename + ")" ); } if( !ply_read_header( ply ) ) { throw MeshException( "Error parsing ply header during second pass (" + filename + ")" ); } MeshGroup& group = getFirstGroup(); PlyData data( m_vertex_data, m_normal_data, group.vertex_indices, group.normal_indices ); ply_set_read_cb( ply, "vertex", "x", plyVertexLoadDataCB, &data, 0); ply_set_read_cb( ply, "vertex", "y", plyVertexLoadDataCB, &data, 1); ply_set_read_cb( ply, "vertex", "z", plyVertexLoadDataCB, &data, 2); ply_set_read_cb( ply, "vertex", "nx", plyVertexLoadDataCB, &data, 3); ply_set_read_cb( ply, "vertex", "ny", plyVertexLoadDataCB, &data, 4); ply_set_read_cb( ply, "vertex", "nz", plyVertexLoadDataCB, &data, 5); ply_set_read_cb( ply, "face", "vertex_indices", plyFaceLoadDataCB, &data, 0); if( !ply_read( ply ) ) { throw MeshException( "Error parsing ply file (" + filename + ")" ); } ply_close(ply); }
/* Read node from memory and continue with remaining nodes. */ void VertexBufferNode::fromMemory( char** addr, VertexBufferData& globalData ) { // read node itself size_t nodeType; memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( size_t ) ); if( nodeType != NODE_TYPE ) throw MeshException( "Error reading binary file. Expected a regular " "node, but found something else instead." ); VertexBufferBase::fromMemory( addr, globalData ); // read left child (peek ahead) memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( size_t ) ); if( nodeType != NODE_TYPE && nodeType != LEAF_TYPE ) throw MeshException( "Error reading binary file. Expected either a " "regular or a leaf node, but found neither." ); *addr -= sizeof( size_t ); if( nodeType == NODE_TYPE ) _left = new VertexBufferNode; else _left = new VertexBufferLeaf( globalData ); static_cast< VertexBufferNode* >( _left )->fromMemory( addr, globalData ); // read right child (peek ahead) memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( size_t ) ); if( nodeType != NODE_TYPE && nodeType != LEAF_TYPE ) throw MeshException( "Error reading binary file. Expected either a " "regular or a leaf node, but found neither." ); *addr -= sizeof( size_t ); if( nodeType == NODE_TYPE ) _right = new VertexBufferNode; else _right = new VertexBufferLeaf( globalData ); static_cast< VertexBufferNode* >( _right )->fromMemory( addr, globalData ); }
/* Read root node from memory and continue with other nodes. */ void VertexBufferRoot::fromMemory( char* start ) { char** addr = &start; size_t version; memRead( reinterpret_cast< char* >( &version ), addr, sizeof( size_t ) ); if( version != FILE_VERSION ) throw MeshException( "Error reading binary file. Version in file " "does not match the expected version." ); size_t nodeType; memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( size_t ) ); if( nodeType != ROOT_TYPE ) throw MeshException( "Error reading binary file. Expected the root " "node, but found something else instead." ); _data.fromMemory( addr ); VertexBufferNode::fromMemory( addr, _data ); }
MeshGroup& MeshBase::getFirstGroup() { if( m_mesh_groups.size() < 1 ) { throw MeshException( "There is no first group to return." ); } return m_mesh_groups.begin()->second; }
void MeshBase::computeAabb() { if( !m_vertex_data || !m_bbox_min || !m_bbox_max) { throw MeshException( "The mesh is not ready for bounding box computation." ); } optix::Aabb aabb; for( int i = 0; i < m_num_vertices; ++i ) { aabb.include(reinterpret_cast<const optix::float3*>(m_vertex_data)[i]); } reinterpret_cast<optix::float3*>(m_bbox_min)[0] = aabb.m_min; reinterpret_cast<optix::float3*>(m_bbox_max)[0] = aabb.m_max; }
void MeshBase::loadInfoFromPly( const std::string& filename ) { p_ply ply = ply_open( filename.c_str(), 0); if( !ply ) { throw MeshException( "Error opening ply file during first pass (" + filename + ")" ); } if( !ply_read_header( ply ) ) { throw MeshException( "Error parsing ply header during first pass (" + filename + ")" ); } // Simply get the counts without setting real callbacks; that's for the second pass int num_vertices = ply_set_read_cb( ply, "vertex", "x", NULL, NULL, 0 ); int num_normals = ply_set_read_cb( ply, "vertex", "nx", NULL, NULL, 3 ); int num_triangles = ply_set_read_cb( ply, "face", "vertex_indices", NULL, NULL, 0 ); m_num_vertices = num_vertices; m_num_normals = num_normals; initSingleGroup(); MeshGroup& group = getFirstGroup(); group.num_triangles = num_triangles; }
/* Read leaf node from memory. */ void VertexBufferLeaf::fromMemory( char** addr, VertexBufferData& globalData ) { size_t nodeType; memRead( reinterpret_cast< char* >( &nodeType ), addr, sizeof( size_t ) ); if( nodeType != LEAF_TYPE ) throw MeshException( "Error reading binary file. Expected a leaf " "node, but found something else instead." ); VertexBufferBase::fromMemory( addr, globalData ); memRead( reinterpret_cast< char* >( &_vertexStart ), addr, sizeof( Index ) ); memRead( reinterpret_cast< char* >( &_vertexLength ), addr, sizeof( ShortIndex ) ); memRead( reinterpret_cast< char* >( &_indexStart ), addr, sizeof( Index ) ); memRead( reinterpret_cast< char* >( &_indexLength ), addr, sizeof( Index ) ); }
void MeshBase::loadModel( const std::string& filename ) { m_filename = filename; m_pathname = directoryOfFilePath( filename ); if( fileIsObj(filename) ) { loadFromObj( filename ); } else if( fileIsPly(filename) ) { loadFromPly( filename ); } else { throw MeshException( "Unrecognized model file extension (" + filename + ")" ); } m_filename.clear(); m_pathname.clear(); }