Skip to content

Commit 7c3295f

Browse files
committed
Improvements and fixes in deviation highlighting feature:
1. Threshold is coupled to mean value. 2. Fixed value calculation on the selection content stage. 3. Deviation of the hard margins values is highlighted on the margins stage.
1 parent c0941c9 commit 7c3295f

File tree

6 files changed

+49
-33
lines changed

6 files changed

+49
-33
lines changed

src/app/SettingsDialog.ui

+5-2
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,14 @@
398398
<property name="toolTip">
399399
<string>The minimum deviation to be highlighted.</string>
400400
</property>
401+
<property name="decimals">
402+
<number>1</number>
403+
</property>
401404
<property name="maximum">
402-
<double>45.000000000000000</double>
405+
<double>999.000000000000000</double>
403406
</property>
404407
<property name="singleStep">
405-
<double>0.100000000000000</double>
408+
<double>1.000000000000000</double>
406409
</property>
407410
</widget>
408411
</item>

src/core/DeviationProvider.h

+27-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DeviationProvider {
1818

1919
explicit DeviationProvider(const std::function<double(const K&)>& computeValueByKey);
2020

21-
bool isDeviant(const K& key, double coefficient = 1.0, double threshold = 0.0) const;
21+
bool isDeviant(const K& key, double coefficient = 1.0, double threshold = 0.0, bool defaultVal = false) const;
2222

2323
double getDeviationValue(const K& key) const;
2424

@@ -51,27 +51,38 @@ DeviationProvider<K, Hash>::DeviationProvider(const std::function<double(const K
5151
: m_computeValueByKey(computeValueByKey) {}
5252

5353
template <typename K, typename Hash>
54-
bool DeviationProvider<K, Hash>::isDeviant(const K& key, const double coefficient, const double threshold) const {
54+
bool DeviationProvider<K, Hash>::isDeviant(const K& key, double coefficient, double threshold, bool defaultVal) const {
5555
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
5656
return false;
5757
}
5858
if (m_keyValueMap.size() < 3) {
5959
return false;
6060
}
6161

62+
double value = m_keyValueMap.at(key);
63+
if (std::isnan(value)) {
64+
return defaultVal;
65+
}
66+
6267
update();
63-
return (std::abs(m_keyValueMap.at(key) - m_meanValue) > std::max((coefficient * m_standardDeviation), threshold));
68+
return (std::abs(value - m_meanValue)
69+
> std::max((coefficient * m_standardDeviation), (threshold / 100) * m_meanValue));
6470
}
6571

6672
template <typename K, typename Hash>
6773
double DeviationProvider<K, Hash>::getDeviationValue(const K& key) const {
6874
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
69-
return .0;
75+
return -1.0;
7076
}
7177
if (m_keyValueMap.size() < 2) {
7278
return .0;
7379
}
7480

81+
double value = m_keyValueMap.at(key);
82+
if (std::isnan(value)) {
83+
return -1.0;
84+
}
85+
7586
update();
7687
return std::abs(m_keyValueMap.at(key) - m_meanValue);
7788
}
@@ -97,7 +108,6 @@ void DeviationProvider<K, Hash>::remove(const K& key) {
97108
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
98109
return;
99110
}
100-
101111
m_keyValueMap.erase(key);
102112
}
103113

@@ -110,20 +120,26 @@ void DeviationProvider<K, Hash>::update() const {
110120
return;
111121
}
112122

123+
int count = 0;
113124
{
114125
double sum = .0;
115-
for (const std::pair<K, double>& keyAndValue : m_keyValueMap) {
116-
sum += keyAndValue.second;
126+
for (const auto& [key, value] : m_keyValueMap) {
127+
if (!std::isnan(value)) {
128+
sum += value;
129+
count++;
130+
}
117131
}
118-
m_meanValue = sum / m_keyValueMap.size();
132+
m_meanValue = sum / count;
119133
}
120134

121135
{
122136
double differencesSum = .0;
123-
for (const std::pair<K, double>& keyAndValue : m_keyValueMap) {
124-
differencesSum += std::pow(keyAndValue.second - m_meanValue, 2);
137+
for (const auto& [key, value] : m_keyValueMap) {
138+
if (!std::isnan(value)) {
139+
differencesSum += std::pow(value - m_meanValue, 2);
140+
}
125141
}
126-
m_standardDeviation = std::sqrt(differencesSum / (m_keyValueMap.size() - 1));
142+
m_standardDeviation = std::sqrt(differencesSum / (count - 1));
127143
}
128144

129145
m_needUpdate = false;

src/core/filters/deskew/Settings.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ Settings::Settings() {
1616
if (it != m_perPageParams.end()) {
1717
const Params& params = it->second;
1818
return params.deskewAngle();
19-
} else {
20-
return .0;
21-
};
19+
}
20+
return NAN;
2221
});
2322
}
2423

src/core/filters/page_layout/Settings.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,14 @@ Settings::Impl::Impl()
351351
m_autoMarginsDefault(false),
352352
m_showMiddleRect(true) {
353353
m_deviationProvider.setComputeValueByKey([this](const PageId& pageId) -> double {
354-
auto it(m_items.find(pageId));
354+
auto it = m_items.find(pageId);
355355
if (it != m_items.end()) {
356-
if (it->alignment.isNull()) {
357-
return std::sqrt(it->hardWidthMM() * it->hardHeightMM() / 4 / 25.4);
358-
} else {
359-
return .0;
360-
}
361-
} else {
362-
return .0;
363-
};
356+
const Margins& marginsMm = it->hardMarginsMM;
357+
double horHardMargins = marginsMm.left() + marginsMm.right();
358+
double vertHardMargins = marginsMm.top() + marginsMm.bottom();
359+
return std::sqrt(std::pow(horHardMargins, 2) * std::pow(vertHardMargins, 2));
360+
}
361+
return NAN;
364362
});
365363
}
366364

src/core/filters/select_content/CacheDrivenTask.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ void CacheDrivenTask::process(const PageInfo& pageInfo,
5353
const double deviationThreshold = settings.getSelectContentDeviationThreshold();
5454

5555
if (auto* thumbCol = dynamic_cast<ThumbnailCollector*>(collector)) {
56-
thumbCol->processThumbnail(std::unique_ptr<QGraphicsItem>(
57-
new Thumbnail(thumbCol->thumbnailCache(), thumbCol->maxLogicalThumbSize(), pageInfo.imageId(), xform,
58-
params->contentRect(), params->pageRect(), params->pageDetectionMode() != MODE_DISABLED,
59-
m_settings->deviationProvider().isDeviant(pageInfo.id(), deviationCoef, deviationThreshold))));
56+
thumbCol->processThumbnail(std::unique_ptr<QGraphicsItem>(new Thumbnail(
57+
thumbCol->thumbnailCache(), thumbCol->maxLogicalThumbSize(), pageInfo.imageId(), xform, params->contentRect(),
58+
params->pageRect(), params->pageDetectionMode() != MODE_DISABLED,
59+
m_settings->deviationProvider().isDeviant(pageInfo.id(), deviationCoef, deviationThreshold, true))));
6060
}
6161
} // CacheDrivenTask::process
6262
} // namespace select_content

src/core/filters/select_content/Settings.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Settings::Settings() : m_pageDetectionBox(0.0, 0.0), m_pageDetectionTolerance(0.
1818
if (it != m_pageParams.end()) {
1919
const Params& params = it->second;
2020
const QSizeF& contentSizeMM = params.contentSizeMM();
21-
return std::sqrt(contentSizeMM.width() * contentSizeMM.height() / 4 / 25.4);
22-
} else {
23-
return .0;
24-
};
21+
if (!contentSizeMM.toSize().isEmpty())
22+
return std::sqrt(std::pow(contentSizeMM.width(), 2) + std::pow(contentSizeMM.height(), 2));
23+
}
24+
return NAN;
2525
});
2626
}
2727

0 commit comments

Comments
 (0)