1 #include <2geom/d2.h>
2 #include <2geom/sbasis.h>
3 #include <2geom/sbasis-geometric.h>
4 #include <2geom/sbasis-2d.h>
5 #include <2geom/bezier-to-sbasis.h>
6 #include <2geom/transforms.h>
7 #include <2geom/sbasis-math.h>
8 
9 #include <toys/path-cairo.h>
10 #include <toys/toy-framework-2.h>
11 #include <2geom/path.h>
12 #include <2geom/svg-path-parser.h>
13 
14 #include <gsl/gsl_matrix.h>
15 
16 #include <vector>
17 using std::vector;
18 using namespace Geom;
19 using namespace std;
20 
21 unsigned total_pieces_sub;
22 unsigned total_pieces_inc;
23 
cairo_pw(cairo_t * cr,Piecewise<SBasis> p)24 void cairo_pw(cairo_t *cr, Piecewise<SBasis> p) {
25     for(unsigned i = 0; i < p.size(); i++) {
26         D2<SBasis> B;
27         B[0] = Linear(p.cuts[i], p.cuts[i+1]);
28         B[1] = p[i];
29         cairo_d2_sb(cr, B);
30     }
31 }
32 
draw_line(cairo_t * cr,Geom::Point n,double d)33 void draw_line(cairo_t* cr, Geom::Point n, double d) {
34     cairo_move_to(cr, d*n + rot90(n)*1000);
35     cairo_line_to(cr, d*n - rot90(n)*1000);
36     cairo_move_to(cr, d*n);
37     cairo_line_to(cr, (d+10)*n);
38 }
39 
40 class InnerProductClip: public Toy {
41     Path path_a;
42     Piecewise<D2<SBasis> >  path_a_pw;
43     std::vector<Toggle> togs;
44     PointHandle start_handle, end_handle;
45 
draw(cairo_t * cr,std::ostringstream * notify,int width,int height,bool save,std::ostringstream * timer_stream)46     void draw(cairo_t *cr, std::ostringstream *notify, int width, int height, bool save, std::ostringstream *timer_stream) {
47 	cairo_set_source_rgba (cr, 0., 0.125, 0, 1);
48 
49 	D2<Piecewise<SBasis> > B = make_cuts_independent(path_a_pw);
50 
51 	Point n;
52 	double d;
53         {
54             double x = width - 60, y = height - 60;
55             Point p(x, y), dpoint(25,25), xo(25,0), yo(0,25);
56             togs[0].bounds = Rect(p,     p + dpoint);
57             togs[1].bounds = Rect(p + xo, p + xo + dpoint);
58             draw_toggles(cr, togs);
59         }
60         if(togs[0].on) {
61             d = L2(end_handle.pos - start_handle.pos);
62             cairo_save(cr);
63             cairo_set_line_width(cr, 0.3);
64             cairo_new_sub_path(cr);
65             cairo_arc(cr, start_handle.pos[0], start_handle.pos[1], d, 0, M_PI*2);
66             cairo_stroke(cr);
67             cairo_restore(cr);
68         } else {
69             n = unit_vector(rot90(end_handle.pos - start_handle.pos));
70             d = dot(n, start_handle.pos);
71             draw_line(cr, n, d);
72         }
73         //printf("%g\n", d);
74 
75         vector<double> all_roots;
76         for(unsigned i = 0; i <= path_a.size(); i++) {
77             //deriv = p[i].derivative();
78             D2<SBasis> curpw = path_a[i].toSBasis();
79             SBasis inner;
80             if(togs[0].on) {
81                 D2<SBasis> test = curpw - start_handle.pos;
82                 inner = test[0]*test[0] + test[0]*test[1] + 2*test[1]*test[1] - d*d;
83             } else {
84                 inner = n[0]*curpw[0] + n[1]*curpw[1] - d;
85             }
86             vector<double> lr = roots(inner);
87             all_roots.insert(all_roots.end(), lr.begin(), lr.end());
88             for(unsigned i = 0; i < lr.size(); i++)
89                 draw_handle(cr, curpw(lr[i]));
90             sort(lr.begin(), lr.end());
91             lr.insert(lr.begin(), 0);
92             lr.insert(lr.end(), 1);
93             Path out;
94             for(unsigned j = 0; j < lr.size()-1; j++) {
95                 //Point s = curpw(lr[j]);
96                 Point m = curpw((lr[j] + lr[j+1])/2);
97                 if(togs[0].on)
98                     m -= start_handle.pos;
99                 //Point e = curpw(lr[j+1]);
100                 double dd;
101                 if(togs[0].on)
102                     //dd = dot(m, m) - d*d;
103                     dd = m[0]*m[0] + m[0]*m[1] + 2*m[1]*m[1] - d*d;
104                 else
105                     dd = dot(n, m) - d;
106                 if(togs[1].on)
107                     dd = -dd;
108                 //printf("%d [%g, %g] %g (%g, %g) (%g, %g)\n",
109                 //       i, lr[j], lr[j+1], dd, s[0], s[1], e[0], e[1]);
110                 if(0 > dd) {
111                     //Curve * cv = path_a[i].portion(lr[j], lr[j+1]);
112                     cairo_d2_sb(cr, portion(curpw, lr[j], lr[j+1]));
113                     cairo_set_source_rgba (cr, 0., 0.125, 0, 1);
114                     cairo_stroke(cr);
115                     /*cairo_curve(cr, path_a[i]);
116                     cairo_set_source_rgba (cr, 0., 0.125, 0, 1);
117                     cairo_stroke(cr);*/
118                 }
119 
120             }
121 	}
122 
123 	//cairo_pw_d2_sb(cr, path_a_pw);
124 	cairo_set_source_rgba (cr, 0., 0.125, 0, 1);
125 	cairo_stroke(cr);
126 
127         Toy::draw(cr, notify, width, height, save,timer_stream);
128     }
key_hit(GdkEventKey * e)129     void key_hit(GdkEventKey *e) {
130         if(e->keyval == 's') togs[1].toggle(); else
131         if(e->keyval == 'c') togs[0].toggle();
132         redraw();
133     }
mouse_pressed(GdkEventButton * e)134     void mouse_pressed(GdkEventButton* e) {
135         toggle_events(togs, e);
136         Toy::mouse_pressed(e);
137     }
first_time(int argc,char ** argv)138     void first_time(int argc, char** argv) {
139         const char *path_a_name="star.svgd";
140         if(argc > 1)
141             path_a_name = argv[1];
142         PathVector paths_a = read_svgd(path_a_name);
143         assert(paths_a.size() > 0);
144         path_a = paths_a[0];
145 
146 	path_a.close(true);
147         path_a_pw = path_a.toPwSb();
148 
149         // Finite images of the three vanishing points and the origin
150         handles.push_back(&start_handle);
151         handles.push_back(&end_handle);
152         togs.push_back(Toggle("C", true));
153         togs.push_back(Toggle("S", true));
154     }
155 public:
InnerProductClip()156     InnerProductClip() : start_handle(150,300),
157            end_handle(380,40)  {}
158 };
159 
main(int argc,char ** argv)160 int main(int argc, char **argv) {
161     init(argc, argv, new InnerProductClip);
162     return 0;
163 }
164 
165 /*
166   Local Variables:
167   mode:c++
168   c-file-style:"stroustrup"
169   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
170   indent-tabs-mode:nil
171   fill-column:99
172   End:
173 */
174 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
175