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