Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sculpt_stroke.c « sculpt_paint « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 554ff580358646edc595ad8689a26069b07e4f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software  Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2007 by Nicholas Bishop
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 *
 * Storage and manipulation of sculptmode brush strokes.
 *
 */

#include "MEM_guardedalloc.h"

#include "DNA_listBase.h"
#include "DNA_scene_types.h"

#include "BKE_sculpt.h"
#include "BLI_blenlib.h"
#include "BIF_gl.h"

#include "sculpt_intern.h"

#include <math.h>

/* Temporary storage of input stroke control points */
typedef struct StrokePoint {
	struct StrokePoint *next, *prev;
	short x, y;
} StrokePoint;
typedef struct SculptStroke {
	short (*loc)[2];
	int max;
	int index;
	float length;
	ListBase final;
	StrokePoint *final_mem;
	float offset;
} SculptStroke;

SculptStroke *sculpt_stroke_new(const int max)
{
	SculptStroke *stroke = MEM_callocN(sizeof(SculptStroke), "SculptStroke");
	stroke->loc = MEM_callocN(sizeof(short) * 4 * max, "SculptStroke.loc");
	stroke->max = max;
	stroke->index = -1;
	return stroke;
}

void sculpt_stroke_free(SculptStroke *stroke)
{
	if(stroke) {
		if(stroke->loc) MEM_freeN(stroke->loc);
		if(stroke->final_mem) MEM_freeN(stroke->final_mem);

		MEM_freeN(stroke);
	}
}

void sculpt_stroke_add_point(SculptStroke *stroke, const short x, const short y)
{
	const int next = stroke->index + 1;

	if(stroke->index == -1) {
		stroke->loc[0][0] = x;
		stroke->loc[0][1] = y;
		stroke->index = 0;
	}
	else if(next < stroke->max) {
		const int dx = x - stroke->loc[stroke->index][0];
		const int dy = y - stroke->loc[stroke->index][1];
		stroke->loc[next][0] = x;
		stroke->loc[next][1] = y;
		stroke->length += sqrt(dx*dx + dy*dy);
		stroke->index = next;
	}
}

static void sculpt_stroke_smooth(SculptStroke *stroke)
{
	/* Apply smoothing (exclude the first and last points)*/
	StrokePoint *p = stroke->final.first;
	if(p && p->next && p->next->next) {
		for(p = p->next->next; p && p->next && p->next->next; p = p->next) {
			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;
			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;
		}
	}	
}

static void sculpt_stroke_create_final(SculptStroke *stroke)
{
	if(stroke) {
		StrokePoint *p, *pnext;
		int i;

		/* Copy loc into final */
		if(stroke->final_mem)
			MEM_freeN(stroke->final_mem);
		stroke->final_mem = MEM_callocN(sizeof(StrokePoint) * (stroke->index + 1) * 2, "SculptStroke.final");
		stroke->final.first = stroke->final.last = NULL;
		for(i = 0; i <= stroke->index; ++i) {
			p = &stroke->final_mem[i];
			p->x = stroke->loc[i][0];
			p->y = stroke->loc[i][1];
			BLI_addtail(&stroke->final, p);
		}

		/* Remove shortest edges */
		if(stroke->final.first) {
			for(p = ((StrokePoint*)stroke->final.first)->next; p && p->next; p = pnext) {
				const int dx = p->x - p->prev->x;
				const int dy = p->y - p->prev->y;
				const float len = sqrt(dx*dx + dy*dy);
				pnext = p->next;
				if(len < 10) {
					BLI_remlink(&stroke->final, p);
				}
			}
		}

		sculpt_stroke_smooth(stroke);

		/* Subdivide edges */
		for(p = stroke->final.first; p && p->next; p = pnext) {
			StrokePoint *np = &stroke->final_mem[i++];

			pnext = p->next;
			np->x = (p->x + p->next->x) / 2;
			np->y = (p->y + p->next->y) / 2;
			BLI_insertlink(&stroke->final, p, np);
		}

		sculpt_stroke_smooth(stroke);
	}
}

static float sculpt_stroke_seglen(StrokePoint *p1, StrokePoint *p2)
{
	int dx = p2->x - p1->x;
	int dy = p2->y - p1->y;
	return sqrt(dx*dx + dy*dy);
}

static float sculpt_stroke_final_length(SculptStroke *stroke)
{
	StrokePoint *p;
	float len = 0;
	for(p = stroke->final.first; p && p->next; ++p)
		len += sculpt_stroke_seglen(p, p->next);
	return len;
}

/* If partial is nonzero, cuts off apply after that length has been processed */
static StrokePoint *sculpt_stroke_apply_generic(Sculpt *sd, SculptStroke *stroke, const int partial)
{
	const int sdspace = 0; //XXX: sd->spacing;
	const short spacing = sdspace > 0 ? sdspace : 2;
	const int dots = sculpt_stroke_final_length(stroke) / spacing;
	int i;
	StrokePoint *p = stroke->final.first;
	float startloc = stroke->offset;

	for(i = 0; i < dots && p && p->next; ++i) {
		const float dotloc = spacing * i;
		short co[2];
		float len = sculpt_stroke_seglen(p, p->next);
		float u, v;

		/* Find edge containing dot */
		while(dotloc > startloc + len && p && p->next && p->next->next) {
			p = p->next;
			startloc += len;
			len = sculpt_stroke_seglen(p, p->next);
		}

		if(!p || !p->next || dotloc > startloc + len)
			break;

		if(partial && startloc > partial) {
			/* Calculate offset for next stroke segment */
			stroke->offset = startloc + len - dotloc;
			break;
		}

		u = (dotloc - startloc) / len;
		v = 1 - u;
					
		co[0] = p->x*v + p->next->x*u;
		co[1] = p->y*v + p->next->y*u;

		//do_symmetrical_brush_actions(sd, a, co, NULL);
	}

	return p ? p->next : NULL;
}

void sculpt_stroke_apply(Sculpt *sd, SculptStroke *stroke)
{
	/* TODO: make these values user-modifiable? */
	const int partial_len = 100;
	const int min_len = 200;

	if(stroke) {
		sculpt_stroke_create_final(stroke);

		if(sculpt_stroke_final_length(stroke) > min_len) {
			StrokePoint *p = sculpt_stroke_apply_generic(sd, stroke, partial_len);

			/* Replace remaining values in stroke->loc with remaining stroke->final values */
			stroke->index = -1;
			stroke->length = 0;
			for(; p; p = p->next) {
				++stroke->index;
				stroke->loc[stroke->index][0] = p->x;
				stroke->loc[stroke->index][1] = p->y;
				if(p->next) {
					stroke->length += sculpt_stroke_seglen(p, p->next);
				}
			}
		}
	}
}

void sculpt_stroke_apply_all(Sculpt *sd, SculptStroke *stroke)
{
	sculpt_stroke_create_final(stroke);

	if(stroke) {
		sculpt_stroke_apply_generic(sd, stroke, 0);
	}
}

/* XXX: drawing goes elsewhere */
void sculpt_stroke_draw(SculptStroke *stroke)
{
	if(stroke) {
		StrokePoint *p;

		/* Draws the original stroke */
		/*glColor3f(1, 0, 0);		
		glBegin(GL_LINE_STRIP);
		for(i = 0; i <= stroke->index; ++i)
			glVertex2s(stroke->loc[i][0], stroke->loc[i][1]);
		glEnd();*/

		/* Draws the smoothed stroke */
		glColor3f(0, 1, 0);
		glBegin(GL_LINE_STRIP);
		for(p = stroke->final.first; p; p = p->next)
			glVertex2s(p->x, p->y);
		glEnd();
	}
}