From bbb1f7c5ad9287511892cf16dadeac59904070cf Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Thu, 4 May 2023 14:30:21 +0200
Subject: [PATCH] Correct bug in makeReLayout: don't modify fct arg z_ref

---
 CHANGELOG                       | 1 +
 Resample/Processed/ReSample.cpp | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 69f6db6ca2b..337b891cd74 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -7,6 +7,7 @@ BornAgain-20.2, released 2023.05.04
         reported by Liz Margarita Montanez Huaman)
     * Repair GUI support for angular grazing scans (#574)
     * Consistently mention K-correlation model which is really used for roughness (#581)
+    * Correct vertical shift for multi-particle layouts (#584, reported by Timo Fuchs)
   > Dependencies
     * Adapt to libheinz-1.1.0
 
diff --git a/Resample/Processed/ReSample.cpp b/Resample/Processed/ReSample.cpp
index fca9642005e..1dca4f6cd9d 100644
--- a/Resample/Processed/ReSample.cpp
+++ b/Resample/Processed/ReSample.cpp
@@ -243,7 +243,8 @@ ReLayout* makeReLayout(const ParticleLayout& layout, const SliceStack& slices, d
         OwningVector<IReParticle> terms;
 
         for (const auto* subparticle : particle->decompose()) {
-            const auto slice_indices = SliceIndexSpan(*subparticle, slices, z_ref);
+            double z_sub = z_ref;
+            const auto slice_indices = SliceIndexSpan(*subparticle, slices, z_sub);
             bool single_layer = (slice_indices.first == slice_indices.second);
             double z0 = slices.at(0).low();
             std::unique_ptr<IParticle> myparticle(subparticle->clone());
@@ -251,8 +252,8 @@ ReLayout* makeReLayout(const ParticleLayout& layout, const SliceStack& slices, d
             for (size_t i = slice_indices.first; i < slice_indices.second + 1; ++i) {
                 const Slice& slice = slices[i];
                 double z_top_i = i == 0 ? 0 : slice.hig() - z0;
-                R3 translation(0.0, 0.0, z_ref - z_top_i);
-                z_ref = z_top_i; // subparticle now has coordinates relative to z_top_i
+                R3 translation(0.0, 0.0, z_sub - z_top_i);
+                z_sub = z_top_i; // subparticle now has coordinates relative to z_top_i
                 myparticle->translate(translation);
 
                 // if subparticle is contained in this layer, set limits to infinite:
-- 
GitLab