/// <summary>
/// Inserts a point in the Space-Partitioning tree.
/// </summary>
///
public bool Add(double[] point)
{
// Ignore objects which do not belong in this quad tree
if (!boundary.Contains(point))
{
return(false);
}
// On-line update of cumulative size and center-of-mass
cum_size++;
double mult1 = (double)(cum_size - 1) / (double)cum_size;
double mult2 = 1.0 / (double)cum_size;
for (int d = 0; d < center_of_mass.Length; d++)
{
center_of_mass[d] *= mult1;
center_of_mass[d] += mult2 * point[d];
}
// If there is space in this quad tree and it is a leaf, add the object here
if (IsLeaf && this.point == null)
{
this.point = point;
return(true);
}
// Don't add duplicates for now (this is not very nice)
if (this.point.IsEqual(point, rtol: 1e-10))
{
return(true);
}
// Otherwise, we need to subdivide the current cell
if (IsLeaf)
{
subdivide();
}
// Find out where the point can be inserted
for (int i = 0; i < Children.Length; i++)
{
if (Children[i].Add(point))
{
return(true);
}
}
// Otherwise, the point cannot be inserted (this should never happen)
return(false);
}