View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.contour;
31  
32  import org.openimaj.image.FImage;
33  import org.openimaj.image.pixel.Pixel;
34  
35  /**
36   * A pixel direction
37   * 
38   * @author Sina Samangooei (ss@ecs.soton.ac.uk)
39   * 
40   */
41  enum Direction {
42  	NORTH, NORTH_EAST, EAST, SOUTH_EAST, SOUTH, SOUTH_WEST, WEST, NORTH_WEST;
43  	static int[] dirx = new int[] { 0, 1, 1, 1, 0, -1, -1, -1 };
44  	static int[] diry = new int[] { -1, -1, 0, 1, 1, 1, 0, -1 };
45  	static Direction[] entry = new Direction[] {
46  			WEST, WEST, NORTH, NORTH, EAST, EAST, SOUTH, SOUTH
47  	};
48  	static Direction[] ccentry = new Direction[] {
49  			EAST, SOUTH, SOUTH, WEST, WEST, NORTH, NORTH, EAST
50  	};
51  
52  	/**
53  	 * @return the direction clockwise from this direction
54  	 */
55  	public Direction clockwise() {
56  		final Direction[] vals = Direction.values();
57  		final Direction dir = vals[(ordinal() + 1) % vals.length];
58  
59  		return dir;
60  	}
61  
62  	/**
63  	 * @return the direction counterClockwise from this direction
64  	 */
65  	public Direction counterClockwise() {
66  		final Direction[] vals = Direction.values();
67  		final int desired = ordinal() - 1;
68  		final Direction dir = vals[desired == -1 ? desired + vals.length : desired];
69  		return dir;
70  	}
71  
72  	/**
73  	 * @param img
74  	 * @param point
75  	 * @return whether the pixel point to from this point on this img in this
76  	 *         direction is active
77  	 */
78  	public Pixel active(FImage img, Pixel point) {
79  		final int ord = ordinal();
80  		final int yy = point.y + diry[ord];
81  		final int xx = point.x + dirx[ord];
82  		if (xx < 0 || xx >= img.width || yy < 0 || yy >= img.height)
83  			return null;
84  		final float pix = img.pixels[yy][xx];
85  		return pix != 0 ? new Pixel(xx, yy) : null;
86  	}
87  
88  	/**
89  	 * @return the direction which this pixel was entered from if it were
90  	 *         entered clockwise
91  	 */
92  	public Direction clockwiseEntryDirection() {
93  		return entry[ordinal()];
94  	}
95  
96  	/**
97  	 * @return the direction which this pixel was entered from if it were
98  	 *         entered clockwise
99  	 */
100 	public Direction counterClockwiseEntryDirection() {
101 		return ccentry[ordinal()];
102 	}
103 
104 	/**
105 	 * @param from
106 	 * @param to
107 	 * @return the DIRECTION from and to adjacent pixels
108 	 */
109 	public static Direction fromTo(Pixel from, Pixel to) {
110 		if (from.equals(to))
111 			return null;
112 		if (from.y == to.y) {
113 			if (from.x < to.x)
114 				return EAST;
115 			else
116 				return WEST;
117 		}
118 		else if (from.y < to.y) {
119 			if (from.x == to.x)
120 				return SOUTH;
121 			else if (from.x < to.x)
122 				return SOUTH_EAST;
123 			else
124 				return SOUTH_WEST;
125 		}
126 		else {
127 			if (from.x == to.x)
128 				return NORTH;
129 			if (from.x < to.x)
130 				return NORTH_EAST;
131 			else
132 				return NORTH_WEST;
133 		}
134 	}
135 
136 	/**
137 	 * @param from
138 	 * @return the pixel in the direction from some point
139 	 */
140 	public Pixel pixel(Pixel from) {
141 		return new Pixel(from.x + dirx[ordinal()], from.y + diry[ordinal()]);
142 	}
143 }