Skip to content

Commit

Permalink
topwriter use topreader (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed May 10, 2024
1 parent 49821be commit df635d8
Showing 1 changed file with 2 additions and 119 deletions.
121 changes: 2 additions & 119 deletions extensions/topwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,16 @@
# (at your option) any later version.
# --------------------------------------------------------------------

import io
import copy
import struct
import time
from typing import List, Iterable, Callable
from typing import List, Iterable
from pathlib import Path
from collections import defaultdict

from topreader import (
EXTEND_RIGHT,
EXTEND_LEFT,
KEY_EXTEND,
KEY_TAPE,
KEY_DECLINATION,
distmm_inv,
adegrees_inv,
_make_Point_inv,
PTICKS,
NANOSEC,
dump,
load,
)

Expand Down Expand Up @@ -79,114 +70,6 @@
}


def _write_date(tripdate: time.struct_time) -> Iterable[bytes]:
ticks = int((time.mktime(tripdate) + PTICKS) * NANOSEC)
yield struct.pack('<Q', ticks)


def _write_comment(comment) -> Iterable[bytes]:
commentlength = 0
yield struct.pack('<B', commentlength)


def _write_trip(trip) -> Iterable[bytes]:
yield from _write_date(trip["date"])
yield from _write_comment(trip["comment"])
yield struct.pack('<H', adegrees_inv(trip[KEY_DECLINATION]))


def _write_shot(shot) -> Iterable[bytes]:
yield _write_station(shot["from"])
yield _write_station(shot["to"])

Dist = distmm_inv(shot[KEY_TAPE])
yield struct.pack('<L', Dist)

azimuth = adegrees_inv(shot['compass'])
yield struct.pack('<H', azimuth)

inclination = adegrees_inv(shot['clino'])
yield struct.pack('<h', inclination)

flags = 0

if shot.get(KEY_EXTEND, EXTEND_RIGHT) == EXTEND_LEFT:
flags |= 0b00000001

comment = shot.get('comment', '')
if comment:
# flags |= 0b00000010
pass

yield struct.pack('<B', flags)

rawroll = adegrees_inv(shot['rollangle'], 0xFF)
yield struct.pack('<B', rawroll)

tripindex = shot['trip']
yield struct.pack('<h', tripindex)

if comment:
# yield from _write_comments(comment)
pass


def _write_reference(ref: list) -> Iterable[bytes]:
raise NotImplementedError


def _write_station(shot: str) -> bytes:
idm, dot, idd = shot.rpartition(".")
if dot:
iidd, iidm = int(idd), int(idm)
elif shot:
iidd, iidm = int(idd) + 1, 0x8000
else:
assert not (idd or idm)
iidd, iidm = 0, 0x8000
return struct.pack('<HH', iidd, iidm)


def _write_mapping(mapping: dict) -> Iterable[bytes]:
x, y = _make_Point_inv(*mapping['center'])
yield struct.pack('<iii', x, y, mapping['scale'])


def _write_drawing(drawing: dict) -> Iterable[bytes]:
yield from _write_mapping(drawing["transform"])
# TODO
yield struct.pack('<B', 0)


def _write_one(fp, sec, writefunc: Callable):
for chunk in writefunc(sec):
fp.write(chunk)


def _write_fsection(fp, section: list, writefunc: Callable):
fp.write(struct.pack('<L', len(section)))
for sec in section:
_write_one(fp, sec, writefunc)


def dump(top: dict, fp):
fp.write(b"Top")
fp.write(struct.pack('B', top['version']))
_write_fsection(fp, top["trips"], _write_trip)
_write_fsection(fp, top["shots"], _write_shot)
_write_fsection(fp, top["ref"], _write_reference)
_write_one(fp, top["transform"], _write_mapping)
_write_one(fp, top["outline"], _write_drawing)
_write_one(fp, top["sideview"], _write_drawing)
fp.write(b'\x00\x00\x00\x00')


def dumps(top: dict) -> bytes:
fp = io.BytesIO()
dump(top, fp)
return fp.getvalue()


def dim_to_float(dim: str) -> float:
dim = dim.strip()
return 0.0 if dim in ("", "*") else float(dim)
Expand Down

0 comments on commit df635d8

Please sign in to comment.