Blender  V2.93
mathutils_bvhtree.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
24 #include <Python.h>
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "BLI_ghash.h"
29 #include "BLI_kdopbvh.h"
30 #include "BLI_math.h"
31 #include "BLI_memarena.h"
32 #include "BLI_polyfill_2d.h"
33 #include "BLI_utildefines.h"
34 
35 #include "BKE_bvhutils.h"
36 
37 #include "../generic/py_capi_utils.h"
38 #include "../generic/python_utildefines.h"
39 
40 #include "mathutils.h"
41 #include "mathutils_bvhtree.h" /* own include */
42 
43 #ifndef MATH_STANDALONE
44 # include "DNA_mesh_types.h"
45 # include "DNA_meshdata_types.h"
46 # include "DNA_object_types.h"
47 
48 # include "BKE_customdata.h"
49 # include "BKE_editmesh_bvh.h"
50 # include "BKE_lib_id.h"
51 # include "BKE_mesh.h"
52 # include "BKE_mesh_runtime.h"
53 
54 # include "DEG_depsgraph_query.h"
55 
56 # include "bmesh.h"
57 
58 # include "../bmesh/bmesh_py_types.h"
59 #endif /* MATH_STANDALONE */
60 
61 #include "BLI_strict_flags.h"
62 
63 /* -------------------------------------------------------------------- */
67 #define PYBVH_FIND_GENERIC_DISTANCE_DOC \
68  " :arg distance: Maximum distance threshold.\n" \
69  " :type distance: float\n"
70 
71 #define PYBVH_FIND_GENERIC_RETURN_DOC \
72  " :return: Returns a tuple\n" \
73  " (:class:`Vector` location, :class:`Vector` normal, int index, float distance),\n" \
74  " Values will all be None if no hit is found.\n" \
75  " :rtype: :class:`tuple`\n"
76 
77 #define PYBVH_FIND_GENERIC_RETURN_LIST_DOC \
78  " :return: Returns a list of tuples\n" \
79  " (:class:`Vector` location, :class:`Vector` normal, int index, float distance),\n" \
80  " :rtype: :class:`list`\n"
81 
82 #define PYBVH_FROM_GENERIC_EPSILON_DOC \
83  " :arg epsilon: Increase the threshold for detecting overlap and raycast hits.\n" \
84  " :type epsilon: float\n"
85 
88 /* sqrt(FLT_MAX) */
89 #define PYBVH_MAX_DIST_STR "1.84467e+19"
90 static const float max_dist_default = 1.844674352395373e+19f;
91 
92 static const char PY_BVH_TREE_TYPE_DEFAULT = 4;
93 static const char PY_BVH_AXIS_DEFAULT = 6;
94 
95 typedef struct {
96  PyObject_HEAD BVHTree *tree;
97  float epsilon;
98 
99  float (*coords)[3];
100  uint (*tris)[3];
101  uint coords_len, tris_len;
102 
103  /* Optional members */
104  /* aligned with 'tris' */
106  /* aligned with array that 'orig_index' points to */
107  float (*orig_normal)[3];
108 } PyBVHTree;
109 
110 /* -------------------------------------------------------------------- */
115  float epsilon,
116 
117  float (*coords)[3],
118  uint coords_len,
119  uint (*tris)[3],
120  uint tris_len,
121 
122  /* optional arrays */
123  int *orig_index,
124  float (*orig_normal)[3])
125 {
126  PyBVHTree *result = PyObject_New(PyBVHTree, &PyBVHTree_Type);
127 
128  result->tree = tree;
129  result->epsilon = epsilon;
130 
131  result->coords = coords;
132  result->tris = tris;
133  result->coords_len = coords_len;
134  result->tris_len = tris_len;
135 
136  result->orig_index = orig_index;
137  result->orig_normal = orig_normal;
138 
139  return (PyObject *)result;
140 }
141 
144 /* -------------------------------------------------------------------- */
148 static void py_bvhtree_raycast_to_py_tuple(const BVHTreeRayHit *hit, PyObject *py_retval)
149 {
150  BLI_assert(hit->index >= 0);
151  BLI_assert(PyTuple_GET_SIZE(py_retval) == 4);
152 
153  PyTuple_SET_ITEMS(py_retval,
154  Vector_CreatePyObject(hit->co, 3, NULL),
155  Vector_CreatePyObject(hit->no, 3, NULL),
156  PyLong_FromLong(hit->index),
157  PyFloat_FromDouble(hit->dist));
158 }
159 
160 static PyObject *py_bvhtree_raycast_to_py(const BVHTreeRayHit *hit)
161 {
162  PyObject *py_retval = PyTuple_New(4);
163 
164  py_bvhtree_raycast_to_py_tuple(hit, py_retval);
165 
166  return py_retval;
167 }
168 
169 static PyObject *py_bvhtree_raycast_to_py_none(void)
170 {
171  PyObject *py_retval = PyTuple_New(4);
172 
173  PyC_Tuple_Fill(py_retval, Py_None);
174 
175  return py_retval;
176 }
177 
178 #if 0
179 static PyObject *py_bvhtree_raycast_to_py_and_check(const BVHTreeRayHit *hit)
180 {
181  PyObject *py_retval;
182 
183  py_retval = PyTuple_New(4);
184 
185  if (hit->index != -1) {
186  py_bvhtree_raycast_to_py_tuple(hit, py_retval);
187  }
188  else {
189  PyC_Tuple_Fill(py_retval, Py_None);
190  }
191 
192  return py_retval;
193 }
194 #endif
195 
198 /* -------------------------------------------------------------------- */
202 static void py_bvhtree_nearest_to_py_tuple(const BVHTreeNearest *nearest, PyObject *py_retval)
203 {
204  BLI_assert(nearest->index >= 0);
205  BLI_assert(PyTuple_GET_SIZE(py_retval) == 4);
206 
207  PyTuple_SET_ITEMS(py_retval,
208  Vector_CreatePyObject(nearest->co, 3, NULL),
209  Vector_CreatePyObject(nearest->no, 3, NULL),
210  PyLong_FromLong(nearest->index),
211  PyFloat_FromDouble(sqrtf(nearest->dist_sq)));
212 }
213 
214 static PyObject *py_bvhtree_nearest_to_py(const BVHTreeNearest *nearest)
215 {
216  PyObject *py_retval = PyTuple_New(4);
217 
218  py_bvhtree_nearest_to_py_tuple(nearest, py_retval);
219 
220  return py_retval;
221 }
222 
223 static PyObject *py_bvhtree_nearest_to_py_none(void)
224 {
225  PyObject *py_retval = PyTuple_New(4);
226 
227  PyC_Tuple_Fill(py_retval, Py_None);
228 
229  return py_retval;
230 }
231 
232 #if 0
233 static PyObject *py_bvhtree_nearest_to_py_and_check(const BVHTreeNearest *nearest)
234 {
235  PyObject *py_retval;
236 
237  py_retval = PyTuple_New(4);
238 
239  if (nearest->index != -1) {
240  py_bvhtree_nearest_to_py_tuple(nearest, py_retval);
241  }
242  else {
243  PyC_Tuple_Fill(py_retval, Py_None);
244  }
245 
246  return py_retval;
247 }
248 #endif
249 
253 {
254  if (self->tree) {
255  BLI_bvhtree_free(self->tree);
256  }
257 
258  MEM_SAFE_FREE(self->coords);
259  MEM_SAFE_FREE(self->tris);
260 
261  MEM_SAFE_FREE(self->orig_index);
262  MEM_SAFE_FREE(self->orig_normal);
263 
264  Py_TYPE(self)->tp_free((PyObject *)self);
265 }
266 
267 /* -------------------------------------------------------------------- */
271 static void py_bvhtree_raycast_cb(void *userdata,
272  int index,
273  const BVHTreeRay *ray,
274  BVHTreeRayHit *hit)
275 {
276  const PyBVHTree *self = userdata;
277 
278  const float(*coords)[3] = (const float(*)[3])self->coords;
279  const uint *tri = self->tris[index];
280  const float *tri_co[3] = {coords[tri[0]], coords[tri[1]], coords[tri[2]]};
281  float dist;
282 
283  if (self->epsilon == 0.0f) {
284  dist = bvhtree_ray_tri_intersection(ray, hit->dist, UNPACK3(tri_co));
285  }
286  else {
287  dist = bvhtree_sphereray_tri_intersection(ray, self->epsilon, hit->dist, UNPACK3(tri_co));
288  }
289 
290  if (dist >= 0 && dist < hit->dist) {
291  hit->index = self->orig_index ? self->orig_index[index] : index;
292  hit->dist = dist;
293  madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
294  if (self->orig_normal) {
295  copy_v3_v3(hit->no, self->orig_normal[hit->index]);
296  }
297  else {
298  normal_tri_v3(hit->no, UNPACK3(tri_co));
299  }
300  }
301 }
302 
303 static void py_bvhtree_nearest_point_cb(void *userdata,
304  int index,
305  const float co[3],
306  BVHTreeNearest *nearest)
307 {
308  PyBVHTree *self = userdata;
309 
310  const float(*coords)[3] = (const float(*)[3])self->coords;
311  const uint *tri = self->tris[index];
312  const float *tri_co[3] = {coords[tri[0]], coords[tri[1]], coords[tri[2]]};
313  float nearest_tmp[3], dist_sq;
314 
315  closest_on_tri_to_point_v3(nearest_tmp, co, UNPACK3(tri_co));
316  dist_sq = len_squared_v3v3(co, nearest_tmp);
317 
318  if (dist_sq < nearest->dist_sq) {
319  nearest->index = self->orig_index ? self->orig_index[index] : index;
320  nearest->dist_sq = dist_sq;
321  copy_v3_v3(nearest->co, nearest_tmp);
322  if (self->orig_normal) {
323  copy_v3_v3(nearest->no, self->orig_normal[nearest->index]);
324  }
325  else {
326  normal_tri_v3(nearest->no, UNPACK3(tri_co));
327  }
328  }
329 }
330 
331 PyDoc_STRVAR(py_bvhtree_ray_cast_doc,
332  ".. method:: ray_cast(origin, direction, distance=sys.float_info.max)\n"
333  "\n"
334  " Cast a ray onto the mesh.\n"
335  "\n"
336  " :arg origin: Start location of the ray in object space.\n"
337  " :type origin: :class:`Vector`\n"
338  " :arg direction: Direction of the ray in object space.\n"
339  " :type direction: :class:`Vector`\n" PYBVH_FIND_GENERIC_DISTANCE_DOC
341 static PyObject *py_bvhtree_ray_cast(PyBVHTree *self, PyObject *args)
342 {
343  const char *error_prefix = "ray_cast";
344  float co[3], direction[3];
345  float max_dist = FLT_MAX;
346  BVHTreeRayHit hit;
347 
348  /* parse args */
349  {
350  PyObject *py_co, *py_direction;
351 
352  if (!PyArg_ParseTuple(args, "OO|f:ray_cast", &py_co, &py_direction, &max_dist)) {
353  return NULL;
354  }
355 
356  if ((mathutils_array_parse(co, 2, 3 | MU_ARRAY_ZERO, py_co, error_prefix) == -1) ||
357  (mathutils_array_parse(direction, 2, 3 | MU_ARRAY_ZERO, py_direction, error_prefix) ==
358  -1)) {
359  return NULL;
360  }
361 
362  normalize_v3(direction);
363  }
364 
365  hit.dist = max_dist;
366  hit.index = -1;
367 
368  /* may fail if the mesh has no faces, in that case the ray-cast misses */
369  if (self->tree) {
370  if (BLI_bvhtree_ray_cast(self->tree, co, direction, 0.0f, &hit, py_bvhtree_raycast_cb, self) !=
371  -1) {
372  return py_bvhtree_raycast_to_py(&hit);
373  }
374  }
375 
377 }
378 
379 PyDoc_STRVAR(py_bvhtree_find_nearest_doc,
380  ".. method:: find_nearest(origin, distance=" PYBVH_MAX_DIST_STR
381  ")\n"
382  "\n"
383  " Find the nearest element (typically face index) to a point.\n"
384  "\n"
385  " :arg co: Find nearest element to this point.\n"
386  " :type co: :class:`Vector`\n" PYBVH_FIND_GENERIC_DISTANCE_DOC
388 static PyObject *py_bvhtree_find_nearest(PyBVHTree *self, PyObject *args)
389 {
390  const char *error_prefix = "find_nearest";
391  float co[3];
392  float max_dist = max_dist_default;
393 
394  BVHTreeNearest nearest;
395 
396  /* parse args */
397  {
398  PyObject *py_co;
399 
400  if (!PyArg_ParseTuple(args, "O|f:find_nearest", &py_co, &max_dist)) {
401  return NULL;
402  }
403 
404  if (mathutils_array_parse(co, 2, 3 | MU_ARRAY_ZERO, py_co, error_prefix) == -1) {
405  return NULL;
406  }
407  }
408 
409  nearest.index = -1;
410  nearest.dist_sq = max_dist * max_dist;
411 
412  /* may fail if the mesh has no faces, in that case the ray-cast misses */
413  if (self->tree) {
414  if (BLI_bvhtree_find_nearest(self->tree, co, &nearest, py_bvhtree_nearest_point_cb, self) !=
415  -1) {
416  return py_bvhtree_nearest_to_py(&nearest);
417  }
418  }
419 
421 }
422 
424  PyBVHTree *self;
425  PyObject *result;
426  float dist_sq;
427 };
428 
429 static void py_bvhtree_nearest_point_range_cb(void *userdata,
430  int index,
431  const float co[3],
432  float UNUSED(dist_sq_bvh))
433 {
434  struct PyBVH_RangeData *data = userdata;
435  PyBVHTree *self = data->self;
436 
437  const float(*coords)[3] = (const float(*)[3])self->coords;
438  const uint *tri = self->tris[index];
439  const float *tri_co[3] = {coords[tri[0]], coords[tri[1]], coords[tri[2]]};
440  float nearest_tmp[3], dist_sq;
441 
442  closest_on_tri_to_point_v3(nearest_tmp, co, UNPACK3(tri_co));
443  dist_sq = len_squared_v3v3(co, nearest_tmp);
444 
445  if (dist_sq < data->dist_sq) {
446  BVHTreeNearest nearest;
447  nearest.index = self->orig_index ? self->orig_index[index] : index;
448  nearest.dist_sq = dist_sq;
449  copy_v3_v3(nearest.co, nearest_tmp);
450  if (self->orig_normal) {
451  copy_v3_v3(nearest.no, self->orig_normal[nearest.index]);
452  }
453  else {
454  normal_tri_v3(nearest.no, UNPACK3(tri_co));
455  }
456 
457  PyList_APPEND(data->result, py_bvhtree_nearest_to_py(&nearest));
458  }
459 }
460 
462  py_bvhtree_find_nearest_range_doc,
463  ".. method:: find_nearest_range(origin, distance=" PYBVH_MAX_DIST_STR
464  ")\n"
465  "\n"
466  " Find the nearest elements (typically face index) to a point in the distance range.\n"
467  "\n"
468  " :arg co: Find nearest elements to this point.\n"
469  " :type co: :class:`Vector`\n" PYBVH_FIND_GENERIC_DISTANCE_DOC
471 static PyObject *py_bvhtree_find_nearest_range(PyBVHTree *self, PyObject *args)
472 {
473  const char *error_prefix = "find_nearest_range";
474  float co[3];
475  float max_dist = max_dist_default;
476 
477  /* parse args */
478  {
479  PyObject *py_co;
480 
481  if (!PyArg_ParseTuple(args, "O|f:find_nearest_range", &py_co, &max_dist)) {
482  return NULL;
483  }
484 
485  if (mathutils_array_parse(co, 2, 3 | MU_ARRAY_ZERO, py_co, error_prefix) == -1) {
486  return NULL;
487  }
488  }
489 
490  PyObject *ret = PyList_New(0);
491 
492  if (self->tree) {
493  struct PyBVH_RangeData data = {
494  .self = self,
495  .result = ret,
496  .dist_sq = square_f(max_dist),
497  };
498 
500  }
501 
502  return ret;
503 }
504 
505 BLI_INLINE uint overlap_hash(const void *overlap_v)
506 {
507  const BVHTreeOverlap *overlap = overlap_v;
508  /* same constants as edge-hash */
509  return (((uint)overlap->indexA * 65) ^ ((uint)overlap->indexA * 31));
510 }
511 
512 BLI_INLINE bool overlap_cmp(const void *a_v, const void *b_v)
513 {
514  const BVHTreeOverlap *a = a_v;
515  const BVHTreeOverlap *b = b_v;
516  return (memcmp(a, b, sizeof(*a)) != 0);
517 }
518 
521  float epsilon;
522 };
523 
524 static bool py_bvhtree_overlap_cb(void *userdata, int index_a, int index_b, int UNUSED(thread))
525 {
526  struct PyBVHTree_OverlapData *data = userdata;
527  PyBVHTree *tree_a = data->tree_pair[0];
528  PyBVHTree *tree_b = data->tree_pair[1];
529  const uint *tri_a = tree_a->tris[index_a];
530  const uint *tri_b = tree_b->tris[index_b];
531  const float *tri_a_co[3] = {
532  tree_a->coords[tri_a[0]], tree_a->coords[tri_a[1]], tree_a->coords[tri_a[2]]};
533  const float *tri_b_co[3] = {
534  tree_b->coords[tri_b[0]], tree_b->coords[tri_b[1]], tree_b->coords[tri_b[2]]};
535  float ix_pair[2][3];
536  int verts_shared = 0;
537 
538  if (tree_a == tree_b) {
539  if (UNLIKELY(index_a == index_b)) {
540  return false;
541  }
542 
543  verts_shared = (ELEM(tri_a_co[0], UNPACK3(tri_b_co)) + ELEM(tri_a_co[1], UNPACK3(tri_b_co)) +
544  ELEM(tri_a_co[2], UNPACK3(tri_b_co)));
545 
546  /* if 2 points are shared, bail out */
547  if (verts_shared >= 2) {
548  return false;
549  }
550  }
551 
552  return (isect_tri_tri_v3(UNPACK3(tri_a_co), UNPACK3(tri_b_co), ix_pair[0], ix_pair[1]) &&
553  ((verts_shared == 0) || (len_squared_v3v3(ix_pair[0], ix_pair[1]) > data->epsilon)));
554 }
555 
557  py_bvhtree_overlap_doc,
558  ".. method:: overlap(other_tree)\n"
559  "\n"
560  " Find overlapping indices between 2 trees.\n"
561  "\n"
562  " :arg other_tree: Other tree to perform overlap test on.\n"
563  " :type other_tree: :class:`BVHTree`\n"
564  " :return: Returns a list of unique index pairs,"
565  " the first index referencing this tree, the second referencing the **other_tree**.\n"
566  " :rtype: :class:`list`\n");
567 static PyObject *py_bvhtree_overlap(PyBVHTree *self, PyBVHTree *other)
568 {
570  BVHTreeOverlap *overlap;
571  uint overlap_len = 0;
572  PyObject *ret;
573 
574  if (!PyBVHTree_CheckExact(other)) {
575  PyErr_SetString(PyExc_ValueError, "Expected a BVHTree argument");
576  return NULL;
577  }
578 
579  data.tree_pair[0] = self;
580  data.tree_pair[1] = other;
581  data.epsilon = max_ff(self->epsilon, other->epsilon);
582 
583  overlap = BLI_bvhtree_overlap(
584  self->tree, other->tree, &overlap_len, py_bvhtree_overlap_cb, &data);
585 
586  ret = PyList_New(0);
587 
588  if (overlap == NULL) {
589  /* pass */
590  }
591  else {
592  const bool use_unique = (self->orig_index || other->orig_index);
593  GSet *pair_test = use_unique ?
594  BLI_gset_new_ex(overlap_hash, overlap_cmp, __func__, overlap_len) :
595  NULL;
596  /* simple case, no index remapping */
597  uint i;
598 
599  for (i = 0; i < overlap_len; i++) {
600  PyObject *item;
601  if (use_unique) {
602  if (self->orig_index) {
603  overlap[i].indexA = self->orig_index[overlap[i].indexA];
604  }
605  if (other->orig_index) {
606  overlap[i].indexB = other->orig_index[overlap[i].indexB];
607  }
608 
609  /* skip if its already added */
610  if (!BLI_gset_add(pair_test, &overlap[i])) {
611  continue;
612  }
613  }
614 
615  item = PyTuple_New(2);
617  item, PyLong_FromLong(overlap[i].indexA), PyLong_FromLong(overlap[i].indexB));
618 
619  PyList_Append(ret, item);
620  Py_DECREF(item);
621  }
622 
623  if (pair_test) {
624  BLI_gset_free(pair_test, NULL);
625  }
626  }
627 
628  if (overlap) {
629  MEM_freeN(overlap);
630  }
631 
632  return ret;
633 }
634 
637 /* -------------------------------------------------------------------- */
642  C_BVHTree_FromPolygons_doc,
643  ".. classmethod:: FromPolygons(vertices, polygons, all_triangles=False, epsilon=0.0)\n"
644  "\n"
645  " BVH tree constructed geometry passed in as arguments.\n"
646  "\n"
647  " :arg vertices: float triplets each representing ``(x, y, z)``\n"
648  " :type vertices: float triplet sequence\n"
649  " :arg polygons: Sequence of polyugons, each containing indices to the vertices argument.\n"
650  " :type polygons: Sequence of sequences containing ints\n"
651  " :arg all_triangles: Use when all **polygons** are triangles for more efficient "
652  "conversion.\n"
653  " :type all_triangles: bool\n" PYBVH_FROM_GENERIC_EPSILON_DOC);
654 static PyObject *C_BVHTree_FromPolygons(PyObject *UNUSED(cls), PyObject *args, PyObject *kwargs)
655 {
656  const char *error_prefix = "BVHTree.FromPolygons";
657  const char *keywords[] = {"vertices", "polygons", "all_triangles", "epsilon", NULL};
658 
659  PyObject *py_coords, *py_tris;
660  PyObject *py_coords_fast = NULL, *py_tris_fast = NULL;
661 
662  MemArena *poly_arena = NULL;
663  MemArena *pf_arena = NULL;
664 
665  float(*coords)[3] = NULL;
666  uint(*tris)[3] = NULL;
667  uint coords_len, tris_len;
668  float epsilon = 0.0f;
669  bool all_triangles = false;
670 
671  /* when all_triangles is False */
672  int *orig_index = NULL;
673  float(*orig_normal)[3] = NULL;
674 
675  uint i;
676  bool valid = true;
677 
678  if (!PyArg_ParseTupleAndKeywords(args,
679  kwargs,
680  "OO|$O&f:BVHTree.FromPolygons",
681  (char **)keywords,
682  &py_coords,
683  &py_tris,
685  &all_triangles,
686  &epsilon)) {
687  return NULL;
688  }
689 
690  if (!(py_coords_fast = PySequence_Fast(py_coords, error_prefix)) ||
691  !(py_tris_fast = PySequence_Fast(py_tris, error_prefix))) {
692  Py_XDECREF(py_coords_fast);
693  return NULL;
694  }
695 
696  if (valid) {
697  PyObject **py_coords_fast_items = PySequence_Fast_ITEMS(py_coords_fast);
698  coords_len = (uint)PySequence_Fast_GET_SIZE(py_coords_fast);
699  coords = MEM_mallocN((size_t)coords_len * sizeof(*coords), __func__);
700 
701  for (i = 0; i < coords_len; i++) {
702  PyObject *py_vert = py_coords_fast_items[i];
703 
704  if (mathutils_array_parse(coords[i], 3, 3, py_vert, "BVHTree vertex: ") == -1) {
705  valid = false;
706  break;
707  }
708  }
709  }
710 
711  if (valid == false) {
712  /* pass */
713  }
714  else if (all_triangles) {
715  /* all triangles, simple case */
716  PyObject **py_tris_fast_items = PySequence_Fast_ITEMS(py_tris_fast);
717  tris_len = (uint)PySequence_Fast_GET_SIZE(py_tris_fast);
718  tris = MEM_mallocN((size_t)tris_len * sizeof(*tris), __func__);
719 
720  for (i = 0; i < tris_len; i++) {
721  PyObject *py_tricoords = py_tris_fast_items[i];
722  PyObject *py_tricoords_fast;
723  PyObject **py_tricoords_fast_items;
724  uint *tri = tris[i];
725  int j;
726 
727  if (!(py_tricoords_fast = PySequence_Fast(py_tricoords, error_prefix))) {
728  valid = false;
729  break;
730  }
731 
732  if (PySequence_Fast_GET_SIZE(py_tricoords_fast) != 3) {
733  Py_DECREF(py_tricoords_fast);
734  PyErr_Format(PyExc_ValueError,
735  "%s: non triangle found at index %d with length of %d",
736  error_prefix,
737  i,
738  PySequence_Fast_GET_SIZE(py_tricoords_fast));
739  valid = false;
740  break;
741  }
742 
743  py_tricoords_fast_items = PySequence_Fast_ITEMS(py_tricoords_fast);
744 
745  for (j = 0; j < 3; j++) {
746  tri[j] = PyC_Long_AsU32(py_tricoords_fast_items[j]);
747  if (UNLIKELY(tri[j] >= (uint)coords_len)) {
748  PyErr_Format(PyExc_ValueError,
749  "%s: index %d must be less than %d",
750  error_prefix,
751  tri[j],
752  coords_len);
753 
754  /* decref below */
755  valid = false;
756  break;
757  }
758  }
759 
760  Py_DECREF(py_tricoords_fast);
761  }
762  }
763  else {
764  /* ngon support (much more involved) */
765  const uint polys_len = (uint)PySequence_Fast_GET_SIZE(py_tris_fast);
766  struct PolyLink {
767  struct PolyLink *next;
768  uint len;
769  uint poly[0];
770  } *plink_first = NULL, **p_plink_prev = &plink_first, *plink = NULL;
771  int poly_index;
772 
773  tris_len = 0;
774 
775  poly_arena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
776 
777  for (i = 0; i < polys_len; i++) {
778  PyObject *py_tricoords = PySequence_Fast_GET_ITEM(py_tris_fast, i);
779  PyObject *py_tricoords_fast;
780  PyObject **py_tricoords_fast_items;
781  uint py_tricoords_len;
782  uint j;
783 
784  if (!(py_tricoords_fast = PySequence_Fast(py_tricoords, error_prefix))) {
785  valid = false;
786  break;
787  }
788 
789  py_tricoords_len = (uint)PySequence_Fast_GET_SIZE(py_tricoords_fast);
790  py_tricoords_fast_items = PySequence_Fast_ITEMS(py_tricoords_fast);
791 
792  plink = BLI_memarena_alloc(poly_arena,
793  sizeof(*plink) + (sizeof(int) * (size_t)py_tricoords_len));
794 
795  plink->len = (uint)py_tricoords_len;
796  *p_plink_prev = plink;
797  p_plink_prev = &plink->next;
798 
799  for (j = 0; j < py_tricoords_len; j++) {
800  plink->poly[j] = PyC_Long_AsU32(py_tricoords_fast_items[j]);
801  if (UNLIKELY(plink->poly[j] >= (uint)coords_len)) {
802  PyErr_Format(PyExc_ValueError,
803  "%s: index %d must be less than %d",
804  error_prefix,
805  plink->poly[j],
806  coords_len);
807  /* decref below */
808  valid = false;
809  break;
810  }
811  }
812 
813  Py_DECREF(py_tricoords_fast);
814 
815  if (py_tricoords_len >= 3) {
816  tris_len += (py_tricoords_len - 2);
817  }
818  }
819  *p_plink_prev = NULL;
820 
821  /* all ngon's are parsed, now tessellate */
822 
823  pf_arena = BLI_memarena_new(BLI_POLYFILL_ARENA_SIZE, __func__);
824  tris = MEM_mallocN(sizeof(*tris) * (size_t)tris_len, __func__);
825 
826  orig_index = MEM_mallocN(sizeof(*orig_index) * (size_t)tris_len, __func__);
827  orig_normal = MEM_mallocN(sizeof(*orig_normal) * (size_t)polys_len, __func__);
828 
829  for (plink = plink_first, poly_index = 0, i = 0; plink; plink = plink->next, poly_index++) {
830  if (plink->len == 3) {
831  uint *tri = tris[i];
832  memcpy(tri, plink->poly, sizeof(uint[3]));
833  orig_index[i] = poly_index;
834  normal_tri_v3(orig_normal[poly_index], coords[tri[0]], coords[tri[1]], coords[tri[2]]);
835  i++;
836  }
837  else if (plink->len > 3) {
838  float(*proj_coords)[2] = BLI_memarena_alloc(pf_arena, sizeof(*proj_coords) * plink->len);
839  float *normal = orig_normal[poly_index];
840  const float *co_prev;
841  const float *co_curr;
842  float axis_mat[3][3];
843  uint(*tris_offset)[3] = &tris[i];
844  uint j;
845 
846  /* calc normal and setup 'proj_coords' */
847  zero_v3(normal);
848  co_prev = coords[plink->poly[plink->len - 1]];
849  for (j = 0; j < plink->len; j++) {
850  co_curr = coords[plink->poly[j]];
851  add_newell_cross_v3_v3v3(normal, co_prev, co_curr);
852  co_prev = co_curr;
853  }
855 
857 
858  for (j = 0; j < plink->len; j++) {
859  mul_v2_m3v3(proj_coords[j], axis_mat, coords[plink->poly[j]]);
860  }
861 
862  BLI_polyfill_calc_arena(proj_coords, plink->len, 1, tris_offset, pf_arena);
863 
864  j = plink->len - 2;
865  while (j--) {
866  uint *tri = tris_offset[j];
867  /* remap to global indices */
868  tri[0] = plink->poly[tri[0]];
869  tri[1] = plink->poly[tri[1]];
870  tri[2] = plink->poly[tri[2]];
871 
872  orig_index[i] = poly_index;
873  i++;
874  }
875 
876  BLI_memarena_clear(pf_arena);
877  }
878  else {
879  zero_v3(orig_normal[poly_index]);
880  }
881  }
882  }
883 
884  Py_DECREF(py_coords_fast);
885  Py_DECREF(py_tris_fast);
886 
887  if (pf_arena) {
888  BLI_memarena_free(pf_arena);
889  }
890 
891  if (poly_arena) {
892  BLI_memarena_free(poly_arena);
893  }
894 
895  if (valid) {
896  BVHTree *tree;
897 
899  if (tree) {
900  for (i = 0; i < tris_len; i++) {
901  float co[3][3];
902 
903  copy_v3_v3(co[0], coords[tris[i][0]]);
904  copy_v3_v3(co[1], coords[tris[i][1]]);
905  copy_v3_v3(co[2], coords[tris[i][2]]);
906 
907  BLI_bvhtree_insert(tree, (int)i, co[0], 3);
908  }
909 
911  }
912 
913  return bvhtree_CreatePyObject(
914  tree, epsilon, coords, coords_len, tris, tris_len, orig_index, orig_normal);
915  }
916 
917  if (coords) {
918  MEM_freeN(coords);
919  }
920  if (tris) {
921  MEM_freeN(tris);
922  }
923 
924  return NULL;
925 }
926 
927 #ifndef MATH_STANDALONE
928 
929 PyDoc_STRVAR(C_BVHTree_FromBMesh_doc,
930  ".. classmethod:: FromBMesh(bmesh, epsilon=0.0)\n"
931  "\n"
932  " BVH tree based on :class:`BMesh` data.\n"
933  "\n"
934  " :arg bmesh: BMesh data.\n"
935  " :type bmesh: :class:`BMesh`\n" PYBVH_FROM_GENERIC_EPSILON_DOC);
936 static PyObject *C_BVHTree_FromBMesh(PyObject *UNUSED(cls), PyObject *args, PyObject *kwargs)
937 {
938  const char *keywords[] = {"bmesh", "epsilon", NULL};
939 
940  BPy_BMesh *py_bm;
941 
942  float(*coords)[3] = NULL;
943  uint(*tris)[3] = NULL;
944  uint coords_len, tris_len;
945  float epsilon = 0.0f;
946 
947  BMesh *bm;
948  BMLoop *(*looptris)[3];
949 
950  if (!PyArg_ParseTupleAndKeywords(args,
951  kwargs,
952  "O!|$f:BVHTree.FromBMesh",
953  (char **)keywords,
955  &py_bm,
956  &epsilon)) {
957  return NULL;
958  }
959 
960  bm = py_bm->bm;
961 
962  /* Get data for tessellation */
963  {
964  int tris_len_dummy;
965 
966  coords_len = (uint)bm->totvert;
967  tris_len = (uint)poly_to_tri_count(bm->totface, bm->totloop);
968 
969  coords = MEM_mallocN(sizeof(*coords) * (size_t)coords_len, __func__);
970  tris = MEM_mallocN(sizeof(*tris) * (size_t)tris_len, __func__);
971 
972  looptris = MEM_mallocN(sizeof(*looptris) * (size_t)tris_len, __func__);
973 
974  BM_mesh_calc_tessellation(bm, looptris, &tris_len_dummy);
975  BLI_assert(tris_len_dummy == (int)tris_len);
976  }
977 
978  {
979  BMIter iter;
980  BVHTree *tree;
981  uint i;
982 
983  int *orig_index = NULL;
984  float(*orig_normal)[3] = NULL;
985 
987  if (tree) {
988  BMFace *f;
989  BMVert *v;
990 
991  orig_index = MEM_mallocN(sizeof(*orig_index) * (size_t)tris_len, __func__);
992  orig_normal = MEM_mallocN(sizeof(*orig_normal) * (size_t)bm->totface, __func__);
993 
994  BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
995  copy_v3_v3(coords[i], v->co);
996  BM_elem_index_set(v, (int)i); /* set_inline */
997  }
998  BM_ITER_MESH_INDEX (f, &iter, bm, BM_FACES_OF_MESH, i) {
999  copy_v3_v3(orig_normal[i], f->no);
1000  BM_elem_index_set(f, (int)i); /* set_inline */
1001  }
1002  bm->elem_index_dirty &= (char)~(BM_VERT | BM_FACE);
1003 
1004  for (i = 0; i < tris_len; i++) {
1005  float co[3][3];
1006 
1007  tris[i][0] = (uint)BM_elem_index_get(looptris[i][0]->v);
1008  tris[i][1] = (uint)BM_elem_index_get(looptris[i][1]->v);
1009  tris[i][2] = (uint)BM_elem_index_get(looptris[i][2]->v);
1010 
1011  copy_v3_v3(co[0], coords[tris[i][0]]);
1012  copy_v3_v3(co[1], coords[tris[i][1]]);
1013  copy_v3_v3(co[2], coords[tris[i][2]]);
1014 
1015  BLI_bvhtree_insert(tree, (int)i, co[0], 3);
1016  orig_index[i] = BM_elem_index_get(looptris[i][0]->f);
1017  }
1018 
1020  }
1021 
1022  MEM_freeN(looptris);
1023 
1024  return bvhtree_CreatePyObject(
1025  tree, epsilon, coords, coords_len, tris, tris_len, orig_index, orig_normal);
1026  }
1027 }
1028 
1029 /* return various derived meshes based on requested settings */
1030 static Mesh *bvh_get_mesh(const char *funcname,
1031  struct Depsgraph *depsgraph,
1032  struct Scene *scene,
1033  Object *ob,
1034  const bool use_deform,
1035  const bool use_cage,
1036  bool *r_free_mesh)
1037 {
1038  Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
1039  /* we only need minimum mesh data for topology and vertex locations */
1040  const CustomData_MeshMasks data_masks = CD_MASK_BAREMESH;
1041  const bool use_render = DEG_get_mode(depsgraph) == DAG_EVAL_RENDER;
1042  *r_free_mesh = false;
1043 
1044  /* Write the display mesh into the dummy mesh */
1045  if (use_deform) {
1046  if (use_render) {
1047  if (use_cage) {
1048  PyErr_Format(
1049  PyExc_ValueError,
1050  "%s(...): cage arg is unsupported when dependency graph evaluation mode is RENDER",
1051  funcname);
1052  return NULL;
1053  }
1054 
1055  *r_free_mesh = true;
1056  return mesh_create_eval_final(depsgraph, scene, ob, &data_masks);
1057  }
1058  if (ob_eval != NULL) {
1059  if (use_cage) {
1060  return mesh_get_eval_deform(depsgraph, scene, ob_eval, &data_masks);
1061  }
1062 
1063  return mesh_get_eval_final(depsgraph, scene, ob_eval, &data_masks);
1064  }
1065 
1066  PyErr_Format(PyExc_ValueError,
1067  "%s(...): Cannot get evaluated data from given dependency graph / object pair",
1068  funcname);
1069  return NULL;
1070  }
1071 
1072  /* !use_deform */
1073  if (use_render) {
1074  if (use_cage) {
1075  PyErr_Format(
1076  PyExc_ValueError,
1077  "%s(...): cage arg is unsupported when dependency graph evaluation mode is RENDER",
1078  funcname);
1079  return NULL;
1080  }
1081 
1082  *r_free_mesh = true;
1083  return mesh_create_eval_no_deform_render(depsgraph, scene, ob, &data_masks);
1084  }
1085 
1086  if (use_cage) {
1087  PyErr_Format(PyExc_ValueError,
1088  "%s(...): cage arg is unsupported when deform=False and dependency graph "
1089  "evaluation mode is not RENDER",
1090  funcname);
1091  return NULL;
1092  }
1093 
1094  *r_free_mesh = true;
1095  return mesh_create_eval_no_deform(depsgraph, scene, ob, &data_masks);
1096 }
1097 
1098 PyDoc_STRVAR(C_BVHTree_FromObject_doc,
1099  ".. classmethod:: FromObject(object, depsgraph, deform=True, render=False, "
1100  "cage=False, epsilon=0.0)\n"
1101  "\n"
1102  " BVH tree based on :class:`Object` data.\n"
1103  "\n"
1104  " :arg object: Object data.\n"
1105  " :type object: :class:`Object`\n"
1106  " :arg depsgraph: Depsgraph to use for evaluating the mesh.\n"
1107  " :type depsgraph: :class:`Depsgraph`\n"
1108  " :arg deform: Use mesh with deformations.\n"
1109  " :type deform: bool\n"
1110  " :arg cage: Use modifiers cage.\n"
1111  " :type cage: bool\n" PYBVH_FROM_GENERIC_EPSILON_DOC);
1112 static PyObject *C_BVHTree_FromObject(PyObject *UNUSED(cls), PyObject *args, PyObject *kwargs)
1113 {
1114  /* note, options here match 'bpy_bmesh_from_object' */
1115  const char *keywords[] = {"object", "depsgraph", "deform", "cage", "epsilon", NULL};
1116 
1117  PyObject *py_ob, *py_depsgraph;
1118  Object *ob;
1119  struct Depsgraph *depsgraph;
1120  struct Scene *scene;
1121  Mesh *mesh;
1122  bool use_deform = true;
1123  bool use_cage = false;
1124  bool free_mesh = false;
1125 
1126  const MLoopTri *lt;
1127  const MLoop *mloop;
1128 
1129  float(*coords)[3] = NULL;
1130  uint(*tris)[3] = NULL;
1131  uint coords_len, tris_len;
1132  float epsilon = 0.0f;
1133 
1134  if (!PyArg_ParseTupleAndKeywords(args,
1135  kwargs,
1136  "OO|$O&O&f:BVHTree.FromObject",
1137  (char **)keywords,
1138  &py_ob,
1139  &py_depsgraph,
1140  PyC_ParseBool,
1141  &use_deform,
1142  PyC_ParseBool,
1143  &use_cage,
1144  &epsilon) ||
1145  ((ob = PyC_RNA_AsPointer(py_ob, "Object")) == NULL) ||
1146  ((depsgraph = PyC_RNA_AsPointer(py_depsgraph, "Depsgraph")) == NULL)) {
1147  return NULL;
1148  }
1149 
1151  mesh = bvh_get_mesh("BVHTree", depsgraph, scene, ob, use_deform, use_cage, &free_mesh);
1152 
1153  if (mesh == NULL) {
1154  return NULL;
1155  }
1156 
1157  /* Get data for tessellation */
1158  {
1160 
1161  tris_len = (uint)BKE_mesh_runtime_looptri_len(mesh);
1162  coords_len = (uint)mesh->totvert;
1163 
1164  coords = MEM_mallocN(sizeof(*coords) * (size_t)coords_len, __func__);
1165  tris = MEM_mallocN(sizeof(*tris) * (size_t)tris_len, __func__);
1166 
1167  MVert *mv = mesh->mvert;
1168  for (int i = 0; i < mesh->totvert; i++, mv++) {
1169  copy_v3_v3(coords[i], mv->co);
1170  }
1171 
1172  mloop = mesh->mloop;
1173  }
1174 
1175  {
1176  BVHTree *tree;
1177  uint i;
1178 
1179  int *orig_index = NULL;
1180  float(*orig_normal)[3] = NULL;
1181 
1183  if (tree) {
1184  orig_index = MEM_mallocN(sizeof(*orig_index) * (size_t)tris_len, __func__);
1185  CustomData *pdata = &mesh->pdata;
1186  orig_normal = CustomData_get_layer(pdata, CD_NORMAL); /* can be NULL */
1187  if (orig_normal) {
1188  orig_normal = MEM_dupallocN(orig_normal);
1189  }
1190 
1191  for (i = 0; i < tris_len; i++, lt++) {
1192  float co[3][3];
1193 
1194  tris[i][0] = mloop[lt->tri[0]].v;
1195  tris[i][1] = mloop[lt->tri[1]].v;
1196  tris[i][2] = mloop[lt->tri[2]].v;
1197 
1198  copy_v3_v3(co[0], coords[tris[i][0]]);
1199  copy_v3_v3(co[1], coords[tris[i][1]]);
1200  copy_v3_v3(co[2], coords[tris[i][2]]);
1201 
1202  BLI_bvhtree_insert(tree, (int)i, co[0], 3);
1203  orig_index[i] = (int)lt->poly;
1204  }
1205 
1207  }
1208 
1209  if (free_mesh) {
1210  BKE_id_free(NULL, mesh);
1211  }
1212 
1213  return bvhtree_CreatePyObject(
1214  tree, epsilon, coords, coords_len, tris, tris_len, orig_index, orig_normal);
1215  }
1216 }
1217 #endif /* MATH_STANDALONE */
1218 
1221 /* -------------------------------------------------------------------- */
1225 static PyMethodDef py_bvhtree_methods[] = {
1226  {"ray_cast", (PyCFunction)py_bvhtree_ray_cast, METH_VARARGS, py_bvhtree_ray_cast_doc},
1227  {"find_nearest",
1228  (PyCFunction)py_bvhtree_find_nearest,
1229  METH_VARARGS,
1230  py_bvhtree_find_nearest_doc},
1231  {"find_nearest_range",
1232  (PyCFunction)py_bvhtree_find_nearest_range,
1233  METH_VARARGS,
1234  py_bvhtree_find_nearest_range_doc},
1235  {"overlap", (PyCFunction)py_bvhtree_overlap, METH_O, py_bvhtree_overlap_doc},
1236 
1237  /* class methods */
1238  {"FromPolygons",
1239  (PyCFunction)C_BVHTree_FromPolygons,
1240  METH_VARARGS | METH_KEYWORDS | METH_CLASS,
1241  C_BVHTree_FromPolygons_doc},
1242 #ifndef MATH_STANDALONE
1243  {"FromBMesh",
1244  (PyCFunction)C_BVHTree_FromBMesh,
1245  METH_VARARGS | METH_KEYWORDS | METH_CLASS,
1246  C_BVHTree_FromBMesh_doc},
1247  {"FromObject",
1248  (PyCFunction)C_BVHTree_FromObject,
1249  METH_VARARGS | METH_KEYWORDS | METH_CLASS,
1250  C_BVHTree_FromObject_doc},
1251 #endif
1252  {NULL, NULL, 0, NULL},
1253 };
1254 
1255 PyTypeObject PyBVHTree_Type = {
1256  PyVarObject_HEAD_INIT(NULL, 0) "BVHTree", /* tp_name */
1257  sizeof(PyBVHTree), /* tp_basicsize */
1258  0, /* tp_itemsize */
1259  /* methods */
1260  (destructor)py_bvhtree__tp_dealloc, /* tp_dealloc */
1261  (printfunc)NULL, /* tp_print */
1262  NULL, /* tp_getattr */
1263  NULL, /* tp_setattr */
1264  NULL, /* tp_compare */
1265  NULL, /* tp_repr */
1266  NULL, /* tp_as_number */
1267  NULL, /* tp_as_sequence */
1268  NULL, /* tp_as_mapping */
1269  NULL, /* tp_hash */
1270  NULL, /* tp_call */
1271  NULL, /* tp_str */
1272  NULL, /* tp_getattro */
1273  NULL, /* tp_setattro */
1274  NULL, /* tp_as_buffer */
1275  Py_TPFLAGS_DEFAULT, /* tp_flags */
1276  NULL, /* Documentation string */
1277  NULL, /* tp_traverse */
1278  NULL, /* tp_clear */
1279  NULL, /* tp_richcompare */
1280  0, /* tp_weaklistoffset */
1281  NULL, /* tp_iter */
1282  NULL, /* tp_iternext */
1283  py_bvhtree_methods, /* tp_methods */
1284  NULL, /* tp_members */
1285  NULL, /* tp_getset */
1286  NULL, /* tp_base */
1287  NULL, /* tp_dict */
1288  NULL, /* tp_descr_get */
1289  NULL, /* tp_descr_set */
1290  0, /* tp_dictoffset */
1291  NULL, /* tp_init */
1292  (allocfunc)PyType_GenericAlloc, /* tp_alloc */
1293  (newfunc)PyType_GenericNew, /* tp_new */
1294  (freefunc)0, /* tp_free */
1295  NULL, /* tp_is_gc */
1296  NULL, /* tp_bases */
1297  NULL, /* tp_mro */
1298  NULL, /* tp_cache */
1299  NULL, /* tp_subclasses */
1300  NULL, /* tp_weaklist */
1301  (destructor)NULL, /* tp_del */
1302 };
1303 
1304 /* -------------------------------------------------------------------- */
1305 /* Module definition */
1306 
1307 PyDoc_STRVAR(py_bvhtree_doc,
1308  "BVH tree structures for proximity searches and ray casts on geometry.");
1309 static struct PyModuleDef bvhtree_moduledef = {
1310  PyModuleDef_HEAD_INIT,
1311  "mathutils.bvhtree", /* m_name */
1312  py_bvhtree_doc, /* m_doc */
1313  0, /* m_size */
1314  NULL, /* m_methods */
1315  NULL, /* m_reload */
1316  NULL, /* m_traverse */
1317  NULL, /* m_clear */
1318  NULL, /* m_free */
1319 };
1320 
1321 PyMODINIT_FUNC PyInit_mathutils_bvhtree(void)
1322 {
1323  PyObject *m = PyModule_Create(&bvhtree_moduledef);
1324 
1325  if (m == NULL) {
1326  return NULL;
1327  }
1328 
1329  /* Register classes */
1330  if (PyType_Ready(&PyBVHTree_Type) < 0) {
1331  return NULL;
1332  }
1333 
1334  PyModule_AddType(m, &PyBVHTree_Type);
1335 
1336  return m;
1337 }
1338 
typedef float(TangentPoint)[2]
float bvhtree_ray_tri_intersection(const BVHTreeRay *ray, const float m_dist, const float v0[3], const float v1[3], const float v2[3])
float bvhtree_sphereray_tri_intersection(const BVHTreeRay *ray, float radius, const float m_dist, const float v0[3], const float v1[3], const float v2[3])
Definition: bvhutils.c:190
CustomData interface, see also DNA_customdata_types.h.
const CustomData_MeshMasks CD_MASK_BAREMESH
Definition: customdata.c:1919
void * CustomData_get_layer(const struct CustomData *data, int type)
void BKE_id_free(struct Main *bmain, void *idv)
struct Mesh * mesh_get_eval_deform(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
struct Mesh * mesh_create_eval_no_deform(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
struct Mesh * mesh_get_eval_final(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
const struct MLoopTri * BKE_mesh_runtime_looptri_ensure(struct Mesh *mesh)
Definition: mesh_runtime.c:155
struct Mesh * mesh_create_eval_no_deform_render(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
int BKE_mesh_runtime_looptri_len(const struct Mesh *mesh)
struct Mesh * mesh_create_eval_final(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, const struct CustomData_MeshMasks *dataMask)
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_INLINE
struct GSet GSet
Definition: BLI_ghash.h:189
GSet * BLI_gset_new_ex(GSetHashFP hashfp, GSetCmpFP cmpfp, const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:1117
void BLI_gset_free(GSet *gs, GSetKeyFreeFP keyfreefp)
Definition: BLI_ghash.c:1253
bool BLI_gset_add(GSet *gs, void *key)
Definition: BLI_ghash.c:1160
int BLI_bvhtree_range_query(BVHTree *tree, const float co[3], float radius, BVHTree_RangeQuery callback, void *userdata)
Definition: BLI_kdopbvh.c:2131
BVHTreeOverlap * BLI_bvhtree_overlap(const BVHTree *tree1, const BVHTree *tree2, unsigned int *r_overlap_tot, BVHTree_OverlapCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1401
void BLI_bvhtree_balance(BVHTree *tree)
Definition: BLI_kdopbvh.c:956
BVHTree * BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis)
Definition: BLI_kdopbvh.c:873
void BLI_bvhtree_free(BVHTree *tree)
Definition: BLI_kdopbvh.c:945
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints)
Definition: BLI_kdopbvh.c:998
int BLI_bvhtree_ray_cast(BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1984
int BLI_bvhtree_find_nearest(BVHTree *tree, const float co[3], BVHTreeNearest *nearest, BVHTree_NearestPointCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1654
MINLINE float max_ff(float a, float b)
MINLINE float square_f(float a)
MINLINE int poly_to_tri_count(const int poly_count, const int corner_count)
void closest_on_tri_to_point_v3(float r[3], const float p[3], const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:1023
void axis_dominant_v3_to_m3_negate(float r_mat[3][3], const float normal[3])
Definition: math_geom.c:3772
bool isect_tri_tri_v3(const float t_a0[3], const float t_a1[3], const float t_a2[3], const float t_b0[3], const float t_b1[3], const float t_b2[3], float r_i1[3], float r_i2[3])
Definition: math_geom.c:2521
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition: math_geom.c:51
void mul_v2_m3v3(float r[2], const float M[3][3], const float a[3])
Definition: math_matrix.c:921
MINLINE float normalize_v3(float r[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:109
#define BLI_MEMARENA_STD_BUFSIZE
Definition: BLI_memarena.h:36
void * BLI_memarena_alloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
Definition: BLI_memarena.c:131
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:185
struct MemArena * BLI_memarena_new(const size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2) ATTR_MALLOC
Definition: BLI_memarena.c:79
void BLI_polyfill_calc_arena(const float(*coords)[2], const unsigned int coords_tot, const int coords_sign, unsigned int(*r_tris)[3], struct MemArena *arena)
Definition: polyfill_2d.c:847
#define BLI_POLYFILL_ARENA_SIZE
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define UNPACK3(a)
#define UNLIKELY(x)
#define ELEM(...)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
@ DAG_EVAL_RENDER
Definition: DEG_depsgraph.h:62
eEvaluationMode DEG_get_mode(const Depsgraph *graph)
struct Object * DEG_get_evaluated_object(const struct Depsgraph *depsgraph, struct Object *object)
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
Object is a sort of wrapper for general info.
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
#define BM_elem_index_get(ele)
Definition: bmesh_inline.h:124
#define BM_elem_index_set(ele, index)
Definition: bmesh_inline.h:125
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_calc_tessellation(BMesh *bm, BMLoop *(*looptris)[3], int *r_looptris_tot)
BM_mesh_calc_tessellation get the looptris and its number from a certain bmesh.
PyTypeObject BPy_BMesh_Type
ATTR_WARN_UNUSED_RESULT const BMVert * v
PyObject * self
Definition: bpy_driver.c:185
Scene scene
const Depsgraph * depsgraph
void * tree
IconTextureDrawCall normal
#define sqrtf(x)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
int mathutils_array_parse(float *array, int array_min, int array_max, PyObject *value, const char *error_prefix)
Definition: mathutils.c:118
#define MU_ARRAY_ZERO
Definition: mathutils.h:184
PyObject * Vector_CreatePyObject(const float *vec, const int size, PyTypeObject *base_type)
static PyObject * py_bvhtree_raycast_to_py_none(void)
static PyObject * C_BVHTree_FromPolygons(PyObject *UNUSED(cls), PyObject *args, PyObject *kwargs)
PyDoc_STRVAR(py_bvhtree_ray_cast_doc, ".. method:: ray_cast(origin, direction, distance=sys.float_info.max)\n" "\n" " Cast a ray onto the mesh.\n" "\n" " :arg origin: Start location of the ray in object space.\n" " :type origin: :class:`Vector`\n" " :arg direction: Direction of the ray in object space.\n" " :type direction: :class:`Vector`\n" PYBVH_FIND_GENERIC_DISTANCE_DOC PYBVH_FIND_GENERIC_RETURN_DOC)
#define PYBVH_FIND_GENERIC_RETURN_DOC
static bool py_bvhtree_overlap_cb(void *userdata, int index_a, int index_b, int UNUSED(thread))
static PyObject * py_bvhtree_find_nearest(PyBVHTree *self, PyObject *args)
static void py_bvhtree_raycast_to_py_tuple(const BVHTreeRayHit *hit, PyObject *py_retval)
BLI_INLINE bool overlap_cmp(const void *a_v, const void *b_v)
static void py_bvhtree__tp_dealloc(PyBVHTree *self)
static PyObject * py_bvhtree_nearest_to_py(const BVHTreeNearest *nearest)
static PyObject * py_bvhtree_find_nearest_range(PyBVHTree *self, PyObject *args)
static void py_bvhtree_nearest_point_cb(void *userdata, int index, const float co[3], BVHTreeNearest *nearest)
static const char PY_BVH_AXIS_DEFAULT
static PyObject * C_BVHTree_FromBMesh(PyObject *UNUSED(cls), PyObject *args, PyObject *kwargs)
#define PYBVH_MAX_DIST_STR
static struct PyModuleDef bvhtree_moduledef
#define PYBVH_FROM_GENERIC_EPSILON_DOC
static void py_bvhtree_raycast_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
static PyObject * py_bvhtree_raycast_to_py(const BVHTreeRayHit *hit)
BLI_INLINE uint overlap_hash(const void *overlap_v)
static PyMethodDef py_bvhtree_methods[]
static const char PY_BVH_TREE_TYPE_DEFAULT
static Mesh * bvh_get_mesh(const char *funcname, struct Depsgraph *depsgraph, struct Scene *scene, Object *ob, const bool use_deform, const bool use_cage, bool *r_free_mesh)
PyMODINIT_FUNC PyInit_mathutils_bvhtree(void)
PyTypeObject PyBVHTree_Type
static PyObject * C_BVHTree_FromObject(PyObject *UNUSED(cls), PyObject *args, PyObject *kwargs)
static PyObject * py_bvhtree_overlap(PyBVHTree *self, PyBVHTree *other)
#define PYBVH_FIND_GENERIC_DISTANCE_DOC
#define PYBVH_FIND_GENERIC_RETURN_LIST_DOC
static void py_bvhtree_nearest_to_py_tuple(const BVHTreeNearest *nearest, PyObject *py_retval)
static PyObject * py_bvhtree_ray_cast(PyBVHTree *self, PyObject *args)
static PyObject * bvhtree_CreatePyObject(BVHTree *tree, float epsilon, float(*coords)[3], uint coords_len, uint(*tris)[3], uint tris_len, int *orig_index, float(*orig_normal)[3])
static const float max_dist_default
static void py_bvhtree_nearest_point_range_cb(void *userdata, int index, const float co[3], float UNUSED(dist_sq_bvh))
static PyObject * py_bvhtree_nearest_to_py_none(void)
#define PyBVHTree_CheckExact(v)
static ulong * next
static unsigned a[3]
Definition: RandGen.cpp:92
static double epsilon
void * PyC_RNA_AsPointer(PyObject *value, const char *type_name)
uint32_t PyC_Long_AsU32(PyObject *value)
int PyC_ParseBool(PyObject *o, void *p)
void PyC_Tuple_Fill(PyObject *tuple, PyObject *value)
#define PyTuple_SET_ITEMS(op_arg,...)
return ret
float no[3]
Definition: bmesh_class.h:280
float co[3]
Definition: bmesh_class.h:99
int totvert
Definition: bmesh_class.h:297
char elem_index_dirty
Definition: bmesh_class.h:305
int totloop
Definition: bmesh_class.h:297
int totface
Definition: bmesh_class.h:297
PyObject_VAR_HEAD struct BMesh * bm
float co[3]
Definition: BLI_kdopbvh.h:59
float no[3]
Definition: BLI_kdopbvh.h:62
float co[3]
Definition: BLI_kdopbvh.h:84
float no[3]
Definition: BLI_kdopbvh.h:86
unsigned int poly
unsigned int tri[3]
unsigned int v
struct MVert * mvert
int totvert
struct MLoop * mloop
float(* coords)[3]
uint(* tris)[3]
PyObject_HEAD BVHTree * tree
uint len