OMWriter.cc 21 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
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
Typo  
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                               *
Jan Möbius's avatar
Jan Möbius committed
8
 *                                                                           *
9
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 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.   *
 *                                                                           *
 * 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.               *
 *                                                                           *
 * 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$                   *
Jan Möbius's avatar
Jan Möbius committed
46 47 48 49 50 51 52 53 54
 *                                                                           *
\*===========================================================================*/


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

#include <OpenMesh/Core/System/config.h>
// -------------------- STL
#if defined( OM_CC_MIPS )
Jan Möbius's avatar
Jan Möbius committed
55 56
  #include <time.h>
  #include <string.h>
Jan Möbius's avatar
Jan Möbius committed
57
#else
Jan Möbius's avatar
Jan Möbius committed
58 59
  #include <ctime>
  #include <cstring>
Jan Möbius's avatar
Jan Möbius committed
60
#endif
Jan Möbius's avatar
Jan Möbius committed
61

Jan Möbius's avatar
Jan Möbius committed
62
#include <fstream>
63 64
#include <vector>

Jan Möbius's avatar
Jan Möbius committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/OMFormat.hh>
#include <OpenMesh/Core/IO/exporter/BaseExporter.hh>
#include <OpenMesh/Core/IO/writer/OMWriter.hh>

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


namespace OpenMesh {
namespace IO {


//=== INSTANCIATE =============================================================


// register the OMLoader singleton with MeshLoader
_OMWriter_  __OMWriterInstance;
_OMWriter_& OMWriter() { return __OMWriterInstance; }


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


const OMFormat::uchar _OMWriter_::magic_[3] = "OM";
89
const OMFormat::uint8 _OMWriter_::version_  = OMFormat::mk_version(2,0);
Jan Möbius's avatar
Jan Möbius committed
90 91 92


_OMWriter_::
93 94 95
_OMWriter_()
{
  IOManager().register_module(this);
Jan Möbius's avatar
Jan Möbius committed
96 97 98 99 100
}


bool
_OMWriter_::write(const std::string& _filename, BaseExporter& _be,
101
                   Options _opt, std::streamsize /*_precision*/) const
Jan Möbius's avatar
Jan Möbius committed
102 103 104 105 106
{
  // check whether exporter can give us an OpenMesh BaseKernel
  if (!_be.kernel()) return false;


107 108
  // check for om extension in filename, we can only handle OM
  if (_filename.rfind(".om") == std::string::npos)
Jan Möbius's avatar
Jan Möbius committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    return false;

  _opt += Options::Binary; // only binary format supported

  std::ofstream ofs(_filename.c_str(), std::ios::binary);

  // check if file is open
  if (!ofs.is_open())
  {
    omerr() << "[OMWriter] : cannot open file " << _filename << std::endl;
    return false;
  }

  // call stream save method
  bool rc = write(ofs, _be, _opt);
124

Jan Möbius's avatar
Jan Möbius committed
125 126 127 128 129 130 131 132 133 134 135
  // close filestream
  ofs.close();

  // return success/failure notice
  return rc;
}


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

bool
136
_OMWriter_::write(std::ostream& _os, BaseExporter& _be, Options _opt, std::streamsize /*_precision*/) const
Jan Möbius's avatar
Jan Möbius committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
{
//   std::clog << "[OMWriter]::write( stream )\n";

  // check exporter features
  if ( !check( _be, _opt ) )
  {
    omerr() << "[OMWriter]: exporter does not support wanted feature!\n";
    return false;
  }

  // Maybe an ascii version will be implemented in the future.
  // For now, support only a binary format
  if ( !_opt.check( Options::Binary ) )
    _opt += Options::Binary;

  // Ignore LSB/MSB bit. Always store in LSB (little endian)
  _opt += Options::LSB;
  _opt -= Options::MSB;

  return write_binary(_os, _be, _opt);
}


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


#ifndef DOXY_IGNORE_THIS
template <typename T> struct Enabler
{
Jan Möbius's avatar
Jan Möbius committed
166
  explicit Enabler( T& obj ) : obj_(obj)
Jan Möbius's avatar
Jan Möbius committed
167 168 169 170 171 172 173 174 175 176 177 178
  {}

  ~Enabler() { obj_.enable(); }

  T& obj_;
};
#endif


bool _OMWriter_::write_binary(std::ostream& _os, BaseExporter& _be,
                               Options _opt) const
{
Jan Möbius's avatar
Jan Möbius committed
179 180 181
  #ifndef DOXY_IGNORE_THIS
    Enabler<mostream> enabler(omlog());
  #endif
Jan Möbius's avatar
Jan Möbius committed
182 183 184 185 186

  size_t bytes = 0;

  bool swap = _opt.check(Options::Swap) || (Endian::local() == Endian::MSB);

Max Lyon's avatar
Max Lyon committed
187
  unsigned int i, nV, nF;
Jan Möbius's avatar
Jan Möbius committed
188 189 190 191 192
  Vec3f v;
  Vec2f t;

  // -------------------- write header
  OMFormat::Header header;
193

Jan Möbius's avatar
Jan Möbius committed
194 195 196 197
  header.magic_[0]   = 'O';
  header.magic_[1]   = 'M';
  header.mesh_       = _be.is_triangle_mesh() ? 'T' : 'P';
  header.version_    = version_;
Jan Möbius's avatar
Jan Möbius committed
198 199 200
  header.n_vertices_ = int(_be.n_vertices());
  header.n_faces_    = int(_be.n_faces());
  header.n_edges_    = int(_be.n_edges());
201

Jan Möbius's avatar
Jan Möbius committed
202
  bytes += store( _os, header, swap );
203

Jan Möbius's avatar
Jan Möbius committed
204
  // ---------------------------------------- write chunks
205

Jan Möbius's avatar
Jan Möbius committed
206
  OMFormat::Chunk::Header chunk_header;
207 208


Jan Möbius's avatar
Jan Möbius committed
209
  // -------------------- write vertex data
210

Jan Möbius's avatar
Jan Möbius committed
211 212 213 214 215 216 217 218 219 220 221 222
  // ---------- write vertex position
  if (_be.n_vertices())
  {
    v = _be.point(VertexHandle(0));
    chunk_header.reserved_ = 0;
    chunk_header.name_     = false;
    chunk_header.entity_   = OMFormat::Chunk::Entity_Vertex;
    chunk_header.type_     = OMFormat::Chunk::Type_Pos;
    chunk_header.signed_   = OMFormat::is_signed(v[0]);
    chunk_header.float_    = OMFormat::is_float(v[0]);
    chunk_header.dim_      = OMFormat::dim(v);
    chunk_header.bits_     = OMFormat::bits(v[0]);
223

Jan Möbius's avatar
Jan Möbius committed
224
    bytes += store( _os, chunk_header, swap );
Jan Möbius's avatar
Jan Möbius committed
225
    for (i=0, nV=header.n_vertices_; i<nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      bytes += vector_store( _os, _be.point(VertexHandle(i)), swap );
  }


  // ---------- write vertex normal
  if (_be.n_vertices() && _opt.check( Options::VertexNormal ))
  {
    Vec3f n = _be.normal(VertexHandle(0));

    chunk_header.name_     = false;
    chunk_header.entity_   = OMFormat::Chunk::Entity_Vertex;
    chunk_header.type_     = OMFormat::Chunk::Type_Normal;
    chunk_header.signed_   = OMFormat::is_signed(n[0]);
    chunk_header.float_    = OMFormat::is_float(n[0]);
    chunk_header.dim_      = OMFormat::dim(n);
    chunk_header.bits_     = OMFormat::bits(n[0]);
242

Jan Möbius's avatar
Jan Möbius committed
243
    bytes += store( _os, chunk_header, swap );
Jan Möbius's avatar
Jan Möbius committed
244
    for (i=0, nV=header.n_vertices_; i<nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
245 246 247 248
      bytes += vector_store( _os, _be.normal(VertexHandle(i)), swap );
  }

  // ---------- write vertex color
249
  if (_be.n_vertices() && _opt.check( Options::VertexColor ) && _be.has_vertex_colors() )
250
  {
Jan Möbius's avatar
Jan Möbius committed
251
    Vec3uc c = _be.color(VertexHandle(0));
252

Jan Möbius's avatar
Jan Möbius committed
253 254 255
    chunk_header.name_     = false;
    chunk_header.entity_   = OMFormat::Chunk::Entity_Vertex;
    chunk_header.type_     = OMFormat::Chunk::Type_Color;
Matthias Möller's avatar
Matthias Möller committed
256 257
    chunk_header.signed_   = OMFormat::is_signed( c[0] );
    chunk_header.float_    = OMFormat::is_float( c[0] );
Jan Möbius's avatar
Jan Möbius committed
258
    chunk_header.dim_      = OMFormat::dim( c );
Matthias Möller's avatar
Matthias Möller committed
259
    chunk_header.bits_     = OMFormat::bits( c[0] );
Jan Möbius's avatar
Jan Möbius committed
260 261

    bytes += store( _os, chunk_header, swap );
Jan Möbius's avatar
Jan Möbius committed
262
    for (i=0, nV=header.n_vertices_; i<nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
263 264 265 266
      bytes += vector_store( _os, _be.color(VertexHandle(i)), swap );
  }

  // ---------- write vertex texture coords
Jan Möbius's avatar
Jan Möbius committed
267 268
  if (_be.n_vertices() && _opt.check(Options::VertexTexCoord)) {

Jan Möbius's avatar
Jan Möbius committed
269 270
    t = _be.texcoord(VertexHandle(0));

Jan Möbius's avatar
Jan Möbius committed
271 272 273 274 275 276 277
    chunk_header.name_ = false;
    chunk_header.entity_ = OMFormat::Chunk::Entity_Vertex;
    chunk_header.type_ = OMFormat::Chunk::Type_Texcoord;
    chunk_header.signed_ = OMFormat::is_signed(t[0]);
    chunk_header.float_ = OMFormat::is_float(t[0]);
    chunk_header.dim_ = OMFormat::dim(t);
    chunk_header.bits_ = OMFormat::bits(t[0]);
278

Jan Möbius's avatar
Jan Möbius committed
279
    // std::clog << chunk_header << std::endl;
Jan Möbius's avatar
Jan Möbius committed
280 281
    bytes += store(_os, chunk_header, swap);

Jan Möbius's avatar
Jan Möbius committed
282
    for (i = 0, nV = header.n_vertices_; i < nV; ++i)
Jan Möbius's avatar
Jan Möbius committed
283
      bytes += vector_store(_os, _be.texcoord(VertexHandle(i)), swap);
Jan Möbius's avatar
Jan Möbius committed
284 285 286

  }

287 288 289 290 291 292 293 294 295 296
  // ---------- wirte halfedge data
  if (_be.n_edges())
  {
    chunk_header.reserved_ = 0;
    chunk_header.name_     = false;
    chunk_header.entity_   = OMFormat::Chunk::Entity_Halfedge;
    chunk_header.type_     = OMFormat::Chunk::Type_Topology;
    chunk_header.signed_   = true;
    chunk_header.float_    = true; // TODO: is this correct? This causes a scalar size of 1 in OMFormat.hh scalar_size which we need I think?
    chunk_header.dim_      = OMFormat::Chunk::Dim_3D;
297
    chunk_header.bits_     = OMFormat::needed_bits(_be.n_edges()*4); // *2 due to halfedge ids being stored, *2 due to signedness
298 299

    bytes += store( _os, chunk_header, swap );
Max Lyon's avatar
Max Lyon committed
300 301
    auto nE=header.n_edges_*2;
    for (i=0; i<nE; ++i)
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    {
      auto next_id      = _be.get_next_halfedge_id(HalfedgeHandle(static_cast<int>(i)));
      auto to_vertex_id = _be.get_to_vertex_id(HalfedgeHandle(static_cast<int>(i)));
      auto face_id      = _be.get_face_id(HalfedgeHandle(static_cast<int>(i)));

      bytes += store( _os, next_id,      OMFormat::Chunk::Integer_Size(chunk_header.bits_), swap );
      bytes += store( _os, to_vertex_id, OMFormat::Chunk::Integer_Size(chunk_header.bits_), swap );
      bytes += store( _os, face_id,      OMFormat::Chunk::Integer_Size(chunk_header.bits_), swap );
    }
  }

  // ---------- write vertex topology (outgoing halfedge)
  if (_be.n_vertices())
  {
    chunk_header.reserved_ = 0;
    chunk_header.name_     = false;
    chunk_header.entity_   = OMFormat::Chunk::Entity_Vertex;
    chunk_header.type_     = OMFormat::Chunk::Type_Topology;
    chunk_header.signed_   = true;
    chunk_header.float_    = true; // TODO: is this correct? This causes a scalar size of 1 in OMFormat.hh scalar_size which we need I think?
    chunk_header.dim_      = OMFormat::Chunk::Dim_1D;
323
    chunk_header.bits_     = OMFormat::needed_bits(_be.n_edges()*4); // *2 due to halfedge ids being stored, *2 due to signedness
324 325 326 327 328 329 330

    bytes += store( _os, chunk_header, swap );
    for (i=0, nV=header.n_vertices_; i<nV; ++i)
      bytes += store( _os, _be.get_halfedge_id(VertexHandle(i)), OMFormat::Chunk::Integer_Size(chunk_header.bits_), swap );
  }


Jan Möbius's avatar
Jan Möbius committed
331 332 333 334 335 336 337
  // -------------------- write face data

  // ---------- write topology
  {
    chunk_header.name_     = false;
    chunk_header.entity_   = OMFormat::Chunk::Entity_Face;
    chunk_header.type_     = OMFormat::Chunk::Type_Topology;
338 339 340
    chunk_header.signed_   = true;
    chunk_header.float_    = true; // TODO: is this correct? This causes a scalar size of 1 in OMFormat.hh scalar_size which we need I think?
    chunk_header.dim_      = OMFormat::Chunk::Dim_1D;
341
    chunk_header.bits_     = OMFormat::needed_bits(_be.n_edges()*4); // *2 due to halfedge ids being stored, *2 due to signedness
Jan Möbius's avatar
Jan Möbius committed
342 343 344

    bytes += store( _os, chunk_header, swap );

Jan Möbius's avatar
Jan Möbius committed
345
    for (i=0, nF=header.n_faces_; i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
346
    {
347 348
      auto size = OMFormat::Chunk::Integer_Size(chunk_header.bits_);
      bytes += store( _os, _be.get_halfedge_id(FaceHandle(i)), size, swap);
Jan Möbius's avatar
Jan Möbius committed
349 350 351 352 353
    }
  }

  // ---------- write face normals

354
  if (_be.n_faces() && _be.has_face_normals() && _opt.check(Options::FaceNormal) )
Jan Möbius's avatar
Jan Möbius committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  {
#define NEW_STYLE 0
#if NEW_STYLE
    const BaseProperty *bp = _be.kernel()._get_fprop("f:normals");

    if (bp)
    {
#endif
      Vec3f n = _be.normal(FaceHandle(0));

      chunk_header.name_     = false;
      chunk_header.entity_   = OMFormat::Chunk::Entity_Face;
      chunk_header.type_     = OMFormat::Chunk::Type_Normal;
      chunk_header.signed_   = OMFormat::is_signed(n[0]);
      chunk_header.float_    = OMFormat::is_float(n[0]);
      chunk_header.dim_      = OMFormat::dim(n);
      chunk_header.bits_     = OMFormat::bits(n[0]);
372

Jan Möbius's avatar
Jan Möbius committed
373 374
      bytes += store( _os, chunk_header, swap );
#if !NEW_STYLE
Jan Möbius's avatar
Jan Möbius committed
375
      for (i=0, nF=header.n_faces_; i<nF; ++i)
Jan Möbius's avatar
Jan Möbius committed
376 377 378 379 380 381 382 383 384 385 386 387 388
        bytes += vector_store( _os, _be.normal(FaceHandle(i)), swap );
#else
      bytes += bp->store(_os, swap );
    }
    else
      return false;
#endif
#undef NEW_STYLE
  }


  // ---------- write face color

389
  if (_be.n_faces() && _be.has_face_colors() && _opt.check( Options::FaceColor ))
390
  {
Jan Möbius's avatar
Jan Möbius committed
391 392 393
#define NEW_STYLE 0
#if NEW_STYLE
    const BaseProperty *bp = _be.kernel()._get_fprop("f:colors");
394

Jan Möbius's avatar
Jan Möbius committed
395 396 397 398
    if (bp)
    {
#endif
      Vec3uc c;
399

Jan Möbius's avatar
Jan Möbius committed
400 401 402 403 404 405 406
      chunk_header.name_     = false;
      chunk_header.entity_   = OMFormat::Chunk::Entity_Face;
      chunk_header.type_     = OMFormat::Chunk::Type_Color;
      chunk_header.signed_   = OMFormat::is_signed( c[0] );
      chunk_header.float_    = OMFormat::is_float( c[0] );
      chunk_header.dim_      = OMFormat::dim( c );
      chunk_header.bits_     = OMFormat::bits( c[0] );
407

Jan Möbius's avatar
Jan Möbius committed
408 409
      bytes += store( _os, chunk_header, swap );
#if !NEW_STYLE
Jan Möbius's avatar
Jan Möbius committed
410
      for (i=0, nF=header.n_faces_; i<nF; ++i)
411
        bytes += vector_store( _os, _be.color(FaceHandle(i)), swap );
Jan Möbius's avatar
Jan Möbius committed
412 413 414 415 416 417 418 419
#else
      bytes += bp->store(_os, swap);
    }
    else
      return false;
#endif
  }

Max Lyon's avatar
Max Lyon committed
420 421 422
  // ---------- write vertex status
  if (_be.n_vertices() && _be.has_vertex_status() && _opt.check(Options::Status))
  {
423 424 425 426 427 428 429 430 431 432 433 434 435 436
    auto s = _be.status(VertexHandle(0));
    chunk_header.name_ = false;
    chunk_header.entity_ = OMFormat::Chunk::Entity_Vertex;
    chunk_header.type_ = OMFormat::Chunk::Type_Status;
    chunk_header.signed_ = false;
    chunk_header.float_ = false;
    chunk_header.dim_ = OMFormat::Chunk::Dim_1D;
    chunk_header.bits_ = OMFormat::bits(s);

    // std::clog << chunk_header << std::endl;
    bytes += store(_os, chunk_header, swap);

    for (i = 0, nV = header.n_vertices_; i < nV; ++i)
      bytes += store(_os, _be.status(VertexHandle(i)), swap);
Max Lyon's avatar
Max Lyon committed
437 438 439 440 441
  }

  // ---------- write edge status
  if (_be.n_edges() && _be.has_edge_status() && _opt.check(Options::Status))
  {
442 443
    auto s = _be.status(EdgeHandle(0));
    chunk_header.name_ = false;
444
    chunk_header.entity_ = OMFormat::Chunk::Entity_Edge;
445 446 447 448 449 450 451 452 453 454 455
    chunk_header.type_ = OMFormat::Chunk::Type_Status;
    chunk_header.signed_ = false;
    chunk_header.float_ = false;
    chunk_header.dim_ = OMFormat::Chunk::Dim_1D;
    chunk_header.bits_ = OMFormat::bits(s);

    // std::clog << chunk_header << std::endl;
    bytes += store(_os, chunk_header, swap);

    for (i = 0, nV = header.n_edges_; i < nV; ++i)
      bytes += store(_os, _be.status(EdgeHandle(i)), swap);
Max Lyon's avatar
Max Lyon committed
456 457 458 459 460
  }

  // ---------- write halfedge status
  if (_be.n_edges() && _be.has_halfedge_status() && _opt.check(Options::Status))
  {
461 462
    auto s = _be.status(HalfedgeHandle(0));
    chunk_header.name_ = false;
463
    chunk_header.entity_ = OMFormat::Chunk::Entity_Halfedge;
464 465 466 467 468 469 470 471 472 473 474
    chunk_header.type_ = OMFormat::Chunk::Type_Status;
    chunk_header.signed_ = false;
    chunk_header.float_ = false;
    chunk_header.dim_ = OMFormat::Chunk::Dim_1D;
    chunk_header.bits_ = OMFormat::bits(s);

    // std::clog << chunk_header << std::endl;
    bytes += store(_os, chunk_header, swap);

    for (i = 0, nV = header.n_edges_ * 2; i < nV; ++i)
      bytes += store(_os, _be.status(HalfedgeHandle(i)), swap);
Max Lyon's avatar
Max Lyon committed
475 476 477 478 479
  }

  // ---------- write face status
  if (_be.n_faces() && _be.has_face_status() && _opt.check(Options::Status))
  {
480 481
    auto s = _be.status(FaceHandle(0));
    chunk_header.name_ = false;
482
    chunk_header.entity_ = OMFormat::Chunk::Entity_Face;
483 484 485 486 487 488 489 490 491 492 493
    chunk_header.type_ = OMFormat::Chunk::Type_Status;
    chunk_header.signed_ = false;
    chunk_header.float_ = false;
    chunk_header.dim_ = OMFormat::Chunk::Dim_1D;
    chunk_header.bits_ = OMFormat::bits(s);

    // std::clog << chunk_header << std::endl;
    bytes += store(_os, chunk_header, swap);

    for (i = 0, nV = header.n_faces_; i < nV; ++i)
      bytes += store(_os, _be.status(FaceHandle(i)), swap);
Max Lyon's avatar
Max Lyon committed
494 495
  }

Jan Möbius's avatar
Jan Möbius committed
496 497
  // -------------------- write custom properties

498

Jan Möbius's avatar
Jan Möbius committed
499
  BaseKernel::const_prop_iterator prop;
500

Jan Möbius's avatar
Jan Möbius committed
501 502 503 504
  for (prop  = _be.kernel()->vprops_begin();
       prop != _be.kernel()->vprops_end(); ++prop)
  {
    if ( !*prop ) continue;
Max Lyon's avatar
Max Lyon committed
505
    if ( (*prop)->name()[1]==':') continue;
506
    bytes += store_binary_custom_chunk(_os, **prop,
Jan Möbius's avatar
Jan Möbius committed
507 508 509 510 511 512
				       OMFormat::Chunk::Entity_Vertex, swap );
  }
  for (prop  = _be.kernel()->fprops_begin();
       prop != _be.kernel()->fprops_end(); ++prop)
  {
    if ( !*prop ) continue;
Max Lyon's avatar
Max Lyon committed
513
    if ( (*prop)->name()[1]==':') continue;
514
    bytes += store_binary_custom_chunk(_os, **prop,
Jan Möbius's avatar
Jan Möbius committed
515 516 517 518 519 520
				       OMFormat::Chunk::Entity_Face, swap );
  }
  for (prop  = _be.kernel()->eprops_begin();
       prop != _be.kernel()->eprops_end(); ++prop)
  {
    if ( !*prop ) continue;
Max Lyon's avatar
Max Lyon committed
521
    if ( (*prop)->name()[1]==':') continue;
522
    bytes += store_binary_custom_chunk(_os, **prop,
Jan Möbius's avatar
Jan Möbius committed
523 524 525 526 527 528
				       OMFormat::Chunk::Entity_Edge, swap );
  }
  for (prop  = _be.kernel()->hprops_begin();
       prop != _be.kernel()->hprops_end(); ++prop)
  {
    if ( !*prop ) continue;
Max Lyon's avatar
Max Lyon committed
529
    if ( (*prop)->name()[1]==':') continue;
530
    bytes += store_binary_custom_chunk(_os, **prop,
Jan Möbius's avatar
Jan Möbius committed
531 532 533 534 535 536 537
				       OMFormat::Chunk::Entity_Halfedge, swap );
  }
  for (prop  = _be.kernel()->mprops_begin();
       prop != _be.kernel()->mprops_end(); ++prop)
  {
    if ( !*prop ) continue;
    if ( (*prop)->name()[1]==':') continue;
538
    bytes += store_binary_custom_chunk(_os, **prop,
Jan Möbius's avatar
Jan Möbius committed
539 540 541
				       OMFormat::Chunk::Entity_Mesh, swap );
  }

542 543 544 545 546
  memset(&chunk_header, 0, sizeof(chunk_header));
  chunk_header.name_ = false;
  chunk_header.entity_ = OMFormat::Chunk::Entity_Sentinel;
  bytes += store(_os, chunk_header, swap);

Jan Möbius's avatar
Jan Möbius committed
547
  std::clog << "#bytes written: " << bytes << std::endl;
Jan Möbius's avatar
Jan Möbius committed
548 549 550 551 552 553

  return true;
}

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

554
size_t _OMWriter_::store_binary_custom_chunk(std::ostream& _os,
Jan Möbius's avatar
Jan Möbius committed
555 556 557 558
					     const BaseProperty& _bp,
					     OMFormat::Chunk::Entity _entity,
					     bool _swap) const
{
Jan Möbius's avatar
Jan Möbius committed
559 560
  //omlog() << "Custom Property " << OMFormat::as_string(_entity) << " property ["
  //	<< _bp.name() << "]" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
561

562
  // Don't store if
Jan Möbius's avatar
Jan Möbius committed
563 564 565 566
  // 1. it is not persistent
  // 2. it's name is empty
  if ( !_bp.persistent() || _bp.name().empty() )
  {
Jan Möbius's avatar
Jan Möbius committed
567
    //omlog() << "  skipped\n";
Jan Möbius's avatar
Jan Möbius committed
568 569 570 571 572
    return 0;
  }

  size_t bytes = 0;

Jan Möbius's avatar
Jan Möbius committed
573
  OMFormat::Chunk::esize_t element_size   = OMFormat::Chunk::esize_t(_bp.element_size());
Jan Möbius's avatar
Jan Möbius committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587
  OMFormat::Chunk::Header  chdr;

  // set header
  chdr.name_     = true;
  chdr.entity_   = _entity;
  chdr.type_     = OMFormat::Chunk::Type_Custom;
  chdr.signed_   = 0;
  chdr.float_    = 0;
  chdr.dim_      = OMFormat::Chunk::Dim_1D; // ignored
  chdr.bits_     = element_size;


  // write custom chunk

588
  // 1. chunk header
Jan Möbius's avatar
Jan Möbius committed
589 590 591 592 593 594
  bytes += store( _os, chdr, _swap );

  // 2. property name
  bytes += store( _os, OMFormat::Chunk::PropertyName(_bp.name()), _swap );

  // 3. block size
595
  bytes += store( _os, _bp.size_of(), OMFormat::Chunk::Integer_32, _swap );
Jan Möbius's avatar
Jan Möbius committed
596
  //omlog() << "  n_bytes = " << _bp.size_of() << std::endl;
Jan Möbius's avatar
Jan Möbius committed
597 598 599 600 601

  // 4. data
  {
    size_t b;
    bytes += ( b=_bp.store( _os, _swap ) );
Jan Möbius's avatar
Jan Möbius committed
602
    //omlog() << "  b       = " << b << std::endl;
Jan Möbius's avatar
Jan Möbius committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    assert( b == _bp.size_of() );
  }
  return bytes;
}

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

size_t _OMWriter_::binary_size(BaseExporter& /* _be */, Options /* _opt */) const
{
  // std::clog << "[OMWriter]: binary_size()" << std::endl;
  size_t bytes  = sizeof( OMFormat::Header );

  // !!!TODO!!!

  return bytes;
}

//=============================================================================
} // namespace IO
} // namespace OpenMesh
//=============================================================================