Skip to content

Commit

Permalink
Add int quadtrees
Browse files Browse the repository at this point in the history
This adds quadtrees with int coordinates.

Affects #6
  • Loading branch information
io7m committed May 19, 2017
1 parent 0f77806 commit 9053177
Show file tree
Hide file tree
Showing 24 changed files with 3,384 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ default boolean intersectsVolume(
tmin = Math.max(tmin, Math.min(tz0, tz1));
tmax = Math.min(tmax, Math.max(tz0, tz1));

return ((tmax >= Math.max(0.0, tmin)) && (tmin < Double.POSITIVE_INFINITY));
final boolean tmax_ok = tmax >= Math.max(0.0, tmin);
final boolean tmin_ok = tmin < Double.POSITIVE_INFINITY;
return tmax_ok && tmin_ok;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright © 2017 <[email protected]> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jspatial.api.quadtrees;

import com.io7m.jregions.core.unparameterized.areas.AreaI;
import com.io7m.jspatial.api.JSpatialImmutableStyleType;
import org.immutables.value.Value;

/**
* The type of integer quadtree configurations.
*
* @since 3.0.0
*/

@JSpatialImmutableStyleType
@Value.Immutable
public interface QuadTreeConfigurationIType
{
/**
* @return The maximum bounding area of the tree
*/

@Value.Parameter
AreaI area();

/**
* @return The minimum width of quadrants (must be {@code >= 2})
*/

@Value.Parameter
@Value.Default
default int minimumQuadrantWidth()
{
return 2;
}

/**
* @return The minimum height of quadrants (must be {@code >= 2})
*/

@Value.Parameter
@Value.Default
default int minimumQuadrantHeight()
{
return 2;
}

/**
* @return {@code true} iff the implementation should attempt to trim empty
* leaf nodes when an item is removed
*/

@Value.Parameter
@Value.Default
default boolean trimOnRemove()
{
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright © 2017 <[email protected]> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jspatial.api.quadtrees;

import com.io7m.jregions.core.unparameterized.areas.AreaI;

import java.util.function.BiFunction;

/**
* The type of mutable quadtrees with {@code int} integer coordinates.
*
* @param <A> The precise type of quadtree members
*
* @since 3.0.0
*/

public interface QuadTreeIType<A> extends QuadTreeReadableIType<A>
{
/**
* <p>Insert the object {@code item} into the quadtree.</p>
*
* <p>The function returns {@code false} if the object could not be
* inserted for any reason (perhaps due to being too large).</p>
*
* <p>If the object is already in the tree, it is replaced. This can be
* used to update the bounds of an object within the tree.</p>
*
* @param item The object to insert
* @param bounds The object's bounds
*
* @return {@code true} if the object was inserted
*/

boolean insert(
final A item,
final AreaI bounds);

/**
* <p>Remove the object {@code item} from the quadtree.</p>
*
* <p>The function returns {@code false} if the object could not be
* removed for any reason (perhaps due to not being in the tree in the first
* place).</p>
*
* @param item The object to remove
*
* @return {@code true} if the object was removed
*/

boolean remove(
A item);

/**
* Remove all objects from the tree.
*/

void clear();

/**
* Trim all empty quadrants from the tree.
*/

void trim();

/**
* Apply {@code f} to each element of the tree.
*
* @param f A mapping function
* @param <B> The type of result elements
*
* @return A new tree
*/

@Override
<B> QuadTreeIType<B> map(BiFunction<A, AreaI, B> f);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright © 2017 <[email protected]> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jspatial.api.quadtrees;

import com.io7m.jregions.core.unparameterized.areas.AreaI;
import com.io7m.jregions.core.unparameterized.areas.AreaL;

import java.util.Map;

/**
* A quadrant as it appears within the context of a quadtree.
*
* @param <T> The precise type of objects
*
* @since 3.0.0
*/

public interface QuadTreeQuadrantIType<T>
{
/**
* @return A read-only view of the objects directly contained within this
* quadrant
*/

Map<T, AreaI> objects();

/**
* @return The area of the quadrant
*/

AreaI area();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright © 2017 <[email protected]> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jspatial.api.quadtrees;

import com.io7m.jspatial.api.TreeVisitResult;

/**
* The type of functions used to iterate over the quadrants of quadtrees.
*
* @param <A> The type of tree objects
* @param <C> The type of contextual values
*
* @since 3.0.0
*/

@FunctionalInterface
public interface QuadTreeQuadrantIterationIType<A, C>
{
/**
* Apply the function.
*
* @param context A context value
* @param quadrant The current quadrant
* @param depth The current quadrant depth
*
* @return A value indicating how or if the traversal should continue
*/

TreeVisitResult apply(
C context,
QuadTreeQuadrantIType<A> quadrant,
long depth);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright © 2017 <[email protected]> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jspatial.api.quadtrees;

import com.io7m.jregions.core.unparameterized.areas.AreaI;
import com.io7m.jregions.core.unparameterized.areas.AreaL;
import com.io7m.jspatial.api.JSpatialImmutableStyleType;
import org.immutables.value.Value;

/**
* The type of quadtree raycast results.
*
* @param <T> The precise type of objects
*
* @since 3.0.0
*/

@Value.Immutable
@JSpatialImmutableStyleType
public interface QuadTreeRaycastResultIType<T>
extends Comparable<QuadTreeRaycastResultIType<T>>
{
@Override
default int compareTo(final QuadTreeRaycastResultIType<T> o)
{
return Double.compare(this.distance(), o.distance());
}

/**
* @return The distance to the object
*/

@Value.Parameter(order = 0)
double distance();

/**
* @return The object area
*/

@Value.Parameter(order = 1)
AreaI area();

/**
* @return The object
*/

@Value.Parameter(order = 2)
T item();
}
Loading

0 comments on commit 9053177

Please sign in to comment.