Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 16 additions & 1 deletion src/js/models/figure_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
clearFigureFromStorage,
figureConfirmDialog,
getJson,
saveFigureToStorage} from "../views/util";
saveFigureToStorage,
normalizeZProjectionBounds} from "../views/util";

// Version of the json file we're saving.
// This only needs to increment when we make breaking changes (not linked to release versions.)
Expand Down Expand Up @@ -105,6 +106,20 @@

_.each(data.panels, function(p){
p.selected = false;

// If one of z_start, z_end or z_projection is defined, ensure that they are within bounds
if (p.z_start !== undefined || p.z_end !== undefined || p.z_projection !== undefined) {
var normalized = normalizeZProjectionBounds(
p.z_start,
p.z_end,
p.z_projection,
p.sizeZ
);
p.z_projection = normalized.z_projection;
p.z_start = normalized.z_start;
p.z_end = normalized.z_end;
}
Comment thread
Tom-TBT marked this conversation as resolved.

self.panels.create(p);
});

Expand Down
16 changes: 15 additions & 1 deletion src/js/models/panel_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Backbone from "backbone";
import _ from "underscore";
import $ from "jquery";
import { rotatePoint, figureConfirmDialog } from "../views/util";
import { rotatePoint, figureConfirmDialog, normalizeZProjectionBounds } from "../views/util";

// Corresponds to css - allows us to calculate size of labels
var LINE_HEIGHT = 1.43;
Expand Down Expand Up @@ -302,10 +302,24 @@
if (this.get('theT') >= newData.sizeT) {
newData.theT = newData.sizeT - 1;
}

if (this.get('theZ') >= newData.sizeZ) {
newData.theZ = newData.sizeZ - 1;
}

// Ensure z-projection parameters are within bounds of the new image
if (this.get('z_start') !== undefined || this.get('z_end') !== undefined || this.get('z_projection') !== undefined) {
var normalized = normalizeZProjectionBounds(
this.get('z_start'),
this.get('z_end'),
this.get('z_projection'),
newData.sizeZ
);
newData.z_projection = normalized.z_projection;
newData.z_start = normalized.z_start;
newData.z_end = normalized.z_end;
}

// Make sure dx and dy are not outside the new image
if (Math.abs(this.get('dx')) > newData.orig_width/2) {
newData.dx = 0;
Expand Down
27 changes: 27 additions & 0 deletions src/js/views/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,30 @@ export function updateRoiIds(panelsJson) {

return updatedPanels;
}

// Normalize z-projection bounds to be within [0, sizeZ-1] and ensure start <= end
// Takes z_start, z_end, z_projection values and sizeZ, returns normalized bounds
// If sizeZ is 1, disables z_projection and resets to 0
export function normalizeZProjectionBounds(z_start, z_end, z_projection, sizeZ) {
var result = {
z_projection: (z_projection === undefined) ? false : z_projection,
z_start: (z_start === undefined) ? 0 : z_start,
z_end: (z_end === undefined) ? 0 : z_end
};

if (sizeZ === 1) {
result.z_projection = false;
result.z_start = 0;
result.z_end = 0;
} else {
// bounds checking and ensures start <= end
result.z_end = Math.max(Math.min(result.z_end, sizeZ - 1), 0);
result.z_start = Math.max(Math.min(result.z_start, sizeZ - 1), 0);
if (result.z_start > result.z_end) {
var tmp = result.z_start;
result.z_start = result.z_end;
result.z_end = tmp;
}
}
return result;
}