MeshIO.hh 10.9 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
2 3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
6 7
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
8
 *                                                                           *
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10 11
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
12
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
13 14 15
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
16
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
17 18
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
19
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
20 21 22
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
23
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
24 25 26
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
27
 *                                                                           *
Jan Möbius's avatar
Jan Möbius committed
28 29 30 31 32 33 34 35 36 37 38
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40
 *                                                                           *
 * ========================================================================= */
41 42

/*===========================================================================*\
43
 *                                                                           *
44 45
 *   $Revision$                                                         *
 *   $Date$                   *
46 47 48 49 50 51 52 53 54 55 56 57
 *                                                                           *
\*===========================================================================*/


#ifndef OM_MESHIO_HH
#define OM_MESHIO_HH


//=== INCLUDES ================================================================

// -------------------- system settings
#include <OpenMesh/Core/System/config.h>
Max Lyon's avatar
Max Lyon committed
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/SR_store.hh>
#include <OpenMesh/Core/IO/IOManager.hh>
#include <OpenMesh/Core/IO/importer/ImporterT.hh>
#include <OpenMesh/Core/IO/exporter/ExporterT.hh>


//== NAMESPACES ==============================================================

namespace OpenMesh {
namespace IO   {


//=== IMPLEMENTATION ==========================================================


/** \name Mesh Reading / Writing
    Convenience functions the map to IOManager functions.
    \see OpenMesh::IO::_IOManager_
*/
//@{


//-----------------------------------------------------------------------------


85
/** \brief Read a mesh from file _filename.
Jan Möbius's avatar
Jan Möbius committed
86

87
    The file format is determined by the file extension.
Jan Möbius's avatar
Jan Möbius committed
88

89 90
    \note If you link statically against OpenMesh, you have to add
          the define OM_STATIC_BUILD to your application. This will
91
          ensure that readers and writers get initialized correctly.
Jan Möbius's avatar
Jan Möbius committed
92 93 94 95 96 97

    @param _mesh     The target mesh that will be filled with the read data
    @param _filename fill to load

    @return Successful?
 */
98
template <class Mesh>
99
bool
Jan Möbius's avatar
Jan Möbius committed
100
read_mesh(Mesh&         _mesh,
101
	  const std::string&  _filename)
102 103
{
  Options opt;
104
  return read_mesh(_mesh, _filename, opt, true);
105 106 107
}


108
/** \brief Read a mesh from file _filename.
Jan Möbius's avatar
Jan Möbius committed
109

110
    The file format is determined by the file extension.
Jan Möbius's avatar
Jan Möbius committed
111

112 113
    \note If you link statically against OpenMesh, you have to add
          the define OM_STATIC_BUILD to your application. This will
114
          ensure that readers and writers get initialized correctly.
Jan Möbius's avatar
Jan Möbius committed
115 116 117 118

    @param _mesh     The target mesh that will be filled with the read data
    @param _filename fill to load
    @param _opt      Reader options (e.g. skip loading of normals ... depends
119 120
                     on the reader capabilities). Note that simply passing an
                     Options::Flag enum is not sufficient.
Jan Möbius's avatar
Jan Möbius committed
121
    @param _clear    Clear the target data before filling it (allows to
122 123 124
                     load multiple files into one Mesh). If you only want to read a mesh
                     without clearing set _clear to false. Providing a default Options
                     object is sufficient in this case.
Jan Möbius's avatar
Jan Möbius committed
125 126 127

    @return Successful?
*/
128
template <class Mesh>
129
bool
Jan Möbius's avatar
Jan Möbius committed
130
read_mesh(Mesh&         _mesh,
131 132 133
	  const std::string&  _filename,
	  Options&            _opt,
	  bool                _clear = true)
134 135 136
{
  if (_clear) _mesh.clear();
  ImporterT<Mesh> importer(_mesh);
137
  return IOManager().read(_filename, importer, _opt);
138 139 140
}


141
/** \brief Read a mesh from file open std::istream.
Jan Möbius's avatar
Jan Möbius committed
142

143
    The file format is determined by parameter _ext. _ext has to include
Jan Möbius's avatar
Jan Möbius committed
144 145
    ".[format]" in order to work properly (e.g. ".OFF")

146 147
    \note If you link statically against OpenMesh, you have to add
          the define OM_STATIC_BUILD to your application. This will
148
          ensure that readers and writers get initialized correctly.
Jan Möbius's avatar
Jan Möbius committed
149 150 151 152 153 154 155 156 157 158 159

    @param _mesh     The target mesh that will be filled with the read data
    @param _is       stream to load the data from
    @param _ext      The file format that is written to the stream
    @param _opt      Reader options (e.g. skip loading of normals ... depends
                     on the reader capabilities)
    @param _clear    Clear the target data before filling it (allows to
                     load multiple files into one Mesh)

    @return Successful?
*/
160
template <class Mesh>
161
bool
Jan Möbius's avatar
Jan Möbius committed
162 163 164
read_mesh(Mesh&         _mesh,
	  std::istream&       _is,
	  const std::string&  _ext,
165 166
	  Options&            _opt,
	  bool                _clear = true)
167 168 169
{
  if (_clear) _mesh.clear();
  ImporterT<Mesh> importer(_mesh);
170
  return IOManager().read(_is,_ext, importer, _opt);
171 172 173 174
}



175 176 177
//-----------------------------------------------------------------------------


178
/** \brief Write a mesh to the file _filename.
Jan Möbius's avatar
Jan Möbius committed
179

180
    The file format is determined by _filename's extension.
Jan Möbius's avatar
Jan Möbius committed
181

182 183
    \note If you link statically against OpenMesh, you have to add
          the define OM_STATIC_BUILD to your application. This will
184
          ensure that readers and writers get initialized correctly.
Jan Möbius's avatar
Jan Möbius committed
185 186 187 188 189

    @param _mesh     The mesh that will be written to file
    @param _filename output filename
    @param _opt      Writer options (e.g. writing of normals ... depends
                     on the writer capabilities)
Isaak Lim's avatar
Isaak Lim committed
190
    @param _precision specifies stream precision for ascii files
Jan Möbius's avatar
Jan Möbius committed
191 192 193

    @return Successful?
*/
194
template <class Mesh>
195
bool write_mesh(const Mesh&        _mesh,
Jan Möbius's avatar
Jan Möbius committed
196
                const std::string& _filename,
197 198 199
                Options            _opt = Options::Default,
                std::streamsize    _precision = 6)
{
200
  ExporterT<Mesh> exporter(_mesh);
201
  return IOManager().write(_filename, exporter, _opt, _precision);
202 203 204 205 206 207
}


//-----------------------------------------------------------------------------


208
/** Write a mesh to an open std::ostream.
Jan Möbius's avatar
Jan Möbius committed
209

210
    The file format is determined by parameter _ext. _ext has to include
Jan Möbius's avatar
Jan Möbius committed
211 212
    ".[format]" in order to work properly (e.g. ".OFF")

213 214
    \note If you link statically against OpenMesh, you have to add
          the define OM_STATIC_BUILD to your application. This will
215
          ensure that readers and writers get initialized correctly.
Jan Möbius's avatar
Jan Möbius committed
216 217 218 219 220 221

    @param _mesh     The mesh that will be written to file
    @param _os       output stream to write into
    @param _ext      extension defining the type of output
    @param _opt      Writer options (e.g. writing of normals ... depends
                     on the writer capabilities)
Isaak Lim's avatar
Isaak Lim committed
222
    @param _precision specifies stream precision for ascii files
Jan Möbius's avatar
Jan Möbius committed
223 224 225

    @return Successful?
*/
226
template <class Mesh>
227 228 229 230 231 232
bool write_mesh(const Mesh&        _mesh,
		std::ostream&      _os,
	        const std::string& _ext,
                Options            _opt = Options::Default,
                std::streamsize    _precision = 6)
{
233
  ExporterT<Mesh> exporter(_mesh);
234
  return IOManager().write(_os,_ext, exporter, _opt, _precision);
235 236 237 238 239
}


//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
240 241 242 243
/** \brief Get binary size of data

  This function calls the corresponding writer which calculates the size
  of the data that would be written to a binary file
244

Jan Möbius's avatar
Jan Möbius committed
245 246 247 248 249 250 251 252 253 254
  The file format is determined by parameter _ext. _ext has to include
  ".[format]" in order to work properly (e.g. ".OFF")

  @param _mesh Mesh to write
  @param _ext extension of the file (used to determine the writing module)
  @param _opt  Writer options (e.g. writing of normals ... depends
               on the writer capabilities)

  @return Binary size in bytes used when writing the data
*/
255
template <class Mesh>
256
size_t binary_size(const Mesh&        _mesh,
Jan Möbius's avatar
Jan Möbius committed
257 258
                   const std::string& _ext,
                   Options            _opt = Options::Default)
259 260
{
  ExporterT<Mesh> exporter(_mesh);
Jan Möbius's avatar
Jan Möbius committed
261
  return IOManager().binary_size(_ext, exporter, _opt);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}


//-----------------------------------------------------------------------------

//@}


//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================
#if defined(OM_STATIC_BUILD) || defined(ARCH_DARWIN)
#  include <OpenMesh/Core/IO/IOInstances.hh>
#endif
//=============================================================================
#endif
//=============================================================================