-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontour.c
134 lines (96 loc) · 2.7 KB
/
contour.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef CONTOUR_C
#define CONTOUR_C
#include "contour.h"
#include <stdio.h>
#include <stdlib.h>
//variables assigned in crystal_phase
extern int size;
//threshold parameter for acceptable geometry phase
double phase_tol = .99;
//init dest to 0 using helper
int contour2D(double *src, double *dest, int x, int y){
//out of bounds case
if(x < 0 || y < 0 || x >= size || y >= size)
return -1;
//dest [place] > 0? check for negative first in this case
//if this spot is solved, don't solve again, return it.
if(dest[y*size +x] > 0){
return dest[y*size +x];
}
//if active return active, (same as oob)
if(dest[y*size +x]==-1){
return -1;
}
//must be here to close recursion
if(src[y*size+x] < phase_tol){
return dest[y*size +x] = 0;
}
dest[y*size +x] = -1;//set active if all cases pass
int min = size*size;
int tmp;
for(int i = -1; i < 2; i++){
for(int j = -1; j < 2; j++){
tmp = contour2D(src, dest, x+j, y+i);
//if not active or out of bounds and ...
if( tmp != -1 && tmp < min){
min = tmp;
}
if(tmp ==0)
return dest[y*size +x] = min+1;
}
}
// src[y*size +x] = 1;
return (dest[y*size +x] = min+1);
}
//init dest to 0 using helper
//Instead of using phasetol as a threshold we must notice that
//the source for the intended usage of this function is a 3D array of
//1's
int contour3D(double *src, double *dest, int x, int y, int z){
//index in the 3d array of the current point. Calculated and stored
//to save computation time.
int curr = z*size*size + y*size + z;
//out of bounds case
if(x < 0 || y < 0 || z<0 || x >= size || y >= size || z >=size)
return -1;
//dest [place] > 0? check for negative first in this case
//if this spot is solved, don't solve again, return it.
if(dest[curr] > 0){
return dest[curr];
}
//if active return active, (same as oob)
if(dest[curr]==-1){
return -1;
}
//must be here to close recursion
if(src[curr] == 1){
return dest[curr] = 0;
}
dest[curr] = -1;//set active if all cases pass
int min = size*size*size;
int tmp=0;
for(int k = -1; k<2; k++){
for(int i = -1; i < 2; i++){
for(int j = -1; j < 2; j++){
tmp = contour3D(src, dest, x+j, y+i, z+k);
//if not active or out of bounds and ...
if( tmp != -1 && tmp < min){
min = tmp;
}
if(tmp ==0)
return dest[curr] = min+1;
}
}
}
// src[y*size +x] = 1;
return (dest[curr] = min+1);
}
void printArray(double *a){
for(int i = 0; i < size; i++){
for(int j = 100; j< 200; j++){
printf("%f ",a[i*size+j]);
}
printf("\n%d\n",i);
}
}
#endif