2011-02-18 13:58:08 +00:00
|
|
|
/*
|
2008-05-07 20:42:16 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-05-07 20:42:16 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2006 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2012-02-17 18:59:41 +00:00
|
|
|
#pragma once
|
2008-05-07 20:42:16 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2011-02-18 13:58:08 +00:00
|
|
|
*/
|
|
|
|
|
2019-09-12 19:58:11 +02:00
|
|
|
#include "BLI_sys_types.h"
|
|
|
|
|
2010-04-15 10:28:32 +00:00
|
|
|
#ifdef __cplusplus
|
2017-06-11 18:42:11 +10:00
|
|
|
extern "C" {
|
2010-04-15 10:28:32 +00:00
|
|
|
#endif
|
|
|
|
|
2008-05-07 20:42:16 +00:00
|
|
|
struct BVHTree;
|
2018-05-14 23:12:51 +02:00
|
|
|
struct DistProjectedAABBPrecalc;
|
|
|
|
|
2008-05-07 20:42:16 +00:00
|
|
|
typedef struct BVHTree BVHTree;
|
2015-08-21 17:46:23 +10:00
|
|
|
#define USE_KDOPBVH_WATERTIGHT
|
2008-05-07 20:42:16 +00:00
|
|
|
|
2016-02-09 22:12:05 +11:00
|
|
|
typedef struct BVHTreeAxisRange {
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
float min, max;
|
|
|
|
};
|
|
|
|
/* alternate access */
|
|
|
|
float range[2];
|
|
|
|
};
|
|
|
|
} BVHTreeAxisRange;
|
|
|
|
|
2008-05-07 20:42:16 +00:00
|
|
|
typedef struct BVHTreeOverlap {
|
|
|
|
int indexA;
|
|
|
|
int indexB;
|
|
|
|
} BVHTreeOverlap;
|
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
typedef struct BVHTreeNearest {
|
2019-01-08 10:28:20 +11:00
|
|
|
/** The index of the nearest found
|
|
|
|
* (untouched if none is found within a dist radius from the given coordinates) */
|
|
|
|
int index;
|
|
|
|
/** Nearest coordinates
|
|
|
|
* (untouched it none is found within a dist radius from the given coordinates). */
|
|
|
|
float co[3];
|
|
|
|
/** Normal at nearest coordinates
|
|
|
|
* (untouched it none is found within a dist radius from the given coordinates). */
|
|
|
|
float no[3];
|
|
|
|
/** squared distance to search around */
|
|
|
|
float dist_sq;
|
2012-05-30 12:53:13 +00:00
|
|
|
int flags;
|
2008-08-07 17:27:29 +00:00
|
|
|
} BVHTreeNearest;
|
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
typedef struct BVHTreeRay {
|
2019-01-08 10:28:20 +11:00
|
|
|
/** ray origin */
|
|
|
|
float origin[3];
|
|
|
|
/** ray direction */
|
|
|
|
float direction[3];
|
|
|
|
/** radius around ray */
|
|
|
|
float radius;
|
2015-08-21 17:46:23 +10:00
|
|
|
#ifdef USE_KDOPBVH_WATERTIGHT
|
|
|
|
struct IsectRayPrecalc *isect_precalc;
|
|
|
|
#endif
|
2008-08-07 17:27:29 +00:00
|
|
|
} BVHTreeRay;
|
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
typedef struct BVHTreeRayHit {
|
2019-01-08 10:28:20 +11:00
|
|
|
/** Index of the tree node (untouched if no hit is found). */
|
|
|
|
int index;
|
|
|
|
/** Coordinates of the hit point. */
|
|
|
|
float co[3];
|
|
|
|
/** Normal on hit point. */
|
|
|
|
float no[3];
|
|
|
|
/** Distance to the hit point. */
|
|
|
|
float dist;
|
2008-08-07 17:27:29 +00:00
|
|
|
} BVHTreeRayHit;
|
|
|
|
|
2019-09-12 12:26:03 -03:00
|
|
|
enum {
|
|
|
|
/* Use a priority queue to process nodes in the optimal order (for slow callbacks) */
|
|
|
|
BVH_OVERLAP_USE_THREADING = (1 << 0),
|
|
|
|
BVH_OVERLAP_RETURN_PAIRS = (1 << 1),
|
|
|
|
};
|
2018-10-20 21:02:52 +03:00
|
|
|
enum {
|
|
|
|
/* Use a priority queue to process nodes in the optimal order (for slow callbacks) */
|
|
|
|
BVH_NEAREST_OPTIMAL_ORDER = (1 << 0),
|
|
|
|
};
|
2015-08-21 17:46:23 +10:00
|
|
|
enum {
|
|
|
|
/* calculate IsectRayPrecalc data */
|
|
|
|
BVH_RAYCAST_WATERTIGHT = (1 << 0),
|
|
|
|
};
|
|
|
|
#define BVH_RAYCAST_DEFAULT (BVH_RAYCAST_WATERTIGHT)
|
2016-01-12 09:37:56 +01:00
|
|
|
#define BVH_RAYCAST_DIST_MAX (FLT_MAX / 2.0f)
|
2015-08-21 17:46:23 +10:00
|
|
|
|
2008-08-07 17:27:29 +00:00
|
|
|
/* callback must update nearest in case it finds a nearest result */
|
2012-10-15 23:11:59 +00:00
|
|
|
typedef void (*BVHTree_NearestPointCallback)(void *userdata,
|
|
|
|
int index,
|
|
|
|
const float co[3],
|
|
|
|
BVHTreeNearest *nearest);
|
2008-08-07 17:27:29 +00:00
|
|
|
|
|
|
|
/* callback must update hit in case it finds a nearest successful hit */
|
2012-05-12 20:39:39 +00:00
|
|
|
typedef void (*BVHTree_RayCastCallback)(void *userdata,
|
|
|
|
int index,
|
|
|
|
const BVHTreeRay *ray,
|
|
|
|
BVHTreeRayHit *hit);
|
2008-08-07 17:27:29 +00:00
|
|
|
|
2015-08-20 17:32:25 +10:00
|
|
|
/* callback to check if 2 nodes overlap (use thread if intersection results need to be stored) */
|
2015-08-20 19:22:09 +10:00
|
|
|
typedef bool (*BVHTree_OverlapCallback)(void *userdata, int index_a, int index_b, int thread);
|
2015-08-20 17:32:25 +10:00
|
|
|
|
2008-10-01 03:35:53 +00:00
|
|
|
/* callback to range search query */
|
2016-03-19 17:16:50 +11:00
|
|
|
typedef void (*BVHTree_RangeQuery)(void *userdata, int index, const float co[3], float dist_sq);
|
2008-08-07 17:27:29 +00:00
|
|
|
|
2018-05-14 16:00:13 -03:00
|
|
|
/* callback to find nearest projected */
|
2018-05-14 23:12:51 +02:00
|
|
|
typedef void (*BVHTree_NearestProjectedCallback)(void *userdata,
|
|
|
|
int index,
|
|
|
|
const struct DistProjectedAABBPrecalc *precalc,
|
2018-05-16 10:31:27 -03:00
|
|
|
const float (*clip_plane)[4],
|
|
|
|
const int clip_plane_len,
|
2018-05-14 23:12:51 +02:00
|
|
|
BVHTreeNearest *nearest);
|
2016-02-09 22:12:05 +11:00
|
|
|
|
|
|
|
/* callbacks to BLI_bvhtree_walk_dfs */
|
|
|
|
/* return true to traverse into this nodes children, else skip. */
|
|
|
|
typedef bool (*BVHTree_WalkParentCallback)(const BVHTreeAxisRange *bounds, void *userdata);
|
|
|
|
/* return true to keep walking, else early-exit the search. */
|
|
|
|
typedef bool (*BVHTree_WalkLeafCallback)(const BVHTreeAxisRange *bounds,
|
|
|
|
int index,
|
|
|
|
void *userdata);
|
|
|
|
/* return true to search (min, max) else (max, min). */
|
|
|
|
typedef bool (*BVHTree_WalkOrderCallback)(const BVHTreeAxisRange *bounds,
|
|
|
|
char axis,
|
|
|
|
void *userdata);
|
|
|
|
|
2008-05-07 20:42:16 +00:00
|
|
|
BVHTree *BLI_bvhtree_new(int maxsize, float epsilon, char tree_type, char axis);
|
|
|
|
void BLI_bvhtree_free(BVHTree *tree);
|
|
|
|
|
|
|
|
/* construct: first insert points, then call balance */
|
2013-09-04 20:33:50 +00:00
|
|
|
void BLI_bvhtree_insert(BVHTree *tree, int index, const float co[3], int numpoints);
|
2008-05-07 20:42:16 +00:00
|
|
|
void BLI_bvhtree_balance(BVHTree *tree);
|
|
|
|
|
|
|
|
/* update: first update points/nodes, then call update_tree to refit the bounding volumes */
|
2014-09-16 09:14:33 +10:00
|
|
|
bool BLI_bvhtree_update_node(
|
|
|
|
BVHTree *tree, int index, const float co[3], const float co_moving[3], int numpoints);
|
2008-05-07 20:42:16 +00:00
|
|
|
void BLI_bvhtree_update_tree(BVHTree *tree);
|
|
|
|
|
2015-08-20 19:22:09 +10:00
|
|
|
int BLI_bvhtree_overlap_thread_num(const BVHTree *tree);
|
2015-08-20 17:32:25 +10:00
|
|
|
|
2019-01-15 23:15:58 +11:00
|
|
|
/* collision/overlap: check two trees if they overlap,
|
|
|
|
* alloc's *overlap with length of the int return value */
|
2019-09-12 12:26:03 -03:00
|
|
|
BVHTreeOverlap *BLI_bvhtree_overlap_ex(
|
|
|
|
const BVHTree *tree1,
|
|
|
|
const BVHTree *tree2,
|
|
|
|
uint *r_overlap_tot,
|
|
|
|
/* optional callback to test the overlap before adding (must be thread-safe!) */
|
|
|
|
BVHTree_OverlapCallback callback,
|
|
|
|
void *userdata,
|
2019-12-11 22:21:24 -03:00
|
|
|
const uint max_interactions,
|
|
|
|
const int flag);
|
2015-08-20 17:32:25 +10:00
|
|
|
BVHTreeOverlap *BLI_bvhtree_overlap(const BVHTree *tree1,
|
|
|
|
const BVHTree *tree2,
|
|
|
|
unsigned int *r_overlap_tot,
|
|
|
|
BVHTree_OverlapCallback callback,
|
|
|
|
void *userdata);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-07-07 09:45:53 -03:00
|
|
|
int *BLI_bvhtree_intersect_plane(BVHTree *tree, float plane[4], uint *r_intersect_tot);
|
|
|
|
|
2018-02-15 23:36:11 +11:00
|
|
|
int BLI_bvhtree_get_len(const BVHTree *tree);
|
2018-05-03 14:26:39 -03:00
|
|
|
int BLI_bvhtree_get_tree_type(const BVHTree *tree);
|
2016-05-06 06:03:03 +10:00
|
|
|
float BLI_bvhtree_get_epsilon(const BVHTree *tree);
|
2008-05-07 20:42:16 +00:00
|
|
|
|
2012-10-15 23:11:59 +00:00
|
|
|
/* find nearest node to the given coordinates
|
2019-01-15 23:15:58 +11:00
|
|
|
* (if nearest is given it will only search nodes where
|
|
|
|
* square distance is smaller than nearest->dist) */
|
2018-10-20 21:02:52 +03:00
|
|
|
int BLI_bvhtree_find_nearest_ex(BVHTree *tree,
|
|
|
|
const float co[3],
|
|
|
|
BVHTreeNearest *nearest,
|
|
|
|
BVHTree_NearestPointCallback callback,
|
|
|
|
void *userdata,
|
|
|
|
int flag);
|
2016-02-11 05:36:56 +11:00
|
|
|
int BLI_bvhtree_find_nearest(BVHTree *tree,
|
|
|
|
const float co[3],
|
|
|
|
BVHTreeNearest *nearest,
|
|
|
|
BVHTree_NearestPointCallback callback,
|
|
|
|
void *userdata);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-26 14:15:25 -03:00
|
|
|
int BLI_bvhtree_find_nearest_first(BVHTree *tree,
|
|
|
|
const float co[3],
|
|
|
|
const float dist_sq,
|
|
|
|
BVHTree_NearestPointCallback callback,
|
|
|
|
void *userdata);
|
|
|
|
|
2015-08-21 17:46:23 +10:00
|
|
|
int BLI_bvhtree_ray_cast_ex(BVHTree *tree,
|
|
|
|
const float co[3],
|
|
|
|
const float dir[3],
|
|
|
|
float radius,
|
|
|
|
BVHTreeRayHit *hit,
|
|
|
|
BVHTree_RayCastCallback callback,
|
|
|
|
void *userdata,
|
|
|
|
int flag);
|
|
|
|
int BLI_bvhtree_ray_cast(BVHTree *tree,
|
|
|
|
const float co[3],
|
|
|
|
const float dir[3],
|
|
|
|
float radius,
|
|
|
|
BVHTreeRayHit *hit,
|
|
|
|
BVHTree_RayCastCallback callback,
|
|
|
|
void *userdata);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-11 14:33:49 +10:00
|
|
|
void BLI_bvhtree_ray_cast_all_ex(BVHTree *tree,
|
|
|
|
const float co[3],
|
|
|
|
const float dir[3],
|
|
|
|
float radius,
|
|
|
|
float hit_dist,
|
2015-08-21 17:46:23 +10:00
|
|
|
BVHTree_RayCastCallback callback,
|
|
|
|
void *userdata,
|
|
|
|
int flag);
|
2016-05-11 14:33:49 +10:00
|
|
|
void BLI_bvhtree_ray_cast_all(BVHTree *tree,
|
|
|
|
const float co[3],
|
|
|
|
const float dir[3],
|
|
|
|
float radius,
|
|
|
|
float hit_dist,
|
2015-08-21 17:46:23 +10:00
|
|
|
BVHTree_RayCastCallback callback,
|
|
|
|
void *userdata);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-10-15 23:11:59 +00:00
|
|
|
float BLI_bvhtree_bb_raycast(const float bv[6],
|
|
|
|
const float light_start[3],
|
|
|
|
const float light_end[3],
|
|
|
|
float pos[3]);
|
2009-07-30 15:00:26 +00:00
|
|
|
|
2008-10-01 03:35:53 +00:00
|
|
|
/* range query */
|
2016-02-11 05:36:56 +11:00
|
|
|
int BLI_bvhtree_range_query(
|
|
|
|
BVHTree *tree, const float co[3], float radius, BVHTree_RangeQuery callback, void *userdata);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-05-14 16:00:13 -03:00
|
|
|
int BLI_bvhtree_find_nearest_projected(BVHTree *tree,
|
|
|
|
float projmat[4][4],
|
|
|
|
float winsize[2],
|
|
|
|
float mval[2],
|
|
|
|
float clip_planes[6][4],
|
2020-09-04 20:59:13 +02:00
|
|
|
int clip_plane_len,
|
2018-05-14 16:00:13 -03:00
|
|
|
BVHTreeNearest *nearest,
|
|
|
|
BVHTree_NearestProjectedCallback callback,
|
|
|
|
void *userdata);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-09 22:12:05 +11:00
|
|
|
void BLI_bvhtree_walk_dfs(BVHTree *tree,
|
|
|
|
BVHTree_WalkParentCallback walk_parent_cb,
|
|
|
|
BVHTree_WalkLeafCallback walk_leaf_cb,
|
|
|
|
BVHTree_WalkOrderCallback walk_order_cb,
|
|
|
|
void *userdata);
|
2016-02-09 21:53:35 +11:00
|
|
|
|
|
|
|
/* expose for bvh callbacks to use */
|
|
|
|
extern const float bvhtree_kdop_axes[13][3];
|
|
|
|
|
2010-04-15 10:28:32 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|