Skip to content

Commit

Permalink
build of RCP
Browse files Browse the repository at this point in the history
  • Loading branch information
skramm committed Dec 12, 2023
1 parent d682685 commit 958646e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
37 changes: 27 additions & 10 deletions homog2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5415,11 +5415,11 @@ template<
set( vec );
}

/// Constructor that build a regular convex polygon of \c n points
explicit
PolylineBase( size_t n )
/// Constructor that build a regular convex polygon of \c n points at a distance \c rad, centered at (0,0).
template<typename FPT2>
PolylineBase( FPT2 rad, size_t n )
{
impl_constr_RCP( n, detail::PlHelper<PLT>() );
impl_constr_RCP( rad, n, detail::PlHelper<PLT>() );
}


Expand Down Expand Up @@ -5494,11 +5494,13 @@ this should work !!! (but doesn't...)
///@}

private:
template<typename FPT2>
void
impl_constr_RCP( size_t n, const detail::PlHelper<type::IsClosed>& );
impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper<type::IsClosed>& );

template<typename FPT2>
constexpr void
impl_constr_RCP( size_t n, const detail::PlHelper<type::IsOpen>& )
impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper<type::IsOpen>& )
{
static_assert( detail::AlwaysFalse<PLT>::value, "cannot build an regular convex polygon for a OPolyline object");
}
Expand Down Expand Up @@ -6058,15 +6060,30 @@ Two tasks:

}; // class PolylineBase


/// Build a Regular Convex Polygon of radius \c rad with \c n points, centered at (0,0)
template<typename PLT,typename FPT>
template<typename FPT2>
void
PolylineBase<PLT,FPT>::impl_constr_RCP( size_t n, const detail::PlHelper<type::IsClosed>& )
PolylineBase<PLT,FPT>::impl_constr_RCP( FPT2 rad, size_t n, const detail::PlHelper<type::IsClosed>& )
{

if( n < 3 )
HOMOG2D_THROW_ERROR_1( "unable, nb of points must be > 2" );
if( rad <= 0 )
HOMOG2D_THROW_ERROR_1( "unable, radius must be >= 0" );
std::vector<Point2d_<HOMOG2D_INUMTYPE>> v_pts(n);
auto it = std::begin( v_pts );
for( size_t i=0; i<n; i++ )
{
HOMOG2D_INUMTYPE angle = (HOMOG2D_INUMTYPE)2. * M_PI * i / n;
std::cout << i << ": angle=" << angle * 180 / M_PI << '\n';
auto x = std::cos( angle );
auto y = std::sin( angle );
*it = Point2d_<HOMOG2D_INUMTYPE>( x * rad, y * rad );
it++;
}
*this = PolylineBase<PLT,FPT>( v_pts );
}


} // namespace base


Expand Down
37 changes: 37 additions & 0 deletions misc/demo_opencv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,42 @@ void demo_orthSeg( int demidx )
action_ORS( &data );
data.setMouseCB( action_ORS );

kbloop.start( data );
}

//------------------------------------------------------------------
struct Param_RCP : Data
{
explicit Param_RCP( int demidx, std::string title ): Data( demidx, title )
{

}
size_t nbPts = 5;
};

void action_RCP( void* param )
{
auto& data = *reinterpret_cast<Param_RCP*>(param);
data.clearImage();
CPolyline pol(100, data.nbPts );
pol.translate(150,150);
pol.draw( data.img );
data.showImage();
}

void demo_RCP( int demidx )
{
Param_RCP data( demidx, "Regular Convex Polygon" );
std::cout << "Demo " << demidx << ": Regular Convex Polygon\n";

KeyboardLoop kbloop;
kbloop.addKeyAction( 'a', [&](void*){ data.nbPts++; }, "more points" );
kbloop.addKeyAction( 'w', [&](void*){ data.nbPts--; }, "less points" );

kbloop.addCommonAction( action_RCP );
action_RCP( &data );
// data.setMouseCB( action_ORS );

kbloop.start( data );

}
Expand All @@ -1679,6 +1715,7 @@ int main( int argc, const char** argv )
std::cout << "double: size=" << pt3.dsize().first << "-" << pt3.dsize().second << '\n';

std::vector<std::function<void(int)>> v_demo{
demo_RCP,
demo_orthSeg, // Perpendicular segment
demo_NFP, // Nearest/Farthest Point
demo_RI, // rectangle intersection
Expand Down

0 comments on commit 958646e

Please sign in to comment.