1 #include <2geom/d2.h>
2 #include <2geom/sbasis.h>
3 #include <2geom/sbasis-2d.h>
4 #include <2geom/bezier-to-sbasis.h>
5 #include <2geom/transforms.h>
6 #include <2geom/pathvector.h>
7 #include <2geom/svg-path-parser.h>
8 
9 #include <toys/path-cairo.h>
10 #include <toys/toy-framework-2.h>
11 
12 #include <vector>
13 using std::vector;
14 using namespace Geom;
15 
16 unsigned total_pieces_sub;
17 unsigned total_pieces_inc;
18 
19 class Sb2d2: public Toy {
20     Path path_a;
21     D2<SBasis2d> sb2;
22     Piecewise<D2<SBasis> >  path_a_pw;
23     PointSetHandle hand;
24 
draw(cairo_t * cr,std::ostringstream * notify,int width,int height,bool save,std::ostringstream * timer_stream)25     void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) {
26         Geom::Point dir(1,-2);
27         for(unsigned dim = 0; dim < 2; dim++) {
28             Geom::Point dir(0,0);
29             dir[dim] = 1;
30             for(unsigned vi = 0; vi < sb2[dim].vs; vi++)
31                 for(unsigned ui = 0; ui < sb2[dim].us; ui++)
32                     for(unsigned iv = 0; iv < 2; iv++)
33                         for(unsigned iu = 0; iu < 2; iu++) {
34                             unsigned corner = iu + 2*iv;
35                             unsigned i = ui + vi*sb2[dim].us;
36                             Geom::Point base((2*(iu+ui)/(2.*ui+1)+1)*width/4.,
37                                              (2*(iv+vi)/(2.*vi+1)+1)*width/4.);
38                             if(vi == 0 && ui == 0) {
39                                 base = Geom::Point(width/4., width/4.);
40                             }
41                             double dl = dot((hand.pts[corner+4*i] - base), dir)/dot(dir,dir);
42                             sb2[dim][i][corner] = dl/(width/2)*pow(4.0,(double)ui+vi);
43                         }
44         }
45         cairo_d2_sb2d(cr, sb2, dir*0.1, width);
46         cairo_set_source_rgba (cr, 0., 0., 0, 0.5);
47         cairo_stroke(cr);
48         for(unsigned i = 0; i < path_a_pw.size(); i++) {
49             D2<SBasis> B = path_a_pw[i];
50             //const int depth = sb2[0].us*sb2[0].vs;
51             //const int surface_hand.pts = 4*depth;
52             //D2<SBasis> B = hand.pts_to_sbasis<3>(hand.pts.begin() + surface_hand.pts);
53             cairo_d2_sb(cr, B);
54             for(unsigned dim = 0; dim < 2; dim++) {
55                 std::vector<double> r = roots(B[dim]);
56                 for(unsigned i = 0; i < r.size(); i++)
57                     draw_cross(cr, B(r[i]));
58                 r = roots(Linear(width/4) - B[dim]);
59                 for(unsigned i = 0; i < r.size(); i++)
60                     draw_cross(cr, B(r[i]));
61             }
62             cairo_set_source_rgba (cr, 0., 0.125, 0, 1);
63             cairo_stroke(cr);
64             B *= (4./width);
65             D2<SBasis> tB = compose_each(sb2, B);
66             B = B*(width/2) + Geom::Point(width/4, width/4);
67             tB = tB*(width/2) + Geom::Point(width/4, width/4);
68 
69             cairo_d2_sb(cr, tB);
70         }
71 
72         //*notify << "bo = " << sb2.index(0,0);
73 
74         Toy::draw(cr, notify, width, height, save,timer_stream);
75     }
first_time(int argc,char ** argv)76     void first_time(int argc, char** argv) {
77         const char *path_a_name="star.svgd";
78         if(argc > 1)
79             path_a_name = argv[1];
80         PathVector paths_a = read_svgd(path_a_name);
81         assert(!paths_a.empty());
82         path_a = paths_a[0];
83         Rect bounds = path_a[0].boundsFast();
84         std::cout << bounds.min() <<std::endl;
85         path_a = path_a * Affine(Translate(-bounds.min()));
86         double extreme = std::max(bounds.width(), bounds.height());
87         path_a = path_a * Scale(40./extreme);
88 
89         path_a_pw = path_a.toPwSb();
90         for(unsigned dim = 0; dim < 2; dim++) {
91             sb2[dim].us = 2;
92             sb2[dim].vs = 2;
93             const int depth = sb2[dim].us*sb2[dim].vs;
94             sb2[dim].resize(depth, Linear2d(0));
95         }
96 
97         hand.pts.resize(sb2[0].vs*sb2[0].us*4);
98         handles.push_back(&hand);
99 
100     }
resize_canvas(Geom::Rect const & s)101     virtual void resize_canvas(Geom::Rect const & s) {
102         double width = s[0].extent();
103         unsigned ii = 0;
104         for(unsigned vi = 0; vi < sb2[0].vs; vi++)
105             for(unsigned ui = 0; ui < sb2[0].us; ui++)
106                 for(unsigned iv = 0; iv < 2; iv++)
107                     for(unsigned iu = 0; iu < 2; iu++)
108                         hand.pts[ii++] = Geom::Point((2*(iu+ui)/(2.*ui+1)+1)*width/4.,
109                                                     (2*(iv+vi)/(2.*vi+1)+1)*width/4.);
110 
111     }
112 };
113 
main(int argc,char ** argv)114 int main(int argc, char **argv) {
115     init(argc, argv, new Sb2d2);
116     return 0;
117 }
118 
119 /*
120   Local Variables:
121   mode:c++
122   c-file-style:"stroustrup"
123   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
124   indent-tabs-mode:nil
125   fill-column:99
126   End:
127 */
128 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
129