1 package cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.basic;
2
3 import java.util.logging.Logger;
4
5 import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
6 import cz.cuni.amis.pogamut.defcon.agent.DefConAgent;
7 import cz.cuni.amis.pogamut.defcon.communication.worldview.DefConWorldView;
8 import cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.IGrid;
9
10
11
12
13
14
15
16
17 public class BasicGrid<AGENT extends DefConAgent>
18 extends SensorModule<AGENT>
19 implements IGrid<SymmetricGridCell, SymmetricGridCellId, BasicGridIterator> {
20
21
22 protected float gridXStart;
23 protected float gridYStart;
24 protected float gridStep;
25 protected int gridXCellCount;
26 protected int gridYCellCount;
27
28
29
30
31 public BasicGrid(AGENT agent, float gridStep, int gridXCellCount, int gridYCellCount,
32 int gridXStart, int gridYStart) {
33 this(agent, gridStep, gridXCellCount, gridYCellCount, gridXStart, gridYStart, null);
34 }
35
36 public BasicGrid(DefConAgent agent, float gridStep, int gridXCellCount, int gridYCellCount,
37 int gridXStart, int gridYStart, Logger log) {
38 super((AGENT) agent, log);
39 initGrid(gridStep, gridXCellCount, gridYCellCount, gridXStart, gridYStart);
40 }
41
42 protected void initGrid(float gridStep, int gridXCellCount, int gridYCellCount,
43 float gridXStart, float gridYStart) {
44
45 this.gridStep = gridStep;
46 this.gridXCellCount = gridXCellCount;
47 this.gridYCellCount = gridYCellCount;
48 this.gridXStart = gridXStart;
49 this.gridYStart = gridYStart;
50
51 }
52
53 @Override
54 public SymmetricGridCell getCell(SymmetricGridCellId cellId) {
55 return new SymmetricGridCell(cellId);
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public class CellIndices {
75 private float x;
76 private float y;
77
78 public CellIndices(float x, float y) {
79 this.x = x;
80 this.y = y;
81 }
82
83 public CellIndices(CellIndices source) {
84 x = source.getX();
85 y = source.getY();
86 }
87
88 public float getX() {
89 return x;
90 }
91
92 public float getY() {
93 return y;
94 }
95
96 public float setX(int x) {
97 return this.x = x;
98 }
99
100 public float setY(int y) {
101 return this.y = y;
102 }
103
104 public CellIndices setIndices(SymmetricGridCellId cellId, BasicGrid grid) {
105 x = (float)(Math.floor((cellId.getX() - gridXStart)/gridStep)*gridStep) + gridXStart;
106 y = (float)(Math.floor((cellId.getY() - gridYStart)/gridStep)*gridStep) + gridYStart;
107 return this;
108 }
109
110 @Override
111 public boolean equals(Object o) {
112 return (o instanceof BasicGrid.CellIndices) &&
113 ((CellIndices)o).getX() == x && ((CellIndices)o).getY() == y;
114 }
115 }
116
117 public CellIndices getCellIndices(SymmetricGridCellId cellId) {
118 return new CellIndices((int)(Math.floor((cellId.getX() - gridXStart)/gridStep) + gridXStart),
119 (int)((Math.floor(cellId.getY() - gridYStart)/gridStep) + gridYStart));
120 }
121
122 @Override
123 public DefConWorldView getWorldView() {
124 return (DefConWorldView) worldView;
125 }
126
127 public float getLowerXBound() {
128 return gridXStart;
129 }
130
131 public float getUpperXBound() {
132 return gridXCellCount * gridStep - gridXStart;
133 }
134
135 public float getLowerYBound() {
136 return gridYStart;
137 }
138
139 public float getUpperYBound() {
140 return gridYCellCount * gridStep - gridYStart;
141 }
142
143 public float getGridStep() {
144 return gridStep;
145 }
146
147 public int getXCount() {
148 return gridXCellCount;
149 }
150
151 public int getYCount() {
152 return gridYCellCount;
153 }
154
155 public SymmetricGridCell getCell(float x, float y) {
156 if (y < gridYStart || y >= gridYCellCount - gridYStart)
157 return null;
158
159 return new SymmetricGridCell(new SymmetricGridCellId(
160 (x - gridXStart)/gridStep % gridXCellCount + gridXStart, y));
161 }
162
163 @Override
164 public BasicGridIterator getIterator(SymmetricGridCellId cellId) {
165 return new BasicGridIterator(cellId, this);
166 }
167 }