Blender V2.61 - r43446

BKE_bvhutils.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2006 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): André Pinto
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 #ifndef BKE_BVHUTILS_H
00028 #define BKE_BVHUTILS_H
00029 
00034 #include "BLI_kdopbvh.h"
00035 
00036 /*
00037  * This header encapsulates necessary code to buld a BVH
00038  */
00039 
00040 struct DerivedMesh;
00041 struct MVert;
00042 struct MFace;
00043 
00044 /*
00045  * struct that kepts basic information about a BVHTree build from a mesh
00046  */
00047 typedef struct BVHTreeFromMesh
00048 {
00049     struct BVHTree *tree;
00050 
00051     /* default callbacks to bvh nearest and raycast */
00052     BVHTree_NearestPointCallback nearest_callback;
00053     BVHTree_RayCastCallback      raycast_callback;
00054 
00055     /* Mesh represented on this BVHTree */
00056     struct DerivedMesh *mesh;
00057 
00058     /* Vertex array, so that callbacks have instante access to data */
00059     struct MVert *vert;
00060     struct MEdge *edge;     /* only used for BVHTreeFromMeshEdges */
00061     struct MFace *face;
00062 
00063     /* radius for raycast */
00064     float sphere_radius;
00065 
00066     /* Private data */
00067     int cached;
00068     void *em_evil;  /* var only for snapping */
00069 
00070 } BVHTreeFromMesh;
00071 
00072 /*
00073  * Builds a bvh tree where nodes are the vertexs of the given mesh.
00074  * Configures BVHTreeFromMesh.
00075  *
00076  * The tree is build in mesh space coordinates, this means special care must be made on queries
00077  * so that the coordinates and rays are first translated on the mesh local coordinates.
00078  * Reason for this is that later bvh_from_mesh_* might use a cache system and so it becames possible to reuse
00079  * a BVHTree.
00080  * 
00081  * free_bvhtree_from_mesh should be called when the tree is no longer needed.
00082  */
00083 BVHTree* bvhtree_from_mesh_verts(struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
00084 
00085 /*
00086  * Builds a bvh tree where nodes are the faces of the given mesh.
00087  * Configures BVHTreeFromMesh.
00088  *
00089  * The tree is build in mesh space coordinates, this means special care must be made on queries
00090  * so that the coordinates and rays are first translated on the mesh local coordinates.
00091  * Reason for this is that later bvh_from_mesh_* might use a cache system and so it becames possible to reuse
00092  * a BVHTree.
00093  *
00094  * The returned value is the same as in data->tree, its only returned to make it easier to test
00095  * the success 
00096  * 
00097  * free_bvhtree_from_mesh should be called when the tree is no longer needed.
00098  */
00099 BVHTree* bvhtree_from_mesh_faces(struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
00100 
00101 BVHTree* bvhtree_from_mesh_edges(struct BVHTreeFromMesh *data, struct DerivedMesh *mesh, float epsilon, int tree_type, int axis);
00102 
00103 /*
00104  * Frees data allocated by a call to bvhtree_from_mesh_*.
00105  */
00106 void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data);
00107 
00108 /*
00109 * Math functions used by callbacks
00110 */
00111 float bvhtree_ray_tri_intersection(const BVHTreeRay *ray, const float m_dist, const float v0[3], const float v1[3], const float v2[3]);
00112 float nearest_point_in_tri_surface(const float v0[3], const float v1[3], const float v2[3], const float p[3], int *v, int *e, float nearest[3]);
00113 
00114 /*
00115  * BVHCache
00116  */
00117 
00118 //Using local coordinates
00119 #define BVHTREE_FROM_FACES      0
00120 #define BVHTREE_FROM_VERTICES   1
00121 #define BVHTREE_FROM_EDGES      2
00122 
00123 typedef struct LinkNode* BVHCache;
00124 
00125 
00126 /*
00127  * Queries a bvhcache for the chache bvhtree of the request type
00128  */
00129 BVHTree *bvhcache_find(BVHCache *cache, int type);
00130 
00131 /*
00132  * Inserts a BVHTree of the given type under the cache
00133  * After that the caller no longer needs to worry when to free the BVHTree
00134  * as that will be done when the cache is freed.
00135  *
00136  * A call to this assumes that there was no previous cached tree of the given type
00137  */
00138 void bvhcache_insert(BVHCache *cache, BVHTree *tree, int type);
00139 
00140 /*
00141  * inits and frees a bvhcache
00142  */
00143 void bvhcache_init(BVHCache *cache);
00144 void bvhcache_free(BVHCache *cache);
00145 
00146 #endif
00147