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{
8
11template <typename V>
12struct Line
13{
14 using T = typename V::ValueType;
15
16 V p, d;
17
18 constexpr Line() noexcept = default;
19 constexpr Line( const V & p, const V & d ) noexcept : p( p ), d( d ) { }
20 template <typename U>
21 constexpr explicit Line( const Line<U> & l ) noexcept : p( l.p ), d( l.d ) { }
22
24 [[nodiscard]] V operator()( T param ) const { return p + param * d; }
25
27 [[nodiscard]] T distanceSq( const V & x ) const
28 { return ( x - project( x ) ).lengthSq(); }
29
31 [[nodiscard]] Line operator -() const { return Line( p, -d ); }
33 [[nodiscard]] const Line & operator +() const { return *this; }
35 [[nodiscard]] Line normalized() const { return { p, d.normalized() }; }
36
38 [[nodiscard]] V project( const V & x ) const { return p + dot( d, x - p ) / d.lengthSq() * d; }
39
40 friend std::ostream& operator<<( std::ostream& s, const Line& l )
41 {
42 return s << l.p << '\n' << l.d;
43 }
44
45 friend std::istream& operator>>( std::istream& s, Line& l )
46 {
47 return s >> l.p >> l.d;
48 }
49};
50
53
57template <typename V>
58[[nodiscard]] inline Line<V> transformed( const Line<V> & l, const AffineXf<V> & xf )
59{
60 return Line<V>{ xf( l.p ), xf.A * l.d };
61}
62
63template <typename V>
64[[nodiscard]] inline bool operator == ( const Line<V> & a, const Line<V> & b )
65{
66 return a.p == b.p && a.d == b.d;
67}
68
70
71} // namespace MR
MRMESH_API 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...
Definition MRCameraOrientationPlugin.h:8
Definition MRMesh/MRAffineXf.h:23
M A
Definition MRMesh/MRAffineXf.h:27
Definition MRLine.h:13
friend std::istream & operator>>(std::istream &s, Line &l)
Definition MRLine.h:45
typename V::ValueType T
Definition MRLine.h:14
const Line & operator+() const
returns same representation
Definition MRLine.h:33
T distanceSq(const V &x) const
returns squared distance from given point to this line
Definition MRLine.h:27
constexpr Line(const Line< U > &l) noexcept
Definition MRLine.h:21
Line operator-() const
returns same line represented with flipped direction of d-vector
Definition MRLine.h:31
constexpr Line() noexcept=default
friend std::ostream & operator<<(std::ostream &s, const Line &l)
Definition MRLine.h:40
V operator()(T param) const
returns point on the line, where param=0 returns p and param=1 returns p+d
Definition MRLine.h:24
V d
Definition MRLine.h:16
Line< V > transformed(const Line< V > &l, const AffineXf< V > &xf)
Definition MRLine.h:58
Line normalized() const
returns same line represented with unit d-vector
Definition MRLine.h:35
V p
Definition MRLine.h:16
V project(const V &x) const
finds the closest point on line
Definition MRLine.h:38