CVB++ 15.0
area_2d.hpp
1#pragma once
2
3#include "global.hpp"
4
5#include "affine_matrix_2d.hpp"
6#include "matrix_2d.hpp"
7#include "point_2d.hpp"
8#include "rect.hpp"
9#include "size_2d.hpp"
10
11namespace Cvb
12{
13
14 CVB_BEGIN_INLINE_NS
16
20 class Area2D final
21 {
22 public:
24
27 Area2D() noexcept = default;
28
30
36 Area2D(Point2D<double> p0, Point2D<double> p1, Point2D<double> p2) noexcept
37 : p0_(p0)
38 , p1_(p1)
39 , p2_(p2)
40 {
41 }
42
44
49 Area2D(Rect<double> rect, AffineMatrix2D affineMatrix) noexcept
50 {
51 Area2D area(rect);
52 CExports::CoordinateMapTransformArea(*reinterpret_cast<CExports::TArea *>(&area),
53 *reinterpret_cast<CExports::TCoordinateMap *>(&affineMatrix),
54 reinterpret_cast<CExports::TArea &>(*this));
55 }
56
58
67 Area2D(double x0, double y0, double x1, double y1, double x2, double y2) noexcept
68 : Area2D(Point2D<double>(x0, y0), Point2D<double>(x1, y1), Point2D<double>(x2, y2))
69 {
70 }
71
73
77 explicit Area2D(Rect<double> rect) noexcept
78 : Area2D(rect.Left(), rect.Top(), rect.Right(), rect.Top(), rect.Left(), rect.Bottom())
79 {
80 }
81
83
87 explicit Area2D(double radius) noexcept
88 : Area2D(-radius, -radius, -radius, radius, radius, -radius)
89 {
90 }
91
93
97 Point2D<double> P0() const noexcept
98 {
99 return p0_;
100 }
101
103
107 void SetP0(Point2D<double> p0) noexcept
108 {
109 p0_ = p0;
110 }
111
113
117 Point2D<double> P1() const noexcept
118 {
119 return p1_;
120 }
121
123
127 void SetP1(Point2D<double> p1) noexcept
128 {
129 p1_ = p1;
130 }
131
133
137 Point2D<double> P2() const noexcept
138 {
139 return p2_;
140 }
141
143
147 void SetP2(Point2D<double> p2) noexcept
148 {
149 p2_ = p2;
150 }
151
153
157 Point2D<double> P3() const noexcept
158 {
159 return p1_ + p2_ - p0_;
160 }
161
163
168 {
169 Rect<double> bb(p0_, Size2D<double>());
170 ExpandBBByPoint(bb, p1_);
171 ExpandBBByPoint(bb, p2_);
172 ExpandBBByPoint(bb, P3());
173
174 return bb;
175 }
176
178
182 Size2D<double> Size() const noexcept
183 {
184 auto vP0P2 = p2_ - p0_;
185 auto vP0P1 = p1_ - p0_;
186 return Size2D<double>(vP0P2.Length(), vP0P1.Length());
187 }
188
190
196 double Area() const noexcept
197 {
198 auto vP0P2 = p2_ - p0_;
199 auto vP0P1 = p1_ - p0_;
200 return std::abs(vP0P2.X() * vP0P1.Y() - vP0P2.Y() * vP0P1.X());
201 }
202
204
208 void Transform(Matrix2D matrix) noexcept
209 {
210 p0_ = matrix * p0_;
211 p1_ = matrix * p1_;
212 p2_ = matrix * p2_;
213 }
214
216
220 void Transform(AffineMatrix2D affineMatrix) noexcept
221 {
222 p0_ = affineMatrix * p0_;
223 p1_ = affineMatrix * p1_;
224 p2_ = affineMatrix * p2_;
225 }
226
228
233 void SwapOrientation() noexcept
234 {
235 auto tmp = p1_;
236 p1_ = p2_;
237 p2_ = tmp;
238 }
239
242
250
252
257 bool operator==(const Area2D &area) const noexcept
258 {
259 return p0_ == area.p0_ && p1_ == area.p1_ && p2_ == area.p2_;
260 }
261
263
268 bool operator!=(const Area2D &area) const noexcept
269 {
270 return !(*this == area);
271 }
272
273 private:
274 static void ExpandBBByPoint(Rect<double> &bb, Point2D<double> p)
275 {
276 if (p.X() < bb.Left())
277 bb.SetLeft(p.X());
278 else if (p.X() > bb.Right())
279 bb.SetRight(p.X());
280 if (p.Y() < bb.Top())
281 bb.SetTop(p.Y());
282 else if (p.Y() > bb.Bottom())
283 bb.SetBottom(p.Y());
284 }
285
286 Point2D<double> p0_;
287 Point2D<double> p1_;
288 Point2D<double> p2_;
289 };
290
291 CVB_END_INLINE_NS
292
293} // namespace Cvb
Compacted affine matrix describing the Common Vision Blox coordinate system.
Definition affine_matrix_2d.hpp:17
Point2D< double > P2() const noexcept
Gets P2 of the area (top right corner).
Definition area_2d.hpp:137
void SetP2(Point2D< double > p2) noexcept
Sets P2 of the area (top right corner).
Definition area_2d.hpp:147
Point2D< double > P0() const noexcept
Gets P0 of the area (top left corner).
Definition area_2d.hpp:97
double Area() const noexcept
Size (in pixels) of the area of interest.
Definition area_2d.hpp:196
void Transform(AffineMatrix2D affineMatrix) noexcept
Transform this Area2D using a AffineMatrix2D.
Definition area_2d.hpp:220
Area2D(double radius) noexcept
Constructor for Area2D.
Definition area_2d.hpp:87
void Transform(Matrix2D matrix) noexcept
Transform this Area2D using a matrix.
Definition area_2d.hpp:208
bool operator==(const Area2D &area) const noexcept
Compares to an other area.
Definition area_2d.hpp:257
Point2D< double > P3() const noexcept
Gets the calculated P3 of the area (lower right corner).
Definition area_2d.hpp:157
void SetP1(Point2D< double > p1) noexcept
Sets P1 of the area (lower left corner).
Definition area_2d.hpp:127
bool operator!=(const Area2D &area) const noexcept
Compares to an other area.
Definition area_2d.hpp:268
Area2D(Rect< double > rect, AffineMatrix2D affineMatrix) noexcept
Creates an area from the given rectangle using a transformation.
Definition area_2d.hpp:49
Area2D(double x0, double y0, double x1, double y1, double x2, double y2) noexcept
Constructor for Area2D.
Definition area_2d.hpp:67
Area2D() noexcept=default
Default constructor for an empty area.
void SetP0(Point2D< double > p0) noexcept
Sets P0 of the area (top left corner).
Definition area_2d.hpp:107
CoordinateSystemType CoordinateSystem() const noexcept
Indicates the coordinate system, in which this object is being measured (when used as an area of inte...
Definition area_2d.hpp:246
Area2D(Rect< double > rect) noexcept
Create an area from the given rectangle.
Definition area_2d.hpp:77
Rect< double > BoundingRectangle() const noexcept
Gets the bounding box of this area.
Definition area_2d.hpp:167
Size2D< double > Size() const noexcept
Size (width and height) of the area of interest.
Definition area_2d.hpp:182
void SwapOrientation() noexcept
Let the vertices P1 and P2 swap their place.
Definition area_2d.hpp:233
Point2D< double > P1() const noexcept
Gets P1 of the area (lower left corner).
Definition area_2d.hpp:117
Double precision 2x2 matrix class.
Definition matrix_2d.hpp:16
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
Rectangle object.
Definition rect.hpp:24
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition rect.hpp:144
void SetRight(T right) noexcept
Sets rightmost column of the rectangle (still inside the rectangle).
Definition rect.hpp:134
void SetBottom(T bottom) noexcept
Sets bottom row of the rectangle (still inside the rectangle).
Definition rect.hpp:154
void SetLeft(T left) noexcept
Sets first column of the rectangle.
Definition rect.hpp:94
T Top() const noexcept
Gets first row of the rectangle.
Definition rect.hpp:104
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition rect.hpp:124
T Left() const noexcept
Gets first column of the rectangle.
Definition rect.hpp:84
void SetTop(T top) noexcept
Sets first row of the rectangle.
Definition rect.hpp:114
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
CoordinateSystemType
Enumeration of the different available coordinate systems that an Area of interest may be defined in.
Definition global.hpp:290
@ ImageCoordinates
Definition global.hpp:307