~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/simple_deform.c

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * deform_simple.c
 
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) Blender Foundation.
 
21
 * All rights reserved.
 
22
 *
 
23
 * The Original Code is: all of this file.
 
24
 *
 
25
 * Contributor(s): André Pinto
 
26
 *
 
27
 * ***** END GPL LICENSE BLOCK *****
 
28
 */
 
29
#include "DNA_object_types.h"
 
30
#include "DNA_modifier_types.h"
 
31
#include "DNA_meshdata_types.h"
 
32
 
 
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"
 
39
 
 
40
#include <string.h>
 
41
#include <math.h>
 
42
 
 
43
 
 
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])
 
47
{
 
48
        float val = co[axis];
 
49
        if(limits[0] > val) val = limits[0];
 
50
        if(limits[1] < val) val = limits[1];
 
51
 
 
52
        dcut[axis] = co[axis] - val;
 
53
        co[axis] = val;
 
54
}
 
55
 
 
56
static void simpleDeform_taper(const float factor, const float dcut[3], float *co)
 
57
{
 
58
        float x = co[0], y = co[1], z = co[2];
 
59
        float scale = z*factor;
 
60
 
 
61
        co[0] = x + x*scale;
 
62
        co[1] = y + y*scale;
 
63
        co[2] = z;
 
64
 
 
65
        if(dcut)
 
66
        {
 
67
                co[0] += dcut[0];
 
68
                co[1] += dcut[1];
 
69
                co[2] += dcut[2];
 
70
        }
 
71
}
 
72
 
 
73
static void simpleDeform_stretch(const float factor, const float dcut[3], float *co)
 
74
{
 
75
        float x = co[0], y = co[1], z = co[2];
 
76
        float scale;
 
77
 
 
78
        scale = (z*z*factor-factor + 1.0);
 
79
 
 
80
        co[0] = x*scale;
 
81
        co[1] = y*scale;
 
82
        co[2] = z*(1.0+factor);
 
83
 
 
84
 
 
85
        if(dcut)
 
86
        {
 
87
                co[0] += dcut[0];
 
88
                co[1] += dcut[1];
 
89
                co[2] += dcut[2]; 
 
90
        }
 
91
}
 
92
 
 
93
static void simpleDeform_twist(const float factor, const float *dcut, float *co)
 
94
{
 
95
        float x = co[0], y = co[1], z = co[2];
 
96
        float theta, sint, cost;
 
97
 
 
98
        theta = z*factor;
 
99
        sint  = sin(theta);
 
100
        cost  = cos(theta);
 
101
 
 
102
        co[0] = x*cost - y*sint;
 
103
        co[1] = x*sint + y*cost;
 
104
        co[2] = z;
 
105
 
 
106
        if(dcut)
 
107
        {
 
108
                co[0] += dcut[0];
 
109
                co[1] += dcut[1];
 
110
                co[2] += dcut[2];
 
111
        }
 
112
}
 
113
 
 
114
static void simpleDeform_bend(const float factor, const float dcut[3], float *co)
 
115
{
 
116
        float x = co[0], y = co[1], z = co[2];
 
117
        float theta, sint, cost;
 
118
 
 
119
        theta = x*factor;
 
120
        sint = sin(theta);
 
121
        cost = cos(theta);
 
122
 
 
123
        if(fabs(factor) > 1e-7f)
 
124
        {
 
125
                co[0] = -(y-1.0f/factor)*sint;
 
126
                co[1] =  (y-1.0f/factor)*cost + 1.0f/factor;
 
127
                co[2] = z;
 
128
        }
 
129
 
 
130
 
 
131
        if(dcut)
 
132
        {
 
133
                co[0] += cost*dcut[0];
 
134
                co[1] += sint*dcut[0];
 
135
                co[2] += dcut[2]; 
 
136
        }
 
137
 
 
138
}
 
139
 
 
140
 
 
141
/* simple deform modifier */
 
142
void SimpleDeformModifier_do(SimpleDeformModifierData *smd, struct Object *ob, struct DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
 
143
{
 
144
        static const float lock_axis[2] = {0.0f, 0.0f};
 
145
 
 
146
        int i;
 
147
        int limit_axis = 0;                     
 
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;
 
153
 
 
154
        //Safe-check
 
155
        if(smd->origin == ob) smd->origin = NULL;                                       //No self references
 
156
 
 
157
        if(smd->limit[0] < 0.0) smd->limit[0] = 0.0f;
 
158
        if(smd->limit[0] > 1.0) smd->limit[0] = 1.0f;
 
159
 
 
160
        smd->limit[0] = MIN2(smd->limit[0], smd->limit[1]);                     //Upper limit >= than lower limit
 
161
 
 
162
        //Calculate matrixs do convert between coordinate spaces
 
163
        if(smd->origin)
 
164
        {
 
165
                transf = &tmp_transf;
 
166
                
 
167
                if(smd->originOpts & MOD_SIMPLEDEFORM_ORIGIN_LOCAL)
 
168
                {
 
169
                        space_transform_from_matrixs(transf, ob->obmat, smd->origin->obmat);
 
170
                }
 
171
                else
 
172
                {
 
173
                        Mat4CpyMat4(transf->local2target, smd->origin->obmat);
 
174
                        Mat4Invert(transf->target2local, transf->local2target);
 
175
                }
 
176
        }
 
177
 
 
178
        //Setup vars
 
179
        limit_axis  = (smd->mode == MOD_SIMPLEDEFORM_MODE_BEND) ? 0 : 2; //Bend limits on X.. all other modes limit on Z
 
180
 
 
181
        //Update limits if needed
 
182
        {
 
183
                float lower =  FLT_MAX;
 
184
                float upper = -FLT_MAX;
 
185
 
 
186
                for(i=0; i<numVerts; i++)
 
187
                {
 
188
                        float tmp[3];
 
189
                        VECCOPY(tmp, vertexCos[i]);
 
190
 
 
191
                        if(transf) space_transform_apply(transf, tmp);
 
192
 
 
193
                        lower = MIN2(lower, tmp[limit_axis]);
 
194
                        upper = MAX2(upper, tmp[limit_axis]);
 
195
                }
 
196
 
 
197
 
 
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];
 
201
 
 
202
                smd_factor   = smd->factor / MAX2(FLT_EPSILON, smd_limit[1]-smd_limit[0]);
 
203
        }
 
204
 
 
205
 
 
206
        if(dm)
 
207
                dvert   = dm->getVertDataArray(dm, CD_MDEFORMVERT);
 
208
 
 
209
 
 
210
        switch(smd->mode)
 
211
        {
 
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;
 
216
                default:
 
217
                        return; //No simpledeform mode?
 
218
        }
 
219
 
 
220
        for(i=0; i<numVerts; i++)
 
221
        {
 
222
                float weight = vertexgroup_get_vertex_weight(dvert, i, vgroup);
 
223
 
 
224
                if(weight != 0.0f)
 
225
                {
 
226
                        float co[3], dcut[3] = {0.0f, 0.0f, 0.0f};
 
227
 
 
228
                        if(transf) space_transform_apply(transf, vertexCos[i]);
 
229
 
 
230
                        VECCOPY(co, vertexCos[i]);
 
231
 
 
232
                        //Apply axis limits
 
233
                        if(smd->mode != MOD_SIMPLEDEFORM_MODE_BEND) //Bend mode shoulnt have any lock axis
 
234
                        {
 
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);
 
237
                        }
 
238
                        axis_limit(limit_axis, smd_limit, co, dcut);
 
239
 
 
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
 
242
        
 
243
                        if(transf) space_transform_invert(transf, vertexCos[i]);
 
244
                }
 
245
        }
 
246
}
 
247
 
 
248