View Javadoc

1   /*
2    * @(#)$Id: WiiAccelerations.java
3    *
4    * Author       :   Arthur Vogels, arthur.vogels@gmail.com
5    *
6    * Purpose      :	Temporarily stores acceleration data from the Wii Remote. 
7    * 					Used to convert to position data in WiiMoteTools.
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date             Who         	Reason
14   *
15   * Dec 02, 2008     arthurvogels    Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  package org.ximtec.igesture.util.additions3d;
27  
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.ArrayList;
31  import java.util.Vector;
32  import java.util.logging.Level;
33  import java.util.logging.Logger;
34  
35  public class Accelerations implements Cloneable{
36  	
37  	private static final Logger LOGGER = Logger.getLogger(Accelerations.class.getName());
38  	
39  	private List<AccelerationSample> samples;
40  
41  	public Accelerations() {
42  		samples = new ArrayList<AccelerationSample>();
43  	}
44  
45  	/**
46  	 * Returns the number of acceleration samples
47  	 * 
48  	 * @return The number of samples in this WiiAccelerations object
49  	 */
50  	public int numberOfSamples() {
51  		return samples.size();
52  	}
53  
54  	/**
55  	 * Adds a sample to the acceleration samples list
56  	 * 
57  	 * @param sample
58  	 *            The Sample to be added
59  	 */
60  	public void addSample(AccelerationSample sample) {
61  		samples.add(sample);
62  	}
63  
64  	/**
65  	 * Returns the sample at position number
66  	 * 
67  	 * @param number
68  	 *            The number of the sample to be returned
69  	 * @return The Sample with the given number
70  	 */
71  	public AccelerationSample getSample(int number) {
72  		return samples.get(number);
73  	}
74  
75  	/**
76  	 * Removes the sample at position number
77  	 * 
78  	 * @param number
79  	 *            The number of the Sample to be removed
80  	 */
81  	public void removeSample(int number) {
82  		samples.remove(number);
83  	}
84  
85  	/**
86  	 * Clears the accelerations list
87  	 */
88  	public void clear() {
89  		this.samples.clear();
90  	}
91  
92  	/**
93  	 * Returns the timestamp of the first sample in the list. If no samples are
94  	 * available 0 is returned.
95  	 * 
96  	 * @return The timestamp of the first Sample in this WiiAccelerations object
97  	 */
98  	public long getFirstSampleTime() {
99  		if (samples.size() > 0)
100 			return samples.get(0).getTimeStamp();
101 		else
102 			return 0;
103 	}
104 
105 	/**
106 	 * Returns the timestamp of the last sample in the list. If no samples are
107 	 * available 0 is returned.
108 	 * 
109 	 * @return The timestamp of the last Sample in this WiiAccelerations object
110 	 */
111 	public long getLastSampleTime() {
112 		if (samples.size() > 0)
113 			return samples.get(samples.size() - 1).getTimeStamp();
114 		else
115 			return 0;
116 	}
117 
118 	/**
119 	 * Returns the maximum x acceleration value
120 	 * 
121 	 * @return The maximum x acceleration value
122 	 */
123 	public double getMaxXValue() {
124 		double value = 0;
125 		boolean found = false;
126 		Iterator<AccelerationSample> i = samples.iterator();
127 		while (i.hasNext()) {
128 			AccelerationSample s = i.next();
129 			if (!found) {
130 				value = s.getXAcceleration();
131 				found = true;
132 			} else {
133 				if (s.getXAcceleration() > value)
134 					value = s.getXAcceleration();
135 			}
136 		}
137 		return value;
138 	}
139 
140 	/**
141 	 * Returns the maximum y acceleration value
142 	 * 
143 	 * @return The maximum y acceleration value
144 	 */
145 	public double getMaxYValue() {
146 		double value = 0;
147 		boolean found = false;
148 		Iterator<AccelerationSample> i = samples.iterator();
149 		while (i.hasNext()) {
150 			AccelerationSample s = i.next();
151 			if (!found) {
152 				value = s.getYAcceleration();
153 				found = true;
154 			} else {
155 				if (s.getYAcceleration() > value)
156 					value = s.getYAcceleration();
157 			}
158 		}
159 		return value;
160 	}
161 
162 	/**
163 	 * Returns the maximum z acceleration value
164 	 * 
165 	 * @return The maximum z acceleration value
166 	 */
167 	public double getMaxZValue() {
168 		double value = 0;
169 		boolean found = false;
170 		Iterator<AccelerationSample> i = samples.iterator();
171 		while (i.hasNext()) {
172 			AccelerationSample s = i.next();
173 			if (!found) {
174 				value = s.getZAcceleration();
175 				found = true;
176 			} else {
177 				if (s.getZAcceleration() > value)
178 					value = s.getZAcceleration();
179 			}
180 		}
181 		return value;
182 	}
183 
184 	/**
185 	 * Returns the minimum x acceleration value
186 	 * 
187 	 * @return The minimum x acceleration value
188 	 */
189 	public double getMinXValue() {
190 		double value = 0;
191 		boolean found = false;
192 		Iterator<AccelerationSample> i = samples.iterator();
193 		while (i.hasNext()) {
194 			AccelerationSample s = i.next();
195 			if (!found) {
196 				value = s.getXAcceleration();
197 				found = true;
198 			} else {
199 				if (s.getXAcceleration() < value)
200 					value = s.getXAcceleration();
201 			}
202 		}
203 		return value;
204 	}
205 
206 	/**
207 	 * Returns the minimum y acceleration value
208 	 * 
209 	 * @return The minimum y acceleration value
210 	 */
211 	public double getMinYValue() {
212 		double value = 0;
213 		boolean found = false;
214 		Iterator<AccelerationSample> i = samples.iterator();
215 		while (i.hasNext()) {
216 			AccelerationSample s = i.next();
217 			if (!found) {
218 				value = s.getYAcceleration();
219 				found = true;
220 			} else {
221 				if (s.getYAcceleration() < value)
222 					value = s.getYAcceleration();
223 			}
224 		}
225 		return value;
226 	}
227 
228 	/**
229 	 * Returns the minimum z acceleration value
230 	 * 
231 	 * @return The minimum z acceleration value
232 	 */
233 	public double getMinZValue() {
234 		double value = 0;
235 		boolean found = false;
236 		Iterator<AccelerationSample> i = samples.iterator();
237 		while (i.hasNext()) {
238 			AccelerationSample s = i.next();
239 			if (!found) {
240 				value = s.getZAcceleration();
241 				found = true;
242 			} else {
243 				if (s.getZAcceleration() < value)
244 					value = s.getZAcceleration();
245 			}
246 		}
247 		return value;
248 	}
249 
250 	/**
251 	 * Returns the maximum overall acceleration value
252 	 * 
253 	 * @return The maximum overall acceleration value
254 	 */
255 	public double getMaxOverallValue() {
256 		double value = getMaxXValue();
257 		if (getMaxYValue() > value)
258 			value = getMaxYValue();
259 		if (getMaxZValue() > value)
260 			value = getMaxZValue();
261 		return value;
262 	}
263 
264 	/**
265 	 * Returns the minimum overall acceleration value
266 	 * 
267 	 * @return The minimum overall acceleration value
268 	 */
269 	public Double getMinOverallValue() {
270 		double value = getMinXValue();
271 		if (getMinYValue() < value)
272 			value = getMinYValue();
273 		if (getMinZValue() < value)
274 			value = getMinZValue();
275 		return value;
276 	}
277 
278 	/**
279 	 * Returns the maximum absolute value for overall accelerations
280 	 * 
281 	 * @return The maximum absolute value for overall accelerations
282 	 */
283 	public double getMaxAbsoluteAccelerationValue() {
284 		double value = getMaxOverallValue();
285 		// System.err.println("MAX: " + getMaxOverallValue());
286 		// System.err.println("MIN: " + getMinOverallValue());
287 		if (Math.abs(getMinOverallValue()) > value) {
288 			value = Math.abs(getMinOverallValue());
289 			// System.err.println("MIN taken");
290 		}
291 		return value;
292 	}
293 
294 	/**
295 	 * Returns the samples list
296 	 * 
297 	 * @return The list of samples in this WiiAccelerations object
298 	 */
299 	public List<AccelerationSample> getSamples() {
300 		return samples;
301 	}
302 	
303 	/**
304 	 * Clone a WiiAccelerations
305 	 * @return the cloned WiiAccelerations
306 	 */
307    @Override
308    @SuppressWarnings("unchecked")
309    public Object clone() {
310       Accelerations clone = null;
311 
312       try {
313          clone = (Accelerations)super.clone();
314          List<AccelerationSample> clonedSamples = new Vector<AccelerationSample>();
315 
316          for (AccelerationSample sample : samples) {
317         	 clonedSamples.add((AccelerationSample)sample.clone());
318          }
319 
320          clone.samples = clonedSamples;
321       }
322       catch (CloneNotSupportedException e) {
323          LOGGER.log(Level.SEVERE, e.toString());
324       }
325 
326       return clone;
327    } // clone
328 }