-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpropagation.js
185 lines (179 loc) · 5.51 KB
/
propagation.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//function clearpropagation(sections) {
// for (let section of sections) {
// delete section.snr;
// if (section.anchored) {
// delete section.ouv;
// delete section.anchored;
// }
// }
//}
function propagate(sections, atlas) {
let anchoring = false;
for (let section of sections) {
// if (!section.hasOwnProperty("snr"))
// section.snr = parseInt(section.filename.match(/(?<=_s)\d+/));
// if (!section.hasOwnProperty(("anchored")))
// section.anchored = section.hasOwnProperty("ouv");
if (section.anchored === true) {
if (anchoring === false)
anchoring = section;
else
anchoring = true;
}
}
if (anchoring === false) {
let keyslice = sections[Math.min(sections.length - 1, (sections.length >> 1) + 1)];
let ouv = [0, atlas.ydim / 2, atlas.zdim - 1,
atlas.xdim, 0, 0,
0, 0, -atlas.zdim
];
decomp(ouv, keyslice.width, keyslice.height);
for (let section of sections)
recomp(section.ouv = ouv.slice(), section.width, section.height);
} else if (anchoring !== true) {
let ouv = anchoring.ouv;
decomp(ouv, anchoring.width, anchoring.height);
for (let section of sections)
recomp(section.ouv = ouv.slice(), section.width, section.height);
} else {
let linregs = [];
for (let i = 0; i < 11; i++)
linregs.push(new LinReg());
for (let section of sections)
if (section.anchored) {
let ouv = section.ouv;
decomp(ouv, section.width, section.height);
for (let i = 0; i < ouv.length; i++)
linregs[i].add(section.snr, ouv[i]);
}
let clearfirst = false;
let clearlast = false;
if (!sections[0].anchored) {
let section = sections[0];
let w = [];
for (let linreg of linregs)
w.push(linreg.get(section.snr));
orthonormalize(w);
section.ouv = w;
section.anchored = clearfirst = true;
}
if (!sections[sections.length - 1].anchored) {
let section = sections[sections.length - 1];
let w = [];
for (let linreg of linregs)
w.push(linreg.get(section.snr));
orthonormalize(w);
section.ouv = w;
section.anchored = clearlast = true;
}
let start = 0;
while (start < sections.length - 1) {
let end = start + 1;
while (!sections[end].anchored)
end++;
if (end > start + 1) {
let si = sections[start];
let ssnr = si.snr;
let souv = si.ouv;
let ei = sections[end];
let esnr = ei.snr;
let eouv = ei.ouv;
let linints = [];
for (let i = 0; i < 11; i++)
linints.push(new LinInt(ssnr, souv[i], esnr, eouv[i]));
for (let j = start + 1; j < end; j++) {
let section = sections[j];
let snr = section.snr;
let w = [];
for (let linint of linints)
w.push(linint.get(snr));
orthonormalize(w);
section.ouv = w;
}
}
start = end;
}
if (clearfirst)
sections[0].anchored = false;
if (clearlast)
sections[sections.length - 1].anchored = false;
for (let section of sections)
recomp(section.ouv, section.width, section.height);
}
}
function decomp(ouv, width, height) {
if (typeof width === "undefined") {
width = height = 1;
}
let u = 0;
let v = 0;
for (let i = 0; i < 3; i++) {
ouv[i] += (ouv[i + 3] + ouv[i + 6]) / 2;
u += ouv[i + 3] * ouv[i + 3];
v += ouv[i + 6] * ouv[i + 6];
}
u = Math.sqrt(u);
v = Math.sqrt(v);
for (let i = 0; i < 3; i++) {
ouv[i + 3] /= u;
ouv[i + 6] /= v;
}
ouv.push(u / width, v / height);
}
function recomp(ouv, width, height) {
if (typeof width === "undefined") {
width = height = 1;
}
let v = ouv.pop() * height;
let u = ouv.pop() * width;
for (let i = 0; i < 3; i++) {
ouv[i + 3] *= u;
ouv[i + 6] *= v;
ouv[i] -= (ouv[i + 3] + ouv[i + 6]) / 2;
}
}
function normalize(arr, idx) {
let len = 0;
for (let i = 0; i < 3; i++)
len += arr[idx + i] * arr[idx + i];
len = Math.sqrt(len);
for (let i = 0; i < 3; i++)
arr[idx + i] /= len;
return len;
}
function orthonormalize(ouv) {
normalize(ouv, 3);
let dot = 0;
for (let i = 0; i < 3; i++)
dot += ouv[i + 3] * ouv[i + 6];
for (let i = 0; i < 3; i++)
ouv[i + 6] -= ouv[i + 3] * dot;
normalize(ouv, 6);
}
function LinInt(x1, y1, x2, y2) {
this.get = function (x) {
return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
};
}
function LinReg() {
let n = 0;
let Sx = 0;
let Sy = 0;
let Sxx = 0;
let Sxy = 0;
let a, b;
this.add = function (x, y) {
n++;
Sx += x;
Sy += y;
Sxx += x * x;
Sxy += x * y;
if (n >= 2) {
b = (n * Sxy - Sx * Sy) / (n * Sxx - Sx * Sx);
a = Sy / n - b * Sx / n;
}
};
this.get = function (x) {
return a + b * x;
};
}