Open3D (C++ API)  0.18.0+5c982c7
Line3D.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 #include <Eigen/Geometry>
12 #include <limits>
13 
17 
18 #pragma once
19 
20 namespace open3d {
21 namespace geometry {
22 
49 class Line3D : protected Eigen::ParametrizedLine<double, 3> {
50 public:
54  static Line3D Through(const Eigen::Vector3d& p0,
55  const Eigen::Vector3d& p1) {
56  return {p0, (p1 - p0).normalized()};
57  }
58 
62  enum class LineType {
64  Line = 0,
65 
68  Ray = 1,
69 
72  Segment = 2,
73  };
74 
76  Line3D(const Eigen::Vector3d& origin, const Eigen::Vector3d& direction);
77 
78  virtual ~Line3D() = default;
79 
81  LineType GetLineType() const { return line_type_; }
82 
84  const Eigen::Vector3d& Origin() const { return m_origin; }
85 
87  const Eigen::Vector3d& Direction() const { return m_direction; }
88 
91  virtual double Length() const {
92  return std::numeric_limits<double>::infinity();
93  }
94 
96  virtual void Transform(const Eigen::Transform<double, 3, Eigen::Affine>& t);
97 
100  const Eigen::ParametrizedLine<double, 3>& Line() const { return *this; }
101 
107  const Eigen::Hyperplane<double, 3>& plane) const;
108 
119  double ProjectionParameter(const Eigen::Vector3d& point) const;
120 
130  virtual Eigen::Vector3d Projection(const Eigen::Vector3d& point) const;
131 
154  const AxisAlignedBoundingBox& box) const;
155 
179  const AxisAlignedBoundingBox& box) const;
180 
184  std::pair<double, double> ClosestParameters(const Line3D& other) const;
185 
189  std::pair<Eigen::Vector3d, Eigen::Vector3d> ClosestPoints(
190  const Line3D& other) const;
191 
195  double DistanceTo(const Line3D& other) const;
196 
202  virtual double ClampParameter(double parameter) const { return parameter; }
203 
208  virtual bool IsParameterValid(double parameter) const { return true; }
209 
210 protected:
213  Line3D(const Eigen::Vector3d& origin,
214  const Eigen::Vector3d& direction,
215  LineType type);
216 
222  std::pair<double, double> SlabAABBBase(
223  const AxisAlignedBoundingBox& box) const;
224 
225 private:
226  const LineType line_type_ = LineType::Line;
227 
228  // Pre-calculated inverse values for the line's direction used to
229  // accelerate the slab method
230  double x_inv_;
231  double y_inv_;
232  double z_inv_;
233 };
234 
240 class Ray3D : public Line3D {
241 public:
244  static Ray3D Through(const Eigen::Vector3d& p0, const Eigen::Vector3d& p1) {
245  return {p0, (p1 - p0).normalized()};
246  }
247 
249  Ray3D(const Eigen::Vector3d& origin, const Eigen::Vector3d& direction);
250 
253  double Length() const override {
254  return std::numeric_limits<double>::infinity();
255  }
256 
263  const Eigen::Hyperplane<double, 3>& plane) const override;
264 
286  const AxisAlignedBoundingBox& box) const override;
287 
293  double ClampParameter(double parameter) const override {
294  return std::max(parameter, 0.);
295  }
296 
301  bool IsParameterValid(double parameter) const override {
302  return parameter >= 0;
303  }
304 };
305 
320 class Segment3D : public Line3D {
321 public:
325  static Segment3D Through(const Eigen::Vector3d& p0,
326  const Eigen::Vector3d& p1) {
327  return {p0, p1};
328  }
329 
332  Segment3D(const Eigen::Vector3d& start_point,
333  const Eigen::Vector3d& end_point);
334 
337  explicit Segment3D(const std::pair<Eigen::Vector3d, Eigen::Vector3d>& pair);
338 
341  double Length() const override { return length_; }
342 
344  Eigen::Vector3d MidPoint() const { return 0.5 * (origin() + end_point_); }
345 
347  const Eigen::Vector3d& EndPoint() const { return end_point_; }
348 
350  void Transform(
351  const Eigen::Transform<double, 3, Eigen::Affine>& t) override;
352 
356 
362  const Eigen::Hyperplane<double, 3>& plane) const override;
363 
387  const AxisAlignedBoundingBox& box) const override;
388 
409  const AxisAlignedBoundingBox& box) const override;
410 
416  double ClampParameter(double parameter) const override {
417  return std::max(std::min(parameter, length_), 0.);
418  }
419 
424  bool IsParameterValid(double parameter) const override {
425  return parameter >= 0 && parameter <= length_;
426  }
427 
428 private:
429  Eigen::Vector3d end_point_;
430  double length_;
431 };
432 
433 } // namespace geometry
434 } // namespace open3d
A bounding box that is aligned along the coordinate axes and defined by the min_bound and max_bound.
Definition: BoundingVolume.h:160
Line3D is a class which derives from Eigen::ParametrizedLine<double, 3> in order to capture the seman...
Definition: Line3D.h:49
const Eigen::ParametrizedLine< double, 3 > & Line() const
Returns a const reference to the underlying Eigen::ParametrizedLine object.
Definition: Line3D.h:100
virtual ~Line3D()=default
virtual void Transform(const Eigen::Transform< double, 3, Eigen::Affine > &t)
Transform the Line3D by the given matrix.
Definition: Line3D.cpp:36
virtual double ClampParameter(double parameter) const
Clamps/bounds a parameter value to the closest valid place where the entity exists....
Definition: Line3D.h:202
const Eigen::Vector3d & Direction() const
Gets the line's direction vector.
Definition: Line3D.h:87
LineType
Specifies different semantic interpretations of 3d lines.
Definition: Line3D.h:62
@ 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:40
Line3D(const Eigen::Vector3d &origin, const Eigen::Vector3d &direction)
Default user constructor.
Definition: Line3D.cpp:17
const Eigen::Vector3d & Origin() const
Gets the line's origin point.
Definition: Line3D.h:84
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:136
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:260
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:167
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:171
LineType GetLineType() const
Gets the semantic type of the line.
Definition: Line3D.h:81
virtual double Length() const
Gets the length of the line, which for lines and rays will return positive infinity,...
Definition: Line3D.h:91
virtual Eigen::Vector3d Projection(const Eigen::Vector3d &point) const
Calculates a point projected onto the line, taking into account special semantics.
Definition: Line3D.cpp:163
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:72
virtual bool IsParameterValid(double parameter) const
Verifies that a given parameter value is valid for the semantics of the line object....
Definition: Line3D.h:208
double DistanceTo(const Line3D &other) const
Gets the closest distance between two Line3D objects, including derived types Ray3D and Segment3D,...
Definition: Line3D.cpp:270
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:153
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:54
A ray is a semantic interpretation of Eigen::ParametrizedLine which has an origin and a direction and...
Definition: Line3D.h:240
double Length() const override
Gets the length of the line, which for lines and rays will return positive infinity,...
Definition: Line3D.h:253
double ClampParameter(double parameter) const override
Clamps/bounds a parameter value to the closest valid place where the entity exists....
Definition: Line3D.h:293
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:284
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:244
bool IsParameterValid(double parameter) const override
Verifies that a given parameter value is valid for the semantics of the line object....
Definition: Line3D.h:301
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:305
Ray3D(const Eigen::Vector3d &origin, const Eigen::Vector3d &direction)
Default constructor, requires point and direction.
Definition: Line3D.cpp:281
A segment is a semantic interpretation of Eigen::ParametrizedLine which has an origin and an endpoint...
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 segment semant...
Definition: Line3D.cpp:379
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:325
double ClampParameter(double parameter) const override
Clamps/bounds a parameter value to the closest valid place where the entity exists....
Definition: Line3D.h:416
const Eigen::Vector3d & EndPoint() const
Get the end point of the segment.
Definition: Line3D.h:347
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:335
void Transform(const Eigen::Transform< double, 3, Eigen::Affine > &t) override
Transform the segment by the given matrix.
Definition: Line3D.cpp:330
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:358
Eigen::Vector3d MidPoint() const
Calculates the midpoint of the segment.
Definition: Line3D.h:344
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:341
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:319
bool IsParameterValid(double parameter) const override
Verifies that a given parameter value is valid for the semantics of the line object....
Definition: Line3D.h:424
AxisAlignedBoundingBox GetBoundingBox() const
Get an axis-aligned bounding box representing the enclosed volume of the line segment.
Definition: Line3D.cpp:368
Definition: Optional.h:259
char type
Definition: FilePCD.cpp:41
Definition: PinholeCameraIntrinsic.cpp:16