Debian Codegraph @

src/uk/me/parabola/imgfmt/app/net/NODFile.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: 06-Jul-2008
16 */
17 package uk.me.parabola.imgfmt.app.net;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import uk.me.parabola.imgfmt.app.BufferedImgFileReader;
23 import uk.me.parabola.imgfmt.app.BufferedImgFileWriter;
24 import uk.me.parabola.imgfmt.app.ImgFile;
25 import uk.me.parabola.imgfmt.app.ImgFileWriter;
26 import uk.me.parabola.imgfmt.app.Section;
27 import uk.me.parabola.imgfmt.app.SectionWriter;
28 import uk.me.parabola.imgfmt.fs.ImgChannel;
29 import uk.me.parabola.log.Logger;
30
31 /**
32 * The NOD file that contains routing information.
33 *
34 * NOD1 contains several groups of routing nodes.
35 * NOD2 contains road data with links into NOD1.
36 *
37 * NOD1 contains links back to NET (and NET contains links to NOD2). So there
38 * is a loop and we have to write one section first, retaining the offsets
39 * and then go back and fill in offsets that were found later.
40 *
41 * I'm choosing to this with Table A, as the records are fixed size and so
42 * we can write them blank the first time and then go back and fix them
43 * up, once the NET offsets are known.
44 *
45 * So we are writing NOD first before NET and NOD1 before NOD2. Once NET is
46 * written then go back to Table A and fix the label offsets in RGN.
47 *
48 * @author Steve Ratcliffe
49 */
50 public class NODFile extends ImgFile {
51 private static final Logger log = Logger.getLogger(NODFile.class);
52
53 private final NODHeader nodHeader = new NODHeader();
54
55 private List<RouteCenter> centers = new ArrayList<>();
56 private List<RoadDef> roads = new ArrayList<>();
57 private List<RouteNode> boundaryNodes = new ArrayList<>();
58
59 public NODFile(ImgChannel chan, boolean write) {
60 setHeader(nodHeader);
61 if (write) {
62 setWriter(new BufferedImgFileWriter(chan, "NOD"));
63 position(NODHeader.HEADER_LEN);
64 } else {
65 setReader(new BufferedImgFileReader(chan));
66 nodHeader.readHeader(getReader());
67 }
68 }
69
70 public void write() {
71 writeNodes();
72 writeRoadData();
73 writeBoundary();
74 writeHighClassBoundary();
75 }
76
77 public void writePost() {
78 ImgFileWriter writer = new SectionWriter(getWriter(), nodHeader.getNodeSection());
79
80 for (RouteCenter rc : centers) {
81 rc.writePost(writer);
82 }
83 // Refresh the header
84 position(0);
85 getHeader().writeHeader(getWriter());
86 }
87
88 /**
89 * Write the nodes (NOD 1). This is done first as the offsets into
90 * this section are needed to write NOD2.
91 */
92 private void writeNodes() {
93 ImgFileWriter writer = getWriter();
94 nodHeader.setNodeStart(writer.position());
95
96 Section section = nodHeader.getNodeSection();
97 writer = new SectionWriter(writer, section);
98
99 int[] classBoundaries = nodHeader.getClassBoundaries();
100 for (RouteCenter cp : centers){
101 cp.write(writer, classBoundaries);
102 }
103 for (int i = 4; i >= 0; --i){
104 if (classBoundaries[i] > writer.position())
105 classBoundaries[i] = writer.position();
106 }
107 nodHeader.setNodeSize(writer.position());
108 log.debug("the nod offset", Integer.toHexString(getWriter().position()));
109 Section.close(writer);
110 }
111
112 /**
113 * Write the road data (NOD2).
114 */
115 private void writeRoadData() {
116 log.info("writeRoadData");
117
118 ImgFileWriter writer = new SectionWriter(getWriter(), nodHeader.getRoadSection());
119
120 boolean debug = log.isDebugEnabled();
121 for (RoadDef rd : roads) {
122 if(debug)
123 log.debug("wrting nod2", writer.position());
124 rd.writeNod2(writer);
125 }
126 if(debug)
127 log.debug("ending nod2", writer.position());
128 nodHeader.setRoadSize(writer.position());
129 }
130
131 /**
132 * Write the boundary node table (NOD3).
133 */
134 private void writeBoundary() {
135 log.info("writeBoundary");
136
137 ImgFileWriter writer = new SectionWriter(getWriter(), nodHeader.getBoundarySection());
138
139 boolean debug = log.isDebugEnabled();
140 for (RouteNode node : boundaryNodes) {
141 if(debug)
142 log.debug("wrting nod3", writer.position());
143 node.writeNod3OrNod4(writer);
144 }
145 if(debug)
146 log.debug("ending nod3", writer.position());
147 nodHeader.setBoundarySize(writer.position());
148 }
149
150 /**
151 * Write the high class boundary node table (NOD4).
152 * Like NOD3, but contains only nodes on roads with class > 0
153 */
154 private void writeHighClassBoundary() {
155 log.info("writeBoundary");
156
157 Section section = nodHeader.getHighClassBoundary();
158 int pos = section.getPosition();
159 pos = (pos + 0x200) & ~0x1ff; // align on 0x200
160 int numBytesToWrite = pos - section.getPosition();
161 for (int i = 0; i < numBytesToWrite; i++)
162 getWriter().put1u(0);
163 section.setPosition(pos);
164 ImgFileWriter writer = new SectionWriter(getWriter(), section);
165
166 boolean debug = log.isDebugEnabled();
167 for (RouteNode node : boundaryNodes) {
168 if (node.getNodeClass() == 0)
169 continue;
170 if(debug)
171 log.debug("wrting nod4", writer.position());
172 node.writeNod3OrNod4(writer);
173 }
174 if(debug)
175 log.debug("ending nod4", writer.position());
176 nodHeader.setHighClassBoundarySize(writer.position());
177 }
178
179 public void setNetwork(List<RouteCenter> centers, List<RoadDef> roads, List<RouteNode> boundary) {
180 this.centers = centers;
181 this.roads = roads;
182 this.boundaryNodes = new ArrayList<>(boundary);
183 this.boundaryNodes.sort(null);
184 }
185
186 public void setDriveOnLeft(boolean dol) {
187 nodHeader.setDriveOnLeft(dol);
188 }
189 }