OTB  9.0.0
Orfeo Toolbox
lpContour.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Contour Library LP
4  Language: C++
5  author: Lassalle Pierre
6  contact: lassallepierre34@gmail.com
7 
8 
9 
10  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef LP_CONTOUR_H
19 #define LP_CONTOUR_H
20 #include <cassert>
21 #include <iostream>
22 #include <bitset>
23 #include <vector>
24 #include <utility>
25 #include <unordered_set>
26 #include <unordered_map>
27 #include <boost/dynamic_bitset.hpp>
28 
29 // Improvement: do not use a boolean matrix but a collection because
30 // at the end of the process matrices are sparses.
31 
32 namespace lp
33 {
34  /* Move along a contour: 4 possibles: top (0), right(1), bottom (2), left(3). */
35  using Move = std::bitset<2>;
36  /* A contour is a list of moves represented by a pair of bits*/
37  using Contour = boost::dynamic_bitset<>;
38 
39  using ContourIndex = boost::dynamic_bitset<>::size_type;
40 
41  /* Index of a cell in the grid */
42  using CellIndex = std::size_t;
43 
44  /* List of cell indices */
45  using CellLists = std::unordered_set<CellIndex>;
46 
47  struct BoundingBox
48  {
49  std::size_t m_UX;
50  std::size_t m_UY;
51  std::size_t m_W;
52  std::size_t m_H;
53  };
54 
56  {
57  public:
58  /* Methods to fill a contour. */
59  static void Push0(Contour& contour); // Push a move to the top.
60  static void Push1(Contour& contour); // Push a move to the right.
61  static void Push2(Contour& contour); // Push a move to the bottom.
62  static void Push3(Contour& contour); // Push a move to the left.
63 
64  /* Methods to access to elements of a contour. */
65  static Move GetMove2(ContourIndex idx, const Contour& contour); // Retrieve a move
66  static short GetMove10(const Move& m); // Transform the move into a 10 base number (0,1,2 or 3).
67 
68  /* Main methods */
69  static void MergeContour(Contour& mergedContour,
70  BoundingBox& mergedBoundingBox,
71  Contour& contour1,
72  Contour& contour2,
73  BoundingBox& bbox1,
74  BoundingBox& bbox2,
75  const CellIndex cid1,
76  const CellIndex cid2,
77  const std::size_t gridSizeX);
78 
79  static void GenerateBorderCellsForContourFusion(CellLists& borderCells,
80  const Contour& contour,
81  const CellIndex startCellId,
82  const std::size_t gridSizeX,
83  const BoundingBox& mergedBBox);
84 
85  static void GenerateBorderCells(CellLists& borderCells, // From a contour, the smallest id of the cell
86  const Contour& contour, // belonging to the shape and the size of the grid,
87  const CellIndex startCellId, // this method returns the ids of the border cells
88  const std::size_t gridSizeX); // of the shape.
89 
90  static void CreateNewContour(Contour& newContour,
91  const CellIndex startCellId,
92  CellLists& setCells,
93  const std::size_t bboxWidth,
94  const std::size_t bboxHeight);
95 
96  /* Utilities methods */
97  static BoundingBox MergeBoundingBoxes(const BoundingBox& bb1, // Given 2 bounding boxes return the bounding
98  const BoundingBox& bb2); // which is the union.
99 
100  static void EIGHTNeighborhood(long int * neighborhood, // return 8-neighbors of cell id.
101  const CellIndex id,
102  const std::size_t width,
103  const std::size_t height);
104 
105  static CellIndex BBoxToGrid(const CellIndex bboxId,
106  const BoundingBox& bbox,
107  const std::size_t gridSizeX);
108 
109  static CellIndex GridToBBox(const CellIndex gridId, // Return the coordinates of the cell
110  const BoundingBox& bbox, // in the bounding box reference.
111  const std::size_t gridSizeX);
112  };
113 } // end of namespace lp
114 #endif
lp::ContourOperations::GenerateBorderCells
static void GenerateBorderCells(CellLists &borderCells, const Contour &contour, const CellIndex startCellId, const std::vcl_size_t gridSizeX)
lp::BoundingBox::m_W
std::vcl_size_t m_W
Definition: lpContour.h:51
lp::ContourOperations::GetMove10
static short GetMove10(const Move &m)
lp::BoundingBox::m_UY
std::vcl_size_t m_UY
Definition: lpContour.h:50
lp::CellIndex
std::vcl_size_t CellIndex
Definition: lpContour.h:42
lp::CellLists
std::unordered_set< CellIndex > CellLists
Definition: lpContour.h:45
lp::ContourOperations::GridToBBox
static CellIndex GridToBBox(const CellIndex gridId, const BoundingBox &bbox, const std::vcl_size_t gridSizeX)
lp::ContourOperations::Push0
static void Push0(Contour &contour)
lp
Definition: lpContour.h:32
lp::ContourOperations::CreateNewContour
static void CreateNewContour(Contour &newContour, const CellIndex startCellId, CellLists &setCells, const std::vcl_size_t bboxWidth, const std::vcl_size_t bboxHeight)
lp::ContourOperations::Push3
static void Push3(Contour &contour)
lp::ContourOperations::GetMove2
static Move GetMove2(ContourIndex idx, const Contour &contour)
lp::ContourOperations::MergeContour
static void MergeContour(Contour &mergedContour, BoundingBox &mergedBoundingBox, Contour &contour1, Contour &contour2, BoundingBox &bbox1, BoundingBox &bbox2, const CellIndex cid1, const CellIndex cid2, const std::vcl_size_t gridSizeX)
lp::BoundingBox
Definition: lpContour.h:47
lp::ContourOperations::Push1
static void Push1(Contour &contour)
lp::Contour
boost::dynamic_bitset<> Contour
Definition: lpContour.h:37
lp::ContourOperations::BBoxToGrid
static CellIndex BBoxToGrid(const CellIndex bboxId, const BoundingBox &bbox, const std::vcl_size_t gridSizeX)
lp::ContourOperations
Definition: lpContour.h:55
lp::BoundingBox::m_H
std::vcl_size_t m_H
Definition: lpContour.h:52
lp::ContourOperations::Push2
static void Push2(Contour &contour)
lp::ContourOperations::MergeBoundingBoxes
static BoundingBox MergeBoundingBoxes(const BoundingBox &bb1, const BoundingBox &bb2)
lp::ContourIndex
boost::dynamic_bitset<>::size_type ContourIndex
Definition: lpContour.h:39
lp::Move
std::bitset< 2 > Move
Definition: lpContour.h:35
lp::ContourOperations::GenerateBorderCellsForContourFusion
static void GenerateBorderCellsForContourFusion(CellLists &borderCells, const Contour &contour, const CellIndex startCellId, const std::vcl_size_t gridSizeX, const BoundingBox &mergedBBox)
lp::ContourOperations::EIGHTNeighborhood
static void EIGHTNeighborhood(long int *neighborhood, const CellIndex id, const std::vcl_size_t width, const std::vcl_size_t height)
lp::BoundingBox::m_UX
std::vcl_size_t m_UX
Definition: lpContour.h:49