Fishnet 2.0.1
Framework for Graph-Based Analysis of GIS Vector Data
Loading...
Searching...
No Matches
Ray.hpp
Go to the documentation of this file.
1#pragma once
2#include "Vec2D.hpp"
3#include "Line.hpp"
4
5namespace fishnet::geometry{
6
12template<fishnet::math::Number T>
13class Ray{
14private:
15 const Vec2D<T> originPoint;
16 const Vec2D<T> directionVector;
17public:
18 using numeric_type = T;
19 constexpr static GeometryType type = GeometryType::RAY;
20
21 constexpr static Ray<T> up(Vec2D<T> origin) noexcept {
22 return Ray<T>(origin, Vec2D<T>(0,1));
23 }
24
25 constexpr static Ray<T> down(Vec2D<T> origin) noexcept {
26 return Ray<T>(origin, Vec2D<T>(0,-1));
27 }
28
29 constexpr static Ray<T> right(Vec2D<T> origin) noexcept {
30 return Ray<T>(origin,Vec2D<T>(1,0));
31 }
32
33 constexpr static Ray<T> left(Vec2D<T> origin) noexcept {
34 return Ray<T>(origin,Vec2D<T>(-1,0));
35 }
36
37 constexpr Ray(Vec2D<T> origin, Vec2D<T> direction): originPoint(origin), directionVector(direction){
38 [[unlikely]] if (direction == Vec2D(0,0))
39 throw std::invalid_argument("Zero Vector not valid for the direction of a Ray");
40 }
41
42 template<fishnet::math::Number U, typename = std::enable_if_t<!std::is_same_v<U,T> && std::is_same_v<T,fishnet::math::DEFAULT_NUMERIC>>>
43 constexpr Ray(Vec2D<T> origin, Vec2D<U> direction): Ray(origin,static_cast<Vec2D<T>>(direction)){}
44
45 template<fishnet::math::Number U, typename =std::enable_if_t<!std::is_same_v<U,T> && std::is_same_v<T,fishnet::math::DEFAULT_NUMERIC>>>
46 constexpr Ray(Vec2D<U> origin ,Vec2D<T> direction): Ray(static_cast<Vec2D<T>>(origin),direction){}
47
48 constexpr Vec2D<T> direction() const noexcept {
49 return this->directionVector;
50 }
51
52 constexpr Vec2D<T> origin() const noexcept {
53 return this->originPoint;
54 }
55
56 constexpr Line<T> toLine() const noexcept {
57 return Line<T>(originPoint,originPoint+directionVector);
58 }
59
60 constexpr Ray<T> oppositeRay() const noexcept{
61 return Ray(originPoint, -directionVector);
62 }
63
64 constexpr Ray<T> operator - () const {
65 return oppositeRay();
66 }
67
68 constexpr bool isParallel(LinearGeometry auto const& other) const noexcept{
69 return areParallel(*this,other);
70 }
71
72 constexpr bool intersects(LinearGeometry auto const& other) const noexcept {
73 return intersect(*this,other);
74 }
75
76 constexpr bool contains(IPoint auto const & point) const noexcept{
78 if(point==originPoint)
79 return true;
80 if(directionVector.x == 0){ // vertical ray -> point is on ray if its on the origin or above/below depending on the direction vector
81 return fishnet::math::areEqual(point.x,originPoint.x) and (directionVector.y > 0 ? point.y > originPoint.y : point.y < originPoint.y);
82 }
83 if(directionVector.y == 0){ // horizontal ray -> point is on ray if its on the origin or left/right depending on the direction vector
84 return fishnet::math::areEqual(point.y,originPoint.y) and (directionVector.x > 0 ? point.x > originPoint.x : point.x < originPoint.x);
85 }
86 FLOAT_TYPE lX = FLOAT_TYPE(point.x - originPoint.x) / FLOAT_TYPE(directionVector.x);
87 FLOAT_TYPE lY = FLOAT_TYPE(point.y - originPoint.y) / FLOAT_TYPE(directionVector.y);
88 return fishnet::math::areEqual(lX,lY) and (lX > 0); // lambda values have to equal and positive to be on the correct side of the direction vector
89 }
90
91 template<fishnet::math::Number U>
92 constexpr bool operator==(const Ray<U> & other) const noexcept {
93 return this->originPoint == other.origin() and this->direction().normalize() == other.direction().normalize();
94 }
95
96 constexpr std::optional<Vec2DReal> intersection(LinearGeometry auto const& other) const noexcept {
97 return linearIntersection(*this,other);
98 }
99
100 constexpr size_t hash() const noexcept {
101 return fishnet::math::CantorPairing(originPoint.hash(), directionVector.normalize().hash());
102 }
103
104 constexpr std::string toString() const {
105 return "Ray "+this->originPoint.toString()+" + k * "+this->directionVector.toString();
106 }
107};
108
109// Explicit template instantiation for the default numeric type
110template class Ray<fishnet::math::DEFAULT_NUMERIC>;
111static_assert(IRay<Ray<double>>);
112static_assert(LinearGeometry<Ray<double>>);
113}
Implementation of a line.
Definition Line.hpp:14
Implementation of a ray A ray consist of an origin and the direction of the ray.
Definition Ray.hpp:13
constexpr Ray(Vec2D< U > origin, Vec2D< T > direction)
Definition Ray.hpp:46
constexpr Vec2D< T > direction() const noexcept
Definition Ray.hpp:48
constexpr std::optional< Vec2DReal > intersection(LinearGeometry auto const &other) const noexcept
Definition Ray.hpp:96
constexpr Ray(Vec2D< T > origin, Vec2D< U > direction)
Definition Ray.hpp:43
constexpr Ray< T > oppositeRay() const noexcept
Definition Ray.hpp:60
constexpr Line< T > toLine() const noexcept
Definition Ray.hpp:56
constexpr bool intersects(LinearGeometry auto const &other) const noexcept
Definition Ray.hpp:72
constexpr size_t hash() const noexcept
Definition Ray.hpp:100
constexpr Ray(Vec2D< T > origin, Vec2D< T > direction)
Definition Ray.hpp:37
constexpr Vec2D< T > origin() const noexcept
Definition Ray.hpp:52
T numeric_type
Definition Ray.hpp:18
constexpr bool isParallel(LinearGeometry auto const &other) const noexcept
Definition Ray.hpp:68
static constexpr Ray< T > down(Vec2D< T > origin) noexcept
Definition Ray.hpp:25
constexpr Ray< T > operator-() const
Definition Ray.hpp:64
constexpr bool contains(IPoint auto const &point) const noexcept
Definition Ray.hpp:76
static constexpr Ray< T > right(Vec2D< T > origin) noexcept
Definition Ray.hpp:29
constexpr bool operator==(const Ray< U > &other) const noexcept
Definition Ray.hpp:92
constexpr std::string toString() const
Definition Ray.hpp:104
static constexpr Ray< T > left(Vec2D< T > origin) noexcept
Definition Ray.hpp:33
static constexpr GeometryType type
Definition Ray.hpp:19
static constexpr Ray< T > up(Vec2D< T > origin) noexcept
Definition Ray.hpp:21
Implementation of a two-dimensional vector.
Definition Vec2D.hpp:16
T y
Definition Vec2D.hpp:21
T x
Definition Vec2D.hpp:20
constexpr size_t hash() const noexcept
Definition Vec2D.hpp:179
constexpr std::string toString() const noexcept
Definition Vec2D.hpp:185
constexpr Vec2D< fishnet::math::DEFAULT_NUMERIC > normalize() const
Definition Vec2D.hpp:154
Interface for a point.
Definition IPoint.hpp:92
Definition IGeometry.hpp:13
Definition BoundingBoxWrapper.hpp:5
static constexpr fishnet::Option< Vec2DReal > linearIntersection(ILine auto const &l, ILine auto const &r) noexcept
Computation of line intersection.
Definition LinearIntersection.hpp:15
static constexpr bool areParallel(LinearGeometry auto const &lhs, LinearGeometry auto const &rhs) noexcept
Definition LinearIntersection.hpp:41
static constexpr bool intersect(LinearGeometry auto const &lhs, LinearGeometry auto const &rhs) noexcept
Definition LinearIntersection.hpp:45
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_FLOATING_POINT
Definition NumericConcepts.hpp:12