001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 *
032 */
033package org.openimaj.vis.general;
034
035import org.openimaj.vis.general.AxisConfig.AxisRenderingConfig;
036
037/**
038 *      A general axis renderer that can be used for rendering an axis into a visualisation.
039 *
040 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
041 *      @param <Q> The pixel colour representation
042 *  @created 9 Jul 2013
043 */
044public abstract class AxisRenderer<Q>
045{
046        /**
047         *      Draw the major axis
048         *      @param config The axis configuration
049         */
050        public abstract void drawAxis( AxisConfig<Q> config );
051
052        /**
053         *      Draw the axis label
054         *      @param config The axis configuration
055         */
056        public abstract void drawAxisLabel( AxisConfig<Q> config );
057
058        /**
059         *      Draw a tick on the axis at the given location
060         *      @param location The location (in data units)
061         *      @param config The config
062         */
063        public abstract void drawMajorTick( double location, AxisConfig<Q> config );
064
065        /**
066         *      Draw a major grid line on the vis
067         *      @param location The location (in data units)
068         *      @param config The config
069         */
070        public abstract void drawMajorTickGridline( double location, AxisConfig<Q> config );
071
072        /**
073         *      Draw a minor tick on the axis
074         *      @param location The location (in data units)
075         *      @param config The configuration
076         */
077        public abstract void drawMinorTick( double location, AxisConfig<Q> config );
078
079        /**
080         *      Draw a minor grid on the vis
081         *      @param location The location (in data units)
082         *      @param config The axis configuration
083         */
084        public abstract void drawMinorTickGridline( double location, AxisConfig<Q> config );
085
086        /** The axis configuration */
087        protected AxisConfig<Q> config = new AxisConfig<Q>();
088
089        /**
090         *      Find the nearest lower major tick mark to the given value.
091         *      @param value The given value
092         *      @return The nearest lower major tick mark
093         */
094        public double nearestLowerMajorTick( final double value )
095        {
096                final double ts = this.config.getRenderingConfig().getMajorTickSpacing();
097                return Math.floor( value/ts ) * ts;
098        }
099
100        /**
101         *      Find the nearest higher major tick mark to the given value.
102         *      @param value The given value
103         *      @return The nearest higher major tick mark
104         */
105        public double nearestHigherMajorTick( final double value )
106        {
107                final double ts = this.config.getRenderingConfig().getMajorTickSpacing();
108                return Math.ceil( value/ts ) * ts;
109        }
110
111        /**
112         *      Perform any precalculations that will allow the axes to
113         *      be drawn faster, or to allow the data transformer to work.
114         */
115        public void precalc( )
116        {
117                // No implementation. Override if you need to.
118        }
119
120        /**
121         */
122        public void renderAxis()
123        {
124                final AxisRenderingConfig<Q> rc = this.config.getRenderingConfig();
125
126                double min = this.config.getMinValue();
127                double max = this.config.getMaxValue();
128
129                if( min > max ) { final double t = min; min = max; max = t; }
130
131                if( rc.isRenderAxis() )
132                {
133                        if( rc.isDrawMajorTicks() || rc.isDrawMinorTicks() )
134                        {
135                                if( rc.getMinorTickSpacing() > 0 )
136                                        for( double u = min; u <= max; u += rc.getMinorTickSpacing() )
137                                        {
138                                                if( rc.isDrawMinorGrid() )
139                                                        this.drawMinorTickGridline( u, this.config );
140                                                this.drawMinorTick( u, this.config );
141                                        }
142
143                                if( rc.getMajorTickSpacing() > 0 )
144                                        for( double u = min; u <= max; u += rc.getMajorTickSpacing() )
145                                        {
146                                                if( rc.isDrawMajorGrid() )
147                                                        this.drawMajorTickGridline( u, this.config );
148                                                this.drawMajorTick( u, this.config );
149                                        }
150                        }
151
152                        this.drawAxis( this.config );
153                        this.drawAxisLabel( this.config );
154                }
155        }
156
157        /**
158         *      @return the config
159         */
160        public AxisConfig<Q> getConfig()
161        {
162                return this.config;
163        }
164
165        /**
166         *      @param config the config to set
167         */
168        public void setConfig( final AxisConfig<Q> config )
169        {
170                this.config = config;
171        }
172}