Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2019-03-18 01:28:32 +0300
committerCampbell Barton <ideasman42@gmail.com>2019-03-18 01:40:38 +0300
commit4b3aafd44ffd855f0cdb0d5e368c1abce238e11d (patch)
tree61575921a8dc85db3c03be8f2bbc0295101058b3 /source/blender/blenkernel/intern/boids.c
parentdf96455c55110da00f0543c5895376ffbc66313b (diff)
BLI_kdtree: refactor boids specific logic into callback
Logic to for boids to avoid head-on collisions was in BLI_kdtree. Move this into a callback which is now defined in boids.c so the kdtree code can be kept generic.
Diffstat (limited to 'source/blender/blenkernel/intern/boids.c')
-rw-r--r--source/blender/blenkernel/intern/boids.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/source/blender/blenkernel/intern/boids.c b/source/blender/blenkernel/intern/boids.c
index 54a8929a7f7..8b71aa0fb69 100644
--- a/source/blender/blenkernel/intern/boids.c
+++ b/source/blender/blenkernel/intern/boids.c
@@ -45,6 +45,23 @@
#include "RNA_enum_types.h"
+static float len_squared_v3v3_with_normal_bias(
+ const float co_search[3], const float co_test[3], const void *user_data)
+{
+ const float *normal = user_data;
+ float d[3], dist;
+
+ sub_v3_v3v3(d, co_test, co_search);
+
+ dist = len_squared_v3(d);
+
+ /* Avoid head-on collisions. */
+ if (dot_v3v3(d, normal) < 0.0f) {
+ dist *= 10.0f;
+ }
+ return dist;
+}
+
typedef struct BoidValues {
float max_speed, max_acc;
float max_ave, min_speed;
@@ -257,9 +274,9 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
//check boids in own system
if (acbr->options & BRULE_ACOLL_WITH_BOIDS) {
- neighbors = BLI_kdtree_range_search__normal(
- bbd->sim->psys->tree, pa->prev_state.co, pa->prev_state.ave,
- &ptn, acbr->look_ahead * len_v3(pa->prev_state.vel));
+ neighbors = BLI_kdtree_range_search_with_len_squared_cb(
+ bbd->sim->psys->tree, pa->prev_state.co, &ptn, acbr->look_ahead * len_v3(pa->prev_state.vel),
+ len_squared_v3v3_with_normal_bias, pa->prev_state.ave);
if (neighbors > 1) for (n=1; n<neighbors; n++) {
copy_v3_v3(co1, pa->prev_state.co);
copy_v3_v3(vel1, pa->prev_state.vel);
@@ -306,9 +323,9 @@ static int rule_avoid_collision(BoidRule *rule, BoidBrainData *bbd, BoidValues *
if (epsys) {
BLI_assert(epsys->tree != NULL);
- neighbors = BLI_kdtree_range_search__normal(
- epsys->tree, pa->prev_state.co, pa->prev_state.ave,
- &ptn, acbr->look_ahead * len_v3(pa->prev_state.vel));
+ neighbors = BLI_kdtree_range_search_with_len_squared_cb(
+ epsys->tree, pa->prev_state.co, &ptn, acbr->look_ahead * len_v3(pa->prev_state.vel),
+ len_squared_v3v3_with_normal_bias, pa->prev_state.ave);
if (neighbors > 0) for (n=0; n<neighbors; n++) {
copy_v3_v3(co1, pa->prev_state.co);
@@ -406,7 +423,9 @@ static int rule_flock(BoidRule *UNUSED(rule), BoidBrainData *bbd, BoidValues *UN
{
KDTreeNearest ptn[11];
float vec[3] = {0.0f, 0.0f, 0.0f}, loc[3] = {0.0f, 0.0f, 0.0f};
- int neighbors = BLI_kdtree_find_nearest_n__normal(bbd->sim->psys->tree, pa->state.co, pa->prev_state.ave, ptn, 11);
+ int neighbors = BLI_kdtree_find_nearest_n_with_len_squared_cb(
+ bbd->sim->psys->tree, pa->state.co, ptn, ARRAY_SIZE(ptn),
+ len_squared_v3v3_with_normal_bias, pa->prev_state.ave);
int n;
int ret = 0;