Unravel Engine C++ Reference
Loading...
Searching...
No Matches
bsphere.cpp
Go to the documentation of this file.
1#include "bsphere.h"
2#include <limits>
3namespace math
4{
6// Static Member Definitions
8bsphere bsphere::empty(0, 0, 0, 0);
9#define BIGNUMBER 100000000.0 // Hundred million
10
12// BoundingSphere Member Functions
14//-----------------------------------------------------------------------------
15// Name : fromPoints ()
19//-----------------------------------------------------------------------------
20bsphere& bsphere::from_points(const char* point_buffer, unsigned int point_count, unsigned int point_stride)
21{
22 static const float big_number = std::numeric_limits<float>::max();
23 vec3 xmin(big_number, big_number, big_number), xmax(-big_number, -big_number, -big_number),
24 ymin(big_number, big_number, big_number), ymax(-big_number, -big_number, -big_number),
25 zmin(big_number, big_number, big_number), zmax(-big_number, -big_number, -big_number), dia1, dia2;
26
27 // FIRST PASS: find 6 minima/maxima points
28 const char* points = point_buffer;
29 for(unsigned int i = 0; i < point_count; ++i, points += point_stride)
30 {
31 const vec3& p = *(vec3*)points;
32 if(p.x < xmin.x)
33 xmin = p; // New xminimum point
34 if(p.x > xmax.x)
35 xmax = p;
36 if(p.y < ymin.y)
37 ymin = p;
38 if(p.y > ymax.y)
39 ymax = p;
40 if(p.z < zmin.z)
41 zmin = p;
42 if(p.z > zmax.z)
43 zmax = p;
44 }
45
46 // Set xspan = distance between the 2 points xmin & xmax (squared)
47 float dx = xmax.x - xmin.x;
48 float dy = xmax.y - xmin.y;
49 float dz = xmax.z - xmin.z;
50 float xspan = dx * dx + dy * dy + dz * dz;
51
52 // Same for y & z spans
53 dx = ymax.x - ymin.x;
54 dy = ymax.y - ymin.y;
55 dz = ymax.z - ymin.z;
56 float yspan = dx * dx + dy * dy + dz * dz;
57
58 dx = zmax.x - zmin.x;
59 dy = zmax.y - zmin.y;
60 dz = zmax.z - zmin.z;
61 float zspan = dx * dx + dy * dy + dz * dz;
62
63 // Set points dia1 & dia2 to the maximally separated pair
64 dia1 = xmin;
65 dia2 = xmax; // assume xspan biggest
66 float maxspan = xspan;
67
68 if(yspan > maxspan)
69 {
70 maxspan = yspan;
71 dia1 = ymin;
72 dia2 = ymax;
73 }
74
75 if(zspan > maxspan)
76 {
77 dia1 = zmin;
78 dia2 = zmax;
79 }
80
81 // dia1,dia2 is a diameter of initial sphere
82 // calc initial center
83 position.x = (dia1.x + dia2.x) * 0.5f;
84 position.y = (dia1.y + dia2.y) * 0.5f;
85 position.z = (dia1.z + dia2.z) * 0.5f;
86 // calculate initial radius**2 and radius
87 dx = dia2.x - position.x; // x component of radius vector
88 dy = dia2.y - position.y; // y component of radius vector
89 dz = dia2.z - position.z; // z component of radius vector
90 float radiusSq = dx * dx + dy * dy + dz * dz;
91 radius = glm::sqrt(radiusSq);
92
93 // SECOND PASS: increment current sphere
94 points = point_buffer;
95 for(unsigned int i = 0; i < point_count; ++i, points += point_stride)
96 {
97 const vec3& p = *(vec3*)points;
98 dx = p.x - position.x;
99 dy = p.y - position.y;
100 dz = p.z - position.z;
101 float old_to_p_sq = dx * dx + dy * dy + dz * dz;
102 if(old_to_p_sq > radiusSq) // do r**2 test first
103 {
104 // this point is outside of current sphere
105 float old_to_p = glm::sqrt(old_to_p_sq);
106 // calc radius of new sphere
107 radius = (radius + old_to_p) * 0.5f;
108 radiusSq = radius * radius; // for next r**2 compare
109 float old_to_new = old_to_p - radius;
110 // calc center of new sphere
111 float recip = 1.0f / old_to_p;
112
113 position.x = (radius * position.x + old_to_new * p.x) * recip;
114 position.y = (radius * position.y + old_to_new * p.y) * recip;
115 position.z = (radius * position.z + old_to_new * p.z) * recip;
116 }
117 }
118 return *this;
119}
120} // namespace math
Provides storage for common representation of spherical bounding volume, and wraps up common function...
Definition bsphere.h:18
float radius
Definition bsphere.h:120
static bsphere empty
Definition bsphere.h:125
vec3 position
Definition bsphere.h:119
bsphere & from_points(const char *point_buffer, unsigned int point_count, unsigned int point_stride)
Calculates a tight fitting bounding sphere from the points supplied.
Definition bsphere.cpp:20
Definition bbox.cpp:5