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

com.googlecode.blaisemath.graphics.core.GraphicUtils Maven / Gradle / Ivy

/**
 * GraphicUtils.java
 * Created Jul 11, 2014
 */
package com.googlecode.blaisemath.graphics.core;

/*
 * #%L
 * BlaiseGraphics
 * --
 * Copyright (C) 2009 - 2015 Elisha Peterson
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import com.google.common.base.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Predicate;
import com.googlecode.blaisemath.style.StyleHints;
import com.googlecode.blaisemath.style.Styles;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RectangularShape;

/**
 * Utility class for working with {@link Graphic}s.
 * @author Elisha
 */
public class GraphicUtils {
    
    //
    
    /** Filter that can be applied to pass only visible graphics */
    private static final Predicate> VISIBLE_FILTER = new Predicate>(){
        @Override
        public boolean apply(Graphic input) { 
            return !StyleHints.isInvisible(input.getStyleHints());
        }
    };
    
    /** Filter that can be applied to pass only visible graphics */
    private static final Predicate> FUNCTIONAL_FILTER = new Predicate>(){
        @Override
        public boolean apply(Graphic input) { 
            return StyleHints.isFunctional(input.getStyleHints());
        }
    };
    
    //
    
    // utility class
    private GraphicUtils() {
    }
    
    //
    
    /**
     * Return visibility filter for graphics.
     * @return visible filter
     */
    public static Predicate> visibleFilter() {
        return VISIBLE_FILTER;
    }
    
    /**
     * Return functional filter for graphics.
     * @return functional filter
     */
    public static Predicate> functionalFilter() {
        return FUNCTIONAL_FILTER;
    }

    /**
     * Return true if graphic is currently invisible
     * @param gr the graphic
     * @return true if hidden
     */
    public static boolean isInvisible(Graphic gr) {
        return !visibleFilter().apply(gr);
    }

    /**
     * Return true if graphic is currently functional
     * @param gr the graphic
     * @return true if invisible
     */
    public static boolean isFunctional(Graphic gr) {
        return functionalFilter().apply(gr);
    }
    
    //
    
    //
    
    /**
     * Search for graphic with the given id in the composite, returning it.
     * The id is stored in the {@link Styles#ID} attribute of the graphic's style.
     * @param  graphics canvas type
     * @param gr regular or composite graphic to search in
     * @param id what to search for
     * @return the found graphic, or null if none is found
     */
    public  Graphic select(Graphic gr, String id) {
        checkNotNull(id);
        if (Objects.equal(id, gr.getStyle().getString(Styles.ID, null))) {
            return gr;
        } else if (gr instanceof GraphicComposite) {
            GraphicComposite gc = (GraphicComposite) gr;
            for (Graphic g : gc.getGraphics()) {
                Graphic r = select(g, id);
                if (r != null) {
                    return r;
                }
            }
        }
        return null;
    }
    
    //
    
    public static  Rectangle2D boundingBox(Iterable> entries) {
        Rectangle2D res = null;
        for (Graphic en : entries) {
            Rectangle2D enBox = en.boundingBox();
            if (enBox != null) {
                res = res == null ? enBox : res.createUnion(enBox);
            }
        }
        return res;
    }
    
    public static Rectangle2D boundingBoxRect(Iterable shapes) {
        Rectangle2D res = null;
        for (RectangularShape sh : shapes) {
            if (sh != null) {
                res = res == null ? sh.getBounds2D() : res.createUnion(sh.getBounds2D());
            }
        }
        return res;
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy