Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zxing-cpp/core/src/Point.h @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 /* | |
| 2 * Copyright 2020 Axel Waggershauser | |
| 3 */ | |
| 4 // SPDX-License-Identifier: Apache-2.0 | |
| 5 | |
| 6 #pragma once | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <cmath> | |
| 10 | |
| 11 namespace ZXing { | |
| 12 | |
| 13 template <typename T> | |
| 14 struct PointT | |
| 15 { | |
| 16 using value_t = T; | |
| 17 T x = 0, y = 0; | |
| 18 | |
| 19 constexpr PointT() = default; | |
| 20 constexpr PointT(T x, T y) : x(x), y(y) {} | |
| 21 | |
| 22 template <typename U> | |
| 23 constexpr explicit PointT(const PointT<U>& p) : x(static_cast<T>(p.x)), y(static_cast<T>(p.y)) | |
| 24 {} | |
| 25 | |
| 26 template <typename U> | |
| 27 PointT& operator+=(const PointT<U>& b) | |
| 28 { | |
| 29 x += b.x; | |
| 30 y += b.y; | |
| 31 return *this; | |
| 32 } | |
| 33 }; | |
| 34 | |
| 35 template <typename T> | |
| 36 bool operator==(const PointT<T>& a, const PointT<T>& b) | |
| 37 { | |
| 38 return a.x == b.x && a.y == b.y; | |
| 39 } | |
| 40 | |
| 41 template <typename T> | |
| 42 bool operator!=(const PointT<T>& a, const PointT<T>& b) | |
| 43 { | |
| 44 return !(a == b); | |
| 45 } | |
| 46 | |
| 47 template <typename T> | |
| 48 auto operator-(const PointT<T>& a) -> PointT<T> | |
| 49 { | |
| 50 return {-a.x, -a.y}; | |
| 51 } | |
| 52 | |
| 53 template <typename T, typename U> | |
| 54 auto operator+(const PointT<T>& a, const PointT<U>& b) -> PointT<decltype(a.x + b.x)> | |
| 55 { | |
| 56 return {a.x + b.x, a.y + b.y}; | |
| 57 } | |
| 58 | |
| 59 template <typename T, typename U> | |
| 60 auto operator-(const PointT<T>& a, const PointT<U>& b) -> PointT<decltype(a.x - b.x)> | |
| 61 { | |
| 62 return {a.x - b.x, a.y - b.y}; | |
| 63 } | |
| 64 | |
| 65 template <typename T, typename U> | |
| 66 auto operator*(const PointT<T>& a, const PointT<U>& b) -> PointT<decltype(a.x * b.x)> | |
| 67 { | |
| 68 return {a.x * b.x, a.y * b.y}; | |
| 69 } | |
| 70 | |
| 71 template <typename T, typename U> | |
| 72 PointT<T> operator*(U s, const PointT<T>& a) | |
| 73 { | |
| 74 return {s * a.x, s * a.y}; | |
| 75 } | |
| 76 | |
| 77 template <typename T, typename U> | |
| 78 PointT<T> operator/(const PointT<T>& a, U d) | |
| 79 { | |
| 80 return {a.x / d, a.y / d}; | |
| 81 } | |
| 82 | |
| 83 template <typename T, typename U> | |
| 84 auto dot(const PointT<T>& a, const PointT<U>& b) -> decltype (a.x * b.x) | |
| 85 { | |
| 86 return a.x * b.x + a.y * b.y; | |
| 87 } | |
| 88 | |
| 89 template <typename T> | |
| 90 auto cross(PointT<T> a, PointT<T> b) -> decltype(a.x * b.x) | |
| 91 { | |
| 92 return a.x * b.y - b.x * a.y; | |
| 93 } | |
| 94 | |
| 95 /// L1 norm | |
| 96 template <typename T> | |
| 97 T sumAbsComponent(PointT<T> p) | |
| 98 { | |
| 99 return std::abs(p.x) + std::abs(p.y); | |
| 100 } | |
| 101 | |
| 102 /// L2 norm | |
| 103 template <typename T> | |
| 104 auto length(PointT<T> p) -> decltype(std::sqrt(dot(p, p))) | |
| 105 { | |
| 106 return std::sqrt(dot(p, p)); | |
| 107 } | |
| 108 | |
| 109 /// L-inf norm | |
| 110 template <typename T> | |
| 111 T maxAbsComponent(PointT<T> p) | |
| 112 { | |
| 113 return std::max(std::abs(p.x), std::abs(p.y)); | |
| 114 } | |
| 115 | |
| 116 template <typename T> | |
| 117 auto distance(PointT<T> a, PointT<T> b) -> decltype(length(a - b)) | |
| 118 { | |
| 119 return length(a - b); | |
| 120 } | |
| 121 | |
| 122 using PointI = PointT<int>; | |
| 123 using PointF = PointT<double>; | |
| 124 | |
| 125 /// Calculate a floating point pixel coordinate representing the 'center' of the pixel. | |
| 126 /// This is sort of the inverse operation of the PointI(PointF) conversion constructor. | |
| 127 /// See also the documentation of the GridSampler API. | |
| 128 inline PointF centered(PointI p) | |
| 129 { | |
| 130 return p + PointF(0.5f, 0.5f); | |
| 131 } | |
| 132 | |
| 133 inline PointF centered(PointF p) | |
| 134 { | |
| 135 return {std::floor(p.x) + 0.5f, std::floor(p.y) + 0.5f}; | |
| 136 } | |
| 137 | |
| 138 template <typename T> | |
| 139 PointF normalized(PointT<T> d) | |
| 140 { | |
| 141 return PointF(d) / length(PointF(d)); | |
| 142 } | |
| 143 | |
| 144 template <typename T> | |
| 145 PointT<T> bresenhamDirection(PointT<T> d) | |
| 146 { | |
| 147 return d / maxAbsComponent(d); | |
| 148 } | |
| 149 | |
| 150 template <typename T> | |
| 151 PointT<T> mainDirection(PointT<T> d) | |
| 152 { | |
| 153 return std::abs(d.x) > std::abs(d.y) ? PointT<T>(d.x, 0) : PointT<T>(0, d.y); | |
| 154 } | |
| 155 | |
| 156 | |
| 157 } // ZXing | |
| 158 |
