1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/*LICENSE_START*/
/*
* Copyright 1995-2002 Washington University School of Medicine
*
* http://brainmap.wustl.edu
*
* This file is part of CARET.
*
* CARET is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CARET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CARET; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*LICENSE_END*/
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <BrainModelSurfaceDeformDataFile.h>
#include "BrainModelSurface.h"
#include "BrainModelSurfaceDeformation.h"
#include "BrainModelSurfaceDeformationMapCreate.h"
#include "BrainModelSurfacePolyhedron.h"
#include "BrainModelSurfaceStandardSphere.h"
#include "BrainSet.h"
#include "CommandSpecFileChangeResolution.h"
#include "DeformationMapFile.h"
#include "FileFilters.h"
#include "FileUtilities.h"
#include "ProgramParameters.h"
#include "ScriptBuilderParameters.h"
#include "SpecFile.h"
#include "TopologyFile.h"
/**
* constructor.
*/
CommandSpecFileChangeResolution::CommandSpecFileChangeResolution()
: CommandBase("-spec-file-change-resolution",
"SPEC FILE CHANGE RESOLUTION")
{
}
/**
* destructor.
*/
CommandSpecFileChangeResolution::~CommandSpecFileChangeResolution()
{
}
/**
* get the script builder parameters.
*/
void
CommandSpecFileChangeResolution::getScriptBuilderParameters(ScriptBuilderParameters& paramsOut) const
{
paramsOut.clear();
paramsOut.addFile("Input Spec File Name",
FileFilters::getSpecFileFilter());
paramsOut.addDirectory("Output Directory Name");
paramsOut.addInt("Number of Nodes", 2562);
}
/**
* get full help information.
*/
QString
CommandSpecFileChangeResolution::getHelpInformation() const
{
BrainModelSurfacePolyhedron poly(NULL, 0);
QString helpInfo =
(indent3 + getShortDescription() + "\n"
+ indent6 + parameters->getProgramNameWithoutPath() + " " + getOperationSwitch() + " \n"
+ indent9 + "<input-spec-file-name>\n"
+ indent9 + "<output-directory-name>\n"
+ indent9 + "<number-of-nodes>\n"
+ indent9 + "\n"
+ indent9 + "Change (typically reduce) the resolution of the surface \n"
+ indent9 + "files in the spec file. This is accomplished by replacing\n"
+ indent9 + "spec file's spherical coordinate file with a spherical \n"
+ indent9 + "coordinate file generated by iteratively subdividing an \n"
+ indent9 + "icosahedron. If there is not a spherical surface, a \n"
+ indent9 + "spherical surface is created from the fiducial surface\n"
+ indent9 + "via inflation and smoothing.\n"
+ indent9 + "\n"
+ indent9 + "The output directory must not exist and will be created\n"
+ indent9 + "by this program.\n"
+ indent9 + "\n"
+ indent9 + "This command should be run from the directory containing\n"
+ indent9 + "the input spec file.\n"
+ indent9 + "\n"
+ indent9 + "Number of Nodes Number of Triangles\n"
+ indent9 + "--------------- -------------------\n");
std::vector<int> iterations, nodes, triangles;
BrainModelSurfacePolyhedron::getNumberOfNodesAndTrianglesFromIterations(
iterations, nodes, triangles);
int numIters = static_cast<int>(iterations.size());
for (int i = 0; i < numIters; i++) {
helpInfo += (indent9 +
QString("%1%2\n").arg(nodes[i], 15).arg(triangles[i], 22));
}
BrainModelSurfaceStandardSphere::getNumberOfNodesAndTriangles(nodes, triangles);
const int num = static_cast<int>(nodes.size());
for (int i = 0; i < num; i++) {
helpInfo += (indent9 +
QString("%1%2\n").arg(nodes[i], 15).arg(triangles[i], 22));
}
helpInfo += "\n";
return helpInfo;
}
/**
* execute the command.
*/
void
CommandSpecFileChangeResolution::executeCommand() throw (BrainModelAlgorithmException,
CommandException,
FileException,
ProgramParametersException,
StatisticException)
{
const QString originalPath = QDir::currentPath();
QString deformedPrefix = ""; //LowRes_";
//
// Get the inputs
//
const QString usersSpecFileName =
parameters->getNextParameterAsString("Spec File Name");
const QString outputDirectoryName =
parameters->getNextParameterAsString("Directory Name");
const int numberOfNodes =
parameters->getNextParameterAsInt("Number of Nodes");
checkForExcessiveParameters();
//
// Validate number of nodes
//
std::vector<int> iterations, nodes, triangles;
BrainModelSurfacePolyhedron::getNumberOfNodesAndTrianglesFromIterations(
iterations, nodes, triangles);
int numberOfIterations = -1;
for (int i = 0; i < static_cast<int>(nodes.size()); i++) {
if (numberOfNodes == nodes[i]) {
numberOfIterations = i;
break;
}
}
int stdSphereNumberOfNodes = -1;
if (numberOfIterations < 0) {
BrainModelSurfaceStandardSphere::getNumberOfNodesAndTriangles(
nodes, triangles);
for (int i = 0; i < static_cast<int>(nodes.size()); i++) {
if (numberOfNodes == nodes[i]) {
stdSphereNumberOfNodes = numberOfNodes;
break;
}
}
}
if ((numberOfIterations < 0) &&
(stdSphereNumberOfNodes < 0)) {
throw CommandException("Invalid number of nodes: "
+ QString::number(numberOfNodes));
}
//
// create the output directory
//
this->createOutputDirectory(outputDirectoryName);
//
// Create the spherical target
//
BrainSet brainSet;
if (numberOfIterations >= 0) {
BrainModelSurfacePolyhedron tetra(&brainSet, numberOfIterations);
tetra.execute();
}
else if (stdSphereNumberOfNodes > 0) {
BrainModelSurfaceStandardSphere sphere(&brainSet, stdSphereNumberOfNodes);
sphere.execute();
brainSet.getLoadedFilesSpecFile()->clear();
QDir::setCurrent(originalPath);
}
else {
throw CommandException("Invalid number of nodes: "
+ QString::number(numberOfNodes));
}
BrainModelSurface* targetSphericalSurface = brainSet.getBrainModelSurface(0);
if (targetSphericalSurface == NULL) {
throw CommandException("Failed to create resampled spherical surface.");
}
TopologyFile* tf = targetSphericalSurface->getTopologyFile();
if (tf == NULL) {
throw CommandException("Created spherical surface has no topology.");
}
//
// Prefix for deformation sphere files
//
const QString defTargetPrefix = "def_sphere";
const QString defTargetCoordName = defTargetPrefix
+ SpecFile::getCoordinateFileExtension();
const QString defTargetTopoName = defTargetPrefix
+ SpecFile::getTopoFileExtension();
const QString defTargetSpecName = deformedPrefix
+ FileUtilities::basename(usersSpecFileName);
const QString defTargetDefMapName = defTargetPrefix
+ SpecFile::getDeformationMapFileExtension();
//
// Read the user's brain set
//
BrainSet* usersBrainSet = this->readUsersSpecFile(usersSpecFileName);
BrainModelSurface* fiducialSurface =
usersBrainSet->getBrainModelSurfaceOfType(
BrainModelSurface::SURFACE_TYPE_FIDUCIAL);
QString fiducialSurfaceName;
if (fiducialSurface != NULL) {
fiducialSurfaceName = fiducialSurface->getFileName();
}
BrainModelSurface* usersSphericalSurface =
usersBrainSet->getBrainModelSurfaceOfType(
BrainModelSurface::SURFACE_TYPE_SPHERICAL);
if (usersSphericalSurface == NULL) {
usersSphericalSurface = createSphereFromUsersFiducial(usersBrainSet);
}
//
// Set the radius of the target sphere
//
targetSphericalSurface->convertToSphereWithRadius(
usersSphericalSurface->getSphericalSurfaceRadius());
//
// Update items in target spec
//
brainSet.setSpecies(usersBrainSet->getSpecies());
brainSet.setStructure(usersBrainSet->getStructure());
brainSet.setSubject(usersBrainSet->getSubject());
brainSet.setStereotaxicSpace(usersBrainSet->getStereotaxicSpace());
//
// Write out sphere for debugging
//
QDir::setCurrent(outputDirectoryName);
brainSet.setSpecFileName(defTargetSpecName, false);
CoordinateFile* cf = targetSphericalSurface->getCoordinateFile();
brainSet.writeCoordinateFile(defTargetCoordName,
targetSphericalSurface->getSurfaceType(),
cf,
true);
brainSet.writeTopologyFile(defTargetTopoName,
tf->getTopologyType(),
tf);
QDir::setCurrent(originalPath);
//
// Deformation Map File
//
DeformationMapFile defMapFile;
//
// Execute the deformation
//
BrainModelSurfaceDeformationMapCreate bmsdmc(&brainSet,
usersSphericalSurface,
targetSphericalSurface,
&defMapFile,
BrainModelSurfaceDeformationMapCreate::DEFORMATION_SURFACE_TYPE_SPHERE);
bmsdmc.execute();
//
// Write the deformation map file
//
if (QFileInfo(usersSpecFileName).isAbsolute()) {
defMapFile.setSourceSpecFileName(usersSpecFileName);
}
else {
defMapFile.setSourceSpecFileName(QDir::currentPath()
+ QDir::separator()
+ usersSpecFileName);
}
defMapFile.setSourceFiducialCoordFileName(fiducialSurfaceName);
defMapFile.setSourceDirectory(QDir::currentPath());
QDir::setCurrent(outputDirectoryName);
defMapFile.setTargetDirectory(QDir::currentPath());
if (QFileInfo(defTargetSpecName).isAbsolute()) {
defMapFile.setTargetSpecFileName(defTargetSpecName);
}
else {
defMapFile.setTargetSpecFileName(QDir::currentPath()
+ QDir::separator()
+ defTargetSpecName);
}
defMapFile.setOutputSpecFileName(defMapFile.getTargetSpecFileName());
defMapFile.setDeformedFileNamePrefix(deformedPrefix);
defMapFile.setDeformedColumnNamePrefix("");
defMapFile.writeFile(defTargetDefMapName);
QDir::setCurrent(originalPath);
//
// Create the deformed fiducial surface and load the fiducial surface
// This allows border and foci projections to deform
//
SpecFile userSpecFile;
userSpecFile.readFile(usersSpecFileName);
if (userSpecFile.fiducialCoordFile.getNumberOfFiles() > 0) {
userSpecFile.readFile(usersSpecFileName);
QString deformErrorMessage;
BrainModelSurfaceDeformDataFile::deformCoordinateFiles(&defMapFile,
userSpecFile.fiducialCoordFile,
deformErrorMessage);
if (deformErrorMessage.isEmpty()) {
QDir::setCurrent(outputDirectoryName);
SpecFile tgtSpec;
tgtSpec.readFile(defMapFile.getOutputSpecFileName());
if (tgtSpec.fiducialCoordFile.getNumberOfFiles() > 0) {
const QString fiducialCoordFileName =
tgtSpec.fiducialCoordFile.getFileName(0);
brainSet.readCoordinateFile(fiducialCoordFileName,
BrainModelSurface::SURFACE_TYPE_FIDUCIAL,
false,
true,
true);
defMapFile.setTargetFiducialCoordFileName(fiducialCoordFileName);
}
QDir::setCurrent(originalPath);
}
}
//
// Remove the std sphere from the output spec file
//
QDir::setCurrent(outputDirectoryName);
SpecFile outSpecFile;
outSpecFile.readFile(defMapFile.getOutputSpecFileName());
outSpecFile.sphericalCoordFile.clear(false);
outSpecFile.writeFile(defMapFile.getOutputSpecFileName());
QDir::setCurrent(originalPath);
//
// Deform the data files
//
QString errorMessage;
BrainModelSurfaceDeformation::deformDataFiles(
usersBrainSet,
&brainSet,
usersBrainSet->getSpecFileName(),
&defMapFile,
true,
false, // already did fiducial
true,
true,
true,
false,
errorMessage);
if (errorMessage.isEmpty() == false) {
throw CommandException(errorMessage);
}
}
/**
* create a spherical surface from a fiducial surface.
*/
BrainModelSurface*
CommandSpecFileChangeResolution::createSphereFromUsersFiducial(BrainSet* bs)
{
BrainModelSurface* bms = bs->getBrainModelSurfaceOfType(
BrainModelSurface::SURFACE_TYPE_FIDUCIAL);
if (bms == NULL) {
throw CommandException("Users spec file contains neither a fiducial "
"nor a spherical surface.");
}
//
// Create a spherical surface
//
bms->createInflatedAndEllipsoidFromFiducial(false,
false,
false,
true,
false,
false,
true,
0,
NULL);
bms = bs->getBrainModelSurfaceOfType(
BrainModelSurface::SURFACE_TYPE_SPHERICAL);
if (bms == NULL) {
throw CommandException("Failed to create spherical surface from users "
"fiducial surface.");
}
return bms;
}
/**
* create the output directory.
*/
void
CommandSpecFileChangeResolution::createOutputDirectory(const QString& directoryName)
{
if (QFile::exists(directoryName)) {
throw CommandException("Ouput directory \""
+ directoryName
+ "\" exists. The output directory must not "
+ "and will be created by this program.");
}
QDir currentDir;
if (currentDir.mkdir(directoryName) == false) {
throw CommandException("Unable to create directory \""
+ directoryName
+ "\".");
}
}
/**
* read the user's spec file.
*/
BrainSet*
CommandSpecFileChangeResolution::readUsersSpecFile(const QString& usersSpecFileName)
{
SpecFile sf;
sf.readFile(usersSpecFileName);
sf.setAllFileSelections(SpecFile::SPEC_TRUE);
BrainSet* bs = new BrainSet();
QString errorMessage;
bs->readSpecFile(sf, usersSpecFileName, errorMessage);
if (errorMessage.isEmpty() == false) {
throw CommandException(errorMessage);
}
return bs;
}
|