Skip to content

Commit

Permalink
Initial range class (#418)
Browse files Browse the repository at this point in the history
* added area classes
first Area2d tests

* removed unneeded method

* area3d tests

* build fix

* remaining tests

* fixed typo

* zip coverage report before uploading

* renamed area to range. use std::minmax
  • Loading branch information
turleypol committed Sep 4, 2021
1 parent 0df9b8c commit 5f79897
Show file tree
Hide file tree
Showing 13 changed files with 528 additions and 68 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,17 @@ jobs:
cd bin-build
fastcov -g gcov-${{ matrix.config.compiler-version }} -t PolCore --lcov -i pol-core -o pol-core.info
genhtml --demangle-cpp pol-core.info -o coverage
cmake -E tar c coverage.zip --format=zip coverage
- name: Upload shard test log
uses: actions/upload-artifact@v1
with:
name: Testlog-${{ runner.os }}-${{ matrix.config.cxx-compiler }}
path: bin-build/coretest/log/pol.log
- name: Uploap coverage report
- name: Upload coverage report
uses: actions/upload-artifact@v1
with:
name: Coveragelog
path: bin-build/coverage
path: bin-build/coverage.zip
- name: Coveralls
uses: coverallsapp/github-action@master
with:
Expand Down
3 changes: 3 additions & 0 deletions pol-core/pol/CMakeSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set (pol_sources # sorted !
baseobject.h
base/position.cpp
base/position.h
base/range.cpp
base/range.h
base/vector.cpp
base/vector.h
binaryfilescrobj.cpp
Expand Down Expand Up @@ -387,6 +389,7 @@ set (pol_sources # sorted !
testing/testlos.cpp
testing/testmisc.cpp
testing/testpos.cpp
testing/testrange.cpp
testing/testskill.cpp
testing/testvector.cpp
testing/testwalk.cpp
Expand Down
27 changes: 4 additions & 23 deletions pol-core/pol/base/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,13 @@ UFACING Pos2d::direction_away( const Pos2d& dst ) const
return away_cvt[static_cast<int>( toward )];
}

void Pos2d::update_min( const Pos2d& v )
Pos2d Pos2d::min( const Pos2d& v ) const
{
if ( v._x < _x )
_x = v._x;
if ( v._y < _y )
_y = v._y;
return Pos2d( std::min( _x, v._x ), std::min( _y, v._y ) );
}
void Pos2d::update_max( const Pos2d& v )
Pos2d Pos2d::max( const Pos2d& v ) const
{
if ( v._x > _x )
_x = v._x;
if ( v._y > _y )
_y = v._y;
return Pos2d( std::max( _x, v._x ), std::max( _y, v._y ) );
}

fmt::Writer& operator<<( fmt::Writer& w, const Pos2d& v )
Expand Down Expand Up @@ -331,19 +325,6 @@ Pos3d& Pos3d::crop( const Realms::Realm* realm )
return *this;
}

void Pos3d::update_min( const Pos3d& v )
{
_xy.update_min( v.xy() );
if ( v._z < _z )
_z = v._z;
}
void Pos3d::update_max( const Pos3d& v )
{
_xy.update_max( v.xy() );
if ( v._z > _z )
_z = v._z;
}

fmt::Writer& operator<<( fmt::Writer& w, const Pos3d& v )
{
w << "( " << v.x() << ", " << v.y() << ", " << v.z() << " )";
Expand Down
7 changes: 2 additions & 5 deletions pol-core/pol/base/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class Pos2d
UFACING direction_toward( const Pos2d& other ) const;
UFACING direction_away( const Pos2d& other ) const;

void update_min( const Pos2d& v );
void update_max( const Pos2d& v );
Pos2d min( const Pos2d& v ) const;
Pos2d max( const Pos2d& v ) const;
};
Pos2d operator-( Pos2d lhs, const Vec2d& rhs );
Pos2d operator+( Pos2d lhs, const Vec2d& rhs );
Expand Down Expand Up @@ -132,9 +132,6 @@ class Pos3d
bool in_range( const Pos2d& other, u16 range ) const;
bool in_range( const Pos3d& other, u16 range ) const;
Pos3d& crop( const Realms::Realm* realm );

void update_min( const Pos3d& v );
void update_max( const Pos3d& v );
};
Pos3d operator-( Pos3d lhs, const Vec2d& rhs );
Pos3d operator+( Pos3d lhs, const Vec2d& rhs );
Expand Down
128 changes: 128 additions & 0 deletions pol-core/pol/base/range.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include "range.h"
#include "realms/realm.h"

#include <algorithm>
#include <tuple>

namespace Pol
{
namespace Core
{
Range2dItr::Range2dItr( Pos2d v, const Pos2d& v_max )
: _v( std::move( v ) ), _xbound( v_max.x() ), _xstart( _v.x() ){};

bool Range2dItr::operator==( const Range2dItr& other ) const
{
return _v == other._v;
}
bool Range2dItr::operator!=( const Range2dItr& other ) const
{
return !( *this == other );
}
Range2dItr& Range2dItr::operator++()
{
if ( _v.x() < _xbound )
_v += Vec2d( 1, 0 );
else
{
_v.x( _xstart );
_v += Vec2d( 0, 1 );
}
return *this;
}

Range2d::Range2d( const Pos2d& p1, const Pos2d& p2, const Realms::Realm* realm )
{
_nw = p1.min( p2 );
_se = p1.max( p2 );
if ( realm != nullptr )
{
_nw.crop( realm );
_se.crop( realm );
}
}
Range2d::Range2d( const Pos4d& p1, const Pos4d& p2 )
{
_nw = p1.xy().min( p2.xy() );
_se = p1.xy().max( p2.xy() );
}

Range2dItr Range2d::begin() const
{
return Range2dItr( _nw, _se );
}
Range2dItr Range2d::end() const
{
return Range2dItr( Pos2d( _nw.x(), _se.y() ) + Vec2d( 0, 1 ), _se );
}

bool Range2d::contains( const Pos2d& other ) const
{
return _nw <= other && _se >= other;
}

bool Range2d::intersect( const Range2d& other ) const
{
return _nw.x() <= other._se.x() && other._nw.x() <= _se.x() && _nw.y() <= other._se.y() &&
other._nw.y() <= _se.y();
}

bool Range2d::operator==( const Range2d& other ) const
{
return _nw == other._nw && _se == other._se;
}
bool Range2d::operator!=( const Range2d& other ) const
{
return !( *this == other );
}

fmt::Writer& operator<<( fmt::Writer& w, const Range2d& v )
{
w << "( " << v.nw() << " - " << v.se() << " )";
return w;
}

Range3d::Range3d( const Pos3d& p1, const Pos3d& p2, const Realms::Realm* realm )
: _range( p1.xy(), p2.xy(), realm )
{
std::tie( _z_bottom, _z_top ) = std::minmax( p1.z(), p2.z() );
}
Range3d::Range3d( const Pos4d& p1, const Pos4d& p2 ) : _range( p1, p2 )
{
std::tie( _z_bottom, _z_top ) = std::minmax( p1.z(), p2.z() );
}

bool Range3d::contains( const Pos2d& other ) const
{
return _range.contains( other );
}
bool Range3d::contains( const Pos3d& other ) const
{
return contains( other.xy() ) && _z_bottom <= other.z() && _z_top >= other.z();
}

bool Range3d::intersect( const Range2d& other ) const
{
return _range.intersect( other );
}
bool Range3d::intersect( const Range3d& other ) const
{
return intersect( other._range ) && _z_bottom <= other._z_top && other._z_bottom <= _z_top;
}

bool Range3d::operator==( const Range3d& other ) const
{
return _range == other._range && _z_bottom == other._z_bottom && _z_top == other._z_top;
}
bool Range3d::operator!=( const Range3d& other ) const
{
return !( *this == other );
}

fmt::Writer& operator<<( fmt::Writer& w, const Range3d& v )
{
w << "( " << v.nw_b() << " - " << v.se_t() << " )";
return w;
}
} // namespace Core
} // namespace Pol
142 changes: 142 additions & 0 deletions pol-core/pol/base/range.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#ifndef POL_BASE_RANGE_H
#define POL_BASE_RANGE_H

#include "position.h"
#include <format/format.h>
#include <iterator>

namespace Pol
{
namespace Core
{
class Range2dItr
{
private:
Pos2d _v;
u16 _xbound;
u16 _xstart;

public:
Range2dItr( Pos2d v, const Pos2d& v_max );

typedef Pos2d value_type;
typedef std::ptrdiff_t difference_type;
typedef const Pos2d* pointer;
typedef const Pos2d& reference;
typedef std::input_iterator_tag iterator_category;

reference operator*() const;
bool operator==( const Range2dItr& other ) const;
bool operator!=( const Range2dItr& other ) const;
// does someone really need itr++?
Range2dItr& operator++();
};

class Range2d
{
private:
Pos2d _nw;
Pos2d _se;

public:
Range2d() = default;
Range2d( const Pos2d& p1, const Pos2d& p2, const Realms::Realm* realm );
Range2d( const Pos4d& p1, const Pos4d& p2 );
Range2d( const Range2d& other ) = default;
Range2d( Range2d&& other ) = default;
~Range2d() = default;
Range2d& operator=( const Range2d& other ) = default;
Range2d& operator=( Range2d&& other ) = default;

const Pos2d& nw() const;
const Pos2d& se() const;
// not implemented on purpose, forced min/max could give weird results
// Range2d& nw( const Pos2d& p );
// Range2d& se( const Pos2d& p );

Range2dItr begin() const;
Range2dItr end() const;

bool contains( const Pos2d& other ) const;
bool intersect( const Range2d& other ) const;

bool operator==( const Range2d& other ) const;
bool operator!=( const Range2d& other ) const;
};
fmt::Writer& operator<<( fmt::Writer& w, const Range2d& v );

class Range3d
{
private:
Range2d _range;
s8 _z_bottom;
s8 _z_top;

public:
Range3d() = default;
Range3d( const Pos3d& p1, const Pos3d& p2, const Realms::Realm* realm );
Range3d( const Pos4d& p1, const Pos4d& p2 );
Range3d( const Range3d& other ) = default;
Range3d( Range3d&& other ) = default;
~Range3d() = default;
Range3d& operator=( const Range3d& other ) = default;
Range3d& operator=( Range3d&& other ) = default;

const Pos2d& nw() const;
const Pos2d& se() const;
Pos3d nw_b() const;
Pos3d se_t() const;
// not implemented on purpose, forced min/max could give weird results
// Range3d& nw_b( const Pos3d& p );
// Range3d& se_t( const Pos3d& p );

const Range2d& range() const;

bool contains( const Pos2d& other ) const;
bool contains( const Pos3d& other ) const;
bool intersect( const Range2d& other ) const;
bool intersect( const Range3d& other ) const;

bool operator==( const Range3d& other ) const;
bool operator!=( const Range3d& other ) const;
};
fmt::Writer& operator<<( fmt::Writer& w, const Range3d& v );

inline Range2dItr::reference Range2dItr::operator*() const
{
return _v;
}

inline const Pos2d& Range2d::nw() const
{
return _nw;
}
inline const Pos2d& Range2d::se() const
{
return _se;
}

inline const Pos2d& Range3d::nw() const
{
return _range.nw();
}
inline const Pos2d& Range3d::se() const
{
return _range.se();
}
inline Pos3d Range3d::nw_b() const
{
return Pos3d( _range.nw(), _z_bottom );
}
inline Pos3d Range3d::se_t() const
{
return Pos3d( _range.se(), _z_top );
}
inline const Range2d& Range3d::range() const
{
return _range;
}
} // namespace Core
} // namespace Pol

#endif
Loading

0 comments on commit 5f79897

Please sign in to comment.