Skip to content

Commit

Permalink
Replace is_2d by dim variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ting Xu authored and Ting Xu committed Apr 7, 2016
1 parent c345989 commit 12c8b70
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 42 deletions.
8 changes: 3 additions & 5 deletions main_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -916,17 +916,15 @@ void MainWindow::DeformSnakes() {
}

void MainWindow::DeformSnakesInAction() {
bool is_2d = multisnake_->is_2d();
viewer_->RemoveSnakes();
progress_bar_->setMaximum(multisnake_->GetNumberOfInitialSnakes());
unsigned ncompleted = 0;
while (!multisnake_->initial_snakes().empty()) {
Snake *s = multisnake_->PopLastInitialSnake();
viewer_->SetupSnake(s, 0);
viewer_->Render();
s->Evolve(multisnake_->solver_bank(),
multisnake_->converged_snakes(),
Snake::iterations_per_press(), is_2d);
s->Evolve(multisnake_->solver_bank(), multisnake_->converged_snakes(),
Snake::iterations_per_press(), multisnake_->dim());

if (s->viable()) {
if (s->converged()) {
Expand Down Expand Up @@ -988,7 +986,7 @@ void MainWindow::DeformOneSnake() {

viewer_->trimmed_snake()->EvolveWithTipFixed(
multisnake_->solver_bank(), Snake::iterations_per_press(),
multisnake_->is_2d());
multisnake_->dim());

if (viewer_->trimmed_snake()->converged()) {
statusBar()->showMessage(tr("Snake is converged."));
Expand Down
30 changes: 14 additions & 16 deletions multisnake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Multisnake::Multisnake(QObject *parent) :
QObject(parent), image_(NULL), external_force_(NULL),
intensity_scaling_(0.0), sigma_(0.0),
ridge_threshold_(0.01), foreground_(65535),
background_(0), initialize_z_(true), is_2d_(false) {
background_(0), initialize_z_(true), dim_(kDimension) {
interpolator_ = InterpolatorType::New();
vector_interpolator_ = VectorInterpolatorType::New();
transform_ = TransformType::New();
Expand Down Expand Up @@ -94,7 +94,7 @@ void Multisnake::LoadImage(const std::string &filename) {
image_->GetLargestPossibleRegion().GetSize();
std::cout << "Image size: " << size << std::endl;
if (size[2] < 2) {
is_2d_ = true;
dim_ = 2;
}
interpolator_->SetInputImage(image_);

Expand Down Expand Up @@ -339,7 +339,7 @@ void Multisnake::ComputeImageGradient(bool reset) {
InternalImageType::Pointer img = scaler->GetOutput();

if (sigma_ > 0.0) {
if (is_2d_) {
if (dim_ == 2) {
typedef itk::Image<double, 2> TwoDImageType;
typedef itk::ExtractImageFilter<InternalImageType,
TwoDImageType> ExtractFilterType;
Expand Down Expand Up @@ -410,7 +410,7 @@ void Multisnake::InitializeSnakes() {
this->ScanGradient(ridge_image);

unsigned num_directions = 2;
if (!is_2d_ && initialize_z_) num_directions = 3;
if (dim_ == 3 && initialize_z_) num_directions = 3;

BoolVectorImageType::Pointer candidate_image =
InitializeBoolVectorImage();
Expand Down Expand Up @@ -445,8 +445,7 @@ void Multisnake::ScanGradient(BoolVectorImageType::Pointer ridge_image) {
OutputIteratorType iter(ridge_image,
ridge_image->GetLargestPossibleRegion());

const unsigned d = is_2d_? 2 : 3;
for (unsigned i = 0; i < d; ++i) {
for (unsigned i = 0; i < dim_; ++i) {
for (iter.GoToBegin(); !iter.IsAtEnd(); ++iter) {
VectorImageType::IndexType index = iter.GetIndex();
VectorImageType::IndexType current_index = index;
Expand Down Expand Up @@ -500,12 +499,11 @@ void Multisnake::GenerateCandidates(
BoolVectorImageType::IndexType index = iter.GetIndex();
BoolVectorImageType::PixelType ridge_value = ridge_image->GetPixel(index);

unsigned d = is_2d_ ? 2 : 3;
if (is_2d_) {
iter.Value()[direction] = ridge_value[(direction+1) % d];
if (dim_ == 2) {
iter.Value()[direction] = ridge_value[(direction+1) % dim_];
} else {
iter.Value()[direction] = ridge_value[(direction+1) % d] &&
ridge_value[(direction+2) % d];
iter.Value()[direction] = ridge_value[(direction+1) % dim_] &&
ridge_value[(direction+2) % dim_];
}
}
}
Expand All @@ -525,7 +523,7 @@ void Multisnake::LinkCandidates(
BoolVectorImageType::Pointer candidate_image, unsigned direction) {
ImageType::SizeType size = image_->GetLargestPossibleRegion().GetSize();

if (is_2d_) {
if (dim_ == 2) {
for (unsigned c0 = 0; c0 < size[direction]; ++c0) {
for (unsigned c1 = 0; c1 < size[(direction+1)%2]; ++c1) {
BoolVectorImageType::IndexType current_index;
Expand Down Expand Up @@ -605,7 +603,7 @@ bool Multisnake::FindNextCandidate(
if (candidate_image->GetPixel(index)[direction])
return true;

if (is_2d_) {
if (dim_ == 2) {
int d1 = (direction + 1) % 2;
for (int c1 = current_index[d1] - 1;
c1 <= current_index[d1] + 1; ++c1) {
Expand Down Expand Up @@ -643,7 +641,7 @@ void Multisnake::DeformSnakes() {
Snake *snake = initial_snakes_.back();
initial_snakes_.pop_back();
solver_bank_->Reset(false);
snake->Evolve(solver_bank_, converged_snakes_, kBigNumber, is_2d_);
snake->Evolve(solver_bank_, converged_snakes_, kBigNumber, dim_);

if (snake->viable()) {
converged_snakes_.push_back(snake);
Expand Down Expand Up @@ -701,7 +699,7 @@ void Multisnake::GroupSnakes() {
SnakeIterator it = converged_snakes_.begin();
while (it != converged_snakes_.end()) {
solver_bank_->Reset(false);
(*it)->EvolveWithTipFixed(solver_bank_, 100, is_2d_);
(*it)->EvolveWithTipFixed(solver_bank_, 100, dim_);
if ((*it)->viable()) {
it++;
} else {
Expand Down Expand Up @@ -959,7 +957,7 @@ void Multisnake::SaveSnakes(const SnakeContainer &snakes,
for (unsigned j = 0; j != (*it)->GetSize(); ++j) {
double intensity = interpolator_->Evaluate((*it)->GetPoint(j));
double background_intensity = -1.0;
if (is_2d_) {
if (dim_ == 2) {
background_intensity = (*it)->ComputeBackgroundMeanIntensity2d(j);
} else {
background_intensity = (*it)->ComputeBackgroundMeanIntensity(j);
Expand Down
7 changes: 3 additions & 4 deletions multisnake.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Multisnake : public QObject {
bool initialize_z() const {return initialize_z_;}
void set_initialize_z(bool init_z) {initialize_z_ = init_z;}

bool is_2d() const {return is_2d_;}
unsigned dim() const {return dim_;}

SolverBank *solver_bank() const {return solver_bank_;}

Expand Down Expand Up @@ -362,10 +362,9 @@ class Multisnake : public QObject {
bool initialize_z_;

/*
* True if input image is 2d. If true, SOAX behaviour is adapted to
* 2D. The output SOACs z coordinates is 0.
* Image dimentionality in which snakes operate on.
*/
bool is_2d_;
unsigned dim_;

DISALLOW_COPY_AND_ASSIGN(Multisnake);
};
Expand Down
24 changes: 11 additions & 13 deletions snake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void Snake::InterpolateVertices(const PairContainer *sums,


void Snake::Evolve(SolverBank *solver, const SnakeContainer &converged_snakes,
unsigned max_iter, bool is_2d) {
unsigned max_iter, unsigned dim) {
unsigned iter = 0;

while (iter <= max_iter) {
Expand All @@ -176,7 +176,7 @@ void Snake::Evolve(SolverBank *solver, const SnakeContainer &converged_snakes,
if (!viable_) break;
this->HandleTailOverlap(converged_snakes);
if (!viable_) break;
this->IterateOnce(solver, is_2d);
this->IterateOnce(solver, dim);
this->Resample();
iter++;
if (!viable_) break;
Expand Down Expand Up @@ -370,14 +370,13 @@ double Snake::FindClosestIndexTo(const PointType &p, unsigned &ind) {
return min_d;
}

void Snake::IterateOnce(SolverBank *solver, bool is_2d) {
void Snake::IterateOnce(SolverBank *solver, unsigned dim) {
VectorContainer rhs;
this->ComputeRHSVector(solver->gamma(), rhs, is_2d);
// this->PrintVectorContainer(rhs);
const unsigned d = is_2d ? 2 : 3;
for (unsigned dim = 0; dim < d; ++dim) {
solver->SolveSystem(rhs, dim, open_);
this->CopySolutionToVertices(solver, dim);
this->ComputeRHSVector(solver->gamma(), rhs, dim);

for (unsigned i = 0; i < dim; ++i) {
solver->SolveSystem(rhs, i, open_);
this->CopySolutionToVertices(solver, i);
}

if (this->HeadIsFixed())
Expand All @@ -404,8 +403,7 @@ void Snake::CopySolutionToVertices(SolverBank *solver, unsigned dim) {
}
}

void Snake::ComputeRHSVector(double gamma, VectorContainer &rhs, bool is_2d) {
unsigned dim = is_2d ? 2 : kDimension;
void Snake::ComputeRHSVector(double gamma, VectorContainer &rhs, unsigned dim) {
this->AddVerticesInfo(gamma, rhs);
this->AddExternalForce(rhs, dim);
if (open_)
Expand Down Expand Up @@ -820,7 +818,7 @@ void Snake::CopySubSnakes(SnakeContainer &c) {
}

void Snake::EvolveWithTipFixed(SolverBank *solver, unsigned max_iter,
bool is_2d) {
unsigned dim) {
unsigned iter = 0;
fixed_head_ = vertices_.front();
fixed_tail_ = vertices_.back();
Expand All @@ -831,7 +829,7 @@ void Snake::EvolveWithTipFixed(SolverBank *solver, unsigned max_iter,
break;
}

this->IterateOnce(solver, is_2d);
this->IterateOnce(solver, dim);
this->Resample();
iter++;
}
Expand Down
8 changes: 4 additions & 4 deletions snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ class Snake {
const SnakeContainer &subsnakes() const {return subsnakes_;}

void Evolve(SolverBank *solver, const SnakeContainer &converged_snakes,
unsigned max_iter, bool is_2d);
void EvolveWithTipFixed(SolverBank *solver, unsigned max_iter, bool is_2d);
unsigned max_iter, unsigned dim);
void EvolveWithTipFixed(SolverBank *solver, unsigned max_iter, unsigned dim);
void UpdateHookedIndices();
void CopySubSnakes(SnakeContainer &c);
bool PassThrough(const PointType &p, double threshold) const;
Expand Down Expand Up @@ -239,14 +239,14 @@ class Snake {
Snake * &s, unsigned &index);
double FindClosestIndexTo(const PointType &p, unsigned &ind);

void IterateOnce(SolverBank *solver, bool is_2d);
void IterateOnce(SolverBank *solver, unsigned dim);

/*
* Ensure the updated snake points stay within image after each
* iteration.
*/
void CopySolutionToVertices(SolverBank *solver, unsigned direction);
void ComputeRHSVector(double gamma, VectorContainer &rhs, bool is_2d);
void ComputeRHSVector(double gamma, VectorContainer &rhs, unsigned dim);
void AddExternalForce(VectorContainer &rhs, unsigned dim);
void AddStretchingForce(VectorContainer &rhs, unsigned dim);
void AddVerticesInfo(double gamma, VectorContainer &rhs);
Expand Down

0 comments on commit 12c8b70

Please sign in to comment.