22 :
public std::runtime_error
30 enum { RequiredSamples = 2 };
40 bool Init(
bool binary, std::istream* i);
42 void Init(
float* array);
43 inline float Distance(
const Vec3f& p)
const;
44 inline void Normal(
const Vec3f& p,
Vec3f* normal)
const;
45 inline float DistanceAndNormal(
const Vec3f& p,
Vec3f* normal)
const;
46 inline float SignedDistance(
const Vec3f& p)
const;
48 const Vec3f& Center()
const;
61 template<
class IteratorT >
62 bool LeastSquaresFit(IteratorT begin, IteratorT end);
67 return LeastSquaresFit(
pc, begin, end);
71 void Serialize(
bool binary, std::ostream* o)
const;
72 static size_t SerializedSize();
73 void Serialize(FILE* o)
const;
74 void Serialize(
float* array)
const;
75 static size_t SerializedFloatSize();
77 void Transform(
float scale,
const Vec3f& translate);
78 inline unsigned int Intersect(
const Vec3f& p,
const Vec3f& r,
79 float* first,
float* second)
const;
82 template<
class WeightT >
83 class LevMarSimpleSphere
87 enum { NumParams = 4 };
88 typedef float ScalarType;
90 template<
class IteratorT >
91 ScalarType Chi(
const ScalarType* params, IteratorT begin, IteratorT end,
92 ScalarType*
values, ScalarType* temp)
const
95 int size = end - begin;
96 #pragma omp parallel for schedule(static) reduction(+:chi)
97 for (
int idx = 0; idx < size; ++idx)
99 float s = begin[idx][0] - params[0];
101 for (
unsigned int j = 1; j < 3; ++j)
103 float ss = begin[idx][j] - params[j];
112 template<
class IteratorT >
113 void Derivatives(
const ScalarType* params, IteratorT begin, IteratorT end,
114 const ScalarType*
values,
const ScalarType* temp, ScalarType* matrix)
const
116 int size = end - begin;
117 #pragma omp parallel for schedule(static)
118 for (
int idx = 0; idx < size; ++idx)
121 s[0] = begin[idx][0] - params[0];
122 float sl =
s[0] *
s[0];
123 for (
unsigned int i = 1; i < 3; ++i)
125 s[i] = begin[idx][i] - params[i];
129 matrix[idx * NumParams + 0] = -
s[0] / sl;
130 matrix[idx * NumParams + 1] = -
s[1] / sl;
131 matrix[idx * NumParams + 2] = -
s[2] / sl;
132 matrix[idx * NumParams + 3] = -1;
133 WeightT::template DerivWeigh< NumParams >(sl - params[3],
134 matrix + idx * NumParams);
138 void Normalize(ScalarType*)
const
142 template<
class WeightT >
147 enum { NumParams = 7 };
148 typedef float ScalarType;
154 template<
class IteratorT >
155 ScalarType Chi(
const ScalarType* params, IteratorT begin, IteratorT end,
156 ScalarType*
values, ScalarType* temp)
const
159 ScalarType radius = 1 / params[6];
160 Vec3f center = -radius *
Vec3f(params[0], params[1], params[2])
161 +
Vec3f(params[3], params[4], params[5]);
162 int size = end - begin;
163 #pragma omp parallel for schedule(static) reduction(+:chi)
164 for (
int idx = 0; idx < size; ++idx)
166 temp[idx] = (begin[idx] - center).length();
167 chi += (
values[idx] = WeightT::Weigh(temp[idx] - radius))
173 template<
class IteratorT >
174 void Derivatives(
const ScalarType* params, IteratorT begin, IteratorT end,
175 const ScalarType*
values,
const ScalarType* temp, ScalarType* matrix)
const
177 Vec3f normal(params[0], params[1], params[2]);
178 Vec3f point(params[3], params[4], params[5]);
179 int size = end - begin;
180 #pragma omp parallel for schedule(static)
181 for (
int idx = 0; idx < size; ++idx)
183 ScalarType denominator = -1.f / temp[idx] * params[6];
184 matrix[idx * NumParams + 0] =
185 (matrix[idx * NumParams + 3] = (point[0] - normal[0] * params[6] - begin[idx][0]))
187 matrix[idx * NumParams + 1] =
188 (matrix[idx * NumParams + 4] = (point[1] - normal[1] * params[6] - begin[idx][1]))
190 matrix[idx * NumParams + 2] =
191 (matrix[idx * NumParams + 5] = (point[2] - normal[2] * params[6] - begin[idx][2]))
193 matrix[idx * NumParams + 3] /= temp[idx];
194 matrix[idx * NumParams + 4] /= temp[idx];
195 matrix[idx * NumParams + 5] /= temp[idx];
196 matrix[idx * NumParams + 6] = (normal[0] * matrix[idx * NumParams + 3]
197 + normal[1] * matrix[idx * NumParams + 4]
198 + normal[2] * matrix[idx * NumParams + 5] + 1) * params[6] * params[6];
199 WeightT::template DerivWeigh< NumParams >(temp[idx] - 1.f / params[6],
200 matrix + idx * NumParams);
204 void Normalize(ScalarType* params)
const
206 ScalarType len =
std::sqrt(params[0] * params[0]
207 + params[1] * params[1] + params[2] * params[2]);
221 return abs((m_center - p).length() - m_radius);
226 *normal = p - m_center;
232 *normal = p - m_center;
233 float l = normal->
length();
238 return abs(l - m_radius);
243 return (m_center - p).length() - m_radius;
246 template<
class IteratorT >
249 LevMarSimpleSphere< LevMarLSWeight > levMarSphere;
251 for (
size_t i = 0; i < 3; ++i)
253 param[i] = m_center[i];
256 if (!
LevMar(begin, end, levMarSphere, param))
260 for (
size_t i = 0; i < 3; ++i)
262 m_center[i] = param[i];
269 float* first,
float* second)
const
272 Vec3f kDiff = p - m_center;
273 float fA0 = kDiff.
dot(kDiff) - m_radius * m_radius;
274 float fA1, fDiscr, fRoot;
279 fDiscr = fA1 * fA1 - fA0;
280 fRoot =
sqrt(fDiscr);
281 *first = -fA1 + fRoot;
290 fDiscr = fA1 * fA1 - fA0;
295 else if (fDiscr >= 1e-7f)
297 fRoot =
sqrt(fDiscr);
298 *first = -fA1 - fRoot;
299 *second = -fA1 + fRoot;
311 const Vec3f& planeNormal);
315 float Parameters(
const Vec3f& p,
316 std::pair< float, float >* param)
const;
317 bool InSpace(
const std::pair< float, float >& param,
bool lower,
319 bool InSpace(
const std::pair< float, float >& param,
bool lower,
323 void HyperplaneCoordinateSystem(
Vec3f* hcs0,
Vec3f* hcs1,
Vec3f* hcs2)
const;
326 void Hemisphere2Disk(
const Vec3f& p,
327 std::pair< float, float >* inDisk)
const;
328 void Disk2Square(
const std::pair< float, float >& inDisk,
329 std::pair< float, float >* inSquare)
const;
330 void Square2Disk(
const std::pair< float, float >& inSquare,
331 std::pair< float, float >* inDisk)
const;
332 void Disk2Hemisphere(
const std::pair< float, float >& inDisk,