Debian Codegraph @

utils/x3dom/x3dom_support.js

raw
1 /*
2 @licstart The following is the entire license notice for the
3 JavaScript code in this page.
4
5 Copyright (C) 2016 Peter Scott
6
7 The JavaScript code in this page is free software: you can
8 redistribute it and/or modify it under the terms of the GNU
9 Lesser General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option)
11 any later version.
12
13 @licend The above is the entire license notice
14 for the JavaScript code in this page.
15 */
16
17 var scalarField;
18 var addWarp = true;
19
20 /* function to convert a list of numbers (in string form) to a 3d array of numbers */
21 function listToArray3d(pts) {
22 pts = pts.trim().split(" ");
23
24 var array = [];
25 var idx = 0;
26 for (var i = 0; i < pts.length; i++) {
27 if (i % 3 == 0) {
28 array[idx] = [];
29 }
30
31 array[idx][i % 3] = Number(pts[i]);
32
33 if (i % 3 == 2) {
34 idx++;
35 }
36 }
37
38 return array;
39 }
40
41 /* function to convert a 3d array of numbers to a list of numbers (in string form) */
42 function array3dToList(arr) {
43 var pts = "";
44
45 for (var i = 0; i < arr.length * 3; i++) {
46 var x = Math.floor(i / 3);
47 var y = i % 3;
48 pts += arr[x][y].toString();
49 pts += " ";
50 }
51
52 return pts;
53 }
54
55 /* function to convert a list of numbers (in string form) to a 1D array of numbers */
56 function listToArray1d(pts) {
57 pts = pts.trim().split(" ");
58
59 var array = [];
60 for (var i = 0; i < pts.length; i++) {
61 array[i] = Number(pts[i]);
62 }
63
64 return array;
65 }
66
67 /* function to convert a 1d array of numbers to a list of numbers (in string form) */
68 function array1dToList(arr) {
69 var pts = "";
70 for (var i = 0; i < arr.length; i++) {
71 pts += arr[i].toString();
72 pts += " ";
73 }
74 }
75
76 /* function to translate colormap and intensity indices into intensity color on the figure */
77 function addColor() {
78 // get information from metadata tag:
79 var metadata = $("metadata")[0];
80 var colormap = listToArray3d($(metadata).attr("color_map"));
81 var indices = listToArray1d($(metadata).attr("indices"));
82
83 // add rgb values to color tag
84 var rgb = [];
85 for (var i = 0; i < indices.length; i++) {
86 var idx = indices[i];
87 rgb[i] = colormap[idx];
88 }
89
90 // add color node to each indexed face set
91 var faces = $("indexedFaceSet");
92 for (var i = 0; i < faces.length; i++) {
93 // update color node with rgb values
94 var color = document.createElement("color");
95 $(color).attr("color", array3dToList(rgb));
96
97 var face = faces[i];
98 face.appendChild(color);
99
100 // update shape's indexed face set -- remove old face set
101 // (this is because x3dom does not automatically regenerate dom tree based when updated)
102 var parent = face.parentElement;
103 parent.removeChild(face);
104 parent.appendChild(face);
105 }
106 }
107
108 /* function to remove color on the figure */
109 function removeColor() {
110 // need to remove the colors from each face set
111 var faces = $("indexedFaceSet");
112 for (var i = 0; i < faces.length; i++) {
113 // remove the color tag
114 var color = $(faces[i]).children("color");
115 color.remove();
116
117 // re-add the faces to the dom then remove old one (with color) -- x3dom doesn't register updates
118 var parent = faces[i].parentElement;
119 parent.removeChild(faces[i]);
120 parent.appendChild(faces[i]);
121 }
122 }
123
124 /* function to calculate the scalar field values at each vertex */
125 function calculateScalars() {
126 // get information from metadata tag:
127 var metadata = $("metadata")[0];
128 var max_value = Number($(metadata).attr("max_value"));
129 var min_value = Number($(metadata).attr("min_value"));
130 var indices = listToArray1d($(metadata).attr("indices"));
131
132 // find scale used to normalize color values (there are 256 colors in map by default)
133 var scale = (min_value == max_value) ? 1.0 : 255.0 / (max_value - min_value);
134
135 // calculate scalar value for each index
136 var scalars = [];
137 for (var i = 0; i < indices.length; i++) {
138 scalars[i] = (indices[i] / scale) + min_value;
139 }
140
141 return scalars;
142 }
143
144 /* function to warp a shape by a scalar field */
145 function warpByScalar() {
146 if (scalarField.length == 0) {
147 return;
148 }
149
150 // add warped shapes if not added already
151 if (addWarp) {
152 var shapes = $("shape");
153 var parent = shapes[0].parentElement;
154
155 for (var i = 0; i < shapes.length; i++) {
156 // find the points for each shape
157 var curr = $(shapes[i]).clone()[0];
158 var coord = $(curr).find("coordinate");
159 var points = listToArray3d($(coord).attr("point"));
160
161 // change the points according to the scalar field
162 for (var j = 0; j < points.length; j++) {
163 // FIXME: generalize perpendicular direction
164 // change the z-coordinate to be the scalar value
165 points[j][2] = scalarField[j];
166 }
167 $(coord).attr("point", array3dToList(points));
168
169 // add classname to new shape
170 $(curr).addClass("warped");
171
172 // add new shape to the parent
173 parent.appendChild(curr);
174 }
175
176 addWarp = false;
177 }
178
179 // adjust the scalar warping by the current scale factor
180 var factor = Number($("#warp-slider")[0].value);
181 var shapes = $(".warped");
182 for (var i = 0; i < shapes.length; i++) {
183 var coord = $(shapes[i]).find("coordinate");
184 var points = listToArray3d($(coord).attr("point"));
185
186 // change the scalar warping by current scale
187 for (var j = 0; j < points.length; j++) {
188 points[j][2] = scalarField[j] * factor;
189 }
190 $(coord).attr("point", array3dToList(points));
191 }
192 }
193
194 /* function to determine whether the x3dom shape should be colored */
195 function shouldColor() {
196 var metadata = $("metadata")[0];
197 if (!metadata) {
198 return false;
199 }
200
201 if (metadata.color) {
202 return true;
203 }
204
205 return false;
206 }
207
208 /* function to call all helper setup functions needed for the menu */
209 function setupMenu() {
210 // first setup the menu buttons
211 setupButtons();
212
213 // next setup the content for each subsection
214 setupColorContent();
215 setupWarpContent();
216 // setupOpacityContent();
217 setupViewpointContent();
218 }
219
220 /* change the content in the menu based on the button selected */
221 function setupButtons() {
222 $("#menu input[type='radio']").change(function() {
223 // get the name from the calling button
224 var name = $(this).attr("id");
225
226 // find the content's parent by using the id for the menu content
227 var parent = $("#menu-content");
228
229 // find the content from the parent by using the name of calling button
230 var selector = "div[for='" + name + "']";
231 var content = $(parent).children(selector);
232 var children = $(parent).children();
233
234 for (var i = 0; i < children.length; i++) {
235 if ($(children[i]).is(content)) {
236 $(children[i]).show();
237 } else {
238 $(children[i]).hide();
239 }
240 }
241 });
242
243 $("#content-options input[type='checkbox']").change(function() {
244 // get the name from the calling button
245 var name = $(this).attr("id").split("-")[1];
246
247 // find the parent for the menu-items
248 var parent = $("#menu-items");
249
250 // find the corresponding label element in the menu buttons
251 var selector = "label[for='button-" + name +"']";
252 var label = $(parent).children(selector);
253
254 // hide or show
255 if (this.checked) {
256 $(label).show();
257 } else {
258 $(label).hide();
259 }
260 });
261 }
262
263 /* function to setup the content for the color subsection in menu */
264 function setupColorContent() {
265 // first set up the listener for the 'show color' checkbox
266 $("#color-checkbox").change(function() {
267 if ($(this).prop("checked")) {
268 addColor();
269 } else {
270 removeColor();
271 }
272 });
273
274 // build the color map into the menu-content location
275 var map_parent = $("#color-map")[0];
276
277 // get information from metadata tag:
278 var colormap = listToArray3d($("metadata").attr("color_map"));
279
280 // add spans to the color map parent with the every other color
281 for (var i = 0; i < colormap.length; i += 2) {
282 var r = Math.round(colormap[i][0] * 256);
283 var g = Math.round(colormap[i][1] * 256);
284 var b = Math.round(colormap[i][2] * 256);
285
286 var span = document.createElement("span");
287 var background_color = "background-color: rgb(" + r + "," + g + "," + b + ")";
288 span.style = background_color;
289 map_parent.appendChild(span);
290 }
291
292 // set minimum and maximum values
293 var min_value = Number($("metadata").attr("min_value"));
294 $("#min-color-value").html(min_value.toPrecision(3));
295
296 var max_value = Number($("metadata").attr("max_value"));
297 $("#max-color-value").html(max_value.toPrecision(3));
298 }
299
300 /* function to setup the content for the warping section in the menu */
301 function setupWarpContent() {
302 // calculate the scalars initially for frame of reference -- global value
303 scalarField = calculateScalars();
304
305 // setup the checkbox to toggle the slider
306 $("#warp-checkbox").change(function() {
307 document.getElementById("warp-slider").disabled = !document.getElementById("warp-slider").disabled;
308
309 if (!document.getElementById("warp-slider").disabled) {
310 warpByScalar();
311 } else {
312 // hide the warped shapes
313 $(".warped").remove();
314 addWarp = true;
315 }
316 });
317
318 // setup the slider to change the warped shape and to change the value of the label
319 $("#warp-slider").change(function() {
320 $("#warp-slider-val").html($(this).val());
321 warpByScalar();
322 });
323 }
324
325 /* adjust opacity */
326 function adjustOpacity(val) {
327 // get the first indexed face set in the scene
328 var faces = $("indexedFaceSet")[0];
329 var parent = faces.parentElement;
330 var material = $(parent).find("material");
331
332 var transparency = 1 - val;
333 material.attr("transparency", transparency);
334 }
335
336 function setupOpacityContent() {
337 $("#opacity-slider").change(function() {
338 $("#opacity-slider-val").html($(this).val());
339 adjustOpacity(Number($(this).val()));
340 });
341 }
342
343 /* function to setup the content for the viewpoint buttons */
344 function setupViewpointContent() {
345 // add corresponding click listeners for each button
346 var buttons = $(".viewpoint");
347 for (var i = 0; i < buttons.length; i++) {
348 $(buttons[i]).click(function() {
349 // the name of the button is the corresponding id of the viewpoint
350 var name = $(this).text();
351 $("#" + name).attr("set_bind", true);
352 });
353 }
354 }
355
356 // add color as soon as the document is ready
357 $(document).ready(function() {
358 setupMenu();
359
360 // TODO check if color should be added
361 if (shouldColor() || true) {
362 addColor();
363 }
364 });