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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/*
* ***** 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.
*
* Contributor(s): Blender Foundation, full recode and added functions
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/object/object_iterators.c
* \ingroup edobj
*/
#include "DNA_curve_types.h"
#include "DNA_lattice_types.h"
#include "DNA_meta_types.h"
#include "DNA_armature_types.h"
#include "DNA_object_types.h"
#include "BLI_utildefines.h"
#include "BLI_listbase.h"
#include "BLI_rect.h"
#include "BKE_armature.h"
#include "BKE_curve.h"
#include "BKE_DerivedMesh.h"
#include "BKE_displist.h"
#include "bmesh.h"
#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_armature.h"
#include "ED_object.h"
#include "ED_view3d.h"
typedef struct foreachScreenVert_userData {
void (*func)(void *userData, BMVert *eve, const float screen_co_b[2], int index);
void *userData;
ViewContext vc;
eV3DProjTest clip_flag;
} foreachScreenVert_userData;
/* user data structures for derived mesh callbacks */
typedef struct foreachScreenEdge_userData {
void (*func)(void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index);
void *userData;
ViewContext vc;
rctf win_rect; /* copy of: vc.ar->winx/winy, use for faster tests, minx/y will always be 0 */
eV3DProjTest clip_flag;
} foreachScreenEdge_userData;
typedef struct foreachScreenFace_userData {
void (*func)(void *userData, BMFace *efa, const float screen_co_b[2], int index);
void *userData;
ViewContext vc;
eV3DProjTest clip_flag;
} foreachScreenFace_userData;
/* Note! - foreach funcs should be called while drawing or directly after
* if not, ED_view3d_init_mats_rv3d() can be used for selection tools
* but would not give correct results with dupli's for eg. which don't
* use the object matrix in the usual way */
/* ------------------------------------------------------------------------ */
static void mesh_foreachScreenVert__mapFunc(void *userData, int index, const float co[3],
const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
{
foreachScreenVert_userData *data = userData;
BMVert *eve = EDBM_vert_at_index(data->vc.em, index);
if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
float screen_co[2];
if (ED_view3d_project_float_object(data->vc.ar, co, screen_co, data->clip_flag) != V3D_PROJ_RET_OK) {
return;
}
data->func(data->userData, eve, screen_co, index);
}
}
void mesh_foreachScreenVert(
ViewContext *vc,
void (*func)(void *userData, BMVert *eve, const float screen_co[2], int index),
void *userData, eV3DProjTest clip_flag)
{
foreachScreenVert_userData data;
DerivedMesh *dm = editbmesh_get_derived_cage(vc->scene, vc->obedit, vc->em, CD_MASK_BAREMESH);
data.vc = *vc;
data.func = func;
data.userData = userData;
data.clip_flag = clip_flag;
if (clip_flag & V3D_PROJ_TEST_CLIP_BB) {
ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
}
EDBM_index_arrays_init(vc->em, 1, 0, 0);
dm->foreachMappedVert(dm, mesh_foreachScreenVert__mapFunc, &data);
EDBM_index_arrays_free(vc->em);
dm->release(dm);
}
/* ------------------------------------------------------------------------ */
static void mesh_foreachScreenEdge__mapFunc(void *userData, int index, const float v0co[3], const float v1co[3])
{
foreachScreenEdge_userData *data = userData;
BMEdge *eed = EDBM_edge_at_index(data->vc.em, index);
if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
float screen_co_a[2];
float screen_co_b[2];
if (ED_view3d_project_float_object(data->vc.ar, v0co, screen_co_a, data->clip_flag) != V3D_PROJ_RET_OK) {
return;
}
if (ED_view3d_project_float_object(data->vc.ar, v1co, screen_co_b, data->clip_flag) != V3D_PROJ_RET_OK) {
return;
}
if (data->clip_flag & V3D_PROJ_TEST_CLIP_WIN) {
if (!BLI_rctf_isect_segment(&data->win_rect, screen_co_a, screen_co_b)) {
return;
}
}
data->func(data->userData, eed, screen_co_a, screen_co_b, index);
}
}
void mesh_foreachScreenEdge(
ViewContext *vc,
void (*func)(void *userData, BMEdge *eed, const float screen_co_a[2], const float screen_co_b[2], int index),
void *userData, eV3DProjTest clip_flag)
{
foreachScreenEdge_userData data;
DerivedMesh *dm = editbmesh_get_derived_cage(vc->scene, vc->obedit, vc->em, CD_MASK_BAREMESH);
data.vc = *vc;
data.win_rect.xmin = 0;
data.win_rect.ymin = 0;
data.win_rect.xmax = vc->ar->winx;
data.win_rect.ymax = vc->ar->winy;
data.func = func;
data.userData = userData;
data.clip_flag = clip_flag;
if (clip_flag & V3D_PROJ_TEST_CLIP_BB) {
ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
}
EDBM_index_arrays_init(vc->em, 0, 1, 0);
dm->foreachMappedEdge(dm, mesh_foreachScreenEdge__mapFunc, &data);
EDBM_index_arrays_free(vc->em);
dm->release(dm);
}
/* ------------------------------------------------------------------------ */
static void mesh_foreachScreenFace__mapFunc(void *userData, int index, const float cent[3], const float UNUSED(no[3]))
{
foreachScreenFace_userData *data = userData;
BMFace *efa = EDBM_face_at_index(data->vc.em, index);
if (efa && !BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
float screen_co[2];
if (ED_view3d_project_float_object(data->vc.ar, cent, screen_co, data->clip_flag) == V3D_PROJ_RET_OK)
{
data->func(data->userData, efa, screen_co, index);
}
}
}
void mesh_foreachScreenFace(
ViewContext *vc,
void (*func)(void *userData, BMFace *efa, const float screen_co_b[2], int index),
void *userData, const eV3DProjTest clip_flag)
{
foreachScreenFace_userData data;
DerivedMesh *dm = editbmesh_get_derived_cage(vc->scene, vc->obedit, vc->em, CD_MASK_BAREMESH);
data.vc = *vc;
data.func = func;
data.userData = userData;
data.clip_flag = clip_flag;
ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
EDBM_index_arrays_init(vc->em, 0, 0, 1);
dm->foreachMappedFaceCenter(dm, mesh_foreachScreenFace__mapFunc, &data);
EDBM_index_arrays_free(vc->em);
dm->release(dm);
}
/* ------------------------------------------------------------------------ */
void nurbs_foreachScreenVert(
ViewContext *vc,
void (*func)(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, const float screen_co_b[2]),
void *userData, const eV3DProjTest clip_flag)
{
Curve *cu = vc->obedit->data;
Nurb *nu;
int i;
ListBase *nurbs = BKE_curve_editNurbs_get(cu);
if (clip_flag & V3D_PROJ_TEST_CLIP_BB) {
ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
}
for (nu = nurbs->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
for (i = 0; i < nu->pntsu; i++) {
BezTriple *bezt = &nu->bezt[i];
if (bezt->hide == 0) {
float screen_co[2];
if (cu->drawflag & CU_HIDE_HANDLES) {
if (ED_view3d_project_float_object(vc->ar, bezt->vec[1], screen_co,
V3D_PROJ_RET_CLIP_BB | V3D_PROJ_RET_CLIP_WIN) == V3D_PROJ_RET_OK)
{
func(userData, nu, NULL, bezt, 1, screen_co);
}
}
else {
if (ED_view3d_project_float_object(vc->ar, bezt->vec[0], screen_co,
V3D_PROJ_RET_CLIP_BB | V3D_PROJ_RET_CLIP_WIN) == V3D_PROJ_RET_OK)
{
func(userData, nu, NULL, bezt, 0, screen_co);
}
if (ED_view3d_project_float_object(vc->ar, bezt->vec[1], screen_co,
V3D_PROJ_RET_CLIP_BB | V3D_PROJ_RET_CLIP_WIN) == V3D_PROJ_RET_OK)
{
func(userData, nu, NULL, bezt, 1, screen_co);
}
if (ED_view3d_project_float_object(vc->ar, bezt->vec[2], screen_co,
V3D_PROJ_RET_CLIP_BB | V3D_PROJ_RET_CLIP_WIN) == V3D_PROJ_RET_OK)
{
func(userData, nu, NULL, bezt, 2, screen_co);
}
}
}
}
}
else {
for (i = 0; i < nu->pntsu * nu->pntsv; i++) {
BPoint *bp = &nu->bp[i];
if (bp->hide == 0) {
float screen_co[2];
if (ED_view3d_project_float_object(vc->ar, bp->vec, screen_co,
V3D_PROJ_RET_CLIP_BB | V3D_PROJ_RET_CLIP_WIN) == V3D_PROJ_RET_OK)
{
func(userData, nu, bp, NULL, -1, screen_co);
}
}
}
}
}
}
/* ------------------------------------------------------------------------ */
/* ED_view3d_init_mats_rv3d must be called first */
void mball_foreachScreenElem(
struct ViewContext *vc,
void (*func)(void *userData, struct MetaElem *ml, const float screen_co_b[2]),
void *userData, const eV3DProjTest clip_flag)
{
MetaBall *mb = (MetaBall *)vc->obedit->data;
MetaElem *ml;
for (ml = mb->editelems->first; ml; ml = ml->next) {
float screen_co[2];
if (ED_view3d_project_float_object(vc->ar, &ml->x, screen_co, clip_flag) == V3D_PROJ_RET_OK)
{
func(userData, ml, screen_co);
}
}
}
/* ------------------------------------------------------------------------ */
void lattice_foreachScreenVert(
ViewContext *vc,
void (*func)(void *userData, BPoint *bp, const float screen_co[2]),
void *userData, const eV3DProjTest clip_flag)
{
Object *obedit = vc->obedit;
Lattice *lt = obedit->data;
BPoint *bp = lt->editlatt->latt->def;
DispList *dl = BKE_displist_find(&obedit->disp, DL_VERTS);
float *co = dl ? dl->verts : NULL;
int i, N = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
if (clip_flag & V3D_PROJ_TEST_CLIP_BB) {
ED_view3d_clipping_local(vc->rv3d, obedit->obmat); /* for local clipping lookups */
}
for (i = 0; i < N; i++, bp++, co += 3) {
if (bp->hide == 0) {
float screen_co[2];
if (ED_view3d_project_float_object(vc->ar, dl ? co : bp->vec, screen_co, clip_flag) == V3D_PROJ_RET_OK) {
func(userData, bp, screen_co);
}
}
}
}
/* ------------------------------------------------------------------------ */
/* ED_view3d_init_mats_rv3d must be called first */
void armature_foreachScreenBone(
struct ViewContext *vc,
void (*func)(void *userData, struct EditBone *ebone, const float screen_co_a[2], const float screen_co_b[2]),
void *userData, const eV3DProjTest clip_flag)
{
bArmature *arm = vc->obedit->data;
EditBone *ebone;
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (EBONE_VISIBLE(arm, ebone)) {
float screen_co_a[2], screen_co_b[2];
int points_proj_tot = 0;
/* project head location to screenspace */
if (ED_view3d_project_float_object(vc->ar, ebone->head, screen_co_a, clip_flag) == V3D_PROJ_RET_OK) {
points_proj_tot++;
}
else {
screen_co_a[0] = IS_CLIPPED; /* weak */
/* screen_co_a[1]: intentionally dont set this so we get errors on misuse */
}
/* project tail location to screenspace */
if (ED_view3d_project_float_object(vc->ar, ebone->tail, screen_co_b, clip_flag) == V3D_PROJ_RET_OK) {
points_proj_tot++;
}
else {
screen_co_b[0] = IS_CLIPPED; /* weak */
/* screen_co_b[1]: intentionally dont set this so we get errors on misuse */
}
if (points_proj_tot) { /* at least one point's projection worked */
func(userData, ebone, screen_co_a, screen_co_b);
}
}
}
}
/* ------------------------------------------------------------------------ */
/* ED_view3d_init_mats_rv3d must be called first */
/* almost _exact_ copy of #armature_foreachScreenBone */
void pose_foreachScreenBone(
struct ViewContext *vc,
void (*func)(void *userData, struct bPoseChannel *pchan, const float screen_co_a[2], const float screen_co_b[2]),
void *userData, const eV3DProjTest clip_flag)
{
bArmature *arm = vc->obact->data;
bPose *pose = vc->obact->pose;
bPoseChannel *pchan;
for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
if (PBONE_VISIBLE(arm, pchan->bone)) {
float screen_co_a[2], screen_co_b[2];
int points_proj_tot = 0;
/* project head location to screenspace */
if (ED_view3d_project_float_object(vc->ar, pchan->pose_head, screen_co_a, clip_flag) == V3D_PROJ_RET_OK) {
points_proj_tot++;
}
else {
screen_co_a[0] = IS_CLIPPED; /* weak */
/* screen_co_a[1]: intentionally dont set this so we get errors on misuse */
}
/* project tail location to screenspace */
if (ED_view3d_project_float_object(vc->ar, pchan->pose_tail, screen_co_b, clip_flag) == V3D_PROJ_RET_OK) {
points_proj_tot++;
}
else {
screen_co_b[0] = IS_CLIPPED; /* weak */
/* screen_co_b[1]: intentionally dont set this so we get errors on misuse */
}
if (points_proj_tot) { /* at least one point's projection worked */
func(userData, pchan, screen_co_a, screen_co_b);
}
}
}
}
|