CurveExtractor.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <Mathematics/Logger.h>
  9. #include <array>
  10. #include <cstdint>
  11. #include <map>
  12. #include <type_traits>
  13. #include <vector>
  14. namespace WwiseGTE
  15. {
  16. // The image type T must be one of the integer types: int8_t, int16_t,
  17. // int32_t, uint8_t, uint16_t or uint32_t. Internal integer computations
  18. // are performed using int64_t. The type Real is for extraction to
  19. // floating-point vertices.
  20. template <typename T, typename Real>
  21. class CurveExtractor
  22. {
  23. public:
  24. // Abstract base class.
  25. virtual ~CurveExtractor() = default;
  26. // The level curves form a graph of vertices and edges. The vertices
  27. // are computed as pairs of nonnegative rational numbers. Vertex
  28. // represents the rational pair (xNumer/xDenom, yNumer/yDenom) as
  29. // (xNumer, xDenom, yNumer, yDenom), where all components are
  30. // nonnegative. The edges connect pairs of vertices, forming a graph
  31. // that represents the level set.
  32. struct Vertex
  33. {
  34. Vertex() = default;
  35. Vertex(int64_t inXNumer, int64_t inXDenom, int64_t inYNumer, int64_t inYDenom)
  36. {
  37. // The vertex generation leads to the numerator and
  38. // denominator having the same sign. This constructor changes
  39. // sign to ensure the numerator and denominator are both
  40. // positive.
  41. if (inXDenom > 0)
  42. {
  43. xNumer = inXNumer;
  44. xDenom = inXDenom;
  45. }
  46. else
  47. {
  48. xNumer = -inXNumer;
  49. xDenom = -inXDenom;
  50. }
  51. if (inYDenom > 0)
  52. {
  53. yNumer = inYNumer;
  54. yDenom = inYDenom;
  55. }
  56. else
  57. {
  58. yNumer = -inYNumer;
  59. yDenom = -inYDenom;
  60. }
  61. }
  62. // The non-default constructor guarantees that xDenom > 0 and
  63. // yDenom > 0. The following comparison operators assume that
  64. // the denominators are positive.
  65. bool operator==(Vertex const& other) const
  66. {
  67. return
  68. // xn0 / xd0 == xn1 / xd1
  69. xNumer * other.xDenom == other.xNumer * xDenom
  70. &&
  71. // yn0/yd0 == yn1/yd1
  72. yNumer * other.yDenom == other.yNumer * yDenom;
  73. }
  74. bool operator<(Vertex const& other) const
  75. {
  76. int64_t xn0txd1 = xNumer * other.xDenom;
  77. int64_t xn1txd0 = other.xNumer * xDenom;
  78. if (xn0txd1 < xn1txd0)
  79. {
  80. // xn0/xd0 < xn1/xd1
  81. return true;
  82. }
  83. if (xn0txd1 > xn1txd0)
  84. {
  85. // xn0/xd0 > xn1/xd1
  86. return false;
  87. }
  88. int64_t yn0tyd1 = yNumer * other.yDenom;
  89. int64_t yn1tyd0 = other.yNumer * yDenom;
  90. // yn0/yd0 < yn1/yd1
  91. return yn0tyd1 < yn1tyd0;
  92. }
  93. int64_t xNumer, xDenom, yNumer, yDenom;
  94. };
  95. struct Edge
  96. {
  97. Edge() = default;
  98. Edge(int v0, int v1)
  99. {
  100. if (v0 < v1)
  101. {
  102. v[0] = v0;
  103. v[1] = v1;
  104. }
  105. else
  106. {
  107. v[0] = v1;
  108. v[1] = v0;
  109. }
  110. }
  111. bool operator==(Edge const& other) const
  112. {
  113. return v[0] == other.v[0] && v[1] == other.v[1];
  114. }
  115. bool operator<(Edge const& other) const
  116. {
  117. for (int i = 0; i < 2; ++i)
  118. {
  119. if (v[i] < other.v[i])
  120. {
  121. return true;
  122. }
  123. if (v[i] > other.v[i])
  124. {
  125. return false;
  126. }
  127. }
  128. return false;
  129. }
  130. std::array<int, 2> v;
  131. };
  132. // Extract level curves and return rational vertices.
  133. virtual void Extract(T level, std::vector<Vertex>& vertices,
  134. std::vector<Edge>& edges) = 0;
  135. void Extract(T level, bool removeDuplicateVertices,
  136. std::vector<std::array<Real, 2>>& vertices, std::vector<Edge>& edges)
  137. {
  138. std::vector<Vertex> rationalVertices;
  139. Extract(level, rationalVertices, edges);
  140. if (removeDuplicateVertices)
  141. {
  142. MakeUnique(rationalVertices, edges);
  143. }
  144. Convert(rationalVertices, vertices);
  145. }
  146. // The extraction has duplicate vertices on edges shared by pixels.
  147. // This function will eliminate the duplicates.
  148. void MakeUnique(std::vector<Vertex>& vertices, std::vector<Edge>& edges)
  149. {
  150. size_t numVertices = vertices.size();
  151. size_t numEdges = edges.size();
  152. if (numVertices == 0 || numEdges == 0)
  153. {
  154. return;
  155. }
  156. // Compute the map of unique vertices and assign to them new and
  157. // unique indices.
  158. std::map<Vertex, int> vmap;
  159. int nextVertex = 0;
  160. for (size_t v = 0; v < numVertices; ++v)
  161. {
  162. // Keep only unique vertices.
  163. auto result = vmap.insert(std::make_pair(vertices[v], nextVertex));
  164. if (result.second)
  165. {
  166. ++nextVertex;
  167. }
  168. }
  169. // Compute the map of unique edges and assign to them new and
  170. // unique indices.
  171. std::map<Edge, int> emap;
  172. int nextEdge = 0;
  173. for (size_t e = 0; e < numEdges; ++e)
  174. {
  175. // Replace old vertex indices by new vertex indices.
  176. Edge& edge = edges[e];
  177. for (int i = 0; i < 2; ++i)
  178. {
  179. auto iter = vmap.find(vertices[edge.v[i]]);
  180. LogAssert(iter != vmap.end(), "Expecting the vertex to be in the vmap.");
  181. edge.v[i] = iter->second;
  182. }
  183. // Keep only unique edges.
  184. auto result = emap.insert(std::make_pair(edge, nextEdge));
  185. if (result.second)
  186. {
  187. ++nextEdge;
  188. }
  189. }
  190. // Pack the vertices into an array.
  191. vertices.resize(vmap.size());
  192. for (auto const& element : vmap)
  193. {
  194. vertices[element.second] = element.first;
  195. }
  196. // Pack the edges into an array.
  197. edges.resize(emap.size());
  198. for (auto const& element : emap)
  199. {
  200. edges[element.second] = element.first;
  201. }
  202. }
  203. // Convert from Vertex to std::array<Real, 2> rationals.
  204. void Convert(std::vector<Vertex> const& input, std::vector<std::array<Real, 2>>& output)
  205. {
  206. output.resize(input.size());
  207. for (size_t i = 0; i < input.size(); ++i)
  208. {
  209. Real rxNumer = static_cast<Real>(input[i].xNumer);
  210. Real rxDenom = static_cast<Real>(input[i].xDenom);
  211. Real ryNumer = static_cast<Real>(input[i].yNumer);
  212. Real ryDenom = static_cast<Real>(input[i].yDenom);
  213. output[i][0] = rxNumer / rxDenom;
  214. output[i][1] = ryNumer / ryDenom;
  215. }
  216. }
  217. protected:
  218. // The input is a 2D image with lexicographically ordered pixels (x,y)
  219. // stored in a linear array. Pixel (x,y) is stored in the array at
  220. // location index = x + xBound * y. The inputs xBound and yBound must
  221. // each be 2 or larger so that there is at least one image square to
  222. // process. The inputPixels must be nonnull and point to contiguous
  223. // storage that contains at least xBound * yBound elements.
  224. CurveExtractor(int xBound, int yBound, T const* inputPixels)
  225. :
  226. mXBound(xBound),
  227. mYBound(yBound),
  228. mInputPixels(inputPixels)
  229. {
  230. static_assert(std::is_integral<T>::value && sizeof(T) <= 4,
  231. "Type T must be int{8,16,32}_t or uint{8,16,32}_t.");
  232. LogAssert(mXBound > 1 && mYBound > 1 && mInputPixels != nullptr, "Invalid input.");
  233. mPixels.resize(static_cast<size_t>(mXBound * mYBound));
  234. }
  235. void AddVertex(std::vector<Vertex>& vertices,
  236. int64_t xNumer, int64_t xDenom, int64_t yNumer, int64_t yDenom)
  237. {
  238. vertices.push_back(Vertex(xNumer, xDenom, yNumer, yDenom));
  239. }
  240. void AddEdge(std::vector<Vertex>& vertices, std::vector<Edge>& edges,
  241. int64_t xNumer0, int64_t xDenom0, int64_t yNumer0, int64_t yDenom0,
  242. int64_t xNumer1, int64_t xDenom1, int64_t yNumer1, int64_t yDenom1)
  243. {
  244. int v0 = static_cast<int>(vertices.size());
  245. int v1 = v0 + 1;
  246. edges.push_back(Edge(v0, v1));
  247. vertices.push_back(Vertex(xNumer0, xDenom0, yNumer0, yDenom0));
  248. vertices.push_back(Vertex(xNumer1, xDenom1, yNumer1, yDenom1));
  249. }
  250. int mXBound, mYBound;
  251. T const* mInputPixels;
  252. std::vector<int64_t> mPixels;
  253. };
  254. }