<commit_before>
#include "PolygonImage.h"
#include "clipper.hpp"
#include "poly2tri.h"

const static float PRECISION = 10.f;

/** Clamp a value between from and to.
 */

inline float clampf(float value, float min_inclusive, float max_inclusive)
{
    if (min_inclusive > max_inclusive) {
        std::swap(min_inclusive, max_inclusive);
    }
    return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
}

PolygonImage::PolygonImage(const QImage& image, const QRectF& rect, const float epsilon, const float threshold)
    : _width(image.width())
    , _height(image.height())
    , _threshold(threshold)
{
    _image = image.convertToFormat(QImage::Format_RGBA8888);

    QRectF realRect = rect;

    // find first bigger
    double area_big = 0;
    std::vector<QPointF> p_big;
    {
        auto p = trace(realRect, threshold);
        while (p.size() >= 3) {
            // calculate area of polygon
            std::vector<QPointF> polyPoint = p;
            if (polyPoint.size() >= 9) {
                polyPoint = reduce(polyPoint, realRect, epsilon);
            }
            if (polyPoint.size() >= 3) {
                polyPoint = expand(polyPoint, realRect, epsilon);
            }

            // erase contour for find next
            QPolygonF fillPolygon(QVector<QPointF>::fromStdVector(polyPoint));
            fillPolygon.translate(rect.x(), rect.y());
            QColor fillColor(0, 0, 0, 0);
            QPen pen(fillColor);
            pen.setWidthF(2);//qMax(epsilon, 2.f));
            QPainter painer(&_image);
            painer.setPen(pen);
            painer.setBrush(QBrush(fillColor));
            painer.setCompositionMode(QPainter::CompositionMode_Source);
            painer.drawPolygon(fillPolygon);
            painer.end();

            if (polyPoint.size() >= 3) {
                ClipperLib::Path poly;
                for(auto it = polyPoint.begin(); it<polyPoint.end(); ++it) {
                    poly << ClipperLib::IntPoint(it->x()* PRECISION, it->y() * PRECISION);
                }
                double area = fabs(ClipperLib::Area(poly));
                if (area > area_big) {
                    p_big = p;<commit_msg>move comment<commit_after>
#include "PolygonImage.h"
#include "clipper.hpp"
#include "poly2tri.h"

const static float PRECISION = 10.f;

/** Clamp a value between from and to.
 */

inline float clampf(float value, float min_inclusive, float max_inclusive)
{
    if (min_inclusive > max_inclusive) {
        std::swap(min_inclusive, max_inclusive);
    }
    return value < min_inclusive ? min_inclusive : value < max_inclusive? value : max_inclusive;
}

PolygonImage::PolygonImage(const QImage& image, const QRectF& rect, const float epsilon, const float threshold)
    : _width(image.width())
    , _height(image.height())
    , _threshold(threshold)
{
    _image = image.convertToFormat(QImage::Format_RGBA8888);

    QRectF realRect = rect;

    // find first bigger
    double area_big = 0;
    std::vector<QPointF> p_big;
    {
        auto p = trace(realRect, threshold);
        while (p.size() >= 3) {
            std::vector<QPointF> polyPoint = p;
            if (polyPoint.size() >= 9) {
                polyPoint = reduce(polyPoint, realRect, epsilon);
            }
            if (polyPoint.size() >= 3) {
                polyPoint = expand(polyPoint, realRect, epsilon);
            }

            // erase contour for find next
            QPolygonF fillPolygon(QVector<QPointF>::fromStdVector(polyPoint));
            fillPolygon.translate(rect.x(), rect.y());
            QColor fillColor(0, 0, 0, 0);
            QPen pen(fillColor);
            pen.setWidthF(2);//qMax(epsilon, 2.f));
            QPainter painer(&_image);
            painer.setPen(pen);
            painer.setBrush(QBrush(fillColor));
            painer.setCompositionMode(QPainter::CompositionMode_Source);
            painer.drawPolygon(fillPolygon);
            painer.end();

            // calculate area of polygon
            if (polyPoint.size() >= 3) {
                ClipperLib::Path poly;
                for(auto it = polyPoint.begin(); it<polyPoint.end(); ++it) {
                    poly << ClipperLib::IntPoint(it->x()* PRECISION, it->y() * PRECISION);
                }
                double area = fabs(ClipperLib::Area(poly));
                if (area > area_big) {
                    p_big = p;
{'fraction_non_alphanumeric': 0.12383760489907009, 'fraction_numerical': 0.008165116806532093, 'mean_word_length': 2.2049418604651163, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7537735', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1334, 'n_words': 288}