<commit_before>#include "samplers/scalloped.h"

#include <math.h>
#include <stdint.h>

#include <algorithm>
#include <memory>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility>

#include "montecarlo.h"
#include "progressreporter.h"
#include "rng.h"
#include "tree_style_array.h"


using std::lower_bound;
using std::pair;
using std::queue;
using std::sort;
using std::unique;
using std::unordered_map;
using std::unordered_set;


namespace {


class ArcRegion : public IRegion {
public:
    ArcRegion(Dart *dart, float theta0, float theta) :
            IRegion(dart), theta0_(theta0), theta_(theta) {}

    float area() const { return theta_; }

    bool Eclipse(const Circle &circle, vector<IRegion*> *out) const {
        Vector2D c12(circle.center - dart()->position);
        float l2 = c12.LengthSquared();
        float l = sqrtf(l2);
        if (l + 1.f <= circle.radius) {
            out->clear();
            return true;
        }
        float inv_l2 = 1.f / l2;
        float r12 = inv_l2;
        float r22 = circle.radius * circle.radius * inv_l2;

        float theta = acosf((1.f + r12 - r22) * 0.5f * l);
        if (isnan(theta) || theta == 0.f) {
            return false;
        }

        float phi = atan2f(c12.y, c12.x);

        return Explode_(phi - theta, phi + theta, out);
    }

    bool Eclipse(const Line2D &line, vector<IRegion*> *out) const {
        Vector2D v(~-Normalize(line.v));

        float theta = acosf((line.p0 - dart()->position).Dot(v));
        if (isnan(theta) || theta == 0.f) {
            return false;
        }

        float phi = atan2f(v.y, v.x);

        return Explode_(phi - theta, phi + theta, out);
    }

    Point2D SelectPoint(const RNG &rng) const {
        float angle = theta0_ + rng.RandomFloat() * theta_;
        return dart()->position + Vector2D(1.f, 0.f).Rotate(angle);
    }

private:
    bool Explode_(float t1, float t2, vector<IRegion*> *out) const {
        t1 = AdjustToRange_(t1);
        t2 = AdjustToRange_(t2);
        if (t2 <= t1) {
            swap(t1, t2);
            if (t1 == theta0_) {
                return false;
            }
            out->clear();
            float tlen = min(t2 - t1, theta0_ + theta_ - t1);
            if (tlen > 0.f) {
                out->emplace_back(
                        new ArcRegion(dart(), AdjustAngle_(t1), tlen));
            }
        } else {
            float t3 = theta0_ + theta_;
            if (t3 <= t1) {
                return false;
            }
            out->clear();
            if (t1 != theta0_) {
                out->emplace_back(new ArcRegion(dart(), theta0_, t1 - theta0_));
            }
            if (t2 < t3) {
                out->emplace_back(
                        new ArcRegion(dart(), AdjustAngle_(t2), t3 - t2));
            }
        }
        return true;
    }

    static float AdjustAngle_(float angle) {
        while (angle < 0.f) {
            angle += 2.f * M_PI;
        }
        while (angle > 2.f * M_PI) {
            angle -= 2.f * M_PI;
        }
        return angle;
    }

    float AdjustToRange_(float angle) const {
        while (angle - theta0_ > theta_) {
            angle -= 2.f * M_PI;
        }
        while (angle < theta0_) {
            angle += 2.f * M_PI;
        }
        return angle;
    }

    float theta0_;
    float theta_;
};


class ArcRegionFactory : public RegionFactory {
public:
    vector<IRegion*> CreateRegions(Dart *dart) const {
        return vector<IRegion*>(1, new ArcRegion(dart, 0, 2.f * M_PI));
    }
};


class ScallopeRegion : public IRegion {
public:
    ScallopeRegion(Dart *dart, float theta0, float theta,
            Circle nearC, float nearCt0, float nearCt,
            Circle farC, float farCt0, float farCt) :
            IRegion(dart), theta0_(theta0), theta_(theta),
            nearC_(nearC), nearCt0_(nearCt0), nearCt_(nearCt),
            farC_(farC), farCt0_(farCt0), farCt_(farCt) {
            center_ = dart->position;
        }

    float area() const {
        float nearT0 = Vector2D(nearC_.center - center_).getAngle();
        float farT0 = Vector2D(farC_.center - center_).getAngle();
        float farK = isFar(farT0, farCt0_ + 0.5f * farCt_)? 1 : -1;
        float nearK = isFar(nearT0, nearCt0_ + 0.5f * nearCt_)? 1 : -1;
        return 0.5f * (getIntegral_(farC_, theta0_ + theta_, farK)
                - getIntegral_(farC_, theta0_, farK)
                - getIntegral_(nearC_, theta0_ + theta_, nearK)
                + getIntegral_(nearC_, theta0_, nearK));
    }

    bool Eclipse(const Circle &circle, vector<IRegion*> *out) const {

        /*** Insert possible new boundary of scallopes in radius ***/
        //TODO Think about angle comparisons
        printf("Ec\n");
        vector<float> angles;
        Vector2D oc(circle.center - center_);<commit_msg>fix coding style again<commit_after>#include "samplers/scalloped.h"

#include <math.h>
#include <stdint.h>

#include <algorithm>
#include <memory>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility>

#include "montecarlo.h"
#include "progressreporter.h"
#include "rng.h"
#include "tree_style_array.h"


using std::lower_bound;
using std::pair;
using std::queue;
using std::sort;
using std::unique;
using std::unordered_map;
using std::unordered_set;


namespace {


class ArcRegion : public IRegion {
public:
    ArcRegion(Dart *dart, float theta0, float theta) :
            IRegion(dart), theta0_(theta0), theta_(theta) {}

    float area() const { return theta_; }

    bool Eclipse(const Circle &circle, vector<IRegion*> *out) const {
        Vector2D c12(circle.center - dart()->position);
        float l2 = c12.LengthSquared();
        float l = sqrtf(l2);
        if (l + 1.f <= circle.radius) {
            out->clear();
            return true;
        }
        float inv_l2 = 1.f / l2;
        float r12 = inv_l2;
        float r22 = circle.radius * circle.radius * inv_l2;

        float theta = acosf((1.f + r12 - r22) * 0.5f * l);
        if (isnan(theta) || theta == 0.f) {
            return false;
        }

        float phi = atan2f(c12.y, c12.x);

        return Explode_(phi - theta, phi + theta, out);
    }

    bool Eclipse(const Line2D &line, vector<IRegion*> *out) const {
        Vector2D v(~-Normalize(line.v));

        float theta = acosf((line.p0 - dart()->position).Dot(v));
        if (isnan(theta) || theta == 0.f) {
            return false;
        }

        float phi = atan2f(v.y, v.x);

        return Explode_(phi - theta, phi + theta, out);
    }

    Point2D SelectPoint(const RNG &rng) const {
        float angle = theta0_ + rng.RandomFloat() * theta_;
        return dart()->position + Vector2D(1.f, 0.f).Rotate(angle);
    }

private:
    bool Explode_(float t1, float t2, vector<IRegion*> *out) const {
        t1 = AdjustToRange_(t1);
        t2 = AdjustToRange_(t2);
        if (t2 <= t1) {
            swap(t1, t2);
            if (t1 == theta0_) {
                return false;
            }
            out->clear();
            float tlen = min(t2 - t1, theta0_ + theta_ - t1);
            if (tlen > 0.f) {
                out->emplace_back(
                        new ArcRegion(dart(), AdjustAngle_(t1), tlen));
            }
        } else {
            float t3 = theta0_ + theta_;
            if (t3 <= t1) {
                return false;
            }
            out->clear();
            if (t1 != theta0_) {
                out->emplace_back(new ArcRegion(dart(), theta0_, t1 - theta0_));
            }
            if (t2 < t3) {
                out->emplace_back(
                        new ArcRegion(dart(), AdjustAngle_(t2), t3 - t2));
            }
        }
        return true;
    }

    static float AdjustAngle_(float angle) {
        while (angle < 0.f) {
            angle += 2.f * M_PI;
        }
        while (angle > 2.f * M_PI) {
            angle -= 2.f * M_PI;
        }
        return angle;
    }

    float AdjustToRange_(float angle) const {
        while (angle - theta0_ > theta_) {
            angle -= 2.f * M_PI;
        }
        while (angle < theta0_) {
            angle += 2.f * M_PI;
        }
        return angle;
    }

    float theta0_;
    float theta_;
};


class ArcRegionFactory : public RegionFactory {
public:
    vector<IRegion*> CreateRegions(Dart *dart) const {
        return vector<IRegion*>(1, new ArcRegion(dart, 0, 2.f * M_PI));
    }
};


class ScallopeRegion : public IRegion {
public:
    ScallopeRegion(Dart *dart, float theta0, float theta,
            Circle nearC, float nearCt0, float nearCt,
            Circle farC, float farCt0, float farCt) :
            IRegion(dart), theta0_(theta0), theta_(theta),
            nearC_(nearC), nearCt0_(nearCt0), nearCt_(nearCt),
            farC_(farC), farCt0_(farCt0), farCt_(farCt) {
        center_ = dart->position;
    }

    float area() const {
        float nearT0 = Vector2D(nearC_.center - center_).getAngle();
        float farT0 = Vector2D(farC_.center - center_).getAngle();
        float farK = isFar(farT0, farCt0_ + 0.5f * farCt_)? 1 : -1;
        float nearK = isFar(nearT0, nearCt0_ + 0.5f * nearCt_)? 1 : -1;
        return 0.5f * (getIntegral_(farC_, theta0_ + theta_, farK)
                - getIntegral_(farC_, theta0_, farK)
                - getIntegral_(nearC_, theta0_ + theta_, nearK)
                + getIntegral_(nearC_, theta0_, nearK));
    }

    bool Eclipse(const Circle &circle, vector<IRegion*> *out) const {

        /*** Insert possible new boundary of scallopes in radius ***/
        //TODO Think about angle comparisons
        printf("Ec\n");
        vector<float> angles;
        Vector2D oc(circle.center - center_);
{'fraction_non_alphanumeric': 0.12976863753213366, 'fraction_numerical': 0.024473007712082263, 'mean_word_length': 1.9259927797833936, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 59, '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': '1321334', 'n_tokens_mistral': 3466, 'n_tokens_neox': 3203, 'n_words': 784}