表格线识别通用库文档
载入中...
搜索中...
未找到
point.hpp
浏览该文件的文档.
1/*
2 * @Description: 点类 头文件及其实现
3 * @Version:
4 * @Autor: dreamy-xay
5 * @Date: 2023-12-01 15:02:12
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-03-06
8 */
9
10#ifndef COMMON_POINT_HPP
11#define COMMON_POINT_HPP
12
13#include <cmath>
14#include <iostream>
15#include <opencv2/core.hpp>
16
17#include "common/enum.h"
18#include "common/macro.h"
19#include "common/type.h"
20
21namespace cm {
22
51template <typename T>
52class Point {
53 public:
55 T x;
57 T y;
58
60 Point(T x, T y);
61 template <typename U>
62 Point(const Point<U>& pt);
63 template <typename U>
64 Point(const cv::Point_<U>& pt);
66
67 template <typename U>
68 operator cv::Point_<U>() const;
69
70 template <typename U>
71 operator Point<U>() const;
72
73 template <typename U>
74 friend bool operator==(const Point<U>& pt1, const Point<U>& pt2);
75 template <typename U>
76 friend bool operator!=(const Point<U>& pt1, const Point<U>& pt2);
77 template <typename U>
78 friend bool operator<(const Point<U>& pt1, const Point<U>& pt2);
79 template <typename U>
80 friend bool operator>(const Point<U>& pt1, const Point<U>& pt2);
81 template <typename U>
82 friend bool operator<=(const Point<U>& pt1, const Point<U>& pt2);
83 template <typename U>
84 friend bool operator>=(const Point<U>& pt1, const Point<U>& pt2);
85
86 template <typename U>
87 friend Point<U> operator-(const Point<U>& pt1, const Point<U>& pt2);
88
89 template <typename U>
90 friend std::ostream& operator<<(std::ostream& out, const Point<U>& pt);
91
92 template <typename U>
93 double DistanceTo(const Point<U>& pt) const;
95 Point<T>& Scale(double factor, bool round = false);
96 template <typename U>
97 Point<T>& Rotate(double angle, const Point<U>& center = {0, 0});
98
99 template <typename U>
100 double Dot(const Point<U>& pt) const;
101 template <typename U>
102 double Cross(const Point<U>& pt) const;
103
104 template <typename U>
105 Point<T>& LimitX(U start, U end);
106 template <typename U>
107 Point<T>& LimitY(U start, U end);
108};
109
116
124template <typename T>
125inline Point<T>::Point() : x{}, y{} {}
126
137template <typename T>
138inline Point<T>::Point(T x, T y) : x(x), y(y) {}
139
151template <typename T>
152template <typename U>
153inline Point<T>::Point(const Point<U>& pt) : x(static_cast<T>(pt.x)), y(static_cast<T>(pt.y)) {}
154
168template <typename T>
169template <typename U>
170inline Point<T>::Point(const cv::Point_<U>& pt) : x(static_cast<T>(pt.x)), y(static_cast<T>(pt.y)) {}
171
179template <typename T>
181
197template <typename T>
198template <typename U>
199inline Point<T>::operator cv::Point_<U>() const {
200 return cv::Point_<U>(static_cast<U>(x), static_cast<U>(y));
201}
202
214template <typename T>
215template <typename U>
217 return Point<U>(static_cast<U>(x), static_cast<U>(y));
218}
219
236template <typename U>
237inline bool operator==(const Point<U>& pt1, const Point<U>& pt2) {
238 return pt1.x == pt2.x && pt1.y == pt2.y;
239}
240
257template <typename U>
258inline bool operator!=(const Point<U>& pt1, const Point<U>& pt2) {
259 return pt1.x != pt2.x || pt1.y != pt2.y;
260}
261
278template <typename U>
279inline bool operator<(const Point<U>& pt1, const Point<U>& pt2) {
280 if (pt1.x == pt2.x)
281 return pt1.y < pt2.y;
282 return pt1.x < pt2.x;
283}
284
301template <typename U>
302inline bool operator>(const Point<U>& pt1, const Point<U>& pt2) {
303 if (pt1.x == pt2.x)
304 return pt1.y > pt2.y;
305 return pt1.x > pt2.x;
306}
307
324template <typename U>
325inline bool operator<=(const Point<U>& pt1, const Point<U>& pt2) {
326 if (pt1.x == pt2.x)
327 return pt1.y <= pt2.y;
328 return pt1.x <= pt2.x;
329}
330
347template <typename U>
348inline bool operator>=(const Point<U>& pt1, const Point<U>& pt2) {
349 if (pt1.x == pt2.x)
350 return pt1.y >= pt2.y;
351 return pt1.x >= pt2.x;
352}
353
368template <typename U>
369inline Point<U> operator-(const Point<U>& pt1, const Point<U>& pt2) {
370 return {pt1.x - pt2.x, pt1.y - pt2.y};
371}
372
387template <typename U>
388inline std::ostream& operator<<(std::ostream& out, const Point<U>& pt) {
389 out << "{ x: " << pt.x << ", y: " << pt.y << " }";
390 return out;
391}
392
408template <typename T>
409template <typename U>
410inline double Point<T>::DistanceTo(const Point<U>& pt) const {
411 return std::sqrt(static_cast<double>((pt.x - x) * (pt.x - x) + (pt.y - y) * (pt.y - y)));
412}
413
426template <typename T>
428 x += dx;
429 y += dy;
430
431 return *this;
432}
433
446template <typename T>
447inline Point<T>& Point<T>::Scale(double factor, bool round) {
448 if (round) {
449 x = std::round(x * factor);
450 y = std::round(y * factor);
451 } else {
452 x *= factor;
453 y *= factor;
454 }
455
456 return *this;
457}
458
473template <typename T>
474template <typename U>
476 // 将相对于中心点的坐标转换为相对于原点的坐标
477 T translated_x = x - center.x;
478 T translated_y = y - center.y;
479
480 // 计算旋转后的坐标
481 T rotated_x = translated_x * std::cos(angle) - translated_y * std::sin(angle);
482 T rotated_y = translated_x * std::sin(angle) + translated_y * std::cos(angle);
483
484 // 将相对于原点的坐标转换回相对于中心点的坐标
485 x = rotated_x + center.x;
486 y = rotated_y + center.y;
487
488 return *this;
489}
490
504template <typename T>
505template <typename U>
506inline double Point<T>::Dot(const Point<U>& pt) const {
507 return x * pt.x + y * pt.y;
508}
509
523template <typename T>
524template <typename U>
525inline double Point<T>::Cross(const Point<U>& pt) const {
526 return x * pt.y - pt.x * y;
527}
528
543template <typename T>
544template <typename U>
546 if (x < min_x)
547 x = min_x;
548
549 if (x > max_x)
550 x = max_x;
551
552 return *this;
553}
554
569template <typename T>
570template <typename U>
572 if (y < min_y)
573 y = min_y;
574
575 if (y > max_y)
576 y = max_y;
577
578 return *this;
579}
580
581} // namespace cm
582
583#endif
点类
Definition point.hpp:52
T y
点的 y 坐标
Definition point.hpp:57
Point< T > & Scale(double factor, bool round=false)
点的坐标缩放操作
Definition point.hpp:447
friend bool operator>=(const Point< U > &pt1, const Point< U > &pt2)
点类重载大于等于号
Definition point.hpp:348
Point< T > & LimitX(U start, U end)
限制点的 x 坐标在区间 [min_x, max_x] 范围内
Definition point.hpp:545
friend std::ostream & operator<<(std::ostream &out, const Point< U > &pt)
点类重载输出流运算符
Definition point.hpp:388
friend Point< U > operator-(const Point< U > &pt1, const Point< U > &pt2)
点类重载减号运算符
Definition point.hpp:369
double Cross(const Point< U > &pt) const
计算叉积
Definition point.hpp:525
T x
点的 x 坐标
Definition point.hpp:55
friend bool operator<(const Point< U > &pt1, const Point< U > &pt2)
点类重载小于号
Definition point.hpp:279
double DistanceTo(const Point< U > &pt) const
计算点到点之间的距离
Definition point.hpp:410
Point()
点类的默认构造函数
Definition point.hpp:125
friend bool operator==(const Point< U > &pt1, const Point< U > &pt2)
点类重载等于号
Definition point.hpp:237
Point< T > & Rotate(double angle, const Point< U > &center={0, 0})
点的坐标旋转操作
Definition point.hpp:475
friend bool operator!=(const Point< U > &pt1, const Point< U > &pt2)
点类重载不等于号
Definition point.hpp:258
Point(T x, T y)
点类的带参构造函数
Definition point.hpp:138
friend bool operator<=(const Point< U > &pt1, const Point< U > &pt2)
点类重载小于等于号
Definition point.hpp:325
Point(const Point< U > &pt)
点类拷贝构造函数
Definition point.hpp:153
friend bool operator>(const Point< U > &pt1, const Point< U > &pt2)
点类重载大于号
Definition point.hpp:302
double Dot(const Point< U > &pt) const
计算点积
Definition point.hpp:506
Point< T > & Translate(T dx, T dy)
点的坐标平移操作
Definition point.hpp:427
Point(const cv::Point_< U > &pt)
点类的重载构造函数
Definition point.hpp:170
Point< T > & LimitY(U start, U end)
限制点的 y 坐标在区间 [min_x, max_x] 范围内
Definition point.hpp:571
~Point()
点类的析构函数
Definition point.hpp:180
bool operator>=(const Interval &interval1, const Interval &interval2)
区间类重载大于等于号
Definition interval.hpp:310
bool operator!=(const Interval &interval1, const Interval &interval2)
区间类重载不等于号
Definition interval.hpp:232
T operator-(const T &value, const Percent &percent)
百分比类重载减法运算符
Definition percent.hpp:351
Point< float > Point2f
单精度浮点型坐标点类型
Definition point.hpp:113
Point< int > Point2i
整型坐标点类型
Definition point.hpp:111
bool operator<(const Interval &interval1, const Interval &interval2)
区间类重载小于号
Definition interval.hpp:250
Point< double > Point2d
双精度浮点型坐标点类型
Definition point.hpp:115
bool operator==(const Interval &interval1, const Interval &interval2)
区间类重载等于号
Definition interval.hpp:214
bool operator<=(const Interval &interval1, const Interval &interval2)
区间类重载小于等于号
Definition interval.hpp:290
std::ostream & operator<<(std::ostream &out, const Interval &interval)
区间类重载输出流运算符
Definition interval.hpp:328
bool operator>(const Interval &interval1, const Interval &interval2)
区间类重载大于号
Definition interval.hpp:270