View Javadoc

1   package cz.cuni.amis.pogamut.defcon.communication.worldview.polygons.loaders.borders;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileReader;
6   import java.io.IOException;
7   import java.util.ArrayList;
8   import java.util.LinkedList;
9   import java.util.List;
10  import java.util.logging.Logger;
11  import java.util.regex.Matcher;
12  import java.util.regex.Pattern;
13  
14  import cz.cuni.amis.pogamut.defcon.base3d.worldview.object.DefConLocation;
15  
16  /**
17   * Loads borders (contours) from a given path.
18   * 
19   * @author Radek 'Black_Hand' Pibil
20   * 
21   */
22  public class FilePrecomputedBordersLoader implements IPrecomputedBordersLoader {
23  
24  	private String pathString;
25  	private File path;
26  	private Pattern pattern = Pattern.compile("[-0-9\\.]+");
27  	private Logger log;
28  
29  	public FilePrecomputedBordersLoader(String pathString) {
30  		this(pathString, null);
31  	}
32  
33  	public FilePrecomputedBordersLoader(String pathString, Logger log) {
34  		this.pathString = pathString;
35  		this.path = new File(pathString);
36  		this.log = log;
37  
38  		if (!this.path.isDirectory())
39  			throw new IllegalArgumentException(
40  					"FilePrecomputedBordersSaver: pathString has to point to a directory");
41  	}
42  
43  
44  
45  	@Override
46  	public List<List<DefConLocation>> loadBordersForTerritory(String fileName)
47  			throws NumberFormatException, IOException {
48  
49  		if (log != null) {
50  			log.info("path: " + path.getAbsolutePath()
51  				+ "\\" + fileName);
52  		}
53  		BufferedReader input = new BufferedReader(new FileReader(new File(
54  				path, fileName)));
55  
56  		String line;
57  		ArrayList<List<DefConLocation>> territory = new ArrayList<List<DefConLocation>>();
58  		double[] singleLocation = new double[3];
59  
60  		while ((line = input.readLine()) != null) {
61  			Matcher match = pattern.matcher(line);
62  			LinkedList<DefConLocation> border = new LinkedList<DefConLocation>();
63  			
64  			int i = 0;
65  			while (match.find()) {
66  								
67  				String member = match.group();
68  
69  				singleLocation[i] = Double.parseDouble(member);
70  				i = (i + 1) % 3;
71  
72  				if (i == 0) {
73  					border.add(new DefConLocation(singleLocation));
74  					singleLocation = new double[3];
75  				}
76  			}
77  			territory.add(border);
78  		}
79  		return territory;
80  	}
81  
82  	public String getPathString() {
83  		return pathString;
84  	}
85  
86  }