#!/usr/bin/env python # This software and supporting documentation are distributed by # Institut Federatif de Recherche 49 # CEA/NeuroSpin, Batiment 145, # 91191 Gif-sur-Yvette cedex # France # # This software is governed by the CeCILL license version 2 under # French law and abiding by the rules of distribution of free software. # You can use, modify and/or redistribute the software under the # terms of the CeCILL license version 2 as circulated by CEA, CNRS # and INRIA at the following URL "http://www.cecill.info". # # As a counterpart to the access to the source code and rights to copy, # modify and redistribute granted by the license, users are provided only # with a limited warranty and the software's author, the holder of the # economic rights, and the successive licensors have only limited # liability. # # In this respect, the user's attention is drawn to the risks associated # with loading, using, modifying and/or developing or reproducing the # software by the user in light of its specific status of free software, # that may mean that it is complicated to manipulate, and that also # therefore means that it is reserved for developers and experienced # professionals having in-depth computer knowledge. Users are therefore # encouraged to load and test the software's suitability as regards their # requirements in conditions enabling the security of their systems and/or # data to be ensured and, more generally, to use and operate it in the # same conditions as regards security. # # The fact that you are presently reading this means that you have had # knowledge of the CeCILL license version 2 and that you accept its terms. import sys, numpy, qt from optparse import OptionParser from soma import aims import anatomist.direct.api as anatomist from connectomist.api import BundleListener, StartEndBundleListener, \ BundleReader # compute only one sphere unit_sphere = aims.SurfaceGenerator.sphere([0, 0, 0], 1, 16) class NameListener(BundleListener): '''Get all names of bundles''' def __init__(self): BundleListener.__init__(self) self._names = [] def bundleStarted(self, producer, info): self._names.append(info.name()) def names(self): return self._names def getExtremities(filename): '''Get All fibers extremities couples''' br = BundleReader(filename) s = StartEndBundleListener(0, 10000) n = NameListener() br.addBundleListener(s) br.addBundleListener(n) br.read() ext = [] for bundle in n.names(): size = s.getBundleSize(bundle) for i in range(size): p1, p2 = s.getStartEnd(bundle, i) # copy because getStartEnd returns references p1 = aims.Point3df(p1) p2 = aims.Point3df(p2) ext.append((p1, p2)) return ext def displayExtremity(e): '''Create one Mesh for one extremitiy e''' mesh = aims.AimsTimeSurface_3(unit_sphere) motion = aims.Motion() motion.setTranslation(e) aims.SurfaceManip.meshTransform(mesh, motion) return mesh def displayAllExtremities(extremities): '''Generate one big Mesh for all extremities''' mesh = aims.AimsTimeSurface_3() for e in extremities: mesh += displayExtremity(e[0]) mesh += displayExtremity(e[1]) return mesh def parseOpts(argv): description = 'Generate sphere representing fibers extremities of '\ 'the given bundle.' parser = OptionParser(description) parser.add_option('-b', '--bundle', dest='bundlename', metavar = 'FILE', action='store', default = None, help='Bundle filename') parser.add_option('-m', '--mesh', dest='meshname', metavar = 'FILE', action='store', default = None, help='Output meshname (default : don\'t store resulting mesh)') parser.add_option('-d', '--display', dest='display', action='store_true', default = False, help='Display resulting mesh under anatomist') return parser, parser.parse_args(argv) def main(): parser, (options, args) = parseOpts(sys.argv) if None in [options.bundlename]: parser.print_help() sys.exit(1) ext = getExtremities(options.bundlename) mesh = displayAllExtremities(ext) if options.meshname: writer = aims.Writer() writer.write(mesh, options.meshname) if options.display: a = anatomist.Anatomist() amesh = a.toAObject(mesh) win = a.createWindow('3D') win.setHasCursor(0) a.addObjects([amesh], [win]) qt.qApp.exec_loop() if __name__ == "__main__" : main()