Artword.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Artword.cpp
  2. *
  3. * Copyright (C) 1992-2009,2011,2015-2018 Paul Boersma
  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.
  13. * See the GNU 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. #include "Artword.h"
  19. #include "oo_DESTROY.h"
  20. #include "Artword_def.h"
  21. #include "oo_COPY.h"
  22. #include "Artword_def.h"
  23. #include "oo_CAN_WRITE_AS_ENCODING.h"
  24. #include "Artword_def.h"
  25. #include "oo_EQUAL.h"
  26. #include "Artword_def.h"
  27. #include "oo_WRITE_TEXT.h"
  28. #include "Artword_def.h"
  29. #include "oo_READ_TEXT.h"
  30. #include "Artword_def.h"
  31. #include "oo_WRITE_BINARY.h"
  32. #include "Artword_def.h"
  33. #include "oo_READ_BINARY.h"
  34. #include "Artword_def.h"
  35. #include "oo_DESCRIPTION.h"
  36. #include "Artword_def.h"
  37. Thing_implement (Artword, Daata, 0);
  38. autoArtword Artword_create (double totalTime) {
  39. autoArtword me = Thing_new (Artword);
  40. my totalTime = totalTime;
  41. for (kArt_muscle muscle = (kArt_muscle) 1; muscle <= kArt_muscle::MAX; ++ muscle)
  42. Artword_setDefault (me.get(), muscle);
  43. return me;
  44. }
  45. void Artword_setDefault (Artword me, kArt_muscle muscle) {
  46. ArtwordData f = & my data [(int) muscle];
  47. f -> times = VECzero (2);
  48. f -> targets = VECzero (2);
  49. f -> numberOfTargets = 2;
  50. f -> times [1] = 0.0;
  51. f -> targets [1] = 0.0;
  52. f -> times [2] = my totalTime;
  53. f -> targets [2] = 0.0;
  54. f -> _iTarget = 1;
  55. }
  56. static void ArtwordData_setTarget (ArtwordData me, double time, double target) {
  57. Melder_assert (my numberOfTargets >= 2);
  58. int32 insertionPosition = 1; // should be able to go up to 32768
  59. while (insertionPosition <= my numberOfTargets && my times [insertionPosition] < time)
  60. insertionPosition ++;
  61. Melder_assert (insertionPosition <= my numberOfTargets); // can never insert past totalTime
  62. if (my times [insertionPosition] == time) {
  63. my targets [insertionPosition] = target;
  64. return;
  65. }
  66. if (my numberOfTargets == INT16_MAX)
  67. Melder_throw (U"An Artword cannot have more than ", INT16_MAX, U" targets.");
  68. my times.insert (insertionPosition, time);
  69. my targets.insert (insertionPosition, target);
  70. my numberOfTargets ++; // maintain invariant
  71. }
  72. void Artword_setTarget (Artword me, kArt_muscle muscle, double time, double target) {
  73. try {
  74. Melder_assert ((int) muscle >= 1);
  75. Melder_assert ((int) muscle <= (int) kArt_muscle::MAX);
  76. Melder_assert (muscle <= kArt_muscle::MAX);
  77. if (time < 0.0) time = 0.0;
  78. if (time > my totalTime) time = my totalTime;
  79. ArtwordData_setTarget (& my data [(int) muscle], time, target);
  80. } catch (MelderError) {
  81. Melder_throw (me, U": target not set.");
  82. }
  83. }
  84. static double ArtwordData_getTarget (ArtwordData me, double time) {
  85. int16 targetNumber = my _iTarget;
  86. if (! targetNumber) targetNumber = 1;
  87. while (time > my times [targetNumber + 1] && targetNumber < my numberOfTargets - 1)
  88. targetNumber ++;
  89. while (time < my times [targetNumber] && targetNumber > 1)
  90. targetNumber --;
  91. my _iTarget = targetNumber;
  92. Melder_assert (targetNumber > 0 && targetNumber < my numberOfTargets);
  93. return my targets [targetNumber] + (time - my times [targetNumber]) *
  94. (my targets [targetNumber + 1] - my targets [targetNumber]) /
  95. (my times [targetNumber + 1] - my times [targetNumber]);
  96. }
  97. double Artword_getTarget (Artword me, kArt_muscle muscle, double time) {
  98. return ArtwordData_getTarget (& my data [(int) muscle], time);
  99. }
  100. void Artword_removeTarget (Artword me, kArt_muscle muscle, int16 targetNumber) {
  101. ArtwordData f = & my data [(int) muscle];
  102. Melder_assert (targetNumber >= 1);
  103. Melder_assert (targetNumber <= f -> numberOfTargets);
  104. if (targetNumber == 1) {
  105. f -> targets [targetNumber] = 0.0;
  106. } else if (targetNumber == f -> numberOfTargets) {
  107. f -> targets [f -> numberOfTargets] = 0.0;
  108. } else {
  109. f -> times. remove (targetNumber);
  110. f -> targets. remove (targetNumber);
  111. f -> numberOfTargets --; // maintain invariant
  112. }
  113. f -> _iTarget = 1;
  114. }
  115. void Artword_intoArt (Artword me, Art art, double time) {
  116. for (int muscle = 1; muscle <= (int) kArt_muscle::MAX; muscle ++)
  117. art -> art [muscle] = Artword_getTarget (me, (kArt_muscle) muscle, time);
  118. }
  119. void Artword_draw (Artword me, Graphics g, kArt_muscle muscle, bool garnish) {
  120. int16 numberOfTargets = my data [(int) muscle]. numberOfTargets;
  121. if (numberOfTargets > 0) {
  122. autoVEC x = VECraw (numberOfTargets), y = VECraw (numberOfTargets);
  123. Graphics_setInner (g);
  124. Graphics_setWindow (g, 0, my totalTime, -1.0, 1.0);
  125. for (int16 i = 1; i <= numberOfTargets; i ++) {
  126. x [i] = my data [(int) muscle]. times [i];
  127. y [i] = my data [(int) muscle]. targets [i];
  128. }
  129. Graphics_polyline (g, numberOfTargets, & x [1], & y [1]);
  130. Graphics_unsetInner (g);
  131. }
  132. if (garnish) {
  133. Graphics_drawInnerBox (g);
  134. Graphics_marksBottom (g, 2, true, true, false);
  135. Graphics_marksLeft (g, 3, true, true, true);
  136. Graphics_textTop (g, false, kArt_muscle_getText (muscle));
  137. Graphics_textBottom (g, true, U"Time (s)");
  138. }
  139. }
  140. /* End of file Artword.cpp */