libMesh
Public Member Functions | Private Attributes | List of all members
libMesh::LineConstraint Class Reference

Represents a line constraint defined by a base point and direction vector. More...

#include <variational_smoother_constraint.h>

Public Member Functions

 LineConstraint ()=default
 
 LineConstraint (const Point &point, const Point &direction, const Real &tol=TOLERANCE *TOLERANCE)
 Constructor. More...
 
bool operator< (const LineConstraint &other) const
 Comparison operator for ordering LineConstraint objects. More...
 
bool operator== (const LineConstraint &other) const
 Equality operator. More...
 
bool contains_point (const PointConstraint &p) const
 Query whether a point lies on the line. More...
 
bool is_parallel (const LineConstraint &l) const
 Query whether a line is parallel to this line. More...
 
bool is_parallel (const PlaneConstraint &p) const
 Query whether a plane is parallel to this line. More...
 
ConstraintVariant intersect (const ConstraintVariant &other) const
 Computes the intersection of this line with another constraint. More...
 
const Pointpoint () const
 Const getter for the _point attribute. More...
 
const Pointdirection () const
 Const getter for the _direction attribute. More...
 
const Realtol () const
 Const getter for the _tol attribute. More...
 

Private Attributes

Point _point
 A point on the constraining line. More...
 
Point _direction
 Direction of the constraining line. More...
 
Real _tol
 Tolerance to use for numerical comparisons. More...
 

Detailed Description

Represents a line constraint defined by a base point and direction vector.

Definition at line 123 of file variational_smoother_constraint.h.

Constructor & Destructor Documentation

◆ LineConstraint() [1/2]

libMesh::LineConstraint::LineConstraint ( )
default

◆ LineConstraint() [2/2]

libMesh::LineConstraint::LineConstraint ( const Point point,
const Point direction,
const Real tol = TOLERANCE * TOLERANCE 
)

Constructor.

Parameters
pointA point on the constraining line.
directionthe direction of the constraining line.
tolThe tolerance to use for numerical comparisons.

Definition at line 79 of file variational_smoother_constraint.C.

References _direction, _tol, and libMesh::TypeVector< T >::norm().

81 {
82  libmesh_error_msg_if(_direction.norm() < _tol,
83  "Can't define a line with zero magnitude direction vector.");
84 }
auto norm() const -> decltype(std::norm(T()))
Definition: type_vector.h:907
Point _point
A point on the constraining line.
Point _direction
Direction of the constraining line.
const Point & direction() const
Const getter for the _direction attribute.
Real _tol
Tolerance to use for numerical comparisons.
const Point & point() const
Const getter for the _point attribute.
const Real & tol() const
Const getter for the _tol attribute.

Member Function Documentation

◆ contains_point()

bool libMesh::LineConstraint::contains_point ( const PointConstraint p) const

Query whether a point lies on the line.

Parameters
pThe point in question
Returns
bool indicating whether p lies on the line.

Definition at line 103 of file variational_smoother_constraint.C.

References _direction, _point, _tol, libMesh::TypeVector< T >::cross(), libMesh::TensorTools::norm(), and libMesh::PointConstraint::point().

Referenced by intersect(), and operator==().

104 {
105  // If the point lies on the line, then the vector p - point is parallel to the
106  // line In that case, the cross product of p - point with the line's direction
107  // will be zero.
108  return _direction.cross(p.point() - _point).norm() < _tol;
109 }
Point _point
A point on the constraining line.
Point _direction
Direction of the constraining line.
Real _tol
Tolerance to use for numerical comparisons.
TypeVector< typename CompareTypes< T, T2 >::supertype > cross(const TypeVector< T2 > &v) const
Definition: type_vector.h:884
auto norm(const T &a) -> decltype(std::abs(a))
Definition: tensor_tools.h:74

◆ direction()

const Point& libMesh::LineConstraint::direction ( ) const
inline

Const getter for the _direction attribute.

Definition at line 194 of file variational_smoother_constraint.h.

References _direction.

Referenced by libMesh::PlaneConstraint::contains_line(), is_parallel(), operator<(), and operator==().

194 { return _direction; }
Point _direction
Direction of the constraining line.

◆ intersect()

ConstraintVariant libMesh::LineConstraint::intersect ( const ConstraintVariant other) const

Computes the intersection of this line with another constraint.

Handles intersection with LineConstraint, PlaneConstraint, or PointConstraint.

Parameters
otherThe constraint to intersect with.
Returns
The most specific ConstraintVariant that satisfies both constraints. constraints. If no intersection exists, return an InvalidConstraint.

Definition at line 121 of file variational_smoother_constraint.C.

References _direction, _point, _tol, contains_point(), libMesh::TypeVector< T >::cross(), is_parallel(), libMesh::TensorTools::norm(), libMesh::TypeVector< T >::norm_sq(), and libMesh::Real.

122 {
123  // using visit to resolve the variant to its actual type
124  return std::visit(
125  [&](auto && o) -> ConstraintVariant {
126  // Use std::decay_t to strip references/const/volatile from o's type,
127  // so we can match the actual stored variant type in std::is_same_v.
128  using T = std::decay_t<decltype(o)>;
129  if constexpr (std::is_same_v<T, LineConstraint>)
130  {
131  if (*this == o)
132  return *this;
133 
134  if (this->is_parallel(o))
135  // Lines are parallel and do not intersect
136  return InvalidConstraint();
137 
138  // Solve for t in the equation p1 + t·d1 = p2 + s·d2
139  // The shortest vector between skew lines lies along the normal vector
140  // (d1 × d2). Projecting the vector (p2 - p1) onto this normal gives a
141  // scalar proportional to the distance. This is equivalent to solving:
142  // ((p2 - p1) × d2) · (d1 × d2) = t · |d1 × d2|²
143  // ⇒ t = ((delta × d2) · (d1 × d2)) / |d1 × d2|²
144 
145  const Point delta = o.point() - _point;
146  const Point cross_d1_d2 = _direction.cross(o.direction());
147  const Real cross_dot = (delta.cross(o.direction())) * cross_d1_d2;
148  const Real denom = cross_d1_d2.norm_sq();
149 
150  const Real t = cross_dot / denom;
151  const Point intersection = _point + t * _direction;
152 
153  // Verify that intersection lies on both lines
154  if (o.direction().cross(intersection - o.point()).norm() > _tol)
155  // Lines do not intersect at a single point
156  return InvalidConstraint();
157 
158  return PointConstraint{intersection};
159  }
160 
161  else if constexpr (std::is_same_v<T, PlaneConstraint>)
162  return o.intersect(*this);
163 
164  else if constexpr (std::is_same_v<T, PointConstraint>)
165  {
166  if (!this->contains_point(o))
167  // Point is not on the line
168  return InvalidConstraint();
169 
170  return o;
171  }
172 
173  else
174  libmesh_error_msg("Unsupported constraint type in Line::intersect.");
175  },
176  other);
177 }
bool is_parallel(const LineConstraint &l) const
Query whether a line is parallel to this line.
Point _point
A point on the constraining line.
Point _direction
Direction of the constraining line.
Real _tol
Tolerance to use for numerical comparisons.
TypeVector< typename CompareTypes< T, T2 >::supertype > cross(const TypeVector< T2 > &v) const
Definition: type_vector.h:884
auto norm(const T &a) -> decltype(std::abs(a))
Definition: tensor_tools.h:74
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::variant< PointConstraint, LineConstraint, PlaneConstraint, InvalidConstraint > ConstraintVariant
Type used to store a constraint that may be a PlaneConstraint, LineConstraint, or PointConstraint...
bool contains_point(const PointConstraint &p) const
Query whether a point lies on the line.

◆ is_parallel() [1/2]

bool libMesh::LineConstraint::is_parallel ( const LineConstraint l) const

Query whether a line is parallel to this line.

Parameters
lThe line in question
Returns
bool indicating whether l is parallel to this line.

Definition at line 111 of file variational_smoother_constraint.C.

References _direction, _tol, libMesh::TypeVector< T >::absolute_fuzzy_equals(), and direction().

Referenced by intersect(), and libMesh::PlaneConstraint::is_parallel().

112 {
113  return _direction.absolute_fuzzy_equals(l.direction(), _tol);
114 }
Point _direction
Direction of the constraining line.
Real _tol
Tolerance to use for numerical comparisons.
bool absolute_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
Definition: type_vector.h:972

◆ is_parallel() [2/2]

bool libMesh::LineConstraint::is_parallel ( const PlaneConstraint p) const

Query whether a plane is parallel to this line.

Parameters
pThe plane in question
Returns
bool indicating whether p is parallel to this line.

Definition at line 116 of file variational_smoother_constraint.C.

References _direction, _tol, and libMesh::PlaneConstraint::normal().

117 {
118  return _direction * p.normal() < _tol;
119 }
Point _direction
Direction of the constraining line.
Real _tol
Tolerance to use for numerical comparisons.

◆ operator<()

bool libMesh::LineConstraint::operator< ( const LineConstraint other) const

Comparison operator for ordering LineConstraint objects.

The comparison is primarily based on the direction vector. If the direction vectors are equal (within tolerance), the tie is broken using the dot product of the direction with the base point.

Parameters
otherThe LineConstraint to compare with.
Returns
True if this LineConstraint is less than the other.

Definition at line 86 of file variational_smoother_constraint.C.

References _direction, _point, _tol, libMesh::TypeVector< T >::absolute_fuzzy_equals(), direction(), and point().

87 {
88  if (*this == other)
89  return false;
90 
91  if (!(_direction.absolute_fuzzy_equals(other.direction(), _tol)))
92  return _direction < other.direction();
93  return (_direction * _point) < (other.direction() * other.point());
94 }
Point _point
A point on the constraining line.
Point _direction
Direction of the constraining line.
Real _tol
Tolerance to use for numerical comparisons.
bool absolute_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
Definition: type_vector.h:972

◆ operator==()

bool libMesh::LineConstraint::operator== ( const LineConstraint other) const

Equality operator.

Parameters
otherThe LineConstraint to compare with.
Returns
True if both LineConstraints represent the same line.

Definition at line 96 of file variational_smoother_constraint.C.

References _direction, _tol, libMesh::TypeVector< T >::absolute_fuzzy_equals(), contains_point(), direction(), and point().

97 {
98  if (!(_direction.absolute_fuzzy_equals(other.direction(), _tol)))
99  return false;
100  return this->contains_point(other.point());
101 }
Point _direction
Direction of the constraining line.
Real _tol
Tolerance to use for numerical comparisons.
bool absolute_fuzzy_equals(const TypeVector< T > &rhs, Real tol=TOLERANCE) const
Definition: type_vector.h:972
bool contains_point(const PointConstraint &p) const
Query whether a point lies on the line.

◆ point()

const Point& libMesh::LineConstraint::point ( ) const
inline

Const getter for the _point attribute.

Definition at line 189 of file variational_smoother_constraint.h.

References _point.

Referenced by libMesh::PlaneConstraint::contains_line(), operator<(), and operator==().

189 { return _point; }
Point _point
A point on the constraining line.

◆ tol()

const Real& libMesh::LineConstraint::tol ( ) const
inline

Const getter for the _tol attribute.

Definition at line 199 of file variational_smoother_constraint.h.

References _tol.

199 { return _tol; }
Real _tol
Tolerance to use for numerical comparisons.

Member Data Documentation

◆ _direction

Point libMesh::LineConstraint::_direction
private

Direction of the constraining line.

Definition at line 211 of file variational_smoother_constraint.h.

Referenced by contains_point(), direction(), intersect(), is_parallel(), LineConstraint(), operator<(), and operator==().

◆ _point

Point libMesh::LineConstraint::_point
private

A point on the constraining line.

Definition at line 206 of file variational_smoother_constraint.h.

Referenced by contains_point(), intersect(), operator<(), and point().

◆ _tol

Real libMesh::LineConstraint::_tol
private

Tolerance to use for numerical comparisons.

Definition at line 216 of file variational_smoother_constraint.h.

Referenced by contains_point(), intersect(), is_parallel(), LineConstraint(), operator<(), operator==(), and tol().


The documentation for this class was generated from the following files: