Commit of the debugged version of Alex' improvement to OpenMesh. File i/o is now done via istream/ostream instances such that direct buffer writing to files (serialization) will also be possible in future releases.

Code is tested.

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@221 fdac6126-5c0c-442c-9429-916003d36597
This commit is contained in:
Mike Kremer
2009-11-17 13:54:16 +00:00
parent 86b53bf930
commit efa67fbcfc
24 changed files with 512 additions and 242 deletions

View File

@@ -4,10 +4,10 @@
* Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen *
* www.openmesh.org *
* *
*---------------------------------------------------------------------------*
*---------------------------------------------------------------------------*
* This file is part of OpenMesh. *
* *
* OpenMesh is free software: you can redistribute it and/or modify *
* OpenMesh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
@@ -30,10 +30,10 @@
* License along with OpenMesh. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
\*===========================================================================*/
/*===========================================================================*\
* *
* *
* $Revision$ *
* $Date$ *
* *
@@ -75,15 +75,15 @@ _OMReader_& OMReader() { return __OMReaderInstance; }
_OMReader_::_OMReader_()
{
IOManager().register_module(this);
IOManager().register_module(this);
}
//-----------------------------------------------------------------------------
bool
_OMReader_::read(const std::string& _filename,
bool
_OMReader_::read(const std::string& _filename,
BaseImporter& _bi,
Options& _opt)
{
@@ -95,42 +95,58 @@ _OMReader_::read(const std::string& _filename,
// Open file
std::ifstream ifs( _filename.c_str(), std::ios::binary );
if (!ifs.is_open())
if (!ifs.is_open() || !ifs.good())
{
omerr() << "[OMReader] : cannot not open file "
omerr() << "[OMReader] : cannot not open file "
<< _filename
<< std::endl;
return false;
}
// Pass stream to read method, remember result
bool result = read(ifs, _bi, _opt);
// close input stream
ifs.close();
return result;
}
//-----------------------------------------------------------------------------
bool
_OMReader_::read(std::istream& _is,
BaseImporter& _bi,
Options& _opt)
{
// check whether importer can give us an OpenMesh BaseKernel
if (!_bi.kernel()) return false;
_opt += Options::Binary; // only binary format supported!
if (!_is.good())
{
omerr() << "[OMReader] : cannot read from stream "
<< std::endl;
return false;
}
// Pass stream to read method, remember result
bool result = read_binary(_is, _bi, _opt);
if(result) _opt += Options::Binary;
return result;
}
//-----------------------------------------------------------------------------
bool
_OMReader_::read(std::istream& _is, BaseImporter& _bi, Options& _opt ) const
{
// currently only binary file format is supported
_opt += Options::Binary;
return read_binary( _is, _bi, _opt );
}
//-----------------------------------------------------------------------------
bool
_OMReader_::read_ascii( std::istream& /* _is */, BaseImporter& /* _bi */,
bool
_OMReader_::read_ascii( std::istream& /* _is */, BaseImporter& /* _bi */,
Options& /* _opt */) const
{
// not supported yet!
@@ -140,14 +156,14 @@ _OMReader_::read_ascii( std::istream& /* _is */, BaseImporter& /* _bi */,
//-----------------------------------------------------------------------------
bool
_OMReader_::read_binary( std::istream& _is, BaseImporter& _bi,
bool
_OMReader_::read_binary( std::istream& _is, BaseImporter& _bi,
Options& _opt) const
{
bool swap = _opt.check(Options::Swap) || (Endian::local() == Endian::MSB);
// intialize byte counter
bytes_ = 0;
bytes_ = 0;
bytes_ += restore( _is, header_, swap );
@@ -172,7 +188,7 @@ _OMReader_::read_binary( std::istream& _is, BaseImporter& _bi,
data_bytes = bytes_;
switch( chunk_header_.entity_ )
{
case OMFormat::Chunk::Entity_Vertex:
case OMFormat::Chunk::Entity_Vertex:
if (!read_binary_vertex_chunk( _is, _bi, _opt, swap ))
return false;
break;
@@ -214,13 +230,13 @@ _OMReader_::can_u_read(const std::string& _filename) const
{
std::ifstream ifile( _filename.c_str() );
if ( ifile && can_u_read( ifile ) )
return true;
return true;
}
return false;
}
//-----------------------------------------------------------------------------
bool
_OMReader_::can_u_read(std::istream& _is) const
{
@@ -231,7 +247,7 @@ _OMReader_::can_u_read(std::istream& _is) const
while( evt.size() < 4 )
evt.push_back( static_cast<char>(_is.get()) );
// put back all read characters
// put back all read characters
std::vector<char>::reverse_iterator it = evt.rbegin();
while (it != evt.rend() )
_is.putback( *it++ );
@@ -254,14 +270,14 @@ _OMReader_::can_u_read(std::istream& _is) const
default: // ?
return false;
}
// 4th characters encodes the version
return supports( hdr->version_ );
}
//-----------------------------------------------------------------------------
bool
bool
_OMReader_::supports( const OMFormat::uint8 /* version */ ) const
{
return true;
@@ -270,16 +286,16 @@ _OMReader_::supports( const OMFormat::uint8 /* version */ ) const
//-----------------------------------------------------------------------------
bool
_OMReader_::read_binary_vertex_chunk( std::istream &_is,
BaseImporter &_bi,
bool
_OMReader_::read_binary_vertex_chunk( std::istream &_is,
BaseImporter &_bi,
Options &_opt,
bool _swap) const
{
using OMFormat::Chunk;
assert( chunk_header_.entity_ == Chunk::Entity_Vertex );
OpenMesh::Vec3f v3f;
OpenMesh::Vec2f v2f;
OpenMesh::Vec3uc v3uc; // rgb
@@ -290,7 +306,7 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is,
switch (chunk_header_.type_)
{
case Chunk::Type_Pos:
assert( OMFormat::dimensions(chunk_header_)
assert( OMFormat::dimensions(chunk_header_)
== size_t(OpenMesh::Vec3f::dim()) );
for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx)
@@ -300,9 +316,9 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is,
}
break;
case Chunk::Type_Normal:
assert( OMFormat::dimensions(chunk_header_)
assert( OMFormat::dimensions(chunk_header_)
== size_t(OpenMesh::Vec3f::dim()) );
_opt += Options::VertexNormal;
@@ -313,9 +329,9 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is,
}
break;
case Chunk::Type_Texcoord:
assert( OMFormat::dimensions(chunk_header_)
assert( OMFormat::dimensions(chunk_header_)
== size_t(OpenMesh::Vec2f::dim()) );
_opt += Options::VertexTexCoord;
@@ -324,14 +340,14 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is,
bytes_ += vector_restore( _is, v2f, _swap );
_bi.set_texcoord(VertexHandle(vidx), v2f);
}
break;
break;
case Chunk::Type_Color:
assert( OMFormat::dimensions(chunk_header_) == 3 );
_opt += Options::VertexColor;
for (; vidx < header_.n_vertices_ && !_is.eof(); ++vidx)
{
bytes_ += vector_restore( _is, v3uc, _swap );
@@ -340,8 +356,8 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is,
break;
case Chunk::Type_Custom:
bytes_ +=
bytes_ +=
restore_binary_custom_data( _is,
_bi.kernel()->_get_vprop( property_name_ ),
header_.n_vertices_,
@@ -354,7 +370,7 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is,
default: // skip unknown chunks
{
omerr() << "Unknown chunk type ignored!\n";
size_t size_of = header_.n_vertices_
size_t size_of = header_.n_vertices_
* OMFormat::vector_size(chunk_header_);
_is.ignore( size_of );
bytes_ += size_of;
@@ -369,8 +385,8 @@ _OMReader_::read_binary_vertex_chunk( std::istream &_is,
//-----------------------------------------------------------------------------
bool
_OMReader_::read_binary_face_chunk( std::istream &_is,
BaseImporter &_bi,
_OMReader_::read_binary_face_chunk( std::istream &_is,
BaseImporter &_bi,
Options &_opt,
bool _swap ) const
{
@@ -395,7 +411,7 @@ _OMReader_::read_binary_face_chunk( std::istream &_is,
case 'T': nV = 3; break;
case 'Q': nV = 4; break;
}
for (; fidx < header_.n_faces_; ++fidx)
{
if ( header_.mesh_ == 'P' )
@@ -403,8 +419,8 @@ _OMReader_::read_binary_face_chunk( std::istream &_is,
vhandles.clear();
for (size_t j=0; j<nV; ++j)
{
bytes_ += restore( _is, vidx,
{
bytes_ += restore( _is, vidx,
Chunk::Integer_Size(chunk_header_.bits_),
_swap );
@@ -418,7 +434,7 @@ _OMReader_::read_binary_face_chunk( std::istream &_is,
case Chunk::Type_Normal:
assert( OMFormat::dimensions(chunk_header_)
assert( OMFormat::dimensions(chunk_header_)
== size_t(OpenMesh::Vec3f::dim()) );
_opt += Options::FaceNormal;
@@ -434,7 +450,7 @@ _OMReader_::read_binary_face_chunk( std::istream &_is,
assert( OMFormat::dimensions(chunk_header_) == 3 );
_opt += Options::FaceColor;
_opt += Options::FaceColor;
for (; fidx < header_.n_faces_ && !_is.eof(); ++fidx)
{
bytes_ += vector_restore( _is, v3uc, _swap );
@@ -444,8 +460,8 @@ _OMReader_::read_binary_face_chunk( std::istream &_is,
case Chunk::Type_Custom:
bytes_ +=
bytes_ +=
restore_binary_custom_data( _is,
_bi.kernel()->_get_fprop( property_name_ ),
header_.n_faces_,
@@ -454,13 +470,13 @@ _OMReader_::read_binary_face_chunk( std::istream &_is,
fidx = header_.n_faces_;
break;
default: // skip unknown chunks
{
omerr() << "Unknown chunk type ignore!\n";
size_t size_of = OMFormat::chunk_data_size(header_, chunk_header_);
_is.ignore( size_of );
bytes_ += size_of;
bytes_ += size_of;
}
}
return fidx == header_.n_faces_;
@@ -470,8 +486,8 @@ _OMReader_::read_binary_face_chunk( std::istream &_is,
//-----------------------------------------------------------------------------
bool
_OMReader_::read_binary_edge_chunk( std::istream &_is,
BaseImporter &_bi,
_OMReader_::read_binary_edge_chunk( std::istream &_is,
BaseImporter &_bi,
Options &/*_opt */,
bool _swap ) const
{
@@ -483,9 +499,9 @@ _OMReader_::read_binary_edge_chunk( std::istream &_is,
switch( chunk_header_.type_ )
{
case Chunk::Type_Custom:
case Chunk::Type_Custom:
bytes_ +=
bytes_ +=
restore_binary_custom_data( _is,
_bi.kernel()->_get_eprop( property_name_ ),
header_.n_edges_,
@@ -507,8 +523,8 @@ _OMReader_::read_binary_edge_chunk( std::istream &_is,
//-----------------------------------------------------------------------------
bool
_OMReader_::read_binary_halfedge_chunk( std::istream &_is,
BaseImporter &_bi,
_OMReader_::read_binary_halfedge_chunk( std::istream &_is,
BaseImporter &_bi,
Options &/* _opt */,
bool _swap ) const
{
@@ -522,11 +538,11 @@ _OMReader_::read_binary_halfedge_chunk( std::istream &_is,
{
case Chunk::Type_Custom:
bytes_ +=
bytes_ +=
restore_binary_custom_data( _is,
_bi.kernel()->_get_hprop( property_name_ ),
2*header_.n_edges_,
_swap );
_swap );
break;
default:
@@ -544,8 +560,8 @@ _OMReader_::read_binary_halfedge_chunk( std::istream &_is,
//-----------------------------------------------------------------------------
bool
_OMReader_::read_binary_mesh_chunk( std::istream &_is,
BaseImporter &_bi,
_OMReader_::read_binary_mesh_chunk( std::istream &_is,
BaseImporter &_bi,
Options & /* _opt */,
bool _swap ) const
{
@@ -558,13 +574,13 @@ _OMReader_::read_binary_mesh_chunk( std::istream &_is,
switch( chunk_header_.type_ )
{
case Chunk::Type_Custom:
bytes_ +=
bytes_ +=
restore_binary_custom_data( _is,
_bi.kernel()->_get_mprop( property_name_ ),
1,
_swap );
break;
default:
@@ -581,10 +597,10 @@ _OMReader_::read_binary_mesh_chunk( std::istream &_is,
//-----------------------------------------------------------------------------
size_t
size_t
_OMReader_::restore_binary_custom_data( std::istream& _is, BaseProperty* _bp,
size_t _n_elem, bool _swap) const
{
{
assert( !_bp || (_bp->name() == property_name_) );
using OMFormat::Chunk;
@@ -605,7 +621,7 @@ _OMReader_::restore_binary_custom_data( std::istream& _is, BaseProperty* _bp,
if ( ((n_bytes == BaseProperty::UnknownSize) || (n_bytes == block_size)) &&
(_bp->element_size() == BaseProperty::UnknownSize ||
(_n_elem * _bp->element_size() == block_size) ) )
(_n_elem * _bp->element_size() == block_size) ) )
{
#if defined(OM_DEBUG)
bytes += (b=_bp->restore( _is, _swap ));
@@ -621,7 +637,7 @@ _OMReader_::restore_binary_custom_data( std::istream& _is, BaseProperty* _bp,
}
else
{
omerr() << "Warning! Property " << _bp->name() << " not loaded: "
omerr() << "Warning! Property " << _bp->name() << " not loaded: "
<< "Mismatching data sizes!n";
}
}