MeshLib C++ Docs
Loading...
Searching...
No Matches
MRLine.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRAffineXf.h"
4#include <iosfwd>
5
6namespace MR
7{
10
11
14template <typename V>
15struct Line
16{
17 using T = typename V::ValueType;
18
19 V p, d;
20
21 constexpr Line() noexcept = default;
22 constexpr Line( const V & p, const V & d ) noexcept : p( p ), d( d ) { }
23 template <typename U>
24 constexpr explicit Line( const Line<U> & l ) noexcept : p( l.p ), d( l.d ) { }
25
27 [[nodiscard]] V operator()( T param ) const { return p + param * d; }
28
30 [[nodiscard]] T distanceSq( const V & x ) const
31 { return ( x - project( x ) ).lengthSq(); }
32
34 [[nodiscard]] Line operator -() const { return Line( p, -d ); }
35
37 [[nodiscard]] const Line & operator +() const { return *this; }
38
40 [[nodiscard]] Line normalized() const { return { p, d.normalized() }; }
41
43 [[nodiscard]] T projectionParam( const V & x ) const { return dot( d, x - p ) / d.lengthSq(); }
44
46 [[nodiscard]] V project( const V & x ) const { return operator()( projectionParam( x ) ); }
47
48 friend std::ostream& operator<<( std::ostream& s, const Line& l )
49 {
50 return s << l.p << '\n' << l.d;
51 }
52
53 friend std::istream& operator>>( std::istream& s, Line& l )
54 {
55 return s >> l.p >> l.d;
56 }
57};
58
61
65template <typename V>
66[[nodiscard]] inline Line<V> transformed( const Line<V> & l, const AffineXf<V> & xf )
67{
68 return Line<V>{ xf( l.p ), xf.A * l.d };
69}
70
71template <typename V>
72[[nodiscard]] inline bool operator == ( const Line<V> & a, const Line<V> & b )
73{
74 return a.p == b.p && a.d == b.d;
75}
76
78
79}
bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
friend std::istream & operator>>(std::istream &s, Line &l)
Definition MRLine.h:53
typename V::ValueType T
Definition MRLine.h:17
T projectionParam(const V &x) const
finds the parameter of the closest point to the given one on this line
Definition MRLine.h:43
const Line & operator+() const
returns same representation
Definition MRLine.h:37
T distanceSq(const V &x) const
returns squared distance from given point to this line
Definition MRLine.h:30
auto dot(const Matrix2< T > &a, const Matrix2< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMatrix2.h:142
constexpr Line(const Line< U > &l) noexcept
Definition MRLine.h:24
Line operator-() const
returns same line represented with flipped direction of d-vector
Definition MRLine.h:34
constexpr Line() noexcept=default
friend std::ostream & operator<<(std::ostream &s, const Line &l)
Definition MRLine.h:48
V operator()(T param) const
returns point on the line, where param=0 returns p and param=1 returns p+d
Definition MRLine.h:27
Vector3< T > d
Definition MRLine.h:19
AffineXf
Definition MRMeshFwd.h:290
Line< Vector3< T > > transformed(const Line< Vector3< T > > &l, const AffineXf< Vector3< T > > &xf)
Definition MRLine.h:66
Line normalized() const
returns same line represented with unit d-vector
Definition MRLine.h:40
Vector3< T > p
Definition MRLine.h:19
V project(const V &x) const
finds the closest point to the given one on this line
Definition MRLine.h:46
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRLine.h:16