Euphoria
sphere.builder.cc
Go to the documentation of this file.
1 #include "core/sphere.builder.h"
2 
3 #include "assert/assert.h"
4 
5 #include "base/cint.h"
6 #include "base/numeric.h"
7 
8 
9 namespace eu::core
10 {
11  vec3f
12  calc_center(const std::vector<vec3f>& positions)
13  {
14  ASSERT(positions.empty() == false);
15 
16  auto center = zero3f;
17 
18  for(const auto& p: positions)
19  {
20  center += p;
21  }
22 
23  return center / c_sizet_to_float(positions.size());
24  }
25 
26  SphereAndPosition
27  build_bounding_sphere(const std::vector<vec3f>& positions)
28  {
29  ASSERT(positions.empty() == false);
30 
31  const auto center = calc_center(positions);
32  float radius_squared = 0;
33 
34  for(const auto& p: positions)
35  {
36  const auto lensq = vec3f::from_to(p, center).get_length_squared();
37  if(lensq > radius_squared)
38  {
39  radius_squared = lensq;
40  }
41  }
42 
43  return
44  {
45  Sphere{sqrt(radius_squared)},
46  center
47  };
48  }
49 }
#define ASSERT(x)
Definition: assert.h:29
SphereAndPosition build_bounding_sphere(const std::vector< vec3f > &positions)
vec3f calc_center(const std::vector< vec3f > &positions)
float sqrt(float r)
Definition: numeric.cc:101
constexpr float c_sizet_to_float(std::size_t f)
Definition: cint.h:43
constexpr vec3f zero3f
Definition: vec3.h:95
constexpr float get_length_squared() const
Definition: vec3.h:79
static vec3f from_to(const vec3f &from, const vec3f &to)
Definition: vec3.cc:127