Procrustes.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Procrustes.cpp
  2. *
  3. * Copyright (C) 1993-2018 David Weenink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 2001
  20. djmw 20020813 GPL header
  21. djmw 20040117 Corrected bug in classProcrustes_transform: scale (s) was not used.
  22. djmw 20050406 Renamed Procrustus Procrustes
  23. djmw 20071012 Added: o_CAN_WRITE_AS_ENCODING.h
  24. djmw 20110304 Thing_new
  25. */
  26. #include "Procrustes.h"
  27. #include "oo_DESTROY.h"
  28. #include "Procrustes_def.h"
  29. #include "oo_COPY.h"
  30. #include "Procrustes_def.h"
  31. #include "oo_EQUAL.h"
  32. #include "Procrustes_def.h"
  33. #include "oo_CAN_WRITE_AS_ENCODING.h"
  34. #include "Procrustes_def.h"
  35. #include "oo_WRITE_TEXT.h"
  36. #include "Procrustes_def.h"
  37. #include "oo_WRITE_BINARY.h"
  38. #include "Procrustes_def.h"
  39. #include "oo_READ_TEXT.h"
  40. #include "Procrustes_def.h"
  41. #include "oo_READ_BINARY.h"
  42. #include "Procrustes_def.h"
  43. #include "oo_DESCRIPTION.h"
  44. #include "Procrustes_def.h"
  45. Thing_implement (Procrustes, AffineTransform, 0);
  46. void structProcrustes :: v_transform (double **in, integer nrows, double **out) {
  47. for (integer i = 1; i <= nrows; i ++) {
  48. for (integer j = 1; j <= dimension; j ++) {
  49. longdouble tmp = 0.0;
  50. for (integer k = 1; k <= dimension; k ++) {
  51. tmp += in [i] [k] * r [k] [j];
  52. }
  53. out [i] [j] = s * tmp + t [j];
  54. }
  55. }
  56. }
  57. autoAffineTransform structProcrustes :: v_invert () {
  58. autoProcrustes thee = Data_copy (this);
  59. /*
  60. R is symmetric rotation matrix -->
  61. inverse is transpose!
  62. */
  63. thy s = s == 0.0 ? 1.0 : 1.0 / s;
  64. for (integer i = 1; i <= dimension; i ++) {
  65. for (integer j = i + 1; j <= dimension; j ++) {
  66. thy r [i] [j] = r [j] [i];
  67. thy r [j] [i] = r [i] [j];
  68. }
  69. thy t [i] = 0.0;
  70. for (integer j = 1; j <= thy dimension; j ++) {
  71. thy t [i] -= thy r [j] [i] * t [j];
  72. }
  73. thy t [i] *= thy s;
  74. }
  75. return thee.move(); // explicit move() seems to be needed because of the type difference
  76. }
  77. static void Procrustes_setDefaults (Procrustes me) {
  78. my s = 1.0;
  79. for (integer i = 1; i <= my dimension; i ++) {
  80. my t [i] = 0.0;
  81. my r [i] [i] = 1.0;
  82. for (integer j = i + 1; j <= my dimension; j ++) {
  83. my r [i] [j] = my r [j] [i] = 0.0;
  84. }
  85. }
  86. }
  87. autoProcrustes Procrustes_create (integer dimension) {
  88. try {
  89. autoProcrustes me = Thing_new (Procrustes);
  90. AffineTransform_init (me.get(), dimension);
  91. Procrustes_setDefaults (me.get());
  92. return me;
  93. } catch (MelderError) {
  94. Melder_throw (U"Procrustes not created.");
  95. }
  96. }
  97. /* End of file Procrustes.c */