Blender V5.3
transform_snap_object.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "BLI_listbase.hh"
12#include "BLI_math_matrix.hh"
13#include "BLI_math_matrix_c.hh"
14#include "BLI_math_vector_c.hh"
15
16#include "DNA_screen_types.h"
17
18#include "BKE_bvhutils.hh"
19#include "BKE_duplilist.hh"
21#include "BKE_layer.hh"
22#include "BKE_mesh.hh"
23#include "BKE_mesh_wrapper.hh"
24#include "BKE_object.hh"
25
27
29#include "ED_view3d.hh"
30
32
33#ifdef DEBUG_SNAP_TIME
34# include "BLI_timeit.hh"
35# include <iostream>
36
37# if WIN32 and NDEBUG
38# pragma optimize("t", on)
39# endif
40
41static int64_t total_count_ = 0;
42static timeit::Nanoseconds duration_;
43#endif
44
45namespace blender::ed::transform {
46
48 float3 ray_dir,
49 float3 ray_co,
50 float3 ray_no)
51{
52 float4 plane;
53 plane_from_point_normal_v3(plane, ray_co, ray_no);
54 if (dot_v3v3(ray_dir, plane) > 0.0f) {
55 /* The plane is facing the wrong direction. */
56 negate_v4(plane);
57 }
58
59 /* Small offset to simulate a kind of volume for edges and vertices.
60 * NOTE: The offset added to plane[3] was previously hardcoded as 0.01f
61 * but that caused snapping to pass through occluding geometry at small
62 * scales, so scale it by the view depth instead. Values of 1e-6f and
63 * above work for the scale factor, 1e-7f or smaller are lost to float
64 * precision. See: #154426. */
65 const float depth = math::dot(ray_co - ray_start, ray_dir);
66 const float scale_factor = 1e-5f;
67 plane[3] += std::max(depth * scale_factor, FLT_EPSILON);
68
69 return plane;
70}
71
73 const float (*clip_plane)[4],
74 const int clip_plane_len,
75 const bool is_persp,
76 const float *co,
77 BVHTreeNearest *nearest)
78{
79 if (!isect_point_planes_v3_negated(clip_plane, clip_plane_len, co)) {
80 return false;
81 }
82
83 float co2d[2] = {
84 (dot_m4_v3_row_x(precalc->pmat, co) + precalc->pmat[3][0]),
85 (dot_m4_v3_row_y(precalc->pmat, co) + precalc->pmat[3][1]),
86 };
87
88 if (is_persp) {
89 float w = mul_project_m4_v3_zfac(precalc->pmat, co);
90 mul_v2_fl(co2d, 1.0f / w);
91 }
92
93 const float dist_sq = len_squared_v2v2(precalc->mval, co2d);
94 if (dist_sq < nearest->dist_sq) {
95 copy_v3_v3(nearest->co, co);
96 nearest->dist_sq = dist_sq;
97 return true;
98 }
99 return false;
100}
101
103 const float (*clip_plane)[4],
104 const int clip_plane_len,
105 const bool is_persp,
106 const float va[3],
107 const float vb[3],
108 BVHTreeNearest *nearest)
109{
110 float near_co[3];
111 closest_ray_to_segment_v3(precalc->ray_origin, precalc->ray_direction, va, vb, near_co);
112 return test_projected_vert_dist(precalc, clip_plane, clip_plane_len, is_persp, near_co, nearest);
113}
114
116 const float (*clip_plane)[4],
117 const int clip_plane_len,
118 const bool is_persp,
119 const float center[3],
120 BVHTreeNearest *nearest)
121{
122 return test_projected_vert_dist(precalc, clip_plane, clip_plane_len, is_persp, center, nearest);
123}
124
126 : nearest_precalc(),
127 obmat_(obmat),
128 is_persp(sctx->runtime.rv3d ? sctx->runtime.rv3d->is_persp : false),
129 use_backface_culling(sctx->runtime.params.use_backface_culling)
130{
131 if (sctx->runtime.rv3d) {
132 this->pmat_local = float4x4(sctx->runtime.rv3d->persmat) * obmat;
133 }
134 else {
135 this->pmat_local = obmat;
136 }
137
139 &this->nearest_precalc, this->pmat_local.ptr(), sctx->runtime.win_size, sctx->runtime.mval);
140
141 this->nearest_point.index = -2;
142 this->nearest_point.dist_sq = sctx->ret.dist_px_sq;
143 copy_v3_fl3(this->nearest_point.no, 0.0f, 0.0f, 1.0f);
144}
145
147 const Object *ob_eval,
148 bool skip_occlusion_plane)
149{
150 float4x4 tobmat = math::transpose(this->obmat_);
151 if (!skip_occlusion_plane) {
152 const bool is_in_front = (sctx->runtime.params.occlusion_test == SNAP_OCCLUSION_AS_SEEM) &&
153 ob_eval && (ob_eval->dtx & OB_DRAW_IN_FRONT) != 0;
154 if (!is_in_front && sctx->runtime.has_occlusion_plane) {
155 this->clip_planes.append(tobmat * sctx->runtime.occlusion_plane);
156 }
157 else if (sctx->runtime.has_occlusion_plane_in_front) {
158 this->clip_planes.append(tobmat * sctx->runtime.occlusion_plane_in_front);
159 }
160 }
161
162 for (float4 &plane : sctx->runtime.clip_planes) {
163 this->clip_planes.append(tobmat * plane);
164 }
165}
166
168{
169 /* In vertex and edges you need to get the pixel distance from ray to bounding box,
170 * see: #46099, #46816 */
171
172#ifdef TEST_CLIPPLANES_IN_BOUNDBOX
173 int isect_type = isect_aabb_planes_v3(
174 reinterpret_cast<const float (*)[4]>(this->clip_planes.data()),
175 this->clip_planes.size(),
176 min,
177 max);
178
179 if (isect_type == ISECT_AABB_PLANE_BEHIND_ANY) {
180 return false;
181 }
182#endif
183
184 bool dummy[3];
185 float bb_dist_px_sq = dist_squared_to_projected_aabb(&this->nearest_precalc, min, max, dummy);
186 if (bb_dist_px_sq > this->nearest_point.dist_sq) {
187 return false;
188 }
189
190 return true;
191}
192
194{
196 reinterpret_cast<const float (*)[4]>(this->clip_planes.data()),
197 this->clip_planes.size(),
198 this->is_persp,
199 co,
200 &this->nearest_point))
201 {
202 this->nearest_point.index = index;
203 return true;
204 }
205 return false;
206}
207
208bool SnapData::snap_edge(const float3 &va, const float3 &vb, int edge_index)
209{
211 reinterpret_cast<const float (*)[4]>(this->clip_planes.data()),
212 this->clip_planes.size(),
213 this->is_persp,
214 va,
215 vb,
216 &this->nearest_point))
217 {
218 this->nearest_point.index = edge_index;
219 sub_v3_v3v3(this->nearest_point.no, vb, va);
220 return true;
221 }
222 return false;
223}
224
226 int edge_index,
227 float dist_px_sq_orig)
228{
230
231 int vindex[2];
232 this->get_edge_verts_index(edge_index, vindex);
233
234 const float *v_pair[2];
235 this->get_vert_co(vindex[0], &v_pair[0]);
236 this->get_vert_co(vindex[1], &v_pair[1]);
237
238 float lambda;
240 this->nearest_precalc.ray_direction,
241 v_pair[0],
242 v_pair[1],
243 &lambda))
244 {
245 /* Do nothing. */
246 }
247 else {
248 this->nearest_point.dist_sq = dist_px_sq_orig;
249
250 eSnapMode snap_to = sctx->runtime.snap_to_flag;
251 int e_mode_len = ((snap_to & SCE_SNAP_TO_EDGE) != 0) +
252 ((snap_to & SCE_SNAP_TO_EDGE_ENDPOINT) != 0) +
253 ((snap_to & SCE_SNAP_TO_EDGE_MIDPOINT) != 0);
254
255 float range = 1.0f / (2 * e_mode_len - 1);
256
257 if (snap_to & SCE_SNAP_TO_EDGE_MIDPOINT) {
258 range *= e_mode_len - 1;
259 if ((range) < lambda && lambda < (1.0f - range)) {
260 float vmid[3];
261 mid_v3_v3v3(vmid, v_pair[0], v_pair[1]);
262
263 if (this->snap_point(vmid, edge_index)) {
264 sub_v3_v3v3(this->nearest_point.no, v_pair[1], v_pair[0]);
266 }
267 }
268 }
269
270 if (snap_to & SCE_SNAP_TO_EDGE_PERPENDICULAR) {
271 float v_near[3], va_g[3], vb_g[3];
272
273 mul_v3_m4v3(va_g, this->obmat_.ptr(), v_pair[0]);
274 mul_v3_m4v3(vb_g, this->obmat_.ptr(), v_pair[1]);
275 float lambda_perp = line_point_factor_v3(sctx->runtime.curr_co, va_g, vb_g);
276
277 if (IN_RANGE(lambda_perp, 0.0f, 1.0f)) {
278 interp_v3_v3v3(v_near, v_pair[0], v_pair[1], lambda_perp);
279
280 if (this->snap_point(v_near, edge_index)) {
281 sub_v3_v3v3(this->nearest_point.no, v_pair[1], v_pair[0]);
283 }
284 }
285 }
286
287 /* Leave this one for last so it doesn't change the normal. */
288 if (snap_to & SCE_SNAP_TO_EDGE_ENDPOINT) {
289 if (lambda < (range) || (1.0f - range) < lambda) {
290 int v_id = lambda < 0.5f ? 0 : 1;
291
292 if (this->snap_point(v_pair[v_id], vindex[v_id])) {
294 this->copy_vert_no(vindex[v_id], this->nearest_point.no);
295 }
296 }
297 }
298 }
299
300 return elem;
301}
302
304 const Object *ob_eval,
305 const ID *id_eval,
306 const float4x4 &obmat,
307 BVHTreeNearest *r_nearest)
308{
309 BLI_assert(r_nearest->index != -2);
310
311 copy_v3_v3(sctx->ret.loc, r_nearest->co);
312 copy_v3_v3(sctx->ret.no, r_nearest->no);
313 sctx->ret.index = r_nearest->index;
314 sctx->ret.obmat = obmat;
315 sctx->ret.ob = ob_eval;
316 sctx->ret.data = id_eval;
317 sctx->ret.dist_px_sq = r_nearest->dist_sq;
318
319 /* Global space. */
320 sctx->ret.loc = math::transform_point(obmat, sctx->ret.loc);
321 sctx->ret.no = math::normalize(
323
324#ifndef NDEBUG
325 /* Make sure this is only called once. */
326 r_nearest->index = -2;
327#endif
328}
329
330void SnapData::register_result(SnapObjectContext *sctx, const Object *ob_eval, const ID *id_eval)
331{
332 register_result(sctx, ob_eval, id_eval, this->obmat_, &this->nearest_point);
333}
334
336 const Object *ob_eval,
337 const ID *id_eval,
338 const float4x4 &obmat,
339 const BVHTreeRayHit *hit,
340 const bool is_in_front)
341{
342 const float depth_max = is_in_front ? sctx->ret.ray_depth_max_in_front : sctx->ret.ray_depth_max;
343 if (hit->dist <= depth_max) {
344 float3 co = math::transform_point(obmat, float3(hit->co));
347
348 sctx->ret.loc = co;
349 sctx->ret.no = no;
350 sctx->ret.index = hit->index;
351 sctx->ret.obmat = obmat;
352 sctx->ret.ob = ob_eval;
353 sctx->ret.data = id_eval;
354 sctx->ret.ray_depth_max = std::min(hit->dist, sctx->ret.ray_depth_max);
355
356 if (is_in_front) {
357 sctx->runtime.occlusion_plane_in_front = occlusion_plane_create(
358 sctx->runtime.ray_start, sctx->runtime.ray_dir, co, no);
359 sctx->runtime.has_occlusion_plane_in_front = true;
360 }
361 }
362}
363
364/* -------------------------------------------------------------------- */
367
375static const ID *data_for_snap(Object *ob_eval, eSnapEditType edit_mode_type, bool *r_use_hide)
376{
377 if (r_use_hide) {
378 *r_use_hide = false;
379 }
380
381 /* Get evaluated edit mesh when in mesh edit mode. */
382 if (ob_eval->type == OB_MESH && BKE_object_is_in_editmode(ob_eval)) {
383 if (edit_mode_type == SNAP_GEOM_EDIT) {
384 return nullptr;
385 }
386
387 const Mesh *editmesh_eval = (edit_mode_type == SNAP_GEOM_FINAL) ?
389 (edit_mode_type == SNAP_GEOM_CAGE) ?
391 nullptr;
392
393 if (editmesh_eval) {
394 if (editmesh_eval->runtime->wrapper_type == ME_WRAPPER_TYPE_BMESH) {
395 return nullptr;
396 }
397 if (*r_use_hide) {
398 *r_use_hide = true;
399 }
400 return &editmesh_eval->id;
401 }
402 }
403
404 /* For curves and surfaces in edit mode, use their original data when snapping.
405 * Only use the evaluated mesh when snapping to the final geometry. */
406 if (ELEM(ob_eval->type, OB_CURVES_LEGACY, OB_SURF) && BKE_object_is_in_editmode(ob_eval) &&
407 edit_mode_type != SNAP_GEOM_FINAL)
408 {
409 return static_cast<const ID *>(ob_eval->data);
410 }
411
412 /* Get evaluated mesh including subdivision. This may come from a mesh object,
413 * or another object type that has modifiers producing a mesh. */
414 if (Mesh *mesh_eval = BKE_object_get_evaluated_mesh(ob_eval)) {
415 return &mesh_eval->id;
416 }
417
418 return static_cast<const ID *>(ob_eval->data);
419}
420
425static const ID *data_for_snap_dupli(ID *ob_data)
426{
427 if (GS(ob_data->name) == ID_ME) {
428 Mesh *mesh = id_cast<Mesh *>(ob_data);
429 return reinterpret_cast<const ID *>(BKE_mesh_wrapper_ensure_subdivision(mesh));
430 }
431 return ob_data;
432}
433
435
436/* -------------------------------------------------------------------- */
439
441 const Object *ob_eval,
442 const ID *ob_data,
443 const float4x4 &obmat,
444 bool is_object_active,
445 bool use_hide);
446
448 const eSnapTargetOP snap_target_select,
449 const Base *base_act,
450 const Base *base)
451{
452 if (!BASE_VISIBLE(sctx->runtime.v3d, base)) {
453 return false;
454 }
455
456 if (snap_target_select == SCE_SNAP_TARGET_ALL) {
457 return true;
458 }
459
460 /* Get attributes of potential target. */
461 const bool is_active = (base_act == base);
462 const bool is_selected = (base->flag & BASE_SELECTED) || (base->flag_legacy & BA_WAS_SEL);
463 const bool is_edited = (base->object->mode == OB_MODE_EDIT);
464 const bool is_selectable = (base->flag & BASE_SELECTABLE);
465 /* Get attributes of state. */
466 const bool is_in_object_mode = (base_act == nullptr) ||
467 (base_act->object->mode == OB_MODE_OBJECT);
468
469 if (is_in_object_mode) {
470 /* Handle target selection options that make sense for object mode. */
471 if ((snap_target_select & SCE_SNAP_TARGET_NOT_SELECTED) && is_selected) {
472 /* What is selectable or not is part of the object and depends on the mode. */
473 return false;
474 }
475 }
476 else {
477 /* Handle target selection options that make sense for edit/pose mode. */
478 if ((snap_target_select & SCE_SNAP_TARGET_NOT_ACTIVE) && is_active) {
479 return false;
480 }
481 if ((snap_target_select & SCE_SNAP_TARGET_NOT_EDITED) && is_edited && !is_active) {
482 /* Base is edited, but not active. */
483 return false;
484 }
485 if ((snap_target_select & SCE_SNAP_TARGET_NOT_NONEDITED) && !is_edited) {
486 return false;
487 }
488 }
489
490 if ((snap_target_select & SCE_SNAP_TARGET_ONLY_SELECTABLE) && !is_selectable) {
491 return false;
492 }
493
494 return true;
495}
496
501{
503 eSnapMode tmp;
504
505 Scene *scene = DEG_get_input_scene(sctx->runtime.depsgraph);
506 ViewLayer *view_layer = DEG_get_input_view_layer(sctx->runtime.depsgraph);
507 const eSnapTargetOP snap_target_select = sctx->runtime.params.snap_target_select;
508 BKE_view_layer_synced_ensure(*DEG_get_bmain(sctx->runtime.depsgraph), scene, view_layer);
509 Base *base_act = BKE_view_layer_active_base_get(view_layer);
510
511 DupliList duplilist;
512 for (Base &base : *BKE_view_layer_object_bases_get(view_layer)) {
513 if (!snap_object_is_snappable(sctx, snap_target_select, base_act, &base)) {
514 continue;
515 }
516
517 const bool is_object_active = (&base == base_act);
518 Object *obj_eval = DEG_get_evaluated(sctx->runtime.depsgraph, base.object);
519
520 bool use_hide = false;
521 const ID *ob_data = data_for_snap(obj_eval, sctx->runtime.params.edit_mode_type, &use_hide);
522 if ((tmp = sob_callback(
523 sctx, obj_eval, ob_data, obj_eval->object_to_world(), is_object_active, use_hide)) !=
525 {
526 ret = tmp;
527 }
528
529 /*Skip if self*/
530 if (base.flag_legacy & BA_SNAP_FIX_DEPS_FIASCO) {
531 continue;
532 }
533
534 if (obj_eval->transflag & OB_DUPLI || bke::object_has_geometry_set_instances(*obj_eval)) {
535 object_duplilist(sctx->runtime.depsgraph, obj_eval, nullptr, duplilist);
536 for (DupliObject &dupli_ob : duplilist) {
537 BLI_assert(DEG_is_evaluated(dupli_ob.ob));
538 const ID *ob_data = dupli_ob.ob_data ? data_for_snap_dupli(dupli_ob.ob_data) : nullptr;
539 if ((tmp = sob_callback(
540 sctx, dupli_ob.ob, ob_data, float4x4(dupli_ob.mat), is_object_active, false)) !=
542 {
543 ret = tmp;
544 }
545 }
546 duplilist.clear();
547 }
548 }
549 return ret;
550}
551
553
554/* -------------------------------------------------------------------- */
557
558/* Store all ray-hits
559 * Support for storing all depths, not just the first (ray-cast 'all'). */
560
561static SnapObjectHitDepth *hit_depth_create(const float depth, const float co[3], uint ob_uuid)
562{
563 SnapObjectHitDepth *hit = MEM_new<SnapObjectHitDepth>(__func__);
564
565 hit->depth = depth;
566 copy_v3_v3(hit->co, co);
567 hit->ob_uuid = ob_uuid;
568
569 return hit;
570}
571
572static int hit_depth_cmp(const void *arg1, const void *arg2)
573{
574 const SnapObjectHitDepth *h1 = static_cast<const SnapObjectHitDepth *>(arg1);
575 const SnapObjectHitDepth *h2 = static_cast<const SnapObjectHitDepth *>(arg2);
576 int val = 0;
577
578 if (h1->depth < h2->depth) {
579 val = -1;
580 }
581 else if (h1->depth > h2->depth) {
582 val = 1;
583 }
584
585 return val;
586}
587
588void raycast_all_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
589{
590 RayCastAll_Data *data = static_cast<RayCastAll_Data *>(userdata);
591 data->raycast_callback(data->bvhdata, index, ray, hit);
592 if (hit->index != -1) {
593 /* Get all values in world-space. */
594 float location[3];
595 float depth;
596
597 /* World-space location. */
599 location, reinterpret_cast<float (*)[4]>(const_cast<float4x4 *>(data->obmat)), hit->co);
600 depth = (hit->dist + data->len_diff) / data->local_scale;
601
602 SnapObjectHitDepth *hit_item = hit_depth_create(depth, location, data->ob_uuid);
603 BLI_addtail(data->hit_list, hit_item);
604 }
605}
606
608 const float dir[3], const float v0[3], const float v1[3], const float v2[3], float no[3])
609{
610 cross_tri_v3(no, v0, v1, v2);
611 return dot_v3v3(no, dir) < 0.0f;
612}
613
618 const Object *ob_eval,
619 const ID *ob_data,
620 const float4x4 &obmat,
621 bool /*is_object_active*/,
622 bool use_hide)
623{
624 bool retval = false;
625
626 if (ob_eval->visibility_flag & OB_HIDE_SURFACE_PICK) {
627 /* Do not snap it surface picking is disabled. */
628 return SCE_SNAP_TO_NONE;
629 }
630
631 if (ob_data == nullptr) {
632 if ((sctx->runtime.occlusion_test_edit == SNAP_OCCLUSION_AS_SEEM) &&
633 ELEM(ob_eval->dt, OB_BOUNDBOX, OB_WIRE))
634 {
635 /* Do not hit objects that are in wire or bounding box display mode. */
636 return SCE_SNAP_TO_NONE;
637 }
638 if (ob_eval->type == OB_MESH) {
639 if (snap_object_editmesh(sctx, ob_eval, nullptr, obmat, SCE_SNAP_TO_FACE, use_hide)) {
640 retval = true;
641 }
642 }
643 else {
644 return SCE_SNAP_TO_NONE;
645 }
646 }
647 else if ((sctx->runtime.params.occlusion_test == SNAP_OCCLUSION_AS_SEEM) &&
648 ELEM(ob_eval->dt, OB_BOUNDBOX, OB_WIRE))
649 {
650 /* Do not hit objects that are in wire or bounding box display mode. */
651 return SCE_SNAP_TO_NONE;
652 }
653 else if (GS(ob_data->name) != ID_ME) {
654 return SCE_SNAP_TO_NONE;
655 }
656 else if (ELEM(ob_eval->type, OB_CURVES_LEGACY, OB_SURF) &&
657 (sctx->runtime.params.edit_mode_type != SNAP_GEOM_FINAL) &&
659 {
660 /* The final Curves geometry is generated as a Mesh. Skip this Mesh if the target is not
661 * #SNAP_GEOM_FINAL. This also avoids creating and destroying BVH Trees too frequently while
662 * editing. */
663 return SCE_SNAP_TO_NONE;
664 }
665 else {
666 retval = snap_object_mesh(sctx, ob_eval, ob_data, obmat, SCE_SNAP_TO_FACE, use_hide);
667 }
668
669 if (retval) {
670 return SCE_SNAP_TO_FACE;
671 }
672 return SCE_SNAP_TO_NONE;
673}
674
690{
692}
693
695
696/* -------------------------------------------------------------------- */
699
700static void nearest_world_tree_co(const BVHTree *tree,
702 void *treedata,
703 const float3 &co,
704 BVHTreeNearest *r_nearest)
705{
706 r_nearest->index = -1;
707 copy_v3_fl(r_nearest->co, FLT_MAX);
708 r_nearest->dist_sq = FLT_MAX;
709
710 BLI_bvhtree_find_nearest(tree, co, r_nearest, nearest_cb, treedata);
711}
712
714 const BVHTree *tree,
716 const float4x4 &obmat,
717 void *treedata,
718 BVHTreeNearest *r_nearest)
719{
720 float4x4 imat = math::invert(obmat);
721 float3 init_co = math::transform_point(imat, sctx->runtime.init_co);
722 float3 curr_co = math::transform_point(imat, sctx->runtime.curr_co);
723
724 BVHTreeNearest nearest{};
725 float3 vec;
726 if (sctx->runtime.params.keep_on_same_target) {
727 nearest_world_tree_co(tree, nearest_cb, treedata, init_co, &nearest);
728 vec = float3(nearest.co) - init_co;
729 }
730 else {
731 /* NOTE: when `params->face_nearest_steps == 1`, the return variables of function below contain
732 * the answer. We could return immediately after updating r_loc, r_no, r_index, but that would
733 * also complicate the code. Foregoing slight optimization for code clarity. */
734 nearest_world_tree_co(tree, nearest_cb, treedata, curr_co, &nearest);
735 vec = float3(nearest.co) - curr_co;
736 }
737
738 float original_distance = math::length_squared(math::transform_direction(obmat, vec));
739 if (r_nearest->dist_sq <= original_distance) {
740 return false;
741 }
742
743 /* Scale to make `snap_face_nearest_steps` steps. */
744 float step_scale_factor = 1.0f / max_ff(1.0f, float(sctx->runtime.params.face_nearest_steps));
745
746 /* Compute offset between init co and prev co. */
747 float3 delta = (curr_co - init_co) * step_scale_factor;
748
749 float3 co = init_co;
750 for (int i = 0; i < sctx->runtime.params.face_nearest_steps; i++) {
751 co += delta;
752 nearest_world_tree_co(tree, nearest_cb, treedata, co, &nearest);
753 co = nearest.co;
754 }
755
756 *r_nearest = nearest;
757 if (sctx->runtime.params.keep_on_same_target) {
758 r_nearest->dist_sq = original_distance;
759 }
760 else if (sctx->runtime.params.face_nearest_steps > 1) {
761 /* Recalculate the distance.
762 * When multiple steps are tested, we cannot depend on the distance calculated for
763 * `nearest.dist_sq`, as it reduces with each step. */
764 vec = co - curr_co;
766 }
767 return true;
768}
769
771 const Object *ob_eval,
772 const ID *ob_data,
773 const float4x4 &obmat,
774 bool /*is_object_active*/,
775 bool use_hide)
776{
778
779 if (ob_data == nullptr) {
780 if (ob_eval->type == OB_MESH) {
781 retval = snap_object_editmesh(
782 sctx, ob_eval, nullptr, obmat, SCE_SNAP_INDIVIDUAL_NEAREST, use_hide);
783 }
784 else {
785 return SCE_SNAP_TO_NONE;
786 }
787 }
788 else if (GS(ob_data->name) != ID_ME) {
789 return SCE_SNAP_TO_NONE;
790 }
791 else if (ELEM(ob_eval->type, OB_CURVES_LEGACY, OB_SURF) &&
792 (sctx->runtime.params.edit_mode_type != SNAP_GEOM_FINAL) &&
794 {
795 /* The final Curves geometry is generated as a Mesh. Skip this Mesh if the target is not
796 * #SNAP_GEOM_FINAL. This also avoids creating and destroying BVH Trees too frequently while
797 * editing. */
798 return SCE_SNAP_TO_NONE;
799 }
800 else {
801 retval = snap_object_mesh(
802 sctx, ob_eval, ob_data, obmat, SCE_SNAP_INDIVIDUAL_NEAREST, use_hide);
803 }
804
805 return retval;
806}
807
823
825
826/* -------------------------------------------------------------------- */
829
830void cb_snap_vert(void *userdata,
831 int index,
832 const DistProjectedAABBPrecalc *precalc,
833 const float (*clip_plane)[4],
834 const int clip_plane_len,
835 BVHTreeNearest *nearest)
836{
837 SnapData *data = static_cast<SnapData *>(userdata);
838
839 const float *co;
840 data->get_vert_co(index, &co);
841
842 if (test_projected_vert_dist(precalc, clip_plane, clip_plane_len, data->is_persp, co, nearest)) {
843 data->copy_vert_no(index, nearest->no);
844 nearest->index = index;
845 }
846}
847
848void cb_snap_edge(void *userdata,
849 int index,
850 const DistProjectedAABBPrecalc *precalc,
851 const float (*clip_plane)[4],
852 const int clip_plane_len,
853 BVHTreeNearest *nearest)
854{
855 SnapData *data = static_cast<SnapData *>(userdata);
856
857 int vindex[2];
858 data->get_edge_verts_index(index, vindex);
859
860 const float *v_pair[2];
861 data->get_vert_co(vindex[0], &v_pair[0]);
862 data->get_vert_co(vindex[1], &v_pair[1]);
863
865 precalc, clip_plane, clip_plane_len, data->is_persp, v_pair[0], v_pair[1], nearest))
866 {
867 sub_v3_v3v3(nearest->no, v_pair[1], v_pair[0]);
868 nearest->index = index;
869 }
870}
871
872void cb_snap_face_midpoint(void *userdata,
873 const int face_index,
874 const DistProjectedAABBPrecalc *precalc,
875 const float (*clip_plane)[4],
876 const int clip_plane_len,
877 BVHTreeNearest *nearest)
878{
879 SnapData *data = static_cast<SnapData *>(userdata);
880
881 float3 center;
882 data->get_face_center(face_index, center);
883
885 precalc, clip_plane, clip_plane_len, data->is_persp, center, nearest))
886 {
887 data->copy_face_no(face_index, nearest->no);
888 nearest->index = face_index;
889 }
890}
891
893
894/* -------------------------------------------------------------------- */
897
899{
900 if (!sctx->ret.data || GS(sctx->ret.data->name) != ID_ME) {
901 return SCE_SNAP_TO_NONE;
902 }
903
904 return snap_polygon_mesh(
905 sctx, sctx->ret.ob, sctx->ret.data, sctx->ret.obmat, snap_to_flag, sctx->ret.index);
906}
907
908static eSnapMode snap_edge_points(SnapObjectContext *sctx, const float dist_px_sq_orig)
909{
910 if (!sctx->ret.data || GS(sctx->ret.data->name) != ID_ME) {
911 return SCE_SNAP_TO_EDGE;
912 }
913
915 sctx, sctx->ret.ob, sctx->ret.data, sctx->ret.obmat, dist_px_sq_orig, sctx->ret.index);
916}
917
919 const Object *ob_eval,
920 const float4x4 &obmat,
921 eSnapMode snap_to_flag)
922{
923
925 if ((snap_to_flag & SCE_SNAP_TO_POINT) && ELEM(ob_eval->type, OB_EMPTY, OB_LAMP, OB_CAMERA)) {
926 retval = SCE_SNAP_TO_POINT;
927 }
928 else if (snap_to_flag & SCE_SNAP_TO_ORIGIN) {
929 retval = SCE_SNAP_TO_ORIGIN;
930 }
931 else {
932 return SCE_SNAP_TO_NONE;
933 }
934
935 SnapData nearest2d(sctx, obmat);
936
937 nearest2d.clip_planes_enable(sctx, ob_eval, true);
938
939 if (nearest2d.snap_point(float3(0.0f))) {
940 nearest2d.register_result(sctx, ob_eval, static_cast<const ID *>(ob_eval->data));
941 return retval;
942 }
943
944 return SCE_SNAP_TO_NONE;
945}
946
951 const Object *ob_eval,
952 const ID *ob_data,
953 const float4x4 &obmat,
954 bool is_object_active,
955 bool use_hide)
956{
957
959
960 if (ob_data == nullptr) {
961 if (ob_eval->type == OB_MESH) {
962 retval = snap_object_editmesh(
963 sctx, ob_eval, nullptr, obmat, sctx->runtime.snap_to_flag, use_hide);
964
965 if (retval != SCE_SNAP_TO_NONE) {
966 return retval;
967 }
968 }
969
970 return snap_object_center(sctx, ob_eval, obmat, sctx->runtime.snap_to_flag);
971 }
972
973 if (ob_eval->dt == OB_BOUNDBOX) {
974 /* Do not snap to objects that are in bounding box display mode. */
975 return SCE_SNAP_TO_NONE;
976 }
977
978 if (GS(ob_data->name) == ID_ME) {
979 if (ELEM(ob_eval->type, OB_CURVES_LEGACY, OB_SURF) &&
980 (sctx->runtime.params.edit_mode_type != SNAP_GEOM_FINAL) &&
982 {
983 /* The final Curves geometry is generated as a Mesh. Skip this Mesh if the target is not
984 * #SNAP_GEOM_FINAL. */
985 retval = SCE_SNAP_TO_NONE;
986 }
987 else {
988 retval = snap_object_mesh(
989 sctx, ob_eval, ob_data, obmat, sctx->runtime.snap_to_flag, use_hide);
990 }
991 }
992
993 switch (ob_eval->type) {
994 case OB_ARMATURE:
995 retval = snapArmature(sctx, ob_eval, obmat, is_object_active);
996 break;
997 case OB_LATTICE:
998 retval = snapLattice(sctx, ob_eval, obmat);
999 break;
1000 case OB_CURVES_LEGACY:
1001 case OB_SURF:
1002 if (ob_eval->type == OB_CURVES_LEGACY || BKE_object_is_in_editmode(ob_eval)) {
1003 retval = snapCurve(sctx, ob_eval, obmat);
1004 }
1005 break;
1006 case OB_CAMERA:
1007 retval = snapCamera(sctx, ob_eval, obmat);
1008 break;
1009 /* TODO: Add remaining specific handling of objects (grease pencil, meta-ball, ...). */
1010 default:
1011 break;
1012 }
1013
1014 if (retval == SCE_SNAP_TO_NONE) {
1015 return snap_object_center(sctx, ob_eval, obmat, sctx->runtime.snap_to_flag);
1016 }
1017
1018 return retval;
1019}
1020
1035{
1036 return iter_snap_objects(sctx, snap_obj_fn);
1037}
1038
1040{
1041 SnapData nearest2d(sctx);
1042 nearest2d.clip_planes_enable(sctx, nullptr, true);
1043
1044 /* Ignore the maximum pixel distance when snapping to grid.
1045 * This avoids undesirable jumps of the element being snapped. */
1046 nearest2d.nearest_point.dist_sq = FLT_MAX;
1047
1048 float grid_dist = sctx->grid.size;
1049
1050 if (sctx->grid.use_init_co) {
1051 float3 co = math::round((sctx->runtime.init_co) / grid_dist) * grid_dist;
1052 if (nearest2d.snap_point(co)) {
1053 nearest2d.register_result(sctx, nullptr, nullptr);
1054 return true;
1055 }
1056 }
1057
1058 float ray_dist;
1059 for (int i = 0; i < 4; i++) {
1060 if (isect_ray_plane_v3(sctx->runtime.ray_start,
1061 sctx->runtime.ray_dir,
1062 sctx->grid.planes[i],
1063 &ray_dist,
1064 false) &&
1065 (ray_dist > 0.0f))
1066 {
1067 float3 co = math::round((sctx->runtime.ray_start + sctx->runtime.ray_dir * ray_dist) /
1068 grid_dist) *
1069 grid_dist;
1070
1071 if (nearest2d.snap_point(co)) {
1072 copy_v3_v3(nearest2d.nearest_point.no, sctx->grid.planes[i]);
1073 if (!sctx->runtime.rv3d->is_persp && RV3D_VIEW_IS_AXIS(sctx->runtime.rv3d->view)) {
1074 /* Project in #sctx->runtime.curr_co plane. */
1075 add_v3_v3(nearest2d.nearest_point.co,
1076 sctx->runtime.curr_co * float3(nearest2d.nearest_point.no));
1077 }
1078 nearest2d.register_result(sctx, nullptr, nullptr);
1079 return true;
1080 }
1081 }
1082 }
1083
1084 return false;
1085}
1086
1088
1089/* -------------------------------------------------------------------- */
1092
1094{
1095 return MEM_new<SnapObjectContext>(__func__);
1096}
1097
1099{
1100 MEM_delete(sctx);
1101}
1102
1104 const SnapEditMeshTarget target)
1105{
1106 if (sctx->editmesh_target == target) {
1107 return;
1108 }
1109 sctx->editmesh_target = target;
1110
1111 /* Rebuild the meshes too, as they are not checked for updates during transform. When snapping
1112 * to the base, the selection is restored to its original positions. */
1113 sctx->editmesh_caches.clear();
1114}
1115
1117 Depsgraph *depsgraph,
1118 const ARegion *region,
1119 const View3D *v3d,
1120 eSnapMode snap_to_flag,
1121 eSnapOcclusionTest occlusion_test,
1122 const SnapObjectParams *params,
1123 const float ray_start[3],
1124 const float ray_dir[3],
1125 const float ray_depth,
1126 const float mval[2],
1127 const float init_co[3],
1128 const float prev_co[3],
1129 const float dist_px_sq,
1130 ListBaseT<SnapObjectHitDepth> *hit_list)
1131{
1132 if (snap_to_flag &
1134 {
1135 if (prev_co) {
1136 copy_v3_v3(sctx->runtime.curr_co, prev_co);
1137 if (init_co) {
1138 copy_v3_v3(sctx->runtime.init_co, init_co);
1139 }
1140 else {
1141 snap_to_flag &= ~SCE_SNAP_INDIVIDUAL_NEAREST;
1142 }
1143 }
1144 else {
1146 }
1147 }
1148
1149 if (snap_to_flag == SCE_SNAP_TO_NONE) {
1150 return false;
1151 }
1152
1153 sctx->runtime.depsgraph = depsgraph;
1154 sctx->runtime.rv3d = nullptr;
1155 sctx->runtime.v3d = v3d;
1156 sctx->runtime.snap_to_flag = snap_to_flag;
1157 sctx->runtime.params = *params;
1158 sctx->runtime.params.occlusion_test = occlusion_test;
1159 sctx->runtime.occlusion_test_edit = (snap_to_flag & SCE_SNAP_TO_FACE) ? SNAP_OCCLUSION_ALWAYS :
1160 occlusion_test;
1161 sctx->runtime.has_occlusion_plane = false;
1162 sctx->runtime.has_occlusion_plane_in_front = false;
1163 sctx->runtime.object_index = 0;
1164
1165 copy_v3_v3(sctx->runtime.ray_start, ray_start);
1166 copy_v3_v3(sctx->runtime.ray_dir, ray_dir);
1167
1168 if (mval) {
1169 copy_v2_v2(sctx->runtime.mval, mval);
1170 }
1171
1172 if (region) {
1173 const RegionView3D *rv3d = static_cast<RegionView3D *>(region->regiondata);
1174 sctx->runtime.win_size[0] = region->winx;
1175 sctx->runtime.win_size[1] = region->winy;
1176
1177 sctx->runtime.clip_planes.resize(2);
1178
1180 nullptr,
1181 nullptr,
1182 nullptr,
1183 nullptr,
1184 sctx->runtime.clip_planes[0],
1185 sctx->runtime.clip_planes[1]);
1186
1187 if ((rv3d->rflag & RV3D_CLIPPING) && (rv3d->clipbb != nullptr)) {
1188 sctx->runtime.clip_planes.extend_unchecked(reinterpret_cast<const float4 *>(rv3d->clip), 4);
1189 }
1190
1191 sctx->runtime.rv3d = rv3d;
1192
1193 if (sctx->runtime.snap_to_flag & SCE_SNAP_TO_GRID) {
1194 sctx->grid.use_init_co = init_co != nullptr;
1195 if (params->grid_size) {
1196 sctx->grid.size = params->grid_size;
1197 }
1198 if (!compare_m4m4(sctx->grid.persmat.ptr(), rv3d->persmat, FLT_EPSILON)) {
1199 sctx->grid.persmat = float4x4(rv3d->persmat);
1200 if (params->grid_size == 0.0f) {
1201 const Scene *scene = DEG_get_evaluated_scene(sctx->runtime.depsgraph);
1202 sctx->grid.size = ED_view3d_grid_view_scale(scene, sctx->runtime.v3d, region, nullptr);
1203 }
1204
1205 if (!sctx->grid.use_init_co) {
1206 memset(reinterpret_cast<void *>(sctx->grid.planes), 0, sizeof(sctx->grid.planes));
1207 sctx->grid.planes[0][2] = 1.0f;
1208 if (math::abs(sctx->runtime.ray_dir[0]) < math::abs(sctx->runtime.ray_dir[1])) {
1209 sctx->grid.planes[1][1] = 1.0f;
1210 sctx->grid.planes[2][0] = 1.0f;
1211 }
1212 else {
1213 sctx->grid.planes[1][0] = 1.0f;
1214 sctx->grid.planes[2][1] = 1.0f;
1215 }
1216
1218 sctx->grid.planes[3], sctx->runtime.curr_co, rv3d->viewinv[2]);
1219 }
1220 }
1221 }
1222 }
1223 sctx->runtime.hit_list = hit_list;
1224
1225 sctx->ret.ray_depth_max = sctx->ret.ray_depth_max_in_front = ray_depth;
1226 sctx->ret.index = -1;
1227 sctx->ret.ob = nullptr;
1228 sctx->ret.data = nullptr;
1229 sctx->ret.dist_px_sq = dist_px_sq;
1230
1231 return true;
1232}
1233
1235 Depsgraph *depsgraph,
1236 const View3D *v3d,
1237 const SnapObjectParams *params,
1238 const float ray_start[3],
1239 const float ray_normal[3],
1240 float *ray_depth,
1241 float r_loc[3],
1242 float r_no[3],
1243 int *r_index,
1244 const Object **r_ob,
1245 float r_obmat[4][4])
1246{
1248 depsgraph,
1249 nullptr,
1250 v3d,
1252 params->occlusion_test,
1253 params,
1254 ray_start,
1255 ray_normal,
1256 !ray_depth || *ray_depth == -1.0f ? BVH_RAYCAST_DIST_MAX :
1257 *ray_depth,
1258 nullptr,
1259 nullptr,
1260 nullptr,
1261 0,
1262 nullptr))
1263 {
1264 return false;
1265 }
1266
1267 if (raycastObjects(sctx)) {
1268 copy_v3_v3(r_loc, sctx->ret.loc);
1269 if (r_no) {
1270 copy_v3_v3(r_no, sctx->ret.no);
1271 }
1272 if (r_index) {
1273 *r_index = sctx->ret.index;
1274 }
1275 if (r_ob) {
1276 *r_ob = sctx->ret.ob;
1277 }
1278 if (r_obmat) {
1279 copy_m4_m4(r_obmat, sctx->ret.obmat.ptr());
1280 }
1281 if (ray_depth) {
1282 *ray_depth = sctx->ret.ray_depth_max;
1283 }
1284 return true;
1285 }
1286 return false;
1287}
1288
1290 Depsgraph *depsgraph,
1291 const View3D *v3d,
1292 const SnapObjectParams *params,
1293 const float ray_start[3],
1294 const float ray_normal[3],
1295 float ray_depth,
1296 bool sort,
1297 ListBaseT<SnapObjectHitDepth> *r_hit_list)
1298{
1300 depsgraph,
1301 nullptr,
1302 v3d,
1304 params->occlusion_test,
1305 params,
1306 ray_start,
1307 ray_normal,
1308 ray_depth == -1.0f ? BVH_RAYCAST_DIST_MAX : ray_depth,
1309 nullptr,
1310 nullptr,
1311 nullptr,
1312 0,
1313 r_hit_list))
1314 {
1315 return false;
1316 }
1317
1318#ifndef NDEBUG
1319 float ray_depth_prev = sctx->ret.ray_depth_max;
1320#endif
1321 if (raycastObjects(sctx)) {
1322 if (sort) {
1323 BLI_listbase_sort(r_hit_list, hit_depth_cmp);
1324 }
1325 /* Meant to be read-only for 'all' hits, ensure it is. */
1326#ifndef NDEBUG
1327 BLI_assert(ray_depth_prev == sctx->ret.ray_depth_max);
1328#endif
1329 return true;
1330 }
1331 return false;
1332}
1333
1335 Depsgraph *depsgraph,
1336 const View3D *v3d,
1337 const SnapObjectParams *params,
1338 const float ray_start[3],
1339 const float ray_normal[3],
1340 float *ray_depth,
1341 float r_co[3],
1342 float r_no[3])
1343{
1344 return snap_object_project_ray_ex(sctx,
1345 depsgraph,
1346 v3d,
1347 params,
1348 ray_start,
1349 ray_normal,
1350 ray_depth,
1351 r_co,
1352 r_no,
1353 nullptr,
1354 nullptr,
1355 nullptr);
1356}
1357
1359 Depsgraph *depsgraph,
1360 const ARegion *region,
1361 const View3D *v3d,
1362 eSnapMode snap_to_flag,
1363 const SnapObjectParams *params,
1364 const float init_co[3],
1365 const float mval[2],
1366 const float prev_co[3],
1367 float *dist_px,
1368 float r_loc[3],
1369 float r_no[3],
1370 int *r_index,
1371 const Object **r_ob,
1372 float r_obmat[4][4],
1373 float r_face_nor[3])
1374{
1375 eSnapMode retval = SCE_SNAP_TO_NONE;
1376 float ray_depth_max = BVH_RAYCAST_DIST_MAX;
1377 bool use_occlusion_plane = false;
1378
1379 /* It is required `mval` to calculate the occlusion plane. */
1380 if (mval && (snap_to_flag & (SCE_SNAP_TO_GEOM | SCE_SNAP_TO_GRID))) {
1381 if (params->occlusion_test == SNAP_OCCLUSION_AS_SEEM) {
1382 use_occlusion_plane = !XRAY_ENABLED(v3d);
1383 }
1384 else if (params->occlusion_test == SNAP_OCCLUSION_ALWAYS) {
1385 use_occlusion_plane = true;
1386 }
1387 }
1388
1389 if (use_occlusion_plane || (snap_to_flag & SCE_SNAP_TO_FACE) ||
1390 /* Snap to Grid requires `ray_start` and `ray_dir`. */
1391 (snap_to_flag & SCE_SNAP_TO_GRID))
1392 {
1393 /* Calculate the direction (`ray_dir`) and starting point (`ray_start`) of the ray from the
1394 * viewport to a 3D point under the mouse cursor (`mval`), taking into account potential view
1395 * clipping. */
1396
1397 const RegionView3D *rv3d = static_cast<const RegionView3D *>(region->regiondata);
1398 float3 ray_end;
1400 region,
1401 v3d,
1402 mval,
1403 false,
1404 nullptr,
1405 sctx->runtime.ray_dir,
1406 sctx->runtime.ray_start,
1407 ray_end);
1408
1409 if ((rv3d->rflag & RV3D_CLIPPING) && (rv3d->clipbb != nullptr)) {
1411 sctx->runtime.ray_start, ray_end, rv3d->clip, 6, sctx->runtime.ray_start, ray_end))
1412 {
1413 ray_depth_max = math::dot(ray_end - sctx->runtime.ray_start, sctx->runtime.ray_dir);
1414 }
1415 else {
1416 snap_to_flag &= ~SCE_SNAP_TO_FACE;
1417 use_occlusion_plane = false;
1418 }
1419 }
1420 }
1421
1423 depsgraph,
1424 region,
1425 v3d,
1426 snap_to_flag,
1427 use_occlusion_plane ? params->occlusion_test :
1429 params,
1430 sctx->runtime.ray_start,
1431 sctx->runtime.ray_dir,
1432 ray_depth_max,
1433 mval,
1434 init_co,
1435 prev_co,
1436 dist_px ? square_f(*dist_px) : FLT_MAX,
1437 nullptr))
1438 {
1439 return retval;
1440 }
1441
1442#ifdef DEBUG_SNAP_TIME
1443 const timeit::TimePoint start_ = timeit::Clock::now();
1444#endif
1445
1446 snap_to_flag = sctx->runtime.snap_to_flag;
1447
1450
1451 bool has_hit = false;
1452
1453 /* NOTE: if both face ray-cast and face nearest are enabled, first find result of nearest, then
1454 * override with ray-cast. */
1455 if ((snap_to_flag & SCE_SNAP_INDIVIDUAL_NEAREST) && !has_hit) {
1456 has_hit = nearestWorldObjects(sctx);
1457
1458 if (has_hit) {
1460 }
1461 }
1462
1463 if (use_occlusion_plane || (snap_to_flag & SCE_SNAP_TO_FACE)) {
1464 has_hit = raycastObjects(sctx);
1465
1466 if (has_hit) {
1467 if (r_face_nor) {
1468 copy_v3_v3(r_face_nor, sctx->ret.no);
1469 }
1470
1471 if (snap_to_flag & SCE_SNAP_TO_FACE) {
1472 retval |= SCE_SNAP_TO_FACE;
1473 }
1474 }
1475 }
1476
1477 if (snap_to_flag &
1479 {
1480 eSnapMode elem_test, elem = SCE_SNAP_TO_NONE;
1481
1482 /* Remove what has already been computed. */
1483 sctx->runtime.snap_to_flag &= ~(SCE_SNAP_TO_FACE | SCE_SNAP_INDIVIDUAL_NEAREST);
1484
1485 SnapObjectContext::Output ret_bak{};
1486 if (!(sctx->runtime.snap_to_flag & SCE_SNAP_TO_EDGE) &&
1487 (sctx->runtime.snap_to_flag &
1489 {
1490 /* 'Snap to Edge' may occur even if it is not included among the selected snap types.
1491 * Save a backup to restore the previous result if needed. */
1492 ret_bak = sctx->ret;
1493 }
1494
1495 if (use_occlusion_plane && has_hit) {
1496 /* Compute the new clip plane but do not add it yet. */
1497 BLI_ASSERT_UNIT_V3(sctx->ret.no);
1498 sctx->runtime.occlusion_plane = occlusion_plane_create(
1499 sctx->runtime.ray_start, sctx->runtime.ray_dir, sctx->ret.loc, sctx->ret.no);
1500
1501 /* First, snap to the geometry of the polygon obtained via raycast.
1502 * This is necessary because the occlusion plane may "occlude" part of the polygon's
1503 * geometry. It also reduces the snap distance, optimizing the process.
1504 *
1505 * Note that if 'Snap to Edge Midpoint' or 'Snap to Edge Perpendicular' is selected, 'Snap to
1506 * Edge' will be returned instead.
1507 * This is because the same point can be tested in `snapObjectsRay` and fail this time due to
1508 * a mismatched snap distance, also resulting in snapping to the edge instead. */
1509 elem_test = snap_polygon(sctx, sctx->runtime.snap_to_flag);
1510 if (elem_test) {
1511 elem = elem_test;
1512 }
1513
1514 /* Add the new clip plane. */
1515 sctx->runtime.has_occlusion_plane = true;
1516 }
1517
1518 /* `snapObjectsRay` does 'Snap to Edge' instead of 'Snap to Edge Midpoint' or 'Snap to Edge
1519 * Perpendicular'. These points will be tested in the `snap_edge_points` function. */
1520 elem_test = snapObjectsRay(sctx);
1521 if (elem_test != SCE_SNAP_TO_NONE) {
1522 elem = elem_test;
1523 }
1524
1525 if (elem == SCE_SNAP_TO_EDGE) {
1526 if (snap_to_flag &
1528 {
1529 elem = snap_edge_points(sctx, square_f(*dist_px));
1530 }
1531
1532 if (!(elem & snap_to_flag)) {
1533 /* Restore the previous snap. */
1534 elem = SCE_SNAP_TO_NONE;
1535 sctx->ret = ret_bak;
1536 }
1537 }
1538
1539 if (elem != SCE_SNAP_TO_NONE) {
1540 retval = elem & snap_to_flag;
1541 }
1542 }
1543
1544 if ((retval == SCE_SNAP_TO_NONE) && (snap_to_flag & SCE_SNAP_TO_GRID)) {
1545 if (snap_grid(sctx)) {
1546 retval = SCE_SNAP_TO_GRID;
1547 }
1548 }
1549
1550 if (retval != SCE_SNAP_TO_NONE) {
1551 copy_v3_v3(r_loc, sctx->ret.loc);
1552 if (r_no) {
1553 copy_v3_v3(r_no, sctx->ret.no);
1554 }
1555 if (r_ob) {
1556 *r_ob = sctx->ret.ob;
1557 }
1558 if (r_obmat) {
1559 copy_m4_m4(r_obmat, sctx->ret.obmat.ptr());
1560 }
1561 if (r_index) {
1562 *r_index = sctx->ret.index;
1563 }
1564
1565 if (dist_px) {
1566 *dist_px = math::sqrt(sctx->ret.dist_px_sq);
1567 }
1568 }
1569
1570#ifdef DEBUG_SNAP_TIME
1571 duration_ += timeit::Clock::now() - start_;
1572 total_count_++;
1573#endif
1574
1575 return retval;
1576}
1577
1579 Depsgraph *depsgraph,
1580 const ARegion *region,
1581 const View3D *v3d,
1582 const eSnapMode snap_to,
1583 const SnapObjectParams *params,
1584 const float init_co[3],
1585 const float mval[2],
1586 const float prev_co[3],
1587 float *dist_px,
1588 float r_loc[3],
1589 float r_no[3])
1590{
1592 depsgraph,
1593 region,
1594 v3d,
1595 snap_to,
1596 params,
1597 init_co,
1598 mval,
1599 prev_co,
1600 dist_px,
1601 r_loc,
1602 r_no,
1603 nullptr,
1604 nullptr,
1605 nullptr,
1606 nullptr);
1607}
1608
1610 Depsgraph *depsgraph,
1611 const ARegion *region,
1612 const View3D *v3d,
1613 const SnapObjectParams *params,
1614 const float mval[2],
1615 float ray_depth,
1616 bool sort,
1617 ListBaseT<SnapObjectHitDepth> *r_hit_list)
1618{
1619 float3 ray_start, ray_normal, ray_end;
1620 const RegionView3D *rv3d = static_cast<const RegionView3D *>(region->regiondata);
1621
1623 depsgraph, region, v3d, mval, false, nullptr, ray_normal, ray_start, ray_end))
1624 {
1625 return false;
1626 }
1627
1628 if (((rv3d->rflag & RV3D_CLIPPING) != 0) && (rv3d->clipbb != nullptr) &&
1629 clip_segment_v3_plane_n(ray_start, ray_end, rv3d->clip, 6, ray_start, ray_end))
1630 {
1631 float ray_depth_max = math::dot(ray_end - ray_start, ray_normal);
1632 if ((ray_depth == -1.0f) || (ray_depth > ray_depth_max)) {
1633 ray_depth = ray_depth_max;
1634 }
1635 }
1636
1638 sctx, depsgraph, v3d, params, ray_start, ray_normal, ray_depth, sort, r_hit_list);
1639}
1640
1641#ifdef DEBUG_SNAP_TIME
1643{
1644 std::cout << "Average snapping time: ";
1645 std::cout << std::fixed << duration_.count() / 1.0e6 << " ms";
1646 std::cout << '\n';
1647
1648 duration_ = timeit::Nanoseconds::zero();
1649 total_count_ = 0;
1650}
1651#endif
1652
1654
1655} // namespace blender::ed::transform
General operations, lookup, etc. for blender objects.
#define BVH_RAYCAST_DIST_MAX
#define BLI_ASSERT_UNIT_V3(v)
#define ISECT_AABB_PLANE_BEHIND_ANY
#define SCE_SNAP_TO_GEOM
#define BASE_VISIBLE(v3d, base)
#define RV3D_VIEW_IS_AXIS(view)
#define ED_transform_snap_object_time_average_print()
#define XRAY_ENABLED(v3d)
#define IN_RANGE(a, b, c)
#define ELEM(...)
long long int int64_t
btScalar max
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
virtual void get_edge_verts_index(const int, int[2])
void clip_planes_enable(SnapObjectContext *sctx, const Object *ob_eval, bool skip_occlusion_plane=false)
Vector< float4, MAX_CLIPPLANE_LEN+1 > clip_planes
bool snap_edge(const float3 &va, const float3 &vb, int edge_index=-1)
virtual void copy_vert_no(const int, float[3])
SnapData(SnapObjectContext *sctx, const float4x4 &obmat=float4x4::identity())
virtual void get_vert_co(const int, const float **)
static void register_result_raycast(SnapObjectContext *sctx, const Object *ob_eval, const ID *id_eval, const float4x4 &obmat, const BVHTreeRayHit *hit, const bool is_in_front)
eSnapMode snap_edge_points_impl(SnapObjectContext *sctx, int edge_index, float dist_px_sq_orig)
bool snap_point(const float3 &co, int index=-1)
static void register_result(SnapObjectContext *sctx, const Object *ob_eval, const ID *id_eval, const float4x4 &obmat, BVHTreeNearest *r_nearest)
bool snap_boundbox(const float3 &min, const float3 &max)
#define FLT_MAX
#define FLT_EPSILON
bool object_has_geometry_set_instances(const Object &object)
static eSnapMode snapObjectsRay(SnapObjectContext *sctx)
eSnapMode snapCamera(SnapObjectContext *sctx, const Object *object, const float4x4 &obmat)
static eSnapMode iter_snap_objects(SnapObjectContext *sctx, IterSnapObjsCallback sob_callback)
eSnapMode snap_object_project_view3d_ex(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const eSnapMode snap_to, const SnapObjectParams *params, const float init_co[3], const float mval[2], const float prev_co[3], float *dist_px, float r_loc[3], float r_no[3], int *r_index, const Object **r_ob, float r_obmat[4][4], float r_face_nor[3])
bool snap_object_project_ray_ex(SnapObjectContext *sctx, Depsgraph *depsgraph, const View3D *v3d, const SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float *ray_depth, float r_loc[3], float r_no[3], int *r_index, const Object **r_ob, float r_obmat[4][4])
void snap_object_context_destroy(SnapObjectContext *sctx)
SnapObjectContext * snap_object_context_create()
eSnapMode snapLattice(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat)
void raycast_all_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
static bool test_projected_edge_dist(const DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, const bool is_persp, const float va[3], const float vb[3], BVHTreeNearest *nearest)
eSnapMode snapArmature(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat, bool is_object_active)
eSnapMode(*)(SnapObjectContext *sctx, const Object *ob_eval, const ID *ob_data, const float4x4 &obmat, bool is_object_active, bool use_hide) IterSnapObjsCallback
static bool snap_object_is_snappable(const SnapObjectContext *sctx, const eSnapTargetOP snap_target_select, const Base *base_act, const Base *base)
static SnapObjectHitDepth * hit_depth_create(const float depth, const float co[3], uint ob_uuid)
static const ID * data_for_snap_dupli(ID *ob_data)
bool snap_object_project_ray_all(SnapObjectContext *sctx, Depsgraph *depsgraph, const View3D *v3d, const SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float ray_depth, bool sort, ListBaseT< SnapObjectHitDepth > *r_hit_list)
static bool test_projected_vert_dist(const DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, const bool is_persp, const float *co, BVHTreeNearest *nearest)
bool nearest_world_tree(SnapObjectContext *sctx, const BVHTree *tree, BVHTree_NearestPointCallback nearest_cb, const float4x4 &obmat, void *treedata, BVHTreeNearest *r_nearest)
bool raycast_tri_backface_culling_test(const float dir[3], const float v0[3], const float v1[3], const float v2[3], float no[3])
eSnapMode snapCurve(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat)
eSnapMode snap_edge_points_mesh(SnapObjectContext *sctx, const Object *ob_eval, const ID *id, const float4x4 &obmat, float dist_px_sq_orig, int edge_index)
static float4 occlusion_plane_create(float3 ray_start, float3 ray_dir, float3 ray_co, float3 ray_no)
static bool snap_object_context_runtime_init(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, eSnapMode snap_to_flag, eSnapOcclusionTest occlusion_test, const SnapObjectParams *params, const float ray_start[3], const float ray_dir[3], const float ray_depth, const float mval[2], const float init_co[3], const float prev_co[3], const float dist_px_sq, ListBaseT< SnapObjectHitDepth > *hit_list)
void snap_object_context_set_editmesh_target(SnapObjectContext *sctx, SnapEditMeshTarget target)
void cb_snap_vert(void *userdata, int index, const DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, BVHTreeNearest *nearest)
static bool raycastObjects(SnapObjectContext *sctx)
static bool test_projected_face_midpoint_dist(const DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, const bool is_persp, const float center[3], BVHTreeNearest *nearest)
eSnapMode snap_object_project_view3d(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const eSnapMode snap_to, const SnapObjectParams *params, const float init_co[3], const float mval[2], const float prev_co[3], float *dist_px, float r_loc[3], float r_no[3])
static eSnapMode snap_polygon(SnapObjectContext *sctx, eSnapMode snap_to_flag)
static eSnapMode snap_edge_points(SnapObjectContext *sctx, const float dist_px_sq_orig)
static void nearest_world_tree_co(const BVHTree *tree, BVHTree_NearestPointCallback nearest_cb, void *treedata, const float3 &co, BVHTreeNearest *r_nearest)
void cb_snap_edge(void *userdata, int index, const DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, BVHTreeNearest *nearest)
eSnapMode snap_object_editmesh(SnapObjectContext *sctx, const Object *ob_eval, const ID *id, const float4x4 &obmat, eSnapMode snap_to_flag, bool use_hide)
static eSnapMode nearest_world_object_fn(SnapObjectContext *sctx, const Object *ob_eval, const ID *ob_data, const float4x4 &obmat, bool, bool use_hide)
eSnapMode snap_object_mesh(SnapObjectContext *sctx, const Object *ob_eval, const ID *id, const float4x4 &obmat, eSnapMode snap_to_flag, bool skip_hidden)
void cb_snap_face_midpoint(void *userdata, const int face_index, const DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, BVHTreeNearest *nearest)
static eSnapMode raycast_obj_fn(SnapObjectContext *sctx, const Object *ob_eval, const ID *ob_data, const float4x4 &obmat, bool, bool use_hide)
static eSnapMode snap_obj_fn(SnapObjectContext *sctx, const Object *ob_eval, const ID *ob_data, const float4x4 &obmat, bool is_object_active, bool use_hide)
bool snap_object_project_ray(SnapObjectContext *sctx, Depsgraph *depsgraph, const View3D *v3d, const SnapObjectParams *params, const float ray_start[3], const float ray_normal[3], float *ray_depth, float r_co[3], float r_no[3])
static int hit_depth_cmp(const void *arg1, const void *arg2)
static const ID * data_for_snap(Object *ob_eval, eSnapEditType edit_mode_type, bool *r_use_hide)
static bool nearestWorldObjects(SnapObjectContext *sctx)
bool object_project_all_view3d_ex(SnapObjectContext *sctx, Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const SnapObjectParams *params, const float mval[2], float ray_depth, bool sort, ListBaseT< SnapObjectHitDepth > *r_hit_list)
eSnapMode snap_polygon_mesh(SnapObjectContext *sctx, const Object *ob_eval, const ID *id, const float4x4 &obmat, eSnapMode snap_to_flag, int face_index)
static bool snap_grid(SnapObjectContext *sctx)
eSnapMode snap_object_center(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat, eSnapMode snap_to_flag)
T length_squared(const VecBase< T, Size > &a)
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
VecBase< T, 3 > transform_direction_transposed(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
T sqrt(const T &a)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
CartesianBasis invert(const CartesianBasis &basis)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T abs(const T &a)
VecBase< T, 3 > transform_direction(const MatBase< T, 3, 3 > &mat, const VecBase< T, 3 > &direction)
T round(const T &a)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
Clock::time_point TimePoint
Definition BLI_timeit.hh:20
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const Mesh * BKE_object_get_editmesh_eval_cage(const Object *object)
Definition object.cc:4492
float line_point_factor_v3(const float p[3], const float l1[3], const float l2[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
int BLI_bvhtree_find_nearest(const BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
int isect_aabb_planes_v3(const float(*planes)[4], int totplane, const float bbmin[3], const float bbmax[3])
bool isect_ray_plane_v3(const float ray_origin[3], const float ray_direction[3], const float plane[4], float *r_lambda, bool clip)
MINLINE void mul_v2_fl(float r[2], float f)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
bool ED_view3d_win_to_ray_clipped_ex(Depsgraph *depsgraph, const ARegion *region, const View3D *v3d, const float mval[2], const bool do_clip_planes, float r_ray_co[3], float r_ray_normal[3], float r_ray_start[3], float r_ray_end[3])
bool isect_ray_line_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float *r_lambda)
MINLINE float mul_project_m4_v3_zfac(const float mat[4][4], const float co[3]) ATTR_WARN_UNUSED_RESULT
@ BA_SNAP_FIX_DEPS_FIASCO
bool compare_m4m4(const float mat1[4][4], const float mat2[4][4], float limit)
void planes_from_projmat(const float mat[4][4], float left[4], float right[4], float bottom[4], float top[4], float near[4], float far[4])
const Mesh * BKE_object_get_editmesh_eval_final(const Object *object)
Definition object.cc:4477
Base * BKE_view_layer_active_base_get(ViewLayer *view_layer)
MatBase< float, 4, 4 > float4x4
void cross_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:30
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
void dist_squared_to_projected_aabb_precalc(struct DistProjectedAABBPrecalc *precalc, const float projmat[4][4], const float winsize[2], const float mval[2])
Definition math_geom.cc:832
VecBase< float, 4 > float4
@ SCE_SNAP_TARGET_NOT_ACTIVE
@ SCE_SNAP_TARGET_NOT_NONEDITED
@ SCE_SNAP_TARGET_ONLY_SELECTABLE
@ SCE_SNAP_TARGET_NOT_SELECTED
@ SCE_SNAP_TARGET_NOT_EDITED
ViewLayer * DEG_get_input_view_layer(const Depsgraph *graph)
MINLINE float dot_m4_v3_row_x(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
Main * DEG_get_bmain(const Depsgraph *graph)
BMesh const char void * data
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void object_duplilist(Depsgraph *depsgraph, Object *ob, Set< const Object * > *include_objects, DupliList &r_duplilist)
@ SCE_SNAP_TO_EDGE_MIDPOINT
@ SCE_SNAP_TO_EDGE_PERPENDICULAR
@ SCE_SNAP_TO_FACE_MIDPOINT
@ SCE_SNAP_INDIVIDUAL_NEAREST
@ SCE_SNAP_TO_EDGE_ENDPOINT
float ED_view3d_grid_view_scale(const Scene *scene, const View3D *v3d, const ARegion *region, const char **r_grid_unit)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
VectorList< DupliObject > DupliList
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MatBase< float, 3, 3 > float3x3
void void BLI_listbase_sort(ListBase *listbase, int(*cmp)(const void *, const void *)) ATTR_NONNULL(1
Definition listbase.cc:319
bool BKE_object_is_in_editmode(const Object *ob)
Definition object.cc:1898
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
float closest_ray_to_segment_v3(const float ray_origin[3], const float ray_direction[3], const float v0[3], const float v1[3], float r_close[3])
Definition math_geom.cc:425
MINLINE float dot_m4_v3_row_y(const float M[4][4], const float a[3]) ATTR_WARN_UNUSED_RESULT
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE float max_ff(float a, float b)
bool isect_point_planes_v3_negated(const float(*planes)[4], int totplane, const float p[3])
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Scene * DEG_get_input_scene(const Depsgraph *graph)
MINLINE float square_f(float a)
MINLINE void negate_v4(float r[4])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
VecBase< float, 3 > float3
@ ME_WRAPPER_TYPE_BMESH
float dist_squared_to_projected_aabb(struct DistProjectedAABBPrecalc *data, const float bbmin[3], const float bbmax[3], bool r_axis_closest[3])
Definition math_geom.cc:884
bool DEG_is_evaluated(const T *id)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
bool clip_segment_v3_plane_n(const float p1[3], const float p2[3], const float plane_array[][4], int plane_num, float r_p1[3], float r_p2[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
Mesh * BKE_mesh_wrapper_ensure_subdivision(Mesh *mesh)
ListBaseT< Base > * BKE_view_layer_object_bases_get(ViewLayer *view_layer)
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
void(*)(void *userdata, int index, const float co[3], BVHTreeNearest *nearest) BVHTree_NearestPointCallback
#define min(a, b)
Definition sort.cc:38
struct Object * object
char name[258]
Definition DNA_ID.h:433
const c_style_mat & ptr() const
bke::MeshRuntime * runtime
eObject_DrawExtraFlag dtx
eObject_VisibilityFlag visibility_flag
eObject_TransFlag transflag
eRegionView3D_Flag rflag
struct BoundBox * clipbb
eMeshWrapperType wrapper_type
struct blender::ed::transform::SnapObjectContext::Output ret
struct blender::ed::transform::SnapObjectContext::@247331373256345130115246240121215056001150353127 runtime
Map< const ID *, std::unique_ptr< SnapCache > > editmesh_caches
struct blender::ed::transform::SnapObjectContext::@310300172364020037033024163131337007352124137204 grid
#define SNAP_TO_EDGE_ELEMENTS