Nebula
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//------------------------------------------------------------------------------
20namespace Math
21{
22class sphere
23{
24public:
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;
53
56};
57
58//------------------------------------------------------------------------------
61inline
63 r(1.0f)
64{
65 // empty
66}
67
68//------------------------------------------------------------------------------
71inline
72sphere::sphere(const point& _p, scalar _r) :
73 p(_p),
74 r(_r)
75{
76 // empty
77}
78
79//------------------------------------------------------------------------------
82inline
84 p(_x, _y, _z),
85 r(_r)
86{
87 // empty
88}
89
90//------------------------------------------------------------------------------
93inline
95 p(rhs.p),
96 r(rhs.r)
97{
98 // empty
99}
100
101//------------------------------------------------------------------------------
104inline void
106{
107 this->p = _p;
108 this->r = _r;
109}
110
111//------------------------------------------------------------------------------
114inline void
116{
117 this->p.set(_x, _y, _z);
118 this->r = _r;
119}
120
121//------------------------------------------------------------------------------
124inline bool
126{
127 vec3 d(s.p - p);
128 scalar rsum = s.r + r;
129 return (lengthsq(d) <= (rsum * rsum));
130}
131
132//------------------------------------------------------------------------------
136inline bool
137sphere::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//------------------------------------------------------------------------------
152inline
154sphere::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//------------------------------------------------------------------------------
Type
Definition clipstatus.h:22
@ Outside
Definition clipstatus.h:24
@ Inside
Definition clipstatus.h:23
@ Clipped
Definition clipstatus.h:25
Nebula's bounding box class.
Definition bbox.h:24
point pmax
Definition bbox.h:93
point pmin
Definition bbox.h:92
A line in 3d space.
Definition line.h:22
A 2d rectangle class.
Definition rectangle.h:20
A 3-dimensional sphere.
Definition sphere.h:23
void set(const point &_p, scalar _r)
set position and radius
Definition sphere.h:105
sphere()
default constructor
Definition sphere.h:62
bool intersects(const sphere &s) const
check if 2 spheres overlap
Definition sphere.h:125
ClipStatus::Type clipstatus(const bbox &box) const
get clip status of box against sphere
Definition sphere.h:154
bool inside(const bbox &box) const
return true if box is completely inside sphere
Definition sphere.h:137
static vec3 random_point_on_unit_sphere()
generate a random point on a unit sphere
Definition sphere.cc:84
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
point p
Definition sphere.h:54
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
bool intersects_ray(const line &l) const
check if sphere intersects ray
Definition sphere.cc:70
scalar r
Definition sphere.h:55
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline scalar lengthsq(const quat &q)
Definition quat.h:268
__forceinline bool greaterequal_all(const point &v0, const point &v1)
Definition point.h:463
__forceinline bool less_all(const point &v0, const point &v1)
Definition point.h:397
float scalar
Definition scalar.h:45
A 4x4 single point precision float matrix.
Definition mat4.h:49
Represents a 3D point in space.
Definition point.h:22
void set(scalar x, scalar y, scalar z)
set content
Definition point.h:332
A 3D vector.
Definition vec3.h:40
A vector is a 3D direction in space.
Definition vector.h:22