Commit 12b7069c authored by Matthias Möller's avatar Matthias Möller

- fix warning with vector_cast

- add compile error, if vector_cast cannot be performed
- adds some color cast specializations
- add simple vector_cast unittest

closes #2409

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@1249 fdac6126-5c0c-442c-9429-916003d36597
parent 823b0486
......@@ -82,7 +82,7 @@ struct color_caster
inline static return_type cast(const src_t& _src)
{
dst_t dst;
vector_copy(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
vector_cast(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
return dst;
}
};
......@@ -222,6 +222,32 @@ struct color_caster<Vec4f,Vec3f>
}
};
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>
{
......
......@@ -77,57 +77,33 @@ namespace OpenMesh {
//-----------------------------------------------------------------------------
template <typename src_t, typename dst_t>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<1> )
template <typename src_t, typename dst_t, int n>
inline void vector_cast( const src_t &_src, dst_t &_dst, GenProg::Int2Type<n> )
{
_dst[0] = _src[0];
GenProg::AssertCompile< (vector_traits<dst_t>::size_ <= vector_traits<src_t>::size_) > vectorCastingToHigherDimension;
vector_cast(_src,_dst, GenProg::Int2Type<n-1>());
_dst[n-1] = static_cast<typename vector_traits<dst_t>::value_type >(_src[n-1]);
}
template <typename src_t, typename dst_t>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<2> )
inline void vector_cast( const src_t &_src, dst_t &_dst, GenProg::Int2Type<0> )
{
_dst[0] = _src[0];
_dst[1] = _src[1];
}
template <typename src_t, typename dst_t>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<3> )
{
_dst[0] = _src[0];
_dst[1] = _src[1];
_dst[2] = _src[2];
}
template <typename src_t, typename dst_t>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<4> )
template <typename src_t, typename dst_t, int n>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<n> )
{
_dst[0] = _src[0];
_dst[1] = _src[1];
_dst[2] = _src[2];
_dst[3] = _src[3];
GenProg::AssertCompile< (vector_traits<dst_t>::size_ <= vector_traits<src_t>::size_) > vectorCopyToHigherDimension;
vector_copy(_src,_dst, GenProg::Int2Type<n-1>());
_dst[n-1] = _src[n-1];
}
template <typename src_t, typename dst_t>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<5> )
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<0> )
{
_dst[0] = _src[0];
_dst[1] = _src[1];
_dst[2] = _src[2];
_dst[3] = _src[3];
_dst[4] = _src[4];
}
template <typename src_t, typename dst_t>
inline void vector_copy( const src_t &_src, dst_t &_dst, GenProg::Int2Type<6> )
{
_dst[0] = _src[0];
_dst[1] = _src[1];
_dst[2] = _src[2];
_dst[3] = _src[3];
_dst[4] = _src[4];
_dst[5] = _src[5];
}
//-----------------------------------------------------------------------------
......@@ -141,7 +117,7 @@ struct vector_caster
inline static return_type cast(const src_t& _src)
{
dst_t dst;
vector_copy(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
vector_cast(_src, dst, GenProg::Int2Type<vector_traits<dst_t>::size_>());
return dst;
}
};
......
......@@ -65,6 +65,24 @@ TEST_F(OpenMeshVectorTest, AbsTest) {
}
/* Compute surface area via cross product
*/
TEST_F(OpenMeshVectorTest, VectorCasting) {
OpenMesh::Vec3d vecd(1.0,2.0,3.0);
OpenMesh::Vec3f vecf = OpenMesh::vector_cast<OpenMesh::Vec3f>(vecd);
EXPECT_EQ(1.f, vecf[0]) << "vector type cast failed on component 0";
EXPECT_EQ(2.f, vecf[1]) << "vector type cast failed on component 1";
EXPECT_EQ(3.f, vecf[2]) << "vector type cast failed on component 2";
OpenMesh::Vec4d vecd4(40.0,30.0,20.0,10.0);
vecd = OpenMesh::vector_cast<OpenMesh::Vec3d>(vecd4);
EXPECT_EQ(40.0, vecd[0]) << "vector dimension cast failed on component 0";
EXPECT_EQ(30.0, vecd[1]) << "vector dimension cast failed on component 1";
EXPECT_EQ(20.0, vecd[2]) << "vector dimension cast failed on component 2";
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment