Trax3 3.1.0
trax track library
Loading...
Searching...
No Matches
Sphere.h
1// trax track library
2// AD 2013
3//
4// "And the arms of the ocean are carrying me"
5//
6// Florence + the Machine
7//
8// Copyright (c) 2025 Trend Redaktions- und Verlagsgesellschaft mbH
9// Copyright (c) 2019 Marc-Michael Horstmann
10//
11// Permission is hereby granted to any person obtaining a copy of this software
12// and associated source code (the "Software"), to use, view, and study the
13// Software for personal or internal business purposes, subject to the following
14// conditions:
15//
16// 1. Redistribution, modification, sublicensing, or commercial use of the
17// Software is NOT permitted without prior written consent from the copyright
18// holder.
19//
20// 2. The Software is provided "AS IS", without warranty of any kind, express
21// or implied.
22//
23// 3. All copies of the Software must retain this license notice.
24//
25// For further information, please contact: horstmann.marc@trendverlag.de
26
27#pragma once
28
29#include "Position.h"
30
31namespace spat{
32
33 template<typename> struct Box;
34 template<typename,typename> struct VectorBundle;
35
37 template<typename Valtype>
38 struct Sphere
39 {
40 typedef Valtype value_type;
41
43 Valtype r;
44
45
50 Sphere() noexcept = default;
51 Sphere( const Position<Valtype>& center, Valtype radius ) noexcept;
53
54
56 void Init() noexcept;
57
58
60 Position<Valtype> Center() const noexcept;
61
62
64 Valtype Radius() const noexcept;
65
66
70 bool Includes( Valtype x, Valtype y, Valtype z ) const noexcept;
71
72
76 bool Includes( const Position<Valtype>& pt ) const noexcept;
77
78
80 Box<Valtype> ExBox() const noexcept;
81
82
84 Box<Valtype> InBox() const noexcept;
85 };
86
89 template<typename Valtype>
90 bool operator==( const Sphere<Valtype> a, const Sphere<Valtype> b ) noexcept;
91 template<typename Valtype>
92 bool operator!=( const Sphere<Valtype> a, const Sphere<Valtype> b ) noexcept;
94
97 template<typename Valtype>
98 bool Intersecting( const Sphere<Valtype>& sphereA, const Sphere<Valtype>& sphereB ) noexcept;
99
100 template<typename Valtype,typename ValtypeT>
101 bool Intersecting( const Sphere<Valtype>& sphere, const VectorBundle<Valtype,ValtypeT>& ray ) noexcept;
103
105template<typename Valtype> inline
106Sphere<Valtype>::Sphere( const Position<Valtype>& center, Valtype radius ) noexcept
107 : c{center},
108 r{radius}
109{}
110
111template<typename Valtype> inline
112void Sphere<Valtype>::Init() noexcept{
113 c.Init();
114 r = static_cast<Valtype>(1);
115}
116
117template<typename Valtype> inline
119 return c;
120}
121
122template<typename Valtype> inline
123Valtype Sphere<Valtype>::Radius() const noexcept{
124 return r;
125}
126
127template<typename Valtype> inline
128bool Sphere<Valtype>::Includes( Valtype x, Valtype y, Valtype z ) const noexcept{
129 return Includes( { x, y, z } );
130}
131
132template<typename Valtype> inline
133bool Sphere<Valtype>::Includes( const Position<Valtype>& pt ) const noexcept{
134 return (pt - c).Length() < r;
135}
136
137template<typename Valtype> inline
139 return Box<Valtype>(
140 c.x - r, c.y - r, c.z - r,
141 c.x + r, c.y + r, c.z + r );
142}
143
144template<typename Valtype> inline
146 Valtype a = r / std::sqrt(3);
147 return Box<Valtype>(
148 c.x - a, c.y - a, c.z - a,
149 c.x + a, c.y + a, c.z + a );
150}
151
152template<typename Valtype> inline
153bool operator==( const Sphere<Valtype> a, const Sphere<Valtype> b ) noexcept{
154 return a.Center() == b.Center() && a.Radius() == b.Radius();
155}
156
157template<typename Valtype> inline
158bool operator!=( const Sphere<Valtype> a, const Sphere<Valtype> b ) noexcept{
159 return !(a == b);
160}
161
162template<typename Valtype>
163bool Intersecting( const Sphere<Valtype>& sphereA, const Sphere<Valtype>& sphereB ) noexcept{
164 return (sphereA.Center() - sphereB.Center()).Length() < sphereA.Radius() + sphereB.Radius();
165}
166
167template<typename Valtype,typename ValtypeT>
168bool Intersecting( const Sphere<Valtype>& sphere, const VectorBundle<Valtype,ValtypeT>& ray ) noexcept{
169 const Vector<Valtype> D = sphere.Center() - ray.P;
170 return (D - (D*ray.T) * ray.T).Length() < sphere.Radius();
171}
172
173} // namespace spat
The namespace provides classes and methods for spatial computations.
Definition Box.h:32
Axis aligned box.
Definition Box.h:41
Implements a 3D - position in cartesian coordinates.
Definition Position.h:46
Sphere with center and radius.
Definition Sphere.h:39
bool Includes(dim::Value< Dimension< 1, 0, 0 > > x, dim::Value< Dimension< 1, 0, 0 > > y, dim::Value< Dimension< 1, 0, 0 > > z) const noexcept
Definition Sphere.h:128
dim::Value< Dimension< 1, 0, 0 > > Radius() const noexcept
Definition Sphere.h:123
Box< dim::Value< Dimension< 1, 0, 0 > > > InBox() const noexcept
Definition Sphere.h:145
Valtype r
Radius of the sphere.
Definition Sphere.h:43
Position< Valtype > c
Center of the sphere.
Definition Sphere.h:42
Position< dim::Value< Dimension< 1, 0, 0 > > > Center() const noexcept
Definition Sphere.h:118
Box< dim::Value< Dimension< 1, 0, 0 > > > ExBox() const noexcept
Definition Sphere.h:138
void Init() noexcept
Definition Sphere.h:112
Sphere() noexcept=default
Does not initialize the members.
Implements a Vector bundle.
Definition VectorBundle.h:42
Implements a 3D - vector in cartesian coordinates.
Definition Vector.h:48