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
14CVB_BEGIN_INLINE_NS
16
20class Area2D final
21{
22public:
24
27 Area2D() noexcept = default;
28
30
36 Area2D(Point2D<double> p0, Point2D<double> p1, Point2D<double> p2) noexcept : p0_(p0), p1_(p1), p2_(p2) {}
37
39
44 Area2D(Rect<double> rect, AffineMatrix2D affineMatrix) noexcept
45 {
46 Area2D area(rect);
47 CExports::CoordinateMapTransformArea(*reinterpret_cast<CExports::TArea *>(&area),
48 *reinterpret_cast<CExports::TCoordinateMap *>(&affineMatrix),
49 reinterpret_cast<CExports::TArea &>(*this));
50 }
51
53
62 Area2D(double x0, double y0, double x1, double y1, double x2, double y2) noexcept
63 : Area2D(Point2D<double>(x0, y0), Point2D<double>(x1, y1), Point2D<double>(x2, y2))
64 {
65 }
66
68
72 explicit Area2D(Rect<double> rect) noexcept
73 : Area2D(rect.Left(), rect.Top(), rect.Right(), rect.Top(), rect.Left(), rect.Bottom())
74 {
75 }
76
78
82 explicit Area2D(double radius) noexcept : Area2D(-radius, -radius, -radius, radius, radius, -radius) {}
83
85
89 Point2D<double> P0() const noexcept { return p0_; }
90
92
96 void SetP0(Point2D<double> p0) noexcept { p0_ = p0; }
97
99
103 Point2D<double> P1() const noexcept { return p1_; }
104
106
110 void SetP1(Point2D<double> p1) noexcept { p1_ = p1; }
111
113
117 Point2D<double> P2() const noexcept { return p2_; }
118
120
124 void SetP2(Point2D<double> p2) noexcept { p2_ = p2; }
125
127
131 Point2D<double> P3() const noexcept { return p1_ + p2_ - p0_; }
132
134
139 {
140 Rect<double> bb(p0_, Size2D<double>());
141 ExpandBBByPoint(bb, p1_);
142 ExpandBBByPoint(bb, p2_);
143 ExpandBBByPoint(bb, P3());
144
145 return bb;
146 }
147
149
153 Size2D<double> Size() const noexcept
154 {
155 auto vP0P2 = p2_ - p0_;
156 auto vP0P1 = p1_ - p0_;
157 return Size2D<double>(vP0P2.Length(), vP0P1.Length());
158 }
159
161
167 double Area() const noexcept
168 {
169 auto vP0P2 = p2_ - p0_;
170 auto vP0P1 = p1_ - p0_;
171 return std::abs(vP0P2.X() * vP0P1.Y() - vP0P2.Y() * vP0P1.X());
172 }
173
175
179 void Transform(Matrix2D matrix) noexcept
180 {
181 p0_ = matrix * p0_;
182 p1_ = matrix * p1_;
183 p2_ = matrix * p2_;
184 }
185
187
191 void Transform(AffineMatrix2D affineMatrix) noexcept
192 {
193 p0_ = affineMatrix * p0_;
194 p1_ = affineMatrix * p1_;
195 p2_ = affineMatrix * p2_;
196 }
197
199
204 void SwapOrientation() noexcept
205 {
206 auto tmp = p1_;
207 p1_ = p2_;
208 p2_ = tmp;
209 }
210
212
217
219
224 bool operator==(const Area2D &area) const noexcept { return p0_ == area.p0_ && p1_ == area.p1_ && p2_ == area.p2_; }
225
227
232 bool operator!=(const Area2D &area) const noexcept { return !(*this == area); }
233
234private:
235 static void ExpandBBByPoint(Rect<double> &bb, Point2D<double> p)
236 {
237 if (p.X() < bb.Left())
238 bb.SetLeft(p.X());
239 else if (p.X() > bb.Right())
240 bb.SetRight(p.X());
241 if (p.Y() < bb.Top())
242 bb.SetTop(p.Y());
243 else if (p.Y() > bb.Bottom())
244 bb.SetBottom(p.Y());
245 }
246
247 Point2D<double> p0_;
248 Point2D<double> p1_;
249 Point2D<double> p2_;
250};
251
252CVB_END_INLINE_NS
253
254}
Compacted affine matrix describing the Common Vision Blox coordinate system.
Definition: affine_matrix_2d.hpp:18
Structure that represents an area of interest in the image.
Definition: area_2d.hpp:21
Point2D< double > P2() const noexcept
Gets P2 of the area (top right corner).
Definition: area_2d.hpp:117
void SetP2(Point2D< double > p2) noexcept
Sets P2 of the area (top right corner).
Definition: area_2d.hpp:124
Point2D< double > P0() const noexcept
Gets P0 of the area (top left corner).
Definition: area_2d.hpp:89
double Area() const noexcept
Size (in pixels) of the area of interest.
Definition: area_2d.hpp:167
void Transform(AffineMatrix2D affineMatrix) noexcept
Transform this Area2D using a AffineMatrix2D.
Definition: area_2d.hpp:191
Area2D(double radius) noexcept
Constructor for Area2D.
Definition: area_2d.hpp:82
void Transform(Matrix2D matrix) noexcept
Transform this Area2D using a matrix.
Definition: area_2d.hpp:179
bool operator==(const Area2D &area) const noexcept
Compares to an other area.
Definition: area_2d.hpp:224
Point2D< double > P3() const noexcept
Gets the calculated P3 of the area (lower right corner).
Definition: area_2d.hpp:131
void SetP1(Point2D< double > p1) noexcept
Sets P1 of the area (lower left corner).
Definition: area_2d.hpp:110
bool operator!=(const Area2D &area) const noexcept
Compares to an other area.
Definition: area_2d.hpp:232
Area2D(Rect< double > rect, AffineMatrix2D affineMatrix) noexcept
Creates an area from the given rectangle using a transformation.
Definition: area_2d.hpp:44
Area2D(double x0, double y0, double x1, double y1, double x2, double y2) noexcept
Constructor for Area2D.
Definition: area_2d.hpp:62
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:96
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:216
Area2D(Rect< double > rect) noexcept
Create an area from the given rectangle.
Definition: area_2d.hpp:72
Rect< double > BoundingRectangle() const noexcept
Gets the bounding box of this area.
Definition: area_2d.hpp:138
Size2D< double > Size() const noexcept
Size (width and height) of the area of interest.
Definition: area_2d.hpp:153
void SwapOrientation() noexcept
Let the vertices P1 and P2 swap their place.
Definition: area_2d.hpp:204
Point2D< double > P1() const noexcept
Gets P1 of the area (lower left corner).
Definition: area_2d.hpp:103
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:86
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
Rectangle object.
Definition: rect.hpp:26
T Bottom() const noexcept
Gets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:151
void SetRight(T right) noexcept
Sets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:141
void SetBottom(T bottom) noexcept
Sets bottom row of the rectangle (still inside the rectangle).
Definition: rect.hpp:161
void SetLeft(T left) noexcept
Sets first column of the rectangle.
Definition: rect.hpp:101
T Top() const noexcept
Gets first row of the rectangle.
Definition: rect.hpp:111
T Right() const noexcept
Gets rightmost column of the rectangle (still inside the rectangle).
Definition: rect.hpp:131
T Left() const noexcept
Gets first column of the rectangle.
Definition: rect.hpp:91
void SetTop(T top) noexcept
Sets first row of the rectangle.
Definition: rect.hpp:121
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
CoordinateSystemType
Enumeration of the different available coordinate systems that an Area of interest may be defined in.
Definition: global.hpp:270