summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/toys/bounds-test.cpp
blob: f6001628a7c277c3bee67b182695aea54109da22 (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
#include <2geom/d2.h>
#include <2geom/sbasis.h>
#include <2geom/bezier-to-sbasis.h>

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

#include <time.h>
#include <vector>
using std::vector;
using namespace Geom;
using namespace std;

static void plot(cairo_t* cr, SBasis const &B,double vscale=1,double a=0,double b=1){
    D2<SBasis> plot;
    plot[0]=SBasis(Linear(150+a*300,150+b*300));
    plot[1]=B*(-vscale);
    plot[1]+=300;
    cairo_d2_sb(cr, plot);
    cairo_stroke(cr);
}
static void plot_bar(cairo_t* cr, double height, double vscale=1,double a=0,double b=1){
    cairo_move_to(cr, Geom::Point(150+300*a,-height*vscale+300));
    cairo_line_to(cr, Geom::Point(150+300*b,-height*vscale+300));
    cairo_stroke(cr);
}

class BoundsTester: public Toy {
    unsigned size;
    PointSetHandle hand;
    void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) override {
        
        for (unsigned i=0;i<size;i++){
            hand.pts[i    ][0]=150+15*(i-size);
            hand.pts[i+size][0]=450+15*(i+1);
            cairo_move_to(cr, Geom::Point(hand.pts[i    ][0],150));
            cairo_line_to(cr, Geom::Point(hand.pts[i    ][0],450));
            cairo_move_to(cr, Geom::Point(hand.pts[i+size][0],150));
            cairo_line_to(cr, Geom::Point(hand.pts[i+size][0],450));
        }
        cairo_move_to(cr, Geom::Point(0,300));
        cairo_line_to(cr, Geom::Point(600,300));
        
        cairo_set_line_width (cr, 1);
        cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 1);
        cairo_stroke(cr);
        
        SBasis B(size, Linear());
        for (unsigned i=0;i<size;i++){
            B[i] = Linear(-(hand.pts[i     ][1]-300)*std::pow(4.,(int)i),
                          -(hand.pts[i+size][1]-300)*std::pow(4.,(int)i) );
        }
        B.normalize();
        plot(cr,B,1);   
        cairo_set_source_rgba (cr, 0., 0., 0.8, 1);
        cairo_stroke(cr);

        Interval bnds = *bounds_local(B,Interval(0.,.5));
        plot_bar(cr,bnds.min(),1,.0,.5);
        plot_bar(cr,bnds.max(),1,.0,.5);
        cairo_set_source_rgba (cr, 0.4, 0., 0., 1);
        cairo_stroke(cr);
        bnds = *bounds_exact(B);
        plot_bar(cr,bnds.min());
        plot_bar(cr,bnds.max());
        cairo_set_source_rgba (cr, 0.9, 0., 0., 1);
        cairo_stroke(cr);
        
/*
This is a multi-root test...
*/
        hand.pts[2*size  ][0]=150;
        hand.pts[2*size+1][0]=150;
        hand.pts[2*size+2][0]=150;
        hand.pts[2*size  ][1]=std::max(hand.pts[2*size  ][1],hand.pts[2*size+1][1]);
        hand.pts[2*size+1][1]=std::max(hand.pts[2*size+1][1],hand.pts[2*size+2][1]);
        vector<double> levels;
        levels.push_back((300-hand.pts[2*size  ][1]));
        levels.push_back((300-hand.pts[2*size+1][1]));
        levels.push_back((300-hand.pts[2*size+2][1]));
        for (double level : levels) plot_bar(cr,level);
        
        cairo_set_source_rgba (cr, 0., 0.5, 0., 1);
        
        *notify<<"Use hand.pts to set the coefficients of the s-basis."<<std::endl;
        
        vector<double>my_roots;
        
//         cairo_set_source_rgba (cr, 0.9, 0., 0.8, 1);
//         for (unsigned i=0;i<levels.size();i++){
//             my_roots.clear();
//             my_roots=roots(B-Linear(levels[i]));
//             for(unsigned j=0;j<my_roots.size();j++){
//                 draw_cross(cr,Point(150+300*my_roots[j],300-levels[i]));
//             }
//         }

//         cairo_set_source_rgba (cr, 0.9, 0., 0.8, 1);

        vector<vector<double> > sols=multi_roots(B,levels,.001,.001);
        //map<double,unsigned> sols=multi_roots(B,levels);
        //for(map<double,unsigned>::iterator sol=sols.begin();sol!=sols.end();sol++){
        //    draw_handle(cr,Point(150+300*(*sol).first,300-levels[(*sol).second]));
        //}

        for (unsigned i=0;i<sols.size();i++){
            for (unsigned j=0;j<sols[i].size();j++){
                draw_handle(cr,Point(150+300*sols[i][j],300-levels[i]));
            }
        }
        cairo_set_source_rgba (cr, 0.9, 0., 0.8, 1);

/*
        
        clock_t end_t;
        unsigned iterations = 0;
        
        my_roots.clear();
        end_t = clock()+clock_t(0.1*CLOCKS_PER_SEC);
        iterations = 0;
        while(end_t > clock()) {
            my_roots.clear();
            for (unsigned i=0;i<levels.size();i++){
                my_roots=roots(B-Linear(levels[i]));
            }
            iterations++;
        }
        *notify << 1000*0.1/iterations <<" ms = roots time"<< std::endl;
        
        sols.clear();
        end_t = clock()+clock_t(0.1*CLOCKS_PER_SEC);
        iterations = 0;
        while(end_t > clock()) {
            sols.clear();
            sols=multi_roots(B,levels);
            iterations++;
        }
        *notify << 1000*0.1/iterations <<" ms = multi roots time"<< std::endl;
*/               
        Toy::draw(cr, notify, width, height, save,timer_stream);
    }        
    
public:
    BoundsTester(){
        size=5;
        if(hand.pts.empty()) {
            for(unsigned i = 0; i < 2*size; i++)
                hand.pts.emplace_back(0,150+150+uniform()*300*0);
        }
        hand.pts.emplace_back(150,300+ 50+uniform()*100);
        hand.pts.emplace_back(150,300- 50+uniform()*100);
        hand.pts.emplace_back(150,300-150+uniform()*100);
        handles.push_back(&hand);
    }
};

int main(int argc, char **argv) {
    init(argc, argv, new BoundsTester);
    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:encoding = utf-8:textwidth = 99 :