2011-02-23 10:52:22 +00:00
|
|
|
/*
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +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
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
*/
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bli
|
2011-02-27 20:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2009-11-10 20:43:45 +00:00
|
|
|
#include "BLI_math.h"
|
2019-03-18 11:22:48 +11:00
|
|
|
#include "BLI_kdtree_impl.h"
|
2012-10-22 13:00:02 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2013-09-01 20:17:56 +00:00
|
|
|
#include "BLI_strict_flags.h"
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
#define _CONCAT_AUX(MACRO_ARG1, MACRO_ARG2) MACRO_ARG1##MACRO_ARG2
|
2019-03-18 11:22:48 +11:00
|
|
|
#define _CONCAT(MACRO_ARG1, MACRO_ARG2) _CONCAT_AUX(MACRO_ARG1, MACRO_ARG2)
|
|
|
|
|
#define BLI_kdtree_nd_(id) _CONCAT(KDTREE_PREFIX_ID, _##id)
|
|
|
|
|
|
2015-11-17 16:39:29 +11:00
|
|
|
typedef struct KDTreeNode_head {
|
2019-04-17 06:17:24 +02:00
|
|
|
uint left, right;
|
|
|
|
|
float co[KD_DIMS];
|
|
|
|
|
int index;
|
2015-11-17 16:39:29 +11:00
|
|
|
} KDTreeNode_head;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
|
|
|
|
|
typedef struct KDTreeNode {
|
2019-04-17 06:17:24 +02:00
|
|
|
uint left, right;
|
|
|
|
|
float co[KD_DIMS];
|
|
|
|
|
int index;
|
|
|
|
|
uint d; /* range is only (0..KD_DIMS - 1) */
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
} KDTreeNode;
|
|
|
|
|
|
|
|
|
|
struct KDTree {
|
2019-04-17 06:17:24 +02:00
|
|
|
KDTreeNode *nodes;
|
|
|
|
|
uint nodes_len;
|
|
|
|
|
uint root;
|
2014-01-04 01:56:02 +11:00
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
bool is_balanced; /* ensure we call balance first */
|
|
|
|
|
uint nodes_len_capacity; /* max size of the tree */
|
2014-01-04 01:56:02 +11:00
|
|
|
#endif
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
};
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
#define KD_STACK_INIT 100 /* initial size for array (on the stack) */
|
|
|
|
|
#define KD_NEAR_ALLOC_INC 100 /* alloc increment for collecting nearest */
|
|
|
|
|
#define KD_FOUND_ALLOC_INC 50 /* alloc increment for collecting nearest */
|
2013-09-01 08:58:46 +00:00
|
|
|
|
2017-10-28 17:48:45 +11:00
|
|
|
#define KD_NODE_UNSET ((uint)-1)
|
2015-11-17 16:39:29 +11:00
|
|
|
|
2019-04-22 00:54:27 +10:00
|
|
|
/** When set we know all values are unbalanced,
|
|
|
|
|
* otherwise clear them when re-balancing: see T62210. */
|
2019-03-06 14:46:58 +11:00
|
|
|
#define KD_NODE_ROOT_IS_INIT ((uint)-2)
|
|
|
|
|
|
2019-03-18 11:22:48 +11:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Local Math API
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static void copy_vn_vn(float v0[KD_DIMS], const float v1[KD_DIMS])
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
for (uint j = 0; j < KD_DIMS; j++) {
|
|
|
|
|
v0[j] = v1[j];
|
|
|
|
|
}
|
2019-03-18 11:22:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static float len_squared_vnvn(const float v0[KD_DIMS], const float v1[KD_DIMS])
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
float d = 0.0f;
|
|
|
|
|
for (uint j = 0; j < KD_DIMS; j++) {
|
|
|
|
|
d += SQUARE(v0[j] - v1[j]);
|
|
|
|
|
}
|
|
|
|
|
return d;
|
2019-03-18 11:22:48 +11:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
static float len_squared_vnvn_cb(const float co_kdtree[KD_DIMS],
|
|
|
|
|
const float co_search[KD_DIMS],
|
|
|
|
|
const void *UNUSED(user_data))
|
2019-03-18 11:22:48 +11:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return len_squared_vnvn(co_kdtree, co_search);
|
2019-03-18 11:22:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
2013-09-01 20:17:56 +00:00
|
|
|
/**
|
|
|
|
|
* Creates or free a kdtree
|
|
|
|
|
*/
|
2019-03-18 11:22:48 +11:00
|
|
|
KDTree *BLI_kdtree_nd_(new)(uint nodes_len_capacity)
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
KDTree *tree;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
tree = MEM_mallocN(sizeof(KDTree), "KDTree");
|
|
|
|
|
tree->nodes = MEM_mallocN(sizeof(KDTreeNode) * nodes_len_capacity, "KDTreeNode");
|
|
|
|
|
tree->nodes_len = 0;
|
|
|
|
|
tree->root = KD_NODE_ROOT_IS_INIT;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
|
2014-01-04 01:56:02 +11:00
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
tree->is_balanced = false;
|
|
|
|
|
tree->nodes_len_capacity = nodes_len_capacity;
|
2014-01-04 01:56:02 +11:00
|
|
|
#endif
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
return tree;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-18 11:22:48 +11:00
|
|
|
void BLI_kdtree_nd_(free)(KDTree *tree)
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
if (tree) {
|
|
|
|
|
MEM_freeN(tree->nodes);
|
|
|
|
|
MEM_freeN(tree);
|
|
|
|
|
}
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-01 20:17:56 +00:00
|
|
|
/**
|
|
|
|
|
* Construction: first insert points, then call balance. Normal is optional.
|
|
|
|
|
*/
|
2019-03-18 11:22:48 +11:00
|
|
|
void BLI_kdtree_nd_(insert)(KDTree *tree, int index, const float co[KD_DIMS])
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
KDTreeNode *node = &tree->nodes[tree->nodes_len++];
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
|
2014-01-14 17:11:55 +06:00
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(tree->nodes_len <= tree->nodes_len_capacity);
|
2014-01-14 17:11:55 +06:00
|
|
|
#endif
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
/* note, array isn't calloc'd,
|
|
|
|
|
* need to initialize all struct members */
|
2013-09-01 20:17:56 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
node->left = node->right = KD_NODE_UNSET;
|
|
|
|
|
copy_vn_vn(node->co, co);
|
|
|
|
|
node->index = index;
|
|
|
|
|
node->d = 0;
|
2014-01-04 01:56:02 +11:00
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
tree->is_balanced = false;
|
2014-01-04 01:56:02 +11:00
|
|
|
#endif
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-18 10:21:45 +11:00
|
|
|
static uint kdtree_balance(KDTreeNode *nodes, uint nodes_len, uint axis, const uint ofs)
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
KDTreeNode *node;
|
|
|
|
|
float co;
|
|
|
|
|
uint left, right, median, i, j;
|
|
|
|
|
|
|
|
|
|
if (nodes_len <= 0) {
|
|
|
|
|
return KD_NODE_UNSET;
|
|
|
|
|
}
|
|
|
|
|
else if (nodes_len == 1) {
|
|
|
|
|
return 0 + ofs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* quicksort style sorting around median */
|
|
|
|
|
left = 0;
|
|
|
|
|
right = nodes_len - 1;
|
|
|
|
|
median = nodes_len / 2;
|
|
|
|
|
|
|
|
|
|
while (right > left) {
|
|
|
|
|
co = nodes[right].co[axis];
|
|
|
|
|
i = left - 1;
|
|
|
|
|
j = right;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
while (nodes[++i].co[axis] < co) { /* pass */
|
|
|
|
|
}
|
|
|
|
|
while (nodes[--j].co[axis] > co && j > left) { /* pass */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i >= j) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SWAP(KDTreeNode_head, *(KDTreeNode_head *)&nodes[i], *(KDTreeNode_head *)&nodes[j]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SWAP(KDTreeNode_head, *(KDTreeNode_head *)&nodes[i], *(KDTreeNode_head *)&nodes[right]);
|
|
|
|
|
if (i >= median) {
|
|
|
|
|
right = i - 1;
|
|
|
|
|
}
|
|
|
|
|
if (i <= median) {
|
|
|
|
|
left = i + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* set node and sort subnodes */
|
|
|
|
|
node = &nodes[median];
|
|
|
|
|
node->d = axis;
|
|
|
|
|
axis = (axis + 1) % KD_DIMS;
|
|
|
|
|
node->left = kdtree_balance(nodes, median, axis, ofs);
|
|
|
|
|
node->right = kdtree_balance(
|
|
|
|
|
nodes + median + 1, (nodes_len - (median + 1)), axis, (median + 1) + ofs);
|
|
|
|
|
|
|
|
|
|
return median + ofs;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-18 11:22:48 +11:00
|
|
|
void BLI_kdtree_nd_(balance)(KDTree *tree)
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
if (tree->root != KD_NODE_ROOT_IS_INIT) {
|
|
|
|
|
for (uint i = 0; i < tree->nodes_len; i++) {
|
|
|
|
|
tree->nodes[i].left = KD_NODE_UNSET;
|
|
|
|
|
tree->nodes[i].right = KD_NODE_UNSET;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-06 14:46:58 +11:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
tree->root = kdtree_balance(tree->nodes, tree->nodes_len, 0, 0);
|
2014-01-04 01:56:02 +11:00
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
tree->is_balanced = true;
|
2014-01-04 01:56:02 +11:00
|
|
|
#endif
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-18 10:21:45 +11:00
|
|
|
static uint *realloc_nodes(uint *stack, uint *stack_len_capacity, const bool is_alloc)
|
2013-09-01 08:58:46 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
uint *stack_new = MEM_mallocN((*stack_len_capacity + KD_NEAR_ALLOC_INC) * sizeof(uint),
|
|
|
|
|
"KDTree.treestack");
|
|
|
|
|
memcpy(stack_new, stack, *stack_len_capacity * sizeof(uint));
|
|
|
|
|
// memset(stack_new + *stack_len_capacity, 0, sizeof(uint) * KD_NEAR_ALLOC_INC);
|
|
|
|
|
if (is_alloc) {
|
|
|
|
|
MEM_freeN(stack);
|
|
|
|
|
}
|
|
|
|
|
*stack_len_capacity += KD_NEAR_ALLOC_INC;
|
|
|
|
|
return stack_new;
|
2013-09-01 08:58:46 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-01 20:17:56 +00:00
|
|
|
/**
|
|
|
|
|
* Find nearest returns index, and -1 if no node is found.
|
|
|
|
|
*/
|
2019-04-17 06:17:24 +02:00
|
|
|
int BLI_kdtree_nd_(find_nearest)(const KDTree *tree,
|
|
|
|
|
const float co[KD_DIMS],
|
|
|
|
|
KDTreeNearest *r_nearest)
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *nodes = tree->nodes;
|
|
|
|
|
const KDTreeNode *root, *min_node;
|
|
|
|
|
uint *stack, stack_default[KD_STACK_INIT];
|
|
|
|
|
float min_dist, cur_dist;
|
|
|
|
|
uint stack_len_capacity, cur = 0;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
|
2014-01-04 01:56:02 +11:00
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(tree->is_balanced == true);
|
2014-01-04 01:56:02 +11:00
|
|
|
#endif
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (UNLIKELY(tree->root == KD_NODE_UNSET)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stack = stack_default;
|
|
|
|
|
stack_len_capacity = KD_STACK_INIT;
|
|
|
|
|
|
|
|
|
|
root = &nodes[tree->root];
|
|
|
|
|
min_node = root;
|
|
|
|
|
min_dist = len_squared_vnvn(root->co, co);
|
|
|
|
|
|
|
|
|
|
if (co[root->d] < root->co[root->d]) {
|
|
|
|
|
if (root->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->right;
|
|
|
|
|
}
|
|
|
|
|
if (root->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (root->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->left;
|
|
|
|
|
}
|
|
|
|
|
if (root->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (cur--) {
|
|
|
|
|
const KDTreeNode *node = &nodes[stack[cur]];
|
|
|
|
|
|
|
|
|
|
cur_dist = node->co[node->d] - co[node->d];
|
|
|
|
|
|
|
|
|
|
if (cur_dist < 0.0f) {
|
|
|
|
|
cur_dist = -cur_dist * cur_dist;
|
|
|
|
|
|
|
|
|
|
if (-cur_dist < min_dist) {
|
|
|
|
|
cur_dist = len_squared_vnvn(node->co, co);
|
|
|
|
|
if (cur_dist < min_dist) {
|
|
|
|
|
min_dist = cur_dist;
|
|
|
|
|
min_node = node;
|
|
|
|
|
}
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cur_dist = cur_dist * cur_dist;
|
|
|
|
|
|
|
|
|
|
if (cur_dist < min_dist) {
|
|
|
|
|
cur_dist = len_squared_vnvn(node->co, co);
|
|
|
|
|
if (cur_dist < min_dist) {
|
|
|
|
|
min_dist = cur_dist;
|
|
|
|
|
min_node = node;
|
|
|
|
|
}
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (UNLIKELY(cur + KD_DIMS > stack_len_capacity)) {
|
|
|
|
|
stack = realloc_nodes(stack, &stack_len_capacity, stack_default != stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (r_nearest) {
|
|
|
|
|
r_nearest->index = min_node->index;
|
|
|
|
|
r_nearest->dist = sqrtf(min_dist);
|
|
|
|
|
copy_vn_vn(r_nearest->co, min_node->co);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stack != stack_default) {
|
|
|
|
|
MEM_freeN(stack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return min_node->index;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-06 21:29:06 +11:00
|
|
|
/**
|
2019-03-20 00:46:33 +11:00
|
|
|
* A version of #BLI_kdtree_3d_find_nearest which runs a callback
|
2015-12-06 21:29:06 +11:00
|
|
|
* to filter out values.
|
|
|
|
|
*
|
|
|
|
|
* \param filter_cb: Filter find results,
|
|
|
|
|
* Return codes: (1: accept, 0: skip, -1: immediate exit).
|
|
|
|
|
*/
|
2019-03-18 11:22:48 +11:00
|
|
|
int BLI_kdtree_nd_(find_nearest_cb)(
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTree *tree,
|
|
|
|
|
const float co[KD_DIMS],
|
|
|
|
|
int (*filter_cb)(void *user_data, int index, const float co[KD_DIMS], float dist_sq),
|
|
|
|
|
void *user_data,
|
|
|
|
|
KDTreeNearest *r_nearest)
|
2015-12-06 21:29:06 +11:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *nodes = tree->nodes;
|
|
|
|
|
const KDTreeNode *min_node = NULL;
|
2015-12-06 21:29:06 +11:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
uint *stack, stack_default[KD_STACK_INIT];
|
|
|
|
|
float min_dist = FLT_MAX, cur_dist;
|
|
|
|
|
uint stack_len_capacity, cur = 0;
|
2015-12-06 21:29:06 +11:00
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(tree->is_balanced == true);
|
2015-12-06 21:29:06 +11:00
|
|
|
#endif
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (UNLIKELY(tree->root == KD_NODE_UNSET)) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2015-12-06 21:29:06 +11:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
stack = stack_default;
|
|
|
|
|
stack_len_capacity = ARRAY_SIZE(stack_default);
|
2015-12-06 21:29:06 +11:00
|
|
|
|
|
|
|
|
#define NODE_TEST_NEAREST(node) \
|
2019-04-17 06:17:24 +02:00
|
|
|
{ \
|
|
|
|
|
const float dist_sq = len_squared_vnvn((node)->co, co); \
|
|
|
|
|
if (dist_sq < min_dist) { \
|
|
|
|
|
const int result = filter_cb(user_data, (node)->index, (node)->co, dist_sq); \
|
|
|
|
|
if (result == 1) { \
|
|
|
|
|
min_dist = dist_sq; \
|
|
|
|
|
min_node = node; \
|
|
|
|
|
} \
|
|
|
|
|
else if (result == 0) { \
|
|
|
|
|
/* pass */ \
|
|
|
|
|
} \
|
|
|
|
|
else { \
|
|
|
|
|
BLI_assert(result == -1); \
|
|
|
|
|
goto finally; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
((void)0)
|
|
|
|
|
|
|
|
|
|
stack[cur++] = tree->root;
|
|
|
|
|
|
|
|
|
|
while (cur--) {
|
|
|
|
|
const KDTreeNode *node = &nodes[stack[cur]];
|
|
|
|
|
|
|
|
|
|
cur_dist = node->co[node->d] - co[node->d];
|
|
|
|
|
|
|
|
|
|
if (cur_dist < 0.0f) {
|
|
|
|
|
cur_dist = -cur_dist * cur_dist;
|
|
|
|
|
|
|
|
|
|
if (-cur_dist < min_dist) {
|
|
|
|
|
NODE_TEST_NEAREST(node);
|
|
|
|
|
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cur_dist = cur_dist * cur_dist;
|
|
|
|
|
|
|
|
|
|
if (cur_dist < min_dist) {
|
|
|
|
|
NODE_TEST_NEAREST(node);
|
|
|
|
|
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (UNLIKELY(cur + KD_DIMS > stack_len_capacity)) {
|
|
|
|
|
stack = realloc_nodes(stack, &stack_len_capacity, stack_default != stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-06 21:29:06 +11:00
|
|
|
|
|
|
|
|
#undef NODE_TEST_NEAREST
|
|
|
|
|
|
|
|
|
|
finally:
|
2019-04-17 06:17:24 +02:00
|
|
|
if (stack != stack_default) {
|
|
|
|
|
MEM_freeN(stack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (min_node) {
|
|
|
|
|
if (r_nearest) {
|
|
|
|
|
r_nearest->index = min_node->index;
|
|
|
|
|
r_nearest->dist = sqrtf(min_dist);
|
|
|
|
|
copy_vn_vn(r_nearest->co, min_node->co);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return min_node->index;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2015-12-06 21:29:06 +11:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
static void nearest_ordered_insert(KDTreeNearest *nearest,
|
|
|
|
|
uint *nearest_len,
|
|
|
|
|
const uint nearest_len_capacity,
|
|
|
|
|
const int index,
|
|
|
|
|
const float dist,
|
|
|
|
|
const float co[KD_DIMS])
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
uint i;
|
|
|
|
|
|
|
|
|
|
if (*nearest_len < nearest_len_capacity) {
|
|
|
|
|
(*nearest_len)++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = *nearest_len - 1; i > 0; i--) {
|
|
|
|
|
if (dist >= nearest[i - 1].dist) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
nearest[i] = nearest[i - 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nearest[i].index = index;
|
|
|
|
|
nearest[i].dist = dist;
|
|
|
|
|
copy_vn_vn(nearest[i].co, co);
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2013-09-01 20:17:56 +00:00
|
|
|
/**
|
2019-03-18 10:21:45 +11:00
|
|
|
* Find \a nearest_len_capacity nearest returns number of points found, with results in nearest.
|
2013-09-01 20:17:56 +00:00
|
|
|
*
|
2019-03-18 10:21:45 +11:00
|
|
|
* \param r_nearest: An array of nearest, sized at least \a nearest_len_capacity.
|
2013-09-01 20:17:56 +00:00
|
|
|
*/
|
2019-03-18 11:22:48 +11:00
|
|
|
int BLI_kdtree_nd_(find_nearest_n_with_len_squared_cb)(
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTree *tree,
|
|
|
|
|
const float co[KD_DIMS],
|
|
|
|
|
KDTreeNearest r_nearest[],
|
|
|
|
|
const uint nearest_len_capacity,
|
|
|
|
|
float (*len_sq_fn)(const float co_search[KD_DIMS],
|
|
|
|
|
const float co_test[KD_DIMS],
|
|
|
|
|
const void *user_data),
|
|
|
|
|
const void *user_data)
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *nodes = tree->nodes;
|
|
|
|
|
const KDTreeNode *root;
|
|
|
|
|
uint *stack, stack_default[KD_STACK_INIT];
|
|
|
|
|
float cur_dist;
|
|
|
|
|
uint stack_len_capacity, cur = 0;
|
|
|
|
|
uint i, nearest_len = 0;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
|
2014-01-04 01:56:02 +11:00
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(tree->is_balanced == true);
|
2014-01-04 01:56:02 +11:00
|
|
|
#endif
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (UNLIKELY((tree->root == KD_NODE_UNSET) || nearest_len_capacity == 0)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len_sq_fn == NULL) {
|
|
|
|
|
len_sq_fn = len_squared_vnvn_cb;
|
|
|
|
|
BLI_assert(user_data == NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stack = stack_default;
|
|
|
|
|
stack_len_capacity = ARRAY_SIZE(stack_default);
|
|
|
|
|
|
|
|
|
|
root = &nodes[tree->root];
|
|
|
|
|
|
|
|
|
|
cur_dist = len_sq_fn(co, root->co, user_data);
|
|
|
|
|
nearest_ordered_insert(
|
|
|
|
|
r_nearest, &nearest_len, nearest_len_capacity, root->index, cur_dist, root->co);
|
|
|
|
|
|
|
|
|
|
if (co[root->d] < root->co[root->d]) {
|
|
|
|
|
if (root->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->right;
|
|
|
|
|
}
|
|
|
|
|
if (root->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (root->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->left;
|
|
|
|
|
}
|
|
|
|
|
if (root->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = root->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (cur--) {
|
|
|
|
|
const KDTreeNode *node = &nodes[stack[cur]];
|
|
|
|
|
|
|
|
|
|
cur_dist = node->co[node->d] - co[node->d];
|
|
|
|
|
|
|
|
|
|
if (cur_dist < 0.0f) {
|
|
|
|
|
cur_dist = -cur_dist * cur_dist;
|
|
|
|
|
|
|
|
|
|
if (nearest_len < nearest_len_capacity || -cur_dist < r_nearest[nearest_len - 1].dist) {
|
|
|
|
|
cur_dist = len_sq_fn(co, node->co, user_data);
|
|
|
|
|
|
|
|
|
|
if (nearest_len < nearest_len_capacity || cur_dist < r_nearest[nearest_len - 1].dist) {
|
|
|
|
|
nearest_ordered_insert(
|
|
|
|
|
r_nearest, &nearest_len, nearest_len_capacity, node->index, cur_dist, node->co);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cur_dist = cur_dist * cur_dist;
|
|
|
|
|
|
|
|
|
|
if (nearest_len < nearest_len_capacity || cur_dist < r_nearest[nearest_len - 1].dist) {
|
|
|
|
|
cur_dist = len_sq_fn(co, node->co, user_data);
|
|
|
|
|
if (nearest_len < nearest_len_capacity || cur_dist < r_nearest[nearest_len - 1].dist) {
|
|
|
|
|
nearest_ordered_insert(
|
|
|
|
|
r_nearest, &nearest_len, nearest_len_capacity, node->index, cur_dist, node->co);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (UNLIKELY(cur + KD_DIMS > stack_len_capacity)) {
|
|
|
|
|
stack = realloc_nodes(stack, &stack_len_capacity, stack_default != stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < nearest_len; i++) {
|
|
|
|
|
r_nearest[i].dist = sqrtf(r_nearest[i].dist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stack != stack_default) {
|
|
|
|
|
MEM_freeN(stack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (int)nearest_len;
|
Particles
=========
Merge of the famous particle patch by Janne Karhu, a full rewrite
of the Blender particle system. This includes:
- Emitter, Hair and Reactor particle types.
- Newtonian, Keyed and Boids physics.
- Various particle visualisation and rendering types.
- Vertex group and texture control for various properties.
- Interpolated child particles from parents.
- Hair editing with combing, growing, cutting, .. .
- Explode modifier.
- Harmonic, Magnetic fields, and multiple falloff types.
.. and lots of other things, some more info is here:
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite
http://wiki.blender.org/index.php/BlenderDev/Particles_Rewrite_Doc
The new particle system cannot be backwards compatible. Old particle
systems are being converted to the new system, but will require
tweaking to get them looking the same as before.
Point Cache
===========
The new system to replace manual baking, based on automatic caching
on disk. This is currently used by softbodies and the particle system.
See the Cache API section on:
http://wiki.blender.org/index.php/BlenderDev/PhysicsSprint
Documentation
=============
These new features still need good docs for the release logs, help
for this is appreciated.
2007-11-26 22:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
int BLI_kdtree_nd_(find_nearest_n)(const KDTree *tree,
|
|
|
|
|
const float co[KD_DIMS],
|
|
|
|
|
KDTreeNearest r_nearest[],
|
|
|
|
|
const uint nearest_len_capacity)
|
2019-03-18 11:22:48 +11:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return BLI_kdtree_nd_(find_nearest_n_with_len_squared_cb)(
|
|
|
|
|
tree, co, r_nearest, nearest_len_capacity, NULL, NULL);
|
2019-03-18 11:22:48 +11:00
|
|
|
}
|
|
|
|
|
|
2019-03-18 10:21:45 +11:00
|
|
|
static int nearest_cmp_dist(const void *a, const void *b)
|
2009-07-20 23:52:53 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNearest *kda = a;
|
|
|
|
|
const KDTreeNearest *kdb = b;
|
|
|
|
|
|
|
|
|
|
if (kda->dist < kdb->dist) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else if (kda->dist > kdb->dist) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2009-07-20 23:52:53 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
static void nearest_add_in_range(KDTreeNearest **r_nearest,
|
|
|
|
|
uint nearest_index,
|
|
|
|
|
uint *nearest_len_capacity,
|
|
|
|
|
const int index,
|
|
|
|
|
const float dist,
|
|
|
|
|
const float co[KD_DIMS])
|
2009-07-20 23:52:53 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
KDTreeNearest *to;
|
2009-07-20 23:52:53 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (UNLIKELY(nearest_index >= *nearest_len_capacity)) {
|
|
|
|
|
*r_nearest = MEM_reallocN_id(
|
|
|
|
|
*r_nearest, (*nearest_len_capacity += KD_FOUND_ALLOC_INC) * sizeof(KDTreeNode), __func__);
|
|
|
|
|
}
|
2009-07-20 23:52:53 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
to = (*r_nearest) + nearest_index;
|
2009-07-20 23:52:53 +00:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
to->index = index;
|
|
|
|
|
to->dist = sqrtf(dist);
|
|
|
|
|
copy_vn_vn(to->co, co);
|
2009-07-20 23:52:53 +00:00
|
|
|
}
|
2013-09-01 08:58:46 +00:00
|
|
|
|
2013-09-01 20:17:56 +00:00
|
|
|
/**
|
2019-03-18 10:21:45 +11:00
|
|
|
* Range search returns number of points nearest_len, with results in nearest
|
|
|
|
|
*
|
|
|
|
|
* \param r_nearest: Allocated array of nearest nearest_len (caller is responsible for freeing).
|
2013-09-01 20:17:56 +00:00
|
|
|
*/
|
2019-03-18 11:22:48 +11:00
|
|
|
int BLI_kdtree_nd_(range_search_with_len_squared_cb)(
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTree *tree,
|
|
|
|
|
const float co[KD_DIMS],
|
|
|
|
|
KDTreeNearest **r_nearest,
|
|
|
|
|
const float range,
|
|
|
|
|
float (*len_sq_fn)(const float co_search[KD_DIMS],
|
|
|
|
|
const float co_test[KD_DIMS],
|
|
|
|
|
const void *user_data),
|
|
|
|
|
const void *user_data)
|
2009-07-20 23:52:53 +00:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *nodes = tree->nodes;
|
|
|
|
|
uint *stack, stack_default[KD_STACK_INIT];
|
|
|
|
|
KDTreeNearest *nearest = NULL;
|
|
|
|
|
const float range_sq = range * range;
|
|
|
|
|
float dist_sq;
|
|
|
|
|
uint stack_len_capacity, cur = 0;
|
|
|
|
|
uint nearest_len = 0, nearest_len_capacity = 0;
|
2009-07-20 23:52:53 +00:00
|
|
|
|
2014-01-04 01:56:02 +11:00
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(tree->is_balanced == true);
|
2014-01-04 01:56:02 +11:00
|
|
|
#endif
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (UNLIKELY(tree->root == KD_NODE_UNSET)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len_sq_fn == NULL) {
|
|
|
|
|
len_sq_fn = len_squared_vnvn_cb;
|
|
|
|
|
BLI_assert(user_data == NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stack = stack_default;
|
|
|
|
|
stack_len_capacity = ARRAY_SIZE(stack_default);
|
|
|
|
|
|
|
|
|
|
stack[cur++] = tree->root;
|
|
|
|
|
|
|
|
|
|
while (cur--) {
|
|
|
|
|
const KDTreeNode *node = &nodes[stack[cur]];
|
|
|
|
|
|
|
|
|
|
if (co[node->d] + range < node->co[node->d]) {
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (co[node->d] - range > node->co[node->d]) {
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
dist_sq = len_sq_fn(co, node->co, user_data);
|
|
|
|
|
if (dist_sq <= range_sq) {
|
|
|
|
|
nearest_add_in_range(
|
|
|
|
|
&nearest, nearest_len++, &nearest_len_capacity, node->index, dist_sq, node->co);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY(cur + KD_DIMS > stack_len_capacity)) {
|
|
|
|
|
stack = realloc_nodes(stack, &stack_len_capacity, stack_default != stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stack != stack_default) {
|
|
|
|
|
MEM_freeN(stack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nearest_len) {
|
|
|
|
|
qsort(nearest, nearest_len, sizeof(KDTreeNearest), nearest_cmp_dist);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*r_nearest = nearest;
|
|
|
|
|
|
|
|
|
|
return (int)nearest_len;
|
2009-07-20 23:52:53 +00:00
|
|
|
}
|
2015-11-18 09:01:55 +11:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
int BLI_kdtree_nd_(range_search)(const KDTree *tree,
|
|
|
|
|
const float co[KD_DIMS],
|
|
|
|
|
KDTreeNearest **r_nearest,
|
|
|
|
|
const float range)
|
2019-03-18 11:22:48 +11:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
return BLI_kdtree_nd_(range_search_with_len_squared_cb)(tree, co, r_nearest, range, NULL, NULL);
|
2019-03-18 11:22:48 +11:00
|
|
|
}
|
|
|
|
|
|
2015-11-18 09:01:55 +11:00
|
|
|
/**
|
2019-03-20 00:46:33 +11:00
|
|
|
* A version of #BLI_kdtree_3d_range_search which runs a callback
|
2015-11-18 09:01:55 +11:00
|
|
|
* instead of allocating an array.
|
|
|
|
|
*
|
2019-04-22 00:54:27 +10:00
|
|
|
* \param search_cb: Called for every node found in \a range,
|
|
|
|
|
* false return value performs an early exit.
|
2015-11-18 09:01:55 +11:00
|
|
|
*
|
|
|
|
|
* \note the order of calls isn't sorted based on distance.
|
|
|
|
|
*/
|
2019-03-18 11:22:48 +11:00
|
|
|
void BLI_kdtree_nd_(range_search_cb)(
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTree *tree,
|
|
|
|
|
const float co[KD_DIMS],
|
|
|
|
|
float range,
|
|
|
|
|
bool (*search_cb)(void *user_data, int index, const float co[KD_DIMS], float dist_sq),
|
|
|
|
|
void *user_data)
|
2015-11-18 09:01:55 +11:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *nodes = tree->nodes;
|
2015-12-06 15:57:10 +11:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
uint *stack, stack_default[KD_STACK_INIT];
|
|
|
|
|
float range_sq = range * range, dist_sq;
|
|
|
|
|
uint stack_len_capacity, cur = 0;
|
2015-11-18 09:01:55 +11:00
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
BLI_assert(tree->is_balanced == true);
|
2015-11-18 09:01:55 +11:00
|
|
|
#endif
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
if (UNLIKELY(tree->root == KD_NODE_UNSET)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stack = stack_default;
|
|
|
|
|
stack_len_capacity = ARRAY_SIZE(stack_default);
|
|
|
|
|
|
|
|
|
|
stack[cur++] = tree->root;
|
|
|
|
|
|
|
|
|
|
while (cur--) {
|
|
|
|
|
const KDTreeNode *node = &nodes[stack[cur]];
|
|
|
|
|
|
|
|
|
|
if (co[node->d] + range < node->co[node->d]) {
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (co[node->d] - range > node->co[node->d]) {
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
dist_sq = len_squared_vnvn(node->co, co);
|
|
|
|
|
if (dist_sq <= range_sq) {
|
|
|
|
|
if (search_cb(user_data, node->index, node->co, dist_sq) == false) {
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->left;
|
|
|
|
|
}
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
stack[cur++] = node->right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UNLIKELY(cur + KD_DIMS > stack_len_capacity)) {
|
|
|
|
|
stack = realloc_nodes(stack, &stack_len_capacity, stack_default != stack);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-18 09:01:55 +11:00
|
|
|
|
|
|
|
|
finally:
|
2019-04-17 06:17:24 +02:00
|
|
|
if (stack != stack_default) {
|
|
|
|
|
MEM_freeN(stack);
|
|
|
|
|
}
|
2015-11-18 09:01:55 +11:00
|
|
|
}
|
2017-09-03 22:34:49 +10:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use when we want to loop over nodes ordered by index.
|
|
|
|
|
* Requires indices to be aligned with nodes.
|
|
|
|
|
*/
|
|
|
|
|
static uint *kdtree_order(const KDTree *tree)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *nodes = tree->nodes;
|
|
|
|
|
uint *order = MEM_mallocN(sizeof(uint) * tree->nodes_len, __func__);
|
|
|
|
|
for (uint i = 0; i < tree->nodes_len; i++) {
|
|
|
|
|
order[nodes[i].index] = i;
|
|
|
|
|
}
|
|
|
|
|
return order;
|
2017-09-03 22:34:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2019-03-20 00:46:33 +11:00
|
|
|
/** \name BLI_kdtree_3d_calc_duplicates_fast
|
2017-09-03 22:34:49 +10:00
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
struct DeDuplicateParams {
|
2019-04-17 06:17:24 +02:00
|
|
|
/* Static */
|
|
|
|
|
const KDTreeNode *nodes;
|
|
|
|
|
float range;
|
|
|
|
|
float range_sq;
|
|
|
|
|
int *duplicates;
|
|
|
|
|
int *duplicates_found;
|
|
|
|
|
|
|
|
|
|
/* Per Search */
|
|
|
|
|
float search_co[KD_DIMS];
|
|
|
|
|
int search;
|
2017-09-03 22:34:49 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void deduplicate_recursive(const struct DeDuplicateParams *p, uint i)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *node = &p->nodes[i];
|
|
|
|
|
if (p->search_co[node->d] + p->range <= node->co[node->d]) {
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
deduplicate_recursive(p, node->left);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (p->search_co[node->d] - p->range >= node->co[node->d]) {
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
deduplicate_recursive(p, node->right);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if ((p->search != node->index) && (p->duplicates[node->index] == -1)) {
|
|
|
|
|
if (len_squared_vnvn(node->co, p->search_co) <= p->range_sq) {
|
|
|
|
|
p->duplicates[node->index] = (int)p->search;
|
|
|
|
|
*p->duplicates_found += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (node->left != KD_NODE_UNSET) {
|
|
|
|
|
deduplicate_recursive(p, node->left);
|
|
|
|
|
}
|
|
|
|
|
if (node->right != KD_NODE_UNSET) {
|
|
|
|
|
deduplicate_recursive(p, node->right);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-03 22:34:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find duplicate points in \a range.
|
|
|
|
|
* Favors speed over quality since it doesn't find the best target vertex for merging.
|
|
|
|
|
* Nodes are looped over, duplicates are added when found.
|
|
|
|
|
* Nevertheless results are predictable.
|
|
|
|
|
*
|
|
|
|
|
* \param range: Coordinates in this range are candidates to be merged.
|
|
|
|
|
* \param use_index_order: Loop over the coordinates ordered by #KDTreeNode.index
|
|
|
|
|
* At the expense of some performance, this ensures the layout of the tree doesn't influence
|
|
|
|
|
* the iteration order.
|
2019-03-18 10:21:45 +11:00
|
|
|
* \param duplicates: An array of int's the length of #KDTree.nodes_len
|
2017-09-03 22:34:49 +10:00
|
|
|
* Values initialized to -1 are candidates to me merged.
|
|
|
|
|
* Setting the index to it's own position in the array prevents it from being touched,
|
|
|
|
|
* although it can still be used as a target.
|
2019-03-18 10:21:45 +11:00
|
|
|
* \returns The number of merges found (includes any merges already in the \a duplicates array).
|
2017-09-03 22:34:49 +10:00
|
|
|
*
|
2018-06-01 18:19:39 +02:00
|
|
|
* \note Merging is always a single step (target indices wont be marked for merging).
|
2017-09-03 22:34:49 +10:00
|
|
|
*/
|
2019-04-17 06:17:24 +02:00
|
|
|
int BLI_kdtree_nd_(calc_duplicates_fast)(const KDTree *tree,
|
|
|
|
|
const float range,
|
|
|
|
|
bool use_index_order,
|
|
|
|
|
int *duplicates)
|
2017-09-03 22:34:49 +10:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
int found = 0;
|
|
|
|
|
struct DeDuplicateParams p = {
|
|
|
|
|
.nodes = tree->nodes,
|
|
|
|
|
.range = range,
|
|
|
|
|
.range_sq = SQUARE(range),
|
|
|
|
|
.duplicates = duplicates,
|
|
|
|
|
.duplicates_found = &found,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (use_index_order) {
|
|
|
|
|
uint *order = kdtree_order(tree);
|
|
|
|
|
for (uint i = 0; i < tree->nodes_len; i++) {
|
|
|
|
|
const uint node_index = order[i];
|
|
|
|
|
const int index = (int)i;
|
|
|
|
|
if (ELEM(duplicates[index], -1, index)) {
|
|
|
|
|
p.search = index;
|
|
|
|
|
copy_vn_vn(p.search_co, tree->nodes[node_index].co);
|
|
|
|
|
int found_prev = found;
|
|
|
|
|
deduplicate_recursive(&p, tree->root);
|
|
|
|
|
if (found != found_prev) {
|
|
|
|
|
/* Prevent chains of doubles. */
|
|
|
|
|
duplicates[index] = index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MEM_freeN(order);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (uint i = 0; i < tree->nodes_len; i++) {
|
|
|
|
|
const uint node_index = i;
|
|
|
|
|
const int index = p.nodes[node_index].index;
|
|
|
|
|
if (ELEM(duplicates[index], -1, index)) {
|
|
|
|
|
p.search = index;
|
|
|
|
|
copy_vn_vn(p.search_co, tree->nodes[node_index].co);
|
|
|
|
|
int found_prev = found;
|
|
|
|
|
deduplicate_recursive(&p, tree->root);
|
|
|
|
|
if (found != found_prev) {
|
|
|
|
|
/* Prevent chains of doubles. */
|
|
|
|
|
duplicates[index] = index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return found;
|
2017-09-03 22:34:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
2019-03-21 02:27:27 +11:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name BLI_kdtree_3d_deduplicate
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static int kdtree_node_cmp_deduplicate(const void *n0_p, const void *n1_p)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
const KDTreeNode *n0 = n0_p;
|
|
|
|
|
const KDTreeNode *n1 = n1_p;
|
|
|
|
|
for (uint j = 0; j < KD_DIMS; j++) {
|
|
|
|
|
if (n0->co[j] < n1->co[j]) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else if (n0->co[j] > n1->co[j]) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Sort by pointer so the first added will be used.
|
|
|
|
|
* assignment below ignores const correctness,
|
|
|
|
|
* however the values aren't used for sorting and are to be discarded. */
|
|
|
|
|
if (n0 < n1) {
|
|
|
|
|
((KDTreeNode *)n1)->d = KD_DIMS; /* tag invalid */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
((KDTreeNode *)n0)->d = KD_DIMS; /* tag invalid */
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-03-21 02:27:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove exact duplicates (run before before balancing).
|
|
|
|
|
*
|
|
|
|
|
* Keep the first element added when duplicates are found.
|
|
|
|
|
*/
|
|
|
|
|
int BLI_kdtree_nd_(deduplicate)(KDTree *tree)
|
|
|
|
|
{
|
|
|
|
|
#ifdef DEBUG
|
2019-04-17 06:17:24 +02:00
|
|
|
tree->is_balanced = false;
|
2019-03-21 02:27:27 +11:00
|
|
|
#endif
|
2019-04-17 06:17:24 +02:00
|
|
|
qsort(tree->nodes, (size_t)tree->nodes_len, sizeof(*tree->nodes), kdtree_node_cmp_deduplicate);
|
|
|
|
|
uint j = 0;
|
|
|
|
|
for (uint i = 0; i < tree->nodes_len; i++) {
|
|
|
|
|
if (tree->nodes[i].d != KD_DIMS) {
|
|
|
|
|
if (i != j) {
|
|
|
|
|
tree->nodes[j] = tree->nodes[i];
|
|
|
|
|
}
|
|
|
|
|
j++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tree->nodes_len = j;
|
|
|
|
|
return (int)tree->nodes_len;
|
2019-03-21 02:27:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|