Skip to content

Mesh3d cell data checks #3369

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 16 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/traces/mesh3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,21 @@ proto.update = function(data) {
var positions = zip3(
toDataCoords(layout.xaxis, data.x, scene.dataScale[0], data.xcalendar),
toDataCoords(layout.yaxis, data.y, scene.dataScale[1], data.ycalendar),
toDataCoords(layout.zaxis, data.z, scene.dataScale[2], data.zcalendar));
toDataCoords(layout.zaxis, data.z, scene.dataScale[2], data.zcalendar)
);

var cells;
if(data.i && data.j && data.k) {
cells = zip3(data.i, data.j, data.k);
}
else if(data.alphahull === 0) {
cells = zip3(
data.i.map(function(e) { return Math.round(e); }),
data.j.map(function(e) { return Math.round(e); }),
data.k.map(function(e) { return Math.round(e); })
);
} else if(data.alphahull === 0) {
cells = convexHull(positions);
}
else if(data.alphahull > 0) {
} else if(data.alphahull > 0) {
cells = alphaShape(data.alphahull, positions);
}
else {
} else {
var d = ['x', 'y', 'z'].indexOf(data.delaunayaxis);
cells = triangulate(positions.map(function(c) {
return [c[(d + 1) % 3], c[(d + 2) % 3]];
Expand Down Expand Up @@ -113,16 +115,13 @@ proto.update = function(data) {
config.vertexIntensity = data.intensity;
config.vertexIntensityBounds = [data.cmin, data.cmax];
config.colormap = parseColorScale(data.colorscale);
}
else if(data.vertexcolor) {
} else if(data.vertexcolor) {
this.color = data.vertexcolor[0];
config.vertexColors = parseColorArray(data.vertexcolor);
}
else if(data.facecolor) {
} else if(data.facecolor) {
this.color = data.facecolor[0];
config.cellColors = parseColorArray(data.facecolor);
}
else {
} else {
this.color = data.color;
config.meshColor = str2RgbaArray(data.color);
}
Expand Down
52 changes: 46 additions & 6 deletions src/traces/mesh3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,58 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
}

var coords = readComponents(['x', 'y', 'z']);
var indices = readComponents(['i', 'j', 'k']);

if(!coords) {
traceOut.visible = false;
return;
}

if(indices) {
// otherwise, convert all face indices to ints
indices.forEach(function(index) {
for(var i = 0; i < index.length; ++i) index[i] |= 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. So you didn't move this to mesh3d/convert.js? Just 🔪 ing it didn't break any tests?

Can you try plotting a mesh3d trace with non-integer indices and see what happens?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@etpinard Thanks for the review.
That's true! One could get bad errors in case of having non-integer ids.
So, in the revised code I added map functions to round the float values to integers.
However; there is now extra logic to check and handle for such cases. A series of jasmine tests are also added to define the expected behaviour in various scenarios.

// three indices should be all provided or not
if(
(traceIn.i && (!traceIn.j || !traceIn.k)) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Ha, you're using traceIn here. So something like

{
  i: 1,
  j: true,
  k: {}
}

would have visible:true.

Could we be more stringent and use traceOut instead, to ensure that i,j,k are all non-empty arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good Call. Now traceOut is applied.
Noting that visible would "only" be false in case of bad 'vertices'.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. I don't understand traceOut.i, traceOut.j and traceOut.k should be defined at this stage. They get filled in during the coerce() calls in readComponents below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. That is fixed in 29f5a06
Now the traces become invisible in case of bad index arrays.

(traceIn.j && (!traceIn.k || !traceIn.i)) ||
(traceIn.k && (!traceIn.i || !traceIn.j))
) {
traceOut.visible = false;
return;
}

// test for size of indices
if(
traceIn.i && Lib.isArrayOrTypedArray(traceIn.i) &&
traceIn.j && Lib.isArrayOrTypedArray(traceIn.j) &&
traceIn.k && Lib.isArrayOrTypedArray(traceIn.k)
) {
if(traceIn.k.length !== 0 && (
traceIn.i.length !== traceIn.j.length ||
traceIn.j.length !== traceIn.k.length)) {
traceOut.visible = false;
return;
}
}

var allIndices = readComponents(['i', 'j', 'k']);
if(allIndices) {
var numVertices = coords[0].length;
allIndices.forEach(function(indices) {
indices.forEach(function(index) {
if(!Lib.isIndex(index, numVertices)) {
traceOut.visible = false;
return;
}
});
});

var numFaces = allIndices[0].length;
for(var q = 0; q < numFaces; q++) {
if(
allIndices[0][q] === allIndices[1][q] ||
allIndices[0][q] === allIndices[2][q] ||
allIndices[1][q] === allIndices[2][q]
) {
traceOut.visible = false;
return;
}
}
}

var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
Expand Down
266 changes: 218 additions & 48 deletions test/jasmine/tests/mesh3d_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,225 @@ var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var failTest = require('../assets/fail_test');

describe('Test mesh3d restyle', function() {
afterEach(destroyGraphDiv);

it('should clear *cauto* when restyle *cmin* and/or *cmax*', function(done) {
var gd = createGraphDiv();

function _assert(user, full) {
var trace = gd.data[0];
var fullTrace = gd._fullData[0];

expect(trace.cauto).toBe(user[0], 'user cauto');
expect(trace.cmin).toBe(user[1], 'user cmin');
expect(trace.cmax).toBe(user[2], 'user cmax');
expect(fullTrace.cauto).toBe(full[0], 'full cauto');
expect(fullTrace.cmin).toBe(full[1], 'full cmin');
expect(fullTrace.cmax).toBe(full[2], 'full cmax');
describe('Test mesh3d', function() {
'use strict';

describe('restyle', function() {
afterEach(destroyGraphDiv);

it('should clear *cauto* when restyle *cmin* and/or *cmax*', function(done) {
var gd = createGraphDiv();

function _assert(user, full) {
var trace = gd.data[0];
var fullTrace = gd._fullData[0];

expect(trace.cauto).toBe(user[0], 'user cauto');
expect(trace.cmin).toBe(user[1], 'user cmin');
expect(trace.cmax).toBe(user[2], 'user cmax');
expect(fullTrace.cauto).toBe(full[0], 'full cauto');
expect(fullTrace.cmin).toBe(full[1], 'full cmin');
expect(fullTrace.cmax).toBe(full[2], 'full cmax');
}

Plotly.plot(gd, [{
type: 'mesh3d',
x: [0, 1, 2, 0],
y: [0, 0, 1, 2],
z: [0, 2, 0, 1],
i: [0, 0, 0, 1],
j: [1, 2, 3, 2],
k: [2, 3, 1, 3],
intensity: [0, 0.33, 0.66, 3]
}])
.then(function() {
_assert([undefined, undefined, undefined], [true, 0, 3]);

return Plotly.restyle(gd, 'cmin', 0);
})
.then(function() {
_assert([false, 0, undefined], [false, 0, 3]);

return Plotly.restyle(gd, 'cmax', 10);
})
.then(function() {
_assert([false, 0, 10], [false, 0, 10]);

return Plotly.restyle(gd, 'cauto', true);
})
.then(function() {
_assert([true, 0, 10], [true, 0, 3]);

return Plotly.purge(gd);
})
.catch(failTest)
.then(done);
});
});

describe('dimension and expected visibility tests', function() {
var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(function() {
Plotly.purge(gd);
destroyGraphDiv();
});

function assertVisibility(exp, msg) {
expect(gd._fullData[0]).not.toBe(undefined, 'no visibility!');
expect(gd._fullData[0].visible).toBe(exp, msg);
}

Plotly.plot(gd, [{
type: 'mesh3d',
x: [0, 1, 2, 0],
y: [0, 0, 1, 2],
z: [0, 2, 0, 1],
i: [0, 0, 0, 1],
j: [1, 2, 3, 2],
k: [2, 3, 1, 3],
intensity: [0, 0.33, 0.66, 3]
}])
.then(function() {
_assert([undefined, undefined, undefined], [true, 0, 3]);

return Plotly.restyle(gd, 'cmin', 0);
})
.then(function() {
_assert([false, 0, undefined], [false, 0, 3]);

return Plotly.restyle(gd, 'cmax', 10);
})
.then(function() {
_assert([false, 0, 10], [false, 0, 10]);

return Plotly.restyle(gd, 'cauto', true);
})
.then(function() {
_assert([true, 0, 10], [true, 0, 3]);

return Plotly.purge(gd);
})
.catch(failTest)
.then(done);
it('@gl mesh3d should be invisible when the indices are not integer', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
i: [0, 0, 0, 1.00001],
j: [1, 1, 2, 2],
k: [2, 3, 3, 2.99999],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(false, 'not to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be invisible when the indices are equal or greater than the number of vertices', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
i: [0, 0, 0, 1],
j: [1, 1, 2, 2],
k: [2, 3, 3, 4],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(false, 'not to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be invisible when the indices are negative', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
i: [0, 0, 0, -1],
j: [1, 1, 2, 2],
k: [2, 3, 3, 3],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(false, 'not to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be invisible when the indices have different sizes', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
i: [0, 0, 0, 1],
j: [1, 1, 2],
k: [2, 3, 3, 3],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(false, 'not to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be invisible when the indices of a triangle point to identical vertex twice', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
i: [0, 0, 0, 1],
j: [1, 1, 2, 3],
k: [2, 3, 3, 3],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(false, 'not to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be visible when the indices are provided and OK', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
i: [0, 0, 0, 1],
j: [1, 1, 2, 2],
k: [2, 3, 3, 3],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(true, 'to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be visible when the index arrays are empty', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
i: [],
j: [],
k: [],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(true, 'to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be visible when the index arrays are not provided', function(done) {
Plotly.plot(gd, [{
x: [0, 1, 0.5, 0.5],
y: [0, 0.5, 1, 0.5],
z: [0, 0.5, 0.5, 1],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(true, 'to be visible');
})
.catch(failTest)
.then(done);
});

it('@gl mesh3d should be visible when the vertex array are empty', function(done) {
Plotly.plot(gd, [{
x: [],
y: [],
z: [],
type: 'mesh3d'
}])
.then(function() {
assertVisibility(true, 'not to be visible');
})
.catch(failTest)
.then(done);
});
});

});