MeshLib C++ Docs
Loading...
Searching...
No Matches
MRPlane3.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector3.h"
4
5#if MR_HAS_REQUIRES
6#include "MRVectorTraits.h"
7#include <concepts>
8#endif
9
10namespace MR
11{
12
15template <typename T>
16struct Plane3
17{
19 T d = 0;
20
21 constexpr Plane3() noexcept = default;
22 constexpr Plane3( const Vector3<T> & n, T d ) noexcept : n( n ), d( d ) { }
23 template <typename U>
24 constexpr explicit Plane3( const Plane3<U> & p ) noexcept : n( p.n ), d( T( p.d ) ) { }
25 [[nodiscard]] constexpr static Plane3 fromDirAndPt( const Vector3<T> & n, const Vector3<T> & p ) { return { n, dot( n, p ) }; }
26
28 [[nodiscard]] T distance( const Vector3<T> & x ) const { return dot( n, x ) - d; }
29
31 [[nodiscard]] Plane3 operator -() const { return Plane3( -n, -d ); }
33 [[nodiscard]] const Plane3 & operator +() const { return *this; }
35 [[nodiscard]] Plane3 normalized() const
36 {
37 const auto len = n.length();
38 if ( len <= 0 )
39 return {};
40 const auto rlen = 1 / len;
41 return Plane3{ rlen * n, rlen * d };
42 }
43
45 [[nodiscard]] Vector3<T> project( const Vector3<T> & p ) const { return p - distance( p ) / n.lengthSq() * n; }
46};
47
50
54template <typename T>
55[[nodiscard]] inline Plane3<T> invTransformed( const Plane3<T> & pl, const AffineXf3<T> & ixf )
56{
57 return Plane3<T>{ ixf.A.transposed() * pl.n, pl.d - dot( pl.n, ixf.b ) };
58}
59
63template <typename T>
64MR_REQUIRES_IF_SUPPORTED( VectorTraits<T>::size == 1 ) // Fixes ambiguity in the old bindings on VS2019 only. TODO remove when wropping old bindings. Remove `#if MR_HAS_CONCEPTS` above too.
65[[nodiscard]] inline Plane3<T> transformed( const Plane3<T> & plane, const AffineXf3<T> & xf )
66{
67 return invTransformed( plane, xf.inverse() );
68}
69
70template <typename T>
71[[nodiscard]] inline bool operator == ( const Plane3<T> & a, const Plane3<T> & b )
72{
73 return a.n == b.n && a.d == b.d;
74}
75
76template <typename T>
77[[nodiscard]] inline bool operator != ( const Plane3<T> & a, const Plane3<T> & b )
78{
79 return !( a == b );
80}
81
83
84} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:31
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...
MR_BIND_IGNORE bool operator!=(const SetBitIteratorT< T > &a, const SetBitIteratorT< T > &b)
Definition MRMesh/MRBitSet.h:291
Definition MRMesh/MRAffineXf.h:14
constexpr AffineXf inverse() const noexcept
computes inverse transformation
V b
Definition MRMesh/MRAffineXf.h:19
M A
Definition MRMesh/MRAffineXf.h:18
Definition MRPlane3.h:17
constexpr Plane3() noexcept=default
Plane3 normalized() const
returns same plane represented with unit n-vector
Definition MRPlane3.h:35
Plane3< T > transformed(const Plane3< T > &plane, const AffineXf3< T > &xf)
Definition MRPlane3.h:65
Vector3< T > project(const Vector3< T > &p) const
finds the closest point on plane
Definition MRPlane3.h:45
T distance(const Vector3< T > &x) const
returns distance from given point to this plane (if n is a unit vector)
Definition MRPlane3.h:28
const Plane3 & operator+() const
returns same representation
Definition MRPlane3.h:33
Vector3< T > n
Definition MRPlane3.h:18
constexpr Plane3(const Plane3< U > &p) noexcept
Definition MRPlane3.h:24
T d
Definition MRPlane3.h:19
Plane3< T > invTransformed(const Plane3< T > &pl, const AffineXf3< T > &ixf)
Definition MRPlane3.h:55
Plane3 operator-() const
returns same plane represented with flipped direction of n-vector
Definition MRPlane3.h:31
static constexpr Plane3 fromDirAndPt(const Vector3< T > &n, const Vector3< T > &p)
Definition MRPlane3.h:25
Definition MRMesh/MRVector3.h:26
Definition MRMesh/MRVectorTraits.h:15