File indexing completed on 2024-04-21 03:41:37

0001 /*
0002     SPDX-FileCopyrightText: 2005 Carsten Niehaus <cniehaus@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "spectrum.h"
0008 
0009 #include "element.h"
0010 
0011 #include <KLocalizedString>
0012 #include <KUnitConversion/Converter>
0013 
0014 #include <cmath>
0015 
0016 double Spectrum::minPeak()
0017 {
0018     double value = m_peaklist.first()->wavelength;
0019 
0020     for (peak *p : std::as_const(m_peaklist)) {
0021         if (value > p->wavelength) {
0022             value = p->wavelength;
0023         }
0024     }
0025     return value;
0026 }
0027 
0028 double Spectrum::minPeak(const int unit)
0029 {
0030     return KUnitConversion::Value(minPeak(), KUnitConversion::Angstrom).convertTo(KUnitConversion::UnitId(unit)).number();
0031 }
0032 
0033 double Spectrum::maxPeak()
0034 {
0035     double value = m_peaklist.first()->wavelength;
0036 
0037     for (peak *p : std::as_const(m_peaklist)) {
0038         if (value < p->wavelength) {
0039             value = p->wavelength;
0040         }
0041     }
0042 
0043     return value;
0044 }
0045 
0046 double Spectrum::maxPeak(const int unit)
0047 {
0048     return KUnitConversion::Value(maxPeak(), KUnitConversion::Angstrom).convertTo(KUnitConversion::UnitId(unit)).number();
0049 }
0050 
0051 Spectrum *Spectrum::adjustToWavelength(double min, double max)
0052 {
0053     auto spec = new Spectrum();
0054 
0055     for (peak *p : std::as_const(m_peaklist)) {
0056         if (p->wavelength >= min || p->wavelength <= max) {
0057             spec->addPeak(p);
0058         }
0059     }
0060 
0061     return spec;
0062 }
0063 
0064 void Spectrum::adjustIntensities()
0065 {
0066     int maxInt = 0;
0067     // find the highest intensity
0068     for (peak *p : std::as_const(m_peaklist)) {
0069         if (p->intensity > maxInt) {
0070             maxInt = p->intensity;
0071         }
0072     }
0073 
0074     // check if an adjustment is needed or not
0075     if (maxInt == 1000) {
0076         return;
0077     }
0078 
0079     // now adjust the intensities.
0080     for (peak *p : std::as_const(m_peaklist)) {
0081         double newInt = p->intensity * 1000 / maxInt;
0082 
0083         p->intensity = (int)qRound(newInt);
0084     }
0085 }
0086 
0087 QList<double> Spectrum::wavelengths(double min, double max)
0088 {
0089     QList<double> list;
0090 
0091     for (peak *p : std::as_const(m_peaklist)) {
0092         if (p->wavelength >= min || p->wavelength <= max) {
0093             list.append(p->wavelength);
0094         }
0095     }
0096 
0097     return list;
0098 }
0099 
0100 int Spectrum::parentElementNumber() const
0101 {
0102     return m_parentElementNumber;
0103 }
0104 
0105 Spectrum::~Spectrum()
0106 {
0107     qDeleteAll(m_peaklist);
0108 }
0109 
0110 Spectrum::Spectrum()
0111 {
0112     // FIXME this shouldn't be hardcoded
0113     m_parentElementNumber = 16;
0114 }
0115 
0116 double Spectrum::peak::wavelengthToUnit(const int unit)
0117 {
0118     return KUnitConversion::Value(wavelength, KUnitConversion::Angstrom).convertTo(KUnitConversion::UnitId(unit)).number();
0119 }