File size: 4,813 Bytes
4d03ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38d40b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "utility/utility.h"
#include <cmath>
#include <algorithm>
#include <iostream>

#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/cursorstreamwrapper.h"
#include "rapidjson/writer.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"

namespace CityFlow {

    Point operator*(const Point &A, double k) {
        return {A.x * k, A.y * k};
    }

    Point operator-(const Point &A, const Point &B) {
        return {A.x - B.x, A.y - B.y};
    }

    Point operator+(const Point &A, const Point &B) {
        return {A.x + B.x, A.y + B.y};
    }
    Point operator-(const Point &A) {
        return {-A.x, -A.y};
    }

    Point calcIntersectPoint(Point A, Point B, Point C, Point D) {
        Point P = A;
        Point Q = C;
        Vector u = B - A;
        Vector v = D - C;
        return P + u * (crossMultiply(Q - P, v) / crossMultiply(u, v));
    }


    double crossMultiply(const Point &A, const Point &B) {
        return A.x * B.y - A.y * B.x;
    }

    double dotMultiply(const Point &A, const Point &B) {
        return A.x * B.x + A.y * B.y;
    }

    double calcAng(Point A, Point B) {
        double ang = A.ang() - B.ang();
        double pi = acos(-1);
        while(ang >= pi / 2)
            ang -= pi / 2;
        while(ang < 0)
            ang += pi / 2;
        return std::min(ang, pi - ang);
    }

    bool onSegment(Point A, Point B, Point P) {
        double v1 = crossMultiply(B-A, P-A);
        double v2 = dotMultiply(P-A, P-B);
        return Point::sign(v1) == 0 && Point::sign(v2) <= 0;
    }

    Point Point::unit() {
        double l = len();
        return {x / l, y / l};
    }

    Point Point::normal() {
        return {-y, x};
    }

    Point::Point(double x, double y):x(x),y(y) { }

    double Point::len() {
        return sqrt(x * x + y * y);
    }

    double Point::ang() {
        return atan2(y, x);
    }

    int Point::sign(double x) {
        return (x + Point::eps > 0) - (x < Point::eps);
    }

    std::vector<int> generateRandomIndices(size_t n, std::mt19937 *rnd) {
        std::vector<int> randoms;
        randoms.reserve(n);
        for (size_t i = 0; i < n; ++i) {
            randoms.emplace_back(i);
        }
        std::shuffle(randoms.begin(), randoms.end(), *rnd);
        return randoms;
    }

    bool readJsonFromFile(const std::string &filename, rapidjson::Document &document) {
        FILE *fp = fopen(filename.c_str(), "r");
        if (!fp) {
            return false;
        }
        char readBuffer[JSON_BUFFER_SIZE];
        rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
        rapidjson::CursorStreamWrapper<rapidjson::FileReadStream> csw(is);
        document.ParseStream(csw);
        if (document.HasParseError()) {
            std::cerr << "Json parsing error at line " << csw.GetLine() << std::endl;
            std::cerr << rapidjson::GetParseError_En(document.GetParseError());
            std::cerr << std::endl;
            throw JsonFormatError("Json parsing error");
            return false;
        }
        fclose(fp);
        return true;
    }

    bool writeJsonToFile(const std::string &filename, const rapidjson::Document &document) {
        FILE *fp = fopen(filename.c_str(), "w");
        if (!fp) {
            return false;
        }
        char writeBuffer[JSON_BUFFER_SIZE];
        rapidjson::FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
        rapidjson::Writer<rapidjson::FileWriteStream> writer(os);
        document.Accept(writer);
        fclose(fp);
        return true;
    }

    const rapidjson::Value &getJsonMemberValue(const std::string &name, const rapidjson::Value &object) {
        assert(object.IsObject());
        auto iter = object.FindMember(name.c_str());
        if (iter == object.MemberEnd())
            throw JsonMemberMiss(name);
        return iter->value;
    }


    const rapidjson::Value &

    getJsonMemberObject(const std::string &name, const rapidjson::Value &object) {
        const auto &value = getJsonMemberValue(name, object);
        if (!value.IsObject())
            throw JsonTypeError(name, "object");
        return value;
    }

    const rapidjson::Value &

    getJsonMemberArray(const std::string &name, const rapidjson::Value &object) {
        const auto &value = getJsonMemberValue(name, object);
        if (!value.IsArray())
            throw JsonTypeError(name, "array");
        return value;
    }

    template<>
    bool jsonConvertableTo<double>(const rapidjson::Value &value) {
        //We do not differentiate 123.0 and 123
        return value.IsNumber();
    }

}