Skip to content

Commit

Permalink
more work on seperation, still not finished
Browse files Browse the repository at this point in the history
  • Loading branch information
zealot128 committed Mar 18, 2011
1 parent 03ce111 commit b2bb5ed
Show file tree
Hide file tree
Showing 12 changed files with 449 additions and 76 deletions.
10 changes: 10 additions & 0 deletions src/de/htwdd/robotics/wienert/GridApproximatable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.htwdd.robotics.wienert;

import java.util.ArrayList;

import de.htwdd.robotics.map.UniversalGridMap;

public interface GridApproximatable {
ArrayList<GridLine> lines = new ArrayList<GridLine>();
public ArrayList<GridLine> approximate(UniversalGridMap<SkeletonCell> thinnedMap);
}
171 changes: 171 additions & 0 deletions src/de/htwdd/robotics/wienert/GridApproximatorRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package de.htwdd.robotics.wienert;

import java.awt.Point;
import java.util.ArrayList;

import de.htwdd.robotics.datagrid.PreservingUniversalGridCellOperation;
import de.htwdd.robotics.map.UniversalGridMap;

public class GridApproximatorRecursive implements GridApproximatable {

private Thinner helper; // using the connectivity methods etc.
private UniversalGridMap<SkeletonCell> map;

@Override
public ArrayList<GridLine> approximate(
UniversalGridMap<SkeletonCell> thinnedMap) {
map = thinnedMap;
helper = new ThinnerLuWang();
helper.map = map;
Point point = findStartPoint();
logPoint(point.x, point.y);
System.out.println("StartPunkt: " + point.x + ":" + point.y);
collectPoints(point, null);
System.out.println("Found " + lines.size() + " Lines");
return lines;
}

private void collectPoints(Point point, Point includePoint ) {
GridLine currentLine = new GridLine();
if (includePoint != null) {
currentLine.points.add(includePoint);
}
Point currentPoint = point;
while (true) {
System.out.println("Current: " + currentPoint);
currentLine.points.add(currentPoint);
setVisted(currentPoint);

ArrayList<Point> neighbors = findNeighborsFor(point);
int c = connectivity(currentPoint);
if (neighbors.size() == 0 ) {
lines.add(currentLine);
return;
}
if (c == 2) {
currentPoint = getNextPoint(neighbors,currentPoint);
} else if (c > 2) {
if (currentLine.points.size() > 1) {
lines.add(currentLine);
}
for (int i=0; i< neighbors.size(); i++) {
collectPoints(neighbors.get(i), currentPoint);
}
return;
} else {
System.out.println("ERROR connectivity < 2?? sollte eigentlich geloescht sein");
return;
}
}
}
/**
* Returning the directly orthogonal adjacent neighbors first from neighborhood. Otherwise anyone (diagonal)
* @param neighbors
* @param p
* @return
*/
private Point getNextPoint(ArrayList<Point> neighbors, Point p) {
for (Point neighbor : neighbors) {
if (neighbor.x == p.x || neighbor.y == p.y) {
return neighbor;
}
}
return neighbors.get(0);

}


private void setVisted(Point currentPoint) {
SkeletonCell cell = map.get(currentPoint.x, currentPoint.y);
cell.visited = true;
map.set(currentPoint.x, currentPoint.y, cell);
}

/**
* Find all neighbors of point, that are occupied and not visited
*
* @param point
* @return list of neighbors or empty List
*/
private ArrayList<Point> findNeighborsFor(Point point) {
ArrayList<Point> neighborhood = new ArrayList<Point>();
for (int row = -1; row < 2; row++) {
for (int col = -1; col < 2; col++) {
if (row == 0 && col == 0) {
continue; // Dont take the current point
}
int x = row + point.x;
int y = col + point.y;
SkeletonCell cell = map.get(x, y);
// System.out.println(x + ":" + y + "-" + cell.status + "/"
// +cell.visited);
if (!cell.visited && cell.status == SkeletonCell.STATE_OCCUPIED) {
neighborhood.add(new Point(x, y));
}
}
}
return neighborhood;
}

private Point startPoint;
private Point alternateStartPoint;

private int connectivity(Point p) {
int[] n = helper.transformToArray(helper.collectNeighborsFor(p.x, p.y));
return helper.numberOfChanges(n);
}

/**
* Iterate over the map and find a valid startingpoint that is a end Point
* of a line Otherwise find the first point that is a crossroad TODO
* Termination?
* TODO Finding no starting Point for small maps (no intersection)
* @return
*/
private Point findStartPoint() {

map.traverse(new PreservingUniversalGridCellOperation<SkeletonCell>() {
@Override
public void process(int row, int col, SkeletonCell cell) {
if (cell.status != SkeletonCell.STATE_OCCUPIED) {
return;
}
if (row == 0 || col == 0 || row == map.getLastRow() - 1
|| col == map.getLastColumn() - 1) {
return;
}

// ArrayList<Point> neighbors = findNeighborsFor(new Point(row,col));
int c = connectivity(new Point(row, col));
if (c == 3) {
System.out.println("StartPoint? " + row + "," + col);
startPoint = new Point(row, col);
}
// if (neighbors.size() >= 3) {
// alternateStartPoint = new Point(row, col);
// }
}
});

return startPoint;

}

public void logPoint(int x, int y) {
int radius = 5;
SkeletonCell cell = map.get(x,y);
System.out.println("Logging: " + x + ":" + y + " - S:" + cell.status);
String buf = "\n";
for (int i = -radius; i < radius+1; i++) {
buf += (y - i) + ": ";
for (int j = -radius; j < radius+1; j++) {
buf += map.get( x + j , y - i ).status + " ";
}
buf += "\n";
}
System.out.println(buf);
ArrayList<Point> neighbors = findNeighborsFor(new Point(496,
143));
}

}
13 changes: 13 additions & 0 deletions src/de/htwdd/robotics/wienert/GridLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.htwdd.robotics.wienert;

import java.awt.Point;
import java.util.ArrayList;

public class GridLine {
public ArrayList<Point> points = new ArrayList<Point>();

public void addPoint(int x,int y) {
points.add(new Point(x, y));
}

}
28 changes: 12 additions & 16 deletions src/de/htwdd/robotics/wienert/MyRobot.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
package de.htwdd.robotics.wienert;

import java.io.IOException;
import java.nio.ByteOrder;

import de.htwdd.robotics.DefaultScitosConnections;
import de.htwdd.robotics.Robot;
import de.htwdd.robotics.ScitosRobot;
import de.htwdd.robotics.depth.DepthImage;
import de.htwdd.robotics.map.OccupancyGridMap;
import de.htwdd.robotics.map.OccupancyGridMapIO;
import de.htwdd.robotics.map.UniversalGridMap;
import de.htwdd.robotics.map.container.GridMapContainer;
import de.htwdd.robotics.map.container.GridMapContainers;
import de.htwdd.robotics.path.Path;
import de.htwdd.robotics.state.container.StateContainer;
import de.htwdd.robotics.state.container.StateProvider;

/**
* A custom SCITOS robot.
*/
public class MyRobot extends Robot {

private StateContainer<DepthImage> depthContainer;

private GridMapContainer<OccupancyGridMap> mapContainer;

public GridMapContainer<OccupancyGridMap> getMapContainer() {
Expand All @@ -33,7 +27,6 @@ public GridMapContainer<OccupancyGridMap> getMapContainer() {

public UniversalGridMap<SkeletonCell> skeletonGrid;


public UniversalGridMap<SkeletonCell> getSkeletonGrid() {
return skeletonGrid;
}
Expand All @@ -43,20 +36,23 @@ public void setSkeletonGrid(UniversalGridMap<SkeletonCell> skeletonGrid) {
}

public GridMapContainer<UniversalGridMap<SkeletonCell>> skeleton;

public ThinningProcessor thinner;

public MyRobot(String hostname) throws IOException {
// super(new DefaultScitosConnections(hostname), ByteOrder.BIG_ENDIAN,
// true, true, true);
// super(new DefaultScitosConnections(hostname), ByteOrder.BIG_ENDIAN,
// true, true, true);

mapContainer = GridMapContainers.newInstance();
String filename = "htwZ2_1.png";
mapContainer.set(OccupancyGridMapIO.importFromImage(filename, 0, 0, 0.1));

mapContainer.set(OccupancyGridMapIO
.importFromImage(filename, 0, 0, 0.1));

skeleton = GridMapContainers.newInstance();
Thinner thinningAlgorithm = new ThinnerLuWang();

ThinningProcessor thinner = new ThinningProcessor(mapContainer, skeleton, thinningAlgorithm);

Thinner thinningAlgorithm = new ThinnerSuen();
GridApproximatorRecursive ap = new GridApproximatorRecursive();
ThinningProcessor thinner = new ThinningProcessor(mapContainer,
skeleton, thinningAlgorithm, ap);

// / Own Processor
addProcessor(thinner);
}
Expand Down
25 changes: 19 additions & 6 deletions src/de/htwdd/robotics/wienert/MyRobotFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

import org.slf4j.Logger;
Expand Down Expand Up @@ -77,24 +80,34 @@ public MyRobotFrame(final MyRobot robot) {
addComponent("Processors", new JScrollPane(processorPanel), new Point(
50, 50), new Dimension(250, 200), false);

EnvironmentPanel environmentPanel = new EnvironmentPanel(100);
final EnvironmentPanel environmentPanel = new EnvironmentPanel(100);

environmentPanel
.addPlugIn(new MapPlugin(robot.getMapContainer(), true));

addComponent("Environment", environmentPanel, new Point(0, 0),
new Dimension(600, 600), true);
new Dimension(1200, 800), true);

// PathPlugin pp = new PathPlugin(robot.pathProvider, Color.cyan);
environmentPanel.addPlugIn(new SkeletonMapPlugin("skeletonMapPlugin",
robot.skeleton));

environmentPanel.repaint(500);

environmentPanel.setVisible(true);
// ThinningControl tc = new ThinningControl(robot.thinner);
// addComponent("ThinnControl", tc, new Point(
// 50, 50), new Dimension(250, 200), false);
//
addUtilityMenuItem(new OpenMaskedOccupancyMapMenuItem(this, robot
.getMapContainer()));
addUtilityMenuItem(new SaveOccupancyMapMenuItem(this, robot
.getMapContainer()));

// System.exit(0); // TODO
Timer t = new Timer(500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
environmentPanel.repaint();
}
});
t.start();
}

public MyRobot getRobot() {
Expand Down
23 changes: 4 additions & 19 deletions src/de/htwdd/robotics/wienert/SkeletonCell.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.htwdd.robotics.wienert;

import java.awt.Point;
import java.util.ArrayList;

public class SkeletonCell {
public SkeletonCell(int state, int row, int column) {
Expand All @@ -10,33 +8,18 @@ public SkeletonCell(int state, int row, int column) {
this.row = row;
}

public int clearance;
public ArrayList<Point> basePoints;

public int row;
public int col;
public boolean visited=false;


static final int STATE_FREE = 0;
static final int STATE_OCCUPIED = 1;
static final int STATE_THINNED = 0;

public int status;

public int getClearance() {
return clearance;
}

public void setClearance(int clearance) {
this.clearance = clearance;
}

public ArrayList<Point> getBasePoints() {
return basePoints;
}

public void setBasePoints(ArrayList<Point> basePoints) {
this.basePoints = basePoints;
}

public int getStatus() {
return status;
Expand All @@ -52,5 +35,7 @@ public void thinnMe() {
this.status = SkeletonCell.STATE_THINNED;
this.thinned = true;
}



}
Loading

0 comments on commit b2bb5ed

Please sign in to comment.