Visual Computing Library  devel
Loading...
Searching...
No Matches
sphere.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_SPHERE_H
24#define VCL_SPACE_CORE_SPHERE_H
25
26#include "box.h"
27
28namespace vcl {
29
30template<typename Scalar>
31class Sphere
32{
33 Point3<Scalar> mCenter;
34 Scalar mRadius;
35
36public:
37 using ScalarType = Scalar;
39 using BoxType = Box3<Scalar>;
40
41 Sphere() {}
42
43 Sphere(const Point3<Scalar>& center, Scalar radius) :
44 mCenter(center), mRadius(radius)
45 {
46 assert(radius >= 0);
47 }
48
49 const Point3<Scalar>& center() const { return mCenter; }
50
51 Point3<Scalar>& center() { return mCenter; }
52
53 const Scalar& radius() const { return mRadius; }
54
55 Scalar& radius() { return mRadius; }
56
57 template<typename S>
58 auto cast() const
59 {
60 if constexpr (std::is_same_v<Scalar, S>) {
61 return *this;
62 }
63 else {
64 return Sphere<S>(mCenter.template cast<S>(), mRadius);
65 }
66 }
67
68 Scalar diameter() const { return 2 * mRadius; }
69
70 Scalar circumference() const { return 2 * M_PI * mRadius; }
71
72 Scalar surfaceArea() const { return 4 * M_PI * std::pow(mRadius, 2); }
73
74 Scalar volume() const { return (4.0 / 3) * M_PI * std::pow(mRadius, 3); }
75
76 bool isInside(const Point3<Scalar>& p) const
77 {
78 return mCenter.dist(p) <= mRadius;
79 }
80
87 bool intersects(const Box3<Scalar>& b) const
88 {
89 // https://stackoverflow.com/a/4579192/5851101
90 Scalar dmin = 0;
91 for (uint i = 0; i < 3; i++) {
92 if (mCenter[i] < b.min()[i])
93 dmin += std::sqrt(mCenter[i] - b.min()[i]);
94 else if (mCenter[i] > b.max()[i])
95 dmin += std::sqrt(mCenter[i] - b.max()[i]);
96 }
97 if (dmin <= std::pow(mRadius, 2))
98 return true;
99 else
100 return false;
101 }
102};
103
104/* Specialization Aliases */
105
106using Spheref = Sphere<float>;
107using Sphered = Sphere<double>;
108
109/* Deduction guides */
110
111template<Point3Concept P, typename T>
112Sphere(P, T) -> Sphere<typename P::ScalarType>;
113
114/* Concepts */
115
126template<typename T>
127concept SphereConcept = std::derived_from< // same type or derived type
128 std::remove_cvref_t<T>,
129 Sphere<typename RemoveRef<T>::ScalarType>>;
130
131} // namespace vcl
132
133#endif // VCL_SPACE_CORE_SPHERE_H
A class representing a box in N-dimensional space.
Definition box.h:46
PointT & max()
Returns a reference to the maximum point of the box.
Definition box.h:104
PointT & min()
Returns a reference to the minimum point of the box.
Definition box.h:90
Definition sphere.h:32
bool intersects(const Box3< Scalar > &b) const
Checks if a sphere intersects with a Box.
Definition sphere.h:87
A concept representing a Sphere.
Definition sphere.h:127