Guest User

Untitled

a guest
Jan 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import com.badlogic.gdx.graphics.g3d.model.NodeKeyframe;
  2. import com.badlogic.gdx.math.MathUtils;
  3. import com.badlogic.gdx.utils.Array;
  4.  
  5. public class AnimationControllerBench {
  6.  
  7. public static void main (String[] args) {
  8. Array<NodeKeyframe<String>> keyFrames = new Array<NodeKeyframe<String>>();
  9.  
  10. int nentities = 100;
  11. int nnodePerEntity = 8;
  12. int ntracks = 3;
  13.  
  14. float entropyFactor = 0; // 0 for minimum entropy
  15.  
  16. System.out.println("| frames | samples | With Heuristic | Without heuristic | Linear |");
  17. System.out.println("|--------|---------|----------------|-------------------|--------|");
  18.  
  19. int nframes = 2;
  20. for(int k=0 ; k<15 ; k++)
  21. {
  22. nframes *= 2;
  23. int nfactor = nentities * nnodePerEntity * ntracks;
  24. int ntests = MathUtils.floor(nfactor * 100000f / (float)nframes);
  25.  
  26. float duration = 0;
  27. float time = 0;
  28. for(int i=0 ; i<nframes ; i++){
  29. duration = time;
  30. time += MathUtils.random() * entropyFactor + 1;
  31. keyFrames.add(new NodeKeyframe<String>(duration, "frame"));
  32. }
  33.  
  34. float bstime;
  35. {
  36. long ptime = System.currentTimeMillis();
  37. for(int i=0 ; i<ntests ; i++){
  38. getFirstKeyframeIndexAtTimeBinSearchHeuristic(keyFrames, duration * (float)i / (float)(ntests - 1));
  39. }
  40. long ctime = System.currentTimeMillis();
  41. bstime = (ctime - ptime);
  42. }
  43. float nhtime;
  44. {
  45. long ptime = System.currentTimeMillis();
  46. for(int i=0 ; i<ntests ; i++){
  47. getFirstKeyframeIndexAtTimeBinSearch(keyFrames, duration * (float)i / (float)(ntests - 1));
  48. }
  49. long ctime = System.currentTimeMillis();
  50. nhtime = (ctime - ptime);
  51. }
  52. float lrtime;
  53. {
  54. long ptime = System.currentTimeMillis();
  55. for(int i=0 ; i<ntests ; i++){
  56. getFirstKeyframeIndexAtTimeLinear(keyFrames, duration * (float)i / (float)(ntests - 1));
  57. }
  58. long ctime = System.currentTimeMillis();
  59. lrtime = (ctime - ptime);
  60. }
  61. System.out.println("| " + nframes + " | " + ntests + " | " + bstime + " | " + nhtime + " | " + lrtime + " |");
  62. }
  63.  
  64. }
  65.  
  66. private final static <T> int getFirstKeyframeIndexAtTimeLinear (final Array<NodeKeyframe<T>> arr, final float time) {
  67. final int n = arr.size - 1;
  68. for (int i = 0; i < n; i++) {
  69. if (time >= arr.get(i).keytime && time <= arr.get(i + 1).keytime) {
  70. return i;
  71. }
  72. }
  73. return 0;
  74. }
  75.  
  76. final static <T> int getFirstKeyframeIndexAtTimeBinSearch (final Array<NodeKeyframe<T>> arr, final float time) {
  77. final int lastIndex = arr.size - 1;
  78.  
  79. // edges cases : time out of range always return first index
  80. if (lastIndex <= 0 || time < arr.get(0).keytime || time > arr.get(lastIndex).keytime) {
  81. return 0;
  82. }
  83.  
  84. // binary search
  85. int minIndex = 0;
  86. int maxIndex = lastIndex;
  87.  
  88. while (minIndex < maxIndex) {
  89. int i = (minIndex + maxIndex) / 2;
  90. if (time > arr.get(i + 1).keytime) {
  91. minIndex = i + 1;
  92. } else if (time < arr.get(i).keytime) {
  93. maxIndex = i - 1;
  94. } else {
  95. return i;
  96. }
  97. }
  98. return minIndex;
  99. }
  100.  
  101. final static <T> int getFirstKeyframeIndexAtTimeBinSearchHeuristic (final Array<NodeKeyframe<T>> arr, final float time) {
  102. final int lastIndex = arr.size - 1;
  103.  
  104. // edges cases : time out of range always return first index
  105. if (lastIndex <= 0 || time < arr.get(0).keytime || time > arr.get(lastIndex).keytime) {
  106. return 0;
  107. }
  108.  
  109. // binary search
  110. int minIndex = 0;
  111. int maxIndex = lastIndex;
  112.  
  113. for (;;) {
  114. final float minTime = arr.get(minIndex).keytime;
  115. final float maxTime = arr.get(maxIndex).keytime;
  116.  
  117. if (time < minTime) {
  118. return Math.max(0, minIndex - 1);
  119. }
  120. if (time > maxTime) {
  121. return Math.min(lastIndex - 1, maxIndex);
  122. }
  123. if (minTime == maxTime) {
  124. return minIndex;
  125. }
  126.  
  127. // best guess index based on time range
  128. float t = (time - minTime) / (maxTime - minTime);
  129. int index = (int)(minIndex + t * (maxIndex - minIndex));
  130.  
  131. if (time < arr.get(index).keytime) {
  132. maxIndex = index - 1;
  133. } else if (time > arr.get(index).keytime) {
  134. minIndex = index + 1;
  135. } else {
  136. return Math.min(lastIndex - 1, index);
  137. }
  138. }
  139. }
  140. }
Add Comment
Please, Sign In to add comment