color_cast.hh
/* ========================================================================= *
* *
* OpenMesh *
* Copyright (c) 2001-2015, RWTH-Aachen University *
* Department of Computer Graphics and Multimedia *
* All rights reserved. *
* www.openmesh.org *
* *
*---------------------------------------------------------------------------*
* 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. *
* *
* ========================================================================= */
/*===========================================================================*\
* *
* $Revision$ *
* $Date$ *
* *
\*===========================================================================*/
//=============================================================================
//
// Helper Functions for binary reading / writing
//
//=============================================================================
#ifndef OPENMESH_COLOR_CAST_HH
#define OPENMESH_COLOR_CAST_HH
//== INCLUDES =================================================================
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Utils/vector_cast.hh>
//== NAMESPACES ===============================================================
namespace OpenMesh {
//=============================================================================
//-----------------------------------------------------------------------------
#ifndef DOXY_IGNORE_THIS
template <typename dst_t, typename src_t>
struct color_caster
{
typedef dst_t return_type;
inline static return_type cast(const src_t& _src)
{
dst_t dst;
vector_cast(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
return dst;
}
};
template <>
struct color_caster<Vec3uc,Vec3f>
{
typedef Vec3uc return_type;
inline static return_type cast(const Vec3f& _src)
{
return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
(unsigned char)(_src[1]* 255.0f + 0.5f),
(unsigned char)(_src[2]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec3uc,Vec4f>
{
typedef Vec3uc return_type;
inline static return_type cast(const Vec4f& _src)
{
return Vec3uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
(unsigned char)(_src[1]* 255.0f + 0.5f),
(unsigned char)(_src[2]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec3i,Vec3f>
{
typedef Vec3i return_type;
inline static return_type cast(const Vec3f& _src)
{
return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
(int)(_src[1]* 255.0f + 0.5f),
(int)(_src[2]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec3i,Vec4f>
{
typedef Vec3i return_type;
inline static return_type cast(const Vec4f& _src)
{
return Vec3i( (int)(_src[0]* 255.0f + 0.5f),
(int)(_src[1]* 255.0f + 0.5f),
(int)(_src[2]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec4i,Vec4f>
{
typedef Vec4i return_type;
inline static return_type cast(const Vec4f& _src)
{
return Vec4i( (int)(_src[0]* 255.0f + 0.5f),
(int)(_src[1]* 255.0f + 0.5f),
(int)(_src[2]* 255.0f + 0.5f),
(int)(_src[3]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec3ui,Vec3f>
{
typedef Vec3ui return_type;
inline static return_type cast(const Vec3f& _src)
{
return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
(unsigned int)(_src[1]* 255.0f + 0.5f),
(unsigned int)(_src[2]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec3ui,Vec4f>
{
typedef Vec3ui return_type;
inline static return_type cast(const Vec4f& _src)
{
return Vec3ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
(unsigned int)(_src[1]* 255.0f + 0.5f),
(unsigned int)(_src[2]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec4ui,Vec4f>
{
typedef Vec4ui return_type;
inline static return_type cast(const Vec4f& _src)
{
return Vec4ui( (unsigned int)(_src[0]* 255.0f + 0.5f),
(unsigned int)(_src[1]* 255.0f + 0.5f),
(unsigned int)(_src[2]* 255.0f + 0.5f),
(unsigned int)(_src[3]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec4uc,Vec3f>
{
typedef Vec4uc return_type;
inline static return_type cast(const Vec3f& _src)
{
return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
(unsigned char)(_src[1]* 255.0f + 0.5f),
(unsigned char)(_src[2]* 255.0f + 0.5f),
(unsigned char)(255) );
}
};
template <>
struct color_caster<Vec4f,Vec3f>
{
typedef Vec4f return_type;
inline static return_type cast(const Vec3f& _src)
{
return Vec4f( _src[0],
_src[1],
_src[2],
1.0f );
}
};
template <>
struct color_caster<Vec4ui,Vec3uc>
{
typedef Vec4ui return_type;
inline static return_type cast(const Vec3uc& _src)
{
return Vec4ui(_src[0],
_src[1],
_src[2],
255 );
}
};
template <>
struct color_caster<Vec4f,Vec3i>
{
typedef Vec4f return_type;
inline static return_type cast(const Vec3i& _src)
{
const float f = 1.0f / 255.0f;
return Vec4f(_src[0]*f, _src[1]*f, _src[2]*f, 1.0f );
}
};
template <>
struct color_caster<Vec4uc,Vec4f>
{
typedef Vec4uc return_type;
inline static return_type cast(const Vec4f& _src)
{
return Vec4uc( (unsigned char)(_src[0]* 255.0f + 0.5f),
(unsigned char)(_src[1]* 255.0f + 0.5f),
(unsigned char)(_src[2]* 255.0f + 0.5f),
(unsigned char)(_src[3]* 255.0f + 0.5f) );
}
};
template <>
struct color_caster<Vec4f,Vec4i>
{
typedef Vec4f return_type;
inline static return_type cast(const Vec4i& _src)
{
const float f = 1.0f / 255.0f;
return Vec4f( _src[0] * f, _src[1] * f, _src[2] * f , _src[3] * f );
}
};
template <>
struct color_caster<Vec4uc,Vec3uc>
{
typedef Vec4uc return_type;
inline static return_type cast(const Vec3uc& _src)
{
return Vec4uc( _src[0], _src[1], _src[2], 255 );
}
};
template <>
struct color_caster<Vec3f, Vec3uc>
{
typedef Vec3f return_type;
inline static return_type cast(const Vec3uc& _src)
{
const float f = 1.0f / 255.0f;
return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
}
};
template <>
struct color_caster<Vec3f, Vec4uc>
{
typedef Vec3f return_type;
inline static return_type cast(const Vec4uc& _src)
{
const float f = 1.0f / 255.0f;
return Vec3f(_src[0] * f, _src[1] * f, _src[2] * f );
}
};
template <>
struct color_caster<Vec4f, Vec3uc>
{
typedef Vec4f return_type;
inline static return_type cast(const Vec3uc& _src)
{
const float f = 1.0f / 255.0f;
return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, 1.0f );
}
};
template <>
struct color_caster<Vec4f, Vec4uc>
{
typedef Vec4f return_type;
inline static return_type cast(const Vec4uc& _src)
{
const float f = 1.0f / 255.0f;
return Vec4f(_src[0] * f, _src[1] * f, _src[2] * f, _src[3] * f );
}
};
// ----------------------------------------------------------------------------
#ifndef DOXY_IGNORE_THIS
#if !defined(OM_CC_MSVC)
template <typename dst_t>
struct color_caster<dst_t,dst_t>
{
typedef const dst_t& return_type;
inline static return_type cast(const dst_t& _src)
{
return _src;
}
};
#endif
#endif
//-----------------------------------------------------------------------------
template <typename dst_t, typename src_t>
inline
typename color_caster<dst_t, src_t>::return_type
color_cast(const src_t& _src )
{
return color_caster<dst_t, src_t>::cast(_src);
}
#endif
//-----------------------------------------------------------------------------
//=============================================================================
} // namespace OpenMesh
//=============================================================================
#endif // OPENMESH_COLOR_CAST_HH defined
//=============================================================================
OpenMesh::Vec4uc
VectorT< unsigned char, 4 > Vec4uc
4-byte unsigned vector
Definition: Vector11T.hh:820
OpenMesh::Vec3f
VectorT< float, 3 > Vec3f
3-float vector
Definition: Vector11T.hh:811
OpenMesh::Vec3uc
VectorT< unsigned char, 3 > Vec3uc
3-byte unsigned vector
Definition: Vector11T.hh:801
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
OpenMesh::Vec4ui
VectorT< unsigned int, 4 > Vec4ui
4-int unsigned vector
Definition: Vector11T.hh:828
OpenMesh::Vec3i
VectorT< signed int, 3 > Vec3i
3-int signed vector
Definition: Vector11T.hh:807
OpenMesh::Vec3ui
VectorT< unsigned int, 3 > Vec3ui
3-int unsigned vector
Definition: Vector11T.hh:809
OpenMesh::vector_cast
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
Definition: vector_cast.hh:86
OpenMesh::Vec4f
VectorT< float, 4 > Vec4f
4-float vector
Definition: Vector11T.hh:830
OpenMesh::Vec4i
VectorT< signed int, 4 > Vec4i
4-int signed vector
Definition: Vector11T.hh:826
OpenMesh::vector_traits::size_
static const size_t size_
size/dimension of the vector
Definition: vector_traits.hh:102