summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/toys/conic-5.cpp
blob: 51140c178d462953975fa2719af6e7140b856a93 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <iostream>
#include <2geom/path.h>
#include <2geom/svg-path-parser.h>
#include <2geom/path-intersection.h>
#include <2geom/basic-intersection.h>
#include <2geom/pathvector.h>
#include <2geom/exception.h>
#include <2geom/sbasis-geometric.h>
#include <2geom/path-intersection.h>
#include <2geom/nearest-time.h>
#include <2geom/line.h>
#include <2geom/bezier-to-sbasis.h>
#include <2geom/sbasis-to-bezier.h>

#include <cstdlib>
#include <map>
#include <vector>
#include <algorithm>
#include <optional>

#include <toys/path-cairo.h>
#include <toys/toy-framework-2.h>
#include <2geom/bezier-to-sbasis.h>
#include <2geom/ord.h>



#include <2geom/conicsec.h>

using namespace Geom;
using namespace std;


// File: convert.h
#include <sstream>
#include <stdexcept>

class BadConversion : public std::runtime_error {
public:
    BadConversion(const std::string& s)
        : std::runtime_error(s)
    { }
};

template <typename T>
inline std::string stringify(T x)
{
    std::ostringstream o;
    if (!(o << x))
        throw BadConversion("stringify(T)");
    return o.str();
}

void draw_hull(cairo_t*cr, RatQuad rq) {
    cairo_move_to(cr, rq.P[0]);
    cairo_line_to(cr, rq.P[1]);
    cairo_line_to(cr, rq.P[2]);
    cairo_stroke(cr);
}



void draw(cairo_t* cr, xAx C, Rect bnd) {
    if(bnd[1].extent() < 5) return;
    vector<double> prev_rts;
    double py = bnd[Y].min();
    for(int i = 0; i < 100; i++) {
        double t = i/100.;
        double y = bnd[Y].valueAt(t);
        vector<double> rts = C.roots(Point(1, 0), Point(0, y));
        int top = 0;
        for(unsigned j = 0; j < rts.size(); j++) {
            if(bnd[0].contains(rts[j])) {
                rts[top] = rts[j];
                top++;
            }
        }
        rts.erase(rts.begin()+top, rts.end());

        if(rts.size() == prev_rts.size()) {
            for(unsigned j = 0; j < rts.size(); j++) {
                cairo_move_to(cr, prev_rts[j], py);
                cairo_line_to(cr, rts[j], y);
                cairo_stroke(cr);
            }
        } else {
            draw(cr, C, Rect(bnd[X], Interval(py, y)));
        }
        prev_rts = rts;
        py = y;
    }
}

template <typename T>
static T det(T a, T b, T c, T d) {
    return a*d - b*c;
}

template <typename T>
static T det(T M[2][2]) {
    return M[0][0]*M[1][1] - M[1][0]*M[0][1];
}

template <typename T>
static T det3(T M[3][3]) {
    return ( M[0][0] * det(M[1][1], M[1][2],
                           M[2][1], M[2][2])
             -M[1][0] * det(M[0][1], M[0][2],
                            M[2][1], M[2][2])
             +M[2][0] * det(M[0][1], M[0][2],
                            M[1][1], M[1][2]));
}

double xAx_descr(xAx const & C) {
    double mC[3][3] = {{C.c[0], (C.c[1])/2, (C.c[3])/2},
                       {(C.c[1])/2, C.c[2], (C.c[4])/2},
                       {(C.c[3])/2, (C.c[4])/2, C.c[5]}};

    return det3(mC);
}

void draw_ratquad(cairo_t*cr, RatQuad rq, double tol=1e-1) {
    CubicBezier cb = rq.toCubic();
    // I tried using the nearest point to 0.5 for the error, but the improvement was negligible
    if(L2(cb.pointAt(0.5) - rq.pointAt(0.5)) > tol) {
        RatQuad a, b;
        rq.split(a, b);
        draw_ratquad(cr, a, tol);
        draw_ratquad(cr, b, tol);
    } else {
        cairo_curve(cr, cb);
        //draw_cross(cr, cb.pointAt(0));
        //draw_cross(cr, cb.pointAt(1));
    }
}


class Conic5: public Toy {
    PointSetHandle path_handles;
    PointHandle oncurve;
    PointSetHandle cutting_plane;
    std::vector<Slider> sliders;
    RectHandle rh;

    void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) override {
        cairo_set_source_rgba (cr, 0., 0., 0, 1);
        cairo_set_line_width (cr, 1);

        if(0) {
            Path path;
            path = Path(path_handles.pts[0]);
            D2<SBasis> c = handles_to_sbasis(path_handles.pts.begin(), 2);
            path.append(c);

            cairo_save(cr);
            cairo_path(cr, path);
            cairo_set_source_rgba (cr, 0., 1., 0, 0.3);
            cairo_set_line_width (cr, 3);
            cairo_stroke(cr);
            cairo_restore(cr);

            //double w = exp(sliders[0].value());
        }
        Point A = path_handles.pts[0];
        Point B = path_handles.pts[1];
        Point C = path_handles.pts[2];

        if(1) {
            QuadraticBezier qb(A, B, C);
            //double abt = qb.nearestTime(oncurve.pos);
            //oncurve.pos = qb.pointAt(abt);

            RatQuad rq = RatQuad::fromPointsTangents(A, B-A, oncurve.pos, C, B -C); //( A, B, C, w);

            cairo_save(cr);
            cairo_set_source_rgba (cr, 0., 0., 0, 1);
            cairo_set_line_width (cr, 1);
            draw_ratquad(cr, rq);
            //cairo_d2_sb(cr, rq.hermite());
            cairo_stroke(cr);
            cairo_restore(cr);
        }

        if(0) {
            RatQuad rq = RatQuad::circularArc(A, B, C);

            cairo_save(cr);
            cairo_set_source_rgba (cr, 0., 0., 0, 1);
            cairo_set_line_width (cr, 1);
            RatQuad a, b;
            rq.split(a,b);
            cairo_curve(cr, a.toCubic());
            cairo_curve(cr, b.toCubic());
            cairo_stroke(cr);
            cairo_restore(cr);
        }

        Rect screen_rect(Interval(10, width-10), Interval(10, height-10));
        Line cutLine(cutting_plane.pts[0], cutting_plane.pts[1]);
        //double dist;
        //Point norm = cutLine.normalAndDist(dist);

        const unsigned N = 3;
        xAx sources[N] = {
            xAx::fromPoint(A)*(exp(-sliders[0].value())),
            xAx::fromPoint(B)*(exp(-sliders[1].value())),
            xAx::fromPoint(C)*(exp(-sliders[2].value()))
            //xAx::fromLine(Line(A, oncurve.pos))
        };
        for(unsigned i = 0; i < N; i++) {
            //*notify << sources[i] << "\n";
        }
        for(unsigned i = 0; i < N; i++) {
            for(unsigned j = i+1; j < N; j++) {
                xAx Q = sources[i]-sources[j];
                *notify << Q << " is a " << Q.categorise() << "\n";
            }
        }
        {
            cairo_save(cr);
            cairo_set_source_rgba(cr, 0, 0, 1, 0.5);

            ::draw(cr, (sources[0]-sources[1]), screen_rect);
            ::draw(cr, (sources[0]-sources[2]), screen_rect);
            ::draw(cr, (sources[1]-sources[2]), screen_rect);
            cairo_restore(cr);
        }
        {
            string os;
            for(unsigned i = 0; i < N; i++) {
                for(unsigned j = i+1; j < N; j++) {
                    xAx Q = sources[i]-sources[j];
                    Interval iQ = Q.extrema(rh.pos);
                    if(iQ.contains(0)) {
                        os += stringify(iQ) + "\n";

                        Q.toCurve(rh.pos);
                        vector<Point> crs = Q.crossings(rh.pos);
                        for(auto & ei : crs) {
                            draw_cross(cr, ei);
                        }

                    }
                }
            }

            draw_text(cr, rh.pos.midpoint(),
                      os);
        }
        if(1) {
            xAx oxo=sources[0] - sources[2];
            Timer tm;

            tm.ask_for_timeslice();
            tm.start();

            std::vector<Point> intrs = intersect(oxo, sources[0] - sources[1]);
            Timer::Time als_time = tm.lap();
            *notify << "intersect time = " << als_time << std::endl;
            for(auto & intr : intrs) {
                cairo_save(cr);
                cairo_set_source_rgb(cr, 1, 0,0);
                draw_cross(cr, intr);
                cairo_stroke(cr);
                cairo_restore(cr);
            }

            std::optional<RatQuad> orq = oxo.toCurve(rh.pos);
            if(orq) {
                RatQuad rq = *orq;
                draw_hull(cr, rq);
                vector<SBasis> hrq = rq.homogeneous();
                SBasis vertex_poly = (sources[0] - sources[1]).evaluate_at(hrq[0], hrq[1], hrq[2]);
                //*notify << "\n0: " << hrq[0];
                //*notify << "\n1: " << hrq[1];
                //*notify << "\n2: " << hrq[2];
                vector<double> rts = roots(vertex_poly);
                //*notify << "\nvertex poly:" << vertex_poly << '\n';
                for(unsigned i = 0; i < rts.size(); i++) {
                    draw_circ(cr, Point(rq.pointAt(rts[i])));
                    *notify << "\nrq" << i << ":" << rts[i];
                }

                cairo_save(cr);
                cairo_set_source_rgb(cr, 1, 0, 0);
                RatQuad a, b;
                rq.split(a,b);
                cairo_curve(cr, a.toCubic());
                cairo_curve(cr, b.toCubic());
                cairo_stroke(cr);
                cairo_restore(cr);
            }
        }
        if(0) {
            RatQuad a, b;
            //rq.split(a,b);
            //cairo_move_to(cr, rq.toCubic().pointAt(0.5));
            cairo_line_to(cr, a.P[2]);
            cairo_stroke(cr);

            cairo_curve(cr, a.toCubic());
            cairo_curve(cr, b.toCubic());

        }
        cairo_stroke(cr);

        //*notify << "w = " << w << "; lambda = " << rq.lambda() << "\n";
        Toy::draw(cr, notify, width, height, save, timer_stream);
    }

public:
    Conic5() {
        handles.push_back(&path_handles);
        handles.push_back(&rh);
        rh.pos = Rect(Point(100,100), Point(200,200));
        rh.show_center_handle = true;
        handles.push_back(&oncurve);
        for(int j = 0; j < 3; j++){
            path_handles.push_back(uniform()*400, 100+ uniform()*300);
        }
        oncurve.pos = ((path_handles.pts[0]+path_handles.pts[1]+path_handles.pts[2])/3);
        handles.push_back(&cutting_plane);
        for(int j = 0; j < 2; j++){
            cutting_plane.push_back(uniform()*400, 100+ uniform()*300);
        }
        sliders.emplace_back(0.0, 5.0, 0, 0.0, "a");
        sliders.emplace_back(0.0, 5.0, 0, 0.0, "b");
        sliders.emplace_back(0.0, 5.0, 0, 0.0, "c");
        handles.push_back(&(sliders[0]));
        handles.push_back(&(sliders[1]));
        handles.push_back(&(sliders[2]));
        sliders[0].geometry(Point(50, 20), 250);
        sliders[1].geometry(Point(50, 50), 250);
        sliders[2].geometry(Point(50, 80), 250);
    }

    void first_time(int /*argc*/, char**/* argv*/) override {

    }
};

int main(int argc, char **argv) {
    init(argc, argv, new Conic5());
    return 0;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :