From 46e99b85484665b40d37699ca89cdddd702ebc85 Mon Sep 17 00:00:00 2001
From: Walter Van Herck <w.van.herck@fz-juelich.de>
Date: Thu, 12 Feb 2015 13:35:36 +0100
Subject: [PATCH] Implemented comparison of magnetic materials

---
 Core/Tools/inc/LabelSample.h   |  2 +-
 Core/Tools/src/LabelSample.cpp | 26 +++++++++++++++++---------
 2 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/Core/Tools/inc/LabelSample.h b/Core/Tools/inc/LabelSample.h
index 2bd8ed0556c..2ebd5827b1a 100644
--- a/Core/Tools/inc/LabelSample.h
+++ b/Core/Tools/inc/LabelSample.h
@@ -60,7 +60,7 @@ public:
 
 
 private:
-    bool definesSameMaterial(IMaterial *left, IMaterial *right) const;
+    bool definesSameMaterial(const IMaterial *left, const IMaterial *right) const;
     std::map<const IFormFactor *,std::string> m_formFactorLabel;
     std::map<const IInterferenceFunction *, std::string> m_interferenceFunctionLabel;
     std::map<const Layer *,std::string> m_layerLabel;
diff --git a/Core/Tools/src/LabelSample.cpp b/Core/Tools/src/LabelSample.cpp
index c53308ffbe7..55a61de93f5 100644
--- a/Core/Tools/src/LabelSample.cpp
+++ b/Core/Tools/src/LabelSample.cpp
@@ -132,8 +132,7 @@ void LabelSample::insertMaterial(const IMaterial *sample)
     std::map<const IMaterial *,std::string>::const_iterator iEnd = m_materialLabel.end();
     while (it != iEnd)
     {
-        if(it->first->getName() == sample->getName() &&
-                it->first->getRefractiveIndex() == sample->getRefractiveIndex() )
+        if(definesSameMaterial(it->first, sample) )
         {
             m_materialLabel[sample] = it->second;
             break;
@@ -142,9 +141,9 @@ void LabelSample::insertMaterial(const IMaterial *sample)
     }
     if(it == iEnd)
     {
-        std::ostringstream inter;
-        inter << "Material_" << m_materialLabel.size()+1;
-        m_materialLabel[sample] = inter.str();
+        std::ostringstream label_stream;
+        label_stream << "Material_" << m_materialLabel.size()+1;
+        m_materialLabel[sample] = label_stream.str();
     }
 }
 
@@ -169,7 +168,6 @@ void LabelSample::setLabel(const ILayout *sample)
     m_ILayoutLabel[sample] = inter.str();;
 }
 
-
 void LabelSample::setLabel(const Layer *sample)
 {
     std::ostringstream inter;
@@ -216,7 +214,7 @@ void LabelSample::setLabel(const ParticleInfo *sample)
     m_particleInfoLabel[sample] = inter.str();;
 }
 
-bool LabelSample::definesSameMaterial(IMaterial *left, IMaterial *right) const
+bool LabelSample::definesSameMaterial(const IMaterial *left, const IMaterial *right) const
 {
     // Non-magnetic materials
     if (left->isScalarMaterial() && right->isScalarMaterial()) {
@@ -228,8 +226,18 @@ bool LabelSample::definesSameMaterial(IMaterial *left, IMaterial *right) const
     }
     // Magnetic materials TODO
     else if (!left->isScalarMaterial() && !right->isScalarMaterial()) {
-        if (left->getName() == right->getName() &&
-            left->getRefractiveIndex() == right->getRefractiveIndex() ) {
+        const HomogeneousMagneticMaterial *p_left =
+                dynamic_cast<const HomogeneousMagneticMaterial *>(left);
+        const HomogeneousMagneticMaterial *p_right =
+                dynamic_cast<const HomogeneousMagneticMaterial *>(right);
+        if (!p_left || !p_right) {
+            throw Exceptions::RuntimeErrorException("LabelSample::definesSameMaterial: "
+                                                    "non-scalar materials should be of type "
+                                                    "HomogeneousMagneticMaterial");
+        }
+        if (p_left->getName() == p_right->getName() &&
+            p_left->getRefractiveIndex() == p_right->getRefractiveIndex() &&
+            p_left->getMagneticField() == p_right->getMagneticField() ) {
             return true;
         }
         return false;
-- 
GitLab