1 #ifndef SDK_GEOMETRY_TYPES_HELPERS_H_
11 #define SDK_GEOMETRY_TYPES_HELPERS_H_
18 #include <base/inc/float_util.h>
36 typedef RectI2D Rect2D;
39 class CuBezierSegment;
41 typedef std::vector< PointF2D > PointF2DVec;
42 typedef std::vector< PointD2D > PointD2DVec;
43 typedef std::vector< PointI2D > PointI2DVec;
44 typedef std::vector< SizeF > SizeFVec;
45 typedef std::vector< SizeD > SizeDVec;
46 typedef std::vector< Size > SizeVec;
47 typedef std::vector< RectF2D > RectF2DVec;
48 typedef std::vector< RectD2D > RectD2DVec;
49 typedef std::vector< RectI2D > RectI2DVec, Rect2DVec;
50 typedef std::vector< RoundRectF2D > RoundRectF2DVec;
51 typedef std::vector< QBezierSegment > QBezierSegmentVec;
52 typedef std::vector< CuBezierSegment > CuBezierSegmentVec;
53 typedef std::vector< SDKGeoPoint > SDKGeoPointVec;
54 typedef std::vector< SDKGeoIntPoint > SDKGeoIntPointVec;
66 x = coordinate_type(src_pt.x);
67 y = coordinate_type(src_pt.y);
71 PointF2D(
float init_x,
float init_y) noexcept { x = init_x, y = init_y; }
74 x = src_pt.x, y = src_pt.y;
79 x = coordinate_type(src_pt.x);
80 y = coordinate_type(src_pt.y);
85 x += src_pt.x, y += src_pt.y;
90 x -= src_pt.x, y -= src_pt.y;
95 bool operator!=(
const PointF2D& pt)
const noexcept {
return !operator==(pt);}
96 bool operator==(
const PointF2D& pt)
const noexcept
98 if (fabs(x - pt.x) <= FLT_EPSILON && fabs(y - pt.y) <= FLT_EPSILON)
104 typedef std::vector<PointF2D> container;
113 PointD2D() noexcept { x = y = 0.0; }
121 PointD2D(
double init_x,
double init_y) noexcept { x = init_x, y = init_y; }
124 x = src_pt.x, y = src_pt.y;
135 x += src_pt.x, y += src_pt.y;
141 x -= src_pt.x, y -= src_pt.y;
145 bool operator!=(
const PointD2D& pt)
const noexcept {
return !operator==(pt);}
146 bool operator==(
const PointD2D& pt)
const noexcept
148 if (fabs(x - pt.x) <= FLT_EPSILON && fabs(y - pt.y) <= FLT_EPSILON)
153 double Length(
const PointD2D& p)
const noexcept {
156 return sqrt(dx*dx + dy*dy);
160 typedef std::vector<PointD2D> container;
173 PointI2D(SDKInt32 init_x, SDKInt32 init_y) noexcept { x = init_x, y = init_y; }
180 typedef std::vector<PointI2D> container;
182 bool operator!=(
const PointI2D& pt)
const noexcept {
return !operator==(pt); }
183 bool operator==(
const PointI2D& pt)
const noexcept
185 if ((x == pt.x) && (y == pt.y))
194 PointF3D() noexcept { x = 0.f; y = 0.f; z = 0.f; }
196 PointF3D(
float _x,
float _y,
float _z) noexcept { x = _x; y = _y; z = _z; }
199 x = coordinate_type(src.x);
200 y = coordinate_type(src.y);
201 z = coordinate_type(src.z);
204 PointF3D& operator=(
const SDKPointF3D& src) noexcept {
209 PointF3D& operator=(
const SDKPointD3D& src) noexcept {
210 x = coordinate_type(src.x);
211 y = coordinate_type(src.y);
212 z = coordinate_type(src.z);
219 PointD3D() noexcept { x = 0.f; y = 0.f; z = 0.f; }
220 PointD3D(
double _x,
double _y,
double _z) noexcept { x = _x; y = _y; z = _z; }
222 PointD3D(
const SDKPointF3D& src) noexcept { x = src.x; y = src.y; z = src.z; }
223 PointD3D& operator=(
const SDKPointD3D& src) noexcept {
228 PointD3D& operator=(
const SDKPointF3D& src) noexcept {
229 x = coordinate_type(src.x);
230 y = coordinate_type(src.y);
231 z = coordinate_type(src.z);
242 SizeF() noexcept { width = height = 0.0f; }
246 width =
static_cast<float>(src_size.width);
247 height =
static_cast<float>(src_size.height);
251 width =
static_cast<float>(src_size.width);
252 height =
static_cast<float>(src_size.height);
256 SizeF(
float init_width,
float init_height) noexcept {
257 width = init_width, height = init_height;
261 bool IsNull()
const noexcept {
262 return (width == 0.0f && height == 0.0f);
266 bool IsEqual(
const SDKSizeF& s)
const noexcept {
267 return (fabs(width - s.width) <= FLT_EPSILON && fabs(height - s.height) <= FLT_EPSILON);
271 void operator=(
const SDKSizeF& src_size) noexcept {
272 width = src_size.width, height = src_size.height;
274 bool operator==(
const SDKSizeF& s)
const noexcept {
return IsEqual(s); }
275 bool operator!=(
const SDKSizeF& s)
const noexcept {
return !IsEqual(s); }
279 *
static_cast<SDKSizeF*
>(
this) = src_size;
284 width =
static_cast<coordinate_type
>(src_size.width);
285 height =
static_cast<coordinate_type
>(src_size.height);
290 width =
static_cast<coordinate_type
>(src_size.width);
291 height =
static_cast<coordinate_type
>(src_size.height);
295 typedef std::vector<SizeF> container;
304 SizeD() noexcept { width = height = 0.0f; }
308 width = src_size.width, height = src_size.height;
312 width =
static_cast<double>(src_size.width);
313 height =
static_cast<double>(src_size.height);
317 SizeD(
double init_width,
double init_height) noexcept {
318 width = init_width, height = init_height;
322 bool IsNull()
const noexcept {
323 return (width == 0.0f && height == 0.0f);
327 bool IsEqual(
const SDKSizeD& s)
const noexcept {
328 return (fabs(width - s.width) <= DBL_EPSILON && fabs(height - s.height) <= DBL_EPSILON);
332 void operator=(
const SDKSizeD& src_size) noexcept {
333 width = src_size.width, height = src_size.height;
335 bool operator==(
const SDKSizeD& s)
const noexcept {
return IsEqual(s); }
336 bool operator!=(
const SDKSizeD& s)
const noexcept {
return !IsEqual(s); }
340 *
static_cast<SDKSizeD*
>(
this) = src_size;
345 width =
static_cast<coordinate_type
>(src_size.width);
346 height =
static_cast<coordinate_type
>(src_size.height);
351 width =
static_cast<coordinate_type
>(src_size.width);
352 height =
static_cast<coordinate_type
>(src_size.height);
356 typedef std::vector<SizeD> container;
365 Size() { width = height = 0; }
369 width =
static_cast<SDKUInt32
>(src_size.width);
370 height =
static_cast<SDKUInt32
>(src_size.height);
374 width =
static_cast<SDKUInt32
>(src_size.width);
375 height =
static_cast<SDKUInt32
>(src_size.height);
379 Size(SDKUInt32 init_width, SDKUInt32 init_height) noexcept {
380 width = init_width, height = init_height;
385 bool IsNull()
const noexcept {
386 return (width == 0 && height == 0);
389 bool IsEqual(
const SDKSize& s)
const noexcept {
390 return (width == s.width && height == s.height);
394 void operator=(
const SDKSize& src_size) noexcept {
395 width = src_size.width, height = src_size.height;
397 bool operator==(
const SDKSize& s)
const noexcept {
return IsEqual(s); }
398 bool operator!=(
const SDKSize& s)
const noexcept {
return !IsEqual(s); }
401 Size& operator=(
const SDKSize& src_size) noexcept {
402 *
static_cast<SDKSize*
>(
this) = src_size;
407 width =
static_cast<coordinate_type
>(src_size.width);
408 height =
static_cast<coordinate_type
>(src_size.height);
413 width =
static_cast<coordinate_type
>(src_size.width);
414 height =
static_cast<coordinate_type
>(src_size.height);
418 typedef std::vector<Size> container;
427 RectF2D() noexcept { x = y = width = height = 0.0f; }
431 RectF2D(
float init_x,
float init_y,
float init_w,
float init_h) noexcept {
432 x = init_x, y = init_y, width = init_w, height = init_h;
437 x = pt.x, y = pt.y, width = size.width, height = size.height;
442 x = -org.x, y = -org.y, width = size.width, height = size.height;
446 bool IsEmpty()
const noexcept {
return (width == 0.0f && height == 0.0f); }
448 bool IsNull()
const noexcept {
449 return (x == 0.0f && y == 0.0f && width == 0.0f && height == 0.0f);
453 bool IsEqual(
const SDKRectF2D& r)
const noexcept {
454 return (x == r.x && y == r.y && width == r.width && height == r.height);
458 return PointF2D(x + width / 2.0f, y + height / 2.0f);
468 pts[0].x = rc.x < x ? rc.x : x;
469 pts[0].y = rc.y < y ? rc.y : y;
470 pts[1].x = ((rc.x + rc.width) > (x + width)) ?
471 (rc.x + rc.width) : (x + width);
472 pts[1].y = ((rc.y + rc.height) > (y + height)) ?
473 (rc.y + rc.height) : (y + height);
476 width = pts[1].x - pts[0].x;
477 height = pts[1].y - pts[0].y;
483 if (!IsEmpty() && !rc.IsEmpty() && x < rc.x + rc.width &&
484 rc.x < x + width && y + height > rc.y && rc.y + rc.height > y) {
486 res.x = (x > rc.x) ? x : rc.x;
487 res.y = (y > rc.y) ? y : rc.y;
488 res.width = (x + width < rc.x + rc.width) ?
489 x + width - res.x : rc.x + rc.width - res.x;
490 res.height = (y + height < rc.y + rc.height) ?
491 y + height - res.y : rc.y + rc.height - res.y;
503 bool operator==(
const SDKRectF2D& r)
const noexcept {
return IsEqual(r); }
504 bool operator!=(
const SDKRectF2D& r)
const noexcept {
return !IsEqual(r); }
512 bool IsIntersected(
const SDKRectF2D& src)
const noexcept {
513 return !(IsEmpty() || (src.width == 0) || (src.height == 0) ||
514 (x >= (src.x + src.width)) || (src.x >= (x + width)) ||
515 (y >= (src.y + src.height)) || (src.y >= y + height));
519 bool IsIntersected_Geometrical(
const SDKRectF2D& src)
const noexcept {
520 return !((width == 0) || (height == 0) ||
521 (src.width == 0) || (src.height == 0) ||
522 (x >= (src.x + src.width)) || (src.x >= (x + width)) ||
523 (y >= (src.y + src.height)) || (src.y >= y + height));
527 bool IsIntersected_Graphical(
const SDKRectF2D& src)
const noexcept {
528 return !((x > (src.x + src.width)) || (src.x > (x + width)) ||
529 (y > (src.y + src.height)) || (src.y > y + height));
532 bool IsIncludes(
const SDKRectF2D& src)
const noexcept {
533 return (IsPointInRect(src.x, src.y) &&
534 IsPointInRect(src.x + src.width, src.y + src.height));
537 bool IsPointInRect(
float p_x,
float p_y)
const noexcept {
540 return (p_x >= x && p_x <= (x + width) &&
541 p_y >= y && p_y <= (y + height));
544 bool IsPointInRect(
const SDKPointF2D& p)
const noexcept {
545 return IsPointInRect(p.x, p.y);
548 RectF2D& Expand(
float factor) {
549 float expand_x = width * factor;
550 float expand_y = height * factor;
553 width += 2 * expand_x;
554 height += 2 * expand_y;
558 typedef std::vector<RectF2D> container;
567 RectD2D() noexcept { x = y = width = height = 0.0f; }
576 x = src_r.x, y = src_r.y, width = src_r.width, height = src_r.height;
579 RectD2D(
double init_x,
double init_y,
double init_w,
double init_h) noexcept {
580 x = init_x, y = init_y, width = init_w, height = init_h;
586 x = pt.x, y = pt.y, width = size.width, height = size.height;
592 x = -org.x, y = -org.y, width = size.width, height = size.height;
597 bool IsEmpty()
const noexcept {
return (width == 0.0f && height == 0.0f); }
599 bool IsNull()
const noexcept {
600 return (x == 0.0f && y == 0.0f && width == 0.0f && height == 0.0f);
603 bool IsEqual(
const SDKRectD2D& r)
const noexcept {
604 return (x == r.x && y == r.y && width == r.width && height == r.height);
608 return PointD2D(x + width / 2.0, y + height / 2.0);
618 pts[0].x = rc.x < x ? rc.x : x;
619 pts[0].y = rc.y < y ? rc.y : y;
620 pts[1].x = ((rc.x + rc.width) > (x + width)) ? (rc.x + rc.width) : (x + width);
621 pts[1].y = ((rc.y + rc.height) > (y + height)) ? (rc.y + rc.height) : (y + height);
624 width = pts[1].x - pts[0].x;
625 height = pts[1].y - pts[0].y;
632 if (!IsEmpty() && !rc.IsEmpty()
633 && x < rc.x + rc.width && rc.x < x + width
634 && y + height > rc.y && rc.y + rc.height > y)
637 res.x = (x > rc.x) ? x : rc.x;
638 res.y = (y > rc.y) ? y : rc.y;
639 res.width = (x + width < rc.x + rc.width) ?
640 x + width - res.x : rc.x + rc.width - res.x;
641 res.height = (y + height < rc.y + rc.height) ?
642 y + height - res.y : rc.y + rc.height - res.y;
655 x = src_r.x, y = src_r.y, width = src_r.width, height = src_r.height;
660 bool operator==(
const SDKRectD2D& r)
const noexcept {
return IsEqual(r); }
661 bool operator!=(
const SDKRectD2D& r)
const noexcept {
return !IsEqual(r); }
670 bool IsIntersected(
const SDKRectD2D& src)
const noexcept {
671 return !(IsEmpty() || (src.width == 0) || (src.height == 0) ||
672 (x >= (src.x + src.width)) || (src.x >= (x + width)) ||
673 (y >= (src.y + src.height)) || (src.y >= y + height));
677 bool IsIntersected_Geometrical(
const SDKRectD2D& src)
const noexcept {
678 return !((width == 0) || (height == 0) ||
679 (src.width == 0) || (src.height == 0) ||
680 (x >= (src.x + src.width)) || (src.x >= (x + width)) ||
681 (y >= (src.y + src.height)) || (src.y >= y + height));
685 bool IsIntersected_Graphical(
const SDKRectD2D& src)
const noexcept {
686 return !((x > (src.x + src.width)) || (src.x > (x + width)) ||
687 (y > (src.y + src.height)) || (src.y > y + height));
690 bool IsIncludes(
const SDKRectD2D& src)
const noexcept {
691 return (IsPointInRect(src.x, src.y) &&
692 IsPointInRect(src.x + src.width, src.y + src.height));
695 bool IsPointInRect(
double p_x,
double p_y)
const noexcept {
698 return (p_x >= x && p_x <= (x + width) &&
699 p_y >= y && p_y <= (y + height));
702 bool IsPointInRect(
const SDKPointD2D& p)
const noexcept {
703 return IsPointInRect(p.x, p.y);
708 double expand_x = width * factor;
709 double expand_y = height * factor;
712 width += 2 * expand_x;
713 height += 2 * expand_y;
720 static_cast<float>(x),
721 static_cast<float>(y),
722 static_cast<float>(width),
723 static_cast<float>(height));
727 typedef std::vector<RectD2D> container;
736 RectI2D() noexcept { x = y = width = height = 0; }
743 SDKInt32 init_x, SDKInt32 init_y,
744 SDKUInt32 init_w, SDKUInt32 init_h) noexcept {
745 x = init_x, y = init_y, width = init_w, height = init_h;
749 bool IsEmpty()
const noexcept {
return (width == 0 && height == 0); }
751 bool IsNull()
const noexcept {
752 return (x == 0 && y == 0 && width == 0 && height == 0);
756 bool IsEqual(
const SDKRect2D& r)
const noexcept {
757 return (x == r.x && y == r.y && width == r.width && height == r.height);
761 void operator=(
const SDKRectI2D& src_r) noexcept {
762 x = src_r.x, y = src_r.y, width = src_r.width, height = src_r.height;
765 bool operator==(
const SDKRect2D& r)
const noexcept {
return IsEqual(r); }
766 bool operator!=(
const SDKRect2D& r)
const noexcept {
return !IsEqual(r); }
774 x = point_type::coordinate_type(src_r.x);
775 y = point_type::coordinate_type(src_r.y);
776 width = size_type::coordinate_type(src_r.width);
777 height = size_type::coordinate_type(src_r.height);
781 x = point_type::coordinate_type(src_r.x);
782 y = point_type::coordinate_type(src_r.y);
783 width = size_type::coordinate_type(src_r.width);
784 height = size_type::coordinate_type(src_r.height);
788 typedef std::vector<RectI2D> container;
798 rect.x = rect.y = rect.width = rect.height = rad_x = rad_y = 0.0f;
805 rad_x = init_rad_x, rad_y = init_rad_y;
813 typedef std::vector<RoundRectF2D> container;
822 EllipseF2D() noexcept { center.x = center.y = rad_x = rad_y = 0.0f; }
828 rad_x = init_rad_x, rad_y = init_rad_y;
836 typedef std::vector<EllipseF2D> container;
845 QBezierSegment() noexcept { pt1.x = pt1.y = pt2.x = pt2.y = 0.0f; }
854 QBezierSegment(
float x_c,
float y_c,
float x_end,
float y_end) noexcept {
855 pt1.x = x_c, pt1.y = y_c;
856 pt2.x = x_end, pt2.y = y_end;
864 typedef std::vector<QBezierSegment> container;
874 pt1.x = pt1.y = pt2.x = pt2.y = pt3.x = pt3.y = 0.0f;
887 float x_end,
float y_end) noexcept {
888 pt1.x = x_c1, pt1.y = y_c1;
889 pt2.x = x_c2, pt2.y = y_c2;
890 pt3.x = x_end, pt3.y = y_end;
898 typedef std::vector<CuBezierSegment> container;
903 GeoPoint() noexcept {
908 GeoPoint(
double _x,
double _y) noexcept {
913 GeoPoint(
const GeoPoint& src) noexcept {
936 bool IsEqual(
const GeoPoint& pt)
const noexcept {
937 if (IsValid() != pt.IsValid())
943 if (fabs(x - pt.x) >= FLT_EPSILON)
945 if (fabs(y - pt.y) >= FLT_EPSILON)
952 GeoPoint& operator=(
const SDKGeoPoint& src) noexcept {
957 bool operator==(
const GeoPoint& pt)
const noexcept {
return IsEqual(pt); }
958 bool operator!=(
const GeoPoint& pt)
const noexcept {
return !IsEqual(pt); }
960 typedef std::vector<GeoPoint> container;
964 GeoPoint(SDKInt32 x, SDKInt32 y) {
968 template <
typename _GeoDistance,
typename _GeoCoordinate>
969 static inline _GeoDistance GeoLonDistance(_GeoCoordinate lon1, _GeoCoordinate lon2);
971 static inline GeoIntDistance GeoIntLonDistance(GeoInt lon1, GeoInt lon2) {
975 return GeoIntDistance(lon2 - lon1);
978 static inline GeoIntDistance GeoLonDistance(GeoInt lon1, GeoInt lon2) {
979 return GeoIntLonDistance(lon1, lon2);
982 static inline double GeoLonDistance(
double lon1,
double lon2) {
991 static inline double GeoLonDistanceMin(
double lon1,
double lon2)
993 double dist1 = GeoLonDistance(lon1, lon2);
994 double dist2 = GeoLonDistance(lon2, lon1);
995 return (dist1 <= dist2) ? dist1 : dist2;
998 static inline GeoIntDistance GeoIntLonComplimentDistance(GeoIntDistance lon_distance) {
1000 return GeoIntDistance(-GeoInt(lon_distance));
1003 static inline GeoIntDistance GeoLonComplimentDistance(GeoIntDistance lon_distance) {
1004 return GeoIntLonComplimentDistance(lon_distance);
1007 static inline double GeoLonComplimentDistance(
double lon_distance) {
1011 static inline GeoInt GeoIntLonShift(GeoInt lon, GeoInt shift) {
1015 static inline GeoInt GeoLonShift(GeoInt lon, GeoInt shift) {
1016 return GeoIntLonShift(lon, shift);
1019 static inline double GeoLonShift(
double lon,
double shift) {
1031 static inline bool IsGeoIntLonInside(GeoInt lon, GeoInt lon1, GeoInt lon2) {
1036 return GeoIntDistance(lon - lon1) <= GeoIntDistance(lon2 - lon1);
1039 static inline bool IsGeoLonInside(GeoInt lon, GeoInt lon1, GeoInt lon2) {
1040 return IsGeoIntLonInside(lon, lon1, lon2);
1044 static inline bool IsGeoLonInsideR(
double lon,
double lon1,
double lon2) {
1046 return ((lon1 <= lon) && (lon <= lon2));
1047 return ((
kDegLonMin <= lon) && (lon <= lon2)) ||
1051 static inline bool IsGeoLonInside(
double lon,
double lon1,
double lon2) {
1060 return IsGeoLonInsideR(lon, lon1, lon2) || IsGeoLonInsideR(-lon, lon1, lon2);
1061 return IsGeoLonInsideR(lon, lon1, lon2);
1064 static inline GeoIntDistance GeoIntLatDistance(GeoInt lat1, GeoInt lat2) {
1065 return (lat1 <= lat2) ? lat2 - lat1 : lat1 - lat2;
1068 static inline GeoIntDistance GeoLatDistance(GeoInt lat1, GeoInt lat2) {
1070 return abs(lat2 - lat1);
1073 static inline double GeoLatDistance(
double lat1,
double lat2) {
1076 return abs(lat2 - lat1);
1079 static inline GeoInt GeoIntLatShift(GeoInt lat, GeoInt shift) {
1081 return (GeoIntLatDistance(lat,
kGeoIntLatMax) > GeoIntDistance(shift))
1084 return (GeoIntLatDistance(
kGeoIntLatMin, lat) > GeoIntDistance(-shift))
1088 static inline GeoInt GeoLatShift(GeoInt lat, GeoInt shift) {
1089 return GeoIntLatShift(lat, shift);
1092 static inline double GeoLatShift(
double lat,
const double& shift) {
1093 double new_lat = lat + shift;
1101 static inline bool IsGeoIntLatInside(GeoInt lat, GeoInt lat1, GeoInt lat2) {
1103 return ((lat >= lat1) && (lat <= lat2));
1105 return ((lat >= lat2) && (lat <= lat1));
1108 static inline bool IsGeoLatInside(GeoInt lat, GeoInt lat1, GeoInt lat2) {
1109 return IsGeoIntLatInside(lat, lat1, lat2);
1112 static inline bool IsGeoLatInside(
double lat,
double lat1,
double lat2) {
1114 return ((lat >= lat1) && (lat <= lat2));
1116 return ((lat >= lat2) && (lat <= lat1));
1119 static inline double NormalizeGeoLon(
double lon) {
1133 static inline double NormalizeGeoLat(
double lat) {
1143 GeoIntPoint() noexcept {
1146 GeoIntPoint(GeoInt _x, GeoInt _y) noexcept {
1150 x = src.x; y = src.y;
1152 GeoIntPoint(
const GeoIntPoint& src) noexcept {
1153 x = src.x; y = src.y;
1156 void operator=(
const GeoIntPoint& src) noexcept {
1157 x = src.x; y = src.y;
1160 typedef std::vector<GeoIntPoint> container;
1165 GeoIntPoint3D() noexcept {
1168 GeoIntPoint3D(GeoInt _x, GeoInt _y,
double _z) noexcept {
1169 x = _x; y = _y; z = _z;
1171 GeoIntPoint3D(
const GeoIntPoint3D& src) noexcept {
1172 x = src.x; y = src.y; z = src.z;
1175 void operator=(
const GeoIntPoint3D& src) noexcept {
1176 x = src.x; y = src.y; z = src.z;
1179 typedef std::vector<GeoIntPoint3D> container;
1187 GeoPoint3D(
double _x,
double _y,
double _z) {
1188 x = _x; y = _y; z = _z;
1190 GeoPoint3D(
const GeoPoint3D& src) {
1191 x = src.x; y = src.y; z = src.z;
1194 void operator=(
const GeoPoint3D& src) {
1195 x = src.x; y = src.y; z = src.z;
1198 typedef std::vector<GeoPoint3D> container;
1203 GeoIntRect() noexcept {
1204 sw.x = sw.y = ne.x = ne.y = 0;
1207 GeoIntRect(
const GeoIntPoint& _sw,
const GeoIntPoint& _ne) noexcept {
1211 GeoIntRect(GeoInt sw_lon, GeoInt sw_lat, GeoInt ne_lon, GeoInt ne_lat
1213 SetRect(sw_lon, sw_lat, ne_lon, ne_lat);
1217 SetRect(src.sw.lon, src.sw.lat, src.ne.lon, src.ne.lat);
1226 bool operator&(
const GeoIntRect& src) noexcept {
1227 return IsIntersected(*
this, src);
1230 void SetRect(GeoInt sw_lon, GeoInt sw_lat, GeoInt ne_lon, GeoInt ne_lat
1232 sw.lat = sw_lat; sw.lon = sw_lon; ne.lat = ne_lat; ne.lon = ne_lon;
1235 bool IsNormalized() const noexcept {
1236 return (sw.lon <= ne.lon) && (sw.lat <= ne.lat);
1240 bool IsRectEmpty() const noexcept {
1241 return ((min.x == max.x) || (min.y == max.y)) ? true :
false;
1245 bool IsRectNull() const noexcept {
1246 return (min.x == 0 && min.y == 0 &&
1247 max.x == 0 && max.y == 0) ? true :
false;
1252 SDKUInt32 Normalize(GeoIntRect (&rects)[2])
const noexcept {
1254 SDKUInt32 Normalize(GeoIntRect* rects)
const noexcept {
1256 if (ne.lat < sw.lat) {
1259 if (sw.lon <= ne.lon) {
1263 rects[0] = GeoIntRect(sw.lon, sw.lat,
kGeoIntLonMax, ne.lat);
1264 rects[1] = GeoIntRect(
kGeoIntLonMin, sw.lat, ne.lon, ne.lat);
1269 return IsIntersected(*
this, r);
1272 static bool IsIntersected(
1276 if (r1.sw.lon > r1.ne.lon)
1277 return IsIntersected(r2,
1279 || IsIntersected(r2,
1280 GeoIntRect(
kGeoIntLonMin, r1.sw.lat, r1.ne.lon, r1.ne.lat));
1282 if (r2.sw.lon > r2.ne.lon)
1283 return IsIntersected(r2, r1);
1285 return ((r1.sw.lon <= r2.ne.lon && r1.ne.lon >= r2.sw.lon) &&
1286 (r1.sw.lat <= r2.ne.lat && r1.ne.lat >= r2.sw.lat));
1292 GeoIntRect res = r1;
1299 GeoIntRect& r1 = *
this;
1300 const GeoIntRect& r2 =
static_cast<const SDKGeoIntRect&
>(_r2);
1302 if (r1.IsRectNull())
1303 return (*
this = r2), *
this;
1304 if (r2.IsRectNull())
1307 if (r1.ne.lat < r1.sw.lat || r2.ne.lat < r2.sw.lat) {
1308 this->~GeoIntRect();
1309 new (
this) GeoIntRect();
1314 GeoInt sw_lon = 0, ne_lon = 0;
1315 if ((r1.sw.lon == r2.sw.lon) && (r1.ne.lon == r2.ne.lon)) {
1316 sw_lon = r1.sw.lon, ne_lon = r1.ne.lon;
1318 bool is_r1_lon_tail_in_r2 = IsGeoIntLonInside(r1.ne.lon, r2.sw.lon, r2.ne.lon);
1319 bool is_r2_lon_tail_in_r1 = IsGeoIntLonInside(r2.ne.lon, r1.sw.lon, r1.ne.lon);
1320 bool is_r1_lon_nose_in_r2 = IsGeoIntLonInside(r1.sw.lon, r2.sw.lon, r2.ne.lon);
1321 bool is_r2_lon_nose_in_r1 = IsGeoIntLonInside(r2.sw.lon, r1.sw.lon, r1.ne.lon);
1322 if (is_r1_lon_tail_in_r2 && is_r2_lon_tail_in_r1 &&
1323 is_r1_lon_nose_in_r2 && is_r2_lon_nose_in_r1)
1325 else if (is_r1_lon_nose_in_r2 && is_r1_lon_tail_in_r2)
1326 sw_lon = r2.sw.lon, ne_lon = r2.ne.lon;
1327 else if (is_r2_lon_nose_in_r1 && is_r2_lon_tail_in_r1)
1328 sw_lon = r1.sw.lon, ne_lon = r1.ne.lon;
1329 else if (is_r1_lon_tail_in_r2)
1330 sw_lon = r1.sw.lon, ne_lon = r2.ne.lon;
1331 else if (is_r2_lon_tail_in_r1)
1332 sw_lon = r2.sw.lon, ne_lon = r1.ne.lon;
1334 if (GeoIntLonDistance(r1.ne.lon, r2.sw.lon) < GeoIntLonDistance(r2.ne.lon, r1.sw.lon))
1335 sw_lon = r1.sw.lon, ne_lon = r2.ne.lon;
1337 sw_lon = r2.sw.lon, ne_lon = r1.ne.lon;
1344 GeoInt sw_lat = (r1.sw.lat <= r2.sw.lat) ? r1.sw.lat : r2.sw.lat;
1345 GeoInt ne_lat = (r1.ne.lat >= r2.ne.lat) ? r1.ne.lat : r2.ne.lat;
1347 GeoInt sw_lat = 0, ne_lat = 0;
1348 if ((r1.sw.lat == r2.sw.lat) && (r1.ne.lat == r2.ne.lat)) {
1349 sw_lat = r1.sw.lat, ne_lat = r1.ne.lat;
1351 bool is_r1_lat_tail_in_r2 = IsGeoIntLatInside(r1.ne.lat, r2.sw.lat, r2.ne.lat);
1352 bool is_r2_lat_tail_in_r1 = IsGeoIntLatInside(r2.ne.lat, r1.sw.lat, r1.ne.lat);
1353 bool is_r1_lat_nose_in_r2 = IsGeoIntLatInside(r1.sw.lat, r2.sw.lat, r2.ne.lat);
1354 bool is_r2_lat_nose_in_r1 = IsGeoIntLatInside(r2.sw.lat, r1.sw.lat, r1.ne.lat);
1355 if (is_r1_lat_tail_in_r2 && is_r2_lat_tail_in_r1 &&
1356 is_r1_lat_nose_in_r2 && is_r2_lat_nose_in_r1)
1358 else if (is_r1_lat_nose_in_r2 && is_r1_lat_tail_in_r2)
1359 sw_lat = r2.sw.lat, ne_lat = r2.ne.lat;
1360 else if (is_r2_lat_nose_in_r1 && is_r2_lat_tail_in_r1)
1361 sw_lat = r1.sw.lat, ne_lat = r1.ne.lat;
1362 else if (is_r1_lat_tail_in_r2)
1363 sw_lat = r1.sw.lat, ne_lat = r2.ne.lat;
1364 else if (is_r2_lat_tail_in_r1)
1365 sw_lat = r2.sw.lat, ne_lat = r1.ne.lat;
1367 if (GeoIntLatDistance(r1.ne.lat, r2.sw.lat) < GeoIntLatDistance(r2.ne.lat, r1.sw.lat))
1368 sw_lat = r1.sw.lat, ne_lat = r2.ne.lat;
1370 sw_lat = r2.sw.lat, ne_lat = r1.ne.lat;
1382 bool PointInRect(
const GeoIntPoint& p)
const noexcept {
1383 return PointInRect(p, *
this);
1386 static bool PointInRect(
1390 if (GeoIntDistance(p.lon - r.sw.lon)
1391 > GeoIntDistance(r.ne.lon - r.sw.lon))
1393 return r.sw.lat <= p.lat && p.lat <= r.ne.lat;
1397 if (r.ne.lat < r.sw.lat)
1399 if (r.sw.lon > r.ne.lon)
1400 return PointInRect(p,
1404 return (p.lon >= r.sw.lon && p.lon <= r.ne.lon &&
1405 p.lat >= r.sw.lat && p.lat <= r.ne.lat);
1409 GeoIntPoint Center() const noexcept {
1412 GeoInt(sw.lon + (GeoIntDistance(ne.lon - sw.lon) >> 1)),
1413 (sw.lat + ne.lat) / 2);
1425 double center_x = sw_x + ((ne_x - sw_x) / 2);
1426 while(center_x >= 360.0)
1435 center.y = (sw.y + ne.y) / 2;
1440 typedef std::vector<GeoIntRect> container;
1443 inline bool operator==(
const GeoIntRect& r1,
const GeoIntRect& r2) {
1444 return (r1.sw.lon == r2.sw.lon) && (r1.sw.lat == r2.sw.lat) &&
1445 (r1.ne.lon == r2.ne.lon) && (r1.ne.lat == r2.ne.lat);
1449 inline bool operator==(
const GeoPointRect& r1,
const GeoPointRect& r2);
1453 GeoPointRect() noexcept { sw.lat = sw.lon = ne.lat = ne.lon = 0.0; }
1454 GeoPointRect(
const GeoPoint& _sw,
const GeoPoint& _ne) noexcept {
1458 GeoPointRect(
double sw_lon,
double sw_lat,
double ne_lon,
double ne_lat
1460 SetRect(sw_lon, sw_lat, ne_lon, ne_lat);
1464 sw.lon = src.sw.lon; sw.lat = src.sw.lat;
1465 ne.lon = src.ne.lon; ne.lat = src.ne.lat; }
1473 void SetRect(
double sw_lon,
double sw_lat,
double ne_lon,
double ne_lat
1475 sw.lat = sw_lat; sw.lon = sw_lon; ne.lat = ne_lat; ne.lon = ne_lon;
1479 bool IsRectEmpty() const noexcept {
1480 return ((min.x == max.x) || (min.y == max.y));
1484 bool IsRectNull() const noexcept {
1485 return (min.x == 0.0 && min.y == 0.0 &&
1486 max.x == 0.0 && max.y == 0.0);
1491 SDKUInt32 Normalize(GeoPointRect (&rects)[2])
const noexcept {
1493 SDKUInt32 Normalize(GeoPointRect* rects)
const noexcept {
1495 if (ne.lat < sw.lat) {
1499 if (sw.lon <= ne.lon) {
1504 rects[0] = GeoPointRect(sw.lon, sw.lat,
kDegLonMax, ne.lat);
1505 rects[1] = GeoPointRect(
kDegLonMin, sw.lat, ne.lon, ne.lat);
1510 return IsIntersected(*
this, r);
1516 return ((r1.sw.lon <= r2.ne.lon && r1.ne.lon >= r2.sw.lon) &&
1517 (r1.sw.lat <= r2.ne.lat && r1.ne.lat >= r2.sw.lat));
1524 return GeoPointRect(r.sw.lon, r.sw.lat,
kDegLonMax, r.ne.lat); };
1526 return GeoPointRect(
kDegLonMin, r.sw.lat, r.ne.lon, r.ne.lat); };
1528 if ((r1.sw.lon > r1.ne.lon) &&
1529 (r2.sw.lon > r2.ne.lon))
1531 return IsIntersectedSimple(right_rect(r1), right_rect(r2))
1532 || IsIntersectedSimple(right_rect(r1), left_rect(r2))
1533 || IsIntersectedSimple(left_rect(r1), right_rect(r2))
1534 || IsIntersectedSimple(left_rect(r1), left_rect(r2));
1537 if (r1.sw.lon > r1.ne.lon)
1539 return IsIntersectedSimple(right_rect(r1), r2)
1540 || IsIntersectedSimple(left_rect(r1), r2);
1543 if (r2.sw.lon > r2.ne.lon)
1545 return IsIntersectedSimple(r1, right_rect(r2))
1546 || IsIntersectedSimple(r1, left_rect(r2));
1549 return IsIntersectedSimple(r1, r2);
1552 static bool PointInRect(
const GeoPoint& p,
1553 const GeoPointRect& r) noexcept {
1557 if (r.sw.lon > r.ne.lon)
1558 return PointInRect(p,
1559 GeoPointRect(r.sw.lon, r.sw.lat,
kDegLonMax, r.ne.lat))
1561 GeoPointRect(
kDegLonMin, r.sw.lat, r.ne.lon, r.ne.lat));
1562 return (p.lon >= r.sw.lon && p.lon <= r.ne.lon &&
1563 p.lat >= r.sw.lat && p.lat <= r.ne.lat);
1567 static bool RectInRect(
const GeoPointRect& r1,
1568 const GeoPointRect& r2) noexcept {
1570 GeoPointRect r = GeoPointRect::Union(r1, r2);
1574 static GeoPointRect Union(
1578 GeoPointRect res(r1);
1585 GeoPointRect& r1 = *
this;
1586 const GeoPointRect& r2 =
static_cast<const GeoPointRect&
>(r);
1587 if (r1.IsRectNull())
1589 if (r2.IsRectNull())
1591 if (r1.ne.lat < r1.sw.lat || r2.ne.lat < r2.sw.lat) {
1592 this->~GeoPointRect();
1593 new (
this) GeoPointRect();
1598 double sw_lon = 0, ne_lon = 0;
1599 if ((r1.sw.lon == r2.sw.lon) && (r1.ne.lon == r2.ne.lon)) {
1600 sw_lon = r1.sw.lon, ne_lon = r1.ne.lon;
1602 bool is_r1_lon_tail_in_r2 = IsGeoLonInside(r1.ne.lon, r2.sw.lon, r2.ne.lon);
1603 bool is_r2_lon_tail_in_r1 = IsGeoLonInside(r2.ne.lon, r1.sw.lon, r1.ne.lon);
1604 bool is_r1_lon_nose_in_r2 = IsGeoLonInside(r1.sw.lon, r2.sw.lon, r2.ne.lon);
1605 bool is_r2_lon_nose_in_r1 = IsGeoLonInside(r2.sw.lon, r1.sw.lon, r1.ne.lon);
1606 if (is_r1_lon_tail_in_r2 && is_r2_lon_tail_in_r1 &&
1607 is_r1_lon_nose_in_r2 && is_r2_lon_nose_in_r1)
1609 else if (is_r1_lon_nose_in_r2 && is_r1_lon_tail_in_r2)
1610 sw_lon = r2.sw.lon, ne_lon = r2.ne.lon;
1611 else if (is_r2_lon_nose_in_r1 && is_r2_lon_tail_in_r1)
1612 sw_lon = r1.sw.lon, ne_lon = r1.ne.lon;
1613 else if (is_r1_lon_tail_in_r2)
1614 sw_lon = r1.sw.lon, ne_lon = r2.ne.lon;
1615 else if (is_r2_lon_tail_in_r1)
1616 sw_lon = r2.sw.lon, ne_lon = r1.ne.lon;
1618 if (GeoLonDistance(r1.ne.lon, r2.sw.lon) < GeoLonDistance(r2.ne.lon, r1.sw.lon))
1619 sw_lon = r1.sw.lon, ne_lon = r2.ne.lon;
1621 sw_lon = r2.sw.lon, ne_lon = r1.ne.lon;
1624 double sw_lat = r1.sw.lat < r2.sw.lat ? r1.sw.lat : r2.sw.lat;
1625 double ne_lat = r1.ne.lat > r2.ne.lat ? r1.ne.lat : r2.ne.lat;
1635 sdk::GeoPoint Center() const noexcept {
1636 sdk::GeoPoint center;
1645 double center_x = sw_x + ((ne_x - sw_x) / 2);
1649 center.x = center_x;
1655 center.y = (sw.y + ne.y) / 2;
1660 typedef std::vector<GeoPointRect> container;
1663 inline bool operator==(
const GeoPointRect& r1,
const GeoPointRect& r2) {
1664 return (r1.sw.lon == r2.sw.lon) && (r1.sw.lat == r2.sw.lat) &&
1665 (r1.ne.lon == r2.ne.lon) && (r1.ne.lat == r2.ne.lat);
1668 inline GeoPointRect RectD2DToGeoPointRect(
const RectD2D& geo_rect) {
1669 return GeoPointRect(geo_rect.x, geo_rect.y,
1670 GeoLonShift(geo_rect.x, geo_rect.width),
1671 GeoLatShift(geo_rect.y, geo_rect.height));
1674 inline GeoPointRect GeoPointRectFromRectD2D(
const RectD2D& geo_rect) {
1675 return RectD2DToGeoPointRect(geo_rect);
1678 inline RectD2D GeoPointRectToRectD2D(
const GeoPointRect& geo_rect) {
1679 return RectD2D(geo_rect.sw.x, geo_rect.sw.y,
1680 GeoLonDistance(geo_rect.sw.x, geo_rect.ne.x),
1681 GeoLatDistance(geo_rect.sw.y, geo_rect.ne.y));
1684 inline RectD2D RectD2DFromGeoPointRect(
const GeoPointRect& geo_rect) {
1685 return GeoPointRectToRectD2D(geo_rect);
1688 inline GeoIntRect GeoPointRectToGeoIntRect(
const GeoPointRect& geo_rect) {
1694 inline GeoIntRect GeoIntRectFromGeoPointRect(
const GeoPointRect& geo_rect) {
1695 return GeoPointRectToGeoIntRect(geo_rect);
1698 inline GeoPointRect GeoIntRectToGeoPointRect(
const GeoIntRect& geo_rect) {
1699 return GeoPointRect(
1704 inline GeoPointRect GeoPointRectFromGeoIntRect(
const GeoIntRect& geo_rect) {
1705 return GeoIntRectToGeoPointRect(geo_rect);
1710 #endif // SDK_GEOMETRY_TYPES_HELPERS_H_