Cleanup: de-duplicate checks in object-mode item callback
[blender.git] / source / blender / blenkernel / BKE_bvhutils.h
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  * The Original Code is Copyright (C) 2006 by NaN Holding BV.
17  * All rights reserved.
18  */
19 #pragma once
20
21 /** \file
22  * \ingroup bke
23  */
24
25 #include "BLI_bitmap.h"
26 #include "BLI_kdopbvh.h"
27 #include "BLI_threads.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /**
34  * This header encapsulates necessary code to build a BVH
35  */
36
37 struct BMEditMesh;
38 struct MFace;
39 struct MVert;
40 struct Mesh;
41 struct PointCloud;
42
43 struct BVHCache;
44
45 /**
46  * Struct that stores basic information about a BVHTree built from a edit-mesh.
47  */
48 typedef struct BVHTreeFromEditMesh {
49   struct BVHTree *tree;
50
51   /* default callbacks to bvh nearest and raycast */
52   BVHTree_NearestPointCallback nearest_callback;
53   BVHTree_RayCastCallback raycast_callback;
54
55   struct BMEditMesh *em;
56
57   /* Private data */
58   bool cached;
59
60 } BVHTreeFromEditMesh;
61
62 /**
63  * Struct that stores basic information about a BVHTree built from a mesh.
64  */
65 typedef struct BVHTreeFromMesh {
66   struct BVHTree *tree;
67
68   /* default callbacks to bvh nearest and raycast */
69   BVHTree_NearestPointCallback nearest_callback;
70   BVHTree_RayCastCallback raycast_callback;
71
72   /* Vertex array, so that callbacks have instante access to data */
73   const struct MVert *vert;
74   const struct MEdge *edge; /* only used for BVHTreeFromMeshEdges */
75   const struct MFace *face;
76   const struct MLoop *loop;
77   const struct MLoopTri *looptri;
78   bool vert_allocated;
79   bool edge_allocated;
80   bool face_allocated;
81   bool loop_allocated;
82   bool looptri_allocated;
83
84   /* Private data */
85   bool cached;
86
87 } BVHTreeFromMesh;
88
89 typedef enum BVHCacheType {
90   BVHTREE_FROM_VERTS,
91   BVHTREE_FROM_EDGES,
92   BVHTREE_FROM_FACES,
93   BVHTREE_FROM_LOOPTRI,
94   BVHTREE_FROM_LOOPTRI_NO_HIDDEN,
95
96   BVHTREE_FROM_LOOSEVERTS,
97   BVHTREE_FROM_LOOSEEDGES,
98
99   BVHTREE_FROM_EM_VERTS,
100   BVHTREE_FROM_EM_EDGES,
101   BVHTREE_FROM_EM_LOOPTRI,
102
103   /* Keep `BVHTREE_MAX_ITEM` as last item. */
104   BVHTREE_MAX_ITEM,
105 } BVHCacheType;
106
107 /**
108  * Builds a bvh tree where nodes are the relevant elements of the given mesh.
109  * Configures #BVHTreeFromMesh.
110  *
111  * The tree is build in mesh space coordinates, this means special care must be made on queries
112  * so that the coordinates and rays are first translated on the mesh local coordinates.
113  * Reason for this is that bvh_from_mesh_* can use a cache in some cases and so it
114  * becomes possible to reuse a #BVHTree.
115  *
116  * free_bvhtree_from_mesh should be called when the tree is no longer needed.
117  */
118 BVHTree *bvhtree_from_editmesh_verts(
119     BVHTreeFromEditMesh *data, struct BMEditMesh *em, float epsilon, int tree_type, int axis);
120
121 BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data,
122                                         struct BMEditMesh *em,
123                                         const BLI_bitmap *mask,
124                                         int verts_num_active,
125                                         float epsilon,
126                                         int tree_type,
127                                         int axis,
128                                         const BVHCacheType bvh_cache_type,
129                                         struct BVHCache **bvh_cache_p,
130                                         ThreadMutex *mesh_eval_mutex);
131
132 BVHTree *bvhtree_from_mesh_verts_ex(struct BVHTreeFromMesh *data,
133                                     const struct MVert *vert,
134                                     const int verts_num,
135                                     const bool vert_allocated,
136                                     const BLI_bitmap *mask,
137                                     int verts_num_active,
138                                     float epsilon,
139                                     int tree_type,
140                                     int axis,
141                                     const BVHCacheType bvh_cache_type,
142                                     struct BVHCache **bvh_cache_p,
143                                     ThreadMutex *mesh_eval_mutex);
144
145 BVHTree *bvhtree_from_editmesh_edges(
146     BVHTreeFromEditMesh *data, struct BMEditMesh *em, float epsilon, int tree_type, int axis);
147
148 BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data,
149                                         struct BMEditMesh *em,
150                                         const BLI_bitmap *edges_mask,
151                                         int edges_num_active,
152                                         float epsilon,
153                                         int tree_type,
154                                         int axis,
155                                         const BVHCacheType bvh_cache_type,
156                                         struct BVHCache **bvh_cache_p,
157                                         ThreadMutex *mesh_eval_mutex);
158
159 BVHTree *bvhtree_from_mesh_edges_ex(struct BVHTreeFromMesh *data,
160                                     const struct MVert *vert,
161                                     const bool vert_allocated,
162                                     const struct MEdge *edge,
163                                     const int edges_num,
164                                     const bool edge_allocated,
165                                     const BLI_bitmap *edges_mask,
166                                     int edges_num_active,
167                                     float epsilon,
168                                     int tree_type,
169                                     int axis,
170                                     const BVHCacheType bvh_cache_type,
171                                     struct BVHCache **bvh_cache_p,
172                                     ThreadMutex *mesh_eval_mutex);
173
174 BVHTree *bvhtree_from_mesh_faces_ex(struct BVHTreeFromMesh *data,
175                                     const struct MVert *vert,
176                                     const bool vert_allocated,
177                                     const struct MFace *face,
178                                     const int numFaces,
179                                     const bool face_allocated,
180                                     const BLI_bitmap *mask,
181                                     int faces_num_active,
182                                     float epsilon,
183                                     int tree_type,
184                                     int axis,
185                                     const BVHCacheType bvh_cache_type,
186                                     struct BVHCache **bvh_cache_p,
187                                     ThreadMutex *mesh_eval_mutex);
188
189 BVHTree *bvhtree_from_editmesh_looptri(
190     BVHTreeFromEditMesh *data, struct BMEditMesh *em, float epsilon, int tree_type, int axis);
191
192 BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data,
193                                           struct BMEditMesh *em,
194                                           const BLI_bitmap *mask,
195                                           int looptri_num_active,
196                                           float epsilon,
197                                           int tree_type,
198                                           int axis,
199                                           const BVHCacheType bvh_cache_type,
200                                           struct BVHCache **bvh_cache_p,
201                                           ThreadMutex *mesh_eval_mutex);
202
203 BVHTree *bvhtree_from_mesh_looptri_ex(struct BVHTreeFromMesh *data,
204                                       const struct MVert *vert,
205                                       const bool vert_allocated,
206                                       const struct MLoop *mloop,
207                                       const bool loop_allocated,
208                                       const struct MLoopTri *looptri,
209                                       const int looptri_num,
210                                       const bool looptri_allocated,
211                                       const BLI_bitmap *mask,
212                                       int looptri_num_active,
213                                       float epsilon,
214                                       int tree_type,
215                                       int axis,
216                                       const BVHCacheType bvh_cache_type,
217                                       struct BVHCache **bvh_cache_p,
218                                       ThreadMutex *mesh_eval_mutex);
219
220 BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
221                                    struct Mesh *mesh,
222                                    const BVHCacheType bvh_cache_type,
223                                    const int tree_type);
224
225 BVHTree *BKE_bvhtree_from_editmesh_get(BVHTreeFromEditMesh *data,
226                                        struct BMEditMesh *em,
227                                        const int tree_type,
228                                        const BVHCacheType bvh_cache_type,
229                                        struct BVHCache **bvh_cache_p,
230                                        ThreadMutex *mesh_eval_mutex);
231
232 /**
233  * Frees data allocated by a call to bvhtree_from_mesh_*.
234  */
235 void free_bvhtree_from_editmesh(struct BVHTreeFromEditMesh *data);
236 void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data);
237
238 /**
239  * Math functions used by callbacks
240  */
241 float bvhtree_ray_tri_intersection(const BVHTreeRay *ray,
242                                    const float m_dist,
243                                    const float v0[3],
244                                    const float v1[3],
245                                    const float v2[3]);
246 float bvhtree_sphereray_tri_intersection(const BVHTreeRay *ray,
247                                          float radius,
248                                          const float m_dist,
249                                          const float v0[3],
250                                          const float v1[3],
251                                          const float v2[3]);
252
253 typedef struct BVHTreeFromPointCloud {
254   struct BVHTree *tree;
255
256   BVHTree_NearestPointCallback nearest_callback;
257
258   const float (*coords)[3];
259 } BVHTreeFromPointCloud;
260
261 BVHTree *BKE_bvhtree_from_pointcloud_get(struct BVHTreeFromPointCloud *data,
262                                          const struct PointCloud *pointcloud,
263                                          const int tree_type);
264
265 void free_bvhtree_from_pointcloud(struct BVHTreeFromPointCloud *data);
266
267 /**
268  * BVHCache
269  */
270
271 /* Using local coordinates */
272
273 bool bvhcache_has_tree(const struct BVHCache *bvh_cache, const BVHTree *tree);
274 struct BVHCache *bvhcache_init(void);
275 void bvhcache_free(struct BVHCache *bvh_cache);
276
277 #ifdef __cplusplus
278 }
279 #endif