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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkTubeSpatialObject_hxx
#define itkTubeSpatialObject_hxx
#include "itkMath.h"
namespace itk
{
template <unsigned int TDimension, typename TTubePointType>
TubeSpatialObject<TDimension, TTubePointType>::TubeSpatialObject()
{
this->SetTypeName("TubeSpatialObject");
this->Clear();
this->Update();
}
template <unsigned int TDimension, typename TTubePointType>
void
TubeSpatialObject<TDimension, TTubePointType>::Clear()
{
Superclass::Clear();
this->GetProperty().SetRed(1);
this->GetProperty().SetGreen(0);
this->GetProperty().SetBlue(0);
this->GetProperty().SetAlpha(1);
m_Root = false;
m_ParentPoint = -1;
m_EndRounded = true; // default end-type is flat
this->Modified();
}
template <unsigned int TDimension, typename TTubePointType>
void
TubeSpatialObject<TDimension, TTubePointType>::CopyInformation(const DataObject * data)
{
// Standard call to the superclass' method
Superclass::CopyInformation(data);
// Attempt to cast data to an ImageBase
const TubeSpatialObject<TDimension> * soData;
soData = dynamic_cast<const TubeSpatialObject<TDimension> *>(data);
if (soData == nullptr)
{
// pointer could not be cast back down
itkExceptionMacro("itk::TubeSpatialObject::CopyInformation() cannot cast "
<< typeid(data).name() << " to " << typeid(TubeSpatialObject<TDimension> *).name());
}
// check if we are the same type
const auto * source = dynamic_cast<const Self *>(data);
if (!source)
{
std::cerr << "CopyInformation: objects are not of the same type" << std::endl;
return;
}
// copy the ivars
this->SetRoot(source->GetRoot());
this->SetEndRounded(source->GetEndRounded());
// Don't copy parent info
// this->SetParentPoint(source->GetParentPoint());
}
template <unsigned int TDimension, typename TTubePointType>
typename LightObject::Pointer
TubeSpatialObject<TDimension, TTubePointType>::InternalClone() const
{
typename LightObject::Pointer loPtr = Superclass::InternalClone();
typename Self::Pointer rval = dynamic_cast<Self *>(loPtr.GetPointer());
if (rval.IsNull())
{
itkExceptionMacro("downcast to type " << this->GetNameOfClass() << " failed.");
}
rval->SetEndRounded(this->GetEndRounded());
rval->SetParentPoint(this->GetParentPoint());
rval->SetRoot(this->GetRoot());
return loPtr;
}
template <unsigned int TDimension, typename TTubePointType>
void
TubeSpatialObject<TDimension, TTubePointType>::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "ParentPoint : " << m_ParentPoint << std::endl;
os << indent << "EndRounded: " << (m_EndRounded ? "On" : "Off") << std::endl;
os << indent << "Root: " << (m_Root ? "On" : "Off") << std::endl;
}
template <unsigned int TDimension, typename TTubePointType>
void
TubeSpatialObject<TDimension, TTubePointType>::ComputeMyBoundingBox()
{
itkDebugMacro("Computing tube bounding box");
auto it = this->m_Points.begin();
auto end = this->m_Points.end();
if (it == end)
{
typename BoundingBoxType::PointType pnt;
pnt.Fill(typename BoundingBoxType::PointType::ValueType{});
this->GetModifiableMyBoundingBoxInObjectSpace()->SetMinimum(pnt);
this->GetModifiableMyBoundingBoxInObjectSpace()->SetMaximum(pnt);
return;
}
PointType pt = it->GetPositionInObjectSpace();
double ptRadius = it->GetRadiusInObjectSpace();
// Compute a bounding box in object space
PointType tmpPt;
for (unsigned int d = 0; d < TDimension; ++d)
{
tmpPt[d] = pt[d] - ptRadius;
}
this->GetModifiableMyBoundingBoxInObjectSpace()->SetMinimum(tmpPt);
this->GetModifiableMyBoundingBoxInObjectSpace()->SetMaximum(tmpPt);
for (unsigned int d = 0; d < TDimension; ++d)
{
tmpPt[d] = pt[d] + ptRadius;
}
this->GetModifiableMyBoundingBoxInObjectSpace()->ConsiderPoint(tmpPt);
++it;
while (it != end)
{
pt = it->GetPositionInObjectSpace();
ptRadius = it->GetRadiusInObjectSpace();
for (unsigned int d = 0; d < TDimension; ++d)
{
tmpPt[d] = pt[d] - ptRadius;
}
this->GetModifiableMyBoundingBoxInObjectSpace()->ConsiderPoint(tmpPt);
for (unsigned int d = 0; d < TDimension; ++d)
{
tmpPt[d] = pt[d] + ptRadius;
}
this->GetModifiableMyBoundingBoxInObjectSpace()->ConsiderPoint(tmpPt);
++it;
}
this->GetModifiableMyBoundingBoxInObjectSpace()->ComputeBoundingBox();
}
template <unsigned int TDimension, typename TTubePointType>
bool
TubeSpatialObject<TDimension, TTubePointType>::IsInsideInObjectSpace(const PointType & point) const
{
if (this->GetMyBoundingBoxInObjectSpace()->IsInside(point))
{
double tempDist;
auto it = this->m_Points.begin();
auto first = it;
auto it2 = it;
++it2;
auto end = this->m_Points.end();
auto last = end;
--last;
PointType firstP = first->GetPositionInObjectSpace();
double firstR = first->GetRadiusInObjectSpace();
PointType lastP = last->GetPositionInObjectSpace();
double lastR = last->GetRadiusInObjectSpace();
bool withinEndCap = false;
while (it2 != end)
{
// Check if the point is on the normal plane
PointType a = it->GetPositionInObjectSpace();
PointType b = it2->GetPositionInObjectSpace();
if (!m_EndRounded)
{
withinEndCap = false;
double firstDist = a.EuclideanDistanceTo(firstP);
double lastDist = a.EuclideanDistanceTo(lastP);
if (firstDist <= firstR || lastDist <= firstR)
{
withinEndCap = true;
}
else
{
firstDist = b.EuclideanDistanceTo(firstP);
lastDist = b.EuclideanDistanceTo(lastP);
if (firstDist <= lastR || lastDist <= lastR)
{
withinEndCap = true;
}
}
}
double A = 0;
double B = 0;
for (unsigned int i = 0; i < TDimension; ++i)
{
A += (b[i] - a[i]) * (point[i] - a[i]);
B += (b[i] - a[i]) * (b[i] - a[i]);
}
if (B != 0)
{
double lambda = A / B;
B = std::sqrt(B);
double lambdaMin = 0;
double lambdaMax = 1;
double lambdaMinR = it->GetRadiusInObjectSpace();
double lambdaMaxR = it2->GetRadiusInObjectSpace();
if (m_EndRounded || !withinEndCap)
{
lambdaMin = -(lambdaMinR / B);
lambdaMax = 1 + (lambdaMaxR / B);
if (lambdaMax < (lambdaMinR / B))
{
lambdaMax = (lambdaMinR / B);
}
if (lambdaMin > (1 - (lambdaMaxR / B)))
{
lambdaMin = 1 - (lambdaMaxR / B);
}
}
if (lambda >= lambdaMin && lambda <= lambdaMax)
{
if (lambda < 0)
{
lambda = 0;
}
else if (lambda > 1)
{
lambda = 1;
}
double lambdaR = lambdaMinR + lambda * (lambdaMaxR - lambdaMinR);
PointType p;
for (unsigned int i = 0; i < TDimension; ++i)
{
p[i] = a[i] + lambda * (b[i] - a[i]);
}
tempDist = point.EuclideanDistanceTo(p);
if (tempDist <= lambdaR)
{
return true;
}
}
}
++it;
++it2;
}
}
return false;
}
template <unsigned int TDimension, typename TTubePointType>
unsigned int
TubeSpatialObject<TDimension, TTubePointType>::RemoveDuplicatePointsInObjectSpace(double minSpacingInObjectSpace)
{
int nPoints = 0;
auto it = this->m_Points.begin();
while (it != this->m_Points.end())
{
PointType pnt = it->GetPositionInObjectSpace();
++it;
if (it != this->m_Points.end())
{
PointType pnt2 = it->GetPositionInObjectSpace();
double dist = pnt.EuclideanDistanceTo(pnt2);
if (dist <= minSpacingInObjectSpace)
{
it = this->m_Points.erase(it);
++nPoints;
--it;
}
}
}
return nPoints;
}
template <unsigned int TDimension, typename TTubePointType>
bool
TubeSpatialObject<TDimension, TTubePointType>::ComputeTangentsAndNormals()
{
itkDebugMacro("Computing the tangent vectors of the tube");
// Discrete Frenet Frame computation from
// http://purl.flvc.org/fsu/fd/FSU_migr_etd-7477
// Discrete Frenet Frame with Application to Structural Biology and
// Kinematics.
// Lu, Yuanting et al.
// FSU Dissertation FSU_migr_etd-7477, June 27, 2013
const int length = this->GetNumberOfPoints();
if (length == 0)
{
return false;
}
if (length == 1)
{
const VectorType t(0.0);
((TubePointType *)(this->GetPoint(0)))->SetTangentInObjectSpace(t);
return true;
}
unsigned int it1 = 0;
unsigned int it2 = 1;
unsigned int it3 = 2;
while (it3 < static_cast<unsigned int>(length))
{
// Compute tangent using the adjacent points
const PointType & x1 = this->GetPoint(it1)->GetPositionInObjectSpace();
const PointType & x3 = this->GetPoint(it3)->GetPositionInObjectSpace();
double l{ 0.0 };
VectorType t;
for (unsigned int i = 0; i < TDimension; ++i)
{
t[i] = (x3[i] - x1[i]);
l = l + t[i] * t[i];
}
l = std::sqrt(l);
// If the adjacent points correspond, use the current point and one forward point
if (Math::AlmostEquals(l, 0.0) || std::isnan(l))
{
const PointType & x2 = this->GetPoint(it2)->GetPositionInObjectSpace();
l = 0;
for (unsigned int i = 0; i < TDimension; ++i)
{
t[i] = (x3[i] - x2[i]);
l = l + t[i] * t[i];
}
l = std::sqrt(l);
// If the forward point and the current point correspond, then RemoveDuplicatePointsInObjectSpace was not called
if (Math::AlmostEquals(l, 0.0) || std::isnan(l))
{
std::cerr << "TubeSpatialObject::ComputeTangentAndNormals() : "
<< "length between two consecutive points is 0"
<< " (use RemoveDuplicatePointsInObjectSpace())" << std::endl
<< " p1 = " << x1 << std::endl
<< " p2 = " << x2 << std::endl
<< " p3 = " << x3 << std::endl;
return false;
}
}
for (unsigned int i = 0; i < TDimension; ++i)
{
t[i] /= l;
}
((TubePointType *)(this->GetPoint(it2)))->SetTangentInObjectSpace(t);
++it1;
++it2;
++it3;
}
// Calculate tangents are the first and last point on a tube
{
it1 = 0;
it2 = 1;
const VectorType & ta = ((TubePointType *)(this->GetPoint(it2)))->GetTangentInObjectSpace();
((TubePointType *)(this->GetPoint(it1)))->SetTangentInObjectSpace(ta);
}
{
it1 = length - 1;
it2 = length - 2;
const VectorType & tb = ((TubePointType *)(this->GetPoint(it2)))->GetTangentInObjectSpace();
((TubePointType *)(this->GetPoint(it1)))->SetTangentInObjectSpace(tb);
}
CovariantVectorType prevN1;
prevN1.Fill(0);
prevN1[TDimension - 1] = 1;
CovariantVectorType prevN2;
prevN2.Fill(0);
prevN2[TDimension - 2] = 1;
it1 = 0;
it2 = 1;
while (it1 < static_cast<unsigned int>(length))
{
const VectorType & t = ((TubePointType *)(this->GetPoint(it1)))->GetTangentInObjectSpace();
it2 = it1 + 1;
if (it2 >= static_cast<unsigned int>(length))
{
it2 = it1 - 1;
}
const VectorType & t2 = ((TubePointType *)(this->GetPoint(it2)))->GetTangentInObjectSpace();
// Compute the normal
CovariantVectorType n1;
if (TDimension == 2)
{
// The normal to the tangent in 2D is the orthogonal direction to the tangent.
n1[0] = t[1];
n1[1] = -t[0];
if (it1 != 0)
{
double l{ 0.0 };
for (unsigned int i = 0; i < TDimension; ++i)
{
l += n1[i] * prevN1[i];
}
if (l < 0)
{
n1 *= -1;
}
}
((TubePointType *)(this->GetPoint(it1)))->SetNormal1InObjectSpace(n1);
prevN1 = n1;
}
else if (TDimension == 3)
{
// The normal to the tangent in 3D is the cross product of adjacent tangent directions.
n1[0] = t[1] * t2[2] - t[2] * t2[1];
n1[1] = t[2] * t2[0] - t[0] * t2[2];
n1[2] = t[0] * t2[1] - t[1] * t2[0];
const double l_n1 = std::sqrt(n1[0] * n1[0] + n1[1] * n1[1] + n1[2] * n1[2]);
if (Math::AlmostEquals(l_n1, 0.0) || std::isnan(l_n1))
{
if (it1 != 0)
{
n1 = prevN1;
}
else
{
// if the normal is null, pick an orthogonal direction
double d = std::sqrt(t[0] * t[0] + t[1] * t[1]);
if (d != 0)
{
n1[0] = t[1] / d;
n1[1] = -t[0] / d;
n1[2] = 0;
}
else
{
d = std::sqrt(t[1] * t[1] + t[2] * t[2]);
if (d != 0)
{
n1[0] = 0;
n1[1] = t[2] / d;
n1[2] = -t[1] / d;
}
else
{
n1 = prevN1;
}
}
}
}
else
{
n1 /= l_n1;
}
// The second normal is the cross product of the tangent and the first normal
CovariantVectorType n2;
n2[0] = t[1] * n1[2] - t[2] * n1[1];
n2[1] = t[2] * n1[0] - t[0] * n1[2];
n2[2] = t[0] * n1[1] - t[1] * n1[0];
const double l_n2 = std::sqrt(n2[0] * n2[0] + n2[1] * n2[1] + n2[2] * n2[2]);
if (Math::AlmostEquals(l_n2, 0.0) || std::isnan(l_n2))
{
n2 = prevN2;
}
else
{
n2 /= l_n2;
}
if (it1 != 0)
{
double l = 0;
for (unsigned int i = 0; i < TDimension; ++i)
{
l += n1[i] * prevN1[i];
}
if (l < 0)
{
n1 *= -1;
}
l = 0;
for (unsigned int i = 0; i < TDimension; ++i)
{
l += n2[i] * prevN2[i];
}
if (l < 0)
{
n2 *= -1;
}
}
((TubePointType *)(this->GetPoint(it1)))->SetNormal1InObjectSpace(n1);
((TubePointType *)(this->GetPoint(it1)))->SetNormal2InObjectSpace(n2);
prevN1 = n1;
prevN2 = n2;
}
++it1;
}
return true;
}
} // end namespace itk
#endif // end itkTubeSpatialObject_hxx
|