Ocean
Bresenham.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_CV_BRESENHAM_H
9 #define META_OCEAN_CV_BRESENHAM_H
10 
11 #include "ocean/cv/CV.h"
12 #include "ocean/cv/PixelPosition.h"
13 
14 #include "ocean/math/Line2.h"
15 
16 namespace Ocean
17 {
18 
19 namespace CV
20 {
21 
22 /**
23  * This class implements bresenham line algorithms.
24  * @ingroup cv
25  */
26 class OCEAN_CV_EXPORT Bresenham
27 {
28  public:
29 
30  /**
31  * Definition of different line orientations.
32  */
33  enum Orientation : uint32_t
34  {
35  /// Invalid orientation.
36  ORIENTATION_INVALID = 0u,
37  /// Line orientation with range [0, 45] degree.
39  /// Line orientation with range (45, 90] degree.
41  /// Line orientation with range [90, 135) degree.
43  /// Line orientation with range [135, 180] degree.
45  /// Line orientation with range [180, 225] degree.
47  /// Line orientation with range (225, 270] degree.
49  /// Line orientation with range (270, 315] degree.
51  /// Line orientation with range (315, 360] degree.
52  ORIENTATION_7
53  };
54 
55  public:
56 
57  /**
58  * Creates an invalid object.
59  */
60  inline Bresenham();
61 
62  /**
63  * Creates a new Bresenham line.
64  * @param x0 Horizontal start position of the line, with range (-infinity, infinity)
65  * @param y0 Vertical start position of the line, with range (-infinity, infinity)
66  * @param x1 Horizontal stop position of the line (inclusive), with range (-infinity, infinity)
67  * @param y1 Vertical stop position of the line (inclusive), with range (-infinity, infinity)
68  */
69  Bresenham(const int x0, const int y0, const int x1, const int y1);
70 
71  /**
72  * Applies one Bresenham step to find the next pixel.
73  * @param x Current horizontal position which will be updated, with range (-infinity, infinity)
74  * @param y Current vertical position which will be updated, with range (-infinity, infinity)
75  */
76  void findNext(int& x, int& y);
77 
78  /**
79  * Returns whether this object holds a valid line.
80  * @return True, if so
81  */
82  inline bool isValid() const;
83 
84  /**
85  * Returns the current orientation of the bresenham line.
86  * @return The current orientation
87  */
88  inline Orientation orientation() const;
89 
90  /**
91  * Computes the pixel-precise border intersection (the begin and end position) of a sub-pixel-precise 2D line which needs be is visible within a specified pixel-precise frame only.
92  * The begin position will be at the left border, otherwise at the top border or bottom border (in this order).
93  * @param line The 2D line for which the begin position and end position will be determined, must be valid
94  * @param leftBorder Left inclusive pixel border (e.g. could be 0), with range (-infinity, infinity)
95  * @param topBorder Top inclusive pixel border (e.g. could be 0), with range (-infinity, infinity)
96  * @param rightBorder Right inclusive pixel border (e.g. could be width - 1), with range [leftBorder, infinity)
97  * @param bottomBorder Bottom inclusive pixel border (e.g. could be height - 1), with range [topBorder, infinity)
98  * @param x0 Resulting horizontal begin position in pixel, with range [leftBorder, rightBorder]
99  * @param y0 Resulting vertical begin position in pixel, with range [topBorder, bottomBorder]
100  * @param x1 Resulting horizontal end position in pixel, with range [leftBorder, rightBorder]
101  * @param y1 Resulting vertical end position in pixel, with range [topBorder, bottomBorder]
102  * @return True, if the given line lies inside the defined frame
103  */
104  static bool borderIntersection(const Line2& line, const int leftBorder, const int topBorder, const int rightBorder, const int bottomBorder, int& x0, int& y0, int& x1, int& y1);
105 
106  /**
107  * Computes the sub-pixel-precise border intersection (the begin and end position) of a sub-pixel-precise 2D line which needs be is visible within a specified pixel-precise frame only.
108  * The begin position will be at the left border, otherwise at the top border or bottom border (in this order).
109  * @param line The 2D line for which the begin position and end position will be determined, must be valid
110  * @param leftBorder Left inclusive pixel border (e.g. could be 0), with range (-infinity, infinity)
111  * @param topBorder Top inclusive pixel border (e.g. could be 0), with range (-infinity, infinity)
112  * @param rightBorder Right inclusive pixel border (e.g. could be width - 1), with range [leftBorder, infinity)
113  * @param bottomBorder Bottom inclusive pixel border (e.g. could be height - 1), with range [topBorder, infinity)
114  * @param x0 Resulting horizontal begin position in pixel, with range [leftBorder, rightBorder]
115  * @param y0 Resulting vertical begin position in pixel, with range [topBorder, bottomBorder]
116  * @param x1 Resulting horizontal end position in pixel, with range [leftBorder, rightBorder]
117  * @param y1 Resulting vertical end position in pixel, with range [topBorder, bottomBorder]
118  * @return True, if the given line lies inside the defined frame
119  */
120  static bool borderIntersection(const Line2& line, const Scalar leftBorder, const Scalar topBorder, const Scalar rightBorder, const Scalar bottomBorder, Scalar& x0, Scalar& y0, Scalar& x1, Scalar& y1);
121 
122  /**
123  * Determines the number of pixels necessary for a line between two given points.
124  * The number of pixels include the begin and end points.
125  * @param x0 Horizontal begin position of the line, with range (-infinity, infinity)
126  * @param y0 Vertical begin position of the line, with range (-infinity, infinity)
127  * @param x1 Horizontal end position of the line, with range (-infinity, infinity)
128  * @param y1 Vertical end position of the line, with range (-infinity, infinity)
129  * @return Number of line pixels, with range [1, infinity)
130  */
131  static unsigned int numberLinePixels(const int x0, const int y0, const int x1, const int y1);
132 
133  /**
134  * Determines the intermediate pixels between two given pixels.
135  * @param position0 Begin position of the line (excluding), with range (-infinity, infinity)x(-infinity, infinity)
136  * @param position1 End position of the line (excluding), with range (-infinity, infinity)x(-infinity, infinity)
137  * @param pixels The resulting intermediate pixels
138  */
139  static void intermediatePixels(const PixelPosition& position0, const PixelPosition& position1, PixelPositions& pixels);
140 
141  protected:
142 
143  /// Orientation parameter.
145 
146  /// Control parameter.
147  int control_;
148 
149  /// Horizontal update value.
150  int updateX_;
151 
152  /// Horizontal and vertical update value.
154 };
155 
157  orientation_(ORIENTATION_INVALID)
158 {
159  // control_, updateX_, and updateXY_ are not initialized as we use `orientation_` to check for a valid object
160 }
161 
162 inline bool Bresenham::isValid() const
163 {
165 }
166 
168 {
169  return orientation_;
170 }
171 
172 }
173 
174 }
175 
176 #endif // META_OCEAN_CV_BRESENHAM_H
This class implements bresenham line algorithms.
Definition: Bresenham.h:27
static bool borderIntersection(const Line2 &line, const Scalar leftBorder, const Scalar topBorder, const Scalar rightBorder, const Scalar bottomBorder, Scalar &x0, Scalar &y0, Scalar &x1, Scalar &y1)
Computes the sub-pixel-precise border intersection (the begin and end position) of a sub-pixel-precis...
static void intermediatePixels(const PixelPosition &position0, const PixelPosition &position1, PixelPositions &pixels)
Determines the intermediate pixels between two given pixels.
int updateX_
Horizontal update value.
Definition: Bresenham.h:150
void findNext(int &x, int &y)
Applies one Bresenham step to find the next pixel.
static unsigned int numberLinePixels(const int x0, const int y0, const int x1, const int y1)
Determines the number of pixels necessary for a line between two given points.
Bresenham(const int x0, const int y0, const int x1, const int y1)
Creates a new Bresenham line.
static bool borderIntersection(const Line2 &line, const int leftBorder, const int topBorder, const int rightBorder, const int bottomBorder, int &x0, int &y0, int &x1, int &y1)
Computes the pixel-precise border intersection (the begin and end position) of a sub-pixel-precise 2D...
Orientation orientation_
Orientation parameter.
Definition: Bresenham.h:144
bool isValid() const
Returns whether this object holds a valid line.
Definition: Bresenham.h:162
int control_
Control parameter.
Definition: Bresenham.h:147
Bresenham()
Creates an invalid object.
Definition: Bresenham.h:156
Orientation orientation() const
Returns the current orientation of the bresenham line.
Definition: Bresenham.h:167
Orientation
Definition of different line orientations.
Definition: Bresenham.h:34
@ ORIENTATION_1
Line orientation with range (45, 90] degree.
Definition: Bresenham.h:40
@ ORIENTATION_3
Line orientation with range [135, 180] degree.
Definition: Bresenham.h:44
@ ORIENTATION_0
Line orientation with range [0, 45] degree.
Definition: Bresenham.h:38
@ ORIENTATION_4
Line orientation with range [180, 225] degree.
Definition: Bresenham.h:46
@ ORIENTATION_2
Line orientation with range [90, 135) degree.
Definition: Bresenham.h:42
@ ORIENTATION_6
Line orientation with range (270, 315] degree.
Definition: Bresenham.h:50
@ ORIENTATION_INVALID
Invalid orientation.
Definition: Bresenham.h:36
@ ORIENTATION_5
Line orientation with range (225, 270] degree.
Definition: Bresenham.h:48
int updateXY_
Horizontal and vertical update value.
Definition: Bresenham.h:153
This class implements an infinite line in 2D space.
Definition: Line2.h:83
std::vector< PixelPosition > PixelPositions
Definition of a vector holding pixel positions (with positive coordinate values).
Definition: PixelPosition.h:48
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15