Skip to content

Commit

Permalink
add bindings for pixel types, additional tests #15
Browse files Browse the repository at this point in the history
  • Loading branch information
timdewhirst committed Oct 5, 2022
1 parent 57ebc64 commit ca05a7b
Show file tree
Hide file tree
Showing 6 changed files with 551 additions and 22 deletions.
55 changes: 35 additions & 20 deletions openpiv/core/pixel_types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

#pragma once

// local
#include "core/util.h"

// std
#include <cinttypes>
#include <cmath>
Expand Down Expand Up @@ -31,7 +34,8 @@ struct rgba
rgba& operator=(rgba&&) = default;
rgba& operator=( T v )
{
std::swap( *this, rgba(v) );
rgba tmp{ v };
std::swap( *this, tmp );
return *this;
}

Expand All @@ -57,7 +61,7 @@ using rgba_32 = rgba<uint32_t>;
template < typename T >
std::ostream& operator<<(std::ostream& os, const rgba<T>& rgba )
{
os << "rgba(" << rgba.r << ", " << rgba.g << ", " << rgba.b << ", " << rgba.a << ")";
os << "rgba(" << +rgba.r << ", " << +rgba.g << ", " << +rgba.b << ", " << +rgba.a << ")";
return os;
}

Expand All @@ -77,7 +81,8 @@ struct yuva
yuva& operator=(yuva&&) = default;
yuva& operator=( T v )
{
std::swap( *this, yuva(v) );
yuva tmp{ v };
std::swap( *this, tmp );
return *this;
}

Expand All @@ -103,7 +108,7 @@ using yuva_32 = yuva<uint32_t>;
template < typename T >
std::ostream& operator<<(std::ostream& os, const yuva<T>& v )
{
os << "yuva(" << v.y << ", " << v.u << ", " << v.v << ", " << v.a << ")";
os << "yuva(" << +v.y << ", " << +v.u << ", " << +v.v << ", " << +v.a << ")";
return os;
}

Expand All @@ -118,16 +123,25 @@ class complex
complex( const complex& ) = default;
complex( complex&& ) = default;

constexpr complex( value_t r ) : real(r) {}
constexpr complex( value_t r, value_t i ) : real(r), imag(i){}
template < typename U,
typename E = typename std::enable_if_t< std::is_convertible_v<U, T> > >
constexpr complex( U r ) : real(r) {}
template < typename U,
typename E = typename std::enable_if< std::is_convertible<U, T>::value >::type >
constexpr complex( U r, U i = U{} ) : real(r), imag(i){}
typename E = typename std::enable_if_t< std::is_convertible_v<U, T> > >
constexpr complex( U r, U i ) : real(r), imag(i) {}
constexpr complex( const std::complex<T>& c ) : real(c.real()), imag(c.imag()) {}

complex& operator=( const complex& c ) = default;
complex& operator=( complex&& c ) = default;
template < typename U,
typename E = typename std::enable_if< std::is_convertible<U, T>::value >::type >
complex& operator=( const complex<U>& c ) { real = c.real; imag = c.imag; return *this; }
typename E = typename std::enable_if_t< std::is_convertible_v<U, T> > >
complex& operator=( U r ) { real = r; imag = T{}; return *this; }
template < typename U,
typename E = typename std::enable_if_t< std::is_convertible_v<U, T> > >
complex& operator=( const complex<U>
& c ) { real = c.real; imag = c.imag; return *this; }

bool operator==( const complex& rhs ) const
{
Expand Down Expand Up @@ -165,7 +179,7 @@ class complex
inline constexpr complex operator/( const complex& rhs ) const
{
const T &a{ real }, &b{ imag }, &c{ rhs.real }, &d{ rhs.imag };
const T denom{ c*c + d*d };
const T denom{ static_cast<T>(c*c + d*d) };
return { (a*c + b*d)/denom, (b*c - a*d)/denom };
}

Expand Down Expand Up @@ -193,9 +207,9 @@ class complex
return *this;
}

inline constexpr complex conj() const { return { real, -imag }; }
inline constexpr complex conj() const { return { real, static_cast<T>(-imag) }; }
inline constexpr T abs() const { return std::sqrt( abs_sqr() ); }
inline constexpr T abs_sqr() const { return (*this * conj()).real; }
inline constexpr T abs_sqr() const { return real*real + imag*imag; }

T real{};
T imag{};
Expand Down Expand Up @@ -228,20 +242,21 @@ inline constexpr complex<T> operator/( const T& lhs, const complex<T>& rhs )
template <typename T>
inline constexpr complex<T> exp( const complex<T>& c )
{
const T e{ exp( c.real ) };

const T e{ static_cast<T>(std::exp( c.real )) };
return complex<T>{ e * std::cos( c.imag ), e * std::sin( c.imag ) };
}

using c_8 = complex<uint8_t>;
using c_16 = complex<uint16_t>;
using c_32 = complex<uint32_t>;
using c_8 = complex<int8_t>;
using c_16 = complex<int16_t>;
using c_32 = complex<int32_t>;
using c_f = complex<double>;

template < typename T >
std::ostream& operator<<(std::ostream& os, const complex<T>& v )
{
static const char* sign[] = { " ", " +" };
os << v.real << sign[ v.imag< 0 ? 0 : 1 ] << v.imag << "j";
os << "complex(" << v.real << sign[ v.imag< 0 ? 0 : 1 ] << v.imag << "j)";
return os;
}

Expand All @@ -259,13 +274,13 @@ struct g
g(const g&) = default;
g(g&&) = default;
template < typename U,
typename = typename std::enable_if< std::is_convertible< U, T >::value >::type >
typename = typename std::enable_if_t< std::is_convertible_v< U, T > > >
g( U v_ ) : v(v_) {}

g& operator=(const g&) = default;
g& operator=(g&&) = default;
template < typename U,
typename = typename std::enable_if< std::is_convertible< U, T >::value >::type >
typename = typename std::enable_if_t< std::is_convertible_v< U, T > > >
inline g& operator=(U v_) { v = v_; return *this; }

inline operator T() const { return v; }
Expand All @@ -288,7 +303,7 @@ inline g_f operator ""_gf ( long double v ) { return g_f( v ); }
template < typename T >
std::ostream& operator<<(std::ostream& os, const g<T>& v )
{
os << "g" << v.v;
os << "g(" << v.v << ")";
return os;
}

Expand Down Expand Up @@ -462,7 +477,7 @@ constexpr std::string_view pixeltype_name()
if constexpr (std::is_same_v<T, yuva_32>)
return "yuva<uint32_t>";

return {};
return "unknown pixeltype";
}


Expand Down
1 change: 1 addition & 0 deletions pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (BUILD_PYBIND)
core/rect.cpp
core/size.cpp
core/vector.cpp
core/pixel_types.cpp
)

find_package(fmt CONFIG REQUIRED)
Expand Down
2 changes: 2 additions & 0 deletions pybind/core/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void add_point(py::module &);
void add_rect(py::module &);
void add_size(py::module &);
void add_vector(py::module &);
void add_pixel_types(py::module &);

PYBIND11_MODULE(pyopenpivcore, m) {
m.doc() = R"pbdoc(
Expand All @@ -31,6 +32,7 @@ PYBIND11_MODULE(pyopenpivcore, m) {
add_rect(m);
add_size(m);
add_vector(m);
add_pixel_types(m);

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
Expand Down
Loading

0 comments on commit ca05a7b

Please sign in to comment.