Open3D (C++ API)  0.15.1
Line3D.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <Eigen/Core>
30 #include <Eigen/Geometry>
31 #include <limits>
32 
36 
37 #pragma once
38 
39 namespace open3d {
40 namespace geometry {
41 
68 class Line3D : protected Eigen::ParametrizedLine<double, 3> {
69 public:
73  static Line3D Through(const Eigen::Vector3d& p0,
74  const Eigen::Vector3d& p1) {
75  return {p0, (p1 - p0).normalized()};
76  }
77 
81  enum class LineType {
83  Line = 0,
84 
87  Ray = 1,
88 
91  Segment = 2,
92  };
93 
95  Line3D(const Eigen::Vector3d& origin, const Eigen::Vector3d& direction);
96 
97  virtual ~Line3D() = default;
98 
100  LineType GetLineType() const { return line_type_; }
101 
103  const Eigen::Vector3d& Origin() const { return m_origin; }
104 
106  const Eigen::Vector3d& Direction() const { return m_direction; }
107 
110  virtual double Length() const {
111  return std::numeric_limits<double>::infinity();
112  }
113 
115  virtual void Transform(const Eigen::Transform<double, 3, Eigen::Affine>& t);
116 
119  const Eigen::ParametrizedLine<double, 3>& Line() const { return *this; }
120 
126  const Eigen::Hyperplane<double, 3>& plane) const;
127 
138  double ProjectionParameter(const Eigen::Vector3d& point) const;
139 
149  virtual Eigen::Vector3d Projection(const Eigen::Vector3d& point) const;
150 
173  const AxisAlignedBoundingBox& box) const;
174 
198  const AxisAlignedBoundingBox& box) const;
199 
203  std::pair<double, double> ClosestParameters(const Line3D& other) const;
204 
208  std::pair<Eigen::Vector3d, Eigen::Vector3d> ClosestPoints(
209  const Line3D& other) const;
210 
214  double DistanceTo(const Line3D& other) const;
215 
221  virtual double ClampParameter(double parameter) const { return parameter; }
222 
227  virtual bool IsParameterValid(double parameter) const { return true; }
228 
229 protected:
232  Line3D(const Eigen::Vector3d& origin,
233  const Eigen::Vector3d& direction,
234  LineType type);
235 
241  std::pair<double, double> SlabAABBBase(
242  const AxisAlignedBoundingBox& box) const;
243 
244 private:
245  const LineType line_type_ = LineType::Line;
246 
247  // Pre-calculated inverse values for the line's direction used to
248  // accelerate the slab method
249  double x_inv_;
250  double y_inv_;
251  double z_inv_;
252 };
253 
259 class Ray3D : public Line3D {
260 public:
263  static Ray3D Through(const Eigen::Vector3d& p0, const Eigen::Vector3d& p1) {
264  return {p0, (p1 - p0).normalized()};
265  }
266 
268  Ray3D(const Eigen::Vector3d& origin, const Eigen::Vector3d& direction);
269 
272  double Length() const override {
273  return std::numeric_limits<double>::infinity();
274  }
275 
282  const Eigen::Hyperplane<double, 3>& plane) const override;
283 
305  const AxisAlignedBoundingBox& box) const override;
306 
312  double ClampParameter(double parameter) const override {
313  return std::max(parameter, 0.);
314  }
315 
320  bool IsParameterValid(double parameter) const override {
321  return parameter >= 0;
322  }
323 };
324 
339 class Segment3D : public Line3D {
340 public:
344  static Segment3D Through(const Eigen::Vector3d& p0,
345  const Eigen::Vector3d& p1) {
346  return {p0, p1};
347  }
348 
351  Segment3D(const Eigen::Vector3d& start_point,
352  const Eigen::Vector3d& end_point);
353 
356  explicit Segment3D(const std::pair<Eigen::Vector3d, Eigen::Vector3d>& pair);
357 
360  double Length() const override { return length_; }
361 
363  Eigen::Vector3d MidPoint() const { return 0.5 * (origin() + end_point_); }
364 
366  const Eigen::Vector3d& EndPoint() const { return end_point_; }
367 
369  void Transform(
370  const Eigen::Transform<double, 3, Eigen::Affine>& t) override;
371 
375 
381  const Eigen::Hyperplane<double, 3>& plane) const override;
382 
406  const AxisAlignedBoundingBox& box) const override;
407 
428  const AxisAlignedBoundingBox& box) const override;
429 
435  double ClampParameter(double parameter) const override {
436  return std::max(std::min(parameter, length_), 0.);
437  }
438 
443  bool IsParameterValid(double parameter) const override {
444  return parameter >= 0 && parameter <= length_;
445  }
446 
447 private:
448  Eigen::Vector3d end_point_;
449  double length_;
450 };
451 
452 } // namespace geometry
453 } // namespace open3d
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:155
Line3D is a class which derives from Eigen::ParametrizedLine<double, 3> in order to capture the seman...
Definition: Line3D.h:68
const Eigen::ParametrizedLine< double, 3 > & Line() const
Returns a const reference to the underlying Eigen::ParametrizedLine object.
Definition: Line3D.h:119
virtual ~Line3D()=default
virtual void Transform(const Eigen::Transform< double, 3, Eigen::Affine > &t)
Transform the Line3D by the given matrix.
Definition: Line3D.cpp:55
virtual double ClampParameter(double parameter) const
Clamps/bounds a parameter value to the closest valid place where the entity exists....
Definition: Line3D.h:221
const Eigen::Vector3d & Direction() const
Gets the line's direction vector.
Definition: Line3D.h:106
LineType
Specifies different semantic interpretations of 3d lines.
Definition: Line3D.h:81
@ Line
Lines extend infinitely in both directions.
std::pair< double, double > SlabAABBBase(const AxisAlignedBoundingBox &box) const
Calculates the common t_min and t_max values of the slab AABB intersection method....
Definition: Line3D.cpp:59
Line3D(const Eigen::Vector3d &origin, const Eigen::Vector3d &direction)
Default user constructor.
Definition: Line3D.cpp:36
const Eigen::Vector3d & Origin() const
Gets the line's origin point.
Definition: Line3D.h:103
virtual utility::optional< double > SlabAABB(const AxisAlignedBoundingBox &box) const
Returns the lower intersection parameter for a line with an axis aligned bounding box or empty if no ...
Definition: Line3D.cpp:155
std::pair< Eigen::Vector3d, Eigen::Vector3d > ClosestPoints(const Line3D &other) const
Computes the two closest points between this Line3D object and the other, including of derived types ...
Definition: Line3D.cpp:279
double ProjectionParameter(const Eigen::Vector3d &point) const
Calculates the parameter of a point projected onto the line taking into account special semantics.
Definition: Line3D.cpp:186
std::pair< double, double > ClosestParameters(const Line3D &other) const
Computes the two corresponding parameters of the closest distance between two Line3D objects,...
Definition: Line3D.cpp:190
LineType GetLineType() const
Gets the semantic type of the line.
Definition: Line3D.h:100
virtual double Length() const
Gets the length of the line, which for lines and rays will return positive infinity,...
Definition: Line3D.h:110
virtual Eigen::Vector3d Projection(const Eigen::Vector3d &point) const
Calculates a point projected onto the line, taking into account special semantics.
Definition: Line3D.cpp:182
virtual utility::optional< double > ExactAABB(const AxisAlignedBoundingBox &box) const
Returns the lower intersection parameter for a line with an axis aligned bounding box or empty if no ...
Definition: Line3D.cpp:91
virtual bool IsParameterValid(double parameter) const
Verifies that a given parameter value is valid for the semantics of the line object....
Definition: Line3D.h:227
double DistanceTo(const Line3D &other) const
Gets the closest distance between two Line3D objects, including derived types Ray3D and Segment3D,...
Definition: Line3D.cpp:289
virtual utility::optional< double > IntersectionParameter(const Eigen::Hyperplane< double, 3 > &plane) const
Calculates the intersection parameter between the line and a plane taking into account line semantics...
Definition: Line3D.cpp:172
static Line3D Through(const Eigen::Vector3d &p0, const Eigen::Vector3d &p1)
Creates a line through two points. The line origin will take the value of p0, and the line direction ...
Definition: Line3D.h:73
A ray is a semantic interpretation of Eigen::ParametrizedLine which has an origin and a direction and...
Definition: Line3D.h:259
double Length() const override
Gets the length of the line, which for lines and rays will return positive infinity,...
Definition: Line3D.h:272
double ClampParameter(double parameter) const override
Clamps/bounds a parameter value to the closest valid place where the entity exists....
Definition: Line3D.h:312
utility::optional< double > SlabAABB(const AxisAlignedBoundingBox &box) const override
Returns the lower intersection parameter for a ray with an axis aligned bounding box or empty if no i...
Definition: Line3D.cpp:303
static Ray3D Through(const Eigen::Vector3d &p0, const Eigen::Vector3d &p1)
Creates a Ray3D through two points. The ray origin will take the value of p0, and the direction will ...
Definition: Line3D.h:263
bool IsParameterValid(double parameter) const override
Verifies that a given parameter value is valid for the semantics of the line object....
Definition: Line3D.h:320
utility::optional< double > IntersectionParameter(const Eigen::Hyperplane< double, 3 > &plane) const override
Calculates the intersection parameter between the line and a plane taking into account ray semantics....
Definition: Line3D.cpp:324
Ray3D(const Eigen::Vector3d &origin, const Eigen::Vector3d &direction)
Default constructor, requires point and direction.
Definition: Line3D.cpp:300
A segment is a semantic interpretation of Eigen::ParametrizedLine which has an origin and an endpoint...
Definition: Line3D.h:339
utility::optional< double > IntersectionParameter(const Eigen::Hyperplane< double, 3 > &plane) const override
Calculates the intersection parameter between the line and a plane taking into account segment semant...
Definition: Line3D.cpp:398
static Segment3D Through(const Eigen::Vector3d &p0, const Eigen::Vector3d &p1)
Creates a Segment3D through two points. The origin will take the value of p0, and the endpoint be p1....
Definition: Line3D.h:344
double ClampParameter(double parameter) const override
Clamps/bounds a parameter value to the closest valid place where the entity exists....
Definition: Line3D.h:435
const Eigen::Vector3d & EndPoint() const
Get the end point of the segment.
Definition: Line3D.h:366
utility::optional< double > SlabAABB(const AxisAlignedBoundingBox &box) const override
Returns the lower intersection parameter for a segment with an axis aligned bounding box or empty if ...
Definition: Line3D.cpp:354
void Transform(const Eigen::Transform< double, 3, Eigen::Affine > &t) override
Transform the segment by the given matrix.
Definition: Line3D.cpp:349
utility::optional< double > ExactAABB(const AxisAlignedBoundingBox &box) const override
Returns the lower intersection parameter for a segment with an axis aligned bounding box or empty if ...
Definition: Line3D.cpp:377
Eigen::Vector3d MidPoint() const
Calculates the midpoint of the segment.
Definition: Line3D.h:363
double Length() const override
Get the scalar length of the segment as the distance between the start point (origin) and the end poi...
Definition: Line3D.h:360
Segment3D(const Eigen::Vector3d &start_point, const Eigen::Vector3d &end_point)
Default constructor for Segment3D takes the start and end points of the segment start_point end_point...
Definition: Line3D.cpp:338
bool IsParameterValid(double parameter) const override
Verifies that a given parameter value is valid for the semantics of the line object....
Definition: Line3D.h:443
AxisAlignedBoundingBox GetBoundingBox() const
Get an axis-aligned bounding box representing the enclosed volume of the line segment.
Definition: Line3D.cpp:387
Definition: Optional.h:278
char type
Definition: FilePCD.cpp:60
Definition: PinholeCameraIntrinsic.cpp:35