Easy3D 2.6.1
Loading...
Searching...
No Matches
line.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_CORE_LINE_H
28#define EASY3D_CORE_LINE_H
29
30#include <easy3d/core/vec.h>
31#include <easy3d/core/constant.h>
32#include <easy3d/util/logging.h>
33
34
35namespace easy3d {
36
37
44 template <int DIM, typename FT>
46 public:
50
51 public:
58 static GenericLine from_point_and_direction(const Point& p, const Vector& dir) { return GenericLine(p, dir); }
65 static GenericLine from_two_points(const Point& p, const Point& q) { return GenericLine(p, q - p); }
66
70 GenericLine() = default;
71
77 void set(const Point& p, const Vector& dir) {
78 p_ = p;
79 dir_ = normalize(dir);
80 }
81
86 const Vector& direction() const { return dir_; }
87
92 const Point& point() const { return p_; }
93
99 Point projection(const Point &p) const { return p_ + dir_ * dot(p - p_, dir_); }
100
106 FT squared_distance(const Point &p) const { return length2(projection(p) - p); }
107
116 bool feet(const thisclass& other, Point& p1, Point& p2) const;
117
118 private: // Ambiguities exist for this one.
119 GenericLine(const Point & p, const Vector & dir);
120
121 private:
122 Point p_;
123 Vector dir_;
124 };
125
126
127 template <int DIM, typename FT>
128 GenericLine<DIM, FT>::GenericLine(const Point & p, const Vector & dir) : p_(p) {
129 dir_ = normalize(dir);
130 LOG_IF(length(dir_) < 1e-15, ERROR)
131 << "degenerate line constructed from point (" << p << ") and direction (" << dir << ")";
132 }
133
134
135 template <int DIM, typename FT>
136 bool GenericLine<DIM, FT>::feet(const thisclass& other, Point& p1, Point& p2) const {
137 // See the derivation: https://www.jianshu.com/p/34a7c4e1f3f5
138 FT a = dot(dir_, other.direction());
139 FT b = dot(dir_, dir_);
140 FT c = dot(other.direction(), other.direction());
141 if (std::abs(a * a - b * c) < epsilon<FT>()) // the two lines are colinear or parallel
142 return false;
143 FT d = dot(other.point() - p_, dir_);
144 FT e = dot(other.point() - p_, other.direction());
145 FT t1 = 0;
146 FT t2 = 0;
147 if (a == 0) {
148 t1 = d / b;
149 t2 = -e / c;
150 }
151 else {
152 t1 = (a * e - c * d) / (a * a - b * c);
153 t2 = b / a * t1 - d / a;
154 }
155 p1 = p_ + t1 * dir_;
156 p2 = other.point() + t2 * other.direction();
157 return true;
158 }
159
160
169 template <int DIM, typename FT>
170 std::ostream& operator<<(std::ostream& os, const GenericLine<DIM, FT>& line) {
171 return os << line.point() << " " << line.direction();
172 }
173
182 template <int DIM, typename FT>
183 std::istream& operator>>(std::istream& is, GenericLine<DIM, FT>& line) {
185 typename GenericLine<DIM, FT>::Vector dir;
186 is >> p >> dir;
187 line.set(p, dir);
188 return is;
189 }
190
191}
192
193#endif // EASY3D_CORE_LINE_H
194
A generic line representation, which supports both 2D and 3D lines.
Definition line.h:45
GenericLine()=default
Default constructor.
FT squared_distance(const Point &p) const
Returns the squared distance of a point p to this line.
Definition line.h:106
static GenericLine from_two_points(const Point &p, const Point &q)
Constructs a line from two points p and q.
Definition line.h:65
void set(const Point &p, const Vector &dir)
Sets a line from a point p and its direction dir.
Definition line.h:77
Point projection(const Point &p) const
Returns the projection of a point p on this line.
Definition line.h:99
const Vector & direction() const
Returns the direction of the line.
Definition line.h:86
GenericLine< DIM, FT > thisclass
The type of this class.
Definition line.h:49
const Point & point() const
Returns an arbitrary point on the line.
Definition line.h:92
Vec< DIM, FT > Vector
The type of the vector.
Definition line.h:48
static GenericLine from_point_and_direction(const Point &p, const Vector &dir)
Constructs a line from a point p and its direction dir.
Definition line.h:58
Vec< DIM, FT > Point
The type of the point.
Definition line.h:47
bool feet(const thisclass &other, Point &p1, Point &p2) const
Computes the perpendicular feet with another line.
Definition line.h:136
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
Definition collider.cpp:182
Vec< N, T > normalize(const Vec< N, T > &v)
Computes and returns the normalized vector (Note: the input vector is not modified).
Definition vec.h:299
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition vec.h:289
FT epsilon()
Function returning the epsilon value for a given type.
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Output stream support for Graph::Vertex.
Definition graph.h:1300
T length2(const Vec< N, T > &v)
Computes the squared length/magnitude of a vector.
Definition vec.h:293
std::istream & operator>>(std::istream &is, GenericLine< DIM, FT > &line)
Input stream support for GenericLine.
Definition line.h:183
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition matrix.h:1834