/src/fhw/fhw_primitive_recognizer.c

http://ftk.googlecode.com/ · C · 352 lines · 253 code · 67 blank · 32 comment · 52 complexity · bf5686fe03b04294ed6d65debd9fd794 MD5 · raw file

  1. /*
  2. * File: ftk_input_method.h
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: FTK handwrite primtives
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2010-02-13 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_allocator.h"
  31. #include "fhw_primitive_recognizer.h"
  32. #define FHW_MAX_PRIMITIVES 255
  33. struct _FhwPrimitiveRecognizer
  34. {
  35. int nr;
  36. FtkRect rect;
  37. char results[FHW_MAX_PRIMITIVES + 1];
  38. };
  39. FhwPrimitiveRecognizer* fhw_primitive_recognizer_create(FtkRect* rect)
  40. {
  41. FhwPrimitiveRecognizer* thiz = FTK_ZALLOC(sizeof(FhwPrimitiveRecognizer));
  42. if(thiz != NULL)
  43. {
  44. thiz->rect = *rect;
  45. }
  46. return thiz;
  47. }
  48. Ret fhw_primitive_recognizer_reset(FhwPrimitiveRecognizer* thiz)
  49. {
  50. return_val_if_fail(thiz != NULL, RET_FAIL);
  51. thiz->nr = 0;
  52. thiz->results[0] = '\0';
  53. return RET_OK;
  54. }
  55. Ret fhw_primitive_recognizer_flush(FhwPrimitiveRecognizer* thiz)
  56. {
  57. return_val_if_fail(thiz != NULL, RET_FAIL);
  58. /*XXX: do nothing now.*/
  59. return RET_OK;
  60. }
  61. typedef struct _FhwPointsInfo
  62. {
  63. /*some pre-computed values.*/
  64. int x;
  65. int y;
  66. int w;
  67. int h;
  68. int min_x;
  69. int min_y;
  70. int max_x;
  71. int max_y;
  72. int delta_x;
  73. int delta_y;
  74. int dot_range;
  75. int nr;
  76. FtkPoint* points;
  77. }FhwPointsInfo;
  78. #define FHW_MAX_MISS 3
  79. #define FHW_TOLERANCE 5
  80. static FhwPrimitiveType fhw_primitive_recognizer_dian(FhwPointsInfo* info)
  81. {
  82. if(info->delta_x > info->dot_range || info->delta_y > info->dot_range)
  83. {
  84. return FHW_PRIMITIVE_NONE;
  85. }
  86. if(info->delta_y > 3 * info->delta_x || info->delta_x > 3 * info->delta_y)
  87. {
  88. return FHW_PRIMITIVE_NONE;
  89. }
  90. return FHW_PRIMITIVE_DIAN;
  91. }
  92. static FhwPrimitiveType fhw_primitive_recognizer_heng(FhwPointsInfo* info)
  93. {
  94. int i = 0;
  95. int miss = 0;
  96. if(4*(info->delta_y) > (info->delta_x))
  97. {
  98. return FHW_PRIMITIVE_NONE;
  99. }
  100. for(i = 1; i < info->nr ; i++)
  101. {
  102. if(miss >= FHW_MAX_MISS)
  103. {
  104. return FHW_PRIMITIVE_NONE;
  105. }
  106. if((info->points[i].x + FHW_TOLERANCE) < info->points[i-1].x)
  107. {
  108. return FHW_PRIMITIVE_NONE;
  109. }
  110. miss = (info->points[i].x < info->points[i-1].x) ? miss + 1 : 0;
  111. }
  112. return FHW_PRIMITIVE_HENG;
  113. }
  114. static FhwPrimitiveType fhw_primitive_recognizer_shu(FhwPointsInfo* info)
  115. {
  116. int i = 0;
  117. int miss = 0;
  118. if(4 * info->delta_x > info->delta_y)
  119. {
  120. return FHW_PRIMITIVE_NONE;
  121. }
  122. for(i = 1; i < info->nr ; i++)
  123. {
  124. if(miss >= FHW_MAX_MISS)
  125. {
  126. return FHW_PRIMITIVE_NONE;
  127. }
  128. if((info->points[i].y + FHW_TOLERANCE) < info->points[i-1].y)
  129. {
  130. return FHW_PRIMITIVE_NONE;
  131. }
  132. miss = info->points[i].y < info->points[i-1].y ? miss + 1 : 0;
  133. }
  134. return FHW_PRIMITIVE_SHU;
  135. }
  136. static FhwPrimitiveType fhw_primitive_recognizer_pie(FhwPointsInfo* info)
  137. {
  138. int i = 0;
  139. int miss = 0;
  140. if(info->delta_x < info->dot_range || info->delta_y < info->dot_range)
  141. {
  142. return FHW_PRIMITIVE_NONE;
  143. }
  144. for(i = 1; i < info->nr ; i++)
  145. {
  146. if(miss >= FHW_MAX_MISS)
  147. {
  148. return FHW_PRIMITIVE_NONE;
  149. }
  150. if((info->points[i].y + FHW_TOLERANCE < info->points[i-1].y)
  151. || (info->points[i].x - FHW_TOLERANCE > info->points[i-1].x))
  152. {
  153. return FHW_PRIMITIVE_NONE;
  154. }
  155. miss = ((info->points[i].y < info->points[i-1].y) || (info->points[i].x > info->points[i-1].x)) ?
  156. miss + 1 : 0;
  157. }
  158. return FHW_PRIMITIVE_PIE;
  159. }
  160. static FhwPrimitiveType fhw_primitive_recognizer_na(FhwPointsInfo* info)
  161. {
  162. int i = 0;
  163. int miss = 0;
  164. if(info->delta_x < info->dot_range || info->delta_y < info->dot_range)
  165. {
  166. return FHW_PRIMITIVE_NONE;
  167. }
  168. for(i = 1; i < info->nr ; i++)
  169. {
  170. if(miss >= FHW_MAX_MISS)
  171. {
  172. return FHW_PRIMITIVE_NONE;
  173. }
  174. if((info->points[i].y + FHW_TOLERANCE < info->points[i-1].y)
  175. || (info->points[i].x + FHW_TOLERANCE < info->points[i-1].x))
  176. {
  177. return FHW_PRIMITIVE_NONE;
  178. }
  179. miss = ((info->points[i].y < info->points[i-1].y) || (info->points[i].x < info->points[i-1].x)) ?
  180. miss + 1 : 0;
  181. }
  182. return FHW_PRIMITIVE_NA;
  183. }
  184. static FhwPrimitiveType fhw_primitive_recognizer_heng_zhe(FhwPointsInfo* info)
  185. {
  186. int i = 0;
  187. int miss = 0;
  188. if(2*(info->delta_y) < (info->delta_x) || 2*(info->delta_x) < (info->delta_y))
  189. {
  190. return FHW_PRIMITIVE_NONE;
  191. }
  192. /*check heng*/
  193. for(i = 1; i < info->nr ; i++)
  194. {
  195. if(miss >= FHW_MAX_MISS)
  196. {
  197. return FHW_PRIMITIVE_NONE;
  198. }
  199. if((info->points[i].x + FHW_TOLERANCE) < info->points[i-1].x)
  200. {
  201. return FHW_PRIMITIVE_NONE;
  202. }
  203. miss = (info->points[i].x < info->points[i-1].x) ? miss + 1 : 0;
  204. if((info->points[i].y - info->min_y) > info->dot_range)
  205. {
  206. break;
  207. }
  208. }
  209. for(; i < info->nr ; i++)
  210. {
  211. if(miss >= FHW_MAX_MISS)
  212. {
  213. return FHW_PRIMITIVE_NONE;
  214. }
  215. if((info->points[i].y + FHW_TOLERANCE) < info->points[i-1].y)
  216. {
  217. return FHW_PRIMITIVE_NONE;
  218. }
  219. miss = info->points[i].y < info->points[i-1].y ? miss + 1 : 0;
  220. }
  221. return FHW_PRIMITIVE_HENG_ZHE;
  222. }
  223. typedef FhwPrimitiveType (*FhwPrimitiveRecognizerFunc)(FhwPointsInfo* info);
  224. static const FhwPrimitiveRecognizerFunc g_recognizers[] =
  225. {
  226. fhw_primitive_recognizer_dian,
  227. fhw_primitive_recognizer_heng,
  228. fhw_primitive_recognizer_heng_zhe,
  229. fhw_primitive_recognizer_shu,
  230. fhw_primitive_recognizer_pie,
  231. fhw_primitive_recognizer_na,
  232. NULL
  233. };
  234. Ret fhw_primitive_recognizer_add_stroke(FhwPrimitiveRecognizer* thiz, FtkPoint* points, size_t nr)
  235. {
  236. int i = 0;
  237. FhwPointsInfo info = {0};
  238. FhwPrimitiveType result = FHW_PRIMITIVE_NONE;
  239. return_val_if_fail(thiz != NULL && points != NULL, RET_FAIL);
  240. return_val_if_fail(thiz->nr < FHW_MAX_PRIMITIVES, RET_FAIL);
  241. info.nr = nr;
  242. info.points = points;
  243. info.x = thiz->rect.x;
  244. info.y = thiz->rect.y;
  245. info.w = thiz->rect.width;
  246. info.h = thiz->rect.height;
  247. info.min_x = points[0].x;
  248. info.min_y = points[0].y;
  249. info.max_x = points[0].x;
  250. info.max_y = points[0].y;
  251. info.dot_range = (info.w + info.h) >> 3;
  252. for(i = 1; i < nr; i++)
  253. {
  254. info.min_x = FTK_MIN(info.min_x, points[i].x);
  255. info.min_y = FTK_MIN(info.min_y, points[i].y);
  256. info.max_x = FTK_MAX(info.max_x, points[i].x);
  257. info.max_y = FTK_MAX(info.max_y, points[i].y);
  258. }
  259. info.delta_x = info.max_x - info.min_x;
  260. info.delta_y = info.max_y - info.min_y;
  261. for(i = 0; g_recognizers[i] != NULL; i++)
  262. {
  263. if((result = g_recognizers[i](&info)) != FHW_PRIMITIVE_NONE)
  264. {
  265. thiz->results[thiz->nr++] = result;
  266. thiz->results[thiz->nr] = '\0';
  267. break;
  268. }
  269. }
  270. return RET_OK;
  271. }
  272. Ret fhw_primitive_recognizer_get_result(FhwPrimitiveRecognizer* thiz, const char** results)
  273. {
  274. return_val_if_fail(thiz != NULL && results != NULL, RET_FAIL);
  275. *results = thiz->results;
  276. return RET_OK;
  277. }
  278. void fhw_primitive_recognizer_destroy(FhwPrimitiveRecognizer* thiz)
  279. {
  280. if(thiz != NULL)
  281. {
  282. FTK_ZFREE(thiz, sizeof(FhwPrimitiveRecognizer));
  283. }
  284. return;
  285. }