-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpolygon_interactor_ft_demo.html
187 lines (161 loc) · 4.28 KB
/
polygon_interactor_ft_demo.html
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
186
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
html, body {
height: 100%;
overflow: hidden;
}
svg {
float: left;
border-bottom: solid 1px #ccc;
border-right: solid 1px #ccc;
margin-right: -1px;
margin-bottom: -1px;
}
svg .interactors {
cursor: move;
}
div#profile, div#ft {
width: 45%;
height: 100%;
display: inline-block;
}
</style>
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="//www.ncnr.nist.gov/instruments/magik/calculators/js/complex.js"></script>
<script src="polygon-interactor.js"></script>
<div id="profile"></div>
<div id="ft"></div>
<script>
var width = d3.select("#profile").node().clientWidth - 50,
height = d3.select("#profile").node().clientHeight - 50,
radius = 5;
var x = d3.scale.linear()
.domain([0, 10])
.range([0, width])
var y = d3.scale.linear()
.domain([-1, 9])
.range([height, 0]);
var svg = d3.select("#profile").append("svg")
.attr("width", width)
.attr("height", height);
var x0 = 5,
y0 = 5;
var opts1 = {
type:'Polygon',
radius: 15,
name:'roi_polygon',
color1: '#ff0000',
color2: '#ff0000',
interpolation: 'linear',
prevent_crossing: true,
fill: "none",
showcenter: true,
show: false,
points: [[0,0], [1,5],[2,5],[3,5], [4,5],[5, 0], [6, 0],[10,0]]
}
var poly1 = new polygonInteractor(opts1, x, y);
var k1 = Math.PI * 2.0 / 10.0;
var fc = function(k, points) {
var out = new Complex(0,0);
for (var i=0; i<points.length-1; i++) {
var y2 = points[i+1][1],
x2 = points[i+1][0],
y1 = points[i][1],
x1 = points[i][0];
if (x2 < x1) {
throw "non-single valued y(x) (x[n+1] < x[n]) for n=" + i;
}
else if (x2 == x1) { continue }
else {
var m = (y2 - y1) / (x2 - x1);
var c2 = Complex.add(Complex.multiply(-y2/k, Complex.i), m/(k*k)),
c1 = Complex.add(Complex.multiply(-y1/k, Complex.i), m/(k*k)),
a2 = Complex.exp(Complex.multiply(Complex.multiply(Complex.i, k), x2)),
a1 = Complex.exp(Complex.multiply(Complex.multiply(Complex.i, k), x1)),
new_out = Complex.subtract(Complex.multiply(a2, c2), Complex.multiply(a1, c1));
out = Complex.add(new_out, out);
}
}
return out;
}
svg.call(poly1);
var ft = function() {
ft_out = [];
for (var i=0; i<12; i++) {
var ii=i+1;
var k = Math.PI * 2.0 / 10.0 * ii;
ft_out[i] = fc(k, poly1.state.points).magsq(); // .toFixed(4);
}
return ft_out;
}
var margin = {top: 20, right: 20, bottom: 70, left: 40},
widthf = d3.select("#ft").node().clientWidth - margin.left - margin.right,
heightf = d3.select("#ft").node().clientHeight - margin.top - margin.bottom;
var xf = d3.scale.ordinal()
.rangeRoundBands([0, widthf], .1)
.domain(d3.range(13));
var yf = d3.scale.linear()
.range([heightf, 0])
.domain([0,300]);
var xAxisf = d3.svg.axis()
.scale(xf)
.orient("bottom");
var yAxisf = d3.svg.axis()
.scale(yf)
.orient("left")
.ticks(5);
var svgf = d3.select("#ft").append("svg")
.attr("width", widthf + margin.left + margin.right)
.attr("height", heightf + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svgf.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + heightf + ")")
.call(xAxisf);
svgf.append("g")
.attr("class", "y axis")
.call(yAxisf)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("FT");
svgf.selectAll(".bar").
data(ft()).enter()
.append("rect")
.attr("class", "bar")
.attr("x", function(d,i) {return xf(i+1)})
.attr("width", xf.rangeBand())
.attr("y", function(d) {return yf(+d)})
.attr("height", function(d) {return heightf - yf(+d)})
var updatef = function() {
svgf.selectAll(".bar").data(ft())
.attr("y", function(d) {return yf(+d)})
.attr("height", function(d) {return heightf - yf(+d)})
}
poly1.dispatch.on("update", updatef);
</script>
</body>
</html>