BKE_bvhutils.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2006 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): André Pinto
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. #ifndef __BKE_BVHUTILS_H__
  28. #define __BKE_BVHUTILS_H__
  29. /** \file BKE_bvhutils.h
  30. * \ingroup bke
  31. */
  32. #include "BLI_bitmap.h"
  33. #include "BLI_kdopbvh.h"
  34. /**
  35. * This header encapsulates necessary code to buld a BVH
  36. */
  37. struct DerivedMesh;
  38. struct BMEditMesh;
  39. struct MVert;
  40. struct MFace;
  41. typedef struct LinkNode BVHCache;
  42. /**
  43. * struct that kepts basic information about a BVHTree build from a editmesh
  44. */
  45. typedef struct BVHTreeFromEditMesh {
  46. struct BVHTree *tree;
  47. /* default callbacks to bvh nearest and raycast */
  48. BVHTree_NearestPointCallback nearest_callback;
  49. BVHTree_RayCastCallback raycast_callback;
  50. struct BMEditMesh *em;
  51. /* radius for raycast */
  52. float sphere_radius;
  53. /* Private data */
  54. bool cached;
  55. } BVHTreeFromEditMesh;
  56. /**
  57. * struct that kepts basic information about a BVHTree build from a mesh
  58. */
  59. typedef struct BVHTreeFromMesh {
  60. struct BVHTree *tree;
  61. /* default callbacks to bvh nearest and raycast */
  62. BVHTree_NearestPointCallback nearest_callback;
  63. BVHTree_RayCastCallback raycast_callback;
  64. /* Vertex array, so that callbacks have instante access to data */
  65. const struct MVert *vert;
  66. const struct MEdge *edge; /* only used for BVHTreeFromMeshEdges */
  67. const struct MFace *face;
  68. const struct MLoop *loop;
  69. const struct MLoopTri *looptri;
  70. bool vert_allocated;
  71. bool edge_allocated;
  72. bool face_allocated;
  73. bool loop_allocated;
  74. bool looptri_allocated;
  75. /* radius for raycast */
  76. float sphere_radius;
  77. /* Private data */
  78. bool cached;
  79. } BVHTreeFromMesh;
  80. /**
  81. * Builds a bvh tree where nodes are the relevant elements of the given mesh.
  82. * Configures BVHTreeFromMesh.
  83. *
  84. * The tree is build in mesh space coordinates, this means special care must be made on queries
  85. * so that the coordinates and rays are first translated on the mesh local coordinates.
  86. * Reason for this is that bvh_from_mesh_* can use a cache in some cases and so it becomes possible to reuse a BVHTree.
  87. *
  88. * free_bvhtree_from_mesh should be called when the tree is no longer needed.
  89. */
  90. BVHTree *bvhtree_from_editmesh_verts(
  91. BVHTreeFromEditMesh *data, struct BMEditMesh *em,
  92. float epsilon, int tree_type, int axis);
  93. BVHTree *bvhtree_from_editmesh_verts_ex(
  94. BVHTreeFromEditMesh *data, struct BMEditMesh *em,
  95. const BLI_bitmap *mask, int verts_num_active,
  96. float epsilon, int tree_type, int axis);
  97. BVHTree *bvhtree_from_mesh_verts(
  98. struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
  99. BVHTree *bvhtree_from_mesh_verts_ex(
  100. struct BVHTreeFromMesh *data, const struct MVert *vert, const int numVerts,
  101. const bool vert_allocated, const BLI_bitmap *mask, int verts_num_active,
  102. float epsilon, int tree_type, int axis);
  103. BVHTree *bvhtree_from_editmesh_edges(
  104. BVHTreeFromEditMesh *data, struct BMEditMesh *em,
  105. float epsilon, int tree_type, int axis);
  106. BVHTree *bvhtree_from_editmesh_edges_ex(
  107. BVHTreeFromEditMesh *data, struct BMEditMesh *em,
  108. const BLI_bitmap *edges_mask, int edges_num_active,
  109. float epsilon, int tree_type, int axis);
  110. BVHTree *bvhtree_from_mesh_edges(
  111. struct BVHTreeFromMesh *data, struct DerivedMesh *mesh,
  112. float epsilon, int tree_type, int axis);
  113. BVHTree *bvhtree_from_mesh_edges_ex(
  114. struct BVHTreeFromMesh *data,
  115. const struct MVert *vert, const bool vert_allocated,
  116. const struct MEdge *edge, const int edges_num, const bool edge_allocated,
  117. const BLI_bitmap *edges_mask, int edges_num_active,
  118. float epsilon, int tree_type, int axis);
  119. BVHTree *bvhtree_from_mesh_faces(
  120. struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon,
  121. int tree_type, int axis);
  122. BVHTree *bvhtree_from_mesh_faces_ex(
  123. struct BVHTreeFromMesh *data,
  124. const struct MVert *vert, const bool vert_allocated,
  125. const struct MFace *face, const int numFaces, const bool face_allocated,
  126. const BLI_bitmap *mask, int numFaces_active,
  127. float epsilon, int tree_type, int axis);
  128. BVHTree *bvhtree_from_editmesh_looptri(
  129. BVHTreeFromEditMesh *data, struct BMEditMesh *em,
  130. float epsilon, int tree_type, int axis, BVHCache **bvhCache);
  131. BVHTree *bvhtree_from_editmesh_looptri_ex(
  132. BVHTreeFromEditMesh *data, struct BMEditMesh *em,
  133. const BLI_bitmap *mask, int looptri_num_active,
  134. float epsilon, int tree_type, int axis, BVHCache **bvhCache);
  135. BVHTree *bvhtree_from_mesh_looptri(
  136. struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
  137. BVHTree *bvhtree_from_mesh_looptri_ex(
  138. struct BVHTreeFromMesh *data,
  139. const struct MVert *vert, const bool vert_allocated,
  140. const struct MLoop *mloop, const bool loop_allocated,
  141. const struct MLoopTri *looptri, const int looptri_num, const bool looptri_allocated,
  142. const BLI_bitmap *mask, int looptri_num_active,
  143. float epsilon, int tree_type, int axis);
  144. /**
  145. * Frees data allocated by a call to bvhtree_from_mesh_*.
  146. */
  147. void free_bvhtree_from_editmesh(struct BVHTreeFromEditMesh *data);
  148. void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data);
  149. /**
  150. * Math functions used by callbacks
  151. */
  152. float bvhtree_ray_tri_intersection(
  153. const BVHTreeRay *ray, const float m_dist,
  154. const float v0[3], const float v1[3], const float v2[3]);
  155. float bvhtree_sphereray_tri_intersection(
  156. const BVHTreeRay *ray, float radius, const float m_dist,
  157. const float v0[3], const float v1[3], const float v2[3]);
  158. /**
  159. * BVHCache
  160. */
  161. /* Using local coordinates */
  162. enum {
  163. BVHTREE_FROM_VERTS = 0,
  164. BVHTREE_FROM_EDGES = 1,
  165. BVHTREE_FROM_FACES = 2,
  166. BVHTREE_FROM_LOOPTRI = 3,
  167. BVHTREE_FROM_EM_LOOPTRI = 4,
  168. };
  169. BVHTree *bvhcache_find(BVHCache *cache, int type);
  170. bool bvhcache_has_tree(const BVHCache *cache, const BVHTree *tree);
  171. void bvhcache_insert(BVHCache **cache_p, BVHTree *tree, int type);
  172. void bvhcache_init(BVHCache **cache_p);
  173. void bvhcache_free(BVHCache **cache_p);
  174. #endif