OccupancyGridMap.java
Go to the documentation of this file.
00001 package edu.tum.cs.ias.knowrob.vis.items;
00002 
00003 import java.awt.Image;
00004 import java.awt.image.BufferedImage;
00005 import java.awt.image.RescaleOp;
00006 import java.io.File;
00007 import java.io.FileNotFoundException;
00008 import java.io.FileReader;
00009 import java.io.IOException;
00010 import java.util.ArrayList;
00011 import java.util.HashMap;
00012 import java.util.LinkedHashMap;
00013 import java.util.Vector;
00014 
00015 import javax.imageio.ImageIO;
00016 import javax.vecmath.Vector3d;
00017 
00018 import org.yaml.snakeyaml.Yaml;
00019 
00020 import processing.core.PImage;
00021 
00022 import edu.tum.cs.ias.knowrob.prolog.PrologInterface;
00023 import edu.tum.cs.ias.knowrob.utils.ResourceRetriever;
00024 import edu.tum.cs.ias.knowrob.vis.Canvas;
00025 
00026 public class OccupancyGridMap extends Item {
00027 
00028         private String map = null;
00029         private String img = null;
00030         
00031         private PImage mapImg = null;
00032         
00033         private double ymlResolution;
00034         private Vector3d ymlOrigin;
00035         private boolean ymlNegate;
00036         
00037         public OccupancyGridMap(String identifier)
00038         {
00039                 super(null,null);
00040                 
00041                 HashMap<String, Vector<String>> nfo = PrologInterface
00042                                 .executeQuery(
00043                                                 "rdf_has("+identifier+",'http://www.roboearth.org/kb/roboearth.owl#linkToMapFile',literal(Map)),rdf_has("+identifier+",'http://www.roboearth.org/kb/roboearth.owl#linkToImageFile',literal(Img))");
00044                 
00045                 if(nfo!=null) {
00046                         if (nfo.get("Map") != null && nfo.get("Map").size() > 0) {
00047                                 map = PrologInterface.removeSingleQuotes(nfo.get("Map").get(0));
00048                         }
00049                         if (nfo.get("Img") != null && nfo.get("Img").size() > 0) {
00050                                 img = PrologInterface.removeSingleQuotes(nfo.get("Img").get(0));
00051                         }
00052 
00053                         //Parse Yaml
00054 
00055                         if (!parseYaml(ResourceRetriever.retrieve(map)))
00056                         {
00057                                 System.err.println("Couldn't parse YAML file for " + identifier);
00058                                 return;
00059                         }
00060 
00061                         //Init image
00062                         File imgFile = ResourceRetriever.retrieve(img);
00063 
00064                         BufferedImage bimg = null;
00065                         try {
00066                                 bimg = ImageIO.read(imgFile);
00067                         } catch (IOException e) {
00068                                 System.err.println("Couldn't read file: " + imgFile.getAbsolutePath());
00069                                 e.printStackTrace();
00070                         }       
00071 
00072                         try
00073                         {
00074                                 if (ymlNegate)
00075                                 {
00076                                         RescaleOp op = new RescaleOp(-1.0f, 255f, null);
00077                                         bimg = op.filter(bimg, null);
00078                                 }
00079 
00080                                 // Convert BufferedImage to Image otherwise PImage constructor will fail!!
00081                                 Image i = bimg.getScaledInstance(bimg.getWidth(), bimg.getHeight(),0);
00082                                 mapImg = new PImage(i);
00083                         } catch (Exception e) {
00084                                 e.printStackTrace();
00085                                 System.err.println("Couln't initialize image for: " + identifier);
00086                         }
00087                 }
00088         }
00089         
00090         private boolean parseYaml(File file)
00091         {
00092                 Yaml yaml = new Yaml();
00093                 try {
00094                         Object parsed = yaml.load(new FileReader(file));
00095                         if (parsed.getClass() != LinkedHashMap.class)
00096                         {
00097                                 System.err.println("Invalid yaml object: " + parsed.getClass());
00098                                 return false;
00099                         } else {
00100                                 @SuppressWarnings("unchecked")
00101                                 LinkedHashMap<Object, Object> map = (LinkedHashMap<Object, Object>) parsed;
00102                                 
00103                                 ymlResolution = 0;
00104                                 ymlOrigin = null;
00105                                 ymlNegate = false;
00106                                 
00107                                 for (Object k : map.keySet())
00108                                 {
00109                                         if (k.getClass() == String.class)
00110                                         {
00111                                                 String key = (String)k;
00112                                                 
00113                                                 if (key.equals("resolution"))
00114                                                 {
00115                                                         ymlResolution = (Double)map.get(key);
00116                                                 }
00117                                                 else if (key.equals("origin"))
00118                                                 {
00119                                                         @SuppressWarnings("unchecked")
00120                                                         ArrayList<Double> par = (ArrayList<Double>)map.get(key);
00121                                                         ymlOrigin = new Vector3d(par.get(0),par.get(1),par.get(2));
00122                                                 }
00123                                                 else if (key.equals("negate"))
00124                                                 {
00125                                                         ymlNegate = (((Integer)map.get(key))==1);
00126                                                 }
00127                                         }
00128                                 }
00129                                 return (ymlOrigin != null && ymlResolution != 0);
00130                                 
00131                         }
00132                         
00133                 } catch (FileNotFoundException e) {
00134                         System.err.println("File not found for OccupancyGridMap: " + file.getAbsolutePath());
00135                         return false;
00136                 } catch (Exception e) {
00137                         e.printStackTrace();
00138                         return false;
00139                 }
00140         }
00141         
00142         @Override
00143         protected void drawIt(Canvas c) {
00144                 if (mapImg == null || ymlOrigin == null)
00145                         return;
00146                 
00147                 float scale = (float)ymlResolution;
00148                 c.pushMatrix();
00149                 c.scale(scale);
00150                 c.image(mapImg, (float)ymlOrigin.x*1/scale,(float)ymlOrigin.y*1/scale);
00151                 c.popMatrix();
00152         }
00153 
00154 }


mod_vis
Author(s): Moritz Tenorth, Jakob Engel
autogenerated on Sat Dec 28 2013 17:09:49