summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/toys/collinear-normal.cpp
blob: 60638f41a9f91ea63f69e24ae98dfb10eb712df7 (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
/*
 * Show off collinear normals between two Bezier curves.
 * The intersection points are found by using Bezier clipping.
 *
 * Authors:
 *      Marco Cecchetti <mrcekets at gmail.com>
 *
 * Copyright 2008  authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */

#include <2geom/d2.h>
#include <2geom/basic-intersection.h>
#include <2geom/sbasis-to-bezier.h>
#include <2geom/ray.h>

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

using namespace Geom;



class CurveIntersect : public Toy
{

    void draw( cairo_t *cr, std::ostringstream *notify,
               int width, int height, bool save, std::ostringstream *timer_stream) override
    {
        m_width = width;
        m_height = height;
        m_length = (m_width > m_height) ? m_width : m_height;
        m_length *= 2;

        cairo_set_line_width (cr, 0.3);
        cairo_set_source_rgba (cr, 0.8, 0., 0, 1);
        D2<SBasis> A = pshA.asBezier();
        cairo_d2_sb(cr, A);
        cairo_stroke(cr);
        cairo_set_source_rgba (cr, 0.0, 0., 0, 1);
        D2<SBasis> B = pshB.asBezier();
        cairo_d2_sb(cr, B);
        cairo_stroke(cr);
        draw_text(cr, A.at0(), "A");
        draw_text(cr, B.at0(), "B");

        Timer tm;
        tm.ask_for_timeslice();
        tm.start();
        
        find_collinear_normal(xs, pshA.pts, pshB.pts, m_precision);
        Timer::Time als_time = tm.lap();
        *timer_stream << "find_collinear_normal " << als_time << std::endl;
        cairo_set_line_width (cr, 0.3);
        cairo_set_source_rgba (cr, 0.0, 0.0, 0.7, 1);
        for (auto & x : xs)
        {
            Point At = A(x.first);
            Point Bu = B(x.second);
            draw_axis(cr, At, Bu);
            draw_handle(cr, At);
            draw_handle(cr, Bu);

        }
        cairo_stroke(cr);
        
        double h_a_t = 0, h_b_t = 0;

        double h_dist = hausdorfl( A, B, m_precision, &h_a_t, &h_b_t);
        {
            Point At = A(h_a_t);
            Point Bu = B(h_b_t);
            cairo_move_to(cr, At);
            cairo_line_to(cr, Bu);
            draw_handle(cr, At);
            draw_handle(cr, Bu);
            cairo_save(cr);
            cairo_set_line_width (cr, 1);
            cairo_set_source_rgba (cr, 0.7, 0.0, 0.0, 1);
            cairo_stroke(cr);
            cairo_restore(cr);
        }
        /*h_dist = hausdorf( A, B, m_precision, &h_a_t, &h_b_t);
        {
            Point At = A(h_a_t);
            Point Bu = B(h_b_t);
            draw_axis(cr, At, Bu);
            draw_handle(cr, At);
            draw_handle(cr, Bu);
            cairo_save(cr);
            cairo_set_line_width (cr, 0.3);
            cairo_set_source_rgba (cr, 0.0, 0.7, 0.0, 1);
            cairo_stroke(cr);
            cairo_restore(cr);
            }*/
        *notify << "Hausdorf distance = " << h_dist 
                << "occurring at " << h_a_t 
                << " B=" << h_b_t << std::endl;

        Toy::draw(cr, notify, width, height, save,timer_stream);
    }

    void draw_segment(cairo_t* cr, Point const& p1, Point const&  p2)
    {
        cairo_move_to(cr, p1);
        cairo_line_to(cr, p2);
    }

    void draw_segment(cairo_t* cr, Point const& p1, double angle, double length)
    {
        Point p2;
        p2[X] = length * std::cos(angle);
        p2[Y] = length * std::sin(angle);
        p2 += p1;
        draw_segment(cr, p1, p2);
    }

    void draw_ray(cairo_t* cr, Ray const& r)
    {
        double angle = r.angle();
        draw_segment(cr, r.origin(), angle, m_length);
    }

    void draw_axis(cairo_t* cr, Point const& p1, Point const&  p2)
    {
        double d = Geom::distance(p1, p2);
        d = d + d/4;
        Point q1 = Ray(p1, p2).pointAt(d);
        Point q2 = Ray(p2, p1).pointAt(d);
        draw_segment(cr, q1, q2);
    }

public:
    CurveIntersect(unsigned int _A_bez_ord, unsigned int _B_bez_ord)
        : A_bez_ord(_A_bez_ord), B_bez_ord(_B_bez_ord)
    {
        handles.push_back(&pshA);
        for (unsigned int i = 0; i <= A_bez_ord; ++i)
            pshA.push_back(Geom::Point(uniform()*400, uniform()*400)+Point(200,200));
        handles.push_back(&pshB);
        for (unsigned int i = 0; i <= B_bez_ord; ++i)
            pshB.push_back(Geom::Point(uniform()*400, uniform()*400)+Point(200,200));

        m_precision = 1e-6;
    }

private:
    unsigned int A_bez_ord, B_bez_ord;
    PointSetHandle pshA, pshB, pshC;
    std::vector< std::pair<double, double> > xs;
    double m_precision;
    double m_width, m_height, m_length;
};


int main(int argc, char **argv)
{
    unsigned int A_bez_ord = 4;
    unsigned int B_bez_ord = 6;
    if(argc > 1)
        sscanf(argv[1], "%d", &A_bez_ord);
    if(argc > 2)
        sscanf(argv[2], "%d", &B_bez_ord);


    init( argc, argv, new CurveIntersect(A_bez_ord, B_bez_ord), 800, 800);
    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 :