13extern int dmat_solve(
int n,
int rhs_num,
double a[]);
24 m_normal =
Vec3f(std::cos(-m_angle), std::sin(-m_angle), 0);
25 m_normalY = m_normal[1] * m_axisDir;
26 m_n2d[0] = std::cos(m_angle);
27 m_n2d[1] = -std::sin(m_angle);
28 m_hcs.FromNormal(m_axisDir);
29 m_angularRotatedRadians = 0;
38 m_angularRotatedRadians(0)
40 if (!
Init(p1, p2, p3, n1, n2, n3))
49 if (samples.
size() < 6)
53 size_t c = samples.
size() >> 1;
54 return Init(samples[0], samples[1], samples[2], samples[
c], samples[
c + 1], samples[
c + 2]);
67 template <
class IteratorT>
76 intptr_t size = end - begin;
77#pragma omp parallel for schedule(static) reduction(+ : chi)
78 for (intptr_t i = 0; i < size; ++i)
81 for (
unsigned int j = 0; j < 3; ++j)
83 values[i] += begin[i][j] * params[j];
85 values[i] -= begin[i][3];
86 chi += values[i] * values[i];
91 template <
class IteratorT>
101 intptr_t size = end - begin;
102#pragma omp parallel for schedule(static)
103 for (intptr_t i = 0; i < size; ++i)
105 for (
unsigned int j = 0; j < 3; ++j)
122 size_t c = samples.
size() / 2;
124#pragma omp parallel for schedule(static)
125 for (intptr_t i = 0; i <
c; ++i)
127 for (
unsigned int j = 0; j < 3; ++j)
129 planes[i][j] = samples[i][j];
131 planes[i][3] = samples[i].dot(samples[i +
c]);
137 double d1 = samples[0].dot(samples[
c + 0]);
138 double d2 = samples[1].dot(samples[
c + 1]);
139 double d3 = samples[2].dot(samples[
c + 2]);
141 a[0 + 0 * 3] = samples[
c + 0][0];
142 a[1 + 0 * 3] = samples[
c + 1][0];
143 a[2 + 0 * 3] = samples[
c + 2][0];
144 a[0 + 1 * 3] = samples[
c + 0][1];
145 a[1 + 1 * 3] = samples[
c + 1][1];
146 a[2 + 1 * 3] = samples[
c + 2][1];
147 a[0 + 2 * 3] = samples[
c + 0][2];
148 a[1 + 2 * 3] = samples[
c + 1][2];
149 a[2 + 2 * 3] = samples[
c + 2][2];
157 m_center[0] = a[0 + 3 * 3];
158 m_center[1] = a[1 + 3 * 3];
159 m_center[2] = a[2 + 3 * 3];
162 LevMar(planes.
begin(), planes.
end(), planeDistance, (
float*)m_center);
165#pragma omp parallel for schedule(static)
166 for (intptr_t i = 0; i <
c; ++i)
169 spoints[i].Normalize();
178#pragma omp parallel for schedule(static) reduction(+ : heightSum)
179 for (intptr_t i = 0; i <
c; ++i)
181 heightSum +=
Height(samples[i]);
188 float angleReduction = 0;
189#pragma omp parallel for schedule(static) reduction(+ : angleReduction)
190 for (intptr_t i = 0; i <
c; ++i)
192 float angle = m_axisDir.dot(samples[i +
c]);
211 angleReduction +=
angle;
214 m_angle = angleReduction;
215 if (m_angle < 1e-6 || m_angle >
float(
M_PI) / 2 - 1e-6)
220 if (m_angle > 1.4835298641951801403851371532153f)
224 m_normal =
Vec3f(std::cos(-m_angle), std::sin(-m_angle), 0);
225 m_normalY = m_normal[1] * m_axisDir;
226 m_n2d[0] = std::cos(m_angle);
227 m_n2d[1] = -std::sin(m_angle);
228 m_hcs.FromNormal(m_axisDir);
229 m_angularRotatedRadians = 0;
236 if (
angle > 1.4835298641951801403851371532153)
243 m_normal =
Vec3f(std::cos(-m_angle), std::sin(-m_angle), 0);
244 m_normalY = m_normal[1] * m_axisDir;
245 m_n2d[0] = std::cos(m_angle);
246 m_n2d[1] = -std::sin(m_angle);
247 m_hcs.FromNormal(m_axisDir);
248 m_angularRotatedRadians = 0;
267 double d1 = p1.
dot(n1);
268 double d2 = p2.
dot(n2);
269 double d3 = p3.
dot(n3);
271 a[0 + 0 * 3] = n1[0];
272 a[1 + 0 * 3] = n2[0];
273 a[2 + 0 * 3] = n3[0];
274 a[0 + 1 * 3] = n1[1];
275 a[1 + 1 * 3] = n2[1];
276 a[2 + 1 * 3] = n3[1];
277 a[0 + 2 * 3] = n1[2];
278 a[1 + 2 * 3] = n2[2];
279 a[2 + 2 * 3] = n3[2];
287 m_center[0] = a[0 + 3 * 3];
288 m_center[1] = a[1 + 3 * 3];
289 m_center[2] = a[2 + 3 * 3];
292 Vec3f s1 = p1 - m_center;
293 Vec3f s2 = p2 - m_center;
294 Vec3f s3 = p3 - m_center;
298 Plane pl(s1 + m_center, s2 + m_center, s3 + m_center);
302 if (m_axisDir.dot(s1) < 0)
307 float angle = m_axisDir.dot(n1);
327 angle = m_axisDir.dot(n2);
347 angle = m_axisDir.dot(n3);
368 if (m_angle < 1e-6 || m_angle >
float(
M_PI) / 2 - 1e-6)
373 if (m_angle > 1.4835298641951801403851371532153f)
377 m_normal =
Vec3f(std::cos(-m_angle), std::sin(-m_angle), 0);
378 m_normalY = m_normal[1] * m_axisDir;
379 m_n2d[0] = std::cos(m_angle);
380 m_n2d[1] = -std::sin(m_angle);
381 m_hcs.FromNormal(m_axisDir);
382 m_angularRotatedRadians = 0;
392 i->read((
char*)&m_center,
sizeof(m_center));
393 i->read((
char*)&m_axisDir,
sizeof(m_axisDir));
394 i->read((
char*)&m_angle,
sizeof(m_angle));
395 i->read((
char*)&rotate,
sizeof(rotate));
399 for (
size_t j = 0; j < 3; ++j)
403 for (
size_t j = 0; j < 3; ++j)
405 (*i) >> m_axisDir[j];
410 m_normal =
Vec3f(std::cos(-m_angle), std::sin(-m_angle), 0);
411 m_normalY = m_normal[1] * m_axisDir;
412 m_n2d[0] = std::cos(m_angle);
413 m_n2d[1] = -std::sin(m_angle);
414 m_hcs.FromNormal(m_axisDir);
415 m_angularRotatedRadians = 0;
424 for (
int i = 0; i < 3; i++)
426 m_center[i] = array[i];
427 m_axisDir[i] = array[3 + i];
432 m_normal =
Vec3f(std::cos(-m_angle), std::sin(-m_angle), 0);
433 m_normalY = m_normal[1] * m_axisDir;
434 m_n2d[0] = std::cos(m_angle);
435 m_n2d[1] = -std::sin(m_angle);
436 m_hcs.FromNormal(m_axisDir);
437 m_angularRotatedRadians = 0;
445 fread(&m_center,
sizeof(m_center), 1, i);
446 fread(&m_axisDir,
sizeof(m_axisDir), 1, i);
447 fread(&m_angle,
sizeof(m_angle), 1, i);
448 fread(&rotate,
sizeof(rotate), 1, i);
449 m_normal =
Vec3f(std::cos(-m_angle), std::sin(-m_angle), 0);
450 m_normalY = m_normal[1] * m_axisDir;
451 m_n2d[0] = std::cos(m_angle);
452 m_n2d[1] = -std::sin(m_angle);
453 m_hcs.FromNormal(m_axisDir);
454 m_angularRotatedRadians = 0;
462 Vec3f s = p - m_center;
463 float g = s.dot(m_axisDir);
466 float sqrS = s.sqrLength();
467 float f = sqrS - (g * g);
476 float da = m_n2d[0] * f;
477 float db = m_n2d[1] * g;
478 if (g < 0 && da - db < 0)
483 float dist = -(da + db);
485 Vec3f plx = s - g * m_axisDir;
487 Vec3f n = m_normal[0] * plx + m_normalY;
495 Vec3f s = p - m_center;
496 float height = m_axisDir.
dot(s);
497 float planex = s.dot(m_hcs[0].Data());
498 float planey = s.dot(m_hcs[1].Data());
499 float l = planex * planex + planey * planey;
505 float angle = std::atan2(planey, planex);
528 float sqrS = s.sqrLength();
529 float f = sqrS - (height * height);
538 float sdist = abs(m_n2d[0] * f + ((height < 0) ? -1 : 1) * m_n2d[1] * height);
539 float length = std::sqrt(sqrS + sdist * sdist);
540 param->first = length;
541 param->second =
angle;
584 q.RotationRad(radians, m_axisDir[0], m_axisDir[1], m_axisDir[2]);
589 m_angularRotatedRadians += radians;
602 for (
unsigned int i = 0; i < 3; ++i)
604 s[i] =
x[i] - param[i];
606 float g = abs(s[0] * param[3] + s[1] * param[4] + s[2] * param[5]);
607 float f = s.sqrLength() - (g * g);
616 return std::cos(param[6]) * f - std::sin(param[6]) * g;
624 for (
unsigned int i = 0; i < 3; ++i)
626 s[i] =
x[i] - param[i];
628 float g = abs(s[0] * param[3] + s[1] * param[4] + s[2] * param[5]);
629 float f = s.sqrLength() - (g * g);
639 for (
unsigned int i = 0; i < 3; ++i)
641 ggradient[i] = -param[i + 3];
643 for (
unsigned int i = 0; i < 3; ++i)
645 ggradient[i + 3] = s[i] - param[i + 3] * g;
650 fgradient[0] = std::sqrt(1 - param[3] * param[3]);
651 fgradient[1] = std::sqrt(1 - param[4] * param[4]);
652 fgradient[2] = std::sqrt(1 - param[5] * param[5]);
656 fgradient[0] = (param[3] * g - s[0]) / f;
657 fgradient[1] = (param[4] * g - s[1]) / f;
658 fgradient[2] = (param[5] * g - s[2]) / f;
660 fgradient[3] = g * fgradient[0];
661 fgradient[4] = g * fgradient[1];
662 fgradient[5] = g * fgradient[2];
663 float sinPhi = -std::sin(param[6]);
664 float cosPhi = std::cos(param[6]);
665 for (
unsigned int i = 0; i < 6; ++i)
667 gradient[i] = cosPhi * fgradient[i] + sinPhi * ggradient[i];
669 gradient[6] = f * sinPhi - g * cosPhi;
677 float l = std::sqrt(param[3] * param[3] + param[4] * param[4] + param[5] * param[5]);
678 for (
unsigned int i = 3; i < 6; ++i)
683 param[6] -= std::floor(param[6] / (2 *
float(
M_PI))) * (2 *
float(
M_PI));
686 param[6] -= std::floor(param[6] /
float(
M_PI)) *
float(
M_PI);
687 for (
unsigned int i = 3; i < 6; ++i)
692 if (param[6] >
float(
M_PI) / 2)
713 Vec3f center(0, 0, 0);
714 Vec3f axisDir(0, 0, 0);
716 for (
size_t i = 0; i < cones.
size(); ++i)
718 center += weights[i] * cones[i].Center();
719 axisDir += weights[i] * cones[i].AxisDirection();
720 omega += weights[i] * cones[i].Angle();
723 return ic->Init(center, axisDir, omega);
731 o->write((
const char*)&m_center,
sizeof(m_center));
732 o->write((
const char*)&m_axisDir,
sizeof(m_axisDir));
733 o->write((
const char*)&m_angle,
sizeof(m_angle));
734 o->write((
const char*)&m_angularRotatedRadians,
sizeof(m_angularRotatedRadians));
738 (*o) << m_center[0] <<
" " << m_center[1] <<
" " << m_center[2] <<
" " << m_axisDir[0]
739 <<
" " << m_axisDir[1] <<
" " << m_axisDir[2] <<
" " << m_angle <<
" "
740 << m_angularRotatedRadians <<
" ";
753 fwrite(&m_center,
sizeof(m_center), 1, o);
754 fwrite(&m_axisDir,
sizeof(m_axisDir), 1, o);
755 fwrite(&m_angle,
sizeof(m_angle), 1, o);
756 fwrite(&m_angularRotatedRadians,
sizeof(m_angularRotatedRadians), 1, o);
768 for (
int i = 0; i < 3; i++)
770 array[i] = m_center[i];
771 array[i + 3] = m_axisDir[i];
774 array[7] = m_angularRotatedRadians;
781 m_center += translate;
789 m_hcs[0] = rot * m_hcs[0];
790 m_hcs[1] = rot * m_hcs[1];
791 m_normalY = m_normal[1] * m_axisDir;
int dmat_solve(int n, int rhs_num, double a[])
float ConeDistance(const float *param, const float *x)
void NormalizeConeParams(float *param)
void ConeDistanceDerivatives(const float *param, const float *x, float *gradient)
bool LevMar(IteratorT begin, IteratorT end, FuncT &func, typename FuncT::ScalarType *param)
static bool Interpolate(const MiscLib::Vector< Cone > &cones, const MiscLib::Vector< float > &weights, Cone *ic)
static size_t SerializedFloatSize()
static size_t SerializedSize()
bool LeastSquaresFit(const PointCloud &pc, MiscLib::Vector< size_t >::const_iterator begin, MiscLib::Vector< size_t >::const_iterator end)
const Vec3f AngularDirection() const
void Transform(float scale, const Vec3f &translate)
void RotateAngularDirection(float radians)
float Height(const Vec3f &p) const
void Project(const Vec3f &p, Vec3f *pp) const
void Serialize(bool binary, std::ostream *o) const
bool InitAverage(const MiscLib::Vector< Vec3f > &samples)
bool Init(const MiscLib::Vector< Vec3f > &samples)
void Parameters(const Vec3f &p, std::pair< float, float > *param) const
void Derivatives(const ScalarType *params, IteratorT begin, IteratorT end, ScalarType *values, ScalarType *temp, ScalarType *matrix) const
ScalarType Chi(const ScalarType *params, IteratorT begin, IteratorT end, ScalarType *values, ScalarType *temp) const
void Normalize(ScalarType *) const
const Vec3f & getNormal() const
float dot(const Vec3f &v) const
VectorXD< 3, float > Vector3Df
IndexedIterator< IndexIteratorT, IteratorT > IndexIterate(IndexIteratorT idxIt, IteratorT it)
bool MeanOfNormals(NormalsItT begin, NormalsItT end, WeightsItT weights, MeanT *mean)
This file offers overloads of toIce() and fromIce() functions for STL container types.
double angle(const Point &a, const Point &b, const Point &c)