2015-04-28 11:54:17 +00:00
|
|
|
/* ========================================================================= *
|
2009-06-04 08:46:29 +00:00
|
|
|
* *
|
|
|
|
|
* OpenMesh *
|
2024-05-14 10:59:20 +02:00
|
|
|
* Copyright (c) 2001-2025, RWTH-Aachen University *
|
2015-04-28 13:07:46 +00:00
|
|
|
* Department of Computer Graphics and Multimedia *
|
2015-04-28 11:33:32 +00:00
|
|
|
* All rights reserved. *
|
|
|
|
|
* www.openmesh.org *
|
2009-06-04 08:46:29 +00:00
|
|
|
* *
|
2012-09-26 15:02:18 +00:00
|
|
|
*---------------------------------------------------------------------------*
|
2015-04-28 11:33:32 +00:00
|
|
|
* This file is part of OpenMesh. *
|
|
|
|
|
*---------------------------------------------------------------------------*
|
2009-06-04 08:46:29 +00:00
|
|
|
* *
|
2015-04-28 11:33:32 +00:00
|
|
|
* 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. *
|
2015-04-28 11:54:17 +00:00
|
|
|
* *
|
|
|
|
|
* ========================================================================= */
|
2009-06-04 08:46:29 +00:00
|
|
|
|
2019-01-15 11:21:12 +01:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
//
|
|
|
|
|
// Helper Functions for binary reading / writing
|
|
|
|
|
//
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//== INCLUDES =================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------- STL
|
|
|
|
|
#include <algorithm>
|
2015-06-09 08:58:41 +00:00
|
|
|
#include <iostream>
|
2009-02-06 13:37:46 +00:00
|
|
|
// -------------------- OpenMesh
|
|
|
|
|
#include <OpenMesh/Core/IO/BinaryHelper.hh>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//== NAMESPACES ===============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenMesh {
|
|
|
|
|
namespace IO {
|
|
|
|
|
|
|
|
|
|
#ifndef DOXY_IGNORE_THIS
|
|
|
|
|
|
|
|
|
|
//== IMPLEMENTATION ===========================================================
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
short int read_short(FILE* _in, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u1 { short int s; unsigned char c[2]; } sc;
|
2023-08-23 10:16:01 +02:00
|
|
|
fread(reinterpret_cast<char*>(sc.c), 1, 2, _in);
|
2009-02-06 13:37:46 +00:00
|
|
|
if (_swap) std::swap(sc.c[0], sc.c[1]);
|
|
|
|
|
return sc.s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
int read_int(FILE* _in, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u2 { int i; unsigned char c[4]; } ic;
|
2023-08-23 10:16:01 +02:00
|
|
|
fread(reinterpret_cast<char*>(ic.c), 1, 4, _in);
|
2009-02-06 13:37:46 +00:00
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(ic.c[0], ic.c[3]);
|
|
|
|
|
std::swap(ic.c[1], ic.c[2]);
|
|
|
|
|
}
|
|
|
|
|
return ic.i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
float read_float(FILE* _in, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u3 { float f; unsigned char c[4]; } fc;
|
2023-08-23 10:16:01 +02:00
|
|
|
fread(reinterpret_cast<char*>(fc.c), 1, 4, _in);
|
2009-02-06 13:37:46 +00:00
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(fc.c[0], fc.c[3]);
|
|
|
|
|
std::swap(fc.c[1], fc.c[2]);
|
|
|
|
|
}
|
|
|
|
|
return fc.f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
double read_double(FILE* _in, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u4 { double d; unsigned char c[8]; } dc;
|
2023-08-23 10:16:01 +02:00
|
|
|
fread(reinterpret_cast<char*>(dc.c), 1, 8, _in);
|
2009-02-06 13:37:46 +00:00
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(dc.c[0], dc.c[7]);
|
|
|
|
|
std::swap(dc.c[1], dc.c[6]);
|
|
|
|
|
std::swap(dc.c[2], dc.c[5]);
|
|
|
|
|
std::swap(dc.c[3], dc.c[4]);
|
|
|
|
|
}
|
|
|
|
|
return dc.d;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
short int read_short(std::istream& _in, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u1 { short int s; unsigned char c[2]; } sc;
|
2023-08-23 10:16:01 +02:00
|
|
|
_in.read(reinterpret_cast<char*>(sc.c), 2);
|
2012-09-26 15:02:18 +00:00
|
|
|
if (_swap) std::swap(sc.c[0], sc.c[1]);
|
|
|
|
|
return sc.s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int read_int(std::istream& _in, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u2 { int i; unsigned char c[4]; } ic;
|
2023-08-23 10:16:01 +02:00
|
|
|
_in.read(reinterpret_cast<char*>(ic.c), 4);
|
2012-09-26 15:02:18 +00:00
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(ic.c[0], ic.c[3]);
|
|
|
|
|
std::swap(ic.c[1], ic.c[2]);
|
|
|
|
|
}
|
|
|
|
|
return ic.i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float read_float(std::istream& _in, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u3 { float f; unsigned char c[4]; } fc;
|
2023-08-23 10:16:01 +02:00
|
|
|
_in.read(reinterpret_cast<char*>(fc.c), 4);
|
2012-09-26 15:02:18 +00:00
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(fc.c[0], fc.c[3]);
|
|
|
|
|
std::swap(fc.c[1], fc.c[2]);
|
|
|
|
|
}
|
|
|
|
|
return fc.f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
double read_double(std::istream& _in, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u4 { double d; unsigned char c[8]; } dc;
|
2023-08-23 10:16:01 +02:00
|
|
|
_in.read(reinterpret_cast<char*>(dc.c), 8);
|
2012-09-26 15:02:18 +00:00
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(dc.c[0], dc.c[7]);
|
|
|
|
|
std::swap(dc.c[1], dc.c[6]);
|
|
|
|
|
std::swap(dc.c[2], dc.c[5]);
|
|
|
|
|
std::swap(dc.c[3], dc.c[4]);
|
|
|
|
|
}
|
|
|
|
|
return dc.d;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
void write_short(short int _i, FILE* _out, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u1 { short int s; unsigned char c[2]; } sc;
|
2009-02-06 13:37:46 +00:00
|
|
|
sc.s = _i;
|
|
|
|
|
if (_swap) std::swap(sc.c[0], sc.c[1]);
|
2023-08-23 10:16:01 +02:00
|
|
|
fwrite(reinterpret_cast<char*>(sc.c), 1, 2, _out);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
void write_int(int _i, FILE* _out, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u2 { int i; unsigned char c[4]; } ic;
|
2009-02-06 13:37:46 +00:00
|
|
|
ic.i = _i;
|
|
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(ic.c[0], ic.c[3]);
|
|
|
|
|
std::swap(ic.c[1], ic.c[2]);
|
|
|
|
|
}
|
2023-08-23 10:16:01 +02:00
|
|
|
fwrite(reinterpret_cast<char*>(ic.c), 1, 4, _out);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
void write_float(float _f, FILE* _out, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u3 { float f; unsigned char c[4]; } fc;
|
2009-02-06 13:37:46 +00:00
|
|
|
fc.f = _f;
|
|
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(fc.c[0], fc.c[3]);
|
|
|
|
|
std::swap(fc.c[1], fc.c[2]);
|
|
|
|
|
}
|
2023-08-23 10:16:01 +02:00
|
|
|
fwrite(reinterpret_cast<char*>(fc.c), 1, 4, _out);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2012-09-26 15:02:18 +00:00
|
|
|
void write_double(double _d, FILE* _out, bool _swap)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2012-04-05 14:26:34 +00:00
|
|
|
union u4 { double d; unsigned char c[8]; } dc;
|
2009-02-06 13:37:46 +00:00
|
|
|
dc.d = _d;
|
|
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(dc.c[0], dc.c[7]);
|
|
|
|
|
std::swap(dc.c[1], dc.c[6]);
|
|
|
|
|
std::swap(dc.c[2], dc.c[5]);
|
|
|
|
|
std::swap(dc.c[3], dc.c[4]);
|
|
|
|
|
}
|
2023-08-23 10:16:01 +02:00
|
|
|
fwrite(reinterpret_cast<char*>(dc.c), 1, 8, _out);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-27 10:18:43 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void write_short(short int _i, std::ostream& _out, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u1 { short int s; unsigned char c[2]; } sc;
|
|
|
|
|
sc.s = _i;
|
|
|
|
|
if (_swap) std::swap(sc.c[0], sc.c[1]);
|
2023-08-23 10:16:01 +02:00
|
|
|
_out.write(reinterpret_cast<char*>(sc.c), 2);
|
2012-09-27 10:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void write_int(int _i, std::ostream& _out, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u2 { int i; unsigned char c[4]; } ic;
|
|
|
|
|
ic.i = _i;
|
|
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(ic.c[0], ic.c[3]);
|
|
|
|
|
std::swap(ic.c[1], ic.c[2]);
|
|
|
|
|
}
|
2023-08-23 10:16:01 +02:00
|
|
|
_out.write(reinterpret_cast<char*>(ic.c), 4);
|
2012-09-27 10:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void write_float(float _f, std::ostream& _out, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u3 { float f; unsigned char c[4]; } fc;
|
|
|
|
|
fc.f = _f;
|
|
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(fc.c[0], fc.c[3]);
|
|
|
|
|
std::swap(fc.c[1], fc.c[2]);
|
|
|
|
|
}
|
2023-08-23 10:16:01 +02:00
|
|
|
_out.write(reinterpret_cast<char*>(fc.c), 4);
|
2012-09-27 10:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void write_double(double _d, std::ostream& _out, bool _swap)
|
|
|
|
|
{
|
|
|
|
|
union u4 { double d; unsigned char c[8]; } dc;
|
|
|
|
|
dc.d = _d;
|
|
|
|
|
if (_swap) {
|
|
|
|
|
std::swap(dc.c[0], dc.c[7]);
|
|
|
|
|
std::swap(dc.c[1], dc.c[6]);
|
|
|
|
|
std::swap(dc.c[2], dc.c[5]);
|
|
|
|
|
std::swap(dc.c[3], dc.c[4]);
|
|
|
|
|
}
|
2023-08-23 10:16:01 +02:00
|
|
|
_out.write(reinterpret_cast<char*>(dc.c), 8);
|
2012-09-27 10:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
} // namespace IO
|
|
|
|
|
} // namespace OpenMesh
|
|
|
|
|
//=============================================================================
|