ProbabilityMatriz.java 2.0 KB

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