Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
skramm committed Jul 12, 2021
1 parent 90ae6bf commit 838636b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 25 deletions.
3 changes: 2 additions & 1 deletion docs/homog2d_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ See [Release page](https://github.com/skramm/homog2d/releases).
- add conic homography (circle=>ellipse)

- current master branch
- added `Polyline::isPolygon()`, `Polyline::length()`

- [v2.4](https://github.com/skramm/homog2d/releases/tag/v2.4), released on 2021-07-12
- **Major release, some API changes**
Expand All @@ -24,6 +23,8 @@ See [Release page](https://github.com/skramm/homog2d/releases).
- solved numerical rounding issues
- renamed `Segment::get()` to `Segment::getPts()` and `FRect::get2Pts()` to `FRect::getPts()`
- enhanced `Polyline` class (intersection code, ...)
- added `Polyline::isPolygon()`, `Polyline::length()` and corresponding free functions
- added `getBB()` (member function and free functions) to `Polyline`, `Segment` and `Circle`

- [v2.3](https://github.com/skramm/homog2d/releases/tag/v2.3), released on 2021-06-18
- switch to MPLv2 licence
Expand Down
3 changes: 2 additions & 1 deletion docs/homog2d_manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ Additional features: length and bounding box:
Polygon pl;
// ... add points
std::cout << "length=" << pl.length() << '\n';
FRect rect = pl.getBB();
FRect rect = pl.getBB();
FRect rect2 = getBB(pl);
```


Expand Down
96 changes: 80 additions & 16 deletions homog2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,17 @@ class Circle_
#endif // HOMOG2D_USE_OPENCV
};


//------------------------------------------------------------------
/// Returns Bounding Box (free function)
/// \sa Circle_::getBB()
template<typename FPT>
FRect_<FPT> getBB( const Circle_<FPT>& cir )
{
return cir.getBB();
}

//------------------------------------------------------------------
/// Holds private stuff
namespace priv {

Expand Down Expand Up @@ -2317,6 +2328,7 @@ class Segment_
{
return _ptS1.distTo( _ptS2 );
}

/// Returns Bounding Box
FRect_<FPT> getBB() const
{
Expand Down Expand Up @@ -2424,9 +2436,17 @@ the one with smallest y-coordinate will be returned first */
#endif
};


//------------------------------------------------------------------
/// Returns Bounding Box (free function)
/// \sa Segment_::getBB()
template<typename FPT>
FRect_<FPT> getBB( const Segment_<FPT>& seg )
{
return seg.getBB();
}

/// Get segment length (free function)
/// \sa Segment_::length()
template<typename FPT>
HOMOG2D_INUMTYPE length( const Segment_<FPT>& seg )
{
Expand Down Expand Up @@ -2742,8 +2762,8 @@ Segment \c n is the one between point \c n and point \c n+1
{
_plinevec.resize( vec.size() );
auto it = std::begin( _plinevec );
for( const auto& pt: vec )
*it++ = pt;
for( const auto& pt: vec ) // copying one by one will
*it++ = pt; // allow type conversions (std::copy implies same type)
}

/// Add vector of points
Expand All @@ -2752,10 +2772,11 @@ Segment \c n is the one between point \c n and point \c n+1
{
if( vec.size() == 0 )
return;
auto it = std::end( _plinevec );
_plinevec.resize( _plinevec.size() + vec.size() );
// auto it = std::end( _plinevec );
std::cout << "resizing to " << _plinevec.size() << "+" << vec.size() << '\n';
_plinevec.reserve( _plinevec.size() + vec.size() );
for( const auto& pt: vec ) // we cannot use std::copy because vec might not hold points of same type
*it++ = pt;
_plinevec.push_back( pt );
}

const bool& isClosed() const { return _isClosed; }
Expand Down Expand Up @@ -2831,17 +2852,69 @@ template<typename FPT>
bool
Polyline_<FPT>::isPolygon() const
{
if( !_isClosed ) // cant be a polygon if
return false; // it's not closed !

auto nbs = nbSegs();
for( size_t i=0; i<nbs; i++ )
{
auto seg1 = getSegment(i);
for( auto j=i+2; j<nbs; j++ )
for( auto j=i+2; j<nbs-1; j++ )
if( getSegment(j).intersects(seg1)() )
return false;
}
return true;
}

/// Returns true if is a polygon (free function)
///  \sa Polyline_::isPolygon()
template<typename FPT>
bool
isPolygon( const Polyline_<FPT>& pl )
{
return pl.isPolygon();
}

/// Returns the number of segments (free function)
template<typename FPT>
size_t nbSegs( const Polyline_<FPT>& pl )
{
return pl.nbSegs();
}

/// Returns the number of points (free function)
template<typename FPT>
size_t size( const Polyline_<FPT>& pl )
{
return pl.size();
}

/// Returns Bounding Box (free function)
/// \sa FRect_::getBB()
template<typename FPT>
FRect_<FPT>
getBB( const Polyline_<FPT>& pl )
{
return pl.getBB();
}

/// Returns length (free function)
/// \sa Polyline_::length()
template<typename FPT>
HOMOG2D_INUMTYPE
length( const Polyline_<FPT>& pl )
{
return pl.length();
}

/// Returns the segments of the polyline (free function)
/// \sa Polyline_::getSegs()
template<typename FPT>
std::vector<Segment_<FPT>>
getSegs( const Polyline_<FPT>& pl )
{
return pl.getSegs();
}

//------------------------------------------------------------------
/// Returns Bounding Box
Expand Down Expand Up @@ -2884,15 +2957,6 @@ Polyline_<FPT>::getBB() const
);
}

//------------------------------------------------------------------
/// Returns the segments of the polyline (free function)
/// \sa Polyline_::getSegs()
template<typename FPT>
std::vector<Segment_<FPT>>
getSegs( const Polyline_<FPT>& pl )
{
return pl.getSegs();
}

//------------------------------------------------------------------
namespace detail {
Expand Down
16 changes: 13 additions & 3 deletions misc/demo_opencv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,18 @@ checkSelected( int event, int x, int y, std::function<void(void*)> action, void*
data.vpt[data.selected] = data.pt_mouse;
data.vpt[data.selected].draw( data.img, CvDrawParams().selectPoint() );
}
// action_M( param );
break;

case CV_EVENT_RBUTTONDOWN:
data.selected = -1;
for( int i=0; i<data.nbPts(); i++ )
if( data.pt_mouse.distTo( data.vpt[i]) < 10 ) // if mouse is less than 10 pixel away
data.selected = i;
if( data.selected != -1 )
{
data.vpt.erase( std::begin(data.vpt) + data.selected );
data.selected = -1;
}
break;

default: doSomething = false; break;
Expand All @@ -153,7 +164,6 @@ checkSelected( int event, int x, int y, std::function<void(void*)> action, void*
{
data.clearImage();
action( param );
// action_M( param );
data.showImage();
}
}
Expand Down Expand Up @@ -653,7 +663,7 @@ void action_6( void* param )
double K = M_PI / 180.;

Homogr H( data.angle * K );
H.addTranslation(-50,0).inverse().transpose();
H.addTranslation(-50,0);
Line2d l1( data.vpt[0], data.vpt[1] );
Line2d l2 = H*l1;

Expand Down
36 changes: 32 additions & 4 deletions misc/homog2d_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1436,10 +1436,38 @@ TEST_CASE( "FRect", "[frect]" )

TEST_CASE( "Polyline", "[polyline]" )
{
Polyline pl1;
{
// pl1.add{ std::vector<Point2d>{ {0,0}, {1,2}, {3,5}, {5,1} };
}
Polyline_<NUMTYPE> pl1;
pl1.add(
std::vector<Point2d>{ {0,0}, {1,1.5}, {3,5}, {1,4} }
);
CHECK( pl1.size() == 4 );
CHECK( pl1.nbSegs() == 3 );
CHECK( pl1.isPolygon() == false );
CHECK( isPolygon(pl1) == false );
pl1.isClosed() = true;
CHECK( pl1.nbSegs() == 4 );
CHECK( pl1.isPolygon() == true );
CHECK( isPolygon(pl1) == true );
FRect bb1( 0,0, 3,5);
CHECK( getBB(pl1) == bb1 );
CHECK( pl1.getBB() == bb1 );

pl1.set(
std::vector<Point2d>{ {0,0}, {0,1}, {1,1}, {1,0} }
);
CHECK( pl1.size() == 4 );
CHECK( pl1.nbSegs() == 4 );
CHECK( pl1.length() == 4.);
CHECK( length(pl1) == 4.);
pl1.isClosed() = false;
CHECK( pl1.size() == 4 );
CHECK( pl1.nbSegs() == 3 );
CHECK( pl1.length() == 3.);
CHECK( length(pl1) == 3.);

FRect bb2( 0,0, 1,1);
CHECK( getBB(pl1) == bb2 );
CHECK( pl1.getBB() == bb2 );
}

//////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 838636b

Please sign in to comment.