Skip to content
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

Clipping sliceplanes #556

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions cortex/webgl/resources/js/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ var dataset = (function(module) {
this.uniforms = {
framemix: { type:'f', value:0},
dataAlpha: { type:'f', value:1.0},

slicexn: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
sliceyn: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
slicezn: { type:'v3', value:new THREE.Vector3( 0,0,0 )},

slicexc: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
sliceyc: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
slicezc: { type:'v3', value:new THREE.Vector3( 0,0,0 )},

doslicex: { type:'i', value:false},
doslicey: { type:'i', value:false},
doslicez: { type:'i', value:false},
}

if (!this.vertex) {
Expand Down
48 changes: 48 additions & 0 deletions cortex/webgl/resources/js/mriview.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ var mriview = (function(module) {
y: new sliceplane.Plane(this, 1),
z: new sliceplane.Plane(this, 2),
};
this._clipx = false;
this._clipy = false;
this._clipz = false;

this.ui = new jsplot.Menu();
this.ui.addEventListener("update", this.schedule.bind(this));
Expand Down Expand Up @@ -1164,6 +1167,18 @@ var mriview = (function(module) {
rotate_z: {action:[this.sliceplanes.z, 'setAngle', -89, 89]}
});

var sliceplane_clip = sliceplane_ui.addFolder("clip", true);
sliceplane_clip.add({
clip_x: {action:[this, "setClippingX"]},
flip_x: {action:[this.sliceplanes.x, "setFlip"]},
clip_y: {action:[this, "setClippingY"]},
flip_y: {action:[this.sliceplanes.y, "setFlip"]},
clip_z: {action:[this, "setClippingZ"]},
flip_z: {action:[this.sliceplanes.z, "setFlip"]},
})

//

if ($(this.object).find("#colormap_category").length > 0) {
$(this.object).find("#colormap").ddslick({ width:296, height:350,
onSelected: function() {
Expand Down Expand Up @@ -1294,6 +1309,39 @@ var mriview = (function(module) {
this.sliceplanes.z.setVisible(!this.sliceplanes.z._visible);
viewer.schedule();
};
module.Viewer.prototype.setClippingX = function(val) {
if (val === undefined)
return this._clipx;

this._clipx = val;

if (this.active !== undefined) {
this.active.uniforms.doslicex.value = this._clipx;
}
this.schedule();
}
module.Viewer.prototype.setClippingY = function(val) {
if (val === undefined)
return this._clipy;

this._clipy = val;

if (this.active !== undefined) {
this.active.uniforms.doslicey.value = this._clipy;
}
this.schedule();
}
module.Viewer.prototype.setClippingZ = function(val) {
if (val === undefined)
return this._clipz;

this._clipz = val;

if (this.active !== undefined) {
this.active.uniforms.doslicez.value = this._clipz;
}
this.schedule();
}

return module;
}(mriview || {}));
28 changes: 28 additions & 0 deletions cortex/webgl/resources/js/shaderlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ var Shaderlib = (function() {
"varying float vCurv;",
"varying float vMedial;",
"varying float vThickmix;",
"varying vec3 vWorldPosition;",
// "varying float vDrop;",

"varying vec3 vPos_x[2];",
Expand Down Expand Up @@ -444,6 +445,7 @@ var Shaderlib = (function() {

"gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );",

"vWorldPosition = pos;",
"}"
].join("\n");

Expand Down Expand Up @@ -477,6 +479,18 @@ var Shaderlib = (function() {
"uniform vec2 dshape[2];",
"uniform sampler2D data[4];",

"uniform vec3 slicexn;",
"uniform vec3 sliceyn;",
"uniform vec3 slicezn;",

"uniform vec3 slicexc;",
"uniform vec3 sliceyc;",
"uniform vec3 slicezc;",

"uniform bool doslicex;",
"uniform bool doslicey;",
"uniform bool doslicez;",

// "uniform float hatchAlpha;",
// "uniform vec3 hatchColor;",
// "uniform sampler2D hatch;",
Expand All @@ -489,6 +503,7 @@ var Shaderlib = (function() {
"varying float vCurv;",
"varying float vMedial;",
"varying float vThickmix;",
"varying vec3 vWorldPosition;",

utils.standard_frag_vars,
utils.rand,
Expand All @@ -501,6 +516,19 @@ var Shaderlib = (function() {
//Curvature Underlay
"float ctmp = clamp(vCurv / smoothness, -0.5, 0.5);", // use limits here too
"float curv = clamp(ctmp * contrast + brightness, 0.0, 1.0);",

"bool clipx = dot(vWorldPosition - slicexc, slicexn) > 0.0;",
"bool clipy = dot(vWorldPosition - sliceyc, sliceyn) > 0.0;",
"bool clipz = dot(vWorldPosition - slicezc, slicezn) > 0.0;",

"if (clipx && doslicex && !doslicey && !doslicez) discard;",
"if (clipy && !doslicex && doslicey && !doslicez) discard;",
"if (clipz && !doslicex && !doslicey && doslicez) discard;",
"if (clipx && clipy && doslicex && doslicey && !doslicez) discard;",
"if (clipx && clipz && doslicex && !doslicey && doslicez) discard;",
"if (clipy && clipz && !doslicex && doslicey && doslicez) discard;",
"if (clipx && clipy && clipz && doslicex && doslicey && doslicez) discard;",

"vec4 cColor = vec4(vec3(curv), 1.0);",

"vec3 coord_x, coord_y;",
Expand Down
37 changes: 36 additions & 1 deletion cortex/webgl/resources/js/sliceplane.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ var sliceplane = (function(module) {
this.mesh = new THREE.Mesh(this.geometry, this.shader);
this.mesh.doubleSided = true;

this.flip_clip = 1; // 1 or -1

this.object.add(this.mesh);
//this.scene.add(this.mesh);
this.scene.add(this.object);
Expand Down Expand Up @@ -127,6 +129,9 @@ var sliceplane = (function(module) {
imat.multiplyVector3(this.geometry.vertices[2].set(shape[0]-0.5,-0.5,slice));
imat.multiplyVector3(this.geometry.vertices[3].set(shape[0]-0.5,shape[1]-0.5,slice));
}
this.center = new THREE.Vector3().add(this.geometry.vertices[0]).add(this.geometry.vertices[1]).add(this.geometry.vertices[2]).add(this.geometry.vertices[3]).divideScalar(4);

this.updateClipping();

this.geometry.computeBoundingSphere();
var center = this.geometry.boundingSphere.center;
Expand Down Expand Up @@ -184,15 +189,45 @@ var sliceplane = (function(module) {

this.mesh.rotation.set(0,0,0);
this.mesh.rotateOnAxis(axis, angle / 180 * Math.PI);

this.updateClipping();
}
module.Plane.prototype.setVisible = function(val) {
if (val === undefined)
return this._visible;

this._visible = val;

if (this.mesh !== undefined)
if (this.mesh !== undefined) {
this.mesh.visible = this._visible;
this.updateClipping();
}

}
module.Plane.prototype.updateClipping = function() {
this.normal = this.geometry.faces[0].normal.clone().applyEuler(this.mesh.rotation).multiplyScalar(this.flip_clip);

if (this.dir == 0){
this.viewer.active.uniforms.slicexn.value.copy(this.normal);
this.viewer.active.uniforms.slicexc.value.copy(this.center);
} else if (this.dir == 1){
this.viewer.active.uniforms.sliceyn.value.copy(this.normal);
this.viewer.active.uniforms.sliceyc.value.copy(this.center);
} else if (this.dir == 2){
this.viewer.active.uniforms.slicezn.value.copy(this.normal);
this.viewer.active.uniforms.slicezc.value.copy(this.center);
}
}
module.Plane.prototype.setFlip = function(val) {
if (val === undefined) {
return this.flip_clip == -1;
}
if (val) {
this.flip_clip = -1;
} else {
this.flip_clip = 1;
}
this.updateClipping();
}

module.MIP = function(viewer) {
Expand Down