2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2005 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation,
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/blenkernel/intern/crazyspace.c
33 #include "MEM_guardedalloc.h"
35 #include "DNA_scene_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_modifier_types.h"
38 #include "DNA_mesh_types.h"
39 #include "DNA_meshdata_types.h"
41 #include "BLI_utildefines.h"
44 #include "BKE_crazyspace.h"
45 #include "BKE_DerivedMesh.h"
46 #include "BKE_modifier.h"
47 #include "BKE_multires.h"
49 #include "BKE_editmesh.h"
51 BLI_INLINE void tan_calc_quat_v3(
53 const float co_1[3], const float co_2[3], const float co_3[3])
55 float vec_u[3], vec_v[3];
58 sub_v3_v3v3(vec_u, co_1, co_2);
59 sub_v3_v3v3(vec_v, co_1, co_3);
61 cross_v3_v3v3(nor, vec_u, vec_v);
63 if (normalize_v3(nor) > FLT_EPSILON) {
64 const float zero_vec[3] = {0.0f};
65 tri_to_quat_ex(r_quat, zero_vec, vec_u, vec_v, nor);
72 static void set_crazy_vertex_quat(
74 const float co_1[3], const float co_2[3], const float co_3[3],
75 const float vd_1[3], const float vd_2[3], const float vd_3[3])
79 tan_calc_quat_v3(q1, co_1, co_2, co_3);
80 tan_calc_quat_v3(q2, vd_1, vd_2, vd_3);
82 sub_qt_qtqt(r_quat, q2, q1);
85 static int modifiers_disable_subsurf_temporary(Object *ob)
90 for (md = ob->modifiers.first; md; md = md->next)
91 if (md->type == eModifierType_Subsurf)
92 if (md->mode & eModifierMode_OnCage) {
93 md->mode ^= eModifierMode_DisableTemporary;
100 /* disable subsurf temporal, get mapped cos, and enable it */
101 float (*BKE_crazyspace_get_mapped_editverts(Scene *scene, Object *obedit))[3]
103 Mesh *me = obedit->data;
105 float (*vertexcos)[3];
106 int nverts = me->edit_btmesh->bm->totvert;
108 /* disable subsurf temporal, get mapped cos, and enable it */
109 if (modifiers_disable_subsurf_temporary(obedit)) {
110 /* need to make new derivemesh */
111 makeDerivedMesh(scene, obedit, me->edit_btmesh, CD_MASK_BAREMESH, 0);
114 /* now get the cage */
115 vertexcos = MEM_mallocN(sizeof(*vertexcos) * nverts, "vertexcos map");
117 dm = editbmesh_get_derived_cage(scene, obedit, me->edit_btmesh, CD_MASK_BAREMESH);
119 mesh_get_mapped_verts_coords(dm, vertexcos, nverts);
123 /* set back the flag, no new cage needs to be built, transform does it */
124 modifiers_disable_subsurf_temporary(obedit);
129 void BKE_crazyspace_set_quats_editmesh(BMEditMesh *em, float (*origcos)[3], float (*mappedcos)[3], float (*quats)[4],
130 const bool use_select)
138 BM_ITER_MESH_INDEX (v, &iter, em->bm, BM_VERTS_OF_MESH, index) {
139 BM_elem_flag_disable(v, BM_ELEM_TAG);
140 BM_elem_index_set(v, index); /* set_inline */
142 em->bm->elem_index_dirty &= ~BM_VERT;
145 BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
146 BMLoop *l_iter, *l_first;
148 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
150 if (BM_elem_flag_test(l_iter->v, BM_ELEM_HIDDEN) ||
151 BM_elem_flag_test(l_iter->v, BM_ELEM_TAG) ||
152 (use_select && !BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)))
157 if (!BM_elem_flag_test(l_iter->v, BM_ELEM_TAG)) {
158 const float *co_prev, *co_curr, *co_next; /* orig */
159 const float *vd_prev, *vd_curr, *vd_next; /* deform */
161 const int i_prev = BM_elem_index_get(l_iter->prev->v);
162 const int i_curr = BM_elem_index_get(l_iter->v);
163 const int i_next = BM_elem_index_get(l_iter->next->v);
165 /* retrieve mapped coordinates */
166 vd_prev = mappedcos[i_prev];
167 vd_curr = mappedcos[i_curr];
168 vd_next = mappedcos[i_next];
171 co_prev = origcos[i_prev];
172 co_curr = origcos[i_curr];
173 co_next = origcos[i_next];
176 co_prev = l_iter->prev->v->co;
177 co_curr = l_iter->v->co;
178 co_next = l_iter->next->v->co;
181 set_crazy_vertex_quat(quats[i_curr],
182 co_curr, co_next, co_prev,
183 vd_curr, vd_next, vd_prev);
185 BM_elem_flag_enable(l_iter->v, BM_ELEM_TAG);
187 } while ((l_iter = l_iter->next) != l_first);
191 void BKE_crazyspace_set_quats_mesh(Mesh *me, float (*origcos)[3], float (*mappedcos)[3], float (*quats)[4])
199 for (i = 0; i < me->totvert; i++, mvert++)
200 mvert->flag &= ~ME_VERT_TMP_TAG;
202 /* first store two sets of tangent vectors in vertices, we derive it just from the face-edges */
207 for (i = 0; i < me->totpoly; i++, mp++) {
208 MLoop *ml_prev, *ml_curr, *ml_next;
211 ml_next = &mloop[mp->loopstart];
212 ml_curr = &ml_next[mp->totloop - 1];
213 ml_prev = &ml_next[mp->totloop - 2];
215 for (j = 0; j < mp->totloop; j++) {
216 if ((mvert[ml_curr->v].flag & ME_VERT_TMP_TAG) == 0) {
217 const float *co_prev, *co_curr, *co_next; /* orig */
218 const float *vd_prev, *vd_curr, *vd_next; /* deform */
220 /* retrieve mapped coordinates */
221 vd_prev = mappedcos[ml_prev->v];
222 vd_curr = mappedcos[ml_curr->v];
223 vd_next = mappedcos[ml_next->v];
226 co_prev = origcos[ml_prev->v];
227 co_curr = origcos[ml_curr->v];
228 co_next = origcos[ml_next->v];
231 co_prev = mvert[ml_prev->v].co;
232 co_curr = mvert[ml_curr->v].co;
233 co_next = mvert[ml_next->v].co;
236 set_crazy_vertex_quat(quats[ml_curr->v],
237 co_curr, co_next, co_prev,
238 vd_curr, vd_next, vd_prev);
240 mvert[ml_curr->v].flag |= ME_VERT_TMP_TAG;
250 int editbmesh_get_first_deform_matrices(Scene *scene, Object *ob, BMEditMesh *em,
251 float (**deformmats)[3][3], float (**deformcos)[3])
255 int i, a, numleft = 0, numVerts = 0;
256 int cageIndex = modifiers_getCageIndex(scene, ob, NULL, 1);
257 float (*defmats)[3][3] = NULL, (*deformedVerts)[3] = NULL;
258 VirtualModifierData virtualModifierData;
260 modifiers_clearErrors(ob);
263 md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
265 /* compute the deformation matrices and coordinates for the first
266 * modifiers with on cage editing that are enabled and support computing
268 for (i = 0; md && i <= cageIndex; i++, md = md->next) {
269 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
271 if (!editbmesh_modifier_is_enabled(scene, md, dm))
274 if (mti->type == eModifierTypeType_OnlyDeform && mti->deformMatricesEM) {
276 dm = getEditDerivedBMesh(em, ob, NULL);
277 deformedVerts = editbmesh_get_vertex_cos(em, &numVerts);
278 defmats = MEM_mallocN(sizeof(*defmats) * numVerts, "defmats");
280 for (a = 0; a < numVerts; a++)
284 mti->deformMatricesEM(md, ob, em, dm, deformedVerts, defmats,
291 for (; md && i <= cageIndex; md = md->next, i++)
292 if (editbmesh_modifier_is_enabled(scene, md, dm) && modifier_isCorrectableDeformed(md))
298 *deformmats = defmats;
299 *deformcos = deformedVerts;
304 int BKE_sculpt_get_first_deform_matrices(Scene *scene, Object *ob, float (**deformmats)[3][3], float (**deformcos)[3])
309 float (*defmats)[3][3] = NULL, (*deformedVerts)[3] = NULL;
310 MultiresModifierData *mmd = get_multires_modifier(scene, ob, 0);
311 const bool has_multires = mmd != NULL && mmd->sculptlvl > 0;
313 VirtualModifierData virtualModifierData;
322 md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
324 for (; md; md = md->next) {
325 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
327 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
329 if (mti->type == eModifierTypeType_OnlyDeform) {
331 Mesh *me = (Mesh *)ob->data;
332 dm = mesh_create_derived(me, NULL);
333 deformedVerts = BKE_mesh_vertexCos_get(me, &numVerts);
334 defmats = MEM_callocN(sizeof(*defmats) * numVerts, "defmats");
336 for (a = 0; a < numVerts; a++)
340 if (mti->deformMatrices) mti->deformMatrices(md, ob, dm, deformedVerts, defmats, numVerts);
345 for (; md; md = md->next) {
346 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
348 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
350 if (mti->type == eModifierTypeType_OnlyDeform)
357 *deformmats = defmats;
358 *deformcos = deformedVerts;
363 void BKE_crazyspace_build_sculpt(Scene *scene, Object *ob, float (**deformmats)[3][3], float (**deformcos)[3])
365 int totleft = BKE_sculpt_get_first_deform_matrices(scene, ob, deformmats, deformcos);
368 /* there are deformation modifier which doesn't support deformation matrices
369 * calculation. Need additional crazyspace correction */
371 float (*deformedVerts)[3] = *deformcos;
372 float (*origVerts)[3] = MEM_dupallocN(deformedVerts);
375 VirtualModifierData virtualModifierData;
376 ModifierData *md = modifiers_getVirtualModifierList(ob, &virtualModifierData);
377 Mesh *me = (Mesh *)ob->data;
379 for (; md; md = md->next) {
380 const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
382 if (!modifier_isEnabled(scene, md, eModifierMode_Realtime)) continue;
384 if (mti->type == eModifierTypeType_OnlyDeform) {
385 /* skip leading modifiers which have been already
386 * handled in sculpt_get_first_deform_matrices */
387 if (mti->deformMatrices && !deformed)
390 mti->deformVerts(md, ob, NULL, deformedVerts, me->totvert, 0);
395 quats = MEM_mallocN(me->totvert * sizeof(*quats), "crazy quats");
397 BKE_crazyspace_set_quats_mesh(me, origVerts, deformedVerts, quats);
399 for (i = 0; i < me->totvert; i++) {
400 float qmat[3][3], tmat[3][3];
402 quat_to_mat3(qmat, quats[i]);
403 mul_m3_m3m3(tmat, qmat, (*deformmats)[i]);
404 copy_m3_m3((*deformmats)[i], tmat);
407 MEM_freeN(origVerts);
411 if (*deformmats == NULL) {
413 Mesh *me = (Mesh *)ob->data;
415 *deformcos = BKE_mesh_vertexCos_get(me, &numVerts);
416 *deformmats = MEM_callocN(sizeof(*(*deformmats)) * numVerts, "defmats");
418 for (a = 0; a < numVerts; a++)
419 unit_m3((*deformmats)[a]);