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{
15
16
19template <typename T>
20struct Plane3
21{
23 T d = 0;
24
25 constexpr Plane3() noexcept = default;
26 constexpr Plane3( const Vector3<T> & n, T d ) noexcept : n( n ), d( d ) { }
27
30 template <typename U> MR_REQUIRES_IF_SUPPORTED( !std::is_same_v<T, U> )
31 constexpr explicit Plane3( const Plane3<U> & p ) noexcept : n( p.n ), d( T( p.d ) ) { }
32
33 [[nodiscard]] constexpr static Plane3 fromDirAndPt( const Vector3<T> & n, const Vector3<T> & p ) { return { n, dot( n, p ) }; }
34
36 [[nodiscard]] T distance( const Vector3<T> & x ) const { return dot( n, x ) - d; }
37
39 [[nodiscard]] Plane3 operator -() const { return Plane3( -n, -d ); }
41 [[nodiscard]] const Plane3 & operator +() const { return *this; }
43 [[nodiscard]] Plane3 normalized() const
44 {
45 const auto len = n.length();
46 if ( len <= 0 )
47 return {};
48 const auto rlen = 1 / len;
49 return Plane3{ rlen * n, rlen * d };
50 }
51
53 [[nodiscard]] Vector3<T> project( const Vector3<T> & p ) const { return p - distance( p ) / n.lengthSq() * n; }
54
55 friend std::ostream& operator<<( std::ostream& s, const Plane3& pl )
56 {
57 return s << pl.n << '\n' << pl.d;
58 }
59
60 friend std::istream& operator>>( std::istream& s, Plane3& pl )
61 {
62 return s >> pl.n >> pl.d;
63 }
64};
65
68
72template <typename T>
73[[nodiscard]] inline Plane3<T> invTransformed( const Plane3<T> & pl, const AffineXf3<T> & ixf )
74{
75 return Plane3<T>{ ixf.A.transposed() * pl.n, pl.d - dot( pl.n, ixf.b ) };
76}
77
81template <typename T>
83[[nodiscard]] inline Plane3<T> transformed( const Plane3<T> & plane, const AffineXf3<T> & xf )
84{
85 return invTransformed( plane, xf.inverse() );
86}
87
88template <typename T>
89[[nodiscard]] inline bool operator == ( const Plane3<T> & a, const Plane3<T> & b )
90{
91 return a.n == b.n && a.d == b.d;
92}
93
94template <typename T>
95[[nodiscard]] inline bool operator != ( const Plane3<T> & a, const Plane3<T> & b )
96{
97 return !( a == b );
98}
99
101
102}
#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...
constexpr Plane3() noexcept=default
Plane3 normalized() const
returns same plane represented with unit n-vector
Definition MRPlane3.h:43
Plane3< T > transformed(const Plane3< T > &plane, const AffineXf3< T > &xf)
Fixes ambiguity in the old bindings on VS2019 only. TODO remove when wropping old bindings....
Definition MRPlane3.h:83
Vector3< T > project(const Vector3< T > &p) const
finds the closest point on plane
Definition MRPlane3.h:53
T distance(const Vector3< T > &x) const
returns distance from given point to this plane (if n is a unit vector)
Definition MRPlane3.h:36
bool operator!=(const Color &a, const Color &b)
Definition MRColor.h:104
MR_REQUIRES_IF_SUPPORTED(!std::is_same_v< T, U >) const expr explicit Plane3(const Plane3< U > &p) noexcept
Definition MRPlane3.h:30
friend std::ostream & operator<<(std::ostream &s, const Plane3 &pl)
Definition MRPlane3.h:55
const Plane3 & operator+() const
returns same representation
Definition MRPlane3.h:41
Vector3< T > n
Definition MRPlane3.h:22
friend std::istream & operator>>(std::istream &s, Plane3 &pl)
Definition MRPlane3.h:60
T d
Definition MRPlane3.h:23
Plane3< T > invTransformed(const Plane3< T > &pl, const AffineXf3< T > &ixf)
Definition MRPlane3.h:73
Plane3 operator-() const
returns same plane represented with flipped direction of n-vector
Definition MRPlane3.h:39
static constexpr Plane3 fromDirAndPt(const Vector3< T > &n, const Vector3< T > &p)
Definition MRPlane3.h:33
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MRPlane3.h:21
Definition MRVector3.h:33
Common traits for (mathematical) vectors.
Definition MRMesh/MRVectorTraits.h:18