summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/2geom/src/toys/sbasisdim.cpp
blob: 3804090366e8fd84145af3e5ccd4a21ca190df47 (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
#include <iostream>
#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>

#include <2geom/orphan-code/linearN.h>
#include <2geom/orphan-code/sbasisN.h>

using namespace Geom;
using namespace std;


struct Frame
{
    Geom::Point O;
    Geom::Point x;
    Geom::Point y;
    Geom::Point z;
};

void
plot3d(cairo_t *cr, double x, double y, double z, Frame frame){
    Point p;
    for (unsigned dim=0; dim<2; dim++){
        p[dim] = x*frame.x[dim] + y*frame.y[dim] + z*frame.z[dim];
        p[dim] += frame.O[dim];
    }
    draw_cross(cr, p);
}
void
plot3d(cairo_t *cr, SBasis const &x, SBasis const &y, SBasis const &z, Frame frame){
    D2<SBasis> curve;
    for (unsigned dim=0; dim<2; dim++){
        curve[dim] = x*frame.x[dim] + y*frame.y[dim] + z*frame.z[dim];
        curve[dim] += frame.O[dim];
    }
    cairo_d2_sb(cr, curve);
}

void
plot3d(cairo_t *cr, LinearN<2> const &f, Frame frame){
    int iMax = 5;
    for (int i=0; i<iMax; i++){
        double t = i/(iMax-1.);
        plot3d(cr, Linear(0,1), Linear(t), toLinear(f.partialEval(t, 1)), frame);
        plot3d(cr, Linear(t), Linear(0,1), toLinear(f.partialEval(t, 0)), frame);
    }
}
void
plot3d(cairo_t *cr, SBasisN<2> const &f, Frame frame){
    int iMax = 5;
    for (int i=0; i<iMax; i++){
        double t = i/(iMax-1.);
        plot3d(cr, Linear(0,1), Linear(t), toSBasis(f.partialEval(t, 1)), frame);
        plot3d(cr, Linear(t), Linear(0,1), toSBasis(f.partialEval(t, 0)), frame);
    }
}
void
dot_plot3d(cairo_t *cr, SBasisN<2> const &f, Frame frame){
    int iMax = 15;
    double t[2];
    for (int i=0; i<iMax; i++){
        t[0] = i/(iMax-1.);
        for (int j=0; j<iMax; j++){
            t[1] = j/(iMax-1.);
            plot3d(cr, t[0], t[1], f.valueAt(t), frame);
        }
    }
}


class SBasisDimToy: public Toy {
    PointSetHandle hand;

    void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) override {

        double slider_top = width/4.;
        double slider_bot = width*3./4.;
        double slider_margin = width/8.;
        if(hand.pts.empty()) {
            hand.pts.emplace_back(width*3./16., 3*width/4.);
            hand.pts.push_back(hand.pts[0] + Geom::Point(width/2., 0));
            hand.pts.push_back(hand.pts[0] + Geom::Point(width/8., -width/12.));
            hand.pts.push_back(hand.pts[0] + Geom::Point(0,-width/4.));
            hand.pts.emplace_back(slider_margin,slider_bot);
            hand.pts.emplace_back(width-slider_margin,slider_top);
        }
        
        hand.pts[4][X] = slider_margin;
        if (hand.pts[4][Y]<slider_top) hand.pts[4][Y] = slider_top; 
        if (hand.pts[4][Y]>slider_bot) hand.pts[4][Y] = slider_bot; 
        hand.pts[5][X] = width-slider_margin;
        if (hand.pts[5][Y]<slider_top) hand.pts[5][Y] = slider_top; 
        if (hand.pts[5][Y]>slider_bot) hand.pts[5][Y] = slider_bot; 

        double tA = (slider_bot-hand.pts[4][Y])/(slider_bot-slider_top);
        double tB = (slider_bot-hand.pts[5][Y])/(slider_bot-slider_top);

        cairo_move_to(cr,Geom::Point(slider_margin,slider_bot));
        cairo_line_to(cr,Geom::Point(slider_margin,slider_top));
        cairo_move_to(cr,Geom::Point(width-slider_margin,slider_bot));
        cairo_line_to(cr,Geom::Point(width-slider_margin,slider_top));
        cairo_set_line_width(cr,.5);
        cairo_set_source_rgba (cr, 0., 0.3, 0., 1.);
        cairo_stroke(cr);
        
        Frame frame;
        frame.O = hand.pts[0];//
        frame.x = hand.pts[1]-hand.pts[0];//
        frame.y = hand.pts[2]-hand.pts[0];//
        frame.z = hand.pts[3]-hand.pts[0];// 

        plot3d(cr,Linear(0,1),Linear(0,0),Linear(0,0),frame);
        plot3d(cr,Linear(0,1),Linear(1,1),Linear(0,0),frame);
        plot3d(cr,Linear(0,0),Linear(0,1),Linear(0,0),frame);
        plot3d(cr,Linear(1,1),Linear(0,1),Linear(0,0),frame);
        cairo_set_line_width(cr,.2);
        cairo_set_source_rgba (cr, 0., 0., 0., 1.);
        cairo_stroke(cr);

        SBasisN<1> t = LinearN<1>(0,1);

        LinearN<2> u,v;
        setToVariable(u,0);
        setToVariable(v,1);
        SBasisN<2> f, x = u, y = v; //x,y are used for conversion :-(

        
//--------------------
//Basic MultiDegree<2> tests...
//--------------------
#if 0
        unsigned sizes[2];
        sizes[0] = 4;
        sizes[1] = 3;
        MultiDegree<2> d0;
        d0.p[0]=3;
        d0.p[1]=2;
        std::cout<<"(3,2)->"<< d0.asIdx(sizes) <<"\n";
        MultiDegree<2> d1(11,sizes);
        std::cout<<"11->"<< d1.p[0] <<","<<d1.p[1] <<"\n";
#endif
        
//--------------------
//Basic LinearN tests
//--------------------
#if 0
        plot3d(cr, u, frame);
        cairo_set_line_width(cr,1);        
        cairo_set_source_rgba (cr, .75, 0., 0., 1.);
        cairo_stroke(cr);
        plot3d(cr, v, frame);
        cairo_set_line_width(cr,1);        
        cairo_set_source_rgba (cr, 0., 0., 0.75, 1.);
        cairo_stroke(cr);
#endif

//--------------------
//Basic SBasisN tests
//--------------------
#if 1
        f  = x*x + y*y;//(x-one*.5)*(x-one*.5);
        std::cout<<"\nf: "<<f<<"\n";
        std::cout<<"Degrees:\n";
        std::cout<<"quick_deg: "<< f.quick_degree(0)<<", "<<f.quick_degree(1)<<"\n";
        std::cout<<"real s_deg: "<< f.degree(0)<<", "<<f.degree(1)<<"\n";
        std::cout<<"real t_deg: "<< f.real_t_degree(0)<<", "<<f.real_t_degree(1)<<"\n";
        plot3d(cr, f, frame);
        cairo_set_line_width(cr,1);        
        cairo_set_source_rgba (cr, 0., 0.75, 0., 1.);
        cairo_stroke(cr);
#endif

//--------------------
// SBasisOf<SBasisOf<double> > simulation tests
//--------------------
#if 1
        SBasisN<1> y1d = LinearN<1>(0,1);
        SBasisN<2> g,g1;
        g.appendCoef(LinearN<1>(0.), y1d*y1d , 0);
        g.appendCoef(y1d + 1, y1d, 0);
        g1 = x*y*y + x*(-x+1)*( (-x+1)*(y+1) + x*y );
        plot3d(cr, g-g1, frame);
        cairo_set_line_width(cr,1);        
        cairo_set_source_rgba (cr, 0., 0.75, 0., 1.);
        cairo_stroke(cr);
#endif
//--------------------
// SBasisN composition tests
//--------------------
#if 1
        SBasisN<1> z;
        std::vector<SBasisN<1> > var;
        t -=.5;
        var.push_back( t*t + tA);
        var.push_back( (t+.3)*t*(t-.3) + tB);
        z = compose(f,var);
        cairo_set_line_width(cr,1);
        plot3d(cr, toSBasis(var[0]), toSBasis(var[1]), Linear(0.), frame);
        cairo_set_source_rgba (cr, 0., 0., 0.75, 1.);
        cairo_stroke(cr);
        plot3d(cr, toSBasis(var[0]), toSBasis(var[1]), toSBasis(z), frame);
        cairo_set_source_rgba (cr, 0.75, 0., 0., 1.);
        cairo_stroke(cr);
#endif

//--------------------
//Some timing. TODO: Compare to SBasisOf<SBasisOf<double> >
//--------------------
#if 0
        double units = 1e6;
        std::string units_string("us");
        double timer_precision = 0.1;
        clock_t end_t = clock()+clock_t(timer_precision*CLOCKS_PER_SEC);
        // Base loop to remove overhead
        end_t = clock()+clock_t(timer_precision*CLOCKS_PER_SEC);
        long iterations = 0;
        while(end_t > clock()) {
            iterations++;
        }
        double overhead = timer_precision*units/iterations;

        end_t = clock()+clock_t(timer_precision*CLOCKS_PER_SEC);
        iterations = 0;
        while(end_t > clock()) {
            f.valueAt(t);
            iterations++;
        }
        *notify << "recursive eval: " 
                << ", time = " << timer_precision*units/iterations-overhead 
                << units_string << std::endl;
#endif

        Toy::draw(cr, notify, width, height, save,timer_stream);
    }        
    
public:
    SBasisDimToy(){
        handles.push_back(&hand);
    }
};

int main(int argc, char **argv) {
    init(argc, argv, new SBasisDimToy);
    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 :