All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metsci.glimpse.util.quadtree.QuadTreeObjects Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020, Metron, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Metron, Inc. nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL METRON, INC. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.metsci.glimpse.util.quadtree;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author hogye
 */
public abstract class QuadTreeObjects extends QuadTree>
{

    protected final int maxBucketSize;

    public QuadTreeObjects( int maxBucketSize )
    {
        super( new ArrayList( ) );
        this.maxBucketSize = maxBucketSize;
    }

    public abstract float x( V v );

    public abstract float y( V v );

    public Collection search( float xMin, float xMax, float yMin, float yMax )
    {
        Collection results = new ArrayList( );
        search( xMin, xMax, yMin, yMax, results );
        return results;
    }

    /**
     * @return The number of elements appended to {@code results}.
     */
    public int search( final float xMin, final float xMax, final float yMin, final float yMax, final Collection results )
    {
        int nBefore = results.size( );

        accumulate( xMin, xMax, yMin, yMax, new Accumulator>( )
        {
            public void accumulate( Collection bucket, float xMinBucket, float xMaxBucket, float yMinBucket, float yMaxBucket )
            {
                boolean xAll = ( xMin <= xMinBucket && xMaxBucket <= xMax );
                boolean yAll = ( yMin <= yMinBucket && yMaxBucket <= yMax );

                if ( xAll && yAll )
                {
                    results.addAll( bucket );
                }
                else if ( xAll )
                {
                    for ( V v : bucket )
                    {
                        float y = y( v );
                        if ( y < yMin || y > yMax ) continue;

                        results.add( v );
                    }
                }
                else if ( yAll )
                {
                    for ( V v : bucket )
                    {
                        float x = x( v );
                        if ( x < xMin || x > xMax ) continue;

                        results.add( v );
                    }
                }
                else
                {
                    for ( V v : bucket )
                    {
                        float x = x( v );
                        if ( x < xMin || x > xMax ) continue;

                        float y = y( v );
                        if ( y < yMin || y > yMax ) continue;

                        results.add( v );
                    }
                }
            }
        } );

        return results.size( ) - nBefore;
    }

    public Collection search( float xMin, float xMax, float yMin, float yMax, FilterObject vFilter )
    {
        Collection results = new ArrayList( );
        search( xMin, xMax, yMin, yMax, vFilter, results );
        return results;
    }

    /**
     * @return The number of elements appended to {@code results}.
     */
    public int search( final float xMin, final float xMax, final float yMin, final float yMax, final FilterObject vFilter, final Collection results )
    {
        int nBefore = results.size( );

        accumulate( xMin, xMax, yMin, yMax, new Accumulator>( )
        {
            public void accumulate( Collection bucket, float xMinBucket, float xMaxBucket, float yMinBucket, float yMaxBucket )
            {
                boolean xAll = ( xMin <= xMinBucket && xMaxBucket <= xMax );
                boolean yAll = ( yMin <= yMinBucket && yMaxBucket <= yMax );

                if ( xAll && yAll )
                {
                    for ( V v : bucket )
                    {
                        if ( !vFilter.include( v ) ) continue;

                        results.add( v );
                    }
                }
                else if ( xAll )
                {
                    for ( V v : bucket )
                    {
                        if ( !vFilter.include( v ) ) continue;

                        float y = y( v );
                        if ( y < yMin || y > yMax ) continue;

                        results.add( v );
                    }
                }
                else if ( yAll )
                {
                    for ( V v : bucket )
                    {
                        if ( !vFilter.include( v ) ) continue;

                        float x = x( v );
                        if ( x < xMin || x > xMax ) continue;

                        results.add( v );
                    }
                }
                else
                {
                    for ( V v : bucket )
                    {
                        if ( !vFilter.include( v ) ) continue;

                        float x = x( v );
                        if ( x < xMin || x > xMax ) continue;

                        float y = y( v );
                        if ( y < yMin || y > yMax ) continue;

                        results.add( v );
                    }
                }
            }
        } );

        return results.size( ) - nBefore;
    }

    /**
     * If {@code x(v)} or {@code y(v)} returns {@code NaN}, this method returns
     * immediately without adding {@code v} to the tree.
     */
    public void add( V v )
    {
        float x = x( v );
        if ( Float.isNaN( x ) ) return;

        float y = y( v );
        if ( Float.isNaN( y ) ) return;

        LeafNode> leaf = leaf( x, y );
        Collection bucket = leaf.bucket;

        bucket.add( v );

        if ( bucket.size( ) > maxBucketSize ) splitLeaf( leaf );
    }

    @Override
    protected void chooseDividers( float xMin, float xMax, float yMin, float yMax, Collection bucket, float[] result )
    {
        double oneOverSize = 1.0 / bucket.size( );
        double xMean = 0;
        double yMean = 0;
        for ( V v : bucket )
        {
            xMean += truncInf( x( v ) ) * oneOverSize;
            yMean += truncInf( y( v ) ) * oneOverSize;
        }
        result[0] = truncInf( ( float ) xMean );
        result[1] = truncInf( ( float ) yMean );
    }

    @Override
    protected Collection[] splitBucket( Collection bucket, float xDivider, float yDivider )
    {
        @SuppressWarnings( "unchecked" )
        Collection[] newBuckets = new Collection[4];
        for ( int q = 0; q < 4; q++ )
            newBuckets[q] = new ArrayList( );

        for ( V v : bucket )
        {
            int q = quadrant( xDivider, yDivider, x( v ), y( v ) );
            newBuckets[q].add( v );
        }

        return newBuckets;
    }

    @Override
    protected int bucketSize( Collection bucket )
    {
        return bucket.size( );
    }

    public void remove( V v )
    {
        float x = x( v );
        float y = y( v );
        leaf( x, y ).bucket.remove( v );
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy