ProbabilityMatriz.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import java.util.Iterator;
  2. import lejos.geom.Rectangle;
  3. public class ProbabilityMatriz implements Iterable<DiscretePoint> {
  4. public class MatrizIterator implements Iterator<DiscretePoint> {
  5. private final ProbabilityMatriz m;
  6. public long count = 0;
  7. public long end = totalsize;
  8. public MatrizIterator(ProbabilityMatriz m) {
  9. this.m = m;
  10. }
  11. public void set (long start, long end) {
  12. this.end = end;
  13. this.count = start;
  14. }
  15. @Override
  16. public boolean hasNext() {
  17. return count < end;
  18. }
  19. @Override
  20. public DiscretePoint next() {
  21. int b = matriz[0].length;
  22. int c = matriz[0][0].length;
  23. int x = (int) (count/(b*c));
  24. int y = (int) (count%(c*b)/c);
  25. int z = (int) (count%c);
  26. DiscretePoint tmp = new DiscretePoint(m, x, y, z);
  27. count++;
  28. return tmp;
  29. }
  30. }
  31. /*
  32. * Recebe as bordas dos triangulos da matriz (bounds)
  33. * ssize - squere size, tamanho do quadrado utilizado na discretizacao
  34. * alphad - em quantas partes sera dividido os angulos
  35. */
  36. Rectangle bounds;
  37. double[][][] matriz;
  38. double ssize;
  39. int alphad;
  40. long totalsize;
  41. ProbabilityMatriz (Rectangle bounds, double ssize, int alphad) {
  42. this.bounds = bounds;
  43. this.ssize = ssize;
  44. this.alphad = alphad;
  45. int x = (int) Math.ceil(bounds.getWidth()/ssize);
  46. int y = (int) Math.ceil(bounds.getHeight()/ssize);
  47. matriz = new double[x][y][alphad];
  48. totalsize = matriz.length*matriz[0].length*matriz[0][0].length;
  49. setAll(1);
  50. normalize();
  51. }
  52. /*
  53. * Retorna a probabilidade maxima
  54. */
  55. double max () {
  56. double max = 0;
  57. for (DiscretePoint p: this)
  58. max = Math.max(max, p.get());
  59. return max;
  60. }
  61. /*
  62. * adiciona a todos
  63. */
  64. void setAll (double v) {
  65. for (DiscretePoint p: this)
  66. p.set(v);
  67. }
  68. /*
  69. * Retorna a soma de todos o pontos
  70. */
  71. double sum () {
  72. double x = 0;
  73. for (DiscretePoint p: this)
  74. x += p.get();
  75. return x;
  76. }
  77. /*
  78. * Faz a soma ser 1
  79. */
  80. void normalize () {
  81. double s = sum();
  82. for (DiscretePoint p: this)
  83. p.set(p.get()/s);
  84. }
  85. @Override
  86. public Iterator<DiscretePoint> iterator() {
  87. ProbabilityMatriz m = this;
  88. return new MatrizIterator(m);
  89. }
  90. public void set(double v, int x, int y, int z) {
  91. matriz[x][y][z] = v;
  92. }
  93. public double get(int x, int y, int z) {
  94. return matriz[x][y][z];
  95. }
  96. public Iterator<DiscretePoint> iteratorSet(long start, long end) {
  97. ProbabilityMatriz m = this;
  98. MatrizIterator it = new MatrizIterator(m);
  99. it.set(start, end);
  100. return new MatrizIterator(m);
  101. }
  102. }