~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/covira/snake_pick.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include <tina/all_tina.h>
  2 #include <tina/brain.h>
  3 #include <tina/brainfuncs.h>
  4 
  5 static double scale = 16.0;
  6 
  7 static Vec2 snake_closest(Snake * snake, Vec2 p)
  8 {
  9     int i;
 10     double d, dmin = 1e10;
 11     Vec2 pmin;
 12     if (snake == NULL)
 13         return (p);
 14     for (i = 0; i < snake->n; i++)
 15         if ((d = vec2_dist(p, snake->p[i])) < dmin)
 16         {
 17             dmin = d;
 18             pmin = snake->p[i];
 19         }
 20     return (pmin);
 21 }
 22 
 23 static double pusher(Vec2 p, Vec2 centre, double scale)
 24 {
 25     double s;
 26 
 27     s = PI * vec2_dist(centre, p) / scale;
 28     if (s < PI)
 29         return (1.0 + cos(s));
 30     else
 31         return (0.0);
 32 }
 33 
 34 static Vec2 centre, plast;
 35 static void snake_edit_down(Tv * tv, Ipos pos)
 36 {
 37     Snake *snake = ims_snake_get();
 38 
 39     if (snake == NULL)
 40         return;
 41 
 42     plast = tv_backproj2(tv, pos);
 43     centre = snake_centroid(snake);
 44     scale = 2.0 * vec2_dist(plast, snake_closest(snake, plast));
 45     scale = MAX(1.0, scale);
 46 
 47     tv_set_overlay(tv);
 48     snake_draw(tv, snake);
 49 }
 50 
 51 static void snake_push_drag(Tv * tv, Ipos pos)
 52 {
 53     Vec2 dp, p;
 54     double gmax;
 55     int i, n;
 56     Snake *snake = ims_snake_get();
 57 
 58     if (snake == NULL)
 59         return;
 60 
 61     p = tv_backproj2(tv, pos);
 62     dp = vec2_diff(p, plast);
 63     plast = p;
 64 
 65     n = snake->n;
 66     snake_draw(tv, snake);
 67     gmax = 0.0;
 68     for (i = 0; i < n; ++i)
 69     {
 70         Vec2 q;
 71         double g;
 72 
 73         q = snake->p[i];
 74         g = pusher(q, p, scale);
 75         gmax = MAX(gmax, g);
 76     }
 77     if (gmax == 0.0)
 78         gmax = 1.0;
 79     for (i = 0; i < n; ++i)
 80     {
 81         Vec2 q;
 82         double g;
 83 
 84         q = snake->p[i];
 85         g = pusher(q, p, scale) / gmax;
 86         q = vec2_sum(q, vec2_times(g, dp));
 87         snake->p[i] = q;
 88     }
 89     snake_draw(tv, snake);
 90 }
 91 
 92 static void snake_push_up(Tv * tv, Ipos pos)
 93 {
 94     Snake *snake = ims_snake_get();
 95 
 96     if (snake == NULL)
 97         return;
 98 
 99     snake_push_drag(tv, pos);
100     ims_snake_changed();
101     tv_reset_draw(tv);
102     tv_repaint(tv);
103 }
104 
105 static void snake_shift_drag(Tv * tv, Ipos pos)
106 {
107     Snake *snake = ims_snake_get();
108     Vec2 p, dp;
109 
110     if (snake == NULL)
111         return;
112 
113     p = tv_backproj2(tv, pos);
114     dp = vec2_diff(p, plast);
115     plast = p;
116     snake_shift(snake, dp);
117     snake_draw(tv, snake);
118 }
119 
120 static void snake_shift_up(Tv * tv, Ipos pos)
121 {
122     Snake *snake = ims_snake_get();
123 
124     if (snake == NULL)
125         return;
126 
127     snake_shift_drag(tv, pos);
128     ims_snake_changed();
129     tv_reset_draw(tv);
130     tv_repaint(tv);
131 }
132 
133 static void snake_zoom_drag(Tv * tv, Ipos pos)
134 {
135     int i, n;
136     Snake *snake = ims_snake_get();
137     Vec2 p;
138     double zoom, theta;
139     Mat2 r;
140 
141     if (snake == NULL)
142         return;
143 
144     p = tv_backproj2(tv, pos);
145     theta = vec2_angle(vec2_diff(plast, centre), vec2_diff(p, centre));
146     zoom = vec2_dist(p, centre) / vec2_dist(plast, centre);
147     plast = p;
148 
149     r = rot2(theta);
150     n = snake->n;
151     snake_draw(tv, snake);
152     for (i = 0; i < n; ++i)
153     {
154         Vec2 q;
155 
156         q = snake->p[i];
157         q = vec2_sum(centre,
158                      vec2_times(zoom,
159                                 mat2_vprod(r, vec2_diff(q, centre))));
160         snake->p[i] = q;
161     }
162     snake_draw(tv, snake);
163 }
164 
165 static void snake_zoom_up(Tv * tv, Ipos pos)
166 {
167     Snake *snake = ims_snake_get();
168 
169     if (snake == NULL)
170         return;
171 
172     snake_zoom_drag(tv, pos);
173     ims_snake_changed();
174     tv_reset_draw(tv);
175     tv_repaint(tv);
176 }
177 
178 Tv_mouse snake_edit_mouse(void)
179 {
180     return (mouse_define(MOUSE_NAME, "snake push",
181                          LEFT_NAME, "push",
182                          LEFT_DOWN, snake_edit_down,
183                          LEFT_DRAG, snake_push_drag,
184                          LEFT_UP, snake_push_up,
185                          MIDDLE_NAME, "shift",
186                          MIDDLE_DOWN, snake_edit_down,
187                          MIDDLE_DRAG, snake_shift_drag,
188                          MIDDLE_UP, snake_shift_up,
189                          RIGHT_NAME, "zoom",
190                          RIGHT_DOWN, snake_edit_down,
191                          RIGHT_DRAG, snake_zoom_drag,
192                          RIGHT_UP, snake_zoom_up,
193                          NULL));
194 }
195 
196 static List *list = NULL, *end = NULL;
197 static void string_start(Tv *tv, Ipos pos)
198 {
199     Vec2 p = tv_backproj2(tv, pos);
200     tv_save_draw(tv);
201     tv_color_set(tv, green);
202     end = list = dd_ref_addtostart(list, vec2_make(p), VEC2);
203     tv_point(tv, pos);
204 }
205 
206 static void string_drag(Tv *tv, Ipos pos)
207 {
208     Vec2 p = tv_backproj2(tv, pos);
209     list = dd_ref_addtostart(list, vec2_make(p), VEC2);
210     tv_line2(tv, *(Vec2 *)list->next->to, *(Vec2 *)list->to);
211 }
212 
213 static void string_end(Tv *tv, Ipos pos)
214 {
215     Tstring *str, *str1;
216     Vec2 p = tv_backproj2(tv, pos);
217     list = dd_ref_addtostart(list, vec2_make(p), VEC2);
218     tv_line2(tv, *(Vec2 *)list->to, *(Vec2 *)end->to);
219     tv_reset_draw(tv);
220     str = str_make(LOOP, list, end);
221     str1 = str2_fill(str); str2_free(str);
222     ims_string_set(str1);
223     ims_string_changed();
224     redraw_all();
225 }
226 
227 Tv_mouse string_draw_mouse(void)
228 {
229     return (mouse_define(MOUSE_NAME, "string draw",
230                          LEFT_NAME, "freehand",
231                          LEFT_DOWN, string_start,
232                          LEFT_DRAG, string_drag,
233                          LEFT_UP, string_end,
234                          NULL));
235 }
236 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.