Skip to content

Preserve gl3d scene aspectratio after orthographic scroll zoom #4578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 12, 2020
58 changes: 29 additions & 29 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ proto.initializeGLPlot = function() {
y: s * o.y,
z: s * o.z
});
scene._aspectmode = 'manual';
}

relayoutCallback(scene);
Expand Down Expand Up @@ -725,45 +726,44 @@ proto.plot = function(sceneData, fullLayout, layout) {
});
}

var axesScaleRatio = [1, 1, 1];

// Compute axis scale per category
for(i = 0; i < 3; ++i) {
axis = fullSceneLayout[axisProperties[i]];
axisType = axis.type;
var axisRatio = axisTypeRatios[axisType];
axesScaleRatio[i] = Math.pow(axisRatio.acc, 1.0 / axisRatio.count) / dataScale[i];
}

/*
* Dynamically set the aspect ratio depending on the users aspect settings
*/
var axisAutoScaleFactor = 4;
var aspectRatio;

if(fullSceneLayout.aspectmode === 'auto') {
if(Math.max.apply(null, axesScaleRatio) / Math.min.apply(null, axesScaleRatio) <= axisAutoScaleFactor) {
/*
* USE DATA MODE WHEN AXIS RANGE DIMENSIONS ARE RELATIVELY EQUAL
*/

aspectRatio = axesScaleRatio;
} else {
/*
* USE EQUAL MODE WHEN AXIS RANGE DIMENSIONS ARE HIGHLY UNEQUAL
*/
aspectRatio = [1, 1, 1];
}
} else if(fullSceneLayout.aspectmode === 'cube') {
var aspectmode = scene._aspectmode || fullSceneLayout.aspectmode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between scene._aspectmode and fullSceneLayout.aspectmode ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scene._aspectmode is an internal variable which is set after aspectratio values are computed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there situations where scene._aspectmode and fullSceneLayout.aspectmode aren't the same?

Copy link
Contributor Author

@archmoj archmoj Feb 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is. Namely after using scroll zoom.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but shouldn't the changes to aspectmode during scroll zoom propagate to fullLayout ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially tried to achieve that on another branch using relayout updates.
Since tests started failing, I thought that may not be ideal to override fullLayout in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious. Which tests fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there situations where scene._aspectmode and fullSceneLayout.aspectmode aren't the same?

OK. This time I was able to make that work without a temporary variable: 1828797

if(aspectmode === 'cube') {
aspectRatio = [1, 1, 1];
} else if(fullSceneLayout.aspectmode === 'data') {
aspectRatio = axesScaleRatio;
} else if(fullSceneLayout.aspectmode === 'manual') {
} else if(aspectmode === 'manual') {
var userRatio = fullSceneLayout.aspectratio;
aspectRatio = [userRatio.x, userRatio.y, userRatio.z];
} else if(aspectmode === 'auto' || aspectmode === 'data') {
var axesScaleRatio = [1, 1, 1];
// Compute axis scale per category
for(i = 0; i < 3; ++i) {
axis = fullSceneLayout[axisProperties[i]];
axisType = axis.type;
var axisRatio = axisTypeRatios[axisType];
axesScaleRatio[i] = Math.pow(axisRatio.acc, 1.0 / axisRatio.count) / dataScale[i];
}

if(aspectmode === 'data') {
aspectRatio = axesScaleRatio;
} else { // i.e. 'auto' option
if(
Math.max.apply(null, axesScaleRatio) /
Math.min.apply(null, axesScaleRatio) <= 4
) {
// USE DATA MODE WHEN AXIS RANGE DIMENSIONS ARE RELATIVELY EQUAL
aspectRatio = axesScaleRatio;
} else {
// USE EQUAL MODE WHEN AXIS RANGE DIMENSIONS ARE HIGHLY UNEQUAL
aspectRatio = [1, 1, 1];
}
}
} else {
throw new Error('scene.js aspectRatio was not one of the enumerated types');
}
scene._aspectmode = aspectmode;

/*
* Write aspect Ratio back to user data and fullLayout so that it is modifies as user
Expand Down
92 changes: 92 additions & 0 deletions test/jasmine/tests/gl3d_plot_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,98 @@ describe('Test gl3d drag and wheel interactions', function() {
.catch(failTest)
.then(done);
});

it('@gl should preserve aspectratio values when orthographic scroll zoom i.e. after restyle', function(done) {
var coords = {
x: [1, 2, 10, 4, 5],
y: [10, 2, 4, 4, 2],
z: [10, 2, 4, 8, 16],
};

var mock = {
data: [{
type: 'scatter3d',
x: coords.x,
y: coords.y,
z: coords.z,
mode: 'markers',
marker: {
color: 'red',
size: 16,
}
}, {
type: 'scatter3d',
x: [coords.x[0]],
y: [coords.y[0]],
z: [coords.z[0]],
mode: 'markers',
marker: {
color: 'blue',
size: 32,
}
}],
layout: {
width: 400,
height: 400,
scene: {
camera: {
projection: {
type: 'orthographic'
}
},
}
}
};

var sceneTarget;
var relayoutEvent;
var relayoutCnt = 0;

Plotly.plot(gd, mock)
.then(function() {
gd.on('plotly_relayout', function(e) {
relayoutCnt++;
relayoutEvent = e;
});

sceneTarget = gd.querySelector('.svg-container .gl-container #scene canvas');
})
.then(function() {
var aspectratio = gd._fullLayout.scene.aspectratio;
expect(aspectratio.x).toBeCloseTo(0.898, 3, 'aspectratio.x');
expect(aspectratio.y).toBeCloseTo(0.798, 3, 'aspectratio.y');
expect(aspectratio.z).toBeCloseTo(1.396, 3, 'aspectratio.z');
})
.then(function() {
return scroll(sceneTarget);
})
.then(function() {
expect(relayoutCnt).toEqual(1);

var aspectratio = relayoutEvent['scene.aspectratio'];
expect(aspectratio.x).toBeCloseTo(0.816, 3, 'aspectratio.x');
expect(aspectratio.y).toBeCloseTo(0.725, 3, 'aspectratio.y');
expect(aspectratio.z).toBeCloseTo(1.269, 3, 'aspectratio.z');
})
.then(function() {
// select a point
var i = 2;

return Plotly.restyle(gd, {
x: [[coords.x[i]]],
y: [[coords.y[i]]],
z: [[coords.z[i]]],
}, 1);
})
.then(function() {
var aspectratio = gd._fullLayout.scene.aspectratio;
expect(aspectratio.x).toBeCloseTo(0.816, 3, 'aspectratio.x');
expect(aspectratio.y).toBeCloseTo(0.725, 3, 'aspectratio.y');
expect(aspectratio.z).toBeCloseTo(1.269, 3, 'aspectratio.z');
})
.catch(failTest)
.then(done);
});
});

describe('Test gl3d relayout calls', function() {
Expand Down