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

object_data_transform.c « object « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee86c79ead5a30b0a44398e2851dde007b2941f3 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
 * 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) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 */

/** \file
 * \ingroup edobj
 *
 * Use to transform object origins only.
 *
 * This is a small API to store & apply transformations to object data,
 * where a transformation matrix can be continually applied ontop of the original values
 * so we don't loose precision over time.
 */

#include <stdlib.h>
#include <string.h>

#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meta_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_collection_types.h"
#include "DNA_lattice_types.h"

#include "BLI_math.h"
#include "BLI_listbase.h"
#include "BLI_utildefines.h"

#include "BKE_curve.h"
#include "BKE_mball.h"
#include "BKE_mesh.h"
#include "BKE_armature.h"
#include "BKE_lattice.h"

#include "DEG_depsgraph.h"

#include "WM_types.h"

#include "ED_object.h"

#include "MEM_guardedalloc.h"

/* -------------------------------------------------------------------- */
/** \name Internal Transform Get/Apply
 *
 * Some object data types don't have utility functions to access their transformation data.
 * Define these locally.
 *
 * \{ */

/* Armature */

struct ElemData_Armature {
  float tail[3];
  float head[3];
  float roll;
  float arm_tail[3];
  float arm_head[3];
  float arm_roll;
  float rad_tail;
  float rad_head;
  float dist;
  float xwidth;
  float zwidth;
};

static struct ElemData_Armature *armature_coords_and_quats_get_recurse(
    const ListBase *bone_base, struct ElemData_Armature *elem_array)
{
  struct ElemData_Armature *elem = elem_array;
  for (const Bone *bone = bone_base->first; bone; bone = bone->next) {

#define COPY_PTR(member) memcpy(elem->member, bone->member, sizeof(bone->member))
#define COPY_VAL(member) memcpy(&elem->member, &bone->member, sizeof(bone->member))
    COPY_PTR(head);
    COPY_PTR(tail);
    COPY_VAL(roll);
    COPY_PTR(arm_head);
    COPY_PTR(arm_tail);
    COPY_VAL(arm_roll);
    COPY_VAL(rad_tail);
    COPY_VAL(rad_head);
    COPY_VAL(dist);
    COPY_VAL(xwidth);
    COPY_VAL(zwidth);
#undef COPY_PTR
#undef COPY_VAL

    elem = armature_coords_and_quats_get_recurse(&bone->childbase, elem + 1);
  }
  return elem;
}

static void armature_coords_and_quats_get(const bArmature *arm,
                                          struct ElemData_Armature *elem_array)
{
  armature_coords_and_quats_get_recurse(&arm->bonebase, elem_array);
}

static const struct ElemData_Armature *armature_coords_and_quats_apply_with_mat4_recurse(
    ListBase *bone_base, const struct ElemData_Armature *elem_array, const float mat[4][4])
{
  const struct ElemData_Armature *elem = elem_array;
  for (Bone *bone = bone_base->first; bone; bone = bone->next) {

#define COPY_PTR(member) memcpy(bone->member, elem->member, sizeof(bone->member))
#define COPY_VAL(member) memcpy(&bone->member, &elem->member, sizeof(bone->member))
    COPY_PTR(head);
    COPY_PTR(tail);
    COPY_VAL(roll);
    COPY_PTR(arm_head);
    COPY_PTR(arm_tail);
    COPY_VAL(arm_roll);
    COPY_VAL(rad_tail);
    COPY_VAL(rad_head);
    COPY_VAL(dist);
    COPY_VAL(xwidth);
    COPY_VAL(zwidth);
#undef COPY_PTR
#undef COPY_VAL

    elem = armature_coords_and_quats_apply_with_mat4_recurse(&bone->childbase, elem + 1, mat);
  }
  return elem;
}

static void armature_coords_and_quats_apply_with_mat4(bArmature *arm,
                                                      const struct ElemData_Armature *elem_array,
                                                      const float mat[4][4])
{
  armature_coords_and_quats_apply_with_mat4_recurse(&arm->bonebase, elem_array, mat);
  BKE_armature_transform(arm, mat, true);
}

/* MetaBall */

struct ElemData_MetaBall {
  float co[3];
  float quat[4];
  float exp[3];
  float rad;
};

static void metaball_coords_and_quats_get(const MetaBall *mb, struct ElemData_MetaBall *elem_array)
{
  struct ElemData_MetaBall *elem = elem_array;
  for (const MetaElem *ml = mb->elems.first; ml; ml = ml->next, elem++) {
    copy_v3_v3(elem->co, &ml->x);
    copy_qt_qt(elem->quat, ml->quat);
    copy_v3_v3(elem->exp, &ml->expx);
    elem->rad = ml->rad;
  }
}

static void metaball_coords_and_quats_apply_with_mat4(MetaBall *mb,
                                                      const struct ElemData_MetaBall *elem_array,
                                                      const float mat[4][4])
{
  const struct ElemData_MetaBall *elem = elem_array;
  for (MetaElem *ml = mb->elems.first; ml; ml = ml->next, elem++) {
    copy_v3_v3(&ml->x, elem->co);
    copy_qt_qt(ml->quat, elem->quat);
    copy_v3_v3(&ml->expx, elem->exp);
    ml->rad = elem->rad;
  }
  BKE_mball_transform(mb, mat, true);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Public Object Data Storage API
 *
 * Used for interactively transforming object data.
 *
 * Store object data transformation in an opaque struct.
 * \{ */

struct XFormObjectData {
  ID *id;
};

struct XFormObjectData_Mesh {
  struct XFormObjectData base;
  float elem_array[0][3];
};

struct XFormObjectData_Lattice {
  struct XFormObjectData base;
  float elem_array[0][3];
};

struct XFormObjectData_Curve {
  struct XFormObjectData base;
  float elem_array[0][3];
};

struct XFormObjectData_Armature {
  struct XFormObjectData base;
  struct ElemData_Armature elem_array[0];
};

struct XFormObjectData_MetaBall {
  struct XFormObjectData base;
  struct ElemData_MetaBall elem_array[0];
};

struct XFormObjectData *ED_object_data_xform_create(ID *id)
{
  struct XFormObjectData *xod_base = NULL;
  switch (GS(id->name)) {
    case ID_ME: {
      Mesh *me = (Mesh *)id;
      const int elem_array_len = me->totvert;
      struct XFormObjectData_Mesh *xod = MEM_mallocN(
          sizeof(*xod) + (sizeof(*xod->elem_array) * elem_array_len), __func__);
      BKE_mesh_vert_coords_get(me, xod->elem_array);
      xod_base = &xod->base;
      break;
    }
    case ID_LT: {
      Lattice *lt = (Lattice *)id;
      const int elem_array_len = lt->pntsu * lt->pntsv * lt->pntsw;
      struct XFormObjectData_Lattice *xod = MEM_mallocN(
          sizeof(*xod) + (sizeof(*xod->elem_array) * elem_array_len), __func__);
      BKE_lattice_vert_coords_get(lt, xod->elem_array);
      xod_base = &xod->base;
      break;
    }
    case ID_CU: {
      Curve *cu = (Curve *)id;
      const short ob_type = BKE_curve_type_get(cu);
      if (ob_type == OB_FONT) {
        /* We could support translation. */
        break;
      }
      const int elem_array_len = BKE_nurbList_verts_count(&cu->nurb);
      struct XFormObjectData_Curve *xod = MEM_mallocN(
          sizeof(*xod) + (sizeof(*xod->elem_array) * elem_array_len), __func__);
      BKE_curve_nurbs_vert_coords_get(&cu->nurb, xod->elem_array, elem_array_len);
      xod_base = &xod->base;
      break;
    }
    case ID_AR: {
      bArmature *arm = (bArmature *)id;
      const int elem_array_len = BKE_armature_bonelist_count(&arm->bonebase);
      struct XFormObjectData_Armature *xod = MEM_mallocN(
          sizeof(*xod) + (sizeof(*xod->elem_array) * elem_array_len), __func__);
      armature_coords_and_quats_get(arm, xod->elem_array);
      xod_base = &xod->base;
      break;
    }
    case ID_MB: {
      MetaBall *mb = (MetaBall *)id;
      const int elem_array_len = BLI_listbase_count(&mb->elems);
      struct XFormObjectData_MetaBall *xod = MEM_mallocN(
          sizeof(*xod) + (sizeof(*xod->elem_array) * elem_array_len), __func__);
      metaball_coords_and_quats_get(mb, xod->elem_array);
      xod_base = &xod->base;
      break;
    }
    default: {
      break;
    }
  }
  if (xod_base) {
    xod_base->id = id;
  }
  return xod_base;
}

void ED_object_data_xform_destroy(struct XFormObjectData *xod)
{
  MEM_freeN(xod);
}

void ED_object_data_xform_by_mat4(struct XFormObjectData *xod_base, const float mat[4][4])
{
  switch (GS(xod_base->id->name)) {
    case ID_ME: {
      Mesh *me = (Mesh *)xod_base->id;
      struct XFormObjectData_Mesh *xod = (struct XFormObjectData_Mesh *)xod_base;
      BKE_mesh_vert_coords_apply_with_mat4(me, xod->elem_array, mat);
      break;
    }
    case ID_LT: {
      Lattice *lt = (Lattice *)xod_base->id;
      struct XFormObjectData_Lattice *xod = (struct XFormObjectData_Lattice *)xod_base;
      BKE_lattice_vert_coords_apply_with_mat4(lt, xod->elem_array, mat);
      break;
    }
    case ID_CU: {
      Curve *cu = (Curve *)xod_base->id;
      struct XFormObjectData_Curve *xod = (struct XFormObjectData_Curve *)xod_base;
      BKE_curve_nurbs_vert_coords_apply_with_mat4(&cu->nurb, xod->elem_array, mat, true);
      break;
    }
    case ID_AR: {
      bArmature *arm = (bArmature *)xod_base->id;
      struct XFormObjectData_Armature *xod = (struct XFormObjectData_Armature *)xod_base;
      armature_coords_and_quats_apply_with_mat4(arm, xod->elem_array, mat);
      break;
    }
    case ID_MB: {
      MetaBall *mb = (MetaBall *)xod_base->id;
      struct XFormObjectData_MetaBall *xod = (struct XFormObjectData_MetaBall *)xod_base;
      metaball_coords_and_quats_apply_with_mat4(mb, xod->elem_array, mat);
      break;
    }
    default: {
      break;
    }
  }
}

/** \} */