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