Fix float-point accuracy issue in multisize measurements dimensions.

This commit is contained in:
Roman Telezhynskyi 2023-03-23 08:39:31 +02:00
parent 2334a04fbd
commit 9b925dd996
7 changed files with 122 additions and 55 deletions

View File

@ -17,6 +17,7 @@
- Improve labels for V notch.
- Fix QT issue on MacOS version 11.0 "Big Sur".
- Fix excluding objects in internal path.
- Fix float-point accuracy issue in multisize measurements dimensions.
# Valentina 0.7.52 September 12, 2022
- Fix crash when default locale is ru.

View File

@ -184,7 +184,7 @@ void DialogDimensionLabels::InitTable()
}
{
auto *itemLabel = new QTableWidgetItem(labels.value(base));
auto *itemLabel = new QTableWidgetItem(VFuzzyValue(labels, base));
itemLabel->setData(Qt::UserRole, base);
itemLabel->setTextAlignment(Qt::AlignHCenter | Qt::AlignCenter);

View File

@ -51,7 +51,7 @@ auto FilterByMinimum(const QVector<qreal> &base, qreal restriction) -> QVector<q
filtered.reserve(base.size());
for(const auto &b : base)
{
if (b >= restriction)
if (b > restriction || VFuzzyComparePossibleNulls(b, restriction))
{
filtered.append(b);
}
@ -71,7 +71,7 @@ auto FilterByMaximum(const QVector<qreal> &base, qreal restriction) -> QVector<q
filtered.reserve(base.size());
for(const auto &b : base)
{
if (b <= restriction)
if (b < restriction || VFuzzyComparePossibleNulls(b, restriction))
{
filtered.append(b);
}
@ -85,8 +85,8 @@ void InitMinMax(qreal &min, qreal &max, const MeasurementDimension_p &dimension,
{
if (not dimension.isNull())
{
min = bases.indexOf(restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
max = bases.indexOf(restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
min = VFuzzyIndexOf(bases, restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
max = VFuzzyIndexOf(bases, restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
if (max < min)
{
@ -100,14 +100,14 @@ void InitMinMax(qreal &min, qreal &max, const MeasurementDimension_p &dimension,
void SetCellIcon(QTableWidgetItem *item, const QVector<qreal> &validRows, qreal rowValue, qreal columnValue,
const VDimensionRestriction &restriction, qreal min, qreal max)
{
if (validRows.contains(rowValue))
if (VFuzzyContains(validRows, rowValue))
{
const bool leftRestriction = columnValue >= min;
const bool rightRestriction = columnValue <= max;
const bool leftRestriction = columnValue > min || VFuzzyComparePossibleNulls(columnValue, min);
const bool rightRestriction = columnValue < max || VFuzzyComparePossibleNulls(columnValue, max);
if (leftRestriction && rightRestriction)
{
item->setIcon(QIcon(restriction.GetExcludeValues().contains(columnValue)
item->setIcon(QIcon(VFuzzyContains(restriction.GetExcludeValues(), columnValue)
? QStringLiteral("://icon/24x24/close.png")
: QStringLiteral("://icon/24x24/star.png")));
}
@ -246,7 +246,7 @@ void DialogRestrictDimension::RowSelected()
ui->comboBoxMin->blockSignals(true);
ui->comboBoxMin->clear();
QVector<qreal> filtered = FilterByMaximum(bases, restriction.GetMax());
QVector<qreal> filtered = FilterByMinimum(FilterByMaximum(bases, restriction.GetMax()), restriction.GetMin());
FillBases(filtered, dimension, ui->comboBoxMin);
int index = ui->comboBoxMin->findData(restriction.GetMin());
ui->comboBoxMin->setCurrentIndex(index != -1 ? index : 0);
@ -254,8 +254,7 @@ void DialogRestrictDimension::RowSelected()
ui->comboBoxMax->blockSignals(true);
ui->comboBoxMax->clear();
filtered = FilterByMinimum(bases, restriction.GetMin());
FillBases(FilterByMinimum(bases, restriction.GetMin()), dimension, ui->comboBoxMax);
FillBases(filtered, dimension, ui->comboBoxMax);
index = ui->comboBoxMax->findData(restriction.GetMax());
ui->comboBoxMax->setCurrentIndex(index != -1 ? index : ui->comboBoxMax->count() - 1);
ui->comboBoxMax->blockSignals(false);
@ -394,7 +393,7 @@ void DialogRestrictDimension::CellContextMenu(QPoint pos)
}
VDimensionRestriction restriction = m_restrictions.value(coordinates);
bool exclude = not restriction.GetExcludeValues().contains(columnValue);
bool exclude = not VFuzzyContains(restriction.GetExcludeValues(), columnValue);
QScopedPointer<QMenu> menu(new QMenu());
QAction *actionExclude = menu->addAction(exclude ? tr("Exclude") : tr("Include"));
@ -650,7 +649,7 @@ void DialogRestrictDimension::AddCell(int row, int column, qreal rowValue, qreal
if (m_restrictionType == RestrictDimension::First)
{
VDimensionRestriction restriction = m_restrictions.value(QChar('0'));
item->setIcon(QIcon(restriction.GetExcludeValues().contains(columnValue)
item->setIcon(QIcon(VFuzzyContains(restriction.GetExcludeValues(), columnValue)
? QStringLiteral("://icon/24x24/close.png")
: QStringLiteral("://icon/24x24/star.png")));
}
@ -732,9 +731,9 @@ void DialogRestrictDimension::FillBases(const QVector<qreal> &bases, const Measu
{
for(auto base : bases)
{
if (labels.contains(base) && not labels.value(base).isEmpty())
if (VFuzzyContains(labels, base) && not VFuzzyValue(labels, base).isEmpty())
{
control->addItem(labels.value(base), base);
control->addItem(VFuzzyValue(labels, base), base);
}
else
{
@ -746,9 +745,9 @@ void DialogRestrictDimension::FillBases(const QVector<qreal> &bases, const Measu
{
for(auto base : bases)
{
if (labels.contains(base) && not labels.value(base).isEmpty())
if (VFuzzyContains(labels, base) && not VFuzzyValue(labels, base).isEmpty())
{
control->addItem(labels.value(base), base);
control->addItem(VFuzzyValue(labels, base), base);
}
else
{
@ -767,9 +766,9 @@ void DialogRestrictDimension::FillBases(const QVector<qreal> &bases, const Measu
{
for(auto base : bases)
{
if (labels.contains(base) && not labels.value(base).isEmpty())
if (VFuzzyContains(labels, base) && not VFuzzyValue(labels, base).isEmpty())
{
control->addItem(labels.value(base), base);
control->addItem(VFuzzyValue(labels, base), base);
}
else
{
@ -791,9 +790,9 @@ auto DialogRestrictDimension::FillDimensionXBases(const QVector<qreal> &bases,
for(auto base : bases)
{
if (dimensionLabels.contains(base) && not dimensionLabels.value(base).isEmpty())
if (VFuzzyContains(dimensionLabels, base) && not VFuzzyValue(dimensionLabels, base).isEmpty())
{
labels.append(dimensionLabels.value(base));
labels.append(VFuzzyValue(dimensionLabels, base));
}
else
{
@ -816,9 +815,9 @@ auto DialogRestrictDimension::FillDimensionYBases(const QVector<qreal> &bases,
for(auto base : bases)
{
if (dimensionLabels.contains(base) && not dimensionLabels.value(base).isEmpty())
if (VFuzzyContains(dimensionLabels, base) && not VFuzzyValue(dimensionLabels, base).isEmpty())
{
labels.append(dimensionLabels.value(base));
labels.append(VFuzzyValue(dimensionLabels, base));
}
else
{
@ -848,9 +847,9 @@ auto DialogRestrictDimension::FillDimensionWZBases(const QVector<qreal> &bases,
for(auto base : bases)
{
if (dimensionLabels.contains(base) && not dimensionLabels.value(base).isEmpty())
if (VFuzzyContains(dimensionLabels, base) && not VFuzzyValue(dimensionLabels, base).isEmpty())
{
labels.append(dimensionLabels.value(base));
labels.append(VFuzzyValue(dimensionLabels, base));
}
else
{
@ -904,8 +903,8 @@ auto DialogRestrictDimension::DimensionRestrictedValues(const MeasurementDimensi
const QVector<qreal> bases = dimension->ValidBases();
qreal min = bases.indexOf(restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
qreal max = bases.indexOf(restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
qreal min = VFuzzyIndexOf(bases, restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
qreal max = VFuzzyIndexOf(bases, restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
if (min > max)
{
@ -935,7 +934,7 @@ auto DialogRestrictDimension::StartRow() const -> int
for(int i=0; i < basesRow.size(); ++i)
{
if (validRows.contains(basesRow.at(i)))
if (VFuzzyContains(validRows, basesRow.at(i)))
{
return i;
}

View File

@ -142,9 +142,9 @@ void InitDimensionXItems(const QVector<qreal> &bases, const DimesionLabels &labe
for(auto base : bases)
{
if (labels.contains(base) && not labels.value(base).isEmpty())
if (VFuzzyContains(labels, base) && not VFuzzyValue(labels, base).isEmpty())
{
control->addItem(labels.value(base), base);
control->addItem(VFuzzyValue(labels, base), base);
}
else
{
@ -159,9 +159,9 @@ void InitDimensionYWZItems(const QVector<qreal> &bases, const DimesionLabels &la
{
for(auto base : bases)
{
if (labels.contains(base) && not labels.value(base).isEmpty())
if (VFuzzyContains(labels, base) && not VFuzzyValue(labels, base).isEmpty())
{
control->addItem(labels.value(base), base);
control->addItem(VFuzzyValue(labels, base), base);
}
else
{
@ -2844,9 +2844,10 @@ void TMainWindow::InitDimensionsBaseValue()
DimesionLabels labels = dimension->Labels();
if (labels.contains(dimension->BaseValue()) && not labels.value(dimension->BaseValue()).isEmpty())
if (VFuzzyContains(labels, dimension->BaseValue())
&& not VFuzzyValue(labels, dimension->BaseValue()).isEmpty())
{
base->setText(labels.value(dimension->BaseValue()));
base->setText(VFuzzyValue(labels, dimension->BaseValue()));
}
else
{
@ -4300,8 +4301,8 @@ auto TMainWindow::DimensionRestrictedValues(int index, const MeasurementDimensio
const QVector<qreal> bases = dimension->ValidBases();
qreal min = bases.indexOf(restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
qreal max = bases.indexOf(restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
qreal min = VFuzzyIndexOf(bases, restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
qreal max = VFuzzyIndexOf(bases, restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
if (min > max)
{

View File

@ -2335,14 +2335,14 @@ void MainWindow::StoreMultisizeMDimension(const QList<MeasurementDimension_p> &d
case MeasurementDimension::X:
VAbstractValApplication::VApp()->SetDimensionHeight(currentBase);
VAbstractValApplication::VApp()->SetDimensionHeightLabel(
labels.value(currentBase, QString::number(currentBase)));
VFuzzyValue(labels, currentBase, QString::number(currentBase)));
break;
case MeasurementDimension::Y:
{
const bool fc = m_m->IsFullCircumference();
VAbstractValApplication::VApp()->SetDimensionSize(fc ? currentBase*2 : currentBase);
VAbstractValApplication::VApp()->SetDimensionSizeLabel(
labels.value(currentBase, QString::number(fc ? currentBase*2 : currentBase)));
VFuzzyValue(labels, currentBase, QString::number(fc ? currentBase*2 : currentBase)));
const bool measurement = dimension->IsBodyMeasurement();
VAbstractValApplication::VApp()
->SetDimensionSizeUnits(measurement ? m_m->Units() : Unit::LAST_UNIT_DO_NOT_USE);
@ -2353,7 +2353,7 @@ void MainWindow::StoreMultisizeMDimension(const QList<MeasurementDimension_p> &d
const bool fc = m_m->IsFullCircumference();
VAbstractValApplication::VApp()->SetDimensionWaist(fc ? currentBase*2 : currentBase);
VAbstractValApplication::VApp()->SetDimensionWaistLabel(
labels.value(currentBase, QString::number(fc ? currentBase*2 : currentBase)));
VFuzzyValue(labels, currentBase, QString::number(fc ? currentBase*2 : currentBase)));
break;
}
case MeasurementDimension::Z:
@ -2361,7 +2361,7 @@ void MainWindow::StoreMultisizeMDimension(const QList<MeasurementDimension_p> &d
const bool fc = m_m->IsFullCircumference();
VAbstractValApplication::VApp()->SetDimensionHip(fc ? currentBase*2 : currentBase);
VAbstractValApplication::VApp()->SetDimensionHipLabel(
labels.value(currentBase, QString::number(fc ? currentBase*2 : currentBase)));
VFuzzyValue(labels, currentBase, QString::number(fc ? currentBase*2 : currentBase)));
break;
}
default:
@ -2419,8 +2419,8 @@ auto MainWindow::DimensionRestrictedValues(int index, const MeasurementDimension
const QVector<qreal> bases = dimension->ValidBases();
qreal min = bases.indexOf(restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
qreal max = bases.indexOf(restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
qreal min = VFuzzyIndexOf(bases, restriction.GetMin()) != -1 ? restriction.GetMin() : dimension->MinValue();
qreal max = VFuzzyIndexOf(bases, restriction.GetMax()) != -1 ? restriction.GetMax() : dimension->MaxValue();
if (min > max)
{
@ -4943,9 +4943,9 @@ void MainWindow::InitDimensionXGradation(const QVector<qreal> &bases, const Dime
for(auto base : bases)
{
if (labels.contains(base) && not labels.value(base).isEmpty())
if (VFuzzyContains(labels, base) && not VFuzzyValue(labels, base).isEmpty())
{
control->addItem(labels.value(base), base);
control->addItem(VFuzzyValue(labels, base), base);
}
else
{
@ -4963,9 +4963,9 @@ void MainWindow::InitDimensionYWZGradation(const QVector<qreal> &bases, const Di
for(auto base : bases)
{
if (labels.contains(base) && not labels.value(base).isEmpty())
if (VFuzzyContains(labels, base) && not VFuzzyValue(labels, base).isEmpty())
{
control->addItem(labels.value(base), base);
control->addItem(VFuzzyValue(labels, base), base);
}
else
{

View File

@ -110,8 +110,8 @@ auto VAbstartMeasurementDimension::Name() const -> QString
//---------------------------------------------------------------------------------------------------------------------
auto VAbstartMeasurementDimension::ValidSteps() const -> QVector<qreal>
{
const qreal stepBarrier = 8.5;
const qreal s = 0.5;
const qreal stepBarrier = 50;
const qreal s = 0.1;
QVector<qreal> steps;
steps.reserve(qRound((stepBarrier - s) * 2 - 1));
@ -124,6 +124,7 @@ auto VAbstartMeasurementDimension::ValidSteps() const -> QVector<qreal>
else if (diff > 0)
{
qreal candidate = 1;
int i = 1;
do
{
const qreal step = (m_units == Unit::Mm ? candidate * 10 : candidate);
@ -132,7 +133,8 @@ auto VAbstartMeasurementDimension::ValidSteps() const -> QVector<qreal>
{
steps.append(step);
}
candidate += s;
candidate = 1 + s * i;
++i;
}
while(candidate < stepBarrier);
}
@ -179,23 +181,27 @@ auto VAbstartMeasurementDimension::ValidBases(qreal min, qreal max, qreal step,
validBases.reserve(qRound((max - min) / step));
qreal value = min;
int i = 1;
do
{
if (not exclude.contains(value))
if (not VFuzzyContains(exclude, value))
{
validBases.append(value);
}
value += step;
value = min + step * i;
++i;
}
while(value < max + step);
if (validBases.isEmpty())
{
value = min;
int i = 1;
do
{
validBases.append(value);
value += step;
value = min + step * i;
++i;
}
while(value < max + step);
}
@ -222,7 +228,7 @@ auto VAbstartMeasurementDimension::IsRangeValid() const -> bool
//---------------------------------------------------------------------------------------------------------------------
auto VAbstartMeasurementDimension::IsStepValid() const -> bool
{
bool valid = ValidSteps().indexOf(m_step) != -1;
bool valid = VFuzzyIndexOf(ValidSteps(), m_step) != -1;
if (not valid)
{
m_error = QCoreApplication::translate("VAbstartMeasurementDimension", "Invalid step");
@ -234,7 +240,7 @@ auto VAbstartMeasurementDimension::IsStepValid() const -> bool
//---------------------------------------------------------------------------------------------------------------------
auto VAbstartMeasurementDimension::IsBaseValid() const -> bool
{
bool valid = ValidBases().indexOf(m_baseValue) != -1;
bool valid = VFuzzyIndexOf(ValidBases(), m_baseValue) != -1;
if (not valid)
{
m_error = QCoreApplication::translate("VAbstartMeasurementDimension", "Base value invalid");

View File

@ -53,6 +53,66 @@ template <class T> class QSharedPointer;
using MeasurementDimension_p = QSharedPointer<VAbstartMeasurementDimension>;
using DimesionLabels = QMap<qreal, QString>;
//---------------------------------------------------------------------------------------------------------------------
template <typename T>
inline bool VFuzzyContains(const QMap<qreal, T> &c, qreal value)
{
auto i = c.constBegin();
while (i != c.constEnd())
{
if (VFuzzyComparePossibleNulls(i.key(), value))
{
return true;
}
++i;
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------
template <template <typename> class Cont>
inline bool VFuzzyContains(const Cont<qreal> &c, qreal value)
{
for (auto val : c)
{
if (VFuzzyComparePossibleNulls(val, value))
{
return true;
}
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------
template <template <typename> class Cont>
inline vsizetype VFuzzyIndexOf(const Cont<qreal> &c, qreal value)
{
for (int i = 0; i < c.size(); ++i)
{
if (VFuzzyComparePossibleNulls(c.at(i), value))
{
return i;
}
}
return -1;
}
//---------------------------------------------------------------------------------------------------------------------
template <typename T>
inline T VFuzzyValue(const QMap<qreal, T> &c, qreal value, const T &defaultValue = T())
{
auto i = c.constBegin();
while (i != c.constEnd())
{
if (VFuzzyComparePossibleNulls(i.key(), value))
{
return i.value();
}
++i;
}
return defaultValue;
}
class VAbstartMeasurementDimension
{
Q_DECLARE_TR_FUNCTIONS(VAbstartMeasurementDimension) // NOLINT