/
geomath.h
161 lines (133 loc) · 3.7 KB
/
geomath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/** @file geomath.h
*
* @authors Copyright (c) 2018 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef GLOOM_GEOMATH_H
#define GLOOM_GEOMATH_H
#include <de/Vector>
#include <QVector2D>
namespace gloom { namespace geo {
inline de::Vector2d toVector2d(const QVector2D &vec)
{
return de::Vector2d(vec.x(), vec.y());
}
inline QVector2D toQVector2D(const de::Vector2d &vec)
{
return QVector2D(float(vec.x), float(vec.y));
}
/**
* 2D line.
*/
template <typename T>
struct Line
{
T start;
T end;
Line() {}
Line(const T &span)
: end(span)
{}
Line(const T &a, const T &b)
: start(a)
, end(b)
{}
double length() const
{
return span().length();
}
T span() const
{
return end - start;
}
T dir() const
{
return span().normalize();
}
T normal() const
{
/*const auto dirVec = dir();
return de::Vector3<typename T::ValueType>(dirVec.x, 0.0, dirVec.y)
.cross(de::Vector3<typename T::ValueType>(0, 1, 0))
.xz();*/
const auto vec = dir();
return T(-vec.y, vec.x);
}
void flip()
{
std::swap(start, end);
}
bool isFrontSide(const T &pos) const
{
return normal().dot(pos - start) > 0;
}
bool isBackSide(const T &pos) const
{
return normal().dot(pos - start) < 0;
}
int side(const T &pos) const
{
return isFrontSide(pos)? 0 : 1;
}
bool intersect(const Line &other, double &t) const
{
const auto &p = other.start;
const auto &r = other.span();
const auto &q = start;
const auto s = span();
const double r_s = r.cross(s);
if (de::abs(r_s) < de::EPSILON)
{
return false;
}
t = (q - p).cross(r) / r_s;
// It has to hit somewhere on `other`.
const double u = (q - p).cross(s) / r_s;
return u >= 0 && u <= 1;
}
double distanceTo(const T &p) const
{
const T delta = p - start;
double dist = de::min(delta.length(), (p - end).length());
double n = de::abs(normal().dot(delta));
double t = dir().dot(delta);
if (t >= 0 && t <= length())
{
return de::min(n, dist);
}
return dist;
}
double angle(const Line &other) const
{
// If the lines point to the same direction, the angle is 180 degrees.
const T d1 = dir();
const T n = normal();
const T d2 = other.dir();
double deg = 180 + de::radianToDegree(std::acos(d1.dot(d2)));
if (n.dot(d2) > 0) deg = 360 - deg;
return deg;
//return de::radianToDegree(std::atan2(d1.dot(d2), n.dot(d2)));
}
};
using Line2d = Line<de::Vector2d>;
struct Plane
{
de::Vector3d point;
de::Vector3f normal;
bool isPointAbove(const de::Vector3d &pos) const;
double project(const de::Vector2d &pos) const;
};
}} // namespace gloom::geo
#endif // GLOOM_GEOMATH_H