4
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20
* The Original Code is Copyright (C) Blender Foundation.
21
* All rights reserved.
23
* The Original Code is: all of this file.
25
* Contributor(s): André Pinto
27
* ***** END GPL LICENSE BLOCK *****
29
#include "DNA_object_types.h"
30
#include "DNA_modifier_types.h"
31
#include "DNA_meshdata_types.h"
33
#include "BKE_simple_deform.h"
34
#include "BKE_DerivedMesh.h"
35
#include "BKE_deform.h"
36
#include "BKE_utildefines.h"
37
#include "BLI_arithb.h"
38
#include "BKE_shrinkwrap.h"
44
//Clamps/Limits the given coordinate to: limits[0] <= co[axis] <= limits[1]
45
//The ammount of clamp is saved on dcut
46
static void axis_limit(int axis, const float limits[2], float co[3], float dcut[3])
49
if(limits[0] > val) val = limits[0];
50
if(limits[1] < val) val = limits[1];
52
dcut[axis] = co[axis] - val;
56
static void simpleDeform_taper(const float factor, const float dcut[3], float *co)
58
float x = co[0], y = co[1], z = co[2];
59
float scale = z*factor;
73
static void simpleDeform_stretch(const float factor, const float dcut[3], float *co)
75
float x = co[0], y = co[1], z = co[2];
78
scale = (z*z*factor-factor + 1.0);
82
co[2] = z*(1.0+factor);
93
static void simpleDeform_twist(const float factor, const float *dcut, float *co)
95
float x = co[0], y = co[1], z = co[2];
96
float theta, sint, cost;
102
co[0] = x*cost - y*sint;
103
co[1] = x*sint + y*cost;
114
static void simpleDeform_bend(const float factor, const float dcut[3], float *co)
116
float x = co[0], y = co[1], z = co[2];
117
float theta, sint, cost;
123
if(fabs(factor) > 1e-7f)
125
co[0] = -(y-1.0f/factor)*sint;
126
co[1] = (y-1.0f/factor)*cost + 1.0f/factor;
133
co[0] += cost*dcut[0];
134
co[1] += sint*dcut[0];
141
/* simple deform modifier */
142
void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
144
static const float lock_axis[2] = {0.0f, 0.0f};
148
float smd_limit[2], smd_factor;
149
SpaceTransform *transf = NULL, tmp_transf;
150
void (*simpleDeform_callback)(const float factor, const float dcut[3], float *co) = NULL; //Mode callback
151
int vgroup = get_named_vertexgroup_num(ob, smd->vgroup_name);
152
MDeformVert *dvert = NULL;
155
if(smd->origin == ob) smd->origin = NULL; //No self references
157
if(smd->limit[0] < 0.0) smd->limit[0] = 0.0f;
158
if(smd->limit[0] > 1.0) smd->limit[0] = 1.0f;
160
smd->limit[0] = MIN2(smd->limit[0], smd->limit[1]); //Upper limit >= than lower limit
162
//Calculate matrixs do convert between coordinate spaces
165
transf = &tmp_transf;
167
if(smd->originOpts & MOD_SIMPLEDEFORM_ORIGIN_LOCAL)
169
space_transform_from_matrixs(transf, ob->obmat, smd->origin->obmat);
173
Mat4CpyMat4(transf->local2target, smd->origin->obmat);
174
Mat4Invert(transf->target2local, transf->local2target);
179
limit_axis = (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) ? 0 : 2; //Bend limits on X.. all other modes limit on Z
181
//Update limits if needed
183
float lower = FLT_MAX;
184
float upper = -FLT_MAX;
186
for(i=0; i<numVerts; i++)
189
VECCOPY(tmp, vertexCos[i]);
191
if(transf) space_transform_apply(transf, tmp);
193
lower = MIN2(lower, tmp[limit_axis]);
194
upper = MAX2(upper, tmp[limit_axis]);
198
//SMD values are normalized to the BV, calculate the absolut values
199
smd_limit[1] = lower + (upper-lower)*smd->limit[1];
200
smd_limit[0] = lower + (upper-lower)*smd->limit[0];
202
smd_factor = smd->factor / MAX2(FLT_EPSILON, smd_limit[1]-smd_limit[0]);
207
dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
212
case MOD_SIMPLEDEFORM_MODE_TWIST: simpleDeform_callback = simpleDeform_twist; break;
213
case MOD_SIMPLEDEFORM_MODE_BEND: simpleDeform_callback = simpleDeform_bend; break;
214
case MOD_SIMPLEDEFORM_MODE_TAPER: simpleDeform_callback = simpleDeform_taper; break;
215
case MOD_SIMPLEDEFORM_MODE_STRETCH: simpleDeform_callback = simpleDeform_stretch; break;
217
return; //No simpledeform mode?
220
for(i=0; i<numVerts; i++)
222
float weight = vertexgroup_get_vertex_weight(dvert, i, vgroup);
226
float co[3], dcut[3] = {0.0f, 0.0f, 0.0f};
228
if(transf) space_transform_apply(transf, vertexCos[i]);
230
VECCOPY(co, vertexCos[i]);
233
if(smd->mode != MOD_SIMPLEDEFORM_MODE_BEND) //Bend mode shoulnt have any lock axis
235
if(smd->axis & MOD_SIMPLEDEFORM_LOCK_AXIS_X) axis_limit(0, lock_axis, co, dcut);
236
if(smd->axis & MOD_SIMPLEDEFORM_LOCK_AXIS_Y) axis_limit(1, lock_axis, co, dcut);
238
axis_limit(limit_axis, smd_limit, co, dcut);
240
simpleDeform_callback(smd_factor, dcut, co); //Apply deform
241
VecLerpf(vertexCos[i], vertexCos[i], co, weight); //Use vertex weight has coef of linear interpolation
243
if(transf) space_transform_invert(transf, vertexCos[i]);