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 12 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
97 changes: 72 additions & 25 deletions src/traces/mesh3d/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,53 @@ proto.handlePick = function(selection) {
};

function parseColorArray(colors) {
return colors.map(str2RgbaArray);
var b = [];
var len = colors.length;
for(var i = 0; i < len; i++) {
b[i] = str2RgbaArray(colors[i]);
}
return b;
}

// Unpack position data
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 wondering where could these functions go if we want to reuse these functions in other places e.g. in iso-surfaces?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd vote for putting helper functions like this one in a new file src/traces/mesh3d/helpers.js.

function toDataCoords(axis, coord, scale, calendar) {
var b = [];
var len = coord.length;
for(var i = 0; i < len; i++) {
b[i] = axis.d2l(coord[i], 0, calendar) * scale;
}
return b;
}

// Round indices if passed as floats
function toRoundIndex(a) {
var b = [];
var len = a.length;
for(var i = 0; i < len; i++) {
b[i] = Math.round(a[i]);
}
return b;
}

function delaunayCells(delaunayaxis, positions) {
var d = ['x', 'y', 'z'].indexOf(delaunayaxis);
var b = [];
var len = positions.length;
for(var i = 0; i < len; i++) {
b[i] = [positions[i][(d + 1) % 3], positions[i][(d + 2) % 3]];
}
return triangulate(b);
}

// Validate indices
function hasValidIndices(list, numVertices) {
var len = list.length;
for(var i = 0; i < len; i++) {
if(list[i] < 0 || list[i] >= numVertices) {
return false;
}
}
return true;
}

proto.update = function(data) {
Expand All @@ -61,33 +107,37 @@ proto.update = function(data) {

this.data = data;

// Unpack position data
function toDataCoords(axis, coord, scale, calendar) {
return coord.map(function(x) {
return axis.d2l(x, 0, calendar) * scale;
});
}
var numVertices = data.x.length;

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) {

if(
data.i.length !== data.j.length ||
data.j.length !== data.k.length ||
!hasValidIndices(data.i, numVertices) ||
!hasValidIndices(data.j, numVertices) ||
!hasValidIndices(data.k, numVertices)
) {
return;
}
cells = zip3(
toRoundIndex(data.i),
toRoundIndex(data.j),
toRoundIndex(data.k)
);
} else if(data.alphahull === 0) {
cells = convexHull(positions);
}
else if(data.alphahull > 0) {
} else if(data.alphahull > 0) {
cells = alphaShape(data.alphahull, positions);
}
else {
var d = ['x', 'y', 'z'].indexOf(data.delaunayaxis);
cells = triangulate(positions.map(function(c) {
return [c[(d + 1) % 3], c[(d + 2) % 3]];
}));
} else {
cells = delaunayCells(data.delaunayaxis, positions);
}

var config = {
Expand All @@ -113,16 +163,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
16 changes: 9 additions & 7 deletions src/traces/mesh3d/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ 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;
}
readComponents(['i', 'j', 'k']);

var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, ['x', 'y', 'z'], layout);
Expand Down
Loading