def plot_proj_shell(ms, use_sym=True, use_sphere=True, same_color=False, rad=0.025, opacity=1.0, ofile=None, ores=(300, 300)): """ Plot each shell Parameters ---------- ms: list of numpy.ndarray bvecs for each bvalue use_sym: boolean Plot symmetrical vectors use_sphere: boolean rendering of the sphere same_color: boolean use same color for all shell rad: float radius of each point opacity: float opacity for the shells ofile: str output filename ores: tuple resolution of the output png Return ------ """ global vtkcolors if len(ms) > 10: vtkcolors = fury.colormap.distinguishable_colormap(nb_colors=len(ms)) scene = window.Scene() scene.SetBackground(1, 1, 1) if use_sphere: sphere = get_sphere('symmetric724') shape = (1, 1, 1, sphere.vertices.shape[0]) fid, fname = mkstemp(suffix='_odf_slicer.mmap') odfs = np.memmap(fname, dtype=np.float64, mode='w+', shape=shape) odfs[:] = 1 odfs[..., 0] = 1 affine = np.eye(4) sphere_actor = actor.odf_slicer(odfs, affine, sphere=sphere, colormap='winter', scale=1.0, opacity=opacity) scene.add(sphere_actor) for i, shell in enumerate(ms): if same_color: i = 0 pts_actor = actor.point(shell, vtkcolors[i], point_radius=rad) scene.add(pts_actor) if use_sym: pts_actor = actor.point(-shell, vtkcolors[i], point_radius=rad) scene.add(pts_actor) window.show(scene) if ofile: window.snapshot(scene, fname=ofile + '.png', size=ores)
def test_odf_slicer(interactive=False): sphere = get_sphere('symmetric362') shape = (11, 11, 11, sphere.vertices.shape[0]) fid, fname = mkstemp(suffix='_odf_slicer.mmap') print(fid) print(fname) odfs = np.memmap(fname, dtype=np.float64, mode='w+', shape=shape) odfs[:] = 1 affine = np.eye(4) scene = window.Scene() mask = np.ones(odfs.shape[:3]) mask[:4, :4, :4] = 0 odfs[..., 0] = 1 odf_actor = actor.odf_slicer(odfs, affine, mask=mask, sphere=sphere, scale=.25, colormap='blues') fa = 0. * np.zeros(odfs.shape[:3]) fa[:, 0, :] = 1. fa[:, -1, :] = 1. fa[0, :, :] = 1. fa[-1, :, :] = 1. fa[5, 5, 5] = 1 k = 5 I, J, _ = odfs.shape[:3] fa_actor = actor.slicer(fa, affine) fa_actor.display_extent(0, I, 0, J, k, k) scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() odf_actor.display_extent(0, I, 0, J, k, k) odf_actor.GetProperty().SetOpacity(1.0) if interactive: window.show(scene, reset_camera=False) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, find_objects=True) npt.assert_equal(report.objects, 11 * 11) scene.clear() scene.add(fa_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) mask[:] = 0 mask[5, 5, 5] = 1 fa[5, 5, 5] = 0 fa_actor = actor.slicer(fa, None) fa_actor.display(None, None, 5) odf_actor = actor.odf_slicer(odfs, None, mask=mask, sphere=sphere, scale=.25, colormap='blues', norm=False, global_cm=True) scene.clear() scene.add(fa_actor) scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) scene.clear() scene.add(odf_actor) scene.add(fa_actor) odfs[:, :, :] = 1 mask = np.ones(odfs.shape[:3]) odf_actor = actor.odf_slicer(odfs, None, mask=mask, sphere=sphere, scale=.25, colormap='blues', norm=False, global_cm=True) scene.clear() scene.add(odf_actor) scene.add(fa_actor) scene.add(actor.axes((11, 11, 11))) for i in range(11): odf_actor.display(i, None, None) fa_actor.display(i, None, None) if interactive: window.show(scene) for j in range(11): odf_actor.display(None, j, None) fa_actor.display(None, j, None) if interactive: window.show(scene) # with mask equal to zero everything should be black mask = np.zeros(odfs.shape[:3]) odf_actor = actor.odf_slicer(odfs, None, mask=mask, sphere=sphere, scale=.25, colormap='blues', norm=False, global_cm=True) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) npt.assert_raises(IOError, actor.odf_slicer, odfs, None, mask=None, sphere=sphere, scale=.25, colormap=None, norm=False, global_cm=True) # colormap=None and global_cm=False results in directionally encoded colors scene.clear() scene.add(odf_actor) scene.add(fa_actor) odfs[:, :, :] = 1 odf_actor = actor.odf_slicer(odfs, None, mask=None, sphere=sphere, scale=.25, colormap=None, norm=False, global_cm=False) report = window.analyze_scene(scene) npt.assert_equal(report.actors, 1) npt.assert_equal(report.actors_classnames[0], 'vtkLODActor') del odf_actor odfs._mmap.close() del odfs os.close(fid) os.remove(fname)
def test_sh(): from dipy.sims.voxel import multi_tensor, multi_tensor_odf, sticks_and_ball from dipy.data import get_sphere, get_fnames from dipy.core.gradients import gradient_table from dipy.io.gradients import read_bvals_bvecs _, fbvals, fbvecs = get_fnames('small_64D') bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) gtab = gradient_table(bvals, bvecs) d = 0.0015 S, sticks = sticks_and_ball(gtab, d=d, S0=1, angles=[(0, 0), (30, 30)], fractions=[60, 40], snr=None) print(S) print(sticks) mevals = np.array([[0.0015, 0.0003, 0.0003], [0.0015, 0.0003, 0.0003]]) angles = [(0, 0), (60, 0)] fractions = [50, 50] sphere = get_sphere('repulsion724') sphere = sphere.subdivide(2) odf = multi_tensor_odf(sphere.vertices, mevals, angles, fractions) print(odf) ren = window.Scene() odf_actor = actor.odf_slicer(odf[None, None, None, :], sphere=sphere, colormap='plasma') # odf_actor.RotateX(90) ren.add(odf_actor) # window.show(ren) odf_test_dec= \ """ // Constants, see here: http://en.wikipedia.org/wiki/Table_of_spherical_harmonics #define k01 0.2820947918 // sqrt( 1/PI)/2 #define k02 0.4886025119 // sqrt( 3/PI)/2 #define k03 1.0925484306 // sqrt( 15/PI)/2 #define k04 0.3153915652 // sqrt( 5/PI)/4 #define k05 0.5462742153 // sqrt( 15/PI)/4 #define k06 0.5900435860 // sqrt( 70/PI)/8 #define k07 2.8906114210 // sqrt(105/PI)/2 #define k08 0.4570214810 // sqrt( 42/PI)/8 #define k09 0.3731763300 // sqrt( 7/PI)/4 #define k10 1.4453057110 // sqrt(105/PI)/4 // unrolled version of the above float SH_0_0( in vec3 s ) { vec3 n = s.zxy; return k01; } float SH_1_0( in vec3 s ) { vec3 n = s.zxy; return -k02*n.y; } float SH_1_1( in vec3 s ) { vec3 n = s.zxy; return k02*n.z; } float SH_1_2( in vec3 s ) { vec3 n = s.zxy; return -k02*n.x; } float SH_2_0( in vec3 s ) { vec3 n = s.zxy; return k03*n.x*n.y; } float SH_2_1( in vec3 s ) { vec3 n = s.zxy; return -k03*n.y*n.z; } float SH_2_2( in vec3 s ) { vec3 n = s.zxy; return k04*(3.0*n.z*n.z-1.0); } float SH_2_3( in vec3 s ) { vec3 n = s.zxy; return -k03*n.x*n.z; } float SH_2_4( in vec3 s ) { vec3 n = s.zxy; return k05*(n.x*n.x-n.y*n.y); } float SH_3_0( in vec3 s ) { vec3 n = s.zxy; return -k06*n.y*(3.0*n.x*n.x-n.y*n.y); } float SH_3_1( in vec3 s ) { vec3 n = s.zxy; return k07*n.z*n.y*n.x; } float SH_3_2( in vec3 s ) { vec3 n = s.zxy; return -k08*n.y*(5.0*n.z*n.z-1.0); } float SH_3_3( in vec3 s ) { vec3 n = s.zxy; return k09*n.z*(5.0*n.z*n.z-3.0); } float SH_3_4( in vec3 s ) { vec3 n = s.zxy; return -k08*n.x*(5.0*n.z*n.z-1.0); } float SH_3_5( in vec3 s ) { vec3 n = s.zxy; return k10*n.z*(n.x*n.x-n.y*n.y); } float SH_3_6( in vec3 s ) { vec3 n = s.zxy; return -k06*n.x*(n.x*n.x-3.0*n.y*n.y); } vec3 map( in vec3 p ) { vec3 p00 = p - vec3( 0.00, 2.5,0.0); vec3 p01 = p - vec3(-1.25, 1.0,0.0); vec3 p02 = p - vec3( 0.00, 1.0,0.0); vec3 p03 = p - vec3( 1.25, 1.0,0.0); vec3 p04 = p - vec3(-2.50,-0.5,0.0); vec3 p05 = p - vec3(-1.25,-0.5,0.0); vec3 p06 = p - vec3( 0.00,-0.5,0.0); vec3 p07 = p - vec3( 1.25,-0.5,0.0); vec3 p08 = p - vec3( 2.50,-0.5,0.0); vec3 p09 = p - vec3(-3.75,-2.0,0.0); vec3 p10 = p - vec3(-2.50,-2.0,0.0); vec3 p11 = p - vec3(-1.25,-2.0,0.0); vec3 p12 = p - vec3( 0.00,-2.0,0.0); vec3 p13 = p - vec3( 1.25,-2.0,0.0); vec3 p14 = p - vec3( 2.50,-2.0,0.0); vec3 p15 = p - vec3( 3.75,-2.0,0.0); float r, d; vec3 n, s, res; #ifdef SHOW_SPHERES #define SHAPE (vec3(d-0.35, -1.0+2.0*clamp(0.5 + 16.0*r,0.0,1.0),d)) #else #define SHAPE (vec3(d-abs(r), sign(r),d)) #endif d=length(p00); n=p00/d; r = SH_0_0( n ); s = SHAPE; res = s; d=length(p01); n=p01/d; r = SH_1_0( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p02); n=p02/d; r = SH_1_1( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p03); n=p03/d; r = SH_1_2( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p04); n=p04/d; r = SH_2_0( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p05); n=p05/d; r = SH_2_1( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p06); n=p06/d; r = SH_2_2( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p07); n=p07/d; r = SH_2_3( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p08); n=p08/d; r = SH_2_4( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p09); n=p09/d; r = SH_3_0( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p10); n=p10/d; r = SH_3_1( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p11); n=p11/d; r = SH_3_2( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p12); n=p12/d; r = SH_3_3( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p13); n=p13/d; r = SH_3_4( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p14); n=p14/d; r = SH_3_5( n ); s = SHAPE; if( s.x<res.x ) res=s; d=length(p15); n=p15/d; r = SH_3_6( n ); s = SHAPE; if( s.x<res.x ) res=s; return vec3( res.x, 0.5+0.5*res.y, res.z ); } vec3 intersect( in vec3 ro, in vec3 rd ) { vec3 res = vec3(1e10,-1.0, 1.0); float maxd = 10.0; float h = 1.0; float t = 0.0; vec2 m = vec2(-1.0); for( int i=0; i<200; i++ ) { if( h<0.001||t>maxd ) break; vec3 res = map( ro+rd*t ); h = res.x; m = res.yz; t += h*0.3; } if( t<maxd && t<res.x ) res=vec3(t,m); return res; } vec3 calcNormal( in vec3 pos ) { vec3 eps = vec3(0.001,0.0,0.0); return normalize( vec3( map(pos+eps.xyy).x - map(pos-eps.xyy).x, map(pos+eps.yxy).x - map(pos-eps.yxy).x, map(pos+eps.yyx).x - map(pos-eps.yyx).x ) ); } """ odf_test_impl = \ """ // camera matrix vec3 ww = vec3(0.0,0.0,0.0); //MCDCMatrix (2); vec3 uu = vec3(0.0,0.0,0.0); //MCDCMatrix (0); vec3 vv = vec3(0.0,0.0,0.0); //MCDCMatrix (1); vec3 tot = vec3(0.0); vec2 p = (-vec2(0.5, 0.5) + (2.0*point,0)) / 0.5; vec3 ro = vec3(0.0,0.0,0.0); // create view ray vec3 rd = normalize( p.x*uu + p.y*vv + 2.0*ww ); // background vec3 col = vec3(0.3) * clamp(1.0-length(point)*0.5,0.0,1.0); // raymarch vec3 tmat = intersect(ro,rd); if( tmat.y>-0.5 ) { // geometry vec3 pos = ro + tmat.x*rd; vec3 nor = calcNormal(pos); vec3 ref = reflect( rd, nor ); // material vec3 mate = 0.5*mix( vec3(1.0,0.6,0.15), vec3(0.2,0.4,0.5), tmat.y ); float occ = clamp( 2.0*tmat.z, 0.0, 1.0 ); float sss = pow( clamp( 1.0 + dot(nor,rd), 0.0, 1.0 ), 1.0 ); // lights vec3 lin = 2.5*occ*vec3(1.0,1.00,1.00)*(0.6+0.4*nor.y); lin += 1.0*sss*vec3(1.0,0.95,0.70)*occ; // surface-light interacion col = mate.xyz * lin; } // gamma col = pow( clamp(col,0.0,1.0), vec3(0.4545) ); tot += col; fragOutput0 = vec4( tot, 1.0 ); """ scene = window.Scene() scene.background((0.8, 0.8, 0.8)) centers = np.array([[2, 0, 0], [0, 0, 0], [-2, 0, 0]]) # np.random.rand(3, 3) * 3 # colors = np.array([[255, 0, 0], [0, 255, 0], [0, 0, 255]]) colors = np.random.rand(3, 3) * 255 scale = 1 # np.random.rand(3) * 5 # https://www.shadertoy.com/view/MstXWS # https://www.shadertoy.com/view/XsX3R4 fs_dec = \ """ uniform mat4 MCDCMatrix; uniform mat4 MCVCMatrix; float sdRoundBox( vec3 p, vec3 b, float r ) { vec3 q = abs(p) - b; return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r; } float sdEllipsoid( vec3 p, vec3 r ) { float k0 = length(p/r); float k1 = length(p/(r*r)); return k0*(k0-1.0)/k1; } float sdCylinder(vec3 p, float h, float r) { vec2 d = abs(vec2(length(p.xz),p.y)) - vec2(h,r); return min(max(d.x,d.y),0.0) + length(max(d,0.0)); } float sdSphere(vec3 pos, float r) { float d = length(pos) - r; return d; } float map( in vec3 pos) { float d = sdSphere(pos-0.5, .2); float d1 = sdCylinder(pos+0.5, 0.05, .5); float d2 = sdEllipsoid(pos + vec3(-0.5,0.5,0), vec3(0.2,0.3,0.5)); float d3 = sdRoundBox(pos + vec3(0.5,-0.5,0), vec3(0.2,0.1,0.3), .05); //.xy return min(min(min(d, d1), d2), d3); } vec3 calcNormal( in vec3 pos ) { vec2 e = vec2(0.0001,0.0); return normalize( vec3(map(pos + e.xyy) - map(pos - e.xyy ), map(pos + e.yxy) - map(pos - e.yxy), map(pos + e.yyx) - map(pos - e.yyx) ) ); } float castRay(in vec3 ro, vec3 rd) { float t = 0.0; for(int i=0; i < 100; i++) { vec3 pos = ro + t * rd; vec3 nor = calcNormal(pos); float h = map(pos); if (h < 0.001) break; t += h; if (t > 20.0) break; } return t; } """ fake_sphere = \ """ vec3 uu = vec3(MCVCMatrix[0][0], MCVCMatrix[1][0], MCVCMatrix[2][0]); // camera right vec3 vv = vec3(MCVCMatrix[0][1], MCVCMatrix[1][1], MCVCMatrix[2][1]); // camera up vec3 ww = vec3(MCVCMatrix[0][2], MCVCMatrix[1][2], MCVCMatrix[2][2]); // camera direction vec3 ro = MCVCMatrix[3].xyz * mat3(MCVCMatrix); // camera position // create view ray vec3 rd = normalize( point.x*-uu + point.y*-vv + 7*ww); vec3 col = vec3(0.0); float t = castRay(ro, rd); if (t < 20.0) { vec3 pos = ro + t * rd; vec3 nor = calcNormal(pos); vec3 sun_dir = vec3(MCVCMatrix[0][2], MCVCMatrix[1][2], MCVCMatrix[2][2]); //normalize() float dif = clamp( dot(nor, sun_dir), 0.0, 1.0); //vec3 sun_dif = normalize() col = color * dot(color,nor); // (color + diffuseColor + ambientColor + specularColor)*nor.zzz;//vec3(1.0); fragOutput0 = vec4(col, 1.0); } else{ //fragOutput0 = vec4(0,1,0, 1.0); discard; } /*float radius = 1.; if(len > radius) {discard;} //err, lightColor0 vertexColorVSOutput normalVCVSOutput, ambientIntensity; diffuseIntensity;specularIntensity;specularColorUniform; float c = len; fragOutput0 = vec4(c,c,c, 1); vec3 normalizedPoint = normalize(vec3(point.xy, sqrt(1. - len))); vec3 direction = normalize(vec3(1., 1., 1.)); float df = max(0, dot(direction, normalizedPoint)); float sf = pow(df, 24); fragOutput0 = vec4(max(df * color, sf * vec3(1)), 1);*/ """ billboard_actor = actor.billboard(centers, colors=colors.astype(np.uint8), scale=scale, fs_dec=fs_dec, fs_impl=fake_sphere) scene.add(billboard_actor) scene.add(actor.axes()) scene.camera_info() matrix = scene.camera().GetViewTransformMatrix() mat = np.zeros((4, 4)) for i in range(4): for j in range(4): mat[i, j] = matrix.GetElement(i, j) print(mat) print(np.dot(-mat[:3, 3], mat[:3, :3])) # camera position window.show(scene)
############################################################################### # Now, we create a slicer for each orientation to display a slice in # the middle of the volume and we add them to a `scene`. # Change these values to test various parameters combinations. scale = 0.5 norm = False colormap = None radial_scale = True opacity = 1.0 global_cm = False # ODF slicer for axial slice odf_actor_z = actor.odf_slicer(sh, affine=affine, sphere=sphere_low, scale=scale, norm=norm, radial_scale=radial_scale, opacity=opacity, colormap=colormap, global_cm=global_cm, B_matrix=B_low) # ODF slicer for coronal slice odf_actor_y = actor.odf_slicer(sh, affine=affine, sphere=sphere_low, scale=scale, norm=norm, radial_scale=radial_scale, opacity=opacity, colormap=colormap, global_cm=global_cm, B_matrix=B_low) odf_actor_y.display_extent(0, grid_shape[0] - 1, grid_shape[1]//2, grid_shape[1]//2, 0, grid_shape[2] - 1) # ODF slicer for sagittal slice odf_actor_x = actor.odf_slicer(sh, affine=affine, sphere=sphere_low, scale=scale, norm=norm,
def test_odf_slicer(interactive=False): # Prepare our data sphere = get_sphere('repulsion100') shape = (11, 11, 11, sphere.vertices.shape[0]) odfs = np.ones(shape) affine = np.array([[2.0, 0.0, 0.0, 3.0], [0.0, 2.0, 0.0, 3.0], [0.0, 0.0, 2.0, 1.0], [0.0, 0.0, 0.0, 1.0]]) mask = np.ones(odfs.shape[:3], bool) mask[:4, :4, :4] = False # Test that affine and mask work odf_actor = actor.odf_slicer(odfs, sphere=sphere, affine=affine, mask=mask, scale=.25, colormap='blues') k = 2 I, J, _ = odfs.shape[:3] odf_actor.display_extent(0, I - 1, 0, J - 1, k, k) scene = window.Scene() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene, reset_camera=False) arr = window.snapshot(scene) report = window.analyze_snapshot(arr, find_objects=True) npt.assert_equal(report.objects, 11 * 11 - 16) # Test that global colormap works odf_actor = actor.odf_slicer(odfs, sphere=sphere, mask=mask, scale=.25, colormap='blues', norm=False, global_cm=True) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test that the most basic odf_slicer instanciation works odf_actor = actor.odf_slicer(odfs) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test that odf_slicer.display works properly scene.clear() scene.add(odf_actor) scene.add(actor.axes((11, 11, 11))) for i in range(11): odf_actor.display(i, None, None) if interactive: window.show(scene) for j in range(11): odf_actor.display(None, j, None) if interactive: window.show(scene) # With mask equal to zero everything should be black mask = np.zeros(odfs.shape[:3]) odf_actor = actor.odf_slicer(odfs, sphere=sphere, mask=mask, scale=.25, colormap='blues', norm=False, global_cm=True) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # global_cm=True with colormap=None should raise an error npt.assert_raises(IOError, actor.odf_slicer, odfs, sphere=sphere, mask=None, scale=.25, colormap=None, norm=False, global_cm=True) # Dimension mismatch between sphere vertices and number # of SF coefficients will raise an error. npt.assert_raises(ValueError, actor.odf_slicer, odfs, mask=None, sphere=get_sphere('repulsion200'), scale=.25) # colormap=None and global_cm=False results in directionally encoded colors odf_actor = actor.odf_slicer(odfs, sphere=sphere, mask=None, scale=.25, colormap=None, norm=False, global_cm=False) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test that SH coefficients input works B = sh_to_sf_matrix(sphere, return_inv=False) odfs = np.zeros((11, 11, 11, B.shape[0])) odfs[..., 0] = 1.0 odf_actor = actor.odf_slicer(odfs, sphere=sphere, B_matrix=B) scene.clear() scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Dimension mismatch between sphere vertices and dimension of # B matrix will raise an error. npt.assert_raises(ValueError, actor.odf_slicer, odfs, mask=None, sphere=get_sphere('repulsion200')) # Test that constant colormap color works. Also test that sphere # normals are oriented correctly. Will show purple spheres with # a white contour. odf_contour = actor.odf_slicer(odfs, sphere=sphere, B_matrix=B, colormap=(255, 255, 255)) odf_contour.GetProperty().SetAmbient(1.0) odf_contour.GetProperty().SetFrontfaceCulling(True) odf_actor = actor.odf_slicer(odfs, sphere=sphere, B_matrix=B, colormap=(255, 0, 255), scale=0.4) scene.clear() scene.add(odf_contour) scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() if interactive: window.show(scene) # Test that we can change the sphere on an active actor new_sphere = get_sphere('symmetric362') new_B = sh_to_sf_matrix(new_sphere, return_inv=False) odf_actor.update_sphere(new_sphere.vertices, new_sphere.faces, new_B) if interactive: window.show(scene) del odf_actor del odfs
def test_manifest_standard(interactive=False): scene = window.Scene() # Setup scene # Setup surface surface_actor = _generate_surface() material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(surface_actor) arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1) scene.clear() # Reset scene # Contour from roi setup data = np.zeros((50, 50, 50)) data[20:30, 25, 25] = 1. data[25, 20:30, 25] = 1. affine = np.eye(4) surface = actor.contour_from_roi(data, affine, color=np.array([1, 0, 1])) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(surface) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1) scene.clear() # Reset scene # Contour from label setup data = np.zeros((50, 50, 50)) data[5:15, 1:10, 25] = 1. data[25:35, 1:10, 25] = 2. data[40:49, 1:10, 25] = 3. color = np.array([[255, 0, 0, 0.6], [0, 255, 0, 0.5], [0, 0, 255, 1.0]]) surface = actor.contour_from_label(data, color=color) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(surface) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 3) scene.clear() # Reset scene # Streamtube setup data1 = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2.]]) data2 = data1 + np.array([0.5, 0., 0.]) data = [data1, data2] colors = np.array([[1, 0, 0], [0, 0, 1.]]) tubes = actor.streamtube(data, colors, linewidth=.1) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(tubes) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 2) scene.clear() # Reset scene # ODF slicer setup if have_dipy: from dipy.data import get_sphere from tempfile import mkstemp sphere = get_sphere('symmetric362') shape = (11, 11, 11, sphere.vertices.shape[0]) fid, fname = mkstemp(suffix='_odf_slicer.mmap') odfs = np.memmap(fname, dtype=np.float64, mode='w+', shape=shape) odfs[:] = 1 affine = np.eye(4) mask = np.ones(odfs.shape[:3]) mask[:4, :4, :4] = 0 odfs[..., 0] = 1 odf_actor = actor.odf_slicer(odfs, affine, mask=mask, sphere=sphere, scale=.25, colormap='blues') material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) k = 5 I, J, _ = odfs.shape[:3] odf_actor.display_extent(0, I, 0, J, k, k) odf_actor.GetProperty().SetOpacity(1.0) scene.add(odf_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 11 * 11) scene.clear() # Reset scene # Tensor slicer setup if have_dipy: from dipy.data import get_sphere sphere = get_sphere('symmetric724') evals = np.array([1.4, .35, .35]) * 10 ** (-3) evecs = np.eye(3) mevals = np.zeros((3, 2, 4, 3)) mevecs = np.zeros((3, 2, 4, 3, 3)) mevals[..., :] = evals mevecs[..., :, :] = evecs affine = np.eye(4) scene = window.Scene() tensor_actor = actor.tensor_slicer(mevals, mevecs, affine=affine, sphere=sphere, scale=.3) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) _, J, K = mevals.shape[:3] tensor_actor.display_extent(0, 1, 0, J, 0, K) scene.add(tensor_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 4) scene.clear() # Reset scene # Point setup points = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) opacity = 0.5 points_actor = actor.point(points, colors, opacity=opacity) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(points_actor) arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 3) scene.clear() # Reset scene # Sphere setup xyzr = np.array([[0, 0, 0, 10], [100, 0, 0, 25], [200, 0, 0, 50]]) colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [0, 0, 1., 0.99]]) opacity = 0.5 sphere_actor = actor.sphere(centers=xyzr[:, :3], colors=colors[:], radii=xyzr[:, 3], opacity=opacity) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(sphere_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 3) scene.clear() # Reset scene # Advanced geometry actors setup (Arrow, cone, cylinder) xyz = np.array([[0, 0, 0], [50, 0, 0], [100, 0, 0]]) dirs = np.array([[0, 1, 0], [1, 0, 0], [0, 0.5, 0.5]]) colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [1, 1, 0, 1]]) heights = np.array([5, 7, 10]) actor_list = [[actor.cone, {'directions': dirs, 'resolution': 8}], [actor.arrow, {'directions': dirs, 'resolution': 9}], [actor.cylinder, {'directions': dirs}]] for act_func, extra_args in actor_list: aga_actor = act_func(centers=xyz, colors=colors[:], heights=heights, **extra_args) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(aga_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 3) scene.clear() # Basic geometry actors (Box, cube, frustum, octagonalprism, rectangle, # square) centers = np.array([[4, 0, 0], [0, 4, 0], [0, 0, 0]]) colors = np.array([[1, 0, 0, 0.4], [0, 1, 0, 0.8], [0, 0, 1, 0.5]]) directions = np.array([[1, 1, 0]]) scale_list = [1, 2, (1, 1, 1), [3, 2, 1], np.array([1, 2, 3]), np.array([[1, 2, 3], [1, 3, 2], [3, 1, 2]])] actor_list = [[actor.box, {}], [actor.cube, {}], [actor.frustum, {}], [actor.octagonalprism, {}], [actor.rectangle, {}], [actor.square, {}]] for act_func, extra_args in actor_list: for scale in scale_list: scene = window.Scene() bga_actor = act_func(centers=centers, directions=directions, colors=colors, scales=scale, **extra_args) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(bga_actor) arr = window.snapshot(scene) report = window.analyze_snapshot(arr) msg = 'Failed with {}, scale={}'.format(act_func.__name__, scale) npt.assert_equal(report.objects, 3, err_msg=msg) scene.clear() # Cone setup using vertices centers = np.array([[0, 0, 0], [20, 0, 0], [40, 0, 0]]) directions = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]]) colors = np.array([[1, 0, 0, 0.3], [0, 1, 0, 0.4], [0, 0, 1., 0.99]]) vertices = np.array([[0.0, 0.0, 0.0], [0.0, 10.0, 0.0], [10.0, 0.0, 0.0], [0.0, 0.0, 10.0]]) faces = np.array([[0, 1, 3], [0, 1, 2]]) cone_actor = actor.cone(centers=centers, directions=directions, colors=colors[:], vertices=vertices, faces=faces) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(cone_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 3) scene.clear() # Reset scene # Superquadric setup centers = np.array([[8, 0, 0], [0, 8, 0], [0, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) directions = np.random.rand(3, 3) scales = [1, 2, 3] roundness = np.array([[1, 1], [1, 2], [2, 1]]) sq_actor = actor.superquadric(centers, roundness=roundness, directions=directions, colors=colors.astype(np.uint8), scales=scales) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(sq_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 3) scene.clear() # Reset scene # Label setup text_actor = actor.label("Hello") material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(text_actor) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 5) scene.clear() # Reset scene # Texture setup arr = (255 * np.ones((512, 212, 4))).astype('uint8') arr[20:40, 20:40, :] = np.array([255, 0, 0, 255], dtype='uint8') tp2 = actor.texture(arr) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(tp2) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1) scene.clear() # Reset scene # Texture on sphere setup arr = 255 * np.ones((810, 1620, 3), dtype='uint8') rows, cols, _ = arr.shape rs = rows // 2 cs = cols // 2 w = 150 // 2 arr[rs - w: rs + w, cs - 10 * w: cs + 10 * w] = np.array([255, 127, 0]) tsa = actor.texture_on_sphere(arr) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(tsa) scene.reset_camera() scene.reset_clipping_range() arr = window.snapshot(scene) report = window.analyze_snapshot(arr) npt.assert_equal(report.objects, 1) # NOTE: From this point on, these actors don't have full support for PBR # interpolation. This is, the test passes but there is no evidence of the # desired effect. """ # Setup slicer data = (255 * np.random.rand(50, 50, 50)) affine = np.eye(4) slicer = actor.slicer(data, affine, value_range=[data.min(), data.max()]) slicer.display(None, None, 25) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(slicer) """ """ # Line setup data1 = np.array([[0, 0, 0], [1, 1, 1], [2, 2, 2.]]) data2 = data1 + np.array([0.5, 0., 0.]) data = [data1, data2] colors = np.array([[1, 0, 0], [0, 0, 1.]]) lines = actor.line(data, colors, linewidth=5) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(lines) """ """ # Scalar bar setup lut = actor.colormap_lookup_table( scale_range=(0., 100.), hue_range=(0., 0.1), saturation_range=(1, 1), value_range=(1., 1)) sb_actor = actor.scalar_bar(lut, ' ') material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(sb_actor) """ """ # Axes setup axes = actor.axes() material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(axes) """ """ # Peak slicer setup _peak_dirs = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype='f4') # peak_dirs.shape = (1, 1, 1) + peak_dirs.shape peak_dirs = np.zeros((11, 11, 11, 3, 3)) peak_dirs[:, :, :] = _peak_dirs peak_actor = actor.peak_slicer(peak_dirs) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(peak_actor) """ """ # Dots setup points = np.array([[0, 0, 0], [0, 1, 0], [1, 0, 0]]) dots_actor = actor.dots(points, color=(0, 255, 0)) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(dots_actor) """ """ # Text3D setup msg = 'I \nlove\n FURY' txt_actor = actor.text_3d(msg) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(txt_actor) """ """ # Figure setup arr = (255 * np.ones((512, 212, 4))).astype('uint8') arr[20:40, 20:40, 3] = 0 tp = actor.figure(arr) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(tp) """ """ # SDF setup centers = np.array([[2, 0, 0], [0, 2, 0], [0, 0, 0]]) * 11 colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) directions = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]]) scales = [1, 2, 3] primitive = ['sphere', 'ellipsoid', 'torus'] sdf_actor = actor.sdf(centers, directions=directions, colors=colors, primitives=primitive, scales=scales) material.manifest_standard(surface_actor, ambient_level=.3, diffuse_level=.25) scene.add(sdf_actor) """ # NOTE: For these last set of actors, there is not support for PBR # interpolation at all. """ # Billboard setup centers = np.array([[0, 0, 0], [5, -5, 5], [-7, 7, -7], [10, 10, 10], [10.5, 11.5, 11.5], [12, -12, -12], [-17, 17, 17], [-22, -22, 22]]) colors = np.array([[1, 1, 0], [0, 0, 0], [1, 0, 1], [0, 0, 1], [1, 1, 1], [1, 0, 0], [0, 1, 0], [0, 1, 1]]) scales = [6, .4, 1.2, 1, .2, .7, 3, 2] """ fake_sphere = \ """ float len = length(point); float radius = 1.; if (len > radius) discard; vec3 normalizedPoint = normalize(vec3(point.xy, sqrt(1. - len))); vec3 direction = normalize(vec3(1., 1., 1.)); float df_1 = max(0, dot(direction, normalizedPoint)); float sf_1 = pow(df_1, 24); fragOutput0 = vec4(max(df_1 * color, sf_1 * vec3(1)), 1); """ """ billboard_actor = actor.billboard(centers, colors=colors, scales=scales, fs_impl=fake_sphere) material.manifest_pbr(billboard_actor) scene.add(billboard_actor) """ if interactive: window.show(scene)
def plot_an_odf_slice(odf_4d, full_sphere, background_data, tile_size, filename, centroid, axis, camera_distance, subtract_iso, mask_image): view_up = [(0., 0., 1.), (0., 0., 1.), (0., -1., 0.)] # Adjust the centroid so it's only a single slice slicenum = int(np.round(centroid)[axis]) centroid[axis] = 0 position = centroid.copy() position[axis] = position[axis] + camera_distance # Roll if viewing an axial slice roll = 3 if axis == 2 else 0 position[1] = position[1] - roll # Ensure the dimensions reflect that there is only one slice new_shape = list(odf_4d.shape) new_shape[axis] = 1 image_shape = new_shape[:3] if axis == 0: odf_slice = odf_4d[slicenum, :, :, :].reshape(new_shape) image_slice = background_data[slicenum, :, :].reshape(image_shape) elif axis == 1: odf_slice = odf_4d[:, slicenum, :, :].reshape(new_shape) image_slice = background_data[:, slicenum, :].reshape(image_shape) elif axis == 2: odf_slice = odf_4d[:, :, slicenum, :].reshape(new_shape) image_slice = background_data[:, :, slicenum].reshape(image_shape) # Tile to get the whole ODF odf_slice = np.tile(odf_slice, (1, 1, 1, 2)) if subtract_iso: odf_slice = odf_slice - odf_slice.min(3, keepdims=True) # Make graphics objects odf_actor = actor.odf_slicer(odf_slice, sphere=full_sphere, colormap=None, scale=0.6, mask=image_slice) image_actor = actor.slicer(image_slice, opacity=0.6, interpolation='nearest') image_size = (tile_size, tile_size) scene = window.Scene() scene.add(image_actor) scene.add(odf_actor) xfov_min, xfov_max = 0, new_shape[0] - 1 yfov_min, yfov_max = 0, new_shape[1] - 1 zfov_min, zfov_max = 0, new_shape[2] - 1 odf_actor.display_extent(xfov_min, xfov_max, yfov_min, yfov_max, zfov_min, zfov_max) image_actor.display_extent(xfov_min, xfov_max, yfov_min, yfov_max, zfov_min, zfov_max) scene.set_camera(focal_point=tuple(centroid), position=tuple(position), view_up=view_up[axis]) window.record(scene, out_path=filename, reset_camera=False, size=image_size) scene.clear()