26 #ifndef WFMATH_BALL_FUNCS_H
27 #define WFMATH_BALL_FUNCS_H
29 #include <wfmath/ball.h>
31 #include <wfmath/axisbox.h>
32 #include <wfmath/miniball.h>
39 inline bool Ball<dim>::isEqualTo(
const Ball<dim>& b,
double epsilon)
const
41 return Equal(m_center, b.m_center, epsilon)
42 &&
Equal(m_radius, b.m_radius, epsilon);
46 AxisBox<dim> Ball<dim>::boundingBox()
const
48 Point<dim> p_low, p_high;
50 for(
int i = 0; i < dim; ++i) {
51 p_low[i] = m_center[i] - m_radius;
52 p_high[i] = m_center[i] + m_radius;
55 bool valid = m_center.isValid();
57 p_low.setValid(valid);
58 p_high.setValid(valid);
60 return AxisBox<dim>(p_low, p_high,
true);
63 template<
int dim,
template<
class,
class>
class container>
66 _miniball::Miniball<dim> m;
67 _miniball::Wrapped_array<dim> w;
69 typename container<Point<dim>, std::allocator<Point<dim> > >::const_iterator i, end = c.end();
72 for(i = c.begin(); i != end; ++i) {
73 valid = valid && i->isValid();
74 for(
int j = 0; j < dim; ++j)
84 assert(
"Check that bounding sphere is good to library accuracy" &&
85 m.accuracy(dummy) < WFMATH_EPSILON);
90 for(
int j = 0; j < dim; ++j)
95 return Ball<dim>(center, std::sqrt(m.squared_radius()));
98 template<
int dim,
template<
class,
class>
class container>
105 typename container<Point<dim>, std::allocator<Point<dim> > >::const_iterator i = c.begin(),
112 typename container<Point<dim>, std::allocator<Point<dim> > >::const_iterator min_p[dim], max_p[dim];
113 bool valid = i->isValid();
115 for(
int j = 0; j < dim; ++j) {
116 min[j] = max[j] = (*i)[j];
117 min_p[j] = max_p[j] = i;
121 valid = valid && i->isValid();
122 for(
int j = 0; j < dim; ++j) {
123 if(min[j] > (*i)[j]) {
127 if(max[j] < (*i)[j]) {
137 for(
int j = 0; j < dim; ++j) {
139 if(new_span > span) {
145 assert(
"Have a direction of maximum size" && direction != -1);
148 CoordType dist = SloppyDistance(*(min_p[direction]), center);
150 for(i = c.begin(); i != end; ++i) {
151 if(i == min_p[direction] || i == max_p[direction])
154 CoordType new_dist = SloppyDistance(*i, center);
156 if(new_dist > dist) {
157 CoordType delta_dist = (new_dist - dist) / 2;
160 center += (*i - center) * delta_dist / new_dist;
162 assert(
"Shifted ball contains new point" &&
163 SquaredDistance(*i, center) <= dist * dist);
176 inline Ball<dim> Point<dim>::boundingSphere()
const
178 return Ball<dim>(*
this, 0);
182 inline Ball<dim> Point<dim>::boundingSphereSloppy()
const
184 return Ball<dim>(*
this, 0);
189 #endif // WFMATH_BALL_FUNCS_H
bool Equal(const C &c1, const C &c2, double epsilon=WFMATH_EPSILON)
Test for equality up to precision epsilon.
Definition: const.h:103
Ball< dim > BoundingSphereSloppy(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get a bounding sphere for a set of points
Definition: ball_funcs.h:99
void setValid(bool valid=true)
make isValid() return true if you've initialized the point by hand
Definition: point.h:129
Point< dim > Midpoint(const Point< dim > &p1, const Point< dim > &p2, CoordType dist=0.5)
Definition: point_funcs.h:270
float CoordType
Basic floating point type.
Definition: const.h:79
Ball< dim > BoundingSphere(const container< Point< dim >, std::allocator< Point< dim > > > &c)
get the minimal bounding sphere for a set of points
Definition: ball_funcs.h:64
A dim dimensional point.
Definition: const.h:50
A dim dimensional ball.
Definition: ball.h:34