67 using System.Collections.Generic;
141 public void MCRecMain(
int NX,
int NY,
int NZ,
float minV, Vector4[] P,
float tolV,
142 bool reversedThreshold, Vector3 delta, Vector3 origin,
143 Color[] colors = null) {
158 foundVert =
new bool[12];
160 sliceSize = (
ncellsZ) * depthSize;
162 dataLength = ncellsX * ncellsZ * 2;
168 center =
new Vector3(0f,0f,0f);
176 IndQueue =
new Queue<int>();
177 IndQueue2 =
new Queue<int>();
181 XPosQueue =
new Queue<int>();
182 YPosQueue =
new Queue<int>();
183 ZPosQueue =
new Queue<int>();
184 XPosQueue2 =
new Queue<int>();
185 YPosQueue2 =
new Queue<int>();
186 ZPosQueue2 =
new Queue<int>();
188 verts =
new Vector4[8];
190 intVerts =
new Vector3[12];
194 vertIndexGrid =
new int[sliceSize * 12];
195 vertIndexGrid2 =
new int[sliceSize * 12];
209 sliceNb = ncellsY - 2;
212 marchedCubes[i] =
false;
213 marchedCubes2[i] =
false;
214 queuedCubes2[i] =
false;
215 queuedCubes[i] =
false;
217 for (
int i=0; i < sliceSize * 12; i++) {
218 vertIndexGrid[i] = -1;
219 vertIndexGrid2[i] = -1;
225 if (reversedThreshold)
251 fullInd = sliceNb * sliceSize + j;
256 if (points[j].w >= minValue && !queuedCubes[j]) {
257 if ((
int)points[j].x != ncellsX-1 && (
int)points[j].y != ncellsY-1 && (
int)points[j].z != ncellsZ-1) {
259 XPosQueue.Enqueue((
int)points[j].x);
260 YPosQueue.Enqueue((
int)points[j].y);
261 ZPosQueue.Enqueue((
int)points[j].z);
263 queuedCubes[j] =
true;
275 fullInd = sliceNb * sliceSize + j;
280 if (points[j].w >= minValue && !queuedCubes2[j]) {
281 if ((
int)points[j].x != ncellsX-1 && (
int)points[j].y != ncellsY-1 && (
int)points[j].z != ncellsZ-1) {
282 IndQueue2.Enqueue(j);
283 XPosQueue2.Enqueue((
int)points[j].x);
284 YPosQueue2.Enqueue((
int)points[j].y);
285 ZPosQueue2.Enqueue((
int)points[j].z);
287 queuedCubes2[j] =
true;
298 while (sliceNb >= 2) {
301 while (IndQueue.Count != 0) {
302 ind = IndQueue.Dequeue ();
303 XPos = XPosQueue.Dequeue();
304 YPos = YPosQueue.Dequeue();
305 ZPos = ZPosQueue.Dequeue();
319 queuedCubes[i] =
false;
320 marchedCubes2[i] =
false;
322 for (
int i=0; i < sliceSize * 12; i++)
323 vertIndexGrid2[i] = -1;
326 while (IndQueue2.Count != 0) {
327 ind = IndQueue2.Dequeue ();
328 XPos = XPosQueue2.Dequeue();
329 YPos = YPosQueue2.Dequeue();
330 ZPos = ZPosQueue2.Dequeue();
344 queuedCubes2[i] =
false;
345 marchedCubes[i] =
false;
347 for (
int i=0; i < sliceSize * 12; i++)
348 vertIndexGrid[i] = -1;
353 while (IndQueue.Count != 0) {
354 ind = IndQueue.Dequeue ();
355 XPos = XPosQueue.Dequeue();
356 YPos = YPosQueue.Dequeue();
357 ZPos = ZPosQueue.Dequeue();
371 if (XPos > 0 && ((cubeIndex & 1) > 0 || (cubeIndex & 2) > 0 || (cubeIndex & 16) > 0 || (cubeIndex & 32) > 0)) {
372 if (!queuedCubes[ind-1]) {
374 IndQueue.Enqueue(ind - 1);
375 XPosQueue.Enqueue((
int)points[ind].x-1);
376 YPosQueue.Enqueue((
int)points[ind].y);
377 ZPosQueue.Enqueue((
int)points[ind].z);
378 queuedCubes[ind-1] =
true;
383 if (XPos > 0 && ((cubeIndex & 1) > 0 || (cubeIndex & 2) > 0 || (cubeIndex & 16) > 0 || (cubeIndex & 32) > 0)) {
384 if (!queuedCubes2[ind-1]) {
386 IndQueue2.Enqueue(ind - 1);
387 XPosQueue2.Enqueue((
int)points[ind].x-1);
388 YPosQueue2.Enqueue((
int)points[ind].y);
389 ZPosQueue2.Enqueue((
int)points[ind].z);
390 queuedCubes2[ind-1] =
true;
395 if (XPos < ncellsX-2 && ((cubeIndex & 4) > 0 || (cubeIndex & 8) > 0 || (cubeIndex & 64) > 0 || (cubeIndex & 128) > 0)) {
396 if (!queuedCubes[ind+1]) {
398 IndQueue.Enqueue(ind + 1);
399 XPosQueue.Enqueue((
int)points[ind].x+1);
400 YPosQueue.Enqueue((
int)points[ind].y);
401 ZPosQueue.Enqueue((
int)points[ind].z);
402 queuedCubes[ind+1] =
true;
407 if (XPos < ncellsX-2 && ((cubeIndex & 4) > 0 || (cubeIndex & 8) > 0 || (cubeIndex & 64) > 0 || (cubeIndex & 128) > 0)) {
408 if (!queuedCubes2[ind+1]) {
410 IndQueue2.Enqueue(ind + 1);
411 XPosQueue2.Enqueue((
int)points[ind].x+1);
412 YPosQueue2.Enqueue((
int)points[ind].y);
413 ZPosQueue2.Enqueue((
int)points[ind].z);
414 queuedCubes2[ind+1] =
true;
420 if (YPos > 0 && ((cubeIndex & 1) > 0 || (cubeIndex & 8) > 0 || (cubeIndex & 16) > 0 || (cubeIndex & 128) > 0)) {
421 if (!queuedCubes2[ind]) {
423 IndQueue2.Enqueue(ind);
424 XPosQueue2.Enqueue((
int)points[ind].x);
425 YPosQueue2.Enqueue((
int)points[ind].y-1);
426 ZPosQueue2.Enqueue((
int)points[ind].z);
427 queuedCubes2[
ind] =
true;
433 if (YPos > 0 && ((cubeIndex & 1) > 0 || (cubeIndex & 8) > 0 || (cubeIndex & 16) > 0 || (cubeIndex & 128) > 0)) {
434 if (!queuedCubes[ind]) {
436 IndQueue.Enqueue(ind);
437 XPosQueue.Enqueue((
int)points[ind].x);
438 YPosQueue.Enqueue((
int)points[ind].y-1);
439 ZPosQueue.Enqueue((
int)points[ind].z);
440 queuedCubes[
ind] =
true;
445 if (ZPos < depthSize-2 && ((cubeIndex & 16) > 0 || (cubeIndex & 32) > 0 || (cubeIndex & 64) > 0 || (cubeIndex & 128) > 0)) {
446 if (!queuedCubes[ind+depthSize]) {
448 IndQueue.Enqueue(ind+depthSize);
449 XPosQueue.Enqueue((
int)points[ind].x);
450 YPosQueue.Enqueue((
int)points[ind].y);
451 ZPosQueue.Enqueue((
int)points[ind].z+1);
457 if (ZPos < depthSize-2 && ((cubeIndex & 16) > 0 || (cubeIndex & 32) > 0 || (cubeIndex & 64) > 0 || (cubeIndex & 128) > 0)) {
458 if (!queuedCubes2[ind+depthSize]) {
460 IndQueue2.Enqueue(ind+depthSize);
461 XPosQueue2.Enqueue((
int)points[ind].x);
462 YPosQueue2.Enqueue((
int)points[ind].y);
463 ZPosQueue2.Enqueue((
int)points[ind].z+1);
469 if (ZPos > 0 && ((cubeIndex & 1) > 0 || (cubeIndex & 2) > 0 || (cubeIndex & 4) > 0 || (cubeIndex & 8) > 0)) {
470 if (!queuedCubes[ind-depthSize]) {
472 IndQueue.Enqueue(ind-depthSize);
473 XPosQueue.Enqueue((
int)points[ind].x);
474 YPosQueue.Enqueue((
int)points[ind].y);
475 ZPosQueue.Enqueue((
int)points[ind].z-1);
481 if (ZPos > 0 && ((cubeIndex & 1) > 0 || (cubeIndex & 2) > 0 || (cubeIndex & 4) > 0 || (cubeIndex & 8) > 0)) {
482 if (!queuedCubes2[ind-depthSize]) {
484 IndQueue2.Enqueue(ind-depthSize);
485 XPosQueue2.Enqueue((
int)points[ind].x);
486 YPosQueue2.Enqueue((
int)points[ind].y);
487 ZPosQueue2.Enqueue((
int)points[ind].z-1);
497 if (nbIndVertices > 64900) {
523 for (
int i=0; i < sliceSize * 12; i++){
524 vertIndexGrid[i] = -1;
525 vertIndexGrid2[i] = -1;
529 for (
int i=0; i < 12; i++)
530 foundVert[i] =
false;
532 verts[0] = points[
ind];
534 verts[2] = points[ind + sliceSize + 1];
535 verts[3] = points[ind + 1];
537 verts[5] = points[ind + sliceSize +
depthSize];
538 verts[6] = points[ind + sliceSize + depthSize + 1];
539 verts[7] = points[ind + depthSize + 1];
542 marchedCubes[
ind] =
true;
547 for (
int n = 0; n < 8; n++)
549 if (verts[n].w >= minValue)
550 cubeIndex |= (1 << n);
553 if (cubeIndex != 0 && cubeIndex != 255) {
557 if ((edgeIndex & 1) > 0) {
559 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+2] != -1) {
560 vertIndexGrid[ind*12] = vertIndexGrid[(ind-1)*12+2];
564 if (ind-depthSize >= 0) {
565 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+4] != -1) {
566 vertIndexGrid[ind*12] = vertIndexGrid[(ind-
depthSize)*12+4];
576 if ((edgeIndex & 2) > 0) {
577 if (sliceNb < ncellsY-2) {
578 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+3] != - 1) {
579 vertIndexGrid[ind*12+1] = vertIndexGrid2[ind*12+3];
583 if (ind-depthSize >= 0) {
584 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+5] != -1) {
585 vertIndexGrid[ind*12+1] = vertIndexGrid[(ind-
depthSize)*12+5];
595 if ((edgeIndex & 4) > 0) {
596 if (ind+1 <= dataLength) {
597 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12] != -1) {
598 vertIndexGrid[ind*12+2] = vertIndexGrid[(ind+1)*12];
602 if (ind-depthSize >= 0) {
603 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+6] != -1) {
604 vertIndexGrid[ind*12+2] = vertIndexGrid[(ind-
depthSize)*12+6];
614 if ((edgeIndex & 8) > 0) {
615 if (ind - depthSize >= 0) {
616 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+7] != -1) {
617 vertIndexGrid[ind*12+3] = vertIndexGrid[(ind-
depthSize)*12+7];
627 if ((edgeIndex & 16) > 0) {
629 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+6] != -1) {
630 vertIndexGrid[ind*12+4] = vertIndexGrid[(ind-1)*12+6];
634 if (ind+depthSize <= dataLength) {
635 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12] != -1) {
636 vertIndexGrid[ind*12+4] = vertIndexGrid[(ind+
depthSize)*12];
646 if ((edgeIndex & 32) > 0) {
647 if (sliceNb < ncellsY-2) {
648 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+7] != -1) {
649 vertIndexGrid[ind*12+5] = vertIndexGrid2[ind*12+7];
653 if (ind + depthSize <= dataLength) {
654 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12+1] != -1) {
655 vertIndexGrid[ind*12+5] = vertIndexGrid[(ind+
depthSize)*12+1];
665 if ((edgeIndex & 64) > 0) {
666 if (ind+1 <= dataLength) {
667 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12+4] != -1) {
668 vertIndexGrid[ind*12+6] = vertIndexGrid[(ind+1)*12+4];
672 if (ind+ depthSize <= dataLength) {
673 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12+2] != -1) {
674 vertIndexGrid[ind*12+6] = vertIndexGrid[(ind+
depthSize)*12+2];
684 if ((edgeIndex & 128) > 0) {
685 if (ind+depthSize <= dataLength) {
686 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12+3] != -1) {
687 vertIndexGrid[ind*12+7] = vertIndexGrid[(ind+
depthSize)*12+3];
697 if ((edgeIndex & 256) > 0) {
699 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+11] != -1) {
700 vertIndexGrid[ind*12+8] = vertIndexGrid[(ind-1)*12+11];
710 if ((edgeIndex & 512) > 0) {
712 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+10] != -1) {
713 vertIndexGrid[ind*12+9] = vertIndexGrid[(ind-1)*12+10];
717 if (sliceNb < ncellsY-2) {
718 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+8] != -1) {
719 vertIndexGrid[ind*12+9] = vertIndexGrid2[ind*12+8];
729 if ((edgeIndex & 1024) > 0) {
730 if (ind+1 <= dataLength) {
731 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12+9] != -1) {
732 vertIndexGrid[ind*12+10] = vertIndexGrid[(ind+1)*12+9];
733 foundVert[10] =
true;
736 if (sliceNb < ncellsY-2) {
737 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+11] != -1) {
738 vertIndexGrid[ind*12+10] = vertIndexGrid2[ind*12+11];
739 foundVert[10] =
true;
742 if (!foundVert[10]) {
748 if ((edgeIndex & 2048) > 0) {
749 if (ind+1 <= dataLength) {
750 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12+8] != -1) {
751 vertIndexGrid[ind*12+11] = vertIndexGrid[(ind+1)*12+8];
752 foundVert[11] =
true;
755 if (!foundVert[11]) {
767 for (
int h = 0; h < 3; h++)
770 if (vertIndexGrid[ind*12+index[h]] != -1) {
781 intVerts[index[h]].x -= 18;
782 intVerts[index[h]].y -= 18;
783 intVerts[index[h]].z -= 18;
784 intVerts[index[h]].x /=Delta.x;
785 intVerts[index[h]].y /=Delta.y;
786 intVerts[index[h]].z /=Delta.z;
788 intVerts[index[h]].x *=Delta.x;
789 intVerts[index[h]].y *=Delta.y;
790 intVerts[index[h]].z *=Delta.z;
792 intVerts[index[h]].x += Origin.x;
793 intVerts[index[h]].y += Origin.y;
794 intVerts[index[h]].z += Origin.z;
820 if (nbIndVertices > 64900) {
845 for (
int i=0; i < sliceSize * 12; i++){
846 vertIndexGrid[i] = -1;
847 vertIndexGrid2[i] = -1;
851 for (
int i=0; i < 12; i++)
852 foundVert[i] =
false;
854 verts[0] = points[
ind];
856 verts[2] = points[ind + sliceSize + 1];
857 verts[3] = points[ind + 1];
859 verts[5] = points[ind + sliceSize +
depthSize];
860 verts[6] = points[ind + sliceSize + depthSize + 1];
861 verts[7] = points[ind + depthSize + 1];
864 marchedCubes2[
ind] =
true;
869 for (
int n = 0; n < 8; n++)
871 if (verts[n].w >= minValue)
872 cubeIndex |= (1 << n);
875 if (cubeIndex != 0 && cubeIndex != 255) {
879 if ((edgeIndex & 1) > 0) {
881 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+2] != -1) {
882 vertIndexGrid2[ind*12] = vertIndexGrid2[(ind-1)*12+2];
886 if (ind-depthSize >= 0) {
887 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+4] != -1) {
888 vertIndexGrid2[ind*12] = vertIndexGrid2[(ind-
depthSize)*12+4];
898 if ((edgeIndex & 2) > 0) {
899 if (sliceNb < ncellsY-2) {
900 if (marchedCubes[ind] && vertIndexGrid[ind*12+3] != - 1) {
901 vertIndexGrid2[ind*12+1] = vertIndexGrid[ind*12+3];
905 if (ind-depthSize >= 0) {
906 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+5] != -1) {
907 vertIndexGrid2[ind*12+1] = vertIndexGrid2[(ind-
depthSize)*12+5];
917 if ((edgeIndex & 4) > 0) {
918 if (ind+1 <= dataLength) {
919 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12] != -1) {
920 vertIndexGrid2[ind*12+2] = vertIndexGrid2[(ind+1)*12];
924 if (ind-depthSize >= 0) {
925 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+6] != -1) {
926 vertIndexGrid2[ind*12+2] = vertIndexGrid2[(ind-
depthSize)*12+6];
936 if ((edgeIndex & 8) > 0) {
937 if (ind - depthSize >= 0) {
938 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+7] != -1) {
939 vertIndexGrid2[ind*12+3] = vertIndexGrid2[(ind-
depthSize)*12+7];
949 if ((edgeIndex & 16) > 0) {
951 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+6] != -1) {
952 vertIndexGrid2[ind*12+4] = vertIndexGrid2[(ind-1)*12+6];
956 if (ind+depthSize <= dataLength) {
957 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12] != -1) {
958 vertIndexGrid2[ind*12+4] = vertIndexGrid2[(ind+
depthSize)*12];
968 if ((edgeIndex & 32) > 0) {
969 if (sliceNb < ncellsY-2) {
970 if (marchedCubes[ind] && vertIndexGrid[ind*12+7] != -1) {
971 vertIndexGrid2[ind*12+5] = vertIndexGrid[ind*12+7];
975 if (ind + depthSize <= dataLength) {
976 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12+1] != -1) {
977 vertIndexGrid2[ind*12+5] = vertIndexGrid2[(ind+
depthSize)*12+1];
987 if ((edgeIndex & 64) > 0) {
988 if (ind+1 <= dataLength) {
989 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12+4] != -1) {
990 vertIndexGrid2[ind*12+6] = vertIndexGrid2[(ind+1)*12+4];
994 if (ind+ depthSize <= dataLength) {
995 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12+2] != -1) {
996 vertIndexGrid2[ind*12+6] = vertIndexGrid2[(ind+
depthSize)*12+2];
1000 if (!foundVert[6]) {
1006 if ((edgeIndex & 128) > 0) {
1007 if (ind+depthSize <= dataLength) {
1008 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12+3] != -1) {
1009 vertIndexGrid2[ind*12+7] = vertIndexGrid2[(ind+
depthSize)*12+3];
1010 foundVert[7] =
true;
1013 if (!foundVert[7]) {
1019 if ((edgeIndex & 256) > 0) {
1021 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+11] != -1) {
1022 vertIndexGrid2[ind*12+8] = vertIndexGrid2[(ind-1)*12+11];
1023 foundVert[8] =
true;
1026 if (!foundVert[8]) {
1032 if ((edgeIndex & 512) > 0) {
1034 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+10] != -1) {
1035 vertIndexGrid2[ind*12+9] = vertIndexGrid2[(ind-1)*12+10];
1036 foundVert[9] =
true;
1039 if (sliceNb < ncellsY-2) {
1040 if (marchedCubes[ind] && vertIndexGrid[ind*12+8] != -1) {
1041 vertIndexGrid2[ind*12+9] = vertIndexGrid[ind*12+8];
1042 foundVert[9] =
true;
1045 if (!foundVert[9]) {
1051 if ((edgeIndex & 1024) > 0) {
1052 if (ind+1 <= dataLength) {
1053 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12+9] != -1) {
1054 vertIndexGrid2[ind*12+10] = vertIndexGrid2[(ind+1)*12+9];
1055 foundVert[10] =
true;
1058 if (sliceNb < ncellsY-2) {
1059 if (marchedCubes[ind] && vertIndexGrid[ind*12+11] != -1) {
1060 vertIndexGrid2[ind*12+10] = vertIndexGrid[ind*12+11];
1061 foundVert[10] =
true;
1064 if (!foundVert[10]) {
1070 if ((edgeIndex & 2048) > 0) {
1071 if (ind+1 <= dataLength) {
1072 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12+8] != -1) {
1073 vertIndexGrid2[ind*12+11] = vertIndexGrid2[(ind+1)*12+8];
1074 foundVert[11] =
true;
1077 if (!foundVert[11]) {
1089 for (
int h = 0; h < 3; h++)
1091 if (vertIndexGrid2[ind*12+index[h]] != -1) {
1101 intVerts[index[h]].x -= 18;
1102 intVerts[index[h]].y -= 18;
1103 intVerts[index[h]].z -= 18;
1104 intVerts[index[h]].x /=Delta.x;
1105 intVerts[index[h]].y /=Delta.y;
1106 intVerts[index[h]].z /=Delta.z;
1108 intVerts[index[h]].x *=Delta.x;
1109 intVerts[index[h]].y *=Delta.y;
1110 intVerts[index[h]].z *=Delta.z;
1112 intVerts[index[h]].x += Origin.x;
1113 intVerts[index[h]].y += Origin.y;
1114 intVerts[index[h]].z += Origin.z;
1137 if (Math.Abs(p1.w - p2.w) > 0.00001) {
1138 p = p1 + (p2 - p1) / (p2.w - p1.w) * (minValue - p1.w);
1148 marchedCubes = null;
1149 marchedCubes2 = null;
1151 queuedCubes2 = null;
1152 vertIndexGrid = null;
1153 vertIndexGrid2 = null;
1193 fullInd = sliceNb * sliceSize + j;
1198 if (points[j].w <= minValue && !queuedCubes[j]) {
1199 if ((
int)points[j].x != ncellsX-1 && (
int)points[j].y != ncellsY-1 && (
int)points[j].z != ncellsZ-1) {
1200 IndQueue.Enqueue(j);
1201 XPosQueue.Enqueue((
int)points[j].x);
1202 YPosQueue.Enqueue((
int)points[j].y);
1203 ZPosQueue.Enqueue((
int)points[j].z);
1205 queuedCubes[j] =
true;
1217 fullInd = sliceNb * sliceSize + j;
1222 if (points[j].w <= minValue && !queuedCubes2[j]) {
1223 if ((
int)points[j].x != ncellsX-1 && (
int)points[j].y != ncellsY-1 && (
int)points[j].z != ncellsZ-1) {
1224 IndQueue2.Enqueue(j);
1225 XPosQueue2.Enqueue((
int)points[j].x);
1226 YPosQueue2.Enqueue((
int)points[j].y);
1227 ZPosQueue2.Enqueue((
int)points[j].z);
1229 queuedCubes2[j] =
true;
1240 while (sliceNb >= 2) {
1243 while (IndQueue.Count != 0) {
1244 ind = IndQueue.Dequeue ();
1245 XPos = XPosQueue.Dequeue();
1246 YPos = YPosQueue.Dequeue();
1247 ZPos = ZPosQueue.Dequeue();
1261 queuedCubes[i] =
false;
1262 marchedCubes2[i] =
false;
1264 for (
int i=0; i < sliceSize * 12; i++)
1265 vertIndexGrid2[i] = -1;
1268 while (IndQueue2.Count != 0) {
1269 ind = IndQueue2.Dequeue ();
1270 XPos = XPosQueue2.Dequeue();
1271 YPos = YPosQueue2.Dequeue();
1272 ZPos = ZPosQueue2.Dequeue();
1286 queuedCubes2[i] =
false;
1287 marchedCubes[i] =
false;
1289 for (
int i=0; i < sliceSize * 12; i++)
1290 vertIndexGrid[i] = -1;
1295 while (IndQueue.Count != 0) {
1296 ind = IndQueue.Dequeue ();
1297 XPos = XPosQueue.Dequeue();
1298 YPos = YPosQueue.Dequeue();
1299 ZPos = ZPosQueue.Dequeue();
1316 if (nbIndVertices > 64900) {
1342 for (
int i=0; i < sliceSize * 12; i++){
1343 vertIndexGrid[i] = -1;
1344 vertIndexGrid2[i] = -1;
1348 for (
int i=0; i < 12; i++)
1349 foundVert[i] =
false;
1351 verts[0] = points[
ind];
1353 verts[2] = points[ind + sliceSize + 1];
1354 verts[3] = points[ind + 1];
1356 verts[5] = points[ind + sliceSize +
depthSize];
1357 verts[6] = points[ind + sliceSize + depthSize + 1];
1358 verts[7] = points[ind + depthSize + 1];
1361 marchedCubes[
ind] =
true;
1366 for (
int n = 0; n < 8; n++)
1368 if (verts[n].w <= minValue)
1369 cubeIndex |= (1 << n);
1372 if (cubeIndex != 0 && cubeIndex != 255) {
1376 if ((edgeIndex & 1) > 0) {
1378 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+2] != -1) {
1379 vertIndexGrid[ind*12] = vertIndexGrid[(ind-1)*12+2];
1380 foundVert[0] =
true;
1383 if (ind-depthSize >= 0) {
1384 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+4] != -1) {
1385 vertIndexGrid[ind*12] = vertIndexGrid[(ind-
depthSize)*12+4];
1386 foundVert[0] =
true;
1389 if (!foundVert[0]) {
1395 if ((edgeIndex & 2) > 0) {
1396 if (sliceNb < ncellsY-2) {
1397 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+3] != - 1) {
1398 vertIndexGrid[ind*12+1] = vertIndexGrid2[ind*12+3];
1399 foundVert[1] =
true;
1402 if (ind-depthSize >= 0) {
1403 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+5] != -1) {
1404 vertIndexGrid[ind*12+1] = vertIndexGrid[(ind-
depthSize)*12+5];
1405 foundVert[1] =
true;
1408 if (!foundVert[1]) {
1414 if ((edgeIndex & 4) > 0) {
1415 if (ind+1 <= dataLength) {
1416 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12] != -1) {
1417 vertIndexGrid[ind*12+2] = vertIndexGrid[(ind+1)*12];
1418 foundVert[2] =
true;
1421 if (ind-depthSize >= 0) {
1422 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+6] != -1) {
1423 vertIndexGrid[ind*12+2] = vertIndexGrid[(ind-
depthSize)*12+6];
1424 foundVert[2] =
true;
1427 if (!foundVert[2]) {
1433 if ((edgeIndex & 8) > 0) {
1434 if (ind - depthSize >= 0) {
1435 if (marchedCubes[ind-depthSize] && vertIndexGrid[(ind-depthSize)*12+7] != -1) {
1436 vertIndexGrid[ind*12+3] = vertIndexGrid[(ind-
depthSize)*12+7];
1437 foundVert[3] =
true;
1440 if (!foundVert[3]) {
1446 if ((edgeIndex & 16) > 0) {
1448 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+6] != -1) {
1449 vertIndexGrid[ind*12+4] = vertIndexGrid[(ind-1)*12+6];
1450 foundVert[4] =
true;
1453 if (ind+depthSize <= dataLength) {
1454 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12] != -1) {
1455 vertIndexGrid[ind*12+4] = vertIndexGrid[(ind+
depthSize)*12];
1456 foundVert[4] =
true;
1459 if (!foundVert[4]) {
1465 if ((edgeIndex & 32) > 0) {
1466 if (sliceNb < ncellsY-2) {
1467 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+7] != -1) {
1468 vertIndexGrid[ind*12+5] = vertIndexGrid2[ind*12+7];
1469 foundVert[5] =
true;
1472 if (ind + depthSize <= dataLength) {
1473 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12+1] != -1) {
1474 vertIndexGrid[ind*12+5] = vertIndexGrid[(ind+
depthSize)*12+1];
1475 foundVert[5] =
true;
1478 if (!foundVert[5]) {
1484 if ((edgeIndex & 64) > 0) {
1485 if (ind+1 <= dataLength) {
1486 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12+4] != -1) {
1487 vertIndexGrid[ind*12+6] = vertIndexGrid[(ind+1)*12+4];
1488 foundVert[6] =
true;
1491 if (ind+ depthSize <= dataLength) {
1492 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12+2] != -1) {
1493 vertIndexGrid[ind*12+6] = vertIndexGrid[(ind+
depthSize)*12+2];
1494 foundVert[6] =
true;
1497 if (!foundVert[6]) {
1503 if ((edgeIndex & 128) > 0) {
1504 if (ind+depthSize <= dataLength) {
1505 if (marchedCubes[ind+depthSize] && vertIndexGrid[(ind+depthSize)*12+3] != -1) {
1506 vertIndexGrid[ind*12+7] = vertIndexGrid[(ind+
depthSize)*12+3];
1507 foundVert[7] =
true;
1510 if (!foundVert[7]) {
1516 if ((edgeIndex & 256) > 0) {
1518 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+11] != -1) {
1519 vertIndexGrid[ind*12+8] = vertIndexGrid[(ind-1)*12+11];
1520 foundVert[8] =
true;
1523 if (!foundVert[8]) {
1529 if ((edgeIndex & 512) > 0) {
1531 if (marchedCubes[ind-1] && vertIndexGrid[(ind-1)*12+10] != -1) {
1532 vertIndexGrid[ind*12+9] = vertIndexGrid[(ind-1)*12+10];
1533 foundVert[9] =
true;
1536 if (sliceNb < ncellsY-2) {
1537 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+8] != -1) {
1538 vertIndexGrid[ind*12+9] = vertIndexGrid2[ind*12+8];
1539 foundVert[9] =
true;
1542 if (!foundVert[9]) {
1548 if ((edgeIndex & 1024) > 0) {
1549 if (ind+1 <= dataLength) {
1550 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12+9] != -1) {
1551 vertIndexGrid[ind*12+10] = vertIndexGrid[(ind+1)*12+9];
1552 foundVert[10] =
true;
1555 if (sliceNb < ncellsY-2) {
1556 if (marchedCubes2[ind] && vertIndexGrid2[ind*12+11] != -1) {
1557 vertIndexGrid[ind*12+10] = vertIndexGrid2[ind*12+11];
1558 foundVert[10] =
true;
1561 if (!foundVert[10]) {
1567 if ((edgeIndex & 2048) > 0) {
1568 if (ind+1 <= dataLength) {
1569 if (marchedCubes[ind+1] && vertIndexGrid[(ind+1)*12+8] != -1) {
1570 vertIndexGrid[ind*12+11] = vertIndexGrid[(ind+1)*12+8];
1571 foundVert[11] =
true;
1574 if (!foundVert[11]) {
1586 for (
int h = 0; h < 3; h++)
1589 if (vertIndexGrid[ind*12+index[h]] != -1) {
1600 intVerts[index[h]].x -= 18;
1601 intVerts[index[h]].y -= 18;
1602 intVerts[index[h]].z -= 18;
1603 intVerts[index[h]].x /=Delta.x;
1604 intVerts[index[h]].y /=Delta.y;
1605 intVerts[index[h]].z /=Delta.z;
1607 intVerts[index[h]].x *=Delta.x;
1608 intVerts[index[h]].y *=Delta.y;
1609 intVerts[index[h]].z *=Delta.z;
1611 intVerts[index[h]].x += Origin.x;
1612 intVerts[index[h]].y += Origin.y;
1613 intVerts[index[h]].z += Origin.z;
1638 if (nbIndVertices > 64900) {
1662 for (
int i=0; i < sliceSize * 12; i++) {
1663 vertIndexGrid2[i] = -1;
1664 vertIndexGrid[i] = -1;
1668 for (
int i=0; i < 12; i++)
1669 foundVert[i] =
false;
1671 verts[0] = points[
ind];
1673 verts[2] = points[ind + sliceSize + 1];
1674 verts[3] = points[ind + 1];
1676 verts[5] = points[ind + sliceSize +
depthSize];
1677 verts[6] = points[ind + sliceSize + depthSize + 1];
1678 verts[7] = points[ind + depthSize + 1];
1681 marchedCubes2[
ind] =
true;
1686 for (
int n = 0; n < 8; n++)
1688 if (verts[n].w <= minValue)
1689 cubeIndex |= (1 << n);
1692 if (cubeIndex != 0 && cubeIndex != 255) {
1696 if ((edgeIndex & 1) > 0) {
1698 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+2] != -1) {
1699 vertIndexGrid2[ind*12] = vertIndexGrid2[(ind-1)*12+2];
1700 foundVert[0] =
true;
1703 if (ind-depthSize >= 0) {
1704 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+4] != -1) {
1705 vertIndexGrid2[ind*12] = vertIndexGrid2[(ind-
depthSize)*12+4];
1706 foundVert[0] =
true;
1709 if (!foundVert[0]) {
1715 if ((edgeIndex & 2) > 0) {
1716 if (sliceNb < ncellsY-2) {
1717 if (marchedCubes[ind] && vertIndexGrid[ind*12+3] != - 1) {
1718 vertIndexGrid2[ind*12+1] = vertIndexGrid[ind*12+3];
1719 foundVert[1] =
true;
1722 if (ind-depthSize >= 0) {
1723 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+5] != -1) {
1724 vertIndexGrid2[ind*12+1] = vertIndexGrid2[(ind-
depthSize)*12+5];
1725 foundVert[1] =
true;
1728 if (!foundVert[1]) {
1734 if ((edgeIndex & 4) > 0) {
1735 if (ind+1 <= dataLength) {
1736 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12] != -1) {
1737 vertIndexGrid2[ind*12+2] = vertIndexGrid2[(ind+1)*12];
1738 foundVert[2] =
true;
1741 if (ind-depthSize >= 0) {
1742 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+6] != -1) {
1743 vertIndexGrid2[ind*12+2] = vertIndexGrid2[(ind-
depthSize)*12+6];
1744 foundVert[2] =
true;
1747 if (!foundVert[2]) {
1753 if ((edgeIndex & 8) > 0) {
1754 if (ind - depthSize >= 0) {
1755 if (marchedCubes2[ind-depthSize] && vertIndexGrid2[(ind-depthSize)*12+7] != -1) {
1756 vertIndexGrid2[ind*12+3] = vertIndexGrid2[(ind-
depthSize)*12+7];
1757 foundVert[3] =
true;
1760 if (!foundVert[3]) {
1766 if ((edgeIndex & 16) > 0) {
1768 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+6] != -1) {
1769 vertIndexGrid2[ind*12+4] = vertIndexGrid2[(ind-1)*12+6];
1770 foundVert[4] =
true;
1773 if (ind+depthSize <= dataLength) {
1774 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12] != -1) {
1775 vertIndexGrid2[ind*12+4] = vertIndexGrid2[(ind+
depthSize)*12];
1776 foundVert[4] =
true;
1779 if (!foundVert[4]) {
1785 if ((edgeIndex & 32) > 0) {
1786 if (sliceNb < ncellsY-2) {
1787 if (marchedCubes[ind] && vertIndexGrid[ind*12+7] != -1) {
1788 vertIndexGrid2[ind*12+5] = vertIndexGrid[ind*12+7];
1789 foundVert[5] =
true;
1792 if (ind + depthSize <= dataLength) {
1793 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12+1] != -1) {
1794 vertIndexGrid2[ind*12+5] = vertIndexGrid2[(ind+
depthSize)*12+1];
1795 foundVert[5] =
true;
1798 if (!foundVert[5]) {
1804 if ((edgeIndex & 64) > 0) {
1805 if (ind+1 <= dataLength) {
1806 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12+4] != -1) {
1807 vertIndexGrid2[ind*12+6] = vertIndexGrid2[(ind+1)*12+4];
1808 foundVert[6] =
true;
1811 if (ind+ depthSize <= dataLength) {
1812 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12+2] != -1) {
1813 vertIndexGrid2[ind*12+6] = vertIndexGrid2[(ind+
depthSize)*12+2];
1814 foundVert[6] =
true;
1817 if (!foundVert[6]) {
1823 if ((edgeIndex & 128) > 0) {
1824 if (ind+depthSize <= dataLength) {
1825 if (marchedCubes2[ind+depthSize] && vertIndexGrid2[(ind+depthSize)*12+3] != -1) {
1826 vertIndexGrid2[ind*12+7] = vertIndexGrid2[(ind+
depthSize)*12+3];
1827 foundVert[7] =
true;
1830 if (!foundVert[7]) {
1836 if ((edgeIndex & 256) > 0) {
1838 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+11] != -1) {
1839 vertIndexGrid2[ind*12+8] = vertIndexGrid2[(ind-1)*12+11];
1840 foundVert[8] =
true;
1843 if (!foundVert[8]) {
1849 if ((edgeIndex & 512) > 0) {
1851 if (marchedCubes2[ind-1] && vertIndexGrid2[(ind-1)*12+10] != -1) {
1852 vertIndexGrid2[ind*12+9] = vertIndexGrid2[(ind-1)*12+10];
1853 foundVert[9] =
true;
1856 if (sliceNb < ncellsY-2) {
1857 if (marchedCubes[ind] && vertIndexGrid[ind*12+8] != -1) {
1858 vertIndexGrid2[ind*12+9] = vertIndexGrid[ind*12+8];
1859 foundVert[9] =
true;
1862 if (!foundVert[9]) {
1868 if ((edgeIndex & 1024) > 0) {
1869 if (ind+1 <= dataLength) {
1870 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12+9] != -1) {
1871 vertIndexGrid2[ind*12+10] = vertIndexGrid2[(ind+1)*12+9];
1872 foundVert[10] =
true;
1875 if (sliceNb < ncellsY-2) {
1876 if (marchedCubes[ind] && vertIndexGrid[ind*12+11] != -1) {
1877 vertIndexGrid2[ind*12+10] = vertIndexGrid[ind*12+11];
1878 foundVert[10] =
true;
1881 if (!foundVert[10]) {
1887 if ((edgeIndex & 2048) > 0) {
1888 if (ind+1 <= dataLength) {
1889 if (marchedCubes2[ind+1] && vertIndexGrid2[(ind+1)*12+8] != -1) {
1890 vertIndexGrid2[ind*12+11] = vertIndexGrid2[(ind+1)*12+8];
1891 foundVert[11] =
true;
1894 if (!foundVert[11]) {
1906 for (
int h = 0; h < 3; h++)
1908 if (vertIndexGrid2[ind*12+index[h]] != -1) {
1918 intVerts[index[h]].x -= 18;
1919 intVerts[index[h]].y -= 18;
1920 intVerts[index[h]].z -= 18;
1921 intVerts[index[h]].x /=Delta.x;
1922 intVerts[index[h]].y /=Delta.y;
1923 intVerts[index[h]].z /=Delta.z;
1925 intVerts[index[h]].x *=Delta.x;
1926 intVerts[index[h]].y *=Delta.y;
1927 intVerts[index[h]].z *=Delta.z;
1930 intVerts[index[h]].x += Origin.x;
1931 intVerts[index[h]].y += Origin.y;
1932 intVerts[index[h]].z += Origin.z;
void MCInitAndFindEachIndex_2(Vector4[] P)
void MarchOneCube_2_Rev()
void GM(Vector3[] Mvertices, int[] Mtriangles, Vector3 center, int surfaceNb, Color[] Colors)
static Vector3[] Mvertices
void MCRecMain(int NX, int NY, int NZ, float minV, Vector4[] P, float tolV, bool reversedThreshold, Vector3 delta, Vector3 origin, Color[] colors=null)
static Vector3[] TMPvertices
void MCInitAndFindEachIndex_2_Rev(Vector4[] P)
void MCInitAndFindEachIndex_Rev(Vector4[] P)
void MCInitAndFindEachIndex(Vector4[] P)
static int[] TMPtriangles
void LinearInterp(Vector4 p1, Vector4 p2)
void SetAllVariablesToNull()
void MCRecReversedThres(Vector4[] P)