Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
sphere.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
12
#include "
math/vec3.h
"
13
#include "
math/mat4.h
"
14
#include "
math/bbox.h
"
15
#include "
math/rectangle.h
"
16
#include "
math/clipstatus.h
"
17
#include "
math/line.h
"
18
19
//------------------------------------------------------------------------------
20
namespace
Math
21
{
22
class
sphere
23
{
24
public
:
26
sphere
();
28
sphere
(
const
point
& _p,
scalar
_r);
30
sphere
(
scalar
_x,
scalar
_y,
scalar
_z,
scalar
_r);
32
sphere
(
const
sphere
& rhs);
34
void
set
(
const
point
& _p,
scalar
_r);
36
void
set
(
scalar
_x,
scalar
_y,
scalar
_z,
scalar
_r);
38
bool
inside
(
const
bbox
& box)
const
;
40
bool
intersects
(
const
sphere
& s)
const
;
42
bool
intersects
(
const
bbox
& box)
const
;
44
bool
intersects_ray
(
const
line
& l)
const
;
46
bool
intersect_sweep
(
const
vector
& va,
const
sphere
& sb,
const
vector
& vb,
scalar
& u0,
scalar
& u1)
const
;
48
rectangle<scalar>
project_screen_rh
(
const
mat4
& modelView,
const
mat4
& projection,
scalar
nearZ)
const
;
50
ClipStatus::Type
clipstatus
(
const
bbox
& box)
const
;
52
static
vec3
random_point_on_unit_sphere
();
53
54
point
p
;
55
scalar
r
;
56
};
57
58
//------------------------------------------------------------------------------
61
inline
62
sphere::sphere
() :
63
r
(1.0f)
64
{
65
// empty
66
}
67
68
//------------------------------------------------------------------------------
71
inline
72
sphere::sphere
(
const
point
& _p,
scalar
_r) :
73
p
(_p),
74
r
(_r)
75
{
76
// empty
77
}
78
79
//------------------------------------------------------------------------------
82
inline
83
sphere::sphere
(
scalar
_x,
scalar
_y,
scalar
_z,
scalar
_r) :
84
p
(_x, _y, _z),
85
r
(_r)
86
{
87
// empty
88
}
89
90
//------------------------------------------------------------------------------
93
inline
94
sphere::sphere
(
const
sphere
& rhs) :
95
p
(rhs.
p
),
96
r
(rhs.
r
)
97
{
98
// empty
99
}
100
101
//------------------------------------------------------------------------------
104
inline
void
105
sphere::set
(
const
point
& _p,
scalar
_r)
106
{
107
this->
p
= _p;
108
this->
r
= _r;
109
}
110
111
//------------------------------------------------------------------------------
114
inline
void
115
sphere::set
(
scalar
_x,
scalar
_y,
scalar
_z,
scalar
_r)
116
{
117
this->
p
.set(_x, _y, _z);
118
this->
r
= _r;
119
}
120
121
//------------------------------------------------------------------------------
124
inline
bool
125
sphere::intersects
(
const
sphere
& s)
const
126
{
127
vec3
d(s.
p
-
p
);
128
scalar
rsum = s.
r
+
r
;
129
return
(
lengthsq
(d) <= (rsum * rsum));
130
}
131
132
//------------------------------------------------------------------------------
136
inline
bool
137
sphere::inside
(
const
bbox
& box)
const
138
{
139
vector
v(this->
r
, this->
r
, this->
r
);
140
point
pmin(this->
p
- v);
141
point
pmax(this->
p
+ v);
142
bool
lt =
less_all
(box.
pmin
, pmin);
143
bool
ge =
greaterequal_all
(box.
pmax
, pmax);
144
return
lt && ge;
145
}
146
147
//------------------------------------------------------------------------------
152
inline
153
ClipStatus::Type
154
sphere::clipstatus
(
const
bbox
& box)
const
155
{
156
if
(this->
inside
(box))
return
ClipStatus::Inside
;
157
else
if
(this->
intersects
(box))
return
ClipStatus::Clipped
;
158
else
return
ClipStatus::Outside
;
159
}
160
161
}
// namespace Math
162
//------------------------------------------------------------------------------
bbox.h
Math::ClipStatus::Type
Type
Definition
clipstatus.h:22
Math::ClipStatus::Outside
@ Outside
Definition
clipstatus.h:24
Math::ClipStatus::Inside
@ Inside
Definition
clipstatus.h:23
Math::ClipStatus::Clipped
@ Clipped
Definition
clipstatus.h:25
Math::bbox
Nebula's bounding box class.
Definition
bbox.h:24
Math::bbox::pmax
point pmax
Definition
bbox.h:93
Math::bbox::pmin
point pmin
Definition
bbox.h:92
Math::line
A line in 3d space.
Definition
line.h:22
Math::rectangle
A 2d rectangle class.
Definition
rectangle.h:20
Math::sphere::set
void set(const point &_p, scalar _r)
set position and radius
Definition
sphere.h:105
Math::sphere::sphere
sphere()
default constructor
Definition
sphere.h:62
Math::sphere::intersects
bool intersects(const sphere &s) const
check if 2 spheres overlap
Definition
sphere.h:125
Math::sphere::clipstatus
ClipStatus::Type clipstatus(const bbox &box) const
get clip status of box against sphere
Definition
sphere.h:154
Math::sphere::inside
bool inside(const bbox &box) const
return true if box is completely inside sphere
Definition
sphere.h:137
Math::sphere::random_point_on_unit_sphere
static vec3 random_point_on_unit_sphere()
generate a random point on a unit sphere
Definition
sphere.cc:84
Math::sphere::intersect_sweep
bool intersect_sweep(const vector &va, const sphere &sb, const vector &vb, scalar &u0, scalar &u1) const
check if 2 moving sphere have contact
Definition
sphere.cc:106
Math::sphere::p
point p
Definition
sphere.h:54
Math::sphere::project_screen_rh
rectangle< scalar > project_screen_rh(const mat4 &modelView, const mat4 &projection, scalar nearZ) const
project sphere to screen rectangle (right handed coordinate system)
Definition
sphere.cc:172
Math::sphere::intersects_ray
bool intersects_ray(const line &l) const
check if sphere intersects ray
Definition
sphere.cc:70
Math::sphere::r
scalar r
Definition
sphere.h:55
clipstatus.h
line.h
mat4.h
Math
Different curves.
Definition
angularpfeedbackloop.h:17
Math::lengthsq
__forceinline scalar lengthsq(const quat &q)
Definition
quat.h:269
Math::greaterequal_all
__forceinline bool greaterequal_all(const point &v0, const point &v1)
Definition
point.h:463
Math::less_all
__forceinline bool less_all(const point &v0, const point &v1)
Definition
point.h:397
Math::scalar
float scalar
Definition
scalar.h:45
rectangle.h
Math::mat4
A 4x4 single point precision float matrix.
Definition
mat4.h:49
Math::point
Represents a 3D point in space.
Definition
point.h:22
Math::vec3
A 3D vector.
Definition
vec3.h:37
Math::vector
A vector is a 3D direction in space.
Definition
vector.h:22
vec3.h
code
foundation
math
sphere.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.