1# Copyright (c) 2018, ETH Zurich and UNC Chapel Hill. 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are met: 6# 7# * Redistributions of source code must retain the above copyright 8# notice, this list of conditions and the following disclaimer. 9# 10# * Redistributions in binary form must reproduce the above copyright 11# notice, this list of conditions and the following disclaimer in the 12# documentation and/or other materials provided with the distribution. 13# 14# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of 15# its contributors may be used to endorse or promote products derived 16# from this software without specific prior written permission. 17# 18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28# POSSIBILITY OF SUCH DAMAGE. 29# 30# Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de) 31 32import numpy as np 33from read_write_model import read_model, write_model 34from tempfile import mkdtemp 35 36 37def compare_cameras(cameras1, cameras2): 38 assert len(cameras1) == len(cameras2) 39 for camera_id1 in cameras1: 40 camera1 = cameras1[camera_id1] 41 camera2 = cameras2[camera_id1] 42 assert camera1.id == camera2.id 43 assert camera1.width == camera2.width 44 assert camera1.height == camera2.height 45 assert np.allclose(camera1.params, camera2.params) 46 47 48def compare_images(images1, images2): 49 assert len(images1) == len(images2) 50 for image_id1 in images1: 51 image1 = images1[image_id1] 52 image2 = images2[image_id1] 53 assert image1.id == image2.id 54 assert np.allclose(image1.qvec, image2.qvec) 55 assert np.allclose(image1.tvec, image2.tvec) 56 assert image1.camera_id == image2.camera_id 57 assert image1.name == image2.name 58 assert np.allclose(image1.xys, image2.xys) 59 assert np.array_equal(image1.point3D_ids, image2.point3D_ids) 60 61 62def compare_points(points3D1, points3D2): 63 for point3D_id1 in points3D1: 64 point3D1 = points3D1[point3D_id1] 65 point3D2 = points3D2[point3D_id1] 66 assert point3D1.id == point3D2.id 67 assert np.allclose(point3D1.xyz, point3D2.xyz) 68 assert np.array_equal(point3D1.rgb, point3D2.rgb) 69 assert np.allclose(point3D1.error, point3D2.error) 70 assert np.array_equal(point3D1.image_ids, point3D2.image_ids) 71 assert np.array_equal(point3D1.point2D_idxs, point3D2.point2D_idxs) 72 73 74def main(): 75 import sys 76 if len(sys.argv) != 3: 77 print("Usage: python read_model.py " 78 "path/to/model/folder/txt path/to/model/folder/bin") 79 return 80 81 print("Comparing text and binary models ...") 82 83 path_to_model_txt_folder = sys.argv[1] 84 path_to_model_bin_folder = sys.argv[2] 85 cameras_txt, images_txt, points3D_txt = \ 86 read_model(path_to_model_txt_folder, ext=".txt") 87 cameras_bin, images_bin, points3D_bin = \ 88 read_model(path_to_model_bin_folder, ext=".bin") 89 compare_cameras(cameras_txt, cameras_bin) 90 compare_images(images_txt, images_bin) 91 compare_points(points3D_txt, points3D_bin) 92 93 print("... text and binary models are equal.") 94 print("Saving text model and reloading it ...") 95 96 tmpdir = mkdtemp() 97 write_model(cameras_bin, images_bin, points3D_bin, tmpdir, ext='.txt') 98 cameras_txt, images_txt, points3D_txt = \ 99 read_model(tmpdir, ext=".txt") 100 compare_cameras(cameras_txt, cameras_bin) 101 compare_images(images_txt, images_bin) 102 compare_points(points3D_txt, points3D_bin) 103 104 print("... saved text and loaded models are equal.") 105 print("Saving binary model and reloading it ...") 106 107 write_model(cameras_bin, images_bin, points3D_bin, tmpdir, ext='.bin') 108 cameras_bin, images_bin, points3D_bin = \ 109 read_model(tmpdir, ext=".bin") 110 compare_cameras(cameras_txt, cameras_bin) 111 compare_images(images_txt, images_bin) 112 compare_points(points3D_txt, points3D_bin) 113 114 print("... saved binary and loaded models are equal.") 115 116 117if __name__ == "__main__": 118 main() 119