Skip to content

Commit

Permalink
Improvements and fixes in deviation highlighting feature:
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
4lex4 committed Feb 2, 2020
1 parent c0941c9 commit 7c3295f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
7 changes: 5 additions & 2 deletions src/app/SettingsDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,14 @@
<property name="toolTip">
<string>The minimum deviation to be highlighted.</string>
</property>
<property name="decimals">
<number>1</number>
</property>
<property name="maximum">
<double>45.000000000000000</double>
<double>999.000000000000000</double>
</property>
<property name="singleStep">
<double>0.100000000000000</double>
<double>1.000000000000000</double>
</property>
</widget>
</item>
Expand Down
38 changes: 27 additions & 11 deletions src/core/DeviationProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DeviationProvider {

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

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

double getDeviationValue(const K& key) const;

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

template <typename K, typename Hash>
bool DeviationProvider<K, Hash>::isDeviant(const K& key, const double coefficient, const double threshold) const {
bool DeviationProvider<K, Hash>::isDeviant(const K& key, double coefficient, double threshold, bool defaultVal) const {
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
return false;
}
if (m_keyValueMap.size() < 3) {
return false;
}

double value = m_keyValueMap.at(key);
if (std::isnan(value)) {
return defaultVal;
}

update();
return (std::abs(m_keyValueMap.at(key) - m_meanValue) > std::max((coefficient * m_standardDeviation), threshold));
return (std::abs(value - m_meanValue)
> std::max((coefficient * m_standardDeviation), (threshold / 100) * m_meanValue));
}

template <typename K, typename Hash>
double DeviationProvider<K, Hash>::getDeviationValue(const K& key) const {
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
return .0;
return -1.0;
}
if (m_keyValueMap.size() < 2) {
return .0;
}

double value = m_keyValueMap.at(key);
if (std::isnan(value)) {
return -1.0;
}

update();
return std::abs(m_keyValueMap.at(key) - m_meanValue);
}
Expand All @@ -97,7 +108,6 @@ void DeviationProvider<K, Hash>::remove(const K& key) {
if (m_keyValueMap.find(key) == m_keyValueMap.end()) {
return;
}

m_keyValueMap.erase(key);
}

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

int count = 0;
{
double sum = .0;
for (const std::pair<K, double>& keyAndValue : m_keyValueMap) {
sum += keyAndValue.second;
for (const auto& [key, value] : m_keyValueMap) {
if (!std::isnan(value)) {
sum += value;
count++;
}
}
m_meanValue = sum / m_keyValueMap.size();
m_meanValue = sum / count;
}

{
double differencesSum = .0;
for (const std::pair<K, double>& keyAndValue : m_keyValueMap) {
differencesSum += std::pow(keyAndValue.second - m_meanValue, 2);
for (const auto& [key, value] : m_keyValueMap) {
if (!std::isnan(value)) {
differencesSum += std::pow(value - m_meanValue, 2);
}
}
m_standardDeviation = std::sqrt(differencesSum / (m_keyValueMap.size() - 1));
m_standardDeviation = std::sqrt(differencesSum / (count - 1));
}

m_needUpdate = false;
Expand Down
5 changes: 2 additions & 3 deletions src/core/filters/deskew/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ Settings::Settings() {
if (it != m_perPageParams.end()) {
const Params& params = it->second;
return params.deskewAngle();
} else {
return .0;
};
}
return NAN;
});
}

Expand Down
16 changes: 7 additions & 9 deletions src/core/filters/page_layout/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,14 @@ Settings::Impl::Impl()
m_autoMarginsDefault(false),
m_showMiddleRect(true) {
m_deviationProvider.setComputeValueByKey([this](const PageId& pageId) -> double {
auto it(m_items.find(pageId));
auto it = m_items.find(pageId);
if (it != m_items.end()) {
if (it->alignment.isNull()) {
return std::sqrt(it->hardWidthMM() * it->hardHeightMM() / 4 / 25.4);
} else {
return .0;
}
} else {
return .0;
};
const Margins& marginsMm = it->hardMarginsMM;
double horHardMargins = marginsMm.left() + marginsMm.right();
double vertHardMargins = marginsMm.top() + marginsMm.bottom();
return std::sqrt(std::pow(horHardMargins, 2) * std::pow(vertHardMargins, 2));
}
return NAN;
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/filters/select_content/CacheDrivenTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ void CacheDrivenTask::process(const PageInfo& pageInfo,
const double deviationThreshold = settings.getSelectContentDeviationThreshold();

if (auto* thumbCol = dynamic_cast<ThumbnailCollector*>(collector)) {
thumbCol->processThumbnail(std::unique_ptr<QGraphicsItem>(
new Thumbnail(thumbCol->thumbnailCache(), thumbCol->maxLogicalThumbSize(), pageInfo.imageId(), xform,
params->contentRect(), params->pageRect(), params->pageDetectionMode() != MODE_DISABLED,
m_settings->deviationProvider().isDeviant(pageInfo.id(), deviationCoef, deviationThreshold))));
thumbCol->processThumbnail(std::unique_ptr<QGraphicsItem>(new Thumbnail(
thumbCol->thumbnailCache(), thumbCol->maxLogicalThumbSize(), pageInfo.imageId(), xform, params->contentRect(),
params->pageRect(), params->pageDetectionMode() != MODE_DISABLED,
m_settings->deviationProvider().isDeviant(pageInfo.id(), deviationCoef, deviationThreshold, true))));
}
} // CacheDrivenTask::process
} // namespace select_content
8 changes: 4 additions & 4 deletions src/core/filters/select_content/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Settings::Settings() : m_pageDetectionBox(0.0, 0.0), m_pageDetectionTolerance(0.
if (it != m_pageParams.end()) {
const Params& params = it->second;
const QSizeF& contentSizeMM = params.contentSizeMM();
return std::sqrt(contentSizeMM.width() * contentSizeMM.height() / 4 / 25.4);
} else {
return .0;
};
if (!contentSizeMM.toSize().isEmpty())
return std::sqrt(std::pow(contentSizeMM.width(), 2) + std::pow(contentSizeMM.height(), 2));
}
return NAN;
});
}

Expand Down

0 comments on commit 7c3295f

Please sign in to comment.