== Sculpt ==
[blender-staging.git] / source / blender / src / sculptmode-stroke.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software  Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 by Nicholas Bishop
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  * Storage and manipulation of sculptmode brush strokes.
30  *
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_listBase.h"
36 #include "DNA_scene_types.h"
37
38 #include "BKE_sculpt.h"
39 #include "BLI_blenlib.h"
40 #include "BIF_gl.h"
41
42 #include "BDR_sculptmode.h"
43
44 #include <math.h>
45
46 /* Temporary storage of input stroke control points */
47 typedef struct StrokePoint {
48         struct StrokePoint *next, *prev;
49         short x, y;
50 } StrokePoint;
51 typedef struct SculptStroke {
52         short (*loc)[2];
53         int max;
54         int index;
55         float length;
56         ListBase final;
57         StrokePoint *final_mem;
58         float offset;
59 } SculptStroke;
60
61 void sculpt_stroke_new(const int max)
62 {
63         SculptSession *ss = sculpt_session();
64
65         ss->stroke = MEM_callocN(sizeof(SculptStroke), "SculptStroke");
66         ss->stroke->loc = MEM_callocN(sizeof(short) * 4 * max, "SculptStroke.loc");
67         ss->stroke->max = max;
68         ss->stroke->index = -1;
69 }
70
71 void sculpt_stroke_free()
72 {
73         SculptSession *ss = sculpt_session();
74         if(ss && ss->stroke) {
75                 if(ss->stroke->loc) MEM_freeN(ss->stroke->loc);
76                 if(ss->stroke->final_mem) MEM_freeN(ss->stroke->final_mem);
77
78                 MEM_freeN(ss->stroke);
79                 ss->stroke = NULL;
80         }
81 }
82
83 void sculpt_stroke_add_point(const short x, const short y)
84 {
85         SculptStroke *stroke = sculpt_session()->stroke;
86         const int next = stroke->index + 1;
87
88         if(stroke->index == -1) {
89                 stroke->loc[0][0] = x;
90                 stroke->loc[0][1] = y;
91                 stroke->index = 0;
92         }
93         else if(next < stroke->max) {
94                 const int dx = x - stroke->loc[stroke->index][0];
95                 const int dy = y - stroke->loc[stroke->index][1];
96                 stroke->loc[next][0] = x;
97                 stroke->loc[next][1] = y;
98                 stroke->length += sqrt(dx*dx + dy*dy);
99                 stroke->index = next;
100         }
101 }
102
103 void sculpt_stroke_smooth(SculptStroke *stroke)
104 {
105         /* Apply smoothing (exclude the first and last points)*/
106         StrokePoint *p = stroke->final.first;
107         if(p && p->next && p->next->next) {
108                 for(p = p->next->next; p && p->next && p->next->next; p = p->next) {
109                         p->x = p->prev->prev->x*0.1 + p->prev->x*0.2 + p->x*0.4 + p->next->x*0.2 + p->next->next->x*0.1;
110                         p->y = p->prev->prev->y*0.1 + p->prev->y*0.2 + p->y*0.4 + p->next->y*0.2 + p->next->next->y*0.1;
111                 }
112         }       
113 }
114
115 static void sculpt_stroke_create_final()
116 {
117         SculptStroke *stroke = sculpt_session()->stroke;
118
119         if(stroke) {
120                 StrokePoint *p, *pnext;
121                 int i;
122
123                 /* Copy loc into final */
124                 if(stroke->final_mem)
125                         MEM_freeN(stroke->final_mem);
126                 stroke->final_mem = MEM_callocN(sizeof(StrokePoint) * (stroke->index + 1) * 2, "SculptStroke.final");
127                 stroke->final.first = stroke->final.last = NULL;
128                 for(i = 0; i <= stroke->index; ++i) {
129                         p = &stroke->final_mem[i];
130                         p->x = stroke->loc[i][0];
131                         p->y = stroke->loc[i][1];
132                         BLI_addtail(&stroke->final, p);
133                 }
134
135                 /* Remove shortest edges */
136                 if(stroke->final.first) {
137                         for(p = ((StrokePoint*)stroke->final.first)->next; p && p->next; p = pnext) {
138                                 const int dx = p->x - p->prev->x;
139                                 const int dy = p->y - p->prev->y;
140                                 const float len = sqrt(dx*dx + dy*dy);
141                                 pnext = p->next;
142                                 if(len < 10) {
143                                         BLI_remlink(&stroke->final, p);
144                                 }
145                         }
146                 }
147
148                 sculpt_stroke_smooth(stroke);
149
150                 /* Subdivide edges */
151                 for(p = stroke->final.first; p && p->next; p = pnext) {
152                         StrokePoint *np = &stroke->final_mem[i++];
153
154                         pnext = p->next;
155                         np->x = (p->x + p->next->x) / 2;
156                         np->y = (p->y + p->next->y) / 2;
157                         BLI_insertlink(&stroke->final, p, np);
158                 }
159
160                 sculpt_stroke_smooth(stroke);
161         }
162 }
163
164 float sculpt_stroke_seglen(StrokePoint *p1, StrokePoint *p2)
165 {
166         int dx = p2->x - p1->x;
167         int dy = p2->y - p1->y;
168         return sqrt(dx*dx + dy*dy);
169 }
170
171 float sculpt_stroke_final_length(SculptStroke *stroke)
172 {
173         StrokePoint *p;
174         float len = 0;
175         for(p = stroke->final.first; p && p->next; ++p)
176                 len += sculpt_stroke_seglen(p, p->next);
177         return len;
178 }
179
180 /* If partial is nonzero, cuts off apply after that length has been processed */
181 static StrokePoint *sculpt_stroke_apply_generic(SculptStroke *stroke, struct BrushAction *a, const int partial)
182 {
183         const int sdspace = sculpt_data()->spacing;
184         const short spacing = sdspace > 0 ? sdspace : 2;
185         const int dots = sculpt_stroke_final_length(stroke) / spacing;
186         int i;
187         StrokePoint *p = stroke->final.first;
188         float startloc = stroke->offset;
189
190         for(i = 0; i < dots && p && p->next; ++i) {
191                 const float dotloc = spacing * i;
192                 short co[2];
193                 float len = sculpt_stroke_seglen(p, p->next);
194                 float u, v;
195
196                 /* Find edge containing dot */
197                 while(dotloc > startloc + len && p && p->next && p->next->next) {
198                         p = p->next;
199                         startloc += len;
200                         len = sculpt_stroke_seglen(p, p->next);
201                 }
202
203                 if(!p || !p->next || dotloc > startloc + len)
204                         break;
205
206                 if(partial && startloc > partial) {
207                         /* Calculate offset for next stroke segment */
208                         stroke->offset = startloc + len - dotloc;
209                         break;
210                 }
211
212                 u = (dotloc - startloc) / len;
213                 v = 1 - u;
214                                         
215                 co[0] = p->x*v + p->next->x*u;
216                 co[1] = p->y*v + p->next->y*u;
217
218                 do_symmetrical_brush_actions(a, co, NULL);
219         }
220
221         return p ? p->next : NULL;
222 }
223
224 void sculpt_stroke_apply(struct BrushAction *a)
225 {
226         SculptStroke *stroke = sculpt_session()->stroke;
227         /* TODO: make these values user-modifiable? */
228         const int partial_len = 100;
229         const int min_len = 200;
230
231         if(stroke) {
232                 sculpt_stroke_create_final();
233
234                 if(sculpt_stroke_final_length(stroke) > min_len) {
235                         StrokePoint *p = sculpt_stroke_apply_generic(stroke, a, partial_len);
236
237                         /* Replace remaining values in stroke->loc with remaining stroke->final values */
238                         stroke->index = -1;
239                         stroke->length = 0;
240                         for(; p; p = p->next) {
241                                 ++stroke->index;
242                                 stroke->loc[stroke->index][0] = p->x;
243                                 stroke->loc[stroke->index][1] = p->y;
244                                 if(p->next) {
245                                         stroke->length += sculpt_stroke_seglen(p, p->next);
246                                 }
247                         }
248                 }
249         }
250 }
251
252 void sculpt_stroke_apply_all(struct BrushAction *a)
253 {
254         SculptStroke *stroke = sculpt_session()->stroke;
255
256         sculpt_stroke_create_final();
257
258         if(stroke) {
259                 sculpt_stroke_apply_generic(stroke, a, 0);
260         }
261 }
262
263 void sculpt_stroke_draw()
264 {
265         SculptStroke *stroke = sculpt_session()->stroke;
266
267         if(stroke) {
268                 StrokePoint *p;
269
270                 /* Draws the original stroke */
271                 /*glColor3f(1, 0, 0);           
272                 glBegin(GL_LINE_STRIP);
273                 for(i = 0; i <= stroke->index; ++i)
274                         glVertex2s(stroke->loc[i][0], stroke->loc[i][1]);
275                 glEnd();*/
276
277                 /* Draws the smoothed stroke */
278                 glColor3f(0, 1, 0);
279                 glBegin(GL_LINE_STRIP);
280                 for(p = stroke->final.first; p; p = p->next)
281                         glVertex2s(p->x, p->y);
282                 glEnd();
283         }
284 }