🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

292 lines
9.2 KiB

  1. /*
  2. * SOFA utility methods for inspecting SOFA file metrics and determining HRTF
  3. * utility compatible layouts.
  4. *
  5. * Copyright (C) 2018-2019 Christopher Fitzgerald
  6. * Copyright (C) 2019 Christopher Robinson
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * Or visit: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. */
  24. #include "sofa-support.h"
  25. #include <stdio.h>
  26. #include <algorithm>
  27. #include <array>
  28. #include <cmath>
  29. #include <utility>
  30. #include <vector>
  31. #include "mysofa.h"
  32. namespace {
  33. using uint = unsigned int;
  34. using double3 = std::array<double,3>;
  35. /* Produces a sorted array of unique elements from a particular axis of the
  36. * triplets array. The filters are used to focus on particular coordinates
  37. * of other axes as necessary. The epsilons are used to constrain the
  38. * equality of unique elements.
  39. */
  40. std::vector<double> GetUniquelySortedElems(const std::vector<double3> &aers, const uint axis,
  41. const double *const (&filters)[3], const double (&epsilons)[3])
  42. {
  43. std::vector<double> elems;
  44. for(const double3 &aer : aers)
  45. {
  46. const double elem{aer[axis]};
  47. uint j;
  48. for(j = 0;j < 3;j++)
  49. {
  50. if(filters[j] && std::abs(aer[j] - *filters[j]) > epsilons[j])
  51. break;
  52. }
  53. if(j < 3)
  54. continue;
  55. auto iter = elems.begin();
  56. for(;iter != elems.end();++iter)
  57. {
  58. const double delta{elem - *iter};
  59. if(delta > epsilons[axis]) continue;
  60. if(delta >= -epsilons[axis]) break;
  61. iter = elems.emplace(iter, elem);
  62. break;
  63. }
  64. if(iter == elems.end())
  65. elems.emplace_back(elem);
  66. }
  67. return elems;
  68. }
  69. /* Given a list of azimuths, this will produce the smallest step size that can
  70. * uniformly cover the list. Ideally this will be over half, but in degenerate
  71. * cases this can fall to a minimum of 5 (the lower limit).
  72. */
  73. double GetUniformAzimStep(const double epsilon, const std::vector<double> &elems)
  74. {
  75. if(elems.size() < 5) return 0.0;
  76. /* Get the maximum count possible, given the first two elements. It would
  77. * be impossible to have more than this since the first element must be
  78. * included.
  79. */
  80. uint count{static_cast<uint>(std::ceil(360.0 / (elems[1]-elems[0])))};
  81. count = std::min(count, 255u);
  82. for(;count >= 5;--count)
  83. {
  84. /* Given the stepping value for this number of elements, check each
  85. * multiple to ensure there's a matching element.
  86. */
  87. const double step{360.0 / count};
  88. bool good{true};
  89. size_t idx{1u};
  90. for(uint mult{1u};mult < count && good;++mult)
  91. {
  92. const double target{step*mult + elems[0]};
  93. while(idx < elems.size() && target-elems[idx] > epsilon)
  94. ++idx;
  95. good &= (idx < elems.size()) && !(std::abs(target-elems[idx++]) > epsilon);
  96. }
  97. if(good)
  98. return step;
  99. }
  100. return 0.0;
  101. }
  102. /* Given a list of elevations, this will produce the smallest step size that
  103. * can uniformly cover the list. Ideally this will be over half, but in
  104. * degenerate cases this can fall to a minimum of 5 (the lower limit).
  105. */
  106. double GetUniformElevStep(const double epsilon, std::vector<double> &elems)
  107. {
  108. if(elems.size() < 5) return 0.0;
  109. /* Reverse the elevations so it increments starting with -90 (flipped from
  110. * +90). This makes it easier to work out a proper stepping value.
  111. */
  112. std::reverse(elems.begin(), elems.end());
  113. for(auto &v : elems) v *= -1.0;
  114. uint count{static_cast<uint>(std::ceil(180.0 / (elems[1]-elems[0])))};
  115. count = std::min(count, 255u);
  116. double ret{0.0};
  117. for(;count >= 5;--count)
  118. {
  119. const double step{180.0 / count};
  120. bool good{true};
  121. size_t idx{1u};
  122. /* Elevations don't need to match all multiples if there's not enough
  123. * elements to check. Missing elevations can be synthesized.
  124. */
  125. for(uint mult{1u};mult <= count && idx < elems.size() && good;++mult)
  126. {
  127. const double target{step*mult + elems[0]};
  128. while(idx < elems.size() && target-elems[idx] > epsilon)
  129. ++idx;
  130. good &= !(idx < elems.size()) || !(std::abs(target-elems[idx++]) > epsilon);
  131. }
  132. if(good)
  133. {
  134. ret = step;
  135. break;
  136. }
  137. }
  138. /* Re-reverse the elevations to restore the correct order. */
  139. for(auto &v : elems) v *= -1.0;
  140. std::reverse(elems.begin(), elems.end());
  141. return ret;
  142. }
  143. } // namespace
  144. const char *SofaErrorStr(int err)
  145. {
  146. switch(err)
  147. {
  148. case MYSOFA_OK: return "OK";
  149. case MYSOFA_INVALID_FORMAT: return "Invalid format";
  150. case MYSOFA_UNSUPPORTED_FORMAT: return "Unsupported format";
  151. case MYSOFA_INTERNAL_ERROR: return "Internal error";
  152. case MYSOFA_NO_MEMORY: return "Out of memory";
  153. case MYSOFA_READ_ERROR: return "Read error";
  154. }
  155. return "Unknown";
  156. }
  157. std::vector<SofaField> GetCompatibleLayout(const size_t m, const float *xyzs)
  158. {
  159. auto aers = std::vector<double3>(m, double3{});
  160. for(size_t i{0u};i < m;++i)
  161. {
  162. float vals[3]{xyzs[i*3], xyzs[i*3 + 1], xyzs[i*3 + 2]};
  163. mysofa_c2s(&vals[0]);
  164. aers[i] = {vals[0], vals[1], vals[2]};
  165. }
  166. auto radii = GetUniquelySortedElems(aers, 2, {}, {0.1, 0.1, 0.001});
  167. std::vector<SofaField> fds;
  168. fds.reserve(radii.size());
  169. for(const double dist : radii)
  170. {
  171. auto elevs = GetUniquelySortedElems(aers, 1, {nullptr, nullptr, &dist}, {0.1, 0.1, 0.001});
  172. /* Remove elevations that don't have a valid set of azimuths. */
  173. auto invalid_elev = [&dist,&aers](const double ev) -> bool
  174. {
  175. auto azims = GetUniquelySortedElems(aers, 0, {nullptr, &ev, &dist}, {0.1, 0.1, 0.001});
  176. if(std::abs(ev) > 89.999)
  177. return azims.size() != 1;
  178. if(azims.empty() || !(std::abs(azims[0]) < 0.1))
  179. return true;
  180. return GetUniformAzimStep(0.1, azims) <= 0.0;
  181. };
  182. elevs.erase(std::remove_if(elevs.begin(), elevs.end(), invalid_elev), elevs.end());
  183. double step{GetUniformElevStep(0.1, elevs)};
  184. if(step <= 0.0)
  185. {
  186. if(elevs.empty())
  187. fprintf(stdout, "No usable elevations on field distance %f.\n", dist);
  188. else
  189. {
  190. fprintf(stdout, "Non-uniform elevations on field distance %.3f.\nGot: %+.2f", dist,
  191. elevs[0]);
  192. for(size_t ei{1u};ei < elevs.size();++ei)
  193. fprintf(stdout, ", %+.2f", elevs[ei]);
  194. fputc('\n', stdout);
  195. }
  196. continue;
  197. }
  198. uint evStart{0u};
  199. for(uint ei{0u};ei < elevs.size();ei++)
  200. {
  201. if(!(elevs[ei] < 0.0))
  202. {
  203. fprintf(stdout, "Too many missing elevations on field distance %f.\n", dist);
  204. return fds;
  205. }
  206. double eif{(90.0+elevs[ei]) / step};
  207. const double ev_start{std::round(eif)};
  208. if(std::abs(eif - ev_start) < (0.1/step))
  209. {
  210. evStart = static_cast<uint>(ev_start);
  211. break;
  212. }
  213. }
  214. const auto evCount = static_cast<uint>(std::round(180.0 / step)) + 1;
  215. if(evCount < 5)
  216. {
  217. fprintf(stdout, "Too few uniform elevations on field distance %f.\n", dist);
  218. continue;
  219. }
  220. SofaField field{};
  221. field.mDistance = dist;
  222. field.mEvCount = evCount;
  223. field.mEvStart = evStart;
  224. field.mAzCounts.resize(evCount, 0u);
  225. auto &azCounts = field.mAzCounts;
  226. for(uint ei{evStart};ei < evCount;ei++)
  227. {
  228. double ev{-90.0 + ei*180.0/(evCount - 1)};
  229. auto azims = GetUniquelySortedElems(aers, 0, {nullptr, &ev, &dist}, {0.1, 0.1, 0.001});
  230. if(ei == 0 || ei == (evCount-1))
  231. {
  232. if(azims.size() != 1)
  233. {
  234. fprintf(stdout, "Non-singular poles on field distance %f.\n", dist);
  235. return fds;
  236. }
  237. azCounts[ei] = 1;
  238. }
  239. else
  240. {
  241. step = GetUniformAzimStep(0.1, azims);
  242. if(step <= 0.0)
  243. {
  244. fprintf(stdout, "Non-uniform azimuths on elevation %f, field distance %f.\n",
  245. ev, dist);
  246. return fds;
  247. }
  248. azCounts[ei] = static_cast<uint>(std::round(360.0f / step));
  249. }
  250. }
  251. fds.emplace_back(std::move(field));
  252. }
  253. return fds;
  254. }