Easy3D 2.5.3
segment.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_SEGMENT_H
28#define EASY3D_CORE_SEGMENT_H
29
30
31#include <algorithm>
32
33#include <easy3d/core/vec.h>
34#include <easy3d/core/line.h>
35
36
37namespace easy3d {
38
39
42 template <int DIM, typename FT>
44 public:
45 typedef Vec<DIM, FT> Point;
46 typedef Vec<DIM, FT> Vector;
49
50 public:
52 GenericSegment(const Point& s, const Point& t);
53 GenericSegment() = default;
54
56 const Point& source() const { return s_; }
58 const Point& target() const { return t_; }
60 void set_source(const Point& s) { s_ = s; }
62 void set_target(const Point& t) { t_ = t; }
63
65 Line supporting_line() const { return Line::from_two_points(s_, t_); }
66
68 Vector to_vector() const { return t_ - s_; }
69
71 Point projection(const Point &p) const;
72
74 bool projected_inside(const Point& p) const;
75
81 FT squared_distance(const Point &p) const;
82
83 private:
84 Point s_;
85 Point t_;
86 };
87
88
89
90 template<int DIM, typename FT> inline
91 GenericSegment<DIM, FT>::GenericSegment(const Point& s, const Point& t) : s_(s), t_(t) {
92 DLOG_IF(distance2(s, t) < 1e-15, ERROR) << "degenerate segment constructed from 2 points:"
93 << "\t(" << s << ")"
94 << "\t(" << t << ")";
95 }
96
97
98 template<int DIM, typename FT> inline
100 Vector dir = normalize(t_ - s_);
101 return (s_ + dir * dot(p - s_, dir));
102 }
103
104
105 template<int DIM, typename FT> inline
107 return (dot(s_ - p, t_ - p) < 0);
108 }
109
110
111 template<int DIM, typename FT> inline
113 if (projected_inside(p))
114 return distance2(projection(p), p);
115 else {
116 FT ds = distance2(s_, p);
117 FT dt = distance2(t_, p);
118 return std::min(ds, dt);
119 }
120 }
121
122}
123
124
125#endif // EASY3D_CORE_SEGMENT_H
A generic line representation, which supports both 2D and 3D lines.
Definition: line.h:40
static GenericLine from_two_points(const Point &p, const Point &q)
Constructs a line from two points p and q.
Definition: line.h:50
A generic segmentation representation, which supports both 2D and 3D line segments.
Definition: segment.h:43
FT squared_distance(const Point &p) const
Returns the squared distance of a point p to this segment. The return the value is the min of the fol...
Definition: segment.h:112
Point projection(const Point &p) const
Returns the projection of a point p on the supporting line of this segment.
Definition: segment.h:99
Line supporting_line() const
Returns the supporting line of this line segment.
Definition: segment.h:65
const Point & target() const
Returns the target end point of this line segment.
Definition: segment.h:58
bool projected_inside(const Point &p) const
Tests if the projection of a point p is within the two end points of this segment.
Definition: segment.h:106
Vector to_vector() const
Returns a vector originating from source() and pointing to target().
Definition: segment.h:68
const Point & source() const
Returns the source end point of this line segment.
Definition: segment.h:56
void set_target(const Point &t)
Sets/Changes the target end point of this line segment.
Definition: segment.h:62
void set_source(const Point &s)
Sets/Changes the source end point of this line segment.
Definition: segment.h:60
Definition: collider.cpp:182
T distance2(const Vec< N, T > &v1, const Vec< N, T > &v2)
Computes the squared distance between two vectors/points.
Definition: vec.h:297
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition: matrix.h:1803
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