-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parses navvis origin file and converts into Capture::Lamar csv file #65
Merged
Merged
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
102a77f
Add origin parser one test
vjlux f856951
remove
vjlux 0353607
More tests
vjlux 47bd9b7
origin
vjlux ec3f7ba
Merge remote-tracking branch 'origin/main' into user/lugruber/parse_o…
vjlux 1199b52
Revert changes and move to GlobalAlignment as datastructure
vjlux f1050ea
remove test file
vjlux 8dbb94a
saving
vjlux 0cc1407
Remove test
vjlux 50b1a6b
Add tests at parser
vjlux 3013240
haha
vjlux b2fa8e1
fix dict access
vjlux 7a6731b
Rename
vjlux b8b5fab
Tests run now
vjlux 96b61fb
CR
vjlux cd341e5
Info
vjlux f0ee3c7
fix path for origin
vjlux 2fcb451
Code formatting
vjlux a35b4ad
Comments
vjlux 5ad88b2
Add right path
5997c08
Fix return
f8fda6c
Seddling down on adding a separate navvis origins.txt into the NavVis…
283260a
dang
759386d
Cleanup
cfdae9b
CR comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
from .pose import Pose | ||
from .proc import Proc | ||
from .misc import KeyType | ||
from .namedposes import NamedPoses |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from pathlib import Path | ||
from .pose import Pose | ||
from ..utils.io import read_csv, write_csv | ||
|
||
|
||
class NamedPoses(dict): | ||
def __setitem__(self, name: str, pose: Pose): | ||
if not isinstance(name, str): | ||
raise TypeError('expect str type as name') | ||
if not isinstance(pose, Pose): | ||
raise TypeError('expect Pose type as pose') | ||
super().__setitem__(name, pose) | ||
|
||
def load(self, path: Path) -> 'NamedPoses': | ||
table = read_csv(path) | ||
for name, *qt in table: | ||
self[name] = Pose.from_list(qt) | ||
|
||
def save(self, path: Path): | ||
columns = ['name', 'qw', 'qx', 'qy', 'qz', 'tx', 'ty', 'tz'] | ||
table = [] | ||
for name, pose in self.items(): | ||
table.append([name] + pose.to_list()) | ||
write_csv(path, table, columns=columns) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
UNKNOWN_CRS_NAME = 'UNKNOWN' | ||
|
||
def is_navvis_origin_valid(navvis_origin : dict): | ||
""" | ||
Check if the NavVis origin dictionary is valid | ||
:param navvis_origin: NavVis origin dictionary | ||
:return: True if valid, False otherwise | ||
:rtype: bool | ||
""" | ||
if navvis_origin['Pose']['position']['x'] is None or \ | ||
navvis_origin['Pose']['position']['y'] is None or \ | ||
navvis_origin['Pose']['position']['z'] is None or \ | ||
navvis_origin['Pose']['orientation']['w'] is None or \ | ||
navvis_origin['Pose']['orientation']['x'] is None or \ | ||
navvis_origin['Pose']['orientation']['y'] is None or \ | ||
navvis_origin['Pose']['orientation']['z'] is None: | ||
return False | ||
return True | ||
|
||
def parse_navvis_origin_file(file_path : Path): | ||
""" | ||
Read NavVis Origin File Format Version 1.0 | ||
:param file_path: Path to the file | ||
:return: NavVis anchor origin dictionary | ||
:rtype: Dict | ||
""" | ||
if not file_path.exists(): | ||
print(f"Warning: Origin '{file_path}' does not exist.") | ||
return {} | ||
|
||
try: | ||
with file_path.open() as f: | ||
origin = json.load(f) | ||
if not is_navvis_origin_valid(origin): | ||
print("Invalid origin.json file", json.dumps(origin, indent=4)) | ||
return origin | ||
except Exception as e: | ||
print("Warning Failed reading origin.json file.", e) | ||
vjlux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return {} | ||
|
||
def convert_navvis_origin_to_csv(navvis_origin : dict): | ||
vjlux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
csv_str = "# CRS, qw, qx, qy, qz, tx, ty, tz\n" | ||
|
||
if 'CRS' in navvis_origin: | ||
crs = navvis_origin['CRS'] | ||
else: | ||
crs = UNKNOWN_CRS_NAME | ||
|
||
position = navvis_origin['Pose']['position'] | ||
orientation = navvis_origin['Pose']['orientation'] | ||
|
||
csv_str += (f"{crs}," | ||
f"{orientation['w']}," | ||
f"{orientation['x']}," | ||
f"{orientation['y']}," | ||
f"{orientation['z']}," | ||
f"{position['x']}," | ||
f"{position['y']}," | ||
f"{position['z']}\n") | ||
return csv_str | ||
|
||
|
||
vjlux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pytest | ||
import numpy as np | ||
import os | ||
from scipy.spatial.transform import Rotation | ||
|
||
from ..capture import Pose | ||
from ..capture import NamedPoses | ||
|
||
def Rz(theta): | ||
return Rotation.from_euler('z', theta, degrees=True) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"rot,tvec", | ||
[ | ||
(['1', '0', '0', '0'], ['0', '1', '1']), | ||
(Rz(30), ['1', '1', '0']) | ||
]) | ||
def test_pose_valid(rot, tvec, tmp_path): | ||
temp_file_path = tmp_path / 'named_poses.csv' | ||
pose = Pose(rot, tvec) | ||
poses = NamedPoses() | ||
poses['pose1'] = pose | ||
poses['pose2'] = pose | ||
poses.save(temp_file_path) | ||
loaded_poses = NamedPoses() | ||
loaded_poses.load(temp_file_path) | ||
assert len(poses) == len(loaded_poses) | ||
assert poses.keys() == loaded_poses.keys() | ||
for k in poses.keys(): | ||
assert poses[k].to_list() == loaded_poses[k].to_list() | ||
os.remove(temp_file_path) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" Tests for origin_parse.py """ | ||
import pytest | ||
import json | ||
import os | ||
|
||
from ..scanners.navvis import origin_parser | ||
|
||
@pytest.mark.parametrize("nominal_origin, expected_output_csv", [({ | ||
"CRS": "EPSG:25834", | ||
"Pose": { | ||
"orientation": { | ||
"w": 0.8, | ||
"x": 0, | ||
"y": 0, | ||
"z": -0.5 | ||
}, | ||
"position": { | ||
"x": 6.3, | ||
"y": 2.4, | ||
"z": 99.95 | ||
} | ||
}}, | ||
"# CRS, qw, qx, qy, qz, \ | ||
tx, ty, tz\n\ | ||
EPSG:25834,0.8,0,0,-0.5,\ | ||
6.3,2.4,99.95\n"), | ||
({ | ||
"Pose": { | ||
"orientation": { | ||
"w": 0.5, | ||
"x": 0, | ||
"y": 0, | ||
"z": 0 | ||
}, | ||
"position": { | ||
"x": 0, | ||
"y": 0, | ||
"z": 0 | ||
} | ||
} | ||
}, "# CRS, qw, qx, qy, qz, tx, ty, tz\n" + origin_parser.UNKNOWN_CRS_NAME + ",0.5,0,0,0,0,0,0\n"), | ||
]) | ||
def test_parse_navvis_origin(nominal_origin, expected_output_csv, tmp_path): | ||
temp_origin_path = tmp_path / "input_data.json" | ||
with open(temp_origin_path, 'w') as file: | ||
json.dump(nominal_origin, file) | ||
origin = origin_parser.parse_navvis_origin_file(temp_origin_path) | ||
assert origin == nominal_origin | ||
assert expected_output_csv.replace(" ","") == origin_parser.convert_navvis_origin_to_csv(origin).replace(" ","") | ||
os.remove(temp_origin_path) | ||
|
||
@pytest.mark.parametrize("bad_json_keys_origin", [{ | ||
"CRS": "EPSG:25834", | ||
"Pose": { | ||
"orientation": { | ||
"w": 0.8, | ||
"x": 0, | ||
"y": 0, | ||
"z": -0.5 | ||
}, | ||
"positon": { # misspelled key | ||
"x": 6.3, | ||
"y": 2.4, | ||
"z": 99.95 | ||
} | ||
}}, | ||
{ | ||
"Pose": { | ||
"orentation": { # misspelled key | ||
"w": 0.5, | ||
"x": 0, | ||
"y": 0, | ||
"z": 0 | ||
}, | ||
"position": { | ||
"x": 0, | ||
"y": 0, | ||
"z": 0 | ||
} | ||
} | ||
} | ||
]) | ||
def test_parse_navvis_origin_bad_input(bad_json_keys_origin, tmp_path): | ||
temp_origin_path = tmp_path / "bad_json_keys_origin.json" | ||
with open(temp_origin_path, 'w') as file: | ||
json.dump(bad_json_keys_origin, file) | ||
assert not origin_parser.parse_navvis_origin_file(temp_origin_path) | ||
with pytest.raises(KeyError): | ||
origin_parser.convert_navvis_origin_to_csv(bad_json_keys_origin) | ||
os.remove(temp_origin_path) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this redundant with
capture.proc.GlobalAlignment
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.. I can write here now. was looking at it with the wrong account. I changed it using GlobalAlignment.