From 7c63dc636ee9b68b34deaea01cad5b7ddad98245 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 18:13:14 +0100 Subject: [PATCH 01/11] New: hexagon mapping --- .../applications/crossing/HexagonMapping.java | 178 ++++++++++++++++++ .../opennars/applications/crossing/Util.java | 15 +- .../applications/crossing/Vec2Int.java | 44 +++++ 3 files changed, 231 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/opennars/applications/crossing/HexagonMapping.java create mode 100644 src/main/java/com/opennars/applications/crossing/Vec2Int.java diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java new file mode 100644 index 0000000..971517b --- /dev/null +++ b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java @@ -0,0 +1,178 @@ +/* + * The MIT License + * + * Copyright 2018 The OpenNARS authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.opennars.applications.crossing; + +public class HexagonMapping { + public double width; // width of hexagon + public double height; // height of hexagon + + public HexagonMapping(final double width, final double height) { + this.width = width; + this.height = height; + } + + public Vec2Int map(final double x, final double y) { + final int ix = (int)(x / (width*2.0)); + final int iy = (int)(y / (height*1.5)); + + final double relX = x - ix * (width*2.0); + final double relY = y - iy * (height*1.5); + + final Vec2Int relativeHexagonIndices = mapGroupToRelCell(relX, relY); + return new Vec2Int(relativeHexagonIndices.x + ix*2, relativeHexagonIndices.y + iy*2); + } + + private Vec2Int mapGroupToRelCell(final double x, final double y) { + // see https://www.redblobgames.com/grids/hexagons/ for illustration + + double[][] positions = new double[][]{ + {0.5 * width, -0.25 * height}, + {1.5 * width, -0.25 * height}, + + {0.0, 0.5 * height}, + {width, 0.5 * height}, + {width + width, 0.5 * height}, + + {0.5 * width, 1.25 * height}, + {1.5 * width, 1.25 * height}, + }; + + double[] distances = new double[positions.length]; + + for(int i=0;i 1.0) { + return false; + } + + // compute coordinate system relative to the edge + final double rrx = absRelXCenter + 2.0; + final double rry = absRelYCenter; + + // is in hexagon if it is inside the cut area + return rrx <= rry; + } + + // maps 0.0 - 1.0 to -1.0 - 1.0 + private static double mapTo11(final double v) { + return -1.0 + v * 2.0; + } +} diff --git a/src/main/java/com/opennars/applications/crossing/Util.java b/src/main/java/com/opennars/applications/crossing/Util.java index f1442d0..7dac658 100644 --- a/src/main/java/com/opennars/applications/crossing/Util.java +++ b/src/main/java/com/opennars/applications/crossing/Util.java @@ -33,18 +33,21 @@ public class Util { public static Random rnd = new Random(); - public static final int discretization =10; - + + public static double hexagonWidth = 10.0; + public static double hexagonHeight = 10.0; + + public static int discretization = 10; + public static double distance(double posX, double posY, double posX2, double posY2) { double dx = posX - posX2; double dy = posY - posY2; return Math.sqrt(dx * dx + dy * dy); } - public static String positionToTerm(int X, int Y) { - int posX = X / discretization; - int posY = Y / discretization; - return posX + "_" + posY; + public static String positionToTerm(int x, int y) { + final Vec2Int pos = (new HexagonMapping(hexagonWidth, hexagonHeight)).map(x, y); + return pos.x + "_" + pos.y; } public static float truthToValue(TruthValue truth) { diff --git a/src/main/java/com/opennars/applications/crossing/Vec2Int.java b/src/main/java/com/opennars/applications/crossing/Vec2Int.java new file mode 100644 index 0000000..a48480f --- /dev/null +++ b/src/main/java/com/opennars/applications/crossing/Vec2Int.java @@ -0,0 +1,44 @@ +/* + * The MIT License + * + * Copyright 2018 The OpenNARS authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.opennars.applications.crossing; + +public class Vec2Int { + public int x; + public int y; + + public Vec2Int(final int x, final int y) { + this.x = x; + this.y = y; + } + + @Override + public int hashCode() { + return x * 5000 + y; + } + + @Override + public boolean equals(Object other) { + return other instanceof Vec2Int && x == ((Vec2Int)other).x && y == ((Vec2Int)other).y; + } +} \ No newline at end of file From 8836e0dc0a0b281f6b50117fac37c3124015e4ad Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 18:21:35 +0100 Subject: [PATCH 02/11] Refactor: created variable for heaxgon mapping --- .../java/com/opennars/applications/crossing/Crossing.java | 5 +++++ src/main/java/com/opennars/applications/crossing/Util.java | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Crossing.java b/src/main/java/com/opennars/applications/crossing/Crossing.java index 5e04c6f..29bf1b1 100644 --- a/src/main/java/com/opennars/applications/crossing/Crossing.java +++ b/src/main/java/com/opennars/applications/crossing/Crossing.java @@ -34,6 +34,11 @@ import processing.event.MouseEvent; public class Crossing extends PApplet { + public static double hexagonWidth = 10.0; + public static double hexagonHeight = 10.0; + + static HexagonMapping hexagonMapping = new HexagonMapping(hexagonWidth, hexagonHeight); + Nar nar; int entityID = 1; diff --git a/src/main/java/com/opennars/applications/crossing/Util.java b/src/main/java/com/opennars/applications/crossing/Util.java index 7dac658..84ae5c1 100644 --- a/src/main/java/com/opennars/applications/crossing/Util.java +++ b/src/main/java/com/opennars/applications/crossing/Util.java @@ -34,8 +34,6 @@ public class Util { public static Random rnd = new Random(); - public static double hexagonWidth = 10.0; - public static double hexagonHeight = 10.0; public static int discretization = 10; @@ -46,7 +44,7 @@ public static double distance(double posX, double posY, double posX2, double pos } public static String positionToTerm(int x, int y) { - final Vec2Int pos = (new HexagonMapping(hexagonWidth, hexagonHeight)).map(x, y); + final Vec2Int pos = Crossing.hexagonMapping.map(x, y); return pos.x + "_" + pos.y; } From 10d8c591b5c1d7c38f645446008efb68ac5188c8 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 18:23:51 +0100 Subject: [PATCH 03/11] Add: method for returning position of hexagon --- .../opennars/applications/crossing/HexagonMapping.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java index 971517b..c6b3f91 100644 --- a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java +++ b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java @@ -32,6 +32,14 @@ public HexagonMapping(final double width, final double height) { this.height = height; } + public double[] calcPositionOfHexagon(final int x, final int y) { + final double offsetX = (y % 2) == 0 ? 0.0 : -0.5*width; + + double resultX = (x + 1) * width + offsetX; + double resultY = 0.5*height + 0.75*height*y; + return new double[]{resultX, resultY}; + } + public Vec2Int map(final double x, final double y) { final int ix = (int)(x / (width*2.0)); final int iy = (int)(y / (height*1.5)); From a51bc0ea443219a362db394d6a8dbf8dca0225f1 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 19:02:32 +0100 Subject: [PATCH 04/11] Add: rendering of hexagon --- .../applications/crossing/Crossing.java | 35 ++++++++++++++++--- .../applications/crossing/NarListener.java | 9 +++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Crossing.java b/src/main/java/com/opennars/applications/crossing/Crossing.java index 29bf1b1..849609c 100644 --- a/src/main/java/com/opennars/applications/crossing/Crossing.java +++ b/src/main/java/com/opennars/applications/crossing/Crossing.java @@ -116,12 +116,15 @@ public void draw() { nar.addInput(questions); } } - for (int i = 0; i < 1000; i += Util.discretization) { - stroke(128); - line(0, i, 1000, i); - line(i, 0, i, 1000); + + for (int y=50;y<100;y++) { + for (int x=25;x<75;x++) { + final double[] pos = hexagonMapping.calcPositionOfHexagon(x, y); + drawHexagon(pos[0], pos[1]); + } } + for (Entity e : entities) { e.draw(this, streets, trafficLights, entities, null, 0); } @@ -162,6 +165,17 @@ public void draw() { System.out.println("Concepts: " + nar.memory.concepts.size()); } + private void drawHexagon(final double x, final double y) { + stroke(128); + + for (int i=0; i < relatives.length; i++) { + final double[] aRel = relatives[i]; + final double[] bRel = relatives[(i+1) % relatives.length]; + + line((float)(x + aRel[0]), (float)(y + aRel[1]), (float)(x + bRel[0]), (float)(y + bRel[1])); + } + } + public void removeOutdatedPredictions(List predictions) { List toDelete = new ArrayList(); for(Prediction pred : predictions) { @@ -218,4 +232,17 @@ public static void main(String[] args) { new IncidentSimulator().show(); PApplet.runSketch(args2, mp); } + + + final double[][] relatives = new double[][]{ + {hexagonMapping.width * -0.5, hexagonMapping.height * -0.25}, + {hexagonMapping.width * -0.5, hexagonMapping.height * 0.25}, + + {0.0, hexagonMapping.height * 0.5}, + + {hexagonMapping.width * 0.5, hexagonMapping.height * 0.25}, + {hexagonMapping.width * 0.5, hexagonMapping.height * -0.25}, + + {0.0, hexagonMapping.height * -0.5}, + }; } diff --git a/src/main/java/com/opennars/applications/crossing/NarListener.java b/src/main/java/com/opennars/applications/crossing/NarListener.java index d0c211e..2c8b54b 100644 --- a/src/main/java/com/opennars/applications/crossing/NarListener.java +++ b/src/main/java/com/opennars/applications/crossing/NarListener.java @@ -109,8 +109,13 @@ public Prediction predictionFromTask(Task t) { String position = prod.term[1].toString(); if(position.contains("_")) { try { - int posX = camera.minX + Util.discretization * Integer.valueOf(position.split("_")[0]); - int posY = camera.minY + Util.discretization * Integer.valueOf(position.split("_")[1]); + + final int mappingCoordinateOfProductX = Integer.valueOf(position.split("_")[0]); + final int mappingCoordinateOfProductY = Integer.valueOf(position.split("_")[1]); + final double[] mappedCoordinate = Crossing.hexagonMapping.calcPositionOfHexagon(mappingCoordinateOfProductX, mappingCoordinateOfProductY); + + int posX = camera.minX + (int)mappedCoordinate[0];//camera.minX + Util.discretization * Integer.valueOf(position.split("_")[0]); + int posY = camera.minY + (int)mappedCoordinate[1];//camera.minY + Util.discretization * Integer.valueOf(position.split("_")[1]); //int id = 0; //Integer.valueOf(idStr.toString()); often a dep var Entity pred; if(type.toString().startsWith(car.toString())) { From 734bc9c951cf19f928d1792fd2d17b71c851f13e Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 19:04:44 +0100 Subject: [PATCH 05/11] Refactor: removal of old complicated code --- .../applications/crossing/HexagonMapping.java | 76 ------------------- 1 file changed, 76 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java index c6b3f91..32287e9 100644 --- a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java +++ b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java @@ -106,81 +106,5 @@ else if (minDistanceIdx == 5) { else { return new Vec2Int(1, 1); } - - /* - - final int topRowResult = mapXRowOfHexagons(x + width * 0.5, y + height * 0.75); - if (topRowResult != 2) { - return new Vec2Int(topRowResult, -1); - } - - final int centerRowResult = mapXRowOfHexagons(x, y); - if (centerRowResult != 2) { - return new Vec2Int(centerRowResult, 0); - } - - final int bottomRowResult = mapXRowOfHexagons(x + width * 0.5, y - height * 0.75); - if (bottomRowResult != 2) { - return new Vec2Int(bottomRowResult, 1); - } - */ - - //System.out.println(x); - //System.out.println(y); - - // must never happen - //throw new InternalError(); - } - - // maps the coordinate to the x index of the cell - // see top row of illustration - // -1 if the cell before the center cell - // 0 if it is the center cell - // 1 if it is after the center cell - // 2 if it not a cell before or after or the center cell - private int mapXRowOfHexagons(final double x, final double y) { - // see https://www.redblobgames.com/grids/hexagons/ for illustration - - // map coordinates to relative hexagon coordinates - - // absolute to hexagon pairs - final double hx = x / (width*2.0); - final double hy = y / (height*1.0); - - if( isInSingleCell(mapTo11((hx) * 0.5), mapTo11(hy)) ) { - return -1; - } - if( isInSingleCell(mapTo11((hx - 0.5) * 0.5), mapTo11(hy)) ) { - return 0; - } - else if( isInSingleCell(mapTo11((hx - 1.0) * 0.5), mapTo11(hy)) ) { - return 1; - } - else { - return 2; - } - } - - // /param rx relative x to center - -1.0 to 1.0 - // /param ry relative y to center - -1.0 to 1.0 - private boolean isInSingleCell(final double rx, final double ry) { - final double absRelYCenter = Math.abs(ry); - final double absRelXCenter = Math.abs(rx); - // is not inside it if outside of -1.0 to 1.0 range - if (absRelXCenter > 1.0) { - return false; - } - - // compute coordinate system relative to the edge - final double rrx = absRelXCenter + 2.0; - final double rry = absRelYCenter; - - // is in hexagon if it is inside the cut area - return rrx <= rry; - } - - // maps 0.0 - 1.0 to -1.0 - 1.0 - private static double mapTo11(final double v) { - return -1.0 + v * 2.0; } } From a75ef248b71b72f7c1dd410cd2087395a547cf4f Mon Sep 17 00:00:00 2001 From: PtrMan Date: Thu, 22 Nov 2018 13:50:09 +0100 Subject: [PATCH 06/11] Fix: fixed offset of drawn entity --- .../com/opennars/applications/crossing/Entity.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index fa3dba9..fabb4de 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -81,13 +81,21 @@ public void draw(PApplet applet, List streets, List traffi //float posYDiscrete = (((int) this.posY)/Util.discretization * Util.discretization); applet.translate((float) posX, (float) posY); applet.rotate((float) angle); + if(truth == null) { - applet.rect(0, 0, Util.discretization*scale, Util.discretization/2*scale); + final float width = Util.discretization/2*scale; + applet.rect(0.0f*Util.discretization*scale, -0.5f*width, Util.discretization*scale, width); } - applet.ellipse(2.5f, 2.5f, Util.discretization*scale, Util.discretization*scale); + applet.ellipse(0, 0, Util.discretization*scale, Util.discretization*scale); + applet.popMatrix(); applet.fill(0); applet.text(String.valueOf(id), (float)posX, (float)posY); + + // used for debugging the "real position" + applet.rect((float)posX, (float)posY, 3, 3); + + if(truth != null) { return; } From 3983c2c853d16dd822a2913b21a32f13827bbdc5 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Thu, 22 Nov 2018 13:53:45 +0100 Subject: [PATCH 07/11] Update: optimized allocation --- .../applications/crossing/HexagonMapping.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java index 32287e9..1ce8a9d 100644 --- a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java +++ b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java @@ -24,12 +24,26 @@ package com.opennars.applications.crossing; public class HexagonMapping { + private final double[][] positions; + public double width; // width of hexagon public double height; // height of hexagon public HexagonMapping(final double width, final double height) { this.width = width; this.height = height; + + positions = new double[][]{ + {0.5 * width, -0.25 * height}, + {1.5 * width, -0.25 * height}, + + {0.0, 0.5 * height}, + {width, 0.5 * height}, + {width + width, 0.5 * height}, + + {0.5 * width, 1.25 * height}, + {1.5 * width, 1.25 * height}, + }; } public double[] calcPositionOfHexagon(final int x, final int y) { @@ -54,17 +68,7 @@ public Vec2Int map(final double x, final double y) { private Vec2Int mapGroupToRelCell(final double x, final double y) { // see https://www.redblobgames.com/grids/hexagons/ for illustration - double[][] positions = new double[][]{ - {0.5 * width, -0.25 * height}, - {1.5 * width, -0.25 * height}, - - {0.0, 0.5 * height}, - {width, 0.5 * height}, - {width + width, 0.5 * height}, - {0.5 * width, 1.25 * height}, - {1.5 * width, 1.25 * height}, - }; double[] distances = new double[positions.length]; From 43eb5c3f07a8d8f668a069b8c46d2c128792444a Mon Sep 17 00:00:00 2001 From: PtrMan Date: Thu, 22 Nov 2018 13:55:32 +0100 Subject: [PATCH 08/11] Update: optimized allocation --- .../com/opennars/applications/crossing/HexagonMapping.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java index 1ce8a9d..ca8732a 100644 --- a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java +++ b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java @@ -25,10 +25,11 @@ public class HexagonMapping { private final double[][] positions; + private final double[] distances; public double width; // width of hexagon public double height; // height of hexagon - + public HexagonMapping(final double width, final double height) { this.width = width; this.height = height; @@ -44,6 +45,8 @@ public HexagonMapping(final double width, final double height) { {0.5 * width, 1.25 * height}, {1.5 * width, 1.25 * height}, }; + + distances = new double[positions.length]; } public double[] calcPositionOfHexagon(final int x, final int y) { @@ -70,8 +73,6 @@ private Vec2Int mapGroupToRelCell(final double x, final double y) { - double[] distances = new double[positions.length]; - for(int i=0;i Date: Thu, 22 Nov 2018 13:55:58 +0100 Subject: [PATCH 09/11] Update: optimized allocation --- .../java/com/opennars/applications/crossing/HexagonMapping.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java index ca8732a..9c43527 100644 --- a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java +++ b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java @@ -29,7 +29,7 @@ public class HexagonMapping { public double width; // width of hexagon public double height; // height of hexagon - + public HexagonMapping(final double width, final double height) { this.width = width; this.height = height; From 124ae81270fdc461fa8ca8c59bbcd4f8785c5906 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Thu, 22 Nov 2018 18:10:58 +0100 Subject: [PATCH 10/11] Update: Fix bugs in hexagon mapping, added hack for position --- .../applications/crossing/Crossing.java | 60 +++++++--- .../applications/crossing/DebugObject.java | 14 +++ .../applications/crossing/Entity.java | 2 +- .../applications/crossing/HexagonMapping.java | 105 +++++++++++++++++- .../applications/crossing/NarListener.java | 19 +++- .../applications/crossing/Vec2Int.java | 2 +- 6 files changed, 177 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/opennars/applications/crossing/DebugObject.java diff --git a/src/main/java/com/opennars/applications/crossing/Crossing.java b/src/main/java/com/opennars/applications/crossing/Crossing.java index 849609c..da9b744 100644 --- a/src/main/java/com/opennars/applications/crossing/Crossing.java +++ b/src/main/java/com/opennars/applications/crossing/Crossing.java @@ -54,6 +54,7 @@ public void setup() { nar.narParameters.VOLUME = 0; nar.narParameters.DURATION*=10; NarListener listener = new NarListener(cameras.get(0), nar, predictions, disappointments, entities); + listener.crossing = this; nar.on(Events.TaskAdd.class, listener); nar.on(DISAPPOINT.class, listener); } catch (Exception ex) { @@ -83,7 +84,31 @@ public void setup() { String narsese = "<(*,{" + l.id + "}," + pos + ") --> at>."; nar.addInput(narsese); }*/ - + + + /** + * test if the hexagon mapping works as expected + */ + /* + for(int y=500;y<500+60; y+=1) { + for(int x=500;x<500+60; x+= 1) { + + Vec2Int mapped = hexagonMapping.map(x, y); + + int color = mapped.hashCode() % 4; + + DebugObject d = new DebugObject(x, y); + d.colorR = (float)color / 4.0f; + + debugObjects.add(d); + } + } + */ + + + + + size(1000, 1000); frameRate(fps); new NarSimpleGUI(nar); @@ -93,6 +118,9 @@ public void setup() { List trafficLights = new ArrayList(); List entities = new ArrayList(); List cameras = new ArrayList(); + + List debugObjects = new ArrayList<>(); + int t = 0; public static boolean showAnomalies = false; @@ -117,8 +145,8 @@ public void draw() { } } - for (int y=50;y<100;y++) { - for (int x=25;x<75;x++) { + for (int y=50;y<80;y++) { + for (int x=40;x<65;x++) { final double[] pos = hexagonMapping.calcPositionOfHexagon(x, y); drawHexagon(pos[0], pos[1]); } @@ -132,6 +160,12 @@ public void draw() { tl.draw(this, t); } + for (final DebugObject iDebug : debugObjects) { + this.stroke(0, 0); // no stroke + this.fill(iDebug.colorR * 255.0f, 0, 0); + this.rect((float)(iDebug.posX-0.5), (float)(iDebug.posY-0.5), 1, 1); + } + // tick for (Entity ie : entities) { ie.tick(); @@ -166,11 +200,15 @@ public void draw() { } private void drawHexagon(final double x, final double y) { + stroke(0); + // used for debugging the "real position" + rect((float)x-1.5f, (float)y-1.5f, 3, 3); + stroke(128); - for (int i=0; i < relatives.length; i++) { - final double[] aRel = relatives[i]; - final double[] bRel = relatives[(i+1) % relatives.length]; + for (int i=0; i < hexagonMapping.verticesRelative.length; i++) { + final double[] aRel = hexagonMapping.verticesRelative[i]; + final double[] bRel = hexagonMapping.verticesRelative[(i+1) % hexagonMapping.verticesRelative.length]; line((float)(x + aRel[0]), (float)(y + aRel[1]), (float)(x + bRel[0]), (float)(y + bRel[1])); } @@ -234,15 +272,5 @@ public static void main(String[] args) { } - final double[][] relatives = new double[][]{ - {hexagonMapping.width * -0.5, hexagonMapping.height * -0.25}, - {hexagonMapping.width * -0.5, hexagonMapping.height * 0.25}, - - {0.0, hexagonMapping.height * 0.5}, - - {hexagonMapping.width * 0.5, hexagonMapping.height * 0.25}, - {hexagonMapping.width * 0.5, hexagonMapping.height * -0.25}, - {0.0, hexagonMapping.height * -0.5}, - }; } diff --git a/src/main/java/com/opennars/applications/crossing/DebugObject.java b/src/main/java/com/opennars/applications/crossing/DebugObject.java new file mode 100644 index 0000000..913e684 --- /dev/null +++ b/src/main/java/com/opennars/applications/crossing/DebugObject.java @@ -0,0 +1,14 @@ +package com.opennars.applications.crossing; + +public class DebugObject { + public double posX; + public double posY; + public int remainingTime = 100; + + public float colorR = 1.0f; + + public DebugObject(final double posX, final double posY) { + this.posX = posX; + this.posY = posY; + } +} diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index fabb4de..9a8703b 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -93,7 +93,7 @@ public void draw(PApplet applet, List streets, List traffi applet.text(String.valueOf(id), (float)posX, (float)posY); // used for debugging the "real position" - applet.rect((float)posX, (float)posY, 3, 3); + applet.rect((float)posX-1.5f, (float)posY-1.5f, 3, 3); if(truth != null) { diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java index 9c43527..799283d 100644 --- a/src/main/java/com/opennars/applications/crossing/HexagonMapping.java +++ b/src/main/java/com/opennars/applications/crossing/HexagonMapping.java @@ -24,8 +24,9 @@ package com.opennars.applications.crossing; public class HexagonMapping { - private final double[][] positions; - private final double[] distances; + //private final double[][] positions; + //private final double[] distances; + public final double[][] verticesRelative; public double width; // width of hexagon public double height; // height of hexagon @@ -34,6 +35,20 @@ public HexagonMapping(final double width, final double height) { this.width = width; this.height = height; + // relative positions of the vertices of a single hexagon + verticesRelative = new double[][]{ + {width * -0.5, height * -0.25}, + {width * -0.5, height * 0.25}, + + {0.0, height * 0.5}, + + {width * 0.5, height * 0.25}, + {width * 0.5, height * -0.25}, + + {0.0, height * -0.5}, + }; + + /* positions = new double[][]{ {0.5 * width, -0.25 * height}, {1.5 * width, -0.25 * height}, @@ -47,6 +62,7 @@ public HexagonMapping(final double width, final double height) { }; distances = new double[positions.length]; + */ } public double[] calcPositionOfHexagon(final int x, final int y) { @@ -58,6 +74,33 @@ public double[] calcPositionOfHexagon(final int x, final int y) { } public Vec2Int map(final double x, final double y) { + for(int ix=0;ix<100;ix++) { + for(int iy=0;iy<100;iy++) { + final double[] positionOfHexagon = calcPositionOfHexagon(ix, iy); + + + + boolean isInHexagon = isInHexagonRelative(x - positionOfHexagon[0], y - positionOfHexagon[1]); + + // HACK + /* + isInHexagon = isInPolygon(x - positionOfHexagon[0], y - positionOfHexagon[1], + 0, 0, + 5, 0, + 0, 5 + ); + */ + + if (isInHexagon) { + return new Vec2Int(ix, iy); + } + } + } + + // default! + return new Vec2Int(5, 0); + + /* final int ix = (int)(x / (width*2.0)); final int iy = (int)(y / (height*1.5)); @@ -66,8 +109,64 @@ public Vec2Int map(final double x, final double y) { final Vec2Int relativeHexagonIndices = mapGroupToRelCell(relX, relY); return new Vec2Int(relativeHexagonIndices.x + ix*2, relativeHexagonIndices.y + iy*2); + */ + } + + // public for testing + public boolean isInHexagonRelative(final double x, final double y) { + if (x==0 && y==0) { + return true; + } + + for(int i=0;i<6;i++) { + final int iNext = (i + 1) % 6; + final int iOtherSide = (i + 4) % 6; + + final boolean isInPolygonOfHexagon = isInPolygon( + x, y, + verticesRelative[iNext][0], verticesRelative[iNext][1], + verticesRelative[i][0], verticesRelative[i][1], + verticesRelative[iOtherSide][0], verticesRelative[iOtherSide][1] + ); + if (isInPolygonOfHexagon) { + return true; + } + } + + return false; + } + + + public static boolean isInPolygon(final double x, final double y, final double x0, final double y0, final double x1, final double y1, final double x2, final double y2) { + final boolean side0 = side(x, y, x0, y0, x1, y1); + final boolean side1 = side(x, y, x1, y1, x2, y2); + final boolean side2 = side(x, y, x2, y2, x0, y0); + + return side0 && side1 && side2; + } + + public static boolean side(final double x, final double y, final double ax, final double ay, final double bx, final double by) { + final double dx = bx - ax; + final double dy = by - ay; + + // perpendicular direction - which is the orientation of the edge + final double idx = -dy; + final double idy = dx; + + final double pdx = x - ax; + final double pdy = y - ay; + + // side is computed by direction and difference + return dot(idx, idy, pdx, pdy) >= 0.0; + } + + private static double dot(final double ax, final double ay, final double bx, final double by) { + return ax*bx + ay*by; } + + +/* private Vec2Int mapGroupToRelCell(final double x, final double y) { // see https://www.redblobgames.com/grids/hexagons/ for illustration @@ -112,4 +211,6 @@ else if (minDistanceIdx == 5) { return new Vec2Int(1, 1); } } + + */ } diff --git a/src/main/java/com/opennars/applications/crossing/NarListener.java b/src/main/java/com/opennars/applications/crossing/NarListener.java index 2c8b54b..f61d9c3 100644 --- a/src/main/java/com/opennars/applications/crossing/NarListener.java +++ b/src/main/java/com/opennars/applications/crossing/NarListener.java @@ -23,13 +23,11 @@ */ package com.opennars.applications.crossing; -import java.util.ArrayList; import java.util.List; import org.opennars.entity.Sentence; import org.opennars.entity.Stamp; import org.opennars.entity.Task; import org.opennars.entity.TruthValue; -import org.opennars.interfaces.Timable; import org.opennars.io.Symbols; import org.opennars.io.events.EventEmitter; import org.opennars.io.events.Events; @@ -38,7 +36,6 @@ import org.opennars.language.Product; import org.opennars.language.Term; import org.opennars.main.Nar; -import org.opennars.storage.Memory; public class NarListener implements EventEmitter.EventObserver { public class Prediction @@ -61,6 +58,9 @@ public Prediction(Entity ent, TruthValue truth, long time, String type) { List disappointments; Nar nar; Camera camera; + + Crossing crossing; + public NarListener(Camera camera, Nar nar, List predictions, List disappointments, List entities) { this.predictions = predictions; this.disappointments = disappointments; @@ -110,12 +110,21 @@ public Prediction predictionFromTask(Task t) { if(position.contains("_")) { try { - final int mappingCoordinateOfProductX = Integer.valueOf(position.split("_")[0]); - final int mappingCoordinateOfProductY = Integer.valueOf(position.split("_")[1]); + final int mappingCoordinateOfProductX = Integer.valueOf(position.split("_")[0]); + final int mappingCoordinateOfProductY = Integer.valueOf(position.split("_")[1]); final double[] mappedCoordinate = Crossing.hexagonMapping.calcPositionOfHexagon(mappingCoordinateOfProductX, mappingCoordinateOfProductY); int posX = camera.minX + (int)mappedCoordinate[0];//camera.minX + Util.discretization * Integer.valueOf(position.split("_")[0]); int posY = camera.minY + (int)mappedCoordinate[1];//camera.minY + Util.discretization * Integer.valueOf(position.split("_")[1]); + + // HACK + // Robert< no idea where the offset is comming from - seems to be only a visual issue > + posY -= 10; + + // used for debugging the "real position" + //crossing.rect((float)posX, (float)posY, 3, 3); + crossing.debugObjects.add(new DebugObject(posX, posY)); + //int id = 0; //Integer.valueOf(idStr.toString()); often a dep var Entity pred; if(type.toString().startsWith(car.toString())) { diff --git a/src/main/java/com/opennars/applications/crossing/Vec2Int.java b/src/main/java/com/opennars/applications/crossing/Vec2Int.java index a48480f..eaf0ca3 100644 --- a/src/main/java/com/opennars/applications/crossing/Vec2Int.java +++ b/src/main/java/com/opennars/applications/crossing/Vec2Int.java @@ -34,7 +34,7 @@ public Vec2Int(final int x, final int y) { @Override public int hashCode() { - return x * 5000 + y; + return x + y * 13; } @Override From cb697465157715022d9e8e90d00c8202968b5b1e Mon Sep 17 00:00:00 2001 From: PtrMan Date: Fri, 23 Nov 2018 13:08:13 +0100 Subject: [PATCH 11/11] Add: test --- .../crossing/HexagonMappingTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/com/opennars/applications/crossing/HexagonMappingTest.java diff --git a/src/main/java/com/opennars/applications/crossing/HexagonMappingTest.java b/src/main/java/com/opennars/applications/crossing/HexagonMappingTest.java new file mode 100644 index 0000000..5ad23f1 --- /dev/null +++ b/src/main/java/com/opennars/applications/crossing/HexagonMappingTest.java @@ -0,0 +1,22 @@ +package com.opennars.applications.crossing; + +public class HexagonMappingTest { + static public void main(String[] args) { + + //System.out.println(HexagonMapping.isInPolygon(-0.5, 0.5, 0, 0, 10, 0, 0, 10)); + + + HexagonMapping mapping = new HexagonMapping(5, 10); + + for(int y =0;y<10;y++) { + for(int x=0;x<30;x++) { + + Vec2Int hex = mapping.map(x, y); + + System.out.print("0123456789abcdef".charAt(hex.hashCode() % 16)); + } + + System.out.println("---"); + } + } +}