ClangFormat: apply to source, most of intern
[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 #ifndef __BKE_BVHUTILS_H__
20 #define __BKE_BVHUTILS_H__
21
22 /** \file
23  * \ingroup bke
24  */
25
26 #include "BLI_bitmap.h"
27 #include "BLI_kdopbvh.h"
28
29 /**
30  * This header encapsulates necessary code to build a BVH
31  */
32
33 struct BMEditMesh;
34 struct DerivedMesh;
35 struct MFace;
36 struct MVert;
37 struct Mesh;
38
39 typedef struct LinkNode BVHCache;
40
41 /**
42  * Struct that stores basic information about a BVHTree built from a edit-mesh.
43  */
44 typedef struct BVHTreeFromEditMesh {
45   struct BVHTree *tree;
46
47   /* default callbacks to bvh nearest and raycast */
48   BVHTree_NearestPointCallback nearest_callback;
49   BVHTree_RayCastCallback raycast_callback;
50
51   struct BMEditMesh *em;
52
53   /* Private data */
54   bool cached;
55
56 } BVHTreeFromEditMesh;
57
58 /**
59  * Struct that stores basic information about a BVHTree built from a mesh.
60  */
61 typedef struct BVHTreeFromMesh {
62   struct BVHTree *tree;
63
64   /* default callbacks to bvh nearest and raycast */
65   BVHTree_NearestPointCallback nearest_callback;
66   BVHTree_RayCastCallback raycast_callback;
67
68   /* Vertex array, so that callbacks have instante access to data */
69   const struct MVert *vert;
70   const struct MEdge *edge; /* only used for BVHTreeFromMeshEdges */
71   const struct MFace *face;
72   const struct MLoop *loop;
73   const struct MLoopTri *looptri;
74   bool vert_allocated;
75   bool edge_allocated;
76   bool face_allocated;
77   bool loop_allocated;
78   bool looptri_allocated;
79
80   /* Private data */
81   bool cached;
82
83 } BVHTreeFromMesh;
84
85 /**
86  * Builds a bvh tree where nodes are the relevant elements of the given mesh.
87  * Configures BVHTreeFromMesh.
88  *
89  * The tree is build in mesh space coordinates, this means special care must be made on queries
90  * so that the coordinates and rays are first translated on the mesh local coordinates.
91  * Reason for this is that bvh_from_mesh_* can use a cache in some cases and so it becomes possible to reuse a BVHTree.
92  *
93  * free_bvhtree_from_mesh should be called when the tree is no longer needed.
94  */
95 BVHTree *bvhtree_from_editmesh_verts(BVHTreeFromEditMesh *data,
96                                      struct BMEditMesh *em,
97                                      float epsilon,
98                                      int tree_type,
99                                      int axis,
100                                      BVHCache **bvh_cache);
101 BVHTree *bvhtree_from_editmesh_verts_ex(BVHTreeFromEditMesh *data,
102                                         struct BMEditMesh *em,
103                                         const BLI_bitmap *mask,
104                                         int verts_num_active,
105                                         float epsilon,
106                                         int tree_type,
107                                         int axis);
108
109 BVHTree *bvhtree_from_mesh_verts_ex(struct BVHTreeFromMesh *data,
110                                     const struct MVert *vert,
111                                     const int numVerts,
112                                     const bool vert_allocated,
113                                     const BLI_bitmap *mask,
114                                     int verts_num_active,
115                                     float epsilon,
116                                     int tree_type,
117                                     int axis);
118
119 BVHTree *bvhtree_from_editmesh_edges(BVHTreeFromEditMesh *data,
120                                      struct BMEditMesh *em,
121                                      float epsilon,
122                                      int tree_type,
123                                      int axis,
124                                      BVHCache **bvh_cache);
125 BVHTree *bvhtree_from_editmesh_edges_ex(BVHTreeFromEditMesh *data,
126                                         struct BMEditMesh *em,
127                                         const BLI_bitmap *edges_mask,
128                                         int edges_num_active,
129                                         float epsilon,
130                                         int tree_type,
131                                         int axis);
132
133 BVHTree *bvhtree_from_mesh_edges_ex(struct BVHTreeFromMesh *data,
134                                     const struct MVert *vert,
135                                     const bool vert_allocated,
136                                     const struct MEdge *edge,
137                                     const int edges_num,
138                                     const bool edge_allocated,
139                                     const BLI_bitmap *edges_mask,
140                                     int edges_num_active,
141                                     float epsilon,
142                                     int tree_type,
143                                     int axis);
144
145 BVHTree *bvhtree_from_mesh_faces_ex(struct BVHTreeFromMesh *data,
146                                     const struct MVert *vert,
147                                     const bool vert_allocated,
148                                     const struct MFace *face,
149                                     const int numFaces,
150                                     const bool face_allocated,
151                                     const BLI_bitmap *mask,
152                                     int numFaces_active,
153                                     float epsilon,
154                                     int tree_type,
155                                     int axis);
156
157 BVHTree *bvhtree_from_editmesh_looptri(BVHTreeFromEditMesh *data,
158                                        struct BMEditMesh *em,
159                                        float epsilon,
160                                        int tree_type,
161                                        int axis,
162                                        BVHCache **bvhCache);
163 BVHTree *bvhtree_from_editmesh_looptri_ex(BVHTreeFromEditMesh *data,
164                                           struct BMEditMesh *em,
165                                           const BLI_bitmap *mask,
166                                           int looptri_num_active,
167                                           float epsilon,
168                                           int tree_type,
169                                           int axis,
170                                           BVHCache **bvhCache);
171
172 BVHTree *bvhtree_from_mesh_looptri_ex(struct BVHTreeFromMesh *data,
173                                       const struct MVert *vert,
174                                       const bool vert_allocated,
175                                       const struct MLoop *mloop,
176                                       const bool loop_allocated,
177                                       const struct MLoopTri *looptri,
178                                       const int looptri_num,
179                                       const bool looptri_allocated,
180                                       const BLI_bitmap *mask,
181                                       int looptri_num_active,
182                                       float epsilon,
183                                       int tree_type,
184                                       int axis);
185
186 BVHTree *BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data,
187                                    struct Mesh *mesh,
188                                    const int type,
189                                    const int tree_type);
190
191 /**
192  * Frees data allocated by a call to bvhtree_from_mesh_*.
193  */
194 void free_bvhtree_from_editmesh(struct BVHTreeFromEditMesh *data);
195 void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data);
196
197 /**
198  * Math functions used by callbacks
199  */
200 float bvhtree_ray_tri_intersection(const BVHTreeRay *ray,
201                                    const float m_dist,
202                                    const float v0[3],
203                                    const float v1[3],
204                                    const float v2[3]);
205 float bvhtree_sphereray_tri_intersection(const BVHTreeRay *ray,
206                                          float radius,
207                                          const float m_dist,
208                                          const float v0[3],
209                                          const float v1[3],
210                                          const float v2[3]);
211
212 /**
213  * BVHCache
214  */
215
216 /* Using local coordinates */
217 enum {
218   BVHTREE_FROM_VERTS = 0,
219   BVHTREE_FROM_EDGES = 1,
220   BVHTREE_FROM_FACES = 2,
221   BVHTREE_FROM_LOOPTRI = 3,
222
223   BVHTREE_FROM_LOOSEVERTS = 4,
224   BVHTREE_FROM_LOOSEEDGES = 5,
225
226   BVHTREE_FROM_EM_VERTS = 6,
227   BVHTREE_FROM_EM_EDGES = 7,
228   BVHTREE_FROM_EM_LOOPTRI = 8,
229 };
230
231 bool bvhcache_find(const BVHCache *cache, int type, BVHTree **r_tree);
232 bool bvhcache_has_tree(const BVHCache *cache, const BVHTree *tree);
233 void bvhcache_insert(BVHCache **cache_p, BVHTree *tree, int type);
234 void bvhcache_free(BVHCache **cache_p);
235
236 #endif