1 #ifndef bstm_scene_h_ 2 #define bstm_scene_h_ 3 //: 4 // \file 5 // \brief bstm scene models a very generic (dynamic) scene, only specifies dimensions 6 // \author Ali Osman Ulusoy 7 // \date 07 Aug 2012 8 // 9 #include <iostream> 10 #include <iosfwd> 11 #include <bstm/basic/bstm_block_id.h> 12 #include <bstm/bstm_block_metadata.h> 13 #include <vpgl/vpgl_lvcs.h> 14 #include <vgl/vgl_point_3d.h> 15 #include <vgl/vgl_box_3d.h> 16 #include <vgl/vgl_box_2d.h> 17 #ifdef _MSC_VER 18 # include <vcl_msvc_warnings.h> 19 #endif 20 #include <vul/vul_file.h> 21 22 //smart pointer stuff 23 #include <vbl/vbl_ref_count.h> 24 #include <vbl/vbl_smart_ptr.h> 25 26 //vpgl camera 27 #include <vpgl/vpgl_generic_camera.h> 28 #include <vpgl/vpgl_perspective_camera.h> 29 30 //: bstm_scene_scene: simple scene model that maintains (in world coordinates) 31 // - scene origin 32 // - number of blocks in each dimension 33 // - size of each block in each dimension 34 // - lvcs information 35 // - xml path on disk and data path (directory) on disk 36 class bstm_scene : public vbl_ref_count 37 { 38 public: 39 //: empty scene, needs to be initialized manually 40 bstm_scene() = default; 41 42 bstm_scene(std::string data_path, vgl_point_3d<double> const& origin, int version = 2); 43 44 //: initializes scene from xmlFile 45 bstm_scene(const std::string& filename); 46 47 //: destructor 48 ~bstm_scene() override = default; 49 50 //: save scene xml file 51 void save_scene(); 52 53 //: return a vector of block ids in visibility order 54 std::vector<bstm_block_id> get_vis_blocks(vpgl_generic_camera<double>* cam); 55 std::vector<bstm_block_id> get_vis_blocks(vpgl_perspective_camera<double>* cam); get_vis_blocks(vpgl_camera_double_sptr & cam)56 std::vector<bstm_block_id> get_vis_blocks(vpgl_camera_double_sptr & cam) { 57 if ( cam->type_name() == "vpgl_generic_camera" ) 58 return this->get_vis_blocks( (vpgl_generic_camera<double>*) cam.ptr() ); 59 else if ( cam->type_name() == "vpgl_perspective_camera" ) 60 return this->get_vis_blocks( (vpgl_perspective_camera<double>*) cam.ptr() ); 61 else 62 std::cout<<"bstm_scene::get_vis_blocks doesn't support camera type "<<cam->type_name()<<std::endl; 63 //else return empty 64 std::vector<bstm_block_id> empty; 65 return empty; 66 } 67 //: visibility order from point, blocks must intersect with cam box 68 std::vector<bstm_block_id> 69 get_vis_order_from_pt(vgl_point_3d<double> const& pt, vgl_box_2d<double> camBox = vgl_box_2d<double>()); 70 71 //: return a heap pointer to a scene info block_exists(bstm_block_id id)72 bool block_exists(bstm_block_id id) const { return blocks_.find(id) != blocks_.end(); } block_on_disk(bstm_block_id id)73 bool block_on_disk(bstm_block_id id) const { return vul_file::exists( data_path_ + id.to_string() + ".bin"); } data_on_disk(bstm_block_id id,std::string data_type)74 bool data_on_disk(bstm_block_id id, std::string data_type) { 75 return vul_file::exists(data_path_ + data_type + "_" + id.to_string() + ".bin"); 76 } 77 78 //: a list of block metadata... blocks()79 std::map<bstm_block_id, bstm_block_metadata>& blocks() { return blocks_; } num_blocks()80 unsigned num_blocks() const { return (unsigned) blocks_.size(); } 81 82 //: mutable reference get_block_metadata(bstm_block_id id)83 bstm_block_metadata& get_block_metadata(bstm_block_id id) { return blocks_[id]; } 84 //: const so return a copy 85 bstm_block_metadata get_block_metadata_const(const bstm_block_id& id) const; 86 87 88 std::vector<bstm_block_id> get_block_ids() const; 89 90 //: returns the block ids of blocks that intersect the given bounding box at given time, as well as the local time 91 std::vector<bstm_block_id> get_block_ids(vgl_box_3d<double> bb, float time) const; 92 93 94 //: gets a tight bounding box for the scene 95 vgl_box_3d<double> bounding_box() const; 96 //: gets a tight bounding box for the scene 97 vgl_box_3d<int> bounding_box_blk_ids() const; 98 99 //: gets a tight bounding box for the scene 100 void bounding_box_t(double& min_t, double& max_t) const; 101 102 //: gets a tight bounding box of the block ids 103 void blocks_ids_bounding_box_t(unsigned& min_block_id, unsigned& max_block_id) const; 104 105 // returns the dimesnsion of the scene grid where each grid element is a block 106 vgl_vector_3d<unsigned int> scene_dimensions() const; 107 108 //: If a block contains a 3-d point, set the block id, else return false. The local coordinates of the point are also returned 109 bool contains(vgl_point_3d<double> const& p, bstm_block_id& bid, vgl_point_3d<double>& local_coords, double const t, double& local_time) const; 110 111 //: returns the local time if t is contained in scene 112 bool local_time(double const t, double& local_time) const; 113 114 115 //: scene dimensions accessors local_origin()116 vgl_point_3d<double> local_origin()const { return local_origin_; } rpc_origin()117 vgl_point_3d<double> rpc_origin() const { return rpc_origin_; } lvcs()118 vpgl_lvcs lvcs() const { return lvcs_; } 119 120 //: scene path accessors xml_path()121 std::string xml_path() const { return xml_path_; } data_path()122 std::string data_path() const { return data_path_; } 123 124 //: appearance model accessor appearances()125 std::vector<std::string> appearances() const { return appearances_; } 126 bool has_data_type(const std::string& data_type); 127 128 //: scene version number version()129 int version() { return version_; } set_version(int v)130 void set_version(int v) { version_ = v; } 131 132 //: scene mutators set_local_origin(vgl_point_3d<double> org)133 void set_local_origin(vgl_point_3d<double> org) { local_origin_ = org; } set_rpc_origin(vgl_point_3d<double> rpc)134 void set_rpc_origin(vgl_point_3d<double> rpc) { rpc_origin_ = rpc; } set_lvcs(vpgl_lvcs lvcs)135 void set_lvcs(vpgl_lvcs lvcs) { lvcs_ = lvcs; } set_blocks(std::map<bstm_block_id,bstm_block_metadata> blocks)136 void set_blocks(std::map<bstm_block_id, bstm_block_metadata> blocks) { blocks_ = blocks; } 137 void add_block_metadata(bstm_block_metadata data); set_appearances(std::vector<std::string> const & appearances)138 void set_appearances(std::vector<std::string> const& appearances){ this->appearances_ = appearances; } 139 140 //: scene path mutators set_xml_path(std::string path)141 void set_xml_path(std::string path) { xml_path_ = path; } set_data_path(std::string path)142 void set_data_path(std::string path) { data_path_ = path+"/"; } 143 144 private: 145 146 //: world scene information 147 vpgl_lvcs lvcs_; 148 vgl_point_3d<double> local_origin_; 149 vgl_point_3d<double> rpc_origin_; 150 151 //: location on disk of xml file and data/block files 152 std::string data_path_, xml_path_; 153 154 //: list of block meta data available to this scene 155 std::map<bstm_block_id, bstm_block_metadata> blocks_; 156 157 //: list of appearance models/observation models used by this scene 158 std::vector<std::string> appearances_; 159 int version_; 160 }; 161 162 163 //: utility class for sorting id's by their distance 164 class bstm_dist_id_pair 165 { 166 public: bstm_dist_id_pair(double dist,bstm_block_id id)167 bstm_dist_id_pair(double dist, bstm_block_id id) : dist_(dist), id_(id) {} 168 double dist_; 169 bstm_block_id id_; 170 171 inline bool operator < (bstm_dist_id_pair const& v) const { 172 return dist_ < v.dist_; 173 } 174 }; 175 176 typedef vbl_smart_ptr<bstm_scene> bstm_scene_sptr; 177 178 //: scene output stream operator 179 std::ostream& operator<<(std::ostream &s, bstm_scene& scene); 180 181 //: scene xml write function 182 void x_write(std::ostream &os, bstm_scene& scene, std::string name); 183 184 185 //--- IO read/write for sptrs-------------------------------------------------- 186 //: Binary write bstm_scene scene to stream 187 void vsl_b_write(vsl_b_ostream& os, bstm_scene const& scene); 188 void vsl_b_write(vsl_b_ostream& os, const bstm_scene* &p); 189 void vsl_b_write(vsl_b_ostream& os, bstm_scene_sptr& sptr); 190 void vsl_b_write(vsl_b_ostream& os, bstm_scene_sptr const& sptr); 191 192 //: Binary load bstm_scene scene from stream. 193 void vsl_b_read(vsl_b_istream& is, bstm_scene &scene); 194 void vsl_b_read(vsl_b_istream& is, bstm_scene* p); 195 void vsl_b_read(vsl_b_istream& is, bstm_scene_sptr& sptr); 196 void vsl_b_read(vsl_b_istream& is, bstm_scene_sptr const& sptr); 197 198 199 #endif // bstm_scene_h_ 200