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

editstrands.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fcf98dfc2eedb96e895fd213f02d3c8083b19639 (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
/*
 * ***** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) Blender Foundation
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Lukas Toenne
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/blenkernel/intern/editstrands.c
 *  \ingroup bke
 */

#include "MEM_guardedalloc.h"

#include "BLI_math.h"
#include "BLI_mempool.h"

#include "DNA_cache_library_types.h"
#include "DNA_customdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_object_types.h"
#include "DNA_particle_types.h"
#include "DNA_strands_types.h"

#include "BKE_bvhutils.h"
#include "BKE_cache_library.h"
#include "BKE_customdata.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_DerivedMesh.h"
#include "BKE_editstrands.h"
#include "BKE_effect.h"
#include "BKE_mesh_sample.h"
#include "BKE_particle.h"

#include "BPH_strands.h"

#include "intern/bmesh_strands_conv.h"

/* mat can be used to transform the dm into another space,
 * in case the edited object is not the active object:
 *   mat = inv(M_act) * M_edit
 */
BMEditStrands *BKE_editstrands_create(BMesh *bm, DerivedMesh *root_dm, float mat[4][4])
{
	BMEditStrands *es = MEM_callocN(sizeof(BMEditStrands), __func__);
	
	es->bm = bm;
	es->root_dm = CDDM_copy(root_dm);
	
	if (mat) {
		DerivedMesh *dm = es->root_dm;
		MVert *mv = dm->getVertArray(dm);
		int totvert = dm->getNumVerts(dm), i;
		for (i = 0; i < totvert; ++i, ++mv) {
			mul_m4_v3(mat, mv->co);
		}
	}
	
	return es;
}

BMEditStrands *BKE_editstrands_copy(BMEditStrands *es)
{
	BMEditStrands *es_copy = MEM_callocN(sizeof(BMEditStrands), __func__);
	*es_copy = *es;
	
	es_copy->bm = BM_mesh_copy(es->bm);
	es_copy->root_dm = CDDM_copy(es->root_dm);
	
	return es_copy;
}

/**
 * \brief Return the BMEditStrands for a given object
 */
BMEditStrands *BKE_editstrands_from_object(Object *ob)
{
	{
		ParticleSystem *psys = psys_get_current(ob);
		if (psys && psys->hairedit)
			return psys->hairedit;
	}
	
	{
		StrandsKeyCacheModifier *skmd;
		if (BKE_cache_modifier_strands_key_get(ob, &skmd, NULL, NULL, NULL, NULL, NULL)) {
			if (skmd->edit)
				return skmd->edit;
		}
	}
	
	return NULL;
}

void BKE_editstrands_update_linked_customdata(BMEditStrands *es)
{
	BMesh *bm = es->bm;
	
	/* this is done for BMEditMesh, but should never exist for strands */
	BLI_assert(!CustomData_has_layer(&bm->pdata, CD_MTEXPOLY));
}

/*does not free the BMEditStrands struct itself*/
void BKE_editstrands_free(BMEditStrands *es)
{
	if (es->bm)
		BM_mesh_free(es->bm);
	if (es->root_dm)
		es->root_dm->release(es->root_dm);
}

/* === constraints === */

BMEditStrandsLocations BKE_editstrands_get_locations(BMEditStrands *edit)
{
	BMesh *bm = edit->bm;
	BMEditStrandsLocations locs = MEM_mallocN(3*sizeof(float) * bm->totvert, "editstrands locations");
	
	BMVert *v;
	BMIter iter;
	int i;
	
	BM_ITER_MESH_INDEX(v, &iter, bm, BM_VERTS_OF_MESH, i) {
		copy_v3_v3(locs[i], v->co);
	}
	
	return locs;
}

void BKE_editstrands_free_locations(BMEditStrandsLocations locs)
{
	MEM_freeN(locs);
}

void BKE_editstrands_solve_constraints(Object *ob, BMEditStrands *es, BMEditStrandsLocations orig)
{
	BKE_editstrands_ensure(es);
	
	BPH_strands_solve_constraints(ob, es, orig);
}

static void editstrands_calc_segment_lengths(BMesh *bm)
{
	BMVert *root;
	BMIter iter;
	
	BM_ITER_STRANDS(root, &iter, bm, BM_STRANDS_OF_MESH) {
		BMVert *v, *vprev = NULL;
		BMIter iter_strand;
		BM_ITER_STRANDS_ELEM(v, &iter_strand, root, BM_VERTS_OF_STRAND) {
			if (vprev) {
				float length = len_v3v3(v->co, vprev->co);
				BM_elem_float_data_named_set(&bm->vdata, vprev, CD_PROP_FLT, CD_HAIR_SEGMENT_LENGTH, length);
			}
			vprev = v;
		}
		if (vprev) {
			/* set last to 0 */
			BM_elem_float_data_named_set(&bm->vdata, vprev, CD_PROP_FLT, CD_HAIR_SEGMENT_LENGTH, 0.0f);
		}
	}
}

void BKE_editstrands_ensure(BMEditStrands *es)
{
	BM_strands_cd_flag_ensure(es->bm, 0);
	
	if (es->flag & BM_STRANDS_DIRTY_SEGLEN) {
		editstrands_calc_segment_lengths(es->bm);
		
		es->flag &= ~BM_STRANDS_DIRTY_SEGLEN;
	}
}


/* === cache shape key conversion === */

BMesh *BKE_cache_strands_to_bmesh(struct Strands *strands, struct Key *key, float mat[4][4], int act_key_nr, DerivedMesh *dm)
{
	const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_STRANDS(strands);
	BMesh *bm;
	
	DM_ensure_tessface(dm);
	
	bm = BM_mesh_create(&allocsize);
	BM_strands_bm_from_strands(bm, strands, mat, key, dm, true, act_key_nr);
	editstrands_calc_segment_lengths(bm);
	
	return bm;
}

struct Strands *BKE_cache_strands_from_bmesh(BMEditStrands *edit, struct Key *key, float mat[4][4], DerivedMesh *dm)
{
	BMesh *bm = edit ? edit->bm : NULL;
	Strands *strands = NULL;
	
	if (bm && dm) {
		BVHTreeFromMesh bvhtree = {NULL};
		
		DM_ensure_tessface(dm);
		
		bvhtree_from_mesh_faces(&bvhtree, dm, 0.0, 2, 6);
		
		strands = BM_strands_bm_to_strands(bm, strands, mat, key, dm, &bvhtree);
		
		free_bvhtree_from_mesh(&bvhtree);
	}
	
	return strands;
}


/* === particle conversion === */

BMesh *BKE_particles_to_bmesh(Object *ob, ParticleSystem *psys)
{
	ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
	
	const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_PSYS(psys);
	BMesh *bm;

	bm = BM_mesh_create(&allocsize);

	if (psmd && psmd->dm) {
		DM_ensure_tessface(psmd->dm);
		
		BM_strands_bm_from_psys(bm, ob, psys, psmd->dm, true, /*psys->shapenr*/ -1);
		
		editstrands_calc_segment_lengths(bm);
	}

	return bm;
}

void BKE_particles_from_bmesh(Object *ob, ParticleSystem *psys)
{
	ParticleSystemModifierData *psmd = psys_get_modifier(ob, psys);
	BMesh *bm = psys->hairedit ? psys->hairedit->bm : NULL;
	
	if (bm) {
		if (psmd && psmd->dm) {
			BVHTreeFromMesh bvhtree = {NULL};
			
			DM_ensure_tessface(psmd->dm);
			
			bvhtree_from_mesh_faces(&bvhtree, psmd->dm, 0.0, 2, 6);
			
			BM_strands_bm_to_psys(bm, ob, psys, psmd->dm, &bvhtree);
			
			free_bvhtree_from_mesh(&bvhtree);
		}
	}
}