Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
Vec2D.hpp
Go to the documentation of this file.
1#pragma once
4#include <fishnet/Degrees.hpp>
6#include <fishnet/IPoint.hpp>
7
8namespace fishnet::geometry{
9
15template<fishnet::math::Number T=fishnet::math::DEFAULT_NUMERIC>
16class Vec2D {
17private:
18 constexpr static auto hasher = std::hash<fishnet::math::DEFAULT_NUMERIC>{}; //convert all to double to keep hash consistent with equality function
19public:
20 T x;
21 T y;
22
23 using numeric_type = T;
24 constexpr static GeometryType type = GeometryType::POINT;
25
35 template<fishnet::math::Number U, fishnet::math::Number V>
36 constexpr static auto construct(U x, V y) noexcept{
37 if constexpr(std::is_same_v<U,V>){
38 return Vec2D<U>(x,y);
39 }else if constexpr(std::integral<U> && std::integral<V>){
40 if constexpr(sizeof(U) > sizeof(V)){ // use bigger integral type (U=long, V=int -> Vec2D<long>)
41 return Vec2D<U>(x,y);
42 }else{
43 return Vec2D<V>(x,y);
44 }
45 }else {
46 return Vec2D<fishnet::math::DEFAULT_NUMERIC>(x,y); // use default numeric type in any other case
47 }
48 }
49
50 constexpr Vec2D(T x, T y):x(x),y(y){
51 if (fishnet::math::isZero(x)) // utilize helper function to round floating point values very close to zero.
52 this->x= 0.0;
54 this->y= 0.0;
55 }
56
65 template<fishnet::math::Number U, fishnet::math::Number R, typename = std::enable_if_t<(!std::is_same_v<U,T> || ! std::is_same_v<R,T>)&& std::is_same_v<T,fishnet::math::DEFAULT_NUMERIC>>>
66 constexpr Vec2D(U x, R y):Vec2D(static_cast<T>(x),static_cast<T>(y)){}
67
72 constexpr Vec2D():x(0),y(0){}
73
80 template<fishnet::math::Number U>
81 constexpr operator Vec2D<U> () const noexcept{
82 return Vec2D<U>(static_cast<U>(x),static_cast<U>(y));
83 }
84
85 constexpr T getX() const noexcept{
86 return x;
87 }
88
89 constexpr T getY() const noexcept{
90 return y;
91 }
92
98 constexpr Vec2D<T> operator - () const {
99 return Vec2D<T>(-x,-y);
100 }
101
102 constexpr auto operator+(const IPoint auto & other) const noexcept{
103 return construct(x + other.getX(), y + other.getY());
104 }
105
106 constexpr auto operator-(const IPoint auto & other) const noexcept{
107 return construct(x-other.getX(), y-other.getY());
108 }
109
110 template<fishnet::math::Number U>
111 constexpr auto operator*(U scalar) const noexcept{
112 return construct(x*scalar,y*scalar);
113 }
114
115 constexpr auto operator/(fishnet::math::DEFAULT_NUMERIC scalar) const noexcept{
116 if(scalar==0)
117 scalar=1; // prevent division by zero, return the same Vec2D instead
118 return construct(static_cast<fishnet::math::DEFAULT_NUMERIC>(x)/scalar,static_cast<fishnet::math::DEFAULT_NUMERIC>(y)/scalar);
119 }
120
121 template<fishnet::math::Number U>
122 constexpr bool operator==(const Vec2D<U> & other) const noexcept{
123 return fishnet::math::areEqual(x,other.x) and fishnet::math::areEqual(y,other.y);
124 }
125
126 constexpr auto dot(const IPoint auto & other) const noexcept{
127 return x * other.getX() + y * other.getY();
128 }
129
130 constexpr auto cross(const IPoint auto & other) const noexcept{
131 return x * other.getY() - y * other.getX();
132 }
133
134 constexpr bool isParallel(const IPoint auto & other) const noexcept{
135 return fishnet::math::isZero(cross(other));
136 }
137
138 constexpr bool isOrthogonal(const IPoint auto & other) const noexcept{
139 return fishnet::math::isZero(dot(other));
140 }
141
143 return sqrt(static_cast<fishnet::math::DEFAULT_FLOATING_POINT>(dot(*this)));
144 }
145
146 constexpr fishnet::math::DEFAULT_FLOATING_POINT distance(const IPoint auto & other) const {
147 return (*this-other).length();
148 }
149
150 constexpr Vec2D<T> orthogonal() const noexcept{
151 return {y,-x};
152 }
153
155 return *this / length();
156 }
157
163 fishnet::math::Radians angle(const IPoint auto & reference) const{
164 auto dir = *this - reference;
165 return fishnet::math::Radians::atan2(dir.y,dir.x);
166 }
167
175 fishnet::math::Radians angle(const IPoint auto & reference, fishnet::math::Radians angleRotate) const{
176 return angle(reference) + angleRotate;
177 }
178
179 constexpr size_t hash() const noexcept {
180 size_t xHash = hasher(this->x);
181 size_t yHash = hasher(this->y);
182 return fishnet::math::CantorPairing(xHash,yHash); // utilize cantor pairing to keep hashes unique: hash(Vec2D(2,1)) != hash(Vec2D(1,2))
183 }
184
185 constexpr std::string toString() const noexcept{
186 return "(" + std::to_string(x) + "," + std::to_string(y) + ")";
187 }
188
189};
190
196 template<fishnet::math::Number T,fishnet::math::Number U>
197 constexpr bool operator()(const Vec2D<T> & lhs, const Vec2D<U> & rhs)const noexcept {
198 if(fishnet::math::areEqual(lhs.x,rhs.x))
199 return lhs.y < rhs.y;
200 return lhs.x < rhs.x;
201 }
202};
203
208 template<fishnet::math::Number T,fishnet::math::Number U>
209 constexpr inline bool operator()(const Vec2D<T> & lhs, const Vec2D<U> & rhs)const noexcept {
210 if(fishnet::math::areEqual(lhs.y,rhs.y))
211 return lhs.x < rhs.x;
212 return lhs.y < rhs.y;
213 }
214};
215
216static_assert(IPoint<Vec2D<double>>);
217static_assert(IPoint<Vec2D<int>>);
218
221
222// Explicit template instantiation for the default numeric type
224}
225
Implementation of a two-dimensional vector.
Definition Vec2D.hpp:16
constexpr auto operator/(fishnet::math::DEFAULT_NUMERIC scalar) const noexcept
Definition Vec2D.hpp:115
T y
Definition Vec2D.hpp:21
static constexpr GeometryType type
Definition Vec2D.hpp:24
constexpr auto operator*(U scalar) const noexcept
Definition Vec2D.hpp:111
constexpr auto cross(const IPoint auto &other) const noexcept
Definition Vec2D.hpp:130
fishnet::math::Radians angle(const IPoint auto &reference) const
Definition Vec2D.hpp:163
constexpr auto operator-(const IPoint auto &other) const noexcept
Definition Vec2D.hpp:106
constexpr bool operator==(const Vec2D< U > &other) const noexcept
Definition Vec2D.hpp:122
constexpr Vec2D()
Default construction to (0,0)
Definition Vec2D.hpp:72
constexpr bool isParallel(const IPoint auto &other) const noexcept
Definition Vec2D.hpp:134
constexpr T getY() const noexcept
Definition Vec2D.hpp:89
T x
Definition Vec2D.hpp:20
static constexpr auto construct(U x, V y) noexcept
Helper factory method to construct the correct type of vector depending on the numeric types.
Definition Vec2D.hpp:36
constexpr fishnet::math::DEFAULT_FLOATING_POINT length() const
Definition Vec2D.hpp:142
constexpr T getX() const noexcept
Definition Vec2D.hpp:85
constexpr Vec2D(U x, R y)
Constructor for heterogenous types Converts to the default numeric type.
Definition Vec2D.hpp:66
constexpr size_t hash() const noexcept
Definition Vec2D.hpp:179
T numeric_type
Definition Vec2D.hpp:23
constexpr auto dot(const IPoint auto &other) const noexcept
Definition Vec2D.hpp:126
constexpr std::string toString() const noexcept
Definition Vec2D.hpp:185
constexpr auto operator+(const IPoint auto &other) const noexcept
Definition Vec2D.hpp:102
constexpr Vec2D(T x, T y)
Definition Vec2D.hpp:50
constexpr Vec2D< T > operator-() const
Negation operator e.g.: -Vec(2,1) => Vec(-2,-1)
Definition Vec2D.hpp:98
constexpr fishnet::math::DEFAULT_FLOATING_POINT distance(const IPoint auto &other) const
Definition Vec2D.hpp:146
constexpr Vec2D< T > orthogonal() const noexcept
Definition Vec2D.hpp:150
constexpr bool isOrthogonal(const IPoint auto &other) const noexcept
Definition Vec2D.hpp:138
fishnet::math::Radians angle(const IPoint auto &reference, fishnet::math::Radians angleRotate) const
Definition Vec2D.hpp:175
constexpr Vec2D< fishnet::math::DEFAULT_NUMERIC > normalize() const
Definition Vec2D.hpp:154
Radians implementation of Angle.
Definition Radians.hpp:11
static Radians atan2(double y, double x)
Definition Radians.cpp:46
Interface for a point.
Definition IPoint.hpp:92
Definition BoundingBoxWrapper.hpp:5
GeometryType
Definition GeometryBase.hpp:6
constexpr bool areEqual(T lhs, U rhs) noexcept
Definition Constants.hpp:31
constexpr size_t CantorPairing(size_t x, size_t y) noexcept
Definition CantorPairing.hpp:5
double DEFAULT_NUMERIC
Definition NumericConcepts.hpp:13
double DEFAULT_FLOATING_POINT
Definition NumericConcepts.hpp:12
constexpr bool isZero(T number) noexcept
Definition Constants.hpp:23
static string to_string(fishnet::geometry::GeometryType type) noexcept
Definition GeometryBase.hpp:22
Comparator for lexigraphically-ordering of Vec2D objects Compares first the x-Coordinate and then the...
Definition Vec2D.hpp:195
constexpr bool operator()(const Vec2D< T > &lhs, const Vec2D< U > &rhs) const noexcept
Definition Vec2D.hpp:197
Comparator for lexigraphically-ordering of Vec2D objects, with y Coordinate first.
Definition Vec2D.hpp:207
constexpr bool operator()(const Vec2D< T > &lhs, const Vec2D< U > &rhs) const noexcept
Definition Vec2D.hpp:209