# PeriodicBoundaryCondition

**URL:** https://discourse.itk.org/t/periodicboundarycondition/698
**Category:** Algorithms
**Created:** [February 22, 2018, 11:22am UTC](https://discourse.itk.org/t/periodicboundarycondition/698 "2018-02-22T11:22:59Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [February 22, 2018, 11:22am UTC](https://discourse.itk.org/t/periodicboundarycondition/698/1 "2018-02-22T11:22:59Z")

</div>

Hello,

Is there an example of how to use a `PeriodicBoundaryCondition` anywhere?

I had hoped it was as simple as changing the definition of my `NeighborhoodIterator` to `ConstNeighborhoodIterator<TImage, PeriodicBoundaryCondition<TImage, TImage>>;`, however, this is causing a segmentation fault at line 57 of `itkPeriodicBoundaryCondition.h`, and the call to `GetImagePointer` is hitting a null pointer (which makes no sense to me). If I remove the template parameter so the default constant flux boundary condition is used, the code runs fine.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [February 22, 2018, 2:17pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/2 "2018-02-22T14:17:30Z")

</div>

A short runnable example would kick-start debugging.

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [February 22, 2018, 8:16pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/3 "2018-02-22T20:16:09Z")

</div>

Hi,

I’ve attached an example that gives the segmentation fault (compiled with ITK 4.13).

Thanks!

[example\_periodic.cpp](https://discourse.itk.org/uploads/default/original/1X/481bbb4f78655916aa779d7eb3b619ef6a4f435c.cpp) (1.1 KB)

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [March 2, 2018, 9:51am UTC](https://discourse.itk.org/t/periodicboundarycondition/698/4 "2018-03-02T09:51:43Z")

</div>

Hi,

I’m wondering if anyone had a chance to look at the code I attached above?

Thanks

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 2, 2018, 2:42pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/5 "2018-03-02T14:42:34Z")

</div>

Hi Toby,

Thanks for sharing the nice example.

There were two issues to make it work.

1. Call `OverrideBoundaryCondition` in the example.
2. The use of `auto` has revealed a bug in `itk::PeriodicBoundaryCondition`.

Please review this patch:

[http://review.source.kitware.com/#/c/23222/](http://review.source.kitware.com/#/c/23222/)

And here is the working example code:

```auto
#include <iostream>

#include "itkImage.h"
#include "itkConstNeighborhoodIterator.h"
#include "itkPeriodicBoundaryCondition.h"
#include "itkMultiThreaderBase.h"

int main(int argc, char **argv) {
    itk::MultiThreaderBase::SetGlobalMaximumNumberOfThreads(4);

    typedef itk::Image<float, 3> ImageType;

    auto test_image = ImageType::New();
    ImageType::RegionType test_region;
    test_region.GetModifiableSize() = {{64, 64, 64}};
    test_image->SetRegions(test_region);
    test_image->Allocate();

    using BoundaryConditionType = itk::PeriodicBoundaryCondition< ImageType, ImageType >;
    using IterType = itk::ConstNeighborhoodIterator<ImageType, BoundaryConditionType>;
    IterType::RadiusType radius; radius.Fill(1);
    IterType test_iter(radius, test_image, test_region);
    BoundaryConditionType boundaryCondition;
    test_iter.OverrideBoundaryCondition(&boundaryCondition);
    std::vector<IterType::OffsetType> back = { {{-1, 0, 0}}, {{ 0,-1, 0}}, {{ 0, 0,-1}} };
    test_iter.SetNeedToUseBoundaryCondition(true);
    test_iter.GoToBegin();
    while (!test_iter.IsAtEnd()) {
        float sum = 0;
        for (auto j = 0; j < back.size(); j++) {
            const float d = test_iter.GetPixel(back[j]);
            sum += d*d;
        }
        ++test_iter;
    }
    std::cout << "FINISHED" << std::endl;
    return EXIT_SUCCESS;
}

```

[CMakeLists.txt](https://discourse.itk.org/uploads/default/original/1X/14f32b0975df4b6e7a74cb9139485c3051012639.txt) (224 Bytes)

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [March 2, 2018, 2:55pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/6 "2018-03-02T14:55:25Z")

</div>

Hi Matt,

Thanks for finding the bug. How detailed should code reviews like this be? The change looks good to me, but would you prefer that I check out that gerrit branch and confirm my own program works?

The only comment I really have is that it would be good to add an example to the docs for `PeriodicBoundaryCondition`, as I don’t think it’s intuitive to have to use `OverrideBoundaryCondition`. I’m happy for the above code to be used if necessary. What’s the best way to get that added to the docs?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 2, 2018, 3:39pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/7 "2018-03-02T15:39:18Z")

</div>

Make a pull request to [SoftwareGuide](https://github.com/InsightSoftwareConsortium/ITKSoftwareGuide) and/or edit the doxygen comments in the header and then make a Gerrit [patch](https://itk.org/Wiki/ITK/Git/Develop).

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 2, 2018, 3:48pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/8 "2018-03-02T15:48:12Z")

</div>

@spinicist Yes, if you could please check out the patch and just verify that it works locally for you, that would be helpful.

Yes, the need to call `OverrideBoundaryCondition` is not intuitive. As @dzenanz mentioned, it would be nice to have this noted in the [_Iterators_ chapter of the Software Guide](https://itk.org/ITKSoftwareGuide/html/Book1/ITKSoftwareGuide-Book1ch6.html#x44-1150006.4). We [recently made it easier to contribute to the Software Guide on GitHub](https://discourse.itk.org/t/contribute-to-the-itk-software-guide-on-github/695/2).

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [March 2, 2018, 6:06pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/9 "2018-03-02T18:06:45Z")

</div>

Why is `OverrideBoundaryCondition` needed if we are using the “default” templated `BoundaryCondition` type?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 5, 2018, 3:59pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/10 "2018-03-05T15:59:41Z")

</div>

> [@phcerdan](#):
>
> Why is OverrideBoundaryCondition needed if we are using the “default” templated BoundaryCondition type?

Yes, if we could make the `OverrideBoundaryCondition` call optional, that would be nice.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [March 5, 2018, 10:46pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/11 "2018-03-05T22:46:54Z")

</div>

The second template parameter of ConstNeighborhoodIterator is miss leading.

```auto
template<typename TImage, typename TBoundaryCondition = ZeroFluxNeumannBoundaryCondition< TImage >>
class itk::ConstNeighborhoodIterator< TImage, TBoundaryCondition >

```

It represents the default boundary condition that the neighborhood iterator is constructed ( and can be reset to ), and not the only boundary condition that can be used. The boundary condition can set at runtime via the OverrideBoundaryCondition method to another boundary type. This makes the class have a rather ugly mix of runtime, and compile time specification of the boundary condition. We MUST consider both of these use cases!

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 5, 2018, 11:13pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/12 "2018-03-05T23:13:51Z")

</div>

> [@matt.mccormick](#):
>
> Yes, if we could make the OverrideBoundaryCondition call optional, that would be nice.

After investigation, it looks like calling `OverrideBoundaryCondition` should be / will be optional.

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [March 12, 2018, 5:11pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/13 "2018-03-12T17:11:06Z")

</div>

Hi,

I was very busy this week so have completely lost track of the multiple Gerrit threads. Is there something I can do to help move things along?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 14, 2018, 2:29am UTC](https://discourse.itk.org/t/periodicboundarycondition/698/14 "2018-03-14T02:29:26Z")

</div>

Hi Toby,

The issue has been resolved on ITK Git master.

Matt

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)
#### Post date: [March 15, 2018, 2:47pm UTC](https://discourse.itk.org/t/periodicboundarycondition/698/15 "2018-03-15T14:47:53Z")

</div>

Great, I will hopefully get round to testing it soon.
