Wildmeshing Toolkit
Loading...
Searching...
No Matches
AMIPS.h
1#pragma once
2
3#include <Eigen/Core>
4
5#include <array>
6#include <cmath>
7
8namespace wmtk {
9double AMIPS_energy(const std::array<double, 12>& T);
10void AMIPS_jacobian(const std::array<double, 12>& T, Eigen::Vector3d& result_0);
11void AMIPS_hessian(const std::array<double, 12>& T, Eigen::Matrix3d& result_0);
12template <typename rational, typename dtype>
13double AMIPS_energy_rational_p3(const std::array<dtype, 12>& T){
14 std::array<rational, 12> r_T;
15 for (int j = 0; j < 12; j++) r_T[j] = T[j];
16 const rational twothird = rational(2) / rational(3);
17 auto tmp = ((-r_T[1 + 2] + r_T[1 + 5]) * r_T[1 + 1] + r_T[1 + 2] * r_T[1 + 7] +
18 (r_T[1 + -1] - r_T[1 + 5]) * r_T[1 + 4] - r_T[1 + -1] * r_T[1 + 7]) *
19 r_T[1 + 9] +
20 ((r_T[1 + 2] - r_T[1 + 5]) * r_T[1 + 0] - r_T[1 + 2] * r_T[1 + 6] +
21 (-r_T[1 + -1] + r_T[1 + 5]) * r_T[1 + 3] + r_T[1 + -1] * r_T[1 + 6]) *
22 r_T[1 + 10] +
23 (-r_T[1 + 2] * r_T[1 + 7] + (-r_T[1 + 8] + r_T[1 + 5]) * r_T[1 + 4] +
24 r_T[1 + 8] * r_T[1 + 7]) *
25 r_T[1 + 0] +
26 (r_T[1 + 2] * r_T[1 + 6] + (r_T[1 + 8] - r_T[1 + 5]) * r_T[1 + 3] -
27 r_T[1 + 8] * r_T[1 + 6]) *
28 r_T[1 + 1] +
29 (r_T[1 + 3] * r_T[1 + 7] - r_T[1 + 4] * r_T[1 + 6]) * (r_T[1 + -1] - r_T[1 + 8]);
30 if (tmp == 0) return std::numeric_limits<double>::infinity();
31
32 auto res_r =
33 rational(27) / 16 * pow(tmp, -2) *
34 pow(r_T[1 + 9] * r_T[1 + 9] +
35 (-twothird * r_T[1 + 0] - twothird * r_T[1 + 3] - twothird * r_T[1 + 6]) *
36 r_T[1 + 9] +
37 r_T[1 + 10] * r_T[1 + 10] +
38 (-twothird * r_T[1 + 1] - twothird * r_T[1 + 4] - twothird * r_T[1 + 7]) *
39 r_T[1 + 10] +
40 r_T[1 + 0] * r_T[1 + 0] +
41 (-twothird * r_T[1 + 3] - twothird * r_T[1 + 6]) * r_T[1 + 0] +
42 r_T[1 + 1] * r_T[1 + 1] +
43 (-twothird * r_T[1 + 4] - twothird * r_T[1 + 7]) * r_T[1 + 1] +
44 r_T[1 + 2] * r_T[1 + 2] +
45 (-twothird * r_T[1 + -1] - twothird * r_T[1 + 8] - twothird * r_T[1 + 5]) *
46 r_T[1 + 2] +
47 r_T[1 + 3] * r_T[1 + 3] - twothird * r_T[1 + 3] * r_T[1 + 6] +
48 r_T[1 + 4] * r_T[1 + 4] - twothird * r_T[1 + 4] * r_T[1 + 7] +
49 r_T[1 + 5] * r_T[1 + 5] +
50 (-twothird * r_T[1 + -1] - twothird * r_T[1 + 8]) * r_T[1 + 5] -
51 twothird * r_T[1 + -1] * r_T[1 + 8] + r_T[1 + -1] * r_T[1 + -1] +
52 r_T[1 + 8] * r_T[1 + 8] + r_T[1 + 6] * r_T[1 + 6] + r_T[1 + 7] * r_T[1 + 7],
53 3);
54 return res_r.to_double();
55}
56template <typename rational>
57double AMIPS_energy_stable_p3(const std::array<double, 12>& T)
58{
59 auto res = AMIPS_energy(T);
60
61 if (res < 1e8 && res > 2) {
62 return std::pow(res, 3);
63 } else {
64 return AMIPS_energy_rational_p3<rational>(T);
65 }
66
67}
68} // namespace wmtk