9 extern int dmat_solve(
int n,
int rhs_num,
double a[]);
65 a[0 + 0 * 3] = tetra[0 + 1 * 3] - tetra[0 + 0 * 3];
66 a[0 + 1 * 3] = tetra[1 + 1 * 3] - tetra[1 + 0 * 3];
67 a[0 + 2 * 3] = tetra[2 + 1 * 3] - tetra[2 + 0 * 3];
68 a[0 + 3 * 3] = std::pow(tetra[0 + 1 * 3] - tetra[0 + 0 * 3], 2)
69 + std::pow(tetra[1 + 1 * 3] - tetra[1 + 0 * 3], 2)
70 + std::pow(tetra[2 + 1 * 3] - tetra[2 + 0 * 3], 2);
72 a[1 + 0 * 3] = tetra[0 + 2 * 3] - tetra[0 + 0 * 3];
73 a[1 + 1 * 3] = tetra[1 + 2 * 3] - tetra[1 + 0 * 3];
74 a[1 + 2 * 3] = tetra[2 + 2 * 3] - tetra[2 + 0 * 3];
75 a[1 + 3 * 3] = std::pow(tetra[0 + 2 * 3] - tetra[0 + 0 * 3], 2)
76 + std::pow(tetra[1 + 2 * 3] - tetra[1 + 0 * 3], 2)
77 + std::pow(tetra[2 + 2 * 3] - tetra[2 + 0 * 3], 2);
79 a[2 + 0 * 3] = tetra[0 + 3 * 3] - tetra[0 + 0 * 3];
80 a[2 + 1 * 3] = tetra[1 + 3 * 3] - tetra[1 + 0 * 3];
81 a[2 + 2 * 3] = tetra[2 + 3 * 3] - tetra[2 + 0 * 3];
82 a[2 + 3 * 3] = std::pow(tetra[0 + 3 * 3] - tetra[0 + 0 * 3], 2)
83 + std::pow(tetra[1 + 3 * 3] - tetra[1 + 0 * 3], 2)
84 + std::pow(tetra[2 + 3 * 3] - tetra[2 + 0 * 3], 2);
95 for (
size_t i = 0; i <
DIM_NUM; ++i)
106 (
a[0 + 3 * 3] *
a[0 + 3 * 3]
107 +
a[1 + 3 * 3] *
a[1 + 3 * 3]
108 +
a[2 + 3 * 3] *
a[2 + 3 * 3]);
110 pc[0] = tetra[0 + 0 * 3] + 0.5 *
a[0 + 3 * 3];
111 pc[1] = tetra[1 + 0 * 3] + 0.5 *
a[1 + 3 * 3];
112 pc[2] = tetra[2 + 0 * 3] + 0.5 *
a[2 + 3 * 3];
122 float d1343, d4321, d1321, d4343, d2121;
123 float numer, denom, mua, mub;
128 d1343 = p13[0] * n2[0] + p13[1] * n2[1] + p13[2] * n2[2];
129 d4321 = n2[0] * n1[0] + n2[1] * n1[1] + n2[2] * n1[2];
130 d1321 = p13[0] * n1[0] + p13[1] * n1[1] + p13[2] * n1[2];
131 d4343 = n2[0] * n2[0] + n2[1] * n2[1] + n2[2] * n2[2];
132 d2121 = n1[0] * n1[0] + n1[1] * n1[1] + n1[2] * n1[2];
134 denom = d2121 * d4343 - d4321 * d4321;
135 if (
abs(denom) < 1e-6)
139 numer = d1343 * d4321 - d1321 * d4343;
142 mub = (d1343 + d4321 * (mua)) / d4343;
147 *mid = 0.5f * (pa + pb);
152 :
std::runtime_error(
"Invalid tetrahedon")
166 if (!
Init(p1, p2, p3, p4))
174 if (samples.
size() < 4)
179 size_t c = samples.
size() / 2;
180 m_center =
Vec3f(0, 0, 0);
182 for (
size_t i = 0; i <
c - 1; ++i)
183 for (
size_t j = i + 1; j <
c; ++j)
186 if (!
Midpoint(samples[i], samples[i +
c], samples[j], samples[j +
c], &mid))
197 m_center /= midCount;
199 for (
size_t i = 0; i <
c; ++i)
201 float d = (samples[i] - m_center).length();
213 for (
size_t i = 0; i < 3; ++i)
215 tetra[0 * 3 + i] = p1[i];
217 for (
size_t i = 0; i < 3; ++i)
219 tetra[1 * 3 + i] = p2[i];
221 for (
size_t i = 0; i < 3; ++i)
223 tetra[2 * 3 + i] = p3[i];
225 for (
size_t i = 0; i < 3; ++i)
227 tetra[3 * 3 + i] = p4[i];
252 float d1343, d4321, d1321, d4343, d2121;
253 float numer, denom, mua, mub;
258 d1343 = p13[0] * n2[0] + p13[1] * n2[1] + p13[2] * n2[2];
259 d4321 = n2[0] * n1[0] + n2[1] * n1[1] + n2[2] * n1[2];
260 d1321 = p13[0] * n1[0] + p13[1] * n1[1] + p13[2] * n1[2];
261 d4343 = n2[0] * n2[0] + n2[1] * n2[1] + n2[2] * n2[2];
262 d2121 = n1[0] * n1[0] + n1[1] * n1[1] + n1[2] * n1[2];
264 denom = d2121 * d4343 - d4321 * d4321;
265 if (
abs(denom) < 1e-6)
269 numer = d1343 * d4321 - d1321 * d4343;
272 mub = (d1343 + d4321 * (mua)) / d4343;
279 m_center = 0.5f * (pa + pb);
281 float da = (p1 - m_center).length();
282 float db = (p2 - m_center).length();
283 m_radius = 0.5f * (da + db);
286 float dev = da / m_radius;
287 if (dev < 0.9f || dev > 1.1f)
292 if (dev < 0.9f || dev > 1.1f)
297 dev = (pa - pb).length() / m_radius;
309 i->read((
char*)&m_center,
sizeof(m_center));
310 i->read((
char*)&m_radius,
sizeof(m_radius));
314 for (
size_t j = 0; j < 3; ++j)
325 fread(&m_center,
sizeof(m_center), 1, i);
326 fread(&m_radius,
sizeof(m_radius), 1, i);
331 for (
int i = 0; i < 3; i++)
333 m_center[i] = array[i];
358 float s = x[0] - param[0];
360 for (
unsigned int i = 1; i < 3; ++i)
362 float ss = x[i] - param[i];
372 s[0] = x[0] - param[0];
373 float sl =
s[0] *
s[0];
374 for (
unsigned int i = 1; i < 3; ++i)
376 s[i] = x[i] - param[i];
380 gradient[0] = -
s[0] / sl;
381 gradient[1] = -
s[1] / sl;
382 gradient[2] = -
s[2] / sl;
401 Vec3f center(0, 0, 0);
403 for (
size_t i = 0; i < spheres.
size(); ++i)
405 center += weights[i] * spheres[i].Center();
406 radius += weights[i] * spheres[i].Radius();
417 o->write((
const char*)&m_center,
sizeof(m_center));
418 o->write((
const char*)&m_radius,
sizeof(m_radius));
422 (*o) << m_center[0] <<
" " << m_center[1] <<
" " << m_center[2] <<
" "
440 fwrite(&m_center,
sizeof(m_center), 1, o);
441 fwrite(&m_radius,
sizeof(m_radius), 1, o);
446 for (
int i = 0; i < 3; i++)
448 array[i] = m_center[i];
457 m_center += translate;
462 const Vec3f& planeNormal)
464 , m_planeNormal(planeNormal)
469 const Vec3f& planeNormal)
472 m_planeNormal = planeNormal;
473 m_hcs.FromNormal(planeNormal[0], planeNormal[1], planeNormal[2]);
477 std::pair< float, float >* param)
const
483 hs[0] =
s.dot(m_hcs[0].Data());
484 hs[1] =
s.dot(m_hcs[1].Data());
485 hs[2] =
s.dot(m_planeNormal);
488 std::pair< float, float > inDisk;
489 Hemisphere2Disk(hs, &inDisk);
490 Disk2Square(inDisk, param);
495 const std::pair< float, float >& param,
bool lower,
Vec3f* p)
const
497 if (param.first < -0.1 || param.first > 1.1
498 || param.second < -0.1 || param.second > 1.1)
502 std::pair< float, float > clampedParam;
505 std::pair< float, float > inDisk;
506 Square2Disk(clampedParam, &inDisk);
508 Disk2Hemisphere(inDisk, &
s);
509 *p =
Vec3f((
s[0] * m_hcs[0] +
s[1] * m_hcs[1] +
517 const std::pair< float, float >& param,
bool lower,
Vec3f* p,
520 if (param.first < -0.1 || param.first > 1.1
521 || param.second < -0.1 || param.second > 1.1)
525 std::pair< float, float > clampedParam;
528 std::pair< float, float > inDisk;
529 Square2Disk(clampedParam, &inDisk);
531 Disk2Hemisphere(inDisk, &
s);
536 *n =
Vec3f((
s[0] * m_hcs[0] +
s[1] * m_hcs[1] +
538 *p = m_sphere.
Radius() * (*n);
547 + trans).Data()), m_sphere.
Radius());
549 m_hcs[0] = rot * m_hcs[0];
550 m_hcs[1] = rot * m_hcs[1];
561 void SphereAsSquaresParametrization::Hemisphere2Disk(
const Vec3f& p,
562 std::pair< float, float >* inDisk)
const
565 inDisk->second = std::atan2(p[1], p[0]);
568 void SphereAsSquaresParametrization::Disk2Square(
569 const std::pair< float, float >& inDisk,
570 std::pair< float, float >* inSquare)
const
572 float r = inDisk.first;
573 float phi = inDisk.second;
576 if (phi <
float(-
M_PI / 4.0))
581 if (phi <
float(
M_PI / 4.0))
586 else if (phi <
float(3 *
M_PI / 4.0))
591 else if (phi <
float(5 *
M_PI / 4.0))
602 inSquare->first = (
a +
float(1.0)) /
float(2.0);
603 inSquare->second = (b +
float(1.0)) /
float(2.0);
606 void SphereAsSquaresParametrization::Square2Disk(
607 const std::pair< float, float >& inSquare,
608 std::pair< float, float >* inDisk)
const
611 float a = 2 * inSquare.first - 1;
612 float b = 2 * inSquare.second - 1;
649 inDisk->second = phi;
652 void SphereAsSquaresParametrization::Disk2Hemisphere(
653 const std::pair< float, float >& inDisk,
Vec3f* p)
const
655 (*p)[0] = inDisk.first *
std::sqrt(2 - inDisk.first * inDisk.first)
656 * std::cos(inDisk.second);
657 (*p)[1] = inDisk.first *
std::sqrt(2 - inDisk.first * inDisk.first)
658 * std::sin(inDisk.second);
659 (*p)[2] = 1 - inDisk.first * inDisk.first;