Index: src/Jobs/Grid/SamplingGrid.cpp
===================================================================
--- src/Jobs/Grid/SamplingGrid.cpp	(revision 98f8feac4a80f869fb2b5907d674b4bb7e505326)
+++ src/Jobs/Grid/SamplingGrid.cpp	(revision 1a00bbfd3d648bf38e39a80bc5a7461e50177425)
@@ -46,4 +46,15 @@
 #include "CodePatterns/Assert.hpp"
 #include "CodePatterns/Log.hpp"
+
+// static instances
+const double SamplingGrid::zeroOffset[3] = { 0., 0., 0. };
+
+SamplingGrid::SamplingGrid() :
+    SamplingGridProperties()
+{
+  setWindowSize(zeroOffset, zeroOffset);
+  ASSERT( getWindowGridPoints() == (size_t)0,
+      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
+}
 
 SamplingGrid::SamplingGrid(const double _begin[3],
@@ -53,14 +64,22 @@
   SamplingGridProperties(_begin, _end, _level),
   sampled_grid(_sampled_grid)
-{}
+{
+  setWindowSize(_begin, _end);
+  ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(),
+      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
+}
 
 SamplingGrid::SamplingGrid(const SamplingGrid &_grid) :
   SamplingGridProperties(_grid),
   sampled_grid(_grid.sampled_grid)
-{}
+{
+  setWindowSize(_grid.begin, _grid.end);
+}
 
 SamplingGrid::SamplingGrid(const SamplingGridProperties &_props) :
   SamplingGridProperties(_props)
-{}
+{
+  setWindowSize(_props.begin, _props.end);
+}
 
 SamplingGrid::SamplingGrid(
@@ -69,5 +88,9 @@
   SamplingGridProperties(_props),
   sampled_grid(_sampled_grid)
-{}
+{
+  setWindowSize(_props.begin, _props.end);
+  ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(),
+      "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
+}
 
 SamplingGrid::~SamplingGrid()
@@ -79,4 +102,5 @@
   if (this != &other) {
     static_cast<SamplingGridProperties &>(*this) = other;
+    setWindow(other.begin_window, other.end_window);
     sampled_grid = other.sampled_grid;
   }
@@ -100,6 +124,28 @@
 void SamplingGrid::superposeOtherGrids(const SamplingGrid &other, const double prefactor)
 {
-  // check that grids are compatible
+  /// check that grids are compatible
   if (isCompatible(other)) {
+    /// get maximum of window
+    double max_begin_window[3];
+    double max_end_window[3];
+    bool doExtend = false;
+    for (size_t index=0; index<3;++index) {
+      if (begin_window[index] >= other.begin_window[index]) {
+        max_begin_window[index] = other.begin_window[index];
+        doExtend = true;
+      } else {
+        max_begin_window[index] = begin_window[index];
+      }
+      if (end_window[index] >= other.end_window[index]) {
+        max_end_window[index] = end_window[index];
+      } else {
+        max_end_window[index] = other.end_window[index];
+        doExtend = true;
+      }
+    }
+    if (doExtend)
+      extendWindow(max_begin_window, max_end_window);
+    /// and copy other into larger window, too
+    addOntoWindow(other.begin_window, other.end_window, other.sampled_grid);
     // then add other's grid onto this one
     sampledvalues_t::iterator iter = sampled_grid.begin();
@@ -112,5 +158,5 @@
 }
 
-double SamplingGrid::getVolume() const
+const double SamplingGrid::getVolume() const
 {
   double volume = 1.;
@@ -120,4 +166,12 @@
 }
 
+const double SamplingGrid::getWindowVolume() const
+{
+  double volume = 1.;
+  for (size_t i=0;i<3;++i)
+    volume *= end_window[i]-begin_window[i];
+  return volume;
+}
+
 const size_t SamplingGrid::getTotalGridPoints() const
 {
@@ -128,4 +182,13 @@
 {
   return pow(2, level);
+}
+
+const size_t SamplingGrid::getWindowGridPoints() const
+{
+  const double volume = getVolume();
+  if (fabs(volume) < std::numeric_limits<double>::epsilon())
+    return 0;
+  else
+    return (getWindowVolume()*getTotalGridPoints())/volume;
 }
 
@@ -146,5 +209,4 @@
 {
   if (isCompatible(weight)) {
-    const size_t gridpoints = pow(2, level);
     const double volume_element = getVolume()/(double)getTotalGridPoints();
     double int_value = 0.;
@@ -160,8 +222,106 @@
 }
 
+void SamplingGrid::setWindowSize(
+    const double _begin_window[3],
+    const double _end_window[3])
+{
+  for (size_t index=0;index<3;++index) {
+    begin_window[index] = _begin_window[index];
+    end_window[index] = _end_window[index];
+  }
+}
+
+void SamplingGrid::setWindow(
+    const double _begin_window[3],
+    const double _end_window[3])
+{
+  setWindowSize(_begin_window, _end_window);
+  const size_t gridpoints_window = getWindowGridPoints();
+  sampled_grid.resize(gridpoints_window, 0.);
+}
+
+void SamplingGrid::setWindowSize(
+    const double _begin_window[3],
+    const double _size)
+{
+  for (size_t index=0;index<3;++index) {
+    begin_window[index] = _begin_window[index];
+    end_window[index] = _begin_window[index]+_size;
+  }
+}
+
+void SamplingGrid::extendWindow(
+    const double _begin_window[3],
+    const double _end_window[3])
+{
+  // check that we truly have to extend the window
+#ifndef NDEBUG
+  for(size_t index=0;index < 3; ++index) {
+    ASSERT ( begin_window[index] >= _begin_window[index],
+        "SamplingGrid::extendWindow() - component "+toString(index)+
+        " of window start is greater than old value.");
+    ASSERT ( end_window[index] <= _end_window[index],
+        "SamplingGrid::extendWindow() - component "+toString(index)+
+        " of window end is less than old value.");
+  }
+#endif
+  // copy old window size and values
+  double old_begin_window[3];
+  double old_end_window[3];
+  for(size_t index=0;index<3;++index) {
+    old_begin_window[index] = begin_window[index];
+    old_end_window[index] = end_window[index];
+  }
+  sampledvalues_t old_values(sampled_grid);
+  // now extend it ...
+  addOntoWindow(old_begin_window, old_end_window, old_values);
+}
+
+void SamplingGrid::addOntoWindow(
+    const double _begin_window[3],
+    const double _end_window[3],
+    const sampledvalues_t &_sampled_grid)
+{
+#ifndef NDEBUG
+  for(size_t index=0;index<3;++index) {
+    ASSERT( _begin_window[index] >= begin_window[index],
+        "SamplingGrid::addOntoWindow() - given window starts earlier in component "
+        +toString(index)+".");
+    ASSERT( _end_window[index] <= end_window[index],
+        "SamplingGrid::addOntoWindow() - given window ends later in component "
+        +toString(index)+".");
+  }
+#endif
+  // the only issue are indices
+  const size_t gridpoints_axis = pow(2, level);
+  size_t offset[3];
+  size_t length[3];
+  for(size_t index=0;index<3;++index) {
+    offset[index] = gridpoints_axis*(_begin_window[index] - begin_window[index]);
+    length[index] =  gridpoints_axis*(_end_window[index] - _begin_window[index]);
+  }
+  ASSERT( length[0]*length[1]*length[2] ==  _sampled_grid.size(),
+      "SamplingGrid::addOntoWindow() - not enough sampled values given.");
+  size_t N[3];
+//  sampledvalues_t::iterator griditer = sampled_grid.begin();
+  sampledvalues_t::const_iterator copyiter = _sampled_grid.begin();
+  for(N[0]=offset[0]; N[0] < length[0]; ++N[0]) {
+    for(N[1]=offset[1]; N[1] < length[1]; ++N[1]) {
+      for(N[2]=offset[2]; N[2] < length[2]; ++N[2]) {
+        const size_t index =
+            (N[0]*gridpoints_axis+N[1])*gridpoints_axis+N[2];
+        sampled_grid[index] += *copyiter;
+      }
+    }
+  }
+}
+
 std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other)
 {
-  ost << "SamplingGrid starting at " << other.begin[0] << "," << other.begin[1] << "," << other.begin[2];
-  ost << ", ending at " << other.end[0] << "," << other.end[1] << "," << other.end[2];
+  ost << "SamplingGrid";
+  ost << " starting at " << other.begin[0] << "," << other.begin[1] << "," << other.begin[2];
+  ost << " ending at " << other.end[0] << "," << other.end[1] << "," << other.end[2];
+  ost << ", window starting at " << other.begin_window[0] << "," << other.begin_window[1] << "," << other.begin_window[2];
+  ost << ", window ending at " << other.end_window[0] << "," << other.end_window[1] << "," << other.end_window[2];
   ost << ", level of " << other.level;
   ost << " and integrated value of " << other.integral();
