2#include "Base/Headers/BaseValues.hpp"
3#include "Base/Headers/Vector.hpp"
4#include "BlockModel/Headers/CubeLookupTable.h"
5#define MC33C_VERSION_MAJOR 5
6#define MC33C_VERSION_MINOR 2
8#if defined(integer_GRD)
13typedef uint04 GRD_data_type;
14#elif size_type_GRD == 2
15typedef unsigned short int GRD_data_type;
16#elif size_type_GRD == 1
17typedef unsigned char GRD_data_type;
19#error "Incorrect size of the data type. size_type_GRD permitted values: 1, 2 or 4."
20typedef float GRD_data_type;
22#elif size_type_GRD == 8
23typedef double GRD_data_type;
26#define size_type_GRD 4
28#if defined (__SSE__) || ((defined (_M_IX86) || defined (_M_X64)) && !defined(_CHPE_ONLY_))
32inline float invSqrt(
float f) {
33 __m128 temp = _mm_set_ss(f);
34 temp = _mm_rsqrt_ss(temp);
35 return _mm_cvtss_f32(temp);
39inline float invSqrt(
float f) {
45 typedef float GRD_data_type;
49 template<
class t_po
int_type>
52 _GRD(
unsigned x,
unsigned y,
unsigned z)
58 values = (GRD_data_type***)malloc((N[2] + 1) *
sizeof(
void*));
59 smoothed_values = (GRD_data_type***)malloc((N[2] + 1) *
sizeof(
void*));
60 nodes = (t_point_type***)malloc((N[2] + 1) *
sizeof(
void*));
61 for (k = 0; k <= N[2]; ++k)
63 values[k] = (GRD_data_type**)malloc((N[1] + 1) *
sizeof(
void*));
64 smoothed_values[k] = (GRD_data_type**)malloc((N[1] + 1) *
sizeof(
void*));
65 nodes[k] = (t_point_type**)malloc((N[1] + 1) *
sizeof(
void*));
66 for (j = 0; j <= N[1]; ++j)
68 values[k][j] = (GRD_data_type*)malloc((N[0] + 1) *
sizeof(GRD_data_type));
69 nodes[k][j] = (t_point_type*)malloc((N[0] + 1) *
sizeof(t_point_type));
70 smoothed_values[k][j] = (GRD_data_type*)malloc((N[0] + 1) *
sizeof(GRD_data_type));
76 for (
uint04 k = 0; k <= N[2]; ++k)
78 for (
uint04 j = 0; j <= N[1]; ++j)
82 free(smoothed_values[k][j]);
86 free(smoothed_values[k]);
91 for (
uint04 z = 0; z <= N[2]; ++z)
93 for (
uint04 y = 0; y <= N[1]; ++y)
95 for (
uint04 x = 0; x <= N[0]; ++x)
97 smoothed_values[z][y][x] = values[z][y][x] * (1.0f / 14.0f);
101 for (
uint04 z = 1; z < N[2]; ++z)
103 for (
uint04 y = 1; y < N[1]; ++y)
105 for (
uint04 x = 1; x < N[0]; ++x)
107 values[z][y][x] *= (1.0f / 14.0f);
108 values[z][y][x] += smoothed_values[z + 0][y + 0][x + 1];
109 values[z][y][x] += smoothed_values[z + 0][y + 1][x + 0];
110 values[z][y][x] += smoothed_values[z + 0][y + 1][x + 1];
111 values[z][y][x] += smoothed_values[z + 1][y + 0][x + 0];
112 values[z][y][x] += smoothed_values[z + 1][y + 0][x + 1];
113 values[z][y][x] += smoothed_values[z + 1][y + 1][x + 0];
114 values[z][y][x] += smoothed_values[z + 1][y + 1][x + 1];
115 values[z][y][x] += smoothed_values[z - 0][y - 0][x - 1];
116 values[z][y][x] += smoothed_values[z - 0][y - 1][x - 0];
117 values[z][y][x] += smoothed_values[z - 0][y - 1][x - 1];
118 values[z][y][x] += smoothed_values[z - 1][y - 0][x - 0];
119 values[z][y][x] += smoothed_values[z - 1][y - 0][x - 1];
120 values[z][y][x] += smoothed_values[z - 1][y - 1][x - 0];
121 values[z][y][x] += smoothed_values[z - 1][y - 1][x - 1];
126 GRD_data_type*** values;
127 GRD_data_type*** smoothed_values;
128 t_point_type*** nodes;
131 template<
class t_po
int_type>
140 Lz = (
uint04**)malloc((ny + 1) *
sizeof(
int*));
141 Dy = (
uint04**)malloc(ny *
sizeof(
int*));
142 Uy = (
uint04**)malloc(ny *
sizeof(
int*));
143 Dx = (
uint04**)malloc((ny + 1) *
sizeof(
int*));
144 Ux = (
uint04**)malloc((ny + 1) *
sizeof(
int*));
145 values = (
const GRD_data_type***)G.values;
146 nodes = (
const t_point_type***)G.nodes;
147 x = nx *
sizeof(int);
148 for (y = 0; y != ny; ++y)
150 Dx[y] = (
uint04*)malloc(x);
151 Ux[y] = (
uint04*)malloc(x);
152 Lz[y] = (
uint04*)malloc(x +
sizeof(
int));
153 Dy[y] = (
uint04*)malloc(x +
sizeof(
int));
154 Uy[y] = (
uint04*)malloc(x +
sizeof(
int));
158 Dx[y] = (
uint04*)malloc(x);
159 Ux[y] = (
uint04*)malloc(x);
160 Lz[y] = (
uint04*)malloc(x +
sizeof(
int));
163 uint04 store(
float* r,
const t_point_type& color)
177 N = (float(*)[3])realloc(N, nv * 6 *
sizeof(
float));
178 V = (float(*)[3])realloc(V, nv * 6 *
sizeof(
float));
179 C = (t_point_type*)realloc(C, nv * 2 *
sizeof(t_point_type));
181 for (
uint01 i = 0; i < 3; ++i)
188 for (
uint04 y = 0; y != ny; ++y)
208 t_point_type pt = nodes[z][y][x];
213 r[3] = values[z][y][0] - values[z][y][1];
215 r[3] = values[z][y][x - 1] - values[z][y][x];
217 r[3] = 0.5f * (values[z][y][x - 1] - values[z][y][x + 1]);
219 r[4] = values[z][0][x] - values[z][1][x];
221 r[4] = values[z][y - 1][x] - values[z][y][x];
223 r[4] = 0.5f * (values[z][y - 1][x] - values[z][y + 1][x]);
225 r[5] = values[0][y][x] - values[1][y][x];
227 r[5] = values[z - 1][y][x] - values[z][y][x];
229 r[5] = 0.5f * (values[z - 1][y][x] - values[z + 1][y][x]);
234 float(*V)[3] =
nullptr;
235 float(*N)[3] =
nullptr;
236 t_point_type(*C) =
nullptr;
243 const GRD_data_type*** values;
244 const t_point_type*** nodes;
249 uint04** Dx, ** Dy, ** Ux, ** Uy, ** Lz;
267 static inline int MC33_faceTests(
int* face,
int ind,
const float* v)
271 face[0] = ((ind & 0xCC) == 0x84 ? (v[0] * v[5] < v[1] * v[4] ? -1 : 1) : 0);
272 face[3] = ((ind & 0x99) == 0x81 ? (v[0] * v[7] < v[3] * v[4] ? -1 : 1) : 0);
273 face[4] = ((ind & 0xF0) == 0xA0 ? (v[0] * v[2] < v[1] * v[3] ? -1 : 1) : 0);
277 face[0] = ((ind & 0xCC) == 0x48 ? (v[0] * v[5] < v[1] * v[4] ? 1 : -1) : 0);
278 face[3] = ((ind & 0x99) == 0x18 ? (v[0] * v[7] < v[3] * v[4] ? 1 : -1) : 0);
279 face[4] = ((ind & 0xF0) == 0x50 ? (v[0] * v[2] < v[1] * v[3] ? 1 : -1) : 0);
283 face[1] = ((ind & 0x66) == 0x42 ? (v[1] * v[6] < v[2] * v[5] ? -1 : 1) : 0);
284 face[2] = ((ind & 0x33) == 0x12 ? (v[3] * v[6] < v[2] * v[7] ? -1 : 1) : 0);
285 face[5] = ((ind & 0x0F) == 0x0A ? (v[4] * v[6] < v[5] * v[7] ? -1 : 1) : 0);
289 face[1] = ((ind & 0x66) == 0x24 ? (v[1] * v[6] < v[2] * v[5] ? 1 : -1) : 0);
290 face[2] = ((ind & 0x33) == 0x21 ? (v[3] * v[6] < v[2] * v[7] ? 1 : -1) : 0);
291 face[5] = ((ind & 0x0F) == 0x05 ? (v[4] * v[6] < v[5] * v[7] ? 1 : -1) : 0);
293 return face[0] + face[1] + face[2] + face[3] + face[4] + face[5];
297 static inline int MC33_faceTest1(
int face,
const float* v)
300 case 0:
return (v[0] * v[5] < v[1] * v[4] ? 0x48 : 0x84);
301 case 1:
return (v[1] * v[6] < v[2] * v[5] ? 0x24 : 0x42);
302 case 2:
return (v[3] * v[6] < v[2] * v[7] ? 0x21 : 0x12);
303 case 3:
return (v[0] * v[7] < v[3] * v[4] ? 0x18 : 0x81);
304 case 4:
return (v[0] * v[2] < v[1] * v[3] ? 0x50 : 0xA0);
305 default:
return (v[4] * v[6] < v[5] * v[7] ? 0x05 : 0x0A);
312#pragma GCC diagnostic push
313#pragma GCC diagnostic ignored "-Wstrict-aliasing"
315 inline uint04 signbf(
float x) {
316 return ((*(
uint04*)(
void*)(&x)) & 0x80000000);
319#pragma GCC diagnostic pop
330 static inline int MC33_interiorTest(
int i,
int flag13,
const float* v)
336 float At = v[4] - v[0], Bt = v[5] - v[1], Ct = v[6] - v[2], Dt = v[7] - v[3];
337 float t = At * Ct - Bt * Dt;
343 if (!(i & 0x01) || t == 0)
346 t = 0.5f * (v[3] * Bt - v[2] * At + v[1] * Dt - v[0] * Ct) / t;
347 if (t > 0 && t < 1) {
355 if (Ct < Dt && signbf(Dt) == 0)
356 return cast<int>(signbf(Bt) == signbf(v[i])) + flag13;
359 if (Ct > Dt && signbf(Ct) == 0)
360 return cast<int>(signbf(At) == signbf(v[i])) + flag13;
381 template<
class t_po
int_type>
390 const unsigned short int* pcase = MC33_all_tables;
395 m = (c & 0x800) == 0;
399 m = (c & 0x800) != 0;
407 pcase += ((m ? i : i ^ 0xFF) & MC33_faceTest1(k >> 2, v) ? 183 + (k << 1) : 159 + k);
410 pcase += (MC33_interiorTest(k, 0, v) ? 239 + 6 * k : 231 + (k << 1));
413 if ((m ? i : i ^ 0xFF) & MC33_faceTest1(k % 6, v))
414 pcase += 575 + 5 * k;
416 pcase += (MC33_interiorTest(k / 6, 0, v) ? 407 + 7 * k : 335 + 3 * k);
419 switch (MC33_faceTests(u.f, (m ? i : i ^ 0xFF), v))
422 pcase += 695 + 3 * k;
425 pcase += (u.f[4] + u.f[5] < 0 ? (u.f[0] + u.f[2] < 0 ? 759 : 799) : 719) + 5 * k;
428 pcase += (u.f[4] + u.f[5] < 0 ? 983 : (u.f[0] + u.f[2] < 0 ? 839 : 911)) + 9 * k;
431 pcase += (MC33_interiorTest(k >> 1, 0, v) ? 1095 + 9 * k : 1055 + 5 * k);
435 switch (MC33_faceTests(u.f, (m ? i : i ^ 0xFF), v)) {
437 if (k == 2 ? MC33_interiorTest(0, 0, v) : MC33_interiorTest(0, 0, v) != 0 || MC33_interiorTest(k ? 1 : 3, 0, v) != 0)
438 pcase += 1213 + (k << 3);
440 pcase += 1189 + (k << 2);
443 pcase += (u.f[2 + k] < 0 ? 1261 : 1285) + (k << 3);
446 if (k == 2 ? MC33_interiorTest(1, 0, v) : MC33_interiorTest(2, 0, v) != 0 || MC33_interiorTest(k ? 3 : 1, 0, v) != 0)
447 pcase += 1237 + (k << 3);
449 pcase += 1201 + (k << 2);
453 switch (MC33_faceTests(u.f, (m ? i : i ^ 0xFF), v)) {
455 pcase += (MC33_interiorTest((0xDA010C >> (k << 1)) & 3, 0, v) ? 1453 + (k << 3) : 1357 + (k << 2));
458 pcase += (u.f[k >> 1] < 0 ? 1645 : 1741) + (k << 3);
461 pcase += (MC33_interiorTest((0xA7B7E5 >> (k << 1)) & 3, 0, v) ? 1549 + (k << 3) : 1405 + (k << 2));
465 switch (
abs(MC33_faceTests(u.f, 165, v))) {
467 k = ((u.f[1] < 0) << 1) | (u.f[5] < 0);
468 if (u.f[0] * u.f[1] == u.f[5])
469 pcase += 2157 + 12 * k;
471 c = MC33_interiorTest(k, 1, v);
472 pcase += 2285 + (c ? 10 * k - 40 * c : 6 * k);
476 pcase += 1917 + 10 * ((u.f[0] < 0 ? u.f[2] > 0: 12 + (u.f[2] < 0)) + (u.f[1] < 0 ? u.f[3] < 0 : 6 + (u.f[3] > 0)));
481 k = 21 + 11 * u.f[0] + 4 * u.f[1] + 3 * u.f[2] + 2 * u.f[3] + u.f[4];
483 k -= (k & 32 ? 20 : 10);
484 pcase += 1845 + 3 * k;
487 pcase += 1839 + 2 * u.f[0];
506 else if (y && signbf(v[3]))
508 else if (y && signbf(v[4]))
510 else if (y ? signbf(M->iso - M->values[0][y - 1][0]) : 0)
511 p[0] = M->Dy[y - 1][0];
513 p[0] = M->surfint(0, y, 0, u.r);
515 else if (v[1] == 0) {
521 p[0] = M->surfint(0, y + 1, 0, u.r);
524 t = v[0] / (v[0] - v[1]);
527 u.r[3] = (v[4] - v[0]) * (1 - t) + (v[5] - v[1]) * t;
528 u.r[4] = v[1] - v[0];
529 u.r[5] = (v[3] - v[0]) * (1 - t) + (v[2] - v[1]) * t;
530 p[0] = M->store(u.r, combine(M->nodes[0][y][0], 1.0f - t, M->nodes[0][y + 1][0], t));
537 p[1] = M->Lz[y + 1][x];
544 else if (z && signbf(v[0]))
546 else if (z && signbf(v[5]))
547 p[1] = M->Dx[y + 1][0];
548 else if (z && y + 1 < M->ny ? signbf(M->iso - M->values[z][y + 2][0]) : 0)
549 p[1] = M->Dy[y + 1][0];
550 else if (z ? signbf(M->iso - M->values[z - 1][y + 1][0]) : 0)
551 p[1] = M->Lz[y + 1][0];
553 p[1] = M->surfint(0, y + 1, z, u.r);
555 else if (v[2] == 0) {
561 p[1] = M->surfint(0, y + 1, z + 1, u.r);
564 t = v[1] / (v[1] - v[2]);
568 u.r[3] = (v[5] - v[1]) * (1 - t) + (v[6] - v[2]) * t;
569 u.r[4] = (y + 1 < M->ny ? 0.5f * ((M->values[z][y][0] - M->values[z][y + 2][0]) * (1 - t)
570 + (M->values[z + 1][y][0] - M->values[z + 1][y + 2][0]) * t) :
571 (v[1] - v[0]) * (1 - t) + (v[2] - v[3]) * t);
572 u.r[5] = v[2] - v[1];
573 p[1] = M->store(u.r, combine(M->nodes[z][y + 1][0], 1.0f - t, M->nodes[z + 1][y + 1][0], 1.0f));
575 M->Lz[y + 1][0] = p[1];
587 else if (y && signbf(v[0]))
589 else if (y && signbf(v[7]))
591 else if (y ? signbf(M->iso - M->values[z + 1][y - 1][0]) : 0)
592 p[2] = M->Uy[y - 1][0];
594 p[2] = M->surfint(0, y, z + 1, u.r);
596 else if (v[2] == 0) {
602 p[2] = M->surfint(0, y + 1, z + 1, u.r);
605 t = v[3] / (v[3] - v[2]);
609 u.r[3] = (v[7] - v[3]) * (1 - t) + (v[6] - v[2]) * t;
610 u.r[4] = v[2] - v[3];
611 u.r[5] = (z + 1 < M->nz ? 0.5f * ((M->values[z][y][0] - M->values[z + 2][y][0]) * (1 - t)
612 + (M->values[z][y + 1][0] - M->values[z + 2][y + 1][0]) * t) :
613 (v[3] - v[0]) * (1 - t) + (v[2] - v[1]) * t);
614 p[2] = M->store(u.r, combine(M->nodes[z + 1][y][0], 1.0f - t, M->nodes[z + 1][y + 1][0], t));
628 else if (z && signbf(v[1]))
630 else if (z && signbf(v[4]))
632 else if (z ? signbf(M->iso - M->values[z - 1][0][0]) : 0)
635 p[3] = M->surfint(0, 0, z, u.r);
637 else if (v[3] == 0) {
643 p[3] = M->surfint(0, 0, z + 1, u.r);
646 t = v[0] / (v[0] - v[3]);
649 u.r[3] = (v[4] - v[0]) * (1 - t) + (v[7] - v[3]) * t;
650 u.r[4] = (v[1] - v[0]) * (1 - t) + (v[2] - v[3]) * t;
651 u.r[5] = v[3] - v[0];
652 p[3] = M->store(u.r, combine(M->nodes[z][0][0], 1.0f - t, M->nodes[z + 1][0][0], t));
659 p[4] = M->Dy[y][x + 1];
667 else if (y && signbf(v[0]))
669 else if (y && signbf(v[7]))
670 p[4] = M->Lz[y][x + 1];
671 else if (y ? signbf(M->iso - M->values[0][y - 1][x + 1]) : 0)
672 p[4] = M->Dy[y - 1][x + 1];
673 else if (y && x + 1 < M->nx ? signbf(M->iso - M->values[0][y][x + 2]) : 0)
674 p[4] = M->Dx[y][x + 1];
676 p[4] = M->surfint(x + 1, y, 0, u.r);
678 else if (v[5] == 0) {
684 p[4] = M->surfint(x + 1, y + 1, 0, u.r);
687 t = v[4] / (v[4] - v[5]);
691 u.r[3] = (x + 1 < M->nx ? 0.5f * ((M->values[0][y][x] - M->values[0][y][x + 2]) * (1 - t)
692 + (M->values[0][y + 1][x] - M->values[0][y + 1][x + 2]) * t) :
693 (v[4] - v[0]) * (1 - t) + (v[5] - v[1]) * t);
694 u.r[4] = v[5] - v[4];
695 u.r[5] = (v[7] - v[4]) * (1 - t) + (v[6] - v[5]) * t;
696 p[4] = M->store(u.r, combine(M->nodes[0][y][x + 1], 1.0f - t, M->nodes[0][y + 1][x + 1], t));
698 M->Dy[y][x + 1] = p[4];
705 p[5] = p[4] = M->Dy[y][x + 1];
706 else if (signbf(v[1]))
707 p[5] = p[9] = M->Dx[y + 1][x];
708 else if (x + 1 < M->nx ? signbf(M->iso - M->values[z][y + 1][x + 2]) : 0)
709 p[5] = M->Dx[y + 1][x + 1];
710 else if (y + 1 < M->ny ? signbf(M->iso - M->values[z][y + 2][x + 1]) : 0)
711 p[5] = M->Dy[y + 1][x + 1];
712 else if (signbf(M->iso - M->values[z - 1][y + 1][x + 1]))
713 p[5] = M->Lz[y + 1][x + 1];
715 p[5] = M->surfint(x + 1, y + 1, z, u.r);
718 p[5] = M->surfint(x + 1, y + 1, 0, u.r);
721 p[5] = M->surfint(x + 1, y + 1, z + 1, u.r);
723 t = v[5] / (v[5] - v[6]);
727 u.r[3] = (x + 1 < M->nx ? 0.5f * ((M->values[z][y + 1][x] - M->values[z][y + 1][x + 2]) * (1 - t)
728 + (M->values[z + 1][y + 1][x] - M->values[z + 1][y + 1][x + 2]) * t) :
729 (v[5] - v[1]) * (1 - t) + (v[6] - v[2]) * t);
730 u.r[4] = (y + 1 < M->ny ? 0.5f * ((M->values[z][y][x + 1] - M->values[z][y + 2][x + 1]) * (1 - t)
731 + (M->values[z + 1][y][x + 1] - M->values[z + 1][y + 2][x + 1]) * t) :
732 (v[5] - v[4]) * (1 - t) + (v[6] - v[7]) * t);
733 u.r[5] = v[6] - v[5];
734 p[5] = M->store(u.r, combine(M->nodes[z][y + 1][x + 1], 1.0f - t, M->nodes[z + 1][y + 1][x + 1], t));
736 M->Lz[y + 1][x + 1] = p[5];
742 p[6] = p[11] = M->Ux[y][x];
743 else if (signbf(v[4]))
744 p[6] = p[7] = M->Lz[y][x + 1];
745 else if (signbf(M->iso - M->values[z + 1][y - 1][x + 1]))
746 p[6] = M->Uy[y - 1][x + 1];
747 else if (x + 1 < M->nx ? signbf(M->iso - M->values[z + 1][y][x + 2]) : 0)
748 p[6] = M->Ux[y][x + 1];
750 p[6] = M->surfint(x + 1, y, z + 1, u.r);
758 p[6] = M->surfint(x + 1, 0, z + 1, u.r);
760 else if (v[6] == 0) {
762 p[6] = (
IsInvalid(p[10]) ? M->surfint(x + 1, y + 1, z + 1, u.r) : p[10]);
767 t = v[7] / (v[7] - v[6]);
771 u.r[3] = (x + 1 < M->nx ? 0.5f * ((M->values[z + 1][y][x] - M->values[z + 1][y][x + 2]) * (1 - t)
772 + (M->values[z + 1][y + 1][x] - M->values[z + 1][y + 1][x + 2]) * t) :
773 (v[7] - v[3]) * (1 - t) + (v[6] - v[2]) * t);
774 u.r[4] = v[6] - v[7];
775 u.r[5] = (z + 1 < M->nz ? 0.5f * ((M->values[z][y][x + 1] - M->values[z + 2][y][x + 1]) * (1 - t)
776 + (M->values[z][y + 1][x + 1] - M->values[z + 2][y + 1][x + 1]) * t) :
777 (v[7] - v[4]) * (1 - t) + (v[6] - v[5]) * t);
778 p[6] = M->store(u.r, combine(M->nodes[z + 1][y][x + 1], 1.0f - t, M->nodes[z + 1][y + 1][x + 1], t));
780 M->Uy[y][x + 1] = p[6];
784 p[7] = M->Lz[y][x + 1];
791 else if (z && signbf(v[0]))
793 else if (z && signbf(v[5]))
794 p[7] = M->Dy[0][x + 1];
795 else if (z && x + 1 < M->nx ? signbf(M->iso - M->values[z][0][x + 2]) : 0)
796 p[7] = M->Dx[0][x + 1];
797 else if (z ? signbf(M->iso - M->values[z - 1][0][x + 1]) : 0)
798 p[7] = M->Lz[0][x + 1];
800 p[7] = M->surfint(x + 1, 0, z, u.r);
802 else if (v[7] == 0) {
808 p[7] = M->surfint(x + 1, 0, z + 1, u.r);
811 t = v[4] / (v[4] - v[7]);
815 u.r[3] = (x + 1 < M->nx ? 0.5f * ((M->values[z][0][x] - M->values[z][0][x + 2]) * (1 - t)
816 + (M->values[z + 1][0][x] - M->values[z + 1][0][x + 2]) * t) :
817 (v[4] - v[0]) * (1 - t) + (v[7] - v[3]) * t);
818 u.r[4] = (v[5] - v[4]) * (1 - t) + (v[6] - v[7]) * t;
819 u.r[5] = v[7] - v[4];
820 p[7] = M->store(u.r, combine(M->nodes[z][0][x + 1], 1.0f - t, M->nodes[z + 1][0][x + 1], t));
822 M->Lz[0][x + 1] = p[7];
834 else if (x && signbf(v[3]))
836 else if (x && signbf(v[1]))
838 else if (x ? signbf(M->iso - M->values[0][0][x - 1]) : 0)
839 p[8] = M->Dx[0][x - 1];
841 p[8] = M->surfint(x, 0, 0, u.r);
843 else if (v[4] == 0) {
849 p[8] = M->surfint(x + 1, 0, 0, u.r);
852 t = v[0] / (v[0] - v[4]);
855 u.r[3] = v[4] - v[0];
856 u.r[4] = (v[1] - v[0]) * (1 - t) + (v[5] - v[4]) * t;
857 u.r[5] = (v[3] - v[0]) * (1 - t) + (v[7] - v[4]) * t;
858 p[8] = M->store(u.r, combine(M->nodes[0][0][x], 1.0f - t, M->nodes[0][0][x + 1], t));
865 p[9] = M->Dx[y + 1][x];
872 else if (x && signbf(v[0]))
874 else if (x && signbf(v[2]))
875 p[9] = M->Lz[y + 1][x];
876 else if (x ? signbf(M->iso - M->values[0][y + 1][x - 1]) : 0)
877 p[9] = M->Dx[y + 1][x - 1];
879 p[9] = M->surfint(x, y + 1, 0, u.r);
881 else if (v[5] == 0) {
887 p[9] = M->surfint(x + 1, y + 1, 0, u.r);
890 t = v[1] / (v[1] - v[5]);
894 u.r[3] = v[5] - v[1];
895 u.r[4] = (y + 1 < M->ny ? 0.5f * ((M->values[0][y][x] - M->values[0][y + 2][x]) * (1 - t)
896 + (M->values[0][y][x + 1] - M->values[0][y + 2][x + 1]) * t) :
897 (v[1] - v[0]) * (1 - t) + (v[5] - v[4]) * t);
898 u.r[5] = (v[2] - v[1]) * (1 - t) + (v[6] - v[5]) * t;
899 p[9] = M->store(u.r, combine(M->nodes[0][y + 1][x], 1.0f - t, M->nodes[0][y + 1][x + 1], t));
901 M->Dx[y + 1][x] = p[9];
908 p[10] = p[1] = M->Lz[y + 1][x];
909 else if (signbf(v[3]))
910 p[10] = p[2] = M->Uy[y][x];
911 else if (signbf(M->iso - M->values[z + 1][y + 1][x - 1]))
912 p[10] = M->Ux[y + 1][x - 1];
914 p[10] = M->surfint(x, y + 1, z + 1, u.r);
921 p[10] = M->surfint(0, y + 1, z + 1, u.r);
923 else if (v[6] == 0) {
925 p[10] = (
IsInvalid(p[6]) ? M->surfint(x + 1, y + 1, z + 1, u.r) : p[6]);
930 t = v[2] / (v[2] - v[6]);
934 u.r[3] = v[6] - v[2];
935 u.r[4] = (y + 1 < M->ny ? 0.5f * ((M->values[z + 1][y][x] - M->values[z + 1][y + 2][x]) * (1 - t)
936 + (M->values[z + 1][y][x + 1] - M->values[z + 1][y + 2][x + 1]) * t) :
937 (v[2] - v[3]) * (1 - t) + (v[6] - v[7]) * t);
938 u.r[5] = (z + 1 < M->nz ? 0.5f * ((M->values[z][y + 1][x] - M->values[z + 2][y + 1][x]) * (1 - t)
939 + (M->values[z][y + 1][x + 1] - M->values[z + 2][y + 1][x + 1]) * t) :
940 (v[2] - v[1]) * (1 - t) + (v[6] - v[5]) * t);
941 p[10] = M->store(u.r, combine(M->nodes[z + 1][y + 1][x], 1.0f - t, M->nodes[z + 1][y + 1][x + 1], t));
943 M->Ux[y + 1][x] = p[10];
954 else if (x && signbf(v[0]))
956 else if (x && signbf(v[2]))
958 else if (x ? signbf(M->iso - M->values[z + 1][0][x - 1]) : 0)
959 p[11] = M->Ux[0][x - 1];
961 p[11] = M->surfint(x, 0, z + 1, u.r);
963 else if (v[7] == 0) {
969 p[11] = M->surfint(x + 1, 0, z + 1, u.r);
972 t = v[3] / (v[3] - v[7]);
976 u.r[3] = v[7] - v[3];
977 u.r[4] = (v[2] - v[3]) * (1 - t) + (v[6] - v[7]) * t;
978 u.r[5] = (z + 1 < M->nz ? 0.5f * ((M->values[z][0][x] - M->values[z + 2][0][x]) * (1 - t)
979 + (M->values[z][0][x + 1] - M->values[z + 2][0][x + 1]) * t) :
980 (v[3] - v[0]) * (1 - t) + (v[7] - v[4]) * t);
981 p[11] = M->store(u.r, combine(M->nodes[z + 1][0][x], 1.0f - t, M->nodes[z + 1][0][x + 1], t));
990 u.r[3] = v[4] + v[5] + v[6] + v[7] - v[0] - v[1] - v[2] - v[3];
991 u.r[4] = v[1] + v[2] + v[5] + v[6] - v[0] - v[3] - v[4] - v[7];
992 u.r[5] = v[2] + v[3] + v[6] + v[7] - v[0] - v[1] - v[4] - v[5];
993 p[12] = M->store(u.r, combine(M->nodes[z][y][x], 0.5f, M->nodes[z + 1][y + 1][x + 1], 0.5f));
998 if (ti[0] != ti[1] && ti[0] != ti[2] && ti[1] != ti[2])
1000 if (M->nT == M->capt)
1004 M->T = (
uint04(*)[3])realloc(pt, M->capt * 3 *
sizeof(
int));
1006 uint04* vp = M->T[M->nT++];
1007#ifndef MC_Normal_neg
1008 * vp = ti[!m]; *(++vp) = ti[m]; *(++vp) = ti[2];
1010 * vp = ti[m]; *(++vp) = ti[!m]; *(++vp) = ti[2];
1015 class CubeTriangulation
1023 template<
class t_po
int_type>
1024 static void calculate_isosurface(MC33<t_point_type>& M,
float iso)
1026 uint04 x, y, z, Nx = M.nx;
1028 float* v1 = Vt, * v2 = Vt + 4;
1029 const GRD_data_type*** F = M.values, ** F0, ** F1, * V00, * V01, * V11, * V10;
1034 M.T = (
uint04(*)[3])malloc(3 * 4096 *
sizeof(
int));
1039 M.N = (float(*)[3])malloc(3 * 4096 *
sizeof(
float));
1040 M.V = (float(*)[3])malloc(3 * 4096 *
sizeof(
float));
1041 M.C = (t_point_type(*))malloc(4096 *
sizeof(t_point_type));
1044 for (z = 0; z != M.nz; ++z)
1048 for (y = 0; y != M.ny; ++y)
1060 uint04 i = signbf(v2[3]) != 0;
1061 if (signbf(v2[2])) i |= 2;
1062 if (signbf(v2[1])) i |= 4;
1063 if (signbf(v2[0])) i |= 8;
1064 for (x = 0; x != Nx; ++x)
1066 {
float* P = v1; v1 = v2; v2 = P; }
1067 v2[0] = iso - *(++V00);
1068 v2[1] = iso - *(++V01);
1069 v2[2] = iso - *(++V11);
1070 v2[3] = iso - *(++V10);
1071 i = ((i & 0x0F) << 4) | (signbf(v2[3]) != 0);
1072 if (signbf(v2[2])) i |= 2;
1073 if (signbf(v2[1])) i |= 4;
1074 if (signbf(v2[0])) i |= 8;
1086 MC33_findCase(&M, x, y, z, i, v1);
1090 {
uint04** P = M.Dx; M.Dx = M.Ux; M.Ux = P; }
1091 {
uint04** P = M.Dy; M.Dy = M.Uy; M.Uy = P; }
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
static constexpr bool IsValid(const Angle< t_type > &value)
Checks whether the given Angle holds a valid value.
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Changes an input with a negative sign, to a positive sign.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
t_type sqrt(const t_type &value)
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
3D grid data structure holding scalar field values and node data for marching cubes isosurface extrac...