summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/toys/pair-intersect.cpp
blob: 9cc01caaab112ac9d7a7e2eeb2bd1dd8a58c60a8 (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
#include <2geom/basic-intersection.h>
#include <2geom/d2.h>
#include <2geom/sbasis.h>
#include <2geom/bezier-to-sbasis.h>
#include <2geom/path-intersection.h>

#include <toys/path-cairo.h>
#include <toys/toy-framework-2.h>

using std::vector;
using namespace Geom;

class PairIntersect: public Toy {
    PointSetHandle A_handles;
    PointSetHandle B_handles;
    std::vector<Toggle> toggles;
    void mouse_pressed(GdkEventButton* e) override {
        toggle_events(toggles, e);
        Toy::mouse_pressed(e);
    }
    void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) override {
    
        draw_toggles(cr, toggles);
        cairo_save(cr);
    cairo_set_source_rgba(cr, 0, 0, 0, 1.0);
    cairo_set_line_width (cr, 0.5);
    D2<SBasis> A = A_handles.asBezier();
    cairo_d2_sb(cr, A);
    cairo_stroke(cr);
    cairo_set_source_rgba(cr, 0.0, 0, 0.8, 1.0);
    cairo_set_line_width (cr, 0.5);
    D2<SBasis> B = B_handles.asBezier();
    cairo_d2_sb(cr, B);
    cairo_stroke(cr);
    
    cairo_save(cr);
    cairo_set_source_rgba(cr, 0, 0, 0, 1.0);
    cairo_set_line_width (cr, 0.5);
    SBasis crs (cross(A - A(0), derivative(A)));
    crs = shift(crs*Linear(-1, 0)*Linear(-1, 0), -2);
    crs = crs * (300/(*bounds_exact(crs)).extent());
    vector<double> rts = roots(crs);
    for(double t : rts) {
        cairo_move_to(cr, A(0));
        cairo_line_to(cr, A(t));
        cairo_stroke(cr);
    }
    cairo_restore(cr);
    cairo_move_to(cr, 0, 300);
    cairo_line_to(cr, width, 300);
    crs += 300;
    D2<SBasis > are_graph(SBasis(Linear(0, width)), crs );
    cairo_save(cr);
    cairo_d2_sb(cr, are_graph);
    cairo_set_line_width (cr, .5);
    cairo_set_source_rgba (cr, 0., 0., 0., 1);
    cairo_stroke(cr);
    cairo_restore(cr);
    
    Path PB;
    PB.append(B);
    Path PA;
    PA.append(A);
        
    if (toggles[0].on) {
        PathVector ps;
        ps.push_back(PA);
        ps.push_back(PB);
        CrossingSet cs = crossings_among(ps);
        *notify << "total intersections: " << cs.size() << '\n';
        cairo_stroke(cr);
        cairo_set_source_rgba (cr, 1., 0., 0, 0.8);
        for(unsigned i = 0; i < cs.size(); i++) {
            Crossings section = cs[i];
            *notify << "section " << i << ": " << section.size() << '\n';
            for(auto & j : section) {
                draw_handle(cr, A(j.ta));
                *notify << Geom::distance(A(j.ta), B(j.tb)) 
                        << std::endl;
            }
        }

        cairo_stroke(cr);
    } else {
        vector<Geom::Point> Ab = A_handles.pts, Bb = B_handles.pts;
        std::vector<std::pair<double, double> > section;
        find_intersections( section, A, B);
        std::vector<std::pair<double, double> > polished_section = section;
        *notify << "total intersections: " << section.size();
        polish_intersections( polished_section, A, B);
        cairo_stroke(cr);
        cairo_set_source_rgba (cr, 1., 0., 0, 0.8);
        for(unsigned i = 0; i < section.size(); i++) {
            draw_handle(cr, A(section[i].first));
            *notify << Geom::distance(A(section[i].first), B(section[i].second)) 
                    << " polished "
                    << Geom::distance(A(polished_section[i].first), B(polished_section[i].second)) 
                    << std::endl;
        }

        cairo_stroke(cr);
    }
    cairo_restore(cr);


    Toy::draw(cr, notify, width, height, save,timer_stream);
}
public:
    PairIntersect (unsigned A_bez_ord, unsigned B_bez_ord) {
        toggles.emplace_back("Path", true);
        toggles[0].bounds = Rect(Point(10,100), Point(100, 130));
        //toggles.push_back(Toggle("Curve", true));
        //toggles[1].bounds = Rect(Point(10,130), Point(100, 160));
        handles.push_back(&A_handles);
        handles.push_back(&B_handles);
        A_handles.name = "A";
        B_handles.name = "B";
    for(unsigned i = 0; i < A_bez_ord; i++)
        A_handles.push_back(uniform()*400, uniform()*400);
    for(unsigned i = 0; i < B_bez_ord; i++)
        B_handles.push_back(uniform()*400, uniform()*400);
}
};

int main(int argc, char **argv) {
unsigned A_bez_ord=10;
unsigned B_bez_ord=3;
    if(argc > 2)
        sscanf(argv[2], "%d", &B_bez_ord);
    if(argc > 1)
        sscanf(argv[1], "%d", &A_bez_ord);
    init(argc, argv, new PairIntersect(A_bez_ord, B_bez_ord));

    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=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :