MeshLib
 
Loading...
Searching...
No Matches
MRRectIndexer.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4#include "MRVector2.h"
5#include "MRId.h"
6#include <array>
7
8namespace MR
9{
10
13
14enum class OutEdge2 : signed char
15{
16 Invalid = -1,
17 PlusY,
18 MinusY,
19 PlusX,
20 MinusX,
21 Count
22};
23
24static_assert( sizeof( OutEdge2 ) == 1 );
25
26static const std::initializer_list<OutEdge2> allOutEdges2 = { OutEdge2::PlusY, OutEdge2::MinusY, OutEdge2::PlusX, OutEdge2::MinusX };
27
29{
30 const static std::array<OutEdge2, 5> map{ OutEdge2::Invalid, OutEdge2::MinusY, OutEdge2::PlusY, OutEdge2::MinusX, OutEdge2::PlusX };
31 return map[ (size_t)e + 1 ];
32}
33
34static constexpr int OutEdge2Count = 4;
35
38{
39public:
40 constexpr RectIndexer() noexcept = default;
41 RectIndexer( const Vector2i & dims );
42 void resize( const Vector2i & dims );
43 const Vector2i & dims() const { return dims_; }
44 size_t size() const { return size_; }
45 Vector2i toPos( PixelId id ) const { assert( id.valid() ); return toPos( size_t( int( id ) ) ); }
46 Vector2i toPos( size_t id ) const;
47 PixelId toPixelId( const Vector2i & pos ) const { return PixelId{ int( toIndex( pos ) ) }; }
48 size_t toIndex( const Vector2i & pos ) const;
50 bool areNeigbors( PixelId v0, PixelId v1 ) const { return areNeigbors( toPos( v0 ), toPos( v1 ) ); }
51 bool areNeigbors( const Vector2i & pos0, const Vector2i & pos1 ) const { return ( pos0 - pos1 ).lengthSq() == 1; }
53 PixelId getNeighbor( PixelId v, OutEdge2 toNei ) const { return getNeighbor( v, toPos( v ), toNei ); }
54 MRMESH_API PixelId getNeighbor( PixelId v, const Vector2i & pos, OutEdge2 toNei ) const;
55
56protected:
57 Vector2i dims_;
58 size_t size_ = 0;
59};
60
61inline RectIndexer::RectIndexer( const Vector2i & dims )
62{
63 resize( dims );
64}
65
66inline void RectIndexer::resize( const Vector2i & dims )
67{
68 dims_ = dims;
69 size_ = size_t( dims_.x ) * dims_.y;
70}
71
72inline Vector2i RectIndexer::toPos( size_t id ) const
73{
74 int y = int(id) / dims_.x;
75 int x = int(id) % dims_.x;
76 return {x, y};
77}
78
79inline size_t RectIndexer::toIndex( const Vector2i & pos ) const
80{
81 return pos.x + pos.y * size_t(dims_.x);
82}
83
85MRMESH_API void expandPixelMask( PixelBitSet& mask, const RectIndexer& indexer, int expansion = 1 );
87MRMESH_API void shrinkPixelMask( PixelBitSet& mask, const RectIndexer& indexer, int shrinkage = 1 );
88
90
91} // namespace MR
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
a class for converting 2D integer coordinates into 1D linear coordinates and backward
Definition MRRectIndexer.h:38
PixelId getNeighbor(PixelId v, OutEdge2 toNei) const
returns id of v's neighbor specified by the edge
Definition MRRectIndexer.h:53
const Vector2i & dims() const
Definition MRRectIndexer.h:43
bool areNeigbors(PixelId v0, PixelId v1) const
returns true if v1 is within at most 4 neighbors of v0
Definition MRRectIndexer.h:50
constexpr RectIndexer() noexcept=default
PixelId toPixelId(const Vector2i &pos) const
Definition MRRectIndexer.h:47
bool areNeigbors(const Vector2i &pos0, const Vector2i &pos1) const
Definition MRRectIndexer.h:51
Vector2i toPos(PixelId id) const
Definition MRRectIndexer.h:45
Vector2i dims_
Definition MRRectIndexer.h:57
size_t size() const
Definition MRRectIndexer.h:44
MRMESH_API PixelId getNeighbor(PixelId v, const Vector2i &pos, OutEdge2 toNei) const
size_t size_
= dims_.x * dims_.y
Definition MRRectIndexer.h:58
MRMESH_API void expandPixelMask(PixelBitSet &mask, const RectIndexer &indexer, int expansion=1)
expands PixelBitSet with given number of steps
void resize(const Vector2i &dims)
Definition MRRectIndexer.h:66
OutEdge2 opposite(OutEdge2 e)
Definition MRRectIndexer.h:28
OutEdge2
Definition MRRectIndexer.h:15
MRMESH_API void shrinkPixelMask(PixelBitSet &mask, const RectIndexer &indexer, int shrinkage=1)
shrinks PixelBitSet with given number of steps
size_t toIndex(const Vector2i &pos) const
Definition MRRectIndexer.h:79
Definition MRCameraOrientationPlugin.h:7