svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / blender / editors / sculpt_paint / sculpt_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 "sculpt_intern.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 SculptStroke *sculpt_stroke_new(const int max)
62 {
63         SculptStroke *stroke = MEM_callocN(sizeof(SculptStroke), "SculptStroke");
64         stroke->loc = MEM_callocN(sizeof(short) * 4 * max, "SculptStroke.loc");
65         stroke->max = max;
66         stroke->index = -1;
67         return stroke;
68 }
69
70 void sculpt_stroke_free(SculptStroke *stroke)
71 {
72         if(stroke) {
73                 if(stroke->loc) MEM_freeN(stroke->loc);
74                 if(stroke->final_mem) MEM_freeN(stroke->final_mem);
75
76                 MEM_freeN(stroke);
77         }
78 }
79
80 void sculpt_stroke_add_point(SculptStroke *stroke, const short x, const short y)
81 {
82         const int next = stroke->index + 1;
83
84         if(stroke->index == -1) {
85                 stroke->loc[0][0] = x;
86                 stroke->loc[0][1] = y;
87                 stroke->index = 0;
88         }
89         else if(next < stroke->max) {
90                 const int dx = x - stroke->loc[stroke->index][0];
91                 const int dy = y - stroke->loc[stroke->index][1];
92                 stroke->loc[next][0] = x;
93                 stroke->loc[next][1] = y;
94                 stroke->length += sqrt(dx*dx + dy*dy);
95                 stroke->index = next;
96         }
97 }
98
99 static void sculpt_stroke_smooth(SculptStroke *stroke)
100 {
101         /* Apply smoothing (exclude the first and last points)*/
102         StrokePoint *p = stroke->final.first;
103         if(p && p->next && p->next->next) {
104                 for(p = p->next->next; p && p->next && p->next->next; p = p->next) {
105                         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;
106                         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;
107                 }
108         }       
109 }
110
111 static void sculpt_stroke_create_final(SculptStroke *stroke)
112 {
113         if(stroke) {
114                 StrokePoint *p, *pnext;
115                 int i;
116
117                 /* Copy loc into final */
118                 if(stroke->final_mem)
119                         MEM_freeN(stroke->final_mem);
120                 stroke->final_mem = MEM_callocN(sizeof(StrokePoint) * (stroke->index + 1) * 2, "SculptStroke.final");
121                 stroke->final.first = stroke->final.last = NULL;
122                 for(i = 0; i <= stroke->index; ++i) {
123                         p = &stroke->final_mem[i];
124                         p->x = stroke->loc[i][0];
125                         p->y = stroke->loc[i][1];
126                         BLI_addtail(&stroke->final, p);
127                 }
128
129                 /* Remove shortest edges */
130                 if(stroke->final.first) {
131                         for(p = ((StrokePoint*)stroke->final.first)->next; p && p->next; p = pnext) {
132                                 const int dx = p->x - p->prev->x;
133                                 const int dy = p->y - p->prev->y;
134                                 const float len = sqrt(dx*dx + dy*dy);
135                                 pnext = p->next;
136                                 if(len < 10) {
137                                         BLI_remlink(&stroke->final, p);
138                                 }
139                         }
140                 }
141
142                 sculpt_stroke_smooth(stroke);
143
144                 /* Subdivide edges */
145                 for(p = stroke->final.first; p && p->next; p = pnext) {
146                         StrokePoint *np = &stroke->final_mem[i++];
147
148                         pnext = p->next;
149                         np->x = (p->x + p->next->x) / 2;
150                         np->y = (p->y + p->next->y) / 2;
151                         BLI_insertlink(&stroke->final, p, np);
152                 }
153
154                 sculpt_stroke_smooth(stroke);
155         }
156 }
157
158 static float sculpt_stroke_seglen(StrokePoint *p1, StrokePoint *p2)
159 {
160         int dx = p2->x - p1->x;
161         int dy = p2->y - p1->y;
162         return sqrt(dx*dx + dy*dy);
163 }
164
165 static float sculpt_stroke_final_length(SculptStroke *stroke)
166 {
167         StrokePoint *p;
168         float len = 0;
169         for(p = stroke->final.first; p && p->next; ++p)
170                 len += sculpt_stroke_seglen(p, p->next);
171         return len;
172 }
173
174 /* If partial is nonzero, cuts off apply after that length has been processed */
175 static StrokePoint *sculpt_stroke_apply_generic(Sculpt *sd, SculptStroke *stroke, const int partial)
176 {
177         const int sdspace = 0; //XXX: sd->spacing;
178         const short spacing = sdspace > 0 ? sdspace : 2;
179         const int dots = sculpt_stroke_final_length(stroke) / spacing;
180         int i;
181         StrokePoint *p = stroke->final.first;
182         float startloc = stroke->offset;
183
184         for(i = 0; i < dots && p && p->next; ++i) {
185                 const float dotloc = spacing * i;
186                 short co[2];
187                 float len = sculpt_stroke_seglen(p, p->next);
188                 float u, v;
189
190                 /* Find edge containing dot */
191                 while(dotloc > startloc + len && p && p->next && p->next->next) {
192                         p = p->next;
193                         startloc += len;
194                         len = sculpt_stroke_seglen(p, p->next);
195                 }
196
197                 if(!p || !p->next || dotloc > startloc + len)
198                         break;
199
200                 if(partial && startloc > partial) {
201                         /* Calculate offset for next stroke segment */
202                         stroke->offset = startloc + len - dotloc;
203                         break;
204                 }
205
206                 u = (dotloc - startloc) / len;
207                 v = 1 - u;
208                                         
209                 co[0] = p->x*v + p->next->x*u;
210                 co[1] = p->y*v + p->next->y*u;
211
212                 //do_symmetrical_brush_actions(sd, a, co, NULL);
213         }
214
215         return p ? p->next : NULL;
216 }
217
218 void sculpt_stroke_apply(Sculpt *sd, SculptStroke *stroke)
219 {
220         /* TODO: make these values user-modifiable? */
221         const int partial_len = 100;
222         const int min_len = 200;
223
224         if(stroke) {
225                 sculpt_stroke_create_final(stroke);
226
227                 if(sculpt_stroke_final_length(stroke) > min_len) {
228                         StrokePoint *p = sculpt_stroke_apply_generic(sd, stroke, partial_len);
229
230                         /* Replace remaining values in stroke->loc with remaining stroke->final values */
231                         stroke->index = -1;
232                         stroke->length = 0;
233                         for(; p; p = p->next) {
234                                 ++stroke->index;
235                                 stroke->loc[stroke->index][0] = p->x;
236                                 stroke->loc[stroke->index][1] = p->y;
237                                 if(p->next) {
238                                         stroke->length += sculpt_stroke_seglen(p, p->next);
239                                 }
240                         }
241                 }
242         }
243 }
244
245 void sculpt_stroke_apply_all(Sculpt *sd, SculptStroke *stroke)
246 {
247         sculpt_stroke_create_final(stroke);
248
249         if(stroke) {
250                 sculpt_stroke_apply_generic(sd, stroke, 0);
251         }
252 }
253
254 /* XXX: drawing goes elsewhere */
255 void sculpt_stroke_draw(SculptStroke *stroke)
256 {
257         if(stroke) {
258                 StrokePoint *p;
259
260                 /* Draws the original stroke */
261                 /*glColor3f(1, 0, 0);           
262                 glBegin(GL_LINE_STRIP);
263                 for(i = 0; i <= stroke->index; ++i)
264                         glVertex2s(stroke->loc[i][0], stroke->loc[i][1]);
265                 glEnd();*/
266
267                 /* Draws the smoothed stroke */
268                 glColor3f(0, 1, 0);
269                 glBegin(GL_LINE_STRIP);
270                 for(p = stroke->final.first; p; p = p->next)
271                         glVertex2s(p->x, p->y);
272                 glEnd();
273         }
274 }