| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | #include <stdlib.h> |
|---|
| 17 | #include <stdio.h> |
|---|
| 18 | #include <string.h> |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | #include <trpage_geom.h> |
|---|
| 27 | #include <trpage_read.h> |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | trpgHeader::trpgHeader() |
|---|
| 35 | { |
|---|
| 36 | Reset(); |
|---|
| 37 | } |
|---|
| 38 | trpgHeader::~trpgHeader() |
|---|
| 39 | { |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | bool trpgHeader::isValid() const |
|---|
| 44 | { |
|---|
| 45 | |
|---|
| 46 | if((verMajor>=TRPG_NOMERGE_VERSION_MAJOR) && (verMinor>=TRPG_NOMERGE_VERSION_MINOR)) |
|---|
| 47 | { |
|---|
| 48 | return true; |
|---|
| 49 | } |
|---|
| 50 | else { |
|---|
| 51 | if (numLods <= 0) |
|---|
| 52 | { |
|---|
| 53 | strcpy(errMess, "Number of LOD <= 0"); |
|---|
| 54 | return false; |
|---|
| 55 | } |
|---|
| 56 | if (sw.x == ne.x && sw.y == ne.y) |
|---|
| 57 | { |
|---|
| 58 | strcpy(errMess, "Mbr is invalid"); |
|---|
| 59 | |
|---|
| 60 | return false; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | return true; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | void trpgHeader::Reset() |
|---|
| 69 | { |
|---|
| 70 | |
|---|
| 71 | verMinor = TRPG_VERSION_MINOR; |
|---|
| 72 | verMajor = TRPG_VERSION_MAJOR; |
|---|
| 73 | dbVerMinor = 0; |
|---|
| 74 | dbVerMajor = 0; |
|---|
| 75 | origin = trpg3dPoint(0,0,0); |
|---|
| 76 | sw = ne = trpg2dPoint(0,0); |
|---|
| 77 | tileType = DatabaseLocal; |
|---|
| 78 | |
|---|
| 79 | numLods = 0; |
|---|
| 80 | lodSizes.resize(0); |
|---|
| 81 | lodRanges.resize(0); |
|---|
| 82 | tileSize.resize(0); |
|---|
| 83 | maxGroupID = -1; |
|---|
| 84 | flags = 0; |
|---|
| 85 | errMess[0] = '\0'; |
|---|
| 86 | cols = -1; |
|---|
| 87 | rows = -1; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | void trpgHeader::SetVersion(int32 vmaj,int32 vmin) |
|---|
| 92 | { |
|---|
| 93 | verMinor = vmin; |
|---|
| 94 | verMajor = vmaj; |
|---|
| 95 | } |
|---|
| 96 | void trpgHeader::SetDbVersion(int32 vmaj,int32 vmin) |
|---|
| 97 | { |
|---|
| 98 | dbVerMinor = vmin; |
|---|
| 99 | dbVerMajor = vmaj; |
|---|
| 100 | } |
|---|
| 101 | void trpgHeader::SetTileSize(int id,const trpg2dPoint &pt) |
|---|
| 102 | { |
|---|
| 103 | if (id < 0 || id >= (int)tileSize.size()) return; |
|---|
| 104 | tileSize[id] = pt; |
|---|
| 105 | } |
|---|
| 106 | void trpgHeader::SetOrigin(const trpg3dPoint &pt) |
|---|
| 107 | { |
|---|
| 108 | origin = pt; |
|---|
| 109 | } |
|---|
| 110 | void trpgHeader::SetExtents(const trpg2dPoint &in_sw,const trpg2dPoint &in_ne) |
|---|
| 111 | { |
|---|
| 112 | sw = in_sw; |
|---|
| 113 | ne = in_ne; |
|---|
| 114 | } |
|---|
| 115 | void trpgHeader::SetTileOriginType(trpgTileType type) |
|---|
| 116 | { |
|---|
| 117 | tileType = type; |
|---|
| 118 | } |
|---|
| 119 | void trpgHeader::SetNumLods(int no) |
|---|
| 120 | { |
|---|
| 121 | if (no < 0) return; |
|---|
| 122 | numLods = no; |
|---|
| 123 | |
|---|
| 124 | lodSizes.resize(no); |
|---|
| 125 | lodRanges.resize(no); |
|---|
| 126 | } |
|---|
| 127 | void trpgHeader::SetLodSize(int no,const trpg2iPoint &pt) |
|---|
| 128 | { |
|---|
| 129 | if (no < 0 || no >= numLods) |
|---|
| 130 | return; |
|---|
| 131 | |
|---|
| 132 | lodSizes[no] = pt; |
|---|
| 133 | } |
|---|
| 134 | void trpgHeader::SetLodSize(const trpg2iPoint *pt) |
|---|
| 135 | { |
|---|
| 136 | for (int i=0;i<numLods;i++) |
|---|
| 137 | lodSizes[i] = pt[i]; |
|---|
| 138 | } |
|---|
| 139 | void trpgHeader::SetLodRange(int no,float64 r) |
|---|
| 140 | { |
|---|
| 141 | if (no < 0 || no >= numLods) |
|---|
| 142 | return; |
|---|
| 143 | |
|---|
| 144 | lodRanges[no] = r; |
|---|
| 145 | } |
|---|
| 146 | void trpgHeader::SetLodRange(const float64 *r) |
|---|
| 147 | { |
|---|
| 148 | for (int i=0;i<numLods;i++) |
|---|
| 149 | lodRanges[i] = r[i]; |
|---|
| 150 | } |
|---|
| 151 | void trpgHeader::AddLod(const trpg2iPoint &pt,const trpg2dPoint &sz,float64 r) |
|---|
| 152 | { |
|---|
| 153 | lodRanges.push_back(r); |
|---|
| 154 | lodSizes.push_back(pt); |
|---|
| 155 | tileSize.push_back(sz); |
|---|
| 156 | numLods++; |
|---|
| 157 | } |
|---|
| 158 | void trpgHeader::SetLod(const trpg2iPoint &pt,const trpg2dPoint &sz,float64 r,unsigned int lod) |
|---|
| 159 | { |
|---|
| 160 | if (lodRanges.size()<=lod) |
|---|
| 161 | lodRanges.resize(lod+1); |
|---|
| 162 | lodRanges[lod]=r; |
|---|
| 163 | if (lodSizes.size()<=lod) |
|---|
| 164 | lodSizes.resize(lod+1); |
|---|
| 165 | lodSizes[lod]=pt; |
|---|
| 166 | if (tileSize.size()<=lod) |
|---|
| 167 | tileSize.resize(lod+1); |
|---|
| 168 | tileSize[lod]=sz; |
|---|
| 169 | if (numLods<=static_cast<int>(lod)) |
|---|
| 170 | numLods=lod+1; |
|---|
| 171 | } |
|---|
| 172 | void trpgHeader::SetMaxGroupID(int id) |
|---|
| 173 | { |
|---|
| 174 | maxGroupID = id; |
|---|
| 175 | } |
|---|
| 176 | int trpgHeader::AddGroupID(void) |
|---|
| 177 | { |
|---|
| 178 | maxGroupID++; |
|---|
| 179 | return maxGroupID; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | bool trpgHeader::Write(trpgWriteBuffer &buf) |
|---|
| 184 | { |
|---|
| 185 | |
|---|
| 186 | if (!isValid()) |
|---|
| 187 | return false; |
|---|
| 188 | |
|---|
| 189 | buf.Begin(TRPGHEADER); |
|---|
| 190 | buf.Add((int32)verMajor); |
|---|
| 191 | buf.Add((int32)verMinor); |
|---|
| 192 | buf.Add((int32)dbVerMajor); |
|---|
| 193 | buf.Add((int32)dbVerMinor); |
|---|
| 194 | buf.Add(origin); |
|---|
| 195 | buf.Add(sw); |
|---|
| 196 | buf.Add(ne); |
|---|
| 197 | buf.Add((uint8)tileType); |
|---|
| 198 | |
|---|
| 199 | buf.Add((int32)numLods); |
|---|
| 200 | |
|---|
| 201 | buf.Begin(TRPGHEAD_LODINFO); |
|---|
| 202 | for (int i=0;i<numLods;i++) { |
|---|
| 203 | buf.Add(lodSizes[i]); |
|---|
| 204 | buf.Add(lodRanges[i]); |
|---|
| 205 | buf.Add(tileSize[i]); |
|---|
| 206 | } |
|---|
| 207 | buf.End(); |
|---|
| 208 | |
|---|
| 209 | buf.Add(maxGroupID); |
|---|
| 210 | |
|---|
| 211 | if((verMajor >= TRPG_NOMERGE_VERSION_MAJOR) && (verMinor >=TRPG_NOMERGE_VERSION_MINOR)) { |
|---|
| 212 | buf.Add(flags); |
|---|
| 213 | buf.Add(rows); |
|---|
| 214 | buf.Add(cols); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | buf.End(); |
|---|
| 218 | |
|---|
| 219 | return true; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | bool trpgHeader::GetVersion(int32 &vmaj,int32 &vmin) const |
|---|
| 228 | { |
|---|
| 229 | if (!isValid()) return false; |
|---|
| 230 | vmin = verMinor; |
|---|
| 231 | vmaj = verMajor; |
|---|
| 232 | return true; |
|---|
| 233 | } |
|---|
| 234 | bool trpgHeader::GetDbVersion(int32 &vmaj,int32 &vmin) const |
|---|
| 235 | { |
|---|
| 236 | if (!isValid()) return false; |
|---|
| 237 | vmaj = dbVerMajor; |
|---|
| 238 | vmin = dbVerMinor; |
|---|
| 239 | return true; |
|---|
| 240 | } |
|---|
| 241 | bool trpgHeader::GetTileSize(int id,trpg2dPoint &pt) const |
|---|
| 242 | { |
|---|
| 243 | if (!isValid()) return false; |
|---|
| 244 | if (id < 0 || id >= (int)tileSize.size()) return false; |
|---|
| 245 | pt = tileSize[id]; |
|---|
| 246 | return true; |
|---|
| 247 | } |
|---|
| 248 | bool trpgHeader::GetOrigin(trpg3dPoint &pt) const |
|---|
| 249 | { |
|---|
| 250 | if (!isValid()) return false; |
|---|
| 251 | pt = origin; |
|---|
| 252 | return true; |
|---|
| 253 | } |
|---|
| 254 | bool trpgHeader::GetTileOriginType(trpgTileType &type) const |
|---|
| 255 | { |
|---|
| 256 | if (!isValid()) return false; |
|---|
| 257 | type = tileType; |
|---|
| 258 | return true; |
|---|
| 259 | } |
|---|
| 260 | bool trpgHeader::GetNumLods(int32 &no) const |
|---|
| 261 | { |
|---|
| 262 | if (!isValid()) return false; |
|---|
| 263 | no = numLods; |
|---|
| 264 | return true; |
|---|
| 265 | } |
|---|
| 266 | bool trpgHeader::GetLodSize(int32 id,trpg2iPoint &pt) const |
|---|
| 267 | { |
|---|
| 268 | if (!isValid() || (id < 0 || id >= numLods)) return false; |
|---|
| 269 | pt = lodSizes[id]; |
|---|
| 270 | return true; |
|---|
| 271 | } |
|---|
| 272 | bool trpgHeader::GetLodRange(int32 id,float64 &range) const |
|---|
| 273 | { |
|---|
| 274 | if (!isValid() || (id < 0 || id >= numLods)) return false; |
|---|
| 275 | range = lodRanges[id]; |
|---|
| 276 | return true; |
|---|
| 277 | } |
|---|
| 278 | bool trpgHeader::GetExtents(trpg2dPoint &osw,trpg2dPoint &one) const |
|---|
| 279 | { |
|---|
| 280 | if (!isValid()) return false; |
|---|
| 281 | osw = sw; |
|---|
| 282 | one = ne; |
|---|
| 283 | return true; |
|---|
| 284 | } |
|---|
| 285 | bool trpgHeader::GetMaxGroupID(int &id) const |
|---|
| 286 | { |
|---|
| 287 | id = maxGroupID; |
|---|
| 288 | return true; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | bool trpgHeader::Read(trpgReadBuffer &buf) |
|---|
| 293 | { |
|---|
| 294 | uint8 i8; |
|---|
| 295 | trpgToken tok; |
|---|
| 296 | bool status; |
|---|
| 297 | int32 len; |
|---|
| 298 | |
|---|
| 299 | try { |
|---|
| 300 | buf.Get(verMajor); |
|---|
| 301 | buf.Get(verMinor); |
|---|
| 302 | buf.Get(dbVerMajor); |
|---|
| 303 | buf.Get(dbVerMinor); |
|---|
| 304 | buf.Get(origin); |
|---|
| 305 | buf.Get(sw); |
|---|
| 306 | buf.Get(ne); |
|---|
| 307 | buf.Get(i8); tileType = (trpgTileType)i8; |
|---|
| 308 | buf.Get(numLods); |
|---|
| 309 | if (numLods < 0) throw 1; |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | buf.GetToken(tok,len); |
|---|
| 313 | if (tok != TRPGHEAD_LODINFO) throw 1; |
|---|
| 314 | buf.PushLimit(len); |
|---|
| 315 | status = ReadLodInfo(buf); |
|---|
| 316 | buf.PopLimit(); |
|---|
| 317 | if (!status) throw 1; |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | buf.Get(maxGroupID); |
|---|
| 321 | if((verMajor >= TRPG_NOMERGE_VERSION_MAJOR) && (verMinor >=TRPG_NOMERGE_VERSION_MINOR)) { |
|---|
| 322 | buf.Get(flags); |
|---|
| 323 | buf.Get(rows); |
|---|
| 324 | buf.Get(cols); |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | catch (...) { |
|---|
| 329 | return false; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | return isValid(); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | bool trpgHeader::ReadLodInfo(trpgReadBuffer &buf) |
|---|
| 337 | { |
|---|
| 338 | float64 range; |
|---|
| 339 | trpg2iPoint pt; |
|---|
| 340 | trpg2dPoint sz; |
|---|
| 341 | |
|---|
| 342 | try { |
|---|
| 343 | for (int i=0;i<numLods;i++) { |
|---|
| 344 | buf.Get(pt); |
|---|
| 345 | buf.Get(range); |
|---|
| 346 | buf.Get(sz); |
|---|
| 347 | lodSizes.push_back(pt); |
|---|
| 348 | lodRanges.push_back(range); |
|---|
| 349 | tileSize.push_back(sz); |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | catch (...) { |
|---|
| 353 | return false; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | return true; |
|---|
| 357 | } |
|---|