curve_fit_nd.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2016, DWANGO Co., Ltd.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the <organization> nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef __CURVE_FIT_ND_H__
  28. #define __CURVE_FIT_ND_H__
  29. /** \file curve_fit_nd.h
  30. * \ingroup curve_fit
  31. */
  32. /* curve_fit_cubic.c */
  33. /**
  34. * Takes a flat array of points and evaluates that to calculate a bezier spline.
  35. *
  36. * \param points, points_len: The array of points to calculate a cubics from.
  37. * \param dims: The number of dimensions for for each element in \a points.
  38. * \param error_threshold: the error threshold to allow for,
  39. * the curve will be within this distance from \a points.
  40. * \param corners, corners_len: indices for points which will not have aligned tangents (optional).
  41. * This can use the output of #curve_fit_corners_detect_db which has been included
  42. * to evaluate a line to detect corner indices.
  43. *
  44. * \param r_cubic_array, r_cubic_array_len: Resulting array of tangents and knots, formatted as follows:
  45. * ``r_cubic_array[r_cubic_array_len][3][dims]``,
  46. * where each point has 0 and 2 for the tangents and the middle index 1 for the knot.
  47. * The size of the *flat* array will be ``r_cubic_array_len * 3 * dims``.
  48. * \param r_corner_index_array, r_corner_index_len: Corner indices in in \a r_cubic_array (optional).
  49. * This allows you to access corners on the resulting curve.
  50. *
  51. * \returns zero on success, nonzero is reserved for error values.
  52. */
  53. int curve_fit_cubic_to_points_db(
  54. const double *points,
  55. const unsigned int points_len,
  56. const unsigned int dims,
  57. const double error_threshold,
  58. const unsigned int calc_flag,
  59. const unsigned int *corners,
  60. unsigned int corners_len,
  61. double **r_cubic_array, unsigned int *r_cubic_array_len,
  62. unsigned int **r_cubic_orig_index,
  63. unsigned int **r_corner_index_array, unsigned int *r_corner_index_len);
  64. int curve_fit_cubic_to_points_fl(
  65. const float *points,
  66. const unsigned int points_len,
  67. const unsigned int dims,
  68. const float error_threshold,
  69. const unsigned int calc_flag,
  70. const unsigned int *corners,
  71. const unsigned int corners_len,
  72. float **r_cubic_array, unsigned int *r_cubic_array_len,
  73. unsigned int **r_cubic_orig_index,
  74. unsigned int **r_corners_index_array, unsigned int *r_corners_index_len);
  75. /**
  76. * Takes a flat array of points and evaluates that to calculate handle lengths.
  77. *
  78. * \param points, points_len: The array of points to calculate a cubics from.
  79. * \param dims: The number of dimensions for for each element in \a points.
  80. * \param points_length_cache: Optional pre-calculated lengths between points.
  81. * \param error_threshold: the error threshold to allow for,
  82. * \param tan_l, tan_r: Normalized tangents the handles will be aligned to.
  83. * Note that tangents must both point along the direction of the \a points,
  84. * so \a tan_l points in the same direction of the resulting handle,
  85. * where \a tan_r will point the opposite direction of its handle.
  86. *
  87. * \param r_handle_l, r_handle_r: Resulting calculated handles.
  88. * \param r_error_sq: The maximum distance (squared) this curve diverges from \a points.
  89. */
  90. int curve_fit_cubic_to_points_single_db(
  91. const double *points,
  92. const unsigned int points_len,
  93. const double *points_length_cache,
  94. const unsigned int dims,
  95. const double error_threshold,
  96. const double tan_l[],
  97. const double tan_r[],
  98. double r_handle_l[],
  99. double r_handle_r[],
  100. double *r_error_sq,
  101. unsigned int *r_error_index);
  102. int curve_fit_cubic_to_points_single_fl(
  103. const float *points,
  104. const unsigned int points_len,
  105. const float *points_length_cache,
  106. const unsigned int dims,
  107. const float error_threshold,
  108. const float tan_l[],
  109. const float tan_r[],
  110. float r_handle_l[],
  111. float r_handle_r[],
  112. float *r_error_sq,
  113. unsigned int *r_error_index);
  114. enum {
  115. CURVE_FIT_CALC_HIGH_QUALIY = (1 << 0),
  116. CURVE_FIT_CALC_CYCLIC = (1 << 1),
  117. };
  118. /* curve_fit_cubic_refit.c */
  119. int curve_fit_cubic_to_points_refit_db(
  120. const double *points,
  121. const unsigned int points_len,
  122. const unsigned int dims,
  123. const double error_threshold,
  124. const unsigned int calc_flag,
  125. const unsigned int *corners,
  126. const unsigned int corners_len,
  127. const double corner_angle,
  128. double **r_cubic_array, unsigned int *r_cubic_array_len,
  129. unsigned int **r_cubic_orig_index,
  130. unsigned int **r_corner_index_array, unsigned int *r_corner_index_len);
  131. int curve_fit_cubic_to_points_refit_fl(
  132. const float *points,
  133. const unsigned int points_len,
  134. const unsigned int dims,
  135. const float error_threshold,
  136. const unsigned int calc_flag,
  137. const unsigned int *corners,
  138. unsigned int corners_len,
  139. const float corner_angle,
  140. float **r_cubic_array, unsigned int *r_cubic_array_len,
  141. unsigned int **r_cubic_orig_index,
  142. unsigned int **r_corner_index_array, unsigned int *r_corner_index_len);
  143. /* curve_fit_corners_detect.c */
  144. /**
  145. * A helper function that takes a line and outputs its corner indices.
  146. *
  147. * \param points, points_len: Curve to evaluate.
  148. * \param dims: The number of dimensions for for each element in \a points.
  149. * \param radius_min: Corners on the curve between points below this radius are ignored.
  150. * \param radius_max: Corners on the curve above this radius are ignored.
  151. * \param samples_max: Prevent testing corners beyond this many points
  152. * (prevents a large radius taking excessive time to compute).
  153. * \param angle_threshold: Angles above this value are considered corners
  154. * (higher value for fewer corners).
  155. *
  156. * \param r_corners, r_corners_len: Resulting array of corners.
  157. *
  158. * \returns zero on success, nonzero is reserved for error values.
  159. */
  160. int curve_fit_corners_detect_db(
  161. const double *points,
  162. const unsigned int points_len,
  163. const unsigned int dims,
  164. const double radius_min,
  165. const double radius_max,
  166. const unsigned int samples_max,
  167. const double angle_threshold,
  168. unsigned int **r_corners,
  169. unsigned int *r_corners_len);
  170. int curve_fit_corners_detect_fl(
  171. const float *points,
  172. const unsigned int points_len,
  173. const unsigned int dims,
  174. const float radius_min,
  175. const float radius_max,
  176. const unsigned int samples_max,
  177. const float angle_threshold,
  178. unsigned int **r_corners,
  179. unsigned int *r_corners_len);
  180. #endif /* __CURVE_FIT_ND_H__ */