From c67946385873e0ecfb9a760ad3e6fe74c8c0c698 Mon Sep 17 00:00:00 2001
From: Walter Van Herck <w.van.herck@fz-juelich.de>
Date: Tue, 10 Sep 2013 13:01:19 +0200
Subject: [PATCH] Refactor: internally store material pointers in
 FormFactorDecoratorScalarMaterial instead of the refractive indices

---
 .../inc/FormFactorDecoratorScalarMaterial.h   | 46 +++++++++++--------
 Core/Samples/inc/Particle.h                   |  8 ++--
 Core/Samples/src/Particle.cpp                 | 46 +++++++++----------
 3 files changed, 53 insertions(+), 47 deletions(-)

diff --git a/Core/FormFactors/inc/FormFactorDecoratorScalarMaterial.h b/Core/FormFactors/inc/FormFactorDecoratorScalarMaterial.h
index 664d830bd49..83403ab258f 100644
--- a/Core/FormFactors/inc/FormFactorDecoratorScalarMaterial.h
+++ b/Core/FormFactors/inc/FormFactorDecoratorScalarMaterial.h
@@ -34,26 +34,30 @@ class FormFactorDecoratorScalarMaterial : public FormFactorDecoratorFactor
     //! Sets the material of the scatterer
     void setMaterial(const IMaterial *p_material);
 
+    //! Retrieves the refractive index of the ambient material
     virtual complex_t getAmbientRefractiveIndex() const {
-        return m_refractive_index;
+        if (mp_ambient_material) {
+            return mp_ambient_material->getRefractiveIndex();
+        }
+        return 1.0;
     }
 
+    //! Sets the ambient material
     virtual void setAmbientMaterial(const IMaterial *p_material);
  private:
-    complex_t getRefractiveIndexFactor(const complex_t& ambient_index,
-            const complex_t& particle_index) const;
+    complex_t getRefractiveIndexFactor() const;
 
-    complex_t m_refractive_index;
-    complex_t m_ambient_refractive_index;
     complex_t m_wavevector_scattering_factor;
+    const IMaterial *mp_material;
+    const IMaterial *mp_ambient_material;
 };
 
 inline FormFactorDecoratorScalarMaterial::FormFactorDecoratorScalarMaterial(
         IFormFactor* p_form_factor, complex_t wavevector_scattering_factor)
 : FormFactorDecoratorFactor(p_form_factor, 1.0)
-, m_refractive_index(1.0)
-, m_ambient_refractive_index(1.0)
 , m_wavevector_scattering_factor(wavevector_scattering_factor)
+, mp_material(0)
+, mp_ambient_material(0)
 {
     setName("FormFactorDecoratorScalarMaterial");
 }
@@ -68,9 +72,8 @@ FormFactorDecoratorScalarMaterial::clone() const
     FormFactorDecoratorScalarMaterial *result =
             new FormFactorDecoratorScalarMaterial(mp_form_factor->clone(),
                     m_wavevector_scattering_factor);
-    result->m_refractive_index = m_refractive_index;
-    result->m_ambient_refractive_index = m_ambient_refractive_index;
-    result->m_factor = m_factor;
+    result->setMaterial(mp_material);
+    result->setAmbientMaterial(mp_ambient_material);
     result->setName(getName());
     return result;
 }
@@ -79,27 +82,30 @@ inline void FormFactorDecoratorScalarMaterial::setMaterial(
         const IMaterial* p_material)
 {
     if (p_material) {
-        m_refractive_index = p_material->getRefractiveIndex();
+        mp_material = p_material;
     }
-    m_factor = getRefractiveIndexFactor(m_ambient_refractive_index,
-                                        m_refractive_index);
+    m_factor = getRefractiveIndexFactor();
 }
 
 inline void FormFactorDecoratorScalarMaterial::setAmbientMaterial(
         const IMaterial *p_material)
 {
     if (p_material) {
-        m_ambient_refractive_index = p_material->getRefractiveIndex();
+        mp_ambient_material = p_material;
     }
-    m_factor = getRefractiveIndexFactor(m_ambient_refractive_index,
-                                        m_refractive_index);
+    m_factor = getRefractiveIndexFactor();
 }
 
-inline complex_t FormFactorDecoratorScalarMaterial::getRefractiveIndexFactor(
-        const complex_t& ambient_index, const complex_t& particle_index) const
+inline complex_t
+FormFactorDecoratorScalarMaterial::getRefractiveIndexFactor() const
 {
-    return m_wavevector_scattering_factor *
-            (particle_index*particle_index - ambient_index*ambient_index);
+    if (mp_material && mp_ambient_material) {
+        complex_t particle_index = mp_material->getRefractiveIndex();
+        complex_t ambient_index = mp_ambient_material->getRefractiveIndex();
+        return m_wavevector_scattering_factor *
+                (particle_index*particle_index - ambient_index*ambient_index);
+    }
+    else return m_wavevector_scattering_factor;
 }
 
 #endif /* FORMFACTORDECORATORSCALARMATERIAL_H_ */
diff --git a/Core/Samples/inc/Particle.h b/Core/Samples/inc/Particle.h
index 5cfe112371b..ece024dcffc 100644
--- a/Core/Samples/inc/Particle.h
+++ b/Core/Samples/inc/Particle.h
@@ -63,10 +63,6 @@ class BA_CORE_API_ Particle : public ICompositeSample
     virtual FormFactorPol* createFormFactorMatrix(
             complex_t wavevector_scattering_factor) const;
 
-    //! Sets the form factor of the particle (not including scattering factor
-    //! from refractive index)
-    virtual void setSimpleFormFactor(IFormFactor* p_form_factor);
-
     //! Sets _material_ and _thickness_.
     virtual void setMaterial(const IMaterial* p_material) {
         mp_material = p_material;
@@ -96,6 +92,10 @@ class BA_CORE_API_ Particle : public ICompositeSample
         return mp_form_factor;
     }
 
+    //! Sets the form factor of the particle (not including scattering factor
+    //! from refractive index)
+    virtual void setSimpleFormFactor(IFormFactor* p_form_factor);
+
     //! Creates list of contained particles for diffuse calculations
     virtual std::vector<DiffuseParticleInfo *> *createDiffuseParticleInfo(
             const ParticleInfo& parent_info) const;
diff --git a/Core/Samples/src/Particle.cpp b/Core/Samples/src/Particle.cpp
index 3df6473fbae..2bd3178c1ff 100644
--- a/Core/Samples/src/Particle.cpp
+++ b/Core/Samples/src/Particle.cpp
@@ -99,29 +99,6 @@ Particle* Particle::cloneInvertB() const
     return p_new;
 }
 
-std::vector<ParticleInfo*> Particle::createDistributedParticles(
-        size_t samples_per_particle, double factor) const
-{
-    if(!mp_form_factor) throw NullPointerException("Particle::createDistributedParticles() -> No formfactor is defined.");
-
-    std::vector<ParticleInfo*> result;
-    if (mp_form_factor->isDistributedFormFactor()) {
-        std::vector<IFormFactor *> form_factors;
-        std::vector<double> probabilities;
-        mp_form_factor->createDistributedFormFactors(form_factors, probabilities, samples_per_particle);
-        if (form_factors.size() > 0 && form_factors.size()==probabilities.size()) {
-            for (size_t i=0; i<form_factors.size(); ++i) {
-                Particle *new_particle = clone();
-                new_particle->setSimpleFormFactor(form_factors[i]);
-                ParticleInfo *p_info =
-                    new ParticleInfo(new_particle, 0., probabilities[i]*factor);
-                result.push_back(p_info);
-            }
-        }
-    }
-    return result;
-}
-
 IFormFactor* Particle::createFormFactor(
         complex_t wavevector_scattering_factor) const
 {
@@ -152,6 +129,29 @@ FormFactorPol* Particle::createFormFactorMatrix(
     return p_ff;
 }
 
+std::vector<ParticleInfo*> Particle::createDistributedParticles(
+        size_t samples_per_particle, double factor) const
+{
+    if(!mp_form_factor) throw NullPointerException("Particle::createDistributedParticles() -> No formfactor is defined.");
+
+    std::vector<ParticleInfo*> result;
+    if (mp_form_factor->isDistributedFormFactor()) {
+        std::vector<IFormFactor *> form_factors;
+        std::vector<double> probabilities;
+        mp_form_factor->createDistributedFormFactors(form_factors, probabilities, samples_per_particle);
+        if (form_factors.size() > 0 && form_factors.size()==probabilities.size()) {
+            for (size_t i=0; i<form_factors.size(); ++i) {
+                Particle *new_particle = clone();
+                new_particle->setSimpleFormFactor(form_factors[i]);
+                ParticleInfo *p_info =
+                    new ParticleInfo(new_particle, 0., probabilities[i]*factor);
+                result.push_back(p_info);
+            }
+        }
+    }
+    return result;
+}
+
 void Particle::setSimpleFormFactor(IFormFactor* p_form_factor)
 {
     if (!p_form_factor) return;
-- 
GitLab