|
| 1 | +import json |
| 2 | +import logging |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +logger = logging.getLogger(__name__) |
| 6 | + |
| 7 | +UNKNOWN_CRS_NAME = 'UNKNOWN' |
| 8 | + |
| 9 | +def is_navvis_origin_valid(navvis_origin : dict): |
| 10 | + """ |
| 11 | + Check if the NavVis origin dictionary is valid. |
| 12 | + CRS is optional. Pose is required. |
| 13 | + :param navvis_origin: NavVis origin dictionary |
| 14 | + :return: True if valid, False otherwise |
| 15 | + :rtype: bool |
| 16 | + """ |
| 17 | + if navvis_origin['Pose']['position']['x'] is None or \ |
| 18 | + navvis_origin['Pose']['position']['y'] is None or \ |
| 19 | + navvis_origin['Pose']['position']['z'] is None or \ |
| 20 | + navvis_origin['Pose']['orientation']['w'] is None or \ |
| 21 | + navvis_origin['Pose']['orientation']['x'] is None or \ |
| 22 | + navvis_origin['Pose']['orientation']['y'] is None or \ |
| 23 | + navvis_origin['Pose']['orientation']['z'] is None: |
| 24 | + return False |
| 25 | + return True |
| 26 | + |
| 27 | +def parse_navvis_origin_file(file_path : Path): |
| 28 | + """ |
| 29 | + The origin.json file is optional and if present it can be found |
| 30 | + in the anchors folder. |
| 31 | + |
| 32 | + The origin.json file contains two important values: pose and CRS. |
| 33 | + |
| 34 | + The CRS value stands for Coordinate Reference System (CRS) and explains in which |
| 35 | + coordinate system the origin itself is defined. Example: EPSG:25834 https://epsg.io/25834 |
| 36 | + |
| 37 | + The pose transforms dataset entities into the origin. The origin |
| 38 | + of the dataset can be created in many different ways: |
| 39 | + 0 - The origin is the NavVis 'dataset' origin, where a dataset equals a NavVis session. |
| 40 | + The origin then defaults to identity and the origin.json file might not be even present. |
| 41 | + 1 - NavVis software allows relative alignment between dataset via the NavVis IVION Dataset Web Editor |
| 42 | + but also via the NavVis local processing software which is soon to be deprecated. |
| 43 | + 2 - The origin is the NavVis Site origin. NavVis organizes datasets in the same physical location |
| 44 | + via Sites. The origin file contains then the transformation which moves all the entities of a |
| 45 | + NavVis dataset into the Site origin. Additionally NavVis IVION allows to register the Site origin |
| 46 | + to a global coordinate system. Hence, many NavVis sessions can be registered then to the same |
| 47 | + global coordinate system. Note that this is achieved via the NavVis IVION Dataset Web Editor. |
| 48 | + 3 - The origin lies in a Coordinate Reference System (CRS) like EPSG:25834 https://epsg.io/25834. |
| 49 | + The transformation is computed via geo-referenced Control Points which are registered during |
| 50 | + capture. More information about control points and the origin can be found here: |
| 51 | + https://knowledge.navvis.com/v1/docs/creating-the-control-point-poses-file |
| 52 | + https://knowledge.navvis.com/docs/what-coordinate-system-do-we-use-for-the-control-points-related-tasks |
| 53 | +
|
| 54 | + :param file_path: Path to the file |
| 55 | + :return: NavVis anchor origin dictionary |
| 56 | + :rtype: Dict |
| 57 | + """ |
| 58 | + if not file_path.exists(): |
| 59 | + print(f"Warning: Origin '{file_path}' does not exist.") |
| 60 | + return {} |
| 61 | + |
| 62 | + try: |
| 63 | + with file_path.open() as f: |
| 64 | + origin = json.load(f) |
| 65 | + if not is_navvis_origin_valid(origin): |
| 66 | + print("Invalid origin.json file", json.dumps(origin, indent=4)) |
| 67 | + return origin |
| 68 | + except Exception as e: |
| 69 | + logger.warning( |
| 70 | + "Failed reading origin.json file. %s", e) |
| 71 | + return {} |
| 72 | + |
| 73 | + |
| 74 | +def get_crs_from_navvis_origin(navvis_origin : dict): |
| 75 | + """ |
| 76 | + Get the label from the NavVis origin |
| 77 | + :param navvis_origin: NavVis origin dictionary |
| 78 | + :return: Label |
| 79 | + :rtype: str |
| 80 | + """ |
| 81 | + |
| 82 | + return navvis_origin.get('CRS', UNKNOWN_CRS_NAME) |
| 83 | + |
| 84 | + |
| 85 | +def get_pose_from_navvis_origin(navvis_origin : dict): |
| 86 | + """ |
| 87 | + Extract the pose from the NavVis origin dictionary |
| 88 | + :param navvis_origin: NavVis origin dictionary |
| 89 | + :return: Quaternion and translation vector |
| 90 | + :rtype: qvec, tvec |
| 91 | + """ |
| 92 | + |
| 93 | + qvec = [1, 0, 0, 0] |
| 94 | + tvec = [0, 0, 0] |
| 95 | + if navvis_origin: |
| 96 | + orientation = navvis_origin['Pose']['orientation'] |
| 97 | + position = navvis_origin['Pose']['position'] |
| 98 | + qvec = [orientation['w'], orientation['x'], orientation['y'], orientation['z']] |
| 99 | + tvec = [position['x'], position['y'], position['z']] |
| 100 | + return qvec, tvec |
| 101 | + |
| 102 | + |
| 103 | +def convert_navvis_origin_to_csv(navvis_origin : dict): |
| 104 | + csv_str = "# CRS, qw, qx, qy, qz, tx, ty, tz\n" |
| 105 | + if 'CRS' in navvis_origin: |
| 106 | + crs = navvis_origin['CRS'] |
| 107 | + else: |
| 108 | + crs = UNKNOWN_CRS_NAME |
| 109 | + position = navvis_origin['Pose']['position'] |
| 110 | + orientation = navvis_origin['Pose']['orientation'] |
| 111 | + csv_str += (f"{crs}," |
| 112 | + f"{orientation['w']}," |
| 113 | + f"{orientation['x']}," |
| 114 | + f"{orientation['y']}," |
| 115 | + f"{orientation['z']}," |
| 116 | + f"{position['x']}," |
| 117 | + f"{position['y']}," |
| 118 | + f"{position['z']}\n") |
| 119 | + return csv_str |
0 commit comments