Ignore:
Timestamp:
08/28/18 09:45:51 (7 years ago)
Author:
Mathieu Morlighem
Message:

merged trunk-jpl and trunk for revision 23187

Location:
issm/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk

  • issm/trunk/src

  • TabularUnified issm/trunk/src/m/plot/applyoptions.js

    r22758 r23189  
    1313                        //{{{ Variable options initialization
    1414                        var caxis = options.getfieldvalue('caxis');
    15                         var colorbarinnerlabels = options.getfieldvalue('colorbarinnerlabels','off');
    1615                        var ccanvasid, ctitleid, clabelsid, ccanvas, ctitle, clabels, ccontext, cmap, colorbar, cwidth, cheight, cgradient, color, y, x;
    1716                        //}}}
     
    4948                        } //}}}
    5049                        //{{{ Initialize colorbar canvas
    51                         ccanvasid = options.getfieldvalue('colorbarid', options.getfieldvalue('canvasid').replace('canvas','colorbar-canvas'));
     50                        let colorbarSlug = options.getfieldvalue('colorbarSlug', options.getfieldvalue('canvasid') + '-colorbar');
     51                        ccanvasid = colorbarSlug + '-canvas';
    5252                        ccanvas = $('#'+ccanvasid)[0];
    5353                        cwidth = ccanvas.width*options.getfieldvalue('colorbarwidth', 1);
     
    8181                        //}}}
    8282                        //{{{ Draw colorbar labels
    83                         clabelsid = options.getfieldvalue('colorbarid', ccanvasid).replace('canvas','labels');
     83                        clabelsid = colorbarSlug + '-labels';
    8484                        clabels = $('#'+clabelsid);
    85                         if (colorbarinnerlabels=='on') {
    86                                 clabels.removeClass('sim-colorbar-labels-outer');
    87                                 clabels.addClass('sim-colorbar-labels-inner');
    88                         }
    89                         else {
    90                                 clabels.removeClass('sim-colorbar-labels-inner');
    91                                 clabels.addClass('sim-colorbar-labels-outer');
    92                         }
    9385                        var clabelstring = '';
    9486                        clabels.empty();
     
    10799                        //}}}
    108100                        //{{{ Draw colorbar title
    109                         ctitleid = options.getfieldvalue('colorbarid', ccanvasid).replace('canvas','heading');
     101                        ctitleid = colorbarSlug + '-heading';
    110102                        ctitle = $('#'+ctitleid);
    111                         if (options.exist('colorbartitle')) { ctitle.html(options.getfieldvalue('colorbartitle')); }
     103                        if (options.exist('colorbarHeader')) { ctitle.html(options.getfieldvalue('colorbarHeader')); }
    112104                        //}}}
    113105                }
     
    123115        tcontext = tcanvas.getContext('2d');
    124116        tgradient = tcontext.createLinearGradient(0, 0, 0, 256);
    125        
     117
    126118        var cmap = options.getfieldvalue('colormap','jet');
    127119        var colorbar = colorbars[cmap];
     
    133125        tcontext.fillStyle = tgradient;
    134126        tcontext.fillRect(0, 0, 256, 256);
    135        
     127
    136128        //Allow for special texture colors, drawing each color in equal width vertical rectangles. The last rectanglar section is reserved for the colormap.
    137129        if (options.exist('maskregion')) {
    138130                var maskObject = options.getfieldvalue('maskregion',{'enabled':false});
    139                 if (maskObject.enabled && !VESL.Helpers.isEmptyOrUndefined(maskObject.colors)) {
     131                if (maskObject.enabled && !vesl.helpers.isEmptyOrUndefined(maskObject.colors)) {
    140132                        var x = 0;
    141133                        var sections = Object.keys(maskObject.colors).length + 1;
     
    148140                }
    149141        }
    150        
     142
    151143        tURL = tcanvas.toDataURL();
    152144        if (options.getfieldvalue('clf','on')=='off') {
     
    157149        //}}}
    158150        //{{{ text display
    159         var overlaycanvasid = options.getfieldvalue('overlayid', options.getfieldvalue('canvasid')+'-overlay');
    160         var overlaycanvas = $('#'+overlaycanvasid)[0];
    161         if (!VESL.Helpers.isEmptyOrUndefined(overlaycanvas)) {
     151        var ctx;
     152        var overlaycanvasid;
     153        var overlaycanvas;
     154        //Only intialize overlay canvas once by checking if it's already been defined
     155        if (vesl.helpers.isEmptyOrUndefined(canvas.overlaycanvas)) {
    162156                //Get drawing context and save reference on main WebGL canvas
    163                 var ctx = overlaycanvas.getContext('2d');
     157                overlaycanvasid = options.getfieldvalue('overlayid', options.getfieldvalue('canvasid') + '-overlay')
     158                overlaycanvas = $('#' + overlaycanvasid)[0];
     159                ctx = overlaycanvas.getContext('2d');
    164160                canvas.overlaycanvas = overlaycanvas;
    165                
    166                 //Resize interal viewport coordinates to match screenspace coordinates
    167                 var rect = overlaycanvas.getBoundingClientRect();
    168                 overlaycanvas.width  = rect.width;
    169                 overlaycanvas.height = rect.height;
    170                
    171                 //Clear canvas each frame for any new drawings
    172                 canvas.overlayHandlers['draw'] = function(overlaycanvas) {
    173                         ctx.clearRect(0, 0, overlaycanvas.width, overlaycanvas.height);
    174                 }
    175161        }
    176         //{{{ lat long overlay
    177         if (options.exist('latlongoverlay')) {
    178                 var latitudes = {
    179                         //'-90': 1,
    180                         //'-65': .999,
    181                         '-60': 0.994046875,
    182                         '-45': 0.955729166666666,
    183                         '-30': 0.9226562500000024,
    184                         //'-15': 0.830729166666665,
    185                         '0': 0.74218749999999811,
    186                         //'15': 0.63932291666666663,
    187                         '30': 0.523390625000001,
    188                         '45': 0.4020001,
    189                         '60': 0.26953124999999978,
    190                         //'65': 0.225390625,
    191                         //'90': 0.0,
    192                 }
    193                 var longitudes = [-150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150, 180];
    194                 canvas.overlayHandlers['latlong'] = function(canvas) {
    195                         //Transform from world space to viewport space
    196                         var centerx = overlaycanvas.width / 2;
    197                         var centery = overlaycanvas.height / 2;
    198                         var radius = (overlaycanvas.height) / 2;
    199 
    200                         //Draw latitudes
    201                         ctx.setLineDash([5, 10]);
    202                         for(latitude in latitudes) {
    203                                 ctx.beginPath();
    204                                 ctx.arc(centerx, centery, radius * latitudes[latitude], 0, 2 * Math.PI);
    205                                 ctx.stroke();
    206                                 ctx.font = 'bold ' + String(options.getfieldvalue('colorbarfontsize', 18))+'px "Lato", Helvetica, Arial, sans-serif';
    207                                 ctx.fillStyle = options.getfieldvalue('colorbarfontcolor','black');
    208                                 ctx.strokeStyle = options.getfieldvalue('colorbarfontcolor','black');
    209                                 ctx.textAlign = 'center';
    210                                 ctx.textBaseline = 'middle';
    211                                 ctx.fillText(latitude, centerx, centery + radius * latitudes[latitude]);
    212                                 ctx.strokeText(latitude, centerx, centery + radius * latitudes[latitude]);
    213                         }
    214                         //Draw longitudes
    215                         ctx.setLineDash([1, 0]);
    216                         for (longitude in longitudes) {
    217                                 ctx.beginPath();
    218                                 ctx.moveTo(centerx, centery);
    219                                 ctx.lineTo(centerx + radius * Math.sin(longitudes[longitude] * DEG2RAD), centery + radius * Math.cos(longitudes[longitude] * DEG2RAD));
    220                                 ctx.stroke();
    221                         }
    222                 }
    223         } //}}}
    224         if (options.exist('textlabels')) {
     162        overlaycanvas = canvas.overlaycanvas;
     163        ctx = overlaycanvas.getContext('2d');
     164
     165        if (options.exist('textlabels')) {//{{{
    225166                //Attatch new overlay handler to display text labels
    226167                var textLabels = options.getfieldvalue('textlabels',[]);
     
    236177
    237178                                };
    238                                
     179
    239180                                // function declared in slr-gfm sim-front-end-controller.js
    240181                                // if labels are behind the globe sphere then skip iteartion and do not display them
    241                                 if (VESL.UI.isLabelVisible(textLabel)) {
     182                                if (vesl.ui.isLabelVisible(textLabel)) {
    242183                                        //Transform from world space to viewport space
    243184                                        var screenPoint = vec3.transformMat4(vec3.create(), textLabel.position, canvas.camera.vpMatrix);
    244185                                        var x = (screenPoint[0] + 1.0) * (canvas.width / 2) + canvas.selector.offset().left;
    245186                                        var y = (-screenPoint[1] + 1.0) * (canvas.height / 2) + canvas.selector.offset().top;
    246                                        
     187
    247188                                        //Draw text
    248189                                        ctx.font = 'bold ' + String(textLabel.fontSize) + 'px Arial Black, sans-serif';
     
    256197                        }
    257198                }
    258         } //}}}
     199        }//}}}
     200
    259201        //{{{ additional rendering nodes
    260202        if (options.exist('render')) {
     
    275217
    276218                var renderObjects = options.getfieldvalue('render',{});
     219
    277220                for (var renderObject in renderObjects) {
    278221                        //Modify renderObejct?
     
    289232                                color: defaultFor(object.color, 'black'),                                       //Diffuse color of object
    290233                                height: defaultFor(object.height, 25000),                                       //Height of object along y axis, currently for clouds only
    291                                 range: defaultFor(object.range, 120000),                                                //Range of sz plane to spawn object, currently for clouds only
    292                                 quantity: defaultFor(object.quantity, 15)                                       //Quantity of objects to display, currently for clouds only
     234                                range: defaultFor(object.range, 120000),                                        //Range of sz plane to spawn object, currently for clouds only
     235                                quantity: defaultFor(object.quantity, 15),                                      //Quantity of objects to display, currently for clouds only
     236                                source: defaultFor(object.source, 'NY'),                                        //Quantity of objects to display, currently for clouds only
     237                                targets: defaultFor(object.targets, ['NY'])                                     //Quantity of objects to display, currently for clouds only
    293238                        };
    294239                        if (!object.enabled) { continue; }
     
    367312                                node.geometryShader('Mesh', mesh, 'Vertices', [object.x, object.y, object.z], 'Indices', object.indices);
    368313                        }
    369                         if ('axis' === renderObject && !('axis' in canvas.nodes)) {             
     314                        if ('axis' === renderObject && !('axis' in canvas.nodes)) {
    370315                                node = new Node(
    371316                                        'canvas', canvas,
     
    382327                                node.patch('Vertices', [object.x, object.y, object.z], 'FaceColor', 'none');
    383328                        }
    384                         if ('city' === renderObject) {
     329                        if ('city' === renderObject && !vesl.helpers.isEmptyOrUndefined(overlaycanvas)) {
    385330                                //city
    386331                                var mesh = GL.Mesh.sphere({size: object.size});
     
    413358                        }
    414359                        if ('clouds' === renderObject && !('clouds0' in canvas.nodes)) {
    415                                 //clouds                               
     360                                //clouds
    416361                                var mesh = GL.Mesh.fromURL(canvas.assetsPath + '/obj/cloud.obj');
    417362                                for (var i = 0; i < object.quantity; i++) {
    418363                                        //TODO: More options, less magic numbers. Add animation. Better shading.
    419                                         var offset = [randomizeAxis(translation[0], object.range), 
    420                                                                                                 randomizeCloudHeight(translation[1], object), 
     364                                        var offset = [randomizeAxis(translation[0], object.range),
     365                                                                                                randomizeCloudHeight(translation[1], object),
    421366                                                                                                randomizeAxis(translation[2], object.range)];
    422367                                        var randomSize = randomizeCloudSize(object.scale);
     
    436381                                }
    437382                        }
     383                        if ('citylines' === renderObject) {
     384                                //city
     385                                node = new Node(
     386                                        'canvas', canvas,
     387                                        'options', options,
     388                                        'renderObject', object,
     389                                        'name', 'citylines',
     390                                        'shaderName', 'ColoredDiffuse',
     391                                        'drawMode', gl.LINES,
     392                                        'diffuseColor', object.color,
     393                                        'lineWidth', options.getfieldvalue('linewidth', 1),
     394                                        'scale', [object.scale, object.scale, object.scale],
     395                                        'rotation', [0, 0, 0]
     396                                );
     397
     398                                //For each target city, calculate the shortest line across the earth by performing a quaternion slerp.
     399                                //Treat source and target city as vectors to rotate to from the north pole.
     400                                //Then, slerp between the two rotations, and generate points across equidistance points on the earth to create the line.
     401                                var north = vec3.fromValues(0, 1, 0);
     402                                var source = object.source;
     403                                var sourceXYZ = vec3.fromValues(xcity[source], zcity[source], -ycity[source]);
     404                                var radius = vec3.length(sourceXYZ);
     405                                var lineSteps = 50;
     406                                var lineX = [];
     407                                var lineY = [];
     408                                var lineZ = [];
     409                                var lineXYZ = vec3.create();
     410
     411                                for (var i = 0; i < object.targets.length; i++) {
     412                                        var target = object.targets[i];
     413                                        var targetXYZ = vec3.fromValues(xcity[target], zcity[target], -ycity[target]);
     414                                        var axis = vec3.cross(vec3.create(), sourceXYZ, targetXYZ);
     415                                        vec3.normalize(axis, axis);
     416
     417                                        //Get the total angle between the two cities.
     418                                        var sourceXYZAxis = vec3.normalize(vec3.create(), sourceXYZ);
     419                                        var targetXYZAxis = vec3.normalize(vec3.create(), targetXYZ);
     420                                        var dotProduct = vec3.dot(sourceXYZAxis, targetXYZAxis);
     421                                        var totalAngle = Math.acos(dotProduct); //theta = arccos(u . v / (||u|| * ||v||); in this case, ||u|| and ||v|| are 1, since u and v are unit vectors.
     422
     423                                        var lineQuat = quat.create();
     424                                        for (var j = 1; j <= lineSteps; j++) {
     425                                                //Calculate the partial rotation to obtain points on the line between the two cities.
     426                                                var angle = j / lineSteps * totalAngle;
     427                                                quat.setAxisAngle(lineQuat, axis, angle);
     428                                                quat.normalize(lineQuat, lineQuat);
     429                                                vec3.transformQuat(lineXYZ, sourceXYZ, lineQuat);
     430                                                //GL.LINES needs 2 points for each line - at the beginning, just use the sourceXYZ.
     431                                                //TODO: Eliminate this if statement.
     432                                                if (j === 1) {
     433                                                        lineX.push(sourceXYZ[0]);
     434                                                        lineY.push(sourceXYZ[1]);
     435                                                        lineZ.push(sourceXYZ[2]);
     436                                                } else {
     437                                                        lineX.push(lineX[lineX.length - 1]);
     438                                                        lineY.push(lineY[lineY.length - 1]);
     439                                                        lineZ.push(lineZ[lineZ.length - 1]);
     440                                                }
     441                                                lineX.push(lineXYZ[0]);
     442                                                lineY.push(lineXYZ[1]);
     443                                                lineZ.push(lineXYZ[2]);
     444                                        }
     445                                }
     446                                node.patch('Vertices', [lineX, lineY, lineZ]);
     447                        }
    438448                }
    439449        } //}}}
     
    445455                var minHeigth = object.height - 7000;
    446456                var randomHeight = (Math.random() * (maxHeight - minHeigth)) + minHeigth;
    447                
     457
    448458                return canvasGroundHeight + randomHeight;
    449459}
     
    451461// assumes that originAxisValue is the mid-value between min and max.
    452462function randomizeAxis(originAxisValue, range) {
    453                 return originAxisValue + (Math.random() - 0.5) * (range * 2); 
     463                return originAxisValue + (Math.random() - 0.5) * (range * 2);
    454464}
    455465
     
    457467        var maxResize = 1.3;
    458468        var minResize = 0.5;
    459         var randomizationFactor = Math.random() * (maxResize - minResize) + minResize; 
     469        var randomizationFactor = Math.random() * (maxResize - minResize) + minResize;
    460470        return scale * randomizationFactor;
    461471}
Note: See TracChangeset for help on using the changeset viewer.