gtx_pca.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #include <glm/glm.hpp>
  3. #include <glm/gtx/pca.hpp>
  4. #include <glm/gtc/epsilon.hpp>
  5. #include <glm/gtx/string_cast.hpp>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <random>
  9. #if GLM_COMPILER & GLM_COMPILER_CLANG
  10. # pragma clang diagnostic push
  11. # pragma clang diagnostic ignored "-Wsign-conversion"
  12. #endif
  13. template<typename T>
  14. T myEpsilon();
  15. template<>
  16. GLM_INLINE GLM_CONSTEXPR float myEpsilon<float>() { return 0.00001f; }
  17. template<>
  18. GLM_INLINE GLM_CONSTEXPR double myEpsilon<double>() { return 0.000001; }
  19. template<glm::length_t D, typename T, glm::qualifier Q>
  20. static bool vectorEpsilonEqual(glm::vec<D, T, Q> const& a, glm::vec<D, T, Q> const& b, T epsilon)
  21. {
  22. for (int c = 0; c < D; ++c)
  23. if (!glm::epsilonEqual(a[c], b[c], epsilon))
  24. {
  25. fprintf(stderr, "failing vectorEpsilonEqual: [%d] %lf != %lf (~%lf)\n",
  26. c,
  27. static_cast<double>(a[c]),
  28. static_cast<double>(b[c]),
  29. static_cast<double>(epsilon)
  30. );
  31. return false;
  32. }
  33. return true;
  34. }
  35. template<glm::length_t D, typename T, glm::qualifier Q>
  36. static bool matrixEpsilonEqual(glm::mat<D, D, T, Q> const& a, glm::mat<D, D, T, Q> const& b, T epsilon)
  37. {
  38. for (int c = 0; c < D; ++c)
  39. for (int r = 0; r < D; ++r)
  40. if (!glm::epsilonEqual(a[c][r], b[c][r], epsilon))
  41. {
  42. fprintf(stderr, "failing vectorEpsilonEqual: [%d][%d] %lf != %lf (~%lf)\n",
  43. c, r,
  44. static_cast<double>(a[c][r]),
  45. static_cast<double>(b[c][r]),
  46. static_cast<double>(epsilon)
  47. );
  48. return false;
  49. }
  50. return true;
  51. }
  52. template<typename T>
  53. GLM_INLINE bool sameSign(T const& a, T const& b)
  54. {
  55. return ((a >= 0) && (b >= 0)) || ((a < 0) && (b < 0));
  56. }
  57. template<typename T>
  58. static T failReport(T line)
  59. {
  60. fprintf(stderr, "Failed in line %d\n", static_cast<int>(line));
  61. return line;
  62. }
  63. // Test data: 1AGA 'agarose double helix'
  64. // https://www.rcsb.org/structure/1aga
  65. // The fourth coordinate is randomized
  66. namespace agarose
  67. {
  68. // Fills `outTestData` with hard-coded atom positions from 1AGA
  69. // The fourth coordinate is randomized
  70. template<typename vec>
  71. static void fillTestData(std::vector<vec>& outTestData)
  72. {
  73. // x,y,z coordinates copied from RCSB PDB file of 1AGA
  74. // w coordinate randomized with standard normal distribution
  75. static const double _1aga[] = {
  76. 3.219, -0.637, 19.462, 2.286,
  77. 4.519, 0.024, 18.980, -0.828,
  78. 4.163, 1.425, 18.481, -0.810,
  79. 3.190, 1.341, 17.330, -0.170,
  80. 1.962, 0.991, 18.165, 0.816,
  81. 2.093, 1.952, 19.331, 0.276,
  82. 5.119, -0.701, 17.908, -0.490,
  83. 3.517, 2.147, 19.514, -0.207,
  84. 2.970, 2.609, 16.719, 0.552,
  85. 2.107, -0.398, 18.564, 0.403,
  86. 2.847, 2.618, 15.335, 0.315,
  87. 1.457, 3.124, 14.979, 0.683,
  88. 1.316, 3.291, 13.473, 0.446,
  89. 2.447, 4.155, 12.931, 1.324,
  90. 3.795, 3.614, 13.394, 0.112,
  91. 4.956, 4.494, 12.982, 0.253,
  92. 0.483, 2.217, 15.479, 1.316,
  93. 0.021, 3.962, 13.166, 1.522,
  94. 2.311, 5.497, 13.395, 0.248,
  95. 3.830, 3.522, 14.827, 0.591,
  96. 5.150, 4.461, 11.576, 0.635,
  97. -1.057, 3.106, 13.132, 0.191,
  98. -2.280, 3.902, 12.650, 1.135,
  99. -3.316, 2.893, 12.151, 0.794,
  100. -2.756, 2.092, 11.000, 0.720,
  101. -1.839, 1.204, 11.835, -1.172,
  102. -2.737, 0.837, 13.001, -0.313,
  103. -1.952, 4.784, 11.578, 2.082,
  104. -3.617, 1.972, 13.184, 0.653,
  105. -3.744, 1.267, 10.389, -0.413,
  106. -0.709, 2.024, 12.234, -1.747,
  107. -3.690, 1.156, 9.005, -1.275,
  108. -3.434, -0.300, 8.649, 0.441,
  109. -3.508, -0.506, 7.143, 0.237,
  110. -4.822, 0.042, 6.601, -2.856,
  111. -5.027, 1.480, 7.064, 0.985,
  112. -6.370, 2.045, 6.652, 0.915,
  113. -2.162, -0.690, 9.149, 1.100,
  114. -3.442, -1.963, 6.836, -0.081,
  115. -5.916, -0.747, 7.065, -2.345,
  116. -4.965, 1.556, 8.497, 0.504,
  117. -6.439, 2.230, 5.246, 1.451,
  118. -2.161, -2.469, 6.802, -1.171,
  119. -2.239, -3.925, 6.320, -1.434,
  120. -0.847, -4.318, 5.821, 0.098,
  121. -0.434, -3.433, 4.670, -1.446,
  122. -0.123, -2.195, 5.505, 0.182,
  123. 0.644, -2.789, 6.671, 0.865,
  124. -3.167, -4.083, 5.248, -0.098,
  125. 0.101, -4.119, 6.854, -0.001,
  126. 0.775, -3.876, 4.059, 1.061,
  127. -1.398, -1.625, 5.904, 0.230,
  128. 0.844, -3.774, 2.675, 1.313,
  129. 1.977, -2.824, 2.319, -0.112,
  130. 2.192, -2.785, 0.813, -0.981,
  131. 2.375, -4.197, 0.271, -0.355,
  132. 1.232, -5.093, 0.734, 0.632,
  133. 1.414, -6.539, 0.322, 0.576,
  134. 1.678, -1.527, 2.819, -1.187,
  135. 3.421, -1.999, 0.496, -1.770,
  136. 3.605, -4.750, 0.735, 1.099,
  137. 1.135, -5.078, 2.167, 0.854,
  138. 1.289, -6.691, -1.084, -0.487,
  139. -1.057, 3.106, 22.602, -1.297,
  140. -2.280, 3.902, 22.120, 0.376,
  141. -3.316, 2.893, 21.621, 0.932,
  142. -2.756, 2.092, 20.470, 1.680,
  143. -1.839, 1.204, 21.305, 0.615,
  144. -2.737, 0.837, 22.471, 0.899,
  145. -1.952, 4.784, 21.048, -0.521,
  146. -3.617, 1.972, 22.654, 0.133,
  147. -3.744, 1.267, 19.859, 0.081,
  148. -0.709, 2.024, 21.704, 1.420,
  149. -3.690, 1.156, 18.475, -0.850,
  150. -3.434, -0.300, 18.119, -0.249,
  151. -3.508, -0.506, 16.613, 1.434,
  152. -4.822, 0.042, 16.071, -2.466,
  153. -5.027, 1.480, 16.534, -1.045,
  154. -6.370, 2.045, 16.122, 1.707,
  155. -2.162, -0.690, 18.619, -2.023,
  156. -3.442, -1.963, 16.336, -0.304,
  157. -5.916, -0.747, 16.535, 0.979,
  158. -4.965, 1.556, 17.967, -1.165,
  159. -6.439, 2.230, 14.716, 0.929,
  160. -2.161, -2.469, 16.302, -0.234,
  161. -2.239, -3.925, 15.820, -0.228,
  162. -0.847, -4.318, 15.321, 1.844,
  163. -0.434, -3.433, 14.170, 1.132,
  164. -0.123, -2.195, 15.005, 0.211,
  165. 0.644, -2.789, 16.171, -0.632,
  166. -3.167, -4.083, 14.748, -0.519,
  167. 0.101, -4.119, 16.354, 0.173,
  168. 0.775, -3.876, 13.559, 1.243,
  169. -1.398, -1.625, 15.404, -0.187,
  170. 0.844, -3.774, 12.175, -1.332,
  171. 1.977, -2.824, 11.819, -1.616,
  172. 2.192, -2.785, 10.313, 1.320,
  173. 2.375, -4.197, 9.771, 0.237,
  174. 1.232, -5.093, 10.234, 0.851,
  175. 1.414, -6.539, 9.822, 1.816,
  176. 1.678, -1.527, 12.319, -1.657,
  177. 3.421, -1.999, 10.036, 1.559,
  178. 3.605, -4.750, 10.235, 0.831,
  179. 1.135, -5.078, 11.667, 0.060,
  180. 1.289, -6.691, 8.416, 1.066,
  181. 3.219, -0.637, 10.002, 2.111,
  182. 4.519, 0.024, 9.520, -0.874,
  183. 4.163, 1.425, 9.021, -1.012,
  184. 3.190, 1.341, 7.870, -0.250,
  185. 1.962, 0.991, 8.705, -1.359,
  186. 2.093, 1.952, 9.871, -0.126,
  187. 5.119, -0.701, 8.448, 0.995,
  188. 3.517, 2.147, 10.054, 0.941,
  189. 2.970, 2.609, 7.259, -0.562,
  190. 2.107, -0.398, 9.104, -0.038,
  191. 2.847, 2.618, 5.875, 0.398,
  192. 1.457, 3.124, 5.519, 0.481,
  193. 1.316, 3.291, 4.013, -0.187,
  194. 2.447, 4.155, 3.471, -0.429,
  195. 3.795, 3.614, 3.934, -0.432,
  196. 4.956, 4.494, 3.522, -0.788,
  197. 0.483, 2.217, 6.019, -0.923,
  198. 0.021, 3.962, 3.636, -0.316,
  199. 2.311, 5.497, 3.935, -1.917,
  200. 3.830, 3.522, 5.367, -0.302,
  201. 5.150, 4.461, 2.116, -1.615
  202. };
  203. static const glm::length_t _1agaSize = sizeof(_1aga) / (4 * sizeof(double));
  204. outTestData.resize(_1agaSize);
  205. for(glm::length_t i = 0; i < _1agaSize; ++i)
  206. for(glm::length_t d = 0; d < static_cast<glm::length_t>(vec::length()); ++d)
  207. outTestData[i][d] = static_cast<typename vec::value_type>(_1aga[i * 4 + d]);
  208. }
  209. // All reference values computed separately using symbolic precision
  210. // https://github.com/sgrottel/exp-pca-precision
  211. // This applies to all functions named: `agarose::expected*()`
  212. GLM_INLINE glm::dmat4 const& expectedCovarData()
  213. {
  214. static const glm::dmat4 covar4x4d(
  215. 9.62434068027210898322, -0.00006657369614512471, -4.29321376568405099761, 0.01879374187452758846,
  216. -0.00006657369614512471, 9.62443937868480681175, 5.35113872637944076871, -0.11569259145880574080,
  217. -4.29321376568405099761, 5.35113872637944076871, 35.62848549634668415820, 0.90874239254220201545,
  218. 0.01879374187452758846, -0.11569259145880574080, 0.90874239254220201545, 1.09705971856890904803
  219. );
  220. return covar4x4d;
  221. }
  222. template<glm::length_t D>
  223. GLM_INLINE glm::vec<D, double, glm::defaultp> const& expectedEigenvalues();
  224. template<>
  225. GLM_INLINE glm::dvec2 const& expectedEigenvalues<2>()
  226. {
  227. static const glm::dvec2 evals2(
  228. 9.62447289926297399961763301774251330057894539467032275382255,
  229. 9.62430715969394210015560961264297422776572580714373620309355
  230. );
  231. return evals2;
  232. }
  233. template<>
  234. GLM_INLINE glm::dvec3 const& expectedEigenvalues<3>()
  235. {
  236. static const glm::dvec3 evals3(
  237. 37.3274494274683425233695502581182052836449738530676689472257,
  238. 9.62431434161498823505729817436585077939509766554969096873168,
  239. 7.92550178622027216422369326567668971675332732240052872097887
  240. );
  241. return evals3;
  242. }
  243. template<>
  244. GLM_INLINE glm::dvec4 const& expectedEigenvalues<4>()
  245. {
  246. static const glm::dvec4 evals4(
  247. 37.3477389918792213596879452204499702406947817221901007885630,
  248. 9.62470688921105696017807313860277172063600080413412567999700,
  249. 7.94017075281634999342344275928070533134615133171969063657713,
  250. 1.06170863996588365446060186982477896078741484440002343404155
  251. );
  252. return evals4;
  253. }
  254. template<glm::length_t D>
  255. GLM_INLINE glm::mat<D, D, double, glm::defaultp> const& expectedEigenvectors();
  256. template<>
  257. GLM_INLINE glm::dmat2 const& expectedEigenvectors<2>()
  258. {
  259. static const glm::dmat2 evecs2(
  260. glm::dvec2(
  261. -0.503510847492551904906870957742619139443409162857537237123308,
  262. 1
  263. ),
  264. glm::dvec2(
  265. 1.98605453086051402895741763848787613048533838388005162794043,
  266. 1
  267. )
  268. );
  269. return evecs2;
  270. }
  271. template<>
  272. GLM_INLINE glm::dmat3 const& expectedEigenvectors<3>()
  273. {
  274. static const glm::dmat3 evecs3(
  275. glm::dvec3(
  276. -0.154972738414395866005286433008304444294405085038689821864654,
  277. 0.193161285869815165989799191097521722568079378840201629578695,
  278. 1
  279. ),
  280. glm::dvec3(
  281. -158565.112775416943154745839952575022429933119522746586149868,
  282. -127221.506282351944358932458687410410814983610301927832439675,
  283. 1
  284. ),
  285. glm::dvec3(
  286. 2.52702248596556806145700361724323960543858113426446460406536,
  287. -3.14959802931313870497377546974185300816008580801457419079412,
  288. 1
  289. )
  290. );
  291. return evecs3;
  292. }
  293. template<>
  294. GLM_INLINE glm::dmat4 const& expectedEigenvectors<4>()
  295. {
  296. static const glm::dmat4 evecs4(
  297. glm::dvec4(
  298. -6.35322390281037045217295803597357821705371650876122113027264,
  299. 7.91546394153385394517767054617789939529794642646629201212056,
  300. 41.0301543819240679808549819457450130787045236815736490549663,
  301. 1
  302. ),
  303. glm::dvec4(
  304. -114.622418941087829756565311692197154422302604224781253861297,
  305. -92.2070185807065289900871215218752013659402949497379896153118,
  306. 0.0155846091025912430932734548933329458404665760587569100867246,
  307. 1
  308. ),
  309. glm::dvec4(
  310. 13.1771887761559019483954743159026938257325190511642952175789,
  311. -16.3688257459634877666638419310116970616615816436949741766895,
  312. 5.17386502341472097227408249233288958059579189051394773143190,
  313. 1
  314. ),
  315. glm::dvec4(
  316. -0.0192777078948229800494895064532553117703859768210647632969276,
  317. 0.0348034950916108873629241563077465542944938906271231198634442,
  318. -0.0340715609308469289267379681032545422644143611273049912226126,
  319. 1
  320. )
  321. );
  322. return evecs4;
  323. }
  324. } // namespace agarose
  325. // Compute center of gravity
  326. template<typename vec>
  327. static vec computeCenter(const std::vector<vec>& testData)
  328. {
  329. double c[4];
  330. std::fill(c, c + vec::length(), 0.0);
  331. typename std::vector<vec>::const_iterator e = testData.end();
  332. for(typename std::vector<vec>::const_iterator i = testData.begin(); i != e; ++i)
  333. for(glm::length_t d = 0; d < static_cast<glm::length_t>(vec::length()); ++d)
  334. c[d] += static_cast<double>((*i)[d]);
  335. vec cVec(0);
  336. for(glm::length_t d = 0; d < static_cast<glm::length_t>(vec::length()); ++d)
  337. cVec[d] = static_cast<typename vec::value_type>(c[d] / static_cast<double>(testData.size()));
  338. return cVec;
  339. }
  340. // Test sorting of Eigenvalue&Eigenvector lists. Use exhaustive search.
  341. template<glm::length_t D, typename T, glm::qualifier Q>
  342. static int testEigenvalueSort()
  343. {
  344. // Test input data: four arbitrary values
  345. static const glm::vec<D, T, Q> refVal(
  346. glm::vec<4, T, Q>(
  347. 10, 8, 6, 4
  348. )
  349. );
  350. // Test input data: four arbitrary vectors, which can be matched to the above values
  351. static const glm::mat<D, D, T, Q> refVec(
  352. glm::mat<4, 4, T, Q>(
  353. 10, 20, 5, 40,
  354. 8, 16, 4, 32,
  355. 6, 12, 3, 24,
  356. 4, 8, 2, 16
  357. )
  358. );
  359. // Permutations of test input data for exhaustive check, based on `D` (1 <= D <= 4)
  360. static const int permutationCount[] = {
  361. 0,
  362. 1,
  363. 2,
  364. 6,
  365. 24
  366. };
  367. // The permutations t perform, based on `D` (1 <= D <= 4)
  368. static const glm::ivec4 permutation[] = {
  369. glm::ivec4(0, 1, 2, 3),
  370. glm::ivec4(1, 0, 2, 3), // last for D = 2
  371. glm::ivec4(0, 2, 1, 3),
  372. glm::ivec4(1, 2, 0, 3),
  373. glm::ivec4(2, 0, 1, 3),
  374. glm::ivec4(2, 1, 0, 3), // last for D = 3
  375. glm::ivec4(0, 1, 3, 2),
  376. glm::ivec4(1, 0, 3, 2),
  377. glm::ivec4(0, 2, 3, 1),
  378. glm::ivec4(1, 2, 3, 0),
  379. glm::ivec4(2, 0, 3, 1),
  380. glm::ivec4(2, 1, 3, 0),
  381. glm::ivec4(0, 3, 1, 2),
  382. glm::ivec4(1, 3, 0, 2),
  383. glm::ivec4(0, 3, 2, 1),
  384. glm::ivec4(1, 3, 2, 0),
  385. glm::ivec4(2, 3, 0, 1),
  386. glm::ivec4(2, 3, 1, 0),
  387. glm::ivec4(3, 0, 1, 2),
  388. glm::ivec4(3, 1, 0, 2),
  389. glm::ivec4(3, 0, 2, 1),
  390. glm::ivec4(3, 1, 2, 0),
  391. glm::ivec4(3, 2, 0, 1),
  392. glm::ivec4(3, 2, 1, 0) // last for D = 4
  393. };
  394. // initial sanity check
  395. if(!vectorEpsilonEqual(refVal, refVal, myEpsilon<T>()))
  396. return failReport(__LINE__);
  397. if(!matrixEpsilonEqual(refVec, refVec, myEpsilon<T>()))
  398. return failReport(__LINE__);
  399. // Exhaustive search through all permutations
  400. for(int p = 0; p < permutationCount[D]; ++p)
  401. {
  402. glm::vec<D, T, Q> testVal;
  403. glm::mat<D, D, T, Q> testVec;
  404. for(int i = 0; i < D; ++i)
  405. {
  406. testVal[i] = refVal[permutation[p][i]];
  407. testVec[i] = refVec[permutation[p][i]];
  408. }
  409. glm::sortEigenvalues(testVal, testVec);
  410. if (!vectorEpsilonEqual(testVal, refVal, myEpsilon<T>()))
  411. return failReport(__LINE__);
  412. if (!matrixEpsilonEqual(testVec, refVec, myEpsilon<T>()))
  413. return failReport(__LINE__);
  414. }
  415. return 0;
  416. }
  417. // Test covariance matrix creation functions
  418. template<glm::length_t D, typename T, glm::qualifier Q>
  419. static int testCovar(
  420. glm::length_t dataSize, unsigned int randomEngineSeed
  421. )
  422. {
  423. typedef glm::vec<D, T, Q> vec;
  424. typedef glm::mat<D, D, T, Q> mat;
  425. // #1: test expected result with fixed data set
  426. std::vector<vec> testData;
  427. agarose::fillTestData(testData);
  428. // compute center of gravity
  429. vec center = computeCenter(testData);
  430. mat covarMat = glm::computeCovarianceMatrix(testData.data(), testData.size(), center);
  431. if(!matrixEpsilonEqual(covarMat, mat(agarose::expectedCovarData()), myEpsilon<T>()))
  432. {
  433. fprintf(stderr, "Reconstructed covarMat:\n%s\n", glm::to_string(covarMat).c_str());
  434. return failReport(__LINE__);
  435. }
  436. // #2: test function variant consistency with random data
  437. std::default_random_engine rndEng(randomEngineSeed);
  438. std::normal_distribution<T> normalDist;
  439. testData.resize(dataSize);
  440. // some common offset of all data
  441. T offset[D];
  442. for(glm::length_t d = 0; d < D; ++d)
  443. offset[d] = normalDist(rndEng);
  444. // init data
  445. for(glm::length_t i = 0; i < dataSize; ++i)
  446. for(glm::length_t d = 0; d < D; ++d)
  447. testData[i][d] = offset[d] + normalDist(rndEng);
  448. center = computeCenter(testData);
  449. std::vector<vec> centeredTestData;
  450. centeredTestData.reserve(testData.size());
  451. typename std::vector<vec>::const_iterator e = testData.end();
  452. for(typename std::vector<vec>::const_iterator i = testData.begin(); i != e; ++i)
  453. centeredTestData.push_back((*i) - center);
  454. mat c1 = glm::computeCovarianceMatrix(centeredTestData.data(), centeredTestData.size());
  455. mat c2 = glm::computeCovarianceMatrix<D, T, Q>(centeredTestData.begin(), centeredTestData.end());
  456. mat c3 = glm::computeCovarianceMatrix(testData.data(), testData.size(), center);
  457. mat c4 = glm::computeCovarianceMatrix<D, T, Q>(testData.rbegin(), testData.rend(), center);
  458. if(!matrixEpsilonEqual(c1, c2, myEpsilon<T>()))
  459. return failReport(__LINE__);
  460. if(!matrixEpsilonEqual(c1, c3, myEpsilon<T>()))
  461. return failReport(__LINE__);
  462. if(!matrixEpsilonEqual(c1, c4, myEpsilon<T>()))
  463. return failReport(__LINE__);
  464. return 0;
  465. }
  466. // Computes eigenvalues and eigenvectors from well-known covariance matrix
  467. template<glm::length_t D, typename T, glm::qualifier Q>
  468. static int testEigenvectors(T epsilon)
  469. {
  470. typedef glm::vec<D, T, Q> vec;
  471. typedef glm::mat<D, D, T, Q> mat;
  472. // test expected result with fixed data set
  473. std::vector<vec> testData;
  474. mat covarMat(agarose::expectedCovarData());
  475. vec eigenvalues;
  476. mat eigenvectors;
  477. unsigned int c = glm::findEigenvaluesSymReal(covarMat, eigenvalues, eigenvectors);
  478. if(c != D)
  479. return failReport(__LINE__);
  480. glm::sortEigenvalues(eigenvalues, eigenvectors);
  481. if (!vectorEpsilonEqual(eigenvalues, vec(agarose::expectedEigenvalues<D>()), epsilon))
  482. return failReport(__LINE__);
  483. for (int i = 0; i < D; ++i)
  484. {
  485. vec act = glm::normalize(eigenvectors[i]);
  486. vec exp = glm::normalize(agarose::expectedEigenvectors<D>()[i]);
  487. if (!sameSign(act[0], exp[0])) exp = -exp;
  488. if (!vectorEpsilonEqual(act, exp, epsilon))
  489. return failReport(__LINE__);
  490. }
  491. return 0;
  492. }
  493. // A simple small smoke test:
  494. // - a uniformly sampled block
  495. // - reconstruct main axes
  496. // - check order of eigenvalues equals order of extends of block in direction of main axes
  497. static int smokeTest()
  498. {
  499. using glm::vec3;
  500. using glm::mat3;
  501. std::vector<vec3> pts;
  502. pts.reserve(11 * 15 * 7);
  503. for(int x = -5; x <= 5; ++x)
  504. for(int y = -7; y <= 7; ++y)
  505. for(int z = -3; z <= 3; ++z)
  506. pts.push_back(vec3(x, y, z));
  507. mat3 covar = glm::computeCovarianceMatrix(pts.data(), pts.size());
  508. mat3 eVec;
  509. vec3 eVal;
  510. unsigned int eCnt = glm::findEigenvaluesSymReal(covar, eVal, eVec);
  511. if(eCnt != 3u)
  512. return failReport(__LINE__);
  513. // sort eVec by descending eVal
  514. if(eVal[0] < eVal[1])
  515. {
  516. std::swap(eVal[0], eVal[1]);
  517. std::swap(eVec[0], eVec[1]);
  518. }
  519. if(eVal[0] < eVal[2])
  520. {
  521. std::swap(eVal[0], eVal[2]);
  522. std::swap(eVec[0], eVec[2]);
  523. }
  524. if(eVal[1] < eVal[2])
  525. {
  526. std::swap(eVal[1], eVal[2]);
  527. std::swap(eVec[1], eVec[2]);
  528. }
  529. if(!vectorEpsilonEqual(glm::abs(eVec[0]), vec3(0, 1, 0), myEpsilon<float>()))
  530. return failReport(__LINE__);
  531. if(!vectorEpsilonEqual(glm::abs(eVec[1]), vec3(1, 0, 0), myEpsilon<float>()))
  532. return failReport(__LINE__);
  533. if(!vectorEpsilonEqual(glm::abs(eVec[2]), vec3(0, 0, 1), myEpsilon<float>()))
  534. return failReport(__LINE__);
  535. return 0;
  536. }
  537. static int rndTest(unsigned int randomEngineSeed)
  538. {
  539. std::default_random_engine rndEng(randomEngineSeed);
  540. std::normal_distribution<double> normalDist;
  541. // construct orthonormal system
  542. glm::dvec3 x(normalDist(rndEng), normalDist(rndEng), normalDist(rndEng));
  543. double l = glm::length(x);
  544. while(l < myEpsilon<double>())
  545. x = glm::dvec3(normalDist(rndEng), normalDist(rndEng), normalDist(rndEng));
  546. x = glm::normalize(x);
  547. glm::dvec3 y(normalDist(rndEng), normalDist(rndEng), normalDist(rndEng));
  548. l = glm::length(y);
  549. while(l < myEpsilon<double>())
  550. y = glm::dvec3(normalDist(rndEng), normalDist(rndEng), normalDist(rndEng));
  551. while(glm::abs(glm::dot(x, y)) < myEpsilon<double>())
  552. {
  553. y = glm::dvec3(normalDist(rndEng), normalDist(rndEng), normalDist(rndEng));
  554. while(l < myEpsilon<double>())
  555. y = glm::dvec3(normalDist(rndEng), normalDist(rndEng), normalDist(rndEng));
  556. }
  557. y = glm::normalize(y);
  558. glm::dvec3 z = glm::normalize(glm::cross(x, y));
  559. y = glm::normalize(glm::cross(z, x));
  560. // generate input point data
  561. std::vector<glm::dvec3> ptData;
  562. static const int pattern[] = {
  563. 8, 0, 0,
  564. 4, 1, 2,
  565. 0, 2, 0,
  566. 0, 0, 4
  567. };
  568. glm::dvec3 offset(normalDist(rndEng), normalDist(rndEng), normalDist(rndEng));
  569. for(int p = 0; p < 4; ++p)
  570. for(int xs = 1; xs >= -1; xs -= 2)
  571. for(int ys = 1; ys >= -1; ys -= 2)
  572. for(int zs = 1; zs >= -1; zs -= 2)
  573. ptData.push_back(
  574. offset
  575. + x * static_cast<double>(pattern[p * 3 + 0] * xs)
  576. + y * static_cast<double>(pattern[p * 3 + 1] * ys)
  577. + z * static_cast<double>(pattern[p * 3 + 2] * zs));
  578. // perform PCA:
  579. glm::dvec3 center = computeCenter(ptData);
  580. glm::dmat3 covarMat = glm::computeCovarianceMatrix(ptData.data(), ptData.size(), center);
  581. glm::dvec3 evals;
  582. glm::dmat3 evecs;
  583. unsigned int evcnt = glm::findEigenvaluesSymReal(covarMat, evals, evecs);
  584. if(evcnt != 3u)
  585. return failReport(__LINE__);
  586. glm::sortEigenvalues(evals, evecs);
  587. if (!sameSign(evecs[0][0], x[0])) evecs[0] = -evecs[0];
  588. if(!vectorEpsilonEqual(x, evecs[0], myEpsilon<double>()))
  589. return failReport(__LINE__);
  590. if (!sameSign(evecs[2][0], y[0])) evecs[2] = -evecs[2];
  591. if (!vectorEpsilonEqual(y, evecs[2], myEpsilon<double>()))
  592. return failReport(__LINE__);
  593. if (!sameSign(evecs[1][0], z[0])) evecs[1] = -evecs[1];
  594. if (!vectorEpsilonEqual(z, evecs[1], myEpsilon<double>()))
  595. return failReport(__LINE__);
  596. return 0;
  597. }
  598. int main()
  599. {
  600. int error(0);
  601. // A small smoke test to fail early with most problems
  602. if(smokeTest())
  603. return failReport(__LINE__);
  604. // test sorting utility.
  605. if(testEigenvalueSort<2, float, glm::defaultp>() != 0)
  606. error = failReport(__LINE__);
  607. if(testEigenvalueSort<2, double, glm::defaultp>() != 0)
  608. error = failReport(__LINE__);
  609. if(testEigenvalueSort<3, float, glm::defaultp>() != 0)
  610. error = failReport(__LINE__);
  611. if(testEigenvalueSort<3, double, glm::defaultp>() != 0)
  612. error = failReport(__LINE__);
  613. if(testEigenvalueSort<4, float, glm::defaultp>() != 0)
  614. error = failReport(__LINE__);
  615. if(testEigenvalueSort<4, double, glm::defaultp>() != 0)
  616. error = failReport(__LINE__);
  617. if (error != 0)
  618. return error;
  619. // Note: the random engine uses a fixed seed to create consistent and reproducible test data
  620. // test covariance matrix computation from different data sources
  621. if(testCovar<2, float, glm::defaultp>(100, 12345) != 0)
  622. error = failReport(__LINE__);
  623. if(testCovar<2, double, glm::defaultp>(100, 42) != 0)
  624. error = failReport(__LINE__);
  625. if(testCovar<3, float, glm::defaultp>(100, 2021) != 0)
  626. error = failReport(__LINE__);
  627. if(testCovar<3, double, glm::defaultp>(100, 815) != 0)
  628. error = failReport(__LINE__);
  629. if(testCovar<4, float, glm::defaultp>(100, 3141) != 0)
  630. error = failReport(__LINE__);
  631. if(testCovar<4, double, glm::defaultp>(100, 174) != 0)
  632. error = failReport(__LINE__);
  633. if (error != 0)
  634. return error;
  635. // test PCA eigen vector reconstruction
  636. // Expected epsilon precision evaluated separately:
  637. // https://github.com/sgrottel/exp-pca-precision
  638. if(testEigenvectors<2, float, glm::defaultp>(0.002f) != 0)
  639. error = failReport(__LINE__);
  640. if(testEigenvectors<2, double, glm::defaultp>(0.00000000001) != 0)
  641. error = failReport(__LINE__);
  642. if(testEigenvectors<3, float, glm::defaultp>(0.00001f) != 0)
  643. error = failReport(__LINE__);
  644. if(testEigenvectors<3, double, glm::defaultp>(0.0000000001) != 0)
  645. error = failReport(__LINE__);
  646. if(testEigenvectors<4, float, glm::defaultp>(0.0001f) != 0)
  647. error = failReport(__LINE__);
  648. if(testEigenvectors<4, double, glm::defaultp>(0.0000001) != 0)
  649. error = failReport(__LINE__);
  650. if(error != 0)
  651. return error;
  652. // Final tests with randomized data
  653. if(rndTest(12345) != 0)
  654. error = failReport(__LINE__);
  655. if(rndTest(42) != 0)
  656. error = failReport(__LINE__);
  657. if (error != 0)
  658. return error;
  659. return error;
  660. }
  661. #if GLM_COMPILER & GLM_COMPILER_CLANG
  662. # pragma clang diagnostic pop
  663. #endif