////////////////////////////////////////////////// // generic "graphic object" // constructor // // requires ./micro-utils.js "use strict"; ////////////////////////////////////////////////// // constructor for grob -- graphics object // function grob(section){ this.section = section; this.animated = null; this.running = null; this.start_stop_button = null; this.restore_view_button = null; this.color_list = [ color.black, // 0 color.red, // 1 color.green, // 2 color.blue, // 3 color.orange, // 4 color.cyan, // 5 color.magenta, // 6 color.gray(0.9), // 7 vec(.7, .7, 1), ]; //////////////////////////////////////////////////////////// this.handle_datafile = function(junk, fildes) { print("Data read from file: " + (fildes.name || "??")); print(fildes.text || "zz"); var lines = fildes.text.split("\n"); var data = []; for (var i = 0; (i < lines.length); i++) { var line = lines[i]; if ((line[-1] == "\n")) line = lines[i].slice(0, -1); if ((line.length === 0)) break; var xyzc = line.split(","); var x = parseFloat(xyzc[0]); var y = parseFloat(xyzc[1]); var z = parseFloat(xyzc[2]); var c = (((xyzc.length > 3)) ? parseInt(xyzc[3], 10) : 0); data.push([x,y,z,c,]); } this.set_scale(data); this.plot_dots(data); } // end function handle_datafile this.txtsiz = 16; // in points, I presume this.xmin = null; this.xmax = null; this.ymin = null; this.ymax = null; this.zmin = null; this.zmax = null; this.xscale = null; this.yscale = null; this.zscale = null; this.no_scale = function() { this.xmin = null; this.xmax = null; this.ymin = null; this.ymax = null; this.zmin = null; this.zmax = null; } this.myvec = function(x, y, z){ return vec(x, z, -y); } /////////// find the min and max this.set_scale = function (data){ var d = this.d; var myvec = this.myvec; for (var i = 0; (i < data.length); i++) { var p = data[i]; var x = p[0]; var y = p[1]; var z = p[2]; if (((this.xmin === null) || (x < this.xmin))) this.xmin = x; if (((this.xmax === null) || (x > this.xmax))) this.xmax = x; if (((this.ymin === null) || (y < this.ymin))) this.ymin = y; if (((this.ymax === null) || (y > this.ymax))) this.ymax = y; if (((this.zmin === null) || (z < this.zmin))) this.zmin = z; if (((this.zmax === null) || (z > this.zmax))) this.zmax = z; } this.xscale = (((this.xmax == this.xmin)) ? 0 : 2*d/(this.xmax-(this.xmin))); this.yscale = (((this.ymax == this.ymin)) ? 0 : 2*d/(this.ymax-(this.ymin))); this.zscale = (((this.zmax == this.zmin)) ? 0 : 2*d/(this.zmax-(this.zmin))); label({ pos: myvec(-d, this.axPos.x.y, this.axPos.x.z), text: this.xmin, height: this.txtsiz, box: 0, color: color.red, opacity: 0 }); label({ pos: myvec(d, this.axPos.x.y, this.axPos.x.z), text: this.xmax, height: this.txtsiz, box: 0, color: color.red, opacity: 0 }); label({ pos: myvec(this.axPos.y.x, -d, this.axPos.y.z), text: this.ymin, height: this.txtsiz, box: 0, color: color.green, opacity: 0, align: "right" }); label({ pos: myvec(this.axPos.y.x, d, this.axPos.y.z), text: this.ymax, height: this.txtsiz, box: 0, color: color.green, opacity: 0, align: "right" }); label({ pos: myvec(this.axPos.z.x, this.axPos.z.y, -d), text: this.zmin, height: this.txtsiz, box: 0, color: color.blue, opacity: 0, align: "left" }); label({ pos: myvec(this.axPos.z.x, this.axPos.z.y, d), text: this.zmax, height: this.txtsiz, box: 0, color: color.blue, opacity: 0, align: "left" }); } // end function set_scale this.d = 0.5; this.dsz = null; // dot size this.scene = null; // pointer to canvas this.maincurve = null; this.subcaption = null; this.plot_dots = function(data){ var dots = []; var d = this.d; var myvec = this.myvec; for (var i = 0; (i < data.length); i++) { var p = data[i]; var x = -d+(this.xscale*(p[0]-(this.xmin))); var y = -d+(this.yscale*(p[1]-(this.ymin))); var z = -d+(this.zscale*(p[2]-(this.zmin))); var clr = colorMap(p[3]) || color.black; dots.push(sphere({ pos: myvec(x, y, z), size: myvec(this.dsz, this.dsz, this.dsz), color: clr })); }; var thing = compound(dots); if (0) for (var prop in thing){ xprint("compound: " + prop + " ==> " + thing[prop]); } return thing; } // end function plot_dots this.plot_curve = function(data){ var d = this.d; var trace = curve({ color: color.magenta, radius: d/50 }); var myvec = this.myvec; for (var i = 0; (i < data.length); i++) { var p = data[i]; var x = -d+(this.xscale*(p[0]-(this.xmin))); var y = -d+(this.yscale*(p[1]-(this.ymin))); var z = -d+(this.zscale*(p[2]-(this.zmin))); var clr = colorMap(p[3]) || color.black; trace.push({pos: myvec(x, y, z)}); } return trace; } // end function plot_curve ////////////////////////////////////////////////////////////////////// this.axLabel = vec("x", "y", "z"); var d = this.d; this.axPos = vec( vec( "x", -1.15*d, -1.15*d), // x-label position vec(-1.15*d, "y", -1.15*d), // y-label position vec( 1.15*d, 1.15*d, "z") // z-label position ); // jquery idiom for creating a new button: this.new_button = function(label, attrs){ if (!attrs) attrs = ""; return $('') .text(label).appendTo(this.scene.caption); } this.anim_step = function(){ ; } this.anim_start_stop = function() { if (this.running) { clearTimeout(this.running); this.running = 0; this.start_stop_button.text("Resume"); } else { this.start_stop_button.text("Pause"); this.anim_step(); // will set this.running } } this.restore_view = function(){ this.scene.forward = this.original_fwd; this.scene.range = this.original_range; } this.setup = function(args){ this.original_range = 2.3*this.d; this.original_fwd = vec(0, -0.5, -1); for (var slot in args){ this[slot] = args[slot]; } this.dsz = this.d/15; // dot size this.scene = canvas(); var scene = this.scene; if (0) { scene.userzoom = 0; scene.userspin = 0; } var dumpit = scene.title; if (0) for (var prop in dumpit){ xprint("scene.title: " + prop + " ==> " + dumpit[prop]); } // scene.title.html("Title goes here"); // scene.title.append("blah blah"); if (this.animated) { this.start_stop_button = this.new_button("Pause", 'style="width: 6em;"'); this.start_stop_button.click(methRef(this, this.anim_start_stop)); } this.restore_view_button = this.new_button("Restore View"); this.restore_view_button.click(methRef(this, this.restore_view)); this.subcaption = $('