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
|
/*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*/
#ifdef Q_OS_WIN32
#define NOMINMAX
#endif
#include <cmath>
#include <limits>
#include <iostream>
#include <set>
#include <sstream>
#include <QDir>
#include "BorderFile.h"
#include "BrainModelSurface.h"
#include "BrainModelSurfaceDeformationMeasurement.h"
#include "BrainModelSurfaceGeodesic.h"
#include "BrainModelSurfacePointLocator.h"
#include "BrainSet.h"
#include "CoordinateFile.h"
#include "DebugControl.h"
#include "GeodesicDistanceFile.h"
#include "MathUtilities.h"
#include "MetricFile.h"
#include "TopologyFile.h"
#include "TopologyHelper.h"
/**
* Constructor.
*/
BrainModelSurfaceDeformationMeasurement::BrainModelSurfaceDeformationMeasurement(BrainSet* bs,
BrainModelSurface* bmsIn,
BorderFile* borderFileIn,
MetricFile* metricFileIn,
const int sphereNumberIn)
: BrainModelAlgorithm(bs)
{
bms = bmsIn;
borderFile = borderFileIn;
userMetricFile = metricFileIn;
sphereNumber = sphereNumberIn;
}
/**
* Destructor.
*/
BrainModelSurfaceDeformationMeasurement::~BrainModelSurfaceDeformationMeasurement()
{
}
/**
* execute the algorithm.
*/
void
BrainModelSurfaceDeformationMeasurement::execute() throw (BrainModelAlgorithmException)
{
if (bms == NULL) {
throw BrainModelAlgorithmException("The surface is invalid.");
}
if (bms->getNumberOfNodes() <= 0) {
throw BrainModelAlgorithmException("The surface has no nodes.");
}
if (bms->getTopologyFile() == NULL) {
throw BrainModelAlgorithmException("The surface has no topology.");
}
if (borderFile == NULL) {
throw BrainModelAlgorithmException("The border file is invalid.");
}
if (borderFile->getNumberOfBorders() == 0) {
throw BrainModelAlgorithmException("The border file contains no borders.");
}
if (userMetricFile == NULL) {
throw BrainModelAlgorithmException("The metric file is invalid.");
}
//
// Set up user's metric file
//
const int numNodes = bms->getNumberOfNodes();
int metricColumn = 0;
if (userMetricFile->getNumberOfNodes() <= 0) {
userMetricFile->setNumberOfNodesAndColumns(numNodes, 1);
}
else {
userMetricFile->addColumns(1);
metricColumn = userMetricFile->getNumberOfColumns() - 1;
}
userMetricFile->setColumnName(metricColumn, "Nearest Spherical Border Distance");
//
// Create a topology helper so that only nodes with neighbors are used
//
const TopologyHelper* th = bms->getTopologyFile()->getTopologyHelper(false, true, false);
CoordinateFile* cf = bms->getCoordinateFile();
const float radius = bms->getSphericalSurfaceRadius();
//
// Loop through all of the nodes
//
for (int i = 0; i < numNodes; i++) {
userMetricFile->setValue(i, metricColumn, 0.0);
//
// Only do nodes with neighbors
//
if (th->getNodeHasNeighbors(i)) {
//
// This node's XYZ
//
float nodeXYZ[3];
cf->getCoordinate(i, nodeXYZ);
MathUtilities::normalize(nodeXYZ);
//
// Find the nearest border point
//
float nearestGreatCircleDistance = std::numeric_limits<float>::max();
const int numBorders = borderFile->getNumberOfBorders();
for (int j = 0; j < numBorders; j++) {
const Border* b = borderFile->getBorder(j);
const int numLinks = b->getNumberOfLinks();
for (int k = 0; k < numLinks; k++) {
float linkXYZ[3];
b->getLinkXYZ(k, linkXYZ);
MathUtilities::normalize(linkXYZ);
const float dotProduct = MathUtilities::dotProduct(nodeXYZ, linkXYZ);
const float theta = std::acos(dotProduct);
const float greatCircleDistance = theta * radius;
nearestGreatCircleDistance = std::min(nearestGreatCircleDistance,
greatCircleDistance);
}
}
if (nearestGreatCircleDistance <= 0.0) {
nearestGreatCircleDistance = 0.0001;
}
userMetricFile->setValue(i, metricColumn, nearestGreatCircleDistance);
}
if ((i % 100) == 0) {
//std::cout << i << " of " << numNodes << std::endl;
}
}
//
// Write the user's metric file
//
try {
// brainSet->writeMetricFile(userMetricFile->getFileName());
}
catch (FileException& e) {
throw BrainModelAlgorithmException(e.whatQString());
}
}
/*
* This does geodesic distance.
void
BrainModelSurfaceDeformationMeasurement::execute() throw (BrainModelAlgorithmException)
{
if (bms == NULL) {
throw BrainModelAlgorithmException("The surface is invalid.");
}
if (bms->getNumberOfNodes() <= 0) {
throw BrainModelAlgorithmException("The surface has no nodes.");
}
if (bms->getTopologyFile() == NULL) {
throw BrainModelAlgorithmException("The surface has no topology.");
}
if (borderFile == NULL) {
throw BrainModelAlgorithmException("The border file is invalid.");
}
if (borderFile->getNumberOfBorders() == 0) {
throw BrainModelAlgorithmException("The border file contains no borders.");
}
if (userMetricFile == NULL) {
throw BrainModelAlgorithmException("The metric file is invalid.");
}
//
// Load the standard sphere
//
QString currentPath = QDir::currentPath();
std::ostringstream str;
str << brainSet->getCaretHomeDirectory()
<< "/"
<< "data_files/REGISTER.SPHERE"
<< "/"
<< "sphere.v5."
<< sphereNumber
<< ".spec";
BrainSet sphereBrainSet(str.str().c_str());
if (sphereBrainSet.getNumberOfBrainModels() <= 0) {
QString msg("Unable to read standard sphere spec file ");
msg.append(str.str().c_str());
throw BrainModelAlgorithmException(msg);
}
BrainModelSurface* sphereSurface = sphereBrainSet.getBrainModelSurface(0);
if (sphereSurface == NULL) {
QString msg("Unable to read standard sphere from");
msg.append(str.str().c_str());
throw BrainModelAlgorithmException(msg);
}
QDir::setCurrent(currentPath);
//
// Get the radius of the border file
//
float sphereRadius = 1.0;
const int numBorders = borderFile->getNumberOfBorders();
for (int i = 0; i < numBorders; i++) {
const Border* border = borderFile->getBorder(i);
const int numLinks = border->getNumberOfLinks();
for (int j = 0; j < numLinks; j++) {
const float* xyz = border->getLinkXYZ(j);
sphereRadius = std::sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1] + xyz[2]*xyz[2]);
break;
}
}
//
// Set the radius of the spherical surface
//
sphereSurface->convertToSphereWithRadius(sphereRadius);
//
// Find the nearest node for each borders point
//
std::set<int> nodesNearBordersSet;
BrainModelSurfacePointLocator sphereNodeLocator(sphereSurface,
true);
for (int i = 0; i < numBorders; i++) {
const Border* border = borderFile->getBorder(i);
const int numLinks = border->getNumberOfLinks();
for (int j = 0; j < numLinks; j++) {
const int node = sphereNodeLocator.getNearestPoint(border->getLinkXYZ(j));
if (node >= 0) {
nodesNearBordersSet.insert(node);
if (DebugControl::getDebugOn()) {
std::cout << border->getName().toAscii().constData()
<< " link "
<< j
<< " is closest to node "
<< node
<< std::endl;
}
}
else {
std::cout << "Unable to find node nearest to border "
<< border->getName().toAscii().constData()
<< "link "
<< j
<< std::endl;
}
}
}
//
// Put the nodes near borders in a array for faster access
//
const int numNodesNearBorders = static_cast<int>(nodesNearBordersSet.size());
if (numNodesNearBorders <= 0) {
throw BrainModelAlgorithmException("No nodes near borders were found.");
}
int* nodesNearBorders = new int[numNodesNearBorders];
int cnt = 0;
for (std::set<int>::iterator iter = nodesNearBordersSet.begin();
iter != nodesNearBordersSet.end(); iter++) {
nodesNearBorders[cnt] = *iter;
cnt++;
}
//
// Create a topology helper so that only nodes with neighbors are used
//
const TopologyHelper* th = sphereSurface->getTopologyFile()->getTopologyHelper(false, true, false);
const int sphereNumNodes = sphereSurface->getNumberOfNodes();
CoordinateFile* cf = sphereSurface->getCoordinateFile();
//
// Create a geodesic distance file with one column
//
GeodesicDistanceFile geoDistFile;
geoDistFile.setNumberOfNodesAndColumns(sphereNumNodes, 1);
//
// Set up metric file
//
MetricFile sphereMetricFile;
sphereMetricFile.setNumberOfNodesAndColumns(sphereNumNodes, 1);
const int sphereMetricColumn = 0;
//
// Loop through all of the nodes
//
for (int i = 0; i < sphereNumNodes; i++) {
sphereMetricFile.setValue(i, sphereMetricColumn, 0.0);
//
// Only do nodes with neighbors
//
if (th->getNodeHasNeighbors(i)) {
//
// Determine the geodesic distances for the node
//
BrainModelSurfaceGeodesic bmsg(&sphereBrainSet,
sphereSurface,
NULL,
-3,
"",
&geoDistFile,
0,
"node",
i,
NULL);
try {
bmsg.execute();
}
catch (BrainModelAlgorithmException& e) {
throw e;
}
//
// Find the closest node in the node's nearest to the borders
//
float minDistSQ = std::numeric_limits<float>::max();
const float* myXYZ = cf->getCoordinate(i);
for (int j = 0; j < numNodesNearBorders; j++) {
const int node = nodesNearBorders[j];
const float* nodeXYZ = cf->getCoordinate(node);
const float dx = myXYZ[0] - nodeXYZ[0];
const float dy = myXYZ[1] - nodeXYZ[1];
const float dz = myXYZ[2] - nodeXYZ[2];
const float distSQ = dx*dx + dy*dy + dz*dz;
minDistSQ = std::min(minDistSQ,
distSQ);
}
const float dist = std::max(std::sqrt(minDistSQ), 0.001);
sphereMetricFile.setValue(i, sphereMetricColumn, dist);
}
if ((i % 100) == 0) {
std::cout << i << " of " << sphereNumNodes << std::endl;
}
}
delete[] nodesNearBorders;
//
// Set up user's metric file
//
const int numNodes = bms->getNumberOfNodes();
int metricColumn = 0;
if (userMetricFile->getNumberOfNodes() <= 0) {
userMetricFile->setNumberOfNodesAndColumns(numNodes, 1);
}
else {
userMetricFile->addColumns(1);
metricColumn = userMetricFile->getNumberOfColumns() - 1;
}
userMetricFile->setColumnName(metricColumn, "Deform Distance");
//
// Set distances for the users sphere
//
const CoordinateFile* userCoordFile = bms->getCoordinateFile();
for (int i = 0; i < numNodes; i++) {
const int sphereNode = sphereNodeLocator.getNearestPoint(userCoordFile->getCoordinate(i));
userMetricFile->setValue(i, metricColumn,
sphereMetricFile.getValue(sphereNode, sphereMetricColumn));
}
//
// Write the user's metric file
//
try {
brainSet->writeMetricFile(userMetricFile->getFileName());
}
catch (FileException& e) {
throw BrainModelAlgorithmException(e.whatQString());
}
}
*/
|