Skip to content
Snippets Groups Projects
Commit 14782434 authored by Johannes Junggeburth's avatar Johannes Junggeburth :dog2:
Browse files

Introduce GeoMaterial sorter helper struct

parent f6aa8bbb
No related branches found
No related tags found
1 merge request!412Query is nothing else than a std::optional but just came earlier
/*
Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
*/
#ifndef GeoModelHelpers_GeoMaterialSorter_H
#define GeoModelHelpers_GeoMaterialSorter_H
#include "GeoModelKernel/GeoMaterial.h"
#include "GeoModelKernel/GeoElement.h"
#include "GeoModelKernel/GeoIntrusivePtr.h"
#include <set>
/// @brief Helper struct to deuplicate equivalent materials with different naming
struct GeoMaterialSorter {
/** @brief Comparison operator returing whether Material A is smaller than Material B */
bool operator()(const GeoMaterial* a, const GeoMaterial* b) const;
/** @brief Comparison operator returing whether the Element A is smaller than Element B */
bool operator()(const GeoElement* a, const GeoElement* b) const;
/// @brief Compares 2 GeoMaterials
/// @param a : Pointer to material A
/// @param b : Pointer to material B
/// @return Returns 0 if no defining material property could be found that differs
/// Returns -1 if the first defining & differing property of A is smaller
/// Returns 1 otherwise
int compare(const GeoMaterial* a, const GeoMaterial* b) const;
int compare(const GeoElement* a, const GeoElement* b) const;
};
/// @brief
/// @tparam ShapeType
using GeoMaterialSet = std::set<GeoIntrusivePtr<const GeoMaterial>, GeoMaterialSorter>;
#endif
\ No newline at end of file
/*
Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
*/
#include "GeoModelHelpers/GeoMaterialSorter.h"
namespace {
// Tolerance in density to treat both material equally
constexpr double equivTol = 1.e-6;
}
bool GeoMaterialSorter::operator()(const GeoMaterial* a, const GeoMaterial* b) const{
return compare(a,b) < 0;
}
bool GeoMaterialSorter::operator()(const GeoElement* a, const GeoElement* b) const{
return compare(a,b) < 0;
}
int GeoMaterialSorter::compare(const GeoMaterial* a, const GeoMaterial* b) const {
if (a == b) {
return 0;
}
if (a->getNumElements() != b->getNumElements()) {
return a->getNumElements() < b->getNumElements();
}
const double densityCmp = a->getDensity() - b->getDensity();
if (std::abs(densityCmp) > equivTol) {
return densityCmp < 0. ? -1 : 1;
}
/// Assume sorting of elements by fraction
for (unsigned e = 0; e < a->getNumElements(); ++e) {
const double fracComp = a->getFraction(e) - b->getFraction(e);
if (std::abs(fracComp) > equivTol) {
return fracComp < 0.? -1: 1;
}
const int eleCmp = compare(a->getElement(e), b->getElement(e));
if (eleCmp) {
return eleCmp;
}
}
return 0;
}
int GeoMaterialSorter::compare(const GeoElement* a, const GeoElement* b) const {
if (a == b) {
return 0;
}
if (a->getZ() != b->getZ()) {
return a->getZ() < b->getZ() ? -1 : 1;
}
if (a->getA() != b->getA()) {
return a->getA() < b->getA() ? -1 : 1;
}
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment