1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
import java.util.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.geometry.*;
public class Obstacles { private final static float RADIUS = 0.1f; private final static float HEIGHT = 1.0f; private final static int FLOOR_LEN = 50; private final static Color3f black = new Color3f(0.0f, 0.0f, 0.0f); private final static Color3f specular = new Color3f(1.0f, 1.0f, 1.0f); private final static Color3f red = new Color3f(0.9f, 0.1f, 0.2f);
private boolean obs[][]; private BoundingBox obsBounds[][]; private Group obsGroup;
public Obstacles() { obs = new boolean[FLOOR_LEN+1][FLOOR_LEN+1]; obsBounds = new BoundingBox[FLOOR_LEN+1][FLOOR_LEN+1]; for (int z=0; z <= FLOOR_LEN; z++) for(int x=0; x <= FLOOR_LEN; x++) { obs[z][x] = false; obsBounds[z][x] = null; }
obsGroup = new Group();
}
public void store(String line) { int x=0, z=0; String coordStr; StringTokenizer points; StringTokenizer coords = new StringTokenizer(line); while (coords.hasMoreTokens()) { coordStr = coords.nextToken(); points = new StringTokenizer(coordStr, "(,)"); try { x = Integer.parseInt(points.nextToken()); z = Integer.parseInt(points.nextToken()); } catch (NumberFormatException ex){ System.out.println("Incorrect format for obstacle data in tours file"); break; } markObstacle(x,z); } } public void store2(String line) { int x=0, z=0; String coordStr; StringTokenizer points; StringTokenizer coords = new StringTokenizer(line); while (coords.hasMoreTokens()) { coordStr = coords.nextToken(); points = new StringTokenizer(coordStr, "(,)"); try { x = Integer.parseInt(points.nextToken()); z = Integer.parseInt(points.nextToken()); } catch (NumberFormatException ex){ System.out.println("Incorrect format for obstacle data in tours file"); break; } markObstacle2(x,z); } }
private void markObstacle(int x, int z) { if ((x < -FLOOR_LEN/2) || (x > FLOOR_LEN/2)) { System.out.println("Obstacle x point out of bounds: " + x); x = 0; } if ((z < -FLOOR_LEN/2) || (z > FLOOR_LEN/2)) { System.out.println("Obstacle z point out of bounds: " + z); z = 0; } obs[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] = true; obsBounds[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] = new BoundingBox( new Point3d(x-0.5, 0.0, z+0.5), new Point3d(x+0.5, 1.0, z-0.5));
obsGroup.addChild( makeObs(x, z));
} private void markObstacle2(int x, int z) { if ((x < -FLOOR_LEN/2) || (x > FLOOR_LEN/2)) { System.out.println("Obstacle x point out of bounds: " + x); x = 0; } if ((z < -FLOOR_LEN/2) || (z > FLOOR_LEN/2)) { System.out.println("Obstacle z point out of bounds: " + z); z = 0; } obs[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] = true; obsBounds[z+(FLOOR_LEN/2)][x+(FLOOR_LEN/2)] = new BoundingBox( new Point3d(x-0.5, 0.0, z+0.5), new Point3d(x+0.5, 2.0, z-0.5));
obsGroup.addChild( makeObs2(x, z));
}
public void print() { for(int x=(-FLOOR_LEN/2); x <= (FLOOR_LEN/2); x++) { if (x == 0) System.out.print("0"); else if (x%5 == 0) System.out.print("*"); else System.out.print(" "); } System.out.println(""); for(int x=(-FLOOR_LEN/2); x <= (FLOOR_LEN/2); x++) System.out.print("-"); System.out.println("");
for (int z=0; z <= FLOOR_LEN; z++) { for(int x=0; x <= FLOOR_LEN; x++) { if (obs[z][x]) System.out.print("O"); else System.out.print(" "); } if ((z-FLOOR_LEN/2)%5 == 0) System.out.println("| " + (z-FLOOR_LEN/2)); else System.out.println("|"); }
for(int x=(-FLOOR_LEN/2); x <= (FLOOR_LEN/2); x++) System.out.print("-"); System.out.println(""); }
public boolean nearObstacle(Point3d pos, double radius) { if ((pos.x < -FLOOR_LEN/2) || (pos.x > FLOOR_LEN/2) || (pos.z < -FLOOR_LEN/2) || (pos.z > FLOOR_LEN/2)) return true;
BoundingBox bs = new BoundingBox( new Point3d(pos.x-0.5,pos.y,pos.z+0.5), new Point3d(pos.x+0.5,pos.y+1,pos.z-0.5)); for (int z=0; z <= FLOOR_LEN; z++) for(int x=0; x <= FLOOR_LEN; x++) if (obs[z][x]) { if (obsBounds[z][x].intersect(bs)) return true; } return false; }
private TransformGroup makeObs(int x, int z) { Appearance obsApp = new Appearance(); Material mat = new Material(black, black, red, specular, 100.f); mat.setLightingEnable(true); obsApp.setMaterial( mat ); Cylinder obs = new Cylinder( RADIUS, HEIGHT, Cylinder.GENERATE_NORMALS, obsApp);
TransformGroup posnTG = new TransformGroup(); Transform3D trans = new Transform3D(); trans.setTranslation( new Vector3d(x, HEIGHT/2, z) ); posnTG.setTransform(trans); posnTG.addChild(obs); return posnTG; } private TransformGroup makeObs2(int x, int z) { Appearance obsApp = new Appearance(); Material mat = new Material(black, black, red, specular, 100.f); mat.setLightingEnable(true); obsApp.setMaterial( mat ); Box obs = new Box((float)0.2, (float)1.0,(float)1.0,obsApp);
TransformGroup posnTG = new TransformGroup(); Transform3D trans = new Transform3D(); trans.setTranslation( new Vector3d(x, HEIGHT/2, z) ); posnTG.setTransform(trans); posnTG.addChild(obs); return posnTG; }
public Group getObsGroup() { return obsGroup; }
}
|