PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/KinectAPI/KinectAPI/KinectAPI.Gestures.NDollar/SamplesCollection.cs

#
C# | 230 lines | 147 code | 18 blank | 65 comment | 24 complexity | 8c474de4ba79bdb50b1d8c38d6d4c50d MD5 | raw file
  1. /**
  2. * The $N Multistroke Recognizer (C# version)
  3. *
  4. * Lisa Anthony, Ph.D.
  5. * UMBC
  6. * Information Systems Department
  7. * 1000 Hilltop Circle
  8. * Baltimore, MD 21250
  9. * lanthony@umbc.edu
  10. *
  11. * Jacob O. Wobbrock, Ph.D.
  12. * The Information School
  13. * University of Washington
  14. * Mary Gates Hall, Box 352840
  15. * Seattle, WA 98195-2840
  16. * wobbrock@u.washington.edu
  17. *
  18. * The Protractor enhancement was published by Yang Li and programmed
  19. * here by Lisa Anthony and Jacob O. Wobbrock.
  20. *
  21. * Li, Y. (2010). Protractor: A fast and accurate gesture
  22. * recognizer. Proceedings of the ACM Conference on Human
  23. * Factors in Computing Systems (CHI '10). Atlanta, Georgia
  24. * (April 10-15, 2010). New York: ACM Press, pp. 2169-2172.
  25. *
  26. * This software is distributed under the "New BSD License" agreement:
  27. *
  28. * Copyright (c) 2007-2011, Lisa Anthony and Jacob O. Wobbrock
  29. * All rights reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions are met:
  33. * * Redistributions of source code must retain the above copyright
  34. * notice, this list of conditions and the following disclaimer.
  35. * * Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in the
  37. * documentation and/or other materials provided with the distribution.
  38. * * Neither the name of the University of Washington nor UMBC,
  39. * nor the names of its contributors may be used to endorse or promote
  40. * products derived from this software without specific prior written
  41. * permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  44. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  45. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  46. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Jacob O. Wobbrock OR Lisa Anthony
  47. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  48. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  49. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  53. * SUCH DAMAGE.
  54. **/
  55. // This class stores all samples in a Dictionary (which is Hashtable-like)
  56. // data structure for easier processing of a huge corpus when all files
  57. // are read in at once for batch testing (so you don't have to manually
  58. // test each user independently). It also provides easy accessor methods
  59. // for common corpus questions like number of users, categories, etc.
  60. using System;
  61. using System.Collections;
  62. using System.Collections.Generic;
  63. using System.Text;
  64. using System.Diagnostics;
  65. namespace KinectAPI.Gestures.NDollar
  66. {
  67. public class SamplesCollection : Dictionary<string, Dictionary<string, Category>>
  68. {
  69. public SamplesCollection() : base()
  70. {
  71. // empty, no init
  72. }
  73. public ICollection Users
  74. {
  75. get
  76. {
  77. return this.Keys;
  78. }
  79. }
  80. public List<string> GetUsersList()
  81. {
  82. List<string> allUsers = new List<string>(this.Keys.Count);
  83. foreach (string u in this.Keys)
  84. {
  85. allUsers.Add(u);
  86. }
  87. return allUsers;
  88. }
  89. public bool ContainsUser(string user)
  90. {
  91. return this.ContainsKey(user);
  92. }
  93. public Category GetCategoryByUser(string user, string catName)
  94. {
  95. // returns null if user and/or catname are not valid
  96. if (!this.ContainsKey(user))
  97. {
  98. return null;
  99. }
  100. else if (!this[user].ContainsKey(catName))
  101. {
  102. return null;
  103. }
  104. else return this[user][catName];
  105. }
  106. public bool RemoveSamples(string user, string catName)
  107. {
  108. // returns false if user and/or catname are not valid
  109. if (!this.ContainsKey(user))
  110. {
  111. return false;
  112. }
  113. else if (!this[user].ContainsKey(catName))
  114. {
  115. return false;
  116. }
  117. else
  118. {
  119. this[user].Remove(catName);
  120. return true;
  121. }
  122. }
  123. public void AddExample(Multistroke p)
  124. {
  125. string catName = Category.ParseName(p.Name);
  126. if (this.ContainsKey(p.User))
  127. {
  128. // if this user is in the collection, and has some samples in this category already...
  129. if (this[p.User].ContainsKey(catName))
  130. {
  131. Dictionary<string, Category> forUser = this[p.User];
  132. Category cat = (Category)forUser[catName];
  133. cat.AddExample(p); // if the category has been made before, just add to it
  134. }
  135. else // create new category
  136. {
  137. Dictionary<string, Category> forUser = this[p.User];
  138. forUser.Add(catName, new Category(catName, p));
  139. }
  140. }
  141. else // create new user
  142. {
  143. Dictionary<string, Category> forUser = new Dictionary<string, Category>();
  144. forUser.Add(catName, new Category(catName, p));
  145. this.Add(p.User, forUser);
  146. }
  147. }
  148. // what is the minimum number of examples per category for this user?
  149. public int GetMinNumExamplesForUser(string user)
  150. {
  151. int minNumExamples = 9999;
  152. if ((this != null) && (this.ContainsKey(user)))
  153. {
  154. Dictionary<string, Category> allCats = this[user];
  155. foreach (KeyValuePair<string, Category> c in allCats)
  156. {
  157. if (c.Value.NumExamples < minNumExamples)
  158. minNumExamples = c.Value.NumExamples;
  159. }
  160. }
  161. return minNumExamples;
  162. }
  163. // does this user have the same number of samples per category across all categories?
  164. public bool AreNumExamplesEqualForUser(string user)
  165. {
  166. if ((this != null) && (this.ContainsKey(user)))
  167. {
  168. Dictionary<string, Category> allCats = this[user];
  169. int prevNumExamples = -1;
  170. foreach (KeyValuePair<string, Category> c in allCats)
  171. {
  172. if (prevNumExamples == -1)
  173. prevNumExamples = c.Value.NumExamples;
  174. if (c.Value.NumExamples != prevNumExamples)
  175. return false;
  176. }
  177. }
  178. return true;
  179. }
  180. public int MaxNumCategories() // across all users
  181. {
  182. return GetCategoriesList().Count;
  183. }
  184. public int NumCategoriesForUser(string user)
  185. {
  186. return this[user].Keys.Count;
  187. }
  188. public List<Category> GetCategoriesList()
  189. {
  190. List<Category> allCats = new List<Category>();
  191. foreach (KeyValuePair<string, Dictionary<string, Category>> u in this)
  192. {
  193. foreach (KeyValuePair<string, Category> d in u.Value)
  194. {
  195. if (!allCats.Contains(d.Value))
  196. allCats.Add(d.Value);
  197. }
  198. }
  199. return allCats;
  200. }
  201. public List<Category> GetCategories(string user)
  202. {
  203. List<Category> allCats = new List<Category>();
  204. foreach (KeyValuePair<string, Category> d in this[user])
  205. {
  206. if (!allCats.Contains(d.Value))
  207. allCats.Add(d.Value);
  208. }
  209. return allCats;
  210. }
  211. }
  212. }