Easy3D 2.6.1
Loading...
Searching...
No Matches
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
45 template <int DIM, typename FT>
47 public:
52
53 public:
59 GenericSegment(const Point& s, const Point& t);
63 GenericSegment() = default;
64
69 const Point& source() const { return s_; }
74 const Point& target() const { return t_; }
75
80 void set_source(const Point& s) { s_ = s; }
85 void set_target(const Point& t) { t_ = t; }
86
91 Line supporting_line() const { return Line::from_two_points(s_, t_); }
92
97 Vector to_vector() const { return t_ - s_; }
98
104 Point projection(const Point &p) const;
105
111 bool projected_inside(const Point& p) const;
112
121 FT squared_distance(const Point &p) const;
122
123 private:
124 Point s_;
125 Point t_;
126 };
127
128
129
130 template<int DIM, typename FT> inline
131 GenericSegment<DIM, FT>::GenericSegment(const Point& s, const Point& t) : s_(s), t_(t) {
132 DLOG_IF(distance2(s, t) < 1e-15, ERROR) << "degenerate segment constructed from 2 points:"
133 << "\t(" << s << ")"
134 << "\t(" << t << ")";
135 }
136
137
138 template<int DIM, typename FT> inline
140 Vector dir = normalize(t_ - s_);
141 return (s_ + dir * dot(p - s_, dir));
142 }
143
144
145 template<int DIM, typename FT> inline
147 return (dot(s_ - p, t_ - p) < 0);
148 }
149
150
151 template<int DIM, typename FT> inline
153 if (projected_inside(p))
154 return distance2(projection(p), p);
155 else {
156 FT ds = distance2(s_, p);
157 FT dt = distance2(t_, p);
158 return std::min(ds, dt);
159 }
160 }
161
162}
163
164
165#endif // EASY3D_CORE_SEGMENT_H
A generic line representation, which supports both 2D and 3D lines.
Definition line.h:45
static GenericLine from_two_points(const Point &p, const Point &q)
Constructs a line from two points p and q.
Definition line.h:65
FT squared_distance(const Point &p) const
Returns the squared distance of a point p to this segment.
Definition segment.h:152
GenericSegment< DIM, FT > thisclass
The type of this class.
Definition segment.h:51
GenericSegment()=default
Default constructor.
Point projection(const Point &p) const
Returns the projection of a point p on the supporting line of this segment.
Definition segment.h:139
Line supporting_line() const
Returns the supporting line of this line segment.
Definition segment.h:91
GenericLine< DIM, FT > Line
The type of the line.
Definition segment.h:50
const Point & target() const
Returns the target end point of this line segment.
Definition segment.h:74
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:146
GenericSegment(const Point &s, const Point &t)
Constructs a line segment from its two end points s and t.
Definition segment.h:131
Vector to_vector() const
Returns a vector originating from source() and pointing to target().
Definition segment.h:97
const Point & source() const
Returns the source end point of this line segment.
Definition segment.h:69
Vec< DIM, FT > Vector
The type of the vector.
Definition segment.h:49
void set_target(const Point &t)
Sets/Changes the target end point of this line segment.
Definition segment.h:85
Vec< DIM, FT > Point
The type of the point.
Definition segment.h:48
void set_source(const Point &s)
Sets/Changes the source end point of this line segment.
Definition segment.h:80
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 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:1834