Commit 67e753b6 authored by André Anjos's avatar André Anjos 💬
Browse files

Split casting function into complex and non-complex

parent 8d56a8f4
......@@ -8,7 +8,7 @@
* Copyright (C) Idiap Research Institute, Martigny, Switzerland
*/
#include <bob.core/cast.h>
#include "complex_cast.h"
/**
* @brief Specializations of the cast function for the std::complex type.
......
/**
* @date Wed Feb 9 12:26:11 2011 +0100
* @author Laurent El Shafey <Laurent.El-Shafey@idiap.ch>
*
* @brief This file defines functions which add std::complex support to the
* static_cast function.
*
* Copyright (C) Idiap Research Institute, Martigny, Switzerland
*/
#ifndef BOB_CORE_COMPLEX_CAST_H
#define BOB_CORE_COMPLEX_CAST_H
#include <complex>
#include <stdint.h>
#include <bob.core/cast.h>
namespace bob { namespace core {
// Complex to regular
# define COMPLEX_TO_REGULAR_DECL(COMP, REG) template<> \
REG cast<REG, COMP>( const COMP& in);
# define COMPLEX_TO_REGULAR_FULL_DECL(COMP) \
COMPLEX_TO_REGULAR_DECL(COMP, bool) \
COMPLEX_TO_REGULAR_DECL(COMP, int8_t) \
COMPLEX_TO_REGULAR_DECL(COMP, int16_t) \
COMPLEX_TO_REGULAR_DECL(COMP, int32_t) \
COMPLEX_TO_REGULAR_DECL(COMP, int64_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint8_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint16_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint32_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint64_t) \
COMPLEX_TO_REGULAR_DECL(COMP, float) \
COMPLEX_TO_REGULAR_DECL(COMP, double) \
COMPLEX_TO_REGULAR_DECL(COMP, long double)
COMPLEX_TO_REGULAR_FULL_DECL(std::complex<float>)
COMPLEX_TO_REGULAR_FULL_DECL(std::complex<double>)
COMPLEX_TO_REGULAR_FULL_DECL(std::complex<long double>)
// Complex to complex
# define COMPLEX_TO_COMPLEX_DECL(FROM, TO) template<> \
TO cast<TO, FROM>( const FROM& in);
# define COMPLEX_TO_COMPLEX_FULL_DECL(COMP) \
COMPLEX_TO_COMPLEX_DECL(COMP, std::complex<float>) \
COMPLEX_TO_COMPLEX_DECL(COMP, std::complex<double>) \
COMPLEX_TO_COMPLEX_DECL(COMP, std::complex<long double>)
COMPLEX_TO_COMPLEX_FULL_DECL(std::complex<float>)
COMPLEX_TO_COMPLEX_FULL_DECL(std::complex<double>)
COMPLEX_TO_COMPLEX_FULL_DECL(std::complex<long double>)
}}
#endif /* BOB_CORE_COMPLEX_CAST_H */
......@@ -2,8 +2,8 @@
* @date Wed Feb 9 12:26:11 2011 +0100
* @author Laurent El Shafey <Laurent.El-Shafey@idiap.ch>
*
* @brief This file defines functions which add std::complex support to the
* static_cast function.
* @brief This file defines easy-to-use Blitz++ cast functions for Bob
* applications.
*
* Copyright (C) Idiap Research Institute, Martigny, Switzerland
*/
......@@ -13,121 +13,59 @@
#include <bob.core/assert.h>
#include <blitz/array.h>
#include <stdint.h>
#include <complex>
namespace bob { namespace core {
/**
* @ingroup CORE
* @{
*/
/**
* @brief Functions which add std::complex support to the static_cast
* function. This is done by considering the real part only of any
* complex number.
*/
template<typename T, typename U>
T cast(const U& in) {
return static_cast<T>(in);
}
/**
* @brief Specializations of the cast function for the std::complex type.
*/
// Complex to regular
#define COMPLEX_TO_REGULAR_DECL(COMP, REG) template<> \
REG cast<REG, COMP>( const COMP& in);
#define COMPLEX_TO_REGULAR_FULL_DECL(COMP) \
COMPLEX_TO_REGULAR_DECL(COMP, bool) \
COMPLEX_TO_REGULAR_DECL(COMP, int8_t) \
COMPLEX_TO_REGULAR_DECL(COMP, int16_t) \
COMPLEX_TO_REGULAR_DECL(COMP, int32_t) \
COMPLEX_TO_REGULAR_DECL(COMP, int64_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint8_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint16_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint32_t) \
COMPLEX_TO_REGULAR_DECL(COMP, uint64_t) \
COMPLEX_TO_REGULAR_DECL(COMP, float) \
COMPLEX_TO_REGULAR_DECL(COMP, double) \
COMPLEX_TO_REGULAR_DECL(COMP, long double)
COMPLEX_TO_REGULAR_FULL_DECL(std::complex<float>)
COMPLEX_TO_REGULAR_FULL_DECL(std::complex<double>)
COMPLEX_TO_REGULAR_FULL_DECL(std::complex<long double>)
// Complex to complex
#define COMPLEX_TO_COMPLEX_DECL(FROM, TO) template<> \
TO cast<TO, FROM>( const FROM& in);
#define COMPLEX_TO_COMPLEX_FULL_DECL(COMP) \
COMPLEX_TO_COMPLEX_DECL(COMP, std::complex<float>) \
COMPLEX_TO_COMPLEX_DECL(COMP, std::complex<double>) \
COMPLEX_TO_COMPLEX_DECL(COMP, std::complex<long double>)
COMPLEX_TO_COMPLEX_FULL_DECL(std::complex<float>)
COMPLEX_TO_COMPLEX_FULL_DECL(std::complex<double>)
COMPLEX_TO_COMPLEX_FULL_DECL(std::complex<long double>)
/**
* @}
*/
namespace array {
/**
* @ingroup CORE_ARRAY
* @{
*/
template<typename T, typename U>
blitz::Array<T,1> cast(const blitz::Array<U,1>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,1> out(in.extent(0));
for( int i=0; i<in.extent(0); ++i)
out(i) = bob::core::cast<T>( in(i));
return out;
}
template<typename T, typename U>
blitz::Array<T,2> cast(const blitz::Array<U,2>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,2> out(in.extent(0),in.extent(1));
for( int i=0; i<in.extent(0); ++i)
for( int j=0; j<in.extent(1); ++j)
out(i,j) = bob::core::cast<T>( in(i,j) );
return out;
}
template<typename T, typename U>
blitz::Array<T,3> cast(const blitz::Array<U,3>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,3> out(in.extent(0),in.extent(1),in.extent(2));
for( int i=0; i<in.extent(0); ++i)
for( int j=0; j<in.extent(1); ++j)
for( int k=0; k<in.extent(2); ++k)
out(i,j,k) = bob::core::cast<T>( in(i,j,k) );
return out;
}
template<typename T, typename U>
blitz::Array<T,4> cast(const blitz::Array<U,4>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,4> out(in.extent(0),in.extent(1),in.extent(2),in.extent(3));
for( int i=0; i<in.extent(0); ++i)
for( int j=0; j<in.extent(1); ++j)
for( int k=0; k<in.extent(2); ++k)
for( int l=0; l<in.extent(3); ++l)
out(i,j,k,l) = bob::core::cast<T>( in(i,j,k,l) );
return out;
}
/**
* @}
*/
}}}
template<typename T, typename U> T cast(const U& in) {
return static_cast<T>(in);
}
namespace array {
template<typename T, typename U>
blitz::Array<T,1> cast(const blitz::Array<U,1>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,1> out(in.extent(0));
for( int i=0; i<in.extent(0); ++i)
out(i) = bob::core::cast<T>( in(i));
return out;
}
template<typename T, typename U>
blitz::Array<T,2> cast(const blitz::Array<U,2>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,2> out(in.extent(0),in.extent(1));
for( int i=0; i<in.extent(0); ++i)
for( int j=0; j<in.extent(1); ++j)
out(i,j) = bob::core::cast<T>( in(i,j) );
return out;
}
template<typename T, typename U>
blitz::Array<T,3> cast(const blitz::Array<U,3>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,3> out(in.extent(0),in.extent(1),in.extent(2));
for( int i=0; i<in.extent(0); ++i)
for( int j=0; j<in.extent(1); ++j)
for( int k=0; k<in.extent(2); ++k)
out(i,j,k) = bob::core::cast<T>( in(i,j,k) );
return out;
}
template<typename T, typename U>
blitz::Array<T,4> cast(const blitz::Array<U,4>& in) {
bob::core::array::assertZeroBase(in);
blitz::Array<T,4> out(in.extent(0),in.extent(1),in.extent(2),in.extent(3));
for( int i=0; i<in.extent(0); ++i)
for( int j=0; j<in.extent(1); ++j)
for( int k=0; k<in.extent(2); ++k)
for( int l=0; l<in.extent(3); ++l)
out(i,j,k,l) = bob::core::cast<T>( in(i,j,k,l) );
return out;
}
}
}}
#endif /* BOB_CORE_CAST_H */
......@@ -45,7 +45,7 @@ setup(
"bob/core/cpp/array.cpp",
"bob/core/cpp/array_type.cpp",
"bob/core/cpp/blitz_array.cpp",
"bob/core/cpp/cast.cpp",
"bob/core/cpp/complex_cast.cpp",
"bob/core/version.cpp",
],
version = version,
......
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