Visual Computing Library
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
28#include <vclib/concepts/space/sphere.h>
29
30namespace vcl {
31
32template<typename Scalar>
33class Sphere
34{
35 Point3<Scalar> mCenter;
36 Scalar mRadius;
37
38public:
39 using ScalarType = Scalar;
41 using BoxType = Box3<Scalar>;
42
43 Sphere() {}
44
45 Sphere(const Point3<Scalar>& center, Scalar radius) :
46 mCenter(center), mRadius(radius)
47 {
48 assert(radius >= 0);
49 }
50
51 const Point3<Scalar>& center() const { return mCenter; }
52
53 Point3<Scalar>& center() { return mCenter; }
54
55 const Scalar& radius() const { return mRadius; }
56
57 Scalar& radius() { return mRadius; }
58
59 template<typename S>
60 auto cast() const
61 {
62 if constexpr (std::is_same_v<Scalar, S>) {
63 return *this;
64 }
65 else {
66 return Sphere<S>(mCenter.template cast<S>(), mRadius);
67 }
68 }
69
70 Scalar diameter() const { return 2 * mRadius; }
71
72 Scalar circumference() const { return 2 * M_PI * mRadius; }
73
74 Scalar surfaceArea() const { return 4 * M_PI * std::pow(mRadius, 2); }
75
76 Scalar volume() const { return (4.0 / 3) * M_PI * std::pow(mRadius, 3); }
77
78 bool isInside(const Point3<Scalar>& p) const
79 {
80 return mCenter.dist(p) <= mRadius;
81 }
82
89 bool intersects(const Box3<Scalar>& b) const
90 {
91 // https://stackoverflow.com/a/4579192/5851101
92 Scalar dmin = 0;
93 for (uint i = 0; i < 3; i++) {
94 if (mCenter[i] < b.min()[i])
95 dmin += std::sqrt(mCenter[i] - b.min()[i]);
96 else if (mCenter[i] > b.max()[i])
97 dmin += std::sqrt(mCenter[i] - b.max()[i]);
98 }
99 if (dmin <= std::pow(mRadius, 2))
100 return true;
101 else
102 return false;
103 }
104};
105
106/* Specialization Aliases */
107
108using Spheref = Sphere<float>;
109using Sphered = Sphere<double>;
110
111/* Deduction guides */
112
113template<Point3Concept P, typename T>
114Sphere(P, T) -> Sphere<typename P::ScalarType>;
115
116} // namespace vcl
117
118#endif // VCL_SPACE_CORE_SPHERE_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition sphere.h:34
bool intersects(const Box3< Scalar > &b) const
Checks if a sphere intersects with a Box.
Definition sphere.h:89