Blender V5.3
transform_snap_object.hh
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#pragma once
10
11#include "DNA_listBase.h"
12#include "DNA_scene_types.h"
13
14#include "BLI_kdopbvh.hh"
15#include "BLI_map.hh"
16#include "BLI_math_geom_c.hh"
19
20#include "BKE_bvhutils.hh"
21
23
24namespace blender {
25
26#define MAX_CLIPPLANE_LEN 6
27
28#define SNAP_TO_EDGE_ELEMENTS \
29 (SCE_SNAP_TO_EDGE | SCE_SNAP_TO_EDGE_ENDPOINT | SCE_SNAP_TO_EDGE_MIDPOINT | \
30 SCE_SNAP_TO_EDGE_PERPENDICULAR)
31
32struct Depsgraph;
33struct ID;
34struct Mesh;
35struct Object;
36struct RegionView3D;
37struct Scene;
38struct View3D;
39
40namespace ed::transform {
41
44 public:
45 virtual ~SnapTargetTrees() = default;
46
53};
54
57 const Mesh &mesh_;
58 bool skip_hidden_;
59 std::optional<bke::BVHTreeFromMesh> corner_tris_;
60 std::optional<bke::BVHTreeFromMesh> loose_edges_;
61 std::optional<bke::BVHTreeFromMesh> loose_verts_;
62
63 public:
64 SnapTargetTreesMesh(const Mesh &mesh, const bool skip_hidden)
65 : mesh_(mesh), skip_hidden_(skip_hidden)
66 {
67 }
68
72};
73
75 struct SnapCache {
76 virtual ~SnapCache() = default;
77 };
79
80 /* Which elements of meshes in edit-mode are valid snap targets. */
82
83 struct {
84 /* Compare with #RegionView3D::persmat to update. */
86 float4 planes[4];
87 float size;
88 bool use_init_co;
90
91 struct {
92 Depsgraph *depsgraph;
93 const RegionView3D *rv3d;
94 const View3D *v3d;
95
98
101
104
105 float2 win_size; /* Win x and y. */
106 float2 mval;
107
111
112 /* Read/write. */
114 /* List of #SnapObjectHitDepth (caller must free). */
115 ListBaseT<SnapObjectHitDepth> *hit_list;
116
118
122
123 /* Output. */
124 struct Output {
125 /* Location of snapped point on target surface. */
127 /* Normal of snapped point on target surface. */
129 /* Index of snapped element on target object (-1 when no valid index is found). */
130 int index;
131 /* Matrix of target object (may not be #Object.object_to_world with dupli-instances). */
133 /* Snapped object. */
134 const Object *ob;
135 /* Snapped data. */
136 const ID *data;
137
140 union {
141 float dist_px_sq;
142 float dist_nearest_sq;
143 };
145};
146
148 void *bvhdata;
149
150 /* Internal vars for adding depths. */
152
154
155 float len_diff;
157
159
160 /* Output data. */
161 ListBaseT<SnapObjectHitDepth> *hit_list;
162};
163
164class SnapData {
165 public:
166 /* Read-only. */
171 const bool is_persp;
173
174 /* Read and write. */
176
177 /* Constructor. */
178 SnapData(SnapObjectContext *sctx, const float4x4 &obmat = float4x4::identity());
179
181 const Object *ob_eval,
182 bool skip_occlusion_plane = false);
183 bool snap_boundbox(const float3 &min, const float3 &max);
184 bool snap_point(const float3 &co, int index = -1);
185 bool snap_edge(const float3 &va, const float3 &vb, int edge_index = -1);
186 eSnapMode snap_edge_points_impl(SnapObjectContext *sctx, int edge_index, float dist_px_sq_orig);
187 static void register_result(SnapObjectContext *sctx,
188 const Object *ob_eval,
189 const ID *id_eval,
190 const float4x4 &obmat,
191 BVHTreeNearest *r_nearest);
192 void register_result(SnapObjectContext *sctx, const Object *ob_eval, const ID *id_eval);
194 const Object *ob_eval,
195 const ID *id_eval,
196 const float4x4 &obmat,
197 const BVHTreeRayHit *hit,
198 const bool is_in_front);
199
200 virtual void get_vert_co(const int /*index*/, const float ** /*r_co*/) {};
201 virtual void get_edge_verts_index(const int /*index*/, int /*r_v_index*/[2]) {};
202 virtual void copy_vert_no(const int /*index*/, float /*r_no*/[3]) {};
203 virtual void get_face_center(const int /*face_index*/, float3 & /*r_center*/) {};
204 virtual void copy_face_no(const int /*face_index*/, float /*r_no*/[3]) {};
205};
206
207/* `transform_snap_object.cc` */
208
209void raycast_all_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit);
210
212 const float dir[3], const float v0[3], const float v1[3], const float v2[3], float no[3]);
213
214void cb_snap_vert(void *userdata,
215 int index,
216 const DistProjectedAABBPrecalc *precalc,
217 const float (*clip_plane)[4],
218 const int clip_plane_len,
219 BVHTreeNearest *nearest);
220
221void cb_snap_edge(void *userdata,
222 int index,
223 const DistProjectedAABBPrecalc *precalc,
224 const float (*clip_plane)[4],
225 const int clip_plane_len,
226 BVHTreeNearest *nearest);
227
228void cb_snap_face_midpoint(void *userdata,
229 int face_index,
230 const DistProjectedAABBPrecalc *precalc,
231 const float (*clip_plane)[4],
232 int clip_plane_len,
233 BVHTreeNearest *nearest);
234
235bool nearest_world_tree(SnapObjectContext *sctx,
236 const BVHTree *tree,
238 const float4x4 &obmat,
239 void *treedata,
240 BVHTreeNearest *r_nearest);
241
242eSnapMode snap_object_center(SnapObjectContext *sctx,
243 const Object *ob_eval,
244 const float4x4 &obmat,
245 eSnapMode snap_to_flag);
246
247/* `transform_snap_object_armature.cc` */
248
249eSnapMode snapArmature(SnapObjectContext *sctx,
250 const Object *ob_eval,
251 const float4x4 &obmat,
252 bool is_object_active);
253
254/* `transform_snap_object_lattice.cc` */
255
256eSnapMode snapLattice(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat);
257
258/* `transform_snap_object_camera.cc` */
259
260eSnapMode snapCamera(SnapObjectContext *sctx, const Object *object, const float4x4 &obmat);
261
262/* `transform_snap_object_curve.cc` */
263
264eSnapMode snapCurve(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat);
265
266/* `transform_snap_object_editmesh.cc` */
267
268eSnapMode snap_object_editmesh(SnapObjectContext *sctx,
269 const Object *ob_eval,
270 const ID *id,
271 const float4x4 &obmat,
272 eSnapMode snap_to_flag,
273 bool use_hide);
274
275/* `transform_snap_object_mesh.cc` */
276
278eSnapMode snap_object_mesh(SnapObjectContext *sctx,
279 const Object *ob_eval,
280 const ID *id,
281 const float4x4 &obmat,
282 eSnapMode snap_to_flag,
283 bool skip_hidden);
284
289eSnapMode snap_object_mesh(SnapObjectContext *sctx,
290 const Object *ob_eval,
291 const ID *id,
292 const float4x4 &obmat,
293 eSnapMode snap_to_flag,
294 SnapTargetTrees &trees,
295 bool is_editmesh);
296
297eSnapMode snap_polygon_mesh(SnapObjectContext *sctx,
298 const Object *ob_eval,
299 const ID *id,
300 const float4x4 &obmat,
301 eSnapMode snap_to_flag,
302 int face_index);
303
304eSnapMode snap_edge_points_mesh(SnapObjectContext *sctx,
305 const Object *ob_eval,
306 const ID *id,
307 const float4x4 &obmat,
308 float dist_px_sq_orig,
309 int edge_index);
310
311} // namespace ed::transform
312} // namespace blender
These structs are the foundation for all linked lists in the library system.
btScalar max
Definition scene.h:133
virtual void get_face_center(const int, float3 &)
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)
virtual void copy_face_no(const int, float[3])
SnapTargetTreesMesh(const Mesh &mesh, const bool skip_hidden)
virtual bke::BVHTreeFromMesh & loose_edges()=0
virtual bke::BVHTreeFromMesh & loose_verts()=0
virtual bke::BVHTreeFromMesh & corner_tris()=0
eSnapMode snapCamera(SnapObjectContext *sctx, const Object *object, const float4x4 &obmat)
eSnapMode snapLattice(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat)
void raycast_all_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
eSnapMode snapArmature(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat, bool is_object_active)
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)
void cb_snap_vert(void *userdata, int index, const DistProjectedAABBPrecalc *precalc, const float(*clip_plane)[4], const int clip_plane_len, BVHTreeNearest *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)
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)
eSnapMode snap_polygon_mesh(SnapObjectContext *sctx, const Object *ob_eval, const ID *id, const float4x4 &obmat, eSnapMode snap_to_flag, int face_index)
eSnapMode snap_object_center(SnapObjectContext *sctx, const Object *ob_eval, const float4x4 &obmat, eSnapMode snap_to_flag)
BMesh Iterators.
void(*)(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit) BVHTree_RayCastCallback
unsigned int uint
Definition BKE_text.h:55
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
VecBase< float, 2 > float2
ATTR_WARN_UNUSED_RESULT const BMVert * v2
VecBase< float, 3 > float3
void(*)(void *userdata, int index, const float co[3], BVHTreeNearest *nearest) BVHTree_NearestPointCallback
#define min(a, b)
Definition sort.cc:38
ListBaseT< SnapObjectHitDepth > * hit_list
struct blender::ed::transform::SnapObjectContext::@247331373256345130115246240121215056001150353127 runtime
Map< const ID *, std::unique_ptr< SnapCache > > editmesh_caches
struct blender::ed::transform::SnapObjectContext::@310300172364020037033024163131337007352124137204 grid