{"repo_name": "voxel_svio", "file_name": "/voxel_svio/include/parameters.h", "inference_info": {"prefix_code": "class stateOptions {\n public:\n bool do_fej = true;\n bool do_calib_camera_pose = false;\n bool do_calib_camera_intrinsics = false;\n bool do_calib_camera_timeoffset = false;\n bool do_calib_imu_intrinsics = false;\n bool do_calib_imu_g_sensitivity = false;\n ImuModel imu_model = ImuModel::KALIBR;\n int max_clone_size = 11;\n int max_slam_features = 25;\n int max_slam_in_update = 1000;\n int max_msckf_in_update = 1000;\n double voxel_size = 0.5;\n int max_num_points_in_voxel = 10;\n double min_distance_points = 0.15;\n bool use_huber = true;\n ", "suffix_code": "\n public:\n double init_window_time = 1.0;\n double init_imu_thresh = 1.0;\n double init_max_disparity = 1.0;\n int init_max_features = 50;\n bool init_dyn_use = false;\n bool init_dyn_mle_opt_calib = false;\n int init_dyn_mle_max_iter = 20;\n int init_dyn_mle_max_threads = 20;\n double init_dyn_mle_max_time = 5.0;\n int init_dyn_num_pose = 5;\n double init_dyn_min_deg = 45.0;\n double init_dyn_inflation_orientation = 10.0;\n double init_dyn_inflation_velocity = 10.0;\n double init_dyn_inflation_bias_gyro = 100.0;\n double init_dyn_inflation_bias_accel = 100.0;\n double init_dyn_min_rec_cond = 1e-15;\n Eigen::Vector3d init_dyn_bias_g = Eigen::Vector3d::Zero();\n Eigen::Vector3d init_dyn_bias_a = Eigen::Vector3d::Zero();\n double sigma_w = 1.6968e-04;\n double sigma_wb = 1.9393e-05;\n double sigma_a = 2.0000e-3;\n double sigma_ab = 3.0000e-03;\n double sigma_pix = 1;\n double gravity_mag = 9.81;\n bool downsample_cameras = false;\n double calib_camimu_dt = 0.0;\n std::unordered_map> camera_intrinsics;\n std::map camera_extrinsics;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n bool refine_features = true;\n int max_runs = 5;\n double init_lamda = 1e-3;\n double max_lamda = 1e10;\n double min_dx = 1e-6;\n double min_dcost = 1e-6;\n double lam_mult = 10;\n double min_dist = 0.10;\n double max_dist = 60;\n double max_baseline = 40;\n double max_cond_number = 10000;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n double sigma_w = 1.6968e-04;\n double sigma_w_2 = pow(1.6968e-04, 2);\n double sigma_wb = 1.9393e-05;\n double sigma_wb_2 = pow(1.9393e-05, 2);\n double sigma_a = 2.0000e-3;\n double sigma_a_2 = pow(2.0000e-3, 2);\n double sigma_ab = 3.0000e-03;\n double sigma_ab_2 = pow(3.0000e-03, 2);\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n double chi2_multipler = 5;\n double sigma_pix = 1;\n double sigma_pix_sq = 1;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n stateOptions state_options;\n inertialInitializerOptions init_options;\n double dt_slam_delay = 2.0;\n noiseManager imu_noises;\n updaterOptions msckf_options;\n updaterOptions slam_options;\n double gravity_mag = 9.81;\n Eigen::Matrix vec_dw;\n Eigen::Matrix vec_da;\n Eigen::Matrix vec_tg;\n Eigen::Matrix q_imu_acc;\n Eigen::Matrix q_imu_gyr;\n double calib_camimu_dt = 0.0;\n std::unordered_map> camera_intrinsics;\n std::map camera_extrinsics;\n bool use_mask = false;\n std::map masks;\n bool downsample_cameras = false;\n int num_pts = 150;\n int fast_threshold = 20;\n int patch_size_x = 5;\n int patch_size_y = 5;\n int min_px_dist = 10;\n HistogramMethod histogram_method = HistogramMethod::HISTOGRAM;\n double track_frequency = 20.0;\n featureInitializerOptions featinit_options;\n double voxel_size = 0.5;\n int max_num_points_in_voxel = 10;\n double min_distance_points = 0.15;\n int nb_voxels_visited = 1;\n bool use_all_points = false;\n bool use_keyframe = true;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n};", "middle_code": "void recordParameters() {\n\tstate_options.recordParameters();\n\tinit_options.recordParameters();\n\tstd::string str_temp;\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\timu_noises.recordParameters();\n\tmsckf_options.recordParameters();\n\tslam_options.recordParameters();\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\tfeatinit_options.recordParameters();\n\tfoutC.close();\n}", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "cpp", "sub_task_type": null}, "context_code": [["/voxel_svio/include/stereoVio.h", "class voxelStereoVio {\n private:\n void processImu(imuData &imu_data) {\n double oldest_time = state_ptr->margtimestep();\n\n if (oldest_time > state_ptr->timestamp)\n oldest_time = -1;\n\n if (!initial_flag)\n oldest_time = imu_data.timestamp - odometry_options.init_options.init_window_time + state_ptr->calib_dt_imu_cam->value()(0) - 0.10;\n\n propagator_ptr->feedImu(imu_data, oldest_time);\n\n if (!initial_flag)\n initializer_ptr->feedImu(imu_data, oldest_time);\n}\n void processImage(cameraData &image_measurements_const) {\n rT1 = boost::posix_time::microsec_clock::local_time();\n\n assert(!image_measurements_const.camera_ids.empty());\n assert(image_measurements_const.camera_ids.size() == image_measurements_const.images.size());\n \n for (size_t i = 0; i < image_measurements_const.camera_ids.size() - 1; i++)\n assert(image_measurements_const.camera_ids.at(i) != image_measurements_const.camera_ids.at(i + 1));\n\n // time test\n boost::posix_time::ptime rT_begin7 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n cameraData image_measurements = image_measurements_const;\n\n for (size_t i = 0; i < image_measurements.camera_ids.size() && odometry_options.downsample_cameras; i++)\n {\n cv::Mat img = image_measurements.images.at(i);\n cv::Mat mask = image_measurements.masks.at(i);\n cv::Mat img_temp, mask_temp;\n cv::pyrDown(img, img_temp, cv::Size(img.cols / 2.0, img.rows / 2.0));\n image_measurements.images.at(i) = img_temp;\n cv::pyrDown(mask, mask_temp, cv::Size(mask.cols / 2.0, mask.rows / 2.0));\n image_measurements.masks.at(i) = mask_temp;\n }\n\n std::shared_ptr fh = std::make_shared();\n fh->makeImageLeft(image_measurements.images.at(0).data, image_measurements.masks.at(0).data, gammaPixel_ptr, image_measurements.timestamp);\n fh->makeImageRight(image_measurements.images.at(1).data, image_measurements.masks.at(1).data, gammaPixel_ptr, image_measurements.timestamp);\n fh->frame_id = frame_count;\n\n // time test\n boost::posix_time::ptime rT_end7 = boost::posix_time::microsec_clock::local_time();\n double time_cost = (rT_end7 - rT_begin7).total_microseconds() * 1e-6;\n sum_time_7 = sum_time_7 * n_time_7 + time_cost;\n n_time_7++;\n sum_time_7 = sum_time_7 / n_time_7;\n std::cout << std::fixed << std::setprecision(6) << \"[image processing] time cost = \" << sum_time_7 << std::endl;\n // time test\n\n newest_fh = fh;\n\n // time test\n boost::posix_time::ptime rT_begin1 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n featureTracker->feedNewImage(image_measurements, fh);\n\n // time test\n boost::posix_time::ptime rT_end1 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end1 - rT_begin1).total_microseconds() * 1e-6;\n sum_time_1 = sum_time_1 * n_time_1 + time_cost;\n n_time_1++;\n sum_time_1 = sum_time_1 / n_time_1;\n std::cout << std::fixed << std::setprecision(6) << \"[feature extraction and matching] time cost = \" << sum_time_1 << std::endl;\n // time test\n\n rT2 = boost::posix_time::microsec_clock::local_time();\n\n if (!initial_flag)\n {\n initial_flag = tryToInitialize(image_measurements);\n\n if (!initial_flag)\n {\n double time_track = (rT2 - rT1).total_microseconds() * 1e-6;\n std::cout << std::fixed << \"[processImage]: \" << time_track << \" seconds for tracking\" << std::endl;\n return;\n }\n }\n\n if (featureTracker->getFeatureDatabase()->featureCheckParallax(frame_count))\n marginalize_status = MarginalizeStatus::OLD;\n else\n {\n if (odometry_options.use_keyframe)\n marginalize_status = MarginalizeStatus::NEW;\n else\n marginalize_status = MarginalizeStatus::OLD;\n }\n\n std::cout << \"marginalize_status = \" << marginalize_status << std::endl;\n\n featureUpdate(image_measurements);\n\n frame_count++;\n}\n void featureUpdate(cameraData &image_measurements) {\n if (state_ptr->timestamp > image_measurements.timestamp)\n {\n std::cout << std::fixed << \"Image received out of order, unable to do anything (prop dt = \" << image_measurements.timestamp - state_ptr->timestamp << \")\" << std::endl;\n return;\n }\n\n if (state_ptr->timestamp != image_measurements.timestamp)\n {\n propagator_ptr->propagateAndClone(state_ptr, image_measurements.timestamp);\n\n if (state_ptr->clones_frame.find(image_measurements.timestamp) == state_ptr->clones_frame.end())\n state_ptr->clones_frame[image_measurements.timestamp] = newest_fh;\n\n assert(state_ptr->clones_imu.size() == state_ptr->clones_frame.size());\n }\n\n rT3 = boost::posix_time::microsec_clock::local_time();\n\n if ((int)state_ptr->clones_imu.size() < std::min(state_ptr->options.max_clone_size, 5))\n {\n std::cout << \"Waiting for enough clone states (\" << (int)state_ptr->clones_imu.size() << \" of \" \n << std::min(state_ptr->options.max_clone_size, 5) << \")\" << std::endl;\n return;\n }\n\n if (state_ptr->timestamp > image_measurements.timestamp)\n {\n std::cout << \"Msckf unable to propagate the state forward in time\" << std::endl;\n std::cout << std::fixed << \"It has been \" << image_measurements.timestamp - state_ptr->timestamp << \" since last time we propagated\" << std::endl;\n return;\n }\n\n std::vector> features_lost, features_marg, features_slam;\n features_lost = featureTracker->getFeatureDatabase()->getOldFeatures(state_ptr->timestamp, false, true);\n\n if ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size || (int)state_ptr->clones_imu.size() > 5)\n {\n features_marg = featureTracker->getFeatureDatabase()->getFeatures(state_ptr->margtimestep(), false, true);\n }\n\n auto it_1 = features_lost.begin();\n while (it_1 != features_lost.end())\n {\n bool found_current_cam_id = false;\n for (const auto &cam_uv_pair : (*it_1)->uvs)\n {\n if (std::find(image_measurements.camera_ids.begin(), image_measurements.camera_ids.end(), cam_uv_pair.first) \n != image_measurements.camera_ids.end())\n {\n found_current_cam_id = true;\n break;\n }\n }\n\n if (found_current_cam_id)\n {\n it_1++;\n }\n else \n {\n it_1 = features_lost.erase(it_1);\n }\n }\n\n it_1 = features_lost.begin();\n while (it_1 != features_lost.end())\n {\n if (std::find(features_marg.begin(), features_marg.end(), (*it_1)) != features_marg.end())\n it_1 = features_lost.erase(it_1);\n else\n it_1++;\n }\n\n std::vector> features_maxtracks;\n auto it_2 = features_marg.begin();\n\n while (it_2 != features_marg.end())\n {\n bool reached_max = false;\n \n for (const auto &cams : (*it_2)->timestamps)\n {\n if ((int)cams.second.size() > state_ptr->options.max_clone_size)\n {\n reached_max = true;\n break;\n }\n }\n \n if (reached_max)\n {\n features_maxtracks.push_back(*it_2);\n it_2 = features_marg.erase(it_2);\n }\n else\n {\n it_2++;\n }\n }\n\n if (state_ptr->options.max_slam_features > 0 && image_measurements.timestamp - startup_time >= odometry_options.dt_slam_delay &&\n (int)state_ptr->map_points.size() < state_ptr->options.max_slam_features)\n {\n int amount_to_add = (state_ptr->options.max_slam_features) - (int)state_ptr->map_points.size();\n int valid_amount = (amount_to_add > (int)features_maxtracks.size()) ? (int)features_maxtracks.size() : amount_to_add;\n\n if (valid_amount > 0)\n {\n features_slam.insert(features_slam.end(), features_maxtracks.end() - valid_amount, features_maxtracks.end());\n features_maxtracks.erase(features_maxtracks.end() - valid_amount, features_maxtracks.end());\n }\n }\n\n for (int i = 0; i < recent_voxels.size(); i++)\n {\n short kx = recent_voxels[i][0];\n short ky = recent_voxels[i][1];\n short kz = recent_voxels[i][2];\n\n voxelHashMap::iterator search = voxel_map.find(voxel(kx, ky, kz));\n auto &voxel_block = (search.value());\n\n if (odometry_options.use_all_points)\n {\n for (int j = 0; j < voxel_block.points.size(); j++)\n {\n std::shared_ptr map_point_ptr = voxel_block.points[j];\n\n std::shared_ptr feature = featureTracker->getFeatureDatabase()->getFeature(map_point_ptr->feature_id);\n if (feature != nullptr)\n features_slam.push_back(feature);\n\n assert(map_point_ptr->unique_camera_id != -1);\n\n bool current_host_cam = std::find(image_measurements.camera_ids.begin(), image_measurements.camera_ids.end(), map_point_ptr->unique_camera_id) != image_measurements.camera_ids.end();\n\n if (feature == nullptr && current_host_cam)\n map_point_ptr->should_marg = true;\n\n if (map_point_ptr->update_fail_count > 1)\n map_point_ptr->should_marg = true;\n }\n }\n else\n {\n std::shared_ptr map_point_ptr = voxel_block.points.front();\n\n std::shared_ptr feature = featureTracker->getFeatureDatabase()->getFeature(map_point_ptr->feature_id);\n if (feature != nullptr)\n features_slam.push_back(feature);\n\n assert(map_point_ptr->unique_camera_id != -1);\n\n bool current_host_cam = std::find(image_measurements.camera_ids.begin(), image_measurements.camera_ids.end(), map_point_ptr->unique_camera_id) != image_measurements.camera_ids.end();\n\n if (feature == nullptr && current_host_cam)\n map_point_ptr->should_marg = true;\n\n if (map_point_ptr->update_fail_count > 1)\n map_point_ptr->should_marg = true;\n }\n }\n\n // time test\n boost::posix_time::ptime rT_begin6 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n stateHelper::marginalizeSlam(state_ptr, voxel_map, points_history);\n\n // time test\n boost::posix_time::ptime rT_end6 = boost::posix_time::microsec_clock::local_time();\n double time_cost = (rT_end6 - rT_begin6).total_microseconds() * 1e-6;\n sum_time_6 = sum_time_6 * n_time_6 + time_cost;\n n_time_6++;\n sum_time_6 = sum_time_6 / n_time_6;\n std::cout << std::fixed << std::setprecision(6) << \"[marginalizeSlam] time cost = \" << sum_time_6 << std::endl;\n // time test\n\n std::vector> features_slam_delayed, features_slam_update;\n\n for (size_t i = 0; i < features_slam.size(); i++)\n {\n if (state_ptr->map_points.find(features_slam.at(i)->feature_id) != state_ptr->map_points.end())\n {\n features_slam_update.push_back(features_slam.at(i));\n }\n else\n {\n features_slam_delayed.push_back(features_slam.at(i));\n }\n }\n\n std::vector> features_up_msckf = features_lost;\n features_up_msckf.insert(features_up_msckf.end(), features_marg.begin(), features_marg.end());\n features_up_msckf.insert(features_up_msckf.end(), features_maxtracks.begin(), features_maxtracks.end());\n\n auto compare_feature = [](const std::shared_ptr &a, const std::shared_ptr &b) -> bool {\n size_t size_a = 0;\n size_t size_b = 0;\n \n for (const auto &pair : a->timestamps)\n size_a += pair.second.size();\n \n for (const auto &pair : b->timestamps)\n size_b += pair.second.size();\n\n return size_a < size_b;\n };\n std::sort(features_up_msckf.begin(), features_up_msckf.end(), compare_feature);\n\n if ((int)features_up_msckf.size() > state_ptr->options.max_msckf_in_update)\n features_up_msckf.erase(features_up_msckf.begin(), features_up_msckf.end() - state_ptr->options.max_msckf_in_update);\n\n // time test\n boost::posix_time::ptime rT_begin2 = boost::posix_time::microsec_clock::local_time();\n // time test\n \n updaterMsckf_ptr->update(state_ptr, features_up_msckf);\n\n // time test\n boost::posix_time::ptime rT_end2 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end2 - rT_begin2).total_microseconds() * 1e-6;\n sum_time_2 = sum_time_2 * n_time_2 + time_cost;\n n_time_2++;\n sum_time_2 = sum_time_2 / n_time_2;\n std::cout << std::fixed << std::setprecision(6) << \"[state update without map points] time cost = \" << sum_time_2 << std::endl;\n // time test\n\n propagator_ptr->invalidateCache();\n\n rT4 = boost::posix_time::microsec_clock::local_time();\n\n std::vector> features_slam_update_temp;\n\n while (!features_slam_update.empty())\n {\n std::vector> features_up_temp;\n \n features_up_temp.insert(features_up_temp.begin(), features_slam_update.begin(), \n features_slam_update.begin() + std::min(state_ptr->options.max_slam_in_update, (int)features_slam_update.size()));\n \n features_slam_update.erase(features_slam_update.begin(), \n features_slam_update.begin() + std::min(state_ptr->options.max_slam_in_update, (int)features_slam_update.size()));\n\n // time test\n boost::posix_time::ptime rT_begin3 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n updaterSlam_ptr->update(state_ptr, voxel_map, features_up_temp);\n\n // time test\n boost::posix_time::ptime rT_end3 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end3 - rT_begin3).total_microseconds() * 1e-6;\n sum_time_3 = sum_time_3 * n_time_3 + time_cost;\n n_time_3++;\n sum_time_3 = sum_time_3 / n_time_3;\n std::cout << std::fixed << std::setprecision(6) << \"[state update without map points] time cost = \" << sum_time_3 << std::endl;\n // time test\n\n auto it = state_ptr->map_points.begin();\n\n while (it != state_ptr->map_points.end())\n {\n mapManagement::changeHostVoxel(voxel_map, (*it).second, odometry_options.voxel_size, odometry_options.max_num_points_in_voxel, odometry_options.min_distance_points, voxels_history);\n it++;\n }\n\n features_slam_update_temp.insert(features_slam_update_temp.end(), features_up_temp.begin(), features_up_temp.end());\n propagator_ptr->invalidateCache();\n }\n\n features_slam_update = features_slam_update_temp;\n rT5 = boost::posix_time::microsec_clock::local_time();\n\n // time test\n boost::posix_time::ptime rT_begin4 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n updaterSlam_ptr->delayedInit(state_ptr, voxel_map, features_slam_delayed, voxels_history);\n\n // time test\n boost::posix_time::ptime rT_end4 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end4 - rT_begin4).total_microseconds() * 1e-6;\n sum_time_4 = sum_time_4 * n_time_4 + time_cost;\n n_time_4++;\n sum_time_4 = sum_time_4 / n_time_4;\n std::cout << std::fixed << std::setprecision(6) << \"[map point registration] time cost = \" << sum_time_4 << std::endl;\n // time test\n\n rT6 = boost::posix_time::microsec_clock::local_time();\n\n if (image_measurements.camera_ids.at(0) == 0)\n {\n // time test\n boost::posix_time::ptime rT_begin5 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n triangulateActiveTracks(image_measurements);\n\n getRecentVoxel(image_measurements.timestamp, voxels_visit);\n\n // time test\n boost::posix_time::ptime rT_end5 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end5 - rT_begin5).total_microseconds() * 1e-6;\n sum_time_5 = sum_time_5 * n_time_5 + time_cost;\n n_time_5++;\n sum_time_5 = sum_time_5 / n_time_5;\n std::cout << std::fixed << std::setprecision(6) << \"[suitable map point selection] time cost = \" << sum_time_5 << std::endl;\n // time test\n\n good_features_msckf.clear();\n }\n\n for (auto const &feat : features_up_msckf)\n {\n good_features_msckf.push_back(feat->position_global);\n feat->to_delete = true;\n }\n\n featureTracker->getFeatureDatabase()->cleanUp();\n\n if ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size)\n {\n featureTracker->getFeatureDatabase()->cleanUpOldmeasurements(state_ptr->margtimestep());\n }\n\n if (marginalize_status == MarginalizeStatus::OLD)\n {\n stateHelper::marginalizeOldClone(state_ptr);\n }\n else\n {\n stateHelper::marginalizeNewClone(state_ptr);\n }\n\n rT7 = boost::posix_time::microsec_clock::local_time();\n\n double time_track = (rT2 - rT1).total_microseconds() * 1e-6;\n double time_prop = (rT3 - rT2).total_microseconds() * 1e-6;\n double time_msckf = (rT4 - rT3).total_microseconds() * 1e-6;\n double time_slam_update = (rT5 - rT4).total_microseconds() * 1e-6;\n double time_slam_delay = (rT6 - rT5).total_microseconds() * 1e-6;\n double time_marg = (rT7 - rT6).total_microseconds() * 1e-6;\n double time_total = (rT7 - rT1).total_microseconds() * 1e-6;\n\n // Time test\n /*\n std::cout << std::fixed << \"[processImage]: \" << time_track << \" seconds for tracking\" << std::endl;\n std::cout << std::fixed << \"[featureUpdate]: \" << time_prop << \" seconds for propagation\" << std::endl;\n std::cout << std::fixed << \"[featureUpdate]: \" << time_msckf << \" seconds for MSCKF update (\" << (int)features_up_msckf.size() << \" feats)\" << std::endl;\n \n if (state_ptr->options.max_slam_features > 0)\n {\n std::cout << std::fixed << \"[featureUpdate]: \" << time_slam_update << \" seconds for SLAM update (\" << (int)state_ptr->map_points.size() << \" feats)\" << std::endl;\n std::cout << std::fixed << \"[featureUpdate]: \" << time_slam_delay << \" seconds for SLAM delayed init (\" << (int)features_slam_delayed.size() << \" feats)\" << std::endl;\n }\n\n std::cout << std::fixed << \"[featureUpdate]: \" << time_marg << \" seconds for re-tri & marg (\" << (int)state_ptr->clones_imu.size() << \" clones in state)\" << std::endl;\n\n std::stringstream ss;\n ss << \"[featureUpdate]: \" << std::setprecision(4) << time_total << \" seconds for total (camera\";\n\n for (const auto &id : image_measurements.camera_ids) ss << \" \" << id;\n\n ss << \")\" << std::endl;\n\n std::cout << ss.str();\n */\n // Time test\n\n if (timelastupdate != -1 && state_ptr->clones_imu.find(timelastupdate) != state_ptr->clones_imu.end())\n {\n Eigen::Matrix dx = state_ptr->imu_ptr->getPos() - state_ptr->clones_imu.at(timelastupdate)->getPos();\n distance += dx.norm();\n }\n timelastupdate = image_measurements.timestamp;\n\n // display\n pubOdometry(state_ptr, timelastupdate);\n pubPath(state_ptr, timelastupdate);\n pubHistoryPoints(points_history, timelastupdate);\n pubWindowPoints(state_ptr, timelastupdate);\n pubHistoryVoxels(voxels_history, timelastupdate);\n pubVisitVoxels(voxels_visit, timelastupdate);\n // display\n\n std::cout << std::fixed << std::setprecision(3) << \"q_GtoI = \" << state_ptr->imu_ptr->getQuat()(0) << \" \" << state_ptr->imu_ptr->getQuat()(1) << \" \"\n << state_ptr->imu_ptr->getQuat()(2) << \" \" << state_ptr->imu_ptr->getQuat()(3) << std::endl;\n\n std::cout << std::fixed << std::setprecision(3) << \"p_IinG = \" << state_ptr->imu_ptr->getPos()(0) << \" \" << state_ptr->imu_ptr->getPos()(1) << \" \"\n << state_ptr->imu_ptr->getPos()(2) << std::endl;\n\n std::cout << std::fixed << std::setprecision(2) << \"dist = \" << distance << \" meters\" << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"bg = \" << state_ptr->imu_ptr->getBg()(0) << \" \" << state_ptr->imu_ptr->getBg()(1) << \" \"\n << state_ptr->imu_ptr->getBg()(2) << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"ba = \" << state_ptr->imu_ptr->getBa()(0) << \" \" << state_ptr->imu_ptr->getBa()(1) << \" \"\n << state_ptr->imu_ptr->getBa()(2) << std::endl;\n\n std::ofstream foutC(std::string(output_path + \"/pose.txt\"), std::ios::app);\n foutC.setf(std::ios::scientific, std::ios::floatfield);\n foutC.precision(6);\n foutC << std::fixed << image_measurements.timestamp + state_ptr->calib_dt_imu_cam->value()(0) << \" \" << state_ptr->imu_ptr->getPos()(0) << \" \" << state_ptr->imu_ptr->getPos()(1) << \" \" << state_ptr->imu_ptr->getPos()(2) << \" \" \n << state_ptr->imu_ptr->getQuat()(0) << \" \" << state_ptr->imu_ptr->getQuat()(1) << \" \" << state_ptr->imu_ptr->getQuat()(2) << \" \" << state_ptr->imu_ptr->getQuat()(3) << std::endl;\n foutC.close();\n\n if (state_ptr->options.do_calib_camera_timeoffset)\n std::cout << std::fixed << std::setprecision(5) << \"camera-imu timeoffset = \" << state_ptr->calib_dt_imu_cam->value()(0) << std::endl;\n\n if (state_ptr->options.do_calib_camera_intrinsics)\n {\n for (int i = 0; i < 2; i++)\n {\n std::shared_ptr calib = state_ptr->cam_intrinsics.at(i);\n\n std::cout << std::fixed << std::setprecision(3) << \"cam\" << (int)i << \" intrinsics = \" << calib->value()(0) << \" \" << calib->value()(1) << \" \"\n << calib->value()(2) << \" \" << calib->value()(3) << \" \" << calib->value()(4) << \" \" << calib->value()(5) << \" \"\n << calib->value()(6) << \" \" << calib->value()(7) << std::endl;\n\n }\n }\n\n if (state_ptr->options.do_calib_camera_pose)\n {\n for (int i = 0; i < 2; i++)\n {\n std::shared_ptr calib = state_ptr->calib_cam_imu.at(i);\n\n std::cout << std::fixed << std::setprecision(3) << \"cam\" << (int)i << \" extrinsics = \" << calib->getQuat()(0) << \" \" << calib->getQuat()(1) << \" \"\n << calib->getQuat()(2) << \" \" << calib->getQuat()(3) << \" \" << calib->getPos()(0) << \" \" << calib->getPos()(1) << \" \"\n << calib->getPos()(2) << std::endl;\n }\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::KALIBR)\n {\n std::cout << std::fixed << std::setprecision(3) << \"q_GYROtoI = \" << state_ptr->calib_imu_gyr->value()(0) << \" \" << state_ptr->calib_imu_gyr->value()(1) << \" \"\n << state_ptr->calib_imu_gyr->value()(2) << \" \" << state_ptr->calib_imu_gyr->value()(3) << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::RPNG)\n {\n std::cout << std::fixed << std::setprecision(3) << \"q_ACCtoI = \" << state_ptr->calib_imu_acc->value()(0) << \" \" << state_ptr->calib_imu_acc->value()(1) << \" \"\n << state_ptr->calib_imu_acc->value()(2) << \" \" << state_ptr->calib_imu_acc->value()(3) << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::KALIBR)\n {\n std::cout << std::fixed << std::setprecision(4) << \"Dw = | \" << state_ptr->calib_imu_dw->value()(0) << \", \" << state_ptr->calib_imu_dw->value()(1) << \", \"\n << state_ptr->calib_imu_dw->value()(2) << \" | \" << state_ptr->calib_imu_dw->value()(3) << \", \" << state_ptr->calib_imu_dw->value()(4)\n << \" | \" << state_ptr->calib_imu_dw->value()(5) << \" |\" << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"Da = | \" << state_ptr->calib_imu_da->value()(0) << \", \" << state_ptr->calib_imu_da->value()(1) << \", \"\n << state_ptr->calib_imu_da->value()(2) << \" | \" << state_ptr->calib_imu_da->value()(3) << \", \" << state_ptr->calib_imu_da->value()(4)\n << \" | \" << state_ptr->calib_imu_da->value()(5) << \" |\" << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::RPNG)\n {\n std::cout << std::fixed << std::setprecision(4) << \"Dw = | \" << state_ptr->calib_imu_dw->value()(0) << \" | \" << state_ptr->calib_imu_dw->value()(1) << \", \"\n << state_ptr->calib_imu_dw->value()(2) << \" | \" << state_ptr->calib_imu_dw->value()(3) << \", \" << state_ptr->calib_imu_dw->value()(4)\n << \", \" << state_ptr->calib_imu_dw->value()(5) << \" |\" << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"Da = | \" << state_ptr->calib_imu_da->value()(0) << \" | \" << state_ptr->calib_imu_da->value()(1) << \", \"\n << state_ptr->calib_imu_da->value()(2) << \" | \" << state_ptr->calib_imu_da->value()(3) << \", \" << state_ptr->calib_imu_da->value()(4)\n << \", \" << state_ptr->calib_imu_da->value()(5) << \" |\" << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.do_calib_imu_g_sensitivity)\n {\n std::cout << std::fixed << std::setprecision(4) << \"Tg = | \" << state_ptr->calib_imu_tg->value()(0) << \", \" << state_ptr->calib_imu_tg->value()(1) << \", \"\n << state_ptr->calib_imu_tg->value()(2) << \" | \" << state_ptr->calib_imu_tg->value()(3) << \", \" << state_ptr->calib_imu_tg->value()(4) << \", \"\n << state_ptr->calib_imu_tg->value()(5) << \" | \" << state_ptr->calib_imu_tg->value()(6) << \", \" << state_ptr->calib_imu_tg->value()(7) << \", \"\n << state_ptr->calib_imu_tg->value()(8) << \" |\" << std::endl;\n }\n\n std::cout << std::endl;\n}\n void triangulateActiveTracks(cameraData &image_measurements) {\n boost::posix_time::ptime retri_rT1, retri_rT2, retri_rT3;\n retri_rT1 = boost::posix_time::microsec_clock::local_time();\n\n assert(state_ptr->clones_imu.find(image_measurements.timestamp) != state_ptr->clones_imu.end());\n active_tracks_time = image_measurements.timestamp;\n\n // draw\n /*\n active_image = cv::Mat();\n featureTracker->displayActive(active_image, 255, 255, 255, 255, 255, 255, \" \");\n if (!active_image.empty())\n {\n active_image = active_image(cv::Rect(0, 0, image_measurements.images.at(0).cols, image_measurements.images.at(0).rows));\n }\n */\n // draw\n\n active_tracks_pos_world.clear();\n active_tracks_uvd.clear();\n\n auto last_obs = featureTracker->getLastObs();\n auto last_ids = featureTracker->getLastIds();\n\n std::map active_feat_linsys_A_new;\n std::map active_feat_linsys_b_new;\n std::map active_feat_linsys_count_new;\n std::unordered_map active_tracks_pos_world_new;\n\n std::map feat_uvs_in_cam_0;\n\n // display\n assert(state_ptr->clones_frame.find(active_tracks_time) != state_ptr->clones_frame.end());\n\n cv::Mat img_left = image_measurements.images.at(0);\n cv::Mat img_right = image_measurements.images.at(1);\n\n cv::Mat color_left = convertCvImage(img_left);\n cv::Mat color_right = convertCvImage(img_right);\n\n cv::Vec3b color_feat = cv::Vec3b(0, 255, 0);\n // display\n\n for (auto const &cam_id : image_measurements.camera_ids)\n {\n Eigen::Matrix3d R_GtoI = state_ptr->clones_imu.at(active_tracks_time)->getRot();\n Eigen::Vector3d p_IinG = state_ptr->clones_imu.at(active_tracks_time)->getPos();\n\n Eigen::Matrix3d R_ItoC = state_ptr->calib_cam_imu.at(cam_id)->getRot();\n Eigen::Vector3d p_IinC = state_ptr->calib_cam_imu.at(cam_id)->getPos();\n\n Eigen::Matrix3d R_GtoCi = R_ItoC * R_GtoI;\n Eigen::Vector3d p_CiinG = p_IinG - R_GtoCi.transpose() * p_IinC;\n\n assert(last_obs.find(cam_id) != last_obs.end());\n assert(last_ids.find(cam_id) != last_ids.end());\n\n for (size_t i = 0; i < last_obs.at(cam_id).size(); i++)\n {\n size_t feature_id = last_ids.at(cam_id).at(i);\n cv::Point2f pt_d = last_obs.at(cam_id).at(i).pt;\n\n // display\n if (pt_d.x > 5 && pt_d.x < wG[0] - 5 && pt_d.y > 5 && pt_d.y < hG[0] - 5)\n { \n if (cam_id == 0)\n drawPointL(color_left, pt_d.x, pt_d.y, color_feat);\n else\n drawPointL(color_right, pt_d.x, pt_d.y, color_feat);\n }\n // display\n\n if (cam_id == 0) feat_uvs_in_cam_0[feature_id] = pt_d;\n\n if (state_ptr->map_points.find(feature_id) != state_ptr->map_points.end()) continue;\n\n cv::Point2f pt_n = state_ptr->cam_intrinsics_cameras.at(cam_id)->undistortCV(pt_d);\n Eigen::Matrix b_i;\n b_i << pt_n.x, pt_n.y, 1;\n b_i = R_GtoCi.transpose() * b_i;\n b_i = b_i / b_i.norm();\n Eigen::Matrix3d B_perp = quatType::skewSymmetric(b_i);\n\n Eigen::Matrix3d Ai = B_perp.transpose() * B_perp;\n Eigen::Vector3d bi = Ai * p_CiinG;\n\n if (active_feat_linsys_A.find(feature_id) == active_feat_linsys_A.end())\n {\n active_feat_linsys_A_new.insert({feature_id, Ai});\n active_feat_linsys_b_new.insert({feature_id, bi});\n active_feat_linsys_count_new.insert({feature_id, 1});\n }\n else\n {\n active_feat_linsys_A_new[feature_id] = Ai + active_feat_linsys_A[feature_id];\n active_feat_linsys_b_new[feature_id] = bi + active_feat_linsys_b[feature_id];\n active_feat_linsys_count_new[feature_id] = 1 + active_feat_linsys_count[feature_id];\n }\n\n if (active_feat_linsys_count_new.at(feature_id) > 3)\n {\n Eigen::Matrix3d A = active_feat_linsys_A_new[feature_id];\n Eigen::Vector3d b = active_feat_linsys_b_new[feature_id];\n Eigen::MatrixXd p_FinG = A.colPivHouseholderQr().solve(b);\n Eigen::MatrixXd p_FinCi = R_GtoCi * (p_FinG - p_CiinG);\n\n Eigen::JacobiSVD svd(A);\n Eigen::MatrixXd singularValues;\n singularValues.resize(svd.singularValues().rows(), 1);\n singularValues = svd.singularValues();\n double cond_A = singularValues(0, 0) / singularValues(singularValues.rows() - 1, 0);\n\n if (std::abs(cond_A) <= odometry_options.featinit_options.max_cond_number && p_FinCi(2, 0) >= odometry_options.featinit_options.min_dist &&\n p_FinCi(2, 0) <= odometry_options.featinit_options.max_dist && !std::isnan(p_FinCi.norm()))\n {\n active_tracks_pos_world_new[feature_id] = p_FinG;\n }\n }\n }\n }\n\n // display\n cv::Mat stereo_image;\n cv::vconcat(color_left, color_right, stereo_image);\n\n /*\n cv::imshow(\"Stereo Image\", stereo_image);\n cv::waitKey(1);\n */\n pubFeatImage(stereo_image, active_tracks_time);\n\n img_left.release();\n img_right.release();\n\n color_left.release();\n color_right.release();\n\n stereo_image.release();\n // display\n\n size_t total_triangulated = active_tracks_pos_world.size();\n\n active_feat_linsys_A = active_feat_linsys_A_new;\n active_feat_linsys_b = active_feat_linsys_b_new;\n active_feat_linsys_count = active_feat_linsys_count_new;\n active_tracks_pos_world = active_tracks_pos_world_new;\n retri_rT2 = boost::posix_time::microsec_clock::local_time();\n\n if (active_tracks_pos_world.empty() && state_ptr->map_points.empty()) return;\n\n for (const auto &feat : state_ptr->map_points)\n {\n Eigen::Vector3d p_FinG = feat.second->getPointXYZ(false);\n active_tracks_pos_world[feat.second->feature_id] = p_FinG;\n }\n\n std::shared_ptr distortion = state_ptr->cam_intrinsics.at(0);\n std::shared_ptr calibration = state_ptr->calib_cam_imu.at(0);\n Eigen::Matrix R_ItoC = calibration->getRot();\n Eigen::Matrix p_IinC = calibration->getPos();\n\n std::shared_ptr clone_Ii = state_ptr->clones_imu.at(active_tracks_time);\n Eigen::Matrix3d R_GtoIi = clone_Ii->getRot();\n Eigen::Vector3d p_IiinG = clone_Ii->getPos();\n\n for (const auto &feat : active_tracks_pos_world)\n {\n if (feat_uvs_in_cam_0.find(feat.first) == feat_uvs_in_cam_0.end()) continue;\n\n Eigen::Vector3d p_FinIi = R_GtoIi * (feat.second - p_IiinG);\n Eigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n double depth = p_FinCi(2);\n Eigen::Vector2d uv_dist;\n \n if (feat_uvs_in_cam_0.find(feat.first) != feat_uvs_in_cam_0.end())\n {\n uv_dist << (double)feat_uvs_in_cam_0.at(feat.first).x, (double)feat_uvs_in_cam_0.at(feat.first).y;\n }\n else\n {\n Eigen::Vector2d uv_norm;\n uv_norm << p_FinCi(0) / depth, p_FinCi(1) / depth;\n uv_dist = state_ptr->cam_intrinsics_cameras.at(0)->distortD(uv_norm);\n }\n\n if (depth < 0.1) continue;\n\n int width = state_ptr->cam_intrinsics_cameras.at(0)->w();\n int height = state_ptr->cam_intrinsics_cameras.at(0)->h();\n\n if (uv_dist(0) < 0 || (int)uv_dist(0) >= width || uv_dist(1) < 0 || (int)uv_dist(1) >= height)\n {\n continue;\n }\n\n Eigen::Vector3d uvd;\n uvd << uv_dist, depth;\n active_tracks_uvd.insert({feat.first, uvd});\n }\n retri_rT3 = boost::posix_time::microsec_clock::local_time();\n\n // Time test\n /*\n std::cout << std::fixed << \"[triangulateActiveTracks]: \" << (retri_rT2 - retri_rT1).total_microseconds() * 1e-6 << \" seconds for triangulation (\" \n << total_triangulated << \" tri of \" << active_feat_linsys_A.size() << \" active)\" << std::endl;\n\n std::cout << std::fixed << \"[triangulateActiveTracks]: \" << (retri_rT3 - retri_rT2).total_microseconds() * 1e-6 << \" seconds for re-projection into current\" << std::endl;\n\n std::cout << std::fixed << \"[triangulateActiveTracks]: \" << (retri_rT3 - retri_rT1).total_microseconds() * 1e-6 << \" seconds total\" << std::endl;\n */\n // Time test\n}\n void getRecentVoxel(double timestamp, pcl::PointCloud::Ptr voxels_visit) {\n std::vector>().swap(recent_voxels);\n\n // display\n voxels_visit->clear();\n // display\n\n for (const auto &point : active_tracks_pos_world)\n {\n short kx = static_cast(point.second[0] / odometry_options.voxel_size);\n short ky = static_cast(point.second[1] / odometry_options.voxel_size);\n short kz = static_cast(point.second[2] / odometry_options.voxel_size);\n\n for (short kxx = kx - odometry_options.nb_voxels_visited; kxx < kx + odometry_options.nb_voxels_visited + 1; kxx++)\n {\n for (short kyy = ky - odometry_options.nb_voxels_visited; kyy < ky + odometry_options.nb_voxels_visited + 1; kyy++)\n {\n for (short kzz = kz - odometry_options.nb_voxels_visited; kzz < kz + odometry_options.nb_voxels_visited + 1; kzz++)\n {\n voxelHashMap::iterator search = voxel_map.find(voxel(kxx, kyy, kzz));\n\n if(search != voxel_map.end())\n {\n auto &voxel_block = (search.value());\n\n if (voxel_block.last_visit_time < timestamp && voxel_block.NumPoints() > 0)\n {\n Eigen::Matrix voxel_temp;\n\n voxel_temp[0] = kxx;\n voxel_temp[1] = kyy;\n voxel_temp[2] = kzz;\n\n recent_voxels.push_back(voxel_temp);\n voxel_block.last_visit_time = timestamp;\n\n // display\n pcl::PointXYZI point_temp;\n \n point_temp.x = kxx >= 0 ? (kxx + 0.5) * odometry_options.voxel_size : (kxx - 0.5) * odometry_options.voxel_size;\n point_temp.y = kyy >= 0 ? (kyy + 0.5) * odometry_options.voxel_size : (kyy - 0.5) * odometry_options.voxel_size;\n point_temp.z = kzz >= 0 ? (kzz + 0.5) * odometry_options.voxel_size : (kzz - 0.5) * odometry_options.voxel_size;\n point_temp.intensity = 1;\n\n voxels_visit->points.push_back(point_temp);\n // display\n }\n }\n }\n }\n }\n }\n}\n bool tryToInitialize(cameraData &image_measurements) {\n camera_queue_init.push_back(image_measurements.timestamp);\n\n assert(newest_fh->timestamp == image_measurements.timestamp);\n frame_queue_init.push_back(newest_fh);\n\n double timestamp;\n Eigen::MatrixXd covariance;\n std::vector> order;\n auto init_rT1 = boost::posix_time::microsec_clock::local_time();\n\n bool success = initializer_ptr->initialize(timestamp, covariance, order, state_ptr->imu_ptr, true);\n\n if (success)\n {\n stateHelper::setInitialCovariance(state_ptr, covariance, order);\n\n state_ptr->timestamp = timestamp;\n startup_time = timestamp;\n\n featureTracker->getFeatureDatabase()->cleanUpOldmeasurements(state_ptr->timestamp);\n featureTracker->setNumFeatures(std::floor((double)odometry_options.num_pts / (double)2.0));\n\n auto init_rT2 = boost::posix_time::microsec_clock::local_time();\n std::cout << std::fixed << \"[initialize]: Successful initialization in \" << (init_rT2 - init_rT1).total_microseconds() * 1e-6 << \" seconds\" << std::endl;\n std::cout << std::fixed << \"[initialize]: position = \" << state_ptr->imu_ptr->getPos()(0) << \", \" << state_ptr->imu_ptr->getPos()(1) << \", \"\n << state_ptr->imu_ptr->getPos()(2) << std::endl;\n std::cout << std::fixed << \"[initialize]: orientation = \" << state_ptr->imu_ptr->getQuat()(0) << \", \" << state_ptr->imu_ptr->getQuat()(1) << \", \" << state_ptr->imu_ptr->getQuat()(2) \n << \", \" << state_ptr->imu_ptr->getQuat()(3) << std::endl;\n std::cout << std::fixed << \"[initialize]: velocity = \" << state_ptr->imu_ptr->getVel()(0) << \", \" << state_ptr->imu_ptr->getVel()(1) << \", \"\n << state_ptr->imu_ptr->getVel()(2) << std::endl;\n std::cout << std::fixed << \"[initialize]: bias accel = \" << state_ptr->imu_ptr->getBa()(0) << \", \" << state_ptr->imu_ptr->getBa()(1) << \", \"\n << state_ptr->imu_ptr->getBa()(2) << std::endl;\n std::cout << std::fixed << \"[initialize]: bias gyro = \" << state_ptr->imu_ptr->getBg()(0) << \", \" << state_ptr->imu_ptr->getBg()(1) << \", \" \n << state_ptr->imu_ptr->getBg()(2) << std::endl;\n\n std::vector camera_timestamps_to_init;\n for (size_t i = 0; i < camera_queue_init.size(); i++)\n {\n if (camera_queue_init.at(i) > timestamp)\n camera_timestamps_to_init.push_back(camera_queue_init.at(i));\n }\n\n std::vector> camera_frames_to_init;\n for (size_t i = 0; i < frame_queue_init.size(); i++)\n {\n if (frame_queue_init.at(i)->timestamp > timestamp)\n camera_frames_to_init.push_back(frame_queue_init.at(i));\n }\n\n size_t clone_rate = (size_t)((double)camera_timestamps_to_init.size() / (double)odometry_options.state_options.max_clone_size) + 1;\n \n for (size_t i = 0; i < camera_timestamps_to_init.size(); i += clone_rate)\n {\n propagator_ptr->propagateAndClone(state_ptr, camera_timestamps_to_init.at(i));\n\n if (state_ptr->clones_frame.find(camera_frames_to_init.at(i)->timestamp) == state_ptr->clones_frame.end())\n state_ptr->clones_frame[camera_frames_to_init.at(i)->timestamp] = camera_frames_to_init.at(i);\n\n stateHelper::marginalizeOldClone(state_ptr);\n }\n\n std::cout << \"[initialize]: Moved the state forward \" << state_ptr->timestamp - timestamp << \" seconds\" << std::endl;\n camera_queue_init.clear();\n\n frame_queue_init.clear();\n\n return true;\n }\n else\n {\n auto init_rT2 = boost::posix_time::microsec_clock::local_time();\n std::cout << std::fixed << \"[initialize]: Failed initialization in \" << (init_rT2 - init_rT1).total_microseconds() * 1e-6 << \" seconds\" << std::endl;\n\n double oldest_time = image_measurements.timestamp - odometry_options.init_options.init_window_time - 0.10;\n\n auto it0 = camera_queue_init.begin();\n \n while (it0 != camera_queue_init.end())\n {\n if (*it0 < oldest_time)\n it0 = camera_queue_init.erase(it0);\n else\n it0++;\n }\n\n auto it1 = frame_queue_init.begin();\n \n while (it1 != frame_queue_init.end())\n {\n if ((*it1)->timestamp < oldest_time)\n {\n assert((*it1)->v_feat_ptr[0].size() == 0);\n assert((*it1)->v_feat_ptr[1].size() == 0);\n (*it1)->release();\n it1 = frame_queue_init.erase(it1);\n }\n else\n it1++;\n }\n\n return false;\n }\n}\n std::queue camera_buffer;\n std::queue imu_buffer;\n std::map camera_last_timestamp;\n odometryOptions odometry_options;\n std::shared_ptr featureTracker;\n std::shared_ptr initializer_ptr;\n double startup_time;\n double current_time;\n double time_newest_imu;\n std::vector>> imu_meas;\n std::vector imu_states;\n std::shared_ptr state_ptr;\n imuData last_imu_data;\n std::shared_ptr propagator_ptr;\n std::shared_ptr updaterMsckf_ptr;\n std::shared_ptr updaterSlam_ptr;\n std::shared_ptr gammaPixel_ptr;\n std::shared_ptr newest_fh;\n std::vector camera_queue_init;\n std::vector> frame_queue_init;\n voxelHashMap voxel_map;\n int frame_count;\n MarginalizeStatus marginalize_status;\n double last_time_image;\n std::vector good_features_msckf;\n double active_tracks_time = -1;\n std::unordered_map active_tracks_pos_world;\n std::unordered_map active_tracks_uvd;\n cv::Mat active_image;\n std::map active_feat_linsys_A;\n std::map active_feat_linsys_b;\n std::map active_feat_linsys_count;\n std::vector> recent_voxels;\n std::ofstream of_statistics;\n boost::posix_time::ptime rT1, rT2, rT3, rT4, rT5, rT6, rT7;\n double timelastupdate = -1;\n double distance = 0;\n double sum_time_1 = 0.0;\n double sum_time_2 = 0.0;\n double sum_time_3 = 0.0;\n double sum_time_4 = 0.0;\n double sum_time_5 = 0.0;\n double sum_time_6 = 0.0;\n double sum_time_7 = 0.0;\n double sum_time_sum = 0.0;\n int n_time_1 = 0;\n int n_time_2 = 0;\n int n_time_3 = 0;\n int n_time_4 = 0;\n int n_time_5 = 0;\n int n_time_6 = 0;\n int n_time_7 = 0;\n int n_time_sum = 0;\n public:\n voxelStereoVio() {\n readParameters();\n\n initialValue();\n\n allocateMemory();\n\n sub_imu = nh.subscribe(imu_topic, 500, &voxelStereoVio::imuHandler, this);\n\n auto sub_img_left = std::make_shared>(nh, image_left_topic, 5);\n auto sub_img_right = std::make_shared>(nh, image_right_topic, 5);\n auto sync = std::make_shared>(SyncStereoImage(10), *sub_img_left, *sub_img_right);\n sync->registerCallback(boost::bind(&voxelStereoVio::stereoImageHandler, this, _1, _2, 0, 1));\n\n sync_cam.push_back(sync);\n sync_subs_cam.push_back(sub_img_left);\n sync_subs_cam.push_back(sub_img_right);\n\n // display\n pub_feat_image = it.advertise(\"camera/stereo_feat_image\", 5);\n pub_odom = nh.advertise(\"vio/odom\", 5);\n pub_path = nh.advertise(\"vio/path\", 5);\n pub_points_history = nh.advertise(\"vio/history_map_points\", 2);\n pub_points_window = nh.advertise(\"vio/window_map_points\", 2);\n pub_voxels_history = nh.advertise(\"vio/history_voxels\", 2);\n pub_voxels_visit = nh.advertise(\"vio/visit_voxels\", 2);\n\n points_history.reset(new pcl::PointCloud());\n points_window.reset(new pcl::PointCloud());\n voxels_history.reset(new pcl::PointCloud());\n voxels_visit.reset(new pcl::PointCloud());\n // display\n\n // odometry_options.recordParameters();\n}\n void readParameters() {\t\n int para_int;\n double para_double;\n bool para_bool;\n std::string str_temp;\n\n nh.param(\"common/image_left_topic\", image_left_topic, \"/cam0/image_raw\");\n nh.param(\"common/image_right_topic\", image_right_topic, \"/cam1/image_raw\");\n nh.param(\"common/imu_topic\", imu_topic, \"/imu0\");\n nh.param(\"output_path\", output_path, \"\");\n\n nh.param(\"state_parameter/use_fej\", para_bool, false); odometry_options.state_options.do_fej = para_bool;\n nh.param(\"state_parameter/calib_cam_extrinsics\", para_bool, false); odometry_options.state_options.do_calib_camera_pose = para_bool;\n nh.param(\"state_parameter/calib_cam_intrinsics\", para_bool, false); odometry_options.state_options.do_calib_camera_intrinsics = para_bool;\n nh.param(\"state_parameter/calib_cam_timeoffset\", para_bool, false); odometry_options.state_options.do_calib_camera_timeoffset = para_bool;\n nh.param(\"state_parameter/calib_imu_intrinsics\", para_bool, false); odometry_options.state_options.do_calib_imu_intrinsics = para_bool;\n nh.param(\"state_parameter/calib_imu_g_sensitivity\", para_bool, false); odometry_options.state_options.do_calib_imu_g_sensitivity = para_bool;\n\n nh.param(\"state_parameter/imu_intrinsics_model\", str_temp, \"kalibr\");\n if (str_temp == \"kalibr\" || str_temp == \"calibrated\") odometry_options.state_options.imu_model = ImuModel::KALIBR;\n else if (str_temp == \"rpng\") odometry_options.state_options.imu_model = ImuModel::RPNG;\n else {\n std::cout << \"Invalid IMU model: \" << str_temp << std::endl;\n std::cout << \"Please select a valid model: kalibr, rpng.\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n if (str_temp == \"calibrated\")\n {\n odometry_options.state_options.do_calib_imu_intrinsics = false;\n odometry_options.state_options.do_calib_imu_g_sensitivity = false;\n }\n\n nh.param(\"state_parameter/max_clones\", para_int, 11); odometry_options.state_options.max_clone_size = para_int;\n nh.param(\"state_parameter/max_slam\", para_int, 25); odometry_options.state_options.max_slam_features = para_int;\n nh.param(\"state_parameter/max_slam_in_update\", para_int, 1000); odometry_options.state_options.max_slam_in_update = para_int;\n nh.param(\"state_parameter/max_msckf_in_update\", para_int, 1000); odometry_options.state_options.max_msckf_in_update = para_int;\n\n nh.param(\"initializer_parameter/init_window_time\", para_double, 1.0); odometry_options.init_options.init_window_time = para_double;\n nh.param(\"initializer_parameter/init_imu_thresh\", para_double, 1.0); odometry_options.init_options.init_imu_thresh = para_double;\n nh.param(\"initializer_parameter/init_max_disparity\", para_double, 1.0); odometry_options.init_options.init_max_disparity = para_double;\n nh.param(\"initializer_parameter/init_max_features\", para_int, 50); odometry_options.init_options.init_max_features = para_int;\n nh.param(\"initializer_parameter/init_dyn_use\", para_bool, false); odometry_options.init_options.init_dyn_use = para_bool;\n nh.param(\"initializer_parameter/init_dyn_mle_opt_calib\", para_bool, false); odometry_options.init_options.init_dyn_mle_opt_calib = para_bool;\n nh.param(\"initializer_parameter/init_dyn_mle_max_iter\", para_int, 20); odometry_options.init_options.init_dyn_mle_max_iter = para_int;\n nh.param(\"initializer_parameter/init_dyn_mle_max_threads\", para_int, 20); odometry_options.init_options.init_dyn_mle_max_threads = para_int;\n nh.param(\"initializer_parameter/init_dyn_mle_max_time\", para_double, 5.0); odometry_options.init_options.init_dyn_mle_max_time = para_double;\n nh.param(\"initializer_parameter/init_dyn_num_pose\", para_int, 5); odometry_options.init_options.init_dyn_num_pose = para_int;\n nh.param(\"initializer_parameter/init_dyn_min_deg\", para_double, 45.0); odometry_options.init_options.init_dyn_min_deg = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_ori\", para_double, 10.0); odometry_options.init_options.init_dyn_inflation_orientation = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_vel\", para_double, 10.0); odometry_options.init_options.init_dyn_inflation_velocity = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_bg\", para_double, 100.0); odometry_options.init_options.init_dyn_inflation_bias_gyro = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_ba\", para_double, 100.0); odometry_options.init_options.init_dyn_inflation_bias_accel = para_double;\n nh.param(\"initializer_parameter/init_dyn_min_rec_cond\", para_double, 1e-15); odometry_options.init_options.init_dyn_min_rec_cond = para_double;\n\n std::vector v_bias_acc, v_bias_gyr;\n nh.param>(\"initializer_parameter/init_dyn_bias_g\", v_bias_gyr, std::vector());\n nh.param>(\"initializer_parameter/init_dyn_bias_a\", v_bias_acc, std::vector());\n odometry_options.init_options.init_dyn_bias_g << v_bias_gyr.at(0), v_bias_gyr.at(1), v_bias_gyr.at(2);\n odometry_options.init_options.init_dyn_bias_a << v_bias_acc.at(0), v_bias_acc.at(1), v_bias_acc.at(2);\n\n nh.param(\"initializer_parameter/gravity_mag\", para_double, 9.81); odometry_options.init_options.gravity_mag = para_double;\n nh.param(\"initializer_parameter/downsample_cameras\", para_bool, false); odometry_options.init_options.downsample_cameras = para_bool;\n \n double calib_camimu_dt_left, calib_camimu_dt_right;\n nh.param(\"camera_parameter/timeshift_cam_imu_left\", calib_camimu_dt_left, 0.0);\n nh.param(\"camera_parameter/timeshift_cam_imu_right\", calib_camimu_dt_right, 0.0);\n odometry_options.calib_camimu_dt = calib_camimu_dt_left;\n\n std::string dist_model_left, dist_model_right;\n nh.param(\"camera_parameter/distortion_model_left\", dist_model_left, \"radtan\");\n nh.param(\"camera_parameter/distortion_model_right\", dist_model_right, \"radtan\");\n\n std::vector cam_calib_1_left = {1, 1, 0, 0};\n std::vector cam_calib_1_right = {1, 1, 0, 0};\n std::vector cam_calib_2_left = {0, 0, 0, 0};\n std::vector cam_calib_2_right = {0, 0, 0, 0};\n nh.param>(\"camera_parameter/intrinsics_left\", cam_calib_1_left, std::vector());\n nh.param>(\"camera_parameter/intrinsics_right\", cam_calib_1_right, std::vector());\n nh.param>(\"camera_parameter/distortion_coeffs_left\", cam_calib_2_left, std::vector());\n nh.param>(\"camera_parameter/distortion_coeffs_right\", cam_calib_2_right, std::vector());\n Eigen::VectorXd cam_calib_left = Eigen::VectorXd::Zero(8);\n Eigen::VectorXd cam_calib_right = Eigen::VectorXd::Zero(8);\n cam_calib_left << cam_calib_1_left.at(0), cam_calib_1_left.at(1), cam_calib_1_left.at(2), cam_calib_1_left.at(3), \n cam_calib_2_left.at(0), cam_calib_2_left.at(1), cam_calib_2_left.at(2), cam_calib_2_left.at(3);\n cam_calib_right << cam_calib_1_right.at(0), cam_calib_1_right.at(1), cam_calib_1_right.at(2), cam_calib_1_right.at(3), \n cam_calib_2_right.at(0), cam_calib_2_right.at(1), cam_calib_2_right.at(2), cam_calib_2_right.at(3);\n\n cam_calib_left(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_left(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_left(2) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_left(3) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n cam_calib_right(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_right(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_right(2) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_right(3) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n std::vector matrix_wh_left = {1, 1};\n nh.param>(\"camera_parameter/resolution_left\", matrix_wh_left, std::vector());\n matrix_wh_left.at(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n matrix_wh_left.at(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n std::vector matrix_wh_right = {1, 1};\n nh.param>(\"camera_parameter/resolution_right\", matrix_wh_right, std::vector());\n matrix_wh_right.at(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n matrix_wh_right.at(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n assert(matrix_wh_left.at(0) == matrix_wh_right.at(0));\n assert(matrix_wh_left.at(1) == matrix_wh_right.at(1));\n\n wG[0] = matrix_wh_left.at(0);\n hG[0] = matrix_wh_left.at(1);\n\n std::vector v_T_imu_cam_left;\n nh.param>(\"camera_parameter/T_imu_cam_left\", v_T_imu_cam_left, std::vector());\n Eigen::Matrix4d T_imu_cam_left = mat44FromArray(v_T_imu_cam_left);\n\n std::vector v_T_imu_cam_right;\n nh.param>(\"camera_parameter/T_imu_cam_right\", v_T_imu_cam_right, std::vector());\n Eigen::Matrix4d T_imu_cam_right = mat44FromArray(v_T_imu_cam_right);\n\n Eigen::Matrix cam_eigen_left;\n cam_eigen_left.block<4, 1>(0, 0) = quatType::rotToQuat(T_imu_cam_left.block<3, 3>(0, 0).transpose());\n cam_eigen_left.block<3, 1>(4, 0) = - T_imu_cam_left.block<3, 3>(0, 0).transpose() * T_imu_cam_left.block<3, 1>(0, 3);\n\n Eigen::Matrix cam_eigen_right;\n cam_eigen_right.block<4, 1>(0, 0) = quatType::rotToQuat(T_imu_cam_right.block<3, 3>(0, 0).transpose());\n cam_eigen_right.block<3, 1>(4, 0) = - T_imu_cam_right.block<3, 3>(0, 0).transpose() * T_imu_cam_right.block<3, 1>(0, 3);\n\n if (dist_model_left == \"equidistant\")\n {\n odometry_options.init_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.init_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n\n odometry_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n }\n else\n {\n odometry_options.init_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.init_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n\n odometry_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n }\n odometry_options.init_options.camera_extrinsics.insert({0, cam_eigen_left});\n odometry_options.camera_extrinsics.insert({0, cam_eigen_left});\n\n if (dist_model_right == \"equidistant\")\n {\n odometry_options.init_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.init_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n\n odometry_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n }\n else\n {\n odometry_options.init_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.init_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n\n odometry_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n }\n odometry_options.init_options.camera_extrinsics.insert({1, cam_eigen_right});\n odometry_options.camera_extrinsics.insert({1, cam_eigen_right});\n\n nh.param(\"odometry_parameter/use_mask\", para_bool, false); odometry_options.use_mask = para_bool;\n if (odometry_options.use_mask)\n {\n std::string mask_left_path, mask_right_path;\n nh.param(\"camera_parameter/mask_left_path\", str_temp, \"\"); mask_left_path = str_temp;\n nh.param(\"camera_parameter/mask_right_path\", str_temp, \"\"); mask_right_path = str_temp;\n\n if (!boost::filesystem::exists(mask_left_path))\n {\n std::cout << \"Invalid mask path: \" << mask_left_path << std::endl;\n std::exit(EXIT_FAILURE);\n }\n cv::Mat mask_left = cv::imread(mask_left_path, cv::IMREAD_GRAYSCALE);\n\n if (!boost::filesystem::exists(mask_right_path))\n {\n std::cout << \"Invalid mask path: \" << mask_right_path << std::endl;\n std::exit(EXIT_FAILURE);\n }\n cv::Mat mask_right = cv::imread(mask_right_path, cv::IMREAD_GRAYSCALE);\n\n if (mask_left.cols != odometry_options.camera_intrinsics.at(0)->w() || mask_left.rows != odometry_options.camera_intrinsics.at(0)->h())\n {\n std::cout << \"Mask size does not match left camera!\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n if (mask_right.cols != odometry_options.camera_intrinsics.at(1)->w() || mask_right.rows != odometry_options.camera_intrinsics.at(1)->h())\n {\n std::cout << \"Mask size does not match right camera!\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n odometry_options.masks.insert({0, mask_left});\n odometry_options.masks.insert({1, mask_right});\n }\n\n nh.param(\"imu_parameter/gyroscope_noise_density\", para_double, 1.6968e-04); odometry_options.init_options.sigma_w = para_double;\n nh.param(\"imu_parameter/gyroscope_random_walk\", para_double, 1.9393e-05); odometry_options.init_options.sigma_wb = para_double;\n nh.param(\"imu_parameter/accelerometer_noise_density\", para_double, 2.0000e-3); odometry_options.init_options.sigma_a = para_double;\n nh.param(\"imu_parameter/accelerometer_random_walk\", para_double, 3.0000e-03); odometry_options.init_options.sigma_ab = para_double;\n nh.param(\"imu_parameter/sigma_pix\", para_double, 1.0); odometry_options.init_options.sigma_pix = para_double;\n\n std::vector v_Tw;\n nh.param>(\"imu_parameter/Tw\", v_Tw, std::vector());\n Eigen::Matrix3d Tw = mat33FromArray(v_Tw);\n std::vector v_Ta;\n nh.param>(\"imu_parameter/Ta\", v_Ta, std::vector());\n Eigen::Matrix3d Ta = mat33FromArray(v_Ta);\n std::vector v_R_acc_imu;\n nh.param>(\"imu_parameter/R_acc_imu\", v_R_acc_imu, std::vector());\n Eigen::Matrix3d R_acc_imu = mat33FromArray(v_R_acc_imu);\n std::vector v_R_gyr_imu;\n nh.param>(\"imu_parameter/R_gyr_imu\", v_R_gyr_imu, std::vector());\n Eigen::Matrix3d R_gyr_imu = mat33FromArray(v_R_gyr_imu);\n std::vector v_Tg;\n nh.param>(\"imu_parameter/Tg\", v_Tg, std::vector());\n Eigen::Matrix3d Tg = mat33FromArray(v_Tg);\n\n Eigen::Matrix3d Dw = Tw.colPivHouseholderQr().solve(Eigen::Matrix3d::Identity());\n Eigen::Matrix3d Da = Ta.colPivHouseholderQr().solve(Eigen::Matrix3d::Identity());\n Eigen::Matrix3d R_imu_acc = R_acc_imu.transpose();\n Eigen::Matrix3d R_imu_gyr = R_gyr_imu.transpose();\n\n if (std::isnan(Tw.norm()) || std::isnan(Dw.norm()))\n {\n std::cout << \"gyroscope has bad intrinsic values!\" << std::endl;\n std::cout << \"Tw = \" << Tw << std::endl;\n std::cout << \"Dw = \" << Dw << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n if (std::isnan(Ta.norm()) || std::isnan(Da.norm()))\n {\n std::cout << \"accelerometer has bad intrinsic values!\" << std::endl;\n std::cout << \"Ta = \" << Ta << std::endl;\n std::cout << \"Da = \" << Da << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n if (odometry_options.state_options.imu_model == ImuModel::KALIBR)\n {\n odometry_options.vec_dw << Dw.block<3, 1>(0, 0), Dw.block<2, 1>(1, 1), Dw(2, 2);\n odometry_options.vec_da << Da.block<3, 1>(0, 0), Da.block<2, 1>(1, 1), Da(2, 2);\n }\n else\n {\n odometry_options.vec_dw << Dw(0, 0), Dw.block<2, 1>(0, 1), Dw.block<3, 1>(0, 2);\n odometry_options.vec_da << Da(0, 0), Da.block<2, 1>(0, 1), Da.block<3, 1>(0, 2);\n }\n\n odometry_options.vec_tg << Tg.block<3, 1>(0, 0), Tg.block<3, 1>(0, 1), Tg.block<3, 1>(0, 2);\n odometry_options.q_imu_acc = quatType::rotToQuat(R_imu_acc);\n odometry_options.q_imu_gyr = quatType::rotToQuat(R_imu_gyr);\n\n nh.param(\"odometry_parameter/dt_slam_delay\", para_double, 2.0); odometry_options.dt_slam_delay = para_double;\n\n odometry_options.imu_noises.sigma_w = odometry_options.init_options.sigma_w;\n odometry_options.imu_noises.sigma_wb = odometry_options.init_options.sigma_wb;\n odometry_options.imu_noises.sigma_a = odometry_options.init_options.sigma_a;\n odometry_options.imu_noises.sigma_ab = odometry_options.init_options.sigma_ab;\n odometry_options.imu_noises.sigma_w_2 = std::pow(odometry_options.imu_noises.sigma_w, 2);\n odometry_options.imu_noises.sigma_wb_2 = std::pow(odometry_options.imu_noises.sigma_wb, 2);\n odometry_options.imu_noises.sigma_a_2 = std::pow(odometry_options.imu_noises.sigma_a, 2);\n odometry_options.imu_noises.sigma_ab_2 = std::pow(odometry_options.imu_noises.sigma_ab, 2);\n\n nh.param(\"odometry_parameter/up_msckf_sigma_px\", para_double, 1.0); odometry_options.msckf_options.sigma_pix = para_double;\n nh.param(\"odometry_parameter/up_msckf_chi2_multipler\", para_double, 5.0); odometry_options.msckf_options.chi2_multipler = para_double;\n nh.param(\"odometry_parameter/up_slam_sigma_px\", para_double, 1.0); odometry_options.slam_options.sigma_pix = para_double;\n nh.param(\"odometry_parameter/up_slam_chi2_multipler\", para_double, 5.0); odometry_options.slam_options.chi2_multipler = para_double;\n odometry_options.msckf_options.sigma_pix_sq = std::pow(odometry_options.msckf_options.sigma_pix, 2);\n odometry_options.slam_options.sigma_pix_sq = std::pow(odometry_options.slam_options.sigma_pix, 2);\n\n nh.param(\"odometry_parameter/downsample_cameras\", para_bool, false); odometry_options.downsample_cameras = para_bool;\n nh.param(\"odometry_parameter/num_pts\", para_int, 150); odometry_options.num_pts = para_int;\n nh.param(\"odometry_parameter/fast_threshold\", para_int, 20); odometry_options.fast_threshold = para_int;\n nh.param(\"odometry_parameter/patch_size_x\", para_int, 5); odometry_options.patch_size_x = para_int;\n nh.param(\"odometry_parameter/patch_size_y\", para_int, 5); odometry_options.patch_size_y = para_int;\n nh.param(\"odometry_parameter/min_px_dist\", para_int, 10); odometry_options.min_px_dist = para_int;\n nh.param(\"odometry_parameter/histogram_method\", str_temp, \"histogram\");\n if (str_temp == \"none\") odometry_options.histogram_method = HistogramMethod::NONE;\n else if (str_temp == \"histogram\") odometry_options.histogram_method = HistogramMethod::HISTOGRAM;\n else if (str_temp == \"clahe\") odometry_options.histogram_method = HistogramMethod::CLAHE;\n else {\n std::cout << \"Invalid feature histogram specified: \" << str_temp << std::endl;\n std::cout << \"Please select a valid histogram method: none, histogram, clahe.\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n nh.param(\"odometry_parameter/track_frequency\", para_double, 20.0); odometry_options.track_frequency = para_double;\n\n nh.param(\"odometry_parameter/use_huber\", para_bool, true); odometry_options.state_options.use_huber = para_bool;\n nh.param(\"odometry_parameter/use_keyframe\", para_bool, true); odometry_options.use_keyframe = para_bool;\n\n nh.param(\"feature_parameter/refine_features\", para_bool, true); odometry_options.featinit_options.refine_features = para_bool;\n nh.param(\"feature_parameter/max_runs\", para_int, 5); odometry_options.featinit_options.max_runs = para_int;\n nh.param(\"feature_parameter/init_lamda\", para_double, 1e-3); odometry_options.featinit_options.init_lamda = para_double;\n nh.param(\"feature_parameter/max_lamda\", para_double, 1e10); odometry_options.featinit_options.max_lamda = para_double;\n nh.param(\"feature_parameter/min_dx\", para_double, 1e-6); odometry_options.featinit_options.min_dx = para_double;\n nh.param(\"feature_parameter/min_dcost\", para_double, 1e-6); odometry_options.featinit_options.min_dcost = para_double;\n nh.param(\"feature_parameter/lam_mult\", para_double, 10.0); odometry_options.featinit_options.lam_mult = para_double;\n nh.param(\"feature_parameter/min_dist\", para_double, 0.10); odometry_options.featinit_options.min_dist = para_double;\n nh.param(\"feature_parameter/max_dist\", para_double, 60.0); odometry_options.featinit_options.max_dist = para_double;\n nh.param(\"feature_parameter/max_baseline\", para_double, 40.0); odometry_options.featinit_options.max_baseline = para_double;\n nh.param(\"feature_parameter/max_cond_number\", para_double, 10000.0); odometry_options.featinit_options.max_cond_number = para_double;\n\n nh.param(\"feature_parameter/keyframe_parallax\", para_int, 10); setting_min_parallax = para_int;\n setting_min_parallax = setting_min_parallax / cam_calib_1_left.at(0);\n\n nh.param(\"voxel_parameter/voxel_size\", para_double, 0.1); odometry_options.voxel_size = odometry_options.state_options.voxel_size = para_double;\n nh.param(\"voxel_parameter/max_num_points_in_voxel\", para_int, 5); odometry_options.max_num_points_in_voxel = odometry_options.state_options.max_num_points_in_voxel = para_int;\n nh.param(\"voxel_parameter/min_distance_points\", para_double, 0.03); odometry_options.min_distance_points = odometry_options.state_options.min_distance_points = para_double;\n nh.param(\"voxel_parameter/nb_voxels_visited\", para_int, 1); odometry_options.nb_voxels_visited = para_int;\n nh.param(\"voxel_parameter/use_all_points\", para_bool, false); odometry_options.use_all_points = para_bool;\n}\n void allocateMemory() {\n state_ptr = std::make_shared(odometry_options.state_options);\n\n state_ptr->calib_imu_dw->setValue(odometry_options.vec_dw);\n state_ptr->calib_imu_dw->setFej(odometry_options.vec_dw);\n state_ptr->calib_imu_da->setValue(odometry_options.vec_da);\n state_ptr->calib_imu_da->setFej(odometry_options.vec_da);\n state_ptr->calib_imu_tg->setValue(odometry_options.vec_tg);\n state_ptr->calib_imu_tg->setFej(odometry_options.vec_tg);\n state_ptr->calib_imu_gyr->setValue(odometry_options.q_imu_gyr);\n state_ptr->calib_imu_gyr->setFej(odometry_options.q_imu_gyr);\n state_ptr->calib_imu_acc->setValue(odometry_options.q_imu_acc);\n state_ptr->calib_imu_acc->setFej(odometry_options.q_imu_acc);\n\n Eigen::VectorXd temp_camimu_dt;\n temp_camimu_dt.resize(1);\n temp_camimu_dt(0) = odometry_options.calib_camimu_dt;\n state_ptr->calib_dt_imu_cam->setValue(temp_camimu_dt);\n state_ptr->calib_dt_imu_cam->setFej(temp_camimu_dt);\n\n state_ptr->cam_intrinsics_cameras = odometry_options.camera_intrinsics;\n\n for (int i = 0; i < 2; i++)\n {\n state_ptr->cam_intrinsics.at(i)->setValue(odometry_options.camera_intrinsics.at(i)->getValue());\n state_ptr->cam_intrinsics.at(i)->setFej(odometry_options.camera_intrinsics.at(i)->getValue());\n state_ptr->calib_cam_imu.at(i)->setValue(odometry_options.camera_extrinsics.at(i));\n state_ptr->calib_cam_imu.at(i)->setFej(odometry_options.camera_extrinsics.at(i));\n }\n\n int init_max_features = std::floor((double)odometry_options.init_options.init_max_features / (double)2.0);\n\n featureTracker = std::shared_ptr(new trackKLT(state_ptr->cam_intrinsics_cameras, init_max_features, odometry_options.histogram_method, \n odometry_options.fast_threshold, odometry_options.patch_size_x, odometry_options.patch_size_y, odometry_options.min_px_dist));\n\n propagator_ptr = std::make_shared(odometry_options.imu_noises, odometry_options.gravity_mag);\n\n initializer_ptr = std::make_shared(odometry_options.init_options, featureTracker->getFeatureDatabase());\n\n updaterMsckf_ptr = std::make_shared(odometry_options.msckf_options, odometry_options.featinit_options);\n\n updaterSlam_ptr = std::make_shared(odometry_options.slam_options, odometry_options.featinit_options, featureTracker->getFeatureDatabase());\n\n gammaPixel_ptr = std::make_shared();\n\n int wlvl = wG[0], hlvl = hG[0];\n\n while(wlvl % 2 == 0 && hlvl % 2 == 0 && wlvl * hlvl > 5000 && pyr_levels_used < PYR_LEVELS)\n {\n wlvl /=2;\n hlvl /=2;\n pyr_levels_used++;\n }\n\n for (int level = 1; level < pyr_levels_used; ++ level)\n {\n wG[level] = wG[0] >> level;\n hG[level] = hG[0] >> level;\n }\n\n frame_count = 0;\n}\n void initialValue() {\n last_time_image = -1;\n\n last_imu_data.timestamp = -1;\n last_imu_data.gyr.setZero();\n last_imu_data.acc.setZero();\n\n startup_time = -1;\n current_time = -1;\n\n time_newest_imu = -1;\n}\n void imuHandler(const sensor_msgs::Imu::ConstPtr &msg) {\n imuData imu_data;\n imu_data.timestamp = msg->header.stamp.toSec();\n imu_data.gyr << msg->angular_velocity.x, msg->angular_velocity.y, msg->angular_velocity.z;\n imu_data.acc << msg->linear_acceleration.x, msg->linear_acceleration.y, msg->linear_acceleration.z;\n\n processImu(imu_data);\n\n time_newest_imu = imu_data.timestamp;\n}\n void stereoImageHandler(const sensor_msgs::ImageConstPtr &msg_0, const sensor_msgs::ImageConstPtr &msg_1, int cam_0, int cam_1) {\n double timestamp = msg_0->header.stamp.toSec();\n double time_delta = 1.0 / odometry_options.track_frequency;\n\n if (camera_last_timestamp.find(cam_0) != camera_last_timestamp.end() && timestamp < camera_last_timestamp.at(cam_0) + time_delta) return;\n\n camera_last_timestamp[cam_0] = timestamp;\n\n cv_bridge::CvImageConstPtr cv_ptr_0;\n try\n {\n cv_ptr_0 = cv_bridge::toCvShare(msg_0, sensor_msgs::image_encodings::MONO8);\n }\n catch (cv_bridge::Exception &e)\n {\n std::cout << \"cv_bridge exception: \" << e.what() << std::endl;\n return;\n }\n\n cv_bridge::CvImageConstPtr cv_ptr_1;\n try\n {\n cv_ptr_1 = cv_bridge::toCvShare(msg_1, sensor_msgs::image_encodings::MONO8);\n }\n catch (cv_bridge::Exception &e)\n {\n std::cout << \"cv_bridge exception: \" << e.what() << std::endl;\n return;\n }\n\n cameraData camera_data;\n camera_data.timestamp = cv_ptr_0->header.stamp.toSec();\n camera_data.camera_ids.push_back(cam_0);\n camera_data.camera_ids.push_back(cam_1);\n camera_data.images.push_back(cv_ptr_0->image.clone());\n camera_data.images.push_back(cv_ptr_1->image.clone());\n\n if (odometry_options.use_mask)\n {\n camera_data.masks.push_back(odometry_options.masks.at(cam_0));\n camera_data.masks.push_back(odometry_options.masks.at(cam_1));\n }\n else\n {\n camera_data.masks.push_back(cv::Mat::zeros(cv_ptr_0->image.rows, cv_ptr_0->image.cols, CV_8UC1));\n camera_data.masks.push_back(cv::Mat::zeros(cv_ptr_1->image.rows, cv_ptr_1->image.cols, CV_8UC1));\n }\n\n camera_buffer.push(camera_data);\n}\n void run() {\n double timestamp_imu_in_camera = time_newest_imu - state_ptr->calib_dt_imu_cam->value()(0);\n\n while (!camera_buffer.empty() && camera_buffer.front().timestamp < timestamp_imu_in_camera)\n {\n // time test\n boost::posix_time::ptime rT_begin_sum = boost::posix_time::microsec_clock::local_time();\n // time test\n\n processImage(camera_buffer.front());\n\n // time test\n boost::posix_time::ptime rT_end_sum = boost::posix_time::microsec_clock::local_time();\n double time_cost = (rT_end_sum - rT_begin_sum).total_microseconds() * 1e-6;\n sum_time_sum = sum_time_sum * n_time_sum + time_cost;\n n_time_sum++;\n sum_time_sum = sum_time_sum / n_time_sum;\n std::cout << std::fixed << std::setprecision(6) << \"[total] time cost = \" << sum_time_sum << std::endl;\n // time test\n\n camera_buffer.pop();\n }\n}\n ros::NodeHandle nh;\n std::string image_left_topic;\n std::string image_right_topic;\n std::string imu_topic;\n ros::Subscriber sub_imu;\n std::vector subs_cam;\n std::vector>> sync_cam;\n std::vector>> sync_subs_cam;\n void pubFeatImage(cv::Mat &stereo_image, double ×tamp) {\n if (!stereo_image.empty() && pub_feat_image.getNumSubscribers() > 0)\n {\n try {\n sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), \"bgr8\", stereo_image).toImageMsg();\n msg->header.stamp = ros::Time().fromSec(timestamp);\n msg->header.frame_id = \"camera_init\";\n pub_feat_image.publish(msg);\n }\n catch (cv_bridge::Exception& e) {\n ROS_ERROR(\"cv_bridge exception: %s\", e.what());\n }\n }\n}\n void pubOdometry(std::shared_ptr state_ptr, double ×tamp) {\n if (pub_odom.getNumSubscribers() > 0)\n {\n odom.header.frame_id = \"camera_init\";\n odom.child_frame_id = \"body\";\n odom.header.stamp = ros::Time().fromSec(timestamp);\n odom.pose.pose.orientation.x = state_ptr->imu_ptr->getQuat()(0);\n odom.pose.pose.orientation.y = state_ptr->imu_ptr->getQuat()(1);\n odom.pose.pose.orientation.z = state_ptr->imu_ptr->getQuat()(2);\n odom.pose.pose.orientation.w = state_ptr->imu_ptr->getQuat()(3);\n odom.pose.pose.position.x = state_ptr->imu_ptr->getPos()(0);\n odom.pose.pose.position.y = state_ptr->imu_ptr->getPos()(1);\n odom.pose.pose.position.z = state_ptr->imu_ptr->getPos()(2);\n pub_odom.publish(odom);\n }\n}\n void setPoseStamp(geometry_msgs::PoseStamped &body_pose_out, std::shared_ptr state_ptr) {\n body_pose_out.pose.position.x = state_ptr->imu_ptr->getPos()(0);\n body_pose_out.pose.position.y = state_ptr->imu_ptr->getPos()(1);\n body_pose_out.pose.position.z = state_ptr->imu_ptr->getPos()(2);\n \n body_pose_out.pose.orientation.x = state_ptr->imu_ptr->getQuat()(0);\n body_pose_out.pose.orientation.y = state_ptr->imu_ptr->getQuat()(1);\n body_pose_out.pose.orientation.z = state_ptr->imu_ptr->getQuat()(2);\n body_pose_out.pose.orientation.w = state_ptr->imu_ptr->getQuat()(3);\n}\n void pubPath(std::shared_ptr state_ptr, double ×tamp) {\n if (pub_path.getNumSubscribers() > 0)\n {\n setPoseStamp(msg_body_pose, state_ptr);\n msg_body_pose.header.stamp = ros::Time().fromSec(timestamp);\n msg_body_pose.header.frame_id = \"camera_init\";\n\n static int i = 0;\n i++;\n if (i % 10 == 0) \n {\n path.poses.push_back(msg_body_pose);\n path.header.stamp = ros::Time().fromSec(timestamp);\n path.header.frame_id =\"camera_init\";\n pub_path.publish(path);\n }\n }\n}\n void pubHistoryPoints(pcl::PointCloud::Ptr points_history, double ×tamp) {\n if (pub_points_history.getNumSubscribers() > 0)\n {\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*points_history, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_points_history.publish(point_cloud_msg);\n }\n}\n void pubWindowPoints(std::shared_ptr state_ptr, double ×tamp) {\n points_window->clear();\n\n if (pub_points_window.getNumSubscribers() > 0)\n {\n auto it0 = state_ptr->map_points.begin();\n while (it0 != state_ptr->map_points.end())\n {\n pcl::PointXYZRGB point_temp;\n point_temp.x = (*it0).second->getPointXYZ(false)[0];\n point_temp.y = (*it0).second->getPointXYZ(false)[1];\n point_temp.z = (*it0).second->getPointXYZ(false)[2];\n point_temp.r = (*it0).second->color;\n point_temp.g = (*it0).second->color;\n point_temp.b = (*it0).second->color;\n points_window->points.push_back(point_temp);\n\n it0++;\n }\n\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*points_window, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_points_window.publish(point_cloud_msg);\n }\n}\n void pubHistoryVoxels(pcl::PointCloud::Ptr voxels_history, double ×tamp) {\n if (pub_voxels_history.getNumSubscribers() > 0)\n {\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*voxels_history, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_voxels_history.publish(point_cloud_msg);\n }\n\n voxels_history->clear();\n}\n void pubVisitVoxels(pcl::PointCloud::Ptr voxels_visit, double ×tamp) {\n if (pub_voxels_visit.getNumSubscribers() > 0)\n {\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*voxels_visit, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_voxels_visit.publish(point_cloud_msg);\n }\n}\n image_transport::ImageTransport it;\n image_transport::Publisher pub_feat_image;\n ros::Publisher pub_odom;\n ros::Publisher pub_path;\n ros::Publisher pub_points_history;\n ros::Publisher pub_points_window;\n ros::Publisher pub_voxels_history;\n ros::Publisher pub_voxels_visit;\n geometry_msgs::PoseStamped msg_body_pose;\n nav_msgs::Odometry odom;\n nav_msgs::Path path;\n pcl::PointCloud::Ptr points_history;\n pcl::PointCloud::Ptr points_window;\n pcl::PointCloud::Ptr voxels_history;\n pcl::PointCloud::Ptr voxels_visit;\n};"], ["/voxel_svio/include/initializer.h", "class staticInitializer {\n public:\n inertialInitializer(inertialInitializerOptions &initializer_options_, std::shared_ptr db_) {\n\timu_data = std::make_shared>();\n\n\tstatic_initializer = std::make_shared(initializer_options, db, imu_data);\n\tdynamic_initializer = std::make_shared(initializer_options, db, imu_data);\n}\n void feedImu(const imuData &imu_data_, double oldest_time = -1) {\n\timu_data->emplace_back(imu_data_);\n\n\tif (oldest_time != -1)\n\t{\n\t\tauto it0 = imu_data->begin();\n \n\t\twhile (it0 != imu_data->end())\n\t\t{\n\t\t\tif (it0->timestamp < oldest_time)\n\t\t\t\tit0 = imu_data->erase(it0);\n\t\t\telse\n\t\t\t\tit0++;\n\t\t}\n\t}\n}\n bool initialize(double ×tamp, Eigen::MatrixXd &covariance, std::vector> &order,\n std::shared_ptr imu_state_, bool wait_for_jerk = true) {\n\tauto rT1 = boost::posix_time::microsec_clock::local_time();\n\tdouble newest_cam_time = -1;\n\tfor (auto const &feat : db->getInternalData())\n\t{\n\t\tfor (auto const &cam_time_pair : feat.second->timestamps)\n\t\t{\n\t\t\tfor (auto const &time : cam_time_pair.second)\n\t\t\t{\n\t\t\t\tnewest_cam_time = std::max(newest_cam_time, time);\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble oldest_time = newest_cam_time - initializer_options.init_window_time;\n\n\tif (newest_cam_time < 0 || oldest_time < 0) return false;\n\n\tdb->cleanUpOldmeasurements(oldest_time);\n \n\tbool have_old_imu_readings = false;\n\tauto it_imu = imu_data->begin();\n\n\twhile (it_imu != imu_data->end() && it_imu->timestamp < oldest_time + initializer_options.calib_camimu_dt)\n\t{\n\t\thave_old_imu_readings = true;\n\t\tit_imu = imu_data->erase(it_imu);\n\t}\n\n\tif (db->getInternalData().size() < 0.75 * initializer_options.init_max_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Only \" << db->getInternalData().size() << \" valid features of required (\" \n\t\t\t<< 0.95 * initializer_options.init_max_features << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tif (imu_data->size() < 2 || !have_old_imu_readings)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Waiting for window to reach full size (\" << imu_data->size() << \" imu readings)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::unordered_map> features;\n\n\tfor (const auto &feat : db->getInternalData())\n\t{\n\t\tauto feat_new = std::make_shared();\n\t\tfeat_new->feature_id = feat.second->feature_id;\n\t\tfeat_new->uvs = feat.second->uvs;\n\t\tfeat_new->uvs_norm = feat.second->uvs_norm;\n\t\tfeat_new->timestamps = feat.second->timestamps;\n\t\tfeatures.insert({feat.first, feat_new});\n\t}\n\n\tconst int min_num_meas_to_optimize = (int)initializer_options.init_window_time;\n\tconst int min_valid_features = 8;\n\n\tbool have_stereo = false;\n\tint count_valid_features = 0;\n\tstd::map map_features_num_meas;\n\tint num_measurements = 0;\n\tdouble oldest_camera_time = INFINITY;\n\tstd::map map_camera_times;\n\tmap_camera_times[newest_cam_time] = true;\n\tstd::map map_camera_ids;\n\tdouble pose_dt_avg = initializer_options.init_window_time / (double)(initializer_options.init_dyn_num_pose + 1);\n\n\tfor (auto const &feat : features)\n\t{\n\t\tstd::vector times;\n\t\tstd::map cam_ids;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tfor (double time : cam_time.second)\n\t\t\t{\n\t\t\t\tdouble time_dt = INFINITY;\n\n\t\t\t\tfor (auto const &tmp : map_camera_times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp.first));\n\n\t\t\t\tfor (auto const &tmp : times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp));\n\n\t\t\t\tif (time_dt >= pose_dt_avg || time_dt == 0.0)\n\t\t\t\t{\n\t\t\t\t\ttimes.push_back(time);\n\t\t\t\t\tcam_ids[cam_time.first] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap_features_num_meas[feat.first] = (int)times.size();\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tfor (auto const &tmp : times)\n\t\t{\n\t\t\tmap_camera_times[tmp] = true;\n\t\t\toldest_camera_time = std::min(oldest_camera_time, tmp);\n\t\t\tnum_measurements += 2;\n\t\t}\n\n\t\tfor (auto const &tmp : cam_ids)\n\t\t\tmap_camera_ids[tmp.first] = true;\n\n\t\tif (cam_ids.size() > 1) have_stereo = true;\n\n\t\tcount_valid_features++;\n\t}\n\n\tif ((int)map_camera_times.size() < initializer_options.init_dyn_num_pose)\n\t\treturn false;\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Only \" << count_valid_features << \" valid features of required \" << min_valid_features << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Vector3d gyroscope_bias = initializer_options.init_dyn_bias_g;\n\tEigen::Vector3d accelerometer_bias = initializer_options.init_dyn_bias_a;\n\n\tdouble accel_inI_norm = 0.0;\n\tdouble theta_inI_norm = 0.0;\n\tdouble time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\tdouble time_1_in_imu = newest_cam_time + initializer_options.calib_camimu_dt;\n\tstd::vector readings = initializerHelper::selectImuReadings(*imu_data, time_0_in_imu, time_1_in_imu);\n\n\tassert(readings.size() > 2);\n\tfor (size_t k = 0; k < readings.size() - 1; k++)\n\t{\n\t\tauto imu_0 = readings.at(k);\n\t\tauto imu_1 = readings.at(k + 1);\n\t\tdouble dt = imu_1.timestamp - imu_0.timestamp;\n\t\tEigen::Vector3d gyr = 0.5 * (imu_0.gyr + imu_1.gyr) - gyroscope_bias;\n\t\tEigen::Vector3d acc = 0.5 * (imu_0.acc + imu_1.acc) - accelerometer_bias;\n\t\ttheta_inI_norm += ( -gyr * dt).norm();\n\t\taccel_inI_norm += acc.norm();\n\t}\n\n\taccel_inI_norm /= (double)(readings.size() - 1);\n\n\tif (180.0 / M_PI * theta_inI_norm < initializer_options.init_dyn_min_deg)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Gyroscope only \" << 180.0 / M_PI * theta_inI_norm << \" degree change (\" \n\t\t\t<< initializer_options.init_dyn_min_deg << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\tstd::cout << std::fixed << \"[initialize-dynamic]: |theta_I| = \" << 180.0 / M_PI * theta_inI_norm \n\t\t<< \" deg and |accel| = \" << accel_inI_norm << std::endl;\n\n \tauto rT2 = boost::posix_time::microsec_clock::local_time();\n\n\tconst bool use_single_depth = false;\n\tint size_feature = (use_single_depth) ? 1 : 3;\n\tint num_features = count_valid_features;\n\tint system_size = size_feature * num_features + 3 + 3;\n\n\tif (num_measurements < system_size)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Not enough feature measurements (\" << num_measurements << \" meas vs \" << system_size \n\t\t\t<< \" state size)\" << std::endl;\n\n\t\treturn false;\n\t}\n\n\tassert(oldest_camera_time < newest_cam_time);\n\tdouble last_camera_timestamp = 0.0;\n\tstd::map> map_camera_pre_integration_I0toIi, map_camera_pre_integration_IitoIi1; //map_camera_cpi_I0toIi, map_camera_cpi_IitoIi1;\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble current_time = time_pair.first;\n \n\t\tif (current_time == oldest_camera_time)\n\t\t{\n\t\t\tmap_camera_pre_integration_I0toIi.insert({current_time, nullptr});\n\t\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, nullptr});\n\t\t\tlast_camera_timestamp = current_time;\n\t\t\tcontinue;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_I0toIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_I0toIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, initializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_I0toIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_I0toIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_I0toIi1_time_0_in_imu, pre_integration_I0toIi1_time_1_in_imu);\n \n\t\tif (pre_integration_I0toIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) \n\t\t\t\t<< \" in has \" << pre_integration_I0toIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_dt_imu = pre_integration_I0toIi1_readings.at(pre_integration_I0toIi1_readings.size() - 1).timestamp - pre_integration_I0toIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_I0toIi1_dt_imu - (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_I0toIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_I0toIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_I0toIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_I0toIi1_readings.at(k + 1);\n\t\t\tpre_integration_I0toIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_time_0_in_imu = last_camera_timestamp + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_IitoIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_IitoIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, \n\t\t\tinitializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_IitoIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_IitoIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_IitoIi1_time_0_in_imu, pre_integration_IitoIi1_time_1_in_imu);\n\n\t\tif (pre_integration_IitoIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << \" in has \" \n\t\t\t\t<< pre_integration_IitoIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_dt_imu = pre_integration_IitoIi1_readings.at(pre_integration_IitoIi1_readings.size() - 1).timestamp - pre_integration_IitoIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_IitoIi1_dt_imu - (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_IitoIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_IitoIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_IitoIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_IitoIi1_readings.at(k + 1);\n\t\t\tpre_integration_IitoIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tmap_camera_pre_integration_I0toIi.insert({current_time, pre_integration_I0toIi1});\n\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, pre_integration_IitoIi1});\n\t\tlast_camera_timestamp = current_time;\n\t}\n\n\tEigen::MatrixXd A = Eigen::MatrixXd::Zero(num_measurements, system_size);\n\tEigen::VectorXd b = Eigen::VectorXd::Zero(num_measurements);\n\tstd::cout << \"[initialize-dynamic]: System of \" << num_measurements << \" measurement x \" << system_size << \" states created (\" << num_features \n\t\t<< \" features, \" << ((have_stereo) ? \"stereo\" : \"mono\") << \")\" << std::endl;\n\n\tint index_meas = 0;\n\tint idx_feat = 0;\n\tstd::map A_index_features;\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (A_index_features.find(feat.first) == A_index_features.end())\n\t\t{\n\t\t\tA_index_features.insert({feat.first, idx_feat});\n\t\t\tidx_feat += 1;\n\t\t}\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_norm;\n\t\t\t\tuv_norm << (double)feat.second->uvs_norm.at(cam_id).at(i)(0), (double)feat.second->uvs_norm.at(cam_id).at(i)(1);\n\n\t\t\t\tdouble dt = 0.0;\n\t\t\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\t\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\t\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t\t\t{\n\t\t\t\t\tdt = map_camera_pre_integration_I0toIi.at(time)->dt;\n\t\t\t\t\tR_I0toIk = map_camera_pre_integration_I0toIi.at(time)->R_tau_k;\n\t\t\t\t\talpha_I0toIk = map_camera_pre_integration_I0toIi.at(time)->alpha_tau;\n\t\t\t\t}\n\n\t\t\t\tEigen::MatrixXd H_proj = Eigen::MatrixXd::Zero(2, 3);\n\t\t\t\tH_proj << 1, 0, -uv_norm(0), 0, 1, -uv_norm(1);\n\t\t\t\tEigen::MatrixXd Y = H_proj * R_ItoC * R_I0toIk;\n\t\t\t\tEigen::MatrixXd H_i = Eigen::MatrixXd::Zero(2, system_size);\n\t\t\t\tEigen::MatrixXd b_i = Y * alpha_I0toIk - H_proj * p_IinC;\n\n\t\t\t\tif (size_feature == 1)\n\t\t\t\t{\n\t\t\t\t\tassert(false);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tH_i.block(0, size_feature * A_index_features.at(feat.first), 2, 3) = Y;\n\t\t\t\t}\n\n\t\t\t\tH_i.block(0, size_feature * num_features + 0, 2, 3) = -dt * Y;\n\t\t\t\tH_i.block(0, size_feature * num_features + 3, 2, 3) = 0.5 * dt * dt * Y;\n\n\t\t\t\tA.block(index_meas, 0, 2, A.cols()) = H_i;\n\t\t\t\tb.block(index_meas, 0, 2, 1) = b_i;\n\t\t\t\tindex_meas += 2;\n\t\t\t}\n\t\t}\n\t}\n\tauto rT3 = boost::posix_time::microsec_clock::local_time();\n\n\tEigen::MatrixXd A1 = A.block(0, 0, A.rows(), A.cols() - 3);\n\tEigen::MatrixXd A1A1_inv = (A1.transpose() * A1).llt().solve(Eigen::MatrixXd::Identity(A1.cols(), A1.cols()));\n\tEigen::MatrixXd A2 = A.block(0, A.cols() - 3, A.rows(), 3);\n\tEigen::MatrixXd Temp = A2.transpose() * (Eigen::MatrixXd::Identity(A1.rows(), A1.rows()) - A1 * A1A1_inv * A1.transpose());\n\tEigen::MatrixXd D = Temp * A2;\n\tEigen::MatrixXd d = Temp * b;\n\tEigen::Matrix coeff = initializerHelper::computeDongsiCoeff(D, d, initializer_options.gravity_mag);\n\n\tassert(coeff(0) == 1);\n\tEigen::Matrix companion_matrix = Eigen::Matrix::Zero(coeff.rows() - 1, coeff.rows() - 1);\n\tcompanion_matrix.diagonal(-1).setOnes();\n\tcompanion_matrix.col(companion_matrix.cols() - 1) = - coeff.reverse().head(coeff.rows() - 1);\n\tEigen::JacobiSVD> svd_0(companion_matrix);\n\tEigen::MatrixXd singular_values_0 = svd_0.singularValues();\n\tdouble cond_0 = singular_values_0(0) / singular_values_0(singular_values_0.rows() - 1);\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: CM cond = \" << cond_0 << \" | rank = \" << (int)svd_0.rank() << \" of \" << (int)companion_matrix.cols() \n\t\t<< \" (\" << svd_0.threshold() << \" thresh)\" << std::endl;\n\n\tif (svd_0.rank() != companion_matrix.rows())\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Eigenvalue decomposition not full rank\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::EigenSolver> solver(companion_matrix, false);\n \n\tif (solver.info() != Eigen::Success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: failed to compute the eigenvalue decomposition\" << std::endl;\n\t\treturn false;\n\t}\n\n\tbool lambda_found = false;\n\tdouble lambda_min = -1;\n\tdouble cost_min = INFINITY;\n\tEigen::MatrixXd I_dd = Eigen::MatrixXd::Identity(D.rows(), D.rows());\n\n\tfor (int i = 0; i < solver.eigenvalues().size(); i++) {\n\t\tauto val = solver.eigenvalues()(i);\n\t\tif (val.imag() == 0)\n\t\t{\n\t\t\tdouble lambda = val.real();\n\t\t\tEigen::MatrixXd D_lambdaI_inv = (D - lambda * I_dd).llt().solve(I_dd);\n\t\t\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\t\t\tdouble cost = std::abs(state_grav.norm() - initializer_options.gravity_mag);\n\n\t\t\tif (!lambda_found || cost < cost_min)\n\t\t\t{\n\t\t\t\tlambda_found = true;\n\t\t\t\tlambda_min = lambda;\n\t\t\t\tcost_min = cost;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!lambda_found)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Failed to find a real eigenvalue\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Smallest real eigenvalue = \" << lambda_min << \" (cost of \" \n\t\t<< cost_min << \")\" << std::endl;\n\n\tEigen::MatrixXd D_lambdaI_inv = (D - lambda_min * I_dd).llt().solve(I_dd);\n\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\n\tEigen::VectorXd state_feat_vel = - A1A1_inv * A1.transpose() * A2 * state_grav + A1A1_inv * A1.transpose() * b;\n\tEigen::MatrixXd x_hat = Eigen::MatrixXd::Zero(system_size, 1);\n\tx_hat.block(0, 0, size_feature * num_features + 3, 1) = state_feat_vel;\n\tx_hat.block(size_feature * num_features + 3, 0, 3, 1) = state_grav;\n\tEigen::Vector3d v_I0inI0 = x_hat.block(size_feature * num_features + 0, 0, 3, 1);\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Velocity in I0 was \" << v_I0inI0(0) << \", \" << v_I0inI0(1) << \", \" << v_I0inI0(2) \n\t\t<< \" and |v| = \" << v_I0inI0.norm() << std::endl;\n\n\tEigen::Vector3d gravity_inI0 = x_hat.block(size_feature * num_features + 3, 0, 3, 1);\n\tdouble init_max_grav_difference = 1e-3;\n\n\tif (std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) > init_max_grav_difference)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Gravity did not converge (\" << std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) \n\t\t\t<< \" > \" << init_max_grav_difference << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << \"[initialize-dynamic]: Gravity in I0 was \" << gravity_inI0(0) << \", \" << gravity_inI0(1) << \", \" << gravity_inI0(2) << \" and |g| = \" << gravity_inI0.norm() << std::endl;\n\tauto rT4 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::map ori_I0toIi, pos_IiinI0, vel_IiinI0;\n\t\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble time = time_pair.first;\n\n\t\tdouble dt = 0.0;\n\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\tEigen::MatrixXd beta_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\n\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t{\n\t\t\tauto pre_integration = map_camera_pre_integration_I0toIi.at(time);\n\t\t\tdt = pre_integration->dt;\n\t\t\tR_I0toIk = pre_integration->R_tau_k;\n\t\t\talpha_I0toIk = pre_integration->alpha_tau;\n\t\t\tbeta_I0toIk = pre_integration->beta_tau;\n\t\t}\n\n\t\tEigen::Vector3d p_IkinI0 = v_I0inI0 * dt - 0.5 * gravity_inI0 * dt * dt + alpha_I0toIk;\n\t\tEigen::Vector3d v_IkinI0 = v_I0inI0 - gravity_inI0 * dt + beta_I0toIk;\n\n\t\tori_I0toIi.insert({time, quatType::rotToQuat(R_I0toIk)});\n\t\tpos_IiinI0.insert({time, p_IkinI0});\n\t\tvel_IiinI0.insert({time, v_IkinI0});\n\t}\n\n\tcount_valid_features = 0;\n\tstd::map features_inI0;\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tEigen::Vector3d p_FinI0;\n\t\tif (size_feature == 1)\n\t\t\tassert(false);\n\t\telse\n\t\t\tp_FinI0 = x_hat.block(size_feature * A_index_features.at(feat.first), 0, 3, 1);\n\n\t\tbool is_behind = false;\n\t\t\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Vector3d p_FinC0 = quatType::quatToRot(q_ItoC) * p_FinI0 + p_IinC;\n\n\t\t\tif (p_FinC0(2) < 0) is_behind = true;\n\t\t}\n\t\t\n\t\tif (!is_behind)\n\t\t{\n\t\t\tfeatures_inI0.insert({feat.first, p_FinI0});\n\t\t\tcount_valid_features++;\n\t\t}\n\t}\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Not enough features for our mle (\" << count_valid_features << \" < \" \n\t\t\t<< min_valid_features << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Matrix3d R_GtoI0;\n\tinitializerHelper::gramSchmidt(gravity_inI0, R_GtoI0);\n\tEigen::Vector4d q_GtoI0 = quatType::rotToQuat(R_GtoI0);\n\tEigen::Vector3d gravity;\n\tgravity << 0.0, 0.0, initializer_options.gravity_mag;\n\tstd::map ori_GtoIi, pos_IiinG, vel_IiinG;\n\tstd::map features_inG;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tori_GtoIi[time_pair.first] = quatType::quatMultiply(ori_I0toIi.at(time_pair.first), q_GtoI0);\n\t\tpos_IiinG[time_pair.first] = R_GtoI0.transpose() * pos_IiinI0.at(time_pair.first);\n\t\tvel_IiinG[time_pair.first] = R_GtoI0.transpose() * vel_IiinI0.at(time_pair.first);\n\t}\n\n\tfor (auto const &feat : features_inI0)\n\tfeatures_inG[feat.first] = R_GtoI0.transpose() * feat.second;\n\n\tceres::Problem problem;\n\n\tstd::map map_states;\n\tstd::vector ceres_vars_ori;\n\tstd::vector ceres_vars_pos;\n\tstd::vector ceres_vars_vel;\n\tstd::vector ceres_vars_bias_g;\n\tstd::vector ceres_vars_bias_a;\n\n\tstd::map map_features;\n\tstd::vector ceres_vars_feat;\n\n\tstd::map map_calib_cam2imu;\n\tstd::vector ceres_vars_calib_cam2imu_ori;\n\tstd::vector ceres_vars_calib_cam2imu_pos;\n\n\tstd::map map_calib_cam;\n\tstd::vector ceres_vars_calib_cam_intrinsics;\n\n\tauto freeStateMemory = [&]()\n\t{\n\t\tfor (auto const &ptr : ceres_vars_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_vel)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_g)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_a)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_feat)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam_intrinsics)\n\t\t\tdelete[] ptr;\n\t};\n\n\tceres::Solver::Options options;\n\toptions.linear_solver_type = ceres::DENSE_SCHUR;\n\toptions.trust_region_strategy_type = ceres::DOGLEG;\n\toptions.num_threads = initializer_options.init_dyn_mle_max_threads;\n\toptions.max_solver_time_in_seconds = initializer_options.init_dyn_mle_max_time;\n\toptions.max_num_iterations = initializer_options.init_dyn_mle_max_iter;\n\toptions.function_tolerance = 1e-5;\n\toptions.gradient_tolerance = 1e-4 * options.function_tolerance;\n\n\tdouble timestamp_k = -1;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble timestamp_k1 = time_pair.first;\n\t\tstd::shared_ptr pre_integration = map_camera_pre_integration_IitoIi1.at(timestamp_k1);\n\t\tEigen::Matrix state_k1;\n\t\tstate_k1.block(0, 0, 4, 1) = ori_GtoIi.at(timestamp_k1);\n\t\tstate_k1.block(4, 0, 3, 1) = pos_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(7, 0, 3, 1) = vel_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(10, 0, 3, 1) = gyroscope_bias;\n\t\tstate_k1.block(13, 0, 3, 1) = accelerometer_bias;\n\n\t\tauto *var_ori = new double[4];\n\t\tfor (int j = 0; j < 4; j++)\n\t\t\tvar_ori[j] = state_k1(0 + j, 0);\n\n\t\tauto *var_pos = new double[3];\n\t\tauto *var_vel = new double[3];\n\t\tauto *var_bias_g = new double[3];\n\t\tauto *var_bias_a = new double[3];\n\n\t\tfor (int j = 0; j < 3; j++)\n\t\t{\n\t\t\tvar_pos[j] = state_k1(4 + j, 0);\n\t\t\tvar_vel[j] = state_k1(7 + j, 0);\n\t\t\tvar_bias_g[j] = state_k1(10 + j, 0);\n\t\t\tvar_bias_a[j] = state_k1(13 + j, 0);\n\t\t}\n\n\t\tauto ceres_jplquat = new stateJplQuatLocal();\n\t\tproblem.AddParameterBlock(var_ori, 4, ceres_jplquat);\n\t\tproblem.AddParameterBlock(var_pos, 3);\n\t\tproblem.AddParameterBlock(var_vel, 3);\n\t\tproblem.AddParameterBlock(var_bias_g, 3);\n\t\tproblem.AddParameterBlock(var_bias_a, 3);\n\n\t\tif (map_states.empty())\n\t\t{\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(13, 1);\n\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t{\n\t\t\t\tx_lin(4 + j) = var_pos[j];\n\t\t\t\tx_lin(7 + j) = var_bias_g[j];\n\t\t\t\tx_lin(10 + j) = var_bias_a[j];\n\t\t\t}\n\t\t\t\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(10, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(10, 10);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1e-5, 2);\n\t\t\tprior_Info.block(4, 4, 3, 3) *= 1.0 / std::pow(0.05, 2);\n\t\t\tprior_Info.block(7, 7, 3, 3) *= 1.0 / std::pow(0.10, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_ori);\n\t\t\tx_types.emplace_back(\"quat_yaw\");\n\t\t\tfactor_params.push_back(var_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_g);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_a);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\t\t}\n\n\t\tmap_states.insert({timestamp_k1, (int)ceres_vars_ori.size()});\n\t\tceres_vars_ori.push_back(var_ori);\n\t\tceres_vars_pos.push_back(var_pos);\n\t\tceres_vars_vel.push_back(var_vel);\n\t\tceres_vars_bias_g.push_back(var_bias_g);\n\t\tceres_vars_bias_a.push_back(var_bias_a);\n\n\t\tif (pre_integration != nullptr)\n\t\t{\n\t\t\tassert(timestamp_k != -1);\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k1)));\n\t\t\tauto *factor_imu = new factorImuPreIntegrationV1(pre_integration->dt, gravity, pre_integration->alpha_tau, pre_integration->beta_tau, \n\t\t\t\tpre_integration->q_tau_k, pre_integration->b_a_lin, pre_integration->b_w_lin, pre_integration->J_q, pre_integration->J_b, \n\t\t\t\tpre_integration->J_a, pre_integration->H_b, pre_integration->H_a, pre_integration->P_meas);\n\t\t\t\n\t\t\tproblem.AddResidualBlock(factor_imu, nullptr, factor_params);\n\t\t}\n\n\t\ttimestamp_k = timestamp_k1;\n\t}\n\n\tfor (auto const &id_pair : map_camera_ids)\n\t{\n\t\tsize_t cam_id = id_pair.first;\n\t\tif (map_calib_cam2imu.find(cam_id) == map_calib_cam2imu.end())\n\t\t{\n\t\t\tauto *var_calib_ori = new double[4];\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tvar_calib_ori[j] = initializer_options.camera_extrinsics.at(cam_id)(0 + j, 0);\n\n\t\t\tauto *var_calib_pos = new double[3];\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tvar_calib_pos[j] = initializer_options.camera_extrinsics.at(cam_id)(4 + j, 0);\n\n\t\t\tauto ceres_calib_jplquat = new stateJplQuatLocal();\n\t\t\tproblem.AddParameterBlock(var_calib_ori, 4, ceres_calib_jplquat);\n\t\t\tproblem.AddParameterBlock(var_calib_pos, 3);\n\n\t\t\tmap_calib_cam2imu.insert({cam_id, (int)ceres_vars_calib_cam2imu_ori.size()});\n\t\t\tceres_vars_calib_cam2imu_ori.push_back(var_calib_ori);\n\t\t\tceres_vars_calib_cam2imu_pos.push_back(var_calib_pos);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(7, 1);\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tx_lin(4 + j) = var_calib_pos[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(6, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(6, 6);\n\t\t\tprior_Info.block(0, 0, 3, 3) *= 1.0 / std::pow(0.001, 2);\n\t\t\tprior_Info.block(3, 3, 3, 3) *= 1.0 / std::pow(0.01, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_ori);\n\t\t\tx_types.emplace_back(\"quat\");\n\t\t\tfactor_params.push_back(var_calib_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t{\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_ori);\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_pos);\n\t\t\t}\n\t\t}\n\n\t\tif (map_calib_cam.find(cam_id) == map_calib_cam.end())\n\t\t{\n\t\t\tauto *var_calib_cam = new double[8];\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tvar_calib_cam[j] = initializer_options.camera_intrinsics.at(cam_id)->getValue()(j, 0);\n\n\t\t\tproblem.AddParameterBlock(var_calib_cam, 8);\n\t\t\tmap_calib_cam.insert({cam_id, (int)ceres_vars_calib_cam_intrinsics.size()});\n\t\t\tceres_vars_calib_cam_intrinsics.push_back(var_calib_cam);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_cam[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(8, 8);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1.0, 2);\n\t\t\tprior_Info.block(4, 4, 4, 4) *= 1.0 / std::pow(0.005, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_cam);\n\t\t\tx_types.emplace_back(\"vec8\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_cam);\n\t\t}\n\t}\n\tassert(map_calib_cam2imu.size() == map_calib_cam.size());\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (features_inG.find(feat.first) == features_inG.end()) continue;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t feat_id = feat.first;\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tbool is_fisheye = (std::dynamic_pointer_cast(initializer_options.camera_intrinsics.at(cam_id)) != nullptr);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_raw = feat.second->uvs.at(cam_id).at(i).block(0, 0, 2, 1).cast();\n\n\t\t\t\tif (map_features.find(feat_id) == map_features.end())\n\t\t\t\t{\n\t\t\t\t\tauto *var_feat = new double[3];\n\t\t\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\t\t\tvar_feat[j] = features_inG.at(feat_id)(j);\n\n\t\t\t\t\tproblem.AddParameterBlock(var_feat, 3);\n\t\t\t\t\tmap_features.insert({feat_id, (int)ceres_vars_feat.size()});\n\t\t\t\t\tceres_vars_feat.push_back(var_feat);\n\t\t\t\t}\n\n\t\t\t\tstd::vector factor_params;\n\t\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_feat.at(map_features.at(feat_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_ori.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_pos.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam_intrinsics.at(map_calib_cam.at(cam_id)));\n\t\t\t\tauto *factor_pinhole = new factorImageReprojCalib(uv_raw, initializer_options.sigma_pix, is_fisheye);\n\n\t\t\t\tceres::LossFunction *loss_function = new ceres::CauchyLoss(1.0);\n\t\t\t\tproblem.AddResidualBlock(factor_pinhole, loss_function, factor_params);\n\t\t\t}\n\t\t}\n\t}\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_g.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_vel.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_a.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_pos.size());\n\tauto rT5 = boost::posix_time::microsec_clock::local_time();\n\n\tceres::Solver::Summary summary;\n\tceres::Solve(options, &problem, &summary);\n\n\tstd::cout << \"[initialize-dynamic]: \" << (int)summary.iterations.size() << \" iterations | \" << map_states.size() << \" states, \" \n\t\t<< map_features.size() << \" feats (\" << count_valid_features << \" valid) | \" << summary.num_parameters << \" param and \"\n\t\t<< summary.num_residuals << \" res | cost \" << std::fixed << summary.initial_cost << \" => \" << summary.final_cost << std::endl;\n\n\tauto rT6 = boost::posix_time::microsec_clock::local_time();\n\n\ttimestamp = newest_cam_time;\n\tif (initializer_options.init_dyn_mle_max_iter != 0 && summary.termination_type != ceres::CONVERGENCE)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Optimization failed: \" << summary.message << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\tstd::cout << \"[initialize-dynamic]: \" << summary.message << std::endl;\n\n\tauto getPose = [&](double timestamp)\n\t{\n\t\tEigen::VectorXd state_imu = Eigen::VectorXd::Zero(16);\n\n\t\tfor (int i = 0; i < 4; i++)\n\t\t\tstate_imu(0 + i) = ceres_vars_ori[map_states[timestamp]][i];\n\n\t\tfor (int i = 0; i < 3; i++)\n\t\t{\n\t\t\tstate_imu(4 + i) = ceres_vars_pos[map_states[timestamp]][i];\n\t\t\tstate_imu(7 + i) = ceres_vars_vel[map_states[timestamp]][i];\n\t\t\tstate_imu(10 + i) = ceres_vars_bias_g[map_states[timestamp]][i];\n\t\t\tstate_imu(13 + i) = ceres_vars_bias_a[map_states[timestamp]][i];\n\t\t}\n\n\t\treturn state_imu;\n\t};\n\n\tassert(map_states.find(newest_cam_time) != map_states.end());\n\t\n\tif (imu_state_ == nullptr)\n\t\timu_state_ = std::make_shared();\n\n\tEigen::VectorXd imu_state = getPose(newest_cam_time);\n\timu_state_->setValue(imu_state);\n\timu_state_->setFej(imu_state);\n\n\tfor (auto const &state_pair : map_states)\n\t{\n\t\tEigen::VectorXd pose = getPose(state_pair.first);\n\n\t\tif (clones_imu_.find(state_pair.first) == clones_imu_.end())\n\t\t{\n\t\t\tauto pose_ = std::make_shared();\n\t\t\tpose_->setValue(pose.block(0, 0, 7, 1));\n\t\t\tpose_->setFej(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.insert({state_pair.first, pose_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tclones_imu_.at(state_pair.first)->setValue(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.at(state_pair.first)->setFej(pose.block(0, 0, 7, 1));\n\t\t}\n\t}\n\n\tfor (auto const &feat_pair : map_features)\n\t{\n\t\tEigen::Vector3d feature;\n\t\tfeature << ceres_vars_feat[feat_pair.second][0], ceres_vars_feat[feat_pair.second][1], ceres_vars_feat[feat_pair.second][2];\n\n\t\tif (map_points_.find(feat_pair.first) == map_points_.end())\n\t\t{\n\t\t\tauto feature_ = std::make_shared();\n\t\t\tfeature_->feature_id = feat_pair.first;\n\t\t\tfeature_->setPointXYZ(feature, false);\n\t\t\tfeature_->setPointXYZ(feature, true);\n\t\t\tmap_points_.insert({feat_pair.first, feature_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmap_points_.at(feat_pair.first)->feature_id = feat_pair.first;\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, false);\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, true);\n\t\t}\n\t}\n\n\tstd::vector> covariance_blocks;\n\tint state_index = map_states[newest_cam_time];\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_ori[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index]));\n\n\tceres::Covariance::Options options_cov;\n\toptions_cov.null_space_rank = (!initializer_options.init_dyn_mle_opt_calib) * ((int)map_calib_cam2imu.size() * (6 + 8));\n\toptions_cov.min_reciprocal_condition_number = initializer_options.init_dyn_min_rec_cond;\n \n\toptions_cov.apply_loss_function = true;\n\toptions_cov.num_threads = initializer_options.init_dyn_mle_max_threads;\n\n\tceres::Covariance problem_cov(options_cov);\n\tbool success = problem_cov.Compute(covariance_blocks, &problem);\n\n\tif (!success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Covariance recovery failed...\" << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\n\torder.clear();\n\torder.push_back(imu_state_);\n\tcovariance = Eigen::MatrixXd::Zero(imu_state_->getSize(), imu_state_->getSize());\n\tEigen::Matrix cov_tmp = Eigen::Matrix::Zero();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_ori[state_index], cov_tmp.data()));\n\tcovariance.block(0, 0, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(3, 3, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(6, 6, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(9, 9, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(12, 12, 3, 3) = cov_tmp.eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(0, 3, 3, 3) = cov_tmp.eval();\n\tcovariance.block(3, 0, 3, 3) = cov_tmp.transpose();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(0, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(0, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(0, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 0, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(3, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(3, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(3, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 3, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(6, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 6, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(6, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 6, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(9, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 9, 3, 3) = cov_tmp.transpose().eval();\n\n\tcovariance.block(0, 0, 3, 3) *= initializer_options.init_dyn_inflation_orientation;\n\tcovariance.block(6, 6, 3, 3) *= initializer_options.init_dyn_inflation_velocity;\n\tcovariance.block(9, 9, 3, 3) *= initializer_options.init_dyn_inflation_bias_gyro;\n\tcovariance.block(12, 12, 3, 3) *= initializer_options.init_dyn_inflation_bias_accel;\n\n\tcovariance = 0.5 * (covariance + covariance.transpose());\n\tEigen::Vector3d sigmas_vel = covariance.block(6, 6, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_bg = covariance.block(9, 9, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_ba = covariance.block(12, 12, 3, 3).diagonal().transpose().cwiseSqrt();\n\tstd::cout << \"[initialize-dynamic]: vel priors = \" << sigmas_vel(0) << \", \" << sigmas_vel(1) << \", \" << sigmas_vel(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: bg priors = \" << sigmas_bg(0) << \", \" << sigmas_bg(1) << \", \" << sigmas_bg(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: ba priors = \" << sigmas_ba(0) << \", \" << sigmas_ba(1) << \", \" << sigmas_ba(2) << std::endl;\n\n\tEigen::MatrixXd x = imu_state_->value();\n\tx.block(4, 0, 3, 1).setZero();\n\timu_state_->setValue(x);\n\timu_state_->setFej(x);\n\n\tauto rT7 = boost::posix_time::microsec_clock::local_time();\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" sec for prelim tests\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" sec for linsys setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" sec for linsys\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" sec for ceres opt setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" sec for ceres opt\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT6).total_microseconds() * 1e-6 << \" sec for ceres covariance\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT1).total_microseconds() * 1e-6 << \" sec total for initialization\" << std::endl;\n\tfreeStateMemory();\n\n\treturn true;\n}\n protected:\n inertialInitializerOptions initializer_options;\n std::shared_ptr db;\n std::shared_ptr> imu_data;\n std::shared_ptr static_initializer;\n std::shared_ptr dynamic_initializer;\n public:\n explicit staticInitializer(inertialInitializerOptions &initializer_options_, std::shared_ptr db_,\n std::shared_ptr> imu_data_) {\n\n}\n bool initialize(double ×tamp, Eigen::MatrixXd &covariance, std::vector> &order, \n std::shared_ptr imu_state_, bool wait_for_jerk = true) {\n\tauto rT1 = boost::posix_time::microsec_clock::local_time();\n\tdouble newest_cam_time = -1;\n\tfor (auto const &feat : db->getInternalData())\n\t{\n\t\tfor (auto const &cam_time_pair : feat.second->timestamps)\n\t\t{\n\t\t\tfor (auto const &time : cam_time_pair.second)\n\t\t\t{\n\t\t\t\tnewest_cam_time = std::max(newest_cam_time, time);\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble oldest_time = newest_cam_time - initializer_options.init_window_time;\n\n\tif (newest_cam_time < 0 || oldest_time < 0) return false;\n\n\tdb->cleanUpOldmeasurements(oldest_time);\n \n\tbool have_old_imu_readings = false;\n\tauto it_imu = imu_data->begin();\n\n\twhile (it_imu != imu_data->end() && it_imu->timestamp < oldest_time + initializer_options.calib_camimu_dt)\n\t{\n\t\thave_old_imu_readings = true;\n\t\tit_imu = imu_data->erase(it_imu);\n\t}\n\n\tif (db->getInternalData().size() < 0.75 * initializer_options.init_max_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Only \" << db->getInternalData().size() << \" valid features of required (\" \n\t\t\t<< 0.95 * initializer_options.init_max_features << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tif (imu_data->size() < 2 || !have_old_imu_readings)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Waiting for window to reach full size (\" << imu_data->size() << \" imu readings)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::unordered_map> features;\n\n\tfor (const auto &feat : db->getInternalData())\n\t{\n\t\tauto feat_new = std::make_shared();\n\t\tfeat_new->feature_id = feat.second->feature_id;\n\t\tfeat_new->uvs = feat.second->uvs;\n\t\tfeat_new->uvs_norm = feat.second->uvs_norm;\n\t\tfeat_new->timestamps = feat.second->timestamps;\n\t\tfeatures.insert({feat.first, feat_new});\n\t}\n\n\tconst int min_num_meas_to_optimize = (int)initializer_options.init_window_time;\n\tconst int min_valid_features = 8;\n\n\tbool have_stereo = false;\n\tint count_valid_features = 0;\n\tstd::map map_features_num_meas;\n\tint num_measurements = 0;\n\tdouble oldest_camera_time = INFINITY;\n\tstd::map map_camera_times;\n\tmap_camera_times[newest_cam_time] = true;\n\tstd::map map_camera_ids;\n\tdouble pose_dt_avg = initializer_options.init_window_time / (double)(initializer_options.init_dyn_num_pose + 1);\n\n\tfor (auto const &feat : features)\n\t{\n\t\tstd::vector times;\n\t\tstd::map cam_ids;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tfor (double time : cam_time.second)\n\t\t\t{\n\t\t\t\tdouble time_dt = INFINITY;\n\n\t\t\t\tfor (auto const &tmp : map_camera_times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp.first));\n\n\t\t\t\tfor (auto const &tmp : times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp));\n\n\t\t\t\tif (time_dt >= pose_dt_avg || time_dt == 0.0)\n\t\t\t\t{\n\t\t\t\t\ttimes.push_back(time);\n\t\t\t\t\tcam_ids[cam_time.first] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap_features_num_meas[feat.first] = (int)times.size();\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tfor (auto const &tmp : times)\n\t\t{\n\t\t\tmap_camera_times[tmp] = true;\n\t\t\toldest_camera_time = std::min(oldest_camera_time, tmp);\n\t\t\tnum_measurements += 2;\n\t\t}\n\n\t\tfor (auto const &tmp : cam_ids)\n\t\t\tmap_camera_ids[tmp.first] = true;\n\n\t\tif (cam_ids.size() > 1) have_stereo = true;\n\n\t\tcount_valid_features++;\n\t}\n\n\tif ((int)map_camera_times.size() < initializer_options.init_dyn_num_pose)\n\t\treturn false;\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Only \" << count_valid_features << \" valid features of required \" << min_valid_features << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Vector3d gyroscope_bias = initializer_options.init_dyn_bias_g;\n\tEigen::Vector3d accelerometer_bias = initializer_options.init_dyn_bias_a;\n\n\tdouble accel_inI_norm = 0.0;\n\tdouble theta_inI_norm = 0.0;\n\tdouble time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\tdouble time_1_in_imu = newest_cam_time + initializer_options.calib_camimu_dt;\n\tstd::vector readings = initializerHelper::selectImuReadings(*imu_data, time_0_in_imu, time_1_in_imu);\n\n\tassert(readings.size() > 2);\n\tfor (size_t k = 0; k < readings.size() - 1; k++)\n\t{\n\t\tauto imu_0 = readings.at(k);\n\t\tauto imu_1 = readings.at(k + 1);\n\t\tdouble dt = imu_1.timestamp - imu_0.timestamp;\n\t\tEigen::Vector3d gyr = 0.5 * (imu_0.gyr + imu_1.gyr) - gyroscope_bias;\n\t\tEigen::Vector3d acc = 0.5 * (imu_0.acc + imu_1.acc) - accelerometer_bias;\n\t\ttheta_inI_norm += ( -gyr * dt).norm();\n\t\taccel_inI_norm += acc.norm();\n\t}\n\n\taccel_inI_norm /= (double)(readings.size() - 1);\n\n\tif (180.0 / M_PI * theta_inI_norm < initializer_options.init_dyn_min_deg)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Gyroscope only \" << 180.0 / M_PI * theta_inI_norm << \" degree change (\" \n\t\t\t<< initializer_options.init_dyn_min_deg << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\tstd::cout << std::fixed << \"[initialize-dynamic]: |theta_I| = \" << 180.0 / M_PI * theta_inI_norm \n\t\t<< \" deg and |accel| = \" << accel_inI_norm << std::endl;\n\n \tauto rT2 = boost::posix_time::microsec_clock::local_time();\n\n\tconst bool use_single_depth = false;\n\tint size_feature = (use_single_depth) ? 1 : 3;\n\tint num_features = count_valid_features;\n\tint system_size = size_feature * num_features + 3 + 3;\n\n\tif (num_measurements < system_size)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Not enough feature measurements (\" << num_measurements << \" meas vs \" << system_size \n\t\t\t<< \" state size)\" << std::endl;\n\n\t\treturn false;\n\t}\n\n\tassert(oldest_camera_time < newest_cam_time);\n\tdouble last_camera_timestamp = 0.0;\n\tstd::map> map_camera_pre_integration_I0toIi, map_camera_pre_integration_IitoIi1; //map_camera_cpi_I0toIi, map_camera_cpi_IitoIi1;\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble current_time = time_pair.first;\n \n\t\tif (current_time == oldest_camera_time)\n\t\t{\n\t\t\tmap_camera_pre_integration_I0toIi.insert({current_time, nullptr});\n\t\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, nullptr});\n\t\t\tlast_camera_timestamp = current_time;\n\t\t\tcontinue;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_I0toIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_I0toIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, initializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_I0toIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_I0toIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_I0toIi1_time_0_in_imu, pre_integration_I0toIi1_time_1_in_imu);\n \n\t\tif (pre_integration_I0toIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) \n\t\t\t\t<< \" in has \" << pre_integration_I0toIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_dt_imu = pre_integration_I0toIi1_readings.at(pre_integration_I0toIi1_readings.size() - 1).timestamp - pre_integration_I0toIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_I0toIi1_dt_imu - (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_I0toIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_I0toIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_I0toIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_I0toIi1_readings.at(k + 1);\n\t\t\tpre_integration_I0toIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_time_0_in_imu = last_camera_timestamp + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_IitoIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_IitoIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, \n\t\t\tinitializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_IitoIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_IitoIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_IitoIi1_time_0_in_imu, pre_integration_IitoIi1_time_1_in_imu);\n\n\t\tif (pre_integration_IitoIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << \" in has \" \n\t\t\t\t<< pre_integration_IitoIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_dt_imu = pre_integration_IitoIi1_readings.at(pre_integration_IitoIi1_readings.size() - 1).timestamp - pre_integration_IitoIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_IitoIi1_dt_imu - (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_IitoIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_IitoIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_IitoIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_IitoIi1_readings.at(k + 1);\n\t\t\tpre_integration_IitoIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tmap_camera_pre_integration_I0toIi.insert({current_time, pre_integration_I0toIi1});\n\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, pre_integration_IitoIi1});\n\t\tlast_camera_timestamp = current_time;\n\t}\n\n\tEigen::MatrixXd A = Eigen::MatrixXd::Zero(num_measurements, system_size);\n\tEigen::VectorXd b = Eigen::VectorXd::Zero(num_measurements);\n\tstd::cout << \"[initialize-dynamic]: System of \" << num_measurements << \" measurement x \" << system_size << \" states created (\" << num_features \n\t\t<< \" features, \" << ((have_stereo) ? \"stereo\" : \"mono\") << \")\" << std::endl;\n\n\tint index_meas = 0;\n\tint idx_feat = 0;\n\tstd::map A_index_features;\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (A_index_features.find(feat.first) == A_index_features.end())\n\t\t{\n\t\t\tA_index_features.insert({feat.first, idx_feat});\n\t\t\tidx_feat += 1;\n\t\t}\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_norm;\n\t\t\t\tuv_norm << (double)feat.second->uvs_norm.at(cam_id).at(i)(0), (double)feat.second->uvs_norm.at(cam_id).at(i)(1);\n\n\t\t\t\tdouble dt = 0.0;\n\t\t\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\t\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\t\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t\t\t{\n\t\t\t\t\tdt = map_camera_pre_integration_I0toIi.at(time)->dt;\n\t\t\t\t\tR_I0toIk = map_camera_pre_integration_I0toIi.at(time)->R_tau_k;\n\t\t\t\t\talpha_I0toIk = map_camera_pre_integration_I0toIi.at(time)->alpha_tau;\n\t\t\t\t}\n\n\t\t\t\tEigen::MatrixXd H_proj = Eigen::MatrixXd::Zero(2, 3);\n\t\t\t\tH_proj << 1, 0, -uv_norm(0), 0, 1, -uv_norm(1);\n\t\t\t\tEigen::MatrixXd Y = H_proj * R_ItoC * R_I0toIk;\n\t\t\t\tEigen::MatrixXd H_i = Eigen::MatrixXd::Zero(2, system_size);\n\t\t\t\tEigen::MatrixXd b_i = Y * alpha_I0toIk - H_proj * p_IinC;\n\n\t\t\t\tif (size_feature == 1)\n\t\t\t\t{\n\t\t\t\t\tassert(false);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tH_i.block(0, size_feature * A_index_features.at(feat.first), 2, 3) = Y;\n\t\t\t\t}\n\n\t\t\t\tH_i.block(0, size_feature * num_features + 0, 2, 3) = -dt * Y;\n\t\t\t\tH_i.block(0, size_feature * num_features + 3, 2, 3) = 0.5 * dt * dt * Y;\n\n\t\t\t\tA.block(index_meas, 0, 2, A.cols()) = H_i;\n\t\t\t\tb.block(index_meas, 0, 2, 1) = b_i;\n\t\t\t\tindex_meas += 2;\n\t\t\t}\n\t\t}\n\t}\n\tauto rT3 = boost::posix_time::microsec_clock::local_time();\n\n\tEigen::MatrixXd A1 = A.block(0, 0, A.rows(), A.cols() - 3);\n\tEigen::MatrixXd A1A1_inv = (A1.transpose() * A1).llt().solve(Eigen::MatrixXd::Identity(A1.cols(), A1.cols()));\n\tEigen::MatrixXd A2 = A.block(0, A.cols() - 3, A.rows(), 3);\n\tEigen::MatrixXd Temp = A2.transpose() * (Eigen::MatrixXd::Identity(A1.rows(), A1.rows()) - A1 * A1A1_inv * A1.transpose());\n\tEigen::MatrixXd D = Temp * A2;\n\tEigen::MatrixXd d = Temp * b;\n\tEigen::Matrix coeff = initializerHelper::computeDongsiCoeff(D, d, initializer_options.gravity_mag);\n\n\tassert(coeff(0) == 1);\n\tEigen::Matrix companion_matrix = Eigen::Matrix::Zero(coeff.rows() - 1, coeff.rows() - 1);\n\tcompanion_matrix.diagonal(-1).setOnes();\n\tcompanion_matrix.col(companion_matrix.cols() - 1) = - coeff.reverse().head(coeff.rows() - 1);\n\tEigen::JacobiSVD> svd_0(companion_matrix);\n\tEigen::MatrixXd singular_values_0 = svd_0.singularValues();\n\tdouble cond_0 = singular_values_0(0) / singular_values_0(singular_values_0.rows() - 1);\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: CM cond = \" << cond_0 << \" | rank = \" << (int)svd_0.rank() << \" of \" << (int)companion_matrix.cols() \n\t\t<< \" (\" << svd_0.threshold() << \" thresh)\" << std::endl;\n\n\tif (svd_0.rank() != companion_matrix.rows())\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Eigenvalue decomposition not full rank\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::EigenSolver> solver(companion_matrix, false);\n \n\tif (solver.info() != Eigen::Success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: failed to compute the eigenvalue decomposition\" << std::endl;\n\t\treturn false;\n\t}\n\n\tbool lambda_found = false;\n\tdouble lambda_min = -1;\n\tdouble cost_min = INFINITY;\n\tEigen::MatrixXd I_dd = Eigen::MatrixXd::Identity(D.rows(), D.rows());\n\n\tfor (int i = 0; i < solver.eigenvalues().size(); i++) {\n\t\tauto val = solver.eigenvalues()(i);\n\t\tif (val.imag() == 0)\n\t\t{\n\t\t\tdouble lambda = val.real();\n\t\t\tEigen::MatrixXd D_lambdaI_inv = (D - lambda * I_dd).llt().solve(I_dd);\n\t\t\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\t\t\tdouble cost = std::abs(state_grav.norm() - initializer_options.gravity_mag);\n\n\t\t\tif (!lambda_found || cost < cost_min)\n\t\t\t{\n\t\t\t\tlambda_found = true;\n\t\t\t\tlambda_min = lambda;\n\t\t\t\tcost_min = cost;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!lambda_found)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Failed to find a real eigenvalue\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Smallest real eigenvalue = \" << lambda_min << \" (cost of \" \n\t\t<< cost_min << \")\" << std::endl;\n\n\tEigen::MatrixXd D_lambdaI_inv = (D - lambda_min * I_dd).llt().solve(I_dd);\n\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\n\tEigen::VectorXd state_feat_vel = - A1A1_inv * A1.transpose() * A2 * state_grav + A1A1_inv * A1.transpose() * b;\n\tEigen::MatrixXd x_hat = Eigen::MatrixXd::Zero(system_size, 1);\n\tx_hat.block(0, 0, size_feature * num_features + 3, 1) = state_feat_vel;\n\tx_hat.block(size_feature * num_features + 3, 0, 3, 1) = state_grav;\n\tEigen::Vector3d v_I0inI0 = x_hat.block(size_feature * num_features + 0, 0, 3, 1);\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Velocity in I0 was \" << v_I0inI0(0) << \", \" << v_I0inI0(1) << \", \" << v_I0inI0(2) \n\t\t<< \" and |v| = \" << v_I0inI0.norm() << std::endl;\n\n\tEigen::Vector3d gravity_inI0 = x_hat.block(size_feature * num_features + 3, 0, 3, 1);\n\tdouble init_max_grav_difference = 1e-3;\n\n\tif (std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) > init_max_grav_difference)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Gravity did not converge (\" << std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) \n\t\t\t<< \" > \" << init_max_grav_difference << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << \"[initialize-dynamic]: Gravity in I0 was \" << gravity_inI0(0) << \", \" << gravity_inI0(1) << \", \" << gravity_inI0(2) << \" and |g| = \" << gravity_inI0.norm() << std::endl;\n\tauto rT4 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::map ori_I0toIi, pos_IiinI0, vel_IiinI0;\n\t\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble time = time_pair.first;\n\n\t\tdouble dt = 0.0;\n\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\tEigen::MatrixXd beta_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\n\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t{\n\t\t\tauto pre_integration = map_camera_pre_integration_I0toIi.at(time);\n\t\t\tdt = pre_integration->dt;\n\t\t\tR_I0toIk = pre_integration->R_tau_k;\n\t\t\talpha_I0toIk = pre_integration->alpha_tau;\n\t\t\tbeta_I0toIk = pre_integration->beta_tau;\n\t\t}\n\n\t\tEigen::Vector3d p_IkinI0 = v_I0inI0 * dt - 0.5 * gravity_inI0 * dt * dt + alpha_I0toIk;\n\t\tEigen::Vector3d v_IkinI0 = v_I0inI0 - gravity_inI0 * dt + beta_I0toIk;\n\n\t\tori_I0toIi.insert({time, quatType::rotToQuat(R_I0toIk)});\n\t\tpos_IiinI0.insert({time, p_IkinI0});\n\t\tvel_IiinI0.insert({time, v_IkinI0});\n\t}\n\n\tcount_valid_features = 0;\n\tstd::map features_inI0;\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tEigen::Vector3d p_FinI0;\n\t\tif (size_feature == 1)\n\t\t\tassert(false);\n\t\telse\n\t\t\tp_FinI0 = x_hat.block(size_feature * A_index_features.at(feat.first), 0, 3, 1);\n\n\t\tbool is_behind = false;\n\t\t\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Vector3d p_FinC0 = quatType::quatToRot(q_ItoC) * p_FinI0 + p_IinC;\n\n\t\t\tif (p_FinC0(2) < 0) is_behind = true;\n\t\t}\n\t\t\n\t\tif (!is_behind)\n\t\t{\n\t\t\tfeatures_inI0.insert({feat.first, p_FinI0});\n\t\t\tcount_valid_features++;\n\t\t}\n\t}\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Not enough features for our mle (\" << count_valid_features << \" < \" \n\t\t\t<< min_valid_features << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Matrix3d R_GtoI0;\n\tinitializerHelper::gramSchmidt(gravity_inI0, R_GtoI0);\n\tEigen::Vector4d q_GtoI0 = quatType::rotToQuat(R_GtoI0);\n\tEigen::Vector3d gravity;\n\tgravity << 0.0, 0.0, initializer_options.gravity_mag;\n\tstd::map ori_GtoIi, pos_IiinG, vel_IiinG;\n\tstd::map features_inG;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tori_GtoIi[time_pair.first] = quatType::quatMultiply(ori_I0toIi.at(time_pair.first), q_GtoI0);\n\t\tpos_IiinG[time_pair.first] = R_GtoI0.transpose() * pos_IiinI0.at(time_pair.first);\n\t\tvel_IiinG[time_pair.first] = R_GtoI0.transpose() * vel_IiinI0.at(time_pair.first);\n\t}\n\n\tfor (auto const &feat : features_inI0)\n\tfeatures_inG[feat.first] = R_GtoI0.transpose() * feat.second;\n\n\tceres::Problem problem;\n\n\tstd::map map_states;\n\tstd::vector ceres_vars_ori;\n\tstd::vector ceres_vars_pos;\n\tstd::vector ceres_vars_vel;\n\tstd::vector ceres_vars_bias_g;\n\tstd::vector ceres_vars_bias_a;\n\n\tstd::map map_features;\n\tstd::vector ceres_vars_feat;\n\n\tstd::map map_calib_cam2imu;\n\tstd::vector ceres_vars_calib_cam2imu_ori;\n\tstd::vector ceres_vars_calib_cam2imu_pos;\n\n\tstd::map map_calib_cam;\n\tstd::vector ceres_vars_calib_cam_intrinsics;\n\n\tauto freeStateMemory = [&]()\n\t{\n\t\tfor (auto const &ptr : ceres_vars_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_vel)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_g)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_a)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_feat)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam_intrinsics)\n\t\t\tdelete[] ptr;\n\t};\n\n\tceres::Solver::Options options;\n\toptions.linear_solver_type = ceres::DENSE_SCHUR;\n\toptions.trust_region_strategy_type = ceres::DOGLEG;\n\toptions.num_threads = initializer_options.init_dyn_mle_max_threads;\n\toptions.max_solver_time_in_seconds = initializer_options.init_dyn_mle_max_time;\n\toptions.max_num_iterations = initializer_options.init_dyn_mle_max_iter;\n\toptions.function_tolerance = 1e-5;\n\toptions.gradient_tolerance = 1e-4 * options.function_tolerance;\n\n\tdouble timestamp_k = -1;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble timestamp_k1 = time_pair.first;\n\t\tstd::shared_ptr pre_integration = map_camera_pre_integration_IitoIi1.at(timestamp_k1);\n\t\tEigen::Matrix state_k1;\n\t\tstate_k1.block(0, 0, 4, 1) = ori_GtoIi.at(timestamp_k1);\n\t\tstate_k1.block(4, 0, 3, 1) = pos_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(7, 0, 3, 1) = vel_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(10, 0, 3, 1) = gyroscope_bias;\n\t\tstate_k1.block(13, 0, 3, 1) = accelerometer_bias;\n\n\t\tauto *var_ori = new double[4];\n\t\tfor (int j = 0; j < 4; j++)\n\t\t\tvar_ori[j] = state_k1(0 + j, 0);\n\n\t\tauto *var_pos = new double[3];\n\t\tauto *var_vel = new double[3];\n\t\tauto *var_bias_g = new double[3];\n\t\tauto *var_bias_a = new double[3];\n\n\t\tfor (int j = 0; j < 3; j++)\n\t\t{\n\t\t\tvar_pos[j] = state_k1(4 + j, 0);\n\t\t\tvar_vel[j] = state_k1(7 + j, 0);\n\t\t\tvar_bias_g[j] = state_k1(10 + j, 0);\n\t\t\tvar_bias_a[j] = state_k1(13 + j, 0);\n\t\t}\n\n\t\tauto ceres_jplquat = new stateJplQuatLocal();\n\t\tproblem.AddParameterBlock(var_ori, 4, ceres_jplquat);\n\t\tproblem.AddParameterBlock(var_pos, 3);\n\t\tproblem.AddParameterBlock(var_vel, 3);\n\t\tproblem.AddParameterBlock(var_bias_g, 3);\n\t\tproblem.AddParameterBlock(var_bias_a, 3);\n\n\t\tif (map_states.empty())\n\t\t{\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(13, 1);\n\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t{\n\t\t\t\tx_lin(4 + j) = var_pos[j];\n\t\t\t\tx_lin(7 + j) = var_bias_g[j];\n\t\t\t\tx_lin(10 + j) = var_bias_a[j];\n\t\t\t}\n\t\t\t\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(10, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(10, 10);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1e-5, 2);\n\t\t\tprior_Info.block(4, 4, 3, 3) *= 1.0 / std::pow(0.05, 2);\n\t\t\tprior_Info.block(7, 7, 3, 3) *= 1.0 / std::pow(0.10, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_ori);\n\t\t\tx_types.emplace_back(\"quat_yaw\");\n\t\t\tfactor_params.push_back(var_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_g);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_a);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\t\t}\n\n\t\tmap_states.insert({timestamp_k1, (int)ceres_vars_ori.size()});\n\t\tceres_vars_ori.push_back(var_ori);\n\t\tceres_vars_pos.push_back(var_pos);\n\t\tceres_vars_vel.push_back(var_vel);\n\t\tceres_vars_bias_g.push_back(var_bias_g);\n\t\tceres_vars_bias_a.push_back(var_bias_a);\n\n\t\tif (pre_integration != nullptr)\n\t\t{\n\t\t\tassert(timestamp_k != -1);\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k1)));\n\t\t\tauto *factor_imu = new factorImuPreIntegrationV1(pre_integration->dt, gravity, pre_integration->alpha_tau, pre_integration->beta_tau, \n\t\t\t\tpre_integration->q_tau_k, pre_integration->b_a_lin, pre_integration->b_w_lin, pre_integration->J_q, pre_integration->J_b, \n\t\t\t\tpre_integration->J_a, pre_integration->H_b, pre_integration->H_a, pre_integration->P_meas);\n\t\t\t\n\t\t\tproblem.AddResidualBlock(factor_imu, nullptr, factor_params);\n\t\t}\n\n\t\ttimestamp_k = timestamp_k1;\n\t}\n\n\tfor (auto const &id_pair : map_camera_ids)\n\t{\n\t\tsize_t cam_id = id_pair.first;\n\t\tif (map_calib_cam2imu.find(cam_id) == map_calib_cam2imu.end())\n\t\t{\n\t\t\tauto *var_calib_ori = new double[4];\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tvar_calib_ori[j] = initializer_options.camera_extrinsics.at(cam_id)(0 + j, 0);\n\n\t\t\tauto *var_calib_pos = new double[3];\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tvar_calib_pos[j] = initializer_options.camera_extrinsics.at(cam_id)(4 + j, 0);\n\n\t\t\tauto ceres_calib_jplquat = new stateJplQuatLocal();\n\t\t\tproblem.AddParameterBlock(var_calib_ori, 4, ceres_calib_jplquat);\n\t\t\tproblem.AddParameterBlock(var_calib_pos, 3);\n\n\t\t\tmap_calib_cam2imu.insert({cam_id, (int)ceres_vars_calib_cam2imu_ori.size()});\n\t\t\tceres_vars_calib_cam2imu_ori.push_back(var_calib_ori);\n\t\t\tceres_vars_calib_cam2imu_pos.push_back(var_calib_pos);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(7, 1);\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tx_lin(4 + j) = var_calib_pos[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(6, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(6, 6);\n\t\t\tprior_Info.block(0, 0, 3, 3) *= 1.0 / std::pow(0.001, 2);\n\t\t\tprior_Info.block(3, 3, 3, 3) *= 1.0 / std::pow(0.01, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_ori);\n\t\t\tx_types.emplace_back(\"quat\");\n\t\t\tfactor_params.push_back(var_calib_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t{\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_ori);\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_pos);\n\t\t\t}\n\t\t}\n\n\t\tif (map_calib_cam.find(cam_id) == map_calib_cam.end())\n\t\t{\n\t\t\tauto *var_calib_cam = new double[8];\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tvar_calib_cam[j] = initializer_options.camera_intrinsics.at(cam_id)->getValue()(j, 0);\n\n\t\t\tproblem.AddParameterBlock(var_calib_cam, 8);\n\t\t\tmap_calib_cam.insert({cam_id, (int)ceres_vars_calib_cam_intrinsics.size()});\n\t\t\tceres_vars_calib_cam_intrinsics.push_back(var_calib_cam);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_cam[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(8, 8);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1.0, 2);\n\t\t\tprior_Info.block(4, 4, 4, 4) *= 1.0 / std::pow(0.005, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_cam);\n\t\t\tx_types.emplace_back(\"vec8\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_cam);\n\t\t}\n\t}\n\tassert(map_calib_cam2imu.size() == map_calib_cam.size());\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (features_inG.find(feat.first) == features_inG.end()) continue;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t feat_id = feat.first;\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tbool is_fisheye = (std::dynamic_pointer_cast(initializer_options.camera_intrinsics.at(cam_id)) != nullptr);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_raw = feat.second->uvs.at(cam_id).at(i).block(0, 0, 2, 1).cast();\n\n\t\t\t\tif (map_features.find(feat_id) == map_features.end())\n\t\t\t\t{\n\t\t\t\t\tauto *var_feat = new double[3];\n\t\t\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\t\t\tvar_feat[j] = features_inG.at(feat_id)(j);\n\n\t\t\t\t\tproblem.AddParameterBlock(var_feat, 3);\n\t\t\t\t\tmap_features.insert({feat_id, (int)ceres_vars_feat.size()});\n\t\t\t\t\tceres_vars_feat.push_back(var_feat);\n\t\t\t\t}\n\n\t\t\t\tstd::vector factor_params;\n\t\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_feat.at(map_features.at(feat_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_ori.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_pos.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam_intrinsics.at(map_calib_cam.at(cam_id)));\n\t\t\t\tauto *factor_pinhole = new factorImageReprojCalib(uv_raw, initializer_options.sigma_pix, is_fisheye);\n\n\t\t\t\tceres::LossFunction *loss_function = new ceres::CauchyLoss(1.0);\n\t\t\t\tproblem.AddResidualBlock(factor_pinhole, loss_function, factor_params);\n\t\t\t}\n\t\t}\n\t}\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_g.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_vel.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_a.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_pos.size());\n\tauto rT5 = boost::posix_time::microsec_clock::local_time();\n\n\tceres::Solver::Summary summary;\n\tceres::Solve(options, &problem, &summary);\n\n\tstd::cout << \"[initialize-dynamic]: \" << (int)summary.iterations.size() << \" iterations | \" << map_states.size() << \" states, \" \n\t\t<< map_features.size() << \" feats (\" << count_valid_features << \" valid) | \" << summary.num_parameters << \" param and \"\n\t\t<< summary.num_residuals << \" res | cost \" << std::fixed << summary.initial_cost << \" => \" << summary.final_cost << std::endl;\n\n\tauto rT6 = boost::posix_time::microsec_clock::local_time();\n\n\ttimestamp = newest_cam_time;\n\tif (initializer_options.init_dyn_mle_max_iter != 0 && summary.termination_type != ceres::CONVERGENCE)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Optimization failed: \" << summary.message << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\tstd::cout << \"[initialize-dynamic]: \" << summary.message << std::endl;\n\n\tauto getPose = [&](double timestamp)\n\t{\n\t\tEigen::VectorXd state_imu = Eigen::VectorXd::Zero(16);\n\n\t\tfor (int i = 0; i < 4; i++)\n\t\t\tstate_imu(0 + i) = ceres_vars_ori[map_states[timestamp]][i];\n\n\t\tfor (int i = 0; i < 3; i++)\n\t\t{\n\t\t\tstate_imu(4 + i) = ceres_vars_pos[map_states[timestamp]][i];\n\t\t\tstate_imu(7 + i) = ceres_vars_vel[map_states[timestamp]][i];\n\t\t\tstate_imu(10 + i) = ceres_vars_bias_g[map_states[timestamp]][i];\n\t\t\tstate_imu(13 + i) = ceres_vars_bias_a[map_states[timestamp]][i];\n\t\t}\n\n\t\treturn state_imu;\n\t};\n\n\tassert(map_states.find(newest_cam_time) != map_states.end());\n\t\n\tif (imu_state_ == nullptr)\n\t\timu_state_ = std::make_shared();\n\n\tEigen::VectorXd imu_state = getPose(newest_cam_time);\n\timu_state_->setValue(imu_state);\n\timu_state_->setFej(imu_state);\n\n\tfor (auto const &state_pair : map_states)\n\t{\n\t\tEigen::VectorXd pose = getPose(state_pair.first);\n\n\t\tif (clones_imu_.find(state_pair.first) == clones_imu_.end())\n\t\t{\n\t\t\tauto pose_ = std::make_shared();\n\t\t\tpose_->setValue(pose.block(0, 0, 7, 1));\n\t\t\tpose_->setFej(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.insert({state_pair.first, pose_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tclones_imu_.at(state_pair.first)->setValue(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.at(state_pair.first)->setFej(pose.block(0, 0, 7, 1));\n\t\t}\n\t}\n\n\tfor (auto const &feat_pair : map_features)\n\t{\n\t\tEigen::Vector3d feature;\n\t\tfeature << ceres_vars_feat[feat_pair.second][0], ceres_vars_feat[feat_pair.second][1], ceres_vars_feat[feat_pair.second][2];\n\n\t\tif (map_points_.find(feat_pair.first) == map_points_.end())\n\t\t{\n\t\t\tauto feature_ = std::make_shared();\n\t\t\tfeature_->feature_id = feat_pair.first;\n\t\t\tfeature_->setPointXYZ(feature, false);\n\t\t\tfeature_->setPointXYZ(feature, true);\n\t\t\tmap_points_.insert({feat_pair.first, feature_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmap_points_.at(feat_pair.first)->feature_id = feat_pair.first;\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, false);\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, true);\n\t\t}\n\t}\n\n\tstd::vector> covariance_blocks;\n\tint state_index = map_states[newest_cam_time];\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_ori[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index]));\n\n\tceres::Covariance::Options options_cov;\n\toptions_cov.null_space_rank = (!initializer_options.init_dyn_mle_opt_calib) * ((int)map_calib_cam2imu.size() * (6 + 8));\n\toptions_cov.min_reciprocal_condition_number = initializer_options.init_dyn_min_rec_cond;\n \n\toptions_cov.apply_loss_function = true;\n\toptions_cov.num_threads = initializer_options.init_dyn_mle_max_threads;\n\n\tceres::Covariance problem_cov(options_cov);\n\tbool success = problem_cov.Compute(covariance_blocks, &problem);\n\n\tif (!success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Covariance recovery failed...\" << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\n\torder.clear();\n\torder.push_back(imu_state_);\n\tcovariance = Eigen::MatrixXd::Zero(imu_state_->getSize(), imu_state_->getSize());\n\tEigen::Matrix cov_tmp = Eigen::Matrix::Zero();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_ori[state_index], cov_tmp.data()));\n\tcovariance.block(0, 0, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(3, 3, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(6, 6, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(9, 9, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(12, 12, 3, 3) = cov_tmp.eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(0, 3, 3, 3) = cov_tmp.eval();\n\tcovariance.block(3, 0, 3, 3) = cov_tmp.transpose();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(0, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(0, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(0, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 0, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(3, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(3, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(3, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 3, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(6, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 6, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(6, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 6, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(9, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 9, 3, 3) = cov_tmp.transpose().eval();\n\n\tcovariance.block(0, 0, 3, 3) *= initializer_options.init_dyn_inflation_orientation;\n\tcovariance.block(6, 6, 3, 3) *= initializer_options.init_dyn_inflation_velocity;\n\tcovariance.block(9, 9, 3, 3) *= initializer_options.init_dyn_inflation_bias_gyro;\n\tcovariance.block(12, 12, 3, 3) *= initializer_options.init_dyn_inflation_bias_accel;\n\n\tcovariance = 0.5 * (covariance + covariance.transpose());\n\tEigen::Vector3d sigmas_vel = covariance.block(6, 6, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_bg = covariance.block(9, 9, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_ba = covariance.block(12, 12, 3, 3).diagonal().transpose().cwiseSqrt();\n\tstd::cout << \"[initialize-dynamic]: vel priors = \" << sigmas_vel(0) << \", \" << sigmas_vel(1) << \", \" << sigmas_vel(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: bg priors = \" << sigmas_bg(0) << \", \" << sigmas_bg(1) << \", \" << sigmas_bg(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: ba priors = \" << sigmas_ba(0) << \", \" << sigmas_ba(1) << \", \" << sigmas_ba(2) << std::endl;\n\n\tEigen::MatrixXd x = imu_state_->value();\n\tx.block(4, 0, 3, 1).setZero();\n\timu_state_->setValue(x);\n\timu_state_->setFej(x);\n\n\tauto rT7 = boost::posix_time::microsec_clock::local_time();\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" sec for prelim tests\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" sec for linsys setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" sec for linsys\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" sec for ceres opt setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" sec for ceres opt\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT6).total_microseconds() * 1e-6 << \" sec for ceres covariance\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT1).total_microseconds() * 1e-6 << \" sec total for initialization\" << std::endl;\n\tfreeStateMemory();\n\n\treturn true;\n}\n private:\n inertialInitializerOptions initializer_options;\n std::shared_ptr db;\n std::shared_ptr> imu_data;\n public:\n explicit dynamicInitializer(const inertialInitializerOptions &initializer_options_, std::shared_ptr db_, \n std::shared_ptr> imu_data_) {\n\n}\n bool initialize(double ×tamp, Eigen::MatrixXd &covariance, std::vector> &order, \n std::shared_ptr &imu_state_, std::map> &clones_imu_, \n std::unordered_map> &map_points_) {\n\tauto rT1 = boost::posix_time::microsec_clock::local_time();\n\tdouble newest_cam_time = -1;\n\tfor (auto const &feat : db->getInternalData())\n\t{\n\t\tfor (auto const &cam_time_pair : feat.second->timestamps)\n\t\t{\n\t\t\tfor (auto const &time : cam_time_pair.second)\n\t\t\t{\n\t\t\t\tnewest_cam_time = std::max(newest_cam_time, time);\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble oldest_time = newest_cam_time - initializer_options.init_window_time;\n\n\tif (newest_cam_time < 0 || oldest_time < 0) return false;\n\n\tdb->cleanUpOldmeasurements(oldest_time);\n \n\tbool have_old_imu_readings = false;\n\tauto it_imu = imu_data->begin();\n\n\twhile (it_imu != imu_data->end() && it_imu->timestamp < oldest_time + initializer_options.calib_camimu_dt)\n\t{\n\t\thave_old_imu_readings = true;\n\t\tit_imu = imu_data->erase(it_imu);\n\t}\n\n\tif (db->getInternalData().size() < 0.75 * initializer_options.init_max_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Only \" << db->getInternalData().size() << \" valid features of required (\" \n\t\t\t<< 0.95 * initializer_options.init_max_features << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tif (imu_data->size() < 2 || !have_old_imu_readings)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Waiting for window to reach full size (\" << imu_data->size() << \" imu readings)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::unordered_map> features;\n\n\tfor (const auto &feat : db->getInternalData())\n\t{\n\t\tauto feat_new = std::make_shared();\n\t\tfeat_new->feature_id = feat.second->feature_id;\n\t\tfeat_new->uvs = feat.second->uvs;\n\t\tfeat_new->uvs_norm = feat.second->uvs_norm;\n\t\tfeat_new->timestamps = feat.second->timestamps;\n\t\tfeatures.insert({feat.first, feat_new});\n\t}\n\n\tconst int min_num_meas_to_optimize = (int)initializer_options.init_window_time;\n\tconst int min_valid_features = 8;\n\n\tbool have_stereo = false;\n\tint count_valid_features = 0;\n\tstd::map map_features_num_meas;\n\tint num_measurements = 0;\n\tdouble oldest_camera_time = INFINITY;\n\tstd::map map_camera_times;\n\tmap_camera_times[newest_cam_time] = true;\n\tstd::map map_camera_ids;\n\tdouble pose_dt_avg = initializer_options.init_window_time / (double)(initializer_options.init_dyn_num_pose + 1);\n\n\tfor (auto const &feat : features)\n\t{\n\t\tstd::vector times;\n\t\tstd::map cam_ids;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tfor (double time : cam_time.second)\n\t\t\t{\n\t\t\t\tdouble time_dt = INFINITY;\n\n\t\t\t\tfor (auto const &tmp : map_camera_times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp.first));\n\n\t\t\t\tfor (auto const &tmp : times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp));\n\n\t\t\t\tif (time_dt >= pose_dt_avg || time_dt == 0.0)\n\t\t\t\t{\n\t\t\t\t\ttimes.push_back(time);\n\t\t\t\t\tcam_ids[cam_time.first] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap_features_num_meas[feat.first] = (int)times.size();\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tfor (auto const &tmp : times)\n\t\t{\n\t\t\tmap_camera_times[tmp] = true;\n\t\t\toldest_camera_time = std::min(oldest_camera_time, tmp);\n\t\t\tnum_measurements += 2;\n\t\t}\n\n\t\tfor (auto const &tmp : cam_ids)\n\t\t\tmap_camera_ids[tmp.first] = true;\n\n\t\tif (cam_ids.size() > 1) have_stereo = true;\n\n\t\tcount_valid_features++;\n\t}\n\n\tif ((int)map_camera_times.size() < initializer_options.init_dyn_num_pose)\n\t\treturn false;\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Only \" << count_valid_features << \" valid features of required \" << min_valid_features << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Vector3d gyroscope_bias = initializer_options.init_dyn_bias_g;\n\tEigen::Vector3d accelerometer_bias = initializer_options.init_dyn_bias_a;\n\n\tdouble accel_inI_norm = 0.0;\n\tdouble theta_inI_norm = 0.0;\n\tdouble time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\tdouble time_1_in_imu = newest_cam_time + initializer_options.calib_camimu_dt;\n\tstd::vector readings = initializerHelper::selectImuReadings(*imu_data, time_0_in_imu, time_1_in_imu);\n\n\tassert(readings.size() > 2);\n\tfor (size_t k = 0; k < readings.size() - 1; k++)\n\t{\n\t\tauto imu_0 = readings.at(k);\n\t\tauto imu_1 = readings.at(k + 1);\n\t\tdouble dt = imu_1.timestamp - imu_0.timestamp;\n\t\tEigen::Vector3d gyr = 0.5 * (imu_0.gyr + imu_1.gyr) - gyroscope_bias;\n\t\tEigen::Vector3d acc = 0.5 * (imu_0.acc + imu_1.acc) - accelerometer_bias;\n\t\ttheta_inI_norm += ( -gyr * dt).norm();\n\t\taccel_inI_norm += acc.norm();\n\t}\n\n\taccel_inI_norm /= (double)(readings.size() - 1);\n\n\tif (180.0 / M_PI * theta_inI_norm < initializer_options.init_dyn_min_deg)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Gyroscope only \" << 180.0 / M_PI * theta_inI_norm << \" degree change (\" \n\t\t\t<< initializer_options.init_dyn_min_deg << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\tstd::cout << std::fixed << \"[initialize-dynamic]: |theta_I| = \" << 180.0 / M_PI * theta_inI_norm \n\t\t<< \" deg and |accel| = \" << accel_inI_norm << std::endl;\n\n \tauto rT2 = boost::posix_time::microsec_clock::local_time();\n\n\tconst bool use_single_depth = false;\n\tint size_feature = (use_single_depth) ? 1 : 3;\n\tint num_features = count_valid_features;\n\tint system_size = size_feature * num_features + 3 + 3;\n\n\tif (num_measurements < system_size)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Not enough feature measurements (\" << num_measurements << \" meas vs \" << system_size \n\t\t\t<< \" state size)\" << std::endl;\n\n\t\treturn false;\n\t}\n\n\tassert(oldest_camera_time < newest_cam_time);\n\tdouble last_camera_timestamp = 0.0;\n\tstd::map> map_camera_pre_integration_I0toIi, map_camera_pre_integration_IitoIi1; //map_camera_cpi_I0toIi, map_camera_cpi_IitoIi1;\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble current_time = time_pair.first;\n \n\t\tif (current_time == oldest_camera_time)\n\t\t{\n\t\t\tmap_camera_pre_integration_I0toIi.insert({current_time, nullptr});\n\t\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, nullptr});\n\t\t\tlast_camera_timestamp = current_time;\n\t\t\tcontinue;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_I0toIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_I0toIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, initializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_I0toIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_I0toIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_I0toIi1_time_0_in_imu, pre_integration_I0toIi1_time_1_in_imu);\n \n\t\tif (pre_integration_I0toIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) \n\t\t\t\t<< \" in has \" << pre_integration_I0toIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_dt_imu = pre_integration_I0toIi1_readings.at(pre_integration_I0toIi1_readings.size() - 1).timestamp - pre_integration_I0toIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_I0toIi1_dt_imu - (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_I0toIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_I0toIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_I0toIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_I0toIi1_readings.at(k + 1);\n\t\t\tpre_integration_I0toIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_time_0_in_imu = last_camera_timestamp + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_IitoIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_IitoIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, \n\t\t\tinitializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_IitoIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_IitoIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_IitoIi1_time_0_in_imu, pre_integration_IitoIi1_time_1_in_imu);\n\n\t\tif (pre_integration_IitoIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << \" in has \" \n\t\t\t\t<< pre_integration_IitoIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_dt_imu = pre_integration_IitoIi1_readings.at(pre_integration_IitoIi1_readings.size() - 1).timestamp - pre_integration_IitoIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_IitoIi1_dt_imu - (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_IitoIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_IitoIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_IitoIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_IitoIi1_readings.at(k + 1);\n\t\t\tpre_integration_IitoIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tmap_camera_pre_integration_I0toIi.insert({current_time, pre_integration_I0toIi1});\n\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, pre_integration_IitoIi1});\n\t\tlast_camera_timestamp = current_time;\n\t}\n\n\tEigen::MatrixXd A = Eigen::MatrixXd::Zero(num_measurements, system_size);\n\tEigen::VectorXd b = Eigen::VectorXd::Zero(num_measurements);\n\tstd::cout << \"[initialize-dynamic]: System of \" << num_measurements << \" measurement x \" << system_size << \" states created (\" << num_features \n\t\t<< \" features, \" << ((have_stereo) ? \"stereo\" : \"mono\") << \")\" << std::endl;\n\n\tint index_meas = 0;\n\tint idx_feat = 0;\n\tstd::map A_index_features;\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (A_index_features.find(feat.first) == A_index_features.end())\n\t\t{\n\t\t\tA_index_features.insert({feat.first, idx_feat});\n\t\t\tidx_feat += 1;\n\t\t}\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_norm;\n\t\t\t\tuv_norm << (double)feat.second->uvs_norm.at(cam_id).at(i)(0), (double)feat.second->uvs_norm.at(cam_id).at(i)(1);\n\n\t\t\t\tdouble dt = 0.0;\n\t\t\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\t\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\t\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t\t\t{\n\t\t\t\t\tdt = map_camera_pre_integration_I0toIi.at(time)->dt;\n\t\t\t\t\tR_I0toIk = map_camera_pre_integration_I0toIi.at(time)->R_tau_k;\n\t\t\t\t\talpha_I0toIk = map_camera_pre_integration_I0toIi.at(time)->alpha_tau;\n\t\t\t\t}\n\n\t\t\t\tEigen::MatrixXd H_proj = Eigen::MatrixXd::Zero(2, 3);\n\t\t\t\tH_proj << 1, 0, -uv_norm(0), 0, 1, -uv_norm(1);\n\t\t\t\tEigen::MatrixXd Y = H_proj * R_ItoC * R_I0toIk;\n\t\t\t\tEigen::MatrixXd H_i = Eigen::MatrixXd::Zero(2, system_size);\n\t\t\t\tEigen::MatrixXd b_i = Y * alpha_I0toIk - H_proj * p_IinC;\n\n\t\t\t\tif (size_feature == 1)\n\t\t\t\t{\n\t\t\t\t\tassert(false);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tH_i.block(0, size_feature * A_index_features.at(feat.first), 2, 3) = Y;\n\t\t\t\t}\n\n\t\t\t\tH_i.block(0, size_feature * num_features + 0, 2, 3) = -dt * Y;\n\t\t\t\tH_i.block(0, size_feature * num_features + 3, 2, 3) = 0.5 * dt * dt * Y;\n\n\t\t\t\tA.block(index_meas, 0, 2, A.cols()) = H_i;\n\t\t\t\tb.block(index_meas, 0, 2, 1) = b_i;\n\t\t\t\tindex_meas += 2;\n\t\t\t}\n\t\t}\n\t}\n\tauto rT3 = boost::posix_time::microsec_clock::local_time();\n\n\tEigen::MatrixXd A1 = A.block(0, 0, A.rows(), A.cols() - 3);\n\tEigen::MatrixXd A1A1_inv = (A1.transpose() * A1).llt().solve(Eigen::MatrixXd::Identity(A1.cols(), A1.cols()));\n\tEigen::MatrixXd A2 = A.block(0, A.cols() - 3, A.rows(), 3);\n\tEigen::MatrixXd Temp = A2.transpose() * (Eigen::MatrixXd::Identity(A1.rows(), A1.rows()) - A1 * A1A1_inv * A1.transpose());\n\tEigen::MatrixXd D = Temp * A2;\n\tEigen::MatrixXd d = Temp * b;\n\tEigen::Matrix coeff = initializerHelper::computeDongsiCoeff(D, d, initializer_options.gravity_mag);\n\n\tassert(coeff(0) == 1);\n\tEigen::Matrix companion_matrix = Eigen::Matrix::Zero(coeff.rows() - 1, coeff.rows() - 1);\n\tcompanion_matrix.diagonal(-1).setOnes();\n\tcompanion_matrix.col(companion_matrix.cols() - 1) = - coeff.reverse().head(coeff.rows() - 1);\n\tEigen::JacobiSVD> svd_0(companion_matrix);\n\tEigen::MatrixXd singular_values_0 = svd_0.singularValues();\n\tdouble cond_0 = singular_values_0(0) / singular_values_0(singular_values_0.rows() - 1);\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: CM cond = \" << cond_0 << \" | rank = \" << (int)svd_0.rank() << \" of \" << (int)companion_matrix.cols() \n\t\t<< \" (\" << svd_0.threshold() << \" thresh)\" << std::endl;\n\n\tif (svd_0.rank() != companion_matrix.rows())\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Eigenvalue decomposition not full rank\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::EigenSolver> solver(companion_matrix, false);\n \n\tif (solver.info() != Eigen::Success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: failed to compute the eigenvalue decomposition\" << std::endl;\n\t\treturn false;\n\t}\n\n\tbool lambda_found = false;\n\tdouble lambda_min = -1;\n\tdouble cost_min = INFINITY;\n\tEigen::MatrixXd I_dd = Eigen::MatrixXd::Identity(D.rows(), D.rows());\n\n\tfor (int i = 0; i < solver.eigenvalues().size(); i++) {\n\t\tauto val = solver.eigenvalues()(i);\n\t\tif (val.imag() == 0)\n\t\t{\n\t\t\tdouble lambda = val.real();\n\t\t\tEigen::MatrixXd D_lambdaI_inv = (D - lambda * I_dd).llt().solve(I_dd);\n\t\t\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\t\t\tdouble cost = std::abs(state_grav.norm() - initializer_options.gravity_mag);\n\n\t\t\tif (!lambda_found || cost < cost_min)\n\t\t\t{\n\t\t\t\tlambda_found = true;\n\t\t\t\tlambda_min = lambda;\n\t\t\t\tcost_min = cost;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!lambda_found)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Failed to find a real eigenvalue\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Smallest real eigenvalue = \" << lambda_min << \" (cost of \" \n\t\t<< cost_min << \")\" << std::endl;\n\n\tEigen::MatrixXd D_lambdaI_inv = (D - lambda_min * I_dd).llt().solve(I_dd);\n\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\n\tEigen::VectorXd state_feat_vel = - A1A1_inv * A1.transpose() * A2 * state_grav + A1A1_inv * A1.transpose() * b;\n\tEigen::MatrixXd x_hat = Eigen::MatrixXd::Zero(system_size, 1);\n\tx_hat.block(0, 0, size_feature * num_features + 3, 1) = state_feat_vel;\n\tx_hat.block(size_feature * num_features + 3, 0, 3, 1) = state_grav;\n\tEigen::Vector3d v_I0inI0 = x_hat.block(size_feature * num_features + 0, 0, 3, 1);\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Velocity in I0 was \" << v_I0inI0(0) << \", \" << v_I0inI0(1) << \", \" << v_I0inI0(2) \n\t\t<< \" and |v| = \" << v_I0inI0.norm() << std::endl;\n\n\tEigen::Vector3d gravity_inI0 = x_hat.block(size_feature * num_features + 3, 0, 3, 1);\n\tdouble init_max_grav_difference = 1e-3;\n\n\tif (std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) > init_max_grav_difference)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Gravity did not converge (\" << std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) \n\t\t\t<< \" > \" << init_max_grav_difference << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << \"[initialize-dynamic]: Gravity in I0 was \" << gravity_inI0(0) << \", \" << gravity_inI0(1) << \", \" << gravity_inI0(2) << \" and |g| = \" << gravity_inI0.norm() << std::endl;\n\tauto rT4 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::map ori_I0toIi, pos_IiinI0, vel_IiinI0;\n\t\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble time = time_pair.first;\n\n\t\tdouble dt = 0.0;\n\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\tEigen::MatrixXd beta_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\n\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t{\n\t\t\tauto pre_integration = map_camera_pre_integration_I0toIi.at(time);\n\t\t\tdt = pre_integration->dt;\n\t\t\tR_I0toIk = pre_integration->R_tau_k;\n\t\t\talpha_I0toIk = pre_integration->alpha_tau;\n\t\t\tbeta_I0toIk = pre_integration->beta_tau;\n\t\t}\n\n\t\tEigen::Vector3d p_IkinI0 = v_I0inI0 * dt - 0.5 * gravity_inI0 * dt * dt + alpha_I0toIk;\n\t\tEigen::Vector3d v_IkinI0 = v_I0inI0 - gravity_inI0 * dt + beta_I0toIk;\n\n\t\tori_I0toIi.insert({time, quatType::rotToQuat(R_I0toIk)});\n\t\tpos_IiinI0.insert({time, p_IkinI0});\n\t\tvel_IiinI0.insert({time, v_IkinI0});\n\t}\n\n\tcount_valid_features = 0;\n\tstd::map features_inI0;\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tEigen::Vector3d p_FinI0;\n\t\tif (size_feature == 1)\n\t\t\tassert(false);\n\t\telse\n\t\t\tp_FinI0 = x_hat.block(size_feature * A_index_features.at(feat.first), 0, 3, 1);\n\n\t\tbool is_behind = false;\n\t\t\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Vector3d p_FinC0 = quatType::quatToRot(q_ItoC) * p_FinI0 + p_IinC;\n\n\t\t\tif (p_FinC0(2) < 0) is_behind = true;\n\t\t}\n\t\t\n\t\tif (!is_behind)\n\t\t{\n\t\t\tfeatures_inI0.insert({feat.first, p_FinI0});\n\t\t\tcount_valid_features++;\n\t\t}\n\t}\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Not enough features for our mle (\" << count_valid_features << \" < \" \n\t\t\t<< min_valid_features << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Matrix3d R_GtoI0;\n\tinitializerHelper::gramSchmidt(gravity_inI0, R_GtoI0);\n\tEigen::Vector4d q_GtoI0 = quatType::rotToQuat(R_GtoI0);\n\tEigen::Vector3d gravity;\n\tgravity << 0.0, 0.0, initializer_options.gravity_mag;\n\tstd::map ori_GtoIi, pos_IiinG, vel_IiinG;\n\tstd::map features_inG;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tori_GtoIi[time_pair.first] = quatType::quatMultiply(ori_I0toIi.at(time_pair.first), q_GtoI0);\n\t\tpos_IiinG[time_pair.first] = R_GtoI0.transpose() * pos_IiinI0.at(time_pair.first);\n\t\tvel_IiinG[time_pair.first] = R_GtoI0.transpose() * vel_IiinI0.at(time_pair.first);\n\t}\n\n\tfor (auto const &feat : features_inI0)\n\tfeatures_inG[feat.first] = R_GtoI0.transpose() * feat.second;\n\n\tceres::Problem problem;\n\n\tstd::map map_states;\n\tstd::vector ceres_vars_ori;\n\tstd::vector ceres_vars_pos;\n\tstd::vector ceres_vars_vel;\n\tstd::vector ceres_vars_bias_g;\n\tstd::vector ceres_vars_bias_a;\n\n\tstd::map map_features;\n\tstd::vector ceres_vars_feat;\n\n\tstd::map map_calib_cam2imu;\n\tstd::vector ceres_vars_calib_cam2imu_ori;\n\tstd::vector ceres_vars_calib_cam2imu_pos;\n\n\tstd::map map_calib_cam;\n\tstd::vector ceres_vars_calib_cam_intrinsics;\n\n\tauto freeStateMemory = [&]()\n\t{\n\t\tfor (auto const &ptr : ceres_vars_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_vel)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_g)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_a)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_feat)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam_intrinsics)\n\t\t\tdelete[] ptr;\n\t};\n\n\tceres::Solver::Options options;\n\toptions.linear_solver_type = ceres::DENSE_SCHUR;\n\toptions.trust_region_strategy_type = ceres::DOGLEG;\n\toptions.num_threads = initializer_options.init_dyn_mle_max_threads;\n\toptions.max_solver_time_in_seconds = initializer_options.init_dyn_mle_max_time;\n\toptions.max_num_iterations = initializer_options.init_dyn_mle_max_iter;\n\toptions.function_tolerance = 1e-5;\n\toptions.gradient_tolerance = 1e-4 * options.function_tolerance;\n\n\tdouble timestamp_k = -1;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble timestamp_k1 = time_pair.first;\n\t\tstd::shared_ptr pre_integration = map_camera_pre_integration_IitoIi1.at(timestamp_k1);\n\t\tEigen::Matrix state_k1;\n\t\tstate_k1.block(0, 0, 4, 1) = ori_GtoIi.at(timestamp_k1);\n\t\tstate_k1.block(4, 0, 3, 1) = pos_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(7, 0, 3, 1) = vel_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(10, 0, 3, 1) = gyroscope_bias;\n\t\tstate_k1.block(13, 0, 3, 1) = accelerometer_bias;\n\n\t\tauto *var_ori = new double[4];\n\t\tfor (int j = 0; j < 4; j++)\n\t\t\tvar_ori[j] = state_k1(0 + j, 0);\n\n\t\tauto *var_pos = new double[3];\n\t\tauto *var_vel = new double[3];\n\t\tauto *var_bias_g = new double[3];\n\t\tauto *var_bias_a = new double[3];\n\n\t\tfor (int j = 0; j < 3; j++)\n\t\t{\n\t\t\tvar_pos[j] = state_k1(4 + j, 0);\n\t\t\tvar_vel[j] = state_k1(7 + j, 0);\n\t\t\tvar_bias_g[j] = state_k1(10 + j, 0);\n\t\t\tvar_bias_a[j] = state_k1(13 + j, 0);\n\t\t}\n\n\t\tauto ceres_jplquat = new stateJplQuatLocal();\n\t\tproblem.AddParameterBlock(var_ori, 4, ceres_jplquat);\n\t\tproblem.AddParameterBlock(var_pos, 3);\n\t\tproblem.AddParameterBlock(var_vel, 3);\n\t\tproblem.AddParameterBlock(var_bias_g, 3);\n\t\tproblem.AddParameterBlock(var_bias_a, 3);\n\n\t\tif (map_states.empty())\n\t\t{\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(13, 1);\n\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t{\n\t\t\t\tx_lin(4 + j) = var_pos[j];\n\t\t\t\tx_lin(7 + j) = var_bias_g[j];\n\t\t\t\tx_lin(10 + j) = var_bias_a[j];\n\t\t\t}\n\t\t\t\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(10, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(10, 10);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1e-5, 2);\n\t\t\tprior_Info.block(4, 4, 3, 3) *= 1.0 / std::pow(0.05, 2);\n\t\t\tprior_Info.block(7, 7, 3, 3) *= 1.0 / std::pow(0.10, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_ori);\n\t\t\tx_types.emplace_back(\"quat_yaw\");\n\t\t\tfactor_params.push_back(var_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_g);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_a);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\t\t}\n\n\t\tmap_states.insert({timestamp_k1, (int)ceres_vars_ori.size()});\n\t\tceres_vars_ori.push_back(var_ori);\n\t\tceres_vars_pos.push_back(var_pos);\n\t\tceres_vars_vel.push_back(var_vel);\n\t\tceres_vars_bias_g.push_back(var_bias_g);\n\t\tceres_vars_bias_a.push_back(var_bias_a);\n\n\t\tif (pre_integration != nullptr)\n\t\t{\n\t\t\tassert(timestamp_k != -1);\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k1)));\n\t\t\tauto *factor_imu = new factorImuPreIntegrationV1(pre_integration->dt, gravity, pre_integration->alpha_tau, pre_integration->beta_tau, \n\t\t\t\tpre_integration->q_tau_k, pre_integration->b_a_lin, pre_integration->b_w_lin, pre_integration->J_q, pre_integration->J_b, \n\t\t\t\tpre_integration->J_a, pre_integration->H_b, pre_integration->H_a, pre_integration->P_meas);\n\t\t\t\n\t\t\tproblem.AddResidualBlock(factor_imu, nullptr, factor_params);\n\t\t}\n\n\t\ttimestamp_k = timestamp_k1;\n\t}\n\n\tfor (auto const &id_pair : map_camera_ids)\n\t{\n\t\tsize_t cam_id = id_pair.first;\n\t\tif (map_calib_cam2imu.find(cam_id) == map_calib_cam2imu.end())\n\t\t{\n\t\t\tauto *var_calib_ori = new double[4];\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tvar_calib_ori[j] = initializer_options.camera_extrinsics.at(cam_id)(0 + j, 0);\n\n\t\t\tauto *var_calib_pos = new double[3];\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tvar_calib_pos[j] = initializer_options.camera_extrinsics.at(cam_id)(4 + j, 0);\n\n\t\t\tauto ceres_calib_jplquat = new stateJplQuatLocal();\n\t\t\tproblem.AddParameterBlock(var_calib_ori, 4, ceres_calib_jplquat);\n\t\t\tproblem.AddParameterBlock(var_calib_pos, 3);\n\n\t\t\tmap_calib_cam2imu.insert({cam_id, (int)ceres_vars_calib_cam2imu_ori.size()});\n\t\t\tceres_vars_calib_cam2imu_ori.push_back(var_calib_ori);\n\t\t\tceres_vars_calib_cam2imu_pos.push_back(var_calib_pos);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(7, 1);\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tx_lin(4 + j) = var_calib_pos[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(6, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(6, 6);\n\t\t\tprior_Info.block(0, 0, 3, 3) *= 1.0 / std::pow(0.001, 2);\n\t\t\tprior_Info.block(3, 3, 3, 3) *= 1.0 / std::pow(0.01, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_ori);\n\t\t\tx_types.emplace_back(\"quat\");\n\t\t\tfactor_params.push_back(var_calib_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t{\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_ori);\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_pos);\n\t\t\t}\n\t\t}\n\n\t\tif (map_calib_cam.find(cam_id) == map_calib_cam.end())\n\t\t{\n\t\t\tauto *var_calib_cam = new double[8];\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tvar_calib_cam[j] = initializer_options.camera_intrinsics.at(cam_id)->getValue()(j, 0);\n\n\t\t\tproblem.AddParameterBlock(var_calib_cam, 8);\n\t\t\tmap_calib_cam.insert({cam_id, (int)ceres_vars_calib_cam_intrinsics.size()});\n\t\t\tceres_vars_calib_cam_intrinsics.push_back(var_calib_cam);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_cam[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(8, 8);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1.0, 2);\n\t\t\tprior_Info.block(4, 4, 4, 4) *= 1.0 / std::pow(0.005, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_cam);\n\t\t\tx_types.emplace_back(\"vec8\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_cam);\n\t\t}\n\t}\n\tassert(map_calib_cam2imu.size() == map_calib_cam.size());\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (features_inG.find(feat.first) == features_inG.end()) continue;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t feat_id = feat.first;\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tbool is_fisheye = (std::dynamic_pointer_cast(initializer_options.camera_intrinsics.at(cam_id)) != nullptr);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_raw = feat.second->uvs.at(cam_id).at(i).block(0, 0, 2, 1).cast();\n\n\t\t\t\tif (map_features.find(feat_id) == map_features.end())\n\t\t\t\t{\n\t\t\t\t\tauto *var_feat = new double[3];\n\t\t\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\t\t\tvar_feat[j] = features_inG.at(feat_id)(j);\n\n\t\t\t\t\tproblem.AddParameterBlock(var_feat, 3);\n\t\t\t\t\tmap_features.insert({feat_id, (int)ceres_vars_feat.size()});\n\t\t\t\t\tceres_vars_feat.push_back(var_feat);\n\t\t\t\t}\n\n\t\t\t\tstd::vector factor_params;\n\t\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_feat.at(map_features.at(feat_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_ori.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_pos.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam_intrinsics.at(map_calib_cam.at(cam_id)));\n\t\t\t\tauto *factor_pinhole = new factorImageReprojCalib(uv_raw, initializer_options.sigma_pix, is_fisheye);\n\n\t\t\t\tceres::LossFunction *loss_function = new ceres::CauchyLoss(1.0);\n\t\t\t\tproblem.AddResidualBlock(factor_pinhole, loss_function, factor_params);\n\t\t\t}\n\t\t}\n\t}\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_g.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_vel.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_a.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_pos.size());\n\tauto rT5 = boost::posix_time::microsec_clock::local_time();\n\n\tceres::Solver::Summary summary;\n\tceres::Solve(options, &problem, &summary);\n\n\tstd::cout << \"[initialize-dynamic]: \" << (int)summary.iterations.size() << \" iterations | \" << map_states.size() << \" states, \" \n\t\t<< map_features.size() << \" feats (\" << count_valid_features << \" valid) | \" << summary.num_parameters << \" param and \"\n\t\t<< summary.num_residuals << \" res | cost \" << std::fixed << summary.initial_cost << \" => \" << summary.final_cost << std::endl;\n\n\tauto rT6 = boost::posix_time::microsec_clock::local_time();\n\n\ttimestamp = newest_cam_time;\n\tif (initializer_options.init_dyn_mle_max_iter != 0 && summary.termination_type != ceres::CONVERGENCE)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Optimization failed: \" << summary.message << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\tstd::cout << \"[initialize-dynamic]: \" << summary.message << std::endl;\n\n\tauto getPose = [&](double timestamp)\n\t{\n\t\tEigen::VectorXd state_imu = Eigen::VectorXd::Zero(16);\n\n\t\tfor (int i = 0; i < 4; i++)\n\t\t\tstate_imu(0 + i) = ceres_vars_ori[map_states[timestamp]][i];\n\n\t\tfor (int i = 0; i < 3; i++)\n\t\t{\n\t\t\tstate_imu(4 + i) = ceres_vars_pos[map_states[timestamp]][i];\n\t\t\tstate_imu(7 + i) = ceres_vars_vel[map_states[timestamp]][i];\n\t\t\tstate_imu(10 + i) = ceres_vars_bias_g[map_states[timestamp]][i];\n\t\t\tstate_imu(13 + i) = ceres_vars_bias_a[map_states[timestamp]][i];\n\t\t}\n\n\t\treturn state_imu;\n\t};\n\n\tassert(map_states.find(newest_cam_time) != map_states.end());\n\t\n\tif (imu_state_ == nullptr)\n\t\timu_state_ = std::make_shared();\n\n\tEigen::VectorXd imu_state = getPose(newest_cam_time);\n\timu_state_->setValue(imu_state);\n\timu_state_->setFej(imu_state);\n\n\tfor (auto const &state_pair : map_states)\n\t{\n\t\tEigen::VectorXd pose = getPose(state_pair.first);\n\n\t\tif (clones_imu_.find(state_pair.first) == clones_imu_.end())\n\t\t{\n\t\t\tauto pose_ = std::make_shared();\n\t\t\tpose_->setValue(pose.block(0, 0, 7, 1));\n\t\t\tpose_->setFej(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.insert({state_pair.first, pose_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tclones_imu_.at(state_pair.first)->setValue(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.at(state_pair.first)->setFej(pose.block(0, 0, 7, 1));\n\t\t}\n\t}\n\n\tfor (auto const &feat_pair : map_features)\n\t{\n\t\tEigen::Vector3d feature;\n\t\tfeature << ceres_vars_feat[feat_pair.second][0], ceres_vars_feat[feat_pair.second][1], ceres_vars_feat[feat_pair.second][2];\n\n\t\tif (map_points_.find(feat_pair.first) == map_points_.end())\n\t\t{\n\t\t\tauto feature_ = std::make_shared();\n\t\t\tfeature_->feature_id = feat_pair.first;\n\t\t\tfeature_->setPointXYZ(feature, false);\n\t\t\tfeature_->setPointXYZ(feature, true);\n\t\t\tmap_points_.insert({feat_pair.first, feature_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmap_points_.at(feat_pair.first)->feature_id = feat_pair.first;\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, false);\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, true);\n\t\t}\n\t}\n\n\tstd::vector> covariance_blocks;\n\tint state_index = map_states[newest_cam_time];\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_ori[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index]));\n\n\tceres::Covariance::Options options_cov;\n\toptions_cov.null_space_rank = (!initializer_options.init_dyn_mle_opt_calib) * ((int)map_calib_cam2imu.size() * (6 + 8));\n\toptions_cov.min_reciprocal_condition_number = initializer_options.init_dyn_min_rec_cond;\n \n\toptions_cov.apply_loss_function = true;\n\toptions_cov.num_threads = initializer_options.init_dyn_mle_max_threads;\n\n\tceres::Covariance problem_cov(options_cov);\n\tbool success = problem_cov.Compute(covariance_blocks, &problem);\n\n\tif (!success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Covariance recovery failed...\" << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\n\torder.clear();\n\torder.push_back(imu_state_);\n\tcovariance = Eigen::MatrixXd::Zero(imu_state_->getSize(), imu_state_->getSize());\n\tEigen::Matrix cov_tmp = Eigen::Matrix::Zero();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_ori[state_index], cov_tmp.data()));\n\tcovariance.block(0, 0, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(3, 3, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(6, 6, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(9, 9, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(12, 12, 3, 3) = cov_tmp.eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(0, 3, 3, 3) = cov_tmp.eval();\n\tcovariance.block(3, 0, 3, 3) = cov_tmp.transpose();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(0, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(0, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(0, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 0, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(3, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(3, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(3, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 3, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(6, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 6, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(6, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 6, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(9, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 9, 3, 3) = cov_tmp.transpose().eval();\n\n\tcovariance.block(0, 0, 3, 3) *= initializer_options.init_dyn_inflation_orientation;\n\tcovariance.block(6, 6, 3, 3) *= initializer_options.init_dyn_inflation_velocity;\n\tcovariance.block(9, 9, 3, 3) *= initializer_options.init_dyn_inflation_bias_gyro;\n\tcovariance.block(12, 12, 3, 3) *= initializer_options.init_dyn_inflation_bias_accel;\n\n\tcovariance = 0.5 * (covariance + covariance.transpose());\n\tEigen::Vector3d sigmas_vel = covariance.block(6, 6, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_bg = covariance.block(9, 9, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_ba = covariance.block(12, 12, 3, 3).diagonal().transpose().cwiseSqrt();\n\tstd::cout << \"[initialize-dynamic]: vel priors = \" << sigmas_vel(0) << \", \" << sigmas_vel(1) << \", \" << sigmas_vel(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: bg priors = \" << sigmas_bg(0) << \", \" << sigmas_bg(1) << \", \" << sigmas_bg(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: ba priors = \" << sigmas_ba(0) << \", \" << sigmas_ba(1) << \", \" << sigmas_ba(2) << std::endl;\n\n\tEigen::MatrixXd x = imu_state_->value();\n\tx.block(4, 0, 3, 1).setZero();\n\timu_state_->setValue(x);\n\timu_state_->setFej(x);\n\n\tauto rT7 = boost::posix_time::microsec_clock::local_time();\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" sec for prelim tests\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" sec for linsys setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" sec for linsys\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" sec for ceres opt setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" sec for ceres opt\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT6).total_microseconds() * 1e-6 << \" sec for ceres covariance\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT1).total_microseconds() * 1e-6 << \" sec total for initialization\" << std::endl;\n\tfreeStateMemory();\n\n\treturn true;\n}\n private:\n inertialInitializerOptions initializer_options;\n std::shared_ptr db;\n std::shared_ptr> imu_data;\n};"], ["/voxel_svio/include/msckf.h", "class propagator {\n public:\n propagator(noiseManager noises_, double gravity_mag_) {\n\tnoises.sigma_w_2 = std::pow(noises.sigma_w, 2);\n\tnoises.sigma_a_2 = std::pow(noises.sigma_a, 2);\n\tnoises.sigma_wb_2 = std::pow(noises.sigma_wb, 2);\n\tnoises.sigma_ab_2 = std::pow(noises.sigma_ab, 2);\n\n\tgravity << 0.0, 0.0, gravity_mag_;\n\n\thave_last_prop_time_offset = false;\n\tlast_prop_time_offset = 0.0;\n}\n void feedImu(const imuData &imu_data, double oldest_time = -1) {\n\tv_imu_data.emplace_back(imu_data);\n\n\tcleanOldImuMeasurements(oldest_time - 0.10);\t\n}\n void cleanOldImuMeasurements(double oldest_time) {\n\tif (oldest_time < 0)\n\t\treturn;\n\tauto it0 = v_imu_data.begin();\n\twhile (it0 != v_imu_data.end())\n\t{\n\t\tif (it0->timestamp < oldest_time)\n\t\t{\n\t\t\tit0 = v_imu_data.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n}\n void invalidateCache() {\n\tcache_imu_valid = false;\n}\n void resetTransitionMatrices(std::shared_ptr state_ptr);\n static imuData interpolateData(const imuData &imu_1, const imuData &imu_2, double timestamp) {\n\tdouble lambda = (timestamp - imu_1.timestamp) / (imu_2.timestamp - imu_1.timestamp);\n\n\timuData data;\n\tdata.timestamp = timestamp;\n\tdata.acc = (1 - lambda) * imu_1.acc + lambda * imu_2.acc;\n\tdata.gyr = (1 - lambda) * imu_1.gyr + lambda * imu_2.gyr;\n\n\treturn data;\n}\n static std::vector selectImuReadings(const std::vector &v_imu_data_, double time_0, double time_1, bool warn = true) {\n\tstd::vector prop_data;\n\n\tif (v_imu_data_.empty())\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << \"propagator::selectImuReadings(): No IMU measurements. IMU-CAMERA are likely messed up\" << std::endl;\n\t\t\n\t\treturn prop_data;\n\t}\n\n\tfor (size_t i = 0; i < v_imu_data_.size() - 1; i++)\n\t{\n\t\tif (v_imu_data_.at(i + 1).timestamp > time_0 && v_imu_data_.at(i).timestamp < time_0)\n\t\t{\n\t\t\timuData data = propagator::interpolateData(v_imu_data_.at(i), v_imu_data_.at(i + 1), time_0);\n\t\t\tprop_data.push_back(data);\n\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (v_imu_data_.at(i).timestamp >= time_0 && v_imu_data_.at(i + 1).timestamp <= time_1)\n\t\t{\n\t\t\tprop_data.push_back(v_imu_data_.at(i));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (v_imu_data_.at(i + 1).timestamp > time_1)\n\t\t{\n\t\t\tif (v_imu_data_.at(i).timestamp > time_1 && i == 0)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if (v_imu_data_.at(i).timestamp > time_1)\n\t\t\t{\n\t\t\t\timuData data = propagator::interpolateData(v_imu_data_.at(i - 1), v_imu_data_.at(i), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tprop_data.push_back(v_imu_data_.at(i));\n\t\t\t}\n\n\t\t\tif (prop_data.at(prop_data.size() - 1).timestamp != time_1)\n\t\t\t{\n\t\t\t\timuData data = propagator::interpolateData(v_imu_data_.at(i), v_imu_data_.at(i + 1), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (prop_data.empty())\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << \"propagator::selectImuReadings(): No IMU measurements to propagate with (\" << (int)prop_data.size() << \" of 2). IMU-CAMERA are likely messed up\" << std::endl;\n\n\t\treturn prop_data;\n\t}\n\n\tif (prop_data.at(prop_data.size() - 1).timestamp != time_1)\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << std::fixed << \"propagator::selectImuReadings(): Missing inertial measurements to propagate with (\" << time_1 - v_imu_data_.at(v_imu_data_.size() - 1).timestamp \n\t\t\t\t<< \" sec missing)\" << std::endl;\n\n\t\timuData data = propagator::interpolateData(v_imu_data_.at(v_imu_data_.size() - 2), v_imu_data_.at(v_imu_data_.size() - 1), time_1);\n\t\tprop_data.push_back(data);\n\t}\n\n\tfor (size_t i = 0; i < prop_data.size() - 1; i++)\n\t{\n\t\tif (std::abs(prop_data.at(i + 1).timestamp - prop_data.at(i).timestamp) < 1e-12)\n\t\t{\n\t\t\tif (warn)\n\t\t\t\tstd::cout << \"propagator::selectImuReadings(): Zero DT between IMU reading \" << (int)i << \" and \" << (int)(i + 1) << \", removing it\" << std::endl;\n\n\t\t\tprop_data.erase(prop_data.begin() + i);\n\t\t\ti--;\n\t\t}\n\t}\n\n\tif (prop_data.size() < 2)\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << \"propagator::selectImuReadings(): No IMU measurements to propagate with (\" << (int)prop_data.size() << \" of 2). IMU-CAMERA are likely messed up\" << std::endl;\n\n\t\treturn prop_data;\n\t}\n\n\treturn prop_data;\n}\n void propagateAndClone(std::shared_ptr state_ptr, double timestamp) {\n\tif (state_ptr->timestamp == timestamp)\n\t{\n\t\tstd::cout << \"propagator::propagateAndClone(): Propagation called again at same timestep at last update timestep!\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tif (state_ptr->timestamp > timestamp)\n\t{\n\t\tstd::cout << \"propagator::propagateAndClone(): Propagation called trying to propagate backwards in time!\" << std::endl;\n\t\tstd::cout << std::fixed << \"propagator::propagateAndClone(): desired propagation = \" << timestamp - state_ptr->timestamp << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tif (!have_last_prop_time_offset)\n\t{\n\t\tlast_prop_time_offset = state_ptr->calib_dt_imu_cam->value()(0);\n\t\thave_last_prop_time_offset = true;\n\t}\n\n\tdouble t_off_new = state_ptr->calib_dt_imu_cam->value()(0);\n\n\tdouble time_0 = state_ptr->timestamp + last_prop_time_offset;\n\tdouble time_1 = timestamp + t_off_new;\n\tstd::vector prop_data;\n\t{\n\t\tprop_data = propagator::selectImuReadings(v_imu_data, time_0, time_1);\n\t}\n\n\tEigen::MatrixXd phi_sum = Eigen::MatrixXd::Identity(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tEigen::MatrixXd Qd_sum = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tdouble dt_sum = 0;\n\n\tif (prop_data.size() > 1)\n\t{\n\t\tfor (size_t i = 0; i < prop_data.size() - 1; i++)\n\t\t{\n\t\t\tEigen::MatrixXd F, Qdi;\n\t\t\tpredictAndCompute(state_ptr, prop_data.at(i), prop_data.at(i + 1), F, Qdi);\n\n\t\t\tphi_sum = F * phi_sum;\n\t\t\tQd_sum = F * Qd_sum * F.transpose() + Qdi;\n\t\t\tQd_sum = 0.5 * (Qd_sum + Qd_sum.transpose());\n\t\t\tdt_sum += prop_data.at(i + 1).timestamp - prop_data.at(i).timestamp;\n\t\t}\n\t}\n\tassert(std::abs((time_1 - time_0) - dt_sum) < 1e-4);\n\n\tEigen::Vector3d last_acc = Eigen::Vector3d::Zero();\n\tEigen::Vector3d last_gyr = Eigen::Vector3d::Zero();\n\tif (!prop_data.empty())\n\t{\n\t\tEigen::Matrix3d Dw = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_dw->value());\n\t\tEigen::Matrix3d Da = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_da->value());\n\t\tEigen::Matrix3d Tg = state::Tg(state_ptr->calib_imu_tg->value());\n\t\tlast_acc = state_ptr->calib_imu_acc->getRot() * Da * (prop_data.at(prop_data.size() - 1).acc - state_ptr->imu_ptr->getBa());\n\t\tlast_gyr = state_ptr->calib_imu_gyr->getRot() * Dw * (prop_data.at(prop_data.size() - 1).gyr - state_ptr->imu_ptr->getBg() - Tg * last_acc);\n\t}\n\n\tstd::vector> phi_order;\n\tphi_order.push_back(state_ptr->imu_ptr);\n\t\n\tif (state_ptr->options.do_calib_imu_intrinsics)\n\t{\n\t\tphi_order.push_back(state_ptr->calib_imu_dw);\n\t\tphi_order.push_back(state_ptr->calib_imu_da);\n\n\t\tif (state_ptr->options.do_calib_imu_g_sensitivity)\n\t\t{\n\t\t\tphi_order.push_back(state_ptr->calib_imu_tg);\n\t\t}\n\n\t\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\t{\n\t\t\tphi_order.push_back(state_ptr->calib_imu_gyr);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tphi_order.push_back(state_ptr->calib_imu_acc);\n\t\t}\n\t}\n\n\tstateHelper::ekfPropagation(state_ptr, phi_order, phi_order, phi_sum, Qd_sum);\n\n\tstate_ptr->timestamp = timestamp;\n\tlast_prop_time_offset = t_off_new;\n\n\tstateHelper::augmentClone(state_ptr, last_gyr);\n}\n void predictAndCompute(std::shared_ptr state_ptr, const imuData &data_minus, const imuData &data_plus, \n Eigen::MatrixXd &F, Eigen::MatrixXd &Qd) {\n\tdouble dt = data_plus.timestamp - data_minus.timestamp;\n\n\tEigen::Matrix3d Dw = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_dw->value());\n\tEigen::Matrix3d Da = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_da->value());\n\tEigen::Matrix3d Tg = state::Tg(state_ptr->calib_imu_tg->value());\n\n\tEigen::Vector3d a_hat1 = data_minus.acc - state_ptr->imu_ptr->getBa();\n\tEigen::Vector3d a_hat2 = data_plus.acc - state_ptr->imu_ptr->getBa();\n\tEigen::Vector3d a_hat_avg = 0.5 * (a_hat1 + a_hat2);\n\n\tEigen::Vector3d a_uncorrected = a_hat_avg;\n\tEigen::Matrix3d R_imu_acc = state_ptr->calib_imu_acc->getRot();\n\ta_hat1 = R_imu_acc * Da * a_hat1;\n\ta_hat2 = R_imu_acc * Da * a_hat2;\n\ta_hat_avg = R_imu_acc * Da * a_hat_avg;\n\n\tEigen::Vector3d w_hat1 = data_minus.gyr - state_ptr->imu_ptr->getBg() - Tg * a_hat1;\n\tEigen::Vector3d w_hat2 = data_plus.gyr - state_ptr->imu_ptr->getBg() - Tg * a_hat2;\n\tEigen::Vector3d w_hat_avg = 0.5 * (w_hat1 + w_hat2);\n\n\tEigen::Vector3d w_uncorrected = w_hat_avg;\n\tEigen::Matrix3d R_imu_gyr = state_ptr->calib_imu_gyr->getRot();\n\tw_hat1 = R_imu_gyr * Dw * w_hat1;\n\tw_hat2 = R_imu_gyr * Dw * w_hat2;\n\tw_hat_avg = R_imu_gyr * Dw * w_hat_avg;\n\n\tEigen::Matrix Xi_sum = Eigen::Matrix::Zero(3, 18);\n\tcomputeXiSum(state_ptr, dt, w_hat_avg, a_hat_avg, Xi_sum);\n\n\tEigen::Vector4d new_q;\n\tEigen::Vector3d new_v, new_p;\n\tpredictMeanRk4(state_ptr, dt, w_hat1, a_hat1, w_hat2, a_hat2, new_q, new_v, new_p);\n\n\tF = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tEigen::MatrixXd G = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, 12);\n\tcomputeCoefficientAnalytic(state_ptr, dt, w_hat_avg, a_hat_avg, w_uncorrected, a_uncorrected, new_q, new_v, new_p, Xi_sum, F, G);\n\n\tEigen::Matrix Qc = Eigen::Matrix::Zero();\n\tQc.block(0, 0, 3, 3) = std::pow(noises.sigma_w, 2) / dt * Eigen::Matrix3d::Identity();\n\tQc.block(3, 3, 3, 3) = std::pow(noises.sigma_a, 2) / dt * Eigen::Matrix3d::Identity();\n\tQc.block(6, 6, 3, 3) = std::pow(noises.sigma_wb, 2) / dt * Eigen::Matrix3d::Identity();\n\tQc.block(9, 9, 3, 3) = std::pow(noises.sigma_ab, 2) / dt * Eigen::Matrix3d::Identity();\n\n\tQd = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tQd = G * Qc * G.transpose();\n\tQd = 0.5 * (Qd + Qd.transpose());\n\n\tEigen::Matrix imu_x = state_ptr->imu_ptr->value();\n\timu_x.block(0, 0, 4, 1) = new_q;\n\timu_x.block(4, 0, 3, 1) = new_p;\n\timu_x.block(7, 0, 3, 1) = new_v;\n\tstate_ptr->imu_ptr->setValue(imu_x);\n\tstate_ptr->imu_ptr->setFej(imu_x);\n}\n static Eigen::MatrixXd computeDwH(std::shared_ptr state_ptr, const Eigen::Vector3d &gyr_uncorrected) {\n\tEigen::Vector3d e_1 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 0);\n\tEigen::Vector3d e_2 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 1);\n\tEigen::Vector3d e_3 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 2);\n\n\tdouble w_1 = gyr_uncorrected(0);\n\tdouble w_2 = gyr_uncorrected(1);\n\tdouble w_3 = gyr_uncorrected(2);\n\tassert(state_ptr->options.do_calib_imu_intrinsics);\n\n\tEigen::MatrixXd H_Dw = Eigen::MatrixXd::Zero(3, 6);\n\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\tH_Dw << w_1 * Eigen::MatrixXd::Identity(3, 3), w_2 * e_2, w_2 * e_3, w_3 * e_3;\n\telse\n\t\tH_Dw << w_1 * e_1, w_2 * e_1, w_2 * e_2, w_3 * Eigen::MatrixXd::Identity(3, 3);\n\n\treturn H_Dw;\n}\n static Eigen::MatrixXd computeDaH(std::shared_ptr state_ptr, const Eigen::Vector3d &acc_uncorrected) {\n\tEigen::Vector3d e_1 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 0);\n\tEigen::Vector3d e_2 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 1);\n\tEigen::Vector3d e_3 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 2);\n\n\tdouble a_1 = acc_uncorrected(0);\n\tdouble a_2 = acc_uncorrected(1);\n\tdouble a_3 = acc_uncorrected(2);\n\tassert(state_ptr->options.do_calib_imu_intrinsics);\n\n\tEigen::MatrixXd H_Da = Eigen::MatrixXd::Zero(3, 6);\n\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\tH_Da << a_1 * Eigen::MatrixXd::Identity(3, 3), a_2 * e_2, a_2 * e_3, a_3 * e_3;\n\telse\n\t\tH_Da << a_1 * e_1, a_2 * e_1, a_2 * e_2, a_3 * Eigen::MatrixXd::Identity(3, 3);\n\n\treturn H_Da;\n}\n static Eigen::MatrixXd computeTgH(std::shared_ptr state_ptr, const Eigen::Vector3d &acc) {\n\tdouble a_1 = acc(0);\n\tdouble a_2 = acc(1);\n\tdouble a_3 = acc(2);\n\tassert(state_ptr->options.do_calib_imu_intrinsics);\n\tassert(state_ptr->options.do_calib_imu_g_sensitivity);\n\n\tEigen::MatrixXd H_Tg = Eigen::MatrixXd::Zero(3, 9);\n\tH_Tg << a_1 * Eigen::MatrixXd::Identity(3, 3), a_2 * Eigen::MatrixXd::Identity(3, 3), a_3 * Eigen::MatrixXd::Identity(3, 3);\n\n\treturn H_Tg;\n}\n bool have_last_prop_time_offset;\n double last_prop_time_offset;\n protected:\n void predictMeanRk4(std::shared_ptr state_ptr, double dt, const Eigen::Vector3d &un_gyr_0, const Eigen::Vector3d &un_acc_0, \n const Eigen::Vector3d &un_gyr_1, const Eigen::Vector3d &un_acc_1, Eigen::Vector4d &new_q, Eigen::Vector3d &new_v, Eigen::Vector3d &new_p) {\n\tEigen::Vector3d un_gyr = un_gyr_0;\n\tEigen::Vector3d un_acc = un_acc_0;\n\tEigen::Vector3d w_alpha = (un_gyr_1 - un_gyr_0) / dt;\n\tEigen::Vector3d a_jerk = (un_acc_1 - un_acc_0) / dt;\n\n\tEigen::Vector4d q_0 = state_ptr->imu_ptr->getQuat();\n\tEigen::Vector3d p_0 = state_ptr->imu_ptr->getPos();\n\tEigen::Vector3d v_0 = state_ptr->imu_ptr->getVel();\n\n\tEigen::Vector4d dq_0 = {0, 0, 0, 1};\n\tEigen::Vector4d q_0_dot = 0.5 * quatType::omega(un_gyr) * dq_0;\n\tEigen::Vector3d p_0_dot = v_0;\n\tEigen::Matrix3d R_0_world = quatType::quatToRot(quatType::quatMultiply(dq_0, q_0));\n\tEigen::Vector3d v_0_dot = R_0_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k1_q = q_0_dot * dt;\n\tEigen::Vector3d k1_p = p_0_dot * dt;\n\tEigen::Vector3d k1_v = v_0_dot * dt;\n\n\tun_gyr += 0.5 * w_alpha * dt;\n\tun_acc += 0.5 * a_jerk * dt;\n\n\tEigen::Vector4d dq_1 = quatType::quatNorm(dq_0 + 0.5 * k1_q);\n\tEigen::Vector3d v_1 = v_0 + 0.5 * k1_v;\n\tEigen::Vector4d q_1_dot = 0.5 * quatType::omega(un_gyr) * dq_1;\n\tEigen::Vector3d p_1_dot = v_1;\n\tEigen::Matrix3d R_1_world = quatType::quatToRot(quatType::quatMultiply(dq_1, q_0));\n\tEigen::Vector3d v_1_dot = R_1_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k2_q = q_1_dot * dt;\n\tEigen::Vector3d k2_p = p_1_dot * dt;\n\tEigen::Vector3d k2_v = v_1_dot * dt;\n\n\tEigen::Vector4d dq_2 = quatType::quatNorm(dq_0 + 0.5 * k2_q);\n\tEigen::Vector3d v_2 = v_0 + 0.5 * k2_v;\n\n\tEigen::Vector4d q_2_dot = 0.5 * quatType::omega(un_gyr) * dq_2;\n\tEigen::Vector3d p_2_dot = v_2;\n\tEigen::Matrix3d R_2_world = quatType::quatToRot(quatType::quatMultiply(dq_2, q_0));\n\tEigen::Vector3d v_2_dot = R_2_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k3_q = q_2_dot * dt;\n\tEigen::Vector3d k3_p = p_2_dot * dt;\n\tEigen::Vector3d k3_v = v_2_dot * dt;\n\n\tun_gyr += 0.5 * w_alpha * dt;\n\tun_acc += 0.5 * a_jerk * dt;\n\n\tEigen::Vector4d dq_3 = quatType::quatNorm(dq_0 + k3_q);\n\tEigen::Vector3d v_3 = v_0 + k3_v;\n\n\tEigen::Vector4d q_3_dot = 0.5 * quatType::omega(un_gyr) * dq_3;\n\tEigen::Vector3d p_3_dot = v_3;\n\tEigen::Matrix3d R_3_world = quatType::quatToRot(quatType::quatMultiply(dq_3, q_0));\n\tEigen::Vector3d v_3_dot = R_3_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k4_q = q_3_dot * dt;\n\tEigen::Vector3d k4_p = p_3_dot * dt;\n\tEigen::Vector3d k4_v = v_3_dot * dt;\n\n\tEigen::Vector4d dq = quatType::quatNorm(dq_0 + (1.0 / 6.0) * k1_q + (1.0 / 3.0) * k2_q + (1.0 / 3.0) * k3_q + (1.0 / 6.0) * k4_q);\n\tnew_q = quatType::quatMultiply(dq, q_0);\n\tnew_p = p_0 + (1.0 / 6.0) * k1_p + (1.0 / 3.0) * k2_p + (1.0 / 3.0) * k3_p + (1.0 / 6.0) * k4_p;\n\tnew_v = v_0 + (1.0 / 6.0) * k1_v + (1.0 / 3.0) * k2_v + (1.0 / 3.0) * k3_v + (1.0 / 6.0) * k4_v;\n}\n void computeXiSum(std::shared_ptr state_ptr, double dt, const Eigen::Vector3d &un_gyr, \n const Eigen::Vector3d &un_acc, Eigen::Matrix &Xi_sum) {\n\tdouble un_gyr_norm = un_gyr.norm();\n\tdouble d_th = un_gyr_norm * dt;\n\tEigen::Vector3d un_gyr_normalized = Eigen::Vector3d::Zero();\n\n\tif (un_gyr_norm > 1e-12)\n\t\tun_gyr_normalized = un_gyr / un_gyr_norm;\n\n\tdouble dt_2 = std::pow(dt, 2);\n\tdouble dt_3 = std::pow(dt, 3);\n\tdouble norm_2 = std::pow(un_gyr_norm, 2);\n\tdouble norm_3 = std::pow(un_gyr_norm, 3);\n\tdouble cos_dth = std::cos(d_th);\n\tdouble sin_dth = std::sin(d_th);\n\tdouble d_th_2 = std::pow(d_th, 2);\n\tdouble d_th_3 = std::pow(d_th, 3);\n\n\tEigen::Matrix3d sK = quatType::skewSymmetric(un_gyr_normalized);\n\tEigen::Matrix3d sK2 = sK * sK;\n\tEigen::Matrix3d sA = quatType::skewSymmetric(un_acc);\n\n\tEigen::Matrix3d R_temp = quatType::expSo3(-un_gyr * dt);\n\tEigen::Matrix3d Jr_temp = quatType::JrighySo3(-un_gyr * dt);\n\n\tEigen::Matrix3d Xi_1, Xi_2, Xi_3, Xi_4;\n\n \tif (!(un_gyr_norm < 1.0 / 180 * M_PI / 2))\n \t{\n\t\tXi_1 = Eigen::Matrix3d::Identity() * dt + (1.0 - cos_dth) / un_gyr_norm * sK + (dt - sin_dth / un_gyr_norm) * sK2;\n\n\t\tXi_2 = 1.0 / 2 * dt_2 * Eigen::Matrix3d::Identity() + (d_th - sin_dth) / norm_2 * sK + (1.0 / 2 * dt_2 - (1.0 - cos_dth) / norm_2) * sK2;\n\n\t\tXi_3 = 1.0 / 2 * dt_2 * sA + (sin_dth - d_th) / norm_2 * sA * sK + (sin_dth - d_th * cos_dth) / norm_2 * sK * sA +\n\t\t\t(1.0 / 2 * dt_2 - (1.0 - cos_dth) / norm_2) * sA * sK2 +\n\t\t\t(1.0 / 2 * dt_2 + (1.0 - cos_dth - d_th * sin_dth) / norm_2) * (sK2 * sA + un_gyr_normalized.dot(un_acc) * sK) -\n\t\t\t(3 * sin_dth - 2 * d_th - d_th * cos_dth) / norm_2 * un_gyr_normalized.dot(un_acc) * sK2;\n\n\t\tXi_4 = 1.0 / 6 * dt_3 * sA + (2 * (1.0 - cos_dth) - d_th_2) / (2 * norm_3) * sA * sK +\n\t\t\t((2 * (1.0 - cos_dth) - d_th * sin_dth) / norm_3) * sK * sA + ((sin_dth - d_th) / norm_3 + dt_3 / 6) * sA * sK2 +\n\t\t\t((d_th - 2 * sin_dth + 1.0 / 6 * d_th_3 + d_th * cos_dth) / norm_3) * (sK2 * sA + un_gyr_normalized.dot(un_acc) * sK) +\n\t\t\t(4 * cos_dth - 4 + d_th_2 + d_th * sin_dth) / norm_3 * un_gyr_normalized.dot(un_acc) * sK2;\n\n\t}\n\telse\n\t{\n\t\tXi_1 = dt * (Eigen::Matrix3d::Identity() + sin_dth * sK + (1.0 - cos_dth) * sK2);\n\n\t\tXi_2 = 1.0 / 2 * dt * Xi_1;\n\n\t\tXi_3 = 1.0 / 2 * dt_2 * (sA + sin_dth * (-sA * sK + sK * sA + un_gyr_normalized.dot(un_acc) * sK2) \n\t\t\t+ (1.0 - cos_dth) * (sA * sK2 + sK2 * sA + un_gyr_normalized.dot(un_acc) * sK));\n\n\t\tXi_4 = 1.0 / 3 * dt * Xi_3;\n\t}\n\n\tXi_sum.setZero();\n\tXi_sum.block<3, 3>(0, 0) = R_temp;\n\tXi_sum.block<3, 3>(0, 3) = Xi_1;\n\tXi_sum.block<3, 3>(0, 6) = Xi_2;\n\tXi_sum.block<3, 3>(0, 9) = Jr_temp;\n\tXi_sum.block<3, 3>(0, 12) = Xi_3;\n\tXi_sum.block<3, 3>(0, 15) = Xi_4;\n}\n void computeCoefficientAnalytic(std::shared_ptr state_ptr, double dt, const Eigen::Vector3d &un_gyr, const Eigen::Vector3d &un_acc, \n const Eigen::Vector3d &gyr_uncorrected, const Eigen::Vector3d &acc_uncorrected, const Eigen::Vector4d &new_q, const Eigen::Vector3d &new_v, \n const Eigen::Vector3d &new_p, const Eigen::Matrix &Xi_sum, Eigen::MatrixXd &F, Eigen::MatrixXd &G) {\n\tint local_size = 0;\n\tint th_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->q()->getSize();\n\tint p_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->p()->getSize();\n\tint v_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->v()->getSize();\n\tint bg_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->bg()->getSize();\n\tint ba_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->ba()->getSize();\n\n\tint Dw_id = -1;\n\tint Da_id = -1;\n\tint Tg_id = -1;\n\tint th_imu_acc_id = -1;\n\tint th_imu_gyr_id = -1;\n\n\tif (state_ptr->options.do_calib_imu_intrinsics)\n\t{\n\t\tDw_id = local_size;\n\t\tlocal_size += state_ptr->calib_imu_dw->getSize();\n\t\tDa_id = local_size;\n\t\tlocal_size += state_ptr->calib_imu_da->getSize();\n\n\t\tif (state_ptr->options.do_calib_imu_g_sensitivity)\n\t\t{\n\t\t\tTg_id = local_size;\n\t\t\tlocal_size += state_ptr->calib_imu_tg->getSize();\n\t\t}\n\n\t\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\t{\n\t\t\tth_imu_gyr_id = local_size;\n\t\t\tlocal_size += state_ptr->calib_imu_gyr->getSize();\n\t\t}\n\t\telse\n\t\t{\n\t\t\tth_imu_acc_id = local_size;\n\t\t\tlocal_size += state_ptr->calib_imu_acc->getSize();\n\t\t}\n\t}\n\n\tEigen::Matrix3d R_k = state_ptr->imu_ptr->getRot();\n\tEigen::Vector3d v_k = state_ptr->imu_ptr->getVel();\n\tEigen::Vector3d p_k = state_ptr->imu_ptr->getPos();\n\n\tif (state_ptr->options.do_fej)\n\t{\n\t\tR_k = state_ptr->imu_ptr->getRotFej();\n\t\tv_k = state_ptr->imu_ptr->getVelFej();\n\t\tp_k = state_ptr->imu_ptr->getPosFej();\n\t}\n\n\tEigen::Matrix3d dR_temp = quatType::quatToRot(new_q) * R_k.transpose();\n\n\tEigen::Matrix3d Dw = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_dw->value());\n\tEigen::Matrix3d Da = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_da->value());\n\tEigen::Matrix3d Tg = state::Tg(state_ptr->calib_imu_tg->value());\n\tEigen::Matrix3d R_imu_acc = state_ptr->calib_imu_acc->getRot();\n\tEigen::Matrix3d R_imu_gyr = state_ptr->calib_imu_gyr->getRot();\n\tEigen::Vector3d acc_k = R_imu_acc * Da * acc_uncorrected;\n\tEigen::Vector3d gyr_k = R_imu_gyr * Dw * gyr_uncorrected;\n\n\tEigen::Matrix3d Xi_1 = Xi_sum.block<3, 3>(0, 3);\n\tEigen::Matrix3d Xi_2 = Xi_sum.block<3, 3>(0, 6);\n\tEigen::Matrix3d Jr_temp = Xi_sum.block<3, 3>(0, 9);\n\tEigen::Matrix3d Xi_3 = Xi_sum.block<3, 3>(0, 12);\n\tEigen::Matrix3d Xi_4 = Xi_sum.block<3, 3>(0, 15);\n\n\tF.block<3, 3>(th_id, th_id) = dR_temp;\n\tF.block<3, 3>(p_id, th_id) = - quatType::skewSymmetric(new_p - p_k - v_k * dt + 0.5 * gravity * dt * dt) * R_k.transpose();\n\tF.block<3, 3>(v_id, th_id) = - quatType::skewSymmetric(new_v - v_k + gravity * dt) * R_k.transpose();\n\n\tF.block<3, 3>(p_id, p_id).setIdentity();\n\n\tF.block<3, 3>(p_id, v_id) = Eigen::Matrix3d::Identity() * dt;\n\tF.block<3, 3>(v_id, v_id).setIdentity();\n\n\tF.block<3, 3>(th_id, bg_id) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw;\n\tF.block<3, 3>(p_id, bg_id) = R_k.transpose() * Xi_4 * R_imu_gyr * Dw;\n\tF.block<3, 3>(v_id, bg_id) = R_k.transpose() * Xi_3 * R_imu_gyr * Dw;\n\tF.block<3, 3>(bg_id, bg_id).setIdentity();\n\n\tF.block<3, 3>(th_id, ba_id) = dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * R_imu_acc * Da;\n\tF.block<3, 3>(p_id, ba_id) = - R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tF.block<3, 3>(v_id, ba_id) = - R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tF.block<3, 3>(ba_id, ba_id).setIdentity();\n\n\tif (Dw_id != -1)\n\t{\n\t\tEigen::MatrixXd H_Dw = computeDwH(state_ptr, gyr_uncorrected);\n\t\tF.block(th_id, Dw_id, 3, state_ptr->calib_imu_dw->getSize()) = dR_temp * Jr_temp * dt * R_imu_gyr * H_Dw;\n\t\tF.block(p_id, Dw_id, 3, state_ptr->calib_imu_dw->getSize()) = - R_k.transpose() * Xi_4 * R_imu_gyr * H_Dw;\n\t\tF.block(v_id, Dw_id, 3, state_ptr->calib_imu_dw->getSize()) = - R_k.transpose() * Xi_3 * R_imu_gyr * H_Dw;\n\t\tF.block(Dw_id, Dw_id, state_ptr->calib_imu_dw->getSize(), state_ptr->calib_imu_dw->getSize()).setIdentity();\n\t}\n\n\tif (Da_id != -1)\n\t{\n\t\tEigen::MatrixXd H_Da = computeDaH(state_ptr, acc_uncorrected);\n\t\tF.block(th_id, Da_id, 3, state_ptr->calib_imu_da->getSize()) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * R_imu_acc * H_Da;\n\t\tF.block(p_id, Da_id, 3, state_ptr->calib_imu_da->getSize()) = R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * R_imu_acc * H_Da;\n\t\tF.block(v_id, Da_id, 3, state_ptr->calib_imu_da->getSize()) = R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * R_imu_acc * H_Da;\n\t\tF.block(Da_id, Da_id, state_ptr->calib_imu_da->getSize(), state_ptr->calib_imu_da->getSize()).setIdentity();\n\t}\n\n\tif (Tg_id != -1)\n\t{\n\t\tEigen::MatrixXd H_Tg = computeTgH(state_ptr, acc_k);\n\t\tF.block(th_id, Tg_id, 3, state_ptr->calib_imu_tg->getSize()) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw * H_Tg;\n\t\tF.block(p_id, Tg_id, 3, state_ptr->calib_imu_tg->getSize()) = R_k.transpose() * Xi_4 * R_imu_gyr * Dw * H_Tg;\n\t\tF.block(v_id, Tg_id, 3, state_ptr->calib_imu_tg->getSize()) = R_k.transpose() * Xi_3 * R_imu_gyr * Dw * H_Tg;\n\t\tF.block(Tg_id, Tg_id, state_ptr->calib_imu_tg->getSize(), state_ptr->calib_imu_tg->getSize()).setIdentity();\n\t}\n\n\tif (th_imu_acc_id != -1)\n\t{\n\t\tF.block<3, 3>(th_id, th_imu_acc_id) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * quatType::skewSymmetric(acc_k);\n\t\tF.block<3, 3>(p_id, th_imu_acc_id) = R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * quatType::skewSymmetric(acc_k);\n\t\tF.block<3, 3>(v_id, th_imu_acc_id) = R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * quatType::skewSymmetric(acc_k);\n\t\tF.block<3, 3>(th_imu_acc_id, th_imu_acc_id).setIdentity();\n\t}\n\n\tif (th_imu_gyr_id != -1)\n\t{\n\t\tF.block<3, 3>(th_id, th_imu_gyr_id) = dR_temp * Jr_temp * dt * quatType::skewSymmetric(gyr_k);\n\t\tF.block<3, 3>(p_id, th_imu_gyr_id) = -R_k.transpose() * Xi_4 * quatType::skewSymmetric(gyr_k);\n\t\tF.block<3, 3>(v_id, th_imu_gyr_id) = -R_k.transpose() * Xi_3 * quatType::skewSymmetric(gyr_k);\n\t\tF.block<3, 3>(th_imu_gyr_id, th_imu_gyr_id).setIdentity();\n\t}\n\n\tG.block<3, 3>(th_id, 0) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw;\n\tG.block<3, 3>(p_id, 0) = R_k.transpose() * Xi_4 * R_imu_gyr * Dw;\n\tG.block<3, 3>(v_id, 0) = R_k.transpose() * Xi_3 * R_imu_gyr * Dw;\n\tG.block<3, 3>(th_id, 3) = dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * R_imu_acc * Da;\n\tG.block<3, 3>(p_id, 3) = -R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tG.block<3, 3>(v_id, 3) = -R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tG.block<3, 3>(bg_id, 6) = dt * Eigen::Matrix3d::Identity();\n\tG.block<3, 3>(ba_id, 9) = dt * Eigen::Matrix3d::Identity();\n}\n noiseManager noises;\n std::vector v_imu_data;\n Eigen::Vector3d gravity;\n std::atomic cache_imu_valid;\n double cache_state_time;\n Eigen::MatrixXd cache_state_est;\n Eigen::MatrixXd cache_state_covariance;\n double cache_t_off;\n public:\n updaterMsckf(updaterOptions &options_, featureInitializerOptions &feature_init_options_) {\n\toptions.sigma_pix_sq = std::pow(options.sigma_pix, 2);\n\n\tinitializer_feature = std::shared_ptr(new featureInitializer(feature_init_options_));\n\n\tfor (int i = 1; i < 500; i++)\n\t{\n\t\tboost::math::chi_squared chi_squared_dist(i);\n\t\tchi_squared_table[i] = boost::math::quantile(chi_squared_dist, 0.95);\n\t}\n}\n void update(std::shared_ptr state_ptr, std::vector> &feature_vec) {\n\tif (feature_vec.empty())\n\t\treturn;\n\n\tboost::posix_time::ptime rT0, rT1, rT2, rT3;\n\trT0 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::vector clone_times;\n\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t{\n\t\tclone_times.emplace_back(clone_imu.first);\n\t}\n\n\tauto it0 = feature_vec.begin();\n\twhile (it0 != feature_vec.end())\n\t{\n\t\t(*it0)->cleanOldMeasurements(clone_times);\n\n\t\tint count_meas = 0;\n\t\tfor (const auto &pair : (*it0)->timestamps)\n\t\t{\n\t\t\tcount_meas += (*it0)->timestamps[pair.first].size();\n\t\t}\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it0)->feature_id);\n\t\tint required_meas = 2;\n\n\t\tif (count_meas < 1)\n\t\t{\n\t\t\t(*it0)->to_delete = true;\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse if (count_meas < required_meas)\n\t\t{\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n\trT1 = boost::posix_time::microsec_clock::local_time();\n\n\tsize_t max_meas_size = 0;\n\tfor (size_t i = 0; i < feature_vec.size(); i++)\n\t{\n\t\tfor (const auto &pair : feature_vec.at(i)->timestamps)\n\t\t{\n\t\t\tmax_meas_size += 2 * feature_vec.at(i)->timestamps[pair.first].size();\n\t\t}\n\t}\n\n\tsize_t max_hx_size = state_ptr->maxCovSize();\n\n\tEigen::VectorXd res_big = Eigen::VectorXd::Zero(max_meas_size);\n\tEigen::MatrixXd Hx_big = Eigen::MatrixXd::Zero(max_meas_size, max_hx_size);\n\tEigen::MatrixXd R_big = Eigen::MatrixXd::Identity(max_meas_size, max_meas_size);\n\tstd::unordered_map, size_t> Hx_mapping;\n\tstd::vector> Hx_order_big;\n\tsize_t count_jacob = 0;\n\tsize_t count_meas = 0;\n\n\tauto it2 = feature_vec.begin();\n\twhile (it2 != feature_vec.end())\n\t{\n\t\tassert(state_ptr->map_points.find((*it2)->feature_id) != state_ptr->map_points.end());\n\t\tassert(state_ptr->map_points.at((*it2)->feature_id)->feature_id == (*it2)->feature_id);\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it2)->feature_id);\n\n\t\tupdaterHelper::updaterHelperFeature feat;\n\t\tfeat.feature_id = (*it2)->feature_id;\n\t\tfeat.uvs = (*it2)->uvs;\n\t\tfeat.uvs_norm = (*it2)->uvs_norm;\n\t\tfeat.timestamps = (*it2)->timestamps;\n\t\tfeat.frames = (*it2)->frames;\n\t\tfeat.color = (*it2)->color;\n\n\t\tfeat.position_global = map_point->getPointXYZ(false);\n\t\tfeat.position_global_fej = map_point->getPointXYZ(true);\n\n\t\tEigen::MatrixXd H_f;\n\t\tEigen::MatrixXd H_x;\n\t\tEigen::VectorXd res;\n\t\tstd::vector> Hx_order;\n\n\t\tupdaterHelper::getFeatureJacobianFull(state_ptr, feat, H_f, H_x, res, Hx_order);\n\n\t\tEigen::MatrixXd H_xf = H_x;\n\n\t\tH_xf.conservativeResize(H_x.rows(), H_x.cols() + H_f.cols());\n\t\tH_xf.block(0, H_x.cols(), H_x.rows(), H_f.cols()) = H_f;\n\n\t\tstd::vector> Hxf_order = Hx_order;\n\t\tHxf_order.push_back(map_point);\n\n\t\tEigen::MatrixXd P_marg = stateHelper::getMarginalCovariance(state_ptr, Hxf_order);\n\t\tEigen::MatrixXd S = H_xf * P_marg * H_xf.transpose();\n\t\tdouble sigma_pix_sq = options_slam.sigma_pix_sq;\n\t\tS.diagonal() += sigma_pix_sq * Eigen::VectorXd::Ones(S.rows());\n\t\tdouble chi2 = res.dot(S.llt().solve(res));\n\n\t\tdouble chi2_check;\n\t\tif (res.rows() < 500)\n\t\t{\n\t\t\tchi2_check = chi_squared_table[res.rows()];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tboost::math::chi_squared chi_squared_dist(res.rows());\n\t\t\tchi2_check = boost::math::quantile(chi_squared_dist, 0.95);\n\t\t\tstd::cout << \"chi2_check over the residual limit - \" << (int)res.rows() << std::endl;\n\t\t}\n\n\t\tdouble chi2_multipler = options_slam.chi2_multipler;\n\t\t\n\t\tif (chi2 > chi2_multipler * chi2_check)\n\t\t{\n\t\t\tmap_point->update_fail_count++;\n\n\t\t\t(*it2)->to_delete = true;\n\t\t\tit2 = feature_vec.erase(it2);\n\t\t\tcontinue;\n\t\t}\n\n\t\tsize_t count_hx = 0;\n\t\tfor (const auto &var : Hxf_order)\n\t\t{\n\t\t\tif (Hx_mapping.find(var) == Hx_mapping.end())\n\t\t\t{\n\t\t\t\tHx_mapping.insert({var, count_jacob});\n\t\t\t\tHx_order_big.push_back(var);\n\t\t\t\tcount_jacob += var->getSize();\n\t\t\t}\n\n\t\t\tHx_big.block(count_meas, Hx_mapping[var], H_xf.rows(), var->getSize()) = H_xf.block(0, count_hx, H_xf.rows(), var->getSize());\n\t\t\tcount_hx += var->getSize();\n\t\t}\n\n\t\tR_big.block(count_meas, count_meas, res.rows(), res.rows()) *= sigma_pix_sq;\n\n\t\tres_big.block(count_meas, 0, res.rows(), 1) = res;\n\t\tcount_meas += res.rows();\n\t\tit2++;\n\t}\n\trT2 = boost::posix_time::microsec_clock::local_time();\n\n\tfor (size_t f = 0; f < feature_vec.size(); f++)\n\t{\n\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).back() > clone_times.at(clone_times.size() - 1) + 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).back() > clone_times.at(clone_times.size() - 1) + 1e-5))\n\t\t{\n\t\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).front() < clone_times.at(clone_times.size() - 3) - 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).front() < clone_times.at(clone_times.size() - 3) - 1e-5))\n\t\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t}\n\n\tif (count_meas < 1)\n\t{\n\t\treturn;\n\t}\n\tassert(count_meas <= max_meas_size);\n\tassert(count_jacob <= max_hx_size);\n\tres_big.conservativeResize(count_meas, 1);\n\tHx_big.conservativeResize(count_meas, count_jacob);\n\tR_big.conservativeResize(count_meas, count_meas);\n\n\tstateHelper::ekfUpdate(state_ptr, Hx_order_big, Hx_big, res_big, R_big);\n\trT3 = boost::posix_time::microsec_clock::local_time();\n\n\t// Time test\n\t/*\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT1 - rT0).total_microseconds() * 1e-6 << \" seconds to clean.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds creating linear system.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds to update (\" \n\t\t<< (int)feature_vec.size() << \" feats of \" << (int)Hx_big.rows() << \" size).\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT1).total_microseconds() * 1e-6 << \" seconds total.\" << std::endl;\n\t*/\n\t// Time test\n}\n protected:\n updaterOptions options;\n std::shared_ptr initializer_feature;\n std::map chi_squared_table;\n public:\n updaterSlam(updaterOptions &options_slam_, featureInitializerOptions &feat_init_options_, std::shared_ptr database_) {\n\toptions_slam.sigma_pix_sq = std::pow(options_slam.sigma_pix, 2);\n\n\tinitializer_feature = std::shared_ptr(new featureInitializer(feat_init_options_));\n\n\tfor (int i = 1; i < 500; i++)\n\t{\n\t\tboost::math::chi_squared chi_squared_dist(i);\n\t\tchi_squared_table[i] = boost::math::quantile(chi_squared_dist, 0.95);\n\t}\n}\n void update(std::shared_ptr state_ptr, voxelHashMap &voxel_map, std::vector> &feature_vec) {\n\tif (feature_vec.empty())\n\t\treturn;\n\n\tboost::posix_time::ptime rT0, rT1, rT2, rT3;\n\trT0 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::vector clone_times;\n\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t{\n\t\tclone_times.emplace_back(clone_imu.first);\n\t}\n\n\tauto it0 = feature_vec.begin();\n\twhile (it0 != feature_vec.end())\n\t{\n\t\t(*it0)->cleanOldMeasurements(clone_times);\n\n\t\tint count_meas = 0;\n\t\tfor (const auto &pair : (*it0)->timestamps)\n\t\t{\n\t\t\tcount_meas += (*it0)->timestamps[pair.first].size();\n\t\t}\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it0)->feature_id);\n\t\tint required_meas = 2;\n\n\t\tif (count_meas < 1)\n\t\t{\n\t\t\t(*it0)->to_delete = true;\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse if (count_meas < required_meas)\n\t\t{\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n\trT1 = boost::posix_time::microsec_clock::local_time();\n\n\tsize_t max_meas_size = 0;\n\tfor (size_t i = 0; i < feature_vec.size(); i++)\n\t{\n\t\tfor (const auto &pair : feature_vec.at(i)->timestamps)\n\t\t{\n\t\t\tmax_meas_size += 2 * feature_vec.at(i)->timestamps[pair.first].size();\n\t\t}\n\t}\n\n\tsize_t max_hx_size = state_ptr->maxCovSize();\n\n\tEigen::VectorXd res_big = Eigen::VectorXd::Zero(max_meas_size);\n\tEigen::MatrixXd Hx_big = Eigen::MatrixXd::Zero(max_meas_size, max_hx_size);\n\tEigen::MatrixXd R_big = Eigen::MatrixXd::Identity(max_meas_size, max_meas_size);\n\tstd::unordered_map, size_t> Hx_mapping;\n\tstd::vector> Hx_order_big;\n\tsize_t count_jacob = 0;\n\tsize_t count_meas = 0;\n\n\tauto it2 = feature_vec.begin();\n\twhile (it2 != feature_vec.end())\n\t{\n\t\tassert(state_ptr->map_points.find((*it2)->feature_id) != state_ptr->map_points.end());\n\t\tassert(state_ptr->map_points.at((*it2)->feature_id)->feature_id == (*it2)->feature_id);\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it2)->feature_id);\n\n\t\tupdaterHelper::updaterHelperFeature feat;\n\t\tfeat.feature_id = (*it2)->feature_id;\n\t\tfeat.uvs = (*it2)->uvs;\n\t\tfeat.uvs_norm = (*it2)->uvs_norm;\n\t\tfeat.timestamps = (*it2)->timestamps;\n\t\tfeat.frames = (*it2)->frames;\n\t\tfeat.color = (*it2)->color;\n\n\t\tfeat.position_global = map_point->getPointXYZ(false);\n\t\tfeat.position_global_fej = map_point->getPointXYZ(true);\n\n\t\tEigen::MatrixXd H_f;\n\t\tEigen::MatrixXd H_x;\n\t\tEigen::VectorXd res;\n\t\tstd::vector> Hx_order;\n\n\t\tupdaterHelper::getFeatureJacobianFull(state_ptr, feat, H_f, H_x, res, Hx_order);\n\n\t\tEigen::MatrixXd H_xf = H_x;\n\n\t\tH_xf.conservativeResize(H_x.rows(), H_x.cols() + H_f.cols());\n\t\tH_xf.block(0, H_x.cols(), H_x.rows(), H_f.cols()) = H_f;\n\n\t\tstd::vector> Hxf_order = Hx_order;\n\t\tHxf_order.push_back(map_point);\n\n\t\tEigen::MatrixXd P_marg = stateHelper::getMarginalCovariance(state_ptr, Hxf_order);\n\t\tEigen::MatrixXd S = H_xf * P_marg * H_xf.transpose();\n\t\tdouble sigma_pix_sq = options_slam.sigma_pix_sq;\n\t\tS.diagonal() += sigma_pix_sq * Eigen::VectorXd::Ones(S.rows());\n\t\tdouble chi2 = res.dot(S.llt().solve(res));\n\n\t\tdouble chi2_check;\n\t\tif (res.rows() < 500)\n\t\t{\n\t\t\tchi2_check = chi_squared_table[res.rows()];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tboost::math::chi_squared chi_squared_dist(res.rows());\n\t\t\tchi2_check = boost::math::quantile(chi_squared_dist, 0.95);\n\t\t\tstd::cout << \"chi2_check over the residual limit - \" << (int)res.rows() << std::endl;\n\t\t}\n\n\t\tdouble chi2_multipler = options_slam.chi2_multipler;\n\t\t\n\t\tif (chi2 > chi2_multipler * chi2_check)\n\t\t{\n\t\t\tmap_point->update_fail_count++;\n\n\t\t\t(*it2)->to_delete = true;\n\t\t\tit2 = feature_vec.erase(it2);\n\t\t\tcontinue;\n\t\t}\n\n\t\tsize_t count_hx = 0;\n\t\tfor (const auto &var : Hxf_order)\n\t\t{\n\t\t\tif (Hx_mapping.find(var) == Hx_mapping.end())\n\t\t\t{\n\t\t\t\tHx_mapping.insert({var, count_jacob});\n\t\t\t\tHx_order_big.push_back(var);\n\t\t\t\tcount_jacob += var->getSize();\n\t\t\t}\n\n\t\t\tHx_big.block(count_meas, Hx_mapping[var], H_xf.rows(), var->getSize()) = H_xf.block(0, count_hx, H_xf.rows(), var->getSize());\n\t\t\tcount_hx += var->getSize();\n\t\t}\n\n\t\tR_big.block(count_meas, count_meas, res.rows(), res.rows()) *= sigma_pix_sq;\n\n\t\tres_big.block(count_meas, 0, res.rows(), 1) = res;\n\t\tcount_meas += res.rows();\n\t\tit2++;\n\t}\n\trT2 = boost::posix_time::microsec_clock::local_time();\n\n\tfor (size_t f = 0; f < feature_vec.size(); f++)\n\t{\n\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).back() > clone_times.at(clone_times.size() - 1) + 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).back() > clone_times.at(clone_times.size() - 1) + 1e-5))\n\t\t{\n\t\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).front() < clone_times.at(clone_times.size() - 3) - 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).front() < clone_times.at(clone_times.size() - 3) - 1e-5))\n\t\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t}\n\n\tif (count_meas < 1)\n\t{\n\t\treturn;\n\t}\n\tassert(count_meas <= max_meas_size);\n\tassert(count_jacob <= max_hx_size);\n\tres_big.conservativeResize(count_meas, 1);\n\tHx_big.conservativeResize(count_meas, count_jacob);\n\tR_big.conservativeResize(count_meas, count_meas);\n\n\tstateHelper::ekfUpdate(state_ptr, Hx_order_big, Hx_big, res_big, R_big);\n\trT3 = boost::posix_time::microsec_clock::local_time();\n\n\t// Time test\n\t/*\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT1 - rT0).total_microseconds() * 1e-6 << \" seconds to clean.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds creating linear system.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds to update (\" \n\t\t<< (int)feature_vec.size() << \" feats of \" << (int)Hx_big.rows() << \" size).\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT1).total_microseconds() * 1e-6 << \" seconds total.\" << std::endl;\n\t*/\n\t// Time test\n}\n void delayedInit(std::shared_ptr state_ptr, voxelHashMap &voxel_map, std::vector> &feature_vec, pcl::PointCloud::Ptr voxels_history) {\n\tif (feature_vec.empty())\n\t\treturn;\n\n\tboost::posix_time::ptime rT0, rT1, rT2, rT3;\n\trT0 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::vector clone_times;\n\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t{\n\t\tclone_times.emplace_back(clone_imu.first);\n\t}\n\n\tauto it0 = feature_vec.begin();\n\twhile (it0 != feature_vec.end())\n\t{\n\t\t(*it0)->cleanOldMeasurements(clone_times);\n\n\t\tint count_meas = 0;\n\t\tfor (const auto &pair : (*it0)->timestamps)\n\t\t{\n\t\t\tcount_meas += (*it0)->timestamps[pair.first].size();\n\t\t}\n\n\t\tif (count_meas < 2)\n\t\t{\n\t\t\t(*it0)->to_delete = true;\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n\trT1 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::unordered_map> clones_cam;\n\tfor (const auto &clone_calib : state_ptr->calib_cam_imu)\n\t{\n\t\tstd::unordered_map clones_cami;\n\t\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t\t{\n\t\t\tEigen::Matrix R_GtoCi = clone_calib.second->getRot() * clone_imu.second->getRot();\n\t\t\tEigen::Matrix p_CioinG = clone_imu.second->getPos() - R_GtoCi.transpose() * clone_calib.second->getPos();\n\n\t\t\tclones_cami.insert({clone_imu.first, featureInitializer::clonePose(R_GtoCi, p_CioinG)});\n\t\t}\n\n\t\tclones_cam.insert({clone_calib.first, clones_cami});\n\t}\n\n\tauto it1 = feature_vec.begin();\n\twhile (it1 != feature_vec.end())\n\t{\n\t\tbool success_tri = true;\n\t\tsuccess_tri = initializer_feature->singleTriangulation(*it1, clones_cam);\n\n\t\tbool success_refine = true;\n\t\tif (initializer_feature->config().refine_features)\n\t\t{\n\t\t\tsuccess_refine = initializer_feature->singleGaussnewton(*it1, clones_cam);\n\t\t}\n\n\t\tif (!success_tri || !success_refine)\n\t\t{\n\t\t\t(*it1)->to_delete = true;\n\t\t\tit1 = feature_vec.erase(it1);\n\t\t\tcontinue;\n\t\t}\n\t\tit1++;\n\t}\n\trT2 = boost::posix_time::microsec_clock::local_time();\n\n\tauto it2 = feature_vec.begin();\n\twhile (it2 != feature_vec.end())\n\t{\t\n\t\tupdaterHelper::updaterHelperFeature feat;\n\t\tfeat.feature_id = (*it2)->feature_id;\n\t\tfeat.uvs = (*it2)->uvs;\n\t\tfeat.uvs_norm = (*it2)->uvs_norm;\n\t\tfeat.timestamps = (*it2)->timestamps;\n\n\t\tfeat.frames = (*it2)->frames;\n\t\tfeat.color = (*it2)->color;\n\t\tassert(feat.color[0].size() == feat.uvs[0].size());\n\t\tassert(feat.color[1].size() == feat.uvs[1].size());\n\t\tassert(feat.color[(*it2)->anchor_cam_id].back() >= 0);\n\n\t\tfeat.anchor_cam_id = (*it2)->anchor_cam_id;\n\t\tfeat.anchor_clone_timestamp = (*it2)->anchor_clone_timestamp;\n\n\t\tfeat.position_global = (*it2)->position_global;\n\t\tfeat.position_global_fej = (*it2)->position_global;\n\n\t\tEigen::MatrixXd H_f;\n\t\tEigen::MatrixXd H_x;\n\t\tEigen::VectorXd res;\n\t\tstd::vector> Hx_order;\n\n\t\tupdaterHelper::getFeatureJacobianFull(state_ptr, feat, H_f, H_x, res, Hx_order);\n\n\t\tauto map_point_ptr = std::make_shared();\n\t\tmap_point_ptr->feature_id = feat.feature_id;\n\t\tmap_point_ptr->unique_camera_id = (*it2)->anchor_cam_id;\n\n\t\tmap_point_ptr->color = feat.color[feat.anchor_cam_id].back();\n\t\tmap_point_ptr->host_frame = feat.frames[(*it2)->anchor_cam_id].back();\n\t\tmap_point_ptr->anchor_cam_id = feat.anchor_cam_id;\n\t\tmap_point_ptr->anchor_clone_timestamp = feat.anchor_clone_timestamp;\n\n\t\tmap_point_ptr->setPointXYZ(feat.position_global, false);\n\t\tmap_point_ptr->setPointXYZ(feat.position_global_fej, true);\n\n\t\tdouble sigma_pix_sq = options_slam.sigma_pix_sq;\n\t\tEigen::MatrixXd R = sigma_pix_sq * Eigen::MatrixXd::Identity(res.rows(), res.rows());\n\n\t\tdouble chi2_multipler = options_slam.chi2_multipler;\n\n\t\tif (stateHelper::initialize(state_ptr, map_point_ptr, Hx_order, H_x, H_f, R, res, chi2_multipler))\n\t\t{\n\t\t\tbool add = mapManagement::addPointToVoxel(voxel_map, map_point_ptr, state_ptr->options.voxel_size, state_ptr->options.max_num_points_in_voxel, \n\t\t\t\tstate_ptr->options.min_distance_points, voxels_history);\n\n\t\t\tif (add)\n\t\t\t{\n\t\t\t\tstate_ptr->map_points.insert({(*it2)->feature_id, map_point_ptr});\n\t\t\t\t(*it2)->to_delete = true;\n\t\t\t\tit2++;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t(*it2)->to_delete = true;\n\t\t\t\tit2 = feature_vec.erase(it2);\n\t\t\t}\t\n\t\t}\n\t\telse\n\t\t{\n\t\t\t(*it2)->to_delete = true;\n\t\t\tit2 = feature_vec.erase(it2);\n\t\t}\n\t}\n\trT3 = boost::posix_time::microsec_clock::local_time();\n\n\t// Time test\n\t/*\n\tif (!feature_vec.empty())\n\t{\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT1 - rT0).total_microseconds() * 1e-6 << \" seconds to clean.\" << std::endl;\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds to triangulate.\" << std::endl;\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds initialize (\" << (int)feature_vec.size() << \" features).\" << std::endl;\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT3 - rT1).total_microseconds() * 1e-6 << \" seconds total.\" << std::endl;\n\t}\n\t*/\n\t// Time test\n}\n protected:\n updaterOptions options_slam;\n std::shared_ptr initializer_feature;\n std::shared_ptr database;\n std::map chi_squared_table;\n};"], ["/voxel_svio/include/featureTracker.h", "class feature {\n public:\n trackKLT(std::unordered_map> camera_calib_, int num_features_, \n\t\tHistogramMethod histogram_method_, int fast_threshold_, int patch_size_x_, int patch_size_y_, int min_px_dist_) {\n current_id = 1;\n\n pixelSelector_ptr = std::make_shared(wG[0], hG[0]);\n}\n void feedNewImage(const cameraData &image_measurements, std::shared_ptr fh) {\n if (image_measurements.camera_ids.empty() || image_measurements.camera_ids.size() != image_measurements.images.size() \n || image_measurements.images.size() != image_measurements.masks.size())\n {\n std::cout << \"[ERROR]: image_measurements data sizes do not match or empty.\" << std::endl;\n std::cout << \"[ERROR]: - image_measurements.camera_ids.size() = \" << image_measurements.camera_ids.size() << std::endl;\n std::cout << \"[ERROR]: - image_measurements.images.size() = \" << image_measurements.images.size() << std::endl;\n std::cout << \"[ERROR]: - image_measurements.masks.size() = \" << image_measurements.masks.size() << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n rT1 = boost::posix_time::microsec_clock::local_time();\n size_t num_images = image_measurements.images.size();\n\n for (size_t image_id = 0; image_id < num_images; image_id++)\n {\n size_t cam_id = image_measurements.camera_ids.at(image_id);\n\n cv::Mat image;\n\n if (histogram_method == HistogramMethod::HISTOGRAM)\n {\n cv::equalizeHist(image_measurements.images.at(image_id), image);\n }\n else if (histogram_method == HistogramMethod::CLAHE)\n {\n double eq_clip_limit = 10.0;\n cv::Size eq_win_size = cv::Size(8, 8);\n cv::Ptr clahe = cv::createCLAHE(eq_clip_limit, eq_win_size);\n clahe->apply(image_measurements.images.at(image_id), image);\n }\n else\n {\n image = image_measurements.images.at(image_id);\n }\n\n std::vector img_pyr;\n cv::buildOpticalFlowPyramid(image, img_pyr, win_size, pyr_levels);\n\n img_curr[cam_id] = image;\n img_pyramid_curr[cam_id] = img_pyr;\n }\n\n feedStereo(image_measurements, fh, 0, 1);\n}\n std::shared_ptr getFeatureDatabase() {\n return database;\n}\n std::unordered_map> getLastObs() {\n return pts_last;\n}\n std::unordered_map> getLastIds() {\n return ids_last;\n}\n void setNumFeatures(int num_features_) {\n num_features = num_features_;\n}\n void displayActive(cv::Mat &img_out, int r1, int g1, int b1, int r2, int g2, int b2, std::string overlay = \"\") {\n std::map img_last_cache, img_mask_last_cache;\n std::unordered_map> pts_last_cache;\n {\n img_last_cache = img_last;\n img_mask_last_cache = img_mask_last;\n pts_last_cache = pts_last;\n }\n\n int max_width = -1;\n int max_height = -1;\n\n for (auto const &pair : img_last_cache)\n {\n if (max_width < pair.second.cols)\n max_width = pair.second.cols;\n if (max_height < pair.second.rows)\n max_height = pair.second.rows;\n }\n\n if (img_last_cache.empty() || max_width == -1 || max_height == -1)\n return;\n\n bool is_small = (std::min(max_width, max_height) < 400);\n\n bool image_new = ((int)img_last_cache.size() * max_width != img_out.cols || max_height != img_out.rows);\n\n if (image_new)\n img_out = cv::Mat(max_height, (int)img_last_cache.size() * max_width, CV_8UC3, cv::Scalar(0, 0, 0));\n\n int index_cam = 0;\n for (auto const &pair : img_last_cache)\n {\n cv::Mat img_temp;\n\n if (image_new)\n cv::cvtColor(img_last_cache[pair.first], img_temp, cv::COLOR_GRAY2RGB);\n else\n img_temp = img_out(cv::Rect(max_width * index_cam, 0, max_width, max_height));\n\n for (size_t i = 0; i < pts_last_cache[pair.first].size(); i++)\n {\n cv::Point2f pt_l = pts_last_cache[pair.first].at(i).pt;\n cv::circle(img_temp, pt_l, (is_small) ? 1 : 2, cv::Scalar(r1, g1, b1), cv::FILLED);\n\n cv::Point2f pt_l_top = cv::Point2f(pt_l.x - 3, pt_l.y - 3);\n cv::Point2f pt_l_bot = cv::Point2f(pt_l.x + 3, pt_l.y + 3);\n cv::rectangle(img_temp, pt_l_top, pt_l_bot, cv::Scalar(r2, g2, b2), 1);\n }\n\n auto txtpt = (is_small) ? cv::Point(10, 30) : cv::Point(30, 60);\n \n if (overlay == \"\")\n {\n cv::putText(img_temp, \"CAM:\" + std::to_string((int)pair.first), txtpt, cv::FONT_HERSHEY_COMPLEX_SMALL, (is_small) ? 1.5 : 3.0, cv::Scalar(0, 255, 0), 3);\n }\n else\n {\n cv::putText(img_temp, overlay, txtpt, cv::FONT_HERSHEY_COMPLEX_SMALL, (is_small) ? 1.5 : 3.0, cv::Scalar(0, 0, 255), 3);\n }\n\n cv::Mat mask = cv::Mat::zeros(img_mask_last_cache[pair.first].rows, img_mask_last_cache[pair.first].cols, CV_8UC3);\n mask.setTo(cv::Scalar(0, 0, 255), img_mask_last_cache[pair.first]);\n cv::addWeighted(mask, 0.1, img_temp, 1.0, 0.0, img_temp);\n\n img_temp.copyTo(img_out(cv::Rect(max_width * index_cam, 0, img_last_cache[pair.first].cols, img_last_cache[pair.first].rows)));\n index_cam++;\n }\n}\n private:\n void feedStereo(const cameraData &image_measurements, std::shared_ptr fh, size_t image_id_left, size_t image_id_right) {\n size_t cam_id_left = image_measurements.camera_ids.at(image_id_left);\n size_t cam_id_right = image_measurements.camera_ids.at(image_id_right);\n\n cv::Mat img_left = img_curr.at(cam_id_left);\n cv::Mat img_right = img_curr.at(cam_id_right);\n std::vector img_pyr_left = img_pyramid_curr.at(cam_id_left);\n std::vector img_pyr_right = img_pyramid_curr.at(cam_id_right);\n cv::Mat mask_left = image_measurements.masks.at(image_id_left);\n cv::Mat mask_right = image_measurements.masks.at(image_id_right);\n rT2 = boost::posix_time::microsec_clock::local_time();\n\n if (pts_last[cam_id_left].empty() && pts_last[cam_id_right].empty())\n {\n std::vector good_left, good_right;\n std::vector good_ids_left, good_ids_right;\n performDetectionStereo(fh, img_pyr_left, img_pyr_right, mask_left, mask_right, cam_id_left, cam_id_right, good_left, good_right, good_ids_left, good_ids_right);\n \n img_last[cam_id_left] = img_left;\n img_last[cam_id_right] = img_right;\n img_pyramid_last[cam_id_left] = img_pyr_left;\n img_pyramid_last[cam_id_right] = img_pyr_right;\n img_mask_last[cam_id_left] = mask_left;\n img_mask_last[cam_id_right] = mask_right;\n pts_last[cam_id_left] = good_left;\n pts_last[cam_id_right] = good_right;\n ids_last[cam_id_left] = good_ids_left;\n ids_last[cam_id_right] = good_ids_right;\n \n return;\n }\n\n int pts_before_detect = (int)pts_last[cam_id_left].size();\n auto pts_left_old = pts_last[cam_id_left];\n auto pts_right_old = pts_last[cam_id_right];\n auto ids_left_old = ids_last[cam_id_left];\n auto ids_right_old = ids_last[cam_id_right];\n performDetectionStereo(fh, img_pyramid_last[cam_id_left], img_pyramid_last[cam_id_right], img_mask_last[cam_id_left],\n img_mask_last[cam_id_right], cam_id_left, cam_id_right, pts_left_old, pts_right_old, ids_left_old, ids_right_old);\n\n rT3 = boost::posix_time::microsec_clock::local_time();\n\n std::vector mask_ll, mask_rr;\n std::vector pts_left_new = pts_left_old;\n std::vector pts_right_new = pts_right_old;\n\n parallel_for_(cv::Range(0, 2), lambdaBody([&](const cv::Range &range) {\n for (int i = range.start; i < range.end; i++) {\n bool is_left = (i == 0);\n performMatching(img_pyramid_last[is_left ? cam_id_left : cam_id_right], is_left ? img_pyr_left : img_pyr_right,\n is_left ? pts_left_old : pts_right_old, is_left ? pts_left_new : pts_right_new,\n is_left ? cam_id_left : cam_id_right, is_left ? cam_id_left : cam_id_right,\n is_left ? mask_ll : mask_rr);\n }\n }));\n rT4 = boost::posix_time::microsec_clock::local_time();\n\n\n rT5 = boost::posix_time::microsec_clock::local_time();\n\n if (mask_ll.empty() && mask_rr.empty())\n {\n img_last[cam_id_left] = img_left;\n img_last[cam_id_right] = img_right;\n img_pyramid_last[cam_id_left] = img_pyr_left;\n img_pyramid_last[cam_id_right] = img_pyr_right;\n img_mask_last[cam_id_left] = mask_left;\n img_mask_last[cam_id_right] = mask_right;\n pts_last[cam_id_left].clear();\n pts_last[cam_id_right].clear();\n ids_last[cam_id_left].clear();\n ids_last[cam_id_right].clear();\n std::cout << \"[trackKLT::feedStereo]: Not enough points for RANSAC, resetting.\" << std::endl;\n \n return;\n }\n \n std::vector good_left, good_right;\n std::vector good_ids_left, good_ids_right;\n\n for (size_t i = 0; i < pts_left_new.size(); i++)\n {\n if (pts_left_new.at(i).pt.x < 0 || pts_left_new.at(i).pt.y < 0 || (int)pts_left_new.at(i).pt.x > img_left.cols ||\n (int)pts_left_new.at(i).pt.y > img_left.rows)\n continue;\n \n bool found_right = false;\n size_t index_right = 0;\n for (size_t n = 0; n < ids_right_old.size(); n++)\n {\n if (ids_left_old.at(i) == ids_right_old.at(n))\n {\n found_right = true;\n index_right = n;\n break;\n }\n }\n \n if (mask_ll[i] && found_right && mask_rr[index_right])\n {\n if (pts_right_new.at(index_right).pt.x < 0 || pts_right_new.at(index_right).pt.y < 0 || \n (int)pts_right_new.at(index_right).pt.x >= img_right.cols || (int)pts_right_new.at(index_right).pt.y >= img_right.rows)\n continue;\n\n good_left.push_back(pts_left_new.at(i));\n good_right.push_back(pts_right_new.at(index_right));\n good_ids_left.push_back(ids_left_old.at(i));\n good_ids_right.push_back(ids_right_old.at(index_right));\n }\n else if (mask_ll[i])\n {\n good_left.push_back(pts_left_new.at(i));\n good_ids_left.push_back(ids_left_old.at(i));\n }\n }\n\n for (size_t i = 0; i < pts_right_new.size(); i++)\n {\n if (pts_right_new.at(i).pt.x < 0 || pts_right_new.at(i).pt.y < 0 || (int)pts_right_new.at(i).pt.x >= img_right.cols ||\n (int)pts_right_new.at(i).pt.y >= img_right.rows)\n continue;\n \n bool added_already = (std::find(good_ids_right.begin(), good_ids_right.end(), ids_right_old.at(i)) != good_ids_right.end());\n\n if (mask_rr[i] && !added_already)\n {\n good_right.push_back(pts_right_new.at(i));\n good_ids_right.push_back(ids_right_old.at(i));\n }\n }\n\n for (size_t i = 0; i < good_left.size(); i++)\n {\n cv::Point2f npt_l = camera_calib.at(cam_id_left)->undistortCV(good_left.at(i).pt);\n database->updateFeature(fh, good_ids_left.at(i), image_measurements.timestamp, cam_id_left, good_left.at(i).pt.x, good_left.at(i).pt.y, npt_l.x, npt_l.y);\n }\n for (size_t i = 0; i < good_right.size(); i++)\n {\n cv::Point2f npt_r = camera_calib.at(cam_id_right)->undistortCV(good_right.at(i).pt);\n database->updateFeature(fh, good_ids_right.at(i), image_measurements.timestamp, cam_id_right, good_right.at(i).pt.x, good_right.at(i).pt.y, npt_r.x, npt_r.y);\n }\n\n {\n img_last[cam_id_left] = img_left;\n img_last[cam_id_right] = img_right;\n img_pyramid_last[cam_id_left] = img_pyr_left;\n img_pyramid_last[cam_id_right] = img_pyr_right;\n img_mask_last[cam_id_left] = mask_left;\n img_mask_last[cam_id_right] = mask_right;\n pts_last[cam_id_left] = good_left;\n pts_last[cam_id_right] = good_right;\n ids_last[cam_id_left] = good_ids_left;\n ids_last[cam_id_right] = good_ids_right;\n }\n rT6 = boost::posix_time::microsec_clock::local_time();\n\n // Time test\n /*\n std::cout << std::fixed << \"[trackKLT]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds for pyramid.\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds for detection (\" << (int)pts_last[cam_id_left].size() - pts_before_detect << \" detected).\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" seconds for temporal klt.\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" seconds for stereo klt.\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" seconds for feature DB update (\" << (int)good_left.size() << \" features).\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT6 - rT1).total_microseconds() * 1e-6 << \" seconds for total.\" << std::endl;\n */\n // Time test\n}\n void performDetectionStereo(std::shared_ptr fh, const std::vector &img_0_pyr, const std::vector &img_1_pyr, const cv::Mat &mask_0, const cv::Mat &mask_1, \n\t\tsize_t cam_id_left, size_t cam_id_right, std::vector &pts_0, std::vector &pts_1, std::vector &ids_0, std::vector &ids_1) {\n cv::Size size_close_0((int)((float)img_0_pyr.at(0).cols / (float)min_px_dist),\n (int)((float)img_0_pyr.at(0).rows / (float)min_px_dist));\n cv::Mat grid_2d_close_0 = cv::Mat::zeros(size_close_0, CV_8UC1);\n float size_x_0 = (float)img_0_pyr.at(0).cols / (float)patch_size_x;\n float size_y_0 = (float)img_0_pyr.at(0).rows / (float)patch_size_y;\n\n cv::Size size_grid_0(patch_size_x, patch_size_y);\n cv::Mat grid_2d_grid_0 = cv::Mat::zeros(size_grid_0, CV_8UC1);\n cv::Mat mask_0_updated = mask_0.clone();\n\n auto it_0 = pts_0.begin();\n auto it_1 = ids_0.begin();\n\n while (it_0 != pts_0.end())\n {\n cv::KeyPoint kpt = *it_0;\n int x = (int)kpt.pt.x;\n int y = (int)kpt.pt.y;\n\n int edge = 10;\n if (x < edge || x >= img_0_pyr.at(0).cols - edge || y < edge || y >= img_0_pyr.at(0).rows - edge)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n\n int x_close = (int)(kpt.pt.x / (float)min_px_dist);\n int y_close = (int)(kpt.pt.y / (float)min_px_dist);\n if (x_close < 0 || x_close >= size_close_0.width || y_close < 0 || y_close >= size_close_0.height)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n\n int x_grid = std::floor(kpt.pt.x / size_x_0);\n int y_grid = std::floor(kpt.pt.y / size_y_0);\n\n if (x_grid < 0 || x_grid >= size_grid_0.width || y_grid < 0 || y_grid >= size_grid_0.height)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n\n if (grid_2d_close_0.at(y_close, x_close) > 127)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n \n if (mask_0.at(y, x) > 127)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n \n grid_2d_close_0.at(y_close, x_close) = 255;\n if (grid_2d_grid_0.at(y_grid, x_grid) < 255)\n {\n grid_2d_grid_0.at(y_grid, x_grid) += 1;\n }\n \n if (x - min_px_dist >= 0 && x + min_px_dist < img_0_pyr.at(0).cols && y - min_px_dist >= 0 && y + min_px_dist < img_0_pyr.at(0).rows)\n {\n cv::Point pt_1(x - min_px_dist, y - min_px_dist);\n cv::Point pt_2(x + min_px_dist, y + min_px_dist);\n cv::rectangle(mask_0_updated, pt_1, pt_2, cv::Scalar(255), -1);\n\n int begin_idx = x - min_px_dist + wG[0] * (y - min_px_dist);\n int end_idx = x + min_px_dist + wG[0] * (y + min_px_dist);\n\n for(int i = begin_idx; i <= end_idx; i++) fh->mask_left[i] = true;\n }\n\n it_0++;\n it_1++;\n }\n\n double min_feat_percent = 0.50;\n int num_featsneeded_0 = num_features - (int)pts_0.size();\n\n if (num_featsneeded_0 > std::min(20, (int)(min_feat_percent * num_features)))\n {\n pixelSelector_ptr->pixelSelectionLeft(fh, num_featsneeded_0);\n\n std::vector kpts_0_new;\n std::vector pts_0_new;\n\n for (int y = 0; y < hG[0]; y++)\n {\n for (int x = 0; x < wG[0]; x++)\n {\n int idx = x + y * wG[0];\n\n if (fh->selected_pixels_left[idx] == 0) continue;\n\n cv::KeyPoint kpt;\n kpt.pt.x = x;\n kpt.pt.y = y;\n\n kpts_0_new.push_back(kpt);\n pts_0_new.push_back(kpt.pt);\n }\n }\n\n std::vector kpts_1_new;\n std::vector pts_1_new;\n kpts_1_new = kpts_0_new;\n pts_1_new = pts_0_new;\n\n if (!pts_0_new.empty())\n {\n std::vector mask;\n std::vector error;\n cv::TermCriteria term_crit = cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 30, 0.01);\n cv::calcOpticalFlowPyrLK(img_0_pyr, img_1_pyr, pts_0_new, pts_1_new, mask, error, win_size, pyr_levels, term_crit, cv::OPTFLOW_USE_INITIAL_FLOW);\n\n for (size_t i = 0; i < pts_0_new.size(); i++)\n {\n bool oob_left = ((int)pts_0_new.at(i).x < 0 || (int)pts_0_new.at(i).x >= img_0_pyr.at(0).cols || (int)pts_0_new.at(i).y < 0 ||\n (int)pts_0_new.at(i).y >= img_0_pyr.at(0).rows);\n bool oob_right = ((int)pts_1_new.at(i).x < 0 || (int)pts_1_new.at(i).x >= img_1_pyr.at(0).cols || (int)pts_1_new.at(i).y < 0 ||\n (int)pts_1_new.at(i).y >= img_1_pyr.at(0).rows);\n\n if (!oob_left && !oob_right && mask[i] == 1)\n {\n kpts_0_new.at(i).pt = pts_0_new.at(i);\n kpts_1_new.at(i).pt = pts_1_new.at(i);\n \n pts_0.push_back(kpts_0_new.at(i));\n pts_1.push_back(kpts_1_new.at(i));\n \n size_t temp = ++current_id;\n ids_0.push_back(temp);\n ids_1.push_back(temp);\n }\n else if (!oob_left)\n {\n kpts_0_new.at(i).pt = pts_0_new.at(i);\n pts_0.push_back(kpts_0_new.at(i));\n \n size_t temp = ++current_id;\n ids_0.push_back(temp);\n }\n }\n }\n }\n\n cv::Size size_close_1((int)((float)img_1_pyr.at(0).cols / (float)min_px_dist), (int)((float)img_1_pyr.at(0).rows / (float)min_px_dist));\n cv::Mat grid_2d_close_1 = cv::Mat::zeros(size_close_1, CV_8UC1);\n float size_x_1 = (float)img_1_pyr.at(0).cols / (float)patch_size_x;\n float size_y_1 = (float)img_1_pyr.at(0).rows / (float)patch_size_y;\n \n cv::Size size_grid_1(patch_size_x, patch_size_y);\n cv::Mat grid_2d_grid_1 = cv::Mat::zeros(size_grid_1, CV_8UC1);\n cv::Mat mask_1_updated = mask_0.clone();\n\n it_0 = pts_1.begin();\n it_1 = ids_1.begin();\n\n while (it_0 != pts_1.end())\n {\n cv::KeyPoint kpt = *it_0;\n int x = (int)kpt.pt.x;\n int y = (int)kpt.pt.y;\n int edge = 10;\n if (x < edge || x >= img_1_pyr.at(0).cols - edge || y < edge || y >= img_1_pyr.at(0).rows - edge)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n \n int x_close = (int)(kpt.pt.x / (float)min_px_dist);\n int y_close = (int)(kpt.pt.y / (float)min_px_dist);\n if (x_close < 0 || x_close >= size_close_1.width || y_close < 0 || y_close >= size_close_1.height)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n\n int x_grid = std::floor(kpt.pt.x / size_x_1);\n int y_grid = std::floor(kpt.pt.y / size_y_1);\n if (x_grid < 0 || x_grid >= size_grid_1.width || y_grid < 0 || y_grid >= size_grid_1.height)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n \n bool is_stereo = (std::find(ids_0.begin(), ids_0.end(), *it_1) != ids_0.end());\n\n if (grid_2d_close_1.at(y_close, x_close) > 127 && !is_stereo)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n\n if (mask_1.at(y, x) > 127)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n\n grid_2d_close_1.at(y_close, x_close) = 255;\n\n if (grid_2d_grid_1.at(y_grid, x_grid) < 255)\n {\n grid_2d_grid_1.at(y_grid, x_grid) += 1;\n }\n\n if (x - min_px_dist >= 0 && x + min_px_dist < img_1_pyr.at(0).cols && y - min_px_dist >= 0 && y + min_px_dist < img_1_pyr.at(0).rows)\n {\n cv::Point pt_1(x - min_px_dist, y - min_px_dist);\n cv::Point pt_2(x + min_px_dist, y + min_px_dist);\n cv::rectangle(mask_1_updated, pt_1, pt_2, cv::Scalar(255), -1);\n\n int begin_idx = x - min_px_dist + wG[0] * (y - min_px_dist);\n int end_idx = x + min_px_dist + wG[0] * (y + min_px_dist);\n\n for(int i = begin_idx; i <= end_idx; i++) fh->mask_right[i] = true;\n }\n\n it_0++;\n it_1++;\n }\n\n int num_featsneeded_1 = num_features - (int)pts_1.size();\n if (num_featsneeded_1 > std::min(20, (int)(min_feat_percent * num_features)))\n {\n pixelSelector_ptr->pixelSelectionRight(fh, num_featsneeded_1);\n\n for (int y = 0; y < hG[0]; y++)\n {\n for (int x = 0; x < wG[0]; x++)\n {\n int idx = x + y * wG[0];\n\n if (fh->selected_pixels_right[idx] == 0) continue;\n\n cv::KeyPoint kpt;\n kpt.pt.x = x;\n kpt.pt.y = y;\n\n pts_1.push_back(kpt);\n size_t temp = ++current_id;\n ids_1.push_back(temp);\n }\n }\n }\n}\n void performMatching(const std::vector &img_0_pyr, const std::vector &img_1_pyr, std::vector &pts_0, \n\t\tstd::vector &pts_1, size_t id_0, size_t id_1, std::vector &mask_out) {\n assert(kpts_0.size() == kpts_1.size());\n\n if (kpts_0.empty() || kpts_1.empty())\n return;\n\n std::vector pts_0, pts_1;\n for (size_t i = 0; i < kpts_0.size(); i++)\n {\n pts_0.push_back(kpts_0.at(i).pt);\n pts_1.push_back(kpts_1.at(i).pt);\n }\n\n if (pts_0.size() < 10)\n {\n for (size_t i = 0; i < pts_0.size(); i++)\n mask_out.push_back((uchar)0);\n\n return;\n }\n\n std::vector mask_klt;\n std::vector error;\n cv::TermCriteria term_crit = cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 30, 0.01);\n cv::calcOpticalFlowPyrLK(img_0_pyr, img_1_pyr, pts_0, pts_1, mask_klt, error, win_size, pyr_levels, term_crit, cv::OPTFLOW_USE_INITIAL_FLOW);\n\n std::vector pts_0_n, pts_1_n;\n for (size_t i = 0; i < pts_0.size(); i++)\n {\n pts_0_n.push_back(camera_calib.at(id_0)->undistortCV(pts_0.at(i)));\n pts_1_n.push_back(camera_calib.at(id_1)->undistortCV(pts_1.at(i)));\n }\n\n std::vector mask_rsc;\n double max_focallength_img_0 = std::max(camera_calib.at(id_0)->getK()(0, 0), camera_calib.at(id_0)->getK()(1, 1));\n double max_focallength_img_1 = std::max(camera_calib.at(id_1)->getK()(0, 0), camera_calib.at(id_1)->getK()(1, 1));\n double max_focallength = std::max(max_focallength_img_0, max_focallength_img_1);\n cv::findFundamentalMat(pts_0_n, pts_1_n, cv::FM_RANSAC, 2.0 / max_focallength, 0.999, mask_rsc);\n\n for (size_t i = 0; i < mask_klt.size(); i++)\n {\n auto mask = (uchar)((i < mask_klt.size() && mask_klt[i] && i < mask_rsc.size() && mask_rsc[i]) ? 1 : 0);\n mask_out.push_back(mask);\n }\n\n for (size_t i = 0; i < pts_0.size(); i++)\n {\n kpts_0.at(i).pt = pts_0.at(i);\n kpts_1.at(i).pt = pts_1.at(i);\n }\n}\n std::unordered_map> camera_calib;\n std::shared_ptr database;\n int num_features;\n HistogramMethod histogram_method;\n std::map img_last;\n std::map img_mask_last;\n std::unordered_map> pts_last;\n std::unordered_map> ids_last;\n std::atomic current_id;\n std::shared_ptr pixelSelector_ptr;\n int threshold;\n int patch_size_x;\n int patch_size_y;\n int min_px_dist;\n int pyr_levels = 5;\n cv::Size win_size = cv::Size(15, 15);\n std::map> img_pyramid_last;\n std::map img_curr;\n std::map> img_pyramid_curr;\n boost::posix_time::ptime rT1, rT2, rT3, rT4, rT5, rT6, rT7;\n};"], ["/voxel_svio/include/stateHelper.h", "class baseType {\n private:\n stateHelper() {\n\n}\n public:\n static void ekfPropagation(std::shared_ptr state_ptr, const std::vector> &order_new, \n\t\tconst std::vector> &order_old, const Eigen::MatrixXd &phi, const Eigen::MatrixXd &Q) {\n\tif (order_new.empty() || order_old.empty())\n\t{\n\t\tstd::cout << \"[stateHelper::ekfPropagation]: Called with empty variable arrays!\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tint size_order_new = order_new.at(0)->getSize();\n\tfor (size_t i = 0; i < order_new.size() - 1; i++)\n\t{\n\t\tif (order_new.at(i)->getId() + order_new.at(i)->getSize() != order_new.at(i + 1)->getId())\n\t\t{\n\t\t\tstd::cout << \"[stateHelper::ekfPropagation]: Called with non-contiguous state elements!\" << std::endl;\n\t\t\tstd::cout << \"[stateHelper::ekfPropagation]: This code only support a state transition which is in the same order as the state.\" << std::endl;\n\t\t\tstd::exit(EXIT_FAILURE);\n\t\t}\n\t\tsize_order_new += order_new.at(i + 1)->getSize();\n\t}\n\n\tint size_order_old = order_old.at(0)->getSize();\n\tfor (size_t i = 0; i < order_old.size() - 1; i++)\n\t{\n\t\tsize_order_old += order_old.at(i + 1)->getSize();\n\t}\n\n\tassert(size_order_new == phi.rows());\n\tassert(size_order_old == phi.cols());\n\tassert(size_order_new == Q.cols());\n\tassert(size_order_new == Q.rows());\n\n\tint current_it = 0;\n\tstd::vector phi_id;\n\tfor (const auto &var : order_old)\n\t{\n\t\tphi_id.push_back(current_it);\n\t\tcurrent_it += var->getSize();\n\t}\n\n\tEigen::MatrixXd cov_phiT = Eigen::MatrixXd::Zero(state_ptr->cov.rows(), phi.rows());\n\tfor (size_t i = 0; i < order_old.size(); i++)\n\t{\n\t\tstd::shared_ptr var = order_old.at(i);\n\t\tcov_phiT.noalias() += state_ptr->cov.block(0, var->getId(), state_ptr->cov.rows(), var->getSize()) * phi.block(0, phi_id[i], phi.rows(), var->getSize()).transpose();\n\t}\n\n\tEigen::MatrixXd phi_cov_phiT = Q.selfadjointView();\n\tfor (size_t i = 0; i < order_old.size(); i++)\n\t{\n\t\tstd::shared_ptr var = order_old.at(i);\n\t\tphi_cov_phiT.noalias() += phi.block(0, phi_id[i], phi.rows(), var->getSize()) * cov_phiT.block(var->getId(), 0, var->getSize(), phi.rows());\n\t}\n\n\tint start_id = order_new.at(0)->getId();\n\tint phi_size = phi.rows();\n\tint total_size = state_ptr->cov.rows();\n\tstate_ptr->cov.block(start_id, 0, phi_size, total_size) = cov_phiT.transpose();\n\tstate_ptr->cov.block(0, start_id, total_size, phi_size) = cov_phiT;\n\tstate_ptr->cov.block(start_id, start_id, phi_size, phi_size) = phi_cov_phiT;\n\n\tEigen::VectorXd diags = state_ptr->cov.diagonal();\n\tbool found_neg = false;\n\tfor (int i = 0; i < diags.rows(); i++)\n\t{\n\t\tif (diags(i) < 0.0)\n\t\t{\n\t\t\tstd::cout << \"[stateHelper::ekfPropagation]: Diagonal at \" << i << \" is \" << diags(i) << std::endl;\n\t\t\tfound_neg = true;\n\t\t}\n\t}\n\n\tif (found_neg)\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n}\n static void ekfUpdate(std::shared_ptr state_ptr, const std::vector> &H_order, \n\t\tconst Eigen::MatrixXd &H, const Eigen::VectorXd &res, const Eigen::MatrixXd &R) {\n\tassert(res.rows() == R.rows());\n\tassert(H.rows() == res.rows());\n\tEigen::MatrixXd M_a = Eigen::MatrixXd::Zero(state_ptr->cov.rows(), res.rows());\n\n\tint current_it = 0;\n\tstd::vector H_id;\n\tfor (const auto &meas_var : H_order)\n\t{\n\t\tH_id.push_back(current_it);\n\t\tcurrent_it += meas_var->getSize();\n\t}\n\n\tfor (const auto &var : state_ptr->variables)\n\t{\n\t\t// Sum up effect of each subjacobian = K_i= \\sum_m (P_im Hm^T)\n\t\tEigen::MatrixXd M_i = Eigen::MatrixXd::Zero(var->getSize(), res.rows());\n\t\tfor (size_t i = 0; i < H_order.size(); i++)\n\t\t{\n\t\t\tstd::shared_ptr meas_var = H_order[i];\n\t\t\tM_i.noalias() += state_ptr->cov.block(var->getId(), meas_var->getId(), var->getSize(), meas_var->getSize()) * \n\t\t\t\tH.block(0, H_id[i], H.rows(), meas_var->getSize()).transpose();\n\t\t}\n\t\tM_a.block(var->getId(), 0, var->getSize(), res.rows()) = M_i;\n\t}\n\n\tEigen::MatrixXd P_small = stateHelper::getMarginalCovariance(state_ptr, H_order);\n\tEigen::MatrixXd S = H * P_small * H.transpose() + R;\n\tEigen::MatrixXd S_inv = Eigen::MatrixXd::Identity(R.rows(), R.rows());\n\tS.selfadjointView().llt().solveInPlace(S_inv);\n\tEigen::MatrixXd K = M_a * S_inv.selfadjointView();\n\n\tstate_ptr->cov.triangularView() -= K * M_a.transpose();\n\tstate_ptr->cov = state_ptr->cov.selfadjointView();\n\n\tEigen::VectorXd diags = state_ptr->cov.diagonal();\n\tbool found_neg = false;\n\n\tfor (int i = 0; i < diags.rows(); i++)\n\t{\n\t\tif (diags(i) < 0.0)\n\t\t{\n\t\t\tstd::cout << \"[stateHelper::ekfUpdate: Diagonal at \" << i << \" is \" << diags(i) << std::endl;\n\t\t\tfound_neg = true;\n\t\t}\n\t}\n\n\tif (found_neg)\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tEigen::VectorXd dx = K * res;\n\tfor (size_t i = 0; i < state_ptr->variables.size(); i++)\n\t{\n\t\tstate_ptr->variables.at(i)->update(dx.block(state_ptr->variables.at(i)->getId(), 0, state_ptr->variables.at(i)->getSize(), 1));\n\t}\n\n\tif (state_ptr->options.do_calib_camera_intrinsics)\n\t{\n\t\tfor (auto const &calib : state_ptr->cam_intrinsics)\n\t\t{\n\t\t\tstate_ptr->cam_intrinsics_cameras.at(calib.first)->setValue(calib.second->value());\n\t\t}\n\t}\n}\n static void setInitialCovariance(std::shared_ptr state_ptr, const Eigen::MatrixXd &covariance, \n\t\tconst std::vector> &order) {\n\tint i_index = 0;\n\n\tfor (size_t i = 0; i < order.size(); i++)\n\t{\n\t\tint k_index = 0;\n\t\tfor (size_t k = 0; k < order.size(); k++)\n\t\t{\n\t\t\tstate_ptr->cov.block(order[i]->getId(), order[k]->getId(), order[i]->getSize(), order[k]->getSize()) = \n\t\t\t\tcovariance.block(i_index, k_index, order[i]->getSize(), order[k]->getSize());\n\t\t\tk_index += order[k]->getSize();\n\t\t}\n\t\ti_index += order[i]->getSize();\n\t}\n\tstate_ptr->cov = state_ptr->cov.selfadjointView();\n}\n static Eigen::MatrixXd getMarginalCovariance(std::shared_ptr state_ptr, const std::vector> &small_variables) {\n\tint cov_size = 0;\n\tfor (size_t i = 0; i < small_variables.size(); i++)\n\t{\n\t\tcov_size += small_variables[i]->getSize();\n\t}\n\n\tEigen::MatrixXd small_cov = Eigen::MatrixXd::Zero(cov_size, cov_size);\n\tint i_index = 0;\n\n\tfor (size_t i = 0; i < small_variables.size(); i++)\n\t{\n\t\tint k_index = 0;\n\n\t\tfor (size_t k = 0; k < small_variables.size(); k++)\n\t\t{\n\t\t\tsmall_cov.block(i_index, k_index, small_variables[i]->getSize(), small_variables[k]->getSize()) = \n\t\t\t\tstate_ptr->cov.block(small_variables[i]->getId(), small_variables[k]->getId(), small_variables[i]->getSize(), small_variables[k]->getSize());\n\t\t\tk_index += small_variables[k]->getSize();\n\t\t}\n\n\t\ti_index += small_variables[i]->getSize();\n\t}\n\n\treturn small_cov;\n}\n static Eigen::MatrixXd getFullCovariance(std::shared_ptr state_ptr) {\n\tint cov_size = (int)state_ptr->cov.rows();\n\n\tEigen::MatrixXd full_cov = Eigen::MatrixXd::Zero(cov_size, cov_size);\n\n\tfull_cov.block(0, 0, state_ptr->cov.rows(), state_ptr->cov.rows()) = state_ptr->cov;\n\n\treturn full_cov;\n}\n static void marginalize(std::shared_ptr state_ptr, std::shared_ptr marg) {\n\tif (std::find(state_ptr->variables.begin(), state_ptr->variables.end(), marg) == state_ptr->variables.end())\n\t{\n\t\tstd::cout << \"[stateHelper::marginalize]: Called on variable that is not in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::marginalize]: Marginalization, does NOT work on sub-variables yet...\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tint marg_size = marg->getSize();\n\tint marg_id = marg->getId();\n\tint x2_size = (int)state_ptr->cov.rows() - marg_id - marg_size;\n\n\tEigen::MatrixXd cov_new(state_ptr->cov.rows() - marg_size, state_ptr->cov.rows() - marg_size);\n\n\tcov_new.block(0, 0, marg_id, marg_id) = state_ptr->cov.block(0, 0, marg_id, marg_id);\n\tcov_new.block(0, marg_id, marg_id, x2_size) = state_ptr->cov.block(0, marg_id + marg_size, marg_id, x2_size);\n\tcov_new.block(marg_id, 0, x2_size, marg_id) = cov_new.block(0, marg_id, marg_id, x2_size).transpose();\n\tcov_new.block(marg_id, marg_id, x2_size, x2_size) = state_ptr->cov.block(marg_id + marg_size, marg_id + marg_size, x2_size, x2_size);\n\n\tstate_ptr->cov = cov_new;\n\tassert(state_ptr->cov.rows() == cov_new.rows());\n\n\tstd::vector> remaining_variables;\n\n\tfor (size_t i = 0; i < state_ptr->variables.size(); i++)\n\t{\n\t\tif (state_ptr->variables.at(i) != marg)\n\t\t{\n\t\t\tif (state_ptr->variables.at(i)->getId() > marg_id)\n\t\t\t{\n\t\t\t\tstate_ptr->variables.at(i)->setLocalId(state_ptr->variables.at(i)->getId() - marg_size);\n\t\t\t}\n\t\t\tremaining_variables.push_back(state_ptr->variables.at(i));\n\t\t}\n\t}\n\n\tmarg->setLocalId(-1);\n\tstate_ptr->variables = remaining_variables;\n}\n static std::shared_ptr clone(std::shared_ptr state_ptr, std::shared_ptr variable_to_clone) {\n\tint total_size = variable_to_clone->getSize();\n\tint old_size = (int)state_ptr->cov.rows();\n\tint new_loc = (int)state_ptr->cov.rows();\n\n\tstate_ptr->cov.conservativeResizeLike(Eigen::MatrixXd::Zero(old_size + total_size, old_size + total_size));\n\n\tconst std::vector> new_variables = state_ptr->variables;\n\tstd::shared_ptr new_clone = nullptr;\n\n\tfor (size_t k = 0; k < state_ptr->variables.size(); k++)\n\t{\n\t\tstd::shared_ptr type_check = state_ptr->variables.at(k)->checkIfSubvariable(variable_to_clone);\n\t\tif (state_ptr->variables.at(k) == variable_to_clone)\n\t\t{\n\t\t\ttype_check = state_ptr->variables.at(k);\n\t\t}\n\t\telse if (type_check != variable_to_clone)\n\t\t{\n\t\t\tcontinue;\n\t\t}\n\n\t\tint old_loc = type_check->getId();\n\n\t\tstate_ptr->cov.block(new_loc, new_loc, total_size, total_size) = state_ptr->cov.block(old_loc, old_loc, total_size, total_size);\n\t\tstate_ptr->cov.block(0, new_loc, old_size, total_size) = state_ptr->cov.block(0, old_loc, old_size, total_size);\n\t\tstate_ptr->cov.block(new_loc, 0, total_size, old_size) = state_ptr->cov.block(old_loc, 0, total_size, old_size);\n\n\t\tnew_clone = type_check->clone();\n\t\tnew_clone->setLocalId(new_loc);\n\t\tbreak;\n\t}\n\n\tif (new_clone == nullptr)\n\t{\n\t\tstd::cout << \"[stateHelper::clone]: Called on variable is not in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::clone]: Ensure that the variable specified is a variable, or sub-variable.\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tstate_ptr->variables.push_back(new_clone);\n\treturn new_clone;\n}\n static bool initialize(std::shared_ptr state_ptr, std::shared_ptr new_variable, \n\t\tconst std::vector> &H_order, Eigen::MatrixXd &H_R, Eigen::MatrixXd &H_L, \n\t\tEigen::MatrixXd &R, Eigen::VectorXd &res, double chi_2_mult) {\n\tif (std::find(state_ptr->variables.begin(), state_ptr->variables.end(), new_variable) != state_ptr->variables.end())\n\t{\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Called on variable that is already in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found this variable at \" << new_variable->getId() << \" in covariance.\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tassert(R.rows() == R.cols());\n\tassert(R.rows() > 0);\n\tfor (int r = 0; r < R.rows(); r++)\n\t{\n\t\tfor (int c = 0; c < R.cols(); c++)\n\t\t{\n\t\t\tif (r == c && R(0, 0) != R(r, c))\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Your noise is not isotropic!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Found a value of \" << R(r, c) << \" verses value of \" << R(0, 0) << \".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t\telse if (r != c && R(r, c) != 0.0)\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Your noise is not diagonal!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Found a value of \" << R(r, c) << \" at row \" << r << \" and column \" << c <<\".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t}\n\t}\n\n\tsize_t new_var_size = new_variable->getSize();\n\tassert((int)new_var_size == H_L.cols());\n\n\tEigen::JacobiRotation tempHo_GR;\n\tfor (int n = 0; n < H_L.cols(); ++n)\n\t{\n\t\tfor (int m = (int)H_L.rows() - 1; m > n; m--)\n\t\t{\n\t\t\ttempHo_GR.makeGivens(H_L(m - 1, n), H_L(m, n));\n\t\t\t(H_L.block(m - 1, n, 2, H_L.cols() - n)).applyOnTheLeft(0, 1, tempHo_GR.adjoint());\n\t\t\t(res.block(m - 1, 0, 2, 1)).applyOnTheLeft(0, 1, tempHo_GR.adjoint());\n\t\t\t(H_R.block(m - 1, 0, 2, H_R.cols())).applyOnTheLeft(0, 1, tempHo_GR.adjoint());\n\t\t}\n\t}\n\n\tEigen::MatrixXd Hxinit = H_R.block(0, 0, new_var_size, H_R.cols());\n\tEigen::MatrixXd H_finit = H_L.block(0, 0, new_var_size, new_var_size);\n\tEigen::VectorXd resinit = res.block(0, 0, new_var_size, 1);\n\tEigen::MatrixXd Rinit = R.block(0, 0, new_var_size, new_var_size);\n\n\tEigen::MatrixXd Hup = H_R.block(new_var_size, 0, H_R.rows() - new_var_size, H_R.cols());\n\tEigen::VectorXd resup = res.block(new_var_size, 0, res.rows() - new_var_size, 1);\n\tEigen::MatrixXd Rup = R.block(new_var_size, new_var_size, R.rows() - new_var_size, R.rows() - new_var_size);\n\n\tEigen::MatrixXd P_up = stateHelper::getMarginalCovariance(state_ptr, H_order);\n\tassert(Rup.rows() == Hup.rows());\n\tassert(Hup.cols() == P_up.cols());\n\tEigen::MatrixXd S = Hup * P_up * Hup.transpose() + Rup;\n\tdouble chi2 = resup.dot(S.llt().solve(resup));\n\n\tboost::math::chi_squared chi_squared_dist(res.rows());\n\tdouble chi2_check = boost::math::quantile(chi_squared_dist, 0.95);\n\tif (chi2 > chi_2_mult * chi2_check)\n\t{\n\t\treturn false;\n\t}\n\n\tstateHelper::initializeInvertible(state_ptr, new_variable, H_order, Hxinit, H_finit, Rinit, resinit);\n\n\tif (Hup.rows() > 0)\n\t{\n\t\tstateHelper::ekfUpdate(state_ptr, H_order, Hup, resup, Rup);\n\t}\n\treturn true;\n}\n static void initializeInvertible(std::shared_ptr state_ptr, std::shared_ptr new_variable, \n\t\tconst std::vector> &H_order, const Eigen::MatrixXd &H_R, \n\t\tconst Eigen::MatrixXd &H_L, const Eigen::MatrixXd &R, const Eigen::VectorXd &res) {\n\tif (std::find(state_ptr->variables.begin(), state_ptr->variables.end(), new_variable) != state_ptr->variables.end())\n\t{\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Called on variable that is already in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found this variable at \" << new_variable->getId() << \" in covariance.\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tassert(R.rows() == R.cols());\n\tassert(R.rows() > 0);\n\tfor (int r = 0; r < R.rows(); r++)\n\t{\n\t\tfor (int c = 0; c < R.cols(); c++)\n\t\t{\n\t\t\tif (r == c && R(0, 0) != R(r, c))\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Your noise is not isotropic!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found a value of \" << R(r, c) << \" verses value of \" << R(0, 0) << \".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t\telse if (r != c && R(r, c) != 0.0)\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Your noise is not diagonal!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found a value of \" << R(r, c) << \" at row \" << r << \" and column \" << c << \".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t}\n\t}\n\n\tassert(res.rows() == R.rows());\n\tassert(H_L.rows() == res.rows());\n\tassert(H_L.rows() == H_R.rows());\n\tEigen::MatrixXd M_a = Eigen::MatrixXd::Zero(state_ptr->cov.rows(), res.rows());\n\n\tint current_it = 0;\n\tstd::vector H_id;\n\tfor (const auto &meas_var : H_order) {\n\t\tH_id.push_back(current_it);\n\t\tcurrent_it += meas_var->getSize();\n\t}\n\n\tfor (const auto &var : state_ptr->variables)\n\t{\n\t\tEigen::MatrixXd M_i = Eigen::MatrixXd::Zero(var->getSize(), res.rows());\n\t\tfor (size_t i = 0; i < H_order.size(); i++)\n\t\t{\n\t\t\tstd::shared_ptr meas_var = H_order.at(i);\n\t\t\tM_i += state_ptr->cov.block(var->getId(), meas_var->getId(), var->getSize(), meas_var->getSize()) * \n\t\t\t\tH_R.block(0, H_id[i], H_R.rows(), meas_var->getSize()).transpose();\n\t\t}\n\t\tM_a.block(var->getId(), 0, var->getSize(), res.rows()) = M_i;\n\t}\n\n\tEigen::MatrixXd P_small = stateHelper::getMarginalCovariance(state_ptr, H_order);\n\n\tEigen::MatrixXd M(H_R.rows(), H_R.rows());\n\tM.triangularView() = H_R * P_small * H_R.transpose();\n\tM.triangularView() += R;\n\n\tassert(H_L.rows() == H_L.cols());\n\tassert(H_L.rows() == new_variable->getSize());\n\tEigen::MatrixXd H_Linv = H_L.inverse();\n\tEigen::MatrixXd P_LL = H_Linv * M.selfadjointView() * H_Linv.transpose();\n\n\tsize_t oldSize = state_ptr->cov.rows();\n\tstate_ptr->cov.conservativeResizeLike(Eigen::MatrixXd::Zero(oldSize + new_variable->getSize(), oldSize + new_variable->getSize()));\n\tstate_ptr->cov.block(0, oldSize, oldSize, new_variable->getSize()).noalias() = -M_a * H_Linv.transpose();\n\tstate_ptr->cov.block(oldSize, 0, new_variable->getSize(), oldSize) = state_ptr->cov.block(0, oldSize, oldSize, new_variable->getSize()).transpose();\n\tstate_ptr->cov.block(oldSize, oldSize, new_variable->getSize(), new_variable->getSize()) = P_LL;\n\n\tnew_variable->update(H_Linv * res);\n\tnew_variable->setLocalId(oldSize);\n\tstate_ptr->variables.push_back(new_variable);\n}\n static void augmentClone(std::shared_ptr state_ptr, Eigen::Matrix last_gyr) {\n\tif (state_ptr->clones_imu.find(state_ptr->timestamp) != state_ptr->clones_imu.end())\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tstd::shared_ptr pose_temp = stateHelper::clone(state_ptr, state_ptr->imu_ptr->pose());\n\n\tstd::shared_ptr pose = std::dynamic_pointer_cast(pose_temp);\n\tif (pose == nullptr)\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tstate_ptr->clones_imu[state_ptr->timestamp] = pose;\n\n\tif (state_ptr->options.do_calib_camera_timeoffset)\n\t{\n\t\tEigen::Matrix dnc_dt = Eigen::MatrixXd::Zero(6, 1);\n\t\tdnc_dt.block(0, 0, 3, 1) = last_gyr;\n\t\tdnc_dt.block(3, 0, 3, 1) = state_ptr->imu_ptr->getVel();\n\n\t\tstate_ptr->cov.block(0, pose->getId(), state_ptr->cov.rows(), 6) += \n\t\t\tstate_ptr->cov.block(0, state_ptr->calib_dt_imu_cam->getId(), state_ptr->cov.rows(), 1) * dnc_dt.transpose();\n\t\tstate_ptr->cov.block(pose->getId(), 0, 6, state_ptr->cov.rows()) += \n\t\t\tdnc_dt * state_ptr->cov.block(state_ptr->calib_dt_imu_cam->getId(), 0, 1, state_ptr->cov.rows());\n\t}\n}\n static void marginalizeOldClone(std::shared_ptr state_ptr) {\n\tif ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size)\n\t{\n\t\tdouble marginal_time = state_ptr->margtimestep();\n\n\t\tassert(marginal_time != INFINITY);\n\t\tstateHelper::marginalize(state_ptr, state_ptr->clones_imu.at(marginal_time));\n\n\t\tstate_ptr->clones_imu.erase(marginal_time);\n\n\t\tassert(state_ptr->clones_frame.at(marginal_time)->v_feat_ptr[0].size() == 0);\n\t\tassert(state_ptr->clones_frame.at(marginal_time)->v_feat_ptr[1].size() == 0);\n\t\tstate_ptr->clones_frame.at(marginal_time)->release();\n\t\tstate_ptr->clones_frame.erase(marginal_time);\n\t}\n}\n static void marginalizeNewClone(std::shared_ptr state_ptr) {\n\tif ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size)\n\t{\n\t\tstd::vector clone_times;\n\t\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t\t{\n\t\t\tclone_times.emplace_back(clone_imu.first);\n\t\t}\n\n\t\tdouble marginal_time = clone_times.at(clone_times.size() - 2);\n\n\t\tassert(marginal_time != INFINITY);\n\t\tstateHelper::marginalize(state_ptr, state_ptr->clones_imu.at(marginal_time));\n\n\t\tstd::shared_ptr fh_temp = state_ptr->clones_frame.at(marginal_time);\n\n\t\tif (fh_temp->v_feat_ptr.find(0) != fh_temp->v_feat_ptr.end())\n\t\t{\n\t\t\tauto it0 = fh_temp->v_feat_ptr.at(0).begin();\n\n\t\t\twhile (it0 != fh_temp->v_feat_ptr.at(0).end())\n\t\t\t{\n\t\t\t\tstd::vector marginal_timestamps = {marginal_time};\n\t\t\t\t(*it0).second->cleanInvalidMeasurements(marginal_timestamps);\n\t\t\t\tit0 = fh_temp->v_feat_ptr.at(0).begin();\n\t\t\t}\n\t\t}\n\n\t\tif (fh_temp->v_feat_ptr.find(1) != fh_temp->v_feat_ptr.end())\n\t\t{\n\t\t\tauto it1 = fh_temp->v_feat_ptr.at(1).begin();\n\n\t\t\twhile (it1 != fh_temp->v_feat_ptr.at(1).end())\n\t\t\t{\n\t\t\t\tstd::vector marginal_timestamps = {marginal_time};\n\t\t\t\t(*it1).second->cleanInvalidMeasurements(marginal_timestamps);\n\t\t\t\tit1 = fh_temp->v_feat_ptr.at(1).begin();\n\t\t\t}\n\t\t}\n\n\t\tstate_ptr->clones_imu.erase(marginal_time);\n\t\tstate_ptr->clones_frame.at(marginal_time)->release();\n\t\tstate_ptr->clones_frame.erase(marginal_time);\n\t}\n}\n static void marginalizeSlam(std::shared_ptr state_ptr, voxelHashMap &voxel_map, pcl::PointCloud::Ptr points_history) {\n\t// display\n\tpoints_history->clear();\n\t// display\n\n\tint ct_marginalized = 0;\n\tauto it0 = state_ptr->map_points.begin();\n\twhile (it0 != state_ptr->map_points.end())\n\t{\n\t\tif ((*it0).second->should_marg)\n\t\t{\n\t\t\tmapManagement::deleteFromVoxel(voxel_map, (*it0).second);\n\t\t\tstateHelper::marginalize(state_ptr, (*it0).second);\n\n\t\t\t// display\n\t\t\tpcl::PointXYZRGB point_temp;\n\t\t\tpoint_temp.x = (*it0).second->getPointXYZ(false)[0];\n\t\t\tpoint_temp.y = (*it0).second->getPointXYZ(false)[1];\n\t\t\tpoint_temp.z = (*it0).second->getPointXYZ(false)[2];\n\t\t\tpoint_temp.r = (*it0).second->color;\n\t\t\tpoint_temp.g = (*it0).second->color;\n\t\t\tpoint_temp.b = (*it0).second->color;\n\t\t\tpoints_history->points.push_back(point_temp);\n\t\t\t// display\n\n\t\t\tit0 = state_ptr->map_points.erase(it0);\n\t\t\tct_marginalized++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n}\n};"], ["/voxel_svio/include/preIntegration.h", "class preIntegrationBase {\n public:\n preIntegrationBase(double sigma_w_, double sigma_wb_, double sigma_a_, double sigma_ab_, bool imu_avg_ = false) {\n\tQ_c.block(0, 0, 3, 3) = std::pow(sigma_w_, 2) * Eigen::Matrix::Identity();\n\tQ_c.block(3, 3, 3, 3) = std::pow(sigma_wb_, 2) * Eigen::Matrix::Identity();\n\tQ_c.block(6, 6, 3, 3) = std::pow(sigma_a_, 2) * Eigen::Matrix::Identity();\n\tQ_c.block(9, 9, 3, 3) = std::pow(sigma_ab_, 2) * Eigen::Matrix::Identity();\n\n\timu_avg = imu_avg_;\n\n\te_1 << 1, 0, 0;\n\te_2 << 0, 1, 0;\n\te_3 << 0, 0, 1;\n\te_1x = quatType::skewSymmetric(e_1);\n\te_2x = quatType::skewSymmetric(e_2);\n\te_3x = quatType::skewSymmetric(e_3);\n}\n void setLinearizationPoints(Eigen::Matrix b_w_lin_, Eigen::Matrix b_a_lin_,\n\t\tEigen::Matrix q_k_lin_ = Eigen::Matrix::Zero(), Eigen::Matrix grav_ = Eigen::Matrix::Zero()) {\n\tb_w_lin = b_w_lin_;\n\tb_a_lin = b_a_lin_;\n\tq_k_lin = q_k_lin_;\n\tgrav = grav_;\n}\n virtual void feedImu(double t_0, double t_1, Eigen::Matrix w_m_0, Eigen::Matrix a_m_0, \n\t\tEigen::Matrix w_m_1 = Eigen::Matrix::Zero(), Eigen::Matrix a_m_1 = Eigen::Matrix::Zero()) = 0 {\n\tdouble delta_t = t_1 - t_0;\n\tdt += delta_t;\n\n\tif (delta_t == 0) return;\n\n\tEigen::Matrix w_hat = w_m_0 - b_w_lin;\n\tEigen::Matrix a_hat = a_m_0 - b_a_lin - R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tif (imu_avg)\n\t{\n\t\tw_hat += w_m_1 - b_w_lin;\n\t\tw_hat = 0.5 * w_hat;\n\t}\n\n\tEigen::Matrix w_hat_dt = w_hat * delta_t;\n\n\tdouble w_1 = w_hat(0, 0);\n\tdouble w_2 = w_hat(1, 0);\n\tdouble w_3 = w_hat(2, 0);\n\n\tdouble mag_w = w_hat.norm();\n\tdouble w_dt = mag_w * delta_t;\n\n\tbool small_w = (mag_w < 0.008726646);\n\n\tdouble dt_2 = std::pow(delta_t, 2);\n\tdouble cos_wt = std::cos(w_dt);\n\tdouble sin_wt = std::sin(w_dt);\n\n\tEigen::Matrix w_x = quatType::skewSymmetric(w_hat);\n\tEigen::Matrix w_tx = quatType::skewSymmetric(w_hat_dt);\n\tEigen::Matrix w_x_2 = w_x * w_x;\n\n\tEigen::Matrix R_tau1_tau2 = small_w ? Eigen::Matrix::Identity() - delta_t * w_x + (std::pow(delta_t, 2) / 2) * w_x_2 \n\t\t: Eigen::Matrix::Identity() - (sin_wt / mag_w) * w_x + ((1.0 - cos_wt) / (std::pow(mag_w, 2.0))) * w_x_2;\n\n\tEigen::Matrix R_tau1_k = R_tau1_tau2 * R_tau_k;\n\tEigen::Matrix R_k_tau1 = R_tau1_k.transpose();\n\n\tif (imu_avg)\n\t{\n\t\ta_hat += a_m_1 - b_a_lin - R_tau1_k * quatType::quatToRot(q_k_lin) * grav;\n\t\ta_hat = 0.5 * a_hat;\n\t}\n\tEigen::Matrix a_x = quatType::skewSymmetric(a_hat);\n\n\tdouble f_1;\n\tdouble f_2;\n\tdouble f_3;\n\tdouble f_4;\n\n\tif (small_w)\n\t{\n\t\tf_1 = - (std::pow(delta_t, 3) / 3);\n\t\tf_2 = (std::pow(delta_t, 4) / 8);\n\t\tf_3 = - (std::pow(delta_t, 2) / 2);\n\t\tf_4 = (std::pow(delta_t, 3) / 6);\n\t}\n\telse\n\t{\n\t\tf_1 = (w_dt * cos_wt - sin_wt) / (std::pow(mag_w, 3));\n\t\tf_2 = (std::pow(w_dt, 2) - 2 * cos_wt - 2 * w_dt * sin_wt + 2) / (2 * std::pow(mag_w, 4));\n\t\tf_3 = - (1 - cos_wt) / std::pow(mag_w, 2);\n\t\tf_4 = (w_dt - sin_wt) / std::pow(mag_w, 3);\n\t}\n\n\tEigen::Matrix alpha_arg = ((dt_2 / 2.0) * Eigen::Matrix::Identity() + f_1 * w_x + f_2 * w_x_2);\n\tEigen::Matrix Beta_arg = (delta_t * Eigen::Matrix::Identity() + f_3 * w_x + f_4 * w_x_2);\n\n\tEigen::Matrix H_al = R_k_tau1 * alpha_arg;\n\tEigen::Matrix H_be = R_k_tau1 * Beta_arg;\n\n\talpha_tau += beta_tau * delta_t + H_al * a_hat;\n\tbeta_tau += H_be * a_hat;\n\n\tEigen::Matrix J_r_tau1 = small_w ? Eigen::Matrix::Identity() - 0.5 * w_tx + (1.0 / 6.0) * w_tx * w_tx \n\t\t: Eigen::Matrix::Identity() - ((1 - cos_wt) / (std::pow((w_dt), 2.0))) * w_tx + ((w_dt - sin_wt) / (std::pow(w_dt, 3.0))) * w_tx * w_tx;\n\n\tEigen::Matrix J_save = J_q;\n\tJ_q = R_tau1_tau2 * J_q + J_r_tau1 * delta_t;\n\n\tH_a -= H_al;\n\tH_a += delta_t * H_b;\n\tH_b -= H_be;\n\n\tEigen::Matrix g_k = quatType::quatToRot(q_k_lin) * grav;\n\tO_a += delta_t * O_b;\n\tO_a += -H_al * R_tau_k * quatType::skewSymmetric(g_k);\n\tO_b += -H_be * R_tau_k * quatType::skewSymmetric(g_k);\n\n\tEigen::MatrixXd d_R_bw_1 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_1);\n\tEigen::MatrixXd d_R_bw_2 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_2);\n\tEigen::MatrixXd d_R_bw_3 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_3);\n\n\tdouble df_1_dbw_1;\n\tdouble df_1_dbw_2;\n\tdouble df_1_dbw_3;\n\n\tdouble df_2_dbw_1;\n\tdouble df_2_dbw_2;\n\tdouble df_2_dbw_3;\n\n\tdouble df_3_dbw_1;\n\tdouble df_3_dbw_2;\n\tdouble df_3_dbw_3;\n\n\tdouble df_4_dbw_1;\n\tdouble df_4_dbw_2;\n\tdouble df_4_dbw_3;\n\n\tif (small_w)\n\t{\n\t\tdouble df_1_dw_mag = -(std::pow(delta_t, 5) / 15);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(delta_t, 6) / 72);\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = -(std::pow(delta_t, 4) / 12);\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (std::pow(delta_t, 5) / 60);\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\telse\n\t{\n\t\tdouble df_1_dw_mag = (std::pow(w_dt, 2) * sin_wt - 3 * sin_wt + 3 * w_dt * cos_wt) / std::pow(mag_w, 5);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(w_dt, 2) - 4 * cos_wt - 4 * w_dt * sin_wt + std::pow(w_dt, 2) * cos_wt + 4) / (std::pow(mag_w, 6));\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = (2 * (cos_wt - 1) + w_dt * sin_wt) / (std::pow(mag_w, 4));\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (2 * w_dt + w_dt * cos_wt - 3 * sin_wt) / (std::pow(mag_w, 5));\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\n\tEigen::Matrix g_tau = R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tJ_a += J_b * delta_t;\n\tJ_a.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * alpha_arg + R_k_tau1 * (df_1_dbw_1 * w_x - f_1 * e_1x + df_2_dbw_1 * w_x_2 - f_2 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_a.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * alpha_arg + R_k_tau1 * (df_1_dbw_2 * w_x - f_1 * e_2x + df_2_dbw_2 * w_x_2 - f_2 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_a.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * alpha_arg + R_k_tau1 * (df_1_dbw_3 * w_x - f_1 * e_3x + df_2_dbw_3 * w_x_2 - f_2 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\tJ_b.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * Beta_arg + R_k_tau1 * (df_3_dbw_1 * w_x - f_3 * e_1x + df_4_dbw_1 * w_x_2 - f_4 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\t-H_be * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_b.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * Beta_arg + R_k_tau1 * (df_3_dbw_2 * w_x - f_3 * e_2x + df_4_dbw_2 * w_x_2 - f_4 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_b.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * Beta_arg + R_k_tau1 * (df_3_dbw_3 * w_x - f_3 * e_3x + df_4_dbw_3 * w_x_2 - f_4 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\n\tEigen::Matrix R_G_to_k = quatType::quatToRot(q_k_lin);\n\tdouble dt_mid = delta_t / 2.0;\n\tdouble w_dt_mid = mag_w * dt_mid;\n\tEigen::Matrix R_mid;\n\n\tR_mid = small_w ? Eigen::Matrix::Identity() - dt_mid * w_x + (std::pow(dt_mid, 2) / 2) * w_x_2\n\t\t: Eigen::Matrix::Identity() - (std::sin(w_dt_mid) / mag_w) * w_x + ((1.0 - std::cos(w_dt_mid)) / (std::pow(mag_w, 2.0))) * w_x_2;\n\tR_mid = R_mid * R_tau_k;\n\n\tEigen::Matrix F_k1 = Eigen::Matrix::Zero();\n\tF_k1.block(0, 0, 3, 3) = - w_x;\n\tF_k1.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k1.block(6, 0, 3, 3) = - R_tau_k.transpose() * a_x;\n\tF_k1.block(6, 9, 3, 3) = - R_tau_k.transpose();\n\tF_k1.block(6, 15, 3, 3) = - R_tau_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k1.block(6, 18, 3, 3) = - R_tau_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k1.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k1 = Eigen::Matrix::Zero();\n\tG_k1.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k1.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k1.block(6, 6, 3, 3) = - R_tau_k.transpose();\n\tG_k1.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_dot_k1 = F_k1;\n\tEigen::Matrix P_dot_k1 = F_k1 * P_big + P_big * F_k1.transpose() + G_k1 * Q_c * G_k1.transpose();\n\n\tEigen::Matrix F_k2 = Eigen::Matrix::Zero();\n\tF_k2.block(0, 0, 3, 3) = - w_x;\n\tF_k2.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k2.block(6, 0, 3, 3) = - R_mid.transpose() * a_x;\n\tF_k2.block(6, 9, 3, 3) = - R_mid.transpose();\n\tF_k2.block(6, 15, 3, 3) = - R_mid.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k2.block(6, 18, 3, 3) = - R_mid.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k2.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k2 = Eigen::Matrix::Zero();\n\tG_k2.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k2.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k2.block(6, 6, 3, 3) = - R_mid.transpose();\n\tG_k2.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k2 = Eigen::Matrix::Identity() + Phi_dot_k1 * dt_mid;\n\tEigen::Matrix P_k2 = P_big + P_dot_k1 * dt_mid;\n\tEigen::Matrix Phi_dot_k2 = F_k2 * Phi_k2;\n\tEigen::Matrix P_dot_k2 = F_k2 * P_k2 + P_k2 * F_k2.transpose() + G_k2 * Q_c * G_k2.transpose();\n\n\tEigen::Matrix F_k3 = F_k2;\n\tEigen::Matrix G_k3 = G_k2;\n\n\tEigen::Matrix Phi_k3 = Eigen::Matrix::Identity() + Phi_dot_k2 * dt_mid;\n\tEigen::Matrix P_k3 = P_big + P_dot_k2 * dt_mid;\n\tEigen::Matrix Phi_dot_k3 = F_k3 * Phi_k3;\n\tEigen::Matrix P_dot_k3 = F_k3 * P_k3 + P_k3 * F_k3.transpose() + G_k3 * Q_c * G_k3.transpose();\n\n\tEigen::Matrix F_k4 = Eigen::Matrix::Zero();\n\tF_k4.block(0, 0, 3, 3) = - w_x;\n\tF_k4.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k4.block(6, 0, 3, 3) = - R_tau1_k.transpose() * a_x;\n\tF_k4.block(6, 9, 3, 3) = - R_tau1_k.transpose();\n\tF_k4.block(6, 15, 3, 3) = - R_tau1_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k4.block(6, 18, 3, 3) = - R_tau1_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k4.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k4 = Eigen::Matrix::Zero();\n\tG_k4.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k4.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k4.block(6, 6, 3, 3) = - R_tau1_k.transpose();\n\tG_k4.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k4 = Eigen::Matrix::Identity() + Phi_dot_k3 * delta_t;\n\tEigen::Matrix P_k4 = P_big + P_dot_k3 * delta_t;\n\tEigen::Matrix Phi_dot_k4 = F_k4 * Phi_k4;\n\tEigen::Matrix P_dot_k4 = F_k4 * P_k4 + P_k4 * F_k4.transpose() + G_k4 * Q_c * G_k4.transpose();\n\n\tP_big += (delta_t / 6.0) * (P_dot_k1 + 2.0 * P_dot_k2 + 2.0 * P_dot_k3 + P_dot_k4);\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\n\tEigen::Matrix Phi =\n\t\tEigen::Matrix::Identity() + (delta_t / 6.0) * (Phi_dot_k1 + 2.0 * Phi_dot_k2 + 2.0 * Phi_dot_k3 + Phi_dot_k4);\n\n\tEigen::Matrix B_k = Eigen::Matrix::Identity();\n\tB_k.block(15, 15, 3, 3).setZero();\n\tB_k.block(15, 0, 3, 3) = Eigen::Matrix::Identity();\n\n\tP_big = B_k * P_big * B_k.transpose();\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\tDiscrete_J_b = B_k * Phi * Discrete_J_b;\n\n\tP_meas = P_big.block(0, 0, 15, 15);\n\n\tif (state_transition_jacobians)\n\t{\n\t\tJ_q = - Discrete_J_b.block(0, 3, 3, 3);\n\t\tJ_a = Discrete_J_b.block(12, 3, 3, 3);\n\t\tJ_b = Discrete_J_b.block(6, 3, 3, 3);\n\t\tH_a = Discrete_J_b.block(12, 9, 3, 3);\n\t\tH_b = Discrete_J_b.block(6, 9, 3, 3);\n\t\tO_a = Discrete_J_b.block(12, 18, 3, 3);\n\t\tO_b = Discrete_J_b.block(6, 18, 3, 3);\n\t}\n\n\tR_tau_k = R_tau1_k;\n\tq_tau_k = quatType::rotToQuat(R_tau_k);\n}\n bool imu_avg = false;\n double dt = 0;\n Eigen::Matrix alpha_tau = Eigen::Matrix::Zero();\n Eigen::Matrix beta_tau = Eigen::Matrix::Zero();\n Eigen::Matrix q_tau_k;\n Eigen::Matrix R_tau_k = Eigen::Matrix::Identity();\n Eigen::Matrix J_q = Eigen::Matrix::Zero();\n Eigen::Matrix J_a = Eigen::Matrix::Zero();\n Eigen::Matrix J_b = Eigen::Matrix::Zero();\n Eigen::Matrix H_a = Eigen::Matrix::Zero();\n Eigen::Matrix H_b = Eigen::Matrix::Zero();\n Eigen::Matrix b_w_lin;\n Eigen::Matrix b_a_lin;\n Eigen::Matrix q_k_lin;\n Eigen::Matrix grav = Eigen::Matrix::Zero();\n Eigen::Matrix Q_c = Eigen::Matrix::Zero();\n Eigen::Matrix P_meas = Eigen::Matrix::Zero();\n Eigen::Matrix e_1;\n Eigen::Matrix e_2;\n Eigen::Matrix e_3;\n Eigen::Matrix e_1x;\n Eigen::Matrix e_2x;\n Eigen::Matrix e_3x;\n public:\n void feedImu(double t_0, double t_1, Eigen::Matrix w_m_0, Eigen::Matrix a_m_0, \n\t\tEigen::Matrix w_m_1 = Eigen::Matrix::Zero(), Eigen::Matrix a_m_1 = Eigen::Matrix::Zero()) {\n\tdouble delta_t = t_1 - t_0;\n\tdt += delta_t;\n\n\tif (delta_t == 0) return;\n\n\tEigen::Matrix w_hat = w_m_0 - b_w_lin;\n\tEigen::Matrix a_hat = a_m_0 - b_a_lin - R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tif (imu_avg)\n\t{\n\t\tw_hat += w_m_1 - b_w_lin;\n\t\tw_hat = 0.5 * w_hat;\n\t}\n\n\tEigen::Matrix w_hat_dt = w_hat * delta_t;\n\n\tdouble w_1 = w_hat(0, 0);\n\tdouble w_2 = w_hat(1, 0);\n\tdouble w_3 = w_hat(2, 0);\n\n\tdouble mag_w = w_hat.norm();\n\tdouble w_dt = mag_w * delta_t;\n\n\tbool small_w = (mag_w < 0.008726646);\n\n\tdouble dt_2 = std::pow(delta_t, 2);\n\tdouble cos_wt = std::cos(w_dt);\n\tdouble sin_wt = std::sin(w_dt);\n\n\tEigen::Matrix w_x = quatType::skewSymmetric(w_hat);\n\tEigen::Matrix w_tx = quatType::skewSymmetric(w_hat_dt);\n\tEigen::Matrix w_x_2 = w_x * w_x;\n\n\tEigen::Matrix R_tau1_tau2 = small_w ? Eigen::Matrix::Identity() - delta_t * w_x + (std::pow(delta_t, 2) / 2) * w_x_2 \n\t\t: Eigen::Matrix::Identity() - (sin_wt / mag_w) * w_x + ((1.0 - cos_wt) / (std::pow(mag_w, 2.0))) * w_x_2;\n\n\tEigen::Matrix R_tau1_k = R_tau1_tau2 * R_tau_k;\n\tEigen::Matrix R_k_tau1 = R_tau1_k.transpose();\n\n\tif (imu_avg)\n\t{\n\t\ta_hat += a_m_1 - b_a_lin - R_tau1_k * quatType::quatToRot(q_k_lin) * grav;\n\t\ta_hat = 0.5 * a_hat;\n\t}\n\tEigen::Matrix a_x = quatType::skewSymmetric(a_hat);\n\n\tdouble f_1;\n\tdouble f_2;\n\tdouble f_3;\n\tdouble f_4;\n\n\tif (small_w)\n\t{\n\t\tf_1 = - (std::pow(delta_t, 3) / 3);\n\t\tf_2 = (std::pow(delta_t, 4) / 8);\n\t\tf_3 = - (std::pow(delta_t, 2) / 2);\n\t\tf_4 = (std::pow(delta_t, 3) / 6);\n\t}\n\telse\n\t{\n\t\tf_1 = (w_dt * cos_wt - sin_wt) / (std::pow(mag_w, 3));\n\t\tf_2 = (std::pow(w_dt, 2) - 2 * cos_wt - 2 * w_dt * sin_wt + 2) / (2 * std::pow(mag_w, 4));\n\t\tf_3 = - (1 - cos_wt) / std::pow(mag_w, 2);\n\t\tf_4 = (w_dt - sin_wt) / std::pow(mag_w, 3);\n\t}\n\n\tEigen::Matrix alpha_arg = ((dt_2 / 2.0) * Eigen::Matrix::Identity() + f_1 * w_x + f_2 * w_x_2);\n\tEigen::Matrix Beta_arg = (delta_t * Eigen::Matrix::Identity() + f_3 * w_x + f_4 * w_x_2);\n\n\tEigen::Matrix H_al = R_k_tau1 * alpha_arg;\n\tEigen::Matrix H_be = R_k_tau1 * Beta_arg;\n\n\talpha_tau += beta_tau * delta_t + H_al * a_hat;\n\tbeta_tau += H_be * a_hat;\n\n\tEigen::Matrix J_r_tau1 = small_w ? Eigen::Matrix::Identity() - 0.5 * w_tx + (1.0 / 6.0) * w_tx * w_tx \n\t\t: Eigen::Matrix::Identity() - ((1 - cos_wt) / (std::pow((w_dt), 2.0))) * w_tx + ((w_dt - sin_wt) / (std::pow(w_dt, 3.0))) * w_tx * w_tx;\n\n\tEigen::Matrix J_save = J_q;\n\tJ_q = R_tau1_tau2 * J_q + J_r_tau1 * delta_t;\n\n\tH_a -= H_al;\n\tH_a += delta_t * H_b;\n\tH_b -= H_be;\n\n\tEigen::Matrix g_k = quatType::quatToRot(q_k_lin) * grav;\n\tO_a += delta_t * O_b;\n\tO_a += -H_al * R_tau_k * quatType::skewSymmetric(g_k);\n\tO_b += -H_be * R_tau_k * quatType::skewSymmetric(g_k);\n\n\tEigen::MatrixXd d_R_bw_1 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_1);\n\tEigen::MatrixXd d_R_bw_2 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_2);\n\tEigen::MatrixXd d_R_bw_3 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_3);\n\n\tdouble df_1_dbw_1;\n\tdouble df_1_dbw_2;\n\tdouble df_1_dbw_3;\n\n\tdouble df_2_dbw_1;\n\tdouble df_2_dbw_2;\n\tdouble df_2_dbw_3;\n\n\tdouble df_3_dbw_1;\n\tdouble df_3_dbw_2;\n\tdouble df_3_dbw_3;\n\n\tdouble df_4_dbw_1;\n\tdouble df_4_dbw_2;\n\tdouble df_4_dbw_3;\n\n\tif (small_w)\n\t{\n\t\tdouble df_1_dw_mag = -(std::pow(delta_t, 5) / 15);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(delta_t, 6) / 72);\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = -(std::pow(delta_t, 4) / 12);\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (std::pow(delta_t, 5) / 60);\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\telse\n\t{\n\t\tdouble df_1_dw_mag = (std::pow(w_dt, 2) * sin_wt - 3 * sin_wt + 3 * w_dt * cos_wt) / std::pow(mag_w, 5);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(w_dt, 2) - 4 * cos_wt - 4 * w_dt * sin_wt + std::pow(w_dt, 2) * cos_wt + 4) / (std::pow(mag_w, 6));\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = (2 * (cos_wt - 1) + w_dt * sin_wt) / (std::pow(mag_w, 4));\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (2 * w_dt + w_dt * cos_wt - 3 * sin_wt) / (std::pow(mag_w, 5));\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\n\tEigen::Matrix g_tau = R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tJ_a += J_b * delta_t;\n\tJ_a.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * alpha_arg + R_k_tau1 * (df_1_dbw_1 * w_x - f_1 * e_1x + df_2_dbw_1 * w_x_2 - f_2 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_a.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * alpha_arg + R_k_tau1 * (df_1_dbw_2 * w_x - f_1 * e_2x + df_2_dbw_2 * w_x_2 - f_2 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_a.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * alpha_arg + R_k_tau1 * (df_1_dbw_3 * w_x - f_1 * e_3x + df_2_dbw_3 * w_x_2 - f_2 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\tJ_b.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * Beta_arg + R_k_tau1 * (df_3_dbw_1 * w_x - f_3 * e_1x + df_4_dbw_1 * w_x_2 - f_4 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\t-H_be * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_b.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * Beta_arg + R_k_tau1 * (df_3_dbw_2 * w_x - f_3 * e_2x + df_4_dbw_2 * w_x_2 - f_4 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_b.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * Beta_arg + R_k_tau1 * (df_3_dbw_3 * w_x - f_3 * e_3x + df_4_dbw_3 * w_x_2 - f_4 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\n\tEigen::Matrix R_G_to_k = quatType::quatToRot(q_k_lin);\n\tdouble dt_mid = delta_t / 2.0;\n\tdouble w_dt_mid = mag_w * dt_mid;\n\tEigen::Matrix R_mid;\n\n\tR_mid = small_w ? Eigen::Matrix::Identity() - dt_mid * w_x + (std::pow(dt_mid, 2) / 2) * w_x_2\n\t\t: Eigen::Matrix::Identity() - (std::sin(w_dt_mid) / mag_w) * w_x + ((1.0 - std::cos(w_dt_mid)) / (std::pow(mag_w, 2.0))) * w_x_2;\n\tR_mid = R_mid * R_tau_k;\n\n\tEigen::Matrix F_k1 = Eigen::Matrix::Zero();\n\tF_k1.block(0, 0, 3, 3) = - w_x;\n\tF_k1.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k1.block(6, 0, 3, 3) = - R_tau_k.transpose() * a_x;\n\tF_k1.block(6, 9, 3, 3) = - R_tau_k.transpose();\n\tF_k1.block(6, 15, 3, 3) = - R_tau_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k1.block(6, 18, 3, 3) = - R_tau_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k1.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k1 = Eigen::Matrix::Zero();\n\tG_k1.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k1.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k1.block(6, 6, 3, 3) = - R_tau_k.transpose();\n\tG_k1.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_dot_k1 = F_k1;\n\tEigen::Matrix P_dot_k1 = F_k1 * P_big + P_big * F_k1.transpose() + G_k1 * Q_c * G_k1.transpose();\n\n\tEigen::Matrix F_k2 = Eigen::Matrix::Zero();\n\tF_k2.block(0, 0, 3, 3) = - w_x;\n\tF_k2.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k2.block(6, 0, 3, 3) = - R_mid.transpose() * a_x;\n\tF_k2.block(6, 9, 3, 3) = - R_mid.transpose();\n\tF_k2.block(6, 15, 3, 3) = - R_mid.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k2.block(6, 18, 3, 3) = - R_mid.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k2.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k2 = Eigen::Matrix::Zero();\n\tG_k2.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k2.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k2.block(6, 6, 3, 3) = - R_mid.transpose();\n\tG_k2.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k2 = Eigen::Matrix::Identity() + Phi_dot_k1 * dt_mid;\n\tEigen::Matrix P_k2 = P_big + P_dot_k1 * dt_mid;\n\tEigen::Matrix Phi_dot_k2 = F_k2 * Phi_k2;\n\tEigen::Matrix P_dot_k2 = F_k2 * P_k2 + P_k2 * F_k2.transpose() + G_k2 * Q_c * G_k2.transpose();\n\n\tEigen::Matrix F_k3 = F_k2;\n\tEigen::Matrix G_k3 = G_k2;\n\n\tEigen::Matrix Phi_k3 = Eigen::Matrix::Identity() + Phi_dot_k2 * dt_mid;\n\tEigen::Matrix P_k3 = P_big + P_dot_k2 * dt_mid;\n\tEigen::Matrix Phi_dot_k3 = F_k3 * Phi_k3;\n\tEigen::Matrix P_dot_k3 = F_k3 * P_k3 + P_k3 * F_k3.transpose() + G_k3 * Q_c * G_k3.transpose();\n\n\tEigen::Matrix F_k4 = Eigen::Matrix::Zero();\n\tF_k4.block(0, 0, 3, 3) = - w_x;\n\tF_k4.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k4.block(6, 0, 3, 3) = - R_tau1_k.transpose() * a_x;\n\tF_k4.block(6, 9, 3, 3) = - R_tau1_k.transpose();\n\tF_k4.block(6, 15, 3, 3) = - R_tau1_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k4.block(6, 18, 3, 3) = - R_tau1_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k4.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k4 = Eigen::Matrix::Zero();\n\tG_k4.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k4.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k4.block(6, 6, 3, 3) = - R_tau1_k.transpose();\n\tG_k4.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k4 = Eigen::Matrix::Identity() + Phi_dot_k3 * delta_t;\n\tEigen::Matrix P_k4 = P_big + P_dot_k3 * delta_t;\n\tEigen::Matrix Phi_dot_k4 = F_k4 * Phi_k4;\n\tEigen::Matrix P_dot_k4 = F_k4 * P_k4 + P_k4 * F_k4.transpose() + G_k4 * Q_c * G_k4.transpose();\n\n\tP_big += (delta_t / 6.0) * (P_dot_k1 + 2.0 * P_dot_k2 + 2.0 * P_dot_k3 + P_dot_k4);\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\n\tEigen::Matrix Phi =\n\t\tEigen::Matrix::Identity() + (delta_t / 6.0) * (Phi_dot_k1 + 2.0 * Phi_dot_k2 + 2.0 * Phi_dot_k3 + Phi_dot_k4);\n\n\tEigen::Matrix B_k = Eigen::Matrix::Identity();\n\tB_k.block(15, 15, 3, 3).setZero();\n\tB_k.block(15, 0, 3, 3) = Eigen::Matrix::Identity();\n\n\tP_big = B_k * P_big * B_k.transpose();\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\tDiscrete_J_b = B_k * Phi * Discrete_J_b;\n\n\tP_meas = P_big.block(0, 0, 15, 15);\n\n\tif (state_transition_jacobians)\n\t{\n\t\tJ_q = - Discrete_J_b.block(0, 3, 3, 3);\n\t\tJ_a = Discrete_J_b.block(12, 3, 3, 3);\n\t\tJ_b = Discrete_J_b.block(6, 3, 3, 3);\n\t\tH_a = Discrete_J_b.block(12, 9, 3, 3);\n\t\tH_b = Discrete_J_b.block(6, 9, 3, 3);\n\t\tO_a = Discrete_J_b.block(12, 18, 3, 3);\n\t\tO_b = Discrete_J_b.block(6, 18, 3, 3);\n\t}\n\n\tR_tau_k = R_tau1_k;\n\tq_tau_k = quatType::rotToQuat(R_tau_k);\n}\n private:\n Eigen::Matrix P_big = Eigen::Matrix::Zero();\n Eigen::Matrix Discrete_J_b = Eigen::Matrix::Identity();\n public:\n bool state_transition_jacobians = true;\n Eigen::Matrix O_a = Eigen::Matrix::Zero();\n Eigen::Matrix O_b = Eigen::Matrix::Zero();\n void feedImu(double t_0, double t_1, Eigen::Matrix w_m_0, Eigen::Matrix a_m_0, \n\t\tEigen::Matrix w_m_1 = Eigen::Matrix::Zero(), Eigen::Matrix a_m_1 = Eigen::Matrix::Zero()) {\n\tdouble delta_t = t_1 - t_0;\n\tdt += delta_t;\n\n\tif (delta_t == 0) return;\n\n\tEigen::Matrix w_hat = w_m_0 - b_w_lin;\n\tEigen::Matrix a_hat = a_m_0 - b_a_lin - R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tif (imu_avg)\n\t{\n\t\tw_hat += w_m_1 - b_w_lin;\n\t\tw_hat = 0.5 * w_hat;\n\t}\n\n\tEigen::Matrix w_hat_dt = w_hat * delta_t;\n\n\tdouble w_1 = w_hat(0, 0);\n\tdouble w_2 = w_hat(1, 0);\n\tdouble w_3 = w_hat(2, 0);\n\n\tdouble mag_w = w_hat.norm();\n\tdouble w_dt = mag_w * delta_t;\n\n\tbool small_w = (mag_w < 0.008726646);\n\n\tdouble dt_2 = std::pow(delta_t, 2);\n\tdouble cos_wt = std::cos(w_dt);\n\tdouble sin_wt = std::sin(w_dt);\n\n\tEigen::Matrix w_x = quatType::skewSymmetric(w_hat);\n\tEigen::Matrix w_tx = quatType::skewSymmetric(w_hat_dt);\n\tEigen::Matrix w_x_2 = w_x * w_x;\n\n\tEigen::Matrix R_tau1_tau2 = small_w ? Eigen::Matrix::Identity() - delta_t * w_x + (std::pow(delta_t, 2) / 2) * w_x_2 \n\t\t: Eigen::Matrix::Identity() - (sin_wt / mag_w) * w_x + ((1.0 - cos_wt) / (std::pow(mag_w, 2.0))) * w_x_2;\n\n\tEigen::Matrix R_tau1_k = R_tau1_tau2 * R_tau_k;\n\tEigen::Matrix R_k_tau1 = R_tau1_k.transpose();\n\n\tif (imu_avg)\n\t{\n\t\ta_hat += a_m_1 - b_a_lin - R_tau1_k * quatType::quatToRot(q_k_lin) * grav;\n\t\ta_hat = 0.5 * a_hat;\n\t}\n\tEigen::Matrix a_x = quatType::skewSymmetric(a_hat);\n\n\tdouble f_1;\n\tdouble f_2;\n\tdouble f_3;\n\tdouble f_4;\n\n\tif (small_w)\n\t{\n\t\tf_1 = - (std::pow(delta_t, 3) / 3);\n\t\tf_2 = (std::pow(delta_t, 4) / 8);\n\t\tf_3 = - (std::pow(delta_t, 2) / 2);\n\t\tf_4 = (std::pow(delta_t, 3) / 6);\n\t}\n\telse\n\t{\n\t\tf_1 = (w_dt * cos_wt - sin_wt) / (std::pow(mag_w, 3));\n\t\tf_2 = (std::pow(w_dt, 2) - 2 * cos_wt - 2 * w_dt * sin_wt + 2) / (2 * std::pow(mag_w, 4));\n\t\tf_3 = - (1 - cos_wt) / std::pow(mag_w, 2);\n\t\tf_4 = (w_dt - sin_wt) / std::pow(mag_w, 3);\n\t}\n\n\tEigen::Matrix alpha_arg = ((dt_2 / 2.0) * Eigen::Matrix::Identity() + f_1 * w_x + f_2 * w_x_2);\n\tEigen::Matrix Beta_arg = (delta_t * Eigen::Matrix::Identity() + f_3 * w_x + f_4 * w_x_2);\n\n\tEigen::Matrix H_al = R_k_tau1 * alpha_arg;\n\tEigen::Matrix H_be = R_k_tau1 * Beta_arg;\n\n\talpha_tau += beta_tau * delta_t + H_al * a_hat;\n\tbeta_tau += H_be * a_hat;\n\n\tEigen::Matrix J_r_tau1 = small_w ? Eigen::Matrix::Identity() - 0.5 * w_tx + (1.0 / 6.0) * w_tx * w_tx \n\t\t: Eigen::Matrix::Identity() - ((1 - cos_wt) / (std::pow((w_dt), 2.0))) * w_tx + ((w_dt - sin_wt) / (std::pow(w_dt, 3.0))) * w_tx * w_tx;\n\n\tEigen::Matrix J_save = J_q;\n\tJ_q = R_tau1_tau2 * J_q + J_r_tau1 * delta_t;\n\n\tH_a -= H_al;\n\tH_a += delta_t * H_b;\n\tH_b -= H_be;\n\n\tEigen::Matrix g_k = quatType::quatToRot(q_k_lin) * grav;\n\tO_a += delta_t * O_b;\n\tO_a += -H_al * R_tau_k * quatType::skewSymmetric(g_k);\n\tO_b += -H_be * R_tau_k * quatType::skewSymmetric(g_k);\n\n\tEigen::MatrixXd d_R_bw_1 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_1);\n\tEigen::MatrixXd d_R_bw_2 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_2);\n\tEigen::MatrixXd d_R_bw_3 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_3);\n\n\tdouble df_1_dbw_1;\n\tdouble df_1_dbw_2;\n\tdouble df_1_dbw_3;\n\n\tdouble df_2_dbw_1;\n\tdouble df_2_dbw_2;\n\tdouble df_2_dbw_3;\n\n\tdouble df_3_dbw_1;\n\tdouble df_3_dbw_2;\n\tdouble df_3_dbw_3;\n\n\tdouble df_4_dbw_1;\n\tdouble df_4_dbw_2;\n\tdouble df_4_dbw_3;\n\n\tif (small_w)\n\t{\n\t\tdouble df_1_dw_mag = -(std::pow(delta_t, 5) / 15);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(delta_t, 6) / 72);\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = -(std::pow(delta_t, 4) / 12);\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (std::pow(delta_t, 5) / 60);\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\telse\n\t{\n\t\tdouble df_1_dw_mag = (std::pow(w_dt, 2) * sin_wt - 3 * sin_wt + 3 * w_dt * cos_wt) / std::pow(mag_w, 5);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(w_dt, 2) - 4 * cos_wt - 4 * w_dt * sin_wt + std::pow(w_dt, 2) * cos_wt + 4) / (std::pow(mag_w, 6));\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = (2 * (cos_wt - 1) + w_dt * sin_wt) / (std::pow(mag_w, 4));\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (2 * w_dt + w_dt * cos_wt - 3 * sin_wt) / (std::pow(mag_w, 5));\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\n\tEigen::Matrix g_tau = R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tJ_a += J_b * delta_t;\n\tJ_a.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * alpha_arg + R_k_tau1 * (df_1_dbw_1 * w_x - f_1 * e_1x + df_2_dbw_1 * w_x_2 - f_2 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_a.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * alpha_arg + R_k_tau1 * (df_1_dbw_2 * w_x - f_1 * e_2x + df_2_dbw_2 * w_x_2 - f_2 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_a.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * alpha_arg + R_k_tau1 * (df_1_dbw_3 * w_x - f_1 * e_3x + df_2_dbw_3 * w_x_2 - f_2 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\tJ_b.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * Beta_arg + R_k_tau1 * (df_3_dbw_1 * w_x - f_3 * e_1x + df_4_dbw_1 * w_x_2 - f_4 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\t-H_be * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_b.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * Beta_arg + R_k_tau1 * (df_3_dbw_2 * w_x - f_3 * e_2x + df_4_dbw_2 * w_x_2 - f_4 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_b.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * Beta_arg + R_k_tau1 * (df_3_dbw_3 * w_x - f_3 * e_3x + df_4_dbw_3 * w_x_2 - f_4 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\n\tEigen::Matrix R_G_to_k = quatType::quatToRot(q_k_lin);\n\tdouble dt_mid = delta_t / 2.0;\n\tdouble w_dt_mid = mag_w * dt_mid;\n\tEigen::Matrix R_mid;\n\n\tR_mid = small_w ? Eigen::Matrix::Identity() - dt_mid * w_x + (std::pow(dt_mid, 2) / 2) * w_x_2\n\t\t: Eigen::Matrix::Identity() - (std::sin(w_dt_mid) / mag_w) * w_x + ((1.0 - std::cos(w_dt_mid)) / (std::pow(mag_w, 2.0))) * w_x_2;\n\tR_mid = R_mid * R_tau_k;\n\n\tEigen::Matrix F_k1 = Eigen::Matrix::Zero();\n\tF_k1.block(0, 0, 3, 3) = - w_x;\n\tF_k1.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k1.block(6, 0, 3, 3) = - R_tau_k.transpose() * a_x;\n\tF_k1.block(6, 9, 3, 3) = - R_tau_k.transpose();\n\tF_k1.block(6, 15, 3, 3) = - R_tau_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k1.block(6, 18, 3, 3) = - R_tau_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k1.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k1 = Eigen::Matrix::Zero();\n\tG_k1.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k1.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k1.block(6, 6, 3, 3) = - R_tau_k.transpose();\n\tG_k1.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_dot_k1 = F_k1;\n\tEigen::Matrix P_dot_k1 = F_k1 * P_big + P_big * F_k1.transpose() + G_k1 * Q_c * G_k1.transpose();\n\n\tEigen::Matrix F_k2 = Eigen::Matrix::Zero();\n\tF_k2.block(0, 0, 3, 3) = - w_x;\n\tF_k2.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k2.block(6, 0, 3, 3) = - R_mid.transpose() * a_x;\n\tF_k2.block(6, 9, 3, 3) = - R_mid.transpose();\n\tF_k2.block(6, 15, 3, 3) = - R_mid.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k2.block(6, 18, 3, 3) = - R_mid.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k2.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k2 = Eigen::Matrix::Zero();\n\tG_k2.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k2.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k2.block(6, 6, 3, 3) = - R_mid.transpose();\n\tG_k2.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k2 = Eigen::Matrix::Identity() + Phi_dot_k1 * dt_mid;\n\tEigen::Matrix P_k2 = P_big + P_dot_k1 * dt_mid;\n\tEigen::Matrix Phi_dot_k2 = F_k2 * Phi_k2;\n\tEigen::Matrix P_dot_k2 = F_k2 * P_k2 + P_k2 * F_k2.transpose() + G_k2 * Q_c * G_k2.transpose();\n\n\tEigen::Matrix F_k3 = F_k2;\n\tEigen::Matrix G_k3 = G_k2;\n\n\tEigen::Matrix Phi_k3 = Eigen::Matrix::Identity() + Phi_dot_k2 * dt_mid;\n\tEigen::Matrix P_k3 = P_big + P_dot_k2 * dt_mid;\n\tEigen::Matrix Phi_dot_k3 = F_k3 * Phi_k3;\n\tEigen::Matrix P_dot_k3 = F_k3 * P_k3 + P_k3 * F_k3.transpose() + G_k3 * Q_c * G_k3.transpose();\n\n\tEigen::Matrix F_k4 = Eigen::Matrix::Zero();\n\tF_k4.block(0, 0, 3, 3) = - w_x;\n\tF_k4.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k4.block(6, 0, 3, 3) = - R_tau1_k.transpose() * a_x;\n\tF_k4.block(6, 9, 3, 3) = - R_tau1_k.transpose();\n\tF_k4.block(6, 15, 3, 3) = - R_tau1_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k4.block(6, 18, 3, 3) = - R_tau1_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k4.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k4 = Eigen::Matrix::Zero();\n\tG_k4.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k4.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k4.block(6, 6, 3, 3) = - R_tau1_k.transpose();\n\tG_k4.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k4 = Eigen::Matrix::Identity() + Phi_dot_k3 * delta_t;\n\tEigen::Matrix P_k4 = P_big + P_dot_k3 * delta_t;\n\tEigen::Matrix Phi_dot_k4 = F_k4 * Phi_k4;\n\tEigen::Matrix P_dot_k4 = F_k4 * P_k4 + P_k4 * F_k4.transpose() + G_k4 * Q_c * G_k4.transpose();\n\n\tP_big += (delta_t / 6.0) * (P_dot_k1 + 2.0 * P_dot_k2 + 2.0 * P_dot_k3 + P_dot_k4);\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\n\tEigen::Matrix Phi =\n\t\tEigen::Matrix::Identity() + (delta_t / 6.0) * (Phi_dot_k1 + 2.0 * Phi_dot_k2 + 2.0 * Phi_dot_k3 + Phi_dot_k4);\n\n\tEigen::Matrix B_k = Eigen::Matrix::Identity();\n\tB_k.block(15, 15, 3, 3).setZero();\n\tB_k.block(15, 0, 3, 3) = Eigen::Matrix::Identity();\n\n\tP_big = B_k * P_big * B_k.transpose();\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\tDiscrete_J_b = B_k * Phi * Discrete_J_b;\n\n\tP_meas = P_big.block(0, 0, 15, 15);\n\n\tif (state_transition_jacobians)\n\t{\n\t\tJ_q = - Discrete_J_b.block(0, 3, 3, 3);\n\t\tJ_a = Discrete_J_b.block(12, 3, 3, 3);\n\t\tJ_b = Discrete_J_b.block(6, 3, 3, 3);\n\t\tH_a = Discrete_J_b.block(12, 9, 3, 3);\n\t\tH_b = Discrete_J_b.block(6, 9, 3, 3);\n\t\tO_a = Discrete_J_b.block(12, 18, 3, 3);\n\t\tO_b = Discrete_J_b.block(6, 18, 3, 3);\n\t}\n\n\tR_tau_k = R_tau1_k;\n\tq_tau_k = quatType::rotToQuat(R_tau_k);\n}\n public:\n bool Plus(const double *x, const double *delta, double *x_plus_delta) const override {\n\tEigen::Map q(x);\n\n\tEigen::Map d_th(delta);\n\tEigen::Matrix d_q;\n\tdouble theta = d_th.norm();\n\n\tif (theta < 1e-8)\n\t\td_q << 0.5 * d_th, 1.0;\n\telse\n\t{\n\t\td_q.block(0, 0, 3, 1) = (d_th / theta) * std::sin(theta / 2);\n\t\td_q(3, 0) = std::cos(theta / 2);\n\t}\n\td_q = quatType::quatNorm(d_q);\n\n\tEigen::Map q_plus(x_plus_delta);\n\tq_plus = quatType::quatMultiply(d_q, q);\n\n\treturn true;\n}\n bool ComputeJacobian(const double *x, double *jacobian) const override {\n\tEigen::Map> j(jacobian);\n\tj.topRows<3>().setIdentity();\n\tj.bottomRows<1>().setZero();\n\n\treturn true;\n}\n public:\n Eigen::MatrixXd x_lin;\n std::vector x_type;\n Eigen::MatrixXd sqrt_I;\n Eigen::MatrixXd b;\n factorGenericPrior(const Eigen::MatrixXd &x_lin_, const std::vector &x_type_, const Eigen::MatrixXd &prior_Info, \n\t\tconst Eigen::MatrixXd &prior_grad) {\n\tint state_size = 0;\n\tint state_error_size = 0;\n\n\tfor (auto const &str : x_type_)\n\t{\n\t\tif (str == \"quat\")\n\t\t{\n\t\t\tstate_size += 4;\n\t\t\tstate_error_size += 3;\n\t\t}\n\t\telse if (str == \"quat_yaw\")\n\t\t{\n\t\t\tstate_size += 4;\n\t\t\tstate_error_size += 1;\n\t\t}\n\t\telse if (str == \"vec1\")\n\t\t{\n\t\t\tstate_size += 1;\n\t\t\tstate_error_size += 1;\n\t\t}\n\t\telse if (str == \"vec3\")\n\t\t{\n\t\t\tstate_size += 3;\n\t\t\tstate_error_size += 3;\n\t\t}\n\t\telse if (str == \"vec8\")\n\t\t{\n\t\t\tstate_size += 8;\n\t\t\tstate_error_size += 8;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tstd::cerr << \"type - \" << str << \" not implemented in prior\" << std::endl;\n\t\t\tstd::exit(EXIT_FAILURE);\n\t\t}\n\t}\n\n\tassert(x_lin.rows() == state_size);\n\tassert(x_lin.cols() == 1);\n\tassert(prior_Info.rows() == state_error_size);\n\tassert(prior_Info.cols() == state_error_size);\n\tassert(prior_grad.rows() == state_error_size);\n\tassert(prior_grad.cols() == 1);\n\n\tEigen::LLT llt_of_I(prior_Info);\n\tsqrt_I = llt_of_I.matrixL().transpose();\n\tEigen::MatrixXd I = Eigen::MatrixXd::Identity(prior_Info.rows(), prior_Info.rows());\n\tb = sqrt_I.triangularView().solve(I) * prior_grad;\n\n\tif (std::isnan(prior_Info.norm()) || std::isnan(sqrt_I.norm()) || std::isnan(b.norm()))\n\t{\n\t\tstd::cerr << \"prior_Info - \" << std::endl << prior_Info << std::endl << std::endl;\n\t\tstd::cerr << \"prior_Info_inv - \" << std::endl << prior_Info.inverse() << std::endl << std::endl;\n\t\tstd::cerr << \"b - \" << std::endl << b << std::endl << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tset_num_residuals(state_error_size);\n\n\tfor (auto const &str : x_type_)\n\t{\n\t\tif (str == \"quat\")\n\t\t\tmutable_parameter_block_sizes()->push_back(4);\n\t\tif (str == \"quat_yaw\")\n\t\t\tmutable_parameter_block_sizes()->push_back(4);\n\t\tif (str == \"vec1\")\n\t\t\tmutable_parameter_block_sizes()->push_back(1);\n\t\tif (str == \"vec3\")\n\t\t\tmutable_parameter_block_sizes()->push_back(3);\n\t\tif (str == \"vec8\")\n\t\t\tmutable_parameter_block_sizes()->push_back(8);\n\t}\n}\n bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override {\n\tEigen::Vector4d q_GtoIi = Eigen::Map(parameters[0]);\n\tEigen::Matrix3d R_GtoIi = quatType::quatToRot(q_GtoIi);\n\tEigen::Vector3d p_IiinG = Eigen::Map(parameters[1]);\n\tEigen::Vector3d p_FinG = Eigen::Map(parameters[2]);\n\tEigen::Vector4d q_ItoC = Eigen::Map(parameters[3]);\n\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\tEigen::Vector3d p_IinC = Eigen::Map(parameters[4]);\n\n\tEigen::Matrix camera_vals = Eigen::Map>(parameters[5]);\n\n\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n\tEigen::Vector2d uv_norm;\n\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\tEigen::Matrix sqrt_Q_gate = gate * sqrt_Q;\n\n\tEigen::Vector2d uv_dist;\n\tEigen::MatrixXd H_dz_dzn, H_dz_dzeta;\n\n\tif (is_fisheye)\n\t{\n\t\tcameraEqui cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\telse\n\t{\n\t\tcameraRadtan cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\n\tEigen::Vector2d res = uv_dist - uv_meas;\n\tres = sqrt_Q_gate * res;\n\tresiduals[0] = res(0);\n\tresiduals[1] = res(1);\n\n\tif (jacobians)\n\t{\n\t\tEigen::MatrixXd H_dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\tH_dzn_dpfc << 1.0 / p_FinCi(2), 0, -p_FinCi(0) / std::pow(p_FinCi(2), 2), 0, 1.0 / p_FinCi(2), -p_FinCi(1) / std::pow(p_FinCi(2), 2);\n\t\tEigen::MatrixXd H_dz_dpfc = H_dz_dzn * H_dzn_dpfc;\n\n\t\tif (jacobians[0])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[0]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[1]) {\n\t\t\tEigen::Map> jacobian(jacobians[1]);\n\t\t\tjacobian.block(0, 0, 2, 3) = -H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[2])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[2]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[3])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[3]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * quatType::skewSymmetric(R_ItoC * p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[4])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[4]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc;\n\t\t}\n\n\t\tif (jacobians[5])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[5]);\n\t\t\tjacobian.block(0, 0, 2, 8) = H_dz_dzeta;\n\t\t}\n\t}\n\t\n\treturn true;\n}\n public:\n Eigen::Vector3d alpha;\n Eigen::Vector3d beta;\n Eigen::Vector4d q_breve;\n double dt;\n Eigen::Vector3d b_w_lin_save;\n Eigen::Vector3d b_a_lin_save;\n Eigen::Matrix3d J_q;\n Eigen::Matrix3d J_a;\n Eigen::Matrix3d J_b;\n Eigen::Matrix3d H_a;\n Eigen::Matrix3d H_b;\n Eigen::Matrix sqrt_I_save;\n Eigen::Vector3d grav_save;\n factorImuPreIntegrationV1(double deltatime, Eigen::Vector3d &grav, Eigen::Vector3d &alpha, Eigen::Vector3d &beta, Eigen::Vector4d &q_KtoK1,\n\t\tEigen::Vector3d &ba_lin, Eigen::Vector3d &bg_lin, Eigen::Matrix3d &J_q, Eigen::Matrix3d &J_beta, Eigen::Matrix3d &J_alpha,\n\t\tEigen::Matrix3d &H_beta, Eigen::Matrix3d &H_alpha, Eigen::Matrix &covariance) {\n\tthis->alpha = alpha;\n\tthis->beta = beta;\n\tthis->q_breve = q_KtoK1;\n\tthis->dt = deltatime;\n\tthis->grav_save = grav;\n\n\tthis->b_a_lin_save = ba_lin;\n\tthis->b_w_lin_save = bg_lin;\n\n\tthis->J_q = J_q;\n\tthis->J_a = J_alpha;\n\tthis->J_b = J_beta;\n\tthis->H_a = H_alpha;\n\tthis->H_b = H_beta;\n\n\tEigen::MatrixXd I = Eigen::MatrixXd::Identity(covariance.rows(), covariance.rows());\n\tEigen::MatrixXd information = covariance.llt().solve(I);\n \n\tif (std::isnan(information.norm()))\n\t{\n\t\tstd::cerr << \"P - \" << std::endl << covariance << std::endl << std::endl;\n\t\tstd::cerr << \"Pinv - \" << std::endl << covariance.inverse() << std::endl << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tEigen::LLT llt_of_I(information);\n\tsqrt_I_save = llt_of_I.matrixL().transpose();\n\n\tset_num_residuals(15);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n}\n bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override {\n\tEigen::Vector4d q_GtoIi = Eigen::Map(parameters[0]);\n\tEigen::Matrix3d R_GtoIi = quatType::quatToRot(q_GtoIi);\n\tEigen::Vector3d p_IiinG = Eigen::Map(parameters[1]);\n\tEigen::Vector3d p_FinG = Eigen::Map(parameters[2]);\n\tEigen::Vector4d q_ItoC = Eigen::Map(parameters[3]);\n\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\tEigen::Vector3d p_IinC = Eigen::Map(parameters[4]);\n\n\tEigen::Matrix camera_vals = Eigen::Map>(parameters[5]);\n\n\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n\tEigen::Vector2d uv_norm;\n\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\tEigen::Matrix sqrt_Q_gate = gate * sqrt_Q;\n\n\tEigen::Vector2d uv_dist;\n\tEigen::MatrixXd H_dz_dzn, H_dz_dzeta;\n\n\tif (is_fisheye)\n\t{\n\t\tcameraEqui cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\telse\n\t{\n\t\tcameraRadtan cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\n\tEigen::Vector2d res = uv_dist - uv_meas;\n\tres = sqrt_Q_gate * res;\n\tresiduals[0] = res(0);\n\tresiduals[1] = res(1);\n\n\tif (jacobians)\n\t{\n\t\tEigen::MatrixXd H_dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\tH_dzn_dpfc << 1.0 / p_FinCi(2), 0, -p_FinCi(0) / std::pow(p_FinCi(2), 2), 0, 1.0 / p_FinCi(2), -p_FinCi(1) / std::pow(p_FinCi(2), 2);\n\t\tEigen::MatrixXd H_dz_dpfc = H_dz_dzn * H_dzn_dpfc;\n\n\t\tif (jacobians[0])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[0]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[1]) {\n\t\t\tEigen::Map> jacobian(jacobians[1]);\n\t\t\tjacobian.block(0, 0, 2, 3) = -H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[2])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[2]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[3])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[3]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * quatType::skewSymmetric(R_ItoC * p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[4])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[4]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc;\n\t\t}\n\n\t\tif (jacobians[5])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[5]);\n\t\t\tjacobian.block(0, 0, 2, 8) = H_dz_dzeta;\n\t\t}\n\t}\n\t\n\treturn true;\n}\n public:\n Eigen::Vector2d uv_meas;\n double pix_sigma = 1.0;\n Eigen::Matrix sqrt_Q;\n bool is_fisheye = false;\n double gate = 1.0;\n factorImageReprojCalib(const Eigen::Vector2d &uv_meas_, double pix_sigma_, bool is_fisheye_) {\n\tsqrt_Q = Eigen::Matrix::Identity();\n\tsqrt_Q(0, 0) *= 1.0 / pix_sigma;\n\tsqrt_Q(1, 1) *= 1.0 / pix_sigma;\n\n\tset_num_residuals(2);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(8);\n}\n bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override {\n\tEigen::Vector4d q_GtoIi = Eigen::Map(parameters[0]);\n\tEigen::Matrix3d R_GtoIi = quatType::quatToRot(q_GtoIi);\n\tEigen::Vector3d p_IiinG = Eigen::Map(parameters[1]);\n\tEigen::Vector3d p_FinG = Eigen::Map(parameters[2]);\n\tEigen::Vector4d q_ItoC = Eigen::Map(parameters[3]);\n\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\tEigen::Vector3d p_IinC = Eigen::Map(parameters[4]);\n\n\tEigen::Matrix camera_vals = Eigen::Map>(parameters[5]);\n\n\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n\tEigen::Vector2d uv_norm;\n\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\tEigen::Matrix sqrt_Q_gate = gate * sqrt_Q;\n\n\tEigen::Vector2d uv_dist;\n\tEigen::MatrixXd H_dz_dzn, H_dz_dzeta;\n\n\tif (is_fisheye)\n\t{\n\t\tcameraEqui cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\telse\n\t{\n\t\tcameraRadtan cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\n\tEigen::Vector2d res = uv_dist - uv_meas;\n\tres = sqrt_Q_gate * res;\n\tresiduals[0] = res(0);\n\tresiduals[1] = res(1);\n\n\tif (jacobians)\n\t{\n\t\tEigen::MatrixXd H_dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\tH_dzn_dpfc << 1.0 / p_FinCi(2), 0, -p_FinCi(0) / std::pow(p_FinCi(2), 2), 0, 1.0 / p_FinCi(2), -p_FinCi(1) / std::pow(p_FinCi(2), 2);\n\t\tEigen::MatrixXd H_dz_dpfc = H_dz_dzn * H_dzn_dpfc;\n\n\t\tif (jacobians[0])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[0]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[1]) {\n\t\t\tEigen::Map> jacobian(jacobians[1]);\n\t\t\tjacobian.block(0, 0, 2, 3) = -H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[2])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[2]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[3])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[3]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * quatType::skewSymmetric(R_ItoC * p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[4])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[4]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc;\n\t\t}\n\n\t\tif (jacobians[5])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[5]);\n\t\t\tjacobian.block(0, 0, 2, 8) = H_dz_dzeta;\n\t\t}\n\t}\n\t\n\treturn true;\n}\n};"], ["/voxel_svio/include/state.h", "class mapPoint {\n public:\n baseType(int size_) {\n\tsize = size_;\n}\n virtual void setLocalId(int new_id) {\n\tid = new_id;\n\tpose_->setLocalId(new_id);\n\tv_->setLocalId(pose_->getId() + ((new_id != -1) ? pose_->getSize() : 0));\n\tbg_->setLocalId(v_->getId() + ((new_id != -1) ? v_->getSize() : 0));\n\tba_->setLocalId(bg_->getId() + ((new_id != -1) ? bg_->getSize() : 0));\n}\n int getId() {\n\treturn id;\n}\n int getSize() {\n\treturn size;\n}\n virtual void update(const Eigen::VectorXd &dx) = 0 {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n virtual void setValue(const Eigen::MatrixXd &new_value) {\n\tsetValueInternal(new_value);\n}\n virtual void setFej(const Eigen::MatrixXd &new_value) {\n\tsetFejInternal(new_value);\n}\n virtual std::shared_ptr clone() = 0 {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n virtual std::shared_ptr checkIfSubvariable(const std::shared_ptr check) {\n\tif (check == pose_)\n\t{\n\t\treturn pose_;\n\t}\n\telse if (check == pose_->checkIfSubvariable(check))\n\t{\n\t\treturn pose_->checkIfSubvariable(check);\n\t} \n\telse if (check == v_)\n\t{\n\t\treturn v_;\n\t}\n\telse if (check == bg_)\n\t{\n\t\treturn bg_;\n\t}\n\telse if (check == ba_)\n\t{\n\t\treturn ba_;\n\t}\n\treturn nullptr;\n}\n protected:\n Eigen::MatrixXd fej_;\n Eigen::MatrixXd value_;\n int id = -1;\n int size = -1;\n public:\n jplQuat() {\n\tEigen::Vector4d q_temp = Eigen::Vector4d::Zero();\n\tq_temp(3) = 1.0;\n\tsetValueInternal(q_temp);\n\tsetFejInternal(q_temp);\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n void setValue(const Eigen::MatrixXd &new_value) override {\n\tsetValueInternal(new_value);\n}\n void setFej(const Eigen::MatrixXd &new_value) override {\n\tsetFejInternal(new_value);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n Eigen::Matrix getRot() {\n\treturn pose_->getRot();\n}\n Eigen::Matrix getRotFej() {\n\treturn pose_->getRotFej();\n}\n protected:\n Eigen::Matrix R;\n Eigen::Matrix R_fej;\n void setValueInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setValue(new_value.block<7, 1>(0, 0));\n\tv_->setValue(new_value.block<3, 1>(7, 0));\n\tbg_->setValue(new_value.block<3, 1>(10, 0));\n\tba_->setValue(new_value.block<3, 1>(13, 0));\n\n\tvalue_ = new_value;\n}\n void setFejInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setFej(new_value.block<7, 1>(0, 0));\n\tv_->setFej(new_value.block<3, 1>(7, 0));\n\tbg_->setFej(new_value.block<3, 1>(10, 0));\n\tba_->setFej(new_value.block<3, 1>(13, 0));\n\n\tfej_ = new_value;\n}\n public:\n vec(int dim) {\n\tvalue_ = Eigen::VectorXd::Zero(dim);\n\tfej_ = Eigen::VectorXd::Zero(dim);\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n public:\n poseJpl() {\n\tq_ = std::shared_ptr(new jplQuat());\n\tp_ = std::shared_ptr(new vec(3));\n\n\tEigen::Matrix pose_temp;\n\tpose_temp.setZero();\n pose_temp(3) = 1.0;\n setValueInternal(pose_temp);\n setFejInternal(pose_temp);\n}\n void setLocalId(int new_id) override {\n\tid = new_id;\n\tpose_->setLocalId(new_id);\n\tv_->setLocalId(pose_->getId() + ((new_id != -1) ? pose_->getSize() : 0));\n\tbg_->setLocalId(v_->getId() + ((new_id != -1) ? v_->getSize() : 0));\n\tba_->setLocalId(bg_->getId() + ((new_id != -1) ? bg_->getSize() : 0));\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n void setValue(const Eigen::MatrixXd &new_value) override {\n\tsetValueInternal(new_value);\n}\n void setFej(const Eigen::MatrixXd &new_value) override {\n\tsetFejInternal(new_value);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n std::shared_ptr checkIfSubvariable(const std::shared_ptr check) override {\n\tif (check == pose_)\n\t{\n\t\treturn pose_;\n\t}\n\telse if (check == pose_->checkIfSubvariable(check))\n\t{\n\t\treturn pose_->checkIfSubvariable(check);\n\t} \n\telse if (check == v_)\n\t{\n\t\treturn v_;\n\t}\n\telse if (check == bg_)\n\t{\n\t\treturn bg_;\n\t}\n\telse if (check == ba_)\n\t{\n\t\treturn ba_;\n\t}\n\treturn nullptr;\n}\n Eigen::Matrix getRot() {\n\treturn pose_->getRot();\n}\n Eigen::Matrix getRotFej() {\n\treturn pose_->getRotFej();\n}\n Eigen::Matrix getQuat() {\n\treturn pose_->getQuat();\n}\n Eigen::Matrix getQuatFej() {\n\treturn pose_->getQuatFej();\n}\n Eigen::Matrix getPos() {\n\treturn pose_->getPos();\n}\n Eigen::Matrix getPosFej() {\n\treturn pose_->getPosFej();\n}\n std::shared_ptr q() {\n\treturn pose_->q();\n}\n std::shared_ptr p() {\n\treturn pose_->p();\n}\n protected:\n std::shared_ptr q_;\n std::shared_ptr p_;\n void setValueInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setValue(new_value.block<7, 1>(0, 0));\n\tv_->setValue(new_value.block<3, 1>(7, 0));\n\tbg_->setValue(new_value.block<3, 1>(10, 0));\n\tba_->setValue(new_value.block<3, 1>(13, 0));\n\n\tvalue_ = new_value;\n}\n void setFejInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setFej(new_value.block<7, 1>(0, 0));\n\tv_->setFej(new_value.block<3, 1>(7, 0));\n\tbg_->setFej(new_value.block<3, 1>(10, 0));\n\tba_->setFej(new_value.block<3, 1>(13, 0));\n\n\tfej_ = new_value;\n}\n public:\n imuState() {\n\tpose_ = std::shared_ptr(new poseJpl());\n\tv_ = std::shared_ptr(new vec(3));\n\tbg_ = std::shared_ptr(new vec(3));\n\tba_ = std::shared_ptr(new vec(3));\n\n\tEigen::VectorXd imu_temp = Eigen::VectorXd::Zero(16, 1);\n\timu_temp(3) = 1.0;\n\tsetValueInternal(imu_temp);\n\tsetFejInternal(imu_temp);\n}\n void setLocalId(int new_id) override {\n\tid = new_id;\n\tpose_->setLocalId(new_id);\n\tv_->setLocalId(pose_->getId() + ((new_id != -1) ? pose_->getSize() : 0));\n\tbg_->setLocalId(v_->getId() + ((new_id != -1) ? v_->getSize() : 0));\n\tba_->setLocalId(bg_->getId() + ((new_id != -1) ? bg_->getSize() : 0));\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n void setValue(const Eigen::MatrixXd &new_value) override {\n\tsetValueInternal(new_value);\n}\n void setFej(const Eigen::MatrixXd &new_value) override {\n\tsetFejInternal(new_value);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n std::shared_ptr checkIfSubvariable(const std::shared_ptr check) override {\n\tif (check == pose_)\n\t{\n\t\treturn pose_;\n\t}\n\telse if (check == pose_->checkIfSubvariable(check))\n\t{\n\t\treturn pose_->checkIfSubvariable(check);\n\t} \n\telse if (check == v_)\n\t{\n\t\treturn v_;\n\t}\n\telse if (check == bg_)\n\t{\n\t\treturn bg_;\n\t}\n\telse if (check == ba_)\n\t{\n\t\treturn ba_;\n\t}\n\treturn nullptr;\n}\n Eigen::Matrix getRot() {\n\treturn pose_->getRot();\n}\n Eigen::Matrix getRotFej() {\n\treturn pose_->getRotFej();\n}\n Eigen::Matrix getQuat() {\n\treturn pose_->getQuat();\n}\n Eigen::Matrix getQuatFej() {\n\treturn pose_->getQuatFej();\n}\n Eigen::Matrix getPos() {\n\treturn pose_->getPos();\n}\n Eigen::Matrix getPosFej() {\n\treturn pose_->getPosFej();\n}\n Eigen::Matrix getVel() {\n\treturn v_->value();\n}\n Eigen::Matrix getVelFej() {\n\treturn v_->fej();\n}\n Eigen::Matrix getBg() {\n\treturn bg_->value();\n}\n Eigen::Matrix getBgFej() {\n\treturn bg_->fej();\n}\n Eigen::Matrix getBa() {\n\treturn ba_->value();\n}\n Eigen::Matrix getBaFej() {\n\treturn ba_->fej();\n}\n std::shared_ptr pose() {\n\treturn pose_;\n}\n std::shared_ptr q() {\n\treturn pose_->q();\n}\n std::shared_ptr p() {\n\treturn pose_->p();\n}\n std::shared_ptr v() {\n\treturn v_;\n}\n std::shared_ptr bg() {\n\treturn bg_;\n}\n std::shared_ptr ba() {\n\treturn ba_;\n}\n protected:\n std::shared_ptr pose_;\n std::shared_ptr v_;\n std::shared_ptr bg_;\n std::shared_ptr ba_;\n void setValueInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setValue(new_value.block<7, 1>(0, 0));\n\tv_->setValue(new_value.block<3, 1>(7, 0));\n\tbg_->setValue(new_value.block<3, 1>(10, 0));\n\tba_->setValue(new_value.block<3, 1>(13, 0));\n\n\tvalue_ = new_value;\n}\n void setFejInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setFej(new_value.block<7, 1>(0, 0));\n\tv_->setFej(new_value.block<3, 1>(7, 0));\n\tbg_->setFej(new_value.block<3, 1>(10, 0));\n\tba_->setFej(new_value.block<3, 1>(13, 0));\n\n\tfej_ = new_value;\n}\n public:\n state(stateOptions &options_) {\n\toptions = options_;\n\n\tint current_id = 0;\n\timu_ptr = std::make_shared();\n\timu_ptr->setLocalId(current_id);\n\tvariables.push_back(imu_ptr);\n\tcurrent_id += imu_ptr->getSize();\n\n\tcalib_imu_dw = std::make_shared(6);\n\tcalib_imu_da = std::make_shared(6);\n\n\tif (options.imu_model == ImuModel::KALIBR)\n\t{\n Eigen::Matrix imu_default = Eigen::Matrix::Zero();\n imu_default << 1.0, 0.0, 0.0, 1.0, 0.0, 1.0;\n calib_imu_dw->setValue(imu_default);\n calib_imu_dw->setFej(imu_default);\n calib_imu_da->setValue(imu_default);\n calib_imu_da->setFej(imu_default);\n }\n else\n {\n Eigen::Matrix imu_default = Eigen::Matrix::Zero();\n imu_default << 1.0, 0.0, 0.0, 1.0, 0.0, 1.0;\n calib_imu_dw->setValue(imu_default);\n calib_imu_dw->setFej(imu_default);\n calib_imu_da->setValue(imu_default);\n calib_imu_da->setFej(imu_default);\n }\n calib_imu_tg = std::make_shared(9);\n calib_imu_gyr = std::make_shared();\n calib_imu_acc = std::make_shared();\n\n if (options.do_calib_imu_intrinsics)\n {\n calib_imu_dw->setLocalId(current_id);\n variables.push_back(calib_imu_dw);\n current_id += calib_imu_dw->getSize();\n\n calib_imu_da->setLocalId(current_id);\n variables.push_back(calib_imu_da);\n current_id += calib_imu_da->getSize();\n\n if (options.do_calib_imu_g_sensitivity)\n {\n calib_imu_tg->setLocalId(current_id);\n variables.push_back(calib_imu_tg);\n current_id += calib_imu_tg->getSize();\n }\n\n if (options.imu_model == ImuModel::KALIBR)\n {\n calib_imu_gyr->setLocalId(current_id);\n variables.push_back(calib_imu_gyr);\n current_id += calib_imu_gyr->getSize();\n }\n else\n {\n calib_imu_acc->setLocalId(current_id);\n variables.push_back(calib_imu_acc);\n current_id += calib_imu_acc->getSize();\n }\n }\n\n calib_dt_imu_cam = std::make_shared(1);\n\n if (options.do_calib_camera_timeoffset)\n {\n calib_dt_imu_cam->setLocalId(current_id);\n variables.push_back(calib_dt_imu_cam);\n current_id += calib_dt_imu_cam->getSize();\n }\n\n for (int i = 0; i < 2; i++)\n {\n auto pose = std::make_shared();\n auto intrin = std::make_shared(8);\n\n calib_cam_imu.insert({i, pose});\n cam_intrinsics.insert({i, intrin});\n\n if (options.do_calib_camera_pose)\n {\n pose->setLocalId(current_id);\n variables.push_back(pose);\n current_id += pose->getSize();\n }\n\n if (options.do_calib_camera_intrinsics)\n {\n intrin->setLocalId(current_id);\n variables.push_back(intrin);\n current_id += intrin->getSize();\n }\n }\n\n cov = std::pow(1e-3, 2) * Eigen::MatrixXd::Identity(current_id, current_id);\n\n if (options.do_calib_imu_intrinsics)\n {\n cov.block(calib_imu_dw->getId(), calib_imu_dw->getId(), 6, 6) = std::pow(0.005, 2) * Eigen::Matrix::Identity();\n cov.block(calib_imu_da->getId(), calib_imu_da->getId(), 6, 6) = std::pow(0.008, 2) * Eigen::Matrix::Identity();\n\n if (options.do_calib_imu_g_sensitivity)\n cov.block(calib_imu_tg->getId(), calib_imu_tg->getId(), 9, 9) = std::pow(0.005, 2) * Eigen::Matrix::Identity();\n\n if (options.imu_model == ImuModel::KALIBR)\n cov.block(calib_imu_gyr->getId(), calib_imu_gyr->getId(), 3, 3) = std::pow(0.005, 2) * Eigen::Matrix3d::Identity();\n else\n cov.block(calib_imu_acc->getId(), calib_imu_acc->getId(), 3, 3) = std::pow(0.005, 2) * Eigen::Matrix3d::Identity();\n }\n\n if (options.do_calib_camera_timeoffset)\n cov(calib_dt_imu_cam->getId(), calib_dt_imu_cam->getId()) = std::pow(0.01, 2);\n\n if (options.do_calib_camera_pose)\n {\n for (int i = 0; i < 2; i++)\n {\n cov.block(calib_cam_imu.at(i)->getId(), calib_cam_imu.at(i)->getId(), 3, 3) = std::pow(0.005, 2) * Eigen::MatrixXd::Identity(3, 3);\n cov.block(calib_cam_imu.at(i)->getId() + 3, calib_cam_imu.at(i)->getId() + 3, 3, 3) = std::pow(0.015, 2) * Eigen::MatrixXd::Identity(3, 3);\n }\n }\n\n if (options.do_calib_camera_intrinsics)\n {\n for (int i = 0; i < 2; i++) {\n cov.block(cam_intrinsics.at(i)->getId(), cam_intrinsics.at(i)->getId(), 4, 4) = std::pow(1.0, 2) * Eigen::MatrixXd::Identity(4, 4);\n cov.block(cam_intrinsics.at(i)->getId() + 4, cam_intrinsics.at(i)->getId() + 4, 4, 4) = std::pow(0.005, 2) * Eigen::MatrixXd::Identity(4, 4);\n }\n }\n}\n double margtimestep() {\n\tdouble time = INFINITY;\n\n\tfor (const auto &clone_imu : clones_imu)\n\t{\n\t\tif (clone_imu.first < time)\n\t\t{\n\t\t\ttime = clone_imu.first;\n\t\t}\n\t}\n\n return time;\n}\n int maxCovSize() {\n\treturn (int)cov.rows();\n}\n static Eigen::Matrix3d Dm(ImuModel imu_model_, const Eigen::MatrixXd &vec_temp) {\n\tassert(vec_temp.rows() == 6);\n\tassert(vec_temp.cols() == 1);\n\tEigen::Matrix3d D_matrix = Eigen::Matrix3d::Identity();\n\t\n\tif (imu_model_ == ImuModel::KALIBR)\n\t\tD_matrix << vec_temp(0), 0, 0, vec_temp(1), vec_temp(3), 0, vec_temp(2), vec_temp(4), vec_temp(5);\n\telse\n\t\tD_matrix << vec_temp(0), vec_temp(1), vec_temp(3), 0, vec_temp(2), vec_temp(4), 0, 0, vec_temp(5);\n\n\treturn D_matrix;\n}\n static Eigen::Matrix3d Tg(const Eigen::MatrixXd &vec_temp) {\n\tassert(vec_temp.rows() == 9);\n\tassert(vec_temp.cols() == 1);\n\tEigen::Matrix3d Tg = Eigen::Matrix3d::Zero();\n\n\tTg << vec_temp(0), vec_temp(3), vec_temp(6), vec_temp(1), vec_temp(4), vec_temp(7), vec_temp(2), vec_temp(5), vec_temp(8);\n\t\n\treturn Tg;\n}\n int imuIntrinsicSize() {\n\tint size_temp = 0;\n\n\tif (options.do_calib_imu_intrinsics)\n\t{\n\t\tsize_temp += 15;\n\n\t\tif (options.do_calib_imu_g_sensitivity)\n\t\t\tsize_temp += 9;\n\t}\n\t\n\treturn size_temp;\n}\n double timestamp = -1;\n stateOptions options;\n std::shared_ptr imu_ptr;\n std::map> clones_imu;\n std::map> clones_frame;\n std::unordered_map> map_points;\n std::shared_ptr calib_dt_imu_cam;\n std::unordered_map> calib_cam_imu;\n std::unordered_map> cam_intrinsics;\n std::unordered_map> cam_intrinsics_cameras;\n std::shared_ptr calib_imu_dw;\n std::shared_ptr calib_imu_da;\n std::shared_ptr calib_imu_tg;\n std::shared_ptr calib_imu_gyr;\n std::shared_ptr calib_imu_acc;\n private:\n friend class stateHelper;\n Eigen::MatrixXd cov;\n std::vector> variables;\n};"], ["/voxel_svio/include/updaterHelper.h", "class baseType {\n public:\n struct updaterHelperFeature\n\t{\n\t\tsize_t feature_id;\n\n\t\tstd::unordered_map> uvs;\n\n\t\tstd::unordered_map> uvs_norm;\n\n\t\tstd::unordered_map> timestamps;\n\n\t\tstd::unordered_map>> frames;\n\n\t\tstd::unordered_map> color;\n\n\t\tint anchor_cam_id = -1;\n\n\t\tdouble anchor_clone_timestamp = -1;\n\n\t\tEigen::Vector3d position_anchor;\n\n\t\tEigen::Vector3d position_anchor_fej;\n\n\t\tEigen::Vector3d position_global;\n\n\t\tEigen::Vector3d position_global_fej;\n\t};\n static void getFeatureJacobianRepresentation(std::shared_ptr state_ptr, updaterHelperFeature &feature, Eigen::MatrixXd &H_f,\n\t\tstd::vector &H_x, std::vector> &x_order) {\n\tH_f.resize(3, 3);\n\tH_f.setIdentity();\n\treturn;\n}\n static void getFeatureJacobianFull(std::shared_ptr state_ptr, updaterHelperFeature &feature, Eigen::MatrixXd &H_f, \n\t\tEigen::MatrixXd &H_x, Eigen::VectorXd &res, std::vector> &x_order) {\n\tint total_meas = 0;\n\tfor (auto const &pair : feature.timestamps)\n\t{\n\t\ttotal_meas += (int)pair.second.size();\n\t}\n\n\tint total_hx = 0;\n\tstd::unordered_map, size_t> map_hx;\n\tfor (auto const &pair : feature.timestamps)\n\t{\n\t\tstd::shared_ptr calibration = state_ptr->calib_cam_imu.at(pair.first);\n\t\tstd::shared_ptr distortion = state_ptr->cam_intrinsics.at(pair.first);\n\n\t\tif (state_ptr->options.do_calib_camera_pose)\n\t\t{\n\t\t\tmap_hx.insert({calibration, total_hx});\n\t\t\tx_order.push_back(calibration);\n\t\t\ttotal_hx += calibration->getSize();\n\t\t}\n\n\t\tif (state_ptr->options.do_calib_camera_intrinsics)\n\t\t{\n\t\t\tmap_hx.insert({distortion, total_hx});\n\t\t\tx_order.push_back(distortion);\n\t\t\ttotal_hx += distortion->getSize();\n\t\t}\n\n\t\tfor (size_t m = 0; m < feature.timestamps[pair.first].size(); m++)\n\t\t{\n\t\t\tstd::shared_ptr clone_Ci = state_ptr->clones_imu.at(feature.timestamps[pair.first].at(m));\n\n\t\t\tif (map_hx.find(clone_Ci) == map_hx.end())\n\t\t\t{\n\t\t\t\tmap_hx.insert({clone_Ci, total_hx});\n\t\t\t\tx_order.push_back(clone_Ci);\n\t\t\t\ttotal_hx += clone_Ci->getSize();\n\t\t\t}\n\t\t}\n\t}\n\n\tEigen::Vector3d p_FinG = feature.position_global;\n\tEigen::Vector3d p_FinG_fej = feature.position_global_fej;\n\n\tint c = 0;\n\tint jacobsize = 3;\n\n\tres = Eigen::VectorXd::Zero(2 * total_meas);\n\tH_f = Eigen::MatrixXd::Zero(2 * total_meas, jacobsize);\n\tH_x = Eigen::MatrixXd::Zero(2 * total_meas, total_hx);\n\n\tEigen::MatrixXd dpfg_dlambda;\n\tstd::vector dpfg_dx;\n\tstd::vector> dpfg_dx_order;\n\tupdaterHelper::getFeatureJacobianRepresentation(state_ptr, feature, dpfg_dlambda, dpfg_dx, dpfg_dx_order);\n\n\tfor (auto const &pair : feature.timestamps)\n\t{\n\t\tstd::shared_ptr distortion = state_ptr->cam_intrinsics.at(pair.first);\n\t\tstd::shared_ptr calibration = state_ptr->calib_cam_imu.at(pair.first);\n\t\tEigen::Matrix3d R_ItoC = calibration->getRot();\n\t\tEigen::Vector3d p_IinC = calibration->getPos();\n\n\t\tfor (size_t m = 0; m < feature.timestamps[pair.first].size(); m++)\n\t\t{\n\t\t\tstd::shared_ptr clone_Ii = state_ptr->clones_imu.at(feature.timestamps[pair.first].at(m));\n\t\t\tEigen::Matrix3d R_GtoIi = clone_Ii->getRot();\n\t\t\tEigen::Vector3d p_IiinG = clone_Ii->getPos();\n\n\t\t\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\n\t\t\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\t\t\tEigen::Vector2d uv_norm;\n\t\t\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\t\t\tEigen::Vector2d uv_dist;\n\t\t\tuv_dist = state_ptr->cam_intrinsics_cameras.at(pair.first)->distortD(uv_norm);\n\n\t\t\tEigen::Vector2d uv_m;\n\t\t\tuv_m << (double)feature.uvs[pair.first].at(m)(0), (double)feature.uvs[pair.first].at(m)(1);\n\t\t\tres.block(2 * c, 0, 2, 1) = uv_m - uv_dist;\n\n\t\t\tif (state_ptr->options.do_fej)\n\t\t\t{\n\t\t\t\tR_GtoIi = clone_Ii->getRotFej();\n\t\t\t\tp_IiinG = clone_Ii->getPosFej();\n\n\t\t\t\tp_FinIi = R_GtoIi * (p_FinG_fej - p_IiinG);\n\t\t\t\tp_FinCi = R_ItoC * p_FinIi + p_IinC;\n\t\t\t}\n\n\t\t\tEigen::MatrixXd dz_dzn, dz_dzeta;\n\t\t\tstate_ptr->cam_intrinsics_cameras.at(pair.first)->computeDistortJacobian(uv_norm, dz_dzn, dz_dzeta);\n\n\t\t\tEigen::MatrixXd dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\t\tdzn_dpfc << 1 / p_FinCi(2), 0, -p_FinCi(0) / (p_FinCi(2) * p_FinCi(2)), 0, 1 / p_FinCi(2), -p_FinCi(1) / (p_FinCi(2) * p_FinCi(2));\n\n\t\t\tEigen::MatrixXd dpfc_dpfg = R_ItoC * R_GtoIi;\n\n\t\t\tEigen::MatrixXd dpfc_dclone = Eigen::MatrixXd::Zero(3, 6);\n\t\t\tdpfc_dclone.block(0, 0, 3, 3).noalias() = R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tdpfc_dclone.block(0, 3, 3, 3) = - dpfc_dpfg;\n\n\t\t\tEigen::MatrixXd dz_dpfc = dz_dzn * dzn_dpfc;\n\t\t\tEigen::MatrixXd dz_dpfg = dz_dpfc * dpfc_dpfg;\n\n\t\t\tH_f.block(2 * c, 0, 2, H_f.cols()).noalias() = dz_dpfg * dpfg_dlambda;\n\t\t\tH_x.block(2 * c, map_hx[clone_Ii], 2, clone_Ii->getSize()).noalias() = dz_dpfc * dpfc_dclone;\n\n\t\t\tfor (size_t i = 0; i < dpfg_dx_order.size(); i++)\n\t\t\t{\n\t\t\t\tH_x.block(2 * c, map_hx[dpfg_dx_order.at(i)], 2, dpfg_dx_order.at(i)->getSize()).noalias() += dz_dpfg * dpfg_dx.at(i);\n\t\t\t}\n\n\t\t\tif (state_ptr->options.do_calib_camera_pose)\n\t\t\t{\n\t\t\t\tEigen::MatrixXd dpfc_dcalib = Eigen::MatrixXd::Zero(3, 6);\n\t\t\t\tdpfc_dcalib.block(0, 0, 3, 3) = quatType::skewSymmetric(p_FinCi - p_IinC);\n\t\t\t\tdpfc_dcalib.block(0, 3, 3, 3) = Eigen::Matrix::Identity();\n\n\t\t\t\tH_x.block(2 * c, map_hx[calibration], 2, calibration->getSize()).noalias() += dz_dpfc * dpfc_dcalib;\n\t\t\t}\n\n\t\t\tif (state_ptr->options.do_calib_camera_intrinsics)\n\t\t\t{\n\t\t\t\tH_x.block(2 * c, map_hx[distortion], 2, distortion->getSize()) = dz_dzeta;\n\t\t\t}\n\n\t\t\tif (state_ptr->options.use_huber)\n\t\t\t{\n\t\t\t\tdouble loss = res.block(2 * c, 0, 2, 1).norm();\n\t\t\t\tdouble hw = loss < setting_huber_th ? 1 : setting_huber_th / loss;\n\n\t\t\t\tif (hw < 1) hw = sqrt(hw);\n\n\t\t\t\tres.block(2 * c, 0, 2, 1) *= hw;\n\t\t\t\tH_x.block(2 * c, 0, 2, H_x.cols()) *= hw;\n\t\t\t\tH_f.block(2 * c, 0, 2, H_f.cols()) *= hw;\n\t\t\t}\n\n\t\t\tc++;\n\t\t}\n\t}\n}\n static void nullspaceProjectInplace(Eigen::MatrixXd &H_f, Eigen::MatrixXd &H_x, Eigen::VectorXd &res) {\n\tEigen::JacobiRotation Ho_GR_temp;\n \n\tfor (int n = 0; n < H_f.cols(); ++n)\n\t{\n\t\tfor (int m = (int)H_f.rows() - 1; m > n; m--)\n\t\t{\n\t\t\tHo_GR_temp.makeGivens(H_f(m - 1, n), H_f(m, n));\n\t\t\t(H_f.block(m - 1, n, 2, H_f.cols() - n)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t\t(H_x.block(m - 1, 0, 2, H_x.cols())).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t\t(res.block(m - 1, 0, 2, 1)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t}\n\t}\n\n\tH_x = H_x.block(H_f.cols(), 0, H_x.rows() - H_f.cols(), H_x.cols()).eval();\n\tres = res.block(H_f.cols(), 0, res.rows() - H_f.cols(), res.cols()).eval();\n\n\tassert(H_x.rows() == res.rows());\n}\n static void measurementCompressInplace(Eigen::MatrixXd &H_x, Eigen::VectorXd &res) {\n\tif (H_x.rows() <= H_x.cols())\n\t\treturn;\n\n\tEigen::JacobiRotation Ho_GR_temp;\n\tfor (int n = 0; n < H_x.cols(); n++)\n\t{\n\t\tfor (int m = (int)H_x.rows() - 1; m > n; m--)\n\t\t{\n\t\t\tHo_GR_temp.makeGivens(H_x(m - 1, n), H_x(m, n));\n\n\t\t\t(H_x.block(m - 1, n, 2, H_x.cols() - n)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t\t(res.block(m - 1, 0, 2, 1)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t}\n\t}\n\n\tint r = std::min(H_x.rows(), H_x.cols());\n\n\tassert(r <= H_x.rows());\n\tH_x.conservativeResize(r, H_x.cols());\n\tres.conservativeResize(r, res.cols());\n}\n};"], ["/voxel_svio/include/feature.h", "class state {\n public:\n size_t feature_id;\n bool to_delete;\n std::unordered_map> uvs;\n std::unordered_map> uvs_norm;\n std::unordered_map> timestamps;\n std::unordered_map>> frames;\n std::unordered_map> color;\n int start_frame_id;\n int start_cam_id;\n int num_tracking;\n int anchor_cam_id = -1;\n double anchor_clone_timestamp;\n Eigen::Vector3d position_anchor;\n Eigen::Vector3d position_global;\n void cleanOldMeasurements(const std::vector &valid_times) {\n\tfor (auto const &pair : timestamps)\n\t{\n\t\tassert(timestamps[pair.first].size() == uvs[pair.first].size());\n\t\tassert(timestamps[pair.first].size() == uvs_norm[pair.first].size());\n\n\t\tauto it1 = timestamps[pair.first].begin();\n\t\tauto it2 = uvs[pair.first].begin();\n\t\tauto it3 = uvs_norm[pair.first].begin();\n\t\tauto it4 = frames[pair.first].begin();\n\t\tauto it5 = color[pair.first].begin();\n\n\t\twhile (it1 != timestamps[pair.first].end())\n\t\t{\n\t\t\tif (std::find(valid_times.begin(), valid_times.end(), *it1) == valid_times.end())\n\t\t\t{\n\t\t\t\tit1 = timestamps[pair.first].erase(it1);\n\t\t\t\tit2 = uvs[pair.first].erase(it2);\n\t\t\t\tit3 = uvs_norm[pair.first].erase(it3);\n\n\t\t\t\tassert((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end());\n\n\t\t\t\tif ((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end())\n\t\t\t\t{\n\t\t\t\t\t(*it4)->v_feat_ptr.at(pair.first).erase(feature_id);\n\t\t\t\t}\n\n\t\t\t\tit4 = frames[pair.first].erase(it4);\n\t\t\t\tit5 = color[pair.first].erase(it5);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tit1++;\n\t\t\t\tit2++;\n\t\t\t\tit3++;\n\t\t\t\tit4++;\n\t\t\t\tit5++;\n\t\t\t}\n\t\t}\n\t}\n}\n void cleanInvalidMeasurements(const std::vector &invalid_times) {\n\tfor (auto const &pair : timestamps)\n\t{\n\t\tassert(timestamps[pair.first].size() == uvs[pair.first].size());\n\t\tassert(timestamps[pair.first].size() == uvs_norm[pair.first].size());\n\n\t\tauto it1 = timestamps[pair.first].begin();\n\t\tauto it2 = uvs[pair.first].begin();\n\t\tauto it3 = uvs_norm[pair.first].begin();\n\t\tauto it4 = frames[pair.first].begin();\n\t\tauto it5 = color[pair.first].begin();\n\n\t\twhile (it1 != timestamps[pair.first].end())\n\t\t{\n\t\t\tif (std::find(invalid_times.begin(), invalid_times.end(), *it1) != invalid_times.end())\n\t\t\t{\n\t\t\t\tit1 = timestamps[pair.first].erase(it1);\n\t\t\t\tit2 = uvs[pair.first].erase(it2);\n\t\t\t\tit3 = uvs_norm[pair.first].erase(it3);\n\n\t\t\t\tassert((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end());\n\n\t\t\t\tif ((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end())\n\t\t\t\t{\n\t\t\t\t\t(*it4)->v_feat_ptr.at(pair.first).erase(feature_id);\n\t\t\t\t}\n\n\t\t\t\tit4 = frames[pair.first].erase(it4);\n\t\t\t\tit5 = color[pair.first].erase(it5);\n\t\t\t\t// 2024.10.31 yzk\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tit1++;\n\t\t\t\tit2++;\n\t\t\t\tit3++;\n\t\t\t\tit4++;\n\t\t\t\tit5++;\n\t\t\t}\n\t\t}\n\t}\n}\n void cleanOlderMeasurements(double timestamp) {\n\tfor (auto const &pair : timestamps)\n\t{\n\t\tassert(timestamps[pair.first].size() == uvs[pair.first].size());\n\t\tassert(timestamps[pair.first].size() == uvs_norm[pair.first].size());\n\n\t\tauto it1 = timestamps[pair.first].begin();\n\t\tauto it2 = uvs[pair.first].begin();\n\t\tauto it3 = uvs_norm[pair.first].begin();\n\t\tauto it4 = frames[pair.first].begin();\n\t\tauto it5 = color[pair.first].begin();\n\n\t\twhile (it1 != timestamps[pair.first].end())\n\t\t{\n\t\t\tif (*it1 <= timestamp)\n\t\t\t{\n\t\t\t\tit1 = timestamps[pair.first].erase(it1);\n\t\t\t\tit2 = uvs[pair.first].erase(it2);\n\t\t\t\tit3 = uvs_norm[pair.first].erase(it3);\n\n\t\t\t\tassert((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end());\n\n\t\t\t\tif ((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end())\n\t\t\t\t{\n\t\t\t\t\t(*it4)->v_feat_ptr.at(pair.first).erase(feature_id);\n\t\t\t\t}\n\n\t\t\t\tit4 = frames[pair.first].erase(it4);\n\t\t\t\tit5 = color[pair.first].erase(it5);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tit1++;\n\t\t\t\tit2++;\n\t\t\t\tit3++;\n\t\t\t\tit4++;\n\t\t\t\tit5++;\n\t\t\t}\n\t\t}\n\t}\n}\n public:\n featureDatabase() {\n\tlast_track_num = 0;\n}\n std::shared_ptr getFeature(size_t id, bool remove = false) {\n\tif (features_id_lookup.find(id) != features_id_lookup.end())\n\t{\n\t\tstd::shared_ptr temp = features_id_lookup.at(id);\n\t\tif (remove)\n\t\t{\n\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t{\n\t\t\t\tfor (auto it_fh = temp->frames[i].begin(); it_fh != temp->frames[i].end(); it_fh++)\n\t\t\t\t{\n\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find(temp->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase(temp->feature_id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfeatures_id_lookup.erase(id);\n\t\t}\n\t\treturn temp;\n\t}\n\telse\n\t{\n\t\treturn nullptr;\n\t}\n}\n void updateFeature(std::shared_ptr fh, size_t id, double timestamp, size_t cam_id, float u, float v, float u_n, float v_n) {\n\tif (features_id_lookup.find(id) != features_id_lookup.end())\n\t{\n\t\tstd::shared_ptr feat = features_id_lookup.at(id);\n\n\t\tif (u > 1.1 && u < wG[0] - 3 && v > 1.1 && v < hG[0] - 3)\n\t\t{\n\t\t\tfeat->uvs[cam_id].push_back(Eigen::Vector2f(u, v));\n\t\t\tfeat->uvs_norm[cam_id].push_back(Eigen::Vector2f(u_n, v_n));\n\t\t\tfeat->timestamps[cam_id].push_back(timestamp);\n\t\t\tfeat->frames[cam_id].push_back(fh);\n\n\t\t\tfloat color_temp = cam_id == 0 ? feat->frames[cam_id].back()->dI_left[(int)u + (int)v * wG[0]][0] \n\t\t\t\t: feat->frames[cam_id].back()->dI_right[(int)u + (int)v * wG[0]][0];\n\n\t\t\tassert(color_temp >= 0);\n\n\t\t\tfeat->color[cam_id].push_back(color_temp);\n\t\t\tfeat->num_tracking = fh->frame_id - feat->start_frame_id;\n\t\t\tfh->v_feat_ptr[cam_id][feat->feature_id] = feat;\n\n\t\t\tlast_track_num++;\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif (u > 1.1 && u < wG[0] - 3 && v > 1.1 && v < hG[0] - 3)\n\t{\n\t\tstd::shared_ptr feat = std::make_shared();\n\t\tfeat->feature_id = id;\n\t\tfeat->uvs[cam_id].push_back(Eigen::Vector2f(u, v));\n\t\tfeat->uvs_norm[cam_id].push_back(Eigen::Vector2f(u_n, v_n));\n\t\tfeat->timestamps[cam_id].push_back(timestamp);\n\t\tfeat->frames[cam_id].push_back(fh);\n\n\t\tfloat color_temp = cam_id == 0 ? feat->frames[cam_id].back()->dI_left[(int)u + (int)v * wG[0]][0] \n\t\t\t: feat->frames[cam_id].back()->dI_right[(int)u + (int)v * wG[0]][0];\n\n\t\tassert(color_temp >= 0);\n\n\t\tfeat->color[cam_id].push_back(color_temp);\n\t\tfeat->start_frame_id = fh->frame_id;\n\t\tfeat->start_cam_id = cam_id;\n\t\tfeat->num_tracking = 0;\n\n\t\tfeatures_id_lookup[id] = feat;\n\n\t\tfh->v_feat_ptr[cam_id][feat->feature_id] = feat;\n\t}\n}\n std::vector> getOldFeatures(double timestamp, bool remove = false, bool skip_deleted = false) {\n\tstd::vector> feats_old;\n\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\t// Skip if already deleted\n\t\tif (skip_deleted && (*it).second->to_delete)\n\t\t{\n\t\t\tit++;\n\t\t\tcontinue;\n\t\t}\n\n\t\tbool has_newer_measurement = false;\n\t\tfor (auto const &pair : (*it).second->timestamps)\n\t\t{\n\t\t\thas_newer_measurement = (!pair.second.empty() && pair.second.at(pair.second.size() - 1) >= timestamp);\n\t\t\tif (has_newer_measurement)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (!has_newer_measurement)\n\t\t{\n\t\t\tfeats_old.push_back((*it).second);\n\n\t\t\tif (remove)\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t\t{\n\t\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfeatures_id_lookup.erase(it++);\n\t\t\t}\n\t\t\telse\n\t\t\t\tit++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n\n\treturn feats_old;\n}\n std::vector> getFeatures(double timestamp, bool remove = false, bool skip_deleted = false) {\n\tstd::vector> feats_has_timestamp;\n\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\tif (skip_deleted && (*it).second->to_delete)\n\t\t{\n\t\t\tit++;\n\t\t\tcontinue;\n\t\t}\n \n\t\tbool has_timestamp = false;\n\t\tfor (auto const &pair : (*it).second->timestamps)\n\t\t{\n\t\t\thas_timestamp = (std::find(pair.second.begin(), pair.second.end(), timestamp) != pair.second.end());\n\t\t\tif (has_timestamp)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (has_timestamp)\n\t\t{\n\t\t\tfeats_has_timestamp.push_back((*it).second);\n\n\t\t\tif (remove)\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t\t{\n\t\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfeatures_id_lookup.erase(it++);\n\t\t\t}\n\t\t\telse\n\t\t\t\tit++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n\n\treturn feats_has_timestamp;\n}\n void cleanUp() {\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\tif ((*it).second->to_delete)\n\t\t{\n\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t{\n\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t{\n\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfeatures_id_lookup.erase(it++);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n}\n void cleanUpOldmeasurements(double timestamp) {\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\t(*it).second->cleanOlderMeasurements(timestamp);\n\t\t\n\t\tint count_meas = 0;\n\n\t\tfor (const auto &pair : (*it).second->timestamps)\n\t\t{\n\t\t\tcount_meas += (int)(pair.second.size());\n\t\t}\n\n\t\tif (count_meas < 1)\n\t\t{\n\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t{\n\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t{\n\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfeatures_id_lookup.erase(it++);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n}\n size_t getSize() {\n\treturn features_id_lookup.size();\n}\n std::unordered_map> getInternalData() {\n\treturn features_id_lookup;\n}\n bool featureCheckParallax(int frame_count) {\n double parallax_sum = 0;\n int parallax_num = 0;\n\n if (frame_count < 2 || last_track_num < 20)\n {\n \tlast_track_num = 0;\n return true;\n }\n\n last_track_num = 0;\n\n for (auto it = features_id_lookup.begin(); it != features_id_lookup.end(); it++)\n\t{\n\t\tif ((*it).second->start_frame_id <= frame_count - 2)\n\t\t{\n\t\t\tif ((*it).second->start_cam_id == 0 && (*it).second->start_frame_id + int((*it).second->timestamps.at(0).size()) - 1 >= frame_count)\n\t\t\t{\n\t\t\t\tparallax_sum += compensatedParallax2((*it).first, 0, frame_count);\n \tparallax_num++;\n\t\t\t}\n\t\t\telse if ((*it).second->start_cam_id == 1 && (*it).second->start_frame_id + int((*it).second->timestamps.at(1).size()) - 1 >= frame_count)\n\t\t\t{\n\t\t\t\tparallax_sum += compensatedParallax2((*it).first, 1, frame_count);\n \tparallax_num++;\n\t\t\t}\n\t\t}\n\t}\n\n if (parallax_num == 0)\n {\n return true;\n }\n else\n {\n return parallax_sum / parallax_num >= setting_min_parallax;\n }\n}\n double compensatedParallax2(int feat_id, int cam_id, int frame_count) {\n\tstd::shared_ptr feat_ptr = features_id_lookup.at(feat_id);\n\n\tEigen::Vector2f uvs_norm_i = feat_ptr->uvs_norm.at(cam_id).at(frame_count - 2 - feat_ptr->start_frame_id);\n\tEigen::Vector2f uvs_norm_j = feat_ptr->uvs_norm.at(cam_id).at(frame_count - 1 - feat_ptr->start_frame_id);\n\n float ans = 0;\n float du = uvs_norm_i(0) - uvs_norm_j(0);\n float dv = uvs_norm_i(1) - uvs_norm_j(1);\n\n ans = std::max(ans, sqrt(du * du + dv * dv));\n\n return ans;\n}\n protected:\n std::unordered_map> features_id_lookup;\n int last_track_num;\n public:\n struct clonePose\n {\n Eigen::Matrix rotation;\n\n Eigen::Matrix position;\n\n clonePose(const Eigen::Matrix &R, const Eigen::Matrix &p)\n {\n rotation = R;\n position = p;\n }\n\n clonePose(const Eigen::Matrix &q, const Eigen::Matrix &p)\n {\n rotation = quatType::quatToRot(q);\n position = p;\n }\n\n clonePose()\n {\n rotation = Eigen::Matrix::Identity();\n position = Eigen::Matrix::Zero();\n }\n\n const Eigen::Matrix &Rot() { return rotation; }\n\n const Eigen::Matrix &pos() { return position; }\n };\n featureInitializer(featureInitializerOptions &options_) {\n\n}\n bool singleTriangulation(std::shared_ptr feat, std::unordered_map> &clones_cam) {\n\tint total_meas = 0;\n\tsize_t anchor_most_meas = 0;\n\tsize_t most_meas = 0;\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\t\n\t\ttotal_meas += (int)pair.second.size();\n\t\t\n\t\tif (pair.second.size() > most_meas)\n \t{\n\t\t\tanchor_most_meas = pair.first;\n\t\t\tmost_meas = pair.second.size();\n\t\t}\n\t}\n\n\tfeat->anchor_cam_id = anchor_most_meas;\n\tfeat->anchor_clone_timestamp = feat->timestamps.at(feat->anchor_cam_id).back();\n\n\tEigen::Matrix3d A = Eigen::Matrix3d::Zero();\n\tEigen::Vector3d b = Eigen::Vector3d::Zero();\n\n\tclonePose anchor_clone = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp);\n\tconst Eigen::Matrix &R_GtoA = anchor_clone.Rot();\n\tconst Eigen::Matrix &p_AinG = anchor_clone.pos();\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\n\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t{\n\t\t\tconst Eigen::Matrix &R_GtoCi = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).Rot();\n\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).pos();\n\n\t\t\tEigen::Matrix R_AtoCi;\n\t\t\tR_AtoCi.noalias() = R_GtoCi * R_GtoA.transpose();\n\t\t\tEigen::Matrix p_CiinA;\n\t\t\tp_CiinA.noalias() = R_GtoA * (p_CiinG - p_AinG);\n\n\t\t\tEigen::Matrix b_i;\n\t\t\tb_i << feat->uvs_norm.at(pair.first).at(m)(0), feat->uvs_norm.at(pair.first).at(m)(1), 1;\n\t\t\tb_i = R_AtoCi.transpose() * b_i;\n\t\t\tb_i = b_i / b_i.norm();\n\t\t\tEigen::Matrix3d B_perp = quatType::skewSymmetric(b_i);\n\n\t\t\tEigen::Matrix3d Ai = B_perp.transpose() * B_perp;\n\t\t\tA += Ai;\n\t\t\tb += Ai * p_CiinA;\n\t\t}\n\t}\n\n\tEigen::MatrixXd p_f = A.colPivHouseholderQr().solve(b);\n\n\tEigen::JacobiSVD svd(A);\n\tEigen::MatrixXd singular_values;\n\tsingular_values.resize(svd.singularValues().rows(), 1);\n\tsingular_values = svd.singularValues();\n\tdouble cond_A = singular_values(0, 0) / singular_values(singular_values.rows() - 1, 0);\n\n\tif (std::abs(cond_A) > options.max_cond_number || p_f(2, 0) < options.min_dist || p_f(2, 0) > options.max_dist || std::isnan(p_f.norm()))\n\t{\n\t\treturn false;\n\t}\n\n\tfeat->position_anchor = p_f;\n\tfeat->position_global = R_GtoA.transpose() * feat->position_anchor + p_AinG;\n\n\treturn true;\n}\n bool singleGaussnewton(std::shared_ptr feat, std::unordered_map> &clones_cam) {\n\tdouble rho = 1 / feat->position_anchor(2);\n\tdouble alpha = feat->position_anchor(0) / feat->position_anchor(2);\n\tdouble beta = feat->position_anchor(1) / feat->position_anchor(2);\n\n\tdouble lam = options.init_lamda;\n\tdouble eps = 10000;\n\tint runs = 0;\n\n\tbool recompute = true;\n\tEigen::Matrix Hess = Eigen::Matrix::Zero();\n\tEigen::Matrix grad = Eigen::Matrix::Zero();\n\n\tdouble cost_old = computeError(clones_cam, feat, alpha, beta, rho);\n\n\tconst Eigen::Matrix &R_GtoA = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).Rot();\n\tconst Eigen::Matrix &p_AinG = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).pos();\n\n\twhile (runs < options.max_runs && lam < options.max_lamda && eps > options.min_dx)\n\t{\n\t\tif (recompute)\n\t\t{\n\t\t\tHess.setZero();\n\t\t\tgrad.setZero();\n\n\t\t\tdouble err = 0;\n\n\t\t\tfor (auto const &pair : feat->timestamps)\n\t\t\t{\n\t\t\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t\t\t{\n\t\t\t\t\tconst Eigen::Matrix &R_GtoCi = clones_cam.at(pair.first).at(feat->timestamps[pair.first].at(m)).Rot();\n\t\t\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps[pair.first].at(m)).pos();\n\n\t\t\t\t\tEigen::Matrix R_AtoCi;\n\t\t\t\t\tR_AtoCi.noalias() = R_GtoCi * R_GtoA.transpose();\n\t\t\t\t\tEigen::Matrix p_CiinA;\n\t\t\t\t\tp_CiinA.noalias() = R_GtoA * (p_CiinG - p_AinG);\n\t\t\t\t\tEigen::Matrix p_AinCi;\n\t\t\t\t\tp_AinCi.noalias() = -R_AtoCi * p_CiinA;\n\n\t\t\t\t\tdouble hi1 = R_AtoCi(0, 0) * alpha + R_AtoCi(0, 1) * beta + R_AtoCi(0, 2) + rho * p_AinCi(0, 0);\n\t\t\t\t\tdouble hi2 = R_AtoCi(1, 0) * alpha + R_AtoCi(1, 1) * beta + R_AtoCi(1, 2) + rho * p_AinCi(1, 0);\n\t\t\t\t\tdouble hi3 = R_AtoCi(2, 0) * alpha + R_AtoCi(2, 1) * beta + R_AtoCi(2, 2) + rho * p_AinCi(2, 0);\n\n\t\t\t\t\tdouble d_z1_d_alpha = (R_AtoCi(0, 0) * hi3 - hi1 * R_AtoCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z1_d_beta = (R_AtoCi(0, 1) * hi3 - hi1 * R_AtoCi(2, 1)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z1_d_rho = (p_AinCi(0, 0) * hi3 - hi1 * p_AinCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z2_d_alpha = (R_AtoCi(1, 0) * hi3 - hi2 * R_AtoCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z2_d_beta = (R_AtoCi(1, 1) * hi3 - hi2 * R_AtoCi(2, 1)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z2_d_rho = (p_AinCi(1, 0) * hi3 - hi2 * p_AinCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tEigen::Matrix H;\n\t\t\t\t\tH << d_z1_d_alpha, d_z1_d_beta, d_z1_d_rho, d_z2_d_alpha, d_z2_d_beta, d_z2_d_rho;\n\n\t\t\t\t\tEigen::Matrix z;\n\t\t\t\t\tz << hi1 / hi3, hi2 / hi3;\n\t\t\t\t\tEigen::Matrix res = feat->uvs_norm.at(pair.first).at(m) - z;\n\n\t\t\t\t\terr += std::pow(res.norm(), 2);\n\t\t\t\t\tgrad.noalias() += H.transpose() * res.cast();\n\t\t\t\t\tHess.noalias() += H.transpose() * H;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tEigen::Matrix Hess_l = Hess;\n\n\t\tfor (size_t r = 0; r < (size_t)Hess.rows(); r++)\n\t\t{\n\t\t\tHess_l(r, r) *= (1.0 + lam);\n\t\t}\n\n\t\tEigen::Matrix dx = Hess_l.colPivHouseholderQr().solve(grad);\n\n\t\tdouble cost = computeError(clones_cam, feat, alpha + dx(0, 0), beta + dx(1, 0), rho + dx(2, 0));\n\n\t\tif (cost <= cost_old && (cost_old - cost) / cost_old < options.min_dcost)\n\t\t{\n\t\t\talpha += dx(0, 0);\n\t\t\tbeta += dx(1, 0);\n\t\t\trho += dx(2, 0);\n\t\t\teps = 0;\n\t\t\tbreak;\n\t\t}\n\n\t\tif (cost <= cost_old)\n\t\t{\n\t\t\trecompute = true;\n\t\t\tcost_old = cost;\n\t\t\talpha += dx(0, 0);\n\t\t\tbeta += dx(1, 0);\n\t\t\trho += dx(2, 0);\n\t\t\truns++;\n\t\t\tlam = lam / options.lam_mult;\n\t\t\teps = dx.norm();\n\t\t}\n\t\telse\n\t\t{\n\t\t\trecompute = false;\n\t\t\tlam = lam * options.lam_mult;\n\t\t\tcontinue;\n\t\t}\n\t}\n\n\tfeat->position_anchor(0) = alpha / rho;\n\tfeat->position_anchor(1) = beta / rho;\n\tfeat->position_anchor(2) = 1 / rho;\n\n\tEigen::HouseholderQR qr(feat->position_anchor);\n\tEigen::MatrixXd Q = qr.householderQ();\n\n\tdouble base_line_max = 0.0;\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\n\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t{\n\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).pos();\n\n\t\t\tEigen::Matrix p_CiinA = R_GtoA * (p_CiinG - p_AinG);\n\n\t\t\tdouble base_line = ((Q.block(0, 1, 3, 2)).transpose() * p_CiinA).norm();\n\n\t\t\tif (base_line > base_line_max)\n\t\t\t\tbase_line_max = base_line;\n\t\t}\n\t}\n\n\tif (feat->position_anchor(2) < options.min_dist || feat->position_anchor(2) > options.max_dist ||\n\t\t(feat->position_anchor.norm() / base_line_max) > options.max_baseline || std::isnan(feat->position_anchor.norm()))\n\t{\n\t\treturn false;\n\t}\n\n\tfeat->position_global = R_GtoA.transpose() * feat->position_anchor + p_AinG;\n\t\n\treturn true;\n}\n protected:\n featureInitializerOptions options;\n double computeError(std::unordered_map> &clones_cam, std::shared_ptr feat,\n double alpha, double beta, double rho) {\n\tdouble err = 0;\n\n\tconst Eigen::Matrix &R_GtoA = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).Rot();\n\tconst Eigen::Matrix &p_AinG = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).pos();\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\n\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t{\n\t\t\tconst Eigen::Matrix &R_GtoCi = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).Rot();\n\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).pos();\n \n\t\t\tEigen::Matrix R_AtoCi;\n\t\t\tR_AtoCi.noalias() = R_GtoCi * R_GtoA.transpose();\n\t\t\tEigen::Matrix p_CiinA;\n\t\t\tp_CiinA.noalias() = R_GtoA * (p_CiinG - p_AinG);\n\t\t\tEigen::Matrix p_AinCi;\n\t\t\tp_AinCi.noalias() = -R_AtoCi * p_CiinA;\n\n\t\t\tdouble hi1 = R_AtoCi(0, 0) * alpha + R_AtoCi(0, 1) * beta + R_AtoCi(0, 2) + rho * p_AinCi(0, 0);\n\t\t\tdouble hi2 = R_AtoCi(1, 0) * alpha + R_AtoCi(1, 1) * beta + R_AtoCi(1, 2) + rho * p_AinCi(1, 0);\n\t\t\tdouble hi3 = R_AtoCi(2, 0) * alpha + R_AtoCi(2, 1) * beta + R_AtoCi(2, 2) + rho * p_AinCi(2, 0);\n \n\t\t\tEigen::Matrix z;\n\t\t\tz << hi1 / hi3, hi2 / hi3;\n\t\t\tEigen::Matrix res = feat->uvs_norm.at(pair.first).at(m) - z;\n\n\t\t\terr += pow(res.norm(), 2);\n\t\t}\n\t}\n\n\treturn err;\n}\n};"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_hash.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_HASH_H\n#define TSL_ROBIN_HASH_H \n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \"robin_growth_policy.h\"\n\n\nnamespace tsl {\n \nnamespace detail_robin_hash {\n\ntemplate\nstruct make_void {\n using type = void;\n};\n\ntemplate\nstruct has_is_transparent: std::false_type {\n};\n\ntemplate\nstruct has_is_transparent::type>: std::true_type {\n};\n\ntemplate\nstruct is_power_of_two_policy: std::false_type {\n};\n\ntemplate\nstruct is_power_of_two_policy>: std::true_type {\n};\n\n// Only available in C++17, we need to be compatible with C++11\ntemplate\nconst T& clamp( const T& v, const T& lo, const T& hi) {\n return std::min(hi, std::max(lo, v));\n}\n\ntemplate\nstatic T numeric_cast(U value, const char* error_message = \"numeric_cast() failed.\") {\n T ret = static_cast(value);\n if(static_cast(ret) != value) {\n TSL_RH_THROW_OR_TERMINATE(std::runtime_error, error_message);\n }\n \n const bool is_same_signedness = (std::is_unsigned::value && std::is_unsigned::value) ||\n (std::is_signed::value && std::is_signed::value);\n if(!is_same_signedness && (ret < T{}) != (value < U{})) {\n TSL_RH_THROW_OR_TERMINATE(std::runtime_error, error_message);\n }\n \n return ret;\n}\n\n\nusing truncated_hash_type = std::uint_least32_t;\n\n/**\n * Helper class that stores a truncated hash if StoreHash is true and nothing otherwise.\n */\ntemplate\nclass bucket_entry_hash {\npublic:\n bool bucket_hash_equal(std::size_t /*hash*/) const noexcept {\n return true;\n }\n \n truncated_hash_type truncated_hash() const noexcept {\n return 0;\n }\n \nprotected:\n void set_hash(truncated_hash_type /*hash*/) noexcept {\n }\n};\n\ntemplate<>\nclass bucket_entry_hash {\npublic:\n bool bucket_hash_equal(std::size_t hash) const noexcept {\n return m_hash == truncated_hash_type(hash);\n }\n \n truncated_hash_type truncated_hash() const noexcept {\n return m_hash;\n }\n \nprotected:\n void set_hash(truncated_hash_type hash) noexcept {\n m_hash = truncated_hash_type(hash);\n }\n \nprivate: \n truncated_hash_type m_hash;\n};\n\n\n/**\n * Each bucket entry has:\n * - A value of type `ValueType`.\n * - An integer to store how far the value of the bucket, if any, is from its ideal bucket \n * (ex: if the current bucket 5 has the value 'foo' and `hash('foo') % nb_buckets` == 3,\n * `dist_from_ideal_bucket()` will return 2 as the current value of the bucket is two\n * buckets away from its ideal bucket)\n * If there is no value in the bucket (i.e. `empty()` is true) `dist_from_ideal_bucket()` will be < 0.\n * - A marker which tells us if the bucket is the last bucket of the bucket array (useful for the \n * iterator of the hash table).\n * - If `StoreHash` is true, 32 bits of the hash of the value, if any, are also stored in the bucket. \n * If the size of the hash is more than 32 bits, it is truncated. We don't store the full hash\n * as storing the hash is a potential opportunity to use the unused space due to the alignment\n * of the bucket_entry structure. We can thus potentially store the hash without any extra space \n * (which would not be possible with 64 bits of the hash).\n */\ntemplate\nclass bucket_entry: public bucket_entry_hash {\n using bucket_hash = bucket_entry_hash;\n \npublic:\n using value_type = ValueType;\n using distance_type = std::int_least16_t;\n \n \n bucket_entry() noexcept: bucket_hash(), m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),\n m_last_bucket(false)\n {\n tsl_rh_assert(empty());\n }\n \n bucket_entry(bool last_bucket) noexcept: bucket_hash(), m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),\n m_last_bucket(last_bucket)\n {\n tsl_rh_assert(empty());\n }\n \n bucket_entry(const bucket_entry& other) noexcept(std::is_nothrow_copy_constructible::value): \n bucket_hash(other),\n m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET), \n m_last_bucket(other.m_last_bucket)\n {\n if(!other.empty()) {\n ::new (static_cast(std::addressof(m_value))) value_type(other.value());\n m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;\n }\n }\n \n /**\n * Never really used, but still necessary as we must call resize on an empty `std::vector`.\n * and we need to support move-only types. See robin_hash constructor for details.\n */\n bucket_entry(bucket_entry&& other) noexcept(std::is_nothrow_move_constructible::value): \n bucket_hash(std::move(other)),\n m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET), \n m_last_bucket(other.m_last_bucket) \n {\n if(!other.empty()) {\n ::new (static_cast(std::addressof(m_value))) value_type(std::move(other.value()));\n m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;\n }\n }\n \n bucket_entry& operator=(const bucket_entry& other) \n noexcept(std::is_nothrow_copy_constructible::value) \n {\n if(this != &other) {\n clear();\n \n bucket_hash::operator=(other);\n if(!other.empty()) {\n ::new (static_cast(std::addressof(m_value))) value_type(other.value());\n }\n \n m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;\n m_last_bucket = other.m_last_bucket;\n }\n \n return *this;\n }\n \n bucket_entry& operator=(bucket_entry&& ) = delete;\n \n ~bucket_entry() noexcept {\n clear();\n }\n \n void clear() noexcept {\n if(!empty()) {\n destroy_value();\n m_dist_from_ideal_bucket = EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET;\n }\n }\n \n bool empty() const noexcept {\n return m_dist_from_ideal_bucket == EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET;\n }\n \n value_type& value() noexcept {\n tsl_rh_assert(!empty());\n return *reinterpret_cast(std::addressof(m_value));\n }\n \n const value_type& value() const noexcept {\n tsl_rh_assert(!empty());\n return *reinterpret_cast(std::addressof(m_value));\n }\n \n distance_type dist_from_ideal_bucket() const noexcept {\n return m_dist_from_ideal_bucket;\n }\n \n bool last_bucket() const noexcept {\n return m_last_bucket;\n }\n \n void set_as_last_bucket() noexcept {\n m_last_bucket = true;\n }\n \n template\n void set_value_of_empty_bucket(distance_type dist_from_ideal_bucket, \n truncated_hash_type hash, Args&&... value_type_args) \n {\n tsl_rh_assert(dist_from_ideal_bucket >= 0);\n tsl_rh_assert(empty());\n \n ::new (static_cast(std::addressof(m_value))) value_type(std::forward(value_type_args)...);\n this->set_hash(hash);\n m_dist_from_ideal_bucket = dist_from_ideal_bucket;\n \n tsl_rh_assert(!empty());\n }\n \n void swap_with_value_in_bucket(distance_type& dist_from_ideal_bucket, \n truncated_hash_type& hash, value_type& value) \n {\n tsl_rh_assert(!empty());\n \n using std::swap;\n swap(value, this->value());\n swap(dist_from_ideal_bucket, m_dist_from_ideal_bucket);\n \n // Avoid warning of unused variable if StoreHash is false\n (void) hash;\n if(StoreHash) {\n const truncated_hash_type tmp_hash = this->truncated_hash();\n this->set_hash(hash);\n hash = tmp_hash;\n }\n }\n \n static truncated_hash_type truncate_hash(std::size_t hash) noexcept {\n return truncated_hash_type(hash);\n }\n \nprivate:\n void destroy_value() noexcept {\n tsl_rh_assert(!empty());\n value().~value_type();\n }\n\npublic:\n static const distance_type DIST_FROM_IDEAL_BUCKET_LIMIT = 4096;\n static_assert(DIST_FROM_IDEAL_BUCKET_LIMIT <= std::numeric_limits::max() - 1,\n \"DIST_FROM_IDEAL_BUCKET_LIMIT must be <= std::numeric_limits::max() - 1.\");\n \nprivate:\n using storage = typename std::aligned_storage::type;\n \n static const distance_type EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET = -1;\n \n distance_type m_dist_from_ideal_bucket;\n bool m_last_bucket;\n storage m_value;\n};\n\n\n\n/**\n * Internal common class used by `robin_map` and `robin_set`. \n * \n * ValueType is what will be stored by `robin_hash` (usually `std::pair` for map and `Key` for set).\n * \n * `KeySelect` should be a `FunctionObject` which takes a `ValueType` in parameter and returns a \n * reference to the key.\n * \n * `ValueSelect` should be a `FunctionObject` which takes a `ValueType` in parameter and returns a \n * reference to the value. `ValueSelect` should be void if there is no value (in a set for example).\n * \n * The strong exception guarantee only holds if the expression \n * `std::is_nothrow_swappable::value && std::is_nothrow_move_constructible::value` is true.\n * \n * Behaviour is undefined if the destructor of `ValueType` throws.\n */\ntemplate\nclass robin_hash: private Hash, private KeyEqual, private GrowthPolicy {\nprivate: \n template\n using has_mapped_type = typename std::integral_constant::value>;\n \n static_assert(noexcept(std::declval().bucket_for_hash(std::size_t(0))), \"GrowthPolicy::bucket_for_hash must be noexcept.\");\n static_assert(noexcept(std::declval().clear()), \"GrowthPolicy::clear must be noexcept.\");\n \npublic:\n template\n class robin_iterator;\n \n using key_type = typename KeySelect::key_type;\n using value_type = ValueType;\n using size_type = std::size_t;\n using difference_type = std::ptrdiff_t;\n using hasher = Hash;\n using key_equal = KeyEqual;\n using allocator_type = Allocator;\n using reference = value_type&;\n using const_reference = const value_type&;\n using pointer = value_type*;\n using const_pointer = const value_type*;\n using iterator = robin_iterator;\n using const_iterator = robin_iterator;\n \n \nprivate:\n /**\n * Either store the hash because we are asked by the `StoreHash` template parameter\n * or store the hash because it doesn't cost us anything in size and can be used to speed up rehash.\n */\n static constexpr bool STORE_HASH = StoreHash || \n (\n (sizeof(tsl::detail_robin_hash::bucket_entry) ==\n sizeof(tsl::detail_robin_hash::bucket_entry))\n &&\n (sizeof(std::size_t) == sizeof(truncated_hash_type) ||\n is_power_of_two_policy::value)\n &&\n // Don't store the hash for primitive types with default hash.\n (!std::is_arithmetic::value ||\n !std::is_same>::value)\n );\n \n /**\n * Only use the stored hash on lookup if we are explicitly asked. We are not sure how slow\n * the KeyEqual operation is. An extra comparison may slow things down with a fast KeyEqual.\n */\n static constexpr bool USE_STORED_HASH_ON_LOOKUP = StoreHash;\n\n /**\n * We can only use the hash on rehash if the size of the hash type is the same as the stored one or\n * if we use a power of two modulo. In the case of the power of two modulo, we just mask\n * the least significant bytes, we just have to check that the truncated_hash_type didn't truncated\n * more bytes.\n */\n static bool USE_STORED_HASH_ON_REHASH(size_type bucket_count) {\n (void) bucket_count;\n if(STORE_HASH && sizeof(std::size_t) == sizeof(truncated_hash_type)) {\n return true;\n }\n else if(STORE_HASH && is_power_of_two_policy::value) {\n tsl_rh_assert(bucket_count > 0);\n return (bucket_count - 1) <= std::numeric_limits::max();\n }\n else {\n return false; \n }\n }\n \n using bucket_entry = tsl::detail_robin_hash::bucket_entry;\n using distance_type = typename bucket_entry::distance_type;\n \n using buckets_allocator = typename std::allocator_traits::template rebind_alloc;\n using buckets_container_type = std::vector;\n \n \npublic: \n /**\n * The 'operator*()' and 'operator->()' methods return a const reference and const pointer respectively to the \n * stored value type.\n * \n * In case of a map, to get a mutable reference to the value associated to a key (the '.second' in the \n * stored pair), you have to call 'value()'. \n * \n * The main reason for this is that if we returned a `std::pair&` instead \n * of a `const std::pair&`, the user may modify the key which will put the map in a undefined state.\n */\n template\n class robin_iterator {\n friend class robin_hash;\n \n private:\n using bucket_entry_ptr = typename std::conditional::type;\n \n \n robin_iterator(bucket_entry_ptr bucket) noexcept: m_bucket(bucket) {\n }\n \n public:\n using iterator_category = std::forward_iterator_tag;\n using value_type = const typename robin_hash::value_type;\n using difference_type = std::ptrdiff_t;\n using reference = value_type&;\n using pointer = value_type*;\n \n \n robin_iterator() noexcept {\n }\n \n // Copy constructor from iterator to const_iterator.\n template::type* = nullptr>\n robin_iterator(const robin_iterator& other) noexcept: m_bucket(other.m_bucket) {\n }\n \n robin_iterator(const robin_iterator& other) = default;\n robin_iterator(robin_iterator&& other) = default;\n robin_iterator& operator=(const robin_iterator& other) = default;\n robin_iterator& operator=(robin_iterator&& other) = default;\n \n const typename robin_hash::key_type& key() const {\n return KeySelect()(m_bucket->value());\n }\n\n template::value && IsConst>::type* = nullptr>\n const typename U::value_type& value() const {\n return U()(m_bucket->value());\n }\n\n template::value && !IsConst>::type* = nullptr>\n typename U::value_type& value() const {\n return U()(m_bucket->value());\n }\n \n reference operator*() const {\n return m_bucket->value();\n }\n \n pointer operator->() const {\n return std::addressof(m_bucket->value());\n }\n \n robin_iterator& operator++() {\n while(true) {\n if(m_bucket->last_bucket()) {\n ++m_bucket;\n return *this;\n }\n \n ++m_bucket;\n if(!m_bucket->empty()) {\n return *this;\n }\n }\n }\n \n robin_iterator operator++(int) {\n robin_iterator tmp(*this);\n ++*this;\n \n return tmp;\n }\n \n friend bool operator==(const robin_iterator& lhs, const robin_iterator& rhs) { \n return lhs.m_bucket == rhs.m_bucket; \n }\n \n friend bool operator!=(const robin_iterator& lhs, const robin_iterator& rhs) { \n return !(lhs == rhs); \n }\n \n private:\n bucket_entry_ptr m_bucket;\n };\n\n \npublic:\n#if defined(__cplusplus) && __cplusplus >= 201402L\n robin_hash(size_type bucket_count, \n const Hash& hash,\n const KeyEqual& equal,\n const Allocator& alloc,\n float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,\n float max_load_factor = DEFAULT_MAX_LOAD_FACTOR): \n Hash(hash), \n KeyEqual(equal),\n GrowthPolicy(bucket_count),\n m_buckets_data(\n [&]() {\n if(bucket_count > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \n \"The map exceeds its maximum bucket count.\");\n }\n \n return bucket_count;\n }(), alloc\n ),\n m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),\n m_bucket_count(bucket_count),\n m_nb_elements(0), \n m_grow_on_next_insert(false),\n m_try_shrink_on_next_insert(false)\n {\n if(m_bucket_count > 0) {\n tsl_rh_assert(!m_buckets_data.empty());\n m_buckets_data.back().set_as_last_bucket();\n }\n \n this->min_load_factor(min_load_factor);\n this->max_load_factor(max_load_factor);\n }\n#else\n /**\n * C++11 doesn't support the creation of a std::vector with a custom allocator and 'count' default-inserted elements. \n * The needed contructor `explicit vector(size_type count, const Allocator& alloc = Allocator());` is only\n * available in C++14 and later. We thus must resize after using the `vector(const Allocator& alloc)` constructor.\n * \n * We can't use `vector(size_type count, const T& value, const Allocator& alloc)` as it requires the\n * value T to be copyable.\n */\n robin_hash(size_type bucket_count, \n const Hash& hash,\n const KeyEqual& equal,\n const Allocator& alloc,\n float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,\n float max_load_factor = DEFAULT_MAX_LOAD_FACTOR): \n Hash(hash), \n KeyEqual(equal),\n GrowthPolicy(bucket_count),\n m_buckets_data(alloc), \n m_buckets(static_empty_bucket_ptr()), \n m_bucket_count(bucket_count),\n m_nb_elements(0), \n m_grow_on_next_insert(false),\n m_try_shrink_on_next_insert(false)\n {\n if(bucket_count > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The map exceeds its maximum bucket count.\");\n }\n \n if(m_bucket_count > 0) {\n m_buckets_data.resize(m_bucket_count);\n m_buckets = m_buckets_data.data();\n \n tsl_rh_assert(!m_buckets_data.empty());\n m_buckets_data.back().set_as_last_bucket();\n }\n \n this->min_load_factor(min_load_factor);\n this->max_load_factor(max_load_factor);\n }\n#endif\n \n robin_hash(const robin_hash& other): Hash(other),\n KeyEqual(other),\n GrowthPolicy(other),\n m_buckets_data(other.m_buckets_data),\n m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),\n m_bucket_count(other.m_bucket_count),\n m_nb_elements(other.m_nb_elements),\n m_load_threshold(other.m_load_threshold),\n m_min_load_factor(other.m_min_load_factor),\n m_max_load_factor(other.m_max_load_factor),\n m_grow_on_next_insert(other.m_grow_on_next_insert),\n m_try_shrink_on_next_insert(other.m_try_shrink_on_next_insert)\n {\n }\n \n robin_hash(robin_hash&& other) noexcept(std::is_nothrow_move_constructible::value &&\n std::is_nothrow_move_constructible::value &&\n std::is_nothrow_move_constructible::value &&\n std::is_nothrow_move_constructible::value)\n : Hash(std::move(static_cast(other))),\n KeyEqual(std::move(static_cast(other))),\n GrowthPolicy(std::move(static_cast(other))),\n m_buckets_data(std::move(other.m_buckets_data)),\n m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),\n m_bucket_count(other.m_bucket_count),\n m_nb_elements(other.m_nb_elements),\n m_load_threshold(other.m_load_threshold),\n m_min_load_factor(other.m_min_load_factor),\n m_max_load_factor(other.m_max_load_factor),\n m_grow_on_next_insert(other.m_grow_on_next_insert),\n m_try_shrink_on_next_insert(other.m_try_shrink_on_next_insert)\n {\n other.clear_and_shrink();\n }\n \n robin_hash& operator=(const robin_hash& other) {\n if(&other != this) {\n Hash::operator=(other);\n KeyEqual::operator=(other);\n GrowthPolicy::operator=(other);\n \n m_buckets_data = other.m_buckets_data;\n m_buckets = m_buckets_data.empty()?static_empty_bucket_ptr():\n m_buckets_data.data();\n m_bucket_count = other.m_bucket_count;\n m_nb_elements = other.m_nb_elements;\n \n m_load_threshold = other.m_load_threshold;\n m_min_load_factor = other.m_min_load_factor;\n m_max_load_factor = other.m_max_load_factor;\n \n m_grow_on_next_insert = other.m_grow_on_next_insert;\n m_try_shrink_on_next_insert = other.m_try_shrink_on_next_insert;\n }\n \n return *this;\n }\n \n robin_hash& operator=(robin_hash&& other) {\n other.swap(*this);\n other.clear();\n \n return *this;\n }\n \n allocator_type get_allocator() const {\n return m_buckets_data.get_allocator();\n }\n \n \n /*\n * Iterators\n */\n iterator begin() noexcept {\n std::size_t i = 0;\n while(i < m_bucket_count && m_buckets[i].empty()) {\n i++;\n }\n \n return iterator(m_buckets + i);\n }\n \n const_iterator begin() const noexcept {\n return cbegin();\n }\n \n const_iterator cbegin() const noexcept {\n std::size_t i = 0;\n while(i < m_bucket_count && m_buckets[i].empty()) {\n i++;\n }\n \n return const_iterator(m_buckets + i);\n }\n \n iterator end() noexcept {\n return iterator(m_buckets + m_bucket_count);\n }\n \n const_iterator end() const noexcept {\n return cend();\n }\n \n const_iterator cend() const noexcept {\n return const_iterator(m_buckets + m_bucket_count);\n }\n \n \n /*\n * Capacity\n */\n bool empty() const noexcept {\n return m_nb_elements == 0;\n }\n \n size_type size() const noexcept {\n return m_nb_elements;\n }\n \n size_type max_size() const noexcept {\n return m_buckets_data.max_size();\n }\n \n /*\n * Modifiers\n */\n void clear() noexcept {\n if(m_min_load_factor > 0.0f) {\n clear_and_shrink();\n }\n else {\n for(auto& bucket: m_buckets_data) {\n bucket.clear();\n }\n \n m_nb_elements = 0;\n m_grow_on_next_insert = false;\n }\n }\n \n \n \n template\n std::pair insert(P&& value) {\n return insert_impl(KeySelect()(value), std::forward

(value));\n }\n \n template\n iterator insert_hint(const_iterator hint, P&& value) { \n if(hint != cend() && compare_keys(KeySelect()(*hint), KeySelect()(value))) { \n return mutable_iterator(hint); \n }\n \n return insert(std::forward

(value)).first; \n }\n \n template\n void insert(InputIt first, InputIt last) {\n if(std::is_base_of::iterator_category>::value) \n {\n const auto nb_elements_insert = std::distance(first, last);\n const size_type nb_free_buckets = m_load_threshold - size();\n tsl_rh_assert(m_load_threshold >= size());\n \n if(nb_elements_insert > 0 && nb_free_buckets < size_type(nb_elements_insert)) {\n reserve(size() + size_type(nb_elements_insert));\n }\n }\n \n for(; first != last; ++first) {\n insert(*first);\n }\n }\n \n \n \n template\n std::pair insert_or_assign(K&& key, M&& obj) { \n auto it = try_emplace(std::forward(key), std::forward(obj));\n if(!it.second) {\n it.first.value() = std::forward(obj);\n }\n \n return it;\n }\n \n template\n iterator insert_or_assign(const_iterator hint, K&& key, M&& obj) {\n if(hint != cend() && compare_keys(KeySelect()(*hint), key)) { \n auto it = mutable_iterator(hint); \n it.value() = std::forward(obj);\n \n return it;\n }\n \n return insert_or_assign(std::forward(key), std::forward(obj)).first;\n }\n\n \n template\n std::pair emplace(Args&&... args) {\n return insert(value_type(std::forward(args)...));\n }\n \n template\n iterator emplace_hint(const_iterator hint, Args&&... args) {\n return insert_hint(hint, value_type(std::forward(args)...)); \n }\n \n \n \n template\n std::pair try_emplace(K&& key, Args&&... args) {\n return insert_impl(key, std::piecewise_construct, \n std::forward_as_tuple(std::forward(key)), \n std::forward_as_tuple(std::forward(args)...));\n }\n \n template\n iterator try_emplace_hint(const_iterator hint, K&& key, Args&&... args) { \n if(hint != cend() && compare_keys(KeySelect()(*hint), key)) { \n return mutable_iterator(hint); \n }\n \n return try_emplace(std::forward(key), std::forward(args)...).first;\n }\n \n /**\n * Here to avoid `template size_type erase(const K& key)` being used when\n * we use an `iterator` instead of a `const_iterator`.\n */\n iterator erase(iterator pos) {\n erase_from_bucket(pos);\n \n /**\n * Erase bucket used a backward shift after clearing the bucket.\n * Check if there is a new value in the bucket, if not get the next non-empty.\n */\n if(pos.m_bucket->empty()) {\n ++pos;\n }\n \n m_try_shrink_on_next_insert = true;\n \n return pos;\n }\n \n iterator erase(const_iterator pos) {\n return erase(mutable_iterator(pos));\n }\n \n iterator erase(const_iterator first, const_iterator last) {\n if(first == last) {\n return mutable_iterator(first);\n }\n \n auto first_mutable = mutable_iterator(first);\n auto last_mutable = mutable_iterator(last);\n for(auto it = first_mutable.m_bucket; it != last_mutable.m_bucket; ++it) {\n if(!it->empty()) {\n it->clear();\n m_nb_elements--;\n }\n }\n \n if(last_mutable == end()) {\n m_try_shrink_on_next_insert = true;\n return end();\n }\n \n \n /*\n * Backward shift on the values which come after the deleted values.\n * We try to move the values closer to their ideal bucket.\n */\n std::size_t icloser_bucket = static_cast(first_mutable.m_bucket - m_buckets);\n std::size_t ito_move_closer_value = static_cast(last_mutable.m_bucket - m_buckets);\n tsl_rh_assert(ito_move_closer_value > icloser_bucket);\n \n const std::size_t ireturn_bucket = ito_move_closer_value - \n std::min(ito_move_closer_value - icloser_bucket, \n std::size_t(m_buckets[ito_move_closer_value].dist_from_ideal_bucket()));\n \n while(ito_move_closer_value < m_bucket_count && m_buckets[ito_move_closer_value].dist_from_ideal_bucket() > 0) {\n icloser_bucket = ito_move_closer_value - \n std::min(ito_move_closer_value - icloser_bucket, \n std::size_t(m_buckets[ito_move_closer_value].dist_from_ideal_bucket()));\n \n \n tsl_rh_assert(m_buckets[icloser_bucket].empty());\n const distance_type new_distance = distance_type(m_buckets[ito_move_closer_value].dist_from_ideal_bucket() -\n (ito_move_closer_value - icloser_bucket));\n m_buckets[icloser_bucket].set_value_of_empty_bucket(new_distance, \n m_buckets[ito_move_closer_value].truncated_hash(), \n std::move(m_buckets[ito_move_closer_value].value()));\n m_buckets[ito_move_closer_value].clear();\n \n \n ++icloser_bucket;\n ++ito_move_closer_value;\n }\n \n m_try_shrink_on_next_insert = true;\n \n return iterator(m_buckets + ireturn_bucket);\n }\n \n \n template\n size_type erase(const K& key) {\n return erase(key, hash_key(key));\n }\n \n template\n size_type erase(const K& key, std::size_t hash) {\n auto it = find(key, hash);\n if(it != end()) {\n erase_from_bucket(it);\n m_try_shrink_on_next_insert = true;\n \n return 1;\n }\n else {\n return 0;\n }\n }\n \n \n \n \n \n void swap(robin_hash& other) {\n using std::swap;\n \n swap(static_cast(*this), static_cast(other));\n swap(static_cast(*this), static_cast(other));\n swap(static_cast(*this), static_cast(other));\n swap(m_buckets_data, other.m_buckets_data);\n swap(m_buckets, other.m_buckets);\n swap(m_bucket_count, other.m_bucket_count);\n swap(m_nb_elements, other.m_nb_elements);\n swap(m_load_threshold, other.m_load_threshold);\n swap(m_min_load_factor, other.m_min_load_factor);\n swap(m_max_load_factor, other.m_max_load_factor);\n swap(m_grow_on_next_insert, other.m_grow_on_next_insert);\n swap(m_try_shrink_on_next_insert, other.m_try_shrink_on_next_insert);\n }\n \n \n /*\n * Lookup\n */\n template::value>::type* = nullptr>\n typename U::value_type& at(const K& key) {\n return at(key, hash_key(key));\n }\n \n template::value>::type* = nullptr>\n typename U::value_type& at(const K& key, std::size_t hash) {\n return const_cast(static_cast(this)->at(key, hash));\n }\n \n \n template::value>::type* = nullptr>\n const typename U::value_type& at(const K& key) const {\n return at(key, hash_key(key));\n }\n \n template::value>::type* = nullptr>\n const typename U::value_type& at(const K& key, std::size_t hash) const {\n auto it = find(key, hash);\n if(it != cend()) {\n return it.value();\n }\n else {\n TSL_RH_THROW_OR_TERMINATE(std::out_of_range, \"Couldn't find key.\");\n }\n }\n \n template::value>::type* = nullptr>\n typename U::value_type& operator[](K&& key) {\n return try_emplace(std::forward(key)).first.value();\n }\n \n \n template\n size_type count(const K& key) const {\n return count(key, hash_key(key));\n }\n \n template\n size_type count(const K& key, std::size_t hash) const {\n if(find(key, hash) != cend()) {\n return 1;\n }\n else {\n return 0;\n }\n }\n \n \n template\n iterator find(const K& key) {\n return find_impl(key, hash_key(key));\n }\n \n template\n iterator find(const K& key, std::size_t hash) {\n return find_impl(key, hash);\n }\n \n \n template\n const_iterator find(const K& key) const {\n return find_impl(key, hash_key(key));\n }\n \n template\n const_iterator find(const K& key, std::size_t hash) const {\n return find_impl(key, hash);\n }\n \n \n template\n bool contains(const K& key) const {\n return contains(key, hash_key(key));\n }\n \n template\n bool contains(const K& key, std::size_t hash) const {\n return count(key, hash) != 0;\n }\n \n \n template\n std::pair equal_range(const K& key) {\n return equal_range(key, hash_key(key));\n }\n \n template\n std::pair equal_range(const K& key, std::size_t hash) {\n iterator it = find(key, hash);\n return std::make_pair(it, (it == end())?it:std::next(it));\n }\n \n \n template\n std::pair equal_range(const K& key) const {\n return equal_range(key, hash_key(key));\n }\n \n template\n std::pair equal_range(const K& key, std::size_t hash) const {\n const_iterator it = find(key, hash);\n return std::make_pair(it, (it == cend())?it:std::next(it));\n }\n \n /*\n * Bucket interface \n */\n size_type bucket_count() const {\n return m_bucket_count; \n }\n \n size_type max_bucket_count() const {\n return std::min(GrowthPolicy::max_bucket_count(), m_buckets_data.max_size());\n }\n \n /*\n * Hash policy \n */\n float load_factor() const {\n if(bucket_count() == 0) {\n return 0;\n }\n \n return float(m_nb_elements)/float(bucket_count());\n }\n \n float min_load_factor() const {\n return m_min_load_factor;\n }\n \n float max_load_factor() const {\n return m_max_load_factor;\n }\n \n void min_load_factor(float ml) {\n m_min_load_factor = clamp(ml, float(MINIMUM_MIN_LOAD_FACTOR), \n float(MAXIMUM_MIN_LOAD_FACTOR));\n }\n \n void max_load_factor(float ml) {\n m_max_load_factor = clamp(ml, float(MINIMUM_MAX_LOAD_FACTOR), \n float(MAXIMUM_MAX_LOAD_FACTOR));\n m_load_threshold = size_type(float(bucket_count())*m_max_load_factor);\n }\n \n void rehash(size_type count) {\n count = std::max(count, size_type(std::ceil(float(size())/max_load_factor())));\n rehash_impl(count);\n }\n \n void reserve(size_type count) {\n rehash(size_type(std::ceil(float(count)/max_load_factor())));\n } \n \n /*\n * Observers\n */\n hasher hash_function() const {\n return static_cast(*this);\n }\n \n key_equal key_eq() const {\n return static_cast(*this);\n }\n \n \n /*\n * Other\n */ \n iterator mutable_iterator(const_iterator pos) {\n return iterator(const_cast(pos.m_bucket));\n }\n \nprivate:\n template\n std::size_t hash_key(const K& key) const {\n return Hash::operator()(key);\n }\n \n template\n bool compare_keys(const K1& key1, const K2& key2) const {\n return KeyEqual::operator()(key1, key2);\n }\n \n std::size_t bucket_for_hash(std::size_t hash) const {\n const std::size_t bucket = GrowthPolicy::bucket_for_hash(hash);\n tsl_rh_assert(bucket < m_bucket_count || (bucket == 0 && m_bucket_count == 0));\n \n return bucket;\n }\n \n template::value>::type* = nullptr>\n std::size_t next_bucket(std::size_t index) const noexcept {\n tsl_rh_assert(index < bucket_count());\n \n return (index + 1) & this->m_mask;\n }\n \n template::value>::type* = nullptr>\n std::size_t next_bucket(std::size_t index) const noexcept {\n tsl_rh_assert(index < bucket_count());\n \n index++;\n return (index != bucket_count())?index:0;\n }\n \n \n \n template\n iterator find_impl(const K& key, std::size_t hash) {\n return mutable_iterator(static_cast(this)->find(key, hash));\n }\n \n template\n const_iterator find_impl(const K& key, std::size_t hash) const {\n std::size_t ibucket = bucket_for_hash(hash); \n distance_type dist_from_ideal_bucket = 0;\n \n while(dist_from_ideal_bucket <= m_buckets[ibucket].dist_from_ideal_bucket()) {\n if(TSL_RH_LIKELY((!USE_STORED_HASH_ON_LOOKUP || m_buckets[ibucket].bucket_hash_equal(hash)) && \n compare_keys(KeySelect()(m_buckets[ibucket].value()), key))) \n {\n return const_iterator(m_buckets + ibucket);\n }\n \n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n \n return cend();\n }\n \n void erase_from_bucket(iterator pos) {\n pos.m_bucket->clear();\n m_nb_elements--;\n \n /**\n * Backward shift, swap the empty bucket, previous_ibucket, with the values on its right, ibucket,\n * until we cross another empty bucket or if the other bucket has a distance_from_ideal_bucket == 0.\n * \n * We try to move the values closer to their ideal bucket.\n */\n std::size_t previous_ibucket = static_cast(pos.m_bucket - m_buckets);\n std::size_t ibucket = next_bucket(previous_ibucket);\n \n while(m_buckets[ibucket].dist_from_ideal_bucket() > 0) {\n tsl_rh_assert(m_buckets[previous_ibucket].empty());\n \n const distance_type new_distance = distance_type(m_buckets[ibucket].dist_from_ideal_bucket() - 1);\n m_buckets[previous_ibucket].set_value_of_empty_bucket(new_distance, m_buckets[ibucket].truncated_hash(), \n std::move(m_buckets[ibucket].value()));\n m_buckets[ibucket].clear();\n\n previous_ibucket = ibucket;\n ibucket = next_bucket(ibucket);\n }\n }\n \n template\n std::pair insert_impl(const K& key, Args&&... value_type_args) {\n const std::size_t hash = hash_key(key);\n \n std::size_t ibucket = bucket_for_hash(hash); \n distance_type dist_from_ideal_bucket = 0;\n \n while(dist_from_ideal_bucket <= m_buckets[ibucket].dist_from_ideal_bucket()) {\n if((!USE_STORED_HASH_ON_LOOKUP || m_buckets[ibucket].bucket_hash_equal(hash)) &&\n compare_keys(KeySelect()(m_buckets[ibucket].value()), key)) \n {\n return std::make_pair(iterator(m_buckets + ibucket), false);\n }\n \n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n \n if(rehash_on_extreme_load()) {\n ibucket = bucket_for_hash(hash);\n dist_from_ideal_bucket = 0;\n \n while(dist_from_ideal_bucket <= m_buckets[ibucket].dist_from_ideal_bucket()) {\n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n }\n \n \n if(m_buckets[ibucket].empty()) {\n m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket, bucket_entry::truncate_hash(hash),\n std::forward(value_type_args)...);\n }\n else {\n insert_value(ibucket, dist_from_ideal_bucket, bucket_entry::truncate_hash(hash), \n std::forward(value_type_args)...);\n }\n \n \n m_nb_elements++;\n /*\n * The value will be inserted in ibucket in any case, either because it was\n * empty or by stealing the bucket (robin hood). \n */\n return std::make_pair(iterator(m_buckets + ibucket), true);\n }\n \n \n template\n void insert_value(std::size_t ibucket, distance_type dist_from_ideal_bucket, \n truncated_hash_type hash, Args&&... value_type_args) \n {\n value_type value(std::forward(value_type_args)...);\n insert_value_impl(ibucket, dist_from_ideal_bucket, hash, value);\n }\n\n void insert_value(std::size_t ibucket, distance_type dist_from_ideal_bucket,\n truncated_hash_type hash, value_type&& value)\n {\n insert_value_impl(ibucket, dist_from_ideal_bucket, hash, value);\n }\n\n /*\n * We don't use `value_type&& value` as last argument due to a bug in MSVC when `value_type` is a pointer,\n * The compiler is not able to see the difference between `std::string*` and `std::string*&&` resulting in \n * a compilation error.\n * \n * The `value` will be in a moved state at the end of the function.\n */\n void insert_value_impl(std::size_t ibucket, distance_type dist_from_ideal_bucket,\n truncated_hash_type hash, value_type& value)\n {\n m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket, hash, value);\n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n \n while(!m_buckets[ibucket].empty()) {\n if(dist_from_ideal_bucket > m_buckets[ibucket].dist_from_ideal_bucket()) {\n if(dist_from_ideal_bucket >= bucket_entry::DIST_FROM_IDEAL_BUCKET_LIMIT) {\n /**\n * The number of probes is really high, rehash the map on the next insert.\n * Difficult to do now as rehash may throw an exception.\n */\n m_grow_on_next_insert = true;\n }\n \n m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket, hash, value);\n }\n \n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n \n m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket, hash, std::move(value));\n }\n \n \n void rehash_impl(size_type count) {\n robin_hash new_table(count, static_cast(*this), static_cast(*this), \n get_allocator(), m_min_load_factor, m_max_load_factor);\n \n const bool use_stored_hash = USE_STORED_HASH_ON_REHASH(new_table.bucket_count());\n for(auto& bucket: m_buckets_data) {\n if(bucket.empty()) { \n continue; \n }\n \n const std::size_t hash = use_stored_hash?bucket.truncated_hash():\n new_table.hash_key(KeySelect()(bucket.value()));\n \n new_table.insert_value_on_rehash(new_table.bucket_for_hash(hash), 0, \n bucket_entry::truncate_hash(hash), std::move(bucket.value()));\n }\n \n new_table.m_nb_elements = m_nb_elements;\n new_table.swap(*this);\n }\n \n void clear_and_shrink() noexcept {\n GrowthPolicy::clear();\n m_buckets_data.clear();\n m_buckets = static_empty_bucket_ptr();\n m_bucket_count = 0;\n m_nb_elements = 0;\n m_load_threshold = 0;\n m_grow_on_next_insert = false;\n m_try_shrink_on_next_insert = false;\n }\n \n void insert_value_on_rehash(std::size_t ibucket, distance_type dist_from_ideal_bucket, \n truncated_hash_type hash, value_type&& value) \n {\n while(true) {\n if(dist_from_ideal_bucket > m_buckets[ibucket].dist_from_ideal_bucket()) {\n if(m_buckets[ibucket].empty()) {\n m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket, hash, std::move(value));\n return;\n }\n else {\n m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket, hash, value);\n }\n }\n \n dist_from_ideal_bucket++;\n ibucket = next_bucket(ibucket);\n }\n }\n \n \n \n /**\n * Grow the table if m_grow_on_next_insert is true or we reached the max_load_factor.\n * Shrink the table if m_try_shrink_on_next_insert is true (an erase occurred) and\n * we're below the min_load_factor.\n * \n * Return true if the table has been rehashed.\n */\n bool rehash_on_extreme_load() {\n if(m_grow_on_next_insert || size() >= m_load_threshold) {\n rehash_impl(GrowthPolicy::next_bucket_count());\n m_grow_on_next_insert = false;\n \n return true;\n }\n \n if(m_try_shrink_on_next_insert) {\n m_try_shrink_on_next_insert = false;\n if(m_min_load_factor != 0.0f && load_factor() < m_min_load_factor) {\n reserve(size() + 1);\n \n return true;\n }\n }\n \n return false;\n }\n\n \npublic:\n static const size_type DEFAULT_INIT_BUCKETS_SIZE = 0;\n \n static constexpr float DEFAULT_MAX_LOAD_FACTOR = 0.5f;\n static constexpr float MINIMUM_MAX_LOAD_FACTOR = 0.2f;\n static constexpr float MAXIMUM_MAX_LOAD_FACTOR = 0.95f;\n \n static constexpr float DEFAULT_MIN_LOAD_FACTOR = 0.0f;\n static constexpr float MINIMUM_MIN_LOAD_FACTOR = 0.0f;\n static constexpr float MAXIMUM_MIN_LOAD_FACTOR = 0.15f;\n \n static_assert(MINIMUM_MAX_LOAD_FACTOR < MAXIMUM_MAX_LOAD_FACTOR, \n \"MINIMUM_MAX_LOAD_FACTOR should be < MAXIMUM_MAX_LOAD_FACTOR\");\n static_assert(MINIMUM_MIN_LOAD_FACTOR < MAXIMUM_MIN_LOAD_FACTOR, \n \"MINIMUM_MIN_LOAD_FACTOR should be < MAXIMUM_MIN_LOAD_FACTOR\");\n static_assert(MAXIMUM_MIN_LOAD_FACTOR < MINIMUM_MAX_LOAD_FACTOR, \n \"MAXIMUM_MIN_LOAD_FACTOR should be < MINIMUM_MAX_LOAD_FACTOR\");\n \nprivate:\n /**\n * Return an always valid pointer to an static empty bucket_entry with last_bucket() == true.\n */ \n bucket_entry* static_empty_bucket_ptr() noexcept {\n static bucket_entry empty_bucket(true);\n return &empty_bucket;\n }\n \nprivate:\n buckets_container_type m_buckets_data;\n \n /**\n * Points to m_buckets_data.data() if !m_buckets_data.empty() otherwise points to static_empty_bucket_ptr.\n * This variable is useful to avoid the cost of checking if m_buckets_data is empty when trying \n * to find an element.\n * \n * TODO Remove m_buckets_data and only use a pointer instead of a pointer+vector to save some space in the robin_hash object.\n * Manage the Allocator manually.\n */\n bucket_entry* m_buckets;\n \n /**\n * Used a lot in find, avoid the call to m_buckets_data.size() which is a bit slower.\n */\n size_type m_bucket_count;\n \n size_type m_nb_elements;\n \n size_type m_load_threshold;\n \n float m_min_load_factor;\n float m_max_load_factor;\n \n bool m_grow_on_next_insert;\n \n /**\n * We can't shrink down the map on erase operations as the erase methods need to return the next iterator.\n * Shrinking the map would invalidate all the iterators and we could not return the next iterator in a meaningful way,\n * On erase, we thus just indicate on erase that we should try to shrink the hash table on the next insert\n * if we go below the min_load_factor. \n */\n bool m_try_shrink_on_next_insert;\n};\n\n}\n\n}\n\n#endif\n"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_growth_policy.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_GROWTH_POLICY_H\n#define TSL_ROBIN_GROWTH_POLICY_H \n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n\n#ifdef TSL_DEBUG\n# define tsl_rh_assert(expr) assert(expr)\n#else\n# define tsl_rh_assert(expr) (static_cast(0))\n#endif\n\n\n/**\n * If exceptions are enabled, throw the exception passed in parameter, otherwise call std::terminate.\n */\n#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (defined (_MSC_VER) && defined (_CPPUNWIND))) && !defined(TSL_NO_EXCEPTIONS)\n# define TSL_RH_THROW_OR_TERMINATE(ex, msg) throw ex(msg)\n#else\n# define TSL_RH_NO_EXCEPTIONS\n# ifdef NDEBUG\n# define TSL_RH_THROW_OR_TERMINATE(ex, msg) std::terminate()\n# else\n# include \n# define TSL_RH_THROW_OR_TERMINATE(ex, msg) do { std::cerr << msg << std::endl; std::terminate(); } while(0)\n# endif\n#endif\n\n\n#if defined(__GNUC__) || defined(__clang__)\n# define TSL_RH_LIKELY(exp) (__builtin_expect(!!(exp), true))\n#else\n# define TSL_RH_LIKELY(exp) (exp)\n#endif\n\n\nnamespace tsl {\nnamespace rh {\n \n/**\n * Grow the hash table by a factor of GrowthFactor keeping the bucket count to a power of two. It allows\n * the table to use a mask operation instead of a modulo operation to map a hash to a bucket.\n * \n * GrowthFactor must be a power of two >= 2.\n */\ntemplate\nclass power_of_two_growth_policy {\npublic:\n /**\n * Called on the hash table creation and on rehash. The number of buckets for the table is passed in parameter.\n * This number is a minimum, the policy may update this value with a higher value if needed (but not lower).\n *\n * If 0 is given, min_bucket_count_in_out must still be 0 after the policy creation and\n * bucket_for_hash must always return 0 in this case.\n */\n explicit power_of_two_growth_policy(std::size_t& min_bucket_count_in_out) {\n if(min_bucket_count_in_out > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n if(min_bucket_count_in_out > 0) {\n min_bucket_count_in_out = round_up_to_power_of_two(min_bucket_count_in_out);\n m_mask = min_bucket_count_in_out - 1;\n }\n else {\n m_mask = 0;\n }\n }\n \n /**\n * Return the bucket [0, bucket_count()) to which the hash belongs. \n * If bucket_count() is 0, it must always return 0.\n */\n std::size_t bucket_for_hash(std::size_t hash) const noexcept {\n return hash & m_mask;\n }\n \n /**\n * Return the number of buckets that should be used on next growth.\n */\n std::size_t next_bucket_count() const {\n if((m_mask + 1) > max_bucket_count() / GrowthFactor) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n return (m_mask + 1) * GrowthFactor;\n }\n \n /**\n * Return the maximum number of buckets supported by the policy.\n */\n std::size_t max_bucket_count() const {\n // Largest power of two.\n return (std::numeric_limits::max() / 2) + 1;\n }\n \n /**\n * Reset the growth policy as if it was created with a bucket count of 0.\n * After a clear, the policy must always return 0 when bucket_for_hash is called.\n */\n void clear() noexcept {\n m_mask = 0;\n }\n \nprivate:\n static std::size_t round_up_to_power_of_two(std::size_t value) {\n if(is_power_of_two(value)) {\n return value;\n }\n \n if(value == 0) {\n return 1;\n }\n \n --value;\n for(std::size_t i = 1; i < sizeof(std::size_t) * CHAR_BIT; i *= 2) {\n value |= value >> i;\n }\n \n return value + 1;\n }\n \n static constexpr bool is_power_of_two(std::size_t value) {\n return value != 0 && (value & (value - 1)) == 0;\n }\n \nprotected:\n static_assert(is_power_of_two(GrowthFactor) && GrowthFactor >= 2, \"GrowthFactor must be a power of two >= 2.\");\n \n std::size_t m_mask;\n};\n\n\n/**\n * Grow the hash table by GrowthFactor::num / GrowthFactor::den and use a modulo to map a hash\n * to a bucket. Slower but it can be useful if you want a slower growth.\n */\ntemplate>\nclass mod_growth_policy {\npublic:\n explicit mod_growth_policy(std::size_t& min_bucket_count_in_out) {\n if(min_bucket_count_in_out > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n if(min_bucket_count_in_out > 0) {\n m_mod = min_bucket_count_in_out;\n }\n else {\n m_mod = 1;\n }\n }\n \n std::size_t bucket_for_hash(std::size_t hash) const noexcept {\n return hash % m_mod;\n }\n \n std::size_t next_bucket_count() const {\n if(m_mod == max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n const double next_bucket_count = std::ceil(double(m_mod) * REHASH_SIZE_MULTIPLICATION_FACTOR);\n if(!std::isnormal(next_bucket_count)) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n if(next_bucket_count > double(max_bucket_count())) {\n return max_bucket_count();\n }\n else {\n return std::size_t(next_bucket_count);\n }\n }\n \n std::size_t max_bucket_count() const {\n return MAX_BUCKET_COUNT;\n }\n \n void clear() noexcept {\n m_mod = 1;\n }\n \nprivate:\n static constexpr double REHASH_SIZE_MULTIPLICATION_FACTOR = 1.0 * GrowthFactor::num / GrowthFactor::den;\n static const std::size_t MAX_BUCKET_COUNT = \n std::size_t(double(\n std::numeric_limits::max() / REHASH_SIZE_MULTIPLICATION_FACTOR\n ));\n \n static_assert(REHASH_SIZE_MULTIPLICATION_FACTOR >= 1.1, \"Growth factor should be >= 1.1.\");\n \n std::size_t m_mod;\n};\n\n\n\nnamespace detail {\n\n#if SIZE_MAX >= ULLONG_MAX\n#define TSL_RH_NB_PRIMES 51\n#elif SIZE_MAX >= ULONG_MAX\n#define TSL_RH_NB_PRIMES 40\n#else\n#define TSL_RH_NB_PRIMES 23\n#endif\n\nstatic constexpr const std::array PRIMES = {{\n 1u, 5u, 17u, 29u, 37u, 53u, 67u, 79u, 97u, 131u, 193u, 257u, 389u, 521u, 769u, 1031u, \n 1543u, 2053u, 3079u, 6151u, 12289u, 24593u, 49157u,\n#if SIZE_MAX >= ULONG_MAX\n 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul, \n 25165843ul, 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul, \n 3221225473ul, 4294967291ul,\n#endif\n#if SIZE_MAX >= ULLONG_MAX\n 6442450939ull, 12884901893ull, 25769803751ull, 51539607551ull, 103079215111ull, 206158430209ull, \n 412316860441ull, 824633720831ull, 1649267441651ull, 3298534883309ull, 6597069766657ull,\n#endif\n}};\n\ntemplate\nstatic constexpr std::size_t mod(std::size_t hash) { return hash % PRIMES[IPrime]; }\n\n// MOD_PRIME[iprime](hash) returns hash % PRIMES[iprime]. This table allows for faster modulo as the\n// compiler can optimize the modulo code better with a constant known at the compilation.\nstatic constexpr const std::array MOD_PRIME = {{ \n &mod<0>, &mod<1>, &mod<2>, &mod<3>, &mod<4>, &mod<5>, &mod<6>, &mod<7>, &mod<8>, &mod<9>, &mod<10>, \n &mod<11>, &mod<12>, &mod<13>, &mod<14>, &mod<15>, &mod<16>, &mod<17>, &mod<18>, &mod<19>, &mod<20>, \n &mod<21>, &mod<22>, \n#if SIZE_MAX >= ULONG_MAX\n &mod<23>, &mod<24>, &mod<25>, &mod<26>, &mod<27>, &mod<28>, &mod<29>, &mod<30>, &mod<31>, &mod<32>, \n &mod<33>, &mod<34>, &mod<35>, &mod<36>, &mod<37> , &mod<38>, &mod<39>,\n#endif\n#if SIZE_MAX >= ULLONG_MAX\n &mod<40>, &mod<41>, &mod<42>, &mod<43>, &mod<44>, &mod<45>, &mod<46>, &mod<47>, &mod<48>, &mod<49>, \n &mod<50>,\n#endif\n}};\n\n}\n\n/**\n * Grow the hash table by using prime numbers as bucket count. Slower than tsl::rh::power_of_two_growth_policy in \n * general but will probably distribute the values around better in the buckets with a poor hash function.\n * \n * To allow the compiler to optimize the modulo operation, a lookup table is used with constant primes numbers.\n * \n * With a switch the code would look like:\n * \\code\n * switch(iprime) { // iprime is the current prime of the hash table\n * case 0: hash % 5ul;\n * break;\n * case 1: hash % 17ul;\n * break;\n * case 2: hash % 29ul;\n * break;\n * ...\n * } \n * \\endcode\n * \n * Due to the constant variable in the modulo the compiler is able to optimize the operation\n * by a series of multiplications, substractions and shifts. \n * \n * The 'hash % 5' could become something like 'hash - (hash * 0xCCCCCCCD) >> 34) * 5' in a 64 bits environment.\n */\nclass prime_growth_policy {\npublic:\n explicit prime_growth_policy(std::size_t& min_bucket_count_in_out) {\n auto it_prime = std::lower_bound(detail::PRIMES.begin(), \n detail::PRIMES.end(), min_bucket_count_in_out);\n if(it_prime == detail::PRIMES.end()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n m_iprime = static_cast(std::distance(detail::PRIMES.begin(), it_prime));\n if(min_bucket_count_in_out > 0) {\n min_bucket_count_in_out = *it_prime;\n }\n else {\n min_bucket_count_in_out = 0;\n }\n }\n \n std::size_t bucket_for_hash(std::size_t hash) const noexcept {\n return detail::MOD_PRIME[m_iprime](hash);\n }\n \n std::size_t next_bucket_count() const {\n if(m_iprime + 1 >= detail::PRIMES.size()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n return detail::PRIMES[m_iprime + 1];\n } \n \n std::size_t max_bucket_count() const {\n return detail::PRIMES.back();\n }\n \n void clear() noexcept {\n m_iprime = 0;\n }\n \nprivate:\n unsigned int m_iprime;\n \n static_assert(std::numeric_limits::max() >= detail::PRIMES.size(), \n \"The type of m_iprime is not big enough.\");\n}; \n\n}\n}\n\n#endif\n"], ["/voxel_svio/include/camera.h", "class cameraBase {\n public:\n cameraBase(int width, int height) {\n\n}\n virtual void setValue(const Eigen::MatrixXd &calib) {\n\tassert(calib.rows() == 8);\n\tcamera_values = calib;\n\n\tcv::Matx33d tempK;\n\ttempK(0, 0) = calib(0);\n\ttempK(0, 1) = 0;\n\ttempK(0, 2) = calib(2);\n\ttempK(1, 0) = 0;\n\ttempK(1, 1) = calib(1);\n\ttempK(1, 2) = calib(3);\n\ttempK(2, 0) = 0;\n\ttempK(2, 1) = 0;\n\ttempK(2, 2) = 1;\n\tcamera_k_OPENCV = tempK;\n\n\tcv::Vec4d tempD;\n\ttempD(0) = calib(4);\n\ttempD(1) = calib(5);\n\ttempD(2) = calib(6);\n\ttempD(3) = calib(7);\n\tcamera_d_OPENCV = tempD;\n}\n virtual Eigen::Vector2f undistortF(const Eigen::Vector2f &uv_dist) = 0 {\n cv::Matx33d camK = camera_k_OPENCV;\n cv::Vec4d camD = camera_d_OPENCV;\n\n cv::Mat mat(1, 2, CV_32F);\n mat.at(0, 0) = uv_dist(0);\n mat.at(0, 1) = uv_dist(1);\n mat = mat.reshape(2);\n\n cv::undistortPoints(mat, mat, camK, camD);\n\n Eigen::Vector2f pt_out;\n mat = mat.reshape(1);\n pt_out(0) = mat.at(0, 0);\n pt_out(1) = mat.at(0, 1);\n\n return pt_out;\n}\n Eigen::Vector2d undistortD(const Eigen::Vector2d &uv_dist) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 = uv_dist.cast();\n\tept_2 = undistortF(ept_1);\n\n\treturn ept_2.cast();\n}\n cv::Point2f undistortCV(const cv::Point2f &uv_dist) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 << uv_dist.x, uv_dist.y;\n\tept_2 = undistortF(ept_1);\n\tcv::Point2f pt_out;\n\tpt_out.x = ept_2(0);\n\tpt_out.y = ept_2(1);\n\n\treturn pt_out;\n}\n virtual Eigen::Vector2f distortF(const Eigen::Vector2f &uv_norm) = 0 {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tEigen::Vector2f uv_dist;\n\tuv_dist(0) = (float)(cam_d(0) * x1 + cam_d(2));\n\tuv_dist(1) = (float)(cam_d(1) * y1 + cam_d(3));\n\n\treturn uv_dist;\n}\n Eigen::Vector2d distortD(const Eigen::Vector2d &uv_norm) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 = uv_norm.cast();\n\tept_2 = distortF(ept_1);\n\n\treturn ept_2.cast();\n}\n cv::Point2f distortCV(const cv::Point2f &uv_norm) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 << uv_norm.x, uv_norm.y;\n\tept_2 = distortF(ept_1);\n\tcv::Point2f pt_out;\n\tpt_out.x = ept_2(0);\n\tpt_out.y = ept_2(1);\n\n\treturn pt_out;\n}\n virtual void computeDistortJacobian(const Eigen::Vector2d &uv_norm, Eigen::MatrixXd &H_dz_dzn, Eigen::MatrixXd &H_dz_dzeta) = 0 {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\n\tH_dz_dzn = Eigen::MatrixXd::Zero(2, 2);\n\tdouble x = uv_norm(0);\n\tdouble y = uv_norm(1);\n\tdouble x_2 = uv_norm(0) * uv_norm(0);\n\tdouble y_2 = uv_norm(1) * uv_norm(1);\n\tdouble x_y = uv_norm(0) * uv_norm(1);\n\tH_dz_dzn(0, 0) = cam_d(0) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * x_2 + 4 * cam_d(5) * x_2 * r_2) +\n\t 2 * cam_d(6) * y + (2 * cam_d(7) * x + 4 * cam_d(7) * x));\n\tH_dz_dzn(0, 1) = cam_d(0) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 0) = cam_d(1) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 1) = cam_d(1) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * y_2 + 4 * cam_d(5) * y_2 * r_2) +\n\t 2 * cam_d(7) * x + (2 * cam_d(6) * y + 4 * cam_d(6) * y));\n\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tH_dz_dzeta = Eigen::MatrixXd::Zero(2, 8);\n\tH_dz_dzeta(0, 0) = x1;\n\tH_dz_dzeta(0, 2) = 1;\n\tH_dz_dzeta(0, 4) = cam_d(0) * uv_norm(0) * r_2;\n\tH_dz_dzeta(0, 5) = cam_d(0) * uv_norm(0) * r_4;\n\tH_dz_dzeta(0, 6) = 2 * cam_d(0) * uv_norm(0) * uv_norm(1);\n\tH_dz_dzeta(0, 7) = cam_d(0) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tH_dz_dzeta(1, 1) = y1;\n\tH_dz_dzeta(1, 3) = 1;\n\tH_dz_dzeta(1, 4) = cam_d(1) * uv_norm(1) * r_2;\n\tH_dz_dzeta(1, 5) = cam_d(1) * uv_norm(1) * r_4;\n\tH_dz_dzeta(1, 6) = cam_d(1) * (r_2 + 2 * uv_norm(1) * uv_norm(1));\n\tH_dz_dzeta(1, 7) = 2 * cam_d(1) * uv_norm(0) * uv_norm(1);\n}\n Eigen::MatrixXd getValue() {\n\treturn camera_values;\n}\n cv::Matx33d getK() {\n\treturn camera_k_OPENCV;\n}\n cv::Vec4d getD() {\n\treturn camera_d_OPENCV;\n}\n int w() {\n\treturn width;\n}\n int h() {\n\treturn height;\n}\n protected:\n Eigen::MatrixXd camera_values;\n cv::Matx33d camera_k_OPENCV;\n cv::Vec4d camera_d_OPENCV;\n int width;\n int height;\n public:\n Eigen::Vector2f undistortF(const Eigen::Vector2f &uv_dist) override {\n cv::Matx33d camK = camera_k_OPENCV;\n cv::Vec4d camD = camera_d_OPENCV;\n\n cv::Mat mat(1, 2, CV_32F);\n mat.at(0, 0) = uv_dist(0);\n mat.at(0, 1) = uv_dist(1);\n mat = mat.reshape(2);\n\n cv::undistortPoints(mat, mat, camK, camD);\n\n Eigen::Vector2f pt_out;\n mat = mat.reshape(1);\n pt_out(0) = mat.at(0, 0);\n pt_out(1) = mat.at(0, 1);\n\n return pt_out;\n}\n Eigen::Vector2f distortF(const Eigen::Vector2f &uv_norm) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tEigen::Vector2f uv_dist;\n\tuv_dist(0) = (float)(cam_d(0) * x1 + cam_d(2));\n\tuv_dist(1) = (float)(cam_d(1) * y1 + cam_d(3));\n\n\treturn uv_dist;\n}\n void computeDistortJacobian(const Eigen::Vector2d &uv_norm, Eigen::MatrixXd &H_dz_dzn, Eigen::MatrixXd &H_dz_dzeta) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\n\tH_dz_dzn = Eigen::MatrixXd::Zero(2, 2);\n\tdouble x = uv_norm(0);\n\tdouble y = uv_norm(1);\n\tdouble x_2 = uv_norm(0) * uv_norm(0);\n\tdouble y_2 = uv_norm(1) * uv_norm(1);\n\tdouble x_y = uv_norm(0) * uv_norm(1);\n\tH_dz_dzn(0, 0) = cam_d(0) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * x_2 + 4 * cam_d(5) * x_2 * r_2) +\n\t 2 * cam_d(6) * y + (2 * cam_d(7) * x + 4 * cam_d(7) * x));\n\tH_dz_dzn(0, 1) = cam_d(0) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 0) = cam_d(1) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 1) = cam_d(1) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * y_2 + 4 * cam_d(5) * y_2 * r_2) +\n\t 2 * cam_d(7) * x + (2 * cam_d(6) * y + 4 * cam_d(6) * y));\n\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tH_dz_dzeta = Eigen::MatrixXd::Zero(2, 8);\n\tH_dz_dzeta(0, 0) = x1;\n\tH_dz_dzeta(0, 2) = 1;\n\tH_dz_dzeta(0, 4) = cam_d(0) * uv_norm(0) * r_2;\n\tH_dz_dzeta(0, 5) = cam_d(0) * uv_norm(0) * r_4;\n\tH_dz_dzeta(0, 6) = 2 * cam_d(0) * uv_norm(0) * uv_norm(1);\n\tH_dz_dzeta(0, 7) = cam_d(0) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tH_dz_dzeta(1, 1) = y1;\n\tH_dz_dzeta(1, 3) = 1;\n\tH_dz_dzeta(1, 4) = cam_d(1) * uv_norm(1) * r_2;\n\tH_dz_dzeta(1, 5) = cam_d(1) * uv_norm(1) * r_4;\n\tH_dz_dzeta(1, 6) = cam_d(1) * (r_2 + 2 * uv_norm(1) * uv_norm(1));\n\tH_dz_dzeta(1, 7) = 2 * cam_d(1) * uv_norm(0) * uv_norm(1);\n}\n public:\n Eigen::Vector2f undistortF(const Eigen::Vector2f &uv_dist) override {\n cv::Matx33d camK = camera_k_OPENCV;\n cv::Vec4d camD = camera_d_OPENCV;\n\n cv::Mat mat(1, 2, CV_32F);\n mat.at(0, 0) = uv_dist(0);\n mat.at(0, 1) = uv_dist(1);\n mat = mat.reshape(2);\n\n cv::undistortPoints(mat, mat, camK, camD);\n\n Eigen::Vector2f pt_out;\n mat = mat.reshape(1);\n pt_out(0) = mat.at(0, 0);\n pt_out(1) = mat.at(0, 1);\n\n return pt_out;\n}\n Eigen::Vector2f distortF(const Eigen::Vector2f &uv_norm) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tEigen::Vector2f uv_dist;\n\tuv_dist(0) = (float)(cam_d(0) * x1 + cam_d(2));\n\tuv_dist(1) = (float)(cam_d(1) * y1 + cam_d(3));\n\n\treturn uv_dist;\n}\n void computeDistortJacobian(const Eigen::Vector2d &uv_norm, Eigen::MatrixXd &H_dz_dzn, Eigen::MatrixXd &H_dz_dzeta) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\n\tH_dz_dzn = Eigen::MatrixXd::Zero(2, 2);\n\tdouble x = uv_norm(0);\n\tdouble y = uv_norm(1);\n\tdouble x_2 = uv_norm(0) * uv_norm(0);\n\tdouble y_2 = uv_norm(1) * uv_norm(1);\n\tdouble x_y = uv_norm(0) * uv_norm(1);\n\tH_dz_dzn(0, 0) = cam_d(0) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * x_2 + 4 * cam_d(5) * x_2 * r_2) +\n\t 2 * cam_d(6) * y + (2 * cam_d(7) * x + 4 * cam_d(7) * x));\n\tH_dz_dzn(0, 1) = cam_d(0) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 0) = cam_d(1) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 1) = cam_d(1) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * y_2 + 4 * cam_d(5) * y_2 * r_2) +\n\t 2 * cam_d(7) * x + (2 * cam_d(6) * y + 4 * cam_d(6) * y));\n\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tH_dz_dzeta = Eigen::MatrixXd::Zero(2, 8);\n\tH_dz_dzeta(0, 0) = x1;\n\tH_dz_dzeta(0, 2) = 1;\n\tH_dz_dzeta(0, 4) = cam_d(0) * uv_norm(0) * r_2;\n\tH_dz_dzeta(0, 5) = cam_d(0) * uv_norm(0) * r_4;\n\tH_dz_dzeta(0, 6) = 2 * cam_d(0) * uv_norm(0) * uv_norm(1);\n\tH_dz_dzeta(0, 7) = cam_d(0) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tH_dz_dzeta(1, 1) = y1;\n\tH_dz_dzeta(1, 3) = 1;\n\tH_dz_dzeta(1, 4) = cam_d(1) * uv_norm(1) * r_2;\n\tH_dz_dzeta(1, 5) = cam_d(1) * uv_norm(1) * r_4;\n\tH_dz_dzeta(1, 6) = cam_d(1) * (r_2 + 2 * uv_norm(1) * uv_norm(1));\n\tH_dz_dzeta(1, 7) = 2 * cam_d(1) * uv_norm(0) * uv_norm(1);\n}\n};"], ["/voxel_svio/include/quatOps.h", "class quatType {\n public:\n static Eigen::Matrix rotToQuat(const Eigen::Matrix &rot) {\n\tEigen::Matrix q;\n\tdouble T = rot.trace();\n\n\tif ((rot(0, 0) >= T) && (rot(0, 0) >= rot(1, 1)) && (rot(0, 0) >= rot(2, 2)))\n\t{\n\t\tq(0) = sqrt((1 + (2 * rot(0, 0)) - T) / 4);\n\t\tq(1) = (1 / (4 * q(0))) * (rot(0, 1) + rot(1, 0));\n\t\tq(2) = (1 / (4 * q(0))) * (rot(0, 2) + rot(2, 0));\n\t\tq(3) = (1 / (4 * q(0))) * (rot(1, 2) - rot(2, 1));\n\t} \n\telse if ((rot(1, 1) >= T) && (rot(1, 1) >= rot(0, 0)) && (rot(1, 1) >= rot(2, 2)))\n\t{\n\t\tq(1) = sqrt((1 + (2 * rot(1, 1)) - T) / 4);\n\t\tq(0) = (1 / (4 * q(1))) * (rot(0, 1) + rot(1, 0));\n\t\tq(2) = (1 / (4 * q(1))) * (rot(1, 2) + rot(2, 1));\n\t\tq(3) = (1 / (4 * q(1))) * (rot(2, 0) - rot(0, 2));\n\t}\n\telse if ((rot(2, 2) >= T) && (rot(2, 2) >= rot(0, 0)) && (rot(2, 2) >= rot(1, 1)))\n\t{\n\t\tq(2) = sqrt((1 + (2 * rot(2, 2)) - T) / 4);\n\t\tq(0) = (1 / (4 * q(2))) * (rot(0, 2) + rot(2, 0));\n\t\tq(1) = (1 / (4 * q(2))) * (rot(1, 2) + rot(2, 1));\n\t\tq(3) = (1 / (4 * q(2))) * (rot(0, 1) - rot(1, 0));\n\t} \n\telse\n\t{\n\t\tq(3) = sqrt((1 + T) / 4);\n\t\tq(0) = (1 / (4 * q(3))) * (rot(1, 2) - rot(2, 1));\n\t\tq(1) = (1 / (4 * q(3))) * (rot(2, 0) - rot(0, 2));\n\t\tq(2) = (1 / (4 * q(3))) * (rot(0, 1) - rot(1, 0));\n\t}\n\n\tif (q(3) < 0)\n\t{\n\t\tq = -q;\n\t}\n\n\tq = q / (q.norm());\n\treturn q;\n}\n static Eigen::Matrix quatToRot(const Eigen::Matrix &q) {\n\tEigen::Matrix q_x = skewSymmetric(q.block<3, 1>(0, 0));\n\tEigen::MatrixXd rot = (2 * std::pow(q(3, 0), 2) - 1) * Eigen::MatrixXd::Identity(3, 3) - 2 * q(3, 0) * q_x +\n \t 2 * q.block<3, 1>(0, 0) * (q.block<3, 1>(0, 0).transpose());\n\treturn rot;\n}\n static Eigen::Matrix skewSymmetric(const Eigen::Matrix &w) {\n\tEigen::Matrix w_x;\n\tw_x << 0, -w(2), w(1), w(2), 0, -w(0), -w(1), w(0), 0;\n\treturn w_x;\n}\n static Eigen::Matrix quatMultiply(const Eigen::Matrix &q, const Eigen::Matrix &p) {\n\tEigen::Matrix q_t;\n\tEigen::Matrix Q_m;\n\n\tQ_m.block<3, 3>(0, 0) = q(3, 0) * Eigen::MatrixXd::Identity(3, 3) - skewSymmetric(q.block<3, 1>(0, 0));\n\tQ_m.block<3, 1>(0, 3) = q.block<3, 1>(0, 0);\n\tQ_m.block<1, 3>(3, 0) = -q.block<3, 1>(0, 0).transpose();\n\tQ_m(3, 3) = q(3, 0);\n\tq_t = Q_m * p;\n\n\tif (q_t(3, 0) < 0)\n\t{\n\t\tq_t *= -1;\n\t}\n\n\treturn q_t / q_t.norm();\n}\n static Eigen::Matrix vee(const Eigen::Matrix &w_x) {\n\tEigen::Matrix w;\n\tw << w_x(2, 1), w_x(0, 2), w_x(1, 0);\n\treturn w;\n}\n static Eigen::Matrix expSo3(const Eigen::Matrix &w) {\n\tEigen::Matrix w_x = skewSymmetric(w);\n\tdouble theta = w.norm();\n\n\tdouble A, B;\n\tif (theta < 1e-7) {\n\t\tA = 1;\n\t\tB = 0.5;\n\t}\n\telse\n\t{\n\t\tA = sin(theta) / theta;\n\t\tB = (1 - cos(theta)) / (theta * theta);\n\t}\n\n\tEigen::Matrix R;\n\tif (theta == 0) {\n\t\tR = Eigen::MatrixXd::Identity(3, 3);\n\t} else {\n\t\tR = Eigen::MatrixXd::Identity(3, 3) + A * w_x + B * w_x * w_x;\n\t}\n\treturn R;\n}\n static Eigen::Matrix logSo3(const Eigen::Matrix &R) {\n\tdouble R_11 = R(0, 0), R_12 = R(0, 1), R_13 = R(0, 2);\n\tdouble R_21 = R(1, 0), R_22 = R(1, 1), R_23 = R(1, 2);\n\tdouble R_31 = R(2, 0), R_32 = R(2, 1), R_33 = R(2, 2);\n\n\tconst double tr = R.trace();\n\tEigen::Vector3d omega;\n\n\tif (tr + 1.0 < 1e-10)\n\t{\n \tif (std::abs(R_33 + 1.0) > 1e-5)\n\t\t\tomega = (M_PI / sqrt(2.0 + 2.0 * R_33)) * Eigen::Vector3d(R_13, R_23, 1.0 + R_33);\n\t\telse if (std::abs(R_22 + 1.0) > 1e-5)\n\t\t\tomega = (M_PI / sqrt(2.0 + 2.0 * R_22)) * Eigen::Vector3d(R_12, 1.0 + R_22, R_32);\n\t\telse\n\t\t\tomega = (M_PI / sqrt(2.0 + 2.0 * R_11)) * Eigen::Vector3d(1.0 + R_11, R_21, R_31);\n\t}\n\telse\n\t{\n\t\tdouble magnitude;\n\t\tconst double tr_3 = tr - 3.0;\n\t\tif (tr_3 < -1e-7)\n\t\t{\n\t\t\tdouble theta = acos((tr - 1.0) / 2.0);\n\t\t\tmagnitude = theta / (2.0 * sin(theta));\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmagnitude = 0.5 - tr_3 / 12.0;\n\t\t}\n\t\tomega = magnitude * Eigen::Vector3d(R_32 - R_23, R_13 - R_31, R_21 - R_12);\n\t}\n\treturn omega;\n}\n static Eigen::Matrix4d expSe3(Eigen::Matrix vec) {\n\tEigen::Vector3d w = vec.head(3);\n\tEigen::Vector3d u = vec.tail(3);\n\tdouble theta = sqrt(w.dot(w));\n\tEigen::Matrix3d wskew;\n\twskew << 0, -w(2), w(1), w(2), 0, -w(0), -w(1), w(0), 0;\n\n\tdouble A, B, C;\n\tif (theta < 1e-7)\n\t{\n\t\tA = 1;\n\t\tB = 0.5;\n\t\tC = 1.0 / 6.0;\n\t}\n\telse\n\t{\n\t\tA = sin(theta) / theta;\n\t\tB = (1 - cos(theta)) / (theta * theta);\n\t\tC = (1 - A) / (theta * theta);\n\t}\n\n\tEigen::Matrix3d I_33 = Eigen::Matrix3d::Identity();\n\tEigen::Matrix3d V = I_33 + B * wskew + C * wskew * wskew;\n\n\tEigen::Matrix4d mat = Eigen::Matrix4d::Zero();\n\tmat.block<3, 3>(0, 0) = I_33 + A * wskew + B * wskew * wskew;\n\tmat.block<3, 1>(0, 3) = V * u;\n\tmat(3, 3) = 1;\n\treturn mat;\n}\n static Eigen::Matrix logSe3(Eigen::Matrix4d mat) {\n\tEigen::Vector3d w = logSo3(mat.block<3, 3>(0, 0));\n\tEigen::Vector3d T = mat.block<3, 1>(0, 3);\n\tconst double t = w.norm();\n\tif (t < 1e-10)\n\t{\n\t\tEigen::Matrix log;\n\t\tlog << w, T;\n\t\treturn log;\n\t}\n\telse\n\t{\n\t\tEigen::Matrix3d W = skewSymmetric(w / t);\n \tdouble Tan = tan(0.5 * t);\n \tEigen::Vector3d WT = W * T;\n \tEigen::Vector3d u = T - (0.5 * t) * WT + (1 - t / (2. * Tan)) * (W * WT);\n \tEigen::Matrix log;\n \tlog << w, u;\n \treturn log;\n\t}\n}\n static Eigen::Matrix4d hatSe3(const Eigen::Matrix &vec) {\n\tEigen::Matrix4d mat = Eigen::Matrix4d::Zero();\n\tmat.block<3, 3>(0, 0) = skewSymmetric(vec.head(3));\n\tmat.block<3, 1>(0, 3) = vec.tail(3);\n\treturn mat;\n}\n static Eigen::Matrix4d invSe3(const Eigen::Matrix4d &T) {\n\tEigen::Matrix4d Tinv = Eigen::Matrix4d::Identity();\n\tTinv.block<3, 3>(0, 0) = T.block<3, 3>(0, 0).transpose();\n\tTinv.block<3, 1>(0, 3) = -Tinv.block<3, 3>(0, 0) * T.block<3, 1>(0, 3);\n\treturn Tinv;\n}\n static Eigen::Matrix inv(Eigen::Matrix q) {\n\tEigen::Matrix q_inv;\n\tq_inv.block<3, 1>(0, 0) = -q.block<3, 1>(0, 0);\n\tq_inv(3, 0) = q(3, 0);\n\treturn q_inv;\n}\n static Eigen::Matrix omega(Eigen::Matrix w) {\n\tEigen::Matrix mat;\n\tmat.block<3, 3>(0, 0) = -skewSymmetric(w);\n\tmat.block<1, 3>(3, 0) = -w.transpose();\n\tmat.block<3, 1>(0, 3) = w;\n\tmat(3, 3) = 0;\n\treturn mat;\n}\n static Eigen::Matrix quatNorm(Eigen::Matrix q_t) {\n\tif (q_t(3, 0) < 0)\n\t{\n\t\tq_t *= -1;\n\t}\n\n\treturn q_t / q_t.norm();\n}\n static Eigen::Matrix JleftSo3(const Eigen::Matrix &w) {\n\tdouble theta = w.norm();\n\n\tif (theta < 1e-6) {\n\t\treturn Eigen::MatrixXd::Identity(3, 3);\n\t}\n\telse\n\t{\n\t\tEigen::Matrix a = w / theta;\n\t\tEigen::Matrix J = sin(theta) / theta * Eigen::MatrixXd::Identity(3, 3) + (1 - sin(theta) / theta) * a * a.transpose() +\n\t\t\t\t\t\t\t\t\t\t((1 - cos(theta)) / theta) * skewSymmetric(a);\n\t\treturn J;\n\t}\n}\n static Eigen::Matrix JrighySo3(const Eigen::Matrix &w) {\n\treturn JleftSo3(-w);\n}\n static Eigen::Matrix rotToRpy(const Eigen::Matrix &rot) {\n\tEigen::Matrix rpy;\n\trpy(1, 0) = atan2(-rot(2, 0), sqrt(rot(0, 0) * rot(0, 0) + rot(1, 0) * rot(1, 0)));\n\tif (std::abs(cos(rpy(1, 0))) > 1.0e-12)\n\t{\n\t\trpy(2, 0) = atan2(rot(1, 0) / cos(rpy(1, 0)), rot(0, 0) / cos(rpy(1, 0)));\n\t\trpy(0, 0) = atan2(rot(2, 1) / cos(rpy(1, 0)), rot(2, 2) / cos(rpy(1, 0)));\n\t}\n\telse\n\t{\n\t\trpy(2, 0) = 0;\n\t\trpy(0, 0) = atan2(rot(0, 1), rot(1, 1));\n\t}\n\treturn rpy;\n}\n static Eigen::Matrix rotX(double t) {\n\tEigen::Matrix r;\n\tdouble ct = cos(t);\n\tdouble st = sin(t);\n\tr << 1.0, 0.0, 0.0, 0.0, ct, -st, 0.0, st, ct;\n\treturn r;\n}\n static Eigen::Matrix rotY(double t) {\n\tEigen::Matrix r;\n\tdouble ct = cos(t);\n\tdouble st = sin(t);\n\tr << ct, 0.0, st, 0.0, 1.0, 0.0, -st, 0.0, ct;\n\treturn r;\n}\n static Eigen::Matrix rotZ(double t) {\n\tEigen::Matrix r;\n\tdouble ct = cos(t);\n\tdouble st = sin(t);\n\tr << ct, -st, 0.0, st, ct, 0.0, 0.0, 0.0, 1.0;\n\treturn r;\n}\n};"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_map.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_MAP_H\n#define TSL_ROBIN_MAP_H \n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \"robin_hash.h\"\n\n\nnamespace tsl {\n\n \n/**\n * Implementation of a hash map using open-addressing and the robin hood hashing algorithm with backward shift deletion.\n * \n * For operations modifying the hash map (insert, erase, rehash, ...), the strong exception guarantee \n * is only guaranteed when the expression `std::is_nothrow_swappable>::value &&\n * std::is_nothrow_move_constructible>::value` is true, otherwise if an exception\n * is thrown during the swap or the move, the hash map may end up in a undefined state. Per the standard\n * a `Key` or `T` with a noexcept copy constructor and no move constructor also satisfies the \n * `std::is_nothrow_move_constructible>::value` criterion (and will thus guarantee the \n * strong exception for the map).\n * \n * When `StoreHash` is true, 32 bits of the hash are stored alongside the values. It can improve \n * the performance during lookups if the `KeyEqual` function takes time (if it engenders a cache-miss for example) \n * as we then compare the stored hashes before comparing the keys. When `tsl::rh::power_of_two_growth_policy` is used\n * as `GrowthPolicy`, it may also speed-up the rehash process as we can avoid to recalculate the hash. \n * When it is detected that storing the hash will not incur any memory penalty due to alignment (i.e. \n * `sizeof(tsl::detail_robin_hash::bucket_entry) == \n * sizeof(tsl::detail_robin_hash::bucket_entry)`) and `tsl::rh::power_of_two_growth_policy` is\n * used, the hash will be stored even if `StoreHash` is false so that we can speed-up the rehash (but it will\n * not be used on lookups unless `StoreHash` is true).\n * \n * `GrowthPolicy` defines how the map grows and consequently how a hash value is mapped to a bucket. \n * By default the map uses `tsl::rh::power_of_two_growth_policy`. This policy keeps the number of buckets \n * to a power of two and uses a mask to map the hash to a bucket instead of the slow modulo.\n * Other growth policies are available and you may define your own growth policy, \n * check `tsl::rh::power_of_two_growth_policy` for the interface.\n * \n * `std::pair` must be swappable.\n * \n * `Key` and `T` must be copy and/or move constructible.\n * \n * If the destructor of `Key` or `T` throws an exception, the behaviour of the class is undefined.\n * \n * Iterators invalidation:\n * - clear, operator=, reserve, rehash: always invalidate the iterators.\n * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators.\n * - erase: always invalidate the iterators.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator>,\n bool StoreHash = false,\n class GrowthPolicy = tsl::rh::power_of_two_growth_policy<2>>\nclass robin_map {\nprivate:\n template\n using has_is_transparent = tsl::detail_robin_hash::has_is_transparent;\n \n class KeySelect {\n public:\n using key_type = Key;\n \n const key_type& operator()(const std::pair& key_value) const noexcept {\n return key_value.first;\n }\n \n key_type& operator()(std::pair& key_value) noexcept {\n return key_value.first;\n }\n }; \n \n class ValueSelect {\n public:\n using value_type = T;\n \n const value_type& operator()(const std::pair& key_value) const noexcept {\n return key_value.second;\n }\n \n value_type& operator()(std::pair& key_value) noexcept {\n return key_value.second;\n }\n };\n \n using ht = detail_robin_hash::robin_hash, KeySelect, ValueSelect,\n Hash, KeyEqual, Allocator, StoreHash, GrowthPolicy>; \n \npublic:\n using key_type = typename ht::key_type;\n using mapped_type = T;\n using value_type = typename ht::value_type;\n using size_type = typename ht::size_type;\n using difference_type = typename ht::difference_type;\n using hasher = typename ht::hasher;\n using key_equal = typename ht::key_equal;\n using allocator_type = typename ht::allocator_type;\n using reference = typename ht::reference;\n using const_reference = typename ht::const_reference;\n using pointer = typename ht::pointer;\n using const_pointer = typename ht::const_pointer;\n using iterator = typename ht::iterator;\n using const_iterator = typename ht::const_iterator;\n \n \npublic:\n /*\n * Constructors\n */\n robin_map(): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {\n }\n \n explicit robin_map(size_type bucket_count, \n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n m_ht(bucket_count, hash, equal, alloc)\n {\n }\n \n robin_map(size_type bucket_count,\n const Allocator& alloc): robin_map(bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n robin_map(size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_map(bucket_count, hash, KeyEqual(), alloc)\n {\n }\n \n explicit robin_map(const Allocator& alloc): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {\n }\n \n template\n robin_map(InputIt first, InputIt last,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): robin_map(bucket_count, hash, equal, alloc)\n {\n insert(first, last);\n }\n \n template\n robin_map(InputIt first, InputIt last,\n size_type bucket_count,\n const Allocator& alloc): robin_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n template\n robin_map(InputIt first, InputIt last,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_map(first, last, bucket_count, hash, KeyEqual(), alloc)\n {\n }\n\n robin_map(std::initializer_list init,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n robin_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)\n {\n }\n\n robin_map(std::initializer_list init,\n size_type bucket_count,\n const Allocator& alloc): \n robin_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n\n robin_map(std::initializer_list init,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): \n robin_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)\n {\n }\n \n robin_map& operator=(std::initializer_list ilist) {\n m_ht.clear();\n \n m_ht.reserve(ilist.size());\n m_ht.insert(ilist.begin(), ilist.end());\n \n return *this;\n }\n \n allocator_type get_allocator() const { return m_ht.get_allocator(); }\n \n \n /*\n * Iterators\n */\n iterator begin() noexcept { return m_ht.begin(); }\n const_iterator begin() const noexcept { return m_ht.begin(); }\n const_iterator cbegin() const noexcept { return m_ht.cbegin(); }\n \n iterator end() noexcept { return m_ht.end(); }\n const_iterator end() const noexcept { return m_ht.end(); }\n const_iterator cend() const noexcept { return m_ht.cend(); }\n \n \n /*\n * Capacity\n */\n bool empty() const noexcept { return m_ht.empty(); }\n size_type size() const noexcept { return m_ht.size(); }\n size_type max_size() const noexcept { return m_ht.max_size(); }\n \n /*\n * Modifiers\n */\n void clear() noexcept { m_ht.clear(); }\n \n \n \n std::pair insert(const value_type& value) { \n return m_ht.insert(value); \n }\n \n template::value>::type* = nullptr>\n std::pair insert(P&& value) { \n return m_ht.emplace(std::forward

(value)); \n }\n \n std::pair insert(value_type&& value) { \n return m_ht.insert(std::move(value)); \n }\n \n \n iterator insert(const_iterator hint, const value_type& value) { \n return m_ht.insert_hint(hint, value); \n }\n \n template::value>::type* = nullptr>\n iterator insert(const_iterator hint, P&& value) { \n return m_ht.emplace_hint(hint, std::forward

(value));\n }\n \n iterator insert(const_iterator hint, value_type&& value) { \n return m_ht.insert_hint(hint, std::move(value)); \n }\n \n \n template\n void insert(InputIt first, InputIt last) { \n m_ht.insert(first, last); \n }\n \n void insert(std::initializer_list ilist) { \n m_ht.insert(ilist.begin(), ilist.end()); \n }\n\n \n \n \n template\n std::pair insert_or_assign(const key_type& k, M&& obj) { \n return m_ht.insert_or_assign(k, std::forward(obj)); \n }\n\n template\n std::pair insert_or_assign(key_type&& k, M&& obj) { \n return m_ht.insert_or_assign(std::move(k), std::forward(obj)); \n }\n \n template\n iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {\n return m_ht.insert_or_assign(hint, k, std::forward(obj));\n }\n \n template\n iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {\n return m_ht.insert_or_assign(hint, std::move(k), std::forward(obj));\n }\n \n \n \n /**\n * Due to the way elements are stored, emplace will need to move or copy the key-value once.\n * The method is equivalent to insert(value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n std::pair emplace(Args&&... args) { \n return m_ht.emplace(std::forward(args)...); \n }\n \n \n \n /**\n * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.\n * The method is equivalent to insert(hint, value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n iterator emplace_hint(const_iterator hint, Args&&... args) {\n return m_ht.emplace_hint(hint, std::forward(args)...);\n }\n \n \n \n \n template\n std::pair try_emplace(const key_type& k, Args&&... args) { \n return m_ht.try_emplace(k, std::forward(args)...);\n }\n \n template\n std::pair try_emplace(key_type&& k, Args&&... args) {\n return m_ht.try_emplace(std::move(k), std::forward(args)...);\n }\n \n template\n iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {\n return m_ht.try_emplace_hint(hint, k, std::forward(args)...);\n }\n \n template\n iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {\n return m_ht.try_emplace_hint(hint, std::move(k), std::forward(args)...);\n }\n \n \n\n \n iterator erase(iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }\n size_type erase(const key_type& key) { return m_ht.erase(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n size_type erase(const key_type& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type erase(const K& key) { return m_ht.erase(key); }\n \n /**\n * @copydoc erase(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n template::value>::type* = nullptr> \n size_type erase(const K& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n \n \n void swap(robin_map& other) { other.m_ht.swap(m_ht); }\n \n \n \n /*\n * Lookup\n */\n T& at(const Key& key) { return m_ht.at(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }\n \n \n const T& at(const Key& key) const { return m_ht.at(key); }\n \n /**\n * @copydoc at(const Key& key, std::size_t precalculated_hash)\n */\n const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }\n \n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n T& at(const K& key) { return m_ht.at(key); }\n\n /**\n * @copydoc at(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */ \n template::value>::type* = nullptr> \n T& at(const K& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }\n \n \n /**\n * @copydoc at(const K& key)\n */\n template::value>::type* = nullptr> \n const T& at(const K& key) const { return m_ht.at(key); }\n \n /**\n * @copydoc at(const K& key, std::size_t precalculated_hash)\n */ \n template::value>::type* = nullptr> \n const T& at(const K& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }\n \n \n \n \n T& operator[](const Key& key) { return m_ht[key]; } \n T& operator[](Key&& key) { return m_ht[std::move(key)]; }\n \n \n \n \n size_type count(const Key& key) const { return m_ht.count(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n size_type count(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.count(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type count(const K& key) const { return m_ht.count(key); }\n \n /**\n * @copydoc count(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */ \n template::value>::type* = nullptr> \n size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }\n \n \n \n \n iterator find(const Key& key) { return m_ht.find(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n const_iterator find(const Key& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const Key& key, std::size_t precalculated_hash)\n */\n const_iterator find(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.find(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key) { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n /**\n * @copydoc find(const K& key)\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key, std::size_t precalculated_hash) const { \n return m_ht.find(key, precalculated_hash); \n }\n \n \n \n \n bool contains(const Key& key) const { return m_ht.contains(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n bool contains(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key) const { return m_ht.contains(key); }\n \n /**\n * @copydoc contains(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n \n \n \n std::pair equal_range(const Key& key) { return m_ht.equal_range(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n std::pair equal_range(const Key& key) const { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n\n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) { return m_ht.equal_range(key); }\n \n \n /**\n * @copydoc equal_range(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n /**\n * @copydoc equal_range(const K& key)\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) const { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const K& key, std::size_t precalculated_hash)\n */ \n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n \n \n \n /*\n * Bucket interface \n */\n size_type bucket_count() const { return m_ht.bucket_count(); }\n size_type max_bucket_count() const { return m_ht.max_bucket_count(); }\n \n \n /*\n * Hash policy \n */\n float load_factor() const { return m_ht.load_factor(); }\n \n float min_load_factor() const { return m_ht.min_load_factor(); }\n float max_load_factor() const { return m_ht.max_load_factor(); }\n \n /**\n * Set the `min_load_factor` to `ml`. When the `load_factor` of the map goes\n * below `min_load_factor` after some erase operations, the map will be\n * shrunk when an insertion occurs. The erase method itself never shrinks\n * the map.\n * \n * The default value of `min_load_factor` is 0.0f, the map never shrinks by default.\n */\n void min_load_factor(float ml) { m_ht.min_load_factor(ml); }\n void max_load_factor(float ml) { m_ht.max_load_factor(ml); }\n \n void rehash(size_type count) { m_ht.rehash(count); }\n void reserve(size_type count) { m_ht.reserve(count); }\n \n \n /*\n * Observers\n */\n hasher hash_function() const { return m_ht.hash_function(); }\n key_equal key_eq() const { return m_ht.key_eq(); }\n \n /*\n * Other\n */\n \n /**\n * Convert a const_iterator to an iterator.\n */\n iterator mutable_iterator(const_iterator pos) {\n return m_ht.mutable_iterator(pos);\n }\n \n friend bool operator==(const robin_map& lhs, const robin_map& rhs) {\n if(lhs.size() != rhs.size()) {\n return false;\n }\n \n for(const auto& element_lhs: lhs) {\n const auto it_element_rhs = rhs.find(element_lhs.first);\n if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {\n return false;\n }\n }\n \n return true;\n }\n\n friend bool operator!=(const robin_map& lhs, const robin_map& rhs) {\n return !operator==(lhs, rhs);\n }\n\n friend void swap(robin_map& lhs, robin_map& rhs) {\n lhs.swap(rhs);\n }\n \nprivate:\n ht m_ht;\n};\n\n\n/**\n * Same as `tsl::robin_map`.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator>,\n bool StoreHash = false>\nusing robin_pg_map = robin_map;\n\n} // end namespace tsl\n\n#endif\n"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_set.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_SET_H\n#define TSL_ROBIN_SET_H\n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \"robin_hash.h\"\n\n\nnamespace tsl {\n\n \n/**\n * Implementation of a hash set using open-addressing and the robin hood hashing algorithm with backward shift deletion.\n * \n * For operations modifying the hash set (insert, erase, rehash, ...), the strong exception guarantee \n * is only guaranteed when the expression `std::is_nothrow_swappable::value &&\n * std::is_nothrow_move_constructible::value` is true, otherwise if an exception\n * is thrown during the swap or the move, the hash set may end up in a undefined state. Per the standard\n * a `Key` with a noexcept copy constructor and no move constructor also satisfies the \n * `std::is_nothrow_move_constructible::value` criterion (and will thus guarantee the \n * strong exception for the set).\n * \n * When `StoreHash` is true, 32 bits of the hash are stored alongside the values. It can improve \n * the performance during lookups if the `KeyEqual` function takes time (or engenders a cache-miss for example) \n * as we then compare the stored hashes before comparing the keys. When `tsl::rh::power_of_two_growth_policy` is used\n * as `GrowthPolicy`, it may also speed-up the rehash process as we can avoid to recalculate the hash. \n * When it is detected that storing the hash will not incur any memory penalty due to alignment (i.e. \n * `sizeof(tsl::detail_robin_hash::bucket_entry) == \n * sizeof(tsl::detail_robin_hash::bucket_entry)`) and `tsl::rh::power_of_two_growth_policy` is\n * used, the hash will be stored even if `StoreHash` is false so that we can speed-up the rehash (but it will\n * not be used on lookups unless `StoreHash` is true).\n * \n * `GrowthPolicy` defines how the set grows and consequently how a hash value is mapped to a bucket. \n * By default the set uses `tsl::rh::power_of_two_growth_policy`. This policy keeps the number of buckets \n * to a power of two and uses a mask to set the hash to a bucket instead of the slow modulo.\n * Other growth policies are available and you may define your own growth policy, \n * check `tsl::rh::power_of_two_growth_policy` for the interface.\n * \n * `Key` must be swappable.\n * \n * `Key` must be copy and/or move constructible.\n * \n * If the destructor of `Key` throws an exception, the behaviour of the class is undefined.\n * \n * Iterators invalidation:\n * - clear, operator=, reserve, rehash: always invalidate the iterators.\n * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators.\n * - erase: always invalidate the iterators.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator,\n bool StoreHash = false,\n class GrowthPolicy = tsl::rh::power_of_two_growth_policy<2>>\nclass robin_set {\nprivate:\n template\n using has_is_transparent = tsl::detail_robin_hash::has_is_transparent;\n \n class KeySelect {\n public:\n using key_type = Key;\n \n const key_type& operator()(const Key& key) const noexcept {\n return key;\n }\n \n key_type& operator()(Key& key) noexcept {\n return key;\n }\n };\n \n using ht = detail_robin_hash::robin_hash;\n \npublic:\n using key_type = typename ht::key_type;\n using value_type = typename ht::value_type;\n using size_type = typename ht::size_type;\n using difference_type = typename ht::difference_type;\n using hasher = typename ht::hasher;\n using key_equal = typename ht::key_equal;\n using allocator_type = typename ht::allocator_type;\n using reference = typename ht::reference;\n using const_reference = typename ht::const_reference;\n using pointer = typename ht::pointer;\n using const_pointer = typename ht::const_pointer;\n using iterator = typename ht::iterator;\n using const_iterator = typename ht::const_iterator;\n\n \n /*\n * Constructors\n */\n robin_set(): robin_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {\n }\n \n explicit robin_set(size_type bucket_count, \n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n m_ht(bucket_count, hash, equal, alloc)\n {\n }\n \n robin_set(size_type bucket_count,\n const Allocator& alloc): robin_set(bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n robin_set(size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_set(bucket_count, hash, KeyEqual(), alloc)\n {\n }\n \n explicit robin_set(const Allocator& alloc): robin_set(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {\n }\n \n template\n robin_set(InputIt first, InputIt last,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): robin_set(bucket_count, hash, equal, alloc)\n {\n insert(first, last);\n }\n \n template\n robin_set(InputIt first, InputIt last,\n size_type bucket_count,\n const Allocator& alloc): robin_set(first, last, bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n template\n robin_set(InputIt first, InputIt last,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_set(first, last, bucket_count, hash, KeyEqual(), alloc)\n {\n }\n\n robin_set(std::initializer_list init,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n robin_set(init.begin(), init.end(), bucket_count, hash, equal, alloc)\n {\n }\n\n robin_set(std::initializer_list init,\n size_type bucket_count,\n const Allocator& alloc): \n robin_set(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n\n robin_set(std::initializer_list init,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): \n robin_set(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)\n {\n }\n\n \n robin_set& operator=(std::initializer_list ilist) {\n m_ht.clear();\n \n m_ht.reserve(ilist.size());\n m_ht.insert(ilist.begin(), ilist.end());\n \n return *this;\n }\n \n allocator_type get_allocator() const { return m_ht.get_allocator(); }\n \n \n /*\n * Iterators\n */\n iterator begin() noexcept { return m_ht.begin(); }\n const_iterator begin() const noexcept { return m_ht.begin(); }\n const_iterator cbegin() const noexcept { return m_ht.cbegin(); }\n \n iterator end() noexcept { return m_ht.end(); }\n const_iterator end() const noexcept { return m_ht.end(); }\n const_iterator cend() const noexcept { return m_ht.cend(); }\n \n \n /*\n * Capacity\n */\n bool empty() const noexcept { return m_ht.empty(); }\n size_type size() const noexcept { return m_ht.size(); }\n size_type max_size() const noexcept { return m_ht.max_size(); }\n \n /*\n * Modifiers\n */\n void clear() noexcept { m_ht.clear(); }\n \n \n \n \n std::pair insert(const value_type& value) { \n return m_ht.insert(value); \n }\n \n std::pair insert(value_type&& value) { \n return m_ht.insert(std::move(value)); \n }\n \n iterator insert(const_iterator hint, const value_type& value) { \n return m_ht.insert_hint(hint, value); \n }\n \n iterator insert(const_iterator hint, value_type&& value) { \n return m_ht.insert_hint(hint, std::move(value)); \n }\n \n template\n void insert(InputIt first, InputIt last) { \n m_ht.insert(first, last);\n }\n \n void insert(std::initializer_list ilist) { \n m_ht.insert(ilist.begin(), ilist.end()); \n }\n\n \n \n \n /**\n * Due to the way elements are stored, emplace will need to move or copy the key-value once.\n * The method is equivalent to insert(value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n std::pair emplace(Args&&... args) { \n return m_ht.emplace(std::forward(args)...); \n }\n \n \n \n /**\n * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.\n * The method is equivalent to insert(hint, value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n iterator emplace_hint(const_iterator hint, Args&&... args) {\n return m_ht.emplace_hint(hint, std::forward(args)...);\n }\n \n \n \n iterator erase(iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }\n size_type erase(const key_type& key) { return m_ht.erase(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n size_type erase(const key_type& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type erase(const K& key) { return m_ht.erase(key); }\n \n /**\n * @copydoc erase(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n template::value>::type* = nullptr> \n size_type erase(const K& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n \n \n void swap(robin_set& other) { other.m_ht.swap(m_ht); }\n \n \n \n /*\n * Lookup\n */\n size_type count(const Key& key) const { return m_ht.count(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n size_type count(const Key& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type count(const K& key) const { return m_ht.count(key); }\n \n /**\n * @copydoc count(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }\n \n \n \n \n iterator find(const Key& key) { return m_ht.find(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n const_iterator find(const Key& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const Key& key, std::size_t precalculated_hash)\n */\n const_iterator find(const Key& key, std::size_t precalculated_hash) const { return m_ht.find(key, precalculated_hash); }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key) { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n /**\n * @copydoc find(const K& key)\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key, std::size_t precalculated_hash) const { return m_ht.find(key, precalculated_hash); }\n \n \n \n \n bool contains(const Key& key) const { return m_ht.contains(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n bool contains(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key) const { return m_ht.contains(key); }\n \n /**\n * @copydoc contains(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n \n \n \n std::pair equal_range(const Key& key) { return m_ht.equal_range(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n std::pair equal_range(const Key& key) const { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */ \n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n /**\n * @copydoc equal_range(const K& key)\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) const { return m_ht.equal_range(key); }\n\n /**\n * @copydoc equal_range(const K& key, std::size_t precalculated_hash)\n */ \n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n \n \n\n /*\n * Bucket interface \n */\n size_type bucket_count() const { return m_ht.bucket_count(); }\n size_type max_bucket_count() const { return m_ht.max_bucket_count(); }\n \n \n /*\n * Hash policy \n */\n float load_factor() const { return m_ht.load_factor(); }\n \n float min_load_factor() const { return m_ht.min_load_factor(); }\n float max_load_factor() const { return m_ht.max_load_factor(); }\n \n /**\n * Set the `min_load_factor` to `ml`. When the `load_factor` of the set goes\n * below `min_load_factor` after some erase operations, the set will be\n * shrunk when an insertion occurs. The erase method itself never shrinks\n * the set.\n * \n * The default value of `min_load_factor` is 0.0f, the set never shrinks by default.\n */\n void min_load_factor(float ml) { m_ht.min_load_factor(ml); }\n void max_load_factor(float ml) { m_ht.max_load_factor(ml); }\n \n void rehash(size_type count) { m_ht.rehash(count); }\n void reserve(size_type count) { m_ht.reserve(count); }\n \n \n /*\n * Observers\n */\n hasher hash_function() const { return m_ht.hash_function(); }\n key_equal key_eq() const { return m_ht.key_eq(); }\n \n \n /*\n * Other\n */\n \n /**\n * Convert a const_iterator to an iterator.\n */\n iterator mutable_iterator(const_iterator pos) {\n return m_ht.mutable_iterator(pos);\n }\n \n friend bool operator==(const robin_set& lhs, const robin_set& rhs) {\n if(lhs.size() != rhs.size()) {\n return false;\n }\n \n for(const auto& element_lhs: lhs) {\n const auto it_element_rhs = rhs.find(element_lhs);\n if(it_element_rhs == rhs.cend()) {\n return false;\n }\n }\n \n return true;\n }\n\n friend bool operator!=(const robin_set& lhs, const robin_set& rhs) {\n return !operator==(lhs, rhs);\n }\n\n friend void swap(robin_set& lhs, robin_set& rhs) {\n lhs.swap(rhs);\n }\n \nprivate:\n ht m_ht; \n};\n\n\n/**\n * Same as `tsl::robin_set`.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator,\n bool StoreHash = false>\nusing robin_pg_set = robin_set;\n\n} // end namespace tsl\n\n#endif\n \n"], ["/voxel_svio/include/mapManagement.h", "class mapManagement {\n public:\n static bool addPointToVoxel(voxelHashMap &voxel_map, std::shared_ptr map_point_ptr, double voxel_size, int max_num_points_in_voxel, \n double min_distance_points, pcl::PointCloud::Ptr voxels_history) {\n\tEigen::Vector3d point = map_point_ptr->getPointXYZ(false);\n\n\tshort kx = static_cast(point[0] / voxel_size);\n\tshort ky = static_cast(point[1] / voxel_size);\n\tshort kz = static_cast(point[2] / voxel_size);\n\n\tmap_point_ptr->voxel_idx[0] = kx;\n\tmap_point_ptr->voxel_idx[1] = ky;\n\tmap_point_ptr->voxel_idx[2] = kz;\n\n\tvoxelHashMap::iterator search = voxel_map.find(voxel(kx, ky, kz));\n\n\tif(search != voxel_map.end())\n\t{\n\t\tauto &voxel_block = (search.value());\n\n\t\tif(!voxel_block.IsFull())\n\t\t{\n\t\t\tdouble sq_dist_min_to_points = 10 * voxel_size * voxel_size;\n\t\t\tfor (int i(0); i < voxel_block.NumPoints(); ++i)\n\t\t\t{\n\t\t\t\tauto &map_point_ptr_ = voxel_block.points[i];\n\t\t\t\tdouble sq_dist = (map_point_ptr_->getPointXYZ(false) - point).squaredNorm();\n\n\t\t\t\tif (sq_dist < sq_dist_min_to_points)\n\t\t\t\t{\n\t\t\t\t\tsq_dist_min_to_points = sq_dist;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(sq_dist_min_to_points > (min_distance_points * min_distance_points))\n\t\t\t{\n\t\t\t\tvoxel_block.AddPoint(map_point_ptr);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\telse\n\t{\n\t\tvoxelBlock block(max_num_points_in_voxel);\n\t\tblock.AddPoint(map_point_ptr);\n\t\tvoxel_map[voxel(kx, ky, kz)] = std::move(block);\n\n\t\t// display\n pcl::PointXYZI point_temp;\n\n point_temp.x = kx >= 0 ? (kx + 0.5) * voxel_size : (kx - 0.5) * voxel_size;\n point_temp.y = ky >= 0 ? (ky + 0.5) * voxel_size : (ky - 0.5) * voxel_size;\n point_temp.z = kz >= 0 ? (ky + 0.5) * voxel_size : (kz - 0.5) * voxel_size;\n point_temp.intensity = 1;\n\n voxels_history->points.push_back(point_temp);\n // display\n\n\t\treturn true;\n }\n}\n static void changeHostVoxel(voxelHashMap &voxel_map, std::shared_ptr map_point_ptr, double voxel_size, int max_num_points_in_voxel, \n double min_distance_points, pcl::PointCloud::Ptr voxels_history) {\n\tEigen::Vector3d point = map_point_ptr->getPointXYZ(false);\n\n\tshort kx = static_cast(point[0] / voxel_size);\n\tshort ky = static_cast(point[1] / voxel_size);\n\tshort kz = static_cast(point[2] / voxel_size);\n\n\tif (kx == map_point_ptr->voxel_idx[0] && ky == map_point_ptr->voxel_idx[1] && kz == map_point_ptr->voxel_idx[2]) return;\n\n\tvoxelHashMap::iterator search_old = voxel_map.find(voxel(map_point_ptr->voxel_idx[0], map_point_ptr->voxel_idx[1], map_point_ptr->voxel_idx[2]));\n\n\tauto &voxel_block_old = (search_old.value());\n\tauto it_old = voxel_block_old.points.begin();\n\n\tbool has_found = false;\n\n\twhile (it_old != voxel_block_old.points.end())\n\t{\n\t\tif ((*it_old) == map_point_ptr)\n\t\t{\n\t\t\thas_found = true;\n\t\t\tit_old = voxel_block_old.points.erase(it_old);\n\t\t\tbreak;\n\t\t}\n\n\t\tit_old++;\n\t}\n\n\tassert(has_found);\n\n\tmap_point_ptr->voxel_idx[0] = kx;\n\tmap_point_ptr->voxel_idx[1] = ky;\n\tmap_point_ptr->voxel_idx[2] = kz;\n\n\tvoxelHashMap::iterator search_new = voxel_map.find(voxel(kx, ky, kz));\n\n\tif(search_new != voxel_map.end())\n\t{\n\t\tauto &voxel_block_new = (search_new.value());\n\t\tvoxel_block_new.AddPoint(map_point_ptr);\n\t}\n\telse\n\t{\n\t\tvoxelBlock block_temp(max_num_points_in_voxel);\n\t\tblock_temp.AddPoint(map_point_ptr);\n\t\tvoxel_map[voxel(kx, ky, kz)] = std::move(block_temp);\n\n\t\t// display\n pcl::PointXYZI point_temp;\n\n point_temp.x = kx >= 0 ? (kx + 0.5) * voxel_size : (kx - 0.5) * voxel_size;\n point_temp.y = ky >= 0 ? (ky + 0.5) * voxel_size : (ky - 0.5) * voxel_size;\n point_temp.z = kz >= 0 ? (kz + 0.5) * voxel_size : (kz - 0.5) * voxel_size;\n point_temp.intensity = 1;\n\n voxels_history->points.push_back(point_temp);\n // display\n\t}\n}\n static void deleteFromVoxel(voxelHashMap &voxel_map, std::shared_ptr map_point_ptr) {\n\tvoxelHashMap::iterator search = voxel_map.find(voxel(map_point_ptr->voxel_idx[0], map_point_ptr->voxel_idx[1], map_point_ptr->voxel_idx[2]));\n\n\tauto &voxel_block = (search.value());\n\tauto it = voxel_block.points.begin();\n\n\tbool has_found = false;\n\n\twhile (it != voxel_block.points.end())\n\t{\n\t\tif (*it == map_point_ptr)\n\t\t{\n\t\t\thas_found = true;\n\t\t\tit = voxel_block.points.erase(it);\n\t\t\tbreak;\n\t\t}\n\n\t\tit++;\n\t}\n\n\tassert(has_found);\n}\n private:\n mapManagement();\n};"], ["/voxel_svio/include/initializerHelper.h", "class initializerHelper {\n public:\n static imuData interpolateData(const imuData &imu_1, const imuData &imu_2, double timestamp) {\n\tdouble lambda = (timestamp - imu_1.timestamp) / (imu_2.timestamp - imu_1.timestamp);\n\n\timuData data;\n\tdata.timestamp = timestamp;\n\tdata.acc = (1 - lambda) * imu_1.acc + lambda * imu_2.acc;\n\tdata.gyr = (1 - lambda) * imu_1.gyr + lambda * imu_2.gyr;\n\n\treturn data;\n}\n static std::vector selectImuReadings(const std::vector &imu_data_tmp, double time_0, double time_1) {\n\tstd::vector prop_data;\n\n\tif (imu_data_tmp.empty()) return prop_data;\n\n\tfor (size_t i = 0; i < imu_data_tmp.size() - 1; i++)\n\t{\n\t\tif (imu_data_tmp.at(i + 1).timestamp > time_0 && imu_data_tmp.at(i).timestamp < time_0)\n\t\t{\n\t\t\timuData data = interpolateData(imu_data_tmp.at(i), imu_data_tmp.at(i + 1), time_0);\n\t\t\tprop_data.push_back(data);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (imu_data_tmp.at(i).timestamp >= time_0 && imu_data_tmp.at(i + 1).timestamp <= time_1)\n\t\t{\n\t\t\tprop_data.push_back(imu_data_tmp.at(i));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (imu_data_tmp.at(i + 1).timestamp > time_1)\n\t\t{\n\t\t\tif (imu_data_tmp.at(i).timestamp > time_1 && i == 0)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if (imu_data_tmp.at(i).timestamp > time_1)\n\t\t\t{\n\t\t\t\timuData data = interpolateData(imu_data_tmp.at(i - 1), imu_data_tmp.at(i), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tprop_data.push_back(imu_data_tmp.at(i));\n\t\t\t}\n \n\t\t\tif (prop_data.at(prop_data.size() - 1).timestamp != time_1)\n\t\t\t{\n\t\t\t\timuData data = interpolateData(imu_data_tmp.at(i), imu_data_tmp.at(i + 1), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\t\t\t\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (prop_data.empty()) return prop_data;\n\n\tfor (size_t i = 0; i < prop_data.size() - 1; i++)\n\t{\n\t\tif (std::abs(prop_data.at(i + 1).timestamp - prop_data.at(i).timestamp) < 1e-12)\n\t\t{\n\t\t\tprop_data.erase(prop_data.begin() + i);\n\t\t\ti--;\n\t\t}\n\t}\n\n\treturn prop_data;\n}\n static void gramSchmidt(const Eigen::Vector3d &gravity_inI, Eigen::Matrix3d &R_GtoI) {\n\tEigen::Vector3d z_axis = gravity_inI / gravity_inI.norm();\n\tEigen::Vector3d x_axis, y_axis;\n\tEigen::Vector3d e_1(1.0, 0.0, 0.0);\n\tEigen::Vector3d e_2(0.0, 1.0, 0.0);\n\n\tdouble inner1 = e_1.dot(z_axis) / z_axis.norm();\n\tdouble inner2 = e_2.dot(z_axis) / z_axis.norm();\n\n\tif (fabs(inner1) < fabs(inner2))\n\t{\n\t\tx_axis = z_axis.cross(e_1);\n\t\tx_axis = x_axis / x_axis.norm();\n\t\ty_axis = z_axis.cross(x_axis);\n\t\ty_axis = y_axis / y_axis.norm();\n\t}\n\telse\n\t{\n\t\tx_axis = z_axis.cross(e_2);\n\t\tx_axis = x_axis / x_axis.norm();\n\t\ty_axis = z_axis.cross(x_axis);\n\t\ty_axis = y_axis / y_axis.norm();\n\t}\n\n\tR_GtoI.block(0, 0, 3, 1) = x_axis;\n\tR_GtoI.block(0, 1, 3, 1) = y_axis;\n\tR_GtoI.block(0, 2, 3, 1) = z_axis;\n}\n static Eigen::Matrix computeDongsiCoeff(Eigen::MatrixXd &D, const Eigen::MatrixXd &d, double gravity_mag) {\n\tassert(D.rows() == 3);\n\tassert(D.cols() == 3);\n\tassert(d.rows() == 3);\n\tdouble D1_1 = D(0, 0), D1_2 = D(0, 1), D1_3 = D(0, 2);\n\tdouble D2_1 = D(1, 0), D2_2 = D(1, 1), D2_3 = D(1, 2);\n\tdouble D3_1 = D(2, 0), D3_2 = D(2, 1), D3_3 = D(2, 2);\n\tdouble d1 = d(0, 0), d2 = d(1, 0), d3 = d(2, 0);\n\tdouble g = gravity_mag;\n\n\tdouble D1_1_sq = D1_1 * D1_1, D1_2_sq = D1_2 * D1_2, D1_3_sq = D1_3 * D1_3;\n\tdouble D2_1_sq = D2_1 * D2_1, D2_2_sq = D2_2 * D2_2, D2_3_sq = D2_3 * D2_3;\n\tdouble D3_1_sq = D3_1 * D3_1, D3_2_sq = D3_2 * D3_2, D3_3_sq = D3_3 * D3_3;\n\tdouble d1_sq = d1 * d1, d2_sq = d2 * d2, d3_sq = d3 * d3;\n\tdouble g_sq = g * g;\n\n\tEigen::Matrix coeff = Eigen::Matrix::Zero();\n\n\tcoeff(6) = -(-D1_1_sq * D2_2_sq * D3_3_sq * g_sq + D1_1_sq * D2_2_sq * d3_sq + 2 * D1_1_sq * D2_2 * D2_3 * D3_2 * D3_3 * g_sq -\n\t\t\t\tD1_1_sq * D2_2 * D2_3 * d2 * d3 - D1_1_sq * D2_2 * D3_2 * d2 * d3 - D1_1_sq * D2_3_sq * D3_2_sq * g_sq +\n\t\t\t\tD1_1_sq * D2_3 * D3_2 * d2_sq + D1_1_sq * D2_3 * D3_2 * d3_sq - D1_1_sq * D2_3 * D3_3 * d2 * d3 -\n\t\t\t\tD1_1_sq * D3_2 * D3_3 * d2 * d3 + D1_1_sq * D3_3_sq * d2_sq + 2 * D1_1 * D1_2 * D2_1 * D2_2 * D3_3_sq * g_sq -\n\t\t\t\t2 * D1_1 * D1_2 * D2_1 * D2_2 * d3_sq - 2 * D1_1 * D1_2 * D2_1 * D2_3 * D3_2 * D3_3 * g_sq + D1_1 * D1_2 * D2_1 * D2_3 * d2 * d3 +\n\t\t\t\tD1_1 * D1_2 * D2_1 * D3_2 * d2 * d3 - 2 * D1_1 * D1_2 * D2_2 * D2_3 * D3_1 * D3_3 * g_sq + D1_1 * D1_2 * D2_2 * D2_3 * d1 * d3 +\n\t\t\t\tD1_1 * D1_2 * D2_2 * D3_1 * d2 * d3 + 2 * D1_1 * D1_2 * D2_3_sq * D3_1 * D3_2 * g_sq - D1_1 * D1_2 * D2_3 * D3_1 * d2_sq -\n\t\t\t\tD1_1 * D1_2 * D2_3 * D3_1 * d3_sq - D1_1 * D1_2 * D2_3 * D3_2 * d1 * d2 + D1_1 * D1_2 * D2_3 * D3_3 * d1 * d3 +\n\t\t\t\tD1_1 * D1_2 * D3_1 * D3_3 * d2 * d3 - D1_1 * D1_2 * D3_3_sq * d1 * d2 - 2 * D1_1 * D1_3 * D2_1 * D2_2 * D3_2 * D3_3 * g_sq +\n\t\t\t\tD1_1 * D1_3 * D2_1 * D2_2 * d2 * d3 + 2 * D1_1 * D1_3 * D2_1 * D2_3 * D3_2_sq * g_sq - D1_1 * D1_3 * D2_1 * D3_2 * d2_sq -\n\t\t\t\tD1_1 * D1_3 * D2_1 * D3_2 * d3_sq + D1_1 * D1_3 * D2_1 * D3_3 * d2 * d3 + 2 * D1_1 * D1_3 * D2_2_sq * D3_1 * D3_3 * g_sq -\n\t\t\t\tD1_1 * D1_3 * D2_2_sq * d1 * d3 - 2 * D1_1 * D1_3 * D2_2 * D2_3 * D3_1 * D3_2 * g_sq + D1_1 * D1_3 * D2_2 * D3_2 * d1 * d2 +\n\t\t\t\tD1_1 * D1_3 * D2_3 * D3_1 * d2 * d3 - D1_1 * D1_3 * D2_3 * D3_2 * d1 * d3 + D1_1 * D1_3 * D3_1 * D3_2 * d2 * d3 -\n\t\t\t\t2 * D1_1 * D1_3 * D3_1 * D3_3 * d2_sq + D1_1 * D1_3 * D3_2 * D3_3 * d1 * d2 + D1_1 * D2_1 * D2_2 * D3_2 * d1 * d3 -\n\t\t\t\tD1_1 * D2_1 * D2_3 * D3_2 * d1 * d2 + D1_1 * D2_1 * D3_2 * D3_3 * d1 * d3 - D1_1 * D2_1 * D3_3_sq * d1 * d2 -\n\t\t\t\tD1_1 * D2_2_sq * D3_1 * d1 * d3 + D1_1 * D2_2 * D2_3 * D3_1 * d1 * d2 - D1_1 * D2_3 * D3_1 * D3_2 * d1 * d3 +\n\t\t\t\tD1_1 * D2_3 * D3_1 * D3_3 * d1 * d2 - D1_2_sq * D2_1_sq * D3_3_sq * g_sq + D1_2_sq * D2_1_sq * d3_sq +\n\t\t\t\t2 * D1_2_sq * D2_1 * D2_3 * D3_1 * D3_3 * g_sq - D1_2_sq * D2_1 * D2_3 * d1 * d3 - D1_2_sq * D2_1 * D3_1 * d2 * d3 -\n\t\t\t\tD1_2_sq * D2_3_sq * D3_1_sq * g_sq + D1_2_sq * D2_3 * D3_1 * d1 * d2 + 2 * D1_2 * D1_3 * D2_1_sq * D3_2 * D3_3 * g_sq -\n\t\t\t\tD1_2 * D1_3 * D2_1_sq * d2 * d3 - 2 * D1_2 * D1_3 * D2_1 * D2_2 * D3_1 * D3_3 * g_sq + D1_2 * D1_3 * D2_1 * D2_2 * d1 * d3 -\n\t\t\t\t2 * D1_2 * D1_3 * D2_1 * D2_3 * D3_1 * D3_2 * g_sq + D1_2 * D1_3 * D2_1 * D3_1 * d2_sq + D1_2 * D1_3 * D2_1 * D3_1 * d3_sq -\n\t\t\t\tD1_2 * D1_3 * D2_1 * D3_3 * d1 * d3 + 2 * D1_2 * D1_3 * D2_2 * D2_3 * D3_1_sq * g_sq - D1_2 * D1_3 * D2_2 * D3_1 * d1 * d2 -\n\t\t\t\tD1_2 * D1_3 * D3_1_sq * d2 * d3 + D1_2 * D1_3 * D3_1 * D3_3 * d1 * d2 - D1_2 * D2_1_sq * D3_2 * d1 * d3 +\n\t\t\t\tD1_2 * D2_1 * D2_2 * D3_1 * d1 * d3 + D1_2 * D2_1 * D2_3 * D3_2 * d1_sq + D1_2 * D2_1 * D2_3 * D3_2 * d3_sq -\n\t\t\t\tD1_2 * D2_1 * D2_3 * D3_3 * d2 * d3 - D1_2 * D2_1 * D3_1 * D3_3 * d1 * d3 - D1_2 * D2_1 * D3_2 * D3_3 * d2 * d3 +\n\t\t\t\tD1_2 * D2_1 * D3_3_sq * d1_sq + D1_2 * D2_1 * D3_3_sq * d2_sq - D1_2 * D2_2 * D2_3 * D3_1 * d1_sq -\n\t\t\t\tD1_2 * D2_2 * D2_3 * D3_1 * d3_sq + D1_2 * D2_2 * D2_3 * D3_3 * d1 * d3 + D1_2 * D2_2 * D3_1 * D3_3 * d2 * d3 -\n\t\t\t\tD1_2 * D2_2 * D3_3_sq * d1 * d2 + D1_2 * D2_3_sq * D3_1 * d2 * d3 - D1_2 * D2_3_sq * D3_2 * d1 * d3 +\n\t\t\t\tD1_2 * D2_3 * D3_1_sq * d1 * d3 - D1_2 * D2_3 * D3_1 * D3_3 * d1_sq - D1_2 * D2_3 * D3_1 * D3_3 * d2_sq +\n\t\t\t\tD1_2 * D2_3 * D3_2 * D3_3 * d1 * d2 - D1_3_sq * D2_1_sq * D3_2_sq * g_sq + 2 * D1_3_sq * D2_1 * D2_2 * D3_1 * D3_2 * g_sq -\n\t\t\t\tD1_3_sq * D2_1 * D3_1 * d2 * d3 + D1_3_sq * D2_1 * D3_2 * d1 * d3 - D1_3_sq * D2_2_sq * D3_1_sq * g_sq +\n\t\t\t\tD1_3_sq * D3_1_sq * d2_sq - D1_3_sq * D3_1 * D3_2 * d1 * d2 + D1_3 * D2_1_sq * D3_2 * d1 * d2 -\n\t\t\t\tD1_3 * D2_1 * D2_2 * D3_1 * d1 * d2 - D1_3 * D2_1 * D2_2 * D3_2 * d1_sq - D1_3 * D2_1 * D2_2 * D3_2 * d3_sq +\n\t\t\t\tD1_3 * D2_1 * D2_2 * D3_3 * d2 * d3 + D1_3 * D2_1 * D3_1 * D3_3 * d1 * d2 + D1_3 * D2_1 * D3_2_sq * d2 * d3 -\n\t\t\t\tD1_3 * D2_1 * D3_2 * D3_3 * d1_sq - D1_3 * D2_1 * D3_2 * D3_3 * d2_sq + D1_3 * D2_2_sq * D3_1 * d1_sq +\n\t\t\t\tD1_3 * D2_2_sq * D3_1 * d3_sq - D1_3 * D2_2_sq * D3_3 * d1 * d3 - D1_3 * D2_2 * D2_3 * D3_1 * d2 * d3 +\n\t\t\t\tD1_3 * D2_2 * D2_3 * D3_2 * d1 * d3 - D1_3 * D2_2 * D3_1 * D3_2 * d2 * d3 + D1_3 * D2_2 * D3_2 * D3_3 * d1 * d2 -\n\t\t\t\tD1_3 * D2_3 * D3_1_sq * d1 * d2 + D1_3 * D2_3 * D3_1 * D3_2 * d1_sq + D1_3 * D2_3 * D3_1 * D3_2 * d2_sq -\n\t\t\t\tD1_3 * D2_3 * D3_2_sq * d1 * d2 + D2_1 * D2_2 * D3_2 * D3_3 * d1 * d3 - D2_1 * D2_2 * D3_3_sq * d1 * d2 -\n\t\t\t\tD2_1 * D2_3 * D3_2_sq * d1 * d3 + D2_1 * D2_3 * D3_2 * D3_3 * d1 * d2 - D2_2_sq * D3_1 * D3_3 * d1 * d3 +\n\t\t\t\tD2_2_sq * D3_3_sq * d1_sq + D2_2 * D2_3 * D3_1 * D3_2 * d1 * d3 + D2_2 * D2_3 * D3_1 * D3_3 * d1 * d2 -\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * D3_3 * d1_sq - D2_3_sq * D3_1 * D3_2 * d1 * d2 + D2_3_sq * D3_2_sq * d1_sq) /\n\t\t\t\tg_sq;\n\n\tcoeff(5) = (-(2 * D1_1_sq * D2_2_sq * D3_3 * g_sq - 2 * D1_1_sq * D2_2 * D2_3 * D3_2 * g_sq + 2 * D1_1_sq * D2_2 * D3_3_sq * g_sq -\n\t\t\t\t2 * D1_1_sq * D2_2 * d3_sq - 2 * D1_1_sq * D2_3 * D3_2 * D3_3 * g_sq + 2 * D1_1_sq * D2_3 * d2 * d3 +\n\t\t\t\t2 * D1_1_sq * D3_2 * d2 * d3 - 2 * D1_1_sq * D3_3 * d2_sq - 4 * D1_1 * D1_2 * D2_1 * D2_2 * D3_3 * g_sq +\n\t\t\t\t2 * D1_1 * D1_2 * D2_1 * D2_3 * D3_2 * g_sq - 2 * D1_1 * D1_2 * D2_1 * D3_3_sq * g_sq + 2 * D1_1 * D1_2 * D2_1 * d3_sq +\n\t\t\t\t2 * D1_1 * D1_2 * D2_2 * D2_3 * D3_1 * g_sq + 2 * D1_1 * D1_2 * D2_3 * D3_1 * D3_3 * g_sq - 2 * D1_1 * D1_2 * D2_3 * d1 * d3 -\n\t\t\t\t2 * D1_1 * D1_2 * D3_1 * d2 * d3 + 2 * D1_1 * D1_2 * D3_3 * d1 * d2 + 2 * D1_1 * D1_3 * D2_1 * D2_2 * D3_2 * g_sq +\n\t\t\t\t2 * D1_1 * D1_3 * D2_1 * D3_2 * D3_3 * g_sq - 2 * D1_1 * D1_3 * D2_1 * d2 * d3 - 2 * D1_1 * D1_3 * D2_2_sq * D3_1 * g_sq -\n\t\t\t\t4 * D1_1 * D1_3 * D2_2 * D3_1 * D3_3 * g_sq + 2 * D1_1 * D1_3 * D2_2 * d1 * d3 + 2 * D1_1 * D1_3 * D2_3 * D3_1 * D3_2 * g_sq +\n\t\t\t\t2 * D1_1 * D1_3 * D3_1 * d2_sq - 2 * D1_1 * D1_3 * D3_2 * d1 * d2 - 2 * D1_1 * D2_1 * D3_2 * d1 * d3 +\n\t\t\t\t2 * D1_1 * D2_1 * D3_3 * d1 * d2 + 2 * D1_1 * D2_2_sq * D3_3_sq * g_sq - 2 * D1_1 * D2_2_sq * d3_sq -\n\t\t\t\t4 * D1_1 * D2_2 * D2_3 * D3_2 * D3_3 * g_sq + 2 * D1_1 * D2_2 * D2_3 * d2 * d3 + 2 * D1_1 * D2_2 * D3_1 * d1 * d3 +\n\t\t\t\t2 * D1_1 * D2_2 * D3_2 * d2 * d3 + 2 * D1_1 * D2_3_sq * D3_2_sq * g_sq - 2 * D1_1 * D2_3 * D3_1 * d1 * d2 -\n\t\t\t\t2 * D1_1 * D2_3 * D3_2 * d2_sq - 2 * D1_1 * D2_3 * D3_2 * d3_sq + 2 * D1_1 * D2_3 * D3_3 * d2 * d3 +\n\t\t\t\t2 * D1_1 * D3_2 * D3_3 * d2 * d3 - 2 * D1_1 * D3_3_sq * d2_sq + 2 * D1_2_sq * D2_1_sq * D3_3 * g_sq -\n\t\t\t\t2 * D1_2_sq * D2_1 * D2_3 * D3_1 * g_sq - 2 * D1_2 * D1_3 * D2_1_sq * D3_2 * g_sq + 2 * D1_2 * D1_3 * D2_1 * D2_2 * D3_1 * g_sq +\n\t\t\t\t2 * D1_2 * D1_3 * D2_1 * D3_1 * D3_3 * g_sq - 2 * D1_2 * D1_3 * D2_3 * D3_1_sq * g_sq - 2 * D1_2 * D2_1 * D2_2 * D3_3_sq * g_sq +\n\t\t\t\t2 * D1_2 * D2_1 * D2_2 * d3_sq + 2 * D1_2 * D2_1 * D2_3 * D3_2 * D3_3 * g_sq - 2 * D1_2 * D2_1 * D3_3 * d1_sq -\n\t\t\t\t2 * D1_2 * D2_1 * D3_3 * d2_sq + 2 * D1_2 * D2_2 * D2_3 * D3_1 * D3_3 * g_sq - 2 * D1_2 * D2_2 * D2_3 * d1 * d3 -\n\t\t\t\t2 * D1_2 * D2_2 * D3_1 * d2 * d3 + 2 * D1_2 * D2_2 * D3_3 * d1 * d2 - 2 * D1_2 * D2_3_sq * D3_1 * D3_2 * g_sq +\n\t\t\t\t2 * D1_2 * D2_3 * D3_1 * d1_sq + 2 * D1_2 * D2_3 * D3_1 * d2_sq + 2 * D1_2 * D2_3 * D3_1 * d3_sq -\n\t\t\t\t2 * D1_2 * D2_3 * D3_3 * d1 * d3 - 2 * D1_2 * D3_1 * D3_3 * d2 * d3 + 2 * D1_2 * D3_3_sq * d1 * d2 -\n\t\t\t\t2 * D1_3_sq * D2_1 * D3_1 * D3_2 * g_sq + 2 * D1_3_sq * D2_2 * D3_1_sq * g_sq + 2 * D1_3 * D2_1 * D2_2 * D3_2 * D3_3 * g_sq -\n\t\t\t\t2 * D1_3 * D2_1 * D2_2 * d2 * d3 - 2 * D1_3 * D2_1 * D2_3 * D3_2_sq * g_sq + 2 * D1_3 * D2_1 * D3_2 * d1_sq +\n\t\t\t\t2 * D1_3 * D2_1 * D3_2 * d2_sq + 2 * D1_3 * D2_1 * D3_2 * d3_sq - 2 * D1_3 * D2_1 * D3_3 * d2 * d3 -\n\t\t\t\t2 * D1_3 * D2_2_sq * D3_1 * D3_3 * g_sq + 2 * D1_3 * D2_2_sq * d1 * d3 + 2 * D1_3 * D2_2 * D2_3 * D3_1 * D3_2 * g_sq -\n\t\t\t\t2 * D1_3 * D2_2 * D3_1 * d1_sq - 2 * D1_3 * D2_2 * D3_1 * d3_sq - 2 * D1_3 * D2_2 * D3_2 * d1 * d2 +\n\t\t\t\t2 * D1_3 * D2_2 * D3_3 * d1 * d3 + 2 * D1_3 * D3_1 * D3_3 * d2_sq - 2 * D1_3 * D3_2 * D3_3 * d1 * d2 -\n\t\t\t\t2 * D2_1 * D2_2 * D3_2 * d1 * d3 + 2 * D2_1 * D2_2 * D3_3 * d1 * d2 - 2 * D2_1 * D3_2 * D3_3 * d1 * d3 +\n\t\t\t\t2 * D2_1 * D3_3_sq * d1 * d2 + 2 * D2_2_sq * D3_1 * d1 * d3 - 2 * D2_2_sq * D3_3 * d1_sq - 2 * D2_2 * D2_3 * D3_1 * d1 * d2 +\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * d1_sq + 2 * D2_2 * D3_1 * D3_3 * d1 * d3 - 2 * D2_2 * D3_3_sq * d1_sq -\n\t\t\t\t2 * D2_3 * D3_1 * D3_3 * d1 * d2 + 2 * D2_3 * D3_2 * D3_3 * d1_sq) /\n\t\t\t\tg_sq);\n\n\tcoeff(4) = ((D1_1_sq * D2_2_sq * g_sq + 4 * D1_1_sq * D2_2 * D3_3 * g_sq - 2 * D1_1_sq * D2_3 * D3_2 * g_sq + D1_1_sq * D3_3_sq * g_sq -\n\t\t\t\tD1_1_sq * d2_sq - D1_1_sq * d3_sq - 2 * D1_1 * D1_2 * D2_1 * D2_2 * g_sq - 4 * D1_1 * D1_2 * D2_1 * D3_3 * g_sq +\n\t\t\t\t2 * D1_1 * D1_2 * D2_3 * D3_1 * g_sq + D1_1 * D1_2 * d1 * d2 + 2 * D1_1 * D1_3 * D2_1 * D3_2 * g_sq -\n\t\t\t\t4 * D1_1 * D1_3 * D2_2 * D3_1 * g_sq - 2 * D1_1 * D1_3 * D3_1 * D3_3 * g_sq + D1_1 * D1_3 * d1 * d3 + D1_1 * D2_1 * d1 * d2 +\n\t\t\t\t4 * D1_1 * D2_2_sq * D3_3 * g_sq - 4 * D1_1 * D2_2 * D2_3 * D3_2 * g_sq + 4 * D1_1 * D2_2 * D3_3_sq * g_sq -\n\t\t\t\t4 * D1_1 * D2_2 * d3_sq - 4 * D1_1 * D2_3 * D3_2 * D3_3 * g_sq + 4 * D1_1 * D2_3 * d2 * d3 + D1_1 * D3_1 * d1 * d3 +\n\t\t\t\t4 * D1_1 * D3_2 * d2 * d3 - 4 * D1_1 * D3_3 * d2_sq + D1_2_sq * D2_1_sq * g_sq + 2 * D1_2 * D1_3 * D2_1 * D3_1 * g_sq -\n\t\t\t\t4 * D1_2 * D2_1 * D2_2 * D3_3 * g_sq + 2 * D1_2 * D2_1 * D2_3 * D3_2 * g_sq - 2 * D1_2 * D2_1 * D3_3_sq * g_sq -\n\t\t\t\tD1_2 * D2_1 * d1_sq - D1_2 * D2_1 * d2_sq + 2 * D1_2 * D2_1 * d3_sq + 2 * D1_2 * D2_2 * D2_3 * D3_1 * g_sq +\n\t\t\t\tD1_2 * D2_2 * d1 * d2 + 2 * D1_2 * D2_3 * D3_1 * D3_3 * g_sq - 3 * D1_2 * D2_3 * d1 * d3 - 3 * D1_2 * D3_1 * d2 * d3 +\n\t\t\t\t4 * D1_2 * D3_3 * d1 * d2 + D1_3_sq * D3_1_sq * g_sq + 2 * D1_3 * D2_1 * D2_2 * D3_2 * g_sq +\n\t\t\t\t2 * D1_3 * D2_1 * D3_2 * D3_3 * g_sq - 3 * D1_3 * D2_1 * d2 * d3 - 2 * D1_3 * D2_2_sq * D3_1 * g_sq -\n\t\t\t\t4 * D1_3 * D2_2 * D3_1 * D3_3 * g_sq + 4 * D1_3 * D2_2 * d1 * d3 + 2 * D1_3 * D2_3 * D3_1 * D3_2 * g_sq - D1_3 * D3_1 * d1_sq +\n\t\t\t\t2 * D1_3 * D3_1 * d2_sq - D1_3 * D3_1 * d3_sq - 3 * D1_3 * D3_2 * d1 * d2 + D1_3 * D3_3 * d1 * d3 + D2_1 * D2_2 * d1 * d2 -\n\t\t\t\t3 * D2_1 * D3_2 * d1 * d3 + 4 * D2_1 * D3_3 * d1 * d2 + D2_2_sq * D3_3_sq * g_sq - D2_2_sq * d1_sq - D2_2_sq * d3_sq -\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * D3_3 * g_sq + D2_2 * D2_3 * d2 * d3 + 4 * D2_2 * D3_1 * d1 * d3 + D2_2 * D3_2 * d2 * d3 -\n\t\t\t\t4 * D2_2 * D3_3 * d1_sq + D2_3_sq * D3_2_sq * g_sq - 3 * D2_3 * D3_1 * d1 * d2 + 2 * D2_3 * D3_2 * d1_sq - D2_3 * D3_2 * d2_sq -\n\t\t\t\tD2_3 * D3_2 * d3_sq + D2_3 * D3_3 * d2 * d3 + D3_1 * D3_3 * d1 * d3 + D3_2 * D3_3 * d2 * d3 - D3_3_sq * d1_sq - D3_3_sq * d2_sq) /\n\t\t\t\tg_sq);\n \n\tcoeff(3) = ((2 * D1_1 * d2_sq + 2 * D1_1 * d3_sq + 2 * D2_2 * d1_sq + 2 * D2_2 * d3_sq + 2 * D3_3 * d1_sq + 2 * D3_3 * d2_sq -\n\t\t\t\t2 * D1_1 * D2_2_sq * g_sq - 2 * D1_1_sq * D2_2 * g_sq - 2 * D1_1 * D3_3_sq * g_sq - 2 * D1_1_sq * D3_3 * g_sq -\n\t\t\t\t2 * D2_2 * D3_3_sq * g_sq - 2 * D2_2_sq * D3_3 * g_sq - 2 * D1_2 * d1 * d2 - 2 * D1_3 * d1 * d3 - 2 * D2_1 * d1 * d2 -\n\t\t\t\t2 * D2_3 * d2 * d3 - 2 * D3_1 * d1 * d3 - 2 * D3_2 * d2 * d3 + 2 * D1_1 * D1_2 * D2_1 * g_sq + 2 * D1_1 * D1_3 * D3_1 * g_sq +\n\t\t\t\t2 * D1_2 * D2_1 * D2_2 * g_sq - 8 * D1_1 * D2_2 * D3_3 * g_sq + 4 * D1_1 * D2_3 * D3_2 * g_sq + 4 * D1_2 * D2_1 * D3_3 * g_sq -\n\t\t\t\t2 * D1_2 * D2_3 * D3_1 * g_sq - 2 * D1_3 * D2_1 * D3_2 * g_sq + 4 * D1_3 * D2_2 * D3_1 * g_sq + 2 * D1_3 * D3_1 * D3_3 * g_sq +\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * g_sq + 2 * D2_3 * D3_2 * D3_3 * g_sq) /\n\t\t\t\tg_sq);\n \n\tcoeff(2) = (-(d1_sq + d2_sq + d3_sq - D1_1_sq * g_sq - D2_2_sq * g_sq - D3_3_sq * g_sq - 4 * D1_1 * D2_2 * g_sq + 2 * D1_2 * D2_1 * g_sq -\n\t\t\t\t4 * D1_1 * D3_3 * g_sq + 2 * D1_3 * D3_1 * g_sq - 4 * D2_2 * D3_3 * g_sq + 2 * D2_3 * D3_2 * g_sq) /\n\t\t\t\tg_sq);\n \n\tcoeff(1) = (-(2 * D1_1 * g_sq + 2 * D2_2 * g_sq + 2 * D3_3 * g_sq) / g_sq);\n\t\n\tcoeff(0) = 1;\n\n\treturn coeff;\n}\n};"], ["/voxel_svio/include/pixelSelector.h", "class pixelSelector {\n public:\n EIGEN_MAKE_ALIGNED_OPERATOR_NEW;\n pixelSelector(int w, int h) {\n\trandom_pattern = new unsigned char[w * h];\n\n\tstd::srand(3141592);\n\n\tfor (int i = 0; i < w * h; i++)\n\t\trandom_pattern[i] = rand() & 0xFF;\n\n\tcurrent_potential = 3;\n\n\tgrad_hist = new int[100 * (1 + w / 32) * (1 + h / 32)];\n\tths = new float[(w / 32) * (h / 32) + 100];\n\tths_smoothed = new float[(w / 32) * (h / 32) + 100];\n\n\tallow_fast = false;\n\tgrad_hist_frame = nullptr;\n}\n ~pixelSelector() {\n\tdelete[] random_pattern;\n\tdelete[] grad_hist;\n\tdelete[] ths;\n\tdelete[] ths_smoothed;\n}\n int pixelSelectionLeft(std::shared_ptr fh, float density, int recursions_left = 1, float th_factor = 1) {\n\tfloat num_have = 0;\n\tfloat num_want = density;\n\tfloat quotia;\n\tint ideal_potential = current_potential;\n\n\t// if(fh != grad_hist_frame) makeHistsLeft(fh);\n\tmakeHistsLeft(fh);\n\n\tEigen::Vector3i n = this->selectLeft(fh, current_potential, th_factor);\n\n\tnum_have = n[0] + n[1] + n[2];\n\tquotia = num_want / num_have;\n\n\tfloat K = num_have * (current_potential + 1) * (current_potential + 1);\n\tideal_potential = sqrtf(K / num_want) - 1;\n\n\tif (ideal_potential < 1) ideal_potential = 1;\n\n\tif (recursions_left > 0 && quotia > 1.25 && current_potential > 1)\n\t{\n\t\tif(ideal_potential >= current_potential)\n\t\t\tideal_potential = current_potential - 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionLeft(fh, density, recursions_left - 1, th_factor);\n\t}\n\telse if(recursions_left > 0 && quotia < 0.25)\n\t{\n\t\tif(ideal_potential <= current_potential)\n\t\t\tideal_potential = current_potential + 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionLeft(fh, density, recursions_left - 1, th_factor);\n\t}\n\n\tint num_have_sub = num_have;\n\n\tif(quotia < 0.95)\n\t{\n\t\tint wh = wG[0] * hG[0];\n\t\tint rn = 0;\n\t\tunsigned char char_th = 255*quotia;\n\t\tfor(int i = 0; i < wh; i++)\n\t\t{\n\t\t\tif(fh->selected_pixels_left[i] != 0)\n\t\t\t{\n\t\t\t\tif(random_pattern[rn] > char_th )\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_left[i] = 0;\n\t\t\t\t\tnum_have_sub--;\n\t\t\t\t}\n\t\t\t\trn++;\n\t\t\t}\n\t\t}\n\t}\n\n\tcurrent_potential = ideal_potential;\n\n\treturn num_have_sub;\n}\n int pixelSelectionRight(std::shared_ptr fh, float density, int recursions_left = 1, float th_factor = 1) {\n\tfloat num_have = 0;\n\tfloat num_want = density;\n\tfloat quotia;\n\tint ideal_potential = current_potential;\n\n\t// if(fh != grad_hist_frame) makeHistsRight(fh);\n\tmakeHistsRight(fh);\n\n\tEigen::Vector3i n = this->selectRight(fh, current_potential, th_factor);\n\n\tnum_have = n[0] + n[1] + n[2];\n\tquotia = num_want / num_have;\n\n\tfloat K = num_have * (current_potential + 1) * (current_potential + 1);\n\tideal_potential = sqrtf(K / num_want) - 1;\n\n\tif (ideal_potential < 1) ideal_potential = 1;\n\n\tif (recursions_left > 0 && quotia > 1.25 && current_potential > 1)\n\t{\n\t\tif(ideal_potential >= current_potential)\n\t\t\tideal_potential = current_potential - 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionRight(fh, density, recursions_left - 1, th_factor);\n\t}\n\telse if (recursions_left > 0 && quotia < 0.25)\n\t{\n\t\tif(ideal_potential <= current_potential)\n\t\t\tideal_potential = current_potential + 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionRight(fh, density, recursions_left - 1, th_factor);\n\t}\n\n\tint num_have_sub = num_have;\n\n\tif (quotia < 0.95)\n\t{\n\t\tint wh = wG[0] * hG[0];\n\t\tint rn = 0;\n\t\tunsigned char char_th = 255*quotia;\n\t\tfor (int i = 0; i < wh; i++)\n\t\t{\n\t\t\tif (fh->selected_pixels_right[i] != 0)\n\t\t\t{\n\t\t\t\tif (random_pattern[rn] > char_th)\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_right[i] = 0;\n\t\t\t\t\tnum_have_sub--;\n\t\t\t\t}\n\t\t\t\trn++;\n\t\t\t}\n\t\t}\n\t}\n\n\tcurrent_potential = ideal_potential;\n\n\treturn num_have_sub;\n}\n private:\n void makeHistsLeft(std::shared_ptr fh) {\n\tgrad_hist_frame = fh;\n\tfloat* mapmax_0 = fh->abs_squared_grad_left[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tint w32 = w / 32;\n\tint h32 = h / 32;\n\tths_step = w32;\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat* map_0 = mapmax_0 + 32 * x + 32 * y * w;\n\t\t\tint* hist_0 = grad_hist;\n\n\t\t\tmemset(hist_0, 0, sizeof(int) * 50);\n\n\t\t\tfor(int j = 0; j < 32; j++)\n\t\t\t\tfor(int i = 0; i < 32; i++)\n\t\t\t\t{\n\t\t\t\t\tint it = i + 32 * x;\n\t\t\t\t\tint jt = j + 32 * y;\n\n\t\t\t\t\tif(it > w - 2 || jt > h - 2 || it < 1 || jt < 1) continue;\n\n\t\t\t\t\tint g = sqrtf(map_0[i + j * w]);\n\n\t\t\t\t\tif(g > 48) g = 48;\n\n\t\t\t\t\thist_0[g + 1]++;\n\t\t\t\t\thist_0[0]++;\n\t\t\t\t}\n\n\t\t\tths[x + y * w32] = computeHistQuantil(hist_0, setting_min_grad_hist_cut) + setting_min_grad_hist_add;\n\t\t}\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat sum = 0, num = 0;\n\t\t\tif(x > 0)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y - 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x - 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(x < w32 - 1)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y - 1) * w32];\n\t\t\t\t}\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(y > 0)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y - 1) * w32];\n\t\t\t}\n\n\t\t\tif(y < h32 - 1)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y + 1) * w32];\n\t\t\t}\n\n\t\t\tnum++;\n\t\t\tsum += ths[x + y * w32];\n\n\t\t\tths_smoothed[x + y * w32] = (sum / num) * (sum / num);\n\t\t}\n}\n void makeHistsRight(std::shared_ptr fh) {\n\tgrad_hist_frame = fh;\n\tfloat* mapmax_0 = fh->abs_squared_grad_right[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tint w32 = w / 32;\n\tint h32 = h / 32;\n\tths_step = w32;\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat* map_0 = mapmax_0 + 32 * x + 32 * y * w;\n\t\t\tint* hist_0 = grad_hist;\n\n\t\t\tmemset(hist_0, 0, sizeof(int) * 50);\n\n\t\t\tfor(int j = 0; j < 32; j++)\n\t\t\t\tfor(int i = 0; i < 32; i++)\n\t\t\t\t{\n\t\t\t\t\tint it = i + 32 * x;\n\t\t\t\t\tint jt = j + 32 * y;\n\n\t\t\t\t\tif(it > w - 2 || jt > h - 2 || it < 1 || jt < 1) continue;\n\n\t\t\t\t\tint g = sqrtf(map_0[i + j * w]);\n\n\t\t\t\t\tif(g > 48) g = 48;\n\n\t\t\t\t\thist_0[g + 1]++;\n\t\t\t\t\thist_0[0]++;\n\t\t\t\t}\n\n\t\t\tths[x + y * w32] = computeHistQuantil(hist_0, setting_min_grad_hist_cut) + setting_min_grad_hist_add;\n\t\t}\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat sum = 0, num = 0;\n\n\t\t\tif(x > 0)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y - 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x - 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(x < w32 - 1)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y - 1) * w32];\n\t\t\t\t}\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(y > 0)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y - 1) * w32];\n\t\t\t}\n\n\t\t\tif(y < h32 - 1)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y + 1) * w32];\n\t\t\t}\n\n\t\t\tnum++;\n\t\t\tsum += ths[x + y * w32];\n\n\t\t\tths_smoothed[x + y * w32] = (sum / num) * (sum / num);\n\t\t}\n}\n Eigen::Vector3i selectLeft(std::shared_ptr fh, int pot, float th_factor = 1) {\n\tEigen::Vector3f const * const map_0 = fh->dI_left;\n\n\tfloat* mapmax_0 = fh->abs_squared_grad_left[0];\n\tfloat* mapmax_1 = fh->abs_squared_grad_left[1];\n\tfloat* mapmax_2 = fh->abs_squared_grad_left[2];\n\n\tint w = wG[0];\n\tint w1 = wG[1];\n\tint w2 = wG[2];\n\tint h = hG[0];\n\n\tconst Eigen::Matrix directions[16] = {\n\t\tEigen::Matrix(0, 1.0000), \n\t\tEigen::Matrix(0.3827, 0.9239), \n\t\tEigen::Matrix(0.1951, 0.9808), \n\t\tEigen::Matrix(0.9239, 0.3827), \n\t\tEigen::Matrix(0.7071, 0.7071), \n\t\tEigen::Matrix(0.3827, -0.9239), \n\t\tEigen::Matrix(0.8315, 0.5556), \n\t\tEigen::Matrix(0.8315, -0.5556), \n\t\tEigen::Matrix(0.5556, -0.8315), \n\t\tEigen::Matrix(0.9808, 0.1951), \n\t\tEigen::Matrix(0.9239, -0.3827), \n\t\tEigen::Matrix(0.7071, -0.7071), \n\t\tEigen::Matrix(0.5556, 0.8315), \n\t\tEigen::Matrix(0.9808, -0.1951), \n\t\tEigen::Matrix(1.0000, 0.0000), \n\t\tEigen::Matrix(0.1951, -0.9808)};\n\n\tmemset(fh->selected_pixels_left, 0, w * h * sizeof(PixelSelectorStatus));\n\n\tfloat dw1 = setting_grad_down_weight_per_level;\n\tfloat dw2 = dw1 * dw1;\n\n\tint n3 = 0, n2 = 0, n4 = 0;\n\n\tfor(int y4 = 0; y4 < h; y4 += (4 * pot))\n\t{\n\t\tfor(int x4 = 0; x4 < w; x4 += (4 * pot))\n\t\t{\n\t\t\tint my3 = std::min((4 * pot), h - y4);\n\t\t\tint mx3 = std::min((4 * pot), w - x4);\n\t\t\tint best_idx_4 = -1;\n\t\t\tfloat best_val_4 = 0;\n\n\t\t\tEigen::Matrix dir4 = directions[random_pattern[n2] & 0xF];\n\n\t\t\tfor(int y3 = 0; y3 < my3; y3 += (2 * pot))\n\t\t\t{\n\t\t\t\tfor(int x3 = 0; x3 < mx3; x3 += (2 * pot))\n\t\t\t\t{\n\t\t\t\t\tint x34 = x3 + x4;\n\t\t\t\t\tint y34 = y3 + y4;\n\t\t\t\t\tint my2 = std::min((2 * pot), h - y34);\n\t\t\t\t\tint mx2 = std::min((2 * pot), w - x34);\n\t\t\t\t\t\n\t\t\t\t\tint best_idx_3 = -1;\n\t\t\t\t\tfloat best_val_3 = 0;\n\n\t\t\t\t\tEigen::Matrix dir3 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\tfor(int y2 = 0; y2 < my2; y2 += pot)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor(int x2 = 0; x2 < mx2; x2 += pot)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tint x234 = x2 + x34;\n\t\t\t\t\t\t\tint y234 = y2 + y34;\n\t\t\t\t\t\t\tint my1 = std::min(pot, h - y234);\n\t\t\t\t\t\t\tint mx1 = std::min(pot, w - x234);\n\t\t\t\t\t\n\t\t\t\t\t\t\tint best_idx_2 = -1;\n\t\t\t\t\t\t\tfloat best_val_2 = 0;\n\t\t\t\t\n\t\t\t\t\t\t\tEigen::Matrix dir2 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\t\t\tfor(int y1 = 0; y1 < my1; y1 += 1)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfor(int x1 = 0; x1 < mx1; x1 += 1)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tassert(x1 + x234 < w);\n\t\t\t\t\t\t\t\t\tassert(y1 + y234 < h);\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tint idx = x1 + x234 + w * (y1 + y234);\n\t\t\t\t\t\t\t\t\tint xf = x1 + x234;\n\t\t\t\t\t\t\t\t\tint yf = y1 + y234;\n\n\t\t\t\t\t\t\t\t\tif(xf < 4 || xf >= w - 5 || yf < 4 || yf > h - 4) continue;\n\n\t\t\t\t\t\t\t\t\tif (fh->mask_left[idx] == true) continue;\n\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_0 = ths_smoothed[(xf >> 5) + (yf >> 5) * ths_step];\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_1 = pixel_TH_0 * dw1;\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_2 = pixel_TH_1 * dw2;\n\n\t\t\t\t\t\t\t\t\tfloat ag_0 = mapmax_0[idx];\n\n\t\t\t\t\t\t\t\t\tif(ag_0 > pixel_TH_0 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir2)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_0;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_2)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_2 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_2 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = -2;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tif(best_idx_3 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_1 = mapmax_1[(int)(xf * 0.5f + 0.25f) + (int)(yf * 0.5f + 0.25f) * w1];\n\n\t\t\t\t\t\t\t\t\tif(ag_1 > pixel_TH_1 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir3)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_1;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_3)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_3 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\tif(best_idx_4 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_2 = mapmax_2[(int)(xf * 0.25f + 0.125) + (int)(yf * 0.25f + 0.125) * w2];\n\n\t\t\t\t\t\t\t\t\tif(ag_2 > pixel_TH_2 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir4)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_2;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_4)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_4 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = idx;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif(best_idx_2 > 0)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfh->selected_pixels_left[best_idx_2] = 1;\n\t\t\t\t\t\t\t\tbest_val_3 = 1e10;\n\t\t\t\t\t\t\t\tn2++;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif(best_idx_3 > 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tfh->selected_pixels_left[best_idx_3] = 2;\n\t\t\t\t\t\tbest_val_4 = 1e10;\n\t\t\t\t\t\tn3++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(best_idx_4 > 0)\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_left[best_idx_4] = 4;\n\t\t\t\t\tn4++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn Eigen::Vector3i(n2, n3, n4);\n}\n Eigen::Vector3i selectRight(std::shared_ptr fh, int pot, float th_factor = 1) {\n\tEigen::Vector3f const * const map_0 = fh->dI_right;\n\n\tfloat* mapmax_0 = fh->abs_squared_grad_right[0];\n\tfloat* mapmax_1 = fh->abs_squared_grad_right[1];\n\tfloat* mapmax_2 = fh->abs_squared_grad_right[2];\n\n\tint w = wG[0];\n\tint w1 = wG[1];\n\tint w2 = wG[2];\n\tint h = hG[0];\n\n\tconst Eigen::Matrix directions[16] = {\n\t\tEigen::Matrix(0, 1.0000), \n\t\tEigen::Matrix(0.3827, 0.9239), \n\t\tEigen::Matrix(0.1951, 0.9808), \n\t\tEigen::Matrix(0.9239, 0.3827), \n\t\tEigen::Matrix(0.7071, 0.7071), \n\t\tEigen::Matrix(0.3827, -0.9239), \n\t\tEigen::Matrix(0.8315, 0.5556), \n\t\tEigen::Matrix(0.8315, -0.5556), \n\t\tEigen::Matrix(0.5556, -0.8315), \n\t\tEigen::Matrix(0.9808, 0.1951), \n\t\tEigen::Matrix(0.9239, -0.3827), \n\t\tEigen::Matrix(0.7071, -0.7071), \n\t\tEigen::Matrix(0.5556, 0.8315), \n\t\tEigen::Matrix(0.9808, -0.1951), \n\t\tEigen::Matrix(1.0000, 0.0000), \n\t\tEigen::Matrix(0.1951, -0.9808)};\n\n\tmemset(fh->selected_pixels_right, 0, w * h * sizeof(PixelSelectorStatus));\n\n\tfloat dw1 = setting_grad_down_weight_per_level;\n\tfloat dw2 = dw1 * dw1;\n\n\tint n3 = 0, n2 = 0, n4 = 0;\n\n\tfor(int y4 = 0; y4 < h; y4 += (4 * pot))\n\t{\n\t\tfor(int x4 = 0; x4 < w; x4 += (4 * pot))\n\t\t{\n\t\t\tint my3 = std::min((4 * pot), h - y4);\n\t\t\tint mx3 = std::min((4 * pot), w - x4);\n\t\t\tint best_idx_4 = -1;\n\t\t\tfloat best_val_4 = 0;\n\n\t\t\tEigen::Matrix dir4 = directions[random_pattern[n2] & 0xF];\n\n\t\t\tfor(int y3 = 0; y3 < my3; y3 += (2 * pot))\n\t\t\t{\n\t\t\t\tfor(int x3 = 0; x3 < mx3; x3 += (2 * pot))\n\t\t\t\t{\n\t\t\t\t\tint x34 = x3 + x4;\n\t\t\t\t\tint y34 = y3 + y4;\n\t\t\t\t\tint my2 = std::min((2 * pot), h - y34);\n\t\t\t\t\tint mx2 = std::min((2 * pot), w - x34);\n\t\t\t\t\t\n\t\t\t\t\tint best_idx_3 = -1;\n\t\t\t\t\tfloat best_val_3 = 0;\n\n\t\t\t\t\tEigen::Matrix dir3 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\tfor(int y2 = 0; y2 < my2; y2 += pot)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor(int x2 = 0; x2 < mx2; x2 += pot)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tint x234 = x2 + x34;\n\t\t\t\t\t\t\tint y234 = y2 + y34;\n\t\t\t\t\t\t\tint my1 = std::min(pot, h - y234);\n\t\t\t\t\t\t\tint mx1 = std::min(pot, w - x234);\n\t\t\t\t\t\n\t\t\t\t\t\t\tint best_idx_2 = -1;\n\t\t\t\t\t\t\tfloat best_val_2 = 0;\n\t\t\t\t\n\t\t\t\t\t\t\tEigen::Matrix dir2 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\t\t\tfor(int y1 = 0; y1 < my1; y1 += 1)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfor(int x1 = 0; x1 < mx1; x1 += 1)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tassert(x1 + x234 < w);\n\t\t\t\t\t\t\t\t\tassert(y1 + y234 < h);\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tint idx = x1 + x234 + w * (y1 + y234);\n\t\t\t\t\t\t\t\t\tint xf = x1 + x234;\n\t\t\t\t\t\t\t\t\tint yf = y1 + y234;\n\n\t\t\t\t\t\t\t\t\tif (xf < 4 || xf >= w - 5 || yf < 4 || yf > h - 4) continue;\n\n\t\t\t\t\t\t\t\t\tif (fh->mask_right[idx] == true) continue;\n\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_0 = ths_smoothed[(xf >> 5) + (yf >> 5) * ths_step];\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_1 = pixel_TH_0 * dw1;\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_2 = pixel_TH_1 * dw2;\n\n\t\t\t\t\t\t\t\t\tfloat ag_0 = mapmax_0[idx];\n\n\t\t\t\t\t\t\t\t\tif(ag_0 > pixel_TH_0 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir2)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_0;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_2)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_2 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_2 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = -2;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tif(best_idx_3 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_1 = mapmax_1[(int)(xf * 0.5f + 0.25f) + (int)(yf * 0.5f + 0.25f) * w1];\n\n\t\t\t\t\t\t\t\t\tif(ag_1 > pixel_TH_1 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir3)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_1;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_3)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_3 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\tif(best_idx_4 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_2 = mapmax_2[(int)(xf * 0.25f + 0.125) + (int)(yf * 0.25f + 0.125) * w2];\n\n\t\t\t\t\t\t\t\t\tif(ag_2 > pixel_TH_2 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir4)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_2;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_4)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_4 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = idx;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif(best_idx_2 > 0)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfh->selected_pixels_right[best_idx_2] = 1;\n\t\t\t\t\t\t\t\tbest_val_3 = 1e10;\n\t\t\t\t\t\t\t\tn2++;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif(best_idx_3 > 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tfh->selected_pixels_right[best_idx_3] = 2;\n\t\t\t\t\t\tbest_val_4 = 1e10;\n\t\t\t\t\t\tn3++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(best_idx_4 > 0)\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_right[best_idx_4] = 4;\n\t\t\t\t\tn4++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn Eigen::Vector3i(n2, n3, n4);\n}\n int current_potential;\n bool allow_fast;\n unsigned char* random_pattern;\n int* grad_hist;\n float* ths;\n float* ths_smoothed;\n int ths_step;\n std::shared_ptr grad_hist_frame;\n};"], ["/voxel_svio/include/featureHelper.h", "class featureHelper {\n public:\n static void computeDisparity(std::shared_ptr db, double time_0, double time_1, double &disp_mean, double &disp_var, int &total_feats) {\n\tstd::vector disparities;\n\tfor (auto &feat : db->getInternalData())\n\t{\n\t\tfor (auto &cam_pairs : feat.second->timestamps)\n\t\t{\n\t\t\tif (cam_pairs.second.size() < 2)\n\t\t\t\tcontinue;\n\n\t\t\tsize_t cam_id = cam_pairs.first;\n\t\t\tbool found_0 = false;\n\t\t\tbool found_1 = false;\n \n\t\t\tEigen::Vector2f uv_0 = Eigen::Vector2f::Zero();\n\t\t\tEigen::Vector2f uv_1 = Eigen::Vector2f::Zero();\n\n\t\t\tfor (size_t idx = 0; idx < feat.second->timestamps.at(cam_id).size(); idx++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(idx);\n\t\t\t\tif ((oldest_time == -1 || time > oldest_time) && !found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_0 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_0 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif ((newest_time == -1 || time < newest_time) && found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_1 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_1 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!found_0 || !found_1)\n\t\t\t\tcontinue;\n \n\t\t\tdisparities.push_back((uv_1 - uv_0).norm());\n\t\t}\n\t}\n\n\tif (disparities.size() < 2)\n\t{\n\t\tdisp_mean = -1;\n\t\tdisp_var = -1;\n\t\ttotal_feats = 0;\n\t}\n\n\tdisp_mean = 0;\n\tfor (double disp_i : disparities)\n\t{\n\t\tdisp_mean += disp_i;\n\t}\n \n\tdisp_mean /= (double)disparities.size();\n\tdisp_var = 0;\n\tfor (double &disp_i : disparities)\n\t{\n\t\tdisp_var += std::pow(disp_i - disp_mean, 2);\n\t}\n\n\tdisp_var = std::sqrt(disp_var / (double)(disparities.size() - 1));\n\ttotal_feats = (int)disparities.size();\n}\n static void computeDisparity(std::shared_ptr db, double &disp_mean, double &disp_var, int &total_feats,\n double newest_time = -1, double oldest_time = -1) {\n\tstd::vector disparities;\n\tfor (auto &feat : db->getInternalData())\n\t{\n\t\tfor (auto &cam_pairs : feat.second->timestamps)\n\t\t{\n\t\t\tif (cam_pairs.second.size() < 2)\n\t\t\t\tcontinue;\n\n\t\t\tsize_t cam_id = cam_pairs.first;\n\t\t\tbool found_0 = false;\n\t\t\tbool found_1 = false;\n \n\t\t\tEigen::Vector2f uv_0 = Eigen::Vector2f::Zero();\n\t\t\tEigen::Vector2f uv_1 = Eigen::Vector2f::Zero();\n\n\t\t\tfor (size_t idx = 0; idx < feat.second->timestamps.at(cam_id).size(); idx++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(idx);\n\t\t\t\tif ((oldest_time == -1 || time > oldest_time) && !found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_0 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_0 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif ((newest_time == -1 || time < newest_time) && found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_1 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_1 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!found_0 || !found_1)\n\t\t\t\tcontinue;\n \n\t\t\tdisparities.push_back((uv_1 - uv_0).norm());\n\t\t}\n\t}\n\n\tif (disparities.size() < 2)\n\t{\n\t\tdisp_mean = -1;\n\t\tdisp_var = -1;\n\t\ttotal_feats = 0;\n\t}\n\n\tdisp_mean = 0;\n\tfor (double disp_i : disparities)\n\t{\n\t\tdisp_mean += disp_i;\n\t}\n \n\tdisp_mean /= (double)disparities.size();\n\tdisp_var = 0;\n\tfor (double &disp_i : disparities)\n\t{\n\t\tdisp_var += std::pow(disp_i - disp_mean, 2);\n\t}\n\n\tdisp_var = std::sqrt(disp_var / (double)(disparities.size() - 1));\n\ttotal_feats = (int)disparities.size();\n}\n private:\n featureHelper() {\n\n}\n};"], ["/voxel_svio/include/frame.h", "class feature {\n public:\n Eigen::Vector3f* dI_left;\n Eigen::Vector3f* dI_right;\n Eigen::Vector3f* dIp_left[PYR_LEVELS];\n Eigen::Vector3f* dIp_right[PYR_LEVELS];\n float* abs_squared_grad_left[PYR_LEVELS];\n float* abs_squared_grad_right[PYR_LEVELS];\n bool* mask_left;\n bool* mask_right;\n float* selected_pixels_left;\n float* selected_pixels_right;\n int frame_id;\n double timestamp;\n bool is_invalid = false;\n std::unordered_map>> v_feat_ptr;\n void release() {\n\tdelete[] mask_left;\n\tdelete[] mask_right;\n\n\tdelete[] selected_pixels_left;\n\tdelete[] selected_pixels_right;\n\n\tfor(int i = 0; i < pyr_levels_used; i++)\n\t{\n\t\tdelete[] dIp_left[i];\n\t\tdelete[] dIp_right[i];\n\n\t\tdelete[] abs_squared_grad_left[i];\n\t\tdelete[] abs_squared_grad_right[i];\n\t}\n\n\tis_invalid = true;\n}\n void makeImageLeft(unsigned char* color, unsigned char* mask, std::shared_ptr gammaPixel_ptr, double timestamp_) {\n\ttimestamp = timestamp_;\n\n\tfor(int i = 0; i < pyr_levels_used; i++)\n\t{\n\t\tdIp_left[i] = new Eigen::Vector3f[wG[i] * hG[i]];\n\t\tabs_squared_grad_left[i] = new float[wG[i] * hG[i]];\n\n\t\tif (i == 0) mask_left = new bool[wG[i] * hG[i]];\n\t}\n\n\tdI_left = dIp_left[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tfor(int i = 0; i < w * h; i++)\n\t{\n\t\tdI_left[i][0] = (float)color[i];\n\t\tmask_left[i] = mask[i] > 0 ? true : false;\n\t}\n\n\tfor(int lvl = 0; lvl < pyr_levels_used; lvl++)\n\t{\n\t\tint wl = wG[lvl], hl = hG[lvl];\n\t\tEigen::Vector3f* dI_l = dIp_left[lvl];\n\t\tfloat* dabs_l = abs_squared_grad_left[lvl];\n\n\t\tif (lvl > 0)\n\t\t{\n\t\t\tint lvlm1 = lvl - 1;\n\t\t\tint wlm1 = wG[lvlm1];\n\n\t\t\tEigen::Vector3f* dI_lm = dIp_left[lvlm1];\n\n\t\t\tfor(int y = 0; y < hl; y++)\n\t\t\t{\n\t\t\t\tfor(int x = 0; x < wl; x++)\n\t\t\t\t{\n\t\t\t\t\tdI_l[x + y * wl][0] = 0.25f * (dI_lm[2 * x + 2 * y *wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1][0] \n\t\t\t\t\t\t+ dI_lm[2 * x + 2 * y * wlm1 + wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1 + wlm1][0]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int idx = wl; idx < wl * (hl - 1); idx++)\n\t\t{\n\t\t\tfloat dx = 0.5f * (dI_l[idx + 1][0] - dI_l[idx - 1][0]);\n\t\t\tfloat dy = 0.5f * (dI_l[idx + wl][0] - dI_l[idx - wl][0]);\n\n\t\t\tif(!std::isfinite(dx)) dx = 0;\n\t\t\tif(!std::isfinite(dy)) dy = 0;\n\n\t\t\tdI_l[idx][1] = dx;\n\t\t\tdI_l[idx][2] = dy;\n\n\t\t\tdabs_l[idx] = dx * dx + dy * dy;\n\n\t\t\tif(setting_gamma_pixel_selection == 1 && gammaPixel_ptr != nullptr)\n\t\t\t{\n\t\t\t\tfloat gw = gammaPixel_ptr->getBGradOnly((float)(dI_l[idx][0]));\n\t\t\t\tdabs_l[idx] *= gw * gw;\n\t\t\t}\n\t\t}\n\t}\n\n\tselected_pixels_left = new float[wG[0] * hG[0]];\n}\n void makeImageRight(unsigned char* color, unsigned char* mask, std::shared_ptr gammaPixel_ptr, double timestamp_) {\n\ttimestamp = timestamp_;\n\n\tfor(int i = 0; i < pyr_levels_used; i++)\n\t{\n\t\tdIp_right[i] = new Eigen::Vector3f[wG[i] * hG[i]];\n\t\tabs_squared_grad_right[i] = new float[wG[i] * hG[i]];\n\n\t\tif (i == 0) mask_right = new bool[wG[i] * hG[i]];\n\t}\n\n\tdI_right = dIp_right[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tfor(int i = 0; i < w * h; i++)\n\t{\n\t\tdI_right[i][0] = (float)color[i];\n\t\tmask_right[i] = mask[i] > 0 ? true : false;\n\t}\n\n\tfor(int lvl = 0; lvl < pyr_levels_used; lvl++)\n\t{\n\t\tint wl = wG[lvl], hl = hG[lvl];\n\t\tEigen::Vector3f* dI_l = dIp_right[lvl];\n\t\tfloat* dabs_l = abs_squared_grad_right[lvl];\n\n\t\tif (lvl > 0)\n\t\t{\n\t\t\tint lvlm1 = lvl - 1;\n\t\t\tint wlm1 = wG[lvlm1];\n\n\t\t\tEigen::Vector3f* dI_lm = dIp_right[lvlm1];\n\n\t\t\tfor(int y = 0; y < hl; y++)\n\t\t\t{\n\t\t\t\tfor(int x = 0; x < wl; x++)\n\t\t\t\t{\n\t\t\t\t\tdI_l[x + y * wl][0] = 0.25f * (dI_lm[2 * x + 2 * y *wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1][0] \n\t\t\t\t\t\t+ dI_lm[2 * x + 2 * y * wlm1 + wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1 + wlm1][0]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int idx = wl; idx < wl * (hl - 1); idx++)\n\t\t{\n\t\t\tfloat dx = 0.5f * (dI_l[idx + 1][0] - dI_l[idx - 1][0]);\n\t\t\tfloat dy = 0.5f * (dI_l[idx + wl][0] - dI_l[idx - wl][0]);\n\n\t\t\tif(!std::isfinite(dx)) dx = 0;\n\t\t\tif(!std::isfinite(dy)) dy = 0;\n\n\t\t\tdI_l[idx][1] = dx;\n\t\t\tdI_l[idx][2] = dy;\n\n\t\t\tdabs_l[idx] = dx * dx + dy * dy;\n\n\t\t\tif(setting_gamma_pixel_selection == 1 && gammaPixel_ptr != nullptr)\n\t\t\t{\n\t\t\t\tfloat gw = gammaPixel_ptr->getBGradOnly((float)(dI_l[idx][0]));\n\t\t\t\tdabs_l[idx] *= gw * gw;\n\t\t\t}\n\t\t}\n\t}\n\n\tselected_pixels_right = new float[wG[0] * hG[0]];\n}\n};"], ["/voxel_svio/include/mapPoint.h", "class mapPoint {\n public:\n mapPoint() {\n\n}\n size_t feature_id;\n int unique_camera_id = -1;\n int anchor_cam_id = -1;\n double anchor_clone_timestamp = -1;\n bool has_had_anchor_change = false;\n bool should_marg = false;\n int update_fail_count = 0;\n Eigen::Vector3d uv_normalized;\n Eigen::Vector3d uv_normalized_fej;\n float color;\n std::shared_ptr host_frame;\n short voxel_idx[3];\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\tsetValue(value_ + dx);\n}\n Eigen::Matrix getPointXYZ(bool get_fej) {\n\treturn (get_fej) ? fej() : value();\n}\n void setPointXYZ(Eigen::Matrix p_FinG, bool is_fej) {\n\tif (is_fej)\n\t\tsetFej(p_FinG);\n\telse\n\t\tsetValue(p_FinG);\n\t\n\treturn;\n}\n};"], ["/voxel_svio/include/utility.h", "class gammaPixel {\n public:\n float Binv[256];\n float B[256];\n gammaPixel() {\n for (int i = 0; i < 256; i++)\n Binv[i] = B[i] = i;\n}\n float getBGradOnly(float color) {\n int c = color + 0.5f;\n if(c < 5) c = 5;\n if(c > 250) c = 250;\n return B[c + 1] - B[c];\n}\n float getBInvGradOnly(float color) {\n int c = color + 0.5f;\n if(c < 5) c = 5;\n if(c > 250) c = 250;\n return Binv[c + 1] - Binv[c];\n}\n public:\n private:\n std::function body;\n};"], ["/voxel_svio/include/sensorData.h", "class sensorData {\n};"]], "task_instance_info": {"created_time": "2025-08-20 20:09:18", "created_task_model": "DeepSeek-R1", "function_skeleton": "void recordParameters() {}", "code_description": "This code segment functions to: Capture and persist system configuration settings by writing operational parameters related to sensor calibration, image processing thresholds, and geometric transformations to a designated output file for experimental reproducibility."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```cpp\n[TASK_BEGIN]\nvoid recordParameters() {\n std::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n foutC << \"do_fej: \" << (do_fej ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_camera_pose: \" << (do_calib_camera_pose ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_camera_intrinsics: \" << (do_calib_camera_intrinsics ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_camera_timeoffset: \" << (do_calib_camera_timeoffset ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_imu_intrinsics: \" << (do_calib_imu_intrinsics ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_imu_g_sensitivity: \" << (do_calib_imu_g_sensitivity ? \"true\" : \"false\") << std::endl;\n foutC << \"imu_model: \" << static_cast(imu_model) << std::endl;\n foutC << \"max_clone_size: \" << max_clone_size << std::endl;\n foutC << \"max_slam_features: \" << max_slam_features << std::endl;\n foutC << \"max_slam_in_update: \" << max_slam_in_update << std::endl;\n foutC << \"max_msckf_in_update: \" << max_msckf_in_update << std::endl;\n foutC << \"voxel_size: \" << voxel_size << std::endl;\n foutC << \"max_num_points_in_voxel: \" << max_num_points_in_voxel << std::endl;\n foutC << \"min_distance_points: \" << min_distance_points << std::endl;\n foutC << \"use_huber: \" << (use_huber ? \"true\" : \"false\") << std::endl;\n foutC.close();\n}\n[TASK_END]\n```", "inference_time": "2025-08-20 20-10-28"}, "editdistance_info": {"edit_distance": 20.0971, "calculate_time": "2025-08-20 20:10:28", "true_code_clean": "void recordParameters() {\n\tstate_options.recordParameters();\n\tinit_options.recordParameters();\n\tstd::string str_temp;\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\timu_noises.recordParameters();\n\tmsckf_options.recordParameters();\n\tslam_options.recordParameters();\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\tfeatinit_options.recordParameters();\n\tfoutC.close();\n}", "predict_code_clean": "void recordParameters() {\n std::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n foutC << \"do_fej: \" << (do_fej ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_camera_pose: \" << (do_calib_camera_pose ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_camera_intrinsics: \" << (do_calib_camera_intrinsics ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_camera_timeoffset: \" << (do_calib_camera_timeoffset ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_imu_intrinsics: \" << (do_calib_imu_intrinsics ? \"true\" : \"false\") << std::endl;\n foutC << \"do_calib_imu_g_sensitivity: \" << (do_calib_imu_g_sensitivity ? \"true\" : \"false\") << std::endl;\n foutC << \"imu_model: \" << static_cast(imu_model) << std::endl;\n foutC << \"max_clone_size: \" << max_clone_size << std::endl;\n foutC << \"max_slam_features: \" << max_slam_features << std::endl;\n foutC << \"max_slam_in_update: \" << max_slam_in_update << std::endl;\n foutC << \"max_msckf_in_update: \" << max_msckf_in_update << std::endl;\n foutC << \"voxel_size: \" << voxel_size << std::endl;\n foutC << \"max_num_points_in_voxel: \" << max_num_points_in_voxel << std::endl;\n foutC << \"min_distance_points: \" << min_distance_points << std::endl;\n foutC << \"use_huber: \" << (use_huber ? \"true\" : \"false\") << std::endl;\n foutC.close();\n}"}} {"repo_name": "voxel_svio", "file_name": "/voxel_svio/include/pixelSelector.h", "inference_info": {"prefix_code": "class pixelSelector {\n public:\n EIGEN_MAKE_ALIGNED_OPERATOR_NEW;\n ", "suffix_code": "\n ~pixelSelector() {\n\tdelete[] random_pattern;\n\tdelete[] grad_hist;\n\tdelete[] ths;\n\tdelete[] ths_smoothed;\n}\n int pixelSelectionLeft(std::shared_ptr fh, float density, int recursions_left = 1, float th_factor = 1) {\n\tfloat num_have = 0;\n\tfloat num_want = density;\n\tfloat quotia;\n\tint ideal_potential = current_potential;\n\n\t// if(fh != grad_hist_frame) makeHistsLeft(fh);\n\tmakeHistsLeft(fh);\n\n\tEigen::Vector3i n = this->selectLeft(fh, current_potential, th_factor);\n\n\tnum_have = n[0] + n[1] + n[2];\n\tquotia = num_want / num_have;\n\n\tfloat K = num_have * (current_potential + 1) * (current_potential + 1);\n\tideal_potential = sqrtf(K / num_want) - 1;\n\n\tif (ideal_potential < 1) ideal_potential = 1;\n\n\tif (recursions_left > 0 && quotia > 1.25 && current_potential > 1)\n\t{\n\t\tif(ideal_potential >= current_potential)\n\t\t\tideal_potential = current_potential - 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionLeft(fh, density, recursions_left - 1, th_factor);\n\t}\n\telse if(recursions_left > 0 && quotia < 0.25)\n\t{\n\t\tif(ideal_potential <= current_potential)\n\t\t\tideal_potential = current_potential + 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionLeft(fh, density, recursions_left - 1, th_factor);\n\t}\n\n\tint num_have_sub = num_have;\n\n\tif(quotia < 0.95)\n\t{\n\t\tint wh = wG[0] * hG[0];\n\t\tint rn = 0;\n\t\tunsigned char char_th = 255*quotia;\n\t\tfor(int i = 0; i < wh; i++)\n\t\t{\n\t\t\tif(fh->selected_pixels_left[i] != 0)\n\t\t\t{\n\t\t\t\tif(random_pattern[rn] > char_th )\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_left[i] = 0;\n\t\t\t\t\tnum_have_sub--;\n\t\t\t\t}\n\t\t\t\trn++;\n\t\t\t}\n\t\t}\n\t}\n\n\tcurrent_potential = ideal_potential;\n\n\treturn num_have_sub;\n}\n int pixelSelectionRight(std::shared_ptr fh, float density, int recursions_left = 1, float th_factor = 1) {\n\tfloat num_have = 0;\n\tfloat num_want = density;\n\tfloat quotia;\n\tint ideal_potential = current_potential;\n\n\t// if(fh != grad_hist_frame) makeHistsRight(fh);\n\tmakeHistsRight(fh);\n\n\tEigen::Vector3i n = this->selectRight(fh, current_potential, th_factor);\n\n\tnum_have = n[0] + n[1] + n[2];\n\tquotia = num_want / num_have;\n\n\tfloat K = num_have * (current_potential + 1) * (current_potential + 1);\n\tideal_potential = sqrtf(K / num_want) - 1;\n\n\tif (ideal_potential < 1) ideal_potential = 1;\n\n\tif (recursions_left > 0 && quotia > 1.25 && current_potential > 1)\n\t{\n\t\tif(ideal_potential >= current_potential)\n\t\t\tideal_potential = current_potential - 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionRight(fh, density, recursions_left - 1, th_factor);\n\t}\n\telse if (recursions_left > 0 && quotia < 0.25)\n\t{\n\t\tif(ideal_potential <= current_potential)\n\t\t\tideal_potential = current_potential + 1;\n\n\t\tcurrent_potential = ideal_potential;\n\t\treturn pixelSelectionRight(fh, density, recursions_left - 1, th_factor);\n\t}\n\n\tint num_have_sub = num_have;\n\n\tif (quotia < 0.95)\n\t{\n\t\tint wh = wG[0] * hG[0];\n\t\tint rn = 0;\n\t\tunsigned char char_th = 255*quotia;\n\t\tfor (int i = 0; i < wh; i++)\n\t\t{\n\t\t\tif (fh->selected_pixels_right[i] != 0)\n\t\t\t{\n\t\t\t\tif (random_pattern[rn] > char_th)\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_right[i] = 0;\n\t\t\t\t\tnum_have_sub--;\n\t\t\t\t}\n\t\t\t\trn++;\n\t\t\t}\n\t\t}\n\t}\n\n\tcurrent_potential = ideal_potential;\n\n\treturn num_have_sub;\n}\n private:\n void makeHistsLeft(std::shared_ptr fh) {\n\tgrad_hist_frame = fh;\n\tfloat* mapmax_0 = fh->abs_squared_grad_left[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tint w32 = w / 32;\n\tint h32 = h / 32;\n\tths_step = w32;\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat* map_0 = mapmax_0 + 32 * x + 32 * y * w;\n\t\t\tint* hist_0 = grad_hist;\n\n\t\t\tmemset(hist_0, 0, sizeof(int) * 50);\n\n\t\t\tfor(int j = 0; j < 32; j++)\n\t\t\t\tfor(int i = 0; i < 32; i++)\n\t\t\t\t{\n\t\t\t\t\tint it = i + 32 * x;\n\t\t\t\t\tint jt = j + 32 * y;\n\n\t\t\t\t\tif(it > w - 2 || jt > h - 2 || it < 1 || jt < 1) continue;\n\n\t\t\t\t\tint g = sqrtf(map_0[i + j * w]);\n\n\t\t\t\t\tif(g > 48) g = 48;\n\n\t\t\t\t\thist_0[g + 1]++;\n\t\t\t\t\thist_0[0]++;\n\t\t\t\t}\n\n\t\t\tths[x + y * w32] = computeHistQuantil(hist_0, setting_min_grad_hist_cut) + setting_min_grad_hist_add;\n\t\t}\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat sum = 0, num = 0;\n\t\t\tif(x > 0)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y - 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x - 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(x < w32 - 1)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y - 1) * w32];\n\t\t\t\t}\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(y > 0)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y - 1) * w32];\n\t\t\t}\n\n\t\t\tif(y < h32 - 1)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y + 1) * w32];\n\t\t\t}\n\n\t\t\tnum++;\n\t\t\tsum += ths[x + y * w32];\n\n\t\t\tths_smoothed[x + y * w32] = (sum / num) * (sum / num);\n\t\t}\n}\n void makeHistsRight(std::shared_ptr fh) {\n\tgrad_hist_frame = fh;\n\tfloat* mapmax_0 = fh->abs_squared_grad_right[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tint w32 = w / 32;\n\tint h32 = h / 32;\n\tths_step = w32;\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat* map_0 = mapmax_0 + 32 * x + 32 * y * w;\n\t\t\tint* hist_0 = grad_hist;\n\n\t\t\tmemset(hist_0, 0, sizeof(int) * 50);\n\n\t\t\tfor(int j = 0; j < 32; j++)\n\t\t\t\tfor(int i = 0; i < 32; i++)\n\t\t\t\t{\n\t\t\t\t\tint it = i + 32 * x;\n\t\t\t\t\tint jt = j + 32 * y;\n\n\t\t\t\t\tif(it > w - 2 || jt > h - 2 || it < 1 || jt < 1) continue;\n\n\t\t\t\t\tint g = sqrtf(map_0[i + j * w]);\n\n\t\t\t\t\tif(g > 48) g = 48;\n\n\t\t\t\t\thist_0[g + 1]++;\n\t\t\t\t\thist_0[0]++;\n\t\t\t\t}\n\n\t\t\tths[x + y * w32] = computeHistQuantil(hist_0, setting_min_grad_hist_cut) + setting_min_grad_hist_add;\n\t\t}\n\n\tfor(int y = 0; y < h32; y++)\n\t\tfor(int x = 0; x < w32; x++)\n\t\t{\n\t\t\tfloat sum = 0, num = 0;\n\n\t\t\tif(x > 0)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y - 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x - 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x - 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(x < w32 - 1)\n\t\t\t{\n\t\t\t\tif(y > 0)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y - 1) * w32];\n\t\t\t\t}\n\t\t\t\tif(y < h32 - 1)\n\t\t\t\t{\n\t\t\t\t\tnum++;\n\t\t\t\t\tsum += ths[x + 1 + (y + 1) * w32];\n\t\t\t\t}\n\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + 1 + (y) * w32];\n\t\t\t}\n\n\t\t\tif(y > 0)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y - 1) * w32];\n\t\t\t}\n\n\t\t\tif(y < h32 - 1)\n\t\t\t{\n\t\t\t\tnum++;\n\t\t\t\tsum += ths[x + (y + 1) * w32];\n\t\t\t}\n\n\t\t\tnum++;\n\t\t\tsum += ths[x + y * w32];\n\n\t\t\tths_smoothed[x + y * w32] = (sum / num) * (sum / num);\n\t\t}\n}\n Eigen::Vector3i selectLeft(std::shared_ptr fh, int pot, float th_factor = 1) {\n\tEigen::Vector3f const * const map_0 = fh->dI_left;\n\n\tfloat* mapmax_0 = fh->abs_squared_grad_left[0];\n\tfloat* mapmax_1 = fh->abs_squared_grad_left[1];\n\tfloat* mapmax_2 = fh->abs_squared_grad_left[2];\n\n\tint w = wG[0];\n\tint w1 = wG[1];\n\tint w2 = wG[2];\n\tint h = hG[0];\n\n\tconst Eigen::Matrix directions[16] = {\n\t\tEigen::Matrix(0, 1.0000), \n\t\tEigen::Matrix(0.3827, 0.9239), \n\t\tEigen::Matrix(0.1951, 0.9808), \n\t\tEigen::Matrix(0.9239, 0.3827), \n\t\tEigen::Matrix(0.7071, 0.7071), \n\t\tEigen::Matrix(0.3827, -0.9239), \n\t\tEigen::Matrix(0.8315, 0.5556), \n\t\tEigen::Matrix(0.8315, -0.5556), \n\t\tEigen::Matrix(0.5556, -0.8315), \n\t\tEigen::Matrix(0.9808, 0.1951), \n\t\tEigen::Matrix(0.9239, -0.3827), \n\t\tEigen::Matrix(0.7071, -0.7071), \n\t\tEigen::Matrix(0.5556, 0.8315), \n\t\tEigen::Matrix(0.9808, -0.1951), \n\t\tEigen::Matrix(1.0000, 0.0000), \n\t\tEigen::Matrix(0.1951, -0.9808)};\n\n\tmemset(fh->selected_pixels_left, 0, w * h * sizeof(PixelSelectorStatus));\n\n\tfloat dw1 = setting_grad_down_weight_per_level;\n\tfloat dw2 = dw1 * dw1;\n\n\tint n3 = 0, n2 = 0, n4 = 0;\n\n\tfor(int y4 = 0; y4 < h; y4 += (4 * pot))\n\t{\n\t\tfor(int x4 = 0; x4 < w; x4 += (4 * pot))\n\t\t{\n\t\t\tint my3 = std::min((4 * pot), h - y4);\n\t\t\tint mx3 = std::min((4 * pot), w - x4);\n\t\t\tint best_idx_4 = -1;\n\t\t\tfloat best_val_4 = 0;\n\n\t\t\tEigen::Matrix dir4 = directions[random_pattern[n2] & 0xF];\n\n\t\t\tfor(int y3 = 0; y3 < my3; y3 += (2 * pot))\n\t\t\t{\n\t\t\t\tfor(int x3 = 0; x3 < mx3; x3 += (2 * pot))\n\t\t\t\t{\n\t\t\t\t\tint x34 = x3 + x4;\n\t\t\t\t\tint y34 = y3 + y4;\n\t\t\t\t\tint my2 = std::min((2 * pot), h - y34);\n\t\t\t\t\tint mx2 = std::min((2 * pot), w - x34);\n\t\t\t\t\t\n\t\t\t\t\tint best_idx_3 = -1;\n\t\t\t\t\tfloat best_val_3 = 0;\n\n\t\t\t\t\tEigen::Matrix dir3 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\tfor(int y2 = 0; y2 < my2; y2 += pot)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor(int x2 = 0; x2 < mx2; x2 += pot)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tint x234 = x2 + x34;\n\t\t\t\t\t\t\tint y234 = y2 + y34;\n\t\t\t\t\t\t\tint my1 = std::min(pot, h - y234);\n\t\t\t\t\t\t\tint mx1 = std::min(pot, w - x234);\n\t\t\t\t\t\n\t\t\t\t\t\t\tint best_idx_2 = -1;\n\t\t\t\t\t\t\tfloat best_val_2 = 0;\n\t\t\t\t\n\t\t\t\t\t\t\tEigen::Matrix dir2 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\t\t\tfor(int y1 = 0; y1 < my1; y1 += 1)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfor(int x1 = 0; x1 < mx1; x1 += 1)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tassert(x1 + x234 < w);\n\t\t\t\t\t\t\t\t\tassert(y1 + y234 < h);\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tint idx = x1 + x234 + w * (y1 + y234);\n\t\t\t\t\t\t\t\t\tint xf = x1 + x234;\n\t\t\t\t\t\t\t\t\tint yf = y1 + y234;\n\n\t\t\t\t\t\t\t\t\tif(xf < 4 || xf >= w - 5 || yf < 4 || yf > h - 4) continue;\n\n\t\t\t\t\t\t\t\t\tif (fh->mask_left[idx] == true) continue;\n\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_0 = ths_smoothed[(xf >> 5) + (yf >> 5) * ths_step];\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_1 = pixel_TH_0 * dw1;\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_2 = pixel_TH_1 * dw2;\n\n\t\t\t\t\t\t\t\t\tfloat ag_0 = mapmax_0[idx];\n\n\t\t\t\t\t\t\t\t\tif(ag_0 > pixel_TH_0 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir2)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_0;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_2)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_2 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_2 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = -2;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tif(best_idx_3 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_1 = mapmax_1[(int)(xf * 0.5f + 0.25f) + (int)(yf * 0.5f + 0.25f) * w1];\n\n\t\t\t\t\t\t\t\t\tif(ag_1 > pixel_TH_1 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir3)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_1;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_3)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_3 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\tif(best_idx_4 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_2 = mapmax_2[(int)(xf * 0.25f + 0.125) + (int)(yf * 0.25f + 0.125) * w2];\n\n\t\t\t\t\t\t\t\t\tif(ag_2 > pixel_TH_2 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir4)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_2;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_4)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_4 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = idx;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif(best_idx_2 > 0)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfh->selected_pixels_left[best_idx_2] = 1;\n\t\t\t\t\t\t\t\tbest_val_3 = 1e10;\n\t\t\t\t\t\t\t\tn2++;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif(best_idx_3 > 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tfh->selected_pixels_left[best_idx_3] = 2;\n\t\t\t\t\t\tbest_val_4 = 1e10;\n\t\t\t\t\t\tn3++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(best_idx_4 > 0)\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_left[best_idx_4] = 4;\n\t\t\t\t\tn4++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn Eigen::Vector3i(n2, n3, n4);\n}\n Eigen::Vector3i selectRight(std::shared_ptr fh, int pot, float th_factor = 1) {\n\tEigen::Vector3f const * const map_0 = fh->dI_right;\n\n\tfloat* mapmax_0 = fh->abs_squared_grad_right[0];\n\tfloat* mapmax_1 = fh->abs_squared_grad_right[1];\n\tfloat* mapmax_2 = fh->abs_squared_grad_right[2];\n\n\tint w = wG[0];\n\tint w1 = wG[1];\n\tint w2 = wG[2];\n\tint h = hG[0];\n\n\tconst Eigen::Matrix directions[16] = {\n\t\tEigen::Matrix(0, 1.0000), \n\t\tEigen::Matrix(0.3827, 0.9239), \n\t\tEigen::Matrix(0.1951, 0.9808), \n\t\tEigen::Matrix(0.9239, 0.3827), \n\t\tEigen::Matrix(0.7071, 0.7071), \n\t\tEigen::Matrix(0.3827, -0.9239), \n\t\tEigen::Matrix(0.8315, 0.5556), \n\t\tEigen::Matrix(0.8315, -0.5556), \n\t\tEigen::Matrix(0.5556, -0.8315), \n\t\tEigen::Matrix(0.9808, 0.1951), \n\t\tEigen::Matrix(0.9239, -0.3827), \n\t\tEigen::Matrix(0.7071, -0.7071), \n\t\tEigen::Matrix(0.5556, 0.8315), \n\t\tEigen::Matrix(0.9808, -0.1951), \n\t\tEigen::Matrix(1.0000, 0.0000), \n\t\tEigen::Matrix(0.1951, -0.9808)};\n\n\tmemset(fh->selected_pixels_right, 0, w * h * sizeof(PixelSelectorStatus));\n\n\tfloat dw1 = setting_grad_down_weight_per_level;\n\tfloat dw2 = dw1 * dw1;\n\n\tint n3 = 0, n2 = 0, n4 = 0;\n\n\tfor(int y4 = 0; y4 < h; y4 += (4 * pot))\n\t{\n\t\tfor(int x4 = 0; x4 < w; x4 += (4 * pot))\n\t\t{\n\t\t\tint my3 = std::min((4 * pot), h - y4);\n\t\t\tint mx3 = std::min((4 * pot), w - x4);\n\t\t\tint best_idx_4 = -1;\n\t\t\tfloat best_val_4 = 0;\n\n\t\t\tEigen::Matrix dir4 = directions[random_pattern[n2] & 0xF];\n\n\t\t\tfor(int y3 = 0; y3 < my3; y3 += (2 * pot))\n\t\t\t{\n\t\t\t\tfor(int x3 = 0; x3 < mx3; x3 += (2 * pot))\n\t\t\t\t{\n\t\t\t\t\tint x34 = x3 + x4;\n\t\t\t\t\tint y34 = y3 + y4;\n\t\t\t\t\tint my2 = std::min((2 * pot), h - y34);\n\t\t\t\t\tint mx2 = std::min((2 * pot), w - x34);\n\t\t\t\t\t\n\t\t\t\t\tint best_idx_3 = -1;\n\t\t\t\t\tfloat best_val_3 = 0;\n\n\t\t\t\t\tEigen::Matrix dir3 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\tfor(int y2 = 0; y2 < my2; y2 += pot)\n\t\t\t\t\t{\n\t\t\t\t\t\tfor(int x2 = 0; x2 < mx2; x2 += pot)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tint x234 = x2 + x34;\n\t\t\t\t\t\t\tint y234 = y2 + y34;\n\t\t\t\t\t\t\tint my1 = std::min(pot, h - y234);\n\t\t\t\t\t\t\tint mx1 = std::min(pot, w - x234);\n\t\t\t\t\t\n\t\t\t\t\t\t\tint best_idx_2 = -1;\n\t\t\t\t\t\t\tfloat best_val_2 = 0;\n\t\t\t\t\n\t\t\t\t\t\t\tEigen::Matrix dir2 = directions[random_pattern[n2] & 0xF];\n\n\t\t\t\t\t\t\tfor(int y1 = 0; y1 < my1; y1 += 1)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfor(int x1 = 0; x1 < mx1; x1 += 1)\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tassert(x1 + x234 < w);\n\t\t\t\t\t\t\t\t\tassert(y1 + y234 < h);\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tint idx = x1 + x234 + w * (y1 + y234);\n\t\t\t\t\t\t\t\t\tint xf = x1 + x234;\n\t\t\t\t\t\t\t\t\tint yf = y1 + y234;\n\n\t\t\t\t\t\t\t\t\tif (xf < 4 || xf >= w - 5 || yf < 4 || yf > h - 4) continue;\n\n\t\t\t\t\t\t\t\t\tif (fh->mask_right[idx] == true) continue;\n\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_0 = ths_smoothed[(xf >> 5) + (yf >> 5) * ths_step];\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_1 = pixel_TH_0 * dw1;\n\t\t\t\t\t\t\t\t\tfloat pixel_TH_2 = pixel_TH_1 * dw2;\n\n\t\t\t\t\t\t\t\t\tfloat ag_0 = mapmax_0[idx];\n\n\t\t\t\t\t\t\t\t\tif(ag_0 > pixel_TH_0 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir2)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_0;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_2)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_2 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_2 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = -2;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t\t\tif(best_idx_3 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_1 = mapmax_1[(int)(xf * 0.5f + 0.25f) + (int)(yf * 0.5f + 0.25f) * w1];\n\n\t\t\t\t\t\t\t\t\tif(ag_1 > pixel_TH_1 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir3)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_1;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_3)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_3 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_3 = idx;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = -2;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\tif(best_idx_4 == -2) continue;\n\n\t\t\t\t\t\t\t\t\tfloat ag_2 = mapmax_2[(int)(xf * 0.25f + 0.125) + (int)(yf * 0.25f + 0.125) * w2];\n\n\t\t\t\t\t\t\t\t\tif(ag_2 > pixel_TH_2 * th_factor)\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tEigen::Matrix ag_0_d = map_0[idx].tail<2>();\n\t\t\t\t\t\t\t\t\t\tfloat dir_norm = fabsf((float)(ag_0_d.dot(dir4)));\n\n\t\t\t\t\t\t\t\t\t\tif(!setting_select_direction_distribution) dir_norm = ag_2;\n\n\t\t\t\t\t\t\t\t\t\tif(dir_norm > best_val_4)\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tbest_val_4 = dir_norm;\n\t\t\t\t\t\t\t\t\t\t\tbest_idx_4 = idx;\n\t\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif(best_idx_2 > 0)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tfh->selected_pixels_right[best_idx_2] = 1;\n\t\t\t\t\t\t\t\tbest_val_3 = 1e10;\n\t\t\t\t\t\t\t\tn2++;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif(best_idx_3 > 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tfh->selected_pixels_right[best_idx_3] = 2;\n\t\t\t\t\t\tbest_val_4 = 1e10;\n\t\t\t\t\t\tn3++;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif(best_idx_4 > 0)\n\t\t\t\t{\n\t\t\t\t\tfh->selected_pixels_right[best_idx_4] = 4;\n\t\t\t\t\tn4++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn Eigen::Vector3i(n2, n3, n4);\n}\n int current_potential;\n bool allow_fast;\n unsigned char* random_pattern;\n int* grad_hist;\n float* ths;\n float* ths_smoothed;\n int ths_step;\n std::shared_ptr grad_hist_frame;\n};", "middle_code": "pixelSelector(int w, int h) {\n\trandom_pattern = new unsigned char[w * h];\n\tstd::srand(3141592);\n\tfor (int i = 0; i < w * h; i++)\n\t\trandom_pattern[i] = rand() & 0xFF;\n\tcurrent_potential = 3;\n\tgrad_hist = new int[100 * (1 + w / 32) * (1 + h / 32)];\n\tths = new float[(w / 32) * (h / 32) + 100];\n\tths_smoothed = new float[(w / 32) * (h / 32) + 100];\n\tallow_fast = false;\n\tgrad_hist_frame = nullptr;\n}", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "cpp", "sub_task_type": null}, "context_code": [["/voxel_svio/include/frame.h", "class feature {\n public:\n Eigen::Vector3f* dI_left;\n Eigen::Vector3f* dI_right;\n Eigen::Vector3f* dIp_left[PYR_LEVELS];\n Eigen::Vector3f* dIp_right[PYR_LEVELS];\n float* abs_squared_grad_left[PYR_LEVELS];\n float* abs_squared_grad_right[PYR_LEVELS];\n bool* mask_left;\n bool* mask_right;\n float* selected_pixels_left;\n float* selected_pixels_right;\n int frame_id;\n double timestamp;\n bool is_invalid = false;\n std::unordered_map>> v_feat_ptr;\n void release() {\n\tdelete[] mask_left;\n\tdelete[] mask_right;\n\n\tdelete[] selected_pixels_left;\n\tdelete[] selected_pixels_right;\n\n\tfor(int i = 0; i < pyr_levels_used; i++)\n\t{\n\t\tdelete[] dIp_left[i];\n\t\tdelete[] dIp_right[i];\n\n\t\tdelete[] abs_squared_grad_left[i];\n\t\tdelete[] abs_squared_grad_right[i];\n\t}\n\n\tis_invalid = true;\n}\n void makeImageLeft(unsigned char* color, unsigned char* mask, std::shared_ptr gammaPixel_ptr, double timestamp_) {\n\ttimestamp = timestamp_;\n\n\tfor(int i = 0; i < pyr_levels_used; i++)\n\t{\n\t\tdIp_left[i] = new Eigen::Vector3f[wG[i] * hG[i]];\n\t\tabs_squared_grad_left[i] = new float[wG[i] * hG[i]];\n\n\t\tif (i == 0) mask_left = new bool[wG[i] * hG[i]];\n\t}\n\n\tdI_left = dIp_left[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tfor(int i = 0; i < w * h; i++)\n\t{\n\t\tdI_left[i][0] = (float)color[i];\n\t\tmask_left[i] = mask[i] > 0 ? true : false;\n\t}\n\n\tfor(int lvl = 0; lvl < pyr_levels_used; lvl++)\n\t{\n\t\tint wl = wG[lvl], hl = hG[lvl];\n\t\tEigen::Vector3f* dI_l = dIp_left[lvl];\n\t\tfloat* dabs_l = abs_squared_grad_left[lvl];\n\n\t\tif (lvl > 0)\n\t\t{\n\t\t\tint lvlm1 = lvl - 1;\n\t\t\tint wlm1 = wG[lvlm1];\n\n\t\t\tEigen::Vector3f* dI_lm = dIp_left[lvlm1];\n\n\t\t\tfor(int y = 0; y < hl; y++)\n\t\t\t{\n\t\t\t\tfor(int x = 0; x < wl; x++)\n\t\t\t\t{\n\t\t\t\t\tdI_l[x + y * wl][0] = 0.25f * (dI_lm[2 * x + 2 * y *wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1][0] \n\t\t\t\t\t\t+ dI_lm[2 * x + 2 * y * wlm1 + wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1 + wlm1][0]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int idx = wl; idx < wl * (hl - 1); idx++)\n\t\t{\n\t\t\tfloat dx = 0.5f * (dI_l[idx + 1][0] - dI_l[idx - 1][0]);\n\t\t\tfloat dy = 0.5f * (dI_l[idx + wl][0] - dI_l[idx - wl][0]);\n\n\t\t\tif(!std::isfinite(dx)) dx = 0;\n\t\t\tif(!std::isfinite(dy)) dy = 0;\n\n\t\t\tdI_l[idx][1] = dx;\n\t\t\tdI_l[idx][2] = dy;\n\n\t\t\tdabs_l[idx] = dx * dx + dy * dy;\n\n\t\t\tif(setting_gamma_pixel_selection == 1 && gammaPixel_ptr != nullptr)\n\t\t\t{\n\t\t\t\tfloat gw = gammaPixel_ptr->getBGradOnly((float)(dI_l[idx][0]));\n\t\t\t\tdabs_l[idx] *= gw * gw;\n\t\t\t}\n\t\t}\n\t}\n\n\tselected_pixels_left = new float[wG[0] * hG[0]];\n}\n void makeImageRight(unsigned char* color, unsigned char* mask, std::shared_ptr gammaPixel_ptr, double timestamp_) {\n\ttimestamp = timestamp_;\n\n\tfor(int i = 0; i < pyr_levels_used; i++)\n\t{\n\t\tdIp_right[i] = new Eigen::Vector3f[wG[i] * hG[i]];\n\t\tabs_squared_grad_right[i] = new float[wG[i] * hG[i]];\n\n\t\tif (i == 0) mask_right = new bool[wG[i] * hG[i]];\n\t}\n\n\tdI_right = dIp_right[0];\n\n\tint w = wG[0];\n\tint h = hG[0];\n\n\tfor(int i = 0; i < w * h; i++)\n\t{\n\t\tdI_right[i][0] = (float)color[i];\n\t\tmask_right[i] = mask[i] > 0 ? true : false;\n\t}\n\n\tfor(int lvl = 0; lvl < pyr_levels_used; lvl++)\n\t{\n\t\tint wl = wG[lvl], hl = hG[lvl];\n\t\tEigen::Vector3f* dI_l = dIp_right[lvl];\n\t\tfloat* dabs_l = abs_squared_grad_right[lvl];\n\n\t\tif (lvl > 0)\n\t\t{\n\t\t\tint lvlm1 = lvl - 1;\n\t\t\tint wlm1 = wG[lvlm1];\n\n\t\t\tEigen::Vector3f* dI_lm = dIp_right[lvlm1];\n\n\t\t\tfor(int y = 0; y < hl; y++)\n\t\t\t{\n\t\t\t\tfor(int x = 0; x < wl; x++)\n\t\t\t\t{\n\t\t\t\t\tdI_l[x + y * wl][0] = 0.25f * (dI_lm[2 * x + 2 * y *wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1][0] \n\t\t\t\t\t\t+ dI_lm[2 * x + 2 * y * wlm1 + wlm1][0] + dI_lm[2 * x + 1 + 2 * y * wlm1 + wlm1][0]);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tfor(int idx = wl; idx < wl * (hl - 1); idx++)\n\t\t{\n\t\t\tfloat dx = 0.5f * (dI_l[idx + 1][0] - dI_l[idx - 1][0]);\n\t\t\tfloat dy = 0.5f * (dI_l[idx + wl][0] - dI_l[idx - wl][0]);\n\n\t\t\tif(!std::isfinite(dx)) dx = 0;\n\t\t\tif(!std::isfinite(dy)) dy = 0;\n\n\t\t\tdI_l[idx][1] = dx;\n\t\t\tdI_l[idx][2] = dy;\n\n\t\t\tdabs_l[idx] = dx * dx + dy * dy;\n\n\t\t\tif(setting_gamma_pixel_selection == 1 && gammaPixel_ptr != nullptr)\n\t\t\t{\n\t\t\t\tfloat gw = gammaPixel_ptr->getBGradOnly((float)(dI_l[idx][0]));\n\t\t\t\tdabs_l[idx] *= gw * gw;\n\t\t\t}\n\t\t}\n\t}\n\n\tselected_pixels_right = new float[wG[0] * hG[0]];\n}\n};"], ["/voxel_svio/include/feature.h", "class state {\n public:\n size_t feature_id;\n bool to_delete;\n std::unordered_map> uvs;\n std::unordered_map> uvs_norm;\n std::unordered_map> timestamps;\n std::unordered_map>> frames;\n std::unordered_map> color;\n int start_frame_id;\n int start_cam_id;\n int num_tracking;\n int anchor_cam_id = -1;\n double anchor_clone_timestamp;\n Eigen::Vector3d position_anchor;\n Eigen::Vector3d position_global;\n void cleanOldMeasurements(const std::vector &valid_times) {\n\tfor (auto const &pair : timestamps)\n\t{\n\t\tassert(timestamps[pair.first].size() == uvs[pair.first].size());\n\t\tassert(timestamps[pair.first].size() == uvs_norm[pair.first].size());\n\n\t\tauto it1 = timestamps[pair.first].begin();\n\t\tauto it2 = uvs[pair.first].begin();\n\t\tauto it3 = uvs_norm[pair.first].begin();\n\t\tauto it4 = frames[pair.first].begin();\n\t\tauto it5 = color[pair.first].begin();\n\n\t\twhile (it1 != timestamps[pair.first].end())\n\t\t{\n\t\t\tif (std::find(valid_times.begin(), valid_times.end(), *it1) == valid_times.end())\n\t\t\t{\n\t\t\t\tit1 = timestamps[pair.first].erase(it1);\n\t\t\t\tit2 = uvs[pair.first].erase(it2);\n\t\t\t\tit3 = uvs_norm[pair.first].erase(it3);\n\n\t\t\t\tassert((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end());\n\n\t\t\t\tif ((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end())\n\t\t\t\t{\n\t\t\t\t\t(*it4)->v_feat_ptr.at(pair.first).erase(feature_id);\n\t\t\t\t}\n\n\t\t\t\tit4 = frames[pair.first].erase(it4);\n\t\t\t\tit5 = color[pair.first].erase(it5);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tit1++;\n\t\t\t\tit2++;\n\t\t\t\tit3++;\n\t\t\t\tit4++;\n\t\t\t\tit5++;\n\t\t\t}\n\t\t}\n\t}\n}\n void cleanInvalidMeasurements(const std::vector &invalid_times) {\n\tfor (auto const &pair : timestamps)\n\t{\n\t\tassert(timestamps[pair.first].size() == uvs[pair.first].size());\n\t\tassert(timestamps[pair.first].size() == uvs_norm[pair.first].size());\n\n\t\tauto it1 = timestamps[pair.first].begin();\n\t\tauto it2 = uvs[pair.first].begin();\n\t\tauto it3 = uvs_norm[pair.first].begin();\n\t\tauto it4 = frames[pair.first].begin();\n\t\tauto it5 = color[pair.first].begin();\n\n\t\twhile (it1 != timestamps[pair.first].end())\n\t\t{\n\t\t\tif (std::find(invalid_times.begin(), invalid_times.end(), *it1) != invalid_times.end())\n\t\t\t{\n\t\t\t\tit1 = timestamps[pair.first].erase(it1);\n\t\t\t\tit2 = uvs[pair.first].erase(it2);\n\t\t\t\tit3 = uvs_norm[pair.first].erase(it3);\n\n\t\t\t\tassert((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end());\n\n\t\t\t\tif ((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end())\n\t\t\t\t{\n\t\t\t\t\t(*it4)->v_feat_ptr.at(pair.first).erase(feature_id);\n\t\t\t\t}\n\n\t\t\t\tit4 = frames[pair.first].erase(it4);\n\t\t\t\tit5 = color[pair.first].erase(it5);\n\t\t\t\t// 2024.10.31 yzk\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tit1++;\n\t\t\t\tit2++;\n\t\t\t\tit3++;\n\t\t\t\tit4++;\n\t\t\t\tit5++;\n\t\t\t}\n\t\t}\n\t}\n}\n void cleanOlderMeasurements(double timestamp) {\n\tfor (auto const &pair : timestamps)\n\t{\n\t\tassert(timestamps[pair.first].size() == uvs[pair.first].size());\n\t\tassert(timestamps[pair.first].size() == uvs_norm[pair.first].size());\n\n\t\tauto it1 = timestamps[pair.first].begin();\n\t\tauto it2 = uvs[pair.first].begin();\n\t\tauto it3 = uvs_norm[pair.first].begin();\n\t\tauto it4 = frames[pair.first].begin();\n\t\tauto it5 = color[pair.first].begin();\n\n\t\twhile (it1 != timestamps[pair.first].end())\n\t\t{\n\t\t\tif (*it1 <= timestamp)\n\t\t\t{\n\t\t\t\tit1 = timestamps[pair.first].erase(it1);\n\t\t\t\tit2 = uvs[pair.first].erase(it2);\n\t\t\t\tit3 = uvs_norm[pair.first].erase(it3);\n\n\t\t\t\tassert((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end());\n\n\t\t\t\tif ((*it4)->v_feat_ptr.at(pair.first).find(feature_id) != (*it4)->v_feat_ptr.at(pair.first).end())\n\t\t\t\t{\n\t\t\t\t\t(*it4)->v_feat_ptr.at(pair.first).erase(feature_id);\n\t\t\t\t}\n\n\t\t\t\tit4 = frames[pair.first].erase(it4);\n\t\t\t\tit5 = color[pair.first].erase(it5);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tit1++;\n\t\t\t\tit2++;\n\t\t\t\tit3++;\n\t\t\t\tit4++;\n\t\t\t\tit5++;\n\t\t\t}\n\t\t}\n\t}\n}\n public:\n featureDatabase() {\n\tlast_track_num = 0;\n}\n std::shared_ptr getFeature(size_t id, bool remove = false) {\n\tif (features_id_lookup.find(id) != features_id_lookup.end())\n\t{\n\t\tstd::shared_ptr temp = features_id_lookup.at(id);\n\t\tif (remove)\n\t\t{\n\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t{\n\t\t\t\tfor (auto it_fh = temp->frames[i].begin(); it_fh != temp->frames[i].end(); it_fh++)\n\t\t\t\t{\n\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find(temp->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase(temp->feature_id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfeatures_id_lookup.erase(id);\n\t\t}\n\t\treturn temp;\n\t}\n\telse\n\t{\n\t\treturn nullptr;\n\t}\n}\n void updateFeature(std::shared_ptr fh, size_t id, double timestamp, size_t cam_id, float u, float v, float u_n, float v_n) {\n\tif (features_id_lookup.find(id) != features_id_lookup.end())\n\t{\n\t\tstd::shared_ptr feat = features_id_lookup.at(id);\n\n\t\tif (u > 1.1 && u < wG[0] - 3 && v > 1.1 && v < hG[0] - 3)\n\t\t{\n\t\t\tfeat->uvs[cam_id].push_back(Eigen::Vector2f(u, v));\n\t\t\tfeat->uvs_norm[cam_id].push_back(Eigen::Vector2f(u_n, v_n));\n\t\t\tfeat->timestamps[cam_id].push_back(timestamp);\n\t\t\tfeat->frames[cam_id].push_back(fh);\n\n\t\t\tfloat color_temp = cam_id == 0 ? feat->frames[cam_id].back()->dI_left[(int)u + (int)v * wG[0]][0] \n\t\t\t\t: feat->frames[cam_id].back()->dI_right[(int)u + (int)v * wG[0]][0];\n\n\t\t\tassert(color_temp >= 0);\n\n\t\t\tfeat->color[cam_id].push_back(color_temp);\n\t\t\tfeat->num_tracking = fh->frame_id - feat->start_frame_id;\n\t\t\tfh->v_feat_ptr[cam_id][feat->feature_id] = feat;\n\n\t\t\tlast_track_num++;\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif (u > 1.1 && u < wG[0] - 3 && v > 1.1 && v < hG[0] - 3)\n\t{\n\t\tstd::shared_ptr feat = std::make_shared();\n\t\tfeat->feature_id = id;\n\t\tfeat->uvs[cam_id].push_back(Eigen::Vector2f(u, v));\n\t\tfeat->uvs_norm[cam_id].push_back(Eigen::Vector2f(u_n, v_n));\n\t\tfeat->timestamps[cam_id].push_back(timestamp);\n\t\tfeat->frames[cam_id].push_back(fh);\n\n\t\tfloat color_temp = cam_id == 0 ? feat->frames[cam_id].back()->dI_left[(int)u + (int)v * wG[0]][0] \n\t\t\t: feat->frames[cam_id].back()->dI_right[(int)u + (int)v * wG[0]][0];\n\n\t\tassert(color_temp >= 0);\n\n\t\tfeat->color[cam_id].push_back(color_temp);\n\t\tfeat->start_frame_id = fh->frame_id;\n\t\tfeat->start_cam_id = cam_id;\n\t\tfeat->num_tracking = 0;\n\n\t\tfeatures_id_lookup[id] = feat;\n\n\t\tfh->v_feat_ptr[cam_id][feat->feature_id] = feat;\n\t}\n}\n std::vector> getOldFeatures(double timestamp, bool remove = false, bool skip_deleted = false) {\n\tstd::vector> feats_old;\n\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\t// Skip if already deleted\n\t\tif (skip_deleted && (*it).second->to_delete)\n\t\t{\n\t\t\tit++;\n\t\t\tcontinue;\n\t\t}\n\n\t\tbool has_newer_measurement = false;\n\t\tfor (auto const &pair : (*it).second->timestamps)\n\t\t{\n\t\t\thas_newer_measurement = (!pair.second.empty() && pair.second.at(pair.second.size() - 1) >= timestamp);\n\t\t\tif (has_newer_measurement)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (!has_newer_measurement)\n\t\t{\n\t\t\tfeats_old.push_back((*it).second);\n\n\t\t\tif (remove)\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t\t{\n\t\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfeatures_id_lookup.erase(it++);\n\t\t\t}\n\t\t\telse\n\t\t\t\tit++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n\n\treturn feats_old;\n}\n std::vector> getFeatures(double timestamp, bool remove = false, bool skip_deleted = false) {\n\tstd::vector> feats_has_timestamp;\n\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\tif (skip_deleted && (*it).second->to_delete)\n\t\t{\n\t\t\tit++;\n\t\t\tcontinue;\n\t\t}\n \n\t\tbool has_timestamp = false;\n\t\tfor (auto const &pair : (*it).second->timestamps)\n\t\t{\n\t\t\thas_timestamp = (std::find(pair.second.begin(), pair.second.end(), timestamp) != pair.second.end());\n\t\t\tif (has_timestamp)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (has_timestamp)\n\t\t{\n\t\t\tfeats_has_timestamp.push_back((*it).second);\n\n\t\t\tif (remove)\n\t\t\t{\n\t\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t\t{\n\t\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t\t{\n\t\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfeatures_id_lookup.erase(it++);\n\t\t\t}\n\t\t\telse\n\t\t\t\tit++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n\n\treturn feats_has_timestamp;\n}\n void cleanUp() {\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\tif ((*it).second->to_delete)\n\t\t{\n\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t{\n\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t{\n\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfeatures_id_lookup.erase(it++);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n}\n void cleanUpOldmeasurements(double timestamp) {\n\tfor (auto it = features_id_lookup.begin(); it != features_id_lookup.end();)\n\t{\n\t\t(*it).second->cleanOlderMeasurements(timestamp);\n\t\t\n\t\tint count_meas = 0;\n\n\t\tfor (const auto &pair : (*it).second->timestamps)\n\t\t{\n\t\t\tcount_meas += (int)(pair.second.size());\n\t\t}\n\n\t\tif (count_meas < 1)\n\t\t{\n\t\t\tfor (int i = 0; i < 2; i++)\n\t\t\t{\n\t\t\t\tfor (auto it_fh = (*it).second->frames[i].begin(); it_fh != (*it).second->frames[i].end(); it_fh++)\n\t\t\t\t{\n\t\t\t\t\tif ((*it_fh) == nullptr || (*it_fh)->is_invalid) continue;\n\n\t\t\t\t\tif ((*it_fh)->v_feat_ptr.at(i).find((*it).second->feature_id) != (*it_fh)->v_feat_ptr.at(i).end())\n\t\t\t\t\t\t(*it_fh)->v_feat_ptr.at(i).erase((*it).second->feature_id);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfeatures_id_lookup.erase(it++);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit++;\n\t\t}\n\t}\n}\n size_t getSize() {\n\treturn features_id_lookup.size();\n}\n std::unordered_map> getInternalData() {\n\treturn features_id_lookup;\n}\n bool featureCheckParallax(int frame_count) {\n double parallax_sum = 0;\n int parallax_num = 0;\n\n if (frame_count < 2 || last_track_num < 20)\n {\n \tlast_track_num = 0;\n return true;\n }\n\n last_track_num = 0;\n\n for (auto it = features_id_lookup.begin(); it != features_id_lookup.end(); it++)\n\t{\n\t\tif ((*it).second->start_frame_id <= frame_count - 2)\n\t\t{\n\t\t\tif ((*it).second->start_cam_id == 0 && (*it).second->start_frame_id + int((*it).second->timestamps.at(0).size()) - 1 >= frame_count)\n\t\t\t{\n\t\t\t\tparallax_sum += compensatedParallax2((*it).first, 0, frame_count);\n \tparallax_num++;\n\t\t\t}\n\t\t\telse if ((*it).second->start_cam_id == 1 && (*it).second->start_frame_id + int((*it).second->timestamps.at(1).size()) - 1 >= frame_count)\n\t\t\t{\n\t\t\t\tparallax_sum += compensatedParallax2((*it).first, 1, frame_count);\n \tparallax_num++;\n\t\t\t}\n\t\t}\n\t}\n\n if (parallax_num == 0)\n {\n return true;\n }\n else\n {\n return parallax_sum / parallax_num >= setting_min_parallax;\n }\n}\n double compensatedParallax2(int feat_id, int cam_id, int frame_count) {\n\tstd::shared_ptr feat_ptr = features_id_lookup.at(feat_id);\n\n\tEigen::Vector2f uvs_norm_i = feat_ptr->uvs_norm.at(cam_id).at(frame_count - 2 - feat_ptr->start_frame_id);\n\tEigen::Vector2f uvs_norm_j = feat_ptr->uvs_norm.at(cam_id).at(frame_count - 1 - feat_ptr->start_frame_id);\n\n float ans = 0;\n float du = uvs_norm_i(0) - uvs_norm_j(0);\n float dv = uvs_norm_i(1) - uvs_norm_j(1);\n\n ans = std::max(ans, sqrt(du * du + dv * dv));\n\n return ans;\n}\n protected:\n std::unordered_map> features_id_lookup;\n int last_track_num;\n public:\n struct clonePose\n {\n Eigen::Matrix rotation;\n\n Eigen::Matrix position;\n\n clonePose(const Eigen::Matrix &R, const Eigen::Matrix &p)\n {\n rotation = R;\n position = p;\n }\n\n clonePose(const Eigen::Matrix &q, const Eigen::Matrix &p)\n {\n rotation = quatType::quatToRot(q);\n position = p;\n }\n\n clonePose()\n {\n rotation = Eigen::Matrix::Identity();\n position = Eigen::Matrix::Zero();\n }\n\n const Eigen::Matrix &Rot() { return rotation; }\n\n const Eigen::Matrix &pos() { return position; }\n };\n featureInitializer(featureInitializerOptions &options_) {\n\n}\n bool singleTriangulation(std::shared_ptr feat, std::unordered_map> &clones_cam) {\n\tint total_meas = 0;\n\tsize_t anchor_most_meas = 0;\n\tsize_t most_meas = 0;\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\t\n\t\ttotal_meas += (int)pair.second.size();\n\t\t\n\t\tif (pair.second.size() > most_meas)\n \t{\n\t\t\tanchor_most_meas = pair.first;\n\t\t\tmost_meas = pair.second.size();\n\t\t}\n\t}\n\n\tfeat->anchor_cam_id = anchor_most_meas;\n\tfeat->anchor_clone_timestamp = feat->timestamps.at(feat->anchor_cam_id).back();\n\n\tEigen::Matrix3d A = Eigen::Matrix3d::Zero();\n\tEigen::Vector3d b = Eigen::Vector3d::Zero();\n\n\tclonePose anchor_clone = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp);\n\tconst Eigen::Matrix &R_GtoA = anchor_clone.Rot();\n\tconst Eigen::Matrix &p_AinG = anchor_clone.pos();\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\n\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t{\n\t\t\tconst Eigen::Matrix &R_GtoCi = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).Rot();\n\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).pos();\n\n\t\t\tEigen::Matrix R_AtoCi;\n\t\t\tR_AtoCi.noalias() = R_GtoCi * R_GtoA.transpose();\n\t\t\tEigen::Matrix p_CiinA;\n\t\t\tp_CiinA.noalias() = R_GtoA * (p_CiinG - p_AinG);\n\n\t\t\tEigen::Matrix b_i;\n\t\t\tb_i << feat->uvs_norm.at(pair.first).at(m)(0), feat->uvs_norm.at(pair.first).at(m)(1), 1;\n\t\t\tb_i = R_AtoCi.transpose() * b_i;\n\t\t\tb_i = b_i / b_i.norm();\n\t\t\tEigen::Matrix3d B_perp = quatType::skewSymmetric(b_i);\n\n\t\t\tEigen::Matrix3d Ai = B_perp.transpose() * B_perp;\n\t\t\tA += Ai;\n\t\t\tb += Ai * p_CiinA;\n\t\t}\n\t}\n\n\tEigen::MatrixXd p_f = A.colPivHouseholderQr().solve(b);\n\n\tEigen::JacobiSVD svd(A);\n\tEigen::MatrixXd singular_values;\n\tsingular_values.resize(svd.singularValues().rows(), 1);\n\tsingular_values = svd.singularValues();\n\tdouble cond_A = singular_values(0, 0) / singular_values(singular_values.rows() - 1, 0);\n\n\tif (std::abs(cond_A) > options.max_cond_number || p_f(2, 0) < options.min_dist || p_f(2, 0) > options.max_dist || std::isnan(p_f.norm()))\n\t{\n\t\treturn false;\n\t}\n\n\tfeat->position_anchor = p_f;\n\tfeat->position_global = R_GtoA.transpose() * feat->position_anchor + p_AinG;\n\n\treturn true;\n}\n bool singleGaussnewton(std::shared_ptr feat, std::unordered_map> &clones_cam) {\n\tdouble rho = 1 / feat->position_anchor(2);\n\tdouble alpha = feat->position_anchor(0) / feat->position_anchor(2);\n\tdouble beta = feat->position_anchor(1) / feat->position_anchor(2);\n\n\tdouble lam = options.init_lamda;\n\tdouble eps = 10000;\n\tint runs = 0;\n\n\tbool recompute = true;\n\tEigen::Matrix Hess = Eigen::Matrix::Zero();\n\tEigen::Matrix grad = Eigen::Matrix::Zero();\n\n\tdouble cost_old = computeError(clones_cam, feat, alpha, beta, rho);\n\n\tconst Eigen::Matrix &R_GtoA = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).Rot();\n\tconst Eigen::Matrix &p_AinG = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).pos();\n\n\twhile (runs < options.max_runs && lam < options.max_lamda && eps > options.min_dx)\n\t{\n\t\tif (recompute)\n\t\t{\n\t\t\tHess.setZero();\n\t\t\tgrad.setZero();\n\n\t\t\tdouble err = 0;\n\n\t\t\tfor (auto const &pair : feat->timestamps)\n\t\t\t{\n\t\t\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t\t\t{\n\t\t\t\t\tconst Eigen::Matrix &R_GtoCi = clones_cam.at(pair.first).at(feat->timestamps[pair.first].at(m)).Rot();\n\t\t\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps[pair.first].at(m)).pos();\n\n\t\t\t\t\tEigen::Matrix R_AtoCi;\n\t\t\t\t\tR_AtoCi.noalias() = R_GtoCi * R_GtoA.transpose();\n\t\t\t\t\tEigen::Matrix p_CiinA;\n\t\t\t\t\tp_CiinA.noalias() = R_GtoA * (p_CiinG - p_AinG);\n\t\t\t\t\tEigen::Matrix p_AinCi;\n\t\t\t\t\tp_AinCi.noalias() = -R_AtoCi * p_CiinA;\n\n\t\t\t\t\tdouble hi1 = R_AtoCi(0, 0) * alpha + R_AtoCi(0, 1) * beta + R_AtoCi(0, 2) + rho * p_AinCi(0, 0);\n\t\t\t\t\tdouble hi2 = R_AtoCi(1, 0) * alpha + R_AtoCi(1, 1) * beta + R_AtoCi(1, 2) + rho * p_AinCi(1, 0);\n\t\t\t\t\tdouble hi3 = R_AtoCi(2, 0) * alpha + R_AtoCi(2, 1) * beta + R_AtoCi(2, 2) + rho * p_AinCi(2, 0);\n\n\t\t\t\t\tdouble d_z1_d_alpha = (R_AtoCi(0, 0) * hi3 - hi1 * R_AtoCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z1_d_beta = (R_AtoCi(0, 1) * hi3 - hi1 * R_AtoCi(2, 1)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z1_d_rho = (p_AinCi(0, 0) * hi3 - hi1 * p_AinCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z2_d_alpha = (R_AtoCi(1, 0) * hi3 - hi2 * R_AtoCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z2_d_beta = (R_AtoCi(1, 1) * hi3 - hi2 * R_AtoCi(2, 1)) / (pow(hi3, 2));\n\t\t\t\t\tdouble d_z2_d_rho = (p_AinCi(1, 0) * hi3 - hi2 * p_AinCi(2, 0)) / (pow(hi3, 2));\n\t\t\t\t\tEigen::Matrix H;\n\t\t\t\t\tH << d_z1_d_alpha, d_z1_d_beta, d_z1_d_rho, d_z2_d_alpha, d_z2_d_beta, d_z2_d_rho;\n\n\t\t\t\t\tEigen::Matrix z;\n\t\t\t\t\tz << hi1 / hi3, hi2 / hi3;\n\t\t\t\t\tEigen::Matrix res = feat->uvs_norm.at(pair.first).at(m) - z;\n\n\t\t\t\t\terr += std::pow(res.norm(), 2);\n\t\t\t\t\tgrad.noalias() += H.transpose() * res.cast();\n\t\t\t\t\tHess.noalias() += H.transpose() * H;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tEigen::Matrix Hess_l = Hess;\n\n\t\tfor (size_t r = 0; r < (size_t)Hess.rows(); r++)\n\t\t{\n\t\t\tHess_l(r, r) *= (1.0 + lam);\n\t\t}\n\n\t\tEigen::Matrix dx = Hess_l.colPivHouseholderQr().solve(grad);\n\n\t\tdouble cost = computeError(clones_cam, feat, alpha + dx(0, 0), beta + dx(1, 0), rho + dx(2, 0));\n\n\t\tif (cost <= cost_old && (cost_old - cost) / cost_old < options.min_dcost)\n\t\t{\n\t\t\talpha += dx(0, 0);\n\t\t\tbeta += dx(1, 0);\n\t\t\trho += dx(2, 0);\n\t\t\teps = 0;\n\t\t\tbreak;\n\t\t}\n\n\t\tif (cost <= cost_old)\n\t\t{\n\t\t\trecompute = true;\n\t\t\tcost_old = cost;\n\t\t\talpha += dx(0, 0);\n\t\t\tbeta += dx(1, 0);\n\t\t\trho += dx(2, 0);\n\t\t\truns++;\n\t\t\tlam = lam / options.lam_mult;\n\t\t\teps = dx.norm();\n\t\t}\n\t\telse\n\t\t{\n\t\t\trecompute = false;\n\t\t\tlam = lam * options.lam_mult;\n\t\t\tcontinue;\n\t\t}\n\t}\n\n\tfeat->position_anchor(0) = alpha / rho;\n\tfeat->position_anchor(1) = beta / rho;\n\tfeat->position_anchor(2) = 1 / rho;\n\n\tEigen::HouseholderQR qr(feat->position_anchor);\n\tEigen::MatrixXd Q = qr.householderQ();\n\n\tdouble base_line_max = 0.0;\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\n\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t{\n\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).pos();\n\n\t\t\tEigen::Matrix p_CiinA = R_GtoA * (p_CiinG - p_AinG);\n\n\t\t\tdouble base_line = ((Q.block(0, 1, 3, 2)).transpose() * p_CiinA).norm();\n\n\t\t\tif (base_line > base_line_max)\n\t\t\t\tbase_line_max = base_line;\n\t\t}\n\t}\n\n\tif (feat->position_anchor(2) < options.min_dist || feat->position_anchor(2) > options.max_dist ||\n\t\t(feat->position_anchor.norm() / base_line_max) > options.max_baseline || std::isnan(feat->position_anchor.norm()))\n\t{\n\t\treturn false;\n\t}\n\n\tfeat->position_global = R_GtoA.transpose() * feat->position_anchor + p_AinG;\n\t\n\treturn true;\n}\n protected:\n featureInitializerOptions options;\n double computeError(std::unordered_map> &clones_cam, std::shared_ptr feat,\n double alpha, double beta, double rho) {\n\tdouble err = 0;\n\n\tconst Eigen::Matrix &R_GtoA = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).Rot();\n\tconst Eigen::Matrix &p_AinG = clones_cam.at(feat->anchor_cam_id).at(feat->anchor_clone_timestamp).pos();\n\n\tfor (auto const &pair : feat->timestamps)\n\t{\n\t\tfor (size_t m = 0; m < feat->timestamps.at(pair.first).size(); m++)\n\t\t{\n\t\t\tconst Eigen::Matrix &R_GtoCi = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).Rot();\n\t\t\tconst Eigen::Matrix &p_CiinG = clones_cam.at(pair.first).at(feat->timestamps.at(pair.first).at(m)).pos();\n \n\t\t\tEigen::Matrix R_AtoCi;\n\t\t\tR_AtoCi.noalias() = R_GtoCi * R_GtoA.transpose();\n\t\t\tEigen::Matrix p_CiinA;\n\t\t\tp_CiinA.noalias() = R_GtoA * (p_CiinG - p_AinG);\n\t\t\tEigen::Matrix p_AinCi;\n\t\t\tp_AinCi.noalias() = -R_AtoCi * p_CiinA;\n\n\t\t\tdouble hi1 = R_AtoCi(0, 0) * alpha + R_AtoCi(0, 1) * beta + R_AtoCi(0, 2) + rho * p_AinCi(0, 0);\n\t\t\tdouble hi2 = R_AtoCi(1, 0) * alpha + R_AtoCi(1, 1) * beta + R_AtoCi(1, 2) + rho * p_AinCi(1, 0);\n\t\t\tdouble hi3 = R_AtoCi(2, 0) * alpha + R_AtoCi(2, 1) * beta + R_AtoCi(2, 2) + rho * p_AinCi(2, 0);\n \n\t\t\tEigen::Matrix z;\n\t\t\tz << hi1 / hi3, hi2 / hi3;\n\t\t\tEigen::Matrix res = feat->uvs_norm.at(pair.first).at(m) - z;\n\n\t\t\terr += pow(res.norm(), 2);\n\t\t}\n\t}\n\n\treturn err;\n}\n};"], ["/voxel_svio/include/featureTracker.h", "class feature {\n public:\n trackKLT(std::unordered_map> camera_calib_, int num_features_, \n\t\tHistogramMethod histogram_method_, int fast_threshold_, int patch_size_x_, int patch_size_y_, int min_px_dist_) {\n current_id = 1;\n\n pixelSelector_ptr = std::make_shared(wG[0], hG[0]);\n}\n void feedNewImage(const cameraData &image_measurements, std::shared_ptr fh) {\n if (image_measurements.camera_ids.empty() || image_measurements.camera_ids.size() != image_measurements.images.size() \n || image_measurements.images.size() != image_measurements.masks.size())\n {\n std::cout << \"[ERROR]: image_measurements data sizes do not match or empty.\" << std::endl;\n std::cout << \"[ERROR]: - image_measurements.camera_ids.size() = \" << image_measurements.camera_ids.size() << std::endl;\n std::cout << \"[ERROR]: - image_measurements.images.size() = \" << image_measurements.images.size() << std::endl;\n std::cout << \"[ERROR]: - image_measurements.masks.size() = \" << image_measurements.masks.size() << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n rT1 = boost::posix_time::microsec_clock::local_time();\n size_t num_images = image_measurements.images.size();\n\n for (size_t image_id = 0; image_id < num_images; image_id++)\n {\n size_t cam_id = image_measurements.camera_ids.at(image_id);\n\n cv::Mat image;\n\n if (histogram_method == HistogramMethod::HISTOGRAM)\n {\n cv::equalizeHist(image_measurements.images.at(image_id), image);\n }\n else if (histogram_method == HistogramMethod::CLAHE)\n {\n double eq_clip_limit = 10.0;\n cv::Size eq_win_size = cv::Size(8, 8);\n cv::Ptr clahe = cv::createCLAHE(eq_clip_limit, eq_win_size);\n clahe->apply(image_measurements.images.at(image_id), image);\n }\n else\n {\n image = image_measurements.images.at(image_id);\n }\n\n std::vector img_pyr;\n cv::buildOpticalFlowPyramid(image, img_pyr, win_size, pyr_levels);\n\n img_curr[cam_id] = image;\n img_pyramid_curr[cam_id] = img_pyr;\n }\n\n feedStereo(image_measurements, fh, 0, 1);\n}\n std::shared_ptr getFeatureDatabase() {\n return database;\n}\n std::unordered_map> getLastObs() {\n return pts_last;\n}\n std::unordered_map> getLastIds() {\n return ids_last;\n}\n void setNumFeatures(int num_features_) {\n num_features = num_features_;\n}\n void displayActive(cv::Mat &img_out, int r1, int g1, int b1, int r2, int g2, int b2, std::string overlay = \"\") {\n std::map img_last_cache, img_mask_last_cache;\n std::unordered_map> pts_last_cache;\n {\n img_last_cache = img_last;\n img_mask_last_cache = img_mask_last;\n pts_last_cache = pts_last;\n }\n\n int max_width = -1;\n int max_height = -1;\n\n for (auto const &pair : img_last_cache)\n {\n if (max_width < pair.second.cols)\n max_width = pair.second.cols;\n if (max_height < pair.second.rows)\n max_height = pair.second.rows;\n }\n\n if (img_last_cache.empty() || max_width == -1 || max_height == -1)\n return;\n\n bool is_small = (std::min(max_width, max_height) < 400);\n\n bool image_new = ((int)img_last_cache.size() * max_width != img_out.cols || max_height != img_out.rows);\n\n if (image_new)\n img_out = cv::Mat(max_height, (int)img_last_cache.size() * max_width, CV_8UC3, cv::Scalar(0, 0, 0));\n\n int index_cam = 0;\n for (auto const &pair : img_last_cache)\n {\n cv::Mat img_temp;\n\n if (image_new)\n cv::cvtColor(img_last_cache[pair.first], img_temp, cv::COLOR_GRAY2RGB);\n else\n img_temp = img_out(cv::Rect(max_width * index_cam, 0, max_width, max_height));\n\n for (size_t i = 0; i < pts_last_cache[pair.first].size(); i++)\n {\n cv::Point2f pt_l = pts_last_cache[pair.first].at(i).pt;\n cv::circle(img_temp, pt_l, (is_small) ? 1 : 2, cv::Scalar(r1, g1, b1), cv::FILLED);\n\n cv::Point2f pt_l_top = cv::Point2f(pt_l.x - 3, pt_l.y - 3);\n cv::Point2f pt_l_bot = cv::Point2f(pt_l.x + 3, pt_l.y + 3);\n cv::rectangle(img_temp, pt_l_top, pt_l_bot, cv::Scalar(r2, g2, b2), 1);\n }\n\n auto txtpt = (is_small) ? cv::Point(10, 30) : cv::Point(30, 60);\n \n if (overlay == \"\")\n {\n cv::putText(img_temp, \"CAM:\" + std::to_string((int)pair.first), txtpt, cv::FONT_HERSHEY_COMPLEX_SMALL, (is_small) ? 1.5 : 3.0, cv::Scalar(0, 255, 0), 3);\n }\n else\n {\n cv::putText(img_temp, overlay, txtpt, cv::FONT_HERSHEY_COMPLEX_SMALL, (is_small) ? 1.5 : 3.0, cv::Scalar(0, 0, 255), 3);\n }\n\n cv::Mat mask = cv::Mat::zeros(img_mask_last_cache[pair.first].rows, img_mask_last_cache[pair.first].cols, CV_8UC3);\n mask.setTo(cv::Scalar(0, 0, 255), img_mask_last_cache[pair.first]);\n cv::addWeighted(mask, 0.1, img_temp, 1.0, 0.0, img_temp);\n\n img_temp.copyTo(img_out(cv::Rect(max_width * index_cam, 0, img_last_cache[pair.first].cols, img_last_cache[pair.first].rows)));\n index_cam++;\n }\n}\n private:\n void feedStereo(const cameraData &image_measurements, std::shared_ptr fh, size_t image_id_left, size_t image_id_right) {\n size_t cam_id_left = image_measurements.camera_ids.at(image_id_left);\n size_t cam_id_right = image_measurements.camera_ids.at(image_id_right);\n\n cv::Mat img_left = img_curr.at(cam_id_left);\n cv::Mat img_right = img_curr.at(cam_id_right);\n std::vector img_pyr_left = img_pyramid_curr.at(cam_id_left);\n std::vector img_pyr_right = img_pyramid_curr.at(cam_id_right);\n cv::Mat mask_left = image_measurements.masks.at(image_id_left);\n cv::Mat mask_right = image_measurements.masks.at(image_id_right);\n rT2 = boost::posix_time::microsec_clock::local_time();\n\n if (pts_last[cam_id_left].empty() && pts_last[cam_id_right].empty())\n {\n std::vector good_left, good_right;\n std::vector good_ids_left, good_ids_right;\n performDetectionStereo(fh, img_pyr_left, img_pyr_right, mask_left, mask_right, cam_id_left, cam_id_right, good_left, good_right, good_ids_left, good_ids_right);\n \n img_last[cam_id_left] = img_left;\n img_last[cam_id_right] = img_right;\n img_pyramid_last[cam_id_left] = img_pyr_left;\n img_pyramid_last[cam_id_right] = img_pyr_right;\n img_mask_last[cam_id_left] = mask_left;\n img_mask_last[cam_id_right] = mask_right;\n pts_last[cam_id_left] = good_left;\n pts_last[cam_id_right] = good_right;\n ids_last[cam_id_left] = good_ids_left;\n ids_last[cam_id_right] = good_ids_right;\n \n return;\n }\n\n int pts_before_detect = (int)pts_last[cam_id_left].size();\n auto pts_left_old = pts_last[cam_id_left];\n auto pts_right_old = pts_last[cam_id_right];\n auto ids_left_old = ids_last[cam_id_left];\n auto ids_right_old = ids_last[cam_id_right];\n performDetectionStereo(fh, img_pyramid_last[cam_id_left], img_pyramid_last[cam_id_right], img_mask_last[cam_id_left],\n img_mask_last[cam_id_right], cam_id_left, cam_id_right, pts_left_old, pts_right_old, ids_left_old, ids_right_old);\n\n rT3 = boost::posix_time::microsec_clock::local_time();\n\n std::vector mask_ll, mask_rr;\n std::vector pts_left_new = pts_left_old;\n std::vector pts_right_new = pts_right_old;\n\n parallel_for_(cv::Range(0, 2), lambdaBody([&](const cv::Range &range) {\n for (int i = range.start; i < range.end; i++) {\n bool is_left = (i == 0);\n performMatching(img_pyramid_last[is_left ? cam_id_left : cam_id_right], is_left ? img_pyr_left : img_pyr_right,\n is_left ? pts_left_old : pts_right_old, is_left ? pts_left_new : pts_right_new,\n is_left ? cam_id_left : cam_id_right, is_left ? cam_id_left : cam_id_right,\n is_left ? mask_ll : mask_rr);\n }\n }));\n rT4 = boost::posix_time::microsec_clock::local_time();\n\n\n rT5 = boost::posix_time::microsec_clock::local_time();\n\n if (mask_ll.empty() && mask_rr.empty())\n {\n img_last[cam_id_left] = img_left;\n img_last[cam_id_right] = img_right;\n img_pyramid_last[cam_id_left] = img_pyr_left;\n img_pyramid_last[cam_id_right] = img_pyr_right;\n img_mask_last[cam_id_left] = mask_left;\n img_mask_last[cam_id_right] = mask_right;\n pts_last[cam_id_left].clear();\n pts_last[cam_id_right].clear();\n ids_last[cam_id_left].clear();\n ids_last[cam_id_right].clear();\n std::cout << \"[trackKLT::feedStereo]: Not enough points for RANSAC, resetting.\" << std::endl;\n \n return;\n }\n \n std::vector good_left, good_right;\n std::vector good_ids_left, good_ids_right;\n\n for (size_t i = 0; i < pts_left_new.size(); i++)\n {\n if (pts_left_new.at(i).pt.x < 0 || pts_left_new.at(i).pt.y < 0 || (int)pts_left_new.at(i).pt.x > img_left.cols ||\n (int)pts_left_new.at(i).pt.y > img_left.rows)\n continue;\n \n bool found_right = false;\n size_t index_right = 0;\n for (size_t n = 0; n < ids_right_old.size(); n++)\n {\n if (ids_left_old.at(i) == ids_right_old.at(n))\n {\n found_right = true;\n index_right = n;\n break;\n }\n }\n \n if (mask_ll[i] && found_right && mask_rr[index_right])\n {\n if (pts_right_new.at(index_right).pt.x < 0 || pts_right_new.at(index_right).pt.y < 0 || \n (int)pts_right_new.at(index_right).pt.x >= img_right.cols || (int)pts_right_new.at(index_right).pt.y >= img_right.rows)\n continue;\n\n good_left.push_back(pts_left_new.at(i));\n good_right.push_back(pts_right_new.at(index_right));\n good_ids_left.push_back(ids_left_old.at(i));\n good_ids_right.push_back(ids_right_old.at(index_right));\n }\n else if (mask_ll[i])\n {\n good_left.push_back(pts_left_new.at(i));\n good_ids_left.push_back(ids_left_old.at(i));\n }\n }\n\n for (size_t i = 0; i < pts_right_new.size(); i++)\n {\n if (pts_right_new.at(i).pt.x < 0 || pts_right_new.at(i).pt.y < 0 || (int)pts_right_new.at(i).pt.x >= img_right.cols ||\n (int)pts_right_new.at(i).pt.y >= img_right.rows)\n continue;\n \n bool added_already = (std::find(good_ids_right.begin(), good_ids_right.end(), ids_right_old.at(i)) != good_ids_right.end());\n\n if (mask_rr[i] && !added_already)\n {\n good_right.push_back(pts_right_new.at(i));\n good_ids_right.push_back(ids_right_old.at(i));\n }\n }\n\n for (size_t i = 0; i < good_left.size(); i++)\n {\n cv::Point2f npt_l = camera_calib.at(cam_id_left)->undistortCV(good_left.at(i).pt);\n database->updateFeature(fh, good_ids_left.at(i), image_measurements.timestamp, cam_id_left, good_left.at(i).pt.x, good_left.at(i).pt.y, npt_l.x, npt_l.y);\n }\n for (size_t i = 0; i < good_right.size(); i++)\n {\n cv::Point2f npt_r = camera_calib.at(cam_id_right)->undistortCV(good_right.at(i).pt);\n database->updateFeature(fh, good_ids_right.at(i), image_measurements.timestamp, cam_id_right, good_right.at(i).pt.x, good_right.at(i).pt.y, npt_r.x, npt_r.y);\n }\n\n {\n img_last[cam_id_left] = img_left;\n img_last[cam_id_right] = img_right;\n img_pyramid_last[cam_id_left] = img_pyr_left;\n img_pyramid_last[cam_id_right] = img_pyr_right;\n img_mask_last[cam_id_left] = mask_left;\n img_mask_last[cam_id_right] = mask_right;\n pts_last[cam_id_left] = good_left;\n pts_last[cam_id_right] = good_right;\n ids_last[cam_id_left] = good_ids_left;\n ids_last[cam_id_right] = good_ids_right;\n }\n rT6 = boost::posix_time::microsec_clock::local_time();\n\n // Time test\n /*\n std::cout << std::fixed << \"[trackKLT]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds for pyramid.\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds for detection (\" << (int)pts_last[cam_id_left].size() - pts_before_detect << \" detected).\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" seconds for temporal klt.\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" seconds for stereo klt.\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" seconds for feature DB update (\" << (int)good_left.size() << \" features).\" << std::endl;\n std::cout << std::fixed << \"[trackKLT]: \" << (rT6 - rT1).total_microseconds() * 1e-6 << \" seconds for total.\" << std::endl;\n */\n // Time test\n}\n void performDetectionStereo(std::shared_ptr fh, const std::vector &img_0_pyr, const std::vector &img_1_pyr, const cv::Mat &mask_0, const cv::Mat &mask_1, \n\t\tsize_t cam_id_left, size_t cam_id_right, std::vector &pts_0, std::vector &pts_1, std::vector &ids_0, std::vector &ids_1) {\n cv::Size size_close_0((int)((float)img_0_pyr.at(0).cols / (float)min_px_dist),\n (int)((float)img_0_pyr.at(0).rows / (float)min_px_dist));\n cv::Mat grid_2d_close_0 = cv::Mat::zeros(size_close_0, CV_8UC1);\n float size_x_0 = (float)img_0_pyr.at(0).cols / (float)patch_size_x;\n float size_y_0 = (float)img_0_pyr.at(0).rows / (float)patch_size_y;\n\n cv::Size size_grid_0(patch_size_x, patch_size_y);\n cv::Mat grid_2d_grid_0 = cv::Mat::zeros(size_grid_0, CV_8UC1);\n cv::Mat mask_0_updated = mask_0.clone();\n\n auto it_0 = pts_0.begin();\n auto it_1 = ids_0.begin();\n\n while (it_0 != pts_0.end())\n {\n cv::KeyPoint kpt = *it_0;\n int x = (int)kpt.pt.x;\n int y = (int)kpt.pt.y;\n\n int edge = 10;\n if (x < edge || x >= img_0_pyr.at(0).cols - edge || y < edge || y >= img_0_pyr.at(0).rows - edge)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n\n int x_close = (int)(kpt.pt.x / (float)min_px_dist);\n int y_close = (int)(kpt.pt.y / (float)min_px_dist);\n if (x_close < 0 || x_close >= size_close_0.width || y_close < 0 || y_close >= size_close_0.height)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n\n int x_grid = std::floor(kpt.pt.x / size_x_0);\n int y_grid = std::floor(kpt.pt.y / size_y_0);\n\n if (x_grid < 0 || x_grid >= size_grid_0.width || y_grid < 0 || y_grid >= size_grid_0.height)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n\n if (grid_2d_close_0.at(y_close, x_close) > 127)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n \n if (mask_0.at(y, x) > 127)\n {\n it_0 = pts_0.erase(it_0);\n it_1 = ids_0.erase(it_1);\n continue;\n }\n \n grid_2d_close_0.at(y_close, x_close) = 255;\n if (grid_2d_grid_0.at(y_grid, x_grid) < 255)\n {\n grid_2d_grid_0.at(y_grid, x_grid) += 1;\n }\n \n if (x - min_px_dist >= 0 && x + min_px_dist < img_0_pyr.at(0).cols && y - min_px_dist >= 0 && y + min_px_dist < img_0_pyr.at(0).rows)\n {\n cv::Point pt_1(x - min_px_dist, y - min_px_dist);\n cv::Point pt_2(x + min_px_dist, y + min_px_dist);\n cv::rectangle(mask_0_updated, pt_1, pt_2, cv::Scalar(255), -1);\n\n int begin_idx = x - min_px_dist + wG[0] * (y - min_px_dist);\n int end_idx = x + min_px_dist + wG[0] * (y + min_px_dist);\n\n for(int i = begin_idx; i <= end_idx; i++) fh->mask_left[i] = true;\n }\n\n it_0++;\n it_1++;\n }\n\n double min_feat_percent = 0.50;\n int num_featsneeded_0 = num_features - (int)pts_0.size();\n\n if (num_featsneeded_0 > std::min(20, (int)(min_feat_percent * num_features)))\n {\n pixelSelector_ptr->pixelSelectionLeft(fh, num_featsneeded_0);\n\n std::vector kpts_0_new;\n std::vector pts_0_new;\n\n for (int y = 0; y < hG[0]; y++)\n {\n for (int x = 0; x < wG[0]; x++)\n {\n int idx = x + y * wG[0];\n\n if (fh->selected_pixels_left[idx] == 0) continue;\n\n cv::KeyPoint kpt;\n kpt.pt.x = x;\n kpt.pt.y = y;\n\n kpts_0_new.push_back(kpt);\n pts_0_new.push_back(kpt.pt);\n }\n }\n\n std::vector kpts_1_new;\n std::vector pts_1_new;\n kpts_1_new = kpts_0_new;\n pts_1_new = pts_0_new;\n\n if (!pts_0_new.empty())\n {\n std::vector mask;\n std::vector error;\n cv::TermCriteria term_crit = cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 30, 0.01);\n cv::calcOpticalFlowPyrLK(img_0_pyr, img_1_pyr, pts_0_new, pts_1_new, mask, error, win_size, pyr_levels, term_crit, cv::OPTFLOW_USE_INITIAL_FLOW);\n\n for (size_t i = 0; i < pts_0_new.size(); i++)\n {\n bool oob_left = ((int)pts_0_new.at(i).x < 0 || (int)pts_0_new.at(i).x >= img_0_pyr.at(0).cols || (int)pts_0_new.at(i).y < 0 ||\n (int)pts_0_new.at(i).y >= img_0_pyr.at(0).rows);\n bool oob_right = ((int)pts_1_new.at(i).x < 0 || (int)pts_1_new.at(i).x >= img_1_pyr.at(0).cols || (int)pts_1_new.at(i).y < 0 ||\n (int)pts_1_new.at(i).y >= img_1_pyr.at(0).rows);\n\n if (!oob_left && !oob_right && mask[i] == 1)\n {\n kpts_0_new.at(i).pt = pts_0_new.at(i);\n kpts_1_new.at(i).pt = pts_1_new.at(i);\n \n pts_0.push_back(kpts_0_new.at(i));\n pts_1.push_back(kpts_1_new.at(i));\n \n size_t temp = ++current_id;\n ids_0.push_back(temp);\n ids_1.push_back(temp);\n }\n else if (!oob_left)\n {\n kpts_0_new.at(i).pt = pts_0_new.at(i);\n pts_0.push_back(kpts_0_new.at(i));\n \n size_t temp = ++current_id;\n ids_0.push_back(temp);\n }\n }\n }\n }\n\n cv::Size size_close_1((int)((float)img_1_pyr.at(0).cols / (float)min_px_dist), (int)((float)img_1_pyr.at(0).rows / (float)min_px_dist));\n cv::Mat grid_2d_close_1 = cv::Mat::zeros(size_close_1, CV_8UC1);\n float size_x_1 = (float)img_1_pyr.at(0).cols / (float)patch_size_x;\n float size_y_1 = (float)img_1_pyr.at(0).rows / (float)patch_size_y;\n \n cv::Size size_grid_1(patch_size_x, patch_size_y);\n cv::Mat grid_2d_grid_1 = cv::Mat::zeros(size_grid_1, CV_8UC1);\n cv::Mat mask_1_updated = mask_0.clone();\n\n it_0 = pts_1.begin();\n it_1 = ids_1.begin();\n\n while (it_0 != pts_1.end())\n {\n cv::KeyPoint kpt = *it_0;\n int x = (int)kpt.pt.x;\n int y = (int)kpt.pt.y;\n int edge = 10;\n if (x < edge || x >= img_1_pyr.at(0).cols - edge || y < edge || y >= img_1_pyr.at(0).rows - edge)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n \n int x_close = (int)(kpt.pt.x / (float)min_px_dist);\n int y_close = (int)(kpt.pt.y / (float)min_px_dist);\n if (x_close < 0 || x_close >= size_close_1.width || y_close < 0 || y_close >= size_close_1.height)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n\n int x_grid = std::floor(kpt.pt.x / size_x_1);\n int y_grid = std::floor(kpt.pt.y / size_y_1);\n if (x_grid < 0 || x_grid >= size_grid_1.width || y_grid < 0 || y_grid >= size_grid_1.height)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n \n bool is_stereo = (std::find(ids_0.begin(), ids_0.end(), *it_1) != ids_0.end());\n\n if (grid_2d_close_1.at(y_close, x_close) > 127 && !is_stereo)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n\n if (mask_1.at(y, x) > 127)\n {\n it_0 = pts_1.erase(it_0);\n it_1 = ids_1.erase(it_1);\n continue;\n }\n\n grid_2d_close_1.at(y_close, x_close) = 255;\n\n if (grid_2d_grid_1.at(y_grid, x_grid) < 255)\n {\n grid_2d_grid_1.at(y_grid, x_grid) += 1;\n }\n\n if (x - min_px_dist >= 0 && x + min_px_dist < img_1_pyr.at(0).cols && y - min_px_dist >= 0 && y + min_px_dist < img_1_pyr.at(0).rows)\n {\n cv::Point pt_1(x - min_px_dist, y - min_px_dist);\n cv::Point pt_2(x + min_px_dist, y + min_px_dist);\n cv::rectangle(mask_1_updated, pt_1, pt_2, cv::Scalar(255), -1);\n\n int begin_idx = x - min_px_dist + wG[0] * (y - min_px_dist);\n int end_idx = x + min_px_dist + wG[0] * (y + min_px_dist);\n\n for(int i = begin_idx; i <= end_idx; i++) fh->mask_right[i] = true;\n }\n\n it_0++;\n it_1++;\n }\n\n int num_featsneeded_1 = num_features - (int)pts_1.size();\n if (num_featsneeded_1 > std::min(20, (int)(min_feat_percent * num_features)))\n {\n pixelSelector_ptr->pixelSelectionRight(fh, num_featsneeded_1);\n\n for (int y = 0; y < hG[0]; y++)\n {\n for (int x = 0; x < wG[0]; x++)\n {\n int idx = x + y * wG[0];\n\n if (fh->selected_pixels_right[idx] == 0) continue;\n\n cv::KeyPoint kpt;\n kpt.pt.x = x;\n kpt.pt.y = y;\n\n pts_1.push_back(kpt);\n size_t temp = ++current_id;\n ids_1.push_back(temp);\n }\n }\n }\n}\n void performMatching(const std::vector &img_0_pyr, const std::vector &img_1_pyr, std::vector &pts_0, \n\t\tstd::vector &pts_1, size_t id_0, size_t id_1, std::vector &mask_out) {\n assert(kpts_0.size() == kpts_1.size());\n\n if (kpts_0.empty() || kpts_1.empty())\n return;\n\n std::vector pts_0, pts_1;\n for (size_t i = 0; i < kpts_0.size(); i++)\n {\n pts_0.push_back(kpts_0.at(i).pt);\n pts_1.push_back(kpts_1.at(i).pt);\n }\n\n if (pts_0.size() < 10)\n {\n for (size_t i = 0; i < pts_0.size(); i++)\n mask_out.push_back((uchar)0);\n\n return;\n }\n\n std::vector mask_klt;\n std::vector error;\n cv::TermCriteria term_crit = cv::TermCriteria(cv::TermCriteria::COUNT | cv::TermCriteria::EPS, 30, 0.01);\n cv::calcOpticalFlowPyrLK(img_0_pyr, img_1_pyr, pts_0, pts_1, mask_klt, error, win_size, pyr_levels, term_crit, cv::OPTFLOW_USE_INITIAL_FLOW);\n\n std::vector pts_0_n, pts_1_n;\n for (size_t i = 0; i < pts_0.size(); i++)\n {\n pts_0_n.push_back(camera_calib.at(id_0)->undistortCV(pts_0.at(i)));\n pts_1_n.push_back(camera_calib.at(id_1)->undistortCV(pts_1.at(i)));\n }\n\n std::vector mask_rsc;\n double max_focallength_img_0 = std::max(camera_calib.at(id_0)->getK()(0, 0), camera_calib.at(id_0)->getK()(1, 1));\n double max_focallength_img_1 = std::max(camera_calib.at(id_1)->getK()(0, 0), camera_calib.at(id_1)->getK()(1, 1));\n double max_focallength = std::max(max_focallength_img_0, max_focallength_img_1);\n cv::findFundamentalMat(pts_0_n, pts_1_n, cv::FM_RANSAC, 2.0 / max_focallength, 0.999, mask_rsc);\n\n for (size_t i = 0; i < mask_klt.size(); i++)\n {\n auto mask = (uchar)((i < mask_klt.size() && mask_klt[i] && i < mask_rsc.size() && mask_rsc[i]) ? 1 : 0);\n mask_out.push_back(mask);\n }\n\n for (size_t i = 0; i < pts_0.size(); i++)\n {\n kpts_0.at(i).pt = pts_0.at(i);\n kpts_1.at(i).pt = pts_1.at(i);\n }\n}\n std::unordered_map> camera_calib;\n std::shared_ptr database;\n int num_features;\n HistogramMethod histogram_method;\n std::map img_last;\n std::map img_mask_last;\n std::unordered_map> pts_last;\n std::unordered_map> ids_last;\n std::atomic current_id;\n std::shared_ptr pixelSelector_ptr;\n int threshold;\n int patch_size_x;\n int patch_size_y;\n int min_px_dist;\n int pyr_levels = 5;\n cv::Size win_size = cv::Size(15, 15);\n std::map> img_pyramid_last;\n std::map img_curr;\n std::map> img_pyramid_curr;\n boost::posix_time::ptime rT1, rT2, rT3, rT4, rT5, rT6, rT7;\n};"], ["/voxel_svio/include/initializer.h", "class staticInitializer {\n public:\n inertialInitializer(inertialInitializerOptions &initializer_options_, std::shared_ptr db_) {\n\timu_data = std::make_shared>();\n\n\tstatic_initializer = std::make_shared(initializer_options, db, imu_data);\n\tdynamic_initializer = std::make_shared(initializer_options, db, imu_data);\n}\n void feedImu(const imuData &imu_data_, double oldest_time = -1) {\n\timu_data->emplace_back(imu_data_);\n\n\tif (oldest_time != -1)\n\t{\n\t\tauto it0 = imu_data->begin();\n \n\t\twhile (it0 != imu_data->end())\n\t\t{\n\t\t\tif (it0->timestamp < oldest_time)\n\t\t\t\tit0 = imu_data->erase(it0);\n\t\t\telse\n\t\t\t\tit0++;\n\t\t}\n\t}\n}\n bool initialize(double ×tamp, Eigen::MatrixXd &covariance, std::vector> &order,\n std::shared_ptr imu_state_, bool wait_for_jerk = true) {\n\tauto rT1 = boost::posix_time::microsec_clock::local_time();\n\tdouble newest_cam_time = -1;\n\tfor (auto const &feat : db->getInternalData())\n\t{\n\t\tfor (auto const &cam_time_pair : feat.second->timestamps)\n\t\t{\n\t\t\tfor (auto const &time : cam_time_pair.second)\n\t\t\t{\n\t\t\t\tnewest_cam_time = std::max(newest_cam_time, time);\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble oldest_time = newest_cam_time - initializer_options.init_window_time;\n\n\tif (newest_cam_time < 0 || oldest_time < 0) return false;\n\n\tdb->cleanUpOldmeasurements(oldest_time);\n \n\tbool have_old_imu_readings = false;\n\tauto it_imu = imu_data->begin();\n\n\twhile (it_imu != imu_data->end() && it_imu->timestamp < oldest_time + initializer_options.calib_camimu_dt)\n\t{\n\t\thave_old_imu_readings = true;\n\t\tit_imu = imu_data->erase(it_imu);\n\t}\n\n\tif (db->getInternalData().size() < 0.75 * initializer_options.init_max_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Only \" << db->getInternalData().size() << \" valid features of required (\" \n\t\t\t<< 0.95 * initializer_options.init_max_features << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tif (imu_data->size() < 2 || !have_old_imu_readings)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Waiting for window to reach full size (\" << imu_data->size() << \" imu readings)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::unordered_map> features;\n\n\tfor (const auto &feat : db->getInternalData())\n\t{\n\t\tauto feat_new = std::make_shared();\n\t\tfeat_new->feature_id = feat.second->feature_id;\n\t\tfeat_new->uvs = feat.second->uvs;\n\t\tfeat_new->uvs_norm = feat.second->uvs_norm;\n\t\tfeat_new->timestamps = feat.second->timestamps;\n\t\tfeatures.insert({feat.first, feat_new});\n\t}\n\n\tconst int min_num_meas_to_optimize = (int)initializer_options.init_window_time;\n\tconst int min_valid_features = 8;\n\n\tbool have_stereo = false;\n\tint count_valid_features = 0;\n\tstd::map map_features_num_meas;\n\tint num_measurements = 0;\n\tdouble oldest_camera_time = INFINITY;\n\tstd::map map_camera_times;\n\tmap_camera_times[newest_cam_time] = true;\n\tstd::map map_camera_ids;\n\tdouble pose_dt_avg = initializer_options.init_window_time / (double)(initializer_options.init_dyn_num_pose + 1);\n\n\tfor (auto const &feat : features)\n\t{\n\t\tstd::vector times;\n\t\tstd::map cam_ids;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tfor (double time : cam_time.second)\n\t\t\t{\n\t\t\t\tdouble time_dt = INFINITY;\n\n\t\t\t\tfor (auto const &tmp : map_camera_times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp.first));\n\n\t\t\t\tfor (auto const &tmp : times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp));\n\n\t\t\t\tif (time_dt >= pose_dt_avg || time_dt == 0.0)\n\t\t\t\t{\n\t\t\t\t\ttimes.push_back(time);\n\t\t\t\t\tcam_ids[cam_time.first] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap_features_num_meas[feat.first] = (int)times.size();\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tfor (auto const &tmp : times)\n\t\t{\n\t\t\tmap_camera_times[tmp] = true;\n\t\t\toldest_camera_time = std::min(oldest_camera_time, tmp);\n\t\t\tnum_measurements += 2;\n\t\t}\n\n\t\tfor (auto const &tmp : cam_ids)\n\t\t\tmap_camera_ids[tmp.first] = true;\n\n\t\tif (cam_ids.size() > 1) have_stereo = true;\n\n\t\tcount_valid_features++;\n\t}\n\n\tif ((int)map_camera_times.size() < initializer_options.init_dyn_num_pose)\n\t\treturn false;\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Only \" << count_valid_features << \" valid features of required \" << min_valid_features << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Vector3d gyroscope_bias = initializer_options.init_dyn_bias_g;\n\tEigen::Vector3d accelerometer_bias = initializer_options.init_dyn_bias_a;\n\n\tdouble accel_inI_norm = 0.0;\n\tdouble theta_inI_norm = 0.0;\n\tdouble time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\tdouble time_1_in_imu = newest_cam_time + initializer_options.calib_camimu_dt;\n\tstd::vector readings = initializerHelper::selectImuReadings(*imu_data, time_0_in_imu, time_1_in_imu);\n\n\tassert(readings.size() > 2);\n\tfor (size_t k = 0; k < readings.size() - 1; k++)\n\t{\n\t\tauto imu_0 = readings.at(k);\n\t\tauto imu_1 = readings.at(k + 1);\n\t\tdouble dt = imu_1.timestamp - imu_0.timestamp;\n\t\tEigen::Vector3d gyr = 0.5 * (imu_0.gyr + imu_1.gyr) - gyroscope_bias;\n\t\tEigen::Vector3d acc = 0.5 * (imu_0.acc + imu_1.acc) - accelerometer_bias;\n\t\ttheta_inI_norm += ( -gyr * dt).norm();\n\t\taccel_inI_norm += acc.norm();\n\t}\n\n\taccel_inI_norm /= (double)(readings.size() - 1);\n\n\tif (180.0 / M_PI * theta_inI_norm < initializer_options.init_dyn_min_deg)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Gyroscope only \" << 180.0 / M_PI * theta_inI_norm << \" degree change (\" \n\t\t\t<< initializer_options.init_dyn_min_deg << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\tstd::cout << std::fixed << \"[initialize-dynamic]: |theta_I| = \" << 180.0 / M_PI * theta_inI_norm \n\t\t<< \" deg and |accel| = \" << accel_inI_norm << std::endl;\n\n \tauto rT2 = boost::posix_time::microsec_clock::local_time();\n\n\tconst bool use_single_depth = false;\n\tint size_feature = (use_single_depth) ? 1 : 3;\n\tint num_features = count_valid_features;\n\tint system_size = size_feature * num_features + 3 + 3;\n\n\tif (num_measurements < system_size)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Not enough feature measurements (\" << num_measurements << \" meas vs \" << system_size \n\t\t\t<< \" state size)\" << std::endl;\n\n\t\treturn false;\n\t}\n\n\tassert(oldest_camera_time < newest_cam_time);\n\tdouble last_camera_timestamp = 0.0;\n\tstd::map> map_camera_pre_integration_I0toIi, map_camera_pre_integration_IitoIi1; //map_camera_cpi_I0toIi, map_camera_cpi_IitoIi1;\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble current_time = time_pair.first;\n \n\t\tif (current_time == oldest_camera_time)\n\t\t{\n\t\t\tmap_camera_pre_integration_I0toIi.insert({current_time, nullptr});\n\t\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, nullptr});\n\t\t\tlast_camera_timestamp = current_time;\n\t\t\tcontinue;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_I0toIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_I0toIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, initializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_I0toIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_I0toIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_I0toIi1_time_0_in_imu, pre_integration_I0toIi1_time_1_in_imu);\n \n\t\tif (pre_integration_I0toIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) \n\t\t\t\t<< \" in has \" << pre_integration_I0toIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_dt_imu = pre_integration_I0toIi1_readings.at(pre_integration_I0toIi1_readings.size() - 1).timestamp - pre_integration_I0toIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_I0toIi1_dt_imu - (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_I0toIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_I0toIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_I0toIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_I0toIi1_readings.at(k + 1);\n\t\t\tpre_integration_I0toIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_time_0_in_imu = last_camera_timestamp + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_IitoIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_IitoIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, \n\t\t\tinitializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_IitoIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_IitoIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_IitoIi1_time_0_in_imu, pre_integration_IitoIi1_time_1_in_imu);\n\n\t\tif (pre_integration_IitoIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << \" in has \" \n\t\t\t\t<< pre_integration_IitoIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_dt_imu = pre_integration_IitoIi1_readings.at(pre_integration_IitoIi1_readings.size() - 1).timestamp - pre_integration_IitoIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_IitoIi1_dt_imu - (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_IitoIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_IitoIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_IitoIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_IitoIi1_readings.at(k + 1);\n\t\t\tpre_integration_IitoIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tmap_camera_pre_integration_I0toIi.insert({current_time, pre_integration_I0toIi1});\n\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, pre_integration_IitoIi1});\n\t\tlast_camera_timestamp = current_time;\n\t}\n\n\tEigen::MatrixXd A = Eigen::MatrixXd::Zero(num_measurements, system_size);\n\tEigen::VectorXd b = Eigen::VectorXd::Zero(num_measurements);\n\tstd::cout << \"[initialize-dynamic]: System of \" << num_measurements << \" measurement x \" << system_size << \" states created (\" << num_features \n\t\t<< \" features, \" << ((have_stereo) ? \"stereo\" : \"mono\") << \")\" << std::endl;\n\n\tint index_meas = 0;\n\tint idx_feat = 0;\n\tstd::map A_index_features;\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (A_index_features.find(feat.first) == A_index_features.end())\n\t\t{\n\t\t\tA_index_features.insert({feat.first, idx_feat});\n\t\t\tidx_feat += 1;\n\t\t}\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_norm;\n\t\t\t\tuv_norm << (double)feat.second->uvs_norm.at(cam_id).at(i)(0), (double)feat.second->uvs_norm.at(cam_id).at(i)(1);\n\n\t\t\t\tdouble dt = 0.0;\n\t\t\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\t\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\t\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t\t\t{\n\t\t\t\t\tdt = map_camera_pre_integration_I0toIi.at(time)->dt;\n\t\t\t\t\tR_I0toIk = map_camera_pre_integration_I0toIi.at(time)->R_tau_k;\n\t\t\t\t\talpha_I0toIk = map_camera_pre_integration_I0toIi.at(time)->alpha_tau;\n\t\t\t\t}\n\n\t\t\t\tEigen::MatrixXd H_proj = Eigen::MatrixXd::Zero(2, 3);\n\t\t\t\tH_proj << 1, 0, -uv_norm(0), 0, 1, -uv_norm(1);\n\t\t\t\tEigen::MatrixXd Y = H_proj * R_ItoC * R_I0toIk;\n\t\t\t\tEigen::MatrixXd H_i = Eigen::MatrixXd::Zero(2, system_size);\n\t\t\t\tEigen::MatrixXd b_i = Y * alpha_I0toIk - H_proj * p_IinC;\n\n\t\t\t\tif (size_feature == 1)\n\t\t\t\t{\n\t\t\t\t\tassert(false);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tH_i.block(0, size_feature * A_index_features.at(feat.first), 2, 3) = Y;\n\t\t\t\t}\n\n\t\t\t\tH_i.block(0, size_feature * num_features + 0, 2, 3) = -dt * Y;\n\t\t\t\tH_i.block(0, size_feature * num_features + 3, 2, 3) = 0.5 * dt * dt * Y;\n\n\t\t\t\tA.block(index_meas, 0, 2, A.cols()) = H_i;\n\t\t\t\tb.block(index_meas, 0, 2, 1) = b_i;\n\t\t\t\tindex_meas += 2;\n\t\t\t}\n\t\t}\n\t}\n\tauto rT3 = boost::posix_time::microsec_clock::local_time();\n\n\tEigen::MatrixXd A1 = A.block(0, 0, A.rows(), A.cols() - 3);\n\tEigen::MatrixXd A1A1_inv = (A1.transpose() * A1).llt().solve(Eigen::MatrixXd::Identity(A1.cols(), A1.cols()));\n\tEigen::MatrixXd A2 = A.block(0, A.cols() - 3, A.rows(), 3);\n\tEigen::MatrixXd Temp = A2.transpose() * (Eigen::MatrixXd::Identity(A1.rows(), A1.rows()) - A1 * A1A1_inv * A1.transpose());\n\tEigen::MatrixXd D = Temp * A2;\n\tEigen::MatrixXd d = Temp * b;\n\tEigen::Matrix coeff = initializerHelper::computeDongsiCoeff(D, d, initializer_options.gravity_mag);\n\n\tassert(coeff(0) == 1);\n\tEigen::Matrix companion_matrix = Eigen::Matrix::Zero(coeff.rows() - 1, coeff.rows() - 1);\n\tcompanion_matrix.diagonal(-1).setOnes();\n\tcompanion_matrix.col(companion_matrix.cols() - 1) = - coeff.reverse().head(coeff.rows() - 1);\n\tEigen::JacobiSVD> svd_0(companion_matrix);\n\tEigen::MatrixXd singular_values_0 = svd_0.singularValues();\n\tdouble cond_0 = singular_values_0(0) / singular_values_0(singular_values_0.rows() - 1);\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: CM cond = \" << cond_0 << \" | rank = \" << (int)svd_0.rank() << \" of \" << (int)companion_matrix.cols() \n\t\t<< \" (\" << svd_0.threshold() << \" thresh)\" << std::endl;\n\n\tif (svd_0.rank() != companion_matrix.rows())\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Eigenvalue decomposition not full rank\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::EigenSolver> solver(companion_matrix, false);\n \n\tif (solver.info() != Eigen::Success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: failed to compute the eigenvalue decomposition\" << std::endl;\n\t\treturn false;\n\t}\n\n\tbool lambda_found = false;\n\tdouble lambda_min = -1;\n\tdouble cost_min = INFINITY;\n\tEigen::MatrixXd I_dd = Eigen::MatrixXd::Identity(D.rows(), D.rows());\n\n\tfor (int i = 0; i < solver.eigenvalues().size(); i++) {\n\t\tauto val = solver.eigenvalues()(i);\n\t\tif (val.imag() == 0)\n\t\t{\n\t\t\tdouble lambda = val.real();\n\t\t\tEigen::MatrixXd D_lambdaI_inv = (D - lambda * I_dd).llt().solve(I_dd);\n\t\t\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\t\t\tdouble cost = std::abs(state_grav.norm() - initializer_options.gravity_mag);\n\n\t\t\tif (!lambda_found || cost < cost_min)\n\t\t\t{\n\t\t\t\tlambda_found = true;\n\t\t\t\tlambda_min = lambda;\n\t\t\t\tcost_min = cost;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!lambda_found)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Failed to find a real eigenvalue\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Smallest real eigenvalue = \" << lambda_min << \" (cost of \" \n\t\t<< cost_min << \")\" << std::endl;\n\n\tEigen::MatrixXd D_lambdaI_inv = (D - lambda_min * I_dd).llt().solve(I_dd);\n\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\n\tEigen::VectorXd state_feat_vel = - A1A1_inv * A1.transpose() * A2 * state_grav + A1A1_inv * A1.transpose() * b;\n\tEigen::MatrixXd x_hat = Eigen::MatrixXd::Zero(system_size, 1);\n\tx_hat.block(0, 0, size_feature * num_features + 3, 1) = state_feat_vel;\n\tx_hat.block(size_feature * num_features + 3, 0, 3, 1) = state_grav;\n\tEigen::Vector3d v_I0inI0 = x_hat.block(size_feature * num_features + 0, 0, 3, 1);\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Velocity in I0 was \" << v_I0inI0(0) << \", \" << v_I0inI0(1) << \", \" << v_I0inI0(2) \n\t\t<< \" and |v| = \" << v_I0inI0.norm() << std::endl;\n\n\tEigen::Vector3d gravity_inI0 = x_hat.block(size_feature * num_features + 3, 0, 3, 1);\n\tdouble init_max_grav_difference = 1e-3;\n\n\tif (std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) > init_max_grav_difference)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Gravity did not converge (\" << std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) \n\t\t\t<< \" > \" << init_max_grav_difference << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << \"[initialize-dynamic]: Gravity in I0 was \" << gravity_inI0(0) << \", \" << gravity_inI0(1) << \", \" << gravity_inI0(2) << \" and |g| = \" << gravity_inI0.norm() << std::endl;\n\tauto rT4 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::map ori_I0toIi, pos_IiinI0, vel_IiinI0;\n\t\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble time = time_pair.first;\n\n\t\tdouble dt = 0.0;\n\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\tEigen::MatrixXd beta_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\n\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t{\n\t\t\tauto pre_integration = map_camera_pre_integration_I0toIi.at(time);\n\t\t\tdt = pre_integration->dt;\n\t\t\tR_I0toIk = pre_integration->R_tau_k;\n\t\t\talpha_I0toIk = pre_integration->alpha_tau;\n\t\t\tbeta_I0toIk = pre_integration->beta_tau;\n\t\t}\n\n\t\tEigen::Vector3d p_IkinI0 = v_I0inI0 * dt - 0.5 * gravity_inI0 * dt * dt + alpha_I0toIk;\n\t\tEigen::Vector3d v_IkinI0 = v_I0inI0 - gravity_inI0 * dt + beta_I0toIk;\n\n\t\tori_I0toIi.insert({time, quatType::rotToQuat(R_I0toIk)});\n\t\tpos_IiinI0.insert({time, p_IkinI0});\n\t\tvel_IiinI0.insert({time, v_IkinI0});\n\t}\n\n\tcount_valid_features = 0;\n\tstd::map features_inI0;\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tEigen::Vector3d p_FinI0;\n\t\tif (size_feature == 1)\n\t\t\tassert(false);\n\t\telse\n\t\t\tp_FinI0 = x_hat.block(size_feature * A_index_features.at(feat.first), 0, 3, 1);\n\n\t\tbool is_behind = false;\n\t\t\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Vector3d p_FinC0 = quatType::quatToRot(q_ItoC) * p_FinI0 + p_IinC;\n\n\t\t\tif (p_FinC0(2) < 0) is_behind = true;\n\t\t}\n\t\t\n\t\tif (!is_behind)\n\t\t{\n\t\t\tfeatures_inI0.insert({feat.first, p_FinI0});\n\t\t\tcount_valid_features++;\n\t\t}\n\t}\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Not enough features for our mle (\" << count_valid_features << \" < \" \n\t\t\t<< min_valid_features << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Matrix3d R_GtoI0;\n\tinitializerHelper::gramSchmidt(gravity_inI0, R_GtoI0);\n\tEigen::Vector4d q_GtoI0 = quatType::rotToQuat(R_GtoI0);\n\tEigen::Vector3d gravity;\n\tgravity << 0.0, 0.0, initializer_options.gravity_mag;\n\tstd::map ori_GtoIi, pos_IiinG, vel_IiinG;\n\tstd::map features_inG;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tori_GtoIi[time_pair.first] = quatType::quatMultiply(ori_I0toIi.at(time_pair.first), q_GtoI0);\n\t\tpos_IiinG[time_pair.first] = R_GtoI0.transpose() * pos_IiinI0.at(time_pair.first);\n\t\tvel_IiinG[time_pair.first] = R_GtoI0.transpose() * vel_IiinI0.at(time_pair.first);\n\t}\n\n\tfor (auto const &feat : features_inI0)\n\tfeatures_inG[feat.first] = R_GtoI0.transpose() * feat.second;\n\n\tceres::Problem problem;\n\n\tstd::map map_states;\n\tstd::vector ceres_vars_ori;\n\tstd::vector ceres_vars_pos;\n\tstd::vector ceres_vars_vel;\n\tstd::vector ceres_vars_bias_g;\n\tstd::vector ceres_vars_bias_a;\n\n\tstd::map map_features;\n\tstd::vector ceres_vars_feat;\n\n\tstd::map map_calib_cam2imu;\n\tstd::vector ceres_vars_calib_cam2imu_ori;\n\tstd::vector ceres_vars_calib_cam2imu_pos;\n\n\tstd::map map_calib_cam;\n\tstd::vector ceres_vars_calib_cam_intrinsics;\n\n\tauto freeStateMemory = [&]()\n\t{\n\t\tfor (auto const &ptr : ceres_vars_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_vel)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_g)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_a)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_feat)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam_intrinsics)\n\t\t\tdelete[] ptr;\n\t};\n\n\tceres::Solver::Options options;\n\toptions.linear_solver_type = ceres::DENSE_SCHUR;\n\toptions.trust_region_strategy_type = ceres::DOGLEG;\n\toptions.num_threads = initializer_options.init_dyn_mle_max_threads;\n\toptions.max_solver_time_in_seconds = initializer_options.init_dyn_mle_max_time;\n\toptions.max_num_iterations = initializer_options.init_dyn_mle_max_iter;\n\toptions.function_tolerance = 1e-5;\n\toptions.gradient_tolerance = 1e-4 * options.function_tolerance;\n\n\tdouble timestamp_k = -1;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble timestamp_k1 = time_pair.first;\n\t\tstd::shared_ptr pre_integration = map_camera_pre_integration_IitoIi1.at(timestamp_k1);\n\t\tEigen::Matrix state_k1;\n\t\tstate_k1.block(0, 0, 4, 1) = ori_GtoIi.at(timestamp_k1);\n\t\tstate_k1.block(4, 0, 3, 1) = pos_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(7, 0, 3, 1) = vel_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(10, 0, 3, 1) = gyroscope_bias;\n\t\tstate_k1.block(13, 0, 3, 1) = accelerometer_bias;\n\n\t\tauto *var_ori = new double[4];\n\t\tfor (int j = 0; j < 4; j++)\n\t\t\tvar_ori[j] = state_k1(0 + j, 0);\n\n\t\tauto *var_pos = new double[3];\n\t\tauto *var_vel = new double[3];\n\t\tauto *var_bias_g = new double[3];\n\t\tauto *var_bias_a = new double[3];\n\n\t\tfor (int j = 0; j < 3; j++)\n\t\t{\n\t\t\tvar_pos[j] = state_k1(4 + j, 0);\n\t\t\tvar_vel[j] = state_k1(7 + j, 0);\n\t\t\tvar_bias_g[j] = state_k1(10 + j, 0);\n\t\t\tvar_bias_a[j] = state_k1(13 + j, 0);\n\t\t}\n\n\t\tauto ceres_jplquat = new stateJplQuatLocal();\n\t\tproblem.AddParameterBlock(var_ori, 4, ceres_jplquat);\n\t\tproblem.AddParameterBlock(var_pos, 3);\n\t\tproblem.AddParameterBlock(var_vel, 3);\n\t\tproblem.AddParameterBlock(var_bias_g, 3);\n\t\tproblem.AddParameterBlock(var_bias_a, 3);\n\n\t\tif (map_states.empty())\n\t\t{\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(13, 1);\n\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t{\n\t\t\t\tx_lin(4 + j) = var_pos[j];\n\t\t\t\tx_lin(7 + j) = var_bias_g[j];\n\t\t\t\tx_lin(10 + j) = var_bias_a[j];\n\t\t\t}\n\t\t\t\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(10, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(10, 10);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1e-5, 2);\n\t\t\tprior_Info.block(4, 4, 3, 3) *= 1.0 / std::pow(0.05, 2);\n\t\t\tprior_Info.block(7, 7, 3, 3) *= 1.0 / std::pow(0.10, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_ori);\n\t\t\tx_types.emplace_back(\"quat_yaw\");\n\t\t\tfactor_params.push_back(var_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_g);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_a);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\t\t}\n\n\t\tmap_states.insert({timestamp_k1, (int)ceres_vars_ori.size()});\n\t\tceres_vars_ori.push_back(var_ori);\n\t\tceres_vars_pos.push_back(var_pos);\n\t\tceres_vars_vel.push_back(var_vel);\n\t\tceres_vars_bias_g.push_back(var_bias_g);\n\t\tceres_vars_bias_a.push_back(var_bias_a);\n\n\t\tif (pre_integration != nullptr)\n\t\t{\n\t\t\tassert(timestamp_k != -1);\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k1)));\n\t\t\tauto *factor_imu = new factorImuPreIntegrationV1(pre_integration->dt, gravity, pre_integration->alpha_tau, pre_integration->beta_tau, \n\t\t\t\tpre_integration->q_tau_k, pre_integration->b_a_lin, pre_integration->b_w_lin, pre_integration->J_q, pre_integration->J_b, \n\t\t\t\tpre_integration->J_a, pre_integration->H_b, pre_integration->H_a, pre_integration->P_meas);\n\t\t\t\n\t\t\tproblem.AddResidualBlock(factor_imu, nullptr, factor_params);\n\t\t}\n\n\t\ttimestamp_k = timestamp_k1;\n\t}\n\n\tfor (auto const &id_pair : map_camera_ids)\n\t{\n\t\tsize_t cam_id = id_pair.first;\n\t\tif (map_calib_cam2imu.find(cam_id) == map_calib_cam2imu.end())\n\t\t{\n\t\t\tauto *var_calib_ori = new double[4];\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tvar_calib_ori[j] = initializer_options.camera_extrinsics.at(cam_id)(0 + j, 0);\n\n\t\t\tauto *var_calib_pos = new double[3];\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tvar_calib_pos[j] = initializer_options.camera_extrinsics.at(cam_id)(4 + j, 0);\n\n\t\t\tauto ceres_calib_jplquat = new stateJplQuatLocal();\n\t\t\tproblem.AddParameterBlock(var_calib_ori, 4, ceres_calib_jplquat);\n\t\t\tproblem.AddParameterBlock(var_calib_pos, 3);\n\n\t\t\tmap_calib_cam2imu.insert({cam_id, (int)ceres_vars_calib_cam2imu_ori.size()});\n\t\t\tceres_vars_calib_cam2imu_ori.push_back(var_calib_ori);\n\t\t\tceres_vars_calib_cam2imu_pos.push_back(var_calib_pos);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(7, 1);\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tx_lin(4 + j) = var_calib_pos[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(6, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(6, 6);\n\t\t\tprior_Info.block(0, 0, 3, 3) *= 1.0 / std::pow(0.001, 2);\n\t\t\tprior_Info.block(3, 3, 3, 3) *= 1.0 / std::pow(0.01, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_ori);\n\t\t\tx_types.emplace_back(\"quat\");\n\t\t\tfactor_params.push_back(var_calib_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t{\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_ori);\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_pos);\n\t\t\t}\n\t\t}\n\n\t\tif (map_calib_cam.find(cam_id) == map_calib_cam.end())\n\t\t{\n\t\t\tauto *var_calib_cam = new double[8];\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tvar_calib_cam[j] = initializer_options.camera_intrinsics.at(cam_id)->getValue()(j, 0);\n\n\t\t\tproblem.AddParameterBlock(var_calib_cam, 8);\n\t\t\tmap_calib_cam.insert({cam_id, (int)ceres_vars_calib_cam_intrinsics.size()});\n\t\t\tceres_vars_calib_cam_intrinsics.push_back(var_calib_cam);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_cam[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(8, 8);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1.0, 2);\n\t\t\tprior_Info.block(4, 4, 4, 4) *= 1.0 / std::pow(0.005, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_cam);\n\t\t\tx_types.emplace_back(\"vec8\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_cam);\n\t\t}\n\t}\n\tassert(map_calib_cam2imu.size() == map_calib_cam.size());\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (features_inG.find(feat.first) == features_inG.end()) continue;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t feat_id = feat.first;\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tbool is_fisheye = (std::dynamic_pointer_cast(initializer_options.camera_intrinsics.at(cam_id)) != nullptr);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_raw = feat.second->uvs.at(cam_id).at(i).block(0, 0, 2, 1).cast();\n\n\t\t\t\tif (map_features.find(feat_id) == map_features.end())\n\t\t\t\t{\n\t\t\t\t\tauto *var_feat = new double[3];\n\t\t\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\t\t\tvar_feat[j] = features_inG.at(feat_id)(j);\n\n\t\t\t\t\tproblem.AddParameterBlock(var_feat, 3);\n\t\t\t\t\tmap_features.insert({feat_id, (int)ceres_vars_feat.size()});\n\t\t\t\t\tceres_vars_feat.push_back(var_feat);\n\t\t\t\t}\n\n\t\t\t\tstd::vector factor_params;\n\t\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_feat.at(map_features.at(feat_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_ori.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_pos.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam_intrinsics.at(map_calib_cam.at(cam_id)));\n\t\t\t\tauto *factor_pinhole = new factorImageReprojCalib(uv_raw, initializer_options.sigma_pix, is_fisheye);\n\n\t\t\t\tceres::LossFunction *loss_function = new ceres::CauchyLoss(1.0);\n\t\t\t\tproblem.AddResidualBlock(factor_pinhole, loss_function, factor_params);\n\t\t\t}\n\t\t}\n\t}\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_g.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_vel.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_a.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_pos.size());\n\tauto rT5 = boost::posix_time::microsec_clock::local_time();\n\n\tceres::Solver::Summary summary;\n\tceres::Solve(options, &problem, &summary);\n\n\tstd::cout << \"[initialize-dynamic]: \" << (int)summary.iterations.size() << \" iterations | \" << map_states.size() << \" states, \" \n\t\t<< map_features.size() << \" feats (\" << count_valid_features << \" valid) | \" << summary.num_parameters << \" param and \"\n\t\t<< summary.num_residuals << \" res | cost \" << std::fixed << summary.initial_cost << \" => \" << summary.final_cost << std::endl;\n\n\tauto rT6 = boost::posix_time::microsec_clock::local_time();\n\n\ttimestamp = newest_cam_time;\n\tif (initializer_options.init_dyn_mle_max_iter != 0 && summary.termination_type != ceres::CONVERGENCE)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Optimization failed: \" << summary.message << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\tstd::cout << \"[initialize-dynamic]: \" << summary.message << std::endl;\n\n\tauto getPose = [&](double timestamp)\n\t{\n\t\tEigen::VectorXd state_imu = Eigen::VectorXd::Zero(16);\n\n\t\tfor (int i = 0; i < 4; i++)\n\t\t\tstate_imu(0 + i) = ceres_vars_ori[map_states[timestamp]][i];\n\n\t\tfor (int i = 0; i < 3; i++)\n\t\t{\n\t\t\tstate_imu(4 + i) = ceres_vars_pos[map_states[timestamp]][i];\n\t\t\tstate_imu(7 + i) = ceres_vars_vel[map_states[timestamp]][i];\n\t\t\tstate_imu(10 + i) = ceres_vars_bias_g[map_states[timestamp]][i];\n\t\t\tstate_imu(13 + i) = ceres_vars_bias_a[map_states[timestamp]][i];\n\t\t}\n\n\t\treturn state_imu;\n\t};\n\n\tassert(map_states.find(newest_cam_time) != map_states.end());\n\t\n\tif (imu_state_ == nullptr)\n\t\timu_state_ = std::make_shared();\n\n\tEigen::VectorXd imu_state = getPose(newest_cam_time);\n\timu_state_->setValue(imu_state);\n\timu_state_->setFej(imu_state);\n\n\tfor (auto const &state_pair : map_states)\n\t{\n\t\tEigen::VectorXd pose = getPose(state_pair.first);\n\n\t\tif (clones_imu_.find(state_pair.first) == clones_imu_.end())\n\t\t{\n\t\t\tauto pose_ = std::make_shared();\n\t\t\tpose_->setValue(pose.block(0, 0, 7, 1));\n\t\t\tpose_->setFej(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.insert({state_pair.first, pose_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tclones_imu_.at(state_pair.first)->setValue(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.at(state_pair.first)->setFej(pose.block(0, 0, 7, 1));\n\t\t}\n\t}\n\n\tfor (auto const &feat_pair : map_features)\n\t{\n\t\tEigen::Vector3d feature;\n\t\tfeature << ceres_vars_feat[feat_pair.second][0], ceres_vars_feat[feat_pair.second][1], ceres_vars_feat[feat_pair.second][2];\n\n\t\tif (map_points_.find(feat_pair.first) == map_points_.end())\n\t\t{\n\t\t\tauto feature_ = std::make_shared();\n\t\t\tfeature_->feature_id = feat_pair.first;\n\t\t\tfeature_->setPointXYZ(feature, false);\n\t\t\tfeature_->setPointXYZ(feature, true);\n\t\t\tmap_points_.insert({feat_pair.first, feature_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmap_points_.at(feat_pair.first)->feature_id = feat_pair.first;\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, false);\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, true);\n\t\t}\n\t}\n\n\tstd::vector> covariance_blocks;\n\tint state_index = map_states[newest_cam_time];\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_ori[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index]));\n\n\tceres::Covariance::Options options_cov;\n\toptions_cov.null_space_rank = (!initializer_options.init_dyn_mle_opt_calib) * ((int)map_calib_cam2imu.size() * (6 + 8));\n\toptions_cov.min_reciprocal_condition_number = initializer_options.init_dyn_min_rec_cond;\n \n\toptions_cov.apply_loss_function = true;\n\toptions_cov.num_threads = initializer_options.init_dyn_mle_max_threads;\n\n\tceres::Covariance problem_cov(options_cov);\n\tbool success = problem_cov.Compute(covariance_blocks, &problem);\n\n\tif (!success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Covariance recovery failed...\" << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\n\torder.clear();\n\torder.push_back(imu_state_);\n\tcovariance = Eigen::MatrixXd::Zero(imu_state_->getSize(), imu_state_->getSize());\n\tEigen::Matrix cov_tmp = Eigen::Matrix::Zero();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_ori[state_index], cov_tmp.data()));\n\tcovariance.block(0, 0, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(3, 3, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(6, 6, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(9, 9, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(12, 12, 3, 3) = cov_tmp.eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(0, 3, 3, 3) = cov_tmp.eval();\n\tcovariance.block(3, 0, 3, 3) = cov_tmp.transpose();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(0, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(0, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(0, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 0, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(3, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(3, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(3, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 3, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(6, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 6, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(6, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 6, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(9, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 9, 3, 3) = cov_tmp.transpose().eval();\n\n\tcovariance.block(0, 0, 3, 3) *= initializer_options.init_dyn_inflation_orientation;\n\tcovariance.block(6, 6, 3, 3) *= initializer_options.init_dyn_inflation_velocity;\n\tcovariance.block(9, 9, 3, 3) *= initializer_options.init_dyn_inflation_bias_gyro;\n\tcovariance.block(12, 12, 3, 3) *= initializer_options.init_dyn_inflation_bias_accel;\n\n\tcovariance = 0.5 * (covariance + covariance.transpose());\n\tEigen::Vector3d sigmas_vel = covariance.block(6, 6, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_bg = covariance.block(9, 9, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_ba = covariance.block(12, 12, 3, 3).diagonal().transpose().cwiseSqrt();\n\tstd::cout << \"[initialize-dynamic]: vel priors = \" << sigmas_vel(0) << \", \" << sigmas_vel(1) << \", \" << sigmas_vel(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: bg priors = \" << sigmas_bg(0) << \", \" << sigmas_bg(1) << \", \" << sigmas_bg(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: ba priors = \" << sigmas_ba(0) << \", \" << sigmas_ba(1) << \", \" << sigmas_ba(2) << std::endl;\n\n\tEigen::MatrixXd x = imu_state_->value();\n\tx.block(4, 0, 3, 1).setZero();\n\timu_state_->setValue(x);\n\timu_state_->setFej(x);\n\n\tauto rT7 = boost::posix_time::microsec_clock::local_time();\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" sec for prelim tests\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" sec for linsys setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" sec for linsys\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" sec for ceres opt setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" sec for ceres opt\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT6).total_microseconds() * 1e-6 << \" sec for ceres covariance\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT1).total_microseconds() * 1e-6 << \" sec total for initialization\" << std::endl;\n\tfreeStateMemory();\n\n\treturn true;\n}\n protected:\n inertialInitializerOptions initializer_options;\n std::shared_ptr db;\n std::shared_ptr> imu_data;\n std::shared_ptr static_initializer;\n std::shared_ptr dynamic_initializer;\n public:\n explicit staticInitializer(inertialInitializerOptions &initializer_options_, std::shared_ptr db_,\n std::shared_ptr> imu_data_) {\n\n}\n bool initialize(double ×tamp, Eigen::MatrixXd &covariance, std::vector> &order, \n std::shared_ptr imu_state_, bool wait_for_jerk = true) {\n\tauto rT1 = boost::posix_time::microsec_clock::local_time();\n\tdouble newest_cam_time = -1;\n\tfor (auto const &feat : db->getInternalData())\n\t{\n\t\tfor (auto const &cam_time_pair : feat.second->timestamps)\n\t\t{\n\t\t\tfor (auto const &time : cam_time_pair.second)\n\t\t\t{\n\t\t\t\tnewest_cam_time = std::max(newest_cam_time, time);\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble oldest_time = newest_cam_time - initializer_options.init_window_time;\n\n\tif (newest_cam_time < 0 || oldest_time < 0) return false;\n\n\tdb->cleanUpOldmeasurements(oldest_time);\n \n\tbool have_old_imu_readings = false;\n\tauto it_imu = imu_data->begin();\n\n\twhile (it_imu != imu_data->end() && it_imu->timestamp < oldest_time + initializer_options.calib_camimu_dt)\n\t{\n\t\thave_old_imu_readings = true;\n\t\tit_imu = imu_data->erase(it_imu);\n\t}\n\n\tif (db->getInternalData().size() < 0.75 * initializer_options.init_max_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Only \" << db->getInternalData().size() << \" valid features of required (\" \n\t\t\t<< 0.95 * initializer_options.init_max_features << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tif (imu_data->size() < 2 || !have_old_imu_readings)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Waiting for window to reach full size (\" << imu_data->size() << \" imu readings)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::unordered_map> features;\n\n\tfor (const auto &feat : db->getInternalData())\n\t{\n\t\tauto feat_new = std::make_shared();\n\t\tfeat_new->feature_id = feat.second->feature_id;\n\t\tfeat_new->uvs = feat.second->uvs;\n\t\tfeat_new->uvs_norm = feat.second->uvs_norm;\n\t\tfeat_new->timestamps = feat.second->timestamps;\n\t\tfeatures.insert({feat.first, feat_new});\n\t}\n\n\tconst int min_num_meas_to_optimize = (int)initializer_options.init_window_time;\n\tconst int min_valid_features = 8;\n\n\tbool have_stereo = false;\n\tint count_valid_features = 0;\n\tstd::map map_features_num_meas;\n\tint num_measurements = 0;\n\tdouble oldest_camera_time = INFINITY;\n\tstd::map map_camera_times;\n\tmap_camera_times[newest_cam_time] = true;\n\tstd::map map_camera_ids;\n\tdouble pose_dt_avg = initializer_options.init_window_time / (double)(initializer_options.init_dyn_num_pose + 1);\n\n\tfor (auto const &feat : features)\n\t{\n\t\tstd::vector times;\n\t\tstd::map cam_ids;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tfor (double time : cam_time.second)\n\t\t\t{\n\t\t\t\tdouble time_dt = INFINITY;\n\n\t\t\t\tfor (auto const &tmp : map_camera_times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp.first));\n\n\t\t\t\tfor (auto const &tmp : times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp));\n\n\t\t\t\tif (time_dt >= pose_dt_avg || time_dt == 0.0)\n\t\t\t\t{\n\t\t\t\t\ttimes.push_back(time);\n\t\t\t\t\tcam_ids[cam_time.first] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap_features_num_meas[feat.first] = (int)times.size();\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tfor (auto const &tmp : times)\n\t\t{\n\t\t\tmap_camera_times[tmp] = true;\n\t\t\toldest_camera_time = std::min(oldest_camera_time, tmp);\n\t\t\tnum_measurements += 2;\n\t\t}\n\n\t\tfor (auto const &tmp : cam_ids)\n\t\t\tmap_camera_ids[tmp.first] = true;\n\n\t\tif (cam_ids.size() > 1) have_stereo = true;\n\n\t\tcount_valid_features++;\n\t}\n\n\tif ((int)map_camera_times.size() < initializer_options.init_dyn_num_pose)\n\t\treturn false;\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Only \" << count_valid_features << \" valid features of required \" << min_valid_features << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Vector3d gyroscope_bias = initializer_options.init_dyn_bias_g;\n\tEigen::Vector3d accelerometer_bias = initializer_options.init_dyn_bias_a;\n\n\tdouble accel_inI_norm = 0.0;\n\tdouble theta_inI_norm = 0.0;\n\tdouble time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\tdouble time_1_in_imu = newest_cam_time + initializer_options.calib_camimu_dt;\n\tstd::vector readings = initializerHelper::selectImuReadings(*imu_data, time_0_in_imu, time_1_in_imu);\n\n\tassert(readings.size() > 2);\n\tfor (size_t k = 0; k < readings.size() - 1; k++)\n\t{\n\t\tauto imu_0 = readings.at(k);\n\t\tauto imu_1 = readings.at(k + 1);\n\t\tdouble dt = imu_1.timestamp - imu_0.timestamp;\n\t\tEigen::Vector3d gyr = 0.5 * (imu_0.gyr + imu_1.gyr) - gyroscope_bias;\n\t\tEigen::Vector3d acc = 0.5 * (imu_0.acc + imu_1.acc) - accelerometer_bias;\n\t\ttheta_inI_norm += ( -gyr * dt).norm();\n\t\taccel_inI_norm += acc.norm();\n\t}\n\n\taccel_inI_norm /= (double)(readings.size() - 1);\n\n\tif (180.0 / M_PI * theta_inI_norm < initializer_options.init_dyn_min_deg)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Gyroscope only \" << 180.0 / M_PI * theta_inI_norm << \" degree change (\" \n\t\t\t<< initializer_options.init_dyn_min_deg << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\tstd::cout << std::fixed << \"[initialize-dynamic]: |theta_I| = \" << 180.0 / M_PI * theta_inI_norm \n\t\t<< \" deg and |accel| = \" << accel_inI_norm << std::endl;\n\n \tauto rT2 = boost::posix_time::microsec_clock::local_time();\n\n\tconst bool use_single_depth = false;\n\tint size_feature = (use_single_depth) ? 1 : 3;\n\tint num_features = count_valid_features;\n\tint system_size = size_feature * num_features + 3 + 3;\n\n\tif (num_measurements < system_size)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Not enough feature measurements (\" << num_measurements << \" meas vs \" << system_size \n\t\t\t<< \" state size)\" << std::endl;\n\n\t\treturn false;\n\t}\n\n\tassert(oldest_camera_time < newest_cam_time);\n\tdouble last_camera_timestamp = 0.0;\n\tstd::map> map_camera_pre_integration_I0toIi, map_camera_pre_integration_IitoIi1; //map_camera_cpi_I0toIi, map_camera_cpi_IitoIi1;\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble current_time = time_pair.first;\n \n\t\tif (current_time == oldest_camera_time)\n\t\t{\n\t\t\tmap_camera_pre_integration_I0toIi.insert({current_time, nullptr});\n\t\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, nullptr});\n\t\t\tlast_camera_timestamp = current_time;\n\t\t\tcontinue;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_I0toIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_I0toIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, initializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_I0toIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_I0toIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_I0toIi1_time_0_in_imu, pre_integration_I0toIi1_time_1_in_imu);\n \n\t\tif (pre_integration_I0toIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) \n\t\t\t\t<< \" in has \" << pre_integration_I0toIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_dt_imu = pre_integration_I0toIi1_readings.at(pre_integration_I0toIi1_readings.size() - 1).timestamp - pre_integration_I0toIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_I0toIi1_dt_imu - (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_I0toIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_I0toIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_I0toIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_I0toIi1_readings.at(k + 1);\n\t\t\tpre_integration_I0toIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_time_0_in_imu = last_camera_timestamp + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_IitoIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_IitoIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, \n\t\t\tinitializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_IitoIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_IitoIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_IitoIi1_time_0_in_imu, pre_integration_IitoIi1_time_1_in_imu);\n\n\t\tif (pre_integration_IitoIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << \" in has \" \n\t\t\t\t<< pre_integration_IitoIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_dt_imu = pre_integration_IitoIi1_readings.at(pre_integration_IitoIi1_readings.size() - 1).timestamp - pre_integration_IitoIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_IitoIi1_dt_imu - (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_IitoIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_IitoIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_IitoIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_IitoIi1_readings.at(k + 1);\n\t\t\tpre_integration_IitoIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tmap_camera_pre_integration_I0toIi.insert({current_time, pre_integration_I0toIi1});\n\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, pre_integration_IitoIi1});\n\t\tlast_camera_timestamp = current_time;\n\t}\n\n\tEigen::MatrixXd A = Eigen::MatrixXd::Zero(num_measurements, system_size);\n\tEigen::VectorXd b = Eigen::VectorXd::Zero(num_measurements);\n\tstd::cout << \"[initialize-dynamic]: System of \" << num_measurements << \" measurement x \" << system_size << \" states created (\" << num_features \n\t\t<< \" features, \" << ((have_stereo) ? \"stereo\" : \"mono\") << \")\" << std::endl;\n\n\tint index_meas = 0;\n\tint idx_feat = 0;\n\tstd::map A_index_features;\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (A_index_features.find(feat.first) == A_index_features.end())\n\t\t{\n\t\t\tA_index_features.insert({feat.first, idx_feat});\n\t\t\tidx_feat += 1;\n\t\t}\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_norm;\n\t\t\t\tuv_norm << (double)feat.second->uvs_norm.at(cam_id).at(i)(0), (double)feat.second->uvs_norm.at(cam_id).at(i)(1);\n\n\t\t\t\tdouble dt = 0.0;\n\t\t\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\t\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\t\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t\t\t{\n\t\t\t\t\tdt = map_camera_pre_integration_I0toIi.at(time)->dt;\n\t\t\t\t\tR_I0toIk = map_camera_pre_integration_I0toIi.at(time)->R_tau_k;\n\t\t\t\t\talpha_I0toIk = map_camera_pre_integration_I0toIi.at(time)->alpha_tau;\n\t\t\t\t}\n\n\t\t\t\tEigen::MatrixXd H_proj = Eigen::MatrixXd::Zero(2, 3);\n\t\t\t\tH_proj << 1, 0, -uv_norm(0), 0, 1, -uv_norm(1);\n\t\t\t\tEigen::MatrixXd Y = H_proj * R_ItoC * R_I0toIk;\n\t\t\t\tEigen::MatrixXd H_i = Eigen::MatrixXd::Zero(2, system_size);\n\t\t\t\tEigen::MatrixXd b_i = Y * alpha_I0toIk - H_proj * p_IinC;\n\n\t\t\t\tif (size_feature == 1)\n\t\t\t\t{\n\t\t\t\t\tassert(false);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tH_i.block(0, size_feature * A_index_features.at(feat.first), 2, 3) = Y;\n\t\t\t\t}\n\n\t\t\t\tH_i.block(0, size_feature * num_features + 0, 2, 3) = -dt * Y;\n\t\t\t\tH_i.block(0, size_feature * num_features + 3, 2, 3) = 0.5 * dt * dt * Y;\n\n\t\t\t\tA.block(index_meas, 0, 2, A.cols()) = H_i;\n\t\t\t\tb.block(index_meas, 0, 2, 1) = b_i;\n\t\t\t\tindex_meas += 2;\n\t\t\t}\n\t\t}\n\t}\n\tauto rT3 = boost::posix_time::microsec_clock::local_time();\n\n\tEigen::MatrixXd A1 = A.block(0, 0, A.rows(), A.cols() - 3);\n\tEigen::MatrixXd A1A1_inv = (A1.transpose() * A1).llt().solve(Eigen::MatrixXd::Identity(A1.cols(), A1.cols()));\n\tEigen::MatrixXd A2 = A.block(0, A.cols() - 3, A.rows(), 3);\n\tEigen::MatrixXd Temp = A2.transpose() * (Eigen::MatrixXd::Identity(A1.rows(), A1.rows()) - A1 * A1A1_inv * A1.transpose());\n\tEigen::MatrixXd D = Temp * A2;\n\tEigen::MatrixXd d = Temp * b;\n\tEigen::Matrix coeff = initializerHelper::computeDongsiCoeff(D, d, initializer_options.gravity_mag);\n\n\tassert(coeff(0) == 1);\n\tEigen::Matrix companion_matrix = Eigen::Matrix::Zero(coeff.rows() - 1, coeff.rows() - 1);\n\tcompanion_matrix.diagonal(-1).setOnes();\n\tcompanion_matrix.col(companion_matrix.cols() - 1) = - coeff.reverse().head(coeff.rows() - 1);\n\tEigen::JacobiSVD> svd_0(companion_matrix);\n\tEigen::MatrixXd singular_values_0 = svd_0.singularValues();\n\tdouble cond_0 = singular_values_0(0) / singular_values_0(singular_values_0.rows() - 1);\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: CM cond = \" << cond_0 << \" | rank = \" << (int)svd_0.rank() << \" of \" << (int)companion_matrix.cols() \n\t\t<< \" (\" << svd_0.threshold() << \" thresh)\" << std::endl;\n\n\tif (svd_0.rank() != companion_matrix.rows())\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Eigenvalue decomposition not full rank\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::EigenSolver> solver(companion_matrix, false);\n \n\tif (solver.info() != Eigen::Success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: failed to compute the eigenvalue decomposition\" << std::endl;\n\t\treturn false;\n\t}\n\n\tbool lambda_found = false;\n\tdouble lambda_min = -1;\n\tdouble cost_min = INFINITY;\n\tEigen::MatrixXd I_dd = Eigen::MatrixXd::Identity(D.rows(), D.rows());\n\n\tfor (int i = 0; i < solver.eigenvalues().size(); i++) {\n\t\tauto val = solver.eigenvalues()(i);\n\t\tif (val.imag() == 0)\n\t\t{\n\t\t\tdouble lambda = val.real();\n\t\t\tEigen::MatrixXd D_lambdaI_inv = (D - lambda * I_dd).llt().solve(I_dd);\n\t\t\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\t\t\tdouble cost = std::abs(state_grav.norm() - initializer_options.gravity_mag);\n\n\t\t\tif (!lambda_found || cost < cost_min)\n\t\t\t{\n\t\t\t\tlambda_found = true;\n\t\t\t\tlambda_min = lambda;\n\t\t\t\tcost_min = cost;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!lambda_found)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Failed to find a real eigenvalue\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Smallest real eigenvalue = \" << lambda_min << \" (cost of \" \n\t\t<< cost_min << \")\" << std::endl;\n\n\tEigen::MatrixXd D_lambdaI_inv = (D - lambda_min * I_dd).llt().solve(I_dd);\n\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\n\tEigen::VectorXd state_feat_vel = - A1A1_inv * A1.transpose() * A2 * state_grav + A1A1_inv * A1.transpose() * b;\n\tEigen::MatrixXd x_hat = Eigen::MatrixXd::Zero(system_size, 1);\n\tx_hat.block(0, 0, size_feature * num_features + 3, 1) = state_feat_vel;\n\tx_hat.block(size_feature * num_features + 3, 0, 3, 1) = state_grav;\n\tEigen::Vector3d v_I0inI0 = x_hat.block(size_feature * num_features + 0, 0, 3, 1);\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Velocity in I0 was \" << v_I0inI0(0) << \", \" << v_I0inI0(1) << \", \" << v_I0inI0(2) \n\t\t<< \" and |v| = \" << v_I0inI0.norm() << std::endl;\n\n\tEigen::Vector3d gravity_inI0 = x_hat.block(size_feature * num_features + 3, 0, 3, 1);\n\tdouble init_max_grav_difference = 1e-3;\n\n\tif (std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) > init_max_grav_difference)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Gravity did not converge (\" << std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) \n\t\t\t<< \" > \" << init_max_grav_difference << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << \"[initialize-dynamic]: Gravity in I0 was \" << gravity_inI0(0) << \", \" << gravity_inI0(1) << \", \" << gravity_inI0(2) << \" and |g| = \" << gravity_inI0.norm() << std::endl;\n\tauto rT4 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::map ori_I0toIi, pos_IiinI0, vel_IiinI0;\n\t\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble time = time_pair.first;\n\n\t\tdouble dt = 0.0;\n\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\tEigen::MatrixXd beta_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\n\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t{\n\t\t\tauto pre_integration = map_camera_pre_integration_I0toIi.at(time);\n\t\t\tdt = pre_integration->dt;\n\t\t\tR_I0toIk = pre_integration->R_tau_k;\n\t\t\talpha_I0toIk = pre_integration->alpha_tau;\n\t\t\tbeta_I0toIk = pre_integration->beta_tau;\n\t\t}\n\n\t\tEigen::Vector3d p_IkinI0 = v_I0inI0 * dt - 0.5 * gravity_inI0 * dt * dt + alpha_I0toIk;\n\t\tEigen::Vector3d v_IkinI0 = v_I0inI0 - gravity_inI0 * dt + beta_I0toIk;\n\n\t\tori_I0toIi.insert({time, quatType::rotToQuat(R_I0toIk)});\n\t\tpos_IiinI0.insert({time, p_IkinI0});\n\t\tvel_IiinI0.insert({time, v_IkinI0});\n\t}\n\n\tcount_valid_features = 0;\n\tstd::map features_inI0;\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tEigen::Vector3d p_FinI0;\n\t\tif (size_feature == 1)\n\t\t\tassert(false);\n\t\telse\n\t\t\tp_FinI0 = x_hat.block(size_feature * A_index_features.at(feat.first), 0, 3, 1);\n\n\t\tbool is_behind = false;\n\t\t\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Vector3d p_FinC0 = quatType::quatToRot(q_ItoC) * p_FinI0 + p_IinC;\n\n\t\t\tif (p_FinC0(2) < 0) is_behind = true;\n\t\t}\n\t\t\n\t\tif (!is_behind)\n\t\t{\n\t\t\tfeatures_inI0.insert({feat.first, p_FinI0});\n\t\t\tcount_valid_features++;\n\t\t}\n\t}\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Not enough features for our mle (\" << count_valid_features << \" < \" \n\t\t\t<< min_valid_features << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Matrix3d R_GtoI0;\n\tinitializerHelper::gramSchmidt(gravity_inI0, R_GtoI0);\n\tEigen::Vector4d q_GtoI0 = quatType::rotToQuat(R_GtoI0);\n\tEigen::Vector3d gravity;\n\tgravity << 0.0, 0.0, initializer_options.gravity_mag;\n\tstd::map ori_GtoIi, pos_IiinG, vel_IiinG;\n\tstd::map features_inG;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tori_GtoIi[time_pair.first] = quatType::quatMultiply(ori_I0toIi.at(time_pair.first), q_GtoI0);\n\t\tpos_IiinG[time_pair.first] = R_GtoI0.transpose() * pos_IiinI0.at(time_pair.first);\n\t\tvel_IiinG[time_pair.first] = R_GtoI0.transpose() * vel_IiinI0.at(time_pair.first);\n\t}\n\n\tfor (auto const &feat : features_inI0)\n\tfeatures_inG[feat.first] = R_GtoI0.transpose() * feat.second;\n\n\tceres::Problem problem;\n\n\tstd::map map_states;\n\tstd::vector ceres_vars_ori;\n\tstd::vector ceres_vars_pos;\n\tstd::vector ceres_vars_vel;\n\tstd::vector ceres_vars_bias_g;\n\tstd::vector ceres_vars_bias_a;\n\n\tstd::map map_features;\n\tstd::vector ceres_vars_feat;\n\n\tstd::map map_calib_cam2imu;\n\tstd::vector ceres_vars_calib_cam2imu_ori;\n\tstd::vector ceres_vars_calib_cam2imu_pos;\n\n\tstd::map map_calib_cam;\n\tstd::vector ceres_vars_calib_cam_intrinsics;\n\n\tauto freeStateMemory = [&]()\n\t{\n\t\tfor (auto const &ptr : ceres_vars_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_vel)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_g)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_a)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_feat)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam_intrinsics)\n\t\t\tdelete[] ptr;\n\t};\n\n\tceres::Solver::Options options;\n\toptions.linear_solver_type = ceres::DENSE_SCHUR;\n\toptions.trust_region_strategy_type = ceres::DOGLEG;\n\toptions.num_threads = initializer_options.init_dyn_mle_max_threads;\n\toptions.max_solver_time_in_seconds = initializer_options.init_dyn_mle_max_time;\n\toptions.max_num_iterations = initializer_options.init_dyn_mle_max_iter;\n\toptions.function_tolerance = 1e-5;\n\toptions.gradient_tolerance = 1e-4 * options.function_tolerance;\n\n\tdouble timestamp_k = -1;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble timestamp_k1 = time_pair.first;\n\t\tstd::shared_ptr pre_integration = map_camera_pre_integration_IitoIi1.at(timestamp_k1);\n\t\tEigen::Matrix state_k1;\n\t\tstate_k1.block(0, 0, 4, 1) = ori_GtoIi.at(timestamp_k1);\n\t\tstate_k1.block(4, 0, 3, 1) = pos_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(7, 0, 3, 1) = vel_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(10, 0, 3, 1) = gyroscope_bias;\n\t\tstate_k1.block(13, 0, 3, 1) = accelerometer_bias;\n\n\t\tauto *var_ori = new double[4];\n\t\tfor (int j = 0; j < 4; j++)\n\t\t\tvar_ori[j] = state_k1(0 + j, 0);\n\n\t\tauto *var_pos = new double[3];\n\t\tauto *var_vel = new double[3];\n\t\tauto *var_bias_g = new double[3];\n\t\tauto *var_bias_a = new double[3];\n\n\t\tfor (int j = 0; j < 3; j++)\n\t\t{\n\t\t\tvar_pos[j] = state_k1(4 + j, 0);\n\t\t\tvar_vel[j] = state_k1(7 + j, 0);\n\t\t\tvar_bias_g[j] = state_k1(10 + j, 0);\n\t\t\tvar_bias_a[j] = state_k1(13 + j, 0);\n\t\t}\n\n\t\tauto ceres_jplquat = new stateJplQuatLocal();\n\t\tproblem.AddParameterBlock(var_ori, 4, ceres_jplquat);\n\t\tproblem.AddParameterBlock(var_pos, 3);\n\t\tproblem.AddParameterBlock(var_vel, 3);\n\t\tproblem.AddParameterBlock(var_bias_g, 3);\n\t\tproblem.AddParameterBlock(var_bias_a, 3);\n\n\t\tif (map_states.empty())\n\t\t{\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(13, 1);\n\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t{\n\t\t\t\tx_lin(4 + j) = var_pos[j];\n\t\t\t\tx_lin(7 + j) = var_bias_g[j];\n\t\t\t\tx_lin(10 + j) = var_bias_a[j];\n\t\t\t}\n\t\t\t\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(10, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(10, 10);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1e-5, 2);\n\t\t\tprior_Info.block(4, 4, 3, 3) *= 1.0 / std::pow(0.05, 2);\n\t\t\tprior_Info.block(7, 7, 3, 3) *= 1.0 / std::pow(0.10, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_ori);\n\t\t\tx_types.emplace_back(\"quat_yaw\");\n\t\t\tfactor_params.push_back(var_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_g);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_a);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\t\t}\n\n\t\tmap_states.insert({timestamp_k1, (int)ceres_vars_ori.size()});\n\t\tceres_vars_ori.push_back(var_ori);\n\t\tceres_vars_pos.push_back(var_pos);\n\t\tceres_vars_vel.push_back(var_vel);\n\t\tceres_vars_bias_g.push_back(var_bias_g);\n\t\tceres_vars_bias_a.push_back(var_bias_a);\n\n\t\tif (pre_integration != nullptr)\n\t\t{\n\t\t\tassert(timestamp_k != -1);\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k1)));\n\t\t\tauto *factor_imu = new factorImuPreIntegrationV1(pre_integration->dt, gravity, pre_integration->alpha_tau, pre_integration->beta_tau, \n\t\t\t\tpre_integration->q_tau_k, pre_integration->b_a_lin, pre_integration->b_w_lin, pre_integration->J_q, pre_integration->J_b, \n\t\t\t\tpre_integration->J_a, pre_integration->H_b, pre_integration->H_a, pre_integration->P_meas);\n\t\t\t\n\t\t\tproblem.AddResidualBlock(factor_imu, nullptr, factor_params);\n\t\t}\n\n\t\ttimestamp_k = timestamp_k1;\n\t}\n\n\tfor (auto const &id_pair : map_camera_ids)\n\t{\n\t\tsize_t cam_id = id_pair.first;\n\t\tif (map_calib_cam2imu.find(cam_id) == map_calib_cam2imu.end())\n\t\t{\n\t\t\tauto *var_calib_ori = new double[4];\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tvar_calib_ori[j] = initializer_options.camera_extrinsics.at(cam_id)(0 + j, 0);\n\n\t\t\tauto *var_calib_pos = new double[3];\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tvar_calib_pos[j] = initializer_options.camera_extrinsics.at(cam_id)(4 + j, 0);\n\n\t\t\tauto ceres_calib_jplquat = new stateJplQuatLocal();\n\t\t\tproblem.AddParameterBlock(var_calib_ori, 4, ceres_calib_jplquat);\n\t\t\tproblem.AddParameterBlock(var_calib_pos, 3);\n\n\t\t\tmap_calib_cam2imu.insert({cam_id, (int)ceres_vars_calib_cam2imu_ori.size()});\n\t\t\tceres_vars_calib_cam2imu_ori.push_back(var_calib_ori);\n\t\t\tceres_vars_calib_cam2imu_pos.push_back(var_calib_pos);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(7, 1);\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tx_lin(4 + j) = var_calib_pos[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(6, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(6, 6);\n\t\t\tprior_Info.block(0, 0, 3, 3) *= 1.0 / std::pow(0.001, 2);\n\t\t\tprior_Info.block(3, 3, 3, 3) *= 1.0 / std::pow(0.01, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_ori);\n\t\t\tx_types.emplace_back(\"quat\");\n\t\t\tfactor_params.push_back(var_calib_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t{\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_ori);\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_pos);\n\t\t\t}\n\t\t}\n\n\t\tif (map_calib_cam.find(cam_id) == map_calib_cam.end())\n\t\t{\n\t\t\tauto *var_calib_cam = new double[8];\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tvar_calib_cam[j] = initializer_options.camera_intrinsics.at(cam_id)->getValue()(j, 0);\n\n\t\t\tproblem.AddParameterBlock(var_calib_cam, 8);\n\t\t\tmap_calib_cam.insert({cam_id, (int)ceres_vars_calib_cam_intrinsics.size()});\n\t\t\tceres_vars_calib_cam_intrinsics.push_back(var_calib_cam);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_cam[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(8, 8);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1.0, 2);\n\t\t\tprior_Info.block(4, 4, 4, 4) *= 1.0 / std::pow(0.005, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_cam);\n\t\t\tx_types.emplace_back(\"vec8\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_cam);\n\t\t}\n\t}\n\tassert(map_calib_cam2imu.size() == map_calib_cam.size());\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (features_inG.find(feat.first) == features_inG.end()) continue;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t feat_id = feat.first;\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tbool is_fisheye = (std::dynamic_pointer_cast(initializer_options.camera_intrinsics.at(cam_id)) != nullptr);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_raw = feat.second->uvs.at(cam_id).at(i).block(0, 0, 2, 1).cast();\n\n\t\t\t\tif (map_features.find(feat_id) == map_features.end())\n\t\t\t\t{\n\t\t\t\t\tauto *var_feat = new double[3];\n\t\t\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\t\t\tvar_feat[j] = features_inG.at(feat_id)(j);\n\n\t\t\t\t\tproblem.AddParameterBlock(var_feat, 3);\n\t\t\t\t\tmap_features.insert({feat_id, (int)ceres_vars_feat.size()});\n\t\t\t\t\tceres_vars_feat.push_back(var_feat);\n\t\t\t\t}\n\n\t\t\t\tstd::vector factor_params;\n\t\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_feat.at(map_features.at(feat_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_ori.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_pos.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam_intrinsics.at(map_calib_cam.at(cam_id)));\n\t\t\t\tauto *factor_pinhole = new factorImageReprojCalib(uv_raw, initializer_options.sigma_pix, is_fisheye);\n\n\t\t\t\tceres::LossFunction *loss_function = new ceres::CauchyLoss(1.0);\n\t\t\t\tproblem.AddResidualBlock(factor_pinhole, loss_function, factor_params);\n\t\t\t}\n\t\t}\n\t}\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_g.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_vel.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_a.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_pos.size());\n\tauto rT5 = boost::posix_time::microsec_clock::local_time();\n\n\tceres::Solver::Summary summary;\n\tceres::Solve(options, &problem, &summary);\n\n\tstd::cout << \"[initialize-dynamic]: \" << (int)summary.iterations.size() << \" iterations | \" << map_states.size() << \" states, \" \n\t\t<< map_features.size() << \" feats (\" << count_valid_features << \" valid) | \" << summary.num_parameters << \" param and \"\n\t\t<< summary.num_residuals << \" res | cost \" << std::fixed << summary.initial_cost << \" => \" << summary.final_cost << std::endl;\n\n\tauto rT6 = boost::posix_time::microsec_clock::local_time();\n\n\ttimestamp = newest_cam_time;\n\tif (initializer_options.init_dyn_mle_max_iter != 0 && summary.termination_type != ceres::CONVERGENCE)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Optimization failed: \" << summary.message << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\tstd::cout << \"[initialize-dynamic]: \" << summary.message << std::endl;\n\n\tauto getPose = [&](double timestamp)\n\t{\n\t\tEigen::VectorXd state_imu = Eigen::VectorXd::Zero(16);\n\n\t\tfor (int i = 0; i < 4; i++)\n\t\t\tstate_imu(0 + i) = ceres_vars_ori[map_states[timestamp]][i];\n\n\t\tfor (int i = 0; i < 3; i++)\n\t\t{\n\t\t\tstate_imu(4 + i) = ceres_vars_pos[map_states[timestamp]][i];\n\t\t\tstate_imu(7 + i) = ceres_vars_vel[map_states[timestamp]][i];\n\t\t\tstate_imu(10 + i) = ceres_vars_bias_g[map_states[timestamp]][i];\n\t\t\tstate_imu(13 + i) = ceres_vars_bias_a[map_states[timestamp]][i];\n\t\t}\n\n\t\treturn state_imu;\n\t};\n\n\tassert(map_states.find(newest_cam_time) != map_states.end());\n\t\n\tif (imu_state_ == nullptr)\n\t\timu_state_ = std::make_shared();\n\n\tEigen::VectorXd imu_state = getPose(newest_cam_time);\n\timu_state_->setValue(imu_state);\n\timu_state_->setFej(imu_state);\n\n\tfor (auto const &state_pair : map_states)\n\t{\n\t\tEigen::VectorXd pose = getPose(state_pair.first);\n\n\t\tif (clones_imu_.find(state_pair.first) == clones_imu_.end())\n\t\t{\n\t\t\tauto pose_ = std::make_shared();\n\t\t\tpose_->setValue(pose.block(0, 0, 7, 1));\n\t\t\tpose_->setFej(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.insert({state_pair.first, pose_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tclones_imu_.at(state_pair.first)->setValue(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.at(state_pair.first)->setFej(pose.block(0, 0, 7, 1));\n\t\t}\n\t}\n\n\tfor (auto const &feat_pair : map_features)\n\t{\n\t\tEigen::Vector3d feature;\n\t\tfeature << ceres_vars_feat[feat_pair.second][0], ceres_vars_feat[feat_pair.second][1], ceres_vars_feat[feat_pair.second][2];\n\n\t\tif (map_points_.find(feat_pair.first) == map_points_.end())\n\t\t{\n\t\t\tauto feature_ = std::make_shared();\n\t\t\tfeature_->feature_id = feat_pair.first;\n\t\t\tfeature_->setPointXYZ(feature, false);\n\t\t\tfeature_->setPointXYZ(feature, true);\n\t\t\tmap_points_.insert({feat_pair.first, feature_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmap_points_.at(feat_pair.first)->feature_id = feat_pair.first;\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, false);\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, true);\n\t\t}\n\t}\n\n\tstd::vector> covariance_blocks;\n\tint state_index = map_states[newest_cam_time];\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_ori[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index]));\n\n\tceres::Covariance::Options options_cov;\n\toptions_cov.null_space_rank = (!initializer_options.init_dyn_mle_opt_calib) * ((int)map_calib_cam2imu.size() * (6 + 8));\n\toptions_cov.min_reciprocal_condition_number = initializer_options.init_dyn_min_rec_cond;\n \n\toptions_cov.apply_loss_function = true;\n\toptions_cov.num_threads = initializer_options.init_dyn_mle_max_threads;\n\n\tceres::Covariance problem_cov(options_cov);\n\tbool success = problem_cov.Compute(covariance_blocks, &problem);\n\n\tif (!success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Covariance recovery failed...\" << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\n\torder.clear();\n\torder.push_back(imu_state_);\n\tcovariance = Eigen::MatrixXd::Zero(imu_state_->getSize(), imu_state_->getSize());\n\tEigen::Matrix cov_tmp = Eigen::Matrix::Zero();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_ori[state_index], cov_tmp.data()));\n\tcovariance.block(0, 0, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(3, 3, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(6, 6, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(9, 9, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(12, 12, 3, 3) = cov_tmp.eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(0, 3, 3, 3) = cov_tmp.eval();\n\tcovariance.block(3, 0, 3, 3) = cov_tmp.transpose();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(0, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(0, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(0, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 0, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(3, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(3, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(3, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 3, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(6, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 6, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(6, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 6, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(9, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 9, 3, 3) = cov_tmp.transpose().eval();\n\n\tcovariance.block(0, 0, 3, 3) *= initializer_options.init_dyn_inflation_orientation;\n\tcovariance.block(6, 6, 3, 3) *= initializer_options.init_dyn_inflation_velocity;\n\tcovariance.block(9, 9, 3, 3) *= initializer_options.init_dyn_inflation_bias_gyro;\n\tcovariance.block(12, 12, 3, 3) *= initializer_options.init_dyn_inflation_bias_accel;\n\n\tcovariance = 0.5 * (covariance + covariance.transpose());\n\tEigen::Vector3d sigmas_vel = covariance.block(6, 6, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_bg = covariance.block(9, 9, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_ba = covariance.block(12, 12, 3, 3).diagonal().transpose().cwiseSqrt();\n\tstd::cout << \"[initialize-dynamic]: vel priors = \" << sigmas_vel(0) << \", \" << sigmas_vel(1) << \", \" << sigmas_vel(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: bg priors = \" << sigmas_bg(0) << \", \" << sigmas_bg(1) << \", \" << sigmas_bg(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: ba priors = \" << sigmas_ba(0) << \", \" << sigmas_ba(1) << \", \" << sigmas_ba(2) << std::endl;\n\n\tEigen::MatrixXd x = imu_state_->value();\n\tx.block(4, 0, 3, 1).setZero();\n\timu_state_->setValue(x);\n\timu_state_->setFej(x);\n\n\tauto rT7 = boost::posix_time::microsec_clock::local_time();\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" sec for prelim tests\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" sec for linsys setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" sec for linsys\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" sec for ceres opt setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" sec for ceres opt\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT6).total_microseconds() * 1e-6 << \" sec for ceres covariance\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT1).total_microseconds() * 1e-6 << \" sec total for initialization\" << std::endl;\n\tfreeStateMemory();\n\n\treturn true;\n}\n private:\n inertialInitializerOptions initializer_options;\n std::shared_ptr db;\n std::shared_ptr> imu_data;\n public:\n explicit dynamicInitializer(const inertialInitializerOptions &initializer_options_, std::shared_ptr db_, \n std::shared_ptr> imu_data_) {\n\n}\n bool initialize(double ×tamp, Eigen::MatrixXd &covariance, std::vector> &order, \n std::shared_ptr &imu_state_, std::map> &clones_imu_, \n std::unordered_map> &map_points_) {\n\tauto rT1 = boost::posix_time::microsec_clock::local_time();\n\tdouble newest_cam_time = -1;\n\tfor (auto const &feat : db->getInternalData())\n\t{\n\t\tfor (auto const &cam_time_pair : feat.second->timestamps)\n\t\t{\n\t\t\tfor (auto const &time : cam_time_pair.second)\n\t\t\t{\n\t\t\t\tnewest_cam_time = std::max(newest_cam_time, time);\n\t\t\t}\n\t\t}\n\t}\n\n\tdouble oldest_time = newest_cam_time - initializer_options.init_window_time;\n\n\tif (newest_cam_time < 0 || oldest_time < 0) return false;\n\n\tdb->cleanUpOldmeasurements(oldest_time);\n \n\tbool have_old_imu_readings = false;\n\tauto it_imu = imu_data->begin();\n\n\twhile (it_imu != imu_data->end() && it_imu->timestamp < oldest_time + initializer_options.calib_camimu_dt)\n\t{\n\t\thave_old_imu_readings = true;\n\t\tit_imu = imu_data->erase(it_imu);\n\t}\n\n\tif (db->getInternalData().size() < 0.75 * initializer_options.init_max_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Only \" << db->getInternalData().size() << \" valid features of required (\" \n\t\t\t<< 0.95 * initializer_options.init_max_features << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tif (imu_data->size() < 2 || !have_old_imu_readings)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Waiting for window to reach full size (\" << imu_data->size() << \" imu readings)\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::unordered_map> features;\n\n\tfor (const auto &feat : db->getInternalData())\n\t{\n\t\tauto feat_new = std::make_shared();\n\t\tfeat_new->feature_id = feat.second->feature_id;\n\t\tfeat_new->uvs = feat.second->uvs;\n\t\tfeat_new->uvs_norm = feat.second->uvs_norm;\n\t\tfeat_new->timestamps = feat.second->timestamps;\n\t\tfeatures.insert({feat.first, feat_new});\n\t}\n\n\tconst int min_num_meas_to_optimize = (int)initializer_options.init_window_time;\n\tconst int min_valid_features = 8;\n\n\tbool have_stereo = false;\n\tint count_valid_features = 0;\n\tstd::map map_features_num_meas;\n\tint num_measurements = 0;\n\tdouble oldest_camera_time = INFINITY;\n\tstd::map map_camera_times;\n\tmap_camera_times[newest_cam_time] = true;\n\tstd::map map_camera_ids;\n\tdouble pose_dt_avg = initializer_options.init_window_time / (double)(initializer_options.init_dyn_num_pose + 1);\n\n\tfor (auto const &feat : features)\n\t{\n\t\tstd::vector times;\n\t\tstd::map cam_ids;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tfor (double time : cam_time.second)\n\t\t\t{\n\t\t\t\tdouble time_dt = INFINITY;\n\n\t\t\t\tfor (auto const &tmp : map_camera_times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp.first));\n\n\t\t\t\tfor (auto const &tmp : times)\n\t\t\t\t\ttime_dt = std::min(time_dt, std::abs(time - tmp));\n\n\t\t\t\tif (time_dt >= pose_dt_avg || time_dt == 0.0)\n\t\t\t\t{\n\t\t\t\t\ttimes.push_back(time);\n\t\t\t\t\tcam_ids[cam_time.first] = true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tmap_features_num_meas[feat.first] = (int)times.size();\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tfor (auto const &tmp : times)\n\t\t{\n\t\t\tmap_camera_times[tmp] = true;\n\t\t\toldest_camera_time = std::min(oldest_camera_time, tmp);\n\t\t\tnum_measurements += 2;\n\t\t}\n\n\t\tfor (auto const &tmp : cam_ids)\n\t\t\tmap_camera_ids[tmp.first] = true;\n\n\t\tif (cam_ids.size() > 1) have_stereo = true;\n\n\t\tcount_valid_features++;\n\t}\n\n\tif ((int)map_camera_times.size() < initializer_options.init_dyn_num_pose)\n\t\treturn false;\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Only \" << count_valid_features << \" valid features of required \" << min_valid_features << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Vector3d gyroscope_bias = initializer_options.init_dyn_bias_g;\n\tEigen::Vector3d accelerometer_bias = initializer_options.init_dyn_bias_a;\n\n\tdouble accel_inI_norm = 0.0;\n\tdouble theta_inI_norm = 0.0;\n\tdouble time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\tdouble time_1_in_imu = newest_cam_time + initializer_options.calib_camimu_dt;\n\tstd::vector readings = initializerHelper::selectImuReadings(*imu_data, time_0_in_imu, time_1_in_imu);\n\n\tassert(readings.size() > 2);\n\tfor (size_t k = 0; k < readings.size() - 1; k++)\n\t{\n\t\tauto imu_0 = readings.at(k);\n\t\tauto imu_1 = readings.at(k + 1);\n\t\tdouble dt = imu_1.timestamp - imu_0.timestamp;\n\t\tEigen::Vector3d gyr = 0.5 * (imu_0.gyr + imu_1.gyr) - gyroscope_bias;\n\t\tEigen::Vector3d acc = 0.5 * (imu_0.acc + imu_1.acc) - accelerometer_bias;\n\t\ttheta_inI_norm += ( -gyr * dt).norm();\n\t\taccel_inI_norm += acc.norm();\n\t}\n\n\taccel_inI_norm /= (double)(readings.size() - 1);\n\n\tif (180.0 / M_PI * theta_inI_norm < initializer_options.init_dyn_min_deg)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Gyroscope only \" << 180.0 / M_PI * theta_inI_norm << \" degree change (\" \n\t\t\t<< initializer_options.init_dyn_min_deg << \" thresh)\" << std::endl;\n\t\treturn false;\n\t}\n\tstd::cout << std::fixed << \"[initialize-dynamic]: |theta_I| = \" << 180.0 / M_PI * theta_inI_norm \n\t\t<< \" deg and |accel| = \" << accel_inI_norm << std::endl;\n\n \tauto rT2 = boost::posix_time::microsec_clock::local_time();\n\n\tconst bool use_single_depth = false;\n\tint size_feature = (use_single_depth) ? 1 : 3;\n\tint num_features = count_valid_features;\n\tint system_size = size_feature * num_features + 3 + 3;\n\n\tif (num_measurements < system_size)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Not enough feature measurements (\" << num_measurements << \" meas vs \" << system_size \n\t\t\t<< \" state size)\" << std::endl;\n\n\t\treturn false;\n\t}\n\n\tassert(oldest_camera_time < newest_cam_time);\n\tdouble last_camera_timestamp = 0.0;\n\tstd::map> map_camera_pre_integration_I0toIi, map_camera_pre_integration_IitoIi1; //map_camera_cpi_I0toIi, map_camera_cpi_IitoIi1;\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble current_time = time_pair.first;\n \n\t\tif (current_time == oldest_camera_time)\n\t\t{\n\t\t\tmap_camera_pre_integration_I0toIi.insert({current_time, nullptr});\n\t\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, nullptr});\n\t\t\tlast_camera_timestamp = current_time;\n\t\t\tcontinue;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_time_0_in_imu = oldest_camera_time + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_I0toIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_I0toIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, initializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_I0toIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_I0toIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_I0toIi1_time_0_in_imu, pre_integration_I0toIi1_time_1_in_imu);\n \n\t\tif (pre_integration_I0toIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) \n\t\t\t\t<< \" in has \" << pre_integration_I0toIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_I0toIi1_dt_imu = pre_integration_I0toIi1_readings.at(pre_integration_I0toIi1_readings.size() - 1).timestamp - pre_integration_I0toIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_I0toIi1_dt_imu - (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_I0toIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_I0toIi1_time_1_in_imu - pre_integration_I0toIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_I0toIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_I0toIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_I0toIi1_readings.at(k + 1);\n\t\t\tpre_integration_I0toIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_time_0_in_imu = last_camera_timestamp + initializer_options.calib_camimu_dt;\n\t\tdouble pre_integration_IitoIi1_time_1_in_imu = current_time + initializer_options.calib_camimu_dt;\n\t\tauto pre_integration_IitoIi1 = std::make_shared(initializer_options.sigma_w, initializer_options.sigma_wb, \n\t\t\tinitializer_options.sigma_a, initializer_options.sigma_ab, true);\n\t\tpre_integration_IitoIi1->setLinearizationPoints(gyroscope_bias, accelerometer_bias);\n\n\t\tstd::vector pre_integration_IitoIi1_readings =\n\t\t\tinitializerHelper::selectImuReadings(*imu_data, pre_integration_IitoIi1_time_0_in_imu, pre_integration_IitoIi1_time_1_in_imu);\n\n\t\tif (pre_integration_IitoIi1_readings.size() < 2)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera \" << (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << \" in has \" \n\t\t\t\t<< pre_integration_IitoIi1_readings.size() << \" IMU readings\" << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tdouble pre_integration_IitoIi1_dt_imu = pre_integration_IitoIi1_readings.at(pre_integration_IitoIi1_readings.size() - 1).timestamp - pre_integration_IitoIi1_readings.at(0).timestamp;\n \n\t\tif (std::abs(pre_integration_IitoIi1_dt_imu - (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu)) > 0.01)\n\t\t{\n\t\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Camera IMU was only propagated \" << pre_integration_IitoIi1_dt_imu << \" of \" \n\t\t\t\t<< (pre_integration_IitoIi1_time_1_in_imu - pre_integration_IitoIi1_time_0_in_imu) << std::endl;\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (size_t k = 0; k < pre_integration_IitoIi1_readings.size() - 1; k++)\n\t\t{\n\t\t\tauto imu_0 = pre_integration_IitoIi1_readings.at(k);\n\t\t\tauto imu_1 = pre_integration_IitoIi1_readings.at(k + 1);\n\t\t\tpre_integration_IitoIi1->feedImu(imu_0.timestamp, imu_1.timestamp, imu_0.gyr, imu_0.acc, imu_1.gyr, imu_1.acc);\n\t\t}\n\n\t\tmap_camera_pre_integration_I0toIi.insert({current_time, pre_integration_I0toIi1});\n\t\tmap_camera_pre_integration_IitoIi1.insert({current_time, pre_integration_IitoIi1});\n\t\tlast_camera_timestamp = current_time;\n\t}\n\n\tEigen::MatrixXd A = Eigen::MatrixXd::Zero(num_measurements, system_size);\n\tEigen::VectorXd b = Eigen::VectorXd::Zero(num_measurements);\n\tstd::cout << \"[initialize-dynamic]: System of \" << num_measurements << \" measurement x \" << system_size << \" states created (\" << num_features \n\t\t<< \" features, \" << ((have_stereo) ? \"stereo\" : \"mono\") << \")\" << std::endl;\n\n\tint index_meas = 0;\n\tint idx_feat = 0;\n\tstd::map A_index_features;\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (A_index_features.find(feat.first) == A_index_features.end())\n\t\t{\n\t\t\tA_index_features.insert({feat.first, idx_feat});\n\t\t\tidx_feat += 1;\n\t\t}\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_norm;\n\t\t\t\tuv_norm << (double)feat.second->uvs_norm.at(cam_id).at(i)(0), (double)feat.second->uvs_norm.at(cam_id).at(i)(1);\n\n\t\t\t\tdouble dt = 0.0;\n\t\t\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\t\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\t\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t\t\t{\n\t\t\t\t\tdt = map_camera_pre_integration_I0toIi.at(time)->dt;\n\t\t\t\t\tR_I0toIk = map_camera_pre_integration_I0toIi.at(time)->R_tau_k;\n\t\t\t\t\talpha_I0toIk = map_camera_pre_integration_I0toIi.at(time)->alpha_tau;\n\t\t\t\t}\n\n\t\t\t\tEigen::MatrixXd H_proj = Eigen::MatrixXd::Zero(2, 3);\n\t\t\t\tH_proj << 1, 0, -uv_norm(0), 0, 1, -uv_norm(1);\n\t\t\t\tEigen::MatrixXd Y = H_proj * R_ItoC * R_I0toIk;\n\t\t\t\tEigen::MatrixXd H_i = Eigen::MatrixXd::Zero(2, system_size);\n\t\t\t\tEigen::MatrixXd b_i = Y * alpha_I0toIk - H_proj * p_IinC;\n\n\t\t\t\tif (size_feature == 1)\n\t\t\t\t{\n\t\t\t\t\tassert(false);\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tH_i.block(0, size_feature * A_index_features.at(feat.first), 2, 3) = Y;\n\t\t\t\t}\n\n\t\t\t\tH_i.block(0, size_feature * num_features + 0, 2, 3) = -dt * Y;\n\t\t\t\tH_i.block(0, size_feature * num_features + 3, 2, 3) = 0.5 * dt * dt * Y;\n\n\t\t\t\tA.block(index_meas, 0, 2, A.cols()) = H_i;\n\t\t\t\tb.block(index_meas, 0, 2, 1) = b_i;\n\t\t\t\tindex_meas += 2;\n\t\t\t}\n\t\t}\n\t}\n\tauto rT3 = boost::posix_time::microsec_clock::local_time();\n\n\tEigen::MatrixXd A1 = A.block(0, 0, A.rows(), A.cols() - 3);\n\tEigen::MatrixXd A1A1_inv = (A1.transpose() * A1).llt().solve(Eigen::MatrixXd::Identity(A1.cols(), A1.cols()));\n\tEigen::MatrixXd A2 = A.block(0, A.cols() - 3, A.rows(), 3);\n\tEigen::MatrixXd Temp = A2.transpose() * (Eigen::MatrixXd::Identity(A1.rows(), A1.rows()) - A1 * A1A1_inv * A1.transpose());\n\tEigen::MatrixXd D = Temp * A2;\n\tEigen::MatrixXd d = Temp * b;\n\tEigen::Matrix coeff = initializerHelper::computeDongsiCoeff(D, d, initializer_options.gravity_mag);\n\n\tassert(coeff(0) == 1);\n\tEigen::Matrix companion_matrix = Eigen::Matrix::Zero(coeff.rows() - 1, coeff.rows() - 1);\n\tcompanion_matrix.diagonal(-1).setOnes();\n\tcompanion_matrix.col(companion_matrix.cols() - 1) = - coeff.reverse().head(coeff.rows() - 1);\n\tEigen::JacobiSVD> svd_0(companion_matrix);\n\tEigen::MatrixXd singular_values_0 = svd_0.singularValues();\n\tdouble cond_0 = singular_values_0(0) / singular_values_0(singular_values_0.rows() - 1);\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: CM cond = \" << cond_0 << \" | rank = \" << (int)svd_0.rank() << \" of \" << (int)companion_matrix.cols() \n\t\t<< \" (\" << svd_0.threshold() << \" thresh)\" << std::endl;\n\n\tif (svd_0.rank() != companion_matrix.rows())\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Eigenvalue decomposition not full rank\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::EigenSolver> solver(companion_matrix, false);\n \n\tif (solver.info() != Eigen::Success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: failed to compute the eigenvalue decomposition\" << std::endl;\n\t\treturn false;\n\t}\n\n\tbool lambda_found = false;\n\tdouble lambda_min = -1;\n\tdouble cost_min = INFINITY;\n\tEigen::MatrixXd I_dd = Eigen::MatrixXd::Identity(D.rows(), D.rows());\n\n\tfor (int i = 0; i < solver.eigenvalues().size(); i++) {\n\t\tauto val = solver.eigenvalues()(i);\n\t\tif (val.imag() == 0)\n\t\t{\n\t\t\tdouble lambda = val.real();\n\t\t\tEigen::MatrixXd D_lambdaI_inv = (D - lambda * I_dd).llt().solve(I_dd);\n\t\t\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\t\t\tdouble cost = std::abs(state_grav.norm() - initializer_options.gravity_mag);\n\n\t\t\tif (!lambda_found || cost < cost_min)\n\t\t\t{\n\t\t\t\tlambda_found = true;\n\t\t\t\tlambda_min = lambda;\n\t\t\t\tcost_min = cost;\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!lambda_found)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Failed to find a real eigenvalue\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Smallest real eigenvalue = \" << lambda_min << \" (cost of \" \n\t\t<< cost_min << \")\" << std::endl;\n\n\tEigen::MatrixXd D_lambdaI_inv = (D - lambda_min * I_dd).llt().solve(I_dd);\n\tEigen::VectorXd state_grav = D_lambdaI_inv * d;\n\n\tEigen::VectorXd state_feat_vel = - A1A1_inv * A1.transpose() * A2 * state_grav + A1A1_inv * A1.transpose() * b;\n\tEigen::MatrixXd x_hat = Eigen::MatrixXd::Zero(system_size, 1);\n\tx_hat.block(0, 0, size_feature * num_features + 3, 1) = state_feat_vel;\n\tx_hat.block(size_feature * num_features + 3, 0, 3, 1) = state_grav;\n\tEigen::Vector3d v_I0inI0 = x_hat.block(size_feature * num_features + 0, 0, 3, 1);\n\tstd::cout << std::fixed << \"[initialize-dynamic]: Velocity in I0 was \" << v_I0inI0(0) << \", \" << v_I0inI0(1) << \", \" << v_I0inI0(2) \n\t\t<< \" and |v| = \" << v_I0inI0.norm() << std::endl;\n\n\tEigen::Vector3d gravity_inI0 = x_hat.block(size_feature * num_features + 3, 0, 3, 1);\n\tdouble init_max_grav_difference = 1e-3;\n\n\tif (std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) > init_max_grav_difference)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Gravity did not converge (\" << std::abs(gravity_inI0.norm() - initializer_options.gravity_mag) \n\t\t\t<< \" > \" << init_max_grav_difference << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tstd::cout << \"[initialize-dynamic]: Gravity in I0 was \" << gravity_inI0(0) << \", \" << gravity_inI0(1) << \", \" << gravity_inI0(2) << \" and |g| = \" << gravity_inI0.norm() << std::endl;\n\tauto rT4 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::map ori_I0toIi, pos_IiinI0, vel_IiinI0;\n\t\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble time = time_pair.first;\n\n\t\tdouble dt = 0.0;\n\t\tEigen::MatrixXd R_I0toIk = Eigen::MatrixXd::Identity(3, 3);\n\t\tEigen::MatrixXd alpha_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\t\tEigen::MatrixXd beta_I0toIk = Eigen::MatrixXd::Zero(3, 1);\n\n\t\tif (map_camera_pre_integration_I0toIi.find(time) != map_camera_pre_integration_I0toIi.end() && map_camera_pre_integration_I0toIi.at(time) != nullptr)\n\t\t{\n\t\t\tauto pre_integration = map_camera_pre_integration_I0toIi.at(time);\n\t\t\tdt = pre_integration->dt;\n\t\t\tR_I0toIk = pre_integration->R_tau_k;\n\t\t\talpha_I0toIk = pre_integration->alpha_tau;\n\t\t\tbeta_I0toIk = pre_integration->beta_tau;\n\t\t}\n\n\t\tEigen::Vector3d p_IkinI0 = v_I0inI0 * dt - 0.5 * gravity_inI0 * dt * dt + alpha_I0toIk;\n\t\tEigen::Vector3d v_IkinI0 = v_I0inI0 - gravity_inI0 * dt + beta_I0toIk;\n\n\t\tori_I0toIi.insert({time, quatType::rotToQuat(R_I0toIk)});\n\t\tpos_IiinI0.insert({time, p_IkinI0});\n\t\tvel_IiinI0.insert({time, v_IkinI0});\n\t}\n\n\tcount_valid_features = 0;\n\tstd::map features_inI0;\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tEigen::Vector3d p_FinI0;\n\t\tif (size_feature == 1)\n\t\t\tassert(false);\n\t\telse\n\t\t\tp_FinI0 = x_hat.block(size_feature * A_index_features.at(feat.first), 0, 3, 1);\n\n\t\tbool is_behind = false;\n\t\t\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tEigen::Vector4d q_ItoC = initializer_options.camera_extrinsics.at(cam_id).block(0, 0, 4, 1);\n\t\t\tEigen::Vector3d p_IinC = initializer_options.camera_extrinsics.at(cam_id).block(4, 0, 3, 1);\n\t\t\tEigen::Vector3d p_FinC0 = quatType::quatToRot(q_ItoC) * p_FinI0 + p_IinC;\n\n\t\t\tif (p_FinC0(2) < 0) is_behind = true;\n\t\t}\n\t\t\n\t\tif (!is_behind)\n\t\t{\n\t\t\tfeatures_inI0.insert({feat.first, p_FinI0});\n\t\t\tcount_valid_features++;\n\t\t}\n\t}\n\n\tif (count_valid_features < min_valid_features)\n\t{\n\t\tstd::cout << std::fixed << \"[initialize-dynamic]: Not enough features for our mle (\" << count_valid_features << \" < \" \n\t\t\t<< min_valid_features << \")\" << std::endl;\n\t\treturn false;\n\t}\n\n\tEigen::Matrix3d R_GtoI0;\n\tinitializerHelper::gramSchmidt(gravity_inI0, R_GtoI0);\n\tEigen::Vector4d q_GtoI0 = quatType::rotToQuat(R_GtoI0);\n\tEigen::Vector3d gravity;\n\tgravity << 0.0, 0.0, initializer_options.gravity_mag;\n\tstd::map ori_GtoIi, pos_IiinG, vel_IiinG;\n\tstd::map features_inG;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tori_GtoIi[time_pair.first] = quatType::quatMultiply(ori_I0toIi.at(time_pair.first), q_GtoI0);\n\t\tpos_IiinG[time_pair.first] = R_GtoI0.transpose() * pos_IiinI0.at(time_pair.first);\n\t\tvel_IiinG[time_pair.first] = R_GtoI0.transpose() * vel_IiinI0.at(time_pair.first);\n\t}\n\n\tfor (auto const &feat : features_inI0)\n\tfeatures_inG[feat.first] = R_GtoI0.transpose() * feat.second;\n\n\tceres::Problem problem;\n\n\tstd::map map_states;\n\tstd::vector ceres_vars_ori;\n\tstd::vector ceres_vars_pos;\n\tstd::vector ceres_vars_vel;\n\tstd::vector ceres_vars_bias_g;\n\tstd::vector ceres_vars_bias_a;\n\n\tstd::map map_features;\n\tstd::vector ceres_vars_feat;\n\n\tstd::map map_calib_cam2imu;\n\tstd::vector ceres_vars_calib_cam2imu_ori;\n\tstd::vector ceres_vars_calib_cam2imu_pos;\n\n\tstd::map map_calib_cam;\n\tstd::vector ceres_vars_calib_cam_intrinsics;\n\n\tauto freeStateMemory = [&]()\n\t{\n\t\tfor (auto const &ptr : ceres_vars_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_vel)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_g)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_bias_a)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_feat)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_ori)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam2imu_pos)\n\t\t\tdelete[] ptr;\n\t\tfor (auto const &ptr : ceres_vars_calib_cam_intrinsics)\n\t\t\tdelete[] ptr;\n\t};\n\n\tceres::Solver::Options options;\n\toptions.linear_solver_type = ceres::DENSE_SCHUR;\n\toptions.trust_region_strategy_type = ceres::DOGLEG;\n\toptions.num_threads = initializer_options.init_dyn_mle_max_threads;\n\toptions.max_solver_time_in_seconds = initializer_options.init_dyn_mle_max_time;\n\toptions.max_num_iterations = initializer_options.init_dyn_mle_max_iter;\n\toptions.function_tolerance = 1e-5;\n\toptions.gradient_tolerance = 1e-4 * options.function_tolerance;\n\n\tdouble timestamp_k = -1;\n\n\tfor (auto const &time_pair : map_camera_times)\n\t{\n\t\tdouble timestamp_k1 = time_pair.first;\n\t\tstd::shared_ptr pre_integration = map_camera_pre_integration_IitoIi1.at(timestamp_k1);\n\t\tEigen::Matrix state_k1;\n\t\tstate_k1.block(0, 0, 4, 1) = ori_GtoIi.at(timestamp_k1);\n\t\tstate_k1.block(4, 0, 3, 1) = pos_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(7, 0, 3, 1) = vel_IiinG.at(timestamp_k1);\n\t\tstate_k1.block(10, 0, 3, 1) = gyroscope_bias;\n\t\tstate_k1.block(13, 0, 3, 1) = accelerometer_bias;\n\n\t\tauto *var_ori = new double[4];\n\t\tfor (int j = 0; j < 4; j++)\n\t\t\tvar_ori[j] = state_k1(0 + j, 0);\n\n\t\tauto *var_pos = new double[3];\n\t\tauto *var_vel = new double[3];\n\t\tauto *var_bias_g = new double[3];\n\t\tauto *var_bias_a = new double[3];\n\n\t\tfor (int j = 0; j < 3; j++)\n\t\t{\n\t\t\tvar_pos[j] = state_k1(4 + j, 0);\n\t\t\tvar_vel[j] = state_k1(7 + j, 0);\n\t\t\tvar_bias_g[j] = state_k1(10 + j, 0);\n\t\t\tvar_bias_a[j] = state_k1(13 + j, 0);\n\t\t}\n\n\t\tauto ceres_jplquat = new stateJplQuatLocal();\n\t\tproblem.AddParameterBlock(var_ori, 4, ceres_jplquat);\n\t\tproblem.AddParameterBlock(var_pos, 3);\n\t\tproblem.AddParameterBlock(var_vel, 3);\n\t\tproblem.AddParameterBlock(var_bias_g, 3);\n\t\tproblem.AddParameterBlock(var_bias_a, 3);\n\n\t\tif (map_states.empty())\n\t\t{\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(13, 1);\n\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t{\n\t\t\t\tx_lin(4 + j) = var_pos[j];\n\t\t\t\tx_lin(7 + j) = var_bias_g[j];\n\t\t\t\tx_lin(10 + j) = var_bias_a[j];\n\t\t\t}\n\t\t\t\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(10, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(10, 10);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1e-5, 2);\n\t\t\tprior_Info.block(4, 4, 3, 3) *= 1.0 / std::pow(0.05, 2);\n\t\t\tprior_Info.block(7, 7, 3, 3) *= 1.0 / std::pow(0.10, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_ori);\n\t\t\tx_types.emplace_back(\"quat_yaw\");\n\t\t\tfactor_params.push_back(var_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_g);\n\t\t\tx_types.emplace_back(\"vec3\");\n\t\t\tfactor_params.push_back(var_bias_a);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\t\t}\n\n\t\tmap_states.insert({timestamp_k1, (int)ceres_vars_ori.size()});\n\t\tceres_vars_ori.push_back(var_ori);\n\t\tceres_vars_pos.push_back(var_pos);\n\t\tceres_vars_vel.push_back(var_vel);\n\t\tceres_vars_bias_g.push_back(var_bias_g);\n\t\tceres_vars_bias_a.push_back(var_bias_a);\n\n\t\tif (pre_integration != nullptr)\n\t\t{\n\t\t\tassert(timestamp_k != -1);\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k)));\n\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_g.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_vel.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_bias_a.at(map_states.at(timestamp_k1)));\n\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(timestamp_k1)));\n\t\t\tauto *factor_imu = new factorImuPreIntegrationV1(pre_integration->dt, gravity, pre_integration->alpha_tau, pre_integration->beta_tau, \n\t\t\t\tpre_integration->q_tau_k, pre_integration->b_a_lin, pre_integration->b_w_lin, pre_integration->J_q, pre_integration->J_b, \n\t\t\t\tpre_integration->J_a, pre_integration->H_b, pre_integration->H_a, pre_integration->P_meas);\n\t\t\t\n\t\t\tproblem.AddResidualBlock(factor_imu, nullptr, factor_params);\n\t\t}\n\n\t\ttimestamp_k = timestamp_k1;\n\t}\n\n\tfor (auto const &id_pair : map_camera_ids)\n\t{\n\t\tsize_t cam_id = id_pair.first;\n\t\tif (map_calib_cam2imu.find(cam_id) == map_calib_cam2imu.end())\n\t\t{\n\t\t\tauto *var_calib_ori = new double[4];\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tvar_calib_ori[j] = initializer_options.camera_extrinsics.at(cam_id)(0 + j, 0);\n\n\t\t\tauto *var_calib_pos = new double[3];\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tvar_calib_pos[j] = initializer_options.camera_extrinsics.at(cam_id)(4 + j, 0);\n\n\t\t\tauto ceres_calib_jplquat = new stateJplQuatLocal();\n\t\t\tproblem.AddParameterBlock(var_calib_ori, 4, ceres_calib_jplquat);\n\t\t\tproblem.AddParameterBlock(var_calib_pos, 3);\n\n\t\t\tmap_calib_cam2imu.insert({cam_id, (int)ceres_vars_calib_cam2imu_ori.size()});\n\t\t\tceres_vars_calib_cam2imu_ori.push_back(var_calib_ori);\n\t\t\tceres_vars_calib_cam2imu_pos.push_back(var_calib_pos);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(7, 1);\n\t\t\tfor (int j = 0; j < 4; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_ori[j];\n\n\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\tx_lin(4 + j) = var_calib_pos[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(6, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(6, 6);\n\t\t\tprior_Info.block(0, 0, 3, 3) *= 1.0 / std::pow(0.001, 2);\n\t\t\tprior_Info.block(3, 3, 3, 3) *= 1.0 / std::pow(0.01, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_ori);\n\t\t\tx_types.emplace_back(\"quat\");\n\t\t\tfactor_params.push_back(var_calib_pos);\n\t\t\tx_types.emplace_back(\"vec3\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t{\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_ori);\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_pos);\n\t\t\t}\n\t\t}\n\n\t\tif (map_calib_cam.find(cam_id) == map_calib_cam.end())\n\t\t{\n\t\t\tauto *var_calib_cam = new double[8];\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tvar_calib_cam[j] = initializer_options.camera_intrinsics.at(cam_id)->getValue()(j, 0);\n\n\t\t\tproblem.AddParameterBlock(var_calib_cam, 8);\n\t\t\tmap_calib_cam.insert({cam_id, (int)ceres_vars_calib_cam_intrinsics.size()});\n\t\t\tceres_vars_calib_cam_intrinsics.push_back(var_calib_cam);\n\n\t\t\tEigen::MatrixXd x_lin = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tfor (int j = 0; j < 8; j++)\n\t\t\t\tx_lin(0 + j) = var_calib_cam[j];\n\n\t\t\tEigen::MatrixXd prior_grad = Eigen::MatrixXd::Zero(8, 1);\n\t\t\tEigen::MatrixXd prior_Info = Eigen::MatrixXd::Identity(8, 8);\n\t\t\tprior_Info.block(0, 0, 4, 4) *= 1.0 / std::pow(1.0, 2);\n\t\t\tprior_Info.block(4, 4, 4, 4) *= 1.0 / std::pow(0.005, 2);\n\n\t\t\tstd::vector x_types;\n\t\t\tstd::vector factor_params;\n\t\t\tfactor_params.push_back(var_calib_cam);\n\t\t\tx_types.emplace_back(\"vec8\");\n\n\t\t\tauto *factor_prior = new factorGenericPrior(x_lin, x_types, prior_Info, prior_grad);\n\t\t\tproblem.AddResidualBlock(factor_prior, nullptr, factor_params);\n\n\t\t\tif (!initializer_options.init_dyn_mle_opt_calib)\n\t\t\t\tproblem.SetParameterBlockConstant(var_calib_cam);\n\t\t}\n\t}\n\tassert(map_calib_cam2imu.size() == map_calib_cam.size());\n\n\tfor (auto const &feat : features)\n\t{\n\t\tif (map_features_num_meas[feat.first] < min_num_meas_to_optimize) continue;\n\n\t\tif (features_inG.find(feat.first) == features_inG.end()) continue;\n\n\t\tfor (auto const &cam_time : feat.second->timestamps)\n\t\t{\n\t\t\tsize_t feat_id = feat.first;\n\t\t\tsize_t cam_id = cam_time.first;\n\t\t\tbool is_fisheye = (std::dynamic_pointer_cast(initializer_options.camera_intrinsics.at(cam_id)) != nullptr);\n\n\t\t\tfor (size_t i = 0; i < cam_time.second.size(); i++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(i);\n\t\t\t\tif (map_camera_times.find(time) == map_camera_times.end()) continue;\n\n\t\t\t\tEigen::Vector2d uv_raw = feat.second->uvs.at(cam_id).at(i).block(0, 0, 2, 1).cast();\n\n\t\t\t\tif (map_features.find(feat_id) == map_features.end())\n\t\t\t\t{\n\t\t\t\t\tauto *var_feat = new double[3];\n\t\t\t\t\tfor (int j = 0; j < 3; j++)\n\t\t\t\t\t\tvar_feat[j] = features_inG.at(feat_id)(j);\n\n\t\t\t\t\tproblem.AddParameterBlock(var_feat, 3);\n\t\t\t\t\tmap_features.insert({feat_id, (int)ceres_vars_feat.size()});\n\t\t\t\t\tceres_vars_feat.push_back(var_feat);\n\t\t\t\t}\n\n\t\t\t\tstd::vector factor_params;\n\t\t\t\tfactor_params.push_back(ceres_vars_ori.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_pos.at(map_states.at(time)));\n\t\t\t\tfactor_params.push_back(ceres_vars_feat.at(map_features.at(feat_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_ori.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam2imu_pos.at(map_calib_cam2imu.at(cam_id)));\n\t\t\t\tfactor_params.push_back(ceres_vars_calib_cam_intrinsics.at(map_calib_cam.at(cam_id)));\n\t\t\t\tauto *factor_pinhole = new factorImageReprojCalib(uv_raw, initializer_options.sigma_pix, is_fisheye);\n\n\t\t\t\tceres::LossFunction *loss_function = new ceres::CauchyLoss(1.0);\n\t\t\t\tproblem.AddResidualBlock(factor_pinhole, loss_function, factor_params);\n\t\t\t}\n\t\t}\n\t}\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_g.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_vel.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_bias_a.size());\n\tassert(ceres_vars_ori.size() == ceres_vars_pos.size());\n\tauto rT5 = boost::posix_time::microsec_clock::local_time();\n\n\tceres::Solver::Summary summary;\n\tceres::Solve(options, &problem, &summary);\n\n\tstd::cout << \"[initialize-dynamic]: \" << (int)summary.iterations.size() << \" iterations | \" << map_states.size() << \" states, \" \n\t\t<< map_features.size() << \" feats (\" << count_valid_features << \" valid) | \" << summary.num_parameters << \" param and \"\n\t\t<< summary.num_residuals << \" res | cost \" << std::fixed << summary.initial_cost << \" => \" << summary.final_cost << std::endl;\n\n\tauto rT6 = boost::posix_time::microsec_clock::local_time();\n\n\ttimestamp = newest_cam_time;\n\tif (initializer_options.init_dyn_mle_max_iter != 0 && summary.termination_type != ceres::CONVERGENCE)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Optimization failed: \" << summary.message << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\tstd::cout << \"[initialize-dynamic]: \" << summary.message << std::endl;\n\n\tauto getPose = [&](double timestamp)\n\t{\n\t\tEigen::VectorXd state_imu = Eigen::VectorXd::Zero(16);\n\n\t\tfor (int i = 0; i < 4; i++)\n\t\t\tstate_imu(0 + i) = ceres_vars_ori[map_states[timestamp]][i];\n\n\t\tfor (int i = 0; i < 3; i++)\n\t\t{\n\t\t\tstate_imu(4 + i) = ceres_vars_pos[map_states[timestamp]][i];\n\t\t\tstate_imu(7 + i) = ceres_vars_vel[map_states[timestamp]][i];\n\t\t\tstate_imu(10 + i) = ceres_vars_bias_g[map_states[timestamp]][i];\n\t\t\tstate_imu(13 + i) = ceres_vars_bias_a[map_states[timestamp]][i];\n\t\t}\n\n\t\treturn state_imu;\n\t};\n\n\tassert(map_states.find(newest_cam_time) != map_states.end());\n\t\n\tif (imu_state_ == nullptr)\n\t\timu_state_ = std::make_shared();\n\n\tEigen::VectorXd imu_state = getPose(newest_cam_time);\n\timu_state_->setValue(imu_state);\n\timu_state_->setFej(imu_state);\n\n\tfor (auto const &state_pair : map_states)\n\t{\n\t\tEigen::VectorXd pose = getPose(state_pair.first);\n\n\t\tif (clones_imu_.find(state_pair.first) == clones_imu_.end())\n\t\t{\n\t\t\tauto pose_ = std::make_shared();\n\t\t\tpose_->setValue(pose.block(0, 0, 7, 1));\n\t\t\tpose_->setFej(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.insert({state_pair.first, pose_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tclones_imu_.at(state_pair.first)->setValue(pose.block(0, 0, 7, 1));\n\t\t\tclones_imu_.at(state_pair.first)->setFej(pose.block(0, 0, 7, 1));\n\t\t}\n\t}\n\n\tfor (auto const &feat_pair : map_features)\n\t{\n\t\tEigen::Vector3d feature;\n\t\tfeature << ceres_vars_feat[feat_pair.second][0], ceres_vars_feat[feat_pair.second][1], ceres_vars_feat[feat_pair.second][2];\n\n\t\tif (map_points_.find(feat_pair.first) == map_points_.end())\n\t\t{\n\t\t\tauto feature_ = std::make_shared();\n\t\t\tfeature_->feature_id = feat_pair.first;\n\t\t\tfeature_->setPointXYZ(feature, false);\n\t\t\tfeature_->setPointXYZ(feature, true);\n\t\t\tmap_points_.insert({feat_pair.first, feature_});\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmap_points_.at(feat_pair.first)->feature_id = feat_pair.first;\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, false);\n\t\t\tmap_points_.at(feat_pair.first)->setPointXYZ(feature, true);\n\t\t}\n\t}\n\n\tstd::vector> covariance_blocks;\n\tint state_index = map_states[newest_cam_time];\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_ori[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_pos[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_vel[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index]));\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index]));\n\n\tcovariance_blocks.push_back(std::make_pair(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index]));\n\n\tceres::Covariance::Options options_cov;\n\toptions_cov.null_space_rank = (!initializer_options.init_dyn_mle_opt_calib) * ((int)map_calib_cam2imu.size() * (6 + 8));\n\toptions_cov.min_reciprocal_condition_number = initializer_options.init_dyn_min_rec_cond;\n \n\toptions_cov.apply_loss_function = true;\n\toptions_cov.num_threads = initializer_options.init_dyn_mle_max_threads;\n\n\tceres::Covariance problem_cov(options_cov);\n\tbool success = problem_cov.Compute(covariance_blocks, &problem);\n\n\tif (!success)\n\t{\n\t\tstd::cout << \"[initialize-dynamic]: Covariance recovery failed...\" << std::endl;\n\t\tfreeStateMemory();\n\t\treturn false;\n\t}\n\n\torder.clear();\n\torder.push_back(imu_state_);\n\tcovariance = Eigen::MatrixXd::Zero(imu_state_->getSize(), imu_state_->getSize());\n\tEigen::Matrix cov_tmp = Eigen::Matrix::Zero();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_ori[state_index], cov_tmp.data()));\n\tcovariance.block(0, 0, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(3, 3, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(6, 6, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(9, 9, 3, 3) = cov_tmp.eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_a[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(12, 12, 3, 3) = cov_tmp.eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_pos[state_index], cov_tmp.data()));\n\tcovariance.block(0, 3, 3, 3) = cov_tmp.eval();\n\tcovariance.block(3, 0, 3, 3) = cov_tmp.transpose();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(0, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(0, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 0, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_ori[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(0, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 0, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_vel[state_index], cov_tmp.data()));\n\tcovariance.block(3, 6, 3, 3) = cov_tmp.eval();\n\tcovariance.block(6, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(3, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 3, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_pos[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(3, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 3, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_g[state_index], cov_tmp.data()));\n\tcovariance.block(6, 9, 3, 3) = cov_tmp.eval();\n\tcovariance.block(9, 6, 3, 3) = cov_tmp.transpose().eval();\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_vel[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(6, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 6, 3, 3) = cov_tmp.transpose().eval();\n\n\tCHECK(problem_cov.GetCovarianceBlockInTangentSpace(ceres_vars_bias_g[state_index], ceres_vars_bias_a[state_index], cov_tmp.data()));\n\tcovariance.block(9, 12, 3, 3) = cov_tmp.eval();\n\tcovariance.block(12, 9, 3, 3) = cov_tmp.transpose().eval();\n\n\tcovariance.block(0, 0, 3, 3) *= initializer_options.init_dyn_inflation_orientation;\n\tcovariance.block(6, 6, 3, 3) *= initializer_options.init_dyn_inflation_velocity;\n\tcovariance.block(9, 9, 3, 3) *= initializer_options.init_dyn_inflation_bias_gyro;\n\tcovariance.block(12, 12, 3, 3) *= initializer_options.init_dyn_inflation_bias_accel;\n\n\tcovariance = 0.5 * (covariance + covariance.transpose());\n\tEigen::Vector3d sigmas_vel = covariance.block(6, 6, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_bg = covariance.block(9, 9, 3, 3).diagonal().transpose().cwiseSqrt();\n\tEigen::Vector3d sigmas_ba = covariance.block(12, 12, 3, 3).diagonal().transpose().cwiseSqrt();\n\tstd::cout << \"[initialize-dynamic]: vel priors = \" << sigmas_vel(0) << \", \" << sigmas_vel(1) << \", \" << sigmas_vel(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: bg priors = \" << sigmas_bg(0) << \", \" << sigmas_bg(1) << \", \" << sigmas_bg(2) << std::endl;\n\tstd::cout << \"[initialize-dynamic]: ba priors = \" << sigmas_ba(0) << \", \" << sigmas_ba(1) << \", \" << sigmas_ba(2) << std::endl;\n\n\tEigen::MatrixXd x = imu_state_->value();\n\tx.block(4, 0, 3, 1).setZero();\n\timu_state_->setValue(x);\n\timu_state_->setFej(x);\n\n\tauto rT7 = boost::posix_time::microsec_clock::local_time();\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" sec for prelim tests\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" sec for linsys setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT4 - rT3).total_microseconds() * 1e-6 << \" sec for linsys\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT5 - rT4).total_microseconds() * 1e-6 << \" sec for ceres opt setup\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT6 - rT5).total_microseconds() * 1e-6 << \" sec for ceres opt\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT6).total_microseconds() * 1e-6 << \" sec for ceres covariance\" << std::endl;\n\tstd::cout << std::fixed << \"[initialize-dynamic]: \" << (rT7 - rT1).total_microseconds() * 1e-6 << \" sec total for initialization\" << std::endl;\n\tfreeStateMemory();\n\n\treturn true;\n}\n private:\n inertialInitializerOptions initializer_options;\n std::shared_ptr db;\n std::shared_ptr> imu_data;\n};"], ["/voxel_svio/include/msckf.h", "class propagator {\n public:\n propagator(noiseManager noises_, double gravity_mag_) {\n\tnoises.sigma_w_2 = std::pow(noises.sigma_w, 2);\n\tnoises.sigma_a_2 = std::pow(noises.sigma_a, 2);\n\tnoises.sigma_wb_2 = std::pow(noises.sigma_wb, 2);\n\tnoises.sigma_ab_2 = std::pow(noises.sigma_ab, 2);\n\n\tgravity << 0.0, 0.0, gravity_mag_;\n\n\thave_last_prop_time_offset = false;\n\tlast_prop_time_offset = 0.0;\n}\n void feedImu(const imuData &imu_data, double oldest_time = -1) {\n\tv_imu_data.emplace_back(imu_data);\n\n\tcleanOldImuMeasurements(oldest_time - 0.10);\t\n}\n void cleanOldImuMeasurements(double oldest_time) {\n\tif (oldest_time < 0)\n\t\treturn;\n\tauto it0 = v_imu_data.begin();\n\twhile (it0 != v_imu_data.end())\n\t{\n\t\tif (it0->timestamp < oldest_time)\n\t\t{\n\t\t\tit0 = v_imu_data.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n}\n void invalidateCache() {\n\tcache_imu_valid = false;\n}\n void resetTransitionMatrices(std::shared_ptr state_ptr);\n static imuData interpolateData(const imuData &imu_1, const imuData &imu_2, double timestamp) {\n\tdouble lambda = (timestamp - imu_1.timestamp) / (imu_2.timestamp - imu_1.timestamp);\n\n\timuData data;\n\tdata.timestamp = timestamp;\n\tdata.acc = (1 - lambda) * imu_1.acc + lambda * imu_2.acc;\n\tdata.gyr = (1 - lambda) * imu_1.gyr + lambda * imu_2.gyr;\n\n\treturn data;\n}\n static std::vector selectImuReadings(const std::vector &v_imu_data_, double time_0, double time_1, bool warn = true) {\n\tstd::vector prop_data;\n\n\tif (v_imu_data_.empty())\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << \"propagator::selectImuReadings(): No IMU measurements. IMU-CAMERA are likely messed up\" << std::endl;\n\t\t\n\t\treturn prop_data;\n\t}\n\n\tfor (size_t i = 0; i < v_imu_data_.size() - 1; i++)\n\t{\n\t\tif (v_imu_data_.at(i + 1).timestamp > time_0 && v_imu_data_.at(i).timestamp < time_0)\n\t\t{\n\t\t\timuData data = propagator::interpolateData(v_imu_data_.at(i), v_imu_data_.at(i + 1), time_0);\n\t\t\tprop_data.push_back(data);\n\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (v_imu_data_.at(i).timestamp >= time_0 && v_imu_data_.at(i + 1).timestamp <= time_1)\n\t\t{\n\t\t\tprop_data.push_back(v_imu_data_.at(i));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (v_imu_data_.at(i + 1).timestamp > time_1)\n\t\t{\n\t\t\tif (v_imu_data_.at(i).timestamp > time_1 && i == 0)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if (v_imu_data_.at(i).timestamp > time_1)\n\t\t\t{\n\t\t\t\timuData data = propagator::interpolateData(v_imu_data_.at(i - 1), v_imu_data_.at(i), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tprop_data.push_back(v_imu_data_.at(i));\n\t\t\t}\n\n\t\t\tif (prop_data.at(prop_data.size() - 1).timestamp != time_1)\n\t\t\t{\n\t\t\t\timuData data = propagator::interpolateData(v_imu_data_.at(i), v_imu_data_.at(i + 1), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (prop_data.empty())\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << \"propagator::selectImuReadings(): No IMU measurements to propagate with (\" << (int)prop_data.size() << \" of 2). IMU-CAMERA are likely messed up\" << std::endl;\n\n\t\treturn prop_data;\n\t}\n\n\tif (prop_data.at(prop_data.size() - 1).timestamp != time_1)\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << std::fixed << \"propagator::selectImuReadings(): Missing inertial measurements to propagate with (\" << time_1 - v_imu_data_.at(v_imu_data_.size() - 1).timestamp \n\t\t\t\t<< \" sec missing)\" << std::endl;\n\n\t\timuData data = propagator::interpolateData(v_imu_data_.at(v_imu_data_.size() - 2), v_imu_data_.at(v_imu_data_.size() - 1), time_1);\n\t\tprop_data.push_back(data);\n\t}\n\n\tfor (size_t i = 0; i < prop_data.size() - 1; i++)\n\t{\n\t\tif (std::abs(prop_data.at(i + 1).timestamp - prop_data.at(i).timestamp) < 1e-12)\n\t\t{\n\t\t\tif (warn)\n\t\t\t\tstd::cout << \"propagator::selectImuReadings(): Zero DT between IMU reading \" << (int)i << \" and \" << (int)(i + 1) << \", removing it\" << std::endl;\n\n\t\t\tprop_data.erase(prop_data.begin() + i);\n\t\t\ti--;\n\t\t}\n\t}\n\n\tif (prop_data.size() < 2)\n\t{\n\t\tif (warn)\n\t\t\tstd::cout << \"propagator::selectImuReadings(): No IMU measurements to propagate with (\" << (int)prop_data.size() << \" of 2). IMU-CAMERA are likely messed up\" << std::endl;\n\n\t\treturn prop_data;\n\t}\n\n\treturn prop_data;\n}\n void propagateAndClone(std::shared_ptr state_ptr, double timestamp) {\n\tif (state_ptr->timestamp == timestamp)\n\t{\n\t\tstd::cout << \"propagator::propagateAndClone(): Propagation called again at same timestep at last update timestep!\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tif (state_ptr->timestamp > timestamp)\n\t{\n\t\tstd::cout << \"propagator::propagateAndClone(): Propagation called trying to propagate backwards in time!\" << std::endl;\n\t\tstd::cout << std::fixed << \"propagator::propagateAndClone(): desired propagation = \" << timestamp - state_ptr->timestamp << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tif (!have_last_prop_time_offset)\n\t{\n\t\tlast_prop_time_offset = state_ptr->calib_dt_imu_cam->value()(0);\n\t\thave_last_prop_time_offset = true;\n\t}\n\n\tdouble t_off_new = state_ptr->calib_dt_imu_cam->value()(0);\n\n\tdouble time_0 = state_ptr->timestamp + last_prop_time_offset;\n\tdouble time_1 = timestamp + t_off_new;\n\tstd::vector prop_data;\n\t{\n\t\tprop_data = propagator::selectImuReadings(v_imu_data, time_0, time_1);\n\t}\n\n\tEigen::MatrixXd phi_sum = Eigen::MatrixXd::Identity(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tEigen::MatrixXd Qd_sum = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tdouble dt_sum = 0;\n\n\tif (prop_data.size() > 1)\n\t{\n\t\tfor (size_t i = 0; i < prop_data.size() - 1; i++)\n\t\t{\n\t\t\tEigen::MatrixXd F, Qdi;\n\t\t\tpredictAndCompute(state_ptr, prop_data.at(i), prop_data.at(i + 1), F, Qdi);\n\n\t\t\tphi_sum = F * phi_sum;\n\t\t\tQd_sum = F * Qd_sum * F.transpose() + Qdi;\n\t\t\tQd_sum = 0.5 * (Qd_sum + Qd_sum.transpose());\n\t\t\tdt_sum += prop_data.at(i + 1).timestamp - prop_data.at(i).timestamp;\n\t\t}\n\t}\n\tassert(std::abs((time_1 - time_0) - dt_sum) < 1e-4);\n\n\tEigen::Vector3d last_acc = Eigen::Vector3d::Zero();\n\tEigen::Vector3d last_gyr = Eigen::Vector3d::Zero();\n\tif (!prop_data.empty())\n\t{\n\t\tEigen::Matrix3d Dw = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_dw->value());\n\t\tEigen::Matrix3d Da = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_da->value());\n\t\tEigen::Matrix3d Tg = state::Tg(state_ptr->calib_imu_tg->value());\n\t\tlast_acc = state_ptr->calib_imu_acc->getRot() * Da * (prop_data.at(prop_data.size() - 1).acc - state_ptr->imu_ptr->getBa());\n\t\tlast_gyr = state_ptr->calib_imu_gyr->getRot() * Dw * (prop_data.at(prop_data.size() - 1).gyr - state_ptr->imu_ptr->getBg() - Tg * last_acc);\n\t}\n\n\tstd::vector> phi_order;\n\tphi_order.push_back(state_ptr->imu_ptr);\n\t\n\tif (state_ptr->options.do_calib_imu_intrinsics)\n\t{\n\t\tphi_order.push_back(state_ptr->calib_imu_dw);\n\t\tphi_order.push_back(state_ptr->calib_imu_da);\n\n\t\tif (state_ptr->options.do_calib_imu_g_sensitivity)\n\t\t{\n\t\t\tphi_order.push_back(state_ptr->calib_imu_tg);\n\t\t}\n\n\t\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\t{\n\t\t\tphi_order.push_back(state_ptr->calib_imu_gyr);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tphi_order.push_back(state_ptr->calib_imu_acc);\n\t\t}\n\t}\n\n\tstateHelper::ekfPropagation(state_ptr, phi_order, phi_order, phi_sum, Qd_sum);\n\n\tstate_ptr->timestamp = timestamp;\n\tlast_prop_time_offset = t_off_new;\n\n\tstateHelper::augmentClone(state_ptr, last_gyr);\n}\n void predictAndCompute(std::shared_ptr state_ptr, const imuData &data_minus, const imuData &data_plus, \n Eigen::MatrixXd &F, Eigen::MatrixXd &Qd) {\n\tdouble dt = data_plus.timestamp - data_minus.timestamp;\n\n\tEigen::Matrix3d Dw = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_dw->value());\n\tEigen::Matrix3d Da = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_da->value());\n\tEigen::Matrix3d Tg = state::Tg(state_ptr->calib_imu_tg->value());\n\n\tEigen::Vector3d a_hat1 = data_minus.acc - state_ptr->imu_ptr->getBa();\n\tEigen::Vector3d a_hat2 = data_plus.acc - state_ptr->imu_ptr->getBa();\n\tEigen::Vector3d a_hat_avg = 0.5 * (a_hat1 + a_hat2);\n\n\tEigen::Vector3d a_uncorrected = a_hat_avg;\n\tEigen::Matrix3d R_imu_acc = state_ptr->calib_imu_acc->getRot();\n\ta_hat1 = R_imu_acc * Da * a_hat1;\n\ta_hat2 = R_imu_acc * Da * a_hat2;\n\ta_hat_avg = R_imu_acc * Da * a_hat_avg;\n\n\tEigen::Vector3d w_hat1 = data_minus.gyr - state_ptr->imu_ptr->getBg() - Tg * a_hat1;\n\tEigen::Vector3d w_hat2 = data_plus.gyr - state_ptr->imu_ptr->getBg() - Tg * a_hat2;\n\tEigen::Vector3d w_hat_avg = 0.5 * (w_hat1 + w_hat2);\n\n\tEigen::Vector3d w_uncorrected = w_hat_avg;\n\tEigen::Matrix3d R_imu_gyr = state_ptr->calib_imu_gyr->getRot();\n\tw_hat1 = R_imu_gyr * Dw * w_hat1;\n\tw_hat2 = R_imu_gyr * Dw * w_hat2;\n\tw_hat_avg = R_imu_gyr * Dw * w_hat_avg;\n\n\tEigen::Matrix Xi_sum = Eigen::Matrix::Zero(3, 18);\n\tcomputeXiSum(state_ptr, dt, w_hat_avg, a_hat_avg, Xi_sum);\n\n\tEigen::Vector4d new_q;\n\tEigen::Vector3d new_v, new_p;\n\tpredictMeanRk4(state_ptr, dt, w_hat1, a_hat1, w_hat2, a_hat2, new_q, new_v, new_p);\n\n\tF = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tEigen::MatrixXd G = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, 12);\n\tcomputeCoefficientAnalytic(state_ptr, dt, w_hat_avg, a_hat_avg, w_uncorrected, a_uncorrected, new_q, new_v, new_p, Xi_sum, F, G);\n\n\tEigen::Matrix Qc = Eigen::Matrix::Zero();\n\tQc.block(0, 0, 3, 3) = std::pow(noises.sigma_w, 2) / dt * Eigen::Matrix3d::Identity();\n\tQc.block(3, 3, 3, 3) = std::pow(noises.sigma_a, 2) / dt * Eigen::Matrix3d::Identity();\n\tQc.block(6, 6, 3, 3) = std::pow(noises.sigma_wb, 2) / dt * Eigen::Matrix3d::Identity();\n\tQc.block(9, 9, 3, 3) = std::pow(noises.sigma_ab, 2) / dt * Eigen::Matrix3d::Identity();\n\n\tQd = Eigen::MatrixXd::Zero(state_ptr->imuIntrinsicSize() + 15, state_ptr->imuIntrinsicSize() + 15);\n\tQd = G * Qc * G.transpose();\n\tQd = 0.5 * (Qd + Qd.transpose());\n\n\tEigen::Matrix imu_x = state_ptr->imu_ptr->value();\n\timu_x.block(0, 0, 4, 1) = new_q;\n\timu_x.block(4, 0, 3, 1) = new_p;\n\timu_x.block(7, 0, 3, 1) = new_v;\n\tstate_ptr->imu_ptr->setValue(imu_x);\n\tstate_ptr->imu_ptr->setFej(imu_x);\n}\n static Eigen::MatrixXd computeDwH(std::shared_ptr state_ptr, const Eigen::Vector3d &gyr_uncorrected) {\n\tEigen::Vector3d e_1 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 0);\n\tEigen::Vector3d e_2 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 1);\n\tEigen::Vector3d e_3 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 2);\n\n\tdouble w_1 = gyr_uncorrected(0);\n\tdouble w_2 = gyr_uncorrected(1);\n\tdouble w_3 = gyr_uncorrected(2);\n\tassert(state_ptr->options.do_calib_imu_intrinsics);\n\n\tEigen::MatrixXd H_Dw = Eigen::MatrixXd::Zero(3, 6);\n\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\tH_Dw << w_1 * Eigen::MatrixXd::Identity(3, 3), w_2 * e_2, w_2 * e_3, w_3 * e_3;\n\telse\n\t\tH_Dw << w_1 * e_1, w_2 * e_1, w_2 * e_2, w_3 * Eigen::MatrixXd::Identity(3, 3);\n\n\treturn H_Dw;\n}\n static Eigen::MatrixXd computeDaH(std::shared_ptr state_ptr, const Eigen::Vector3d &acc_uncorrected) {\n\tEigen::Vector3d e_1 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 0);\n\tEigen::Vector3d e_2 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 1);\n\tEigen::Vector3d e_3 = Eigen::MatrixXd::Identity(3, 3).block<3, 1>(0, 2);\n\n\tdouble a_1 = acc_uncorrected(0);\n\tdouble a_2 = acc_uncorrected(1);\n\tdouble a_3 = acc_uncorrected(2);\n\tassert(state_ptr->options.do_calib_imu_intrinsics);\n\n\tEigen::MatrixXd H_Da = Eigen::MatrixXd::Zero(3, 6);\n\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\tH_Da << a_1 * Eigen::MatrixXd::Identity(3, 3), a_2 * e_2, a_2 * e_3, a_3 * e_3;\n\telse\n\t\tH_Da << a_1 * e_1, a_2 * e_1, a_2 * e_2, a_3 * Eigen::MatrixXd::Identity(3, 3);\n\n\treturn H_Da;\n}\n static Eigen::MatrixXd computeTgH(std::shared_ptr state_ptr, const Eigen::Vector3d &acc) {\n\tdouble a_1 = acc(0);\n\tdouble a_2 = acc(1);\n\tdouble a_3 = acc(2);\n\tassert(state_ptr->options.do_calib_imu_intrinsics);\n\tassert(state_ptr->options.do_calib_imu_g_sensitivity);\n\n\tEigen::MatrixXd H_Tg = Eigen::MatrixXd::Zero(3, 9);\n\tH_Tg << a_1 * Eigen::MatrixXd::Identity(3, 3), a_2 * Eigen::MatrixXd::Identity(3, 3), a_3 * Eigen::MatrixXd::Identity(3, 3);\n\n\treturn H_Tg;\n}\n bool have_last_prop_time_offset;\n double last_prop_time_offset;\n protected:\n void predictMeanRk4(std::shared_ptr state_ptr, double dt, const Eigen::Vector3d &un_gyr_0, const Eigen::Vector3d &un_acc_0, \n const Eigen::Vector3d &un_gyr_1, const Eigen::Vector3d &un_acc_1, Eigen::Vector4d &new_q, Eigen::Vector3d &new_v, Eigen::Vector3d &new_p) {\n\tEigen::Vector3d un_gyr = un_gyr_0;\n\tEigen::Vector3d un_acc = un_acc_0;\n\tEigen::Vector3d w_alpha = (un_gyr_1 - un_gyr_0) / dt;\n\tEigen::Vector3d a_jerk = (un_acc_1 - un_acc_0) / dt;\n\n\tEigen::Vector4d q_0 = state_ptr->imu_ptr->getQuat();\n\tEigen::Vector3d p_0 = state_ptr->imu_ptr->getPos();\n\tEigen::Vector3d v_0 = state_ptr->imu_ptr->getVel();\n\n\tEigen::Vector4d dq_0 = {0, 0, 0, 1};\n\tEigen::Vector4d q_0_dot = 0.5 * quatType::omega(un_gyr) * dq_0;\n\tEigen::Vector3d p_0_dot = v_0;\n\tEigen::Matrix3d R_0_world = quatType::quatToRot(quatType::quatMultiply(dq_0, q_0));\n\tEigen::Vector3d v_0_dot = R_0_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k1_q = q_0_dot * dt;\n\tEigen::Vector3d k1_p = p_0_dot * dt;\n\tEigen::Vector3d k1_v = v_0_dot * dt;\n\n\tun_gyr += 0.5 * w_alpha * dt;\n\tun_acc += 0.5 * a_jerk * dt;\n\n\tEigen::Vector4d dq_1 = quatType::quatNorm(dq_0 + 0.5 * k1_q);\n\tEigen::Vector3d v_1 = v_0 + 0.5 * k1_v;\n\tEigen::Vector4d q_1_dot = 0.5 * quatType::omega(un_gyr) * dq_1;\n\tEigen::Vector3d p_1_dot = v_1;\n\tEigen::Matrix3d R_1_world = quatType::quatToRot(quatType::quatMultiply(dq_1, q_0));\n\tEigen::Vector3d v_1_dot = R_1_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k2_q = q_1_dot * dt;\n\tEigen::Vector3d k2_p = p_1_dot * dt;\n\tEigen::Vector3d k2_v = v_1_dot * dt;\n\n\tEigen::Vector4d dq_2 = quatType::quatNorm(dq_0 + 0.5 * k2_q);\n\tEigen::Vector3d v_2 = v_0 + 0.5 * k2_v;\n\n\tEigen::Vector4d q_2_dot = 0.5 * quatType::omega(un_gyr) * dq_2;\n\tEigen::Vector3d p_2_dot = v_2;\n\tEigen::Matrix3d R_2_world = quatType::quatToRot(quatType::quatMultiply(dq_2, q_0));\n\tEigen::Vector3d v_2_dot = R_2_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k3_q = q_2_dot * dt;\n\tEigen::Vector3d k3_p = p_2_dot * dt;\n\tEigen::Vector3d k3_v = v_2_dot * dt;\n\n\tun_gyr += 0.5 * w_alpha * dt;\n\tun_acc += 0.5 * a_jerk * dt;\n\n\tEigen::Vector4d dq_3 = quatType::quatNorm(dq_0 + k3_q);\n\tEigen::Vector3d v_3 = v_0 + k3_v;\n\n\tEigen::Vector4d q_3_dot = 0.5 * quatType::omega(un_gyr) * dq_3;\n\tEigen::Vector3d p_3_dot = v_3;\n\tEigen::Matrix3d R_3_world = quatType::quatToRot(quatType::quatMultiply(dq_3, q_0));\n\tEigen::Vector3d v_3_dot = R_3_world.transpose() * un_acc - gravity;\n\n\tEigen::Vector4d k4_q = q_3_dot * dt;\n\tEigen::Vector3d k4_p = p_3_dot * dt;\n\tEigen::Vector3d k4_v = v_3_dot * dt;\n\n\tEigen::Vector4d dq = quatType::quatNorm(dq_0 + (1.0 / 6.0) * k1_q + (1.0 / 3.0) * k2_q + (1.0 / 3.0) * k3_q + (1.0 / 6.0) * k4_q);\n\tnew_q = quatType::quatMultiply(dq, q_0);\n\tnew_p = p_0 + (1.0 / 6.0) * k1_p + (1.0 / 3.0) * k2_p + (1.0 / 3.0) * k3_p + (1.0 / 6.0) * k4_p;\n\tnew_v = v_0 + (1.0 / 6.0) * k1_v + (1.0 / 3.0) * k2_v + (1.0 / 3.0) * k3_v + (1.0 / 6.0) * k4_v;\n}\n void computeXiSum(std::shared_ptr state_ptr, double dt, const Eigen::Vector3d &un_gyr, \n const Eigen::Vector3d &un_acc, Eigen::Matrix &Xi_sum) {\n\tdouble un_gyr_norm = un_gyr.norm();\n\tdouble d_th = un_gyr_norm * dt;\n\tEigen::Vector3d un_gyr_normalized = Eigen::Vector3d::Zero();\n\n\tif (un_gyr_norm > 1e-12)\n\t\tun_gyr_normalized = un_gyr / un_gyr_norm;\n\n\tdouble dt_2 = std::pow(dt, 2);\n\tdouble dt_3 = std::pow(dt, 3);\n\tdouble norm_2 = std::pow(un_gyr_norm, 2);\n\tdouble norm_3 = std::pow(un_gyr_norm, 3);\n\tdouble cos_dth = std::cos(d_th);\n\tdouble sin_dth = std::sin(d_th);\n\tdouble d_th_2 = std::pow(d_th, 2);\n\tdouble d_th_3 = std::pow(d_th, 3);\n\n\tEigen::Matrix3d sK = quatType::skewSymmetric(un_gyr_normalized);\n\tEigen::Matrix3d sK2 = sK * sK;\n\tEigen::Matrix3d sA = quatType::skewSymmetric(un_acc);\n\n\tEigen::Matrix3d R_temp = quatType::expSo3(-un_gyr * dt);\n\tEigen::Matrix3d Jr_temp = quatType::JrighySo3(-un_gyr * dt);\n\n\tEigen::Matrix3d Xi_1, Xi_2, Xi_3, Xi_4;\n\n \tif (!(un_gyr_norm < 1.0 / 180 * M_PI / 2))\n \t{\n\t\tXi_1 = Eigen::Matrix3d::Identity() * dt + (1.0 - cos_dth) / un_gyr_norm * sK + (dt - sin_dth / un_gyr_norm) * sK2;\n\n\t\tXi_2 = 1.0 / 2 * dt_2 * Eigen::Matrix3d::Identity() + (d_th - sin_dth) / norm_2 * sK + (1.0 / 2 * dt_2 - (1.0 - cos_dth) / norm_2) * sK2;\n\n\t\tXi_3 = 1.0 / 2 * dt_2 * sA + (sin_dth - d_th) / norm_2 * sA * sK + (sin_dth - d_th * cos_dth) / norm_2 * sK * sA +\n\t\t\t(1.0 / 2 * dt_2 - (1.0 - cos_dth) / norm_2) * sA * sK2 +\n\t\t\t(1.0 / 2 * dt_2 + (1.0 - cos_dth - d_th * sin_dth) / norm_2) * (sK2 * sA + un_gyr_normalized.dot(un_acc) * sK) -\n\t\t\t(3 * sin_dth - 2 * d_th - d_th * cos_dth) / norm_2 * un_gyr_normalized.dot(un_acc) * sK2;\n\n\t\tXi_4 = 1.0 / 6 * dt_3 * sA + (2 * (1.0 - cos_dth) - d_th_2) / (2 * norm_3) * sA * sK +\n\t\t\t((2 * (1.0 - cos_dth) - d_th * sin_dth) / norm_3) * sK * sA + ((sin_dth - d_th) / norm_3 + dt_3 / 6) * sA * sK2 +\n\t\t\t((d_th - 2 * sin_dth + 1.0 / 6 * d_th_3 + d_th * cos_dth) / norm_3) * (sK2 * sA + un_gyr_normalized.dot(un_acc) * sK) +\n\t\t\t(4 * cos_dth - 4 + d_th_2 + d_th * sin_dth) / norm_3 * un_gyr_normalized.dot(un_acc) * sK2;\n\n\t}\n\telse\n\t{\n\t\tXi_1 = dt * (Eigen::Matrix3d::Identity() + sin_dth * sK + (1.0 - cos_dth) * sK2);\n\n\t\tXi_2 = 1.0 / 2 * dt * Xi_1;\n\n\t\tXi_3 = 1.0 / 2 * dt_2 * (sA + sin_dth * (-sA * sK + sK * sA + un_gyr_normalized.dot(un_acc) * sK2) \n\t\t\t+ (1.0 - cos_dth) * (sA * sK2 + sK2 * sA + un_gyr_normalized.dot(un_acc) * sK));\n\n\t\tXi_4 = 1.0 / 3 * dt * Xi_3;\n\t}\n\n\tXi_sum.setZero();\n\tXi_sum.block<3, 3>(0, 0) = R_temp;\n\tXi_sum.block<3, 3>(0, 3) = Xi_1;\n\tXi_sum.block<3, 3>(0, 6) = Xi_2;\n\tXi_sum.block<3, 3>(0, 9) = Jr_temp;\n\tXi_sum.block<3, 3>(0, 12) = Xi_3;\n\tXi_sum.block<3, 3>(0, 15) = Xi_4;\n}\n void computeCoefficientAnalytic(std::shared_ptr state_ptr, double dt, const Eigen::Vector3d &un_gyr, const Eigen::Vector3d &un_acc, \n const Eigen::Vector3d &gyr_uncorrected, const Eigen::Vector3d &acc_uncorrected, const Eigen::Vector4d &new_q, const Eigen::Vector3d &new_v, \n const Eigen::Vector3d &new_p, const Eigen::Matrix &Xi_sum, Eigen::MatrixXd &F, Eigen::MatrixXd &G) {\n\tint local_size = 0;\n\tint th_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->q()->getSize();\n\tint p_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->p()->getSize();\n\tint v_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->v()->getSize();\n\tint bg_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->bg()->getSize();\n\tint ba_id = local_size;\n\tlocal_size += state_ptr->imu_ptr->ba()->getSize();\n\n\tint Dw_id = -1;\n\tint Da_id = -1;\n\tint Tg_id = -1;\n\tint th_imu_acc_id = -1;\n\tint th_imu_gyr_id = -1;\n\n\tif (state_ptr->options.do_calib_imu_intrinsics)\n\t{\n\t\tDw_id = local_size;\n\t\tlocal_size += state_ptr->calib_imu_dw->getSize();\n\t\tDa_id = local_size;\n\t\tlocal_size += state_ptr->calib_imu_da->getSize();\n\n\t\tif (state_ptr->options.do_calib_imu_g_sensitivity)\n\t\t{\n\t\t\tTg_id = local_size;\n\t\t\tlocal_size += state_ptr->calib_imu_tg->getSize();\n\t\t}\n\n\t\tif (state_ptr->options.imu_model == ImuModel::KALIBR)\n\t\t{\n\t\t\tth_imu_gyr_id = local_size;\n\t\t\tlocal_size += state_ptr->calib_imu_gyr->getSize();\n\t\t}\n\t\telse\n\t\t{\n\t\t\tth_imu_acc_id = local_size;\n\t\t\tlocal_size += state_ptr->calib_imu_acc->getSize();\n\t\t}\n\t}\n\n\tEigen::Matrix3d R_k = state_ptr->imu_ptr->getRot();\n\tEigen::Vector3d v_k = state_ptr->imu_ptr->getVel();\n\tEigen::Vector3d p_k = state_ptr->imu_ptr->getPos();\n\n\tif (state_ptr->options.do_fej)\n\t{\n\t\tR_k = state_ptr->imu_ptr->getRotFej();\n\t\tv_k = state_ptr->imu_ptr->getVelFej();\n\t\tp_k = state_ptr->imu_ptr->getPosFej();\n\t}\n\n\tEigen::Matrix3d dR_temp = quatType::quatToRot(new_q) * R_k.transpose();\n\n\tEigen::Matrix3d Dw = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_dw->value());\n\tEigen::Matrix3d Da = state::Dm(state_ptr->options.imu_model, state_ptr->calib_imu_da->value());\n\tEigen::Matrix3d Tg = state::Tg(state_ptr->calib_imu_tg->value());\n\tEigen::Matrix3d R_imu_acc = state_ptr->calib_imu_acc->getRot();\n\tEigen::Matrix3d R_imu_gyr = state_ptr->calib_imu_gyr->getRot();\n\tEigen::Vector3d acc_k = R_imu_acc * Da * acc_uncorrected;\n\tEigen::Vector3d gyr_k = R_imu_gyr * Dw * gyr_uncorrected;\n\n\tEigen::Matrix3d Xi_1 = Xi_sum.block<3, 3>(0, 3);\n\tEigen::Matrix3d Xi_2 = Xi_sum.block<3, 3>(0, 6);\n\tEigen::Matrix3d Jr_temp = Xi_sum.block<3, 3>(0, 9);\n\tEigen::Matrix3d Xi_3 = Xi_sum.block<3, 3>(0, 12);\n\tEigen::Matrix3d Xi_4 = Xi_sum.block<3, 3>(0, 15);\n\n\tF.block<3, 3>(th_id, th_id) = dR_temp;\n\tF.block<3, 3>(p_id, th_id) = - quatType::skewSymmetric(new_p - p_k - v_k * dt + 0.5 * gravity * dt * dt) * R_k.transpose();\n\tF.block<3, 3>(v_id, th_id) = - quatType::skewSymmetric(new_v - v_k + gravity * dt) * R_k.transpose();\n\n\tF.block<3, 3>(p_id, p_id).setIdentity();\n\n\tF.block<3, 3>(p_id, v_id) = Eigen::Matrix3d::Identity() * dt;\n\tF.block<3, 3>(v_id, v_id).setIdentity();\n\n\tF.block<3, 3>(th_id, bg_id) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw;\n\tF.block<3, 3>(p_id, bg_id) = R_k.transpose() * Xi_4 * R_imu_gyr * Dw;\n\tF.block<3, 3>(v_id, bg_id) = R_k.transpose() * Xi_3 * R_imu_gyr * Dw;\n\tF.block<3, 3>(bg_id, bg_id).setIdentity();\n\n\tF.block<3, 3>(th_id, ba_id) = dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * R_imu_acc * Da;\n\tF.block<3, 3>(p_id, ba_id) = - R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tF.block<3, 3>(v_id, ba_id) = - R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tF.block<3, 3>(ba_id, ba_id).setIdentity();\n\n\tif (Dw_id != -1)\n\t{\n\t\tEigen::MatrixXd H_Dw = computeDwH(state_ptr, gyr_uncorrected);\n\t\tF.block(th_id, Dw_id, 3, state_ptr->calib_imu_dw->getSize()) = dR_temp * Jr_temp * dt * R_imu_gyr * H_Dw;\n\t\tF.block(p_id, Dw_id, 3, state_ptr->calib_imu_dw->getSize()) = - R_k.transpose() * Xi_4 * R_imu_gyr * H_Dw;\n\t\tF.block(v_id, Dw_id, 3, state_ptr->calib_imu_dw->getSize()) = - R_k.transpose() * Xi_3 * R_imu_gyr * H_Dw;\n\t\tF.block(Dw_id, Dw_id, state_ptr->calib_imu_dw->getSize(), state_ptr->calib_imu_dw->getSize()).setIdentity();\n\t}\n\n\tif (Da_id != -1)\n\t{\n\t\tEigen::MatrixXd H_Da = computeDaH(state_ptr, acc_uncorrected);\n\t\tF.block(th_id, Da_id, 3, state_ptr->calib_imu_da->getSize()) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * R_imu_acc * H_Da;\n\t\tF.block(p_id, Da_id, 3, state_ptr->calib_imu_da->getSize()) = R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * R_imu_acc * H_Da;\n\t\tF.block(v_id, Da_id, 3, state_ptr->calib_imu_da->getSize()) = R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * R_imu_acc * H_Da;\n\t\tF.block(Da_id, Da_id, state_ptr->calib_imu_da->getSize(), state_ptr->calib_imu_da->getSize()).setIdentity();\n\t}\n\n\tif (Tg_id != -1)\n\t{\n\t\tEigen::MatrixXd H_Tg = computeTgH(state_ptr, acc_k);\n\t\tF.block(th_id, Tg_id, 3, state_ptr->calib_imu_tg->getSize()) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw * H_Tg;\n\t\tF.block(p_id, Tg_id, 3, state_ptr->calib_imu_tg->getSize()) = R_k.transpose() * Xi_4 * R_imu_gyr * Dw * H_Tg;\n\t\tF.block(v_id, Tg_id, 3, state_ptr->calib_imu_tg->getSize()) = R_k.transpose() * Xi_3 * R_imu_gyr * Dw * H_Tg;\n\t\tF.block(Tg_id, Tg_id, state_ptr->calib_imu_tg->getSize(), state_ptr->calib_imu_tg->getSize()).setIdentity();\n\t}\n\n\tif (th_imu_acc_id != -1)\n\t{\n\t\tF.block<3, 3>(th_id, th_imu_acc_id) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * quatType::skewSymmetric(acc_k);\n\t\tF.block<3, 3>(p_id, th_imu_acc_id) = R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * quatType::skewSymmetric(acc_k);\n\t\tF.block<3, 3>(v_id, th_imu_acc_id) = R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * quatType::skewSymmetric(acc_k);\n\t\tF.block<3, 3>(th_imu_acc_id, th_imu_acc_id).setIdentity();\n\t}\n\n\tif (th_imu_gyr_id != -1)\n\t{\n\t\tF.block<3, 3>(th_id, th_imu_gyr_id) = dR_temp * Jr_temp * dt * quatType::skewSymmetric(gyr_k);\n\t\tF.block<3, 3>(p_id, th_imu_gyr_id) = -R_k.transpose() * Xi_4 * quatType::skewSymmetric(gyr_k);\n\t\tF.block<3, 3>(v_id, th_imu_gyr_id) = -R_k.transpose() * Xi_3 * quatType::skewSymmetric(gyr_k);\n\t\tF.block<3, 3>(th_imu_gyr_id, th_imu_gyr_id).setIdentity();\n\t}\n\n\tG.block<3, 3>(th_id, 0) = - dR_temp * Jr_temp * dt * R_imu_gyr * Dw;\n\tG.block<3, 3>(p_id, 0) = R_k.transpose() * Xi_4 * R_imu_gyr * Dw;\n\tG.block<3, 3>(v_id, 0) = R_k.transpose() * Xi_3 * R_imu_gyr * Dw;\n\tG.block<3, 3>(th_id, 3) = dR_temp * Jr_temp * dt * R_imu_gyr * Dw * Tg * R_imu_acc * Da;\n\tG.block<3, 3>(p_id, 3) = -R_k.transpose() * (Xi_2 + Xi_4 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tG.block<3, 3>(v_id, 3) = -R_k.transpose() * (Xi_1 + Xi_3 * R_imu_gyr * Dw * Tg) * R_imu_acc * Da;\n\tG.block<3, 3>(bg_id, 6) = dt * Eigen::Matrix3d::Identity();\n\tG.block<3, 3>(ba_id, 9) = dt * Eigen::Matrix3d::Identity();\n}\n noiseManager noises;\n std::vector v_imu_data;\n Eigen::Vector3d gravity;\n std::atomic cache_imu_valid;\n double cache_state_time;\n Eigen::MatrixXd cache_state_est;\n Eigen::MatrixXd cache_state_covariance;\n double cache_t_off;\n public:\n updaterMsckf(updaterOptions &options_, featureInitializerOptions &feature_init_options_) {\n\toptions.sigma_pix_sq = std::pow(options.sigma_pix, 2);\n\n\tinitializer_feature = std::shared_ptr(new featureInitializer(feature_init_options_));\n\n\tfor (int i = 1; i < 500; i++)\n\t{\n\t\tboost::math::chi_squared chi_squared_dist(i);\n\t\tchi_squared_table[i] = boost::math::quantile(chi_squared_dist, 0.95);\n\t}\n}\n void update(std::shared_ptr state_ptr, std::vector> &feature_vec) {\n\tif (feature_vec.empty())\n\t\treturn;\n\n\tboost::posix_time::ptime rT0, rT1, rT2, rT3;\n\trT0 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::vector clone_times;\n\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t{\n\t\tclone_times.emplace_back(clone_imu.first);\n\t}\n\n\tauto it0 = feature_vec.begin();\n\twhile (it0 != feature_vec.end())\n\t{\n\t\t(*it0)->cleanOldMeasurements(clone_times);\n\n\t\tint count_meas = 0;\n\t\tfor (const auto &pair : (*it0)->timestamps)\n\t\t{\n\t\t\tcount_meas += (*it0)->timestamps[pair.first].size();\n\t\t}\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it0)->feature_id);\n\t\tint required_meas = 2;\n\n\t\tif (count_meas < 1)\n\t\t{\n\t\t\t(*it0)->to_delete = true;\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse if (count_meas < required_meas)\n\t\t{\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n\trT1 = boost::posix_time::microsec_clock::local_time();\n\n\tsize_t max_meas_size = 0;\n\tfor (size_t i = 0; i < feature_vec.size(); i++)\n\t{\n\t\tfor (const auto &pair : feature_vec.at(i)->timestamps)\n\t\t{\n\t\t\tmax_meas_size += 2 * feature_vec.at(i)->timestamps[pair.first].size();\n\t\t}\n\t}\n\n\tsize_t max_hx_size = state_ptr->maxCovSize();\n\n\tEigen::VectorXd res_big = Eigen::VectorXd::Zero(max_meas_size);\n\tEigen::MatrixXd Hx_big = Eigen::MatrixXd::Zero(max_meas_size, max_hx_size);\n\tEigen::MatrixXd R_big = Eigen::MatrixXd::Identity(max_meas_size, max_meas_size);\n\tstd::unordered_map, size_t> Hx_mapping;\n\tstd::vector> Hx_order_big;\n\tsize_t count_jacob = 0;\n\tsize_t count_meas = 0;\n\n\tauto it2 = feature_vec.begin();\n\twhile (it2 != feature_vec.end())\n\t{\n\t\tassert(state_ptr->map_points.find((*it2)->feature_id) != state_ptr->map_points.end());\n\t\tassert(state_ptr->map_points.at((*it2)->feature_id)->feature_id == (*it2)->feature_id);\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it2)->feature_id);\n\n\t\tupdaterHelper::updaterHelperFeature feat;\n\t\tfeat.feature_id = (*it2)->feature_id;\n\t\tfeat.uvs = (*it2)->uvs;\n\t\tfeat.uvs_norm = (*it2)->uvs_norm;\n\t\tfeat.timestamps = (*it2)->timestamps;\n\t\tfeat.frames = (*it2)->frames;\n\t\tfeat.color = (*it2)->color;\n\n\t\tfeat.position_global = map_point->getPointXYZ(false);\n\t\tfeat.position_global_fej = map_point->getPointXYZ(true);\n\n\t\tEigen::MatrixXd H_f;\n\t\tEigen::MatrixXd H_x;\n\t\tEigen::VectorXd res;\n\t\tstd::vector> Hx_order;\n\n\t\tupdaterHelper::getFeatureJacobianFull(state_ptr, feat, H_f, H_x, res, Hx_order);\n\n\t\tEigen::MatrixXd H_xf = H_x;\n\n\t\tH_xf.conservativeResize(H_x.rows(), H_x.cols() + H_f.cols());\n\t\tH_xf.block(0, H_x.cols(), H_x.rows(), H_f.cols()) = H_f;\n\n\t\tstd::vector> Hxf_order = Hx_order;\n\t\tHxf_order.push_back(map_point);\n\n\t\tEigen::MatrixXd P_marg = stateHelper::getMarginalCovariance(state_ptr, Hxf_order);\n\t\tEigen::MatrixXd S = H_xf * P_marg * H_xf.transpose();\n\t\tdouble sigma_pix_sq = options_slam.sigma_pix_sq;\n\t\tS.diagonal() += sigma_pix_sq * Eigen::VectorXd::Ones(S.rows());\n\t\tdouble chi2 = res.dot(S.llt().solve(res));\n\n\t\tdouble chi2_check;\n\t\tif (res.rows() < 500)\n\t\t{\n\t\t\tchi2_check = chi_squared_table[res.rows()];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tboost::math::chi_squared chi_squared_dist(res.rows());\n\t\t\tchi2_check = boost::math::quantile(chi_squared_dist, 0.95);\n\t\t\tstd::cout << \"chi2_check over the residual limit - \" << (int)res.rows() << std::endl;\n\t\t}\n\n\t\tdouble chi2_multipler = options_slam.chi2_multipler;\n\t\t\n\t\tif (chi2 > chi2_multipler * chi2_check)\n\t\t{\n\t\t\tmap_point->update_fail_count++;\n\n\t\t\t(*it2)->to_delete = true;\n\t\t\tit2 = feature_vec.erase(it2);\n\t\t\tcontinue;\n\t\t}\n\n\t\tsize_t count_hx = 0;\n\t\tfor (const auto &var : Hxf_order)\n\t\t{\n\t\t\tif (Hx_mapping.find(var) == Hx_mapping.end())\n\t\t\t{\n\t\t\t\tHx_mapping.insert({var, count_jacob});\n\t\t\t\tHx_order_big.push_back(var);\n\t\t\t\tcount_jacob += var->getSize();\n\t\t\t}\n\n\t\t\tHx_big.block(count_meas, Hx_mapping[var], H_xf.rows(), var->getSize()) = H_xf.block(0, count_hx, H_xf.rows(), var->getSize());\n\t\t\tcount_hx += var->getSize();\n\t\t}\n\n\t\tR_big.block(count_meas, count_meas, res.rows(), res.rows()) *= sigma_pix_sq;\n\n\t\tres_big.block(count_meas, 0, res.rows(), 1) = res;\n\t\tcount_meas += res.rows();\n\t\tit2++;\n\t}\n\trT2 = boost::posix_time::microsec_clock::local_time();\n\n\tfor (size_t f = 0; f < feature_vec.size(); f++)\n\t{\n\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).back() > clone_times.at(clone_times.size() - 1) + 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).back() > clone_times.at(clone_times.size() - 1) + 1e-5))\n\t\t{\n\t\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).front() < clone_times.at(clone_times.size() - 3) - 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).front() < clone_times.at(clone_times.size() - 3) - 1e-5))\n\t\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t}\n\n\tif (count_meas < 1)\n\t{\n\t\treturn;\n\t}\n\tassert(count_meas <= max_meas_size);\n\tassert(count_jacob <= max_hx_size);\n\tres_big.conservativeResize(count_meas, 1);\n\tHx_big.conservativeResize(count_meas, count_jacob);\n\tR_big.conservativeResize(count_meas, count_meas);\n\n\tstateHelper::ekfUpdate(state_ptr, Hx_order_big, Hx_big, res_big, R_big);\n\trT3 = boost::posix_time::microsec_clock::local_time();\n\n\t// Time test\n\t/*\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT1 - rT0).total_microseconds() * 1e-6 << \" seconds to clean.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds creating linear system.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds to update (\" \n\t\t<< (int)feature_vec.size() << \" feats of \" << (int)Hx_big.rows() << \" size).\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT1).total_microseconds() * 1e-6 << \" seconds total.\" << std::endl;\n\t*/\n\t// Time test\n}\n protected:\n updaterOptions options;\n std::shared_ptr initializer_feature;\n std::map chi_squared_table;\n public:\n updaterSlam(updaterOptions &options_slam_, featureInitializerOptions &feat_init_options_, std::shared_ptr database_) {\n\toptions_slam.sigma_pix_sq = std::pow(options_slam.sigma_pix, 2);\n\n\tinitializer_feature = std::shared_ptr(new featureInitializer(feat_init_options_));\n\n\tfor (int i = 1; i < 500; i++)\n\t{\n\t\tboost::math::chi_squared chi_squared_dist(i);\n\t\tchi_squared_table[i] = boost::math::quantile(chi_squared_dist, 0.95);\n\t}\n}\n void update(std::shared_ptr state_ptr, voxelHashMap &voxel_map, std::vector> &feature_vec) {\n\tif (feature_vec.empty())\n\t\treturn;\n\n\tboost::posix_time::ptime rT0, rT1, rT2, rT3;\n\trT0 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::vector clone_times;\n\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t{\n\t\tclone_times.emplace_back(clone_imu.first);\n\t}\n\n\tauto it0 = feature_vec.begin();\n\twhile (it0 != feature_vec.end())\n\t{\n\t\t(*it0)->cleanOldMeasurements(clone_times);\n\n\t\tint count_meas = 0;\n\t\tfor (const auto &pair : (*it0)->timestamps)\n\t\t{\n\t\t\tcount_meas += (*it0)->timestamps[pair.first].size();\n\t\t}\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it0)->feature_id);\n\t\tint required_meas = 2;\n\n\t\tif (count_meas < 1)\n\t\t{\n\t\t\t(*it0)->to_delete = true;\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse if (count_meas < required_meas)\n\t\t{\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n\trT1 = boost::posix_time::microsec_clock::local_time();\n\n\tsize_t max_meas_size = 0;\n\tfor (size_t i = 0; i < feature_vec.size(); i++)\n\t{\n\t\tfor (const auto &pair : feature_vec.at(i)->timestamps)\n\t\t{\n\t\t\tmax_meas_size += 2 * feature_vec.at(i)->timestamps[pair.first].size();\n\t\t}\n\t}\n\n\tsize_t max_hx_size = state_ptr->maxCovSize();\n\n\tEigen::VectorXd res_big = Eigen::VectorXd::Zero(max_meas_size);\n\tEigen::MatrixXd Hx_big = Eigen::MatrixXd::Zero(max_meas_size, max_hx_size);\n\tEigen::MatrixXd R_big = Eigen::MatrixXd::Identity(max_meas_size, max_meas_size);\n\tstd::unordered_map, size_t> Hx_mapping;\n\tstd::vector> Hx_order_big;\n\tsize_t count_jacob = 0;\n\tsize_t count_meas = 0;\n\n\tauto it2 = feature_vec.begin();\n\twhile (it2 != feature_vec.end())\n\t{\n\t\tassert(state_ptr->map_points.find((*it2)->feature_id) != state_ptr->map_points.end());\n\t\tassert(state_ptr->map_points.at((*it2)->feature_id)->feature_id == (*it2)->feature_id);\n\n\t\tstd::shared_ptr map_point = state_ptr->map_points.at((*it2)->feature_id);\n\n\t\tupdaterHelper::updaterHelperFeature feat;\n\t\tfeat.feature_id = (*it2)->feature_id;\n\t\tfeat.uvs = (*it2)->uvs;\n\t\tfeat.uvs_norm = (*it2)->uvs_norm;\n\t\tfeat.timestamps = (*it2)->timestamps;\n\t\tfeat.frames = (*it2)->frames;\n\t\tfeat.color = (*it2)->color;\n\n\t\tfeat.position_global = map_point->getPointXYZ(false);\n\t\tfeat.position_global_fej = map_point->getPointXYZ(true);\n\n\t\tEigen::MatrixXd H_f;\n\t\tEigen::MatrixXd H_x;\n\t\tEigen::VectorXd res;\n\t\tstd::vector> Hx_order;\n\n\t\tupdaterHelper::getFeatureJacobianFull(state_ptr, feat, H_f, H_x, res, Hx_order);\n\n\t\tEigen::MatrixXd H_xf = H_x;\n\n\t\tH_xf.conservativeResize(H_x.rows(), H_x.cols() + H_f.cols());\n\t\tH_xf.block(0, H_x.cols(), H_x.rows(), H_f.cols()) = H_f;\n\n\t\tstd::vector> Hxf_order = Hx_order;\n\t\tHxf_order.push_back(map_point);\n\n\t\tEigen::MatrixXd P_marg = stateHelper::getMarginalCovariance(state_ptr, Hxf_order);\n\t\tEigen::MatrixXd S = H_xf * P_marg * H_xf.transpose();\n\t\tdouble sigma_pix_sq = options_slam.sigma_pix_sq;\n\t\tS.diagonal() += sigma_pix_sq * Eigen::VectorXd::Ones(S.rows());\n\t\tdouble chi2 = res.dot(S.llt().solve(res));\n\n\t\tdouble chi2_check;\n\t\tif (res.rows() < 500)\n\t\t{\n\t\t\tchi2_check = chi_squared_table[res.rows()];\n\t\t}\n\t\telse\n\t\t{\n\t\t\tboost::math::chi_squared chi_squared_dist(res.rows());\n\t\t\tchi2_check = boost::math::quantile(chi_squared_dist, 0.95);\n\t\t\tstd::cout << \"chi2_check over the residual limit - \" << (int)res.rows() << std::endl;\n\t\t}\n\n\t\tdouble chi2_multipler = options_slam.chi2_multipler;\n\t\t\n\t\tif (chi2 > chi2_multipler * chi2_check)\n\t\t{\n\t\t\tmap_point->update_fail_count++;\n\n\t\t\t(*it2)->to_delete = true;\n\t\t\tit2 = feature_vec.erase(it2);\n\t\t\tcontinue;\n\t\t}\n\n\t\tsize_t count_hx = 0;\n\t\tfor (const auto &var : Hxf_order)\n\t\t{\n\t\t\tif (Hx_mapping.find(var) == Hx_mapping.end())\n\t\t\t{\n\t\t\t\tHx_mapping.insert({var, count_jacob});\n\t\t\t\tHx_order_big.push_back(var);\n\t\t\t\tcount_jacob += var->getSize();\n\t\t\t}\n\n\t\t\tHx_big.block(count_meas, Hx_mapping[var], H_xf.rows(), var->getSize()) = H_xf.block(0, count_hx, H_xf.rows(), var->getSize());\n\t\t\tcount_hx += var->getSize();\n\t\t}\n\n\t\tR_big.block(count_meas, count_meas, res.rows(), res.rows()) *= sigma_pix_sq;\n\n\t\tres_big.block(count_meas, 0, res.rows(), 1) = res;\n\t\tcount_meas += res.rows();\n\t\tit2++;\n\t}\n\trT2 = boost::posix_time::microsec_clock::local_time();\n\n\tfor (size_t f = 0; f < feature_vec.size(); f++)\n\t{\n\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).back() > clone_times.at(clone_times.size() - 1) + 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).back() > clone_times.at(clone_times.size() - 1) + 1e-5))\n\t\t{\n\t\t\tif ((feature_vec[f]->timestamps.find(0) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(0).empty() && feature_vec[f]->timestamps.at(0).front() < clone_times.at(clone_times.size() - 3) - 1e-5) || \n\t\t\t(feature_vec[f]->timestamps.find(1) != feature_vec[f]->timestamps.end() && !feature_vec[f]->timestamps.at(1).empty() && feature_vec[f]->timestamps.at(1).front() < clone_times.at(clone_times.size() - 3) - 1e-5))\n\t\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tfeature_vec[f]->to_delete = true;\n\t\t}\n\t}\n\n\tif (count_meas < 1)\n\t{\n\t\treturn;\n\t}\n\tassert(count_meas <= max_meas_size);\n\tassert(count_jacob <= max_hx_size);\n\tres_big.conservativeResize(count_meas, 1);\n\tHx_big.conservativeResize(count_meas, count_jacob);\n\tR_big.conservativeResize(count_meas, count_meas);\n\n\tstateHelper::ekfUpdate(state_ptr, Hx_order_big, Hx_big, res_big, R_big);\n\trT3 = boost::posix_time::microsec_clock::local_time();\n\n\t// Time test\n\t/*\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT1 - rT0).total_microseconds() * 1e-6 << \" seconds to clean.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds creating linear system.\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds to update (\" \n\t\t<< (int)feature_vec.size() << \" feats of \" << (int)Hx_big.rows() << \" size).\" << std::endl;\n\tstd::cout << std::fixed << \"[updaterSlam]: \" << (rT3 - rT1).total_microseconds() * 1e-6 << \" seconds total.\" << std::endl;\n\t*/\n\t// Time test\n}\n void delayedInit(std::shared_ptr state_ptr, voxelHashMap &voxel_map, std::vector> &feature_vec, pcl::PointCloud::Ptr voxels_history) {\n\tif (feature_vec.empty())\n\t\treturn;\n\n\tboost::posix_time::ptime rT0, rT1, rT2, rT3;\n\trT0 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::vector clone_times;\n\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t{\n\t\tclone_times.emplace_back(clone_imu.first);\n\t}\n\n\tauto it0 = feature_vec.begin();\n\twhile (it0 != feature_vec.end())\n\t{\n\t\t(*it0)->cleanOldMeasurements(clone_times);\n\n\t\tint count_meas = 0;\n\t\tfor (const auto &pair : (*it0)->timestamps)\n\t\t{\n\t\t\tcount_meas += (*it0)->timestamps[pair.first].size();\n\t\t}\n\n\t\tif (count_meas < 2)\n\t\t{\n\t\t\t(*it0)->to_delete = true;\n\t\t\tit0 = feature_vec.erase(it0);\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n\trT1 = boost::posix_time::microsec_clock::local_time();\n\n\tstd::unordered_map> clones_cam;\n\tfor (const auto &clone_calib : state_ptr->calib_cam_imu)\n\t{\n\t\tstd::unordered_map clones_cami;\n\t\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t\t{\n\t\t\tEigen::Matrix R_GtoCi = clone_calib.second->getRot() * clone_imu.second->getRot();\n\t\t\tEigen::Matrix p_CioinG = clone_imu.second->getPos() - R_GtoCi.transpose() * clone_calib.second->getPos();\n\n\t\t\tclones_cami.insert({clone_imu.first, featureInitializer::clonePose(R_GtoCi, p_CioinG)});\n\t\t}\n\n\t\tclones_cam.insert({clone_calib.first, clones_cami});\n\t}\n\n\tauto it1 = feature_vec.begin();\n\twhile (it1 != feature_vec.end())\n\t{\n\t\tbool success_tri = true;\n\t\tsuccess_tri = initializer_feature->singleTriangulation(*it1, clones_cam);\n\n\t\tbool success_refine = true;\n\t\tif (initializer_feature->config().refine_features)\n\t\t{\n\t\t\tsuccess_refine = initializer_feature->singleGaussnewton(*it1, clones_cam);\n\t\t}\n\n\t\tif (!success_tri || !success_refine)\n\t\t{\n\t\t\t(*it1)->to_delete = true;\n\t\t\tit1 = feature_vec.erase(it1);\n\t\t\tcontinue;\n\t\t}\n\t\tit1++;\n\t}\n\trT2 = boost::posix_time::microsec_clock::local_time();\n\n\tauto it2 = feature_vec.begin();\n\twhile (it2 != feature_vec.end())\n\t{\t\n\t\tupdaterHelper::updaterHelperFeature feat;\n\t\tfeat.feature_id = (*it2)->feature_id;\n\t\tfeat.uvs = (*it2)->uvs;\n\t\tfeat.uvs_norm = (*it2)->uvs_norm;\n\t\tfeat.timestamps = (*it2)->timestamps;\n\n\t\tfeat.frames = (*it2)->frames;\n\t\tfeat.color = (*it2)->color;\n\t\tassert(feat.color[0].size() == feat.uvs[0].size());\n\t\tassert(feat.color[1].size() == feat.uvs[1].size());\n\t\tassert(feat.color[(*it2)->anchor_cam_id].back() >= 0);\n\n\t\tfeat.anchor_cam_id = (*it2)->anchor_cam_id;\n\t\tfeat.anchor_clone_timestamp = (*it2)->anchor_clone_timestamp;\n\n\t\tfeat.position_global = (*it2)->position_global;\n\t\tfeat.position_global_fej = (*it2)->position_global;\n\n\t\tEigen::MatrixXd H_f;\n\t\tEigen::MatrixXd H_x;\n\t\tEigen::VectorXd res;\n\t\tstd::vector> Hx_order;\n\n\t\tupdaterHelper::getFeatureJacobianFull(state_ptr, feat, H_f, H_x, res, Hx_order);\n\n\t\tauto map_point_ptr = std::make_shared();\n\t\tmap_point_ptr->feature_id = feat.feature_id;\n\t\tmap_point_ptr->unique_camera_id = (*it2)->anchor_cam_id;\n\n\t\tmap_point_ptr->color = feat.color[feat.anchor_cam_id].back();\n\t\tmap_point_ptr->host_frame = feat.frames[(*it2)->anchor_cam_id].back();\n\t\tmap_point_ptr->anchor_cam_id = feat.anchor_cam_id;\n\t\tmap_point_ptr->anchor_clone_timestamp = feat.anchor_clone_timestamp;\n\n\t\tmap_point_ptr->setPointXYZ(feat.position_global, false);\n\t\tmap_point_ptr->setPointXYZ(feat.position_global_fej, true);\n\n\t\tdouble sigma_pix_sq = options_slam.sigma_pix_sq;\n\t\tEigen::MatrixXd R = sigma_pix_sq * Eigen::MatrixXd::Identity(res.rows(), res.rows());\n\n\t\tdouble chi2_multipler = options_slam.chi2_multipler;\n\n\t\tif (stateHelper::initialize(state_ptr, map_point_ptr, Hx_order, H_x, H_f, R, res, chi2_multipler))\n\t\t{\n\t\t\tbool add = mapManagement::addPointToVoxel(voxel_map, map_point_ptr, state_ptr->options.voxel_size, state_ptr->options.max_num_points_in_voxel, \n\t\t\t\tstate_ptr->options.min_distance_points, voxels_history);\n\n\t\t\tif (add)\n\t\t\t{\n\t\t\t\tstate_ptr->map_points.insert({(*it2)->feature_id, map_point_ptr});\n\t\t\t\t(*it2)->to_delete = true;\n\t\t\t\tit2++;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\t(*it2)->to_delete = true;\n\t\t\t\tit2 = feature_vec.erase(it2);\n\t\t\t}\t\n\t\t}\n\t\telse\n\t\t{\n\t\t\t(*it2)->to_delete = true;\n\t\t\tit2 = feature_vec.erase(it2);\n\t\t}\n\t}\n\trT3 = boost::posix_time::microsec_clock::local_time();\n\n\t// Time test\n\t/*\n\tif (!feature_vec.empty())\n\t{\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT1 - rT0).total_microseconds() * 1e-6 << \" seconds to clean.\" << std::endl;\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT2 - rT1).total_microseconds() * 1e-6 << \" seconds to triangulate.\" << std::endl;\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT3 - rT2).total_microseconds() * 1e-6 << \" seconds initialize (\" << (int)feature_vec.size() << \" features).\" << std::endl;\n\t\tstd::cout << std::fixed << \"[updaterSlam::delayedInit]: \" << (rT3 - rT1).total_microseconds() * 1e-6 << \" seconds total.\" << std::endl;\n\t}\n\t*/\n\t// Time test\n}\n protected:\n updaterOptions options_slam;\n std::shared_ptr initializer_feature;\n std::shared_ptr database;\n std::map chi_squared_table;\n};"], ["/voxel_svio/include/stateHelper.h", "class baseType {\n private:\n stateHelper() {\n\n}\n public:\n static void ekfPropagation(std::shared_ptr state_ptr, const std::vector> &order_new, \n\t\tconst std::vector> &order_old, const Eigen::MatrixXd &phi, const Eigen::MatrixXd &Q) {\n\tif (order_new.empty() || order_old.empty())\n\t{\n\t\tstd::cout << \"[stateHelper::ekfPropagation]: Called with empty variable arrays!\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tint size_order_new = order_new.at(0)->getSize();\n\tfor (size_t i = 0; i < order_new.size() - 1; i++)\n\t{\n\t\tif (order_new.at(i)->getId() + order_new.at(i)->getSize() != order_new.at(i + 1)->getId())\n\t\t{\n\t\t\tstd::cout << \"[stateHelper::ekfPropagation]: Called with non-contiguous state elements!\" << std::endl;\n\t\t\tstd::cout << \"[stateHelper::ekfPropagation]: This code only support a state transition which is in the same order as the state.\" << std::endl;\n\t\t\tstd::exit(EXIT_FAILURE);\n\t\t}\n\t\tsize_order_new += order_new.at(i + 1)->getSize();\n\t}\n\n\tint size_order_old = order_old.at(0)->getSize();\n\tfor (size_t i = 0; i < order_old.size() - 1; i++)\n\t{\n\t\tsize_order_old += order_old.at(i + 1)->getSize();\n\t}\n\n\tassert(size_order_new == phi.rows());\n\tassert(size_order_old == phi.cols());\n\tassert(size_order_new == Q.cols());\n\tassert(size_order_new == Q.rows());\n\n\tint current_it = 0;\n\tstd::vector phi_id;\n\tfor (const auto &var : order_old)\n\t{\n\t\tphi_id.push_back(current_it);\n\t\tcurrent_it += var->getSize();\n\t}\n\n\tEigen::MatrixXd cov_phiT = Eigen::MatrixXd::Zero(state_ptr->cov.rows(), phi.rows());\n\tfor (size_t i = 0; i < order_old.size(); i++)\n\t{\n\t\tstd::shared_ptr var = order_old.at(i);\n\t\tcov_phiT.noalias() += state_ptr->cov.block(0, var->getId(), state_ptr->cov.rows(), var->getSize()) * phi.block(0, phi_id[i], phi.rows(), var->getSize()).transpose();\n\t}\n\n\tEigen::MatrixXd phi_cov_phiT = Q.selfadjointView();\n\tfor (size_t i = 0; i < order_old.size(); i++)\n\t{\n\t\tstd::shared_ptr var = order_old.at(i);\n\t\tphi_cov_phiT.noalias() += phi.block(0, phi_id[i], phi.rows(), var->getSize()) * cov_phiT.block(var->getId(), 0, var->getSize(), phi.rows());\n\t}\n\n\tint start_id = order_new.at(0)->getId();\n\tint phi_size = phi.rows();\n\tint total_size = state_ptr->cov.rows();\n\tstate_ptr->cov.block(start_id, 0, phi_size, total_size) = cov_phiT.transpose();\n\tstate_ptr->cov.block(0, start_id, total_size, phi_size) = cov_phiT;\n\tstate_ptr->cov.block(start_id, start_id, phi_size, phi_size) = phi_cov_phiT;\n\n\tEigen::VectorXd diags = state_ptr->cov.diagonal();\n\tbool found_neg = false;\n\tfor (int i = 0; i < diags.rows(); i++)\n\t{\n\t\tif (diags(i) < 0.0)\n\t\t{\n\t\t\tstd::cout << \"[stateHelper::ekfPropagation]: Diagonal at \" << i << \" is \" << diags(i) << std::endl;\n\t\t\tfound_neg = true;\n\t\t}\n\t}\n\n\tif (found_neg)\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n}\n static void ekfUpdate(std::shared_ptr state_ptr, const std::vector> &H_order, \n\t\tconst Eigen::MatrixXd &H, const Eigen::VectorXd &res, const Eigen::MatrixXd &R) {\n\tassert(res.rows() == R.rows());\n\tassert(H.rows() == res.rows());\n\tEigen::MatrixXd M_a = Eigen::MatrixXd::Zero(state_ptr->cov.rows(), res.rows());\n\n\tint current_it = 0;\n\tstd::vector H_id;\n\tfor (const auto &meas_var : H_order)\n\t{\n\t\tH_id.push_back(current_it);\n\t\tcurrent_it += meas_var->getSize();\n\t}\n\n\tfor (const auto &var : state_ptr->variables)\n\t{\n\t\t// Sum up effect of each subjacobian = K_i= \\sum_m (P_im Hm^T)\n\t\tEigen::MatrixXd M_i = Eigen::MatrixXd::Zero(var->getSize(), res.rows());\n\t\tfor (size_t i = 0; i < H_order.size(); i++)\n\t\t{\n\t\t\tstd::shared_ptr meas_var = H_order[i];\n\t\t\tM_i.noalias() += state_ptr->cov.block(var->getId(), meas_var->getId(), var->getSize(), meas_var->getSize()) * \n\t\t\t\tH.block(0, H_id[i], H.rows(), meas_var->getSize()).transpose();\n\t\t}\n\t\tM_a.block(var->getId(), 0, var->getSize(), res.rows()) = M_i;\n\t}\n\n\tEigen::MatrixXd P_small = stateHelper::getMarginalCovariance(state_ptr, H_order);\n\tEigen::MatrixXd S = H * P_small * H.transpose() + R;\n\tEigen::MatrixXd S_inv = Eigen::MatrixXd::Identity(R.rows(), R.rows());\n\tS.selfadjointView().llt().solveInPlace(S_inv);\n\tEigen::MatrixXd K = M_a * S_inv.selfadjointView();\n\n\tstate_ptr->cov.triangularView() -= K * M_a.transpose();\n\tstate_ptr->cov = state_ptr->cov.selfadjointView();\n\n\tEigen::VectorXd diags = state_ptr->cov.diagonal();\n\tbool found_neg = false;\n\n\tfor (int i = 0; i < diags.rows(); i++)\n\t{\n\t\tif (diags(i) < 0.0)\n\t\t{\n\t\t\tstd::cout << \"[stateHelper::ekfUpdate: Diagonal at \" << i << \" is \" << diags(i) << std::endl;\n\t\t\tfound_neg = true;\n\t\t}\n\t}\n\n\tif (found_neg)\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tEigen::VectorXd dx = K * res;\n\tfor (size_t i = 0; i < state_ptr->variables.size(); i++)\n\t{\n\t\tstate_ptr->variables.at(i)->update(dx.block(state_ptr->variables.at(i)->getId(), 0, state_ptr->variables.at(i)->getSize(), 1));\n\t}\n\n\tif (state_ptr->options.do_calib_camera_intrinsics)\n\t{\n\t\tfor (auto const &calib : state_ptr->cam_intrinsics)\n\t\t{\n\t\t\tstate_ptr->cam_intrinsics_cameras.at(calib.first)->setValue(calib.second->value());\n\t\t}\n\t}\n}\n static void setInitialCovariance(std::shared_ptr state_ptr, const Eigen::MatrixXd &covariance, \n\t\tconst std::vector> &order) {\n\tint i_index = 0;\n\n\tfor (size_t i = 0; i < order.size(); i++)\n\t{\n\t\tint k_index = 0;\n\t\tfor (size_t k = 0; k < order.size(); k++)\n\t\t{\n\t\t\tstate_ptr->cov.block(order[i]->getId(), order[k]->getId(), order[i]->getSize(), order[k]->getSize()) = \n\t\t\t\tcovariance.block(i_index, k_index, order[i]->getSize(), order[k]->getSize());\n\t\t\tk_index += order[k]->getSize();\n\t\t}\n\t\ti_index += order[i]->getSize();\n\t}\n\tstate_ptr->cov = state_ptr->cov.selfadjointView();\n}\n static Eigen::MatrixXd getMarginalCovariance(std::shared_ptr state_ptr, const std::vector> &small_variables) {\n\tint cov_size = 0;\n\tfor (size_t i = 0; i < small_variables.size(); i++)\n\t{\n\t\tcov_size += small_variables[i]->getSize();\n\t}\n\n\tEigen::MatrixXd small_cov = Eigen::MatrixXd::Zero(cov_size, cov_size);\n\tint i_index = 0;\n\n\tfor (size_t i = 0; i < small_variables.size(); i++)\n\t{\n\t\tint k_index = 0;\n\n\t\tfor (size_t k = 0; k < small_variables.size(); k++)\n\t\t{\n\t\t\tsmall_cov.block(i_index, k_index, small_variables[i]->getSize(), small_variables[k]->getSize()) = \n\t\t\t\tstate_ptr->cov.block(small_variables[i]->getId(), small_variables[k]->getId(), small_variables[i]->getSize(), small_variables[k]->getSize());\n\t\t\tk_index += small_variables[k]->getSize();\n\t\t}\n\n\t\ti_index += small_variables[i]->getSize();\n\t}\n\n\treturn small_cov;\n}\n static Eigen::MatrixXd getFullCovariance(std::shared_ptr state_ptr) {\n\tint cov_size = (int)state_ptr->cov.rows();\n\n\tEigen::MatrixXd full_cov = Eigen::MatrixXd::Zero(cov_size, cov_size);\n\n\tfull_cov.block(0, 0, state_ptr->cov.rows(), state_ptr->cov.rows()) = state_ptr->cov;\n\n\treturn full_cov;\n}\n static void marginalize(std::shared_ptr state_ptr, std::shared_ptr marg) {\n\tif (std::find(state_ptr->variables.begin(), state_ptr->variables.end(), marg) == state_ptr->variables.end())\n\t{\n\t\tstd::cout << \"[stateHelper::marginalize]: Called on variable that is not in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::marginalize]: Marginalization, does NOT work on sub-variables yet...\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tint marg_size = marg->getSize();\n\tint marg_id = marg->getId();\n\tint x2_size = (int)state_ptr->cov.rows() - marg_id - marg_size;\n\n\tEigen::MatrixXd cov_new(state_ptr->cov.rows() - marg_size, state_ptr->cov.rows() - marg_size);\n\n\tcov_new.block(0, 0, marg_id, marg_id) = state_ptr->cov.block(0, 0, marg_id, marg_id);\n\tcov_new.block(0, marg_id, marg_id, x2_size) = state_ptr->cov.block(0, marg_id + marg_size, marg_id, x2_size);\n\tcov_new.block(marg_id, 0, x2_size, marg_id) = cov_new.block(0, marg_id, marg_id, x2_size).transpose();\n\tcov_new.block(marg_id, marg_id, x2_size, x2_size) = state_ptr->cov.block(marg_id + marg_size, marg_id + marg_size, x2_size, x2_size);\n\n\tstate_ptr->cov = cov_new;\n\tassert(state_ptr->cov.rows() == cov_new.rows());\n\n\tstd::vector> remaining_variables;\n\n\tfor (size_t i = 0; i < state_ptr->variables.size(); i++)\n\t{\n\t\tif (state_ptr->variables.at(i) != marg)\n\t\t{\n\t\t\tif (state_ptr->variables.at(i)->getId() > marg_id)\n\t\t\t{\n\t\t\t\tstate_ptr->variables.at(i)->setLocalId(state_ptr->variables.at(i)->getId() - marg_size);\n\t\t\t}\n\t\t\tremaining_variables.push_back(state_ptr->variables.at(i));\n\t\t}\n\t}\n\n\tmarg->setLocalId(-1);\n\tstate_ptr->variables = remaining_variables;\n}\n static std::shared_ptr clone(std::shared_ptr state_ptr, std::shared_ptr variable_to_clone) {\n\tint total_size = variable_to_clone->getSize();\n\tint old_size = (int)state_ptr->cov.rows();\n\tint new_loc = (int)state_ptr->cov.rows();\n\n\tstate_ptr->cov.conservativeResizeLike(Eigen::MatrixXd::Zero(old_size + total_size, old_size + total_size));\n\n\tconst std::vector> new_variables = state_ptr->variables;\n\tstd::shared_ptr new_clone = nullptr;\n\n\tfor (size_t k = 0; k < state_ptr->variables.size(); k++)\n\t{\n\t\tstd::shared_ptr type_check = state_ptr->variables.at(k)->checkIfSubvariable(variable_to_clone);\n\t\tif (state_ptr->variables.at(k) == variable_to_clone)\n\t\t{\n\t\t\ttype_check = state_ptr->variables.at(k);\n\t\t}\n\t\telse if (type_check != variable_to_clone)\n\t\t{\n\t\t\tcontinue;\n\t\t}\n\n\t\tint old_loc = type_check->getId();\n\n\t\tstate_ptr->cov.block(new_loc, new_loc, total_size, total_size) = state_ptr->cov.block(old_loc, old_loc, total_size, total_size);\n\t\tstate_ptr->cov.block(0, new_loc, old_size, total_size) = state_ptr->cov.block(0, old_loc, old_size, total_size);\n\t\tstate_ptr->cov.block(new_loc, 0, total_size, old_size) = state_ptr->cov.block(old_loc, 0, total_size, old_size);\n\n\t\tnew_clone = type_check->clone();\n\t\tnew_clone->setLocalId(new_loc);\n\t\tbreak;\n\t}\n\n\tif (new_clone == nullptr)\n\t{\n\t\tstd::cout << \"[stateHelper::clone]: Called on variable is not in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::clone]: Ensure that the variable specified is a variable, or sub-variable.\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tstate_ptr->variables.push_back(new_clone);\n\treturn new_clone;\n}\n static bool initialize(std::shared_ptr state_ptr, std::shared_ptr new_variable, \n\t\tconst std::vector> &H_order, Eigen::MatrixXd &H_R, Eigen::MatrixXd &H_L, \n\t\tEigen::MatrixXd &R, Eigen::VectorXd &res, double chi_2_mult) {\n\tif (std::find(state_ptr->variables.begin(), state_ptr->variables.end(), new_variable) != state_ptr->variables.end())\n\t{\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Called on variable that is already in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found this variable at \" << new_variable->getId() << \" in covariance.\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tassert(R.rows() == R.cols());\n\tassert(R.rows() > 0);\n\tfor (int r = 0; r < R.rows(); r++)\n\t{\n\t\tfor (int c = 0; c < R.cols(); c++)\n\t\t{\n\t\t\tif (r == c && R(0, 0) != R(r, c))\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Your noise is not isotropic!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Found a value of \" << R(r, c) << \" verses value of \" << R(0, 0) << \".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t\telse if (r != c && R(r, c) != 0.0)\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Your noise is not diagonal!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initialize]: Found a value of \" << R(r, c) << \" at row \" << r << \" and column \" << c <<\".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t}\n\t}\n\n\tsize_t new_var_size = new_variable->getSize();\n\tassert((int)new_var_size == H_L.cols());\n\n\tEigen::JacobiRotation tempHo_GR;\n\tfor (int n = 0; n < H_L.cols(); ++n)\n\t{\n\t\tfor (int m = (int)H_L.rows() - 1; m > n; m--)\n\t\t{\n\t\t\ttempHo_GR.makeGivens(H_L(m - 1, n), H_L(m, n));\n\t\t\t(H_L.block(m - 1, n, 2, H_L.cols() - n)).applyOnTheLeft(0, 1, tempHo_GR.adjoint());\n\t\t\t(res.block(m - 1, 0, 2, 1)).applyOnTheLeft(0, 1, tempHo_GR.adjoint());\n\t\t\t(H_R.block(m - 1, 0, 2, H_R.cols())).applyOnTheLeft(0, 1, tempHo_GR.adjoint());\n\t\t}\n\t}\n\n\tEigen::MatrixXd Hxinit = H_R.block(0, 0, new_var_size, H_R.cols());\n\tEigen::MatrixXd H_finit = H_L.block(0, 0, new_var_size, new_var_size);\n\tEigen::VectorXd resinit = res.block(0, 0, new_var_size, 1);\n\tEigen::MatrixXd Rinit = R.block(0, 0, new_var_size, new_var_size);\n\n\tEigen::MatrixXd Hup = H_R.block(new_var_size, 0, H_R.rows() - new_var_size, H_R.cols());\n\tEigen::VectorXd resup = res.block(new_var_size, 0, res.rows() - new_var_size, 1);\n\tEigen::MatrixXd Rup = R.block(new_var_size, new_var_size, R.rows() - new_var_size, R.rows() - new_var_size);\n\n\tEigen::MatrixXd P_up = stateHelper::getMarginalCovariance(state_ptr, H_order);\n\tassert(Rup.rows() == Hup.rows());\n\tassert(Hup.cols() == P_up.cols());\n\tEigen::MatrixXd S = Hup * P_up * Hup.transpose() + Rup;\n\tdouble chi2 = resup.dot(S.llt().solve(resup));\n\n\tboost::math::chi_squared chi_squared_dist(res.rows());\n\tdouble chi2_check = boost::math::quantile(chi_squared_dist, 0.95);\n\tif (chi2 > chi_2_mult * chi2_check)\n\t{\n\t\treturn false;\n\t}\n\n\tstateHelper::initializeInvertible(state_ptr, new_variable, H_order, Hxinit, H_finit, Rinit, resinit);\n\n\tif (Hup.rows() > 0)\n\t{\n\t\tstateHelper::ekfUpdate(state_ptr, H_order, Hup, resup, Rup);\n\t}\n\treturn true;\n}\n static void initializeInvertible(std::shared_ptr state_ptr, std::shared_ptr new_variable, \n\t\tconst std::vector> &H_order, const Eigen::MatrixXd &H_R, \n\t\tconst Eigen::MatrixXd &H_L, const Eigen::MatrixXd &R, const Eigen::VectorXd &res) {\n\tif (std::find(state_ptr->variables.begin(), state_ptr->variables.end(), new_variable) != state_ptr->variables.end())\n\t{\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Called on variable that is already in the state.\" << std::endl;\n\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found this variable at \" << new_variable->getId() << \" in covariance.\" << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tassert(R.rows() == R.cols());\n\tassert(R.rows() > 0);\n\tfor (int r = 0; r < R.rows(); r++)\n\t{\n\t\tfor (int c = 0; c < R.cols(); c++)\n\t\t{\n\t\t\tif (r == c && R(0, 0) != R(r, c))\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Your noise is not isotropic!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found a value of \" << R(r, c) << \" verses value of \" << R(0, 0) << \".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t\telse if (r != c && R(r, c) != 0.0)\n\t\t\t{\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Your noise is not diagonal!\" << std::endl;\n\t\t\t\tstd::cout << \"[stateHelper::initializeInvertible]: Found a value of \" << R(r, c) << \" at row \" << r << \" and column \" << c << \".\" << std::endl;\n\t\t\t\tstd::exit(EXIT_FAILURE);\n\t\t\t}\n\t\t}\n\t}\n\n\tassert(res.rows() == R.rows());\n\tassert(H_L.rows() == res.rows());\n\tassert(H_L.rows() == H_R.rows());\n\tEigen::MatrixXd M_a = Eigen::MatrixXd::Zero(state_ptr->cov.rows(), res.rows());\n\n\tint current_it = 0;\n\tstd::vector H_id;\n\tfor (const auto &meas_var : H_order) {\n\t\tH_id.push_back(current_it);\n\t\tcurrent_it += meas_var->getSize();\n\t}\n\n\tfor (const auto &var : state_ptr->variables)\n\t{\n\t\tEigen::MatrixXd M_i = Eigen::MatrixXd::Zero(var->getSize(), res.rows());\n\t\tfor (size_t i = 0; i < H_order.size(); i++)\n\t\t{\n\t\t\tstd::shared_ptr meas_var = H_order.at(i);\n\t\t\tM_i += state_ptr->cov.block(var->getId(), meas_var->getId(), var->getSize(), meas_var->getSize()) * \n\t\t\t\tH_R.block(0, H_id[i], H_R.rows(), meas_var->getSize()).transpose();\n\t\t}\n\t\tM_a.block(var->getId(), 0, var->getSize(), res.rows()) = M_i;\n\t}\n\n\tEigen::MatrixXd P_small = stateHelper::getMarginalCovariance(state_ptr, H_order);\n\n\tEigen::MatrixXd M(H_R.rows(), H_R.rows());\n\tM.triangularView() = H_R * P_small * H_R.transpose();\n\tM.triangularView() += R;\n\n\tassert(H_L.rows() == H_L.cols());\n\tassert(H_L.rows() == new_variable->getSize());\n\tEigen::MatrixXd H_Linv = H_L.inverse();\n\tEigen::MatrixXd P_LL = H_Linv * M.selfadjointView() * H_Linv.transpose();\n\n\tsize_t oldSize = state_ptr->cov.rows();\n\tstate_ptr->cov.conservativeResizeLike(Eigen::MatrixXd::Zero(oldSize + new_variable->getSize(), oldSize + new_variable->getSize()));\n\tstate_ptr->cov.block(0, oldSize, oldSize, new_variable->getSize()).noalias() = -M_a * H_Linv.transpose();\n\tstate_ptr->cov.block(oldSize, 0, new_variable->getSize(), oldSize) = state_ptr->cov.block(0, oldSize, oldSize, new_variable->getSize()).transpose();\n\tstate_ptr->cov.block(oldSize, oldSize, new_variable->getSize(), new_variable->getSize()) = P_LL;\n\n\tnew_variable->update(H_Linv * res);\n\tnew_variable->setLocalId(oldSize);\n\tstate_ptr->variables.push_back(new_variable);\n}\n static void augmentClone(std::shared_ptr state_ptr, Eigen::Matrix last_gyr) {\n\tif (state_ptr->clones_imu.find(state_ptr->timestamp) != state_ptr->clones_imu.end())\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tstd::shared_ptr pose_temp = stateHelper::clone(state_ptr, state_ptr->imu_ptr->pose());\n\n\tstd::shared_ptr pose = std::dynamic_pointer_cast(pose_temp);\n\tif (pose == nullptr)\n\t{\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tstate_ptr->clones_imu[state_ptr->timestamp] = pose;\n\n\tif (state_ptr->options.do_calib_camera_timeoffset)\n\t{\n\t\tEigen::Matrix dnc_dt = Eigen::MatrixXd::Zero(6, 1);\n\t\tdnc_dt.block(0, 0, 3, 1) = last_gyr;\n\t\tdnc_dt.block(3, 0, 3, 1) = state_ptr->imu_ptr->getVel();\n\n\t\tstate_ptr->cov.block(0, pose->getId(), state_ptr->cov.rows(), 6) += \n\t\t\tstate_ptr->cov.block(0, state_ptr->calib_dt_imu_cam->getId(), state_ptr->cov.rows(), 1) * dnc_dt.transpose();\n\t\tstate_ptr->cov.block(pose->getId(), 0, 6, state_ptr->cov.rows()) += \n\t\t\tdnc_dt * state_ptr->cov.block(state_ptr->calib_dt_imu_cam->getId(), 0, 1, state_ptr->cov.rows());\n\t}\n}\n static void marginalizeOldClone(std::shared_ptr state_ptr) {\n\tif ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size)\n\t{\n\t\tdouble marginal_time = state_ptr->margtimestep();\n\n\t\tassert(marginal_time != INFINITY);\n\t\tstateHelper::marginalize(state_ptr, state_ptr->clones_imu.at(marginal_time));\n\n\t\tstate_ptr->clones_imu.erase(marginal_time);\n\n\t\tassert(state_ptr->clones_frame.at(marginal_time)->v_feat_ptr[0].size() == 0);\n\t\tassert(state_ptr->clones_frame.at(marginal_time)->v_feat_ptr[1].size() == 0);\n\t\tstate_ptr->clones_frame.at(marginal_time)->release();\n\t\tstate_ptr->clones_frame.erase(marginal_time);\n\t}\n}\n static void marginalizeNewClone(std::shared_ptr state_ptr) {\n\tif ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size)\n\t{\n\t\tstd::vector clone_times;\n\t\tfor (const auto &clone_imu : state_ptr->clones_imu)\n\t\t{\n\t\t\tclone_times.emplace_back(clone_imu.first);\n\t\t}\n\n\t\tdouble marginal_time = clone_times.at(clone_times.size() - 2);\n\n\t\tassert(marginal_time != INFINITY);\n\t\tstateHelper::marginalize(state_ptr, state_ptr->clones_imu.at(marginal_time));\n\n\t\tstd::shared_ptr fh_temp = state_ptr->clones_frame.at(marginal_time);\n\n\t\tif (fh_temp->v_feat_ptr.find(0) != fh_temp->v_feat_ptr.end())\n\t\t{\n\t\t\tauto it0 = fh_temp->v_feat_ptr.at(0).begin();\n\n\t\t\twhile (it0 != fh_temp->v_feat_ptr.at(0).end())\n\t\t\t{\n\t\t\t\tstd::vector marginal_timestamps = {marginal_time};\n\t\t\t\t(*it0).second->cleanInvalidMeasurements(marginal_timestamps);\n\t\t\t\tit0 = fh_temp->v_feat_ptr.at(0).begin();\n\t\t\t}\n\t\t}\n\n\t\tif (fh_temp->v_feat_ptr.find(1) != fh_temp->v_feat_ptr.end())\n\t\t{\n\t\t\tauto it1 = fh_temp->v_feat_ptr.at(1).begin();\n\n\t\t\twhile (it1 != fh_temp->v_feat_ptr.at(1).end())\n\t\t\t{\n\t\t\t\tstd::vector marginal_timestamps = {marginal_time};\n\t\t\t\t(*it1).second->cleanInvalidMeasurements(marginal_timestamps);\n\t\t\t\tit1 = fh_temp->v_feat_ptr.at(1).begin();\n\t\t\t}\n\t\t}\n\n\t\tstate_ptr->clones_imu.erase(marginal_time);\n\t\tstate_ptr->clones_frame.at(marginal_time)->release();\n\t\tstate_ptr->clones_frame.erase(marginal_time);\n\t}\n}\n static void marginalizeSlam(std::shared_ptr state_ptr, voxelHashMap &voxel_map, pcl::PointCloud::Ptr points_history) {\n\t// display\n\tpoints_history->clear();\n\t// display\n\n\tint ct_marginalized = 0;\n\tauto it0 = state_ptr->map_points.begin();\n\twhile (it0 != state_ptr->map_points.end())\n\t{\n\t\tif ((*it0).second->should_marg)\n\t\t{\n\t\t\tmapManagement::deleteFromVoxel(voxel_map, (*it0).second);\n\t\t\tstateHelper::marginalize(state_ptr, (*it0).second);\n\n\t\t\t// display\n\t\t\tpcl::PointXYZRGB point_temp;\n\t\t\tpoint_temp.x = (*it0).second->getPointXYZ(false)[0];\n\t\t\tpoint_temp.y = (*it0).second->getPointXYZ(false)[1];\n\t\t\tpoint_temp.z = (*it0).second->getPointXYZ(false)[2];\n\t\t\tpoint_temp.r = (*it0).second->color;\n\t\t\tpoint_temp.g = (*it0).second->color;\n\t\t\tpoint_temp.b = (*it0).second->color;\n\t\t\tpoints_history->points.push_back(point_temp);\n\t\t\t// display\n\n\t\t\tit0 = state_ptr->map_points.erase(it0);\n\t\t\tct_marginalized++;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tit0++;\n\t\t}\n\t}\n}\n};"], ["/voxel_svio/include/preIntegration.h", "class preIntegrationBase {\n public:\n preIntegrationBase(double sigma_w_, double sigma_wb_, double sigma_a_, double sigma_ab_, bool imu_avg_ = false) {\n\tQ_c.block(0, 0, 3, 3) = std::pow(sigma_w_, 2) * Eigen::Matrix::Identity();\n\tQ_c.block(3, 3, 3, 3) = std::pow(sigma_wb_, 2) * Eigen::Matrix::Identity();\n\tQ_c.block(6, 6, 3, 3) = std::pow(sigma_a_, 2) * Eigen::Matrix::Identity();\n\tQ_c.block(9, 9, 3, 3) = std::pow(sigma_ab_, 2) * Eigen::Matrix::Identity();\n\n\timu_avg = imu_avg_;\n\n\te_1 << 1, 0, 0;\n\te_2 << 0, 1, 0;\n\te_3 << 0, 0, 1;\n\te_1x = quatType::skewSymmetric(e_1);\n\te_2x = quatType::skewSymmetric(e_2);\n\te_3x = quatType::skewSymmetric(e_3);\n}\n void setLinearizationPoints(Eigen::Matrix b_w_lin_, Eigen::Matrix b_a_lin_,\n\t\tEigen::Matrix q_k_lin_ = Eigen::Matrix::Zero(), Eigen::Matrix grav_ = Eigen::Matrix::Zero()) {\n\tb_w_lin = b_w_lin_;\n\tb_a_lin = b_a_lin_;\n\tq_k_lin = q_k_lin_;\n\tgrav = grav_;\n}\n virtual void feedImu(double t_0, double t_1, Eigen::Matrix w_m_0, Eigen::Matrix a_m_0, \n\t\tEigen::Matrix w_m_1 = Eigen::Matrix::Zero(), Eigen::Matrix a_m_1 = Eigen::Matrix::Zero()) = 0 {\n\tdouble delta_t = t_1 - t_0;\n\tdt += delta_t;\n\n\tif (delta_t == 0) return;\n\n\tEigen::Matrix w_hat = w_m_0 - b_w_lin;\n\tEigen::Matrix a_hat = a_m_0 - b_a_lin - R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tif (imu_avg)\n\t{\n\t\tw_hat += w_m_1 - b_w_lin;\n\t\tw_hat = 0.5 * w_hat;\n\t}\n\n\tEigen::Matrix w_hat_dt = w_hat * delta_t;\n\n\tdouble w_1 = w_hat(0, 0);\n\tdouble w_2 = w_hat(1, 0);\n\tdouble w_3 = w_hat(2, 0);\n\n\tdouble mag_w = w_hat.norm();\n\tdouble w_dt = mag_w * delta_t;\n\n\tbool small_w = (mag_w < 0.008726646);\n\n\tdouble dt_2 = std::pow(delta_t, 2);\n\tdouble cos_wt = std::cos(w_dt);\n\tdouble sin_wt = std::sin(w_dt);\n\n\tEigen::Matrix w_x = quatType::skewSymmetric(w_hat);\n\tEigen::Matrix w_tx = quatType::skewSymmetric(w_hat_dt);\n\tEigen::Matrix w_x_2 = w_x * w_x;\n\n\tEigen::Matrix R_tau1_tau2 = small_w ? Eigen::Matrix::Identity() - delta_t * w_x + (std::pow(delta_t, 2) / 2) * w_x_2 \n\t\t: Eigen::Matrix::Identity() - (sin_wt / mag_w) * w_x + ((1.0 - cos_wt) / (std::pow(mag_w, 2.0))) * w_x_2;\n\n\tEigen::Matrix R_tau1_k = R_tau1_tau2 * R_tau_k;\n\tEigen::Matrix R_k_tau1 = R_tau1_k.transpose();\n\n\tif (imu_avg)\n\t{\n\t\ta_hat += a_m_1 - b_a_lin - R_tau1_k * quatType::quatToRot(q_k_lin) * grav;\n\t\ta_hat = 0.5 * a_hat;\n\t}\n\tEigen::Matrix a_x = quatType::skewSymmetric(a_hat);\n\n\tdouble f_1;\n\tdouble f_2;\n\tdouble f_3;\n\tdouble f_4;\n\n\tif (small_w)\n\t{\n\t\tf_1 = - (std::pow(delta_t, 3) / 3);\n\t\tf_2 = (std::pow(delta_t, 4) / 8);\n\t\tf_3 = - (std::pow(delta_t, 2) / 2);\n\t\tf_4 = (std::pow(delta_t, 3) / 6);\n\t}\n\telse\n\t{\n\t\tf_1 = (w_dt * cos_wt - sin_wt) / (std::pow(mag_w, 3));\n\t\tf_2 = (std::pow(w_dt, 2) - 2 * cos_wt - 2 * w_dt * sin_wt + 2) / (2 * std::pow(mag_w, 4));\n\t\tf_3 = - (1 - cos_wt) / std::pow(mag_w, 2);\n\t\tf_4 = (w_dt - sin_wt) / std::pow(mag_w, 3);\n\t}\n\n\tEigen::Matrix alpha_arg = ((dt_2 / 2.0) * Eigen::Matrix::Identity() + f_1 * w_x + f_2 * w_x_2);\n\tEigen::Matrix Beta_arg = (delta_t * Eigen::Matrix::Identity() + f_3 * w_x + f_4 * w_x_2);\n\n\tEigen::Matrix H_al = R_k_tau1 * alpha_arg;\n\tEigen::Matrix H_be = R_k_tau1 * Beta_arg;\n\n\talpha_tau += beta_tau * delta_t + H_al * a_hat;\n\tbeta_tau += H_be * a_hat;\n\n\tEigen::Matrix J_r_tau1 = small_w ? Eigen::Matrix::Identity() - 0.5 * w_tx + (1.0 / 6.0) * w_tx * w_tx \n\t\t: Eigen::Matrix::Identity() - ((1 - cos_wt) / (std::pow((w_dt), 2.0))) * w_tx + ((w_dt - sin_wt) / (std::pow(w_dt, 3.0))) * w_tx * w_tx;\n\n\tEigen::Matrix J_save = J_q;\n\tJ_q = R_tau1_tau2 * J_q + J_r_tau1 * delta_t;\n\n\tH_a -= H_al;\n\tH_a += delta_t * H_b;\n\tH_b -= H_be;\n\n\tEigen::Matrix g_k = quatType::quatToRot(q_k_lin) * grav;\n\tO_a += delta_t * O_b;\n\tO_a += -H_al * R_tau_k * quatType::skewSymmetric(g_k);\n\tO_b += -H_be * R_tau_k * quatType::skewSymmetric(g_k);\n\n\tEigen::MatrixXd d_R_bw_1 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_1);\n\tEigen::MatrixXd d_R_bw_2 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_2);\n\tEigen::MatrixXd d_R_bw_3 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_3);\n\n\tdouble df_1_dbw_1;\n\tdouble df_1_dbw_2;\n\tdouble df_1_dbw_3;\n\n\tdouble df_2_dbw_1;\n\tdouble df_2_dbw_2;\n\tdouble df_2_dbw_3;\n\n\tdouble df_3_dbw_1;\n\tdouble df_3_dbw_2;\n\tdouble df_3_dbw_3;\n\n\tdouble df_4_dbw_1;\n\tdouble df_4_dbw_2;\n\tdouble df_4_dbw_3;\n\n\tif (small_w)\n\t{\n\t\tdouble df_1_dw_mag = -(std::pow(delta_t, 5) / 15);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(delta_t, 6) / 72);\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = -(std::pow(delta_t, 4) / 12);\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (std::pow(delta_t, 5) / 60);\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\telse\n\t{\n\t\tdouble df_1_dw_mag = (std::pow(w_dt, 2) * sin_wt - 3 * sin_wt + 3 * w_dt * cos_wt) / std::pow(mag_w, 5);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(w_dt, 2) - 4 * cos_wt - 4 * w_dt * sin_wt + std::pow(w_dt, 2) * cos_wt + 4) / (std::pow(mag_w, 6));\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = (2 * (cos_wt - 1) + w_dt * sin_wt) / (std::pow(mag_w, 4));\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (2 * w_dt + w_dt * cos_wt - 3 * sin_wt) / (std::pow(mag_w, 5));\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\n\tEigen::Matrix g_tau = R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tJ_a += J_b * delta_t;\n\tJ_a.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * alpha_arg + R_k_tau1 * (df_1_dbw_1 * w_x - f_1 * e_1x + df_2_dbw_1 * w_x_2 - f_2 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_a.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * alpha_arg + R_k_tau1 * (df_1_dbw_2 * w_x - f_1 * e_2x + df_2_dbw_2 * w_x_2 - f_2 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_a.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * alpha_arg + R_k_tau1 * (df_1_dbw_3 * w_x - f_1 * e_3x + df_2_dbw_3 * w_x_2 - f_2 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\tJ_b.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * Beta_arg + R_k_tau1 * (df_3_dbw_1 * w_x - f_3 * e_1x + df_4_dbw_1 * w_x_2 - f_4 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\t-H_be * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_b.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * Beta_arg + R_k_tau1 * (df_3_dbw_2 * w_x - f_3 * e_2x + df_4_dbw_2 * w_x_2 - f_4 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_b.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * Beta_arg + R_k_tau1 * (df_3_dbw_3 * w_x - f_3 * e_3x + df_4_dbw_3 * w_x_2 - f_4 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\n\tEigen::Matrix R_G_to_k = quatType::quatToRot(q_k_lin);\n\tdouble dt_mid = delta_t / 2.0;\n\tdouble w_dt_mid = mag_w * dt_mid;\n\tEigen::Matrix R_mid;\n\n\tR_mid = small_w ? Eigen::Matrix::Identity() - dt_mid * w_x + (std::pow(dt_mid, 2) / 2) * w_x_2\n\t\t: Eigen::Matrix::Identity() - (std::sin(w_dt_mid) / mag_w) * w_x + ((1.0 - std::cos(w_dt_mid)) / (std::pow(mag_w, 2.0))) * w_x_2;\n\tR_mid = R_mid * R_tau_k;\n\n\tEigen::Matrix F_k1 = Eigen::Matrix::Zero();\n\tF_k1.block(0, 0, 3, 3) = - w_x;\n\tF_k1.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k1.block(6, 0, 3, 3) = - R_tau_k.transpose() * a_x;\n\tF_k1.block(6, 9, 3, 3) = - R_tau_k.transpose();\n\tF_k1.block(6, 15, 3, 3) = - R_tau_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k1.block(6, 18, 3, 3) = - R_tau_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k1.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k1 = Eigen::Matrix::Zero();\n\tG_k1.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k1.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k1.block(6, 6, 3, 3) = - R_tau_k.transpose();\n\tG_k1.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_dot_k1 = F_k1;\n\tEigen::Matrix P_dot_k1 = F_k1 * P_big + P_big * F_k1.transpose() + G_k1 * Q_c * G_k1.transpose();\n\n\tEigen::Matrix F_k2 = Eigen::Matrix::Zero();\n\tF_k2.block(0, 0, 3, 3) = - w_x;\n\tF_k2.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k2.block(6, 0, 3, 3) = - R_mid.transpose() * a_x;\n\tF_k2.block(6, 9, 3, 3) = - R_mid.transpose();\n\tF_k2.block(6, 15, 3, 3) = - R_mid.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k2.block(6, 18, 3, 3) = - R_mid.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k2.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k2 = Eigen::Matrix::Zero();\n\tG_k2.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k2.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k2.block(6, 6, 3, 3) = - R_mid.transpose();\n\tG_k2.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k2 = Eigen::Matrix::Identity() + Phi_dot_k1 * dt_mid;\n\tEigen::Matrix P_k2 = P_big + P_dot_k1 * dt_mid;\n\tEigen::Matrix Phi_dot_k2 = F_k2 * Phi_k2;\n\tEigen::Matrix P_dot_k2 = F_k2 * P_k2 + P_k2 * F_k2.transpose() + G_k2 * Q_c * G_k2.transpose();\n\n\tEigen::Matrix F_k3 = F_k2;\n\tEigen::Matrix G_k3 = G_k2;\n\n\tEigen::Matrix Phi_k3 = Eigen::Matrix::Identity() + Phi_dot_k2 * dt_mid;\n\tEigen::Matrix P_k3 = P_big + P_dot_k2 * dt_mid;\n\tEigen::Matrix Phi_dot_k3 = F_k3 * Phi_k3;\n\tEigen::Matrix P_dot_k3 = F_k3 * P_k3 + P_k3 * F_k3.transpose() + G_k3 * Q_c * G_k3.transpose();\n\n\tEigen::Matrix F_k4 = Eigen::Matrix::Zero();\n\tF_k4.block(0, 0, 3, 3) = - w_x;\n\tF_k4.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k4.block(6, 0, 3, 3) = - R_tau1_k.transpose() * a_x;\n\tF_k4.block(6, 9, 3, 3) = - R_tau1_k.transpose();\n\tF_k4.block(6, 15, 3, 3) = - R_tau1_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k4.block(6, 18, 3, 3) = - R_tau1_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k4.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k4 = Eigen::Matrix::Zero();\n\tG_k4.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k4.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k4.block(6, 6, 3, 3) = - R_tau1_k.transpose();\n\tG_k4.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k4 = Eigen::Matrix::Identity() + Phi_dot_k3 * delta_t;\n\tEigen::Matrix P_k4 = P_big + P_dot_k3 * delta_t;\n\tEigen::Matrix Phi_dot_k4 = F_k4 * Phi_k4;\n\tEigen::Matrix P_dot_k4 = F_k4 * P_k4 + P_k4 * F_k4.transpose() + G_k4 * Q_c * G_k4.transpose();\n\n\tP_big += (delta_t / 6.0) * (P_dot_k1 + 2.0 * P_dot_k2 + 2.0 * P_dot_k3 + P_dot_k4);\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\n\tEigen::Matrix Phi =\n\t\tEigen::Matrix::Identity() + (delta_t / 6.0) * (Phi_dot_k1 + 2.0 * Phi_dot_k2 + 2.0 * Phi_dot_k3 + Phi_dot_k4);\n\n\tEigen::Matrix B_k = Eigen::Matrix::Identity();\n\tB_k.block(15, 15, 3, 3).setZero();\n\tB_k.block(15, 0, 3, 3) = Eigen::Matrix::Identity();\n\n\tP_big = B_k * P_big * B_k.transpose();\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\tDiscrete_J_b = B_k * Phi * Discrete_J_b;\n\n\tP_meas = P_big.block(0, 0, 15, 15);\n\n\tif (state_transition_jacobians)\n\t{\n\t\tJ_q = - Discrete_J_b.block(0, 3, 3, 3);\n\t\tJ_a = Discrete_J_b.block(12, 3, 3, 3);\n\t\tJ_b = Discrete_J_b.block(6, 3, 3, 3);\n\t\tH_a = Discrete_J_b.block(12, 9, 3, 3);\n\t\tH_b = Discrete_J_b.block(6, 9, 3, 3);\n\t\tO_a = Discrete_J_b.block(12, 18, 3, 3);\n\t\tO_b = Discrete_J_b.block(6, 18, 3, 3);\n\t}\n\n\tR_tau_k = R_tau1_k;\n\tq_tau_k = quatType::rotToQuat(R_tau_k);\n}\n bool imu_avg = false;\n double dt = 0;\n Eigen::Matrix alpha_tau = Eigen::Matrix::Zero();\n Eigen::Matrix beta_tau = Eigen::Matrix::Zero();\n Eigen::Matrix q_tau_k;\n Eigen::Matrix R_tau_k = Eigen::Matrix::Identity();\n Eigen::Matrix J_q = Eigen::Matrix::Zero();\n Eigen::Matrix J_a = Eigen::Matrix::Zero();\n Eigen::Matrix J_b = Eigen::Matrix::Zero();\n Eigen::Matrix H_a = Eigen::Matrix::Zero();\n Eigen::Matrix H_b = Eigen::Matrix::Zero();\n Eigen::Matrix b_w_lin;\n Eigen::Matrix b_a_lin;\n Eigen::Matrix q_k_lin;\n Eigen::Matrix grav = Eigen::Matrix::Zero();\n Eigen::Matrix Q_c = Eigen::Matrix::Zero();\n Eigen::Matrix P_meas = Eigen::Matrix::Zero();\n Eigen::Matrix e_1;\n Eigen::Matrix e_2;\n Eigen::Matrix e_3;\n Eigen::Matrix e_1x;\n Eigen::Matrix e_2x;\n Eigen::Matrix e_3x;\n public:\n void feedImu(double t_0, double t_1, Eigen::Matrix w_m_0, Eigen::Matrix a_m_0, \n\t\tEigen::Matrix w_m_1 = Eigen::Matrix::Zero(), Eigen::Matrix a_m_1 = Eigen::Matrix::Zero()) {\n\tdouble delta_t = t_1 - t_0;\n\tdt += delta_t;\n\n\tif (delta_t == 0) return;\n\n\tEigen::Matrix w_hat = w_m_0 - b_w_lin;\n\tEigen::Matrix a_hat = a_m_0 - b_a_lin - R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tif (imu_avg)\n\t{\n\t\tw_hat += w_m_1 - b_w_lin;\n\t\tw_hat = 0.5 * w_hat;\n\t}\n\n\tEigen::Matrix w_hat_dt = w_hat * delta_t;\n\n\tdouble w_1 = w_hat(0, 0);\n\tdouble w_2 = w_hat(1, 0);\n\tdouble w_3 = w_hat(2, 0);\n\n\tdouble mag_w = w_hat.norm();\n\tdouble w_dt = mag_w * delta_t;\n\n\tbool small_w = (mag_w < 0.008726646);\n\n\tdouble dt_2 = std::pow(delta_t, 2);\n\tdouble cos_wt = std::cos(w_dt);\n\tdouble sin_wt = std::sin(w_dt);\n\n\tEigen::Matrix w_x = quatType::skewSymmetric(w_hat);\n\tEigen::Matrix w_tx = quatType::skewSymmetric(w_hat_dt);\n\tEigen::Matrix w_x_2 = w_x * w_x;\n\n\tEigen::Matrix R_tau1_tau2 = small_w ? Eigen::Matrix::Identity() - delta_t * w_x + (std::pow(delta_t, 2) / 2) * w_x_2 \n\t\t: Eigen::Matrix::Identity() - (sin_wt / mag_w) * w_x + ((1.0 - cos_wt) / (std::pow(mag_w, 2.0))) * w_x_2;\n\n\tEigen::Matrix R_tau1_k = R_tau1_tau2 * R_tau_k;\n\tEigen::Matrix R_k_tau1 = R_tau1_k.transpose();\n\n\tif (imu_avg)\n\t{\n\t\ta_hat += a_m_1 - b_a_lin - R_tau1_k * quatType::quatToRot(q_k_lin) * grav;\n\t\ta_hat = 0.5 * a_hat;\n\t}\n\tEigen::Matrix a_x = quatType::skewSymmetric(a_hat);\n\n\tdouble f_1;\n\tdouble f_2;\n\tdouble f_3;\n\tdouble f_4;\n\n\tif (small_w)\n\t{\n\t\tf_1 = - (std::pow(delta_t, 3) / 3);\n\t\tf_2 = (std::pow(delta_t, 4) / 8);\n\t\tf_3 = - (std::pow(delta_t, 2) / 2);\n\t\tf_4 = (std::pow(delta_t, 3) / 6);\n\t}\n\telse\n\t{\n\t\tf_1 = (w_dt * cos_wt - sin_wt) / (std::pow(mag_w, 3));\n\t\tf_2 = (std::pow(w_dt, 2) - 2 * cos_wt - 2 * w_dt * sin_wt + 2) / (2 * std::pow(mag_w, 4));\n\t\tf_3 = - (1 - cos_wt) / std::pow(mag_w, 2);\n\t\tf_4 = (w_dt - sin_wt) / std::pow(mag_w, 3);\n\t}\n\n\tEigen::Matrix alpha_arg = ((dt_2 / 2.0) * Eigen::Matrix::Identity() + f_1 * w_x + f_2 * w_x_2);\n\tEigen::Matrix Beta_arg = (delta_t * Eigen::Matrix::Identity() + f_3 * w_x + f_4 * w_x_2);\n\n\tEigen::Matrix H_al = R_k_tau1 * alpha_arg;\n\tEigen::Matrix H_be = R_k_tau1 * Beta_arg;\n\n\talpha_tau += beta_tau * delta_t + H_al * a_hat;\n\tbeta_tau += H_be * a_hat;\n\n\tEigen::Matrix J_r_tau1 = small_w ? Eigen::Matrix::Identity() - 0.5 * w_tx + (1.0 / 6.0) * w_tx * w_tx \n\t\t: Eigen::Matrix::Identity() - ((1 - cos_wt) / (std::pow((w_dt), 2.0))) * w_tx + ((w_dt - sin_wt) / (std::pow(w_dt, 3.0))) * w_tx * w_tx;\n\n\tEigen::Matrix J_save = J_q;\n\tJ_q = R_tau1_tau2 * J_q + J_r_tau1 * delta_t;\n\n\tH_a -= H_al;\n\tH_a += delta_t * H_b;\n\tH_b -= H_be;\n\n\tEigen::Matrix g_k = quatType::quatToRot(q_k_lin) * grav;\n\tO_a += delta_t * O_b;\n\tO_a += -H_al * R_tau_k * quatType::skewSymmetric(g_k);\n\tO_b += -H_be * R_tau_k * quatType::skewSymmetric(g_k);\n\n\tEigen::MatrixXd d_R_bw_1 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_1);\n\tEigen::MatrixXd d_R_bw_2 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_2);\n\tEigen::MatrixXd d_R_bw_3 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_3);\n\n\tdouble df_1_dbw_1;\n\tdouble df_1_dbw_2;\n\tdouble df_1_dbw_3;\n\n\tdouble df_2_dbw_1;\n\tdouble df_2_dbw_2;\n\tdouble df_2_dbw_3;\n\n\tdouble df_3_dbw_1;\n\tdouble df_3_dbw_2;\n\tdouble df_3_dbw_3;\n\n\tdouble df_4_dbw_1;\n\tdouble df_4_dbw_2;\n\tdouble df_4_dbw_3;\n\n\tif (small_w)\n\t{\n\t\tdouble df_1_dw_mag = -(std::pow(delta_t, 5) / 15);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(delta_t, 6) / 72);\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = -(std::pow(delta_t, 4) / 12);\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (std::pow(delta_t, 5) / 60);\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\telse\n\t{\n\t\tdouble df_1_dw_mag = (std::pow(w_dt, 2) * sin_wt - 3 * sin_wt + 3 * w_dt * cos_wt) / std::pow(mag_w, 5);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(w_dt, 2) - 4 * cos_wt - 4 * w_dt * sin_wt + std::pow(w_dt, 2) * cos_wt + 4) / (std::pow(mag_w, 6));\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = (2 * (cos_wt - 1) + w_dt * sin_wt) / (std::pow(mag_w, 4));\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (2 * w_dt + w_dt * cos_wt - 3 * sin_wt) / (std::pow(mag_w, 5));\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\n\tEigen::Matrix g_tau = R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tJ_a += J_b * delta_t;\n\tJ_a.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * alpha_arg + R_k_tau1 * (df_1_dbw_1 * w_x - f_1 * e_1x + df_2_dbw_1 * w_x_2 - f_2 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_a.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * alpha_arg + R_k_tau1 * (df_1_dbw_2 * w_x - f_1 * e_2x + df_2_dbw_2 * w_x_2 - f_2 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_a.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * alpha_arg + R_k_tau1 * (df_1_dbw_3 * w_x - f_1 * e_3x + df_2_dbw_3 * w_x_2 - f_2 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\tJ_b.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * Beta_arg + R_k_tau1 * (df_3_dbw_1 * w_x - f_3 * e_1x + df_4_dbw_1 * w_x_2 - f_4 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\t-H_be * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_b.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * Beta_arg + R_k_tau1 * (df_3_dbw_2 * w_x - f_3 * e_2x + df_4_dbw_2 * w_x_2 - f_4 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_b.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * Beta_arg + R_k_tau1 * (df_3_dbw_3 * w_x - f_3 * e_3x + df_4_dbw_3 * w_x_2 - f_4 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\n\tEigen::Matrix R_G_to_k = quatType::quatToRot(q_k_lin);\n\tdouble dt_mid = delta_t / 2.0;\n\tdouble w_dt_mid = mag_w * dt_mid;\n\tEigen::Matrix R_mid;\n\n\tR_mid = small_w ? Eigen::Matrix::Identity() - dt_mid * w_x + (std::pow(dt_mid, 2) / 2) * w_x_2\n\t\t: Eigen::Matrix::Identity() - (std::sin(w_dt_mid) / mag_w) * w_x + ((1.0 - std::cos(w_dt_mid)) / (std::pow(mag_w, 2.0))) * w_x_2;\n\tR_mid = R_mid * R_tau_k;\n\n\tEigen::Matrix F_k1 = Eigen::Matrix::Zero();\n\tF_k1.block(0, 0, 3, 3) = - w_x;\n\tF_k1.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k1.block(6, 0, 3, 3) = - R_tau_k.transpose() * a_x;\n\tF_k1.block(6, 9, 3, 3) = - R_tau_k.transpose();\n\tF_k1.block(6, 15, 3, 3) = - R_tau_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k1.block(6, 18, 3, 3) = - R_tau_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k1.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k1 = Eigen::Matrix::Zero();\n\tG_k1.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k1.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k1.block(6, 6, 3, 3) = - R_tau_k.transpose();\n\tG_k1.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_dot_k1 = F_k1;\n\tEigen::Matrix P_dot_k1 = F_k1 * P_big + P_big * F_k1.transpose() + G_k1 * Q_c * G_k1.transpose();\n\n\tEigen::Matrix F_k2 = Eigen::Matrix::Zero();\n\tF_k2.block(0, 0, 3, 3) = - w_x;\n\tF_k2.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k2.block(6, 0, 3, 3) = - R_mid.transpose() * a_x;\n\tF_k2.block(6, 9, 3, 3) = - R_mid.transpose();\n\tF_k2.block(6, 15, 3, 3) = - R_mid.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k2.block(6, 18, 3, 3) = - R_mid.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k2.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k2 = Eigen::Matrix::Zero();\n\tG_k2.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k2.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k2.block(6, 6, 3, 3) = - R_mid.transpose();\n\tG_k2.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k2 = Eigen::Matrix::Identity() + Phi_dot_k1 * dt_mid;\n\tEigen::Matrix P_k2 = P_big + P_dot_k1 * dt_mid;\n\tEigen::Matrix Phi_dot_k2 = F_k2 * Phi_k2;\n\tEigen::Matrix P_dot_k2 = F_k2 * P_k2 + P_k2 * F_k2.transpose() + G_k2 * Q_c * G_k2.transpose();\n\n\tEigen::Matrix F_k3 = F_k2;\n\tEigen::Matrix G_k3 = G_k2;\n\n\tEigen::Matrix Phi_k3 = Eigen::Matrix::Identity() + Phi_dot_k2 * dt_mid;\n\tEigen::Matrix P_k3 = P_big + P_dot_k2 * dt_mid;\n\tEigen::Matrix Phi_dot_k3 = F_k3 * Phi_k3;\n\tEigen::Matrix P_dot_k3 = F_k3 * P_k3 + P_k3 * F_k3.transpose() + G_k3 * Q_c * G_k3.transpose();\n\n\tEigen::Matrix F_k4 = Eigen::Matrix::Zero();\n\tF_k4.block(0, 0, 3, 3) = - w_x;\n\tF_k4.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k4.block(6, 0, 3, 3) = - R_tau1_k.transpose() * a_x;\n\tF_k4.block(6, 9, 3, 3) = - R_tau1_k.transpose();\n\tF_k4.block(6, 15, 3, 3) = - R_tau1_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k4.block(6, 18, 3, 3) = - R_tau1_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k4.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k4 = Eigen::Matrix::Zero();\n\tG_k4.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k4.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k4.block(6, 6, 3, 3) = - R_tau1_k.transpose();\n\tG_k4.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k4 = Eigen::Matrix::Identity() + Phi_dot_k3 * delta_t;\n\tEigen::Matrix P_k4 = P_big + P_dot_k3 * delta_t;\n\tEigen::Matrix Phi_dot_k4 = F_k4 * Phi_k4;\n\tEigen::Matrix P_dot_k4 = F_k4 * P_k4 + P_k4 * F_k4.transpose() + G_k4 * Q_c * G_k4.transpose();\n\n\tP_big += (delta_t / 6.0) * (P_dot_k1 + 2.0 * P_dot_k2 + 2.0 * P_dot_k3 + P_dot_k4);\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\n\tEigen::Matrix Phi =\n\t\tEigen::Matrix::Identity() + (delta_t / 6.0) * (Phi_dot_k1 + 2.0 * Phi_dot_k2 + 2.0 * Phi_dot_k3 + Phi_dot_k4);\n\n\tEigen::Matrix B_k = Eigen::Matrix::Identity();\n\tB_k.block(15, 15, 3, 3).setZero();\n\tB_k.block(15, 0, 3, 3) = Eigen::Matrix::Identity();\n\n\tP_big = B_k * P_big * B_k.transpose();\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\tDiscrete_J_b = B_k * Phi * Discrete_J_b;\n\n\tP_meas = P_big.block(0, 0, 15, 15);\n\n\tif (state_transition_jacobians)\n\t{\n\t\tJ_q = - Discrete_J_b.block(0, 3, 3, 3);\n\t\tJ_a = Discrete_J_b.block(12, 3, 3, 3);\n\t\tJ_b = Discrete_J_b.block(6, 3, 3, 3);\n\t\tH_a = Discrete_J_b.block(12, 9, 3, 3);\n\t\tH_b = Discrete_J_b.block(6, 9, 3, 3);\n\t\tO_a = Discrete_J_b.block(12, 18, 3, 3);\n\t\tO_b = Discrete_J_b.block(6, 18, 3, 3);\n\t}\n\n\tR_tau_k = R_tau1_k;\n\tq_tau_k = quatType::rotToQuat(R_tau_k);\n}\n private:\n Eigen::Matrix P_big = Eigen::Matrix::Zero();\n Eigen::Matrix Discrete_J_b = Eigen::Matrix::Identity();\n public:\n bool state_transition_jacobians = true;\n Eigen::Matrix O_a = Eigen::Matrix::Zero();\n Eigen::Matrix O_b = Eigen::Matrix::Zero();\n void feedImu(double t_0, double t_1, Eigen::Matrix w_m_0, Eigen::Matrix a_m_0, \n\t\tEigen::Matrix w_m_1 = Eigen::Matrix::Zero(), Eigen::Matrix a_m_1 = Eigen::Matrix::Zero()) {\n\tdouble delta_t = t_1 - t_0;\n\tdt += delta_t;\n\n\tif (delta_t == 0) return;\n\n\tEigen::Matrix w_hat = w_m_0 - b_w_lin;\n\tEigen::Matrix a_hat = a_m_0 - b_a_lin - R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tif (imu_avg)\n\t{\n\t\tw_hat += w_m_1 - b_w_lin;\n\t\tw_hat = 0.5 * w_hat;\n\t}\n\n\tEigen::Matrix w_hat_dt = w_hat * delta_t;\n\n\tdouble w_1 = w_hat(0, 0);\n\tdouble w_2 = w_hat(1, 0);\n\tdouble w_3 = w_hat(2, 0);\n\n\tdouble mag_w = w_hat.norm();\n\tdouble w_dt = mag_w * delta_t;\n\n\tbool small_w = (mag_w < 0.008726646);\n\n\tdouble dt_2 = std::pow(delta_t, 2);\n\tdouble cos_wt = std::cos(w_dt);\n\tdouble sin_wt = std::sin(w_dt);\n\n\tEigen::Matrix w_x = quatType::skewSymmetric(w_hat);\n\tEigen::Matrix w_tx = quatType::skewSymmetric(w_hat_dt);\n\tEigen::Matrix w_x_2 = w_x * w_x;\n\n\tEigen::Matrix R_tau1_tau2 = small_w ? Eigen::Matrix::Identity() - delta_t * w_x + (std::pow(delta_t, 2) / 2) * w_x_2 \n\t\t: Eigen::Matrix::Identity() - (sin_wt / mag_w) * w_x + ((1.0 - cos_wt) / (std::pow(mag_w, 2.0))) * w_x_2;\n\n\tEigen::Matrix R_tau1_k = R_tau1_tau2 * R_tau_k;\n\tEigen::Matrix R_k_tau1 = R_tau1_k.transpose();\n\n\tif (imu_avg)\n\t{\n\t\ta_hat += a_m_1 - b_a_lin - R_tau1_k * quatType::quatToRot(q_k_lin) * grav;\n\t\ta_hat = 0.5 * a_hat;\n\t}\n\tEigen::Matrix a_x = quatType::skewSymmetric(a_hat);\n\n\tdouble f_1;\n\tdouble f_2;\n\tdouble f_3;\n\tdouble f_4;\n\n\tif (small_w)\n\t{\n\t\tf_1 = - (std::pow(delta_t, 3) / 3);\n\t\tf_2 = (std::pow(delta_t, 4) / 8);\n\t\tf_3 = - (std::pow(delta_t, 2) / 2);\n\t\tf_4 = (std::pow(delta_t, 3) / 6);\n\t}\n\telse\n\t{\n\t\tf_1 = (w_dt * cos_wt - sin_wt) / (std::pow(mag_w, 3));\n\t\tf_2 = (std::pow(w_dt, 2) - 2 * cos_wt - 2 * w_dt * sin_wt + 2) / (2 * std::pow(mag_w, 4));\n\t\tf_3 = - (1 - cos_wt) / std::pow(mag_w, 2);\n\t\tf_4 = (w_dt - sin_wt) / std::pow(mag_w, 3);\n\t}\n\n\tEigen::Matrix alpha_arg = ((dt_2 / 2.0) * Eigen::Matrix::Identity() + f_1 * w_x + f_2 * w_x_2);\n\tEigen::Matrix Beta_arg = (delta_t * Eigen::Matrix::Identity() + f_3 * w_x + f_4 * w_x_2);\n\n\tEigen::Matrix H_al = R_k_tau1 * alpha_arg;\n\tEigen::Matrix H_be = R_k_tau1 * Beta_arg;\n\n\talpha_tau += beta_tau * delta_t + H_al * a_hat;\n\tbeta_tau += H_be * a_hat;\n\n\tEigen::Matrix J_r_tau1 = small_w ? Eigen::Matrix::Identity() - 0.5 * w_tx + (1.0 / 6.0) * w_tx * w_tx \n\t\t: Eigen::Matrix::Identity() - ((1 - cos_wt) / (std::pow((w_dt), 2.0))) * w_tx + ((w_dt - sin_wt) / (std::pow(w_dt, 3.0))) * w_tx * w_tx;\n\n\tEigen::Matrix J_save = J_q;\n\tJ_q = R_tau1_tau2 * J_q + J_r_tau1 * delta_t;\n\n\tH_a -= H_al;\n\tH_a += delta_t * H_b;\n\tH_b -= H_be;\n\n\tEigen::Matrix g_k = quatType::quatToRot(q_k_lin) * grav;\n\tO_a += delta_t * O_b;\n\tO_a += -H_al * R_tau_k * quatType::skewSymmetric(g_k);\n\tO_b += -H_be * R_tau_k * quatType::skewSymmetric(g_k);\n\n\tEigen::MatrixXd d_R_bw_1 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_1);\n\tEigen::MatrixXd d_R_bw_2 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_2);\n\tEigen::MatrixXd d_R_bw_3 = - R_k_tau1 * quatType::skewSymmetric(J_q * e_3);\n\n\tdouble df_1_dbw_1;\n\tdouble df_1_dbw_2;\n\tdouble df_1_dbw_3;\n\n\tdouble df_2_dbw_1;\n\tdouble df_2_dbw_2;\n\tdouble df_2_dbw_3;\n\n\tdouble df_3_dbw_1;\n\tdouble df_3_dbw_2;\n\tdouble df_3_dbw_3;\n\n\tdouble df_4_dbw_1;\n\tdouble df_4_dbw_2;\n\tdouble df_4_dbw_3;\n\n\tif (small_w)\n\t{\n\t\tdouble df_1_dw_mag = -(std::pow(delta_t, 5) / 15);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(delta_t, 6) / 72);\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = -(std::pow(delta_t, 4) / 12);\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (std::pow(delta_t, 5) / 60);\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\telse\n\t{\n\t\tdouble df_1_dw_mag = (std::pow(w_dt, 2) * sin_wt - 3 * sin_wt + 3 * w_dt * cos_wt) / std::pow(mag_w, 5);\n\t\tdf_1_dbw_1 = w_1 * df_1_dw_mag;\n\t\tdf_1_dbw_2 = w_2 * df_1_dw_mag;\n\t\tdf_1_dbw_3 = w_3 * df_1_dw_mag;\n\n\t\tdouble df_2_dw_mag = (std::pow(w_dt, 2) - 4 * cos_wt - 4 * w_dt * sin_wt + std::pow(w_dt, 2) * cos_wt + 4) / (std::pow(mag_w, 6));\n\t\tdf_2_dbw_1 = w_1 * df_2_dw_mag;\n\t\tdf_2_dbw_2 = w_2 * df_2_dw_mag;\n\t\tdf_2_dbw_3 = w_3 * df_2_dw_mag;\n\n\t\tdouble df_3_dw_mag = (2 * (cos_wt - 1) + w_dt * sin_wt) / (std::pow(mag_w, 4));\n\t\tdf_3_dbw_1 = w_1 * df_3_dw_mag;\n\t\tdf_3_dbw_2 = w_2 * df_3_dw_mag;\n\t\tdf_3_dbw_3 = w_3 * df_3_dw_mag;\n\n\t\tdouble df_4_dw_mag = (2 * w_dt + w_dt * cos_wt - 3 * sin_wt) / (std::pow(mag_w, 5));\n\t\tdf_4_dbw_1 = w_1 * df_4_dw_mag;\n\t\tdf_4_dbw_2 = w_2 * df_4_dw_mag;\n\t\tdf_4_dbw_3 = w_3 * df_4_dw_mag;\n\t}\n\n\tEigen::Matrix g_tau = R_tau_k * quatType::quatToRot(q_k_lin) * grav;\n\n\tJ_a += J_b * delta_t;\n\tJ_a.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * alpha_arg + R_k_tau1 * (df_1_dbw_1 * w_x - f_1 * e_1x + df_2_dbw_1 * w_x_2 - f_2 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_a.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * alpha_arg + R_k_tau1 * (df_1_dbw_2 * w_x - f_1 * e_2x + df_2_dbw_2 * w_x_2 - f_2 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_a.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * alpha_arg + R_k_tau1 * (df_1_dbw_3 * w_x - f_1 * e_3x + df_2_dbw_3 * w_x_2 - f_2 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_al * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\tJ_b.block(0, 0, 3, 1) +=\n\t\t(d_R_bw_1 * Beta_arg + R_k_tau1 * (df_3_dbw_1 * w_x - f_3 * e_1x + df_4_dbw_1 * w_x_2 - f_4 * (e_1x * w_x + w_x * e_1x))) * a_hat -\n\t\t-H_be * quatType::skewSymmetric((J_save * e_1)) * g_tau;\n\tJ_b.block(0, 1, 3, 1) +=\n\t\t(d_R_bw_2 * Beta_arg + R_k_tau1 * (df_3_dbw_2 * w_x - f_3 * e_2x + df_4_dbw_2 * w_x_2 - f_4 * (e_2x * w_x + w_x * e_2x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_2)) * g_tau;\n\tJ_b.block(0, 2, 3, 1) +=\n\t\t(d_R_bw_3 * Beta_arg + R_k_tau1 * (df_3_dbw_3 * w_x - f_3 * e_3x + df_4_dbw_3 * w_x_2 - f_4 * (e_3x * w_x + w_x * e_3x))) * a_hat -\n\t\tH_be * quatType::skewSymmetric((J_save * e_3)) * g_tau;\n\n\tEigen::Matrix R_G_to_k = quatType::quatToRot(q_k_lin);\n\tdouble dt_mid = delta_t / 2.0;\n\tdouble w_dt_mid = mag_w * dt_mid;\n\tEigen::Matrix R_mid;\n\n\tR_mid = small_w ? Eigen::Matrix::Identity() - dt_mid * w_x + (std::pow(dt_mid, 2) / 2) * w_x_2\n\t\t: Eigen::Matrix::Identity() - (std::sin(w_dt_mid) / mag_w) * w_x + ((1.0 - std::cos(w_dt_mid)) / (std::pow(mag_w, 2.0))) * w_x_2;\n\tR_mid = R_mid * R_tau_k;\n\n\tEigen::Matrix F_k1 = Eigen::Matrix::Zero();\n\tF_k1.block(0, 0, 3, 3) = - w_x;\n\tF_k1.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k1.block(6, 0, 3, 3) = - R_tau_k.transpose() * a_x;\n\tF_k1.block(6, 9, 3, 3) = - R_tau_k.transpose();\n\tF_k1.block(6, 15, 3, 3) = - R_tau_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k1.block(6, 18, 3, 3) = - R_tau_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k1.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k1 = Eigen::Matrix::Zero();\n\tG_k1.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k1.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k1.block(6, 6, 3, 3) = - R_tau_k.transpose();\n\tG_k1.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_dot_k1 = F_k1;\n\tEigen::Matrix P_dot_k1 = F_k1 * P_big + P_big * F_k1.transpose() + G_k1 * Q_c * G_k1.transpose();\n\n\tEigen::Matrix F_k2 = Eigen::Matrix::Zero();\n\tF_k2.block(0, 0, 3, 3) = - w_x;\n\tF_k2.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k2.block(6, 0, 3, 3) = - R_mid.transpose() * a_x;\n\tF_k2.block(6, 9, 3, 3) = - R_mid.transpose();\n\tF_k2.block(6, 15, 3, 3) = - R_mid.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k2.block(6, 18, 3, 3) = - R_mid.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k2.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k2 = Eigen::Matrix::Zero();\n\tG_k2.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k2.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k2.block(6, 6, 3, 3) = - R_mid.transpose();\n\tG_k2.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k2 = Eigen::Matrix::Identity() + Phi_dot_k1 * dt_mid;\n\tEigen::Matrix P_k2 = P_big + P_dot_k1 * dt_mid;\n\tEigen::Matrix Phi_dot_k2 = F_k2 * Phi_k2;\n\tEigen::Matrix P_dot_k2 = F_k2 * P_k2 + P_k2 * F_k2.transpose() + G_k2 * Q_c * G_k2.transpose();\n\n\tEigen::Matrix F_k3 = F_k2;\n\tEigen::Matrix G_k3 = G_k2;\n\n\tEigen::Matrix Phi_k3 = Eigen::Matrix::Identity() + Phi_dot_k2 * dt_mid;\n\tEigen::Matrix P_k3 = P_big + P_dot_k2 * dt_mid;\n\tEigen::Matrix Phi_dot_k3 = F_k3 * Phi_k3;\n\tEigen::Matrix P_dot_k3 = F_k3 * P_k3 + P_k3 * F_k3.transpose() + G_k3 * Q_c * G_k3.transpose();\n\n\tEigen::Matrix F_k4 = Eigen::Matrix::Zero();\n\tF_k4.block(0, 0, 3, 3) = - w_x;\n\tF_k4.block(0, 3, 3, 3) = - Eigen::Matrix::Identity();\n\tF_k4.block(6, 0, 3, 3) = - R_tau1_k.transpose() * a_x;\n\tF_k4.block(6, 9, 3, 3) = - R_tau1_k.transpose();\n\tF_k4.block(6, 15, 3, 3) = - R_tau1_k.transpose() * quatType::skewSymmetric(R_tau_k * R_G_to_k * grav);\n\tF_k4.block(6, 18, 3, 3) = - R_tau1_k.transpose() * R_tau_k * quatType::skewSymmetric(R_G_to_k * grav);\n\tF_k4.block(12, 6, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix G_k4 = Eigen::Matrix::Zero();\n\tG_k4.block(0, 0, 3, 3) = - Eigen::Matrix::Identity();\n\tG_k4.block(3, 3, 3, 3) = Eigen::Matrix::Identity();\n\tG_k4.block(6, 6, 3, 3) = - R_tau1_k.transpose();\n\tG_k4.block(9, 9, 3, 3) = Eigen::Matrix::Identity();\n\n\tEigen::Matrix Phi_k4 = Eigen::Matrix::Identity() + Phi_dot_k3 * delta_t;\n\tEigen::Matrix P_k4 = P_big + P_dot_k3 * delta_t;\n\tEigen::Matrix Phi_dot_k4 = F_k4 * Phi_k4;\n\tEigen::Matrix P_dot_k4 = F_k4 * P_k4 + P_k4 * F_k4.transpose() + G_k4 * Q_c * G_k4.transpose();\n\n\tP_big += (delta_t / 6.0) * (P_dot_k1 + 2.0 * P_dot_k2 + 2.0 * P_dot_k3 + P_dot_k4);\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\n\tEigen::Matrix Phi =\n\t\tEigen::Matrix::Identity() + (delta_t / 6.0) * (Phi_dot_k1 + 2.0 * Phi_dot_k2 + 2.0 * Phi_dot_k3 + Phi_dot_k4);\n\n\tEigen::Matrix B_k = Eigen::Matrix::Identity();\n\tB_k.block(15, 15, 3, 3).setZero();\n\tB_k.block(15, 0, 3, 3) = Eigen::Matrix::Identity();\n\n\tP_big = B_k * P_big * B_k.transpose();\n\tP_big = 0.5 * (P_big + P_big.transpose());\n\tDiscrete_J_b = B_k * Phi * Discrete_J_b;\n\n\tP_meas = P_big.block(0, 0, 15, 15);\n\n\tif (state_transition_jacobians)\n\t{\n\t\tJ_q = - Discrete_J_b.block(0, 3, 3, 3);\n\t\tJ_a = Discrete_J_b.block(12, 3, 3, 3);\n\t\tJ_b = Discrete_J_b.block(6, 3, 3, 3);\n\t\tH_a = Discrete_J_b.block(12, 9, 3, 3);\n\t\tH_b = Discrete_J_b.block(6, 9, 3, 3);\n\t\tO_a = Discrete_J_b.block(12, 18, 3, 3);\n\t\tO_b = Discrete_J_b.block(6, 18, 3, 3);\n\t}\n\n\tR_tau_k = R_tau1_k;\n\tq_tau_k = quatType::rotToQuat(R_tau_k);\n}\n public:\n bool Plus(const double *x, const double *delta, double *x_plus_delta) const override {\n\tEigen::Map q(x);\n\n\tEigen::Map d_th(delta);\n\tEigen::Matrix d_q;\n\tdouble theta = d_th.norm();\n\n\tif (theta < 1e-8)\n\t\td_q << 0.5 * d_th, 1.0;\n\telse\n\t{\n\t\td_q.block(0, 0, 3, 1) = (d_th / theta) * std::sin(theta / 2);\n\t\td_q(3, 0) = std::cos(theta / 2);\n\t}\n\td_q = quatType::quatNorm(d_q);\n\n\tEigen::Map q_plus(x_plus_delta);\n\tq_plus = quatType::quatMultiply(d_q, q);\n\n\treturn true;\n}\n bool ComputeJacobian(const double *x, double *jacobian) const override {\n\tEigen::Map> j(jacobian);\n\tj.topRows<3>().setIdentity();\n\tj.bottomRows<1>().setZero();\n\n\treturn true;\n}\n public:\n Eigen::MatrixXd x_lin;\n std::vector x_type;\n Eigen::MatrixXd sqrt_I;\n Eigen::MatrixXd b;\n factorGenericPrior(const Eigen::MatrixXd &x_lin_, const std::vector &x_type_, const Eigen::MatrixXd &prior_Info, \n\t\tconst Eigen::MatrixXd &prior_grad) {\n\tint state_size = 0;\n\tint state_error_size = 0;\n\n\tfor (auto const &str : x_type_)\n\t{\n\t\tif (str == \"quat\")\n\t\t{\n\t\t\tstate_size += 4;\n\t\t\tstate_error_size += 3;\n\t\t}\n\t\telse if (str == \"quat_yaw\")\n\t\t{\n\t\t\tstate_size += 4;\n\t\t\tstate_error_size += 1;\n\t\t}\n\t\telse if (str == \"vec1\")\n\t\t{\n\t\t\tstate_size += 1;\n\t\t\tstate_error_size += 1;\n\t\t}\n\t\telse if (str == \"vec3\")\n\t\t{\n\t\t\tstate_size += 3;\n\t\t\tstate_error_size += 3;\n\t\t}\n\t\telse if (str == \"vec8\")\n\t\t{\n\t\t\tstate_size += 8;\n\t\t\tstate_error_size += 8;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tstd::cerr << \"type - \" << str << \" not implemented in prior\" << std::endl;\n\t\t\tstd::exit(EXIT_FAILURE);\n\t\t}\n\t}\n\n\tassert(x_lin.rows() == state_size);\n\tassert(x_lin.cols() == 1);\n\tassert(prior_Info.rows() == state_error_size);\n\tassert(prior_Info.cols() == state_error_size);\n\tassert(prior_grad.rows() == state_error_size);\n\tassert(prior_grad.cols() == 1);\n\n\tEigen::LLT llt_of_I(prior_Info);\n\tsqrt_I = llt_of_I.matrixL().transpose();\n\tEigen::MatrixXd I = Eigen::MatrixXd::Identity(prior_Info.rows(), prior_Info.rows());\n\tb = sqrt_I.triangularView().solve(I) * prior_grad;\n\n\tif (std::isnan(prior_Info.norm()) || std::isnan(sqrt_I.norm()) || std::isnan(b.norm()))\n\t{\n\t\tstd::cerr << \"prior_Info - \" << std::endl << prior_Info << std::endl << std::endl;\n\t\tstd::cerr << \"prior_Info_inv - \" << std::endl << prior_Info.inverse() << std::endl << std::endl;\n\t\tstd::cerr << \"b - \" << std::endl << b << std::endl << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tset_num_residuals(state_error_size);\n\n\tfor (auto const &str : x_type_)\n\t{\n\t\tif (str == \"quat\")\n\t\t\tmutable_parameter_block_sizes()->push_back(4);\n\t\tif (str == \"quat_yaw\")\n\t\t\tmutable_parameter_block_sizes()->push_back(4);\n\t\tif (str == \"vec1\")\n\t\t\tmutable_parameter_block_sizes()->push_back(1);\n\t\tif (str == \"vec3\")\n\t\t\tmutable_parameter_block_sizes()->push_back(3);\n\t\tif (str == \"vec8\")\n\t\t\tmutable_parameter_block_sizes()->push_back(8);\n\t}\n}\n bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override {\n\tEigen::Vector4d q_GtoIi = Eigen::Map(parameters[0]);\n\tEigen::Matrix3d R_GtoIi = quatType::quatToRot(q_GtoIi);\n\tEigen::Vector3d p_IiinG = Eigen::Map(parameters[1]);\n\tEigen::Vector3d p_FinG = Eigen::Map(parameters[2]);\n\tEigen::Vector4d q_ItoC = Eigen::Map(parameters[3]);\n\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\tEigen::Vector3d p_IinC = Eigen::Map(parameters[4]);\n\n\tEigen::Matrix camera_vals = Eigen::Map>(parameters[5]);\n\n\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n\tEigen::Vector2d uv_norm;\n\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\tEigen::Matrix sqrt_Q_gate = gate * sqrt_Q;\n\n\tEigen::Vector2d uv_dist;\n\tEigen::MatrixXd H_dz_dzn, H_dz_dzeta;\n\n\tif (is_fisheye)\n\t{\n\t\tcameraEqui cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\telse\n\t{\n\t\tcameraRadtan cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\n\tEigen::Vector2d res = uv_dist - uv_meas;\n\tres = sqrt_Q_gate * res;\n\tresiduals[0] = res(0);\n\tresiduals[1] = res(1);\n\n\tif (jacobians)\n\t{\n\t\tEigen::MatrixXd H_dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\tH_dzn_dpfc << 1.0 / p_FinCi(2), 0, -p_FinCi(0) / std::pow(p_FinCi(2), 2), 0, 1.0 / p_FinCi(2), -p_FinCi(1) / std::pow(p_FinCi(2), 2);\n\t\tEigen::MatrixXd H_dz_dpfc = H_dz_dzn * H_dzn_dpfc;\n\n\t\tif (jacobians[0])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[0]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[1]) {\n\t\t\tEigen::Map> jacobian(jacobians[1]);\n\t\t\tjacobian.block(0, 0, 2, 3) = -H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[2])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[2]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[3])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[3]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * quatType::skewSymmetric(R_ItoC * p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[4])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[4]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc;\n\t\t}\n\n\t\tif (jacobians[5])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[5]);\n\t\t\tjacobian.block(0, 0, 2, 8) = H_dz_dzeta;\n\t\t}\n\t}\n\t\n\treturn true;\n}\n public:\n Eigen::Vector3d alpha;\n Eigen::Vector3d beta;\n Eigen::Vector4d q_breve;\n double dt;\n Eigen::Vector3d b_w_lin_save;\n Eigen::Vector3d b_a_lin_save;\n Eigen::Matrix3d J_q;\n Eigen::Matrix3d J_a;\n Eigen::Matrix3d J_b;\n Eigen::Matrix3d H_a;\n Eigen::Matrix3d H_b;\n Eigen::Matrix sqrt_I_save;\n Eigen::Vector3d grav_save;\n factorImuPreIntegrationV1(double deltatime, Eigen::Vector3d &grav, Eigen::Vector3d &alpha, Eigen::Vector3d &beta, Eigen::Vector4d &q_KtoK1,\n\t\tEigen::Vector3d &ba_lin, Eigen::Vector3d &bg_lin, Eigen::Matrix3d &J_q, Eigen::Matrix3d &J_beta, Eigen::Matrix3d &J_alpha,\n\t\tEigen::Matrix3d &H_beta, Eigen::Matrix3d &H_alpha, Eigen::Matrix &covariance) {\n\tthis->alpha = alpha;\n\tthis->beta = beta;\n\tthis->q_breve = q_KtoK1;\n\tthis->dt = deltatime;\n\tthis->grav_save = grav;\n\n\tthis->b_a_lin_save = ba_lin;\n\tthis->b_w_lin_save = bg_lin;\n\n\tthis->J_q = J_q;\n\tthis->J_a = J_alpha;\n\tthis->J_b = J_beta;\n\tthis->H_a = H_alpha;\n\tthis->H_b = H_beta;\n\n\tEigen::MatrixXd I = Eigen::MatrixXd::Identity(covariance.rows(), covariance.rows());\n\tEigen::MatrixXd information = covariance.llt().solve(I);\n \n\tif (std::isnan(information.norm()))\n\t{\n\t\tstd::cerr << \"P - \" << std::endl << covariance << std::endl << std::endl;\n\t\tstd::cerr << \"Pinv - \" << std::endl << covariance.inverse() << std::endl << std::endl;\n\t\tstd::exit(EXIT_FAILURE);\n\t}\n\n\tEigen::LLT llt_of_I(information);\n\tsqrt_I_save = llt_of_I.matrixL().transpose();\n\n\tset_num_residuals(15);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n}\n bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override {\n\tEigen::Vector4d q_GtoIi = Eigen::Map(parameters[0]);\n\tEigen::Matrix3d R_GtoIi = quatType::quatToRot(q_GtoIi);\n\tEigen::Vector3d p_IiinG = Eigen::Map(parameters[1]);\n\tEigen::Vector3d p_FinG = Eigen::Map(parameters[2]);\n\tEigen::Vector4d q_ItoC = Eigen::Map(parameters[3]);\n\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\tEigen::Vector3d p_IinC = Eigen::Map(parameters[4]);\n\n\tEigen::Matrix camera_vals = Eigen::Map>(parameters[5]);\n\n\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n\tEigen::Vector2d uv_norm;\n\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\tEigen::Matrix sqrt_Q_gate = gate * sqrt_Q;\n\n\tEigen::Vector2d uv_dist;\n\tEigen::MatrixXd H_dz_dzn, H_dz_dzeta;\n\n\tif (is_fisheye)\n\t{\n\t\tcameraEqui cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\telse\n\t{\n\t\tcameraRadtan cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\n\tEigen::Vector2d res = uv_dist - uv_meas;\n\tres = sqrt_Q_gate * res;\n\tresiduals[0] = res(0);\n\tresiduals[1] = res(1);\n\n\tif (jacobians)\n\t{\n\t\tEigen::MatrixXd H_dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\tH_dzn_dpfc << 1.0 / p_FinCi(2), 0, -p_FinCi(0) / std::pow(p_FinCi(2), 2), 0, 1.0 / p_FinCi(2), -p_FinCi(1) / std::pow(p_FinCi(2), 2);\n\t\tEigen::MatrixXd H_dz_dpfc = H_dz_dzn * H_dzn_dpfc;\n\n\t\tif (jacobians[0])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[0]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[1]) {\n\t\t\tEigen::Map> jacobian(jacobians[1]);\n\t\t\tjacobian.block(0, 0, 2, 3) = -H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[2])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[2]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[3])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[3]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * quatType::skewSymmetric(R_ItoC * p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[4])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[4]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc;\n\t\t}\n\n\t\tif (jacobians[5])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[5]);\n\t\t\tjacobian.block(0, 0, 2, 8) = H_dz_dzeta;\n\t\t}\n\t}\n\t\n\treturn true;\n}\n public:\n Eigen::Vector2d uv_meas;\n double pix_sigma = 1.0;\n Eigen::Matrix sqrt_Q;\n bool is_fisheye = false;\n double gate = 1.0;\n factorImageReprojCalib(const Eigen::Vector2d &uv_meas_, double pix_sigma_, bool is_fisheye_) {\n\tsqrt_Q = Eigen::Matrix::Identity();\n\tsqrt_Q(0, 0) *= 1.0 / pix_sigma;\n\tsqrt_Q(1, 1) *= 1.0 / pix_sigma;\n\n\tset_num_residuals(2);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(4);\n\tmutable_parameter_block_sizes()->push_back(3);\n\tmutable_parameter_block_sizes()->push_back(8);\n}\n bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override {\n\tEigen::Vector4d q_GtoIi = Eigen::Map(parameters[0]);\n\tEigen::Matrix3d R_GtoIi = quatType::quatToRot(q_GtoIi);\n\tEigen::Vector3d p_IiinG = Eigen::Map(parameters[1]);\n\tEigen::Vector3d p_FinG = Eigen::Map(parameters[2]);\n\tEigen::Vector4d q_ItoC = Eigen::Map(parameters[3]);\n\tEigen::Matrix3d R_ItoC = quatType::quatToRot(q_ItoC);\n\tEigen::Vector3d p_IinC = Eigen::Map(parameters[4]);\n\n\tEigen::Matrix camera_vals = Eigen::Map>(parameters[5]);\n\n\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n\tEigen::Vector2d uv_norm;\n\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\tEigen::Matrix sqrt_Q_gate = gate * sqrt_Q;\n\n\tEigen::Vector2d uv_dist;\n\tEigen::MatrixXd H_dz_dzn, H_dz_dzeta;\n\n\tif (is_fisheye)\n\t{\n\t\tcameraEqui cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\telse\n\t{\n\t\tcameraRadtan cam(0, 0);\n\t\tcam.setValue(camera_vals);\n\t\tuv_dist = cam.distortD(uv_norm);\n\t\tif (jacobians)\n\t\t{\n\t\t\tcam.computeDistortJacobian(uv_norm, H_dz_dzn, H_dz_dzeta);\n\t\t\tH_dz_dzn = sqrt_Q_gate * H_dz_dzn;\n\t\t\tH_dz_dzeta = sqrt_Q_gate * H_dz_dzeta;\n\t\t}\n\t}\n\n\tEigen::Vector2d res = uv_dist - uv_meas;\n\tres = sqrt_Q_gate * res;\n\tresiduals[0] = res(0);\n\tresiduals[1] = res(1);\n\n\tif (jacobians)\n\t{\n\t\tEigen::MatrixXd H_dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\tH_dzn_dpfc << 1.0 / p_FinCi(2), 0, -p_FinCi(0) / std::pow(p_FinCi(2), 2), 0, 1.0 / p_FinCi(2), -p_FinCi(1) / std::pow(p_FinCi(2), 2);\n\t\tEigen::MatrixXd H_dz_dpfc = H_dz_dzn * H_dzn_dpfc;\n\n\t\tif (jacobians[0])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[0]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[1]) {\n\t\t\tEigen::Map> jacobian(jacobians[1]);\n\t\t\tjacobian.block(0, 0, 2, 3) = -H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[2])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[2]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * R_ItoC * R_GtoIi;\n\t\t}\n\n\t\tif (jacobians[3])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[3]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc * quatType::skewSymmetric(R_ItoC * p_FinIi);\n\t\t\tjacobian.block(0, 3, 2, 1).setZero();\n\t\t}\n\n\t\tif (jacobians[4])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[4]);\n\t\t\tjacobian.block(0, 0, 2, 3) = H_dz_dpfc;\n\t\t}\n\n\t\tif (jacobians[5])\n\t\t{\n\t\t\tEigen::Map> jacobian(jacobians[5]);\n\t\t\tjacobian.block(0, 0, 2, 8) = H_dz_dzeta;\n\t\t}\n\t}\n\t\n\treturn true;\n}\n};"], ["/voxel_svio/include/stereoVio.h", "class voxelStereoVio {\n private:\n void processImu(imuData &imu_data) {\n double oldest_time = state_ptr->margtimestep();\n\n if (oldest_time > state_ptr->timestamp)\n oldest_time = -1;\n\n if (!initial_flag)\n oldest_time = imu_data.timestamp - odometry_options.init_options.init_window_time + state_ptr->calib_dt_imu_cam->value()(0) - 0.10;\n\n propagator_ptr->feedImu(imu_data, oldest_time);\n\n if (!initial_flag)\n initializer_ptr->feedImu(imu_data, oldest_time);\n}\n void processImage(cameraData &image_measurements_const) {\n rT1 = boost::posix_time::microsec_clock::local_time();\n\n assert(!image_measurements_const.camera_ids.empty());\n assert(image_measurements_const.camera_ids.size() == image_measurements_const.images.size());\n \n for (size_t i = 0; i < image_measurements_const.camera_ids.size() - 1; i++)\n assert(image_measurements_const.camera_ids.at(i) != image_measurements_const.camera_ids.at(i + 1));\n\n // time test\n boost::posix_time::ptime rT_begin7 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n cameraData image_measurements = image_measurements_const;\n\n for (size_t i = 0; i < image_measurements.camera_ids.size() && odometry_options.downsample_cameras; i++)\n {\n cv::Mat img = image_measurements.images.at(i);\n cv::Mat mask = image_measurements.masks.at(i);\n cv::Mat img_temp, mask_temp;\n cv::pyrDown(img, img_temp, cv::Size(img.cols / 2.0, img.rows / 2.0));\n image_measurements.images.at(i) = img_temp;\n cv::pyrDown(mask, mask_temp, cv::Size(mask.cols / 2.0, mask.rows / 2.0));\n image_measurements.masks.at(i) = mask_temp;\n }\n\n std::shared_ptr fh = std::make_shared();\n fh->makeImageLeft(image_measurements.images.at(0).data, image_measurements.masks.at(0).data, gammaPixel_ptr, image_measurements.timestamp);\n fh->makeImageRight(image_measurements.images.at(1).data, image_measurements.masks.at(1).data, gammaPixel_ptr, image_measurements.timestamp);\n fh->frame_id = frame_count;\n\n // time test\n boost::posix_time::ptime rT_end7 = boost::posix_time::microsec_clock::local_time();\n double time_cost = (rT_end7 - rT_begin7).total_microseconds() * 1e-6;\n sum_time_7 = sum_time_7 * n_time_7 + time_cost;\n n_time_7++;\n sum_time_7 = sum_time_7 / n_time_7;\n std::cout << std::fixed << std::setprecision(6) << \"[image processing] time cost = \" << sum_time_7 << std::endl;\n // time test\n\n newest_fh = fh;\n\n // time test\n boost::posix_time::ptime rT_begin1 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n featureTracker->feedNewImage(image_measurements, fh);\n\n // time test\n boost::posix_time::ptime rT_end1 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end1 - rT_begin1).total_microseconds() * 1e-6;\n sum_time_1 = sum_time_1 * n_time_1 + time_cost;\n n_time_1++;\n sum_time_1 = sum_time_1 / n_time_1;\n std::cout << std::fixed << std::setprecision(6) << \"[feature extraction and matching] time cost = \" << sum_time_1 << std::endl;\n // time test\n\n rT2 = boost::posix_time::microsec_clock::local_time();\n\n if (!initial_flag)\n {\n initial_flag = tryToInitialize(image_measurements);\n\n if (!initial_flag)\n {\n double time_track = (rT2 - rT1).total_microseconds() * 1e-6;\n std::cout << std::fixed << \"[processImage]: \" << time_track << \" seconds for tracking\" << std::endl;\n return;\n }\n }\n\n if (featureTracker->getFeatureDatabase()->featureCheckParallax(frame_count))\n marginalize_status = MarginalizeStatus::OLD;\n else\n {\n if (odometry_options.use_keyframe)\n marginalize_status = MarginalizeStatus::NEW;\n else\n marginalize_status = MarginalizeStatus::OLD;\n }\n\n std::cout << \"marginalize_status = \" << marginalize_status << std::endl;\n\n featureUpdate(image_measurements);\n\n frame_count++;\n}\n void featureUpdate(cameraData &image_measurements) {\n if (state_ptr->timestamp > image_measurements.timestamp)\n {\n std::cout << std::fixed << \"Image received out of order, unable to do anything (prop dt = \" << image_measurements.timestamp - state_ptr->timestamp << \")\" << std::endl;\n return;\n }\n\n if (state_ptr->timestamp != image_measurements.timestamp)\n {\n propagator_ptr->propagateAndClone(state_ptr, image_measurements.timestamp);\n\n if (state_ptr->clones_frame.find(image_measurements.timestamp) == state_ptr->clones_frame.end())\n state_ptr->clones_frame[image_measurements.timestamp] = newest_fh;\n\n assert(state_ptr->clones_imu.size() == state_ptr->clones_frame.size());\n }\n\n rT3 = boost::posix_time::microsec_clock::local_time();\n\n if ((int)state_ptr->clones_imu.size() < std::min(state_ptr->options.max_clone_size, 5))\n {\n std::cout << \"Waiting for enough clone states (\" << (int)state_ptr->clones_imu.size() << \" of \" \n << std::min(state_ptr->options.max_clone_size, 5) << \")\" << std::endl;\n return;\n }\n\n if (state_ptr->timestamp > image_measurements.timestamp)\n {\n std::cout << \"Msckf unable to propagate the state forward in time\" << std::endl;\n std::cout << std::fixed << \"It has been \" << image_measurements.timestamp - state_ptr->timestamp << \" since last time we propagated\" << std::endl;\n return;\n }\n\n std::vector> features_lost, features_marg, features_slam;\n features_lost = featureTracker->getFeatureDatabase()->getOldFeatures(state_ptr->timestamp, false, true);\n\n if ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size || (int)state_ptr->clones_imu.size() > 5)\n {\n features_marg = featureTracker->getFeatureDatabase()->getFeatures(state_ptr->margtimestep(), false, true);\n }\n\n auto it_1 = features_lost.begin();\n while (it_1 != features_lost.end())\n {\n bool found_current_cam_id = false;\n for (const auto &cam_uv_pair : (*it_1)->uvs)\n {\n if (std::find(image_measurements.camera_ids.begin(), image_measurements.camera_ids.end(), cam_uv_pair.first) \n != image_measurements.camera_ids.end())\n {\n found_current_cam_id = true;\n break;\n }\n }\n\n if (found_current_cam_id)\n {\n it_1++;\n }\n else \n {\n it_1 = features_lost.erase(it_1);\n }\n }\n\n it_1 = features_lost.begin();\n while (it_1 != features_lost.end())\n {\n if (std::find(features_marg.begin(), features_marg.end(), (*it_1)) != features_marg.end())\n it_1 = features_lost.erase(it_1);\n else\n it_1++;\n }\n\n std::vector> features_maxtracks;\n auto it_2 = features_marg.begin();\n\n while (it_2 != features_marg.end())\n {\n bool reached_max = false;\n \n for (const auto &cams : (*it_2)->timestamps)\n {\n if ((int)cams.second.size() > state_ptr->options.max_clone_size)\n {\n reached_max = true;\n break;\n }\n }\n \n if (reached_max)\n {\n features_maxtracks.push_back(*it_2);\n it_2 = features_marg.erase(it_2);\n }\n else\n {\n it_2++;\n }\n }\n\n if (state_ptr->options.max_slam_features > 0 && image_measurements.timestamp - startup_time >= odometry_options.dt_slam_delay &&\n (int)state_ptr->map_points.size() < state_ptr->options.max_slam_features)\n {\n int amount_to_add = (state_ptr->options.max_slam_features) - (int)state_ptr->map_points.size();\n int valid_amount = (amount_to_add > (int)features_maxtracks.size()) ? (int)features_maxtracks.size() : amount_to_add;\n\n if (valid_amount > 0)\n {\n features_slam.insert(features_slam.end(), features_maxtracks.end() - valid_amount, features_maxtracks.end());\n features_maxtracks.erase(features_maxtracks.end() - valid_amount, features_maxtracks.end());\n }\n }\n\n for (int i = 0; i < recent_voxels.size(); i++)\n {\n short kx = recent_voxels[i][0];\n short ky = recent_voxels[i][1];\n short kz = recent_voxels[i][2];\n\n voxelHashMap::iterator search = voxel_map.find(voxel(kx, ky, kz));\n auto &voxel_block = (search.value());\n\n if (odometry_options.use_all_points)\n {\n for (int j = 0; j < voxel_block.points.size(); j++)\n {\n std::shared_ptr map_point_ptr = voxel_block.points[j];\n\n std::shared_ptr feature = featureTracker->getFeatureDatabase()->getFeature(map_point_ptr->feature_id);\n if (feature != nullptr)\n features_slam.push_back(feature);\n\n assert(map_point_ptr->unique_camera_id != -1);\n\n bool current_host_cam = std::find(image_measurements.camera_ids.begin(), image_measurements.camera_ids.end(), map_point_ptr->unique_camera_id) != image_measurements.camera_ids.end();\n\n if (feature == nullptr && current_host_cam)\n map_point_ptr->should_marg = true;\n\n if (map_point_ptr->update_fail_count > 1)\n map_point_ptr->should_marg = true;\n }\n }\n else\n {\n std::shared_ptr map_point_ptr = voxel_block.points.front();\n\n std::shared_ptr feature = featureTracker->getFeatureDatabase()->getFeature(map_point_ptr->feature_id);\n if (feature != nullptr)\n features_slam.push_back(feature);\n\n assert(map_point_ptr->unique_camera_id != -1);\n\n bool current_host_cam = std::find(image_measurements.camera_ids.begin(), image_measurements.camera_ids.end(), map_point_ptr->unique_camera_id) != image_measurements.camera_ids.end();\n\n if (feature == nullptr && current_host_cam)\n map_point_ptr->should_marg = true;\n\n if (map_point_ptr->update_fail_count > 1)\n map_point_ptr->should_marg = true;\n }\n }\n\n // time test\n boost::posix_time::ptime rT_begin6 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n stateHelper::marginalizeSlam(state_ptr, voxel_map, points_history);\n\n // time test\n boost::posix_time::ptime rT_end6 = boost::posix_time::microsec_clock::local_time();\n double time_cost = (rT_end6 - rT_begin6).total_microseconds() * 1e-6;\n sum_time_6 = sum_time_6 * n_time_6 + time_cost;\n n_time_6++;\n sum_time_6 = sum_time_6 / n_time_6;\n std::cout << std::fixed << std::setprecision(6) << \"[marginalizeSlam] time cost = \" << sum_time_6 << std::endl;\n // time test\n\n std::vector> features_slam_delayed, features_slam_update;\n\n for (size_t i = 0; i < features_slam.size(); i++)\n {\n if (state_ptr->map_points.find(features_slam.at(i)->feature_id) != state_ptr->map_points.end())\n {\n features_slam_update.push_back(features_slam.at(i));\n }\n else\n {\n features_slam_delayed.push_back(features_slam.at(i));\n }\n }\n\n std::vector> features_up_msckf = features_lost;\n features_up_msckf.insert(features_up_msckf.end(), features_marg.begin(), features_marg.end());\n features_up_msckf.insert(features_up_msckf.end(), features_maxtracks.begin(), features_maxtracks.end());\n\n auto compare_feature = [](const std::shared_ptr &a, const std::shared_ptr &b) -> bool {\n size_t size_a = 0;\n size_t size_b = 0;\n \n for (const auto &pair : a->timestamps)\n size_a += pair.second.size();\n \n for (const auto &pair : b->timestamps)\n size_b += pair.second.size();\n\n return size_a < size_b;\n };\n std::sort(features_up_msckf.begin(), features_up_msckf.end(), compare_feature);\n\n if ((int)features_up_msckf.size() > state_ptr->options.max_msckf_in_update)\n features_up_msckf.erase(features_up_msckf.begin(), features_up_msckf.end() - state_ptr->options.max_msckf_in_update);\n\n // time test\n boost::posix_time::ptime rT_begin2 = boost::posix_time::microsec_clock::local_time();\n // time test\n \n updaterMsckf_ptr->update(state_ptr, features_up_msckf);\n\n // time test\n boost::posix_time::ptime rT_end2 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end2 - rT_begin2).total_microseconds() * 1e-6;\n sum_time_2 = sum_time_2 * n_time_2 + time_cost;\n n_time_2++;\n sum_time_2 = sum_time_2 / n_time_2;\n std::cout << std::fixed << std::setprecision(6) << \"[state update without map points] time cost = \" << sum_time_2 << std::endl;\n // time test\n\n propagator_ptr->invalidateCache();\n\n rT4 = boost::posix_time::microsec_clock::local_time();\n\n std::vector> features_slam_update_temp;\n\n while (!features_slam_update.empty())\n {\n std::vector> features_up_temp;\n \n features_up_temp.insert(features_up_temp.begin(), features_slam_update.begin(), \n features_slam_update.begin() + std::min(state_ptr->options.max_slam_in_update, (int)features_slam_update.size()));\n \n features_slam_update.erase(features_slam_update.begin(), \n features_slam_update.begin() + std::min(state_ptr->options.max_slam_in_update, (int)features_slam_update.size()));\n\n // time test\n boost::posix_time::ptime rT_begin3 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n updaterSlam_ptr->update(state_ptr, voxel_map, features_up_temp);\n\n // time test\n boost::posix_time::ptime rT_end3 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end3 - rT_begin3).total_microseconds() * 1e-6;\n sum_time_3 = sum_time_3 * n_time_3 + time_cost;\n n_time_3++;\n sum_time_3 = sum_time_3 / n_time_3;\n std::cout << std::fixed << std::setprecision(6) << \"[state update without map points] time cost = \" << sum_time_3 << std::endl;\n // time test\n\n auto it = state_ptr->map_points.begin();\n\n while (it != state_ptr->map_points.end())\n {\n mapManagement::changeHostVoxel(voxel_map, (*it).second, odometry_options.voxel_size, odometry_options.max_num_points_in_voxel, odometry_options.min_distance_points, voxels_history);\n it++;\n }\n\n features_slam_update_temp.insert(features_slam_update_temp.end(), features_up_temp.begin(), features_up_temp.end());\n propagator_ptr->invalidateCache();\n }\n\n features_slam_update = features_slam_update_temp;\n rT5 = boost::posix_time::microsec_clock::local_time();\n\n // time test\n boost::posix_time::ptime rT_begin4 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n updaterSlam_ptr->delayedInit(state_ptr, voxel_map, features_slam_delayed, voxels_history);\n\n // time test\n boost::posix_time::ptime rT_end4 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end4 - rT_begin4).total_microseconds() * 1e-6;\n sum_time_4 = sum_time_4 * n_time_4 + time_cost;\n n_time_4++;\n sum_time_4 = sum_time_4 / n_time_4;\n std::cout << std::fixed << std::setprecision(6) << \"[map point registration] time cost = \" << sum_time_4 << std::endl;\n // time test\n\n rT6 = boost::posix_time::microsec_clock::local_time();\n\n if (image_measurements.camera_ids.at(0) == 0)\n {\n // time test\n boost::posix_time::ptime rT_begin5 = boost::posix_time::microsec_clock::local_time();\n // time test\n\n triangulateActiveTracks(image_measurements);\n\n getRecentVoxel(image_measurements.timestamp, voxels_visit);\n\n // time test\n boost::posix_time::ptime rT_end5 = boost::posix_time::microsec_clock::local_time();\n time_cost = (rT_end5 - rT_begin5).total_microseconds() * 1e-6;\n sum_time_5 = sum_time_5 * n_time_5 + time_cost;\n n_time_5++;\n sum_time_5 = sum_time_5 / n_time_5;\n std::cout << std::fixed << std::setprecision(6) << \"[suitable map point selection] time cost = \" << sum_time_5 << std::endl;\n // time test\n\n good_features_msckf.clear();\n }\n\n for (auto const &feat : features_up_msckf)\n {\n good_features_msckf.push_back(feat->position_global);\n feat->to_delete = true;\n }\n\n featureTracker->getFeatureDatabase()->cleanUp();\n\n if ((int)state_ptr->clones_imu.size() > state_ptr->options.max_clone_size)\n {\n featureTracker->getFeatureDatabase()->cleanUpOldmeasurements(state_ptr->margtimestep());\n }\n\n if (marginalize_status == MarginalizeStatus::OLD)\n {\n stateHelper::marginalizeOldClone(state_ptr);\n }\n else\n {\n stateHelper::marginalizeNewClone(state_ptr);\n }\n\n rT7 = boost::posix_time::microsec_clock::local_time();\n\n double time_track = (rT2 - rT1).total_microseconds() * 1e-6;\n double time_prop = (rT3 - rT2).total_microseconds() * 1e-6;\n double time_msckf = (rT4 - rT3).total_microseconds() * 1e-6;\n double time_slam_update = (rT5 - rT4).total_microseconds() * 1e-6;\n double time_slam_delay = (rT6 - rT5).total_microseconds() * 1e-6;\n double time_marg = (rT7 - rT6).total_microseconds() * 1e-6;\n double time_total = (rT7 - rT1).total_microseconds() * 1e-6;\n\n // Time test\n /*\n std::cout << std::fixed << \"[processImage]: \" << time_track << \" seconds for tracking\" << std::endl;\n std::cout << std::fixed << \"[featureUpdate]: \" << time_prop << \" seconds for propagation\" << std::endl;\n std::cout << std::fixed << \"[featureUpdate]: \" << time_msckf << \" seconds for MSCKF update (\" << (int)features_up_msckf.size() << \" feats)\" << std::endl;\n \n if (state_ptr->options.max_slam_features > 0)\n {\n std::cout << std::fixed << \"[featureUpdate]: \" << time_slam_update << \" seconds for SLAM update (\" << (int)state_ptr->map_points.size() << \" feats)\" << std::endl;\n std::cout << std::fixed << \"[featureUpdate]: \" << time_slam_delay << \" seconds for SLAM delayed init (\" << (int)features_slam_delayed.size() << \" feats)\" << std::endl;\n }\n\n std::cout << std::fixed << \"[featureUpdate]: \" << time_marg << \" seconds for re-tri & marg (\" << (int)state_ptr->clones_imu.size() << \" clones in state)\" << std::endl;\n\n std::stringstream ss;\n ss << \"[featureUpdate]: \" << std::setprecision(4) << time_total << \" seconds for total (camera\";\n\n for (const auto &id : image_measurements.camera_ids) ss << \" \" << id;\n\n ss << \")\" << std::endl;\n\n std::cout << ss.str();\n */\n // Time test\n\n if (timelastupdate != -1 && state_ptr->clones_imu.find(timelastupdate) != state_ptr->clones_imu.end())\n {\n Eigen::Matrix dx = state_ptr->imu_ptr->getPos() - state_ptr->clones_imu.at(timelastupdate)->getPos();\n distance += dx.norm();\n }\n timelastupdate = image_measurements.timestamp;\n\n // display\n pubOdometry(state_ptr, timelastupdate);\n pubPath(state_ptr, timelastupdate);\n pubHistoryPoints(points_history, timelastupdate);\n pubWindowPoints(state_ptr, timelastupdate);\n pubHistoryVoxels(voxels_history, timelastupdate);\n pubVisitVoxels(voxels_visit, timelastupdate);\n // display\n\n std::cout << std::fixed << std::setprecision(3) << \"q_GtoI = \" << state_ptr->imu_ptr->getQuat()(0) << \" \" << state_ptr->imu_ptr->getQuat()(1) << \" \"\n << state_ptr->imu_ptr->getQuat()(2) << \" \" << state_ptr->imu_ptr->getQuat()(3) << std::endl;\n\n std::cout << std::fixed << std::setprecision(3) << \"p_IinG = \" << state_ptr->imu_ptr->getPos()(0) << \" \" << state_ptr->imu_ptr->getPos()(1) << \" \"\n << state_ptr->imu_ptr->getPos()(2) << std::endl;\n\n std::cout << std::fixed << std::setprecision(2) << \"dist = \" << distance << \" meters\" << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"bg = \" << state_ptr->imu_ptr->getBg()(0) << \" \" << state_ptr->imu_ptr->getBg()(1) << \" \"\n << state_ptr->imu_ptr->getBg()(2) << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"ba = \" << state_ptr->imu_ptr->getBa()(0) << \" \" << state_ptr->imu_ptr->getBa()(1) << \" \"\n << state_ptr->imu_ptr->getBa()(2) << std::endl;\n\n std::ofstream foutC(std::string(output_path + \"/pose.txt\"), std::ios::app);\n foutC.setf(std::ios::scientific, std::ios::floatfield);\n foutC.precision(6);\n foutC << std::fixed << image_measurements.timestamp + state_ptr->calib_dt_imu_cam->value()(0) << \" \" << state_ptr->imu_ptr->getPos()(0) << \" \" << state_ptr->imu_ptr->getPos()(1) << \" \" << state_ptr->imu_ptr->getPos()(2) << \" \" \n << state_ptr->imu_ptr->getQuat()(0) << \" \" << state_ptr->imu_ptr->getQuat()(1) << \" \" << state_ptr->imu_ptr->getQuat()(2) << \" \" << state_ptr->imu_ptr->getQuat()(3) << std::endl;\n foutC.close();\n\n if (state_ptr->options.do_calib_camera_timeoffset)\n std::cout << std::fixed << std::setprecision(5) << \"camera-imu timeoffset = \" << state_ptr->calib_dt_imu_cam->value()(0) << std::endl;\n\n if (state_ptr->options.do_calib_camera_intrinsics)\n {\n for (int i = 0; i < 2; i++)\n {\n std::shared_ptr calib = state_ptr->cam_intrinsics.at(i);\n\n std::cout << std::fixed << std::setprecision(3) << \"cam\" << (int)i << \" intrinsics = \" << calib->value()(0) << \" \" << calib->value()(1) << \" \"\n << calib->value()(2) << \" \" << calib->value()(3) << \" \" << calib->value()(4) << \" \" << calib->value()(5) << \" \"\n << calib->value()(6) << \" \" << calib->value()(7) << std::endl;\n\n }\n }\n\n if (state_ptr->options.do_calib_camera_pose)\n {\n for (int i = 0; i < 2; i++)\n {\n std::shared_ptr calib = state_ptr->calib_cam_imu.at(i);\n\n std::cout << std::fixed << std::setprecision(3) << \"cam\" << (int)i << \" extrinsics = \" << calib->getQuat()(0) << \" \" << calib->getQuat()(1) << \" \"\n << calib->getQuat()(2) << \" \" << calib->getQuat()(3) << \" \" << calib->getPos()(0) << \" \" << calib->getPos()(1) << \" \"\n << calib->getPos()(2) << std::endl;\n }\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::KALIBR)\n {\n std::cout << std::fixed << std::setprecision(3) << \"q_GYROtoI = \" << state_ptr->calib_imu_gyr->value()(0) << \" \" << state_ptr->calib_imu_gyr->value()(1) << \" \"\n << state_ptr->calib_imu_gyr->value()(2) << \" \" << state_ptr->calib_imu_gyr->value()(3) << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::RPNG)\n {\n std::cout << std::fixed << std::setprecision(3) << \"q_ACCtoI = \" << state_ptr->calib_imu_acc->value()(0) << \" \" << state_ptr->calib_imu_acc->value()(1) << \" \"\n << state_ptr->calib_imu_acc->value()(2) << \" \" << state_ptr->calib_imu_acc->value()(3) << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::KALIBR)\n {\n std::cout << std::fixed << std::setprecision(4) << \"Dw = | \" << state_ptr->calib_imu_dw->value()(0) << \", \" << state_ptr->calib_imu_dw->value()(1) << \", \"\n << state_ptr->calib_imu_dw->value()(2) << \" | \" << state_ptr->calib_imu_dw->value()(3) << \", \" << state_ptr->calib_imu_dw->value()(4)\n << \" | \" << state_ptr->calib_imu_dw->value()(5) << \" |\" << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"Da = | \" << state_ptr->calib_imu_da->value()(0) << \", \" << state_ptr->calib_imu_da->value()(1) << \", \"\n << state_ptr->calib_imu_da->value()(2) << \" | \" << state_ptr->calib_imu_da->value()(3) << \", \" << state_ptr->calib_imu_da->value()(4)\n << \" | \" << state_ptr->calib_imu_da->value()(5) << \" |\" << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.imu_model == ImuModel::RPNG)\n {\n std::cout << std::fixed << std::setprecision(4) << \"Dw = | \" << state_ptr->calib_imu_dw->value()(0) << \" | \" << state_ptr->calib_imu_dw->value()(1) << \", \"\n << state_ptr->calib_imu_dw->value()(2) << \" | \" << state_ptr->calib_imu_dw->value()(3) << \", \" << state_ptr->calib_imu_dw->value()(4)\n << \", \" << state_ptr->calib_imu_dw->value()(5) << \" |\" << std::endl;\n\n std::cout << std::fixed << std::setprecision(4) << \"Da = | \" << state_ptr->calib_imu_da->value()(0) << \" | \" << state_ptr->calib_imu_da->value()(1) << \", \"\n << state_ptr->calib_imu_da->value()(2) << \" | \" << state_ptr->calib_imu_da->value()(3) << \", \" << state_ptr->calib_imu_da->value()(4)\n << \", \" << state_ptr->calib_imu_da->value()(5) << \" |\" << std::endl;\n }\n\n if (state_ptr->options.do_calib_imu_intrinsics && state_ptr->options.do_calib_imu_g_sensitivity)\n {\n std::cout << std::fixed << std::setprecision(4) << \"Tg = | \" << state_ptr->calib_imu_tg->value()(0) << \", \" << state_ptr->calib_imu_tg->value()(1) << \", \"\n << state_ptr->calib_imu_tg->value()(2) << \" | \" << state_ptr->calib_imu_tg->value()(3) << \", \" << state_ptr->calib_imu_tg->value()(4) << \", \"\n << state_ptr->calib_imu_tg->value()(5) << \" | \" << state_ptr->calib_imu_tg->value()(6) << \", \" << state_ptr->calib_imu_tg->value()(7) << \", \"\n << state_ptr->calib_imu_tg->value()(8) << \" |\" << std::endl;\n }\n\n std::cout << std::endl;\n}\n void triangulateActiveTracks(cameraData &image_measurements) {\n boost::posix_time::ptime retri_rT1, retri_rT2, retri_rT3;\n retri_rT1 = boost::posix_time::microsec_clock::local_time();\n\n assert(state_ptr->clones_imu.find(image_measurements.timestamp) != state_ptr->clones_imu.end());\n active_tracks_time = image_measurements.timestamp;\n\n // draw\n /*\n active_image = cv::Mat();\n featureTracker->displayActive(active_image, 255, 255, 255, 255, 255, 255, \" \");\n if (!active_image.empty())\n {\n active_image = active_image(cv::Rect(0, 0, image_measurements.images.at(0).cols, image_measurements.images.at(0).rows));\n }\n */\n // draw\n\n active_tracks_pos_world.clear();\n active_tracks_uvd.clear();\n\n auto last_obs = featureTracker->getLastObs();\n auto last_ids = featureTracker->getLastIds();\n\n std::map active_feat_linsys_A_new;\n std::map active_feat_linsys_b_new;\n std::map active_feat_linsys_count_new;\n std::unordered_map active_tracks_pos_world_new;\n\n std::map feat_uvs_in_cam_0;\n\n // display\n assert(state_ptr->clones_frame.find(active_tracks_time) != state_ptr->clones_frame.end());\n\n cv::Mat img_left = image_measurements.images.at(0);\n cv::Mat img_right = image_measurements.images.at(1);\n\n cv::Mat color_left = convertCvImage(img_left);\n cv::Mat color_right = convertCvImage(img_right);\n\n cv::Vec3b color_feat = cv::Vec3b(0, 255, 0);\n // display\n\n for (auto const &cam_id : image_measurements.camera_ids)\n {\n Eigen::Matrix3d R_GtoI = state_ptr->clones_imu.at(active_tracks_time)->getRot();\n Eigen::Vector3d p_IinG = state_ptr->clones_imu.at(active_tracks_time)->getPos();\n\n Eigen::Matrix3d R_ItoC = state_ptr->calib_cam_imu.at(cam_id)->getRot();\n Eigen::Vector3d p_IinC = state_ptr->calib_cam_imu.at(cam_id)->getPos();\n\n Eigen::Matrix3d R_GtoCi = R_ItoC * R_GtoI;\n Eigen::Vector3d p_CiinG = p_IinG - R_GtoCi.transpose() * p_IinC;\n\n assert(last_obs.find(cam_id) != last_obs.end());\n assert(last_ids.find(cam_id) != last_ids.end());\n\n for (size_t i = 0; i < last_obs.at(cam_id).size(); i++)\n {\n size_t feature_id = last_ids.at(cam_id).at(i);\n cv::Point2f pt_d = last_obs.at(cam_id).at(i).pt;\n\n // display\n if (pt_d.x > 5 && pt_d.x < wG[0] - 5 && pt_d.y > 5 && pt_d.y < hG[0] - 5)\n { \n if (cam_id == 0)\n drawPointL(color_left, pt_d.x, pt_d.y, color_feat);\n else\n drawPointL(color_right, pt_d.x, pt_d.y, color_feat);\n }\n // display\n\n if (cam_id == 0) feat_uvs_in_cam_0[feature_id] = pt_d;\n\n if (state_ptr->map_points.find(feature_id) != state_ptr->map_points.end()) continue;\n\n cv::Point2f pt_n = state_ptr->cam_intrinsics_cameras.at(cam_id)->undistortCV(pt_d);\n Eigen::Matrix b_i;\n b_i << pt_n.x, pt_n.y, 1;\n b_i = R_GtoCi.transpose() * b_i;\n b_i = b_i / b_i.norm();\n Eigen::Matrix3d B_perp = quatType::skewSymmetric(b_i);\n\n Eigen::Matrix3d Ai = B_perp.transpose() * B_perp;\n Eigen::Vector3d bi = Ai * p_CiinG;\n\n if (active_feat_linsys_A.find(feature_id) == active_feat_linsys_A.end())\n {\n active_feat_linsys_A_new.insert({feature_id, Ai});\n active_feat_linsys_b_new.insert({feature_id, bi});\n active_feat_linsys_count_new.insert({feature_id, 1});\n }\n else\n {\n active_feat_linsys_A_new[feature_id] = Ai + active_feat_linsys_A[feature_id];\n active_feat_linsys_b_new[feature_id] = bi + active_feat_linsys_b[feature_id];\n active_feat_linsys_count_new[feature_id] = 1 + active_feat_linsys_count[feature_id];\n }\n\n if (active_feat_linsys_count_new.at(feature_id) > 3)\n {\n Eigen::Matrix3d A = active_feat_linsys_A_new[feature_id];\n Eigen::Vector3d b = active_feat_linsys_b_new[feature_id];\n Eigen::MatrixXd p_FinG = A.colPivHouseholderQr().solve(b);\n Eigen::MatrixXd p_FinCi = R_GtoCi * (p_FinG - p_CiinG);\n\n Eigen::JacobiSVD svd(A);\n Eigen::MatrixXd singularValues;\n singularValues.resize(svd.singularValues().rows(), 1);\n singularValues = svd.singularValues();\n double cond_A = singularValues(0, 0) / singularValues(singularValues.rows() - 1, 0);\n\n if (std::abs(cond_A) <= odometry_options.featinit_options.max_cond_number && p_FinCi(2, 0) >= odometry_options.featinit_options.min_dist &&\n p_FinCi(2, 0) <= odometry_options.featinit_options.max_dist && !std::isnan(p_FinCi.norm()))\n {\n active_tracks_pos_world_new[feature_id] = p_FinG;\n }\n }\n }\n }\n\n // display\n cv::Mat stereo_image;\n cv::vconcat(color_left, color_right, stereo_image);\n\n /*\n cv::imshow(\"Stereo Image\", stereo_image);\n cv::waitKey(1);\n */\n pubFeatImage(stereo_image, active_tracks_time);\n\n img_left.release();\n img_right.release();\n\n color_left.release();\n color_right.release();\n\n stereo_image.release();\n // display\n\n size_t total_triangulated = active_tracks_pos_world.size();\n\n active_feat_linsys_A = active_feat_linsys_A_new;\n active_feat_linsys_b = active_feat_linsys_b_new;\n active_feat_linsys_count = active_feat_linsys_count_new;\n active_tracks_pos_world = active_tracks_pos_world_new;\n retri_rT2 = boost::posix_time::microsec_clock::local_time();\n\n if (active_tracks_pos_world.empty() && state_ptr->map_points.empty()) return;\n\n for (const auto &feat : state_ptr->map_points)\n {\n Eigen::Vector3d p_FinG = feat.second->getPointXYZ(false);\n active_tracks_pos_world[feat.second->feature_id] = p_FinG;\n }\n\n std::shared_ptr distortion = state_ptr->cam_intrinsics.at(0);\n std::shared_ptr calibration = state_ptr->calib_cam_imu.at(0);\n Eigen::Matrix R_ItoC = calibration->getRot();\n Eigen::Matrix p_IinC = calibration->getPos();\n\n std::shared_ptr clone_Ii = state_ptr->clones_imu.at(active_tracks_time);\n Eigen::Matrix3d R_GtoIi = clone_Ii->getRot();\n Eigen::Vector3d p_IiinG = clone_Ii->getPos();\n\n for (const auto &feat : active_tracks_pos_world)\n {\n if (feat_uvs_in_cam_0.find(feat.first) == feat_uvs_in_cam_0.end()) continue;\n\n Eigen::Vector3d p_FinIi = R_GtoIi * (feat.second - p_IiinG);\n Eigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\n double depth = p_FinCi(2);\n Eigen::Vector2d uv_dist;\n \n if (feat_uvs_in_cam_0.find(feat.first) != feat_uvs_in_cam_0.end())\n {\n uv_dist << (double)feat_uvs_in_cam_0.at(feat.first).x, (double)feat_uvs_in_cam_0.at(feat.first).y;\n }\n else\n {\n Eigen::Vector2d uv_norm;\n uv_norm << p_FinCi(0) / depth, p_FinCi(1) / depth;\n uv_dist = state_ptr->cam_intrinsics_cameras.at(0)->distortD(uv_norm);\n }\n\n if (depth < 0.1) continue;\n\n int width = state_ptr->cam_intrinsics_cameras.at(0)->w();\n int height = state_ptr->cam_intrinsics_cameras.at(0)->h();\n\n if (uv_dist(0) < 0 || (int)uv_dist(0) >= width || uv_dist(1) < 0 || (int)uv_dist(1) >= height)\n {\n continue;\n }\n\n Eigen::Vector3d uvd;\n uvd << uv_dist, depth;\n active_tracks_uvd.insert({feat.first, uvd});\n }\n retri_rT3 = boost::posix_time::microsec_clock::local_time();\n\n // Time test\n /*\n std::cout << std::fixed << \"[triangulateActiveTracks]: \" << (retri_rT2 - retri_rT1).total_microseconds() * 1e-6 << \" seconds for triangulation (\" \n << total_triangulated << \" tri of \" << active_feat_linsys_A.size() << \" active)\" << std::endl;\n\n std::cout << std::fixed << \"[triangulateActiveTracks]: \" << (retri_rT3 - retri_rT2).total_microseconds() * 1e-6 << \" seconds for re-projection into current\" << std::endl;\n\n std::cout << std::fixed << \"[triangulateActiveTracks]: \" << (retri_rT3 - retri_rT1).total_microseconds() * 1e-6 << \" seconds total\" << std::endl;\n */\n // Time test\n}\n void getRecentVoxel(double timestamp, pcl::PointCloud::Ptr voxels_visit) {\n std::vector>().swap(recent_voxels);\n\n // display\n voxels_visit->clear();\n // display\n\n for (const auto &point : active_tracks_pos_world)\n {\n short kx = static_cast(point.second[0] / odometry_options.voxel_size);\n short ky = static_cast(point.second[1] / odometry_options.voxel_size);\n short kz = static_cast(point.second[2] / odometry_options.voxel_size);\n\n for (short kxx = kx - odometry_options.nb_voxels_visited; kxx < kx + odometry_options.nb_voxels_visited + 1; kxx++)\n {\n for (short kyy = ky - odometry_options.nb_voxels_visited; kyy < ky + odometry_options.nb_voxels_visited + 1; kyy++)\n {\n for (short kzz = kz - odometry_options.nb_voxels_visited; kzz < kz + odometry_options.nb_voxels_visited + 1; kzz++)\n {\n voxelHashMap::iterator search = voxel_map.find(voxel(kxx, kyy, kzz));\n\n if(search != voxel_map.end())\n {\n auto &voxel_block = (search.value());\n\n if (voxel_block.last_visit_time < timestamp && voxel_block.NumPoints() > 0)\n {\n Eigen::Matrix voxel_temp;\n\n voxel_temp[0] = kxx;\n voxel_temp[1] = kyy;\n voxel_temp[2] = kzz;\n\n recent_voxels.push_back(voxel_temp);\n voxel_block.last_visit_time = timestamp;\n\n // display\n pcl::PointXYZI point_temp;\n \n point_temp.x = kxx >= 0 ? (kxx + 0.5) * odometry_options.voxel_size : (kxx - 0.5) * odometry_options.voxel_size;\n point_temp.y = kyy >= 0 ? (kyy + 0.5) * odometry_options.voxel_size : (kyy - 0.5) * odometry_options.voxel_size;\n point_temp.z = kzz >= 0 ? (kzz + 0.5) * odometry_options.voxel_size : (kzz - 0.5) * odometry_options.voxel_size;\n point_temp.intensity = 1;\n\n voxels_visit->points.push_back(point_temp);\n // display\n }\n }\n }\n }\n }\n }\n}\n bool tryToInitialize(cameraData &image_measurements) {\n camera_queue_init.push_back(image_measurements.timestamp);\n\n assert(newest_fh->timestamp == image_measurements.timestamp);\n frame_queue_init.push_back(newest_fh);\n\n double timestamp;\n Eigen::MatrixXd covariance;\n std::vector> order;\n auto init_rT1 = boost::posix_time::microsec_clock::local_time();\n\n bool success = initializer_ptr->initialize(timestamp, covariance, order, state_ptr->imu_ptr, true);\n\n if (success)\n {\n stateHelper::setInitialCovariance(state_ptr, covariance, order);\n\n state_ptr->timestamp = timestamp;\n startup_time = timestamp;\n\n featureTracker->getFeatureDatabase()->cleanUpOldmeasurements(state_ptr->timestamp);\n featureTracker->setNumFeatures(std::floor((double)odometry_options.num_pts / (double)2.0));\n\n auto init_rT2 = boost::posix_time::microsec_clock::local_time();\n std::cout << std::fixed << \"[initialize]: Successful initialization in \" << (init_rT2 - init_rT1).total_microseconds() * 1e-6 << \" seconds\" << std::endl;\n std::cout << std::fixed << \"[initialize]: position = \" << state_ptr->imu_ptr->getPos()(0) << \", \" << state_ptr->imu_ptr->getPos()(1) << \", \"\n << state_ptr->imu_ptr->getPos()(2) << std::endl;\n std::cout << std::fixed << \"[initialize]: orientation = \" << state_ptr->imu_ptr->getQuat()(0) << \", \" << state_ptr->imu_ptr->getQuat()(1) << \", \" << state_ptr->imu_ptr->getQuat()(2) \n << \", \" << state_ptr->imu_ptr->getQuat()(3) << std::endl;\n std::cout << std::fixed << \"[initialize]: velocity = \" << state_ptr->imu_ptr->getVel()(0) << \", \" << state_ptr->imu_ptr->getVel()(1) << \", \"\n << state_ptr->imu_ptr->getVel()(2) << std::endl;\n std::cout << std::fixed << \"[initialize]: bias accel = \" << state_ptr->imu_ptr->getBa()(0) << \", \" << state_ptr->imu_ptr->getBa()(1) << \", \"\n << state_ptr->imu_ptr->getBa()(2) << std::endl;\n std::cout << std::fixed << \"[initialize]: bias gyro = \" << state_ptr->imu_ptr->getBg()(0) << \", \" << state_ptr->imu_ptr->getBg()(1) << \", \" \n << state_ptr->imu_ptr->getBg()(2) << std::endl;\n\n std::vector camera_timestamps_to_init;\n for (size_t i = 0; i < camera_queue_init.size(); i++)\n {\n if (camera_queue_init.at(i) > timestamp)\n camera_timestamps_to_init.push_back(camera_queue_init.at(i));\n }\n\n std::vector> camera_frames_to_init;\n for (size_t i = 0; i < frame_queue_init.size(); i++)\n {\n if (frame_queue_init.at(i)->timestamp > timestamp)\n camera_frames_to_init.push_back(frame_queue_init.at(i));\n }\n\n size_t clone_rate = (size_t)((double)camera_timestamps_to_init.size() / (double)odometry_options.state_options.max_clone_size) + 1;\n \n for (size_t i = 0; i < camera_timestamps_to_init.size(); i += clone_rate)\n {\n propagator_ptr->propagateAndClone(state_ptr, camera_timestamps_to_init.at(i));\n\n if (state_ptr->clones_frame.find(camera_frames_to_init.at(i)->timestamp) == state_ptr->clones_frame.end())\n state_ptr->clones_frame[camera_frames_to_init.at(i)->timestamp] = camera_frames_to_init.at(i);\n\n stateHelper::marginalizeOldClone(state_ptr);\n }\n\n std::cout << \"[initialize]: Moved the state forward \" << state_ptr->timestamp - timestamp << \" seconds\" << std::endl;\n camera_queue_init.clear();\n\n frame_queue_init.clear();\n\n return true;\n }\n else\n {\n auto init_rT2 = boost::posix_time::microsec_clock::local_time();\n std::cout << std::fixed << \"[initialize]: Failed initialization in \" << (init_rT2 - init_rT1).total_microseconds() * 1e-6 << \" seconds\" << std::endl;\n\n double oldest_time = image_measurements.timestamp - odometry_options.init_options.init_window_time - 0.10;\n\n auto it0 = camera_queue_init.begin();\n \n while (it0 != camera_queue_init.end())\n {\n if (*it0 < oldest_time)\n it0 = camera_queue_init.erase(it0);\n else\n it0++;\n }\n\n auto it1 = frame_queue_init.begin();\n \n while (it1 != frame_queue_init.end())\n {\n if ((*it1)->timestamp < oldest_time)\n {\n assert((*it1)->v_feat_ptr[0].size() == 0);\n assert((*it1)->v_feat_ptr[1].size() == 0);\n (*it1)->release();\n it1 = frame_queue_init.erase(it1);\n }\n else\n it1++;\n }\n\n return false;\n }\n}\n std::queue camera_buffer;\n std::queue imu_buffer;\n std::map camera_last_timestamp;\n odometryOptions odometry_options;\n std::shared_ptr featureTracker;\n std::shared_ptr initializer_ptr;\n double startup_time;\n double current_time;\n double time_newest_imu;\n std::vector>> imu_meas;\n std::vector imu_states;\n std::shared_ptr state_ptr;\n imuData last_imu_data;\n std::shared_ptr propagator_ptr;\n std::shared_ptr updaterMsckf_ptr;\n std::shared_ptr updaterSlam_ptr;\n std::shared_ptr gammaPixel_ptr;\n std::shared_ptr newest_fh;\n std::vector camera_queue_init;\n std::vector> frame_queue_init;\n voxelHashMap voxel_map;\n int frame_count;\n MarginalizeStatus marginalize_status;\n double last_time_image;\n std::vector good_features_msckf;\n double active_tracks_time = -1;\n std::unordered_map active_tracks_pos_world;\n std::unordered_map active_tracks_uvd;\n cv::Mat active_image;\n std::map active_feat_linsys_A;\n std::map active_feat_linsys_b;\n std::map active_feat_linsys_count;\n std::vector> recent_voxels;\n std::ofstream of_statistics;\n boost::posix_time::ptime rT1, rT2, rT3, rT4, rT5, rT6, rT7;\n double timelastupdate = -1;\n double distance = 0;\n double sum_time_1 = 0.0;\n double sum_time_2 = 0.0;\n double sum_time_3 = 0.0;\n double sum_time_4 = 0.0;\n double sum_time_5 = 0.0;\n double sum_time_6 = 0.0;\n double sum_time_7 = 0.0;\n double sum_time_sum = 0.0;\n int n_time_1 = 0;\n int n_time_2 = 0;\n int n_time_3 = 0;\n int n_time_4 = 0;\n int n_time_5 = 0;\n int n_time_6 = 0;\n int n_time_7 = 0;\n int n_time_sum = 0;\n public:\n voxelStereoVio() {\n readParameters();\n\n initialValue();\n\n allocateMemory();\n\n sub_imu = nh.subscribe(imu_topic, 500, &voxelStereoVio::imuHandler, this);\n\n auto sub_img_left = std::make_shared>(nh, image_left_topic, 5);\n auto sub_img_right = std::make_shared>(nh, image_right_topic, 5);\n auto sync = std::make_shared>(SyncStereoImage(10), *sub_img_left, *sub_img_right);\n sync->registerCallback(boost::bind(&voxelStereoVio::stereoImageHandler, this, _1, _2, 0, 1));\n\n sync_cam.push_back(sync);\n sync_subs_cam.push_back(sub_img_left);\n sync_subs_cam.push_back(sub_img_right);\n\n // display\n pub_feat_image = it.advertise(\"camera/stereo_feat_image\", 5);\n pub_odom = nh.advertise(\"vio/odom\", 5);\n pub_path = nh.advertise(\"vio/path\", 5);\n pub_points_history = nh.advertise(\"vio/history_map_points\", 2);\n pub_points_window = nh.advertise(\"vio/window_map_points\", 2);\n pub_voxels_history = nh.advertise(\"vio/history_voxels\", 2);\n pub_voxels_visit = nh.advertise(\"vio/visit_voxels\", 2);\n\n points_history.reset(new pcl::PointCloud());\n points_window.reset(new pcl::PointCloud());\n voxels_history.reset(new pcl::PointCloud());\n voxels_visit.reset(new pcl::PointCloud());\n // display\n\n // odometry_options.recordParameters();\n}\n void readParameters() {\t\n int para_int;\n double para_double;\n bool para_bool;\n std::string str_temp;\n\n nh.param(\"common/image_left_topic\", image_left_topic, \"/cam0/image_raw\");\n nh.param(\"common/image_right_topic\", image_right_topic, \"/cam1/image_raw\");\n nh.param(\"common/imu_topic\", imu_topic, \"/imu0\");\n nh.param(\"output_path\", output_path, \"\");\n\n nh.param(\"state_parameter/use_fej\", para_bool, false); odometry_options.state_options.do_fej = para_bool;\n nh.param(\"state_parameter/calib_cam_extrinsics\", para_bool, false); odometry_options.state_options.do_calib_camera_pose = para_bool;\n nh.param(\"state_parameter/calib_cam_intrinsics\", para_bool, false); odometry_options.state_options.do_calib_camera_intrinsics = para_bool;\n nh.param(\"state_parameter/calib_cam_timeoffset\", para_bool, false); odometry_options.state_options.do_calib_camera_timeoffset = para_bool;\n nh.param(\"state_parameter/calib_imu_intrinsics\", para_bool, false); odometry_options.state_options.do_calib_imu_intrinsics = para_bool;\n nh.param(\"state_parameter/calib_imu_g_sensitivity\", para_bool, false); odometry_options.state_options.do_calib_imu_g_sensitivity = para_bool;\n\n nh.param(\"state_parameter/imu_intrinsics_model\", str_temp, \"kalibr\");\n if (str_temp == \"kalibr\" || str_temp == \"calibrated\") odometry_options.state_options.imu_model = ImuModel::KALIBR;\n else if (str_temp == \"rpng\") odometry_options.state_options.imu_model = ImuModel::RPNG;\n else {\n std::cout << \"Invalid IMU model: \" << str_temp << std::endl;\n std::cout << \"Please select a valid model: kalibr, rpng.\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n if (str_temp == \"calibrated\")\n {\n odometry_options.state_options.do_calib_imu_intrinsics = false;\n odometry_options.state_options.do_calib_imu_g_sensitivity = false;\n }\n\n nh.param(\"state_parameter/max_clones\", para_int, 11); odometry_options.state_options.max_clone_size = para_int;\n nh.param(\"state_parameter/max_slam\", para_int, 25); odometry_options.state_options.max_slam_features = para_int;\n nh.param(\"state_parameter/max_slam_in_update\", para_int, 1000); odometry_options.state_options.max_slam_in_update = para_int;\n nh.param(\"state_parameter/max_msckf_in_update\", para_int, 1000); odometry_options.state_options.max_msckf_in_update = para_int;\n\n nh.param(\"initializer_parameter/init_window_time\", para_double, 1.0); odometry_options.init_options.init_window_time = para_double;\n nh.param(\"initializer_parameter/init_imu_thresh\", para_double, 1.0); odometry_options.init_options.init_imu_thresh = para_double;\n nh.param(\"initializer_parameter/init_max_disparity\", para_double, 1.0); odometry_options.init_options.init_max_disparity = para_double;\n nh.param(\"initializer_parameter/init_max_features\", para_int, 50); odometry_options.init_options.init_max_features = para_int;\n nh.param(\"initializer_parameter/init_dyn_use\", para_bool, false); odometry_options.init_options.init_dyn_use = para_bool;\n nh.param(\"initializer_parameter/init_dyn_mle_opt_calib\", para_bool, false); odometry_options.init_options.init_dyn_mle_opt_calib = para_bool;\n nh.param(\"initializer_parameter/init_dyn_mle_max_iter\", para_int, 20); odometry_options.init_options.init_dyn_mle_max_iter = para_int;\n nh.param(\"initializer_parameter/init_dyn_mle_max_threads\", para_int, 20); odometry_options.init_options.init_dyn_mle_max_threads = para_int;\n nh.param(\"initializer_parameter/init_dyn_mle_max_time\", para_double, 5.0); odometry_options.init_options.init_dyn_mle_max_time = para_double;\n nh.param(\"initializer_parameter/init_dyn_num_pose\", para_int, 5); odometry_options.init_options.init_dyn_num_pose = para_int;\n nh.param(\"initializer_parameter/init_dyn_min_deg\", para_double, 45.0); odometry_options.init_options.init_dyn_min_deg = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_ori\", para_double, 10.0); odometry_options.init_options.init_dyn_inflation_orientation = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_vel\", para_double, 10.0); odometry_options.init_options.init_dyn_inflation_velocity = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_bg\", para_double, 100.0); odometry_options.init_options.init_dyn_inflation_bias_gyro = para_double;\n nh.param(\"initializer_parameter/init_dyn_inflation_ba\", para_double, 100.0); odometry_options.init_options.init_dyn_inflation_bias_accel = para_double;\n nh.param(\"initializer_parameter/init_dyn_min_rec_cond\", para_double, 1e-15); odometry_options.init_options.init_dyn_min_rec_cond = para_double;\n\n std::vector v_bias_acc, v_bias_gyr;\n nh.param>(\"initializer_parameter/init_dyn_bias_g\", v_bias_gyr, std::vector());\n nh.param>(\"initializer_parameter/init_dyn_bias_a\", v_bias_acc, std::vector());\n odometry_options.init_options.init_dyn_bias_g << v_bias_gyr.at(0), v_bias_gyr.at(1), v_bias_gyr.at(2);\n odometry_options.init_options.init_dyn_bias_a << v_bias_acc.at(0), v_bias_acc.at(1), v_bias_acc.at(2);\n\n nh.param(\"initializer_parameter/gravity_mag\", para_double, 9.81); odometry_options.init_options.gravity_mag = para_double;\n nh.param(\"initializer_parameter/downsample_cameras\", para_bool, false); odometry_options.init_options.downsample_cameras = para_bool;\n \n double calib_camimu_dt_left, calib_camimu_dt_right;\n nh.param(\"camera_parameter/timeshift_cam_imu_left\", calib_camimu_dt_left, 0.0);\n nh.param(\"camera_parameter/timeshift_cam_imu_right\", calib_camimu_dt_right, 0.0);\n odometry_options.calib_camimu_dt = calib_camimu_dt_left;\n\n std::string dist_model_left, dist_model_right;\n nh.param(\"camera_parameter/distortion_model_left\", dist_model_left, \"radtan\");\n nh.param(\"camera_parameter/distortion_model_right\", dist_model_right, \"radtan\");\n\n std::vector cam_calib_1_left = {1, 1, 0, 0};\n std::vector cam_calib_1_right = {1, 1, 0, 0};\n std::vector cam_calib_2_left = {0, 0, 0, 0};\n std::vector cam_calib_2_right = {0, 0, 0, 0};\n nh.param>(\"camera_parameter/intrinsics_left\", cam_calib_1_left, std::vector());\n nh.param>(\"camera_parameter/intrinsics_right\", cam_calib_1_right, std::vector());\n nh.param>(\"camera_parameter/distortion_coeffs_left\", cam_calib_2_left, std::vector());\n nh.param>(\"camera_parameter/distortion_coeffs_right\", cam_calib_2_right, std::vector());\n Eigen::VectorXd cam_calib_left = Eigen::VectorXd::Zero(8);\n Eigen::VectorXd cam_calib_right = Eigen::VectorXd::Zero(8);\n cam_calib_left << cam_calib_1_left.at(0), cam_calib_1_left.at(1), cam_calib_1_left.at(2), cam_calib_1_left.at(3), \n cam_calib_2_left.at(0), cam_calib_2_left.at(1), cam_calib_2_left.at(2), cam_calib_2_left.at(3);\n cam_calib_right << cam_calib_1_right.at(0), cam_calib_1_right.at(1), cam_calib_1_right.at(2), cam_calib_1_right.at(3), \n cam_calib_2_right.at(0), cam_calib_2_right.at(1), cam_calib_2_right.at(2), cam_calib_2_right.at(3);\n\n cam_calib_left(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_left(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_left(2) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_left(3) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n cam_calib_right(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_right(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_right(2) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n cam_calib_right(3) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n std::vector matrix_wh_left = {1, 1};\n nh.param>(\"camera_parameter/resolution_left\", matrix_wh_left, std::vector());\n matrix_wh_left.at(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n matrix_wh_left.at(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n std::vector matrix_wh_right = {1, 1};\n nh.param>(\"camera_parameter/resolution_right\", matrix_wh_right, std::vector());\n matrix_wh_right.at(0) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n matrix_wh_right.at(1) /= (odometry_options.init_options.downsample_cameras) ? 2.0 : 1.0;\n\n assert(matrix_wh_left.at(0) == matrix_wh_right.at(0));\n assert(matrix_wh_left.at(1) == matrix_wh_right.at(1));\n\n wG[0] = matrix_wh_left.at(0);\n hG[0] = matrix_wh_left.at(1);\n\n std::vector v_T_imu_cam_left;\n nh.param>(\"camera_parameter/T_imu_cam_left\", v_T_imu_cam_left, std::vector());\n Eigen::Matrix4d T_imu_cam_left = mat44FromArray(v_T_imu_cam_left);\n\n std::vector v_T_imu_cam_right;\n nh.param>(\"camera_parameter/T_imu_cam_right\", v_T_imu_cam_right, std::vector());\n Eigen::Matrix4d T_imu_cam_right = mat44FromArray(v_T_imu_cam_right);\n\n Eigen::Matrix cam_eigen_left;\n cam_eigen_left.block<4, 1>(0, 0) = quatType::rotToQuat(T_imu_cam_left.block<3, 3>(0, 0).transpose());\n cam_eigen_left.block<3, 1>(4, 0) = - T_imu_cam_left.block<3, 3>(0, 0).transpose() * T_imu_cam_left.block<3, 1>(0, 3);\n\n Eigen::Matrix cam_eigen_right;\n cam_eigen_right.block<4, 1>(0, 0) = quatType::rotToQuat(T_imu_cam_right.block<3, 3>(0, 0).transpose());\n cam_eigen_right.block<3, 1>(4, 0) = - T_imu_cam_right.block<3, 3>(0, 0).transpose() * T_imu_cam_right.block<3, 1>(0, 3);\n\n if (dist_model_left == \"equidistant\")\n {\n odometry_options.init_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.init_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n\n odometry_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n }\n else\n {\n odometry_options.init_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.init_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n\n odometry_options.camera_intrinsics.insert({0, std::make_shared(matrix_wh_left.at(0), matrix_wh_left.at(1))});\n odometry_options.camera_intrinsics.at(0)->setValue(cam_calib_left);\n }\n odometry_options.init_options.camera_extrinsics.insert({0, cam_eigen_left});\n odometry_options.camera_extrinsics.insert({0, cam_eigen_left});\n\n if (dist_model_right == \"equidistant\")\n {\n odometry_options.init_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.init_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n\n odometry_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n }\n else\n {\n odometry_options.init_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.init_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n\n odometry_options.camera_intrinsics.insert({1, std::make_shared(matrix_wh_right.at(0), matrix_wh_right.at(1))});\n odometry_options.camera_intrinsics.at(1)->setValue(cam_calib_right);\n }\n odometry_options.init_options.camera_extrinsics.insert({1, cam_eigen_right});\n odometry_options.camera_extrinsics.insert({1, cam_eigen_right});\n\n nh.param(\"odometry_parameter/use_mask\", para_bool, false); odometry_options.use_mask = para_bool;\n if (odometry_options.use_mask)\n {\n std::string mask_left_path, mask_right_path;\n nh.param(\"camera_parameter/mask_left_path\", str_temp, \"\"); mask_left_path = str_temp;\n nh.param(\"camera_parameter/mask_right_path\", str_temp, \"\"); mask_right_path = str_temp;\n\n if (!boost::filesystem::exists(mask_left_path))\n {\n std::cout << \"Invalid mask path: \" << mask_left_path << std::endl;\n std::exit(EXIT_FAILURE);\n }\n cv::Mat mask_left = cv::imread(mask_left_path, cv::IMREAD_GRAYSCALE);\n\n if (!boost::filesystem::exists(mask_right_path))\n {\n std::cout << \"Invalid mask path: \" << mask_right_path << std::endl;\n std::exit(EXIT_FAILURE);\n }\n cv::Mat mask_right = cv::imread(mask_right_path, cv::IMREAD_GRAYSCALE);\n\n if (mask_left.cols != odometry_options.camera_intrinsics.at(0)->w() || mask_left.rows != odometry_options.camera_intrinsics.at(0)->h())\n {\n std::cout << \"Mask size does not match left camera!\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n if (mask_right.cols != odometry_options.camera_intrinsics.at(1)->w() || mask_right.rows != odometry_options.camera_intrinsics.at(1)->h())\n {\n std::cout << \"Mask size does not match right camera!\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n odometry_options.masks.insert({0, mask_left});\n odometry_options.masks.insert({1, mask_right});\n }\n\n nh.param(\"imu_parameter/gyroscope_noise_density\", para_double, 1.6968e-04); odometry_options.init_options.sigma_w = para_double;\n nh.param(\"imu_parameter/gyroscope_random_walk\", para_double, 1.9393e-05); odometry_options.init_options.sigma_wb = para_double;\n nh.param(\"imu_parameter/accelerometer_noise_density\", para_double, 2.0000e-3); odometry_options.init_options.sigma_a = para_double;\n nh.param(\"imu_parameter/accelerometer_random_walk\", para_double, 3.0000e-03); odometry_options.init_options.sigma_ab = para_double;\n nh.param(\"imu_parameter/sigma_pix\", para_double, 1.0); odometry_options.init_options.sigma_pix = para_double;\n\n std::vector v_Tw;\n nh.param>(\"imu_parameter/Tw\", v_Tw, std::vector());\n Eigen::Matrix3d Tw = mat33FromArray(v_Tw);\n std::vector v_Ta;\n nh.param>(\"imu_parameter/Ta\", v_Ta, std::vector());\n Eigen::Matrix3d Ta = mat33FromArray(v_Ta);\n std::vector v_R_acc_imu;\n nh.param>(\"imu_parameter/R_acc_imu\", v_R_acc_imu, std::vector());\n Eigen::Matrix3d R_acc_imu = mat33FromArray(v_R_acc_imu);\n std::vector v_R_gyr_imu;\n nh.param>(\"imu_parameter/R_gyr_imu\", v_R_gyr_imu, std::vector());\n Eigen::Matrix3d R_gyr_imu = mat33FromArray(v_R_gyr_imu);\n std::vector v_Tg;\n nh.param>(\"imu_parameter/Tg\", v_Tg, std::vector());\n Eigen::Matrix3d Tg = mat33FromArray(v_Tg);\n\n Eigen::Matrix3d Dw = Tw.colPivHouseholderQr().solve(Eigen::Matrix3d::Identity());\n Eigen::Matrix3d Da = Ta.colPivHouseholderQr().solve(Eigen::Matrix3d::Identity());\n Eigen::Matrix3d R_imu_acc = R_acc_imu.transpose();\n Eigen::Matrix3d R_imu_gyr = R_gyr_imu.transpose();\n\n if (std::isnan(Tw.norm()) || std::isnan(Dw.norm()))\n {\n std::cout << \"gyroscope has bad intrinsic values!\" << std::endl;\n std::cout << \"Tw = \" << Tw << std::endl;\n std::cout << \"Dw = \" << Dw << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n if (std::isnan(Ta.norm()) || std::isnan(Da.norm()))\n {\n std::cout << \"accelerometer has bad intrinsic values!\" << std::endl;\n std::cout << \"Ta = \" << Ta << std::endl;\n std::cout << \"Da = \" << Da << std::endl;\n std::exit(EXIT_FAILURE);\n }\n\n if (odometry_options.state_options.imu_model == ImuModel::KALIBR)\n {\n odometry_options.vec_dw << Dw.block<3, 1>(0, 0), Dw.block<2, 1>(1, 1), Dw(2, 2);\n odometry_options.vec_da << Da.block<3, 1>(0, 0), Da.block<2, 1>(1, 1), Da(2, 2);\n }\n else\n {\n odometry_options.vec_dw << Dw(0, 0), Dw.block<2, 1>(0, 1), Dw.block<3, 1>(0, 2);\n odometry_options.vec_da << Da(0, 0), Da.block<2, 1>(0, 1), Da.block<3, 1>(0, 2);\n }\n\n odometry_options.vec_tg << Tg.block<3, 1>(0, 0), Tg.block<3, 1>(0, 1), Tg.block<3, 1>(0, 2);\n odometry_options.q_imu_acc = quatType::rotToQuat(R_imu_acc);\n odometry_options.q_imu_gyr = quatType::rotToQuat(R_imu_gyr);\n\n nh.param(\"odometry_parameter/dt_slam_delay\", para_double, 2.0); odometry_options.dt_slam_delay = para_double;\n\n odometry_options.imu_noises.sigma_w = odometry_options.init_options.sigma_w;\n odometry_options.imu_noises.sigma_wb = odometry_options.init_options.sigma_wb;\n odometry_options.imu_noises.sigma_a = odometry_options.init_options.sigma_a;\n odometry_options.imu_noises.sigma_ab = odometry_options.init_options.sigma_ab;\n odometry_options.imu_noises.sigma_w_2 = std::pow(odometry_options.imu_noises.sigma_w, 2);\n odometry_options.imu_noises.sigma_wb_2 = std::pow(odometry_options.imu_noises.sigma_wb, 2);\n odometry_options.imu_noises.sigma_a_2 = std::pow(odometry_options.imu_noises.sigma_a, 2);\n odometry_options.imu_noises.sigma_ab_2 = std::pow(odometry_options.imu_noises.sigma_ab, 2);\n\n nh.param(\"odometry_parameter/up_msckf_sigma_px\", para_double, 1.0); odometry_options.msckf_options.sigma_pix = para_double;\n nh.param(\"odometry_parameter/up_msckf_chi2_multipler\", para_double, 5.0); odometry_options.msckf_options.chi2_multipler = para_double;\n nh.param(\"odometry_parameter/up_slam_sigma_px\", para_double, 1.0); odometry_options.slam_options.sigma_pix = para_double;\n nh.param(\"odometry_parameter/up_slam_chi2_multipler\", para_double, 5.0); odometry_options.slam_options.chi2_multipler = para_double;\n odometry_options.msckf_options.sigma_pix_sq = std::pow(odometry_options.msckf_options.sigma_pix, 2);\n odometry_options.slam_options.sigma_pix_sq = std::pow(odometry_options.slam_options.sigma_pix, 2);\n\n nh.param(\"odometry_parameter/downsample_cameras\", para_bool, false); odometry_options.downsample_cameras = para_bool;\n nh.param(\"odometry_parameter/num_pts\", para_int, 150); odometry_options.num_pts = para_int;\n nh.param(\"odometry_parameter/fast_threshold\", para_int, 20); odometry_options.fast_threshold = para_int;\n nh.param(\"odometry_parameter/patch_size_x\", para_int, 5); odometry_options.patch_size_x = para_int;\n nh.param(\"odometry_parameter/patch_size_y\", para_int, 5); odometry_options.patch_size_y = para_int;\n nh.param(\"odometry_parameter/min_px_dist\", para_int, 10); odometry_options.min_px_dist = para_int;\n nh.param(\"odometry_parameter/histogram_method\", str_temp, \"histogram\");\n if (str_temp == \"none\") odometry_options.histogram_method = HistogramMethod::NONE;\n else if (str_temp == \"histogram\") odometry_options.histogram_method = HistogramMethod::HISTOGRAM;\n else if (str_temp == \"clahe\") odometry_options.histogram_method = HistogramMethod::CLAHE;\n else {\n std::cout << \"Invalid feature histogram specified: \" << str_temp << std::endl;\n std::cout << \"Please select a valid histogram method: none, histogram, clahe.\" << std::endl;\n std::exit(EXIT_FAILURE);\n }\n nh.param(\"odometry_parameter/track_frequency\", para_double, 20.0); odometry_options.track_frequency = para_double;\n\n nh.param(\"odometry_parameter/use_huber\", para_bool, true); odometry_options.state_options.use_huber = para_bool;\n nh.param(\"odometry_parameter/use_keyframe\", para_bool, true); odometry_options.use_keyframe = para_bool;\n\n nh.param(\"feature_parameter/refine_features\", para_bool, true); odometry_options.featinit_options.refine_features = para_bool;\n nh.param(\"feature_parameter/max_runs\", para_int, 5); odometry_options.featinit_options.max_runs = para_int;\n nh.param(\"feature_parameter/init_lamda\", para_double, 1e-3); odometry_options.featinit_options.init_lamda = para_double;\n nh.param(\"feature_parameter/max_lamda\", para_double, 1e10); odometry_options.featinit_options.max_lamda = para_double;\n nh.param(\"feature_parameter/min_dx\", para_double, 1e-6); odometry_options.featinit_options.min_dx = para_double;\n nh.param(\"feature_parameter/min_dcost\", para_double, 1e-6); odometry_options.featinit_options.min_dcost = para_double;\n nh.param(\"feature_parameter/lam_mult\", para_double, 10.0); odometry_options.featinit_options.lam_mult = para_double;\n nh.param(\"feature_parameter/min_dist\", para_double, 0.10); odometry_options.featinit_options.min_dist = para_double;\n nh.param(\"feature_parameter/max_dist\", para_double, 60.0); odometry_options.featinit_options.max_dist = para_double;\n nh.param(\"feature_parameter/max_baseline\", para_double, 40.0); odometry_options.featinit_options.max_baseline = para_double;\n nh.param(\"feature_parameter/max_cond_number\", para_double, 10000.0); odometry_options.featinit_options.max_cond_number = para_double;\n\n nh.param(\"feature_parameter/keyframe_parallax\", para_int, 10); setting_min_parallax = para_int;\n setting_min_parallax = setting_min_parallax / cam_calib_1_left.at(0);\n\n nh.param(\"voxel_parameter/voxel_size\", para_double, 0.1); odometry_options.voxel_size = odometry_options.state_options.voxel_size = para_double;\n nh.param(\"voxel_parameter/max_num_points_in_voxel\", para_int, 5); odometry_options.max_num_points_in_voxel = odometry_options.state_options.max_num_points_in_voxel = para_int;\n nh.param(\"voxel_parameter/min_distance_points\", para_double, 0.03); odometry_options.min_distance_points = odometry_options.state_options.min_distance_points = para_double;\n nh.param(\"voxel_parameter/nb_voxels_visited\", para_int, 1); odometry_options.nb_voxels_visited = para_int;\n nh.param(\"voxel_parameter/use_all_points\", para_bool, false); odometry_options.use_all_points = para_bool;\n}\n void allocateMemory() {\n state_ptr = std::make_shared(odometry_options.state_options);\n\n state_ptr->calib_imu_dw->setValue(odometry_options.vec_dw);\n state_ptr->calib_imu_dw->setFej(odometry_options.vec_dw);\n state_ptr->calib_imu_da->setValue(odometry_options.vec_da);\n state_ptr->calib_imu_da->setFej(odometry_options.vec_da);\n state_ptr->calib_imu_tg->setValue(odometry_options.vec_tg);\n state_ptr->calib_imu_tg->setFej(odometry_options.vec_tg);\n state_ptr->calib_imu_gyr->setValue(odometry_options.q_imu_gyr);\n state_ptr->calib_imu_gyr->setFej(odometry_options.q_imu_gyr);\n state_ptr->calib_imu_acc->setValue(odometry_options.q_imu_acc);\n state_ptr->calib_imu_acc->setFej(odometry_options.q_imu_acc);\n\n Eigen::VectorXd temp_camimu_dt;\n temp_camimu_dt.resize(1);\n temp_camimu_dt(0) = odometry_options.calib_camimu_dt;\n state_ptr->calib_dt_imu_cam->setValue(temp_camimu_dt);\n state_ptr->calib_dt_imu_cam->setFej(temp_camimu_dt);\n\n state_ptr->cam_intrinsics_cameras = odometry_options.camera_intrinsics;\n\n for (int i = 0; i < 2; i++)\n {\n state_ptr->cam_intrinsics.at(i)->setValue(odometry_options.camera_intrinsics.at(i)->getValue());\n state_ptr->cam_intrinsics.at(i)->setFej(odometry_options.camera_intrinsics.at(i)->getValue());\n state_ptr->calib_cam_imu.at(i)->setValue(odometry_options.camera_extrinsics.at(i));\n state_ptr->calib_cam_imu.at(i)->setFej(odometry_options.camera_extrinsics.at(i));\n }\n\n int init_max_features = std::floor((double)odometry_options.init_options.init_max_features / (double)2.0);\n\n featureTracker = std::shared_ptr(new trackKLT(state_ptr->cam_intrinsics_cameras, init_max_features, odometry_options.histogram_method, \n odometry_options.fast_threshold, odometry_options.patch_size_x, odometry_options.patch_size_y, odometry_options.min_px_dist));\n\n propagator_ptr = std::make_shared(odometry_options.imu_noises, odometry_options.gravity_mag);\n\n initializer_ptr = std::make_shared(odometry_options.init_options, featureTracker->getFeatureDatabase());\n\n updaterMsckf_ptr = std::make_shared(odometry_options.msckf_options, odometry_options.featinit_options);\n\n updaterSlam_ptr = std::make_shared(odometry_options.slam_options, odometry_options.featinit_options, featureTracker->getFeatureDatabase());\n\n gammaPixel_ptr = std::make_shared();\n\n int wlvl = wG[0], hlvl = hG[0];\n\n while(wlvl % 2 == 0 && hlvl % 2 == 0 && wlvl * hlvl > 5000 && pyr_levels_used < PYR_LEVELS)\n {\n wlvl /=2;\n hlvl /=2;\n pyr_levels_used++;\n }\n\n for (int level = 1; level < pyr_levels_used; ++ level)\n {\n wG[level] = wG[0] >> level;\n hG[level] = hG[0] >> level;\n }\n\n frame_count = 0;\n}\n void initialValue() {\n last_time_image = -1;\n\n last_imu_data.timestamp = -1;\n last_imu_data.gyr.setZero();\n last_imu_data.acc.setZero();\n\n startup_time = -1;\n current_time = -1;\n\n time_newest_imu = -1;\n}\n void imuHandler(const sensor_msgs::Imu::ConstPtr &msg) {\n imuData imu_data;\n imu_data.timestamp = msg->header.stamp.toSec();\n imu_data.gyr << msg->angular_velocity.x, msg->angular_velocity.y, msg->angular_velocity.z;\n imu_data.acc << msg->linear_acceleration.x, msg->linear_acceleration.y, msg->linear_acceleration.z;\n\n processImu(imu_data);\n\n time_newest_imu = imu_data.timestamp;\n}\n void stereoImageHandler(const sensor_msgs::ImageConstPtr &msg_0, const sensor_msgs::ImageConstPtr &msg_1, int cam_0, int cam_1) {\n double timestamp = msg_0->header.stamp.toSec();\n double time_delta = 1.0 / odometry_options.track_frequency;\n\n if (camera_last_timestamp.find(cam_0) != camera_last_timestamp.end() && timestamp < camera_last_timestamp.at(cam_0) + time_delta) return;\n\n camera_last_timestamp[cam_0] = timestamp;\n\n cv_bridge::CvImageConstPtr cv_ptr_0;\n try\n {\n cv_ptr_0 = cv_bridge::toCvShare(msg_0, sensor_msgs::image_encodings::MONO8);\n }\n catch (cv_bridge::Exception &e)\n {\n std::cout << \"cv_bridge exception: \" << e.what() << std::endl;\n return;\n }\n\n cv_bridge::CvImageConstPtr cv_ptr_1;\n try\n {\n cv_ptr_1 = cv_bridge::toCvShare(msg_1, sensor_msgs::image_encodings::MONO8);\n }\n catch (cv_bridge::Exception &e)\n {\n std::cout << \"cv_bridge exception: \" << e.what() << std::endl;\n return;\n }\n\n cameraData camera_data;\n camera_data.timestamp = cv_ptr_0->header.stamp.toSec();\n camera_data.camera_ids.push_back(cam_0);\n camera_data.camera_ids.push_back(cam_1);\n camera_data.images.push_back(cv_ptr_0->image.clone());\n camera_data.images.push_back(cv_ptr_1->image.clone());\n\n if (odometry_options.use_mask)\n {\n camera_data.masks.push_back(odometry_options.masks.at(cam_0));\n camera_data.masks.push_back(odometry_options.masks.at(cam_1));\n }\n else\n {\n camera_data.masks.push_back(cv::Mat::zeros(cv_ptr_0->image.rows, cv_ptr_0->image.cols, CV_8UC1));\n camera_data.masks.push_back(cv::Mat::zeros(cv_ptr_1->image.rows, cv_ptr_1->image.cols, CV_8UC1));\n }\n\n camera_buffer.push(camera_data);\n}\n void run() {\n double timestamp_imu_in_camera = time_newest_imu - state_ptr->calib_dt_imu_cam->value()(0);\n\n while (!camera_buffer.empty() && camera_buffer.front().timestamp < timestamp_imu_in_camera)\n {\n // time test\n boost::posix_time::ptime rT_begin_sum = boost::posix_time::microsec_clock::local_time();\n // time test\n\n processImage(camera_buffer.front());\n\n // time test\n boost::posix_time::ptime rT_end_sum = boost::posix_time::microsec_clock::local_time();\n double time_cost = (rT_end_sum - rT_begin_sum).total_microseconds() * 1e-6;\n sum_time_sum = sum_time_sum * n_time_sum + time_cost;\n n_time_sum++;\n sum_time_sum = sum_time_sum / n_time_sum;\n std::cout << std::fixed << std::setprecision(6) << \"[total] time cost = \" << sum_time_sum << std::endl;\n // time test\n\n camera_buffer.pop();\n }\n}\n ros::NodeHandle nh;\n std::string image_left_topic;\n std::string image_right_topic;\n std::string imu_topic;\n ros::Subscriber sub_imu;\n std::vector subs_cam;\n std::vector>> sync_cam;\n std::vector>> sync_subs_cam;\n void pubFeatImage(cv::Mat &stereo_image, double ×tamp) {\n if (!stereo_image.empty() && pub_feat_image.getNumSubscribers() > 0)\n {\n try {\n sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), \"bgr8\", stereo_image).toImageMsg();\n msg->header.stamp = ros::Time().fromSec(timestamp);\n msg->header.frame_id = \"camera_init\";\n pub_feat_image.publish(msg);\n }\n catch (cv_bridge::Exception& e) {\n ROS_ERROR(\"cv_bridge exception: %s\", e.what());\n }\n }\n}\n void pubOdometry(std::shared_ptr state_ptr, double ×tamp) {\n if (pub_odom.getNumSubscribers() > 0)\n {\n odom.header.frame_id = \"camera_init\";\n odom.child_frame_id = \"body\";\n odom.header.stamp = ros::Time().fromSec(timestamp);\n odom.pose.pose.orientation.x = state_ptr->imu_ptr->getQuat()(0);\n odom.pose.pose.orientation.y = state_ptr->imu_ptr->getQuat()(1);\n odom.pose.pose.orientation.z = state_ptr->imu_ptr->getQuat()(2);\n odom.pose.pose.orientation.w = state_ptr->imu_ptr->getQuat()(3);\n odom.pose.pose.position.x = state_ptr->imu_ptr->getPos()(0);\n odom.pose.pose.position.y = state_ptr->imu_ptr->getPos()(1);\n odom.pose.pose.position.z = state_ptr->imu_ptr->getPos()(2);\n pub_odom.publish(odom);\n }\n}\n void setPoseStamp(geometry_msgs::PoseStamped &body_pose_out, std::shared_ptr state_ptr) {\n body_pose_out.pose.position.x = state_ptr->imu_ptr->getPos()(0);\n body_pose_out.pose.position.y = state_ptr->imu_ptr->getPos()(1);\n body_pose_out.pose.position.z = state_ptr->imu_ptr->getPos()(2);\n \n body_pose_out.pose.orientation.x = state_ptr->imu_ptr->getQuat()(0);\n body_pose_out.pose.orientation.y = state_ptr->imu_ptr->getQuat()(1);\n body_pose_out.pose.orientation.z = state_ptr->imu_ptr->getQuat()(2);\n body_pose_out.pose.orientation.w = state_ptr->imu_ptr->getQuat()(3);\n}\n void pubPath(std::shared_ptr state_ptr, double ×tamp) {\n if (pub_path.getNumSubscribers() > 0)\n {\n setPoseStamp(msg_body_pose, state_ptr);\n msg_body_pose.header.stamp = ros::Time().fromSec(timestamp);\n msg_body_pose.header.frame_id = \"camera_init\";\n\n static int i = 0;\n i++;\n if (i % 10 == 0) \n {\n path.poses.push_back(msg_body_pose);\n path.header.stamp = ros::Time().fromSec(timestamp);\n path.header.frame_id =\"camera_init\";\n pub_path.publish(path);\n }\n }\n}\n void pubHistoryPoints(pcl::PointCloud::Ptr points_history, double ×tamp) {\n if (pub_points_history.getNumSubscribers() > 0)\n {\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*points_history, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_points_history.publish(point_cloud_msg);\n }\n}\n void pubWindowPoints(std::shared_ptr state_ptr, double ×tamp) {\n points_window->clear();\n\n if (pub_points_window.getNumSubscribers() > 0)\n {\n auto it0 = state_ptr->map_points.begin();\n while (it0 != state_ptr->map_points.end())\n {\n pcl::PointXYZRGB point_temp;\n point_temp.x = (*it0).second->getPointXYZ(false)[0];\n point_temp.y = (*it0).second->getPointXYZ(false)[1];\n point_temp.z = (*it0).second->getPointXYZ(false)[2];\n point_temp.r = (*it0).second->color;\n point_temp.g = (*it0).second->color;\n point_temp.b = (*it0).second->color;\n points_window->points.push_back(point_temp);\n\n it0++;\n }\n\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*points_window, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_points_window.publish(point_cloud_msg);\n }\n}\n void pubHistoryVoxels(pcl::PointCloud::Ptr voxels_history, double ×tamp) {\n if (pub_voxels_history.getNumSubscribers() > 0)\n {\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*voxels_history, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_voxels_history.publish(point_cloud_msg);\n }\n\n voxels_history->clear();\n}\n void pubVisitVoxels(pcl::PointCloud::Ptr voxels_visit, double ×tamp) {\n if (pub_voxels_visit.getNumSubscribers() > 0)\n {\n sensor_msgs::PointCloud2 point_cloud_msg;\n pcl::toROSMsg(*voxels_visit, point_cloud_msg);\n point_cloud_msg.header.stamp = ros::Time().fromSec(timestamp);\n point_cloud_msg.header.frame_id = \"camera_init\";\n pub_voxels_visit.publish(point_cloud_msg);\n }\n}\n image_transport::ImageTransport it;\n image_transport::Publisher pub_feat_image;\n ros::Publisher pub_odom;\n ros::Publisher pub_path;\n ros::Publisher pub_points_history;\n ros::Publisher pub_points_window;\n ros::Publisher pub_voxels_history;\n ros::Publisher pub_voxels_visit;\n geometry_msgs::PoseStamped msg_body_pose;\n nav_msgs::Odometry odom;\n nav_msgs::Path path;\n pcl::PointCloud::Ptr points_history;\n pcl::PointCloud::Ptr points_window;\n pcl::PointCloud::Ptr voxels_history;\n pcl::PointCloud::Ptr voxels_visit;\n};"], ["/voxel_svio/include/updaterHelper.h", "class baseType {\n public:\n struct updaterHelperFeature\n\t{\n\t\tsize_t feature_id;\n\n\t\tstd::unordered_map> uvs;\n\n\t\tstd::unordered_map> uvs_norm;\n\n\t\tstd::unordered_map> timestamps;\n\n\t\tstd::unordered_map>> frames;\n\n\t\tstd::unordered_map> color;\n\n\t\tint anchor_cam_id = -1;\n\n\t\tdouble anchor_clone_timestamp = -1;\n\n\t\tEigen::Vector3d position_anchor;\n\n\t\tEigen::Vector3d position_anchor_fej;\n\n\t\tEigen::Vector3d position_global;\n\n\t\tEigen::Vector3d position_global_fej;\n\t};\n static void getFeatureJacobianRepresentation(std::shared_ptr state_ptr, updaterHelperFeature &feature, Eigen::MatrixXd &H_f,\n\t\tstd::vector &H_x, std::vector> &x_order) {\n\tH_f.resize(3, 3);\n\tH_f.setIdentity();\n\treturn;\n}\n static void getFeatureJacobianFull(std::shared_ptr state_ptr, updaterHelperFeature &feature, Eigen::MatrixXd &H_f, \n\t\tEigen::MatrixXd &H_x, Eigen::VectorXd &res, std::vector> &x_order) {\n\tint total_meas = 0;\n\tfor (auto const &pair : feature.timestamps)\n\t{\n\t\ttotal_meas += (int)pair.second.size();\n\t}\n\n\tint total_hx = 0;\n\tstd::unordered_map, size_t> map_hx;\n\tfor (auto const &pair : feature.timestamps)\n\t{\n\t\tstd::shared_ptr calibration = state_ptr->calib_cam_imu.at(pair.first);\n\t\tstd::shared_ptr distortion = state_ptr->cam_intrinsics.at(pair.first);\n\n\t\tif (state_ptr->options.do_calib_camera_pose)\n\t\t{\n\t\t\tmap_hx.insert({calibration, total_hx});\n\t\t\tx_order.push_back(calibration);\n\t\t\ttotal_hx += calibration->getSize();\n\t\t}\n\n\t\tif (state_ptr->options.do_calib_camera_intrinsics)\n\t\t{\n\t\t\tmap_hx.insert({distortion, total_hx});\n\t\t\tx_order.push_back(distortion);\n\t\t\ttotal_hx += distortion->getSize();\n\t\t}\n\n\t\tfor (size_t m = 0; m < feature.timestamps[pair.first].size(); m++)\n\t\t{\n\t\t\tstd::shared_ptr clone_Ci = state_ptr->clones_imu.at(feature.timestamps[pair.first].at(m));\n\n\t\t\tif (map_hx.find(clone_Ci) == map_hx.end())\n\t\t\t{\n\t\t\t\tmap_hx.insert({clone_Ci, total_hx});\n\t\t\t\tx_order.push_back(clone_Ci);\n\t\t\t\ttotal_hx += clone_Ci->getSize();\n\t\t\t}\n\t\t}\n\t}\n\n\tEigen::Vector3d p_FinG = feature.position_global;\n\tEigen::Vector3d p_FinG_fej = feature.position_global_fej;\n\n\tint c = 0;\n\tint jacobsize = 3;\n\n\tres = Eigen::VectorXd::Zero(2 * total_meas);\n\tH_f = Eigen::MatrixXd::Zero(2 * total_meas, jacobsize);\n\tH_x = Eigen::MatrixXd::Zero(2 * total_meas, total_hx);\n\n\tEigen::MatrixXd dpfg_dlambda;\n\tstd::vector dpfg_dx;\n\tstd::vector> dpfg_dx_order;\n\tupdaterHelper::getFeatureJacobianRepresentation(state_ptr, feature, dpfg_dlambda, dpfg_dx, dpfg_dx_order);\n\n\tfor (auto const &pair : feature.timestamps)\n\t{\n\t\tstd::shared_ptr distortion = state_ptr->cam_intrinsics.at(pair.first);\n\t\tstd::shared_ptr calibration = state_ptr->calib_cam_imu.at(pair.first);\n\t\tEigen::Matrix3d R_ItoC = calibration->getRot();\n\t\tEigen::Vector3d p_IinC = calibration->getPos();\n\n\t\tfor (size_t m = 0; m < feature.timestamps[pair.first].size(); m++)\n\t\t{\n\t\t\tstd::shared_ptr clone_Ii = state_ptr->clones_imu.at(feature.timestamps[pair.first].at(m));\n\t\t\tEigen::Matrix3d R_GtoIi = clone_Ii->getRot();\n\t\t\tEigen::Vector3d p_IiinG = clone_Ii->getPos();\n\n\t\t\tEigen::Vector3d p_FinIi = R_GtoIi * (p_FinG - p_IiinG);\n\n\t\t\tEigen::Vector3d p_FinCi = R_ItoC * p_FinIi + p_IinC;\n\t\t\tEigen::Vector2d uv_norm;\n\t\t\tuv_norm << p_FinCi(0) / p_FinCi(2), p_FinCi(1) / p_FinCi(2);\n\n\t\t\tEigen::Vector2d uv_dist;\n\t\t\tuv_dist = state_ptr->cam_intrinsics_cameras.at(pair.first)->distortD(uv_norm);\n\n\t\t\tEigen::Vector2d uv_m;\n\t\t\tuv_m << (double)feature.uvs[pair.first].at(m)(0), (double)feature.uvs[pair.first].at(m)(1);\n\t\t\tres.block(2 * c, 0, 2, 1) = uv_m - uv_dist;\n\n\t\t\tif (state_ptr->options.do_fej)\n\t\t\t{\n\t\t\t\tR_GtoIi = clone_Ii->getRotFej();\n\t\t\t\tp_IiinG = clone_Ii->getPosFej();\n\n\t\t\t\tp_FinIi = R_GtoIi * (p_FinG_fej - p_IiinG);\n\t\t\t\tp_FinCi = R_ItoC * p_FinIi + p_IinC;\n\t\t\t}\n\n\t\t\tEigen::MatrixXd dz_dzn, dz_dzeta;\n\t\t\tstate_ptr->cam_intrinsics_cameras.at(pair.first)->computeDistortJacobian(uv_norm, dz_dzn, dz_dzeta);\n\n\t\t\tEigen::MatrixXd dzn_dpfc = Eigen::MatrixXd::Zero(2, 3);\n\t\t\tdzn_dpfc << 1 / p_FinCi(2), 0, -p_FinCi(0) / (p_FinCi(2) * p_FinCi(2)), 0, 1 / p_FinCi(2), -p_FinCi(1) / (p_FinCi(2) * p_FinCi(2));\n\n\t\t\tEigen::MatrixXd dpfc_dpfg = R_ItoC * R_GtoIi;\n\n\t\t\tEigen::MatrixXd dpfc_dclone = Eigen::MatrixXd::Zero(3, 6);\n\t\t\tdpfc_dclone.block(0, 0, 3, 3).noalias() = R_ItoC * quatType::skewSymmetric(p_FinIi);\n\t\t\tdpfc_dclone.block(0, 3, 3, 3) = - dpfc_dpfg;\n\n\t\t\tEigen::MatrixXd dz_dpfc = dz_dzn * dzn_dpfc;\n\t\t\tEigen::MatrixXd dz_dpfg = dz_dpfc * dpfc_dpfg;\n\n\t\t\tH_f.block(2 * c, 0, 2, H_f.cols()).noalias() = dz_dpfg * dpfg_dlambda;\n\t\t\tH_x.block(2 * c, map_hx[clone_Ii], 2, clone_Ii->getSize()).noalias() = dz_dpfc * dpfc_dclone;\n\n\t\t\tfor (size_t i = 0; i < dpfg_dx_order.size(); i++)\n\t\t\t{\n\t\t\t\tH_x.block(2 * c, map_hx[dpfg_dx_order.at(i)], 2, dpfg_dx_order.at(i)->getSize()).noalias() += dz_dpfg * dpfg_dx.at(i);\n\t\t\t}\n\n\t\t\tif (state_ptr->options.do_calib_camera_pose)\n\t\t\t{\n\t\t\t\tEigen::MatrixXd dpfc_dcalib = Eigen::MatrixXd::Zero(3, 6);\n\t\t\t\tdpfc_dcalib.block(0, 0, 3, 3) = quatType::skewSymmetric(p_FinCi - p_IinC);\n\t\t\t\tdpfc_dcalib.block(0, 3, 3, 3) = Eigen::Matrix::Identity();\n\n\t\t\t\tH_x.block(2 * c, map_hx[calibration], 2, calibration->getSize()).noalias() += dz_dpfc * dpfc_dcalib;\n\t\t\t}\n\n\t\t\tif (state_ptr->options.do_calib_camera_intrinsics)\n\t\t\t{\n\t\t\t\tH_x.block(2 * c, map_hx[distortion], 2, distortion->getSize()) = dz_dzeta;\n\t\t\t}\n\n\t\t\tif (state_ptr->options.use_huber)\n\t\t\t{\n\t\t\t\tdouble loss = res.block(2 * c, 0, 2, 1).norm();\n\t\t\t\tdouble hw = loss < setting_huber_th ? 1 : setting_huber_th / loss;\n\n\t\t\t\tif (hw < 1) hw = sqrt(hw);\n\n\t\t\t\tres.block(2 * c, 0, 2, 1) *= hw;\n\t\t\t\tH_x.block(2 * c, 0, 2, H_x.cols()) *= hw;\n\t\t\t\tH_f.block(2 * c, 0, 2, H_f.cols()) *= hw;\n\t\t\t}\n\n\t\t\tc++;\n\t\t}\n\t}\n}\n static void nullspaceProjectInplace(Eigen::MatrixXd &H_f, Eigen::MatrixXd &H_x, Eigen::VectorXd &res) {\n\tEigen::JacobiRotation Ho_GR_temp;\n \n\tfor (int n = 0; n < H_f.cols(); ++n)\n\t{\n\t\tfor (int m = (int)H_f.rows() - 1; m > n; m--)\n\t\t{\n\t\t\tHo_GR_temp.makeGivens(H_f(m - 1, n), H_f(m, n));\n\t\t\t(H_f.block(m - 1, n, 2, H_f.cols() - n)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t\t(H_x.block(m - 1, 0, 2, H_x.cols())).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t\t(res.block(m - 1, 0, 2, 1)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t}\n\t}\n\n\tH_x = H_x.block(H_f.cols(), 0, H_x.rows() - H_f.cols(), H_x.cols()).eval();\n\tres = res.block(H_f.cols(), 0, res.rows() - H_f.cols(), res.cols()).eval();\n\n\tassert(H_x.rows() == res.rows());\n}\n static void measurementCompressInplace(Eigen::MatrixXd &H_x, Eigen::VectorXd &res) {\n\tif (H_x.rows() <= H_x.cols())\n\t\treturn;\n\n\tEigen::JacobiRotation Ho_GR_temp;\n\tfor (int n = 0; n < H_x.cols(); n++)\n\t{\n\t\tfor (int m = (int)H_x.rows() - 1; m > n; m--)\n\t\t{\n\t\t\tHo_GR_temp.makeGivens(H_x(m - 1, n), H_x(m, n));\n\n\t\t\t(H_x.block(m - 1, n, 2, H_x.cols() - n)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t\t(res.block(m - 1, 0, 2, 1)).applyOnTheLeft(0, 1, Ho_GR_temp.adjoint());\n\t\t}\n\t}\n\n\tint r = std::min(H_x.rows(), H_x.cols());\n\n\tassert(r <= H_x.rows());\n\tH_x.conservativeResize(r, H_x.cols());\n\tres.conservativeResize(r, res.cols());\n}\n};"], ["/voxel_svio/include/state.h", "class mapPoint {\n public:\n baseType(int size_) {\n\tsize = size_;\n}\n virtual void setLocalId(int new_id) {\n\tid = new_id;\n\tpose_->setLocalId(new_id);\n\tv_->setLocalId(pose_->getId() + ((new_id != -1) ? pose_->getSize() : 0));\n\tbg_->setLocalId(v_->getId() + ((new_id != -1) ? v_->getSize() : 0));\n\tba_->setLocalId(bg_->getId() + ((new_id != -1) ? bg_->getSize() : 0));\n}\n int getId() {\n\treturn id;\n}\n int getSize() {\n\treturn size;\n}\n virtual void update(const Eigen::VectorXd &dx) = 0 {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n virtual void setValue(const Eigen::MatrixXd &new_value) {\n\tsetValueInternal(new_value);\n}\n virtual void setFej(const Eigen::MatrixXd &new_value) {\n\tsetFejInternal(new_value);\n}\n virtual std::shared_ptr clone() = 0 {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n virtual std::shared_ptr checkIfSubvariable(const std::shared_ptr check) {\n\tif (check == pose_)\n\t{\n\t\treturn pose_;\n\t}\n\telse if (check == pose_->checkIfSubvariable(check))\n\t{\n\t\treturn pose_->checkIfSubvariable(check);\n\t} \n\telse if (check == v_)\n\t{\n\t\treturn v_;\n\t}\n\telse if (check == bg_)\n\t{\n\t\treturn bg_;\n\t}\n\telse if (check == ba_)\n\t{\n\t\treturn ba_;\n\t}\n\treturn nullptr;\n}\n protected:\n Eigen::MatrixXd fej_;\n Eigen::MatrixXd value_;\n int id = -1;\n int size = -1;\n public:\n jplQuat() {\n\tEigen::Vector4d q_temp = Eigen::Vector4d::Zero();\n\tq_temp(3) = 1.0;\n\tsetValueInternal(q_temp);\n\tsetFejInternal(q_temp);\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n void setValue(const Eigen::MatrixXd &new_value) override {\n\tsetValueInternal(new_value);\n}\n void setFej(const Eigen::MatrixXd &new_value) override {\n\tsetFejInternal(new_value);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n Eigen::Matrix getRot() {\n\treturn pose_->getRot();\n}\n Eigen::Matrix getRotFej() {\n\treturn pose_->getRotFej();\n}\n protected:\n Eigen::Matrix R;\n Eigen::Matrix R_fej;\n void setValueInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setValue(new_value.block<7, 1>(0, 0));\n\tv_->setValue(new_value.block<3, 1>(7, 0));\n\tbg_->setValue(new_value.block<3, 1>(10, 0));\n\tba_->setValue(new_value.block<3, 1>(13, 0));\n\n\tvalue_ = new_value;\n}\n void setFejInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setFej(new_value.block<7, 1>(0, 0));\n\tv_->setFej(new_value.block<3, 1>(7, 0));\n\tbg_->setFej(new_value.block<3, 1>(10, 0));\n\tba_->setFej(new_value.block<3, 1>(13, 0));\n\n\tfej_ = new_value;\n}\n public:\n vec(int dim) {\n\tvalue_ = Eigen::VectorXd::Zero(dim);\n\tfej_ = Eigen::VectorXd::Zero(dim);\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n public:\n poseJpl() {\n\tq_ = std::shared_ptr(new jplQuat());\n\tp_ = std::shared_ptr(new vec(3));\n\n\tEigen::Matrix pose_temp;\n\tpose_temp.setZero();\n pose_temp(3) = 1.0;\n setValueInternal(pose_temp);\n setFejInternal(pose_temp);\n}\n void setLocalId(int new_id) override {\n\tid = new_id;\n\tpose_->setLocalId(new_id);\n\tv_->setLocalId(pose_->getId() + ((new_id != -1) ? pose_->getSize() : 0));\n\tbg_->setLocalId(v_->getId() + ((new_id != -1) ? v_->getSize() : 0));\n\tba_->setLocalId(bg_->getId() + ((new_id != -1) ? bg_->getSize() : 0));\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n void setValue(const Eigen::MatrixXd &new_value) override {\n\tsetValueInternal(new_value);\n}\n void setFej(const Eigen::MatrixXd &new_value) override {\n\tsetFejInternal(new_value);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n std::shared_ptr checkIfSubvariable(const std::shared_ptr check) override {\n\tif (check == pose_)\n\t{\n\t\treturn pose_;\n\t}\n\telse if (check == pose_->checkIfSubvariable(check))\n\t{\n\t\treturn pose_->checkIfSubvariable(check);\n\t} \n\telse if (check == v_)\n\t{\n\t\treturn v_;\n\t}\n\telse if (check == bg_)\n\t{\n\t\treturn bg_;\n\t}\n\telse if (check == ba_)\n\t{\n\t\treturn ba_;\n\t}\n\treturn nullptr;\n}\n Eigen::Matrix getRot() {\n\treturn pose_->getRot();\n}\n Eigen::Matrix getRotFej() {\n\treturn pose_->getRotFej();\n}\n Eigen::Matrix getQuat() {\n\treturn pose_->getQuat();\n}\n Eigen::Matrix getQuatFej() {\n\treturn pose_->getQuatFej();\n}\n Eigen::Matrix getPos() {\n\treturn pose_->getPos();\n}\n Eigen::Matrix getPosFej() {\n\treturn pose_->getPosFej();\n}\n std::shared_ptr q() {\n\treturn pose_->q();\n}\n std::shared_ptr p() {\n\treturn pose_->p();\n}\n protected:\n std::shared_ptr q_;\n std::shared_ptr p_;\n void setValueInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setValue(new_value.block<7, 1>(0, 0));\n\tv_->setValue(new_value.block<3, 1>(7, 0));\n\tbg_->setValue(new_value.block<3, 1>(10, 0));\n\tba_->setValue(new_value.block<3, 1>(13, 0));\n\n\tvalue_ = new_value;\n}\n void setFejInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setFej(new_value.block<7, 1>(0, 0));\n\tv_->setFej(new_value.block<3, 1>(7, 0));\n\tbg_->setFej(new_value.block<3, 1>(10, 0));\n\tba_->setFej(new_value.block<3, 1>(13, 0));\n\n\tfej_ = new_value;\n}\n public:\n imuState() {\n\tpose_ = std::shared_ptr(new poseJpl());\n\tv_ = std::shared_ptr(new vec(3));\n\tbg_ = std::shared_ptr(new vec(3));\n\tba_ = std::shared_ptr(new vec(3));\n\n\tEigen::VectorXd imu_temp = Eigen::VectorXd::Zero(16, 1);\n\timu_temp(3) = 1.0;\n\tsetValueInternal(imu_temp);\n\tsetFejInternal(imu_temp);\n}\n void setLocalId(int new_id) override {\n\tid = new_id;\n\tpose_->setLocalId(new_id);\n\tv_->setLocalId(pose_->getId() + ((new_id != -1) ? pose_->getSize() : 0));\n\tbg_->setLocalId(v_->getId() + ((new_id != -1) ? v_->getSize() : 0));\n\tba_->setLocalId(bg_->getId() + ((new_id != -1) ? bg_->getSize() : 0));\n}\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\n\tEigen::Matrix new_x = value_;\n\n\tEigen::Matrix dq;\n\tdq << 0.5 * dx.block<3, 1>(0, 0), 1.0;\n\tdq = quatType::quatNorm(dq);\n\n\tnew_x.block<4, 1>(0, 0) = quatType::quatMultiply(dq, getQuat());\n\tnew_x.block<3, 1>(4, 0) += dx.block<3, 1>(3, 0);\n\n\tnew_x.block<3, 1>(7, 0) += dx.block<3, 1>(6, 0);\n\tnew_x.block<3, 1>(10, 0) += dx.block<3, 1>(9, 0);\n\tnew_x.block<3, 1>(13, 0) += dx.block<3, 1>(12, 0);\n\n\tsetValue(new_x);\n}\n void setValue(const Eigen::MatrixXd &new_value) override {\n\tsetValueInternal(new_value);\n}\n void setFej(const Eigen::MatrixXd &new_value) override {\n\tsetFejInternal(new_value);\n}\n std::shared_ptr clone() override {\n\tauto clone_temp = std::shared_ptr(new imuState());\n\tclone_temp->setValue(value());\n\tclone_temp->setFej(fej());\n\treturn clone_temp;\n}\n std::shared_ptr checkIfSubvariable(const std::shared_ptr check) override {\n\tif (check == pose_)\n\t{\n\t\treturn pose_;\n\t}\n\telse if (check == pose_->checkIfSubvariable(check))\n\t{\n\t\treturn pose_->checkIfSubvariable(check);\n\t} \n\telse if (check == v_)\n\t{\n\t\treturn v_;\n\t}\n\telse if (check == bg_)\n\t{\n\t\treturn bg_;\n\t}\n\telse if (check == ba_)\n\t{\n\t\treturn ba_;\n\t}\n\treturn nullptr;\n}\n Eigen::Matrix getRot() {\n\treturn pose_->getRot();\n}\n Eigen::Matrix getRotFej() {\n\treturn pose_->getRotFej();\n}\n Eigen::Matrix getQuat() {\n\treturn pose_->getQuat();\n}\n Eigen::Matrix getQuatFej() {\n\treturn pose_->getQuatFej();\n}\n Eigen::Matrix getPos() {\n\treturn pose_->getPos();\n}\n Eigen::Matrix getPosFej() {\n\treturn pose_->getPosFej();\n}\n Eigen::Matrix getVel() {\n\treturn v_->value();\n}\n Eigen::Matrix getVelFej() {\n\treturn v_->fej();\n}\n Eigen::Matrix getBg() {\n\treturn bg_->value();\n}\n Eigen::Matrix getBgFej() {\n\treturn bg_->fej();\n}\n Eigen::Matrix getBa() {\n\treturn ba_->value();\n}\n Eigen::Matrix getBaFej() {\n\treturn ba_->fej();\n}\n std::shared_ptr pose() {\n\treturn pose_;\n}\n std::shared_ptr q() {\n\treturn pose_->q();\n}\n std::shared_ptr p() {\n\treturn pose_->p();\n}\n std::shared_ptr v() {\n\treturn v_;\n}\n std::shared_ptr bg() {\n\treturn bg_;\n}\n std::shared_ptr ba() {\n\treturn ba_;\n}\n protected:\n std::shared_ptr pose_;\n std::shared_ptr v_;\n std::shared_ptr bg_;\n std::shared_ptr ba_;\n void setValueInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setValue(new_value.block<7, 1>(0, 0));\n\tv_->setValue(new_value.block<3, 1>(7, 0));\n\tbg_->setValue(new_value.block<3, 1>(10, 0));\n\tba_->setValue(new_value.block<3, 1>(13, 0));\n\n\tvalue_ = new_value;\n}\n void setFejInternal(const Eigen::MatrixXd &new_value) {\n\tassert(new_value.rows() == 16);\n\tassert(new_value.cols() == 1);\n\n\tpose_->setFej(new_value.block<7, 1>(0, 0));\n\tv_->setFej(new_value.block<3, 1>(7, 0));\n\tbg_->setFej(new_value.block<3, 1>(10, 0));\n\tba_->setFej(new_value.block<3, 1>(13, 0));\n\n\tfej_ = new_value;\n}\n public:\n state(stateOptions &options_) {\n\toptions = options_;\n\n\tint current_id = 0;\n\timu_ptr = std::make_shared();\n\timu_ptr->setLocalId(current_id);\n\tvariables.push_back(imu_ptr);\n\tcurrent_id += imu_ptr->getSize();\n\n\tcalib_imu_dw = std::make_shared(6);\n\tcalib_imu_da = std::make_shared(6);\n\n\tif (options.imu_model == ImuModel::KALIBR)\n\t{\n Eigen::Matrix imu_default = Eigen::Matrix::Zero();\n imu_default << 1.0, 0.0, 0.0, 1.0, 0.0, 1.0;\n calib_imu_dw->setValue(imu_default);\n calib_imu_dw->setFej(imu_default);\n calib_imu_da->setValue(imu_default);\n calib_imu_da->setFej(imu_default);\n }\n else\n {\n Eigen::Matrix imu_default = Eigen::Matrix::Zero();\n imu_default << 1.0, 0.0, 0.0, 1.0, 0.0, 1.0;\n calib_imu_dw->setValue(imu_default);\n calib_imu_dw->setFej(imu_default);\n calib_imu_da->setValue(imu_default);\n calib_imu_da->setFej(imu_default);\n }\n calib_imu_tg = std::make_shared(9);\n calib_imu_gyr = std::make_shared();\n calib_imu_acc = std::make_shared();\n\n if (options.do_calib_imu_intrinsics)\n {\n calib_imu_dw->setLocalId(current_id);\n variables.push_back(calib_imu_dw);\n current_id += calib_imu_dw->getSize();\n\n calib_imu_da->setLocalId(current_id);\n variables.push_back(calib_imu_da);\n current_id += calib_imu_da->getSize();\n\n if (options.do_calib_imu_g_sensitivity)\n {\n calib_imu_tg->setLocalId(current_id);\n variables.push_back(calib_imu_tg);\n current_id += calib_imu_tg->getSize();\n }\n\n if (options.imu_model == ImuModel::KALIBR)\n {\n calib_imu_gyr->setLocalId(current_id);\n variables.push_back(calib_imu_gyr);\n current_id += calib_imu_gyr->getSize();\n }\n else\n {\n calib_imu_acc->setLocalId(current_id);\n variables.push_back(calib_imu_acc);\n current_id += calib_imu_acc->getSize();\n }\n }\n\n calib_dt_imu_cam = std::make_shared(1);\n\n if (options.do_calib_camera_timeoffset)\n {\n calib_dt_imu_cam->setLocalId(current_id);\n variables.push_back(calib_dt_imu_cam);\n current_id += calib_dt_imu_cam->getSize();\n }\n\n for (int i = 0; i < 2; i++)\n {\n auto pose = std::make_shared();\n auto intrin = std::make_shared(8);\n\n calib_cam_imu.insert({i, pose});\n cam_intrinsics.insert({i, intrin});\n\n if (options.do_calib_camera_pose)\n {\n pose->setLocalId(current_id);\n variables.push_back(pose);\n current_id += pose->getSize();\n }\n\n if (options.do_calib_camera_intrinsics)\n {\n intrin->setLocalId(current_id);\n variables.push_back(intrin);\n current_id += intrin->getSize();\n }\n }\n\n cov = std::pow(1e-3, 2) * Eigen::MatrixXd::Identity(current_id, current_id);\n\n if (options.do_calib_imu_intrinsics)\n {\n cov.block(calib_imu_dw->getId(), calib_imu_dw->getId(), 6, 6) = std::pow(0.005, 2) * Eigen::Matrix::Identity();\n cov.block(calib_imu_da->getId(), calib_imu_da->getId(), 6, 6) = std::pow(0.008, 2) * Eigen::Matrix::Identity();\n\n if (options.do_calib_imu_g_sensitivity)\n cov.block(calib_imu_tg->getId(), calib_imu_tg->getId(), 9, 9) = std::pow(0.005, 2) * Eigen::Matrix::Identity();\n\n if (options.imu_model == ImuModel::KALIBR)\n cov.block(calib_imu_gyr->getId(), calib_imu_gyr->getId(), 3, 3) = std::pow(0.005, 2) * Eigen::Matrix3d::Identity();\n else\n cov.block(calib_imu_acc->getId(), calib_imu_acc->getId(), 3, 3) = std::pow(0.005, 2) * Eigen::Matrix3d::Identity();\n }\n\n if (options.do_calib_camera_timeoffset)\n cov(calib_dt_imu_cam->getId(), calib_dt_imu_cam->getId()) = std::pow(0.01, 2);\n\n if (options.do_calib_camera_pose)\n {\n for (int i = 0; i < 2; i++)\n {\n cov.block(calib_cam_imu.at(i)->getId(), calib_cam_imu.at(i)->getId(), 3, 3) = std::pow(0.005, 2) * Eigen::MatrixXd::Identity(3, 3);\n cov.block(calib_cam_imu.at(i)->getId() + 3, calib_cam_imu.at(i)->getId() + 3, 3, 3) = std::pow(0.015, 2) * Eigen::MatrixXd::Identity(3, 3);\n }\n }\n\n if (options.do_calib_camera_intrinsics)\n {\n for (int i = 0; i < 2; i++) {\n cov.block(cam_intrinsics.at(i)->getId(), cam_intrinsics.at(i)->getId(), 4, 4) = std::pow(1.0, 2) * Eigen::MatrixXd::Identity(4, 4);\n cov.block(cam_intrinsics.at(i)->getId() + 4, cam_intrinsics.at(i)->getId() + 4, 4, 4) = std::pow(0.005, 2) * Eigen::MatrixXd::Identity(4, 4);\n }\n }\n}\n double margtimestep() {\n\tdouble time = INFINITY;\n\n\tfor (const auto &clone_imu : clones_imu)\n\t{\n\t\tif (clone_imu.first < time)\n\t\t{\n\t\t\ttime = clone_imu.first;\n\t\t}\n\t}\n\n return time;\n}\n int maxCovSize() {\n\treturn (int)cov.rows();\n}\n static Eigen::Matrix3d Dm(ImuModel imu_model_, const Eigen::MatrixXd &vec_temp) {\n\tassert(vec_temp.rows() == 6);\n\tassert(vec_temp.cols() == 1);\n\tEigen::Matrix3d D_matrix = Eigen::Matrix3d::Identity();\n\t\n\tif (imu_model_ == ImuModel::KALIBR)\n\t\tD_matrix << vec_temp(0), 0, 0, vec_temp(1), vec_temp(3), 0, vec_temp(2), vec_temp(4), vec_temp(5);\n\telse\n\t\tD_matrix << vec_temp(0), vec_temp(1), vec_temp(3), 0, vec_temp(2), vec_temp(4), 0, 0, vec_temp(5);\n\n\treturn D_matrix;\n}\n static Eigen::Matrix3d Tg(const Eigen::MatrixXd &vec_temp) {\n\tassert(vec_temp.rows() == 9);\n\tassert(vec_temp.cols() == 1);\n\tEigen::Matrix3d Tg = Eigen::Matrix3d::Zero();\n\n\tTg << vec_temp(0), vec_temp(3), vec_temp(6), vec_temp(1), vec_temp(4), vec_temp(7), vec_temp(2), vec_temp(5), vec_temp(8);\n\t\n\treturn Tg;\n}\n int imuIntrinsicSize() {\n\tint size_temp = 0;\n\n\tif (options.do_calib_imu_intrinsics)\n\t{\n\t\tsize_temp += 15;\n\n\t\tif (options.do_calib_imu_g_sensitivity)\n\t\t\tsize_temp += 9;\n\t}\n\t\n\treturn size_temp;\n}\n double timestamp = -1;\n stateOptions options;\n std::shared_ptr imu_ptr;\n std::map> clones_imu;\n std::map> clones_frame;\n std::unordered_map> map_points;\n std::shared_ptr calib_dt_imu_cam;\n std::unordered_map> calib_cam_imu;\n std::unordered_map> cam_intrinsics;\n std::unordered_map> cam_intrinsics_cameras;\n std::shared_ptr calib_imu_dw;\n std::shared_ptr calib_imu_da;\n std::shared_ptr calib_imu_tg;\n std::shared_ptr calib_imu_gyr;\n std::shared_ptr calib_imu_acc;\n private:\n friend class stateHelper;\n Eigen::MatrixXd cov;\n std::vector> variables;\n};"], ["/voxel_svio/include/mapManagement.h", "class mapManagement {\n public:\n static bool addPointToVoxel(voxelHashMap &voxel_map, std::shared_ptr map_point_ptr, double voxel_size, int max_num_points_in_voxel, \n double min_distance_points, pcl::PointCloud::Ptr voxels_history) {\n\tEigen::Vector3d point = map_point_ptr->getPointXYZ(false);\n\n\tshort kx = static_cast(point[0] / voxel_size);\n\tshort ky = static_cast(point[1] / voxel_size);\n\tshort kz = static_cast(point[2] / voxel_size);\n\n\tmap_point_ptr->voxel_idx[0] = kx;\n\tmap_point_ptr->voxel_idx[1] = ky;\n\tmap_point_ptr->voxel_idx[2] = kz;\n\n\tvoxelHashMap::iterator search = voxel_map.find(voxel(kx, ky, kz));\n\n\tif(search != voxel_map.end())\n\t{\n\t\tauto &voxel_block = (search.value());\n\n\t\tif(!voxel_block.IsFull())\n\t\t{\n\t\t\tdouble sq_dist_min_to_points = 10 * voxel_size * voxel_size;\n\t\t\tfor (int i(0); i < voxel_block.NumPoints(); ++i)\n\t\t\t{\n\t\t\t\tauto &map_point_ptr_ = voxel_block.points[i];\n\t\t\t\tdouble sq_dist = (map_point_ptr_->getPointXYZ(false) - point).squaredNorm();\n\n\t\t\t\tif (sq_dist < sq_dist_min_to_points)\n\t\t\t\t{\n\t\t\t\t\tsq_dist_min_to_points = sq_dist;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif(sq_dist_min_to_points > (min_distance_points * min_distance_points))\n\t\t\t{\n\t\t\t\tvoxel_block.AddPoint(map_point_ptr);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\t\telse\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\telse\n\t{\n\t\tvoxelBlock block(max_num_points_in_voxel);\n\t\tblock.AddPoint(map_point_ptr);\n\t\tvoxel_map[voxel(kx, ky, kz)] = std::move(block);\n\n\t\t// display\n pcl::PointXYZI point_temp;\n\n point_temp.x = kx >= 0 ? (kx + 0.5) * voxel_size : (kx - 0.5) * voxel_size;\n point_temp.y = ky >= 0 ? (ky + 0.5) * voxel_size : (ky - 0.5) * voxel_size;\n point_temp.z = kz >= 0 ? (ky + 0.5) * voxel_size : (kz - 0.5) * voxel_size;\n point_temp.intensity = 1;\n\n voxels_history->points.push_back(point_temp);\n // display\n\n\t\treturn true;\n }\n}\n static void changeHostVoxel(voxelHashMap &voxel_map, std::shared_ptr map_point_ptr, double voxel_size, int max_num_points_in_voxel, \n double min_distance_points, pcl::PointCloud::Ptr voxels_history) {\n\tEigen::Vector3d point = map_point_ptr->getPointXYZ(false);\n\n\tshort kx = static_cast(point[0] / voxel_size);\n\tshort ky = static_cast(point[1] / voxel_size);\n\tshort kz = static_cast(point[2] / voxel_size);\n\n\tif (kx == map_point_ptr->voxel_idx[0] && ky == map_point_ptr->voxel_idx[1] && kz == map_point_ptr->voxel_idx[2]) return;\n\n\tvoxelHashMap::iterator search_old = voxel_map.find(voxel(map_point_ptr->voxel_idx[0], map_point_ptr->voxel_idx[1], map_point_ptr->voxel_idx[2]));\n\n\tauto &voxel_block_old = (search_old.value());\n\tauto it_old = voxel_block_old.points.begin();\n\n\tbool has_found = false;\n\n\twhile (it_old != voxel_block_old.points.end())\n\t{\n\t\tif ((*it_old) == map_point_ptr)\n\t\t{\n\t\t\thas_found = true;\n\t\t\tit_old = voxel_block_old.points.erase(it_old);\n\t\t\tbreak;\n\t\t}\n\n\t\tit_old++;\n\t}\n\n\tassert(has_found);\n\n\tmap_point_ptr->voxel_idx[0] = kx;\n\tmap_point_ptr->voxel_idx[1] = ky;\n\tmap_point_ptr->voxel_idx[2] = kz;\n\n\tvoxelHashMap::iterator search_new = voxel_map.find(voxel(kx, ky, kz));\n\n\tif(search_new != voxel_map.end())\n\t{\n\t\tauto &voxel_block_new = (search_new.value());\n\t\tvoxel_block_new.AddPoint(map_point_ptr);\n\t}\n\telse\n\t{\n\t\tvoxelBlock block_temp(max_num_points_in_voxel);\n\t\tblock_temp.AddPoint(map_point_ptr);\n\t\tvoxel_map[voxel(kx, ky, kz)] = std::move(block_temp);\n\n\t\t// display\n pcl::PointXYZI point_temp;\n\n point_temp.x = kx >= 0 ? (kx + 0.5) * voxel_size : (kx - 0.5) * voxel_size;\n point_temp.y = ky >= 0 ? (ky + 0.5) * voxel_size : (ky - 0.5) * voxel_size;\n point_temp.z = kz >= 0 ? (kz + 0.5) * voxel_size : (kz - 0.5) * voxel_size;\n point_temp.intensity = 1;\n\n voxels_history->points.push_back(point_temp);\n // display\n\t}\n}\n static void deleteFromVoxel(voxelHashMap &voxel_map, std::shared_ptr map_point_ptr) {\n\tvoxelHashMap::iterator search = voxel_map.find(voxel(map_point_ptr->voxel_idx[0], map_point_ptr->voxel_idx[1], map_point_ptr->voxel_idx[2]));\n\n\tauto &voxel_block = (search.value());\n\tauto it = voxel_block.points.begin();\n\n\tbool has_found = false;\n\n\twhile (it != voxel_block.points.end())\n\t{\n\t\tif (*it == map_point_ptr)\n\t\t{\n\t\t\thas_found = true;\n\t\t\tit = voxel_block.points.erase(it);\n\t\t\tbreak;\n\t\t}\n\n\t\tit++;\n\t}\n\n\tassert(has_found);\n}\n private:\n mapManagement();\n};"], ["/voxel_svio/include/initializerHelper.h", "class initializerHelper {\n public:\n static imuData interpolateData(const imuData &imu_1, const imuData &imu_2, double timestamp) {\n\tdouble lambda = (timestamp - imu_1.timestamp) / (imu_2.timestamp - imu_1.timestamp);\n\n\timuData data;\n\tdata.timestamp = timestamp;\n\tdata.acc = (1 - lambda) * imu_1.acc + lambda * imu_2.acc;\n\tdata.gyr = (1 - lambda) * imu_1.gyr + lambda * imu_2.gyr;\n\n\treturn data;\n}\n static std::vector selectImuReadings(const std::vector &imu_data_tmp, double time_0, double time_1) {\n\tstd::vector prop_data;\n\n\tif (imu_data_tmp.empty()) return prop_data;\n\n\tfor (size_t i = 0; i < imu_data_tmp.size() - 1; i++)\n\t{\n\t\tif (imu_data_tmp.at(i + 1).timestamp > time_0 && imu_data_tmp.at(i).timestamp < time_0)\n\t\t{\n\t\t\timuData data = interpolateData(imu_data_tmp.at(i), imu_data_tmp.at(i + 1), time_0);\n\t\t\tprop_data.push_back(data);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (imu_data_tmp.at(i).timestamp >= time_0 && imu_data_tmp.at(i + 1).timestamp <= time_1)\n\t\t{\n\t\t\tprop_data.push_back(imu_data_tmp.at(i));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (imu_data_tmp.at(i + 1).timestamp > time_1)\n\t\t{\n\t\t\tif (imu_data_tmp.at(i).timestamp > time_1 && i == 0)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\telse if (imu_data_tmp.at(i).timestamp > time_1)\n\t\t\t{\n\t\t\t\timuData data = interpolateData(imu_data_tmp.at(i - 1), imu_data_tmp.at(i), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tprop_data.push_back(imu_data_tmp.at(i));\n\t\t\t}\n \n\t\t\tif (prop_data.at(prop_data.size() - 1).timestamp != time_1)\n\t\t\t{\n\t\t\t\timuData data = interpolateData(imu_data_tmp.at(i), imu_data_tmp.at(i + 1), time_1);\n\t\t\t\tprop_data.push_back(data);\n\t\t\t}\n\t\t\t\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tif (prop_data.empty()) return prop_data;\n\n\tfor (size_t i = 0; i < prop_data.size() - 1; i++)\n\t{\n\t\tif (std::abs(prop_data.at(i + 1).timestamp - prop_data.at(i).timestamp) < 1e-12)\n\t\t{\n\t\t\tprop_data.erase(prop_data.begin() + i);\n\t\t\ti--;\n\t\t}\n\t}\n\n\treturn prop_data;\n}\n static void gramSchmidt(const Eigen::Vector3d &gravity_inI, Eigen::Matrix3d &R_GtoI) {\n\tEigen::Vector3d z_axis = gravity_inI / gravity_inI.norm();\n\tEigen::Vector3d x_axis, y_axis;\n\tEigen::Vector3d e_1(1.0, 0.0, 0.0);\n\tEigen::Vector3d e_2(0.0, 1.0, 0.0);\n\n\tdouble inner1 = e_1.dot(z_axis) / z_axis.norm();\n\tdouble inner2 = e_2.dot(z_axis) / z_axis.norm();\n\n\tif (fabs(inner1) < fabs(inner2))\n\t{\n\t\tx_axis = z_axis.cross(e_1);\n\t\tx_axis = x_axis / x_axis.norm();\n\t\ty_axis = z_axis.cross(x_axis);\n\t\ty_axis = y_axis / y_axis.norm();\n\t}\n\telse\n\t{\n\t\tx_axis = z_axis.cross(e_2);\n\t\tx_axis = x_axis / x_axis.norm();\n\t\ty_axis = z_axis.cross(x_axis);\n\t\ty_axis = y_axis / y_axis.norm();\n\t}\n\n\tR_GtoI.block(0, 0, 3, 1) = x_axis;\n\tR_GtoI.block(0, 1, 3, 1) = y_axis;\n\tR_GtoI.block(0, 2, 3, 1) = z_axis;\n}\n static Eigen::Matrix computeDongsiCoeff(Eigen::MatrixXd &D, const Eigen::MatrixXd &d, double gravity_mag) {\n\tassert(D.rows() == 3);\n\tassert(D.cols() == 3);\n\tassert(d.rows() == 3);\n\tdouble D1_1 = D(0, 0), D1_2 = D(0, 1), D1_3 = D(0, 2);\n\tdouble D2_1 = D(1, 0), D2_2 = D(1, 1), D2_3 = D(1, 2);\n\tdouble D3_1 = D(2, 0), D3_2 = D(2, 1), D3_3 = D(2, 2);\n\tdouble d1 = d(0, 0), d2 = d(1, 0), d3 = d(2, 0);\n\tdouble g = gravity_mag;\n\n\tdouble D1_1_sq = D1_1 * D1_1, D1_2_sq = D1_2 * D1_2, D1_3_sq = D1_3 * D1_3;\n\tdouble D2_1_sq = D2_1 * D2_1, D2_2_sq = D2_2 * D2_2, D2_3_sq = D2_3 * D2_3;\n\tdouble D3_1_sq = D3_1 * D3_1, D3_2_sq = D3_2 * D3_2, D3_3_sq = D3_3 * D3_3;\n\tdouble d1_sq = d1 * d1, d2_sq = d2 * d2, d3_sq = d3 * d3;\n\tdouble g_sq = g * g;\n\n\tEigen::Matrix coeff = Eigen::Matrix::Zero();\n\n\tcoeff(6) = -(-D1_1_sq * D2_2_sq * D3_3_sq * g_sq + D1_1_sq * D2_2_sq * d3_sq + 2 * D1_1_sq * D2_2 * D2_3 * D3_2 * D3_3 * g_sq -\n\t\t\t\tD1_1_sq * D2_2 * D2_3 * d2 * d3 - D1_1_sq * D2_2 * D3_2 * d2 * d3 - D1_1_sq * D2_3_sq * D3_2_sq * g_sq +\n\t\t\t\tD1_1_sq * D2_3 * D3_2 * d2_sq + D1_1_sq * D2_3 * D3_2 * d3_sq - D1_1_sq * D2_3 * D3_3 * d2 * d3 -\n\t\t\t\tD1_1_sq * D3_2 * D3_3 * d2 * d3 + D1_1_sq * D3_3_sq * d2_sq + 2 * D1_1 * D1_2 * D2_1 * D2_2 * D3_3_sq * g_sq -\n\t\t\t\t2 * D1_1 * D1_2 * D2_1 * D2_2 * d3_sq - 2 * D1_1 * D1_2 * D2_1 * D2_3 * D3_2 * D3_3 * g_sq + D1_1 * D1_2 * D2_1 * D2_3 * d2 * d3 +\n\t\t\t\tD1_1 * D1_2 * D2_1 * D3_2 * d2 * d3 - 2 * D1_1 * D1_2 * D2_2 * D2_3 * D3_1 * D3_3 * g_sq + D1_1 * D1_2 * D2_2 * D2_3 * d1 * d3 +\n\t\t\t\tD1_1 * D1_2 * D2_2 * D3_1 * d2 * d3 + 2 * D1_1 * D1_2 * D2_3_sq * D3_1 * D3_2 * g_sq - D1_1 * D1_2 * D2_3 * D3_1 * d2_sq -\n\t\t\t\tD1_1 * D1_2 * D2_3 * D3_1 * d3_sq - D1_1 * D1_2 * D2_3 * D3_2 * d1 * d2 + D1_1 * D1_2 * D2_3 * D3_3 * d1 * d3 +\n\t\t\t\tD1_1 * D1_2 * D3_1 * D3_3 * d2 * d3 - D1_1 * D1_2 * D3_3_sq * d1 * d2 - 2 * D1_1 * D1_3 * D2_1 * D2_2 * D3_2 * D3_3 * g_sq +\n\t\t\t\tD1_1 * D1_3 * D2_1 * D2_2 * d2 * d3 + 2 * D1_1 * D1_3 * D2_1 * D2_3 * D3_2_sq * g_sq - D1_1 * D1_3 * D2_1 * D3_2 * d2_sq -\n\t\t\t\tD1_1 * D1_3 * D2_1 * D3_2 * d3_sq + D1_1 * D1_3 * D2_1 * D3_3 * d2 * d3 + 2 * D1_1 * D1_3 * D2_2_sq * D3_1 * D3_3 * g_sq -\n\t\t\t\tD1_1 * D1_3 * D2_2_sq * d1 * d3 - 2 * D1_1 * D1_3 * D2_2 * D2_3 * D3_1 * D3_2 * g_sq + D1_1 * D1_3 * D2_2 * D3_2 * d1 * d2 +\n\t\t\t\tD1_1 * D1_3 * D2_3 * D3_1 * d2 * d3 - D1_1 * D1_3 * D2_3 * D3_2 * d1 * d3 + D1_1 * D1_3 * D3_1 * D3_2 * d2 * d3 -\n\t\t\t\t2 * D1_1 * D1_3 * D3_1 * D3_3 * d2_sq + D1_1 * D1_3 * D3_2 * D3_3 * d1 * d2 + D1_1 * D2_1 * D2_2 * D3_2 * d1 * d3 -\n\t\t\t\tD1_1 * D2_1 * D2_3 * D3_2 * d1 * d2 + D1_1 * D2_1 * D3_2 * D3_3 * d1 * d3 - D1_1 * D2_1 * D3_3_sq * d1 * d2 -\n\t\t\t\tD1_1 * D2_2_sq * D3_1 * d1 * d3 + D1_1 * D2_2 * D2_3 * D3_1 * d1 * d2 - D1_1 * D2_3 * D3_1 * D3_2 * d1 * d3 +\n\t\t\t\tD1_1 * D2_3 * D3_1 * D3_3 * d1 * d2 - D1_2_sq * D2_1_sq * D3_3_sq * g_sq + D1_2_sq * D2_1_sq * d3_sq +\n\t\t\t\t2 * D1_2_sq * D2_1 * D2_3 * D3_1 * D3_3 * g_sq - D1_2_sq * D2_1 * D2_3 * d1 * d3 - D1_2_sq * D2_1 * D3_1 * d2 * d3 -\n\t\t\t\tD1_2_sq * D2_3_sq * D3_1_sq * g_sq + D1_2_sq * D2_3 * D3_1 * d1 * d2 + 2 * D1_2 * D1_3 * D2_1_sq * D3_2 * D3_3 * g_sq -\n\t\t\t\tD1_2 * D1_3 * D2_1_sq * d2 * d3 - 2 * D1_2 * D1_3 * D2_1 * D2_2 * D3_1 * D3_3 * g_sq + D1_2 * D1_3 * D2_1 * D2_2 * d1 * d3 -\n\t\t\t\t2 * D1_2 * D1_3 * D2_1 * D2_3 * D3_1 * D3_2 * g_sq + D1_2 * D1_3 * D2_1 * D3_1 * d2_sq + D1_2 * D1_3 * D2_1 * D3_1 * d3_sq -\n\t\t\t\tD1_2 * D1_3 * D2_1 * D3_3 * d1 * d3 + 2 * D1_2 * D1_3 * D2_2 * D2_3 * D3_1_sq * g_sq - D1_2 * D1_3 * D2_2 * D3_1 * d1 * d2 -\n\t\t\t\tD1_2 * D1_3 * D3_1_sq * d2 * d3 + D1_2 * D1_3 * D3_1 * D3_3 * d1 * d2 - D1_2 * D2_1_sq * D3_2 * d1 * d3 +\n\t\t\t\tD1_2 * D2_1 * D2_2 * D3_1 * d1 * d3 + D1_2 * D2_1 * D2_3 * D3_2 * d1_sq + D1_2 * D2_1 * D2_3 * D3_2 * d3_sq -\n\t\t\t\tD1_2 * D2_1 * D2_3 * D3_3 * d2 * d3 - D1_2 * D2_1 * D3_1 * D3_3 * d1 * d3 - D1_2 * D2_1 * D3_2 * D3_3 * d2 * d3 +\n\t\t\t\tD1_2 * D2_1 * D3_3_sq * d1_sq + D1_2 * D2_1 * D3_3_sq * d2_sq - D1_2 * D2_2 * D2_3 * D3_1 * d1_sq -\n\t\t\t\tD1_2 * D2_2 * D2_3 * D3_1 * d3_sq + D1_2 * D2_2 * D2_3 * D3_3 * d1 * d3 + D1_2 * D2_2 * D3_1 * D3_3 * d2 * d3 -\n\t\t\t\tD1_2 * D2_2 * D3_3_sq * d1 * d2 + D1_2 * D2_3_sq * D3_1 * d2 * d3 - D1_2 * D2_3_sq * D3_2 * d1 * d3 +\n\t\t\t\tD1_2 * D2_3 * D3_1_sq * d1 * d3 - D1_2 * D2_3 * D3_1 * D3_3 * d1_sq - D1_2 * D2_3 * D3_1 * D3_3 * d2_sq +\n\t\t\t\tD1_2 * D2_3 * D3_2 * D3_3 * d1 * d2 - D1_3_sq * D2_1_sq * D3_2_sq * g_sq + 2 * D1_3_sq * D2_1 * D2_2 * D3_1 * D3_2 * g_sq -\n\t\t\t\tD1_3_sq * D2_1 * D3_1 * d2 * d3 + D1_3_sq * D2_1 * D3_2 * d1 * d3 - D1_3_sq * D2_2_sq * D3_1_sq * g_sq +\n\t\t\t\tD1_3_sq * D3_1_sq * d2_sq - D1_3_sq * D3_1 * D3_2 * d1 * d2 + D1_3 * D2_1_sq * D3_2 * d1 * d2 -\n\t\t\t\tD1_3 * D2_1 * D2_2 * D3_1 * d1 * d2 - D1_3 * D2_1 * D2_2 * D3_2 * d1_sq - D1_3 * D2_1 * D2_2 * D3_2 * d3_sq +\n\t\t\t\tD1_3 * D2_1 * D2_2 * D3_3 * d2 * d3 + D1_3 * D2_1 * D3_1 * D3_3 * d1 * d2 + D1_3 * D2_1 * D3_2_sq * d2 * d3 -\n\t\t\t\tD1_3 * D2_1 * D3_2 * D3_3 * d1_sq - D1_3 * D2_1 * D3_2 * D3_3 * d2_sq + D1_3 * D2_2_sq * D3_1 * d1_sq +\n\t\t\t\tD1_3 * D2_2_sq * D3_1 * d3_sq - D1_3 * D2_2_sq * D3_3 * d1 * d3 - D1_3 * D2_2 * D2_3 * D3_1 * d2 * d3 +\n\t\t\t\tD1_3 * D2_2 * D2_3 * D3_2 * d1 * d3 - D1_3 * D2_2 * D3_1 * D3_2 * d2 * d3 + D1_3 * D2_2 * D3_2 * D3_3 * d1 * d2 -\n\t\t\t\tD1_3 * D2_3 * D3_1_sq * d1 * d2 + D1_3 * D2_3 * D3_1 * D3_2 * d1_sq + D1_3 * D2_3 * D3_1 * D3_2 * d2_sq -\n\t\t\t\tD1_3 * D2_3 * D3_2_sq * d1 * d2 + D2_1 * D2_2 * D3_2 * D3_3 * d1 * d3 - D2_1 * D2_2 * D3_3_sq * d1 * d2 -\n\t\t\t\tD2_1 * D2_3 * D3_2_sq * d1 * d3 + D2_1 * D2_3 * D3_2 * D3_3 * d1 * d2 - D2_2_sq * D3_1 * D3_3 * d1 * d3 +\n\t\t\t\tD2_2_sq * D3_3_sq * d1_sq + D2_2 * D2_3 * D3_1 * D3_2 * d1 * d3 + D2_2 * D2_3 * D3_1 * D3_3 * d1 * d2 -\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * D3_3 * d1_sq - D2_3_sq * D3_1 * D3_2 * d1 * d2 + D2_3_sq * D3_2_sq * d1_sq) /\n\t\t\t\tg_sq;\n\n\tcoeff(5) = (-(2 * D1_1_sq * D2_2_sq * D3_3 * g_sq - 2 * D1_1_sq * D2_2 * D2_3 * D3_2 * g_sq + 2 * D1_1_sq * D2_2 * D3_3_sq * g_sq -\n\t\t\t\t2 * D1_1_sq * D2_2 * d3_sq - 2 * D1_1_sq * D2_3 * D3_2 * D3_3 * g_sq + 2 * D1_1_sq * D2_3 * d2 * d3 +\n\t\t\t\t2 * D1_1_sq * D3_2 * d2 * d3 - 2 * D1_1_sq * D3_3 * d2_sq - 4 * D1_1 * D1_2 * D2_1 * D2_2 * D3_3 * g_sq +\n\t\t\t\t2 * D1_1 * D1_2 * D2_1 * D2_3 * D3_2 * g_sq - 2 * D1_1 * D1_2 * D2_1 * D3_3_sq * g_sq + 2 * D1_1 * D1_2 * D2_1 * d3_sq +\n\t\t\t\t2 * D1_1 * D1_2 * D2_2 * D2_3 * D3_1 * g_sq + 2 * D1_1 * D1_2 * D2_3 * D3_1 * D3_3 * g_sq - 2 * D1_1 * D1_2 * D2_3 * d1 * d3 -\n\t\t\t\t2 * D1_1 * D1_2 * D3_1 * d2 * d3 + 2 * D1_1 * D1_2 * D3_3 * d1 * d2 + 2 * D1_1 * D1_3 * D2_1 * D2_2 * D3_2 * g_sq +\n\t\t\t\t2 * D1_1 * D1_3 * D2_1 * D3_2 * D3_3 * g_sq - 2 * D1_1 * D1_3 * D2_1 * d2 * d3 - 2 * D1_1 * D1_3 * D2_2_sq * D3_1 * g_sq -\n\t\t\t\t4 * D1_1 * D1_3 * D2_2 * D3_1 * D3_3 * g_sq + 2 * D1_1 * D1_3 * D2_2 * d1 * d3 + 2 * D1_1 * D1_3 * D2_3 * D3_1 * D3_2 * g_sq +\n\t\t\t\t2 * D1_1 * D1_3 * D3_1 * d2_sq - 2 * D1_1 * D1_3 * D3_2 * d1 * d2 - 2 * D1_1 * D2_1 * D3_2 * d1 * d3 +\n\t\t\t\t2 * D1_1 * D2_1 * D3_3 * d1 * d2 + 2 * D1_1 * D2_2_sq * D3_3_sq * g_sq - 2 * D1_1 * D2_2_sq * d3_sq -\n\t\t\t\t4 * D1_1 * D2_2 * D2_3 * D3_2 * D3_3 * g_sq + 2 * D1_1 * D2_2 * D2_3 * d2 * d3 + 2 * D1_1 * D2_2 * D3_1 * d1 * d3 +\n\t\t\t\t2 * D1_1 * D2_2 * D3_2 * d2 * d3 + 2 * D1_1 * D2_3_sq * D3_2_sq * g_sq - 2 * D1_1 * D2_3 * D3_1 * d1 * d2 -\n\t\t\t\t2 * D1_1 * D2_3 * D3_2 * d2_sq - 2 * D1_1 * D2_3 * D3_2 * d3_sq + 2 * D1_1 * D2_3 * D3_3 * d2 * d3 +\n\t\t\t\t2 * D1_1 * D3_2 * D3_3 * d2 * d3 - 2 * D1_1 * D3_3_sq * d2_sq + 2 * D1_2_sq * D2_1_sq * D3_3 * g_sq -\n\t\t\t\t2 * D1_2_sq * D2_1 * D2_3 * D3_1 * g_sq - 2 * D1_2 * D1_3 * D2_1_sq * D3_2 * g_sq + 2 * D1_2 * D1_3 * D2_1 * D2_2 * D3_1 * g_sq +\n\t\t\t\t2 * D1_2 * D1_3 * D2_1 * D3_1 * D3_3 * g_sq - 2 * D1_2 * D1_3 * D2_3 * D3_1_sq * g_sq - 2 * D1_2 * D2_1 * D2_2 * D3_3_sq * g_sq +\n\t\t\t\t2 * D1_2 * D2_1 * D2_2 * d3_sq + 2 * D1_2 * D2_1 * D2_3 * D3_2 * D3_3 * g_sq - 2 * D1_2 * D2_1 * D3_3 * d1_sq -\n\t\t\t\t2 * D1_2 * D2_1 * D3_3 * d2_sq + 2 * D1_2 * D2_2 * D2_3 * D3_1 * D3_3 * g_sq - 2 * D1_2 * D2_2 * D2_3 * d1 * d3 -\n\t\t\t\t2 * D1_2 * D2_2 * D3_1 * d2 * d3 + 2 * D1_2 * D2_2 * D3_3 * d1 * d2 - 2 * D1_2 * D2_3_sq * D3_1 * D3_2 * g_sq +\n\t\t\t\t2 * D1_2 * D2_3 * D3_1 * d1_sq + 2 * D1_2 * D2_3 * D3_1 * d2_sq + 2 * D1_2 * D2_3 * D3_1 * d3_sq -\n\t\t\t\t2 * D1_2 * D2_3 * D3_3 * d1 * d3 - 2 * D1_2 * D3_1 * D3_3 * d2 * d3 + 2 * D1_2 * D3_3_sq * d1 * d2 -\n\t\t\t\t2 * D1_3_sq * D2_1 * D3_1 * D3_2 * g_sq + 2 * D1_3_sq * D2_2 * D3_1_sq * g_sq + 2 * D1_3 * D2_1 * D2_2 * D3_2 * D3_3 * g_sq -\n\t\t\t\t2 * D1_3 * D2_1 * D2_2 * d2 * d3 - 2 * D1_3 * D2_1 * D2_3 * D3_2_sq * g_sq + 2 * D1_3 * D2_1 * D3_2 * d1_sq +\n\t\t\t\t2 * D1_3 * D2_1 * D3_2 * d2_sq + 2 * D1_3 * D2_1 * D3_2 * d3_sq - 2 * D1_3 * D2_1 * D3_3 * d2 * d3 -\n\t\t\t\t2 * D1_3 * D2_2_sq * D3_1 * D3_3 * g_sq + 2 * D1_3 * D2_2_sq * d1 * d3 + 2 * D1_3 * D2_2 * D2_3 * D3_1 * D3_2 * g_sq -\n\t\t\t\t2 * D1_3 * D2_2 * D3_1 * d1_sq - 2 * D1_3 * D2_2 * D3_1 * d3_sq - 2 * D1_3 * D2_2 * D3_2 * d1 * d2 +\n\t\t\t\t2 * D1_3 * D2_2 * D3_3 * d1 * d3 + 2 * D1_3 * D3_1 * D3_3 * d2_sq - 2 * D1_3 * D3_2 * D3_3 * d1 * d2 -\n\t\t\t\t2 * D2_1 * D2_2 * D3_2 * d1 * d3 + 2 * D2_1 * D2_2 * D3_3 * d1 * d2 - 2 * D2_1 * D3_2 * D3_3 * d1 * d3 +\n\t\t\t\t2 * D2_1 * D3_3_sq * d1 * d2 + 2 * D2_2_sq * D3_1 * d1 * d3 - 2 * D2_2_sq * D3_3 * d1_sq - 2 * D2_2 * D2_3 * D3_1 * d1 * d2 +\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * d1_sq + 2 * D2_2 * D3_1 * D3_3 * d1 * d3 - 2 * D2_2 * D3_3_sq * d1_sq -\n\t\t\t\t2 * D2_3 * D3_1 * D3_3 * d1 * d2 + 2 * D2_3 * D3_2 * D3_3 * d1_sq) /\n\t\t\t\tg_sq);\n\n\tcoeff(4) = ((D1_1_sq * D2_2_sq * g_sq + 4 * D1_1_sq * D2_2 * D3_3 * g_sq - 2 * D1_1_sq * D2_3 * D3_2 * g_sq + D1_1_sq * D3_3_sq * g_sq -\n\t\t\t\tD1_1_sq * d2_sq - D1_1_sq * d3_sq - 2 * D1_1 * D1_2 * D2_1 * D2_2 * g_sq - 4 * D1_1 * D1_2 * D2_1 * D3_3 * g_sq +\n\t\t\t\t2 * D1_1 * D1_2 * D2_3 * D3_1 * g_sq + D1_1 * D1_2 * d1 * d2 + 2 * D1_1 * D1_3 * D2_1 * D3_2 * g_sq -\n\t\t\t\t4 * D1_1 * D1_3 * D2_2 * D3_1 * g_sq - 2 * D1_1 * D1_3 * D3_1 * D3_3 * g_sq + D1_1 * D1_3 * d1 * d3 + D1_1 * D2_1 * d1 * d2 +\n\t\t\t\t4 * D1_1 * D2_2_sq * D3_3 * g_sq - 4 * D1_1 * D2_2 * D2_3 * D3_2 * g_sq + 4 * D1_1 * D2_2 * D3_3_sq * g_sq -\n\t\t\t\t4 * D1_1 * D2_2 * d3_sq - 4 * D1_1 * D2_3 * D3_2 * D3_3 * g_sq + 4 * D1_1 * D2_3 * d2 * d3 + D1_1 * D3_1 * d1 * d3 +\n\t\t\t\t4 * D1_1 * D3_2 * d2 * d3 - 4 * D1_1 * D3_3 * d2_sq + D1_2_sq * D2_1_sq * g_sq + 2 * D1_2 * D1_3 * D2_1 * D3_1 * g_sq -\n\t\t\t\t4 * D1_2 * D2_1 * D2_2 * D3_3 * g_sq + 2 * D1_2 * D2_1 * D2_3 * D3_2 * g_sq - 2 * D1_2 * D2_1 * D3_3_sq * g_sq -\n\t\t\t\tD1_2 * D2_1 * d1_sq - D1_2 * D2_1 * d2_sq + 2 * D1_2 * D2_1 * d3_sq + 2 * D1_2 * D2_2 * D2_3 * D3_1 * g_sq +\n\t\t\t\tD1_2 * D2_2 * d1 * d2 + 2 * D1_2 * D2_3 * D3_1 * D3_3 * g_sq - 3 * D1_2 * D2_3 * d1 * d3 - 3 * D1_2 * D3_1 * d2 * d3 +\n\t\t\t\t4 * D1_2 * D3_3 * d1 * d2 + D1_3_sq * D3_1_sq * g_sq + 2 * D1_3 * D2_1 * D2_2 * D3_2 * g_sq +\n\t\t\t\t2 * D1_3 * D2_1 * D3_2 * D3_3 * g_sq - 3 * D1_3 * D2_1 * d2 * d3 - 2 * D1_3 * D2_2_sq * D3_1 * g_sq -\n\t\t\t\t4 * D1_3 * D2_2 * D3_1 * D3_3 * g_sq + 4 * D1_3 * D2_2 * d1 * d3 + 2 * D1_3 * D2_3 * D3_1 * D3_2 * g_sq - D1_3 * D3_1 * d1_sq +\n\t\t\t\t2 * D1_3 * D3_1 * d2_sq - D1_3 * D3_1 * d3_sq - 3 * D1_3 * D3_2 * d1 * d2 + D1_3 * D3_3 * d1 * d3 + D2_1 * D2_2 * d1 * d2 -\n\t\t\t\t3 * D2_1 * D3_2 * d1 * d3 + 4 * D2_1 * D3_3 * d1 * d2 + D2_2_sq * D3_3_sq * g_sq - D2_2_sq * d1_sq - D2_2_sq * d3_sq -\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * D3_3 * g_sq + D2_2 * D2_3 * d2 * d3 + 4 * D2_2 * D3_1 * d1 * d3 + D2_2 * D3_2 * d2 * d3 -\n\t\t\t\t4 * D2_2 * D3_3 * d1_sq + D2_3_sq * D3_2_sq * g_sq - 3 * D2_3 * D3_1 * d1 * d2 + 2 * D2_3 * D3_2 * d1_sq - D2_3 * D3_2 * d2_sq -\n\t\t\t\tD2_3 * D3_2 * d3_sq + D2_3 * D3_3 * d2 * d3 + D3_1 * D3_3 * d1 * d3 + D3_2 * D3_3 * d2 * d3 - D3_3_sq * d1_sq - D3_3_sq * d2_sq) /\n\t\t\t\tg_sq);\n \n\tcoeff(3) = ((2 * D1_1 * d2_sq + 2 * D1_1 * d3_sq + 2 * D2_2 * d1_sq + 2 * D2_2 * d3_sq + 2 * D3_3 * d1_sq + 2 * D3_3 * d2_sq -\n\t\t\t\t2 * D1_1 * D2_2_sq * g_sq - 2 * D1_1_sq * D2_2 * g_sq - 2 * D1_1 * D3_3_sq * g_sq - 2 * D1_1_sq * D3_3 * g_sq -\n\t\t\t\t2 * D2_2 * D3_3_sq * g_sq - 2 * D2_2_sq * D3_3 * g_sq - 2 * D1_2 * d1 * d2 - 2 * D1_3 * d1 * d3 - 2 * D2_1 * d1 * d2 -\n\t\t\t\t2 * D2_3 * d2 * d3 - 2 * D3_1 * d1 * d3 - 2 * D3_2 * d2 * d3 + 2 * D1_1 * D1_2 * D2_1 * g_sq + 2 * D1_1 * D1_3 * D3_1 * g_sq +\n\t\t\t\t2 * D1_2 * D2_1 * D2_2 * g_sq - 8 * D1_1 * D2_2 * D3_3 * g_sq + 4 * D1_1 * D2_3 * D3_2 * g_sq + 4 * D1_2 * D2_1 * D3_3 * g_sq -\n\t\t\t\t2 * D1_2 * D2_3 * D3_1 * g_sq - 2 * D1_3 * D2_1 * D3_2 * g_sq + 4 * D1_3 * D2_2 * D3_1 * g_sq + 2 * D1_3 * D3_1 * D3_3 * g_sq +\n\t\t\t\t2 * D2_2 * D2_3 * D3_2 * g_sq + 2 * D2_3 * D3_2 * D3_3 * g_sq) /\n\t\t\t\tg_sq);\n \n\tcoeff(2) = (-(d1_sq + d2_sq + d3_sq - D1_1_sq * g_sq - D2_2_sq * g_sq - D3_3_sq * g_sq - 4 * D1_1 * D2_2 * g_sq + 2 * D1_2 * D2_1 * g_sq -\n\t\t\t\t4 * D1_1 * D3_3 * g_sq + 2 * D1_3 * D3_1 * g_sq - 4 * D2_2 * D3_3 * g_sq + 2 * D2_3 * D3_2 * g_sq) /\n\t\t\t\tg_sq);\n \n\tcoeff(1) = (-(2 * D1_1 * g_sq + 2 * D2_2 * g_sq + 2 * D3_3 * g_sq) / g_sq);\n\t\n\tcoeff(0) = 1;\n\n\treturn coeff;\n}\n};"], ["/voxel_svio/include/camera.h", "class cameraBase {\n public:\n cameraBase(int width, int height) {\n\n}\n virtual void setValue(const Eigen::MatrixXd &calib) {\n\tassert(calib.rows() == 8);\n\tcamera_values = calib;\n\n\tcv::Matx33d tempK;\n\ttempK(0, 0) = calib(0);\n\ttempK(0, 1) = 0;\n\ttempK(0, 2) = calib(2);\n\ttempK(1, 0) = 0;\n\ttempK(1, 1) = calib(1);\n\ttempK(1, 2) = calib(3);\n\ttempK(2, 0) = 0;\n\ttempK(2, 1) = 0;\n\ttempK(2, 2) = 1;\n\tcamera_k_OPENCV = tempK;\n\n\tcv::Vec4d tempD;\n\ttempD(0) = calib(4);\n\ttempD(1) = calib(5);\n\ttempD(2) = calib(6);\n\ttempD(3) = calib(7);\n\tcamera_d_OPENCV = tempD;\n}\n virtual Eigen::Vector2f undistortF(const Eigen::Vector2f &uv_dist) = 0 {\n cv::Matx33d camK = camera_k_OPENCV;\n cv::Vec4d camD = camera_d_OPENCV;\n\n cv::Mat mat(1, 2, CV_32F);\n mat.at(0, 0) = uv_dist(0);\n mat.at(0, 1) = uv_dist(1);\n mat = mat.reshape(2);\n\n cv::undistortPoints(mat, mat, camK, camD);\n\n Eigen::Vector2f pt_out;\n mat = mat.reshape(1);\n pt_out(0) = mat.at(0, 0);\n pt_out(1) = mat.at(0, 1);\n\n return pt_out;\n}\n Eigen::Vector2d undistortD(const Eigen::Vector2d &uv_dist) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 = uv_dist.cast();\n\tept_2 = undistortF(ept_1);\n\n\treturn ept_2.cast();\n}\n cv::Point2f undistortCV(const cv::Point2f &uv_dist) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 << uv_dist.x, uv_dist.y;\n\tept_2 = undistortF(ept_1);\n\tcv::Point2f pt_out;\n\tpt_out.x = ept_2(0);\n\tpt_out.y = ept_2(1);\n\n\treturn pt_out;\n}\n virtual Eigen::Vector2f distortF(const Eigen::Vector2f &uv_norm) = 0 {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tEigen::Vector2f uv_dist;\n\tuv_dist(0) = (float)(cam_d(0) * x1 + cam_d(2));\n\tuv_dist(1) = (float)(cam_d(1) * y1 + cam_d(3));\n\n\treturn uv_dist;\n}\n Eigen::Vector2d distortD(const Eigen::Vector2d &uv_norm) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 = uv_norm.cast();\n\tept_2 = distortF(ept_1);\n\n\treturn ept_2.cast();\n}\n cv::Point2f distortCV(const cv::Point2f &uv_norm) {\n\tEigen::Vector2f ept_1, ept_2;\n\tept_1 << uv_norm.x, uv_norm.y;\n\tept_2 = distortF(ept_1);\n\tcv::Point2f pt_out;\n\tpt_out.x = ept_2(0);\n\tpt_out.y = ept_2(1);\n\n\treturn pt_out;\n}\n virtual void computeDistortJacobian(const Eigen::Vector2d &uv_norm, Eigen::MatrixXd &H_dz_dzn, Eigen::MatrixXd &H_dz_dzeta) = 0 {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\n\tH_dz_dzn = Eigen::MatrixXd::Zero(2, 2);\n\tdouble x = uv_norm(0);\n\tdouble y = uv_norm(1);\n\tdouble x_2 = uv_norm(0) * uv_norm(0);\n\tdouble y_2 = uv_norm(1) * uv_norm(1);\n\tdouble x_y = uv_norm(0) * uv_norm(1);\n\tH_dz_dzn(0, 0) = cam_d(0) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * x_2 + 4 * cam_d(5) * x_2 * r_2) +\n\t 2 * cam_d(6) * y + (2 * cam_d(7) * x + 4 * cam_d(7) * x));\n\tH_dz_dzn(0, 1) = cam_d(0) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 0) = cam_d(1) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 1) = cam_d(1) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * y_2 + 4 * cam_d(5) * y_2 * r_2) +\n\t 2 * cam_d(7) * x + (2 * cam_d(6) * y + 4 * cam_d(6) * y));\n\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tH_dz_dzeta = Eigen::MatrixXd::Zero(2, 8);\n\tH_dz_dzeta(0, 0) = x1;\n\tH_dz_dzeta(0, 2) = 1;\n\tH_dz_dzeta(0, 4) = cam_d(0) * uv_norm(0) * r_2;\n\tH_dz_dzeta(0, 5) = cam_d(0) * uv_norm(0) * r_4;\n\tH_dz_dzeta(0, 6) = 2 * cam_d(0) * uv_norm(0) * uv_norm(1);\n\tH_dz_dzeta(0, 7) = cam_d(0) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tH_dz_dzeta(1, 1) = y1;\n\tH_dz_dzeta(1, 3) = 1;\n\tH_dz_dzeta(1, 4) = cam_d(1) * uv_norm(1) * r_2;\n\tH_dz_dzeta(1, 5) = cam_d(1) * uv_norm(1) * r_4;\n\tH_dz_dzeta(1, 6) = cam_d(1) * (r_2 + 2 * uv_norm(1) * uv_norm(1));\n\tH_dz_dzeta(1, 7) = 2 * cam_d(1) * uv_norm(0) * uv_norm(1);\n}\n Eigen::MatrixXd getValue() {\n\treturn camera_values;\n}\n cv::Matx33d getK() {\n\treturn camera_k_OPENCV;\n}\n cv::Vec4d getD() {\n\treturn camera_d_OPENCV;\n}\n int w() {\n\treturn width;\n}\n int h() {\n\treturn height;\n}\n protected:\n Eigen::MatrixXd camera_values;\n cv::Matx33d camera_k_OPENCV;\n cv::Vec4d camera_d_OPENCV;\n int width;\n int height;\n public:\n Eigen::Vector2f undistortF(const Eigen::Vector2f &uv_dist) override {\n cv::Matx33d camK = camera_k_OPENCV;\n cv::Vec4d camD = camera_d_OPENCV;\n\n cv::Mat mat(1, 2, CV_32F);\n mat.at(0, 0) = uv_dist(0);\n mat.at(0, 1) = uv_dist(1);\n mat = mat.reshape(2);\n\n cv::undistortPoints(mat, mat, camK, camD);\n\n Eigen::Vector2f pt_out;\n mat = mat.reshape(1);\n pt_out(0) = mat.at(0, 0);\n pt_out(1) = mat.at(0, 1);\n\n return pt_out;\n}\n Eigen::Vector2f distortF(const Eigen::Vector2f &uv_norm) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tEigen::Vector2f uv_dist;\n\tuv_dist(0) = (float)(cam_d(0) * x1 + cam_d(2));\n\tuv_dist(1) = (float)(cam_d(1) * y1 + cam_d(3));\n\n\treturn uv_dist;\n}\n void computeDistortJacobian(const Eigen::Vector2d &uv_norm, Eigen::MatrixXd &H_dz_dzn, Eigen::MatrixXd &H_dz_dzeta) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\n\tH_dz_dzn = Eigen::MatrixXd::Zero(2, 2);\n\tdouble x = uv_norm(0);\n\tdouble y = uv_norm(1);\n\tdouble x_2 = uv_norm(0) * uv_norm(0);\n\tdouble y_2 = uv_norm(1) * uv_norm(1);\n\tdouble x_y = uv_norm(0) * uv_norm(1);\n\tH_dz_dzn(0, 0) = cam_d(0) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * x_2 + 4 * cam_d(5) * x_2 * r_2) +\n\t 2 * cam_d(6) * y + (2 * cam_d(7) * x + 4 * cam_d(7) * x));\n\tH_dz_dzn(0, 1) = cam_d(0) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 0) = cam_d(1) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 1) = cam_d(1) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * y_2 + 4 * cam_d(5) * y_2 * r_2) +\n\t 2 * cam_d(7) * x + (2 * cam_d(6) * y + 4 * cam_d(6) * y));\n\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tH_dz_dzeta = Eigen::MatrixXd::Zero(2, 8);\n\tH_dz_dzeta(0, 0) = x1;\n\tH_dz_dzeta(0, 2) = 1;\n\tH_dz_dzeta(0, 4) = cam_d(0) * uv_norm(0) * r_2;\n\tH_dz_dzeta(0, 5) = cam_d(0) * uv_norm(0) * r_4;\n\tH_dz_dzeta(0, 6) = 2 * cam_d(0) * uv_norm(0) * uv_norm(1);\n\tH_dz_dzeta(0, 7) = cam_d(0) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tH_dz_dzeta(1, 1) = y1;\n\tH_dz_dzeta(1, 3) = 1;\n\tH_dz_dzeta(1, 4) = cam_d(1) * uv_norm(1) * r_2;\n\tH_dz_dzeta(1, 5) = cam_d(1) * uv_norm(1) * r_4;\n\tH_dz_dzeta(1, 6) = cam_d(1) * (r_2 + 2 * uv_norm(1) * uv_norm(1));\n\tH_dz_dzeta(1, 7) = 2 * cam_d(1) * uv_norm(0) * uv_norm(1);\n}\n public:\n Eigen::Vector2f undistortF(const Eigen::Vector2f &uv_dist) override {\n cv::Matx33d camK = camera_k_OPENCV;\n cv::Vec4d camD = camera_d_OPENCV;\n\n cv::Mat mat(1, 2, CV_32F);\n mat.at(0, 0) = uv_dist(0);\n mat.at(0, 1) = uv_dist(1);\n mat = mat.reshape(2);\n\n cv::undistortPoints(mat, mat, camK, camD);\n\n Eigen::Vector2f pt_out;\n mat = mat.reshape(1);\n pt_out(0) = mat.at(0, 0);\n pt_out(1) = mat.at(0, 1);\n\n return pt_out;\n}\n Eigen::Vector2f distortF(const Eigen::Vector2f &uv_norm) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tEigen::Vector2f uv_dist;\n\tuv_dist(0) = (float)(cam_d(0) * x1 + cam_d(2));\n\tuv_dist(1) = (float)(cam_d(1) * y1 + cam_d(3));\n\n\treturn uv_dist;\n}\n void computeDistortJacobian(const Eigen::Vector2d &uv_norm, Eigen::MatrixXd &H_dz_dzn, Eigen::MatrixXd &H_dz_dzeta) override {\n\tEigen::MatrixXd cam_d = camera_values;\n\n\tdouble r = std::sqrt(uv_norm(0) * uv_norm(0) + uv_norm(1) * uv_norm(1));\n\tdouble r_2 = r * r;\n\tdouble r_4 = r_2 * r_2;\n\n\tH_dz_dzn = Eigen::MatrixXd::Zero(2, 2);\n\tdouble x = uv_norm(0);\n\tdouble y = uv_norm(1);\n\tdouble x_2 = uv_norm(0) * uv_norm(0);\n\tdouble y_2 = uv_norm(1) * uv_norm(1);\n\tdouble x_y = uv_norm(0) * uv_norm(1);\n\tH_dz_dzn(0, 0) = cam_d(0) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * x_2 + 4 * cam_d(5) * x_2 * r_2) +\n\t 2 * cam_d(6) * y + (2 * cam_d(7) * x + 4 * cam_d(7) * x));\n\tH_dz_dzn(0, 1) = cam_d(0) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 0) = cam_d(1) * (2 * cam_d(4) * x_y + 4 * cam_d(5) * x_y * r_2 + 2 * cam_d(6) * x + 2 * cam_d(7) * y);\n\tH_dz_dzn(1, 1) = cam_d(1) * ((1 + cam_d(4) * r_2 + cam_d(5) * r_4) + (2 * cam_d(4) * y_2 + 4 * cam_d(5) * y_2 * r_2) +\n\t 2 * cam_d(7) * x + (2 * cam_d(6) * y + 4 * cam_d(6) * y));\n\n\tdouble x1 = uv_norm(0) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + 2 * cam_d(6) * uv_norm(0) * uv_norm(1) +\n\t cam_d(7) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tdouble y1 = uv_norm(1) * (1 + cam_d(4) * r_2 + cam_d(5) * r_4) + cam_d(6) * (r_2 + 2 * uv_norm(1) * uv_norm(1)) +\n\t 2 * cam_d(7) * uv_norm(0) * uv_norm(1);\n\n\tH_dz_dzeta = Eigen::MatrixXd::Zero(2, 8);\n\tH_dz_dzeta(0, 0) = x1;\n\tH_dz_dzeta(0, 2) = 1;\n\tH_dz_dzeta(0, 4) = cam_d(0) * uv_norm(0) * r_2;\n\tH_dz_dzeta(0, 5) = cam_d(0) * uv_norm(0) * r_4;\n\tH_dz_dzeta(0, 6) = 2 * cam_d(0) * uv_norm(0) * uv_norm(1);\n\tH_dz_dzeta(0, 7) = cam_d(0) * (r_2 + 2 * uv_norm(0) * uv_norm(0));\n\tH_dz_dzeta(1, 1) = y1;\n\tH_dz_dzeta(1, 3) = 1;\n\tH_dz_dzeta(1, 4) = cam_d(1) * uv_norm(1) * r_2;\n\tH_dz_dzeta(1, 5) = cam_d(1) * uv_norm(1) * r_4;\n\tH_dz_dzeta(1, 6) = cam_d(1) * (r_2 + 2 * uv_norm(1) * uv_norm(1));\n\tH_dz_dzeta(1, 7) = 2 * cam_d(1) * uv_norm(0) * uv_norm(1);\n}\n};"], ["/voxel_svio/include/quatOps.h", "class quatType {\n public:\n static Eigen::Matrix rotToQuat(const Eigen::Matrix &rot) {\n\tEigen::Matrix q;\n\tdouble T = rot.trace();\n\n\tif ((rot(0, 0) >= T) && (rot(0, 0) >= rot(1, 1)) && (rot(0, 0) >= rot(2, 2)))\n\t{\n\t\tq(0) = sqrt((1 + (2 * rot(0, 0)) - T) / 4);\n\t\tq(1) = (1 / (4 * q(0))) * (rot(0, 1) + rot(1, 0));\n\t\tq(2) = (1 / (4 * q(0))) * (rot(0, 2) + rot(2, 0));\n\t\tq(3) = (1 / (4 * q(0))) * (rot(1, 2) - rot(2, 1));\n\t} \n\telse if ((rot(1, 1) >= T) && (rot(1, 1) >= rot(0, 0)) && (rot(1, 1) >= rot(2, 2)))\n\t{\n\t\tq(1) = sqrt((1 + (2 * rot(1, 1)) - T) / 4);\n\t\tq(0) = (1 / (4 * q(1))) * (rot(0, 1) + rot(1, 0));\n\t\tq(2) = (1 / (4 * q(1))) * (rot(1, 2) + rot(2, 1));\n\t\tq(3) = (1 / (4 * q(1))) * (rot(2, 0) - rot(0, 2));\n\t}\n\telse if ((rot(2, 2) >= T) && (rot(2, 2) >= rot(0, 0)) && (rot(2, 2) >= rot(1, 1)))\n\t{\n\t\tq(2) = sqrt((1 + (2 * rot(2, 2)) - T) / 4);\n\t\tq(0) = (1 / (4 * q(2))) * (rot(0, 2) + rot(2, 0));\n\t\tq(1) = (1 / (4 * q(2))) * (rot(1, 2) + rot(2, 1));\n\t\tq(3) = (1 / (4 * q(2))) * (rot(0, 1) - rot(1, 0));\n\t} \n\telse\n\t{\n\t\tq(3) = sqrt((1 + T) / 4);\n\t\tq(0) = (1 / (4 * q(3))) * (rot(1, 2) - rot(2, 1));\n\t\tq(1) = (1 / (4 * q(3))) * (rot(2, 0) - rot(0, 2));\n\t\tq(2) = (1 / (4 * q(3))) * (rot(0, 1) - rot(1, 0));\n\t}\n\n\tif (q(3) < 0)\n\t{\n\t\tq = -q;\n\t}\n\n\tq = q / (q.norm());\n\treturn q;\n}\n static Eigen::Matrix quatToRot(const Eigen::Matrix &q) {\n\tEigen::Matrix q_x = skewSymmetric(q.block<3, 1>(0, 0));\n\tEigen::MatrixXd rot = (2 * std::pow(q(3, 0), 2) - 1) * Eigen::MatrixXd::Identity(3, 3) - 2 * q(3, 0) * q_x +\n \t 2 * q.block<3, 1>(0, 0) * (q.block<3, 1>(0, 0).transpose());\n\treturn rot;\n}\n static Eigen::Matrix skewSymmetric(const Eigen::Matrix &w) {\n\tEigen::Matrix w_x;\n\tw_x << 0, -w(2), w(1), w(2), 0, -w(0), -w(1), w(0), 0;\n\treturn w_x;\n}\n static Eigen::Matrix quatMultiply(const Eigen::Matrix &q, const Eigen::Matrix &p) {\n\tEigen::Matrix q_t;\n\tEigen::Matrix Q_m;\n\n\tQ_m.block<3, 3>(0, 0) = q(3, 0) * Eigen::MatrixXd::Identity(3, 3) - skewSymmetric(q.block<3, 1>(0, 0));\n\tQ_m.block<3, 1>(0, 3) = q.block<3, 1>(0, 0);\n\tQ_m.block<1, 3>(3, 0) = -q.block<3, 1>(0, 0).transpose();\n\tQ_m(3, 3) = q(3, 0);\n\tq_t = Q_m * p;\n\n\tif (q_t(3, 0) < 0)\n\t{\n\t\tq_t *= -1;\n\t}\n\n\treturn q_t / q_t.norm();\n}\n static Eigen::Matrix vee(const Eigen::Matrix &w_x) {\n\tEigen::Matrix w;\n\tw << w_x(2, 1), w_x(0, 2), w_x(1, 0);\n\treturn w;\n}\n static Eigen::Matrix expSo3(const Eigen::Matrix &w) {\n\tEigen::Matrix w_x = skewSymmetric(w);\n\tdouble theta = w.norm();\n\n\tdouble A, B;\n\tif (theta < 1e-7) {\n\t\tA = 1;\n\t\tB = 0.5;\n\t}\n\telse\n\t{\n\t\tA = sin(theta) / theta;\n\t\tB = (1 - cos(theta)) / (theta * theta);\n\t}\n\n\tEigen::Matrix R;\n\tif (theta == 0) {\n\t\tR = Eigen::MatrixXd::Identity(3, 3);\n\t} else {\n\t\tR = Eigen::MatrixXd::Identity(3, 3) + A * w_x + B * w_x * w_x;\n\t}\n\treturn R;\n}\n static Eigen::Matrix logSo3(const Eigen::Matrix &R) {\n\tdouble R_11 = R(0, 0), R_12 = R(0, 1), R_13 = R(0, 2);\n\tdouble R_21 = R(1, 0), R_22 = R(1, 1), R_23 = R(1, 2);\n\tdouble R_31 = R(2, 0), R_32 = R(2, 1), R_33 = R(2, 2);\n\n\tconst double tr = R.trace();\n\tEigen::Vector3d omega;\n\n\tif (tr + 1.0 < 1e-10)\n\t{\n \tif (std::abs(R_33 + 1.0) > 1e-5)\n\t\t\tomega = (M_PI / sqrt(2.0 + 2.0 * R_33)) * Eigen::Vector3d(R_13, R_23, 1.0 + R_33);\n\t\telse if (std::abs(R_22 + 1.0) > 1e-5)\n\t\t\tomega = (M_PI / sqrt(2.0 + 2.0 * R_22)) * Eigen::Vector3d(R_12, 1.0 + R_22, R_32);\n\t\telse\n\t\t\tomega = (M_PI / sqrt(2.0 + 2.0 * R_11)) * Eigen::Vector3d(1.0 + R_11, R_21, R_31);\n\t}\n\telse\n\t{\n\t\tdouble magnitude;\n\t\tconst double tr_3 = tr - 3.0;\n\t\tif (tr_3 < -1e-7)\n\t\t{\n\t\t\tdouble theta = acos((tr - 1.0) / 2.0);\n\t\t\tmagnitude = theta / (2.0 * sin(theta));\n\t\t}\n\t\telse\n\t\t{\n\t\t\tmagnitude = 0.5 - tr_3 / 12.0;\n\t\t}\n\t\tomega = magnitude * Eigen::Vector3d(R_32 - R_23, R_13 - R_31, R_21 - R_12);\n\t}\n\treturn omega;\n}\n static Eigen::Matrix4d expSe3(Eigen::Matrix vec) {\n\tEigen::Vector3d w = vec.head(3);\n\tEigen::Vector3d u = vec.tail(3);\n\tdouble theta = sqrt(w.dot(w));\n\tEigen::Matrix3d wskew;\n\twskew << 0, -w(2), w(1), w(2), 0, -w(0), -w(1), w(0), 0;\n\n\tdouble A, B, C;\n\tif (theta < 1e-7)\n\t{\n\t\tA = 1;\n\t\tB = 0.5;\n\t\tC = 1.0 / 6.0;\n\t}\n\telse\n\t{\n\t\tA = sin(theta) / theta;\n\t\tB = (1 - cos(theta)) / (theta * theta);\n\t\tC = (1 - A) / (theta * theta);\n\t}\n\n\tEigen::Matrix3d I_33 = Eigen::Matrix3d::Identity();\n\tEigen::Matrix3d V = I_33 + B * wskew + C * wskew * wskew;\n\n\tEigen::Matrix4d mat = Eigen::Matrix4d::Zero();\n\tmat.block<3, 3>(0, 0) = I_33 + A * wskew + B * wskew * wskew;\n\tmat.block<3, 1>(0, 3) = V * u;\n\tmat(3, 3) = 1;\n\treturn mat;\n}\n static Eigen::Matrix logSe3(Eigen::Matrix4d mat) {\n\tEigen::Vector3d w = logSo3(mat.block<3, 3>(0, 0));\n\tEigen::Vector3d T = mat.block<3, 1>(0, 3);\n\tconst double t = w.norm();\n\tif (t < 1e-10)\n\t{\n\t\tEigen::Matrix log;\n\t\tlog << w, T;\n\t\treturn log;\n\t}\n\telse\n\t{\n\t\tEigen::Matrix3d W = skewSymmetric(w / t);\n \tdouble Tan = tan(0.5 * t);\n \tEigen::Vector3d WT = W * T;\n \tEigen::Vector3d u = T - (0.5 * t) * WT + (1 - t / (2. * Tan)) * (W * WT);\n \tEigen::Matrix log;\n \tlog << w, u;\n \treturn log;\n\t}\n}\n static Eigen::Matrix4d hatSe3(const Eigen::Matrix &vec) {\n\tEigen::Matrix4d mat = Eigen::Matrix4d::Zero();\n\tmat.block<3, 3>(0, 0) = skewSymmetric(vec.head(3));\n\tmat.block<3, 1>(0, 3) = vec.tail(3);\n\treturn mat;\n}\n static Eigen::Matrix4d invSe3(const Eigen::Matrix4d &T) {\n\tEigen::Matrix4d Tinv = Eigen::Matrix4d::Identity();\n\tTinv.block<3, 3>(0, 0) = T.block<3, 3>(0, 0).transpose();\n\tTinv.block<3, 1>(0, 3) = -Tinv.block<3, 3>(0, 0) * T.block<3, 1>(0, 3);\n\treturn Tinv;\n}\n static Eigen::Matrix inv(Eigen::Matrix q) {\n\tEigen::Matrix q_inv;\n\tq_inv.block<3, 1>(0, 0) = -q.block<3, 1>(0, 0);\n\tq_inv(3, 0) = q(3, 0);\n\treturn q_inv;\n}\n static Eigen::Matrix omega(Eigen::Matrix w) {\n\tEigen::Matrix mat;\n\tmat.block<3, 3>(0, 0) = -skewSymmetric(w);\n\tmat.block<1, 3>(3, 0) = -w.transpose();\n\tmat.block<3, 1>(0, 3) = w;\n\tmat(3, 3) = 0;\n\treturn mat;\n}\n static Eigen::Matrix quatNorm(Eigen::Matrix q_t) {\n\tif (q_t(3, 0) < 0)\n\t{\n\t\tq_t *= -1;\n\t}\n\n\treturn q_t / q_t.norm();\n}\n static Eigen::Matrix JleftSo3(const Eigen::Matrix &w) {\n\tdouble theta = w.norm();\n\n\tif (theta < 1e-6) {\n\t\treturn Eigen::MatrixXd::Identity(3, 3);\n\t}\n\telse\n\t{\n\t\tEigen::Matrix a = w / theta;\n\t\tEigen::Matrix J = sin(theta) / theta * Eigen::MatrixXd::Identity(3, 3) + (1 - sin(theta) / theta) * a * a.transpose() +\n\t\t\t\t\t\t\t\t\t\t((1 - cos(theta)) / theta) * skewSymmetric(a);\n\t\treturn J;\n\t}\n}\n static Eigen::Matrix JrighySo3(const Eigen::Matrix &w) {\n\treturn JleftSo3(-w);\n}\n static Eigen::Matrix rotToRpy(const Eigen::Matrix &rot) {\n\tEigen::Matrix rpy;\n\trpy(1, 0) = atan2(-rot(2, 0), sqrt(rot(0, 0) * rot(0, 0) + rot(1, 0) * rot(1, 0)));\n\tif (std::abs(cos(rpy(1, 0))) > 1.0e-12)\n\t{\n\t\trpy(2, 0) = atan2(rot(1, 0) / cos(rpy(1, 0)), rot(0, 0) / cos(rpy(1, 0)));\n\t\trpy(0, 0) = atan2(rot(2, 1) / cos(rpy(1, 0)), rot(2, 2) / cos(rpy(1, 0)));\n\t}\n\telse\n\t{\n\t\trpy(2, 0) = 0;\n\t\trpy(0, 0) = atan2(rot(0, 1), rot(1, 1));\n\t}\n\treturn rpy;\n}\n static Eigen::Matrix rotX(double t) {\n\tEigen::Matrix r;\n\tdouble ct = cos(t);\n\tdouble st = sin(t);\n\tr << 1.0, 0.0, 0.0, 0.0, ct, -st, 0.0, st, ct;\n\treturn r;\n}\n static Eigen::Matrix rotY(double t) {\n\tEigen::Matrix r;\n\tdouble ct = cos(t);\n\tdouble st = sin(t);\n\tr << ct, 0.0, st, 0.0, 1.0, 0.0, -st, 0.0, ct;\n\treturn r;\n}\n static Eigen::Matrix rotZ(double t) {\n\tEigen::Matrix r;\n\tdouble ct = cos(t);\n\tdouble st = sin(t);\n\tr << ct, -st, 0.0, st, ct, 0.0, 0.0, 0.0, 1.0;\n\treturn r;\n}\n};"], ["/voxel_svio/include/featureHelper.h", "class featureHelper {\n public:\n static void computeDisparity(std::shared_ptr db, double time_0, double time_1, double &disp_mean, double &disp_var, int &total_feats) {\n\tstd::vector disparities;\n\tfor (auto &feat : db->getInternalData())\n\t{\n\t\tfor (auto &cam_pairs : feat.second->timestamps)\n\t\t{\n\t\t\tif (cam_pairs.second.size() < 2)\n\t\t\t\tcontinue;\n\n\t\t\tsize_t cam_id = cam_pairs.first;\n\t\t\tbool found_0 = false;\n\t\t\tbool found_1 = false;\n \n\t\t\tEigen::Vector2f uv_0 = Eigen::Vector2f::Zero();\n\t\t\tEigen::Vector2f uv_1 = Eigen::Vector2f::Zero();\n\n\t\t\tfor (size_t idx = 0; idx < feat.second->timestamps.at(cam_id).size(); idx++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(idx);\n\t\t\t\tif ((oldest_time == -1 || time > oldest_time) && !found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_0 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_0 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif ((newest_time == -1 || time < newest_time) && found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_1 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_1 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!found_0 || !found_1)\n\t\t\t\tcontinue;\n \n\t\t\tdisparities.push_back((uv_1 - uv_0).norm());\n\t\t}\n\t}\n\n\tif (disparities.size() < 2)\n\t{\n\t\tdisp_mean = -1;\n\t\tdisp_var = -1;\n\t\ttotal_feats = 0;\n\t}\n\n\tdisp_mean = 0;\n\tfor (double disp_i : disparities)\n\t{\n\t\tdisp_mean += disp_i;\n\t}\n \n\tdisp_mean /= (double)disparities.size();\n\tdisp_var = 0;\n\tfor (double &disp_i : disparities)\n\t{\n\t\tdisp_var += std::pow(disp_i - disp_mean, 2);\n\t}\n\n\tdisp_var = std::sqrt(disp_var / (double)(disparities.size() - 1));\n\ttotal_feats = (int)disparities.size();\n}\n static void computeDisparity(std::shared_ptr db, double &disp_mean, double &disp_var, int &total_feats,\n double newest_time = -1, double oldest_time = -1) {\n\tstd::vector disparities;\n\tfor (auto &feat : db->getInternalData())\n\t{\n\t\tfor (auto &cam_pairs : feat.second->timestamps)\n\t\t{\n\t\t\tif (cam_pairs.second.size() < 2)\n\t\t\t\tcontinue;\n\n\t\t\tsize_t cam_id = cam_pairs.first;\n\t\t\tbool found_0 = false;\n\t\t\tbool found_1 = false;\n \n\t\t\tEigen::Vector2f uv_0 = Eigen::Vector2f::Zero();\n\t\t\tEigen::Vector2f uv_1 = Eigen::Vector2f::Zero();\n\n\t\t\tfor (size_t idx = 0; idx < feat.second->timestamps.at(cam_id).size(); idx++)\n\t\t\t{\n\t\t\t\tdouble time = feat.second->timestamps.at(cam_id).at(idx);\n\t\t\t\tif ((oldest_time == -1 || time > oldest_time) && !found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_0 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_0 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif ((newest_time == -1 || time < newest_time) && found_0)\n\t\t\t\t{\n\t\t\t\t\tuv_1 = feat.second->uvs.at(cam_id).at(idx).block(0, 0, 2, 1);\n\t\t\t\t\tfound_1 = true;\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!found_0 || !found_1)\n\t\t\t\tcontinue;\n \n\t\t\tdisparities.push_back((uv_1 - uv_0).norm());\n\t\t}\n\t}\n\n\tif (disparities.size() < 2)\n\t{\n\t\tdisp_mean = -1;\n\t\tdisp_var = -1;\n\t\ttotal_feats = 0;\n\t}\n\n\tdisp_mean = 0;\n\tfor (double disp_i : disparities)\n\t{\n\t\tdisp_mean += disp_i;\n\t}\n \n\tdisp_mean /= (double)disparities.size();\n\tdisp_var = 0;\n\tfor (double &disp_i : disparities)\n\t{\n\t\tdisp_var += std::pow(disp_i - disp_mean, 2);\n\t}\n\n\tdisp_var = std::sqrt(disp_var / (double)(disparities.size() - 1));\n\ttotal_feats = (int)disparities.size();\n}\n private:\n featureHelper() {\n\n}\n};"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_hash.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_HASH_H\n#define TSL_ROBIN_HASH_H \n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \"robin_growth_policy.h\"\n\n\nnamespace tsl {\n \nnamespace detail_robin_hash {\n\ntemplate\nstruct make_void {\n using type = void;\n};\n\ntemplate\nstruct has_is_transparent: std::false_type {\n};\n\ntemplate\nstruct has_is_transparent::type>: std::true_type {\n};\n\ntemplate\nstruct is_power_of_two_policy: std::false_type {\n};\n\ntemplate\nstruct is_power_of_two_policy>: std::true_type {\n};\n\n// Only available in C++17, we need to be compatible with C++11\ntemplate\nconst T& clamp( const T& v, const T& lo, const T& hi) {\n return std::min(hi, std::max(lo, v));\n}\n\ntemplate\nstatic T numeric_cast(U value, const char* error_message = \"numeric_cast() failed.\") {\n T ret = static_cast(value);\n if(static_cast(ret) != value) {\n TSL_RH_THROW_OR_TERMINATE(std::runtime_error, error_message);\n }\n \n const bool is_same_signedness = (std::is_unsigned::value && std::is_unsigned::value) ||\n (std::is_signed::value && std::is_signed::value);\n if(!is_same_signedness && (ret < T{}) != (value < U{})) {\n TSL_RH_THROW_OR_TERMINATE(std::runtime_error, error_message);\n }\n \n return ret;\n}\n\n\nusing truncated_hash_type = std::uint_least32_t;\n\n/**\n * Helper class that stores a truncated hash if StoreHash is true and nothing otherwise.\n */\ntemplate\nclass bucket_entry_hash {\npublic:\n bool bucket_hash_equal(std::size_t /*hash*/) const noexcept {\n return true;\n }\n \n truncated_hash_type truncated_hash() const noexcept {\n return 0;\n }\n \nprotected:\n void set_hash(truncated_hash_type /*hash*/) noexcept {\n }\n};\n\ntemplate<>\nclass bucket_entry_hash {\npublic:\n bool bucket_hash_equal(std::size_t hash) const noexcept {\n return m_hash == truncated_hash_type(hash);\n }\n \n truncated_hash_type truncated_hash() const noexcept {\n return m_hash;\n }\n \nprotected:\n void set_hash(truncated_hash_type hash) noexcept {\n m_hash = truncated_hash_type(hash);\n }\n \nprivate: \n truncated_hash_type m_hash;\n};\n\n\n/**\n * Each bucket entry has:\n * - A value of type `ValueType`.\n * - An integer to store how far the value of the bucket, if any, is from its ideal bucket \n * (ex: if the current bucket 5 has the value 'foo' and `hash('foo') % nb_buckets` == 3,\n * `dist_from_ideal_bucket()` will return 2 as the current value of the bucket is two\n * buckets away from its ideal bucket)\n * If there is no value in the bucket (i.e. `empty()` is true) `dist_from_ideal_bucket()` will be < 0.\n * - A marker which tells us if the bucket is the last bucket of the bucket array (useful for the \n * iterator of the hash table).\n * - If `StoreHash` is true, 32 bits of the hash of the value, if any, are also stored in the bucket. \n * If the size of the hash is more than 32 bits, it is truncated. We don't store the full hash\n * as storing the hash is a potential opportunity to use the unused space due to the alignment\n * of the bucket_entry structure. We can thus potentially store the hash without any extra space \n * (which would not be possible with 64 bits of the hash).\n */\ntemplate\nclass bucket_entry: public bucket_entry_hash {\n using bucket_hash = bucket_entry_hash;\n \npublic:\n using value_type = ValueType;\n using distance_type = std::int_least16_t;\n \n \n bucket_entry() noexcept: bucket_hash(), m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),\n m_last_bucket(false)\n {\n tsl_rh_assert(empty());\n }\n \n bucket_entry(bool last_bucket) noexcept: bucket_hash(), m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET),\n m_last_bucket(last_bucket)\n {\n tsl_rh_assert(empty());\n }\n \n bucket_entry(const bucket_entry& other) noexcept(std::is_nothrow_copy_constructible::value): \n bucket_hash(other),\n m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET), \n m_last_bucket(other.m_last_bucket)\n {\n if(!other.empty()) {\n ::new (static_cast(std::addressof(m_value))) value_type(other.value());\n m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;\n }\n }\n \n /**\n * Never really used, but still necessary as we must call resize on an empty `std::vector`.\n * and we need to support move-only types. See robin_hash constructor for details.\n */\n bucket_entry(bucket_entry&& other) noexcept(std::is_nothrow_move_constructible::value): \n bucket_hash(std::move(other)),\n m_dist_from_ideal_bucket(EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET), \n m_last_bucket(other.m_last_bucket) \n {\n if(!other.empty()) {\n ::new (static_cast(std::addressof(m_value))) value_type(std::move(other.value()));\n m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;\n }\n }\n \n bucket_entry& operator=(const bucket_entry& other) \n noexcept(std::is_nothrow_copy_constructible::value) \n {\n if(this != &other) {\n clear();\n \n bucket_hash::operator=(other);\n if(!other.empty()) {\n ::new (static_cast(std::addressof(m_value))) value_type(other.value());\n }\n \n m_dist_from_ideal_bucket = other.m_dist_from_ideal_bucket;\n m_last_bucket = other.m_last_bucket;\n }\n \n return *this;\n }\n \n bucket_entry& operator=(bucket_entry&& ) = delete;\n \n ~bucket_entry() noexcept {\n clear();\n }\n \n void clear() noexcept {\n if(!empty()) {\n destroy_value();\n m_dist_from_ideal_bucket = EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET;\n }\n }\n \n bool empty() const noexcept {\n return m_dist_from_ideal_bucket == EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET;\n }\n \n value_type& value() noexcept {\n tsl_rh_assert(!empty());\n return *reinterpret_cast(std::addressof(m_value));\n }\n \n const value_type& value() const noexcept {\n tsl_rh_assert(!empty());\n return *reinterpret_cast(std::addressof(m_value));\n }\n \n distance_type dist_from_ideal_bucket() const noexcept {\n return m_dist_from_ideal_bucket;\n }\n \n bool last_bucket() const noexcept {\n return m_last_bucket;\n }\n \n void set_as_last_bucket() noexcept {\n m_last_bucket = true;\n }\n \n template\n void set_value_of_empty_bucket(distance_type dist_from_ideal_bucket, \n truncated_hash_type hash, Args&&... value_type_args) \n {\n tsl_rh_assert(dist_from_ideal_bucket >= 0);\n tsl_rh_assert(empty());\n \n ::new (static_cast(std::addressof(m_value))) value_type(std::forward(value_type_args)...);\n this->set_hash(hash);\n m_dist_from_ideal_bucket = dist_from_ideal_bucket;\n \n tsl_rh_assert(!empty());\n }\n \n void swap_with_value_in_bucket(distance_type& dist_from_ideal_bucket, \n truncated_hash_type& hash, value_type& value) \n {\n tsl_rh_assert(!empty());\n \n using std::swap;\n swap(value, this->value());\n swap(dist_from_ideal_bucket, m_dist_from_ideal_bucket);\n \n // Avoid warning of unused variable if StoreHash is false\n (void) hash;\n if(StoreHash) {\n const truncated_hash_type tmp_hash = this->truncated_hash();\n this->set_hash(hash);\n hash = tmp_hash;\n }\n }\n \n static truncated_hash_type truncate_hash(std::size_t hash) noexcept {\n return truncated_hash_type(hash);\n }\n \nprivate:\n void destroy_value() noexcept {\n tsl_rh_assert(!empty());\n value().~value_type();\n }\n\npublic:\n static const distance_type DIST_FROM_IDEAL_BUCKET_LIMIT = 4096;\n static_assert(DIST_FROM_IDEAL_BUCKET_LIMIT <= std::numeric_limits::max() - 1,\n \"DIST_FROM_IDEAL_BUCKET_LIMIT must be <= std::numeric_limits::max() - 1.\");\n \nprivate:\n using storage = typename std::aligned_storage::type;\n \n static const distance_type EMPTY_MARKER_DIST_FROM_IDEAL_BUCKET = -1;\n \n distance_type m_dist_from_ideal_bucket;\n bool m_last_bucket;\n storage m_value;\n};\n\n\n\n/**\n * Internal common class used by `robin_map` and `robin_set`. \n * \n * ValueType is what will be stored by `robin_hash` (usually `std::pair` for map and `Key` for set).\n * \n * `KeySelect` should be a `FunctionObject` which takes a `ValueType` in parameter and returns a \n * reference to the key.\n * \n * `ValueSelect` should be a `FunctionObject` which takes a `ValueType` in parameter and returns a \n * reference to the value. `ValueSelect` should be void if there is no value (in a set for example).\n * \n * The strong exception guarantee only holds if the expression \n * `std::is_nothrow_swappable::value && std::is_nothrow_move_constructible::value` is true.\n * \n * Behaviour is undefined if the destructor of `ValueType` throws.\n */\ntemplate\nclass robin_hash: private Hash, private KeyEqual, private GrowthPolicy {\nprivate: \n template\n using has_mapped_type = typename std::integral_constant::value>;\n \n static_assert(noexcept(std::declval().bucket_for_hash(std::size_t(0))), \"GrowthPolicy::bucket_for_hash must be noexcept.\");\n static_assert(noexcept(std::declval().clear()), \"GrowthPolicy::clear must be noexcept.\");\n \npublic:\n template\n class robin_iterator;\n \n using key_type = typename KeySelect::key_type;\n using value_type = ValueType;\n using size_type = std::size_t;\n using difference_type = std::ptrdiff_t;\n using hasher = Hash;\n using key_equal = KeyEqual;\n using allocator_type = Allocator;\n using reference = value_type&;\n using const_reference = const value_type&;\n using pointer = value_type*;\n using const_pointer = const value_type*;\n using iterator = robin_iterator;\n using const_iterator = robin_iterator;\n \n \nprivate:\n /**\n * Either store the hash because we are asked by the `StoreHash` template parameter\n * or store the hash because it doesn't cost us anything in size and can be used to speed up rehash.\n */\n static constexpr bool STORE_HASH = StoreHash || \n (\n (sizeof(tsl::detail_robin_hash::bucket_entry) ==\n sizeof(tsl::detail_robin_hash::bucket_entry))\n &&\n (sizeof(std::size_t) == sizeof(truncated_hash_type) ||\n is_power_of_two_policy::value)\n &&\n // Don't store the hash for primitive types with default hash.\n (!std::is_arithmetic::value ||\n !std::is_same>::value)\n );\n \n /**\n * Only use the stored hash on lookup if we are explicitly asked. We are not sure how slow\n * the KeyEqual operation is. An extra comparison may slow things down with a fast KeyEqual.\n */\n static constexpr bool USE_STORED_HASH_ON_LOOKUP = StoreHash;\n\n /**\n * We can only use the hash on rehash if the size of the hash type is the same as the stored one or\n * if we use a power of two modulo. In the case of the power of two modulo, we just mask\n * the least significant bytes, we just have to check that the truncated_hash_type didn't truncated\n * more bytes.\n */\n static bool USE_STORED_HASH_ON_REHASH(size_type bucket_count) {\n (void) bucket_count;\n if(STORE_HASH && sizeof(std::size_t) == sizeof(truncated_hash_type)) {\n return true;\n }\n else if(STORE_HASH && is_power_of_two_policy::value) {\n tsl_rh_assert(bucket_count > 0);\n return (bucket_count - 1) <= std::numeric_limits::max();\n }\n else {\n return false; \n }\n }\n \n using bucket_entry = tsl::detail_robin_hash::bucket_entry;\n using distance_type = typename bucket_entry::distance_type;\n \n using buckets_allocator = typename std::allocator_traits::template rebind_alloc;\n using buckets_container_type = std::vector;\n \n \npublic: \n /**\n * The 'operator*()' and 'operator->()' methods return a const reference and const pointer respectively to the \n * stored value type.\n * \n * In case of a map, to get a mutable reference to the value associated to a key (the '.second' in the \n * stored pair), you have to call 'value()'. \n * \n * The main reason for this is that if we returned a `std::pair&` instead \n * of a `const std::pair&`, the user may modify the key which will put the map in a undefined state.\n */\n template\n class robin_iterator {\n friend class robin_hash;\n \n private:\n using bucket_entry_ptr = typename std::conditional::type;\n \n \n robin_iterator(bucket_entry_ptr bucket) noexcept: m_bucket(bucket) {\n }\n \n public:\n using iterator_category = std::forward_iterator_tag;\n using value_type = const typename robin_hash::value_type;\n using difference_type = std::ptrdiff_t;\n using reference = value_type&;\n using pointer = value_type*;\n \n \n robin_iterator() noexcept {\n }\n \n // Copy constructor from iterator to const_iterator.\n template::type* = nullptr>\n robin_iterator(const robin_iterator& other) noexcept: m_bucket(other.m_bucket) {\n }\n \n robin_iterator(const robin_iterator& other) = default;\n robin_iterator(robin_iterator&& other) = default;\n robin_iterator& operator=(const robin_iterator& other) = default;\n robin_iterator& operator=(robin_iterator&& other) = default;\n \n const typename robin_hash::key_type& key() const {\n return KeySelect()(m_bucket->value());\n }\n\n template::value && IsConst>::type* = nullptr>\n const typename U::value_type& value() const {\n return U()(m_bucket->value());\n }\n\n template::value && !IsConst>::type* = nullptr>\n typename U::value_type& value() const {\n return U()(m_bucket->value());\n }\n \n reference operator*() const {\n return m_bucket->value();\n }\n \n pointer operator->() const {\n return std::addressof(m_bucket->value());\n }\n \n robin_iterator& operator++() {\n while(true) {\n if(m_bucket->last_bucket()) {\n ++m_bucket;\n return *this;\n }\n \n ++m_bucket;\n if(!m_bucket->empty()) {\n return *this;\n }\n }\n }\n \n robin_iterator operator++(int) {\n robin_iterator tmp(*this);\n ++*this;\n \n return tmp;\n }\n \n friend bool operator==(const robin_iterator& lhs, const robin_iterator& rhs) { \n return lhs.m_bucket == rhs.m_bucket; \n }\n \n friend bool operator!=(const robin_iterator& lhs, const robin_iterator& rhs) { \n return !(lhs == rhs); \n }\n \n private:\n bucket_entry_ptr m_bucket;\n };\n\n \npublic:\n#if defined(__cplusplus) && __cplusplus >= 201402L\n robin_hash(size_type bucket_count, \n const Hash& hash,\n const KeyEqual& equal,\n const Allocator& alloc,\n float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,\n float max_load_factor = DEFAULT_MAX_LOAD_FACTOR): \n Hash(hash), \n KeyEqual(equal),\n GrowthPolicy(bucket_count),\n m_buckets_data(\n [&]() {\n if(bucket_count > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \n \"The map exceeds its maximum bucket count.\");\n }\n \n return bucket_count;\n }(), alloc\n ),\n m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),\n m_bucket_count(bucket_count),\n m_nb_elements(0), \n m_grow_on_next_insert(false),\n m_try_shrink_on_next_insert(false)\n {\n if(m_bucket_count > 0) {\n tsl_rh_assert(!m_buckets_data.empty());\n m_buckets_data.back().set_as_last_bucket();\n }\n \n this->min_load_factor(min_load_factor);\n this->max_load_factor(max_load_factor);\n }\n#else\n /**\n * C++11 doesn't support the creation of a std::vector with a custom allocator and 'count' default-inserted elements. \n * The needed contructor `explicit vector(size_type count, const Allocator& alloc = Allocator());` is only\n * available in C++14 and later. We thus must resize after using the `vector(const Allocator& alloc)` constructor.\n * \n * We can't use `vector(size_type count, const T& value, const Allocator& alloc)` as it requires the\n * value T to be copyable.\n */\n robin_hash(size_type bucket_count, \n const Hash& hash,\n const KeyEqual& equal,\n const Allocator& alloc,\n float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,\n float max_load_factor = DEFAULT_MAX_LOAD_FACTOR): \n Hash(hash), \n KeyEqual(equal),\n GrowthPolicy(bucket_count),\n m_buckets_data(alloc), \n m_buckets(static_empty_bucket_ptr()), \n m_bucket_count(bucket_count),\n m_nb_elements(0), \n m_grow_on_next_insert(false),\n m_try_shrink_on_next_insert(false)\n {\n if(bucket_count > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The map exceeds its maximum bucket count.\");\n }\n \n if(m_bucket_count > 0) {\n m_buckets_data.resize(m_bucket_count);\n m_buckets = m_buckets_data.data();\n \n tsl_rh_assert(!m_buckets_data.empty());\n m_buckets_data.back().set_as_last_bucket();\n }\n \n this->min_load_factor(min_load_factor);\n this->max_load_factor(max_load_factor);\n }\n#endif\n \n robin_hash(const robin_hash& other): Hash(other),\n KeyEqual(other),\n GrowthPolicy(other),\n m_buckets_data(other.m_buckets_data),\n m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),\n m_bucket_count(other.m_bucket_count),\n m_nb_elements(other.m_nb_elements),\n m_load_threshold(other.m_load_threshold),\n m_min_load_factor(other.m_min_load_factor),\n m_max_load_factor(other.m_max_load_factor),\n m_grow_on_next_insert(other.m_grow_on_next_insert),\n m_try_shrink_on_next_insert(other.m_try_shrink_on_next_insert)\n {\n }\n \n robin_hash(robin_hash&& other) noexcept(std::is_nothrow_move_constructible::value &&\n std::is_nothrow_move_constructible::value &&\n std::is_nothrow_move_constructible::value &&\n std::is_nothrow_move_constructible::value)\n : Hash(std::move(static_cast(other))),\n KeyEqual(std::move(static_cast(other))),\n GrowthPolicy(std::move(static_cast(other))),\n m_buckets_data(std::move(other.m_buckets_data)),\n m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),\n m_bucket_count(other.m_bucket_count),\n m_nb_elements(other.m_nb_elements),\n m_load_threshold(other.m_load_threshold),\n m_min_load_factor(other.m_min_load_factor),\n m_max_load_factor(other.m_max_load_factor),\n m_grow_on_next_insert(other.m_grow_on_next_insert),\n m_try_shrink_on_next_insert(other.m_try_shrink_on_next_insert)\n {\n other.clear_and_shrink();\n }\n \n robin_hash& operator=(const robin_hash& other) {\n if(&other != this) {\n Hash::operator=(other);\n KeyEqual::operator=(other);\n GrowthPolicy::operator=(other);\n \n m_buckets_data = other.m_buckets_data;\n m_buckets = m_buckets_data.empty()?static_empty_bucket_ptr():\n m_buckets_data.data();\n m_bucket_count = other.m_bucket_count;\n m_nb_elements = other.m_nb_elements;\n \n m_load_threshold = other.m_load_threshold;\n m_min_load_factor = other.m_min_load_factor;\n m_max_load_factor = other.m_max_load_factor;\n \n m_grow_on_next_insert = other.m_grow_on_next_insert;\n m_try_shrink_on_next_insert = other.m_try_shrink_on_next_insert;\n }\n \n return *this;\n }\n \n robin_hash& operator=(robin_hash&& other) {\n other.swap(*this);\n other.clear();\n \n return *this;\n }\n \n allocator_type get_allocator() const {\n return m_buckets_data.get_allocator();\n }\n \n \n /*\n * Iterators\n */\n iterator begin() noexcept {\n std::size_t i = 0;\n while(i < m_bucket_count && m_buckets[i].empty()) {\n i++;\n }\n \n return iterator(m_buckets + i);\n }\n \n const_iterator begin() const noexcept {\n return cbegin();\n }\n \n const_iterator cbegin() const noexcept {\n std::size_t i = 0;\n while(i < m_bucket_count && m_buckets[i].empty()) {\n i++;\n }\n \n return const_iterator(m_buckets + i);\n }\n \n iterator end() noexcept {\n return iterator(m_buckets + m_bucket_count);\n }\n \n const_iterator end() const noexcept {\n return cend();\n }\n \n const_iterator cend() const noexcept {\n return const_iterator(m_buckets + m_bucket_count);\n }\n \n \n /*\n * Capacity\n */\n bool empty() const noexcept {\n return m_nb_elements == 0;\n }\n \n size_type size() const noexcept {\n return m_nb_elements;\n }\n \n size_type max_size() const noexcept {\n return m_buckets_data.max_size();\n }\n \n /*\n * Modifiers\n */\n void clear() noexcept {\n if(m_min_load_factor > 0.0f) {\n clear_and_shrink();\n }\n else {\n for(auto& bucket: m_buckets_data) {\n bucket.clear();\n }\n \n m_nb_elements = 0;\n m_grow_on_next_insert = false;\n }\n }\n \n \n \n template\n std::pair insert(P&& value) {\n return insert_impl(KeySelect()(value), std::forward

(value));\n }\n \n template\n iterator insert_hint(const_iterator hint, P&& value) { \n if(hint != cend() && compare_keys(KeySelect()(*hint), KeySelect()(value))) { \n return mutable_iterator(hint); \n }\n \n return insert(std::forward

(value)).first; \n }\n \n template\n void insert(InputIt first, InputIt last) {\n if(std::is_base_of::iterator_category>::value) \n {\n const auto nb_elements_insert = std::distance(first, last);\n const size_type nb_free_buckets = m_load_threshold - size();\n tsl_rh_assert(m_load_threshold >= size());\n \n if(nb_elements_insert > 0 && nb_free_buckets < size_type(nb_elements_insert)) {\n reserve(size() + size_type(nb_elements_insert));\n }\n }\n \n for(; first != last; ++first) {\n insert(*first);\n }\n }\n \n \n \n template\n std::pair insert_or_assign(K&& key, M&& obj) { \n auto it = try_emplace(std::forward(key), std::forward(obj));\n if(!it.second) {\n it.first.value() = std::forward(obj);\n }\n \n return it;\n }\n \n template\n iterator insert_or_assign(const_iterator hint, K&& key, M&& obj) {\n if(hint != cend() && compare_keys(KeySelect()(*hint), key)) { \n auto it = mutable_iterator(hint); \n it.value() = std::forward(obj);\n \n return it;\n }\n \n return insert_or_assign(std::forward(key), std::forward(obj)).first;\n }\n\n \n template\n std::pair emplace(Args&&... args) {\n return insert(value_type(std::forward(args)...));\n }\n \n template\n iterator emplace_hint(const_iterator hint, Args&&... args) {\n return insert_hint(hint, value_type(std::forward(args)...)); \n }\n \n \n \n template\n std::pair try_emplace(K&& key, Args&&... args) {\n return insert_impl(key, std::piecewise_construct, \n std::forward_as_tuple(std::forward(key)), \n std::forward_as_tuple(std::forward(args)...));\n }\n \n template\n iterator try_emplace_hint(const_iterator hint, K&& key, Args&&... args) { \n if(hint != cend() && compare_keys(KeySelect()(*hint), key)) { \n return mutable_iterator(hint); \n }\n \n return try_emplace(std::forward(key), std::forward(args)...).first;\n }\n \n /**\n * Here to avoid `template size_type erase(const K& key)` being used when\n * we use an `iterator` instead of a `const_iterator`.\n */\n iterator erase(iterator pos) {\n erase_from_bucket(pos);\n \n /**\n * Erase bucket used a backward shift after clearing the bucket.\n * Check if there is a new value in the bucket, if not get the next non-empty.\n */\n if(pos.m_bucket->empty()) {\n ++pos;\n }\n \n m_try_shrink_on_next_insert = true;\n \n return pos;\n }\n \n iterator erase(const_iterator pos) {\n return erase(mutable_iterator(pos));\n }\n \n iterator erase(const_iterator first, const_iterator last) {\n if(first == last) {\n return mutable_iterator(first);\n }\n \n auto first_mutable = mutable_iterator(first);\n auto last_mutable = mutable_iterator(last);\n for(auto it = first_mutable.m_bucket; it != last_mutable.m_bucket; ++it) {\n if(!it->empty()) {\n it->clear();\n m_nb_elements--;\n }\n }\n \n if(last_mutable == end()) {\n m_try_shrink_on_next_insert = true;\n return end();\n }\n \n \n /*\n * Backward shift on the values which come after the deleted values.\n * We try to move the values closer to their ideal bucket.\n */\n std::size_t icloser_bucket = static_cast(first_mutable.m_bucket - m_buckets);\n std::size_t ito_move_closer_value = static_cast(last_mutable.m_bucket - m_buckets);\n tsl_rh_assert(ito_move_closer_value > icloser_bucket);\n \n const std::size_t ireturn_bucket = ito_move_closer_value - \n std::min(ito_move_closer_value - icloser_bucket, \n std::size_t(m_buckets[ito_move_closer_value].dist_from_ideal_bucket()));\n \n while(ito_move_closer_value < m_bucket_count && m_buckets[ito_move_closer_value].dist_from_ideal_bucket() > 0) {\n icloser_bucket = ito_move_closer_value - \n std::min(ito_move_closer_value - icloser_bucket, \n std::size_t(m_buckets[ito_move_closer_value].dist_from_ideal_bucket()));\n \n \n tsl_rh_assert(m_buckets[icloser_bucket].empty());\n const distance_type new_distance = distance_type(m_buckets[ito_move_closer_value].dist_from_ideal_bucket() -\n (ito_move_closer_value - icloser_bucket));\n m_buckets[icloser_bucket].set_value_of_empty_bucket(new_distance, \n m_buckets[ito_move_closer_value].truncated_hash(), \n std::move(m_buckets[ito_move_closer_value].value()));\n m_buckets[ito_move_closer_value].clear();\n \n \n ++icloser_bucket;\n ++ito_move_closer_value;\n }\n \n m_try_shrink_on_next_insert = true;\n \n return iterator(m_buckets + ireturn_bucket);\n }\n \n \n template\n size_type erase(const K& key) {\n return erase(key, hash_key(key));\n }\n \n template\n size_type erase(const K& key, std::size_t hash) {\n auto it = find(key, hash);\n if(it != end()) {\n erase_from_bucket(it);\n m_try_shrink_on_next_insert = true;\n \n return 1;\n }\n else {\n return 0;\n }\n }\n \n \n \n \n \n void swap(robin_hash& other) {\n using std::swap;\n \n swap(static_cast(*this), static_cast(other));\n swap(static_cast(*this), static_cast(other));\n swap(static_cast(*this), static_cast(other));\n swap(m_buckets_data, other.m_buckets_data);\n swap(m_buckets, other.m_buckets);\n swap(m_bucket_count, other.m_bucket_count);\n swap(m_nb_elements, other.m_nb_elements);\n swap(m_load_threshold, other.m_load_threshold);\n swap(m_min_load_factor, other.m_min_load_factor);\n swap(m_max_load_factor, other.m_max_load_factor);\n swap(m_grow_on_next_insert, other.m_grow_on_next_insert);\n swap(m_try_shrink_on_next_insert, other.m_try_shrink_on_next_insert);\n }\n \n \n /*\n * Lookup\n */\n template::value>::type* = nullptr>\n typename U::value_type& at(const K& key) {\n return at(key, hash_key(key));\n }\n \n template::value>::type* = nullptr>\n typename U::value_type& at(const K& key, std::size_t hash) {\n return const_cast(static_cast(this)->at(key, hash));\n }\n \n \n template::value>::type* = nullptr>\n const typename U::value_type& at(const K& key) const {\n return at(key, hash_key(key));\n }\n \n template::value>::type* = nullptr>\n const typename U::value_type& at(const K& key, std::size_t hash) const {\n auto it = find(key, hash);\n if(it != cend()) {\n return it.value();\n }\n else {\n TSL_RH_THROW_OR_TERMINATE(std::out_of_range, \"Couldn't find key.\");\n }\n }\n \n template::value>::type* = nullptr>\n typename U::value_type& operator[](K&& key) {\n return try_emplace(std::forward(key)).first.value();\n }\n \n \n template\n size_type count(const K& key) const {\n return count(key, hash_key(key));\n }\n \n template\n size_type count(const K& key, std::size_t hash) const {\n if(find(key, hash) != cend()) {\n return 1;\n }\n else {\n return 0;\n }\n }\n \n \n template\n iterator find(const K& key) {\n return find_impl(key, hash_key(key));\n }\n \n template\n iterator find(const K& key, std::size_t hash) {\n return find_impl(key, hash);\n }\n \n \n template\n const_iterator find(const K& key) const {\n return find_impl(key, hash_key(key));\n }\n \n template\n const_iterator find(const K& key, std::size_t hash) const {\n return find_impl(key, hash);\n }\n \n \n template\n bool contains(const K& key) const {\n return contains(key, hash_key(key));\n }\n \n template\n bool contains(const K& key, std::size_t hash) const {\n return count(key, hash) != 0;\n }\n \n \n template\n std::pair equal_range(const K& key) {\n return equal_range(key, hash_key(key));\n }\n \n template\n std::pair equal_range(const K& key, std::size_t hash) {\n iterator it = find(key, hash);\n return std::make_pair(it, (it == end())?it:std::next(it));\n }\n \n \n template\n std::pair equal_range(const K& key) const {\n return equal_range(key, hash_key(key));\n }\n \n template\n std::pair equal_range(const K& key, std::size_t hash) const {\n const_iterator it = find(key, hash);\n return std::make_pair(it, (it == cend())?it:std::next(it));\n }\n \n /*\n * Bucket interface \n */\n size_type bucket_count() const {\n return m_bucket_count; \n }\n \n size_type max_bucket_count() const {\n return std::min(GrowthPolicy::max_bucket_count(), m_buckets_data.max_size());\n }\n \n /*\n * Hash policy \n */\n float load_factor() const {\n if(bucket_count() == 0) {\n return 0;\n }\n \n return float(m_nb_elements)/float(bucket_count());\n }\n \n float min_load_factor() const {\n return m_min_load_factor;\n }\n \n float max_load_factor() const {\n return m_max_load_factor;\n }\n \n void min_load_factor(float ml) {\n m_min_load_factor = clamp(ml, float(MINIMUM_MIN_LOAD_FACTOR), \n float(MAXIMUM_MIN_LOAD_FACTOR));\n }\n \n void max_load_factor(float ml) {\n m_max_load_factor = clamp(ml, float(MINIMUM_MAX_LOAD_FACTOR), \n float(MAXIMUM_MAX_LOAD_FACTOR));\n m_load_threshold = size_type(float(bucket_count())*m_max_load_factor);\n }\n \n void rehash(size_type count) {\n count = std::max(count, size_type(std::ceil(float(size())/max_load_factor())));\n rehash_impl(count);\n }\n \n void reserve(size_type count) {\n rehash(size_type(std::ceil(float(count)/max_load_factor())));\n } \n \n /*\n * Observers\n */\n hasher hash_function() const {\n return static_cast(*this);\n }\n \n key_equal key_eq() const {\n return static_cast(*this);\n }\n \n \n /*\n * Other\n */ \n iterator mutable_iterator(const_iterator pos) {\n return iterator(const_cast(pos.m_bucket));\n }\n \nprivate:\n template\n std::size_t hash_key(const K& key) const {\n return Hash::operator()(key);\n }\n \n template\n bool compare_keys(const K1& key1, const K2& key2) const {\n return KeyEqual::operator()(key1, key2);\n }\n \n std::size_t bucket_for_hash(std::size_t hash) const {\n const std::size_t bucket = GrowthPolicy::bucket_for_hash(hash);\n tsl_rh_assert(bucket < m_bucket_count || (bucket == 0 && m_bucket_count == 0));\n \n return bucket;\n }\n \n template::value>::type* = nullptr>\n std::size_t next_bucket(std::size_t index) const noexcept {\n tsl_rh_assert(index < bucket_count());\n \n return (index + 1) & this->m_mask;\n }\n \n template::value>::type* = nullptr>\n std::size_t next_bucket(std::size_t index) const noexcept {\n tsl_rh_assert(index < bucket_count());\n \n index++;\n return (index != bucket_count())?index:0;\n }\n \n \n \n template\n iterator find_impl(const K& key, std::size_t hash) {\n return mutable_iterator(static_cast(this)->find(key, hash));\n }\n \n template\n const_iterator find_impl(const K& key, std::size_t hash) const {\n std::size_t ibucket = bucket_for_hash(hash); \n distance_type dist_from_ideal_bucket = 0;\n \n while(dist_from_ideal_bucket <= m_buckets[ibucket].dist_from_ideal_bucket()) {\n if(TSL_RH_LIKELY((!USE_STORED_HASH_ON_LOOKUP || m_buckets[ibucket].bucket_hash_equal(hash)) && \n compare_keys(KeySelect()(m_buckets[ibucket].value()), key))) \n {\n return const_iterator(m_buckets + ibucket);\n }\n \n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n \n return cend();\n }\n \n void erase_from_bucket(iterator pos) {\n pos.m_bucket->clear();\n m_nb_elements--;\n \n /**\n * Backward shift, swap the empty bucket, previous_ibucket, with the values on its right, ibucket,\n * until we cross another empty bucket or if the other bucket has a distance_from_ideal_bucket == 0.\n * \n * We try to move the values closer to their ideal bucket.\n */\n std::size_t previous_ibucket = static_cast(pos.m_bucket - m_buckets);\n std::size_t ibucket = next_bucket(previous_ibucket);\n \n while(m_buckets[ibucket].dist_from_ideal_bucket() > 0) {\n tsl_rh_assert(m_buckets[previous_ibucket].empty());\n \n const distance_type new_distance = distance_type(m_buckets[ibucket].dist_from_ideal_bucket() - 1);\n m_buckets[previous_ibucket].set_value_of_empty_bucket(new_distance, m_buckets[ibucket].truncated_hash(), \n std::move(m_buckets[ibucket].value()));\n m_buckets[ibucket].clear();\n\n previous_ibucket = ibucket;\n ibucket = next_bucket(ibucket);\n }\n }\n \n template\n std::pair insert_impl(const K& key, Args&&... value_type_args) {\n const std::size_t hash = hash_key(key);\n \n std::size_t ibucket = bucket_for_hash(hash); \n distance_type dist_from_ideal_bucket = 0;\n \n while(dist_from_ideal_bucket <= m_buckets[ibucket].dist_from_ideal_bucket()) {\n if((!USE_STORED_HASH_ON_LOOKUP || m_buckets[ibucket].bucket_hash_equal(hash)) &&\n compare_keys(KeySelect()(m_buckets[ibucket].value()), key)) \n {\n return std::make_pair(iterator(m_buckets + ibucket), false);\n }\n \n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n \n if(rehash_on_extreme_load()) {\n ibucket = bucket_for_hash(hash);\n dist_from_ideal_bucket = 0;\n \n while(dist_from_ideal_bucket <= m_buckets[ibucket].dist_from_ideal_bucket()) {\n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n }\n \n \n if(m_buckets[ibucket].empty()) {\n m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket, bucket_entry::truncate_hash(hash),\n std::forward(value_type_args)...);\n }\n else {\n insert_value(ibucket, dist_from_ideal_bucket, bucket_entry::truncate_hash(hash), \n std::forward(value_type_args)...);\n }\n \n \n m_nb_elements++;\n /*\n * The value will be inserted in ibucket in any case, either because it was\n * empty or by stealing the bucket (robin hood). \n */\n return std::make_pair(iterator(m_buckets + ibucket), true);\n }\n \n \n template\n void insert_value(std::size_t ibucket, distance_type dist_from_ideal_bucket, \n truncated_hash_type hash, Args&&... value_type_args) \n {\n value_type value(std::forward(value_type_args)...);\n insert_value_impl(ibucket, dist_from_ideal_bucket, hash, value);\n }\n\n void insert_value(std::size_t ibucket, distance_type dist_from_ideal_bucket,\n truncated_hash_type hash, value_type&& value)\n {\n insert_value_impl(ibucket, dist_from_ideal_bucket, hash, value);\n }\n\n /*\n * We don't use `value_type&& value` as last argument due to a bug in MSVC when `value_type` is a pointer,\n * The compiler is not able to see the difference between `std::string*` and `std::string*&&` resulting in \n * a compilation error.\n * \n * The `value` will be in a moved state at the end of the function.\n */\n void insert_value_impl(std::size_t ibucket, distance_type dist_from_ideal_bucket,\n truncated_hash_type hash, value_type& value)\n {\n m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket, hash, value);\n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n \n while(!m_buckets[ibucket].empty()) {\n if(dist_from_ideal_bucket > m_buckets[ibucket].dist_from_ideal_bucket()) {\n if(dist_from_ideal_bucket >= bucket_entry::DIST_FROM_IDEAL_BUCKET_LIMIT) {\n /**\n * The number of probes is really high, rehash the map on the next insert.\n * Difficult to do now as rehash may throw an exception.\n */\n m_grow_on_next_insert = true;\n }\n \n m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket, hash, value);\n }\n \n ibucket = next_bucket(ibucket);\n dist_from_ideal_bucket++;\n }\n \n m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket, hash, std::move(value));\n }\n \n \n void rehash_impl(size_type count) {\n robin_hash new_table(count, static_cast(*this), static_cast(*this), \n get_allocator(), m_min_load_factor, m_max_load_factor);\n \n const bool use_stored_hash = USE_STORED_HASH_ON_REHASH(new_table.bucket_count());\n for(auto& bucket: m_buckets_data) {\n if(bucket.empty()) { \n continue; \n }\n \n const std::size_t hash = use_stored_hash?bucket.truncated_hash():\n new_table.hash_key(KeySelect()(bucket.value()));\n \n new_table.insert_value_on_rehash(new_table.bucket_for_hash(hash), 0, \n bucket_entry::truncate_hash(hash), std::move(bucket.value()));\n }\n \n new_table.m_nb_elements = m_nb_elements;\n new_table.swap(*this);\n }\n \n void clear_and_shrink() noexcept {\n GrowthPolicy::clear();\n m_buckets_data.clear();\n m_buckets = static_empty_bucket_ptr();\n m_bucket_count = 0;\n m_nb_elements = 0;\n m_load_threshold = 0;\n m_grow_on_next_insert = false;\n m_try_shrink_on_next_insert = false;\n }\n \n void insert_value_on_rehash(std::size_t ibucket, distance_type dist_from_ideal_bucket, \n truncated_hash_type hash, value_type&& value) \n {\n while(true) {\n if(dist_from_ideal_bucket > m_buckets[ibucket].dist_from_ideal_bucket()) {\n if(m_buckets[ibucket].empty()) {\n m_buckets[ibucket].set_value_of_empty_bucket(dist_from_ideal_bucket, hash, std::move(value));\n return;\n }\n else {\n m_buckets[ibucket].swap_with_value_in_bucket(dist_from_ideal_bucket, hash, value);\n }\n }\n \n dist_from_ideal_bucket++;\n ibucket = next_bucket(ibucket);\n }\n }\n \n \n \n /**\n * Grow the table if m_grow_on_next_insert is true or we reached the max_load_factor.\n * Shrink the table if m_try_shrink_on_next_insert is true (an erase occurred) and\n * we're below the min_load_factor.\n * \n * Return true if the table has been rehashed.\n */\n bool rehash_on_extreme_load() {\n if(m_grow_on_next_insert || size() >= m_load_threshold) {\n rehash_impl(GrowthPolicy::next_bucket_count());\n m_grow_on_next_insert = false;\n \n return true;\n }\n \n if(m_try_shrink_on_next_insert) {\n m_try_shrink_on_next_insert = false;\n if(m_min_load_factor != 0.0f && load_factor() < m_min_load_factor) {\n reserve(size() + 1);\n \n return true;\n }\n }\n \n return false;\n }\n\n \npublic:\n static const size_type DEFAULT_INIT_BUCKETS_SIZE = 0;\n \n static constexpr float DEFAULT_MAX_LOAD_FACTOR = 0.5f;\n static constexpr float MINIMUM_MAX_LOAD_FACTOR = 0.2f;\n static constexpr float MAXIMUM_MAX_LOAD_FACTOR = 0.95f;\n \n static constexpr float DEFAULT_MIN_LOAD_FACTOR = 0.0f;\n static constexpr float MINIMUM_MIN_LOAD_FACTOR = 0.0f;\n static constexpr float MAXIMUM_MIN_LOAD_FACTOR = 0.15f;\n \n static_assert(MINIMUM_MAX_LOAD_FACTOR < MAXIMUM_MAX_LOAD_FACTOR, \n \"MINIMUM_MAX_LOAD_FACTOR should be < MAXIMUM_MAX_LOAD_FACTOR\");\n static_assert(MINIMUM_MIN_LOAD_FACTOR < MAXIMUM_MIN_LOAD_FACTOR, \n \"MINIMUM_MIN_LOAD_FACTOR should be < MAXIMUM_MIN_LOAD_FACTOR\");\n static_assert(MAXIMUM_MIN_LOAD_FACTOR < MINIMUM_MAX_LOAD_FACTOR, \n \"MAXIMUM_MIN_LOAD_FACTOR should be < MINIMUM_MAX_LOAD_FACTOR\");\n \nprivate:\n /**\n * Return an always valid pointer to an static empty bucket_entry with last_bucket() == true.\n */ \n bucket_entry* static_empty_bucket_ptr() noexcept {\n static bucket_entry empty_bucket(true);\n return &empty_bucket;\n }\n \nprivate:\n buckets_container_type m_buckets_data;\n \n /**\n * Points to m_buckets_data.data() if !m_buckets_data.empty() otherwise points to static_empty_bucket_ptr.\n * This variable is useful to avoid the cost of checking if m_buckets_data is empty when trying \n * to find an element.\n * \n * TODO Remove m_buckets_data and only use a pointer instead of a pointer+vector to save some space in the robin_hash object.\n * Manage the Allocator manually.\n */\n bucket_entry* m_buckets;\n \n /**\n * Used a lot in find, avoid the call to m_buckets_data.size() which is a bit slower.\n */\n size_type m_bucket_count;\n \n size_type m_nb_elements;\n \n size_type m_load_threshold;\n \n float m_min_load_factor;\n float m_max_load_factor;\n \n bool m_grow_on_next_insert;\n \n /**\n * We can't shrink down the map on erase operations as the erase methods need to return the next iterator.\n * Shrinking the map would invalidate all the iterators and we could not return the next iterator in a meaningful way,\n * On erase, we thus just indicate on erase that we should try to shrink the hash table on the next insert\n * if we go below the min_load_factor. \n */\n bool m_try_shrink_on_next_insert;\n};\n\n}\n\n}\n\n#endif\n"], ["/voxel_svio/include/parameters.h", "class stateOptions {\n public:\n bool do_fej = true;\n bool do_calib_camera_pose = false;\n bool do_calib_camera_intrinsics = false;\n bool do_calib_camera_timeoffset = false;\n bool do_calib_imu_intrinsics = false;\n bool do_calib_imu_g_sensitivity = false;\n ImuModel imu_model = ImuModel::KALIBR;\n int max_clone_size = 11;\n int max_slam_features = 25;\n int max_slam_in_update = 1000;\n int max_msckf_in_update = 1000;\n double voxel_size = 0.5;\n int max_num_points_in_voxel = 10;\n double min_distance_points = 0.15;\n bool use_huber = true;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n double init_window_time = 1.0;\n double init_imu_thresh = 1.0;\n double init_max_disparity = 1.0;\n int init_max_features = 50;\n bool init_dyn_use = false;\n bool init_dyn_mle_opt_calib = false;\n int init_dyn_mle_max_iter = 20;\n int init_dyn_mle_max_threads = 20;\n double init_dyn_mle_max_time = 5.0;\n int init_dyn_num_pose = 5;\n double init_dyn_min_deg = 45.0;\n double init_dyn_inflation_orientation = 10.0;\n double init_dyn_inflation_velocity = 10.0;\n double init_dyn_inflation_bias_gyro = 100.0;\n double init_dyn_inflation_bias_accel = 100.0;\n double init_dyn_min_rec_cond = 1e-15;\n Eigen::Vector3d init_dyn_bias_g = Eigen::Vector3d::Zero();\n Eigen::Vector3d init_dyn_bias_a = Eigen::Vector3d::Zero();\n double sigma_w = 1.6968e-04;\n double sigma_wb = 1.9393e-05;\n double sigma_a = 2.0000e-3;\n double sigma_ab = 3.0000e-03;\n double sigma_pix = 1;\n double gravity_mag = 9.81;\n bool downsample_cameras = false;\n double calib_camimu_dt = 0.0;\n std::unordered_map> camera_intrinsics;\n std::map camera_extrinsics;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n bool refine_features = true;\n int max_runs = 5;\n double init_lamda = 1e-3;\n double max_lamda = 1e10;\n double min_dx = 1e-6;\n double min_dcost = 1e-6;\n double lam_mult = 10;\n double min_dist = 0.10;\n double max_dist = 60;\n double max_baseline = 40;\n double max_cond_number = 10000;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n double sigma_w = 1.6968e-04;\n double sigma_w_2 = pow(1.6968e-04, 2);\n double sigma_wb = 1.9393e-05;\n double sigma_wb_2 = pow(1.9393e-05, 2);\n double sigma_a = 2.0000e-3;\n double sigma_a_2 = pow(2.0000e-3, 2);\n double sigma_ab = 3.0000e-03;\n double sigma_ab_2 = pow(3.0000e-03, 2);\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n double chi2_multipler = 5;\n double sigma_pix = 1;\n double sigma_pix_sq = 1;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n public:\n stateOptions state_options;\n inertialInitializerOptions init_options;\n double dt_slam_delay = 2.0;\n noiseManager imu_noises;\n updaterOptions msckf_options;\n updaterOptions slam_options;\n double gravity_mag = 9.81;\n Eigen::Matrix vec_dw;\n Eigen::Matrix vec_da;\n Eigen::Matrix vec_tg;\n Eigen::Matrix q_imu_acc;\n Eigen::Matrix q_imu_gyr;\n double calib_camimu_dt = 0.0;\n std::unordered_map> camera_intrinsics;\n std::map camera_extrinsics;\n bool use_mask = false;\n std::map masks;\n bool downsample_cameras = false;\n int num_pts = 150;\n int fast_threshold = 20;\n int patch_size_x = 5;\n int patch_size_y = 5;\n int min_px_dist = 10;\n HistogramMethod histogram_method = HistogramMethod::HISTOGRAM;\n double track_frequency = 20.0;\n featureInitializerOptions featinit_options;\n double voxel_size = 0.5;\n int max_num_points_in_voxel = 10;\n double min_distance_points = 0.15;\n int nb_voxels_visited = 1;\n bool use_all_points = false;\n bool use_keyframe = true;\n void recordParameters() {\n\tstate_options.recordParameters();\n\n\tinit_options.recordParameters();\n\n\tstd::string str_temp;\n\n\tstd::ofstream foutC(std::string(output_path + \"/parameter_list.txt\"), std::ios::app);\n\n\tfoutC << std::fixed << \"dt_slam_delay: \" << dt_slam_delay << std::endl;\n\n\timu_noises.recordParameters();\n\n\tmsckf_options.recordParameters();\n\n\tslam_options.recordParameters();\n\n\tfoutC << std::fixed << \"gravity_mag: \" << gravity_mag << std::endl;\n\n\tfoutC << std::fixed << \"vec_dw: \" << vec_dw.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_da: \" << vec_da.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"vec_tg: \" << vec_tg.transpose() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_acc: \" << q_imu_acc.w() << \" \" << q_imu_acc.x() << \" \" << q_imu_acc.y() << \" \" << q_imu_acc.z() << std::endl;\n\n\tfoutC << std::fixed << \"q_imu_gyr: \" << q_imu_gyr.w() << \" \" << q_imu_gyr.x() << \" \" << q_imu_gyr.y() << \" \" << q_imu_gyr.z() << std::endl;\n\n\tfoutC << std::fixed << \"calib_camimu_dt: \" << calib_camimu_dt << std::endl;\n\n\tfor (int i = 0; i < 2; i++)\n\t{\n\t\tif (i == 0) foutC << \"left camera parameters: \" << std::endl;\n\t\telse foutC << \"right camera parameters: \" << std::endl;\n\n\t\tfoutC << std::fixed << \"camera intrinic: \" << camera_intrinsics.at(i)->getValue().transpose() << std::endl;\n\t\tfoutC << std::fixed << \"camera_k_OPENCV: \" \n\t\t\t << camera_intrinsics.at(i)->getK()(0, 0) << \" \" << camera_intrinsics.at(i)->getK()(0, 1) << \" \" << camera_intrinsics.at(i)->getK()(0, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(1, 0) << \" \" << camera_intrinsics.at(i)->getK()(1, 1) << \" \" << camera_intrinsics.at(i)->getK()(1, 2) << std::endl\n\t\t\t << camera_intrinsics.at(i)->getK()(2, 0) << \" \" << camera_intrinsics.at(i)->getK()(2, 1) << \" \" << camera_intrinsics.at(i)->getK()(2, 2) << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_d_OPENCV: \"\n\t\t\t << camera_intrinsics.at(i)->getD()(0) << \" \" << camera_intrinsics.at(i)->getD()(1) << \" \" \n\t\t\t << camera_intrinsics.at(i)->getD()(2) << \" \" << camera_intrinsics.at(i)->getD()(3) << std::endl;\n\n\t\tfoutC << \"image width: \" << camera_intrinsics.at(i)->w() << std::endl;\n\t\tfoutC << \"image height: \" << camera_intrinsics.at(i)->h() << std::endl;\n\n\t\tfoutC << std::fixed << \"camera_extrinsics: \" << camera_extrinsics.at(i).transpose() << std::endl;\n\t}\n\n\tstr_temp = use_mask ? \"true\" : \"false\";\n\tfoutC << \"use_mask: \" << str_temp << std::endl;\n\n\tstr_temp = downsample_cameras ? \"true\" : \"false\";\n\tfoutC << \"downsample_cameras: \" << str_temp << std::endl;\n\n\tfoutC << \"num_pts: \" << num_pts << std::endl;\n\n\tfoutC << \"fast_threshold: \" << fast_threshold << std::endl;\n\n\tfoutC << \"patch_size_x: \" << patch_size_x << std::endl;\n\n\tfoutC << \"patch_size_y: \" << patch_size_y << std::endl;\n\n\tfoutC << \"min_px_dist: \" << min_px_dist << std::endl;\n\n\tswitch (histogram_method)\n\t{\n\tcase 0:\n\t\tstr_temp = \"none\";\n\t\tbreak;\n\tcase 1:\n\t\tstr_temp = \"histogram\";\n\t\tbreak;\n\tcase 2:\n\t\tstr_temp = \"clahe\";\n\t\tbreak;\n\tdefault:\n\t\tbreak;\n\t}\n\tfoutC << \"histogram_method: \" << str_temp << std::endl;\n\n\tfoutC << std::fixed << \"track_frequency: \" << track_frequency << std::endl;\n\n\tfeatinit_options.recordParameters();\n\n\tfoutC.close();\n}\n};"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_growth_policy.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_GROWTH_POLICY_H\n#define TSL_ROBIN_GROWTH_POLICY_H \n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n\n\n#ifdef TSL_DEBUG\n# define tsl_rh_assert(expr) assert(expr)\n#else\n# define tsl_rh_assert(expr) (static_cast(0))\n#endif\n\n\n/**\n * If exceptions are enabled, throw the exception passed in parameter, otherwise call std::terminate.\n */\n#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (defined (_MSC_VER) && defined (_CPPUNWIND))) && !defined(TSL_NO_EXCEPTIONS)\n# define TSL_RH_THROW_OR_TERMINATE(ex, msg) throw ex(msg)\n#else\n# define TSL_RH_NO_EXCEPTIONS\n# ifdef NDEBUG\n# define TSL_RH_THROW_OR_TERMINATE(ex, msg) std::terminate()\n# else\n# include \n# define TSL_RH_THROW_OR_TERMINATE(ex, msg) do { std::cerr << msg << std::endl; std::terminate(); } while(0)\n# endif\n#endif\n\n\n#if defined(__GNUC__) || defined(__clang__)\n# define TSL_RH_LIKELY(exp) (__builtin_expect(!!(exp), true))\n#else\n# define TSL_RH_LIKELY(exp) (exp)\n#endif\n\n\nnamespace tsl {\nnamespace rh {\n \n/**\n * Grow the hash table by a factor of GrowthFactor keeping the bucket count to a power of two. It allows\n * the table to use a mask operation instead of a modulo operation to map a hash to a bucket.\n * \n * GrowthFactor must be a power of two >= 2.\n */\ntemplate\nclass power_of_two_growth_policy {\npublic:\n /**\n * Called on the hash table creation and on rehash. The number of buckets for the table is passed in parameter.\n * This number is a minimum, the policy may update this value with a higher value if needed (but not lower).\n *\n * If 0 is given, min_bucket_count_in_out must still be 0 after the policy creation and\n * bucket_for_hash must always return 0 in this case.\n */\n explicit power_of_two_growth_policy(std::size_t& min_bucket_count_in_out) {\n if(min_bucket_count_in_out > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n if(min_bucket_count_in_out > 0) {\n min_bucket_count_in_out = round_up_to_power_of_two(min_bucket_count_in_out);\n m_mask = min_bucket_count_in_out - 1;\n }\n else {\n m_mask = 0;\n }\n }\n \n /**\n * Return the bucket [0, bucket_count()) to which the hash belongs. \n * If bucket_count() is 0, it must always return 0.\n */\n std::size_t bucket_for_hash(std::size_t hash) const noexcept {\n return hash & m_mask;\n }\n \n /**\n * Return the number of buckets that should be used on next growth.\n */\n std::size_t next_bucket_count() const {\n if((m_mask + 1) > max_bucket_count() / GrowthFactor) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n return (m_mask + 1) * GrowthFactor;\n }\n \n /**\n * Return the maximum number of buckets supported by the policy.\n */\n std::size_t max_bucket_count() const {\n // Largest power of two.\n return (std::numeric_limits::max() / 2) + 1;\n }\n \n /**\n * Reset the growth policy as if it was created with a bucket count of 0.\n * After a clear, the policy must always return 0 when bucket_for_hash is called.\n */\n void clear() noexcept {\n m_mask = 0;\n }\n \nprivate:\n static std::size_t round_up_to_power_of_two(std::size_t value) {\n if(is_power_of_two(value)) {\n return value;\n }\n \n if(value == 0) {\n return 1;\n }\n \n --value;\n for(std::size_t i = 1; i < sizeof(std::size_t) * CHAR_BIT; i *= 2) {\n value |= value >> i;\n }\n \n return value + 1;\n }\n \n static constexpr bool is_power_of_two(std::size_t value) {\n return value != 0 && (value & (value - 1)) == 0;\n }\n \nprotected:\n static_assert(is_power_of_two(GrowthFactor) && GrowthFactor >= 2, \"GrowthFactor must be a power of two >= 2.\");\n \n std::size_t m_mask;\n};\n\n\n/**\n * Grow the hash table by GrowthFactor::num / GrowthFactor::den and use a modulo to map a hash\n * to a bucket. Slower but it can be useful if you want a slower growth.\n */\ntemplate>\nclass mod_growth_policy {\npublic:\n explicit mod_growth_policy(std::size_t& min_bucket_count_in_out) {\n if(min_bucket_count_in_out > max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n if(min_bucket_count_in_out > 0) {\n m_mod = min_bucket_count_in_out;\n }\n else {\n m_mod = 1;\n }\n }\n \n std::size_t bucket_for_hash(std::size_t hash) const noexcept {\n return hash % m_mod;\n }\n \n std::size_t next_bucket_count() const {\n if(m_mod == max_bucket_count()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n const double next_bucket_count = std::ceil(double(m_mod) * REHASH_SIZE_MULTIPLICATION_FACTOR);\n if(!std::isnormal(next_bucket_count)) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n if(next_bucket_count > double(max_bucket_count())) {\n return max_bucket_count();\n }\n else {\n return std::size_t(next_bucket_count);\n }\n }\n \n std::size_t max_bucket_count() const {\n return MAX_BUCKET_COUNT;\n }\n \n void clear() noexcept {\n m_mod = 1;\n }\n \nprivate:\n static constexpr double REHASH_SIZE_MULTIPLICATION_FACTOR = 1.0 * GrowthFactor::num / GrowthFactor::den;\n static const std::size_t MAX_BUCKET_COUNT = \n std::size_t(double(\n std::numeric_limits::max() / REHASH_SIZE_MULTIPLICATION_FACTOR\n ));\n \n static_assert(REHASH_SIZE_MULTIPLICATION_FACTOR >= 1.1, \"Growth factor should be >= 1.1.\");\n \n std::size_t m_mod;\n};\n\n\n\nnamespace detail {\n\n#if SIZE_MAX >= ULLONG_MAX\n#define TSL_RH_NB_PRIMES 51\n#elif SIZE_MAX >= ULONG_MAX\n#define TSL_RH_NB_PRIMES 40\n#else\n#define TSL_RH_NB_PRIMES 23\n#endif\n\nstatic constexpr const std::array PRIMES = {{\n 1u, 5u, 17u, 29u, 37u, 53u, 67u, 79u, 97u, 131u, 193u, 257u, 389u, 521u, 769u, 1031u, \n 1543u, 2053u, 3079u, 6151u, 12289u, 24593u, 49157u,\n#if SIZE_MAX >= ULONG_MAX\n 98317ul, 196613ul, 393241ul, 786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul, \n 25165843ul, 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul, \n 3221225473ul, 4294967291ul,\n#endif\n#if SIZE_MAX >= ULLONG_MAX\n 6442450939ull, 12884901893ull, 25769803751ull, 51539607551ull, 103079215111ull, 206158430209ull, \n 412316860441ull, 824633720831ull, 1649267441651ull, 3298534883309ull, 6597069766657ull,\n#endif\n}};\n\ntemplate\nstatic constexpr std::size_t mod(std::size_t hash) { return hash % PRIMES[IPrime]; }\n\n// MOD_PRIME[iprime](hash) returns hash % PRIMES[iprime]. This table allows for faster modulo as the\n// compiler can optimize the modulo code better with a constant known at the compilation.\nstatic constexpr const std::array MOD_PRIME = {{ \n &mod<0>, &mod<1>, &mod<2>, &mod<3>, &mod<4>, &mod<5>, &mod<6>, &mod<7>, &mod<8>, &mod<9>, &mod<10>, \n &mod<11>, &mod<12>, &mod<13>, &mod<14>, &mod<15>, &mod<16>, &mod<17>, &mod<18>, &mod<19>, &mod<20>, \n &mod<21>, &mod<22>, \n#if SIZE_MAX >= ULONG_MAX\n &mod<23>, &mod<24>, &mod<25>, &mod<26>, &mod<27>, &mod<28>, &mod<29>, &mod<30>, &mod<31>, &mod<32>, \n &mod<33>, &mod<34>, &mod<35>, &mod<36>, &mod<37> , &mod<38>, &mod<39>,\n#endif\n#if SIZE_MAX >= ULLONG_MAX\n &mod<40>, &mod<41>, &mod<42>, &mod<43>, &mod<44>, &mod<45>, &mod<46>, &mod<47>, &mod<48>, &mod<49>, \n &mod<50>,\n#endif\n}};\n\n}\n\n/**\n * Grow the hash table by using prime numbers as bucket count. Slower than tsl::rh::power_of_two_growth_policy in \n * general but will probably distribute the values around better in the buckets with a poor hash function.\n * \n * To allow the compiler to optimize the modulo operation, a lookup table is used with constant primes numbers.\n * \n * With a switch the code would look like:\n * \\code\n * switch(iprime) { // iprime is the current prime of the hash table\n * case 0: hash % 5ul;\n * break;\n * case 1: hash % 17ul;\n * break;\n * case 2: hash % 29ul;\n * break;\n * ...\n * } \n * \\endcode\n * \n * Due to the constant variable in the modulo the compiler is able to optimize the operation\n * by a series of multiplications, substractions and shifts. \n * \n * The 'hash % 5' could become something like 'hash - (hash * 0xCCCCCCCD) >> 34) * 5' in a 64 bits environment.\n */\nclass prime_growth_policy {\npublic:\n explicit prime_growth_policy(std::size_t& min_bucket_count_in_out) {\n auto it_prime = std::lower_bound(detail::PRIMES.begin(), \n detail::PRIMES.end(), min_bucket_count_in_out);\n if(it_prime == detail::PRIMES.end()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n m_iprime = static_cast(std::distance(detail::PRIMES.begin(), it_prime));\n if(min_bucket_count_in_out > 0) {\n min_bucket_count_in_out = *it_prime;\n }\n else {\n min_bucket_count_in_out = 0;\n }\n }\n \n std::size_t bucket_for_hash(std::size_t hash) const noexcept {\n return detail::MOD_PRIME[m_iprime](hash);\n }\n \n std::size_t next_bucket_count() const {\n if(m_iprime + 1 >= detail::PRIMES.size()) {\n TSL_RH_THROW_OR_TERMINATE(std::length_error, \"The hash table exceeds its maximum size.\");\n }\n \n return detail::PRIMES[m_iprime + 1];\n } \n \n std::size_t max_bucket_count() const {\n return detail::PRIMES.back();\n }\n \n void clear() noexcept {\n m_iprime = 0;\n }\n \nprivate:\n unsigned int m_iprime;\n \n static_assert(std::numeric_limits::max() >= detail::PRIMES.size(), \n \"The type of m_iprime is not big enough.\");\n}; \n\n}\n}\n\n#endif\n"], ["/voxel_svio/include/mapPoint.h", "class mapPoint {\n public:\n mapPoint() {\n\n}\n size_t feature_id;\n int unique_camera_id = -1;\n int anchor_cam_id = -1;\n double anchor_clone_timestamp = -1;\n bool has_had_anchor_change = false;\n bool should_marg = false;\n int update_fail_count = 0;\n Eigen::Vector3d uv_normalized;\n Eigen::Vector3d uv_normalized_fej;\n float color;\n std::shared_ptr host_frame;\n short voxel_idx[3];\n void update(const Eigen::VectorXd &dx) override {\n\tassert(dx.rows() == size);\n\tsetValue(value_ + dx);\n}\n Eigen::Matrix getPointXYZ(bool get_fej) {\n\treturn (get_fej) ? fej() : value();\n}\n void setPointXYZ(Eigen::Matrix p_FinG, bool is_fej) {\n\tif (is_fej)\n\t\tsetFej(p_FinG);\n\telse\n\t\tsetValue(p_FinG);\n\t\n\treturn;\n}\n};"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_map.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_MAP_H\n#define TSL_ROBIN_MAP_H \n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \"robin_hash.h\"\n\n\nnamespace tsl {\n\n \n/**\n * Implementation of a hash map using open-addressing and the robin hood hashing algorithm with backward shift deletion.\n * \n * For operations modifying the hash map (insert, erase, rehash, ...), the strong exception guarantee \n * is only guaranteed when the expression `std::is_nothrow_swappable>::value &&\n * std::is_nothrow_move_constructible>::value` is true, otherwise if an exception\n * is thrown during the swap or the move, the hash map may end up in a undefined state. Per the standard\n * a `Key` or `T` with a noexcept copy constructor and no move constructor also satisfies the \n * `std::is_nothrow_move_constructible>::value` criterion (and will thus guarantee the \n * strong exception for the map).\n * \n * When `StoreHash` is true, 32 bits of the hash are stored alongside the values. It can improve \n * the performance during lookups if the `KeyEqual` function takes time (if it engenders a cache-miss for example) \n * as we then compare the stored hashes before comparing the keys. When `tsl::rh::power_of_two_growth_policy` is used\n * as `GrowthPolicy`, it may also speed-up the rehash process as we can avoid to recalculate the hash. \n * When it is detected that storing the hash will not incur any memory penalty due to alignment (i.e. \n * `sizeof(tsl::detail_robin_hash::bucket_entry) == \n * sizeof(tsl::detail_robin_hash::bucket_entry)`) and `tsl::rh::power_of_two_growth_policy` is\n * used, the hash will be stored even if `StoreHash` is false so that we can speed-up the rehash (but it will\n * not be used on lookups unless `StoreHash` is true).\n * \n * `GrowthPolicy` defines how the map grows and consequently how a hash value is mapped to a bucket. \n * By default the map uses `tsl::rh::power_of_two_growth_policy`. This policy keeps the number of buckets \n * to a power of two and uses a mask to map the hash to a bucket instead of the slow modulo.\n * Other growth policies are available and you may define your own growth policy, \n * check `tsl::rh::power_of_two_growth_policy` for the interface.\n * \n * `std::pair` must be swappable.\n * \n * `Key` and `T` must be copy and/or move constructible.\n * \n * If the destructor of `Key` or `T` throws an exception, the behaviour of the class is undefined.\n * \n * Iterators invalidation:\n * - clear, operator=, reserve, rehash: always invalidate the iterators.\n * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators.\n * - erase: always invalidate the iterators.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator>,\n bool StoreHash = false,\n class GrowthPolicy = tsl::rh::power_of_two_growth_policy<2>>\nclass robin_map {\nprivate:\n template\n using has_is_transparent = tsl::detail_robin_hash::has_is_transparent;\n \n class KeySelect {\n public:\n using key_type = Key;\n \n const key_type& operator()(const std::pair& key_value) const noexcept {\n return key_value.first;\n }\n \n key_type& operator()(std::pair& key_value) noexcept {\n return key_value.first;\n }\n }; \n \n class ValueSelect {\n public:\n using value_type = T;\n \n const value_type& operator()(const std::pair& key_value) const noexcept {\n return key_value.second;\n }\n \n value_type& operator()(std::pair& key_value) noexcept {\n return key_value.second;\n }\n };\n \n using ht = detail_robin_hash::robin_hash, KeySelect, ValueSelect,\n Hash, KeyEqual, Allocator, StoreHash, GrowthPolicy>; \n \npublic:\n using key_type = typename ht::key_type;\n using mapped_type = T;\n using value_type = typename ht::value_type;\n using size_type = typename ht::size_type;\n using difference_type = typename ht::difference_type;\n using hasher = typename ht::hasher;\n using key_equal = typename ht::key_equal;\n using allocator_type = typename ht::allocator_type;\n using reference = typename ht::reference;\n using const_reference = typename ht::const_reference;\n using pointer = typename ht::pointer;\n using const_pointer = typename ht::const_pointer;\n using iterator = typename ht::iterator;\n using const_iterator = typename ht::const_iterator;\n \n \npublic:\n /*\n * Constructors\n */\n robin_map(): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE) {\n }\n \n explicit robin_map(size_type bucket_count, \n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n m_ht(bucket_count, hash, equal, alloc)\n {\n }\n \n robin_map(size_type bucket_count,\n const Allocator& alloc): robin_map(bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n robin_map(size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_map(bucket_count, hash, KeyEqual(), alloc)\n {\n }\n \n explicit robin_map(const Allocator& alloc): robin_map(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {\n }\n \n template\n robin_map(InputIt first, InputIt last,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): robin_map(bucket_count, hash, equal, alloc)\n {\n insert(first, last);\n }\n \n template\n robin_map(InputIt first, InputIt last,\n size_type bucket_count,\n const Allocator& alloc): robin_map(first, last, bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n template\n robin_map(InputIt first, InputIt last,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_map(first, last, bucket_count, hash, KeyEqual(), alloc)\n {\n }\n\n robin_map(std::initializer_list init,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n robin_map(init.begin(), init.end(), bucket_count, hash, equal, alloc)\n {\n }\n\n robin_map(std::initializer_list init,\n size_type bucket_count,\n const Allocator& alloc): \n robin_map(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n\n robin_map(std::initializer_list init,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): \n robin_map(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)\n {\n }\n \n robin_map& operator=(std::initializer_list ilist) {\n m_ht.clear();\n \n m_ht.reserve(ilist.size());\n m_ht.insert(ilist.begin(), ilist.end());\n \n return *this;\n }\n \n allocator_type get_allocator() const { return m_ht.get_allocator(); }\n \n \n /*\n * Iterators\n */\n iterator begin() noexcept { return m_ht.begin(); }\n const_iterator begin() const noexcept { return m_ht.begin(); }\n const_iterator cbegin() const noexcept { return m_ht.cbegin(); }\n \n iterator end() noexcept { return m_ht.end(); }\n const_iterator end() const noexcept { return m_ht.end(); }\n const_iterator cend() const noexcept { return m_ht.cend(); }\n \n \n /*\n * Capacity\n */\n bool empty() const noexcept { return m_ht.empty(); }\n size_type size() const noexcept { return m_ht.size(); }\n size_type max_size() const noexcept { return m_ht.max_size(); }\n \n /*\n * Modifiers\n */\n void clear() noexcept { m_ht.clear(); }\n \n \n \n std::pair insert(const value_type& value) { \n return m_ht.insert(value); \n }\n \n template::value>::type* = nullptr>\n std::pair insert(P&& value) { \n return m_ht.emplace(std::forward

(value)); \n }\n \n std::pair insert(value_type&& value) { \n return m_ht.insert(std::move(value)); \n }\n \n \n iterator insert(const_iterator hint, const value_type& value) { \n return m_ht.insert_hint(hint, value); \n }\n \n template::value>::type* = nullptr>\n iterator insert(const_iterator hint, P&& value) { \n return m_ht.emplace_hint(hint, std::forward

(value));\n }\n \n iterator insert(const_iterator hint, value_type&& value) { \n return m_ht.insert_hint(hint, std::move(value)); \n }\n \n \n template\n void insert(InputIt first, InputIt last) { \n m_ht.insert(first, last); \n }\n \n void insert(std::initializer_list ilist) { \n m_ht.insert(ilist.begin(), ilist.end()); \n }\n\n \n \n \n template\n std::pair insert_or_assign(const key_type& k, M&& obj) { \n return m_ht.insert_or_assign(k, std::forward(obj)); \n }\n\n template\n std::pair insert_or_assign(key_type&& k, M&& obj) { \n return m_ht.insert_or_assign(std::move(k), std::forward(obj)); \n }\n \n template\n iterator insert_or_assign(const_iterator hint, const key_type& k, M&& obj) {\n return m_ht.insert_or_assign(hint, k, std::forward(obj));\n }\n \n template\n iterator insert_or_assign(const_iterator hint, key_type&& k, M&& obj) {\n return m_ht.insert_or_assign(hint, std::move(k), std::forward(obj));\n }\n \n \n \n /**\n * Due to the way elements are stored, emplace will need to move or copy the key-value once.\n * The method is equivalent to insert(value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n std::pair emplace(Args&&... args) { \n return m_ht.emplace(std::forward(args)...); \n }\n \n \n \n /**\n * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.\n * The method is equivalent to insert(hint, value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n iterator emplace_hint(const_iterator hint, Args&&... args) {\n return m_ht.emplace_hint(hint, std::forward(args)...);\n }\n \n \n \n \n template\n std::pair try_emplace(const key_type& k, Args&&... args) { \n return m_ht.try_emplace(k, std::forward(args)...);\n }\n \n template\n std::pair try_emplace(key_type&& k, Args&&... args) {\n return m_ht.try_emplace(std::move(k), std::forward(args)...);\n }\n \n template\n iterator try_emplace(const_iterator hint, const key_type& k, Args&&... args) {\n return m_ht.try_emplace_hint(hint, k, std::forward(args)...);\n }\n \n template\n iterator try_emplace(const_iterator hint, key_type&& k, Args&&... args) {\n return m_ht.try_emplace_hint(hint, std::move(k), std::forward(args)...);\n }\n \n \n\n \n iterator erase(iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }\n size_type erase(const key_type& key) { return m_ht.erase(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n size_type erase(const key_type& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type erase(const K& key) { return m_ht.erase(key); }\n \n /**\n * @copydoc erase(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n template::value>::type* = nullptr> \n size_type erase(const K& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n \n \n void swap(robin_map& other) { other.m_ht.swap(m_ht); }\n \n \n \n /*\n * Lookup\n */\n T& at(const Key& key) { return m_ht.at(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n T& at(const Key& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }\n \n \n const T& at(const Key& key) const { return m_ht.at(key); }\n \n /**\n * @copydoc at(const Key& key, std::size_t precalculated_hash)\n */\n const T& at(const Key& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }\n \n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n T& at(const K& key) { return m_ht.at(key); }\n\n /**\n * @copydoc at(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */ \n template::value>::type* = nullptr> \n T& at(const K& key, std::size_t precalculated_hash) { return m_ht.at(key, precalculated_hash); }\n \n \n /**\n * @copydoc at(const K& key)\n */\n template::value>::type* = nullptr> \n const T& at(const K& key) const { return m_ht.at(key); }\n \n /**\n * @copydoc at(const K& key, std::size_t precalculated_hash)\n */ \n template::value>::type* = nullptr> \n const T& at(const K& key, std::size_t precalculated_hash) const { return m_ht.at(key, precalculated_hash); }\n \n \n \n \n T& operator[](const Key& key) { return m_ht[key]; } \n T& operator[](Key&& key) { return m_ht[std::move(key)]; }\n \n \n \n \n size_type count(const Key& key) const { return m_ht.count(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n size_type count(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.count(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type count(const K& key) const { return m_ht.count(key); }\n \n /**\n * @copydoc count(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */ \n template::value>::type* = nullptr> \n size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }\n \n \n \n \n iterator find(const Key& key) { return m_ht.find(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n const_iterator find(const Key& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const Key& key, std::size_t precalculated_hash)\n */\n const_iterator find(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.find(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key) { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n /**\n * @copydoc find(const K& key)\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key, std::size_t precalculated_hash) const { \n return m_ht.find(key, precalculated_hash); \n }\n \n \n \n \n bool contains(const Key& key) const { return m_ht.contains(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n bool contains(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key) const { return m_ht.contains(key); }\n \n /**\n * @copydoc contains(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n \n \n \n std::pair equal_range(const Key& key) { return m_ht.equal_range(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n std::pair equal_range(const Key& key) const { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n\n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) { return m_ht.equal_range(key); }\n \n \n /**\n * @copydoc equal_range(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n /**\n * @copydoc equal_range(const K& key)\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) const { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const K& key, std::size_t precalculated_hash)\n */ \n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n \n \n \n /*\n * Bucket interface \n */\n size_type bucket_count() const { return m_ht.bucket_count(); }\n size_type max_bucket_count() const { return m_ht.max_bucket_count(); }\n \n \n /*\n * Hash policy \n */\n float load_factor() const { return m_ht.load_factor(); }\n \n float min_load_factor() const { return m_ht.min_load_factor(); }\n float max_load_factor() const { return m_ht.max_load_factor(); }\n \n /**\n * Set the `min_load_factor` to `ml`. When the `load_factor` of the map goes\n * below `min_load_factor` after some erase operations, the map will be\n * shrunk when an insertion occurs. The erase method itself never shrinks\n * the map.\n * \n * The default value of `min_load_factor` is 0.0f, the map never shrinks by default.\n */\n void min_load_factor(float ml) { m_ht.min_load_factor(ml); }\n void max_load_factor(float ml) { m_ht.max_load_factor(ml); }\n \n void rehash(size_type count) { m_ht.rehash(count); }\n void reserve(size_type count) { m_ht.reserve(count); }\n \n \n /*\n * Observers\n */\n hasher hash_function() const { return m_ht.hash_function(); }\n key_equal key_eq() const { return m_ht.key_eq(); }\n \n /*\n * Other\n */\n \n /**\n * Convert a const_iterator to an iterator.\n */\n iterator mutable_iterator(const_iterator pos) {\n return m_ht.mutable_iterator(pos);\n }\n \n friend bool operator==(const robin_map& lhs, const robin_map& rhs) {\n if(lhs.size() != rhs.size()) {\n return false;\n }\n \n for(const auto& element_lhs: lhs) {\n const auto it_element_rhs = rhs.find(element_lhs.first);\n if(it_element_rhs == rhs.cend() || element_lhs.second != it_element_rhs->second) {\n return false;\n }\n }\n \n return true;\n }\n\n friend bool operator!=(const robin_map& lhs, const robin_map& rhs) {\n return !operator==(lhs, rhs);\n }\n\n friend void swap(robin_map& lhs, robin_map& rhs) {\n lhs.swap(rhs);\n }\n \nprivate:\n ht m_ht;\n};\n\n\n/**\n * Same as `tsl::robin_map`.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator>,\n bool StoreHash = false>\nusing robin_pg_map = robin_map;\n\n} // end namespace tsl\n\n#endif\n"], ["/voxel_svio/thirdLibrary/tessil-src/include/tsl/robin_set.h", "/**\n * MIT License\n * \n * Copyright (c) 2017 Thibaut Goetghebuer-Planchon \n * \n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * \n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * \n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n#ifndef TSL_ROBIN_SET_H\n#define TSL_ROBIN_SET_H\n\n\n#include \n#include \n#include \n#include \n#include \n#include \n#include \"robin_hash.h\"\n\n\nnamespace tsl {\n\n \n/**\n * Implementation of a hash set using open-addressing and the robin hood hashing algorithm with backward shift deletion.\n * \n * For operations modifying the hash set (insert, erase, rehash, ...), the strong exception guarantee \n * is only guaranteed when the expression `std::is_nothrow_swappable::value &&\n * std::is_nothrow_move_constructible::value` is true, otherwise if an exception\n * is thrown during the swap or the move, the hash set may end up in a undefined state. Per the standard\n * a `Key` with a noexcept copy constructor and no move constructor also satisfies the \n * `std::is_nothrow_move_constructible::value` criterion (and will thus guarantee the \n * strong exception for the set).\n * \n * When `StoreHash` is true, 32 bits of the hash are stored alongside the values. It can improve \n * the performance during lookups if the `KeyEqual` function takes time (or engenders a cache-miss for example) \n * as we then compare the stored hashes before comparing the keys. When `tsl::rh::power_of_two_growth_policy` is used\n * as `GrowthPolicy`, it may also speed-up the rehash process as we can avoid to recalculate the hash. \n * When it is detected that storing the hash will not incur any memory penalty due to alignment (i.e. \n * `sizeof(tsl::detail_robin_hash::bucket_entry) == \n * sizeof(tsl::detail_robin_hash::bucket_entry)`) and `tsl::rh::power_of_two_growth_policy` is\n * used, the hash will be stored even if `StoreHash` is false so that we can speed-up the rehash (but it will\n * not be used on lookups unless `StoreHash` is true).\n * \n * `GrowthPolicy` defines how the set grows and consequently how a hash value is mapped to a bucket. \n * By default the set uses `tsl::rh::power_of_two_growth_policy`. This policy keeps the number of buckets \n * to a power of two and uses a mask to set the hash to a bucket instead of the slow modulo.\n * Other growth policies are available and you may define your own growth policy, \n * check `tsl::rh::power_of_two_growth_policy` for the interface.\n * \n * `Key` must be swappable.\n * \n * `Key` must be copy and/or move constructible.\n * \n * If the destructor of `Key` throws an exception, the behaviour of the class is undefined.\n * \n * Iterators invalidation:\n * - clear, operator=, reserve, rehash: always invalidate the iterators.\n * - insert, emplace, emplace_hint, operator[]: if there is an effective insert, invalidate the iterators.\n * - erase: always invalidate the iterators.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator,\n bool StoreHash = false,\n class GrowthPolicy = tsl::rh::power_of_two_growth_policy<2>>\nclass robin_set {\nprivate:\n template\n using has_is_transparent = tsl::detail_robin_hash::has_is_transparent;\n \n class KeySelect {\n public:\n using key_type = Key;\n \n const key_type& operator()(const Key& key) const noexcept {\n return key;\n }\n \n key_type& operator()(Key& key) noexcept {\n return key;\n }\n };\n \n using ht = detail_robin_hash::robin_hash;\n \npublic:\n using key_type = typename ht::key_type;\n using value_type = typename ht::value_type;\n using size_type = typename ht::size_type;\n using difference_type = typename ht::difference_type;\n using hasher = typename ht::hasher;\n using key_equal = typename ht::key_equal;\n using allocator_type = typename ht::allocator_type;\n using reference = typename ht::reference;\n using const_reference = typename ht::const_reference;\n using pointer = typename ht::pointer;\n using const_pointer = typename ht::const_pointer;\n using iterator = typename ht::iterator;\n using const_iterator = typename ht::const_iterator;\n\n \n /*\n * Constructors\n */\n robin_set(): robin_set(ht::DEFAULT_INIT_BUCKETS_SIZE) {\n }\n \n explicit robin_set(size_type bucket_count, \n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n m_ht(bucket_count, hash, equal, alloc)\n {\n }\n \n robin_set(size_type bucket_count,\n const Allocator& alloc): robin_set(bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n robin_set(size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_set(bucket_count, hash, KeyEqual(), alloc)\n {\n }\n \n explicit robin_set(const Allocator& alloc): robin_set(ht::DEFAULT_INIT_BUCKETS_SIZE, alloc) {\n }\n \n template\n robin_set(InputIt first, InputIt last,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): robin_set(bucket_count, hash, equal, alloc)\n {\n insert(first, last);\n }\n \n template\n robin_set(InputIt first, InputIt last,\n size_type bucket_count,\n const Allocator& alloc): robin_set(first, last, bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n \n template\n robin_set(InputIt first, InputIt last,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): robin_set(first, last, bucket_count, hash, KeyEqual(), alloc)\n {\n }\n\n robin_set(std::initializer_list init,\n size_type bucket_count = ht::DEFAULT_INIT_BUCKETS_SIZE,\n const Hash& hash = Hash(),\n const KeyEqual& equal = KeyEqual(),\n const Allocator& alloc = Allocator()): \n robin_set(init.begin(), init.end(), bucket_count, hash, equal, alloc)\n {\n }\n\n robin_set(std::initializer_list init,\n size_type bucket_count,\n const Allocator& alloc): \n robin_set(init.begin(), init.end(), bucket_count, Hash(), KeyEqual(), alloc)\n {\n }\n\n robin_set(std::initializer_list init,\n size_type bucket_count,\n const Hash& hash,\n const Allocator& alloc): \n robin_set(init.begin(), init.end(), bucket_count, hash, KeyEqual(), alloc)\n {\n }\n\n \n robin_set& operator=(std::initializer_list ilist) {\n m_ht.clear();\n \n m_ht.reserve(ilist.size());\n m_ht.insert(ilist.begin(), ilist.end());\n \n return *this;\n }\n \n allocator_type get_allocator() const { return m_ht.get_allocator(); }\n \n \n /*\n * Iterators\n */\n iterator begin() noexcept { return m_ht.begin(); }\n const_iterator begin() const noexcept { return m_ht.begin(); }\n const_iterator cbegin() const noexcept { return m_ht.cbegin(); }\n \n iterator end() noexcept { return m_ht.end(); }\n const_iterator end() const noexcept { return m_ht.end(); }\n const_iterator cend() const noexcept { return m_ht.cend(); }\n \n \n /*\n * Capacity\n */\n bool empty() const noexcept { return m_ht.empty(); }\n size_type size() const noexcept { return m_ht.size(); }\n size_type max_size() const noexcept { return m_ht.max_size(); }\n \n /*\n * Modifiers\n */\n void clear() noexcept { m_ht.clear(); }\n \n \n \n \n std::pair insert(const value_type& value) { \n return m_ht.insert(value); \n }\n \n std::pair insert(value_type&& value) { \n return m_ht.insert(std::move(value)); \n }\n \n iterator insert(const_iterator hint, const value_type& value) { \n return m_ht.insert_hint(hint, value); \n }\n \n iterator insert(const_iterator hint, value_type&& value) { \n return m_ht.insert_hint(hint, std::move(value)); \n }\n \n template\n void insert(InputIt first, InputIt last) { \n m_ht.insert(first, last);\n }\n \n void insert(std::initializer_list ilist) { \n m_ht.insert(ilist.begin(), ilist.end()); \n }\n\n \n \n \n /**\n * Due to the way elements are stored, emplace will need to move or copy the key-value once.\n * The method is equivalent to insert(value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n std::pair emplace(Args&&... args) { \n return m_ht.emplace(std::forward(args)...); \n }\n \n \n \n /**\n * Due to the way elements are stored, emplace_hint will need to move or copy the key-value once.\n * The method is equivalent to insert(hint, value_type(std::forward(args)...));\n * \n * Mainly here for compatibility with the std::unordered_map interface.\n */\n template\n iterator emplace_hint(const_iterator hint, Args&&... args) {\n return m_ht.emplace_hint(hint, std::forward(args)...);\n }\n \n \n \n iterator erase(iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator pos) { return m_ht.erase(pos); }\n iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }\n size_type erase(const key_type& key) { return m_ht.erase(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n size_type erase(const key_type& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type erase(const K& key) { return m_ht.erase(key); }\n \n /**\n * @copydoc erase(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.\n */ \n template::value>::type* = nullptr> \n size_type erase(const K& key, std::size_t precalculated_hash) { \n return m_ht.erase(key, precalculated_hash); \n }\n \n \n \n void swap(robin_set& other) { other.m_ht.swap(m_ht); }\n \n \n \n /*\n * Lookup\n */\n size_type count(const Key& key) const { return m_ht.count(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n size_type count(const Key& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n size_type count(const K& key) const { return m_ht.count(key); }\n \n /**\n * @copydoc count(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n size_type count(const K& key, std::size_t precalculated_hash) const { return m_ht.count(key, precalculated_hash); }\n \n \n \n \n iterator find(const Key& key) { return m_ht.find(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n iterator find(const Key& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n const_iterator find(const Key& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const Key& key, std::size_t precalculated_hash)\n */\n const_iterator find(const Key& key, std::size_t precalculated_hash) const { return m_ht.find(key, precalculated_hash); }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key) { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n iterator find(const K& key, std::size_t precalculated_hash) { return m_ht.find(key, precalculated_hash); }\n \n /**\n * @copydoc find(const K& key)\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key) const { return m_ht.find(key); }\n \n /**\n * @copydoc find(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n const_iterator find(const K& key, std::size_t precalculated_hash) const { return m_ht.find(key, precalculated_hash); }\n \n \n \n \n bool contains(const Key& key) const { return m_ht.contains(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n bool contains(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key) const { return m_ht.contains(key); }\n \n /**\n * @copydoc contains(const K& key) const\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n template::value>::type* = nullptr> \n bool contains(const K& key, std::size_t precalculated_hash) const { \n return m_ht.contains(key, precalculated_hash); \n }\n \n \n \n \n std::pair equal_range(const Key& key) { return m_ht.equal_range(key); }\n \n /**\n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n std::pair equal_range(const Key& key) const { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const Key& key, std::size_t precalculated_hash)\n */\n std::pair equal_range(const Key& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n /**\n * This overload only participates in the overload resolution if the typedef KeyEqual::is_transparent exists. \n * If so, K must be hashable and comparable to Key.\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) { return m_ht.equal_range(key); }\n \n /**\n * @copydoc equal_range(const K& key)\n * \n * Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same\n * as hash_function()(key). Useful to speed-up the lookup if you already have the hash.\n */ \n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n /**\n * @copydoc equal_range(const K& key)\n */\n template::value>::type* = nullptr> \n std::pair equal_range(const K& key) const { return m_ht.equal_range(key); }\n\n /**\n * @copydoc equal_range(const K& key, std::size_t precalculated_hash)\n */ \n template::value>::type* = nullptr> \n std::pair equal_range(const K& key, std::size_t precalculated_hash) const { \n return m_ht.equal_range(key, precalculated_hash); \n }\n \n \n \n\n /*\n * Bucket interface \n */\n size_type bucket_count() const { return m_ht.bucket_count(); }\n size_type max_bucket_count() const { return m_ht.max_bucket_count(); }\n \n \n /*\n * Hash policy \n */\n float load_factor() const { return m_ht.load_factor(); }\n \n float min_load_factor() const { return m_ht.min_load_factor(); }\n float max_load_factor() const { return m_ht.max_load_factor(); }\n \n /**\n * Set the `min_load_factor` to `ml`. When the `load_factor` of the set goes\n * below `min_load_factor` after some erase operations, the set will be\n * shrunk when an insertion occurs. The erase method itself never shrinks\n * the set.\n * \n * The default value of `min_load_factor` is 0.0f, the set never shrinks by default.\n */\n void min_load_factor(float ml) { m_ht.min_load_factor(ml); }\n void max_load_factor(float ml) { m_ht.max_load_factor(ml); }\n \n void rehash(size_type count) { m_ht.rehash(count); }\n void reserve(size_type count) { m_ht.reserve(count); }\n \n \n /*\n * Observers\n */\n hasher hash_function() const { return m_ht.hash_function(); }\n key_equal key_eq() const { return m_ht.key_eq(); }\n \n \n /*\n * Other\n */\n \n /**\n * Convert a const_iterator to an iterator.\n */\n iterator mutable_iterator(const_iterator pos) {\n return m_ht.mutable_iterator(pos);\n }\n \n friend bool operator==(const robin_set& lhs, const robin_set& rhs) {\n if(lhs.size() != rhs.size()) {\n return false;\n }\n \n for(const auto& element_lhs: lhs) {\n const auto it_element_rhs = rhs.find(element_lhs);\n if(it_element_rhs == rhs.cend()) {\n return false;\n }\n }\n \n return true;\n }\n\n friend bool operator!=(const robin_set& lhs, const robin_set& rhs) {\n return !operator==(lhs, rhs);\n }\n\n friend void swap(robin_set& lhs, robin_set& rhs) {\n lhs.swap(rhs);\n }\n \nprivate:\n ht m_ht; \n};\n\n\n/**\n * Same as `tsl::robin_set`.\n */\ntemplate,\n class KeyEqual = std::equal_to,\n class Allocator = std::allocator,\n bool StoreHash = false>\nusing robin_pg_set = robin_set;\n\n} // end namespace tsl\n\n#endif\n \n"], ["/voxel_svio/include/utility.h", "class gammaPixel {\n public:\n float Binv[256];\n float B[256];\n gammaPixel() {\n for (int i = 0; i < 256; i++)\n Binv[i] = B[i] = i;\n}\n float getBGradOnly(float color) {\n int c = color + 0.5f;\n if(c < 5) c = 5;\n if(c > 250) c = 250;\n return B[c + 1] - B[c];\n}\n float getBInvGradOnly(float color) {\n int c = color + 0.5f;\n if(c < 5) c = 5;\n if(c > 250) c = 250;\n return Binv[c + 1] - Binv[c];\n}\n public:\n private:\n std::function body;\n};"], ["/voxel_svio/include/sensorData.h", "class sensorData {\n};"]], "task_instance_info": {"created_time": "2025-08-20 20:09:18", "created_task_model": "DeepSeek-R1", "function_skeleton": "pixelSelector(int w, int h) {}", "code_description": "This code segment functions to: Initialize data structures for gradient-based pixel selection by allocating memory for randomized sampling patterns, histogram buffers, and threshold values while setting initial parameters for spatial subdivision and processing efficiency."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```cpp\n[TASK_BEGIN]\npixelSelector(int w, int h) {\n current_potential = 3;\n allow_fast = true;\n \n random_pattern = new unsigned char[w*h];\n for(int i=0;i