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