MeshLib C++ Docs
Loading...
Searching...
No Matches
MRSphere.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h" // To fix `attribute declaration must precede definition` on `Sphere`.
4
5namespace MR
6{
7
9template <typename V>
10struct Sphere
11{
12 using T = typename V::ValueType;
13
15 T radius = 0;
16
17 constexpr Sphere() noexcept = default;
18 constexpr Sphere( const V & c, T r ) noexcept : center( c ), radius( r ) { }
19 template <typename U>
20 constexpr explicit Sphere( const Sphere<U> & l ) noexcept : center( l.center ), radius( T( l.radius ) ) { }
21
23 [[nodiscard]] V project( const V & x ) const { return center + radius * ( x - center ).normalized(); }
24
27 [[nodiscard]] T distance( const V & x ) const { return ( x - center ).length() - radius; }
28
30 [[nodiscard]] T distanceSq( const V & x ) const { return sqr( distance( x ) ); }
31
32 friend bool operator == ( const Sphere & a, const Sphere & b ) = default;
33};
34
35} // namespace MR
Definition MRCameraOrientationPlugin.h:8
constexpr T sqr(T x) noexcept
squared value
Definition MRMesh/MRMeshFwd.h:753
Definition MRSphere.h:11
constexpr Sphere() noexcept=default
friend bool operator==(const Sphere &a, const Sphere &b)=default
T distance(const V &x) const
Definition MRSphere.h:27
V project(const V &x) const
finds the closest point on sphere
Definition MRSphere.h:23
constexpr Sphere(const Sphere< U > &l) noexcept
Definition MRSphere.h:20
typename V::ValueType T
Definition MRSphere.h:12
T distanceSq(const V &x) const
returns squared distance from given point to this sphere
Definition MRSphere.h:30
V center
Definition MRSphere.h:14
T radius
Definition MRSphere.h:15