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#include <iosfwd>
6#if MR_HAS_REQUIRES
7#include "MRVectorTraits.h"
8#include <concepts>
9#endif
10
11namespace MR
12{
13
16template <typename T>
17struct Plane3
18{
20 T d = 0;
21
22 constexpr Plane3() noexcept = default;
23 constexpr Plane3( const Vector3<T> & n, T d ) noexcept : n( n ), d( d ) { }
24
25 // Here `T == U` doesn't seem to cause any issues in the C++ code, but we're still disabling it because it somehow gets emitted
26 // when generating the bindings, and results in duplicate functions in C#.
27 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
28 constexpr explicit Plane3( const Plane3<U> & p ) noexcept : n( p.n ), d( T( p.d ) ) { }
29
30 [[nodiscard]] constexpr static Plane3 fromDirAndPt( const Vector3<T> & n, const Vector3<T> & p ) { return { n, dot( n, p ) }; }
31
33 [[nodiscard]] T distance( const Vector3<T> & x ) const { return dot( n, x ) - d; }
34
36 [[nodiscard]] Plane3 operator -() const { return Plane3( -n, -d ); }
38 [[nodiscard]] const Plane3 & operator +() const { return *this; }
40 [[nodiscard]] Plane3 normalized() const
41 {
42 const auto len = n.length();
43 if ( len <= 0 )
44 return {};
45 const auto rlen = 1 / len;
46 return Plane3{ rlen * n, rlen * d };
47 }
48
50 [[nodiscard]] Vector3<T> project( const Vector3<T> & p ) const { return p - distance( p ) / n.lengthSq() * n; }
51
52 friend std::ostream& operator<<( std::ostream& s, const Plane3& pl )
53 {
54 return s << pl.n << '\n' << pl.d;
55 }
56
57 friend std::istream& operator>>( std::istream& s, Plane3& pl )
58 {
59 return s >> pl.n >> pl.d;
60 }
61};
62
65
69template <typename T>
70[[nodiscard]] inline Plane3<T> invTransformed( const Plane3<T> & pl, const AffineXf3<T> & ixf )
71{
72 return Plane3<T>{ ixf.A.transposed() * pl.n, pl.d - dot( pl.n, ixf.b ) };
73}
74
78template <typename T>
79MR_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.
80[[nodiscard]] inline Plane3<T> transformed( const Plane3<T> & plane, const AffineXf3<T> & xf )
81{
82 return invTransformed( plane, xf.inverse() );
83}
84
85template <typename T>
86[[nodiscard]] inline bool operator == ( const Plane3<T> & a, const Plane3<T> & b )
87{
88 return a.n == b.n && a.d == b.d;
89}
90
91template <typename T>
92[[nodiscard]] inline bool operator != ( const Plane3<T> & a, const Plane3<T> & b )
93{
94 return !( a == b );
95}
96
98
99} // namespace MR
#define MR_REQUIRES_IF_SUPPORTED(...)
Definition MRMacros.h:34
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
bool operator!=(const Color &a, const Color &b)
Definition MRMesh/MRColor.h:101
Definition MRMesh/MRAffineXf.h:23
V b
Definition MRMesh/MRAffineXf.h:28
M A
Definition MRMesh/MRAffineXf.h:27
Definition MRPlane3.h:18
constexpr Plane3() noexcept=default
Plane3 normalized() const
returns same plane represented with unit n-vector
Definition MRPlane3.h:40
Plane3< T > transformed(const Plane3< T > &plane, const AffineXf3< T > &xf)
Definition MRPlane3.h:80
Vector3< T > project(const Vector3< T > &p) const
finds the closest point on plane
Definition MRPlane3.h:50
T distance(const Vector3< T > &x) const
returns distance from given point to this plane (if n is a unit vector)
Definition MRPlane3.h:33
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Plane3(const Plane3< U > &p) noexcept
Definition MRPlane3.h:27
friend std::ostream & operator<<(std::ostream &s, const Plane3 &pl)
Definition MRPlane3.h:52
const Plane3 & operator+() const
returns same representation
Definition MRPlane3.h:38
Vector3< T > n
Definition MRPlane3.h:19
friend std::istream & operator>>(std::istream &s, Plane3 &pl)
Definition MRPlane3.h:57
T d
Definition MRPlane3.h:20
Plane3< T > invTransformed(const Plane3< T > &pl, const AffineXf3< T > &ixf)
Definition MRPlane3.h:70
Plane3 operator-() const
returns same plane represented with flipped direction of n-vector
Definition MRPlane3.h:36
static constexpr Plane3 fromDirAndPt(const Vector3< T > &n, const Vector3< T > &p)
Definition MRPlane3.h:30
Definition MRMesh/MRVector3.h:30
Definition MRMesh/MRVectorTraits.h:15