#!/usr/bin/env perl # Plot the surface parcellation in 3D. # # Copyright Alan C. Evans # Professor of Neurology # McGill University # use strict; use warnings "all"; use File::Temp qw/ tempdir /; my ($mid_rsl_left, $mid_rsl_right, $atlas_labels_left, $atlas_labels_right, $output) = @ARGV; unless($output) { print "Usage: $0 \n"; print "Note that none of the inputs may be compressed!\n"; exit(1); } my $tmpdir = tempdir( CLEANUP => 1 ); my $tilesize = 240; my $debug = 0; my $quiet = 1; my @mont_args = (); my @DrawText = ( "-font", "Helvetica" ); my $xpos = 2*$tilesize; my $ypos = 15; # Title: Write number of self intersections on resampled mid surface. my @ret = `check_self_intersect $mid_rsl_left`; my $left_inter = 0; if( $ret[1] =~ m/self_intersecting/ ) { # little legacy typo $ret[1] =~ /Number of self_intersecting triangles = (\d+)/; $left_inter = $1; } else { $ret[1] =~ /Number of self-intersecting triangles = (\d+)/; $left_inter = $1; } @ret = `check_self_intersect $mid_rsl_right`; my $right_inter = 0; if( $ret[1] =~ m/self_intersecting/ ) { # little legacy typo $ret[1] =~ /Number of self_intersecting triangles = (\d+)/; $right_inter = $1; } else { $ret[1] =~ /Number of self-intersecting triangles = (\d+)/; $right_inter = $1; } my $title = sprintf( "Surface parcellation (Left=%d, Right=%d)", $left_inter, $right_inter ); push @DrawText, "-annotate 0x0+${xpos}+${ypos} \"$title\""; my $num_rows = 2; # ROWS 1 - 2: surface atlas on left and right mid-surfaces my $min_label_left = `sort -n -u $atlas_labels_left |head -1`; my $max_label_left = `sort -n -u $atlas_labels_left | tail -1`; my $min_label_right = `sort -n -u $atlas_labels_right | tail -1`; my $max_label_right = `sort -n -u $atlas_labels_right | head -1`; chomp( $min_label_left ); chomp( $max_label_left ); chomp( $min_label_right ); chomp( $max_label_right ); my $min_label = ( ( $min_label_left < $min_label_right ) ? $min_label_left : $min_label_right ) - 3; my $max_label = ( $max_label_left > $max_label_right ) ? $max_label_left : $max_label_right; my $mid_rsl_labels_left = "${tmpdir}/mid_rsl_labels_left.obj"; my $mid_rsl_labels_right = "${tmpdir}/mid_rsl_labels_right.obj"; `colour_object $mid_rsl_left $atlas_labels_left $mid_rsl_labels_left spectral $min_label $max_label`; `colour_object $mid_rsl_right $atlas_labels_right $mid_rsl_labels_right spectral $min_label $max_label`; foreach my $pos ('default', 'left', 'right') { print "Making left mid ${pos} surface\n" unless $quiet; make_hemi($mid_rsl_labels_left, "${tmpdir}/mid_left_$pos.rgb", $pos); push(@mont_args, "${tmpdir}/mid_left_$pos.rgb"); } foreach my $pos ('top', 'bottom') { print "Making left/right mid ${pos} surface\n" unless $quiet; make_surface( $mid_rsl_labels_left, $mid_rsl_labels_right, "${tmpdir}/mid_${pos}.rgb", $pos ); push(@mont_args, "${tmpdir}/mid_${pos}.rgb"); } foreach my $pos ('flipped', 'right', 'left') { print "Making right mid ${pos} surface\n" unless $quiet; make_hemi($mid_rsl_labels_right, "${tmpdir}/mid_right_$pos.rgb", $pos); push(@mont_args, "${tmpdir}/mid_right_$pos.rgb"); } foreach my $pos ('front', 'back') { print "Making left/right mid ${pos} surface\n" unless $quiet; make_surface( $mid_rsl_labels_left, $mid_rsl_labels_right, "${tmpdir}/mid_${pos}.rgb", $pos ); push(@mont_args, "${tmpdir}/mid_${pos}.rgb"); } # do the montage print "Making montage\n" unless $quiet; my $cmd = "montage -tile 5x${num_rows} -background white " . "-geometry ${tilesize}x${tilesize}+1+1 " . join(' ', @mont_args)." ${tmpdir}/mont.png"; print "$cmd\n" if $debug; `$cmd`; # Add the title print "Adding title\n" unless $quiet; $cmd = "convert -box white -stroke green -pointsize 16 @DrawText ${tmpdir}/mont.png ${output}"; print "$cmd\n" if $debug; `$cmd`; print "Done\n" unless $quiet; # end of function sub make_hemi { my ($surface, $temp_output, $pos) = @_; my $cmd = ""; my $viewdir = ""; if ($pos eq 'default') { $viewdir = "-view 0.77 -0.18 -0.6 0.55 0.6 0.55"; } else { if ($pos eq 'flipped') { $viewdir = "-view -0.77 -0.18 -0.6 -0.55 0.6 0.55"; } else { $viewdir = "-$pos"; } } $cmd = "ray_trace -shadows -output ${temp_output} ${surface} -bg white -crop ${viewdir}"; print "$cmd\n" if $debug; `$cmd`; } sub make_surface { my ($left_hemi, $right_hemi, $temp_output, $pos) = @_; my $cmd = ""; my $viewdir = ""; if ($pos eq 'default') { $viewdir = ""; } else { $viewdir = "-$pos"; } $cmd = "ray_trace -shadows -output ${temp_output} ${left_hemi} ${right_hemi} -bg white -crop ${viewdir}"; print "$cmd\n" if $debug; `$cmd`; }