001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/graphics/optimizers/LabelChoice.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004    This file is part of deegree.
005    Copyright (C) 2001-2006 by:
006    EXSE, Department of Geography, University of Bonn
007    http://www.giub.uni-bonn.de/deegree/   
008    lat/lon GmbH
009    http://www.lat-lon.de
010    
011    This library is free software; you can redistribute it and/or
012    modify it under the terms of the GNU Lesser General Public
013    License as published by the Free Software Foundation; either
014    version 2.1 of the License, or (at your option) any later version.
015    
016    This library is distributed in the hope that it will be useful,
017    but WITHOUT ANY WARRANTY; without even the implied warranty of
018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019    Lesser General Public License for more details.
020    
021    You should have received a copy of the GNU Lesser General Public
022    License along with this library; if not, write to the Free Software
023    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025    Contact:
026    
027    Andreas Poth
028    lat/lon GmbH
029    Aennchenstr. 19
030    53115 Bonn
031    Germany
032    E-Mail: poth@lat-lon.de
033    
034    Prof. Dr. Klaus Greve
035    Department of Geography
036    University of Bonn
037    Meckenheimer Allee 166
038    53115 Bonn
039    Germany
040    E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.graphics.optimizers;
044    
045    import org.deegree.graphics.displayelements.Label;
046    import org.deegree.graphics.displayelements.LabelDisplayElement;
047    
048    /**
049     * @version $Revision: 6696 $
050     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
051     * @author last edited by: $Author: apoth $
052     * 
053     * @version 1.0. $Revision: 6696 $, $Date: 2007-04-25 21:57:05 +0200 (Mi, 25 Apr 2007) $
054     * 
055     * @since 2.0
056     */
057    
058    public class LabelChoice {
059    
060        /**
061         * LabelDisplayElement that this LabelChoice belongs to
062         */
063        private LabelDisplayElement element;
064    
065    
066            /**
067         * index of the currently selected Label
068             */ 
069            private int selected;
070    
071        /**
072         *candidates of Labels 
073         */         
074        private Label[] candidates;
075    
076    
077            // quality of each Label
078            private float [] qualities;
079    
080            // boundingbox of all contained labels
081            private int maxX;
082            // boundingbox of all contained labels
083            private int maxY;
084            // boundingbox of all contained labels
085            private int minX;
086            // boundingbox of all contained labels
087            private int minY;
088            
089            /**
090             * @param element
091             * @param candidates
092             * @param qualities
093             * @param selected
094             * @param maxX 
095             * @param maxY
096             * @param minX
097             * @param minY
098             */
099            public LabelChoice (LabelDisplayElement element, Label [] candidates, 
100                                                    float [] qualities, int selected, 
101                                                    int maxX, int maxY, int minX, int minY) {
102                    this.element = element;
103                    this.candidates = candidates;
104                    this.qualities = qualities;
105                    this.selected = selected;
106                    this.maxX = maxX;
107                    this.maxY = maxY;
108                    this.minX = minX;
109                    this.minY = minY;
110            }
111            
112    //      public void paint (Graphics2D g) {
113    //              for (int i = 0; i < candidates.length; i++) {
114    //                      ((Label) candidates [i]).paintBoundaries(g);
115    //              }
116    //      }
117    
118            /**
119             * 
120             */
121            public void selectLabelRandomly () {
122                    selected = (int) (Math.random () * (candidates.length - 1) + 0.5);
123            }
124    
125        /**
126         * @param selected
127         * 
128         */
129        public void setSelected(int selected) {
130            this.selected = selected;
131        }
132    
133        /**
134         * @return
135         * 
136         */
137        public int getSelected() {
138            return selected;
139        }
140    
141    
142            /**
143             * @return
144             */
145            public float getQuality () {
146                    return qualities [selected];
147            }
148    
149            /**
150             * @return
151             */
152            public Label getSelectedLabel () {
153                    return candidates [selected];
154            }
155    
156        /**
157         * @return
158         * 
159         */
160        public LabelDisplayElement getElement() {
161            return element;
162        }
163    
164        /**
165         * @return
166         * 
167         */
168        public int getMaxX() {
169            return maxX;
170        }
171    
172        /**
173         * @return
174         * 
175         */
176        public int getMaxY() {
177            return maxY;
178        }
179    
180        /**
181         * @return
182         * 
183         */
184        public int getMinX() {
185            return minX;
186        }
187    
188        /**
189         * @return
190         * 
191         */
192        public int getMinY() {
193            return minY;
194        }
195    
196            /**
197             * Determines if the <tt>LabelChoice<tt> can intersect another
198             * <tt>LabelChoice</tt> by any chance, i.e. there are two
199             * <tt>Labels</tt> from each choice that intersect.   
200             * <p>
201             * @param that LabelChoice to test
202             * @return true if the LabelChoices can intersect
203             */    
204            public boolean intersects (LabelChoice that) {
205    
206                    int west1  = getMinX ();
207                    int south1 = getMinY ();
208                    int east1  = getMaxX ();
209                    int north1 = getMaxY ();
210          
211                    int west2  = that.getMinX();
212                    int south2 = that.getMinY();
213                    int east2  = that.getMaxX();
214                    int north2 = that.getMaxY();
215    
216                    // special cases: one box lays completly inside the other one
217                    if ( ( west1 <= west2 ) && ( south1 <= south2 ) && ( east1 >= east2 ) && 
218                             ( north1 >= north2 ) ) {
219                            return true;
220                    }
221                    if ( ( west1 >= west2 ) && ( south1 >= south2 ) && ( east1 <= east2 ) && 
222                                     ( north1 <= north2 ) ) {
223                            return true;
224                    }
225                    // in any other case of intersection, at least one line of the BBOX has
226                    // to cross a line of the other BBOX
227                    // check western boundary of box 1
228                    // "touching" boxes must not intersect
229                    if ( ( west1 >= west2 ) && ( west1 < east2 ) ) {
230                            if ( ( south1 <= south2 ) && ( north1 > south2 ) ) {
231                                    return true;
232                            }
233    
234                            if ( ( south1 < north2 ) && ( north1 >= north2 ) ) {
235                                    return true;
236                            }
237                    }
238                    // check eastern boundary of box 1
239                    // "touching" boxes must not intersect
240                    if ( ( east1 > west2 ) && ( east1 <= east2 ) ) {
241                            if ( ( south1 <= south2 ) && ( north1 > south2 ) ) {
242                                    return true;
243                            }
244    
245                            if ( ( south1 < north2 ) && ( north1 >= north2 ) ) {
246                                    return true;
247                            }
248                    }
249                    // check southern boundary of box 1
250                    // "touching" boxes must not intersect
251                    if ( ( south1 >= south2 ) && ( south1 < north2 ) ) {
252                            if ( ( west1 <= west2 ) && ( east1 > west2 ) ) {
253                                    return true;
254                            }
255    
256                            if ( ( west1 < east2 ) && ( east1 >= east2 ) ) {
257                                    return true;
258                            }
259                    }
260                    // check northern boundary of box 1
261                    // "touching" boxes must not intersect
262                    if ( ( north1 > south2 ) && ( north1 <= north2 ) ) {
263                            if ( ( west1 <= west2 ) && ( east1 > west2 ) ) {
264                                    return true;
265                            }
266    
267                            if ( ( west1 < east2 ) && ( east1 >= east2 ) ) {
268                                    return true;
269                            }
270                    }
271                    return false;
272            }
273            
274            /**
275             * @see java.lang.Object#toString()
276             */
277            public String toString () {
278                    if (candidates.length > 0) {
279                            return candidates [0].toString ();
280                    } 
281                return "empty";
282            }
283    }