Skip to content

Commit

Permalink
added size functor and associated free function
Browse files Browse the repository at this point in the history
  • Loading branch information
skramm committed Feb 1, 2025
1 parent 89da83c commit eeaf5bb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
50 changes: 32 additions & 18 deletions homog2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,21 @@ struct AreaFunct
}
};

//------------------------------------------------------------------
/// A functor to get the size (nb of points) of an object in a std::variant, call with std::visit()
/**
\sa CommonType_
\sa size()
*/
struct SizeFunct
{
template<typename T>
HOMOG2D_INUMTYPE operator ()(const T& a)
{
return a.size();
}
};


//------------------------------------------------------------------
/// A functor used to apply a homography matrix to an object
Expand Down Expand Up @@ -1053,6 +1068,7 @@ VariantUnwrapper( const std::variant<Ts...>& ) -> VariantUnwrapper<Ts...>;
} // namespace fct
#endif // HOMOG2D_ENABLE_VRTP


inline
const char* getString( Dtype t )
{
Expand Down Expand Up @@ -9636,16 +9652,6 @@ getSegs( const base::PolylineBase<PLT,FPT>& pl )
return pl.getSegs();
}


/// Returns the number of points (free function)
/// \sa PolylineBase::size()
template<typename PLT,typename FPT>
size_t
size( const base::PolylineBase<PLT,FPT>& pl )
{
return pl.size();
}

/// Rotates the primitive (only available for Polyline and FRect) around (0,0)
template<typename T>
void
Expand All @@ -9662,14 +9668,6 @@ rotate( T& prim, Rotate rot, const Point2d_<FPT>& refpt )
prim.rotate( rot, refpt );
}

/// Returns size (nb of points) of object
template<typename T>
size_t
size( const T& e )
{
return e.size();
}

/// Returns Bounding Box of Ellipse_ (free function)
/// \sa Ellipse_::getBB()
template<typename FPT>
Expand Down Expand Up @@ -9888,6 +9886,21 @@ area( const T& elem )
return elem.area();
}

//------------------------------------------------------------------
/// Returns size of element or variant (free function)
template<typename T>
HOMOG2D_INUMTYPE
size( const T& elem )
{
#ifdef HOMOG2D_ENABLE_VRTP
if constexpr( trait::IsVariant<T>::value )
return std::visit( fct::SizeFunct{}, elem );
else
#endif
return elem.size();
}


#ifdef HOMOG2D_ENABLE_VRTP
namespace priv {

Expand Down Expand Up @@ -10007,6 +10020,7 @@ transform( const Homogr_<FPT>& h, const T& elem )
return h * elem;
}
#endif

/////////////////////////////////////////////////////////////////////////////
// SECTION - FREE FUNCTIONS
/////////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 6 additions & 3 deletions misc/homog2d_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3747,7 +3747,7 @@ TEST_CASE( "general binding", "[gen_bind]" )
/// Helper function for local_draw_test()
template<typename I,typename T>
void
local_draw_test2( img::Image<I>& im, const T& t )
local_draw_test2( img::Image<I>& im, /*const*/ T& t )
{
t.draw( im );
im.draw( t );
Expand All @@ -3759,7 +3759,9 @@ local_draw_test2( img::Image<I>& im, const T& t )
draw( im, t, dp );

std::vector<T> v;
t.translate( 50,20 );
v.push_back(t);
t.translate( 50,20 );
v.push_back(t);
draw( im, v );
draw( im, v, dp );
Expand All @@ -3786,7 +3788,8 @@ void
local_draw_test( img::Image<I>& im, std::string fn )
{
FRect r; local_draw_test2( im, r );
Segment s; local_draw_test2( im, s );
Segment s(50,50,100,100);
local_draw_test2( im, s );
Circle c; local_draw_test2( im, c );
Line2d li; local_draw_test2( im, li );
Point2d pt; local_draw_test2( im, pt );
Expand All @@ -3806,7 +3809,7 @@ TEST_CASE( "drawing (SVG)", "[draw_svg]" )
#ifdef HOMOG2D_USE_OPENCV
TEST_CASE( "drawing (OpenCV)", "[draw_ocv]" )
{
img::Image<cv::Mat> im;
img::Image<cv::Mat> im(300,300);
local_draw_test( im, "BUILD/dummy_draw.png" );
}
#endif // HOMOG2D_USE_OPENCV
Expand Down
3 changes: 2 additions & 1 deletion misc/test_files/rtp_p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ do_prtp( const std::vector<h2d::Point2d>& vecpts, IM& im )
for( auto& e: vec ) // demonstration of polymorphic member functions
{
std::cout << getString(e->type()) << ": " << *e
<< "\n -area = " << e->area();
<< "\n -area = " << e->area()
<< "\n -size = " << e->size();
if( e->type() != Type::Line2d )
std::cout << "\n -length = " << e->length();
else
Expand Down
3 changes: 2 additions & 1 deletion misc/test_files/rtp_v.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ do_vrtp( const std::vector<h2d::Point2d>& vecpts, IM& im )
for( auto& e: vec )
{
std::cout << getString(type(e))
<< "\n -area=" << area(e);
<< "\n -area=" << area(e)
<< "\n -size=" << size(e);
if( type(e) != Type::Line2d )
std::cout << "\n -length=" << length(e);
else
Expand Down

0 comments on commit eeaf5bb

Please sign in to comment.