This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontourWidget.js
231 lines (183 loc) · 5.91 KB
/
contourWidget.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//==============================================================================
// Contour detection for detecting serial sectioned tissue.
// Similar to polyline widget, but reduced editablity and has a sequesnce number.
// If they are autpomatically created, I might save them in the database
// so the user can edit them.
var CONTOUR_WIDGET_WAITING = 0;
var CONTOUR_WIDGET_PROPERTIES_DIALOG = 1;
function ContourWidget (viewer, contour) {
if (viewer === undefined) {
return;
}
this.Contour = contour;
// I do not think we need a dialog.
this.Dialog = new Dialog(this);
this.Dialog.Title.text('Contour Editor');
this.Viewer = viewer;
// Needed for delete.
this.Popup = new WidgetPopup(this);
var cam = viewer.MainView.Camera;
var viewport = viewer.MainView.Viewport;
this.Shape = new Polyline();
this.Shape.OutlineColor = [0.0, 0.0, 0.0];
this.Shape.SetOutlineColor([0.0, 0.0, 1.0]);
this.Shape.FixedSize = false;
this.Viewer.WidgetList.push(this);
// Set line thickness using viewer. (5 pixels).
// The Line width of the shape switches to 0 (single line)
// when the actual line with is too thin.
this.Shape.LineWidth = 0;
this.State = CONTOUR_WIDGET_WAITING;
eventuallyRender();
}
ContourWidget.prototype.Draw = function(view) {
this.Shape.Draw(view);
}
ContourWidget.prototype.Serialize = function() {
if(this.Shape === undefined){ return null; }
var obj = new Object();
obj.type = "contour";
// Copy the points to avoid array reference bug.
obj.points = [];
for (var i = 0; i < this.Shape.Points.length; ++i) {
obj.points.push([this.Shape.Points[i][0], this.Shape.Points[i][1]]);
}
return obj;
}
// Load a widget from a json object (origin MongoDB).
// Object already json decoded.
ContourWidget.prototype.Load = function(obj) {
this.Shape.Points = [];
for(var n=0; n < obj.points.length; n++){
this.Shape.Points[n] = [parseFloat(obj.points[n][0]),
parseFloat(obj.points[n][1])];
}
this.UpdateBounds();
this.Shape.UpdateBuffers();
}
ContourWidget.prototype.RemoveFromViewer = function() {
if (this.Viewer == null) {
return;
}
var idx = this.Viewer.WidgetList.indexOf(this);
if(idx!=-1) {
this.Viewer.WidgetList.splice(idx, 1);
}
}
ContourWidget.prototype.HandleKeyPress = function(event) {
if (event.keyCode == ??) {
// delete key
.....
return false;
}
return true;
}
ContourWidget.prototype.Deactivate = function() {
this.Popup.StartHideTimer();
this.State = CONTOUR_WIDGET_WAITING;
this.Viewer.DeactivateWidget(this);
this.Shape.Active = false;
if (this.DeactivateCallback) {
this.DeactivateCallback();
}
eventuallyRender();
}
// Mouse down does nothing. Mouse up causes all state changes.
ContourWidget.prototype.HandleMouseDown = function(event) {
}
ContourWidget.prototype.HandleMouseUp = function(event) {
}
ContourWidget.prototype.HandleMouseMove = function(event) {
}
ContourWidget.prototype.HandleTouchPan = function(event) {
}
ContourWidget.prototype.HandleTouchPinch = function(event) {
}
ContourWidget.prototype.HandleTouchEnd = function(event) {
}
ContourWidget.prototype.CheckActive = function(event) {
var x = event.offsetX;
var y = event.offsetY;
var pt = this.Viewer.ConvertPointViewerToWorld(x,y);
// Check for mouse touching an edge.
var width = Math.max(this.MinLine * 4, this.LineWidth);
for (var i = 1; i < this.Shape.Points.length; ++i) {
if (this.Shape.IntersectPointLine(pt, this.Shape.Points[i-1],
this.Shape.Points[i], width)) {
this.State = CONTOUR_WIDGET_ACTIVE;
this.Shape.Active = true;
this.PlacePopup();
return true;
}
}
if (this.Shape.Closed) {
if (this.Shape.IntersectPointLine(pt, this.Shape.Points[0],
this.Shape.Points[this.Shape.Points.length-1], width)) {
this.State = CONTOUR_WIDGET_ACTIVE;
this.Shape.Active = true;
this.PlacePopup();
return true;
}
}
return false;
}
// Multiple active states. Active state is a bit confusing.
// Only one state (WAITING) does not receive events from the viewer.
ContourWidget.prototype.GetActive = function() {
if (this.State == CONTOUR_WIDGET_WAITING) {
return false;
}
return true;
}
// Active simply means that the widget is receiving events.
// This widget can activate verticies, the whole polyline, or a middle vertex.
ContourWidget.prototype.SetActive = function(flag) {
if (flag == this.GetActive()) {
return;
}
if (flag) {
this.State = CONTOUR_WIDGET_ACTIVE;
this.Shape.Active = true;
this.Viewer.ActivateWidget(this);
this.PlacePopup();
eventuallyRender();
} else {
this.Deactivate();
}
}
//This also shows the popup if it is not visible already.
ContourWidget.prototype.PlacePopup = function () {
// The popup gets in the way when firt creating the line.
if (this.State == CONTOUR_WIDGET_NEW_EDGE ||
this.State == CONTOUR_WIDGET_NEW) {
return;
}
var roll = this.Viewer.GetCamera().Roll;
var s = Math.sin(roll + (Math.PI*0.25));
var c = Math.cos(roll + (Math.PI*0.25));
if (this.Shape.Points.length < 1) { return; }
// Find the upper right most vertex.
var x = this.Shape.Points[0][0];
var y = this.Shape.Points[0][1];
var best = (c*x)-(s*y);
for (idx = 1; idx < this.Shape.Points.length; ++idx) {
var tx = this.Shape.Points[idx][0];
var ty = this.Shape.Points[idx][1];
var tmp = (c*tx)-(s*ty);
if (tmp > best) {
best = tmp;
x = tx;
y = ty;
}
}
var pt = this.Viewer.ConvertPointWorldToViewer(x, y);
pt[0] += 20;
pt[1] -= 10;
this.Popup.Show(pt[0],pt[1]);
}
// Can we bind the dialog apply callback to an objects method?
var CONTOUR_WIDGET_DIALOG_SELF;
ContourWidget.prototype.ShowPropertiesDialog = function () {
}
ContourWidget.prototype.DialogApplyCallback = function() {
}