1 #ifndef bstm_multi_block_metadata_h_
2 #define bstm_multi_block_metadata_h_
3 //:
4 // \file bstm_multi_block_metadata.h
5 // \brief  bstm_multi_block_metadata data keeps track of non structure, non
6 // visual data for bstm_multi_block. In addition, this also keeps track of the
7 // number and order of space/time subdivisions in the block.
8 //
9 // \author Raphael Kargon
10 // \date   31 Jul 2017
11 //
12 
13 #include <iostream>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 #include <vbl/vbl_ref_count.h>
18 #ifdef _MSC_VER
19 #  include <vcl_msvc_warnings.h>
20 #endif
21 #include <vgl/vgl_box_3d.h>
22 #include <vgl/vgl_point_3d.h>
23 #include <vgl/vgl_vector_3d.h>
24 #include <vsl/vsl_basic_xml_element.h>
25 
26 #include <boxm2/boxm2_block_metadata.h>
27 #include <bstm/basic/bstm_block_id.h>
28 #include <bstm/bstm_block_metadata.h>
29 #include <bstm_multi/bstm_multi_tree_util.h>
30 
31 
32 class bstm_multi_block_metadata : public vbl_ref_count {
33 public:
34   // default constructor does not initialize anything
35   bstm_multi_block_metadata() = default;
36   bstm_multi_block_metadata(const bstm_block_id &id,
37                             const vgl_box_3d<double> &bbox,
38                             std::pair<double, double> bbox_t,
39                             double max_mb,
40                             double p_init,
41                             std::vector<space_time_enum> subdivisions,
42                             int version = 1)
id_(id)43       : id_(id)
44       , bbox_(bbox)
45       , bbox_t_(std::move(bbox_t))
46       , max_mb_(max_mb)
47       , p_init_(p_init)
48       , version_(version)
49       , subdivisions_(std::move(subdivisions)) {}
50 
bstm_multi_block_metadata(const bstm_multi_block_metadata & that)51   bstm_multi_block_metadata(const bstm_multi_block_metadata &that)
52       : vbl_ref_count()
53       , id_(that.id_)
54       , bbox_(that.bbox_)
55       , bbox_t_(that.bbox_t_)
56       , max_mb_(that.max_mb_)
57       , p_init_(that.p_init_)
58       , version_(that.version_)
59       , subdivisions_(that.subdivisions_) {}
60 
61   //: id and local origin of the block
62   bstm_block_id id_;
63   vgl_box_3d<double> bbox_;
64 
65   //: time bounds
66   std::pair<double, double> bbox_t_;
67 
68   // TODO: Should we allow a flat array of sub-blocks at the highest level, or
69   // require one root tree for every block?
70   // //: dimension of time axis, e.g., 1s
71   // double   sub_block_dim_t_;
72 
73   // //: number of sub-blocks time dimension
74   // unsigned   sub_block_num_t_;
75 
76   // //: World dimensions of a sub block .e.g 1 meter x 1 meter x 1 meter
77   // vgl_vector_3d<double>   sub_block_dim_;
78 
79   // //: number of sub blocks in each dimension
80   // vgl_vector_3d<unsigned> sub_block_num_;
81 
82   double max_mb_; // each total block mb
83   double p_init_; // initialize occupancy probs with this
84   int version_;
85 
86   //: Describes order of hierarchical subdivisions of block: e.g. time first,
87   // then space, etc.
88   std::vector<space_time_enum> subdivisions_;
89 
90   //: checks if this block contains the given t, if yes, returns local_time
91   bool contains_t(double const t, double &local_time) const;
92 
93   //: bounding box for this block
bbox()94   vgl_box_3d<double> bbox() const { return bbox_; }
bbox_t(double & min_t,double & max_t)95   void bbox_t(double &min_t, double &max_t) const {
96     min_t = bbox_t_.first;
97     max_t = bbox_t_.second;
98   }
bbox_t()99   std::pair<double, double> bbox_t() const { return bbox_t_; }
100   // Returns size of smallest space voxel and time step representable by this
101   // block.
102   std::pair<vgl_vector_3d<double>, double> resolution() const;
103 
104   bool operator==(bstm_multi_block_metadata const &m) const;
105 
106   template <typename Metadata> bool operator==(Metadata const &m) const;
107 
108   //: Writes this block's metadata to an XML element which can later
109   // be written to a file, e.g. as part of a scene.
110   void to_xml(vsl_basic_xml_element &block) const;
111 
112   //: Load a block from XML attributes.
113   // \param atts - XML attributes for this block. Given as an array of char*'s,
114   // where attribute name and values alternate in the array.
115   static bstm_multi_block_metadata from_xml(const char **atts);
116 };
117 
118 //: Smart_Pointer typedef for bstm_block
119 typedef vbl_smart_ptr<bstm_multi_block_metadata> bstm_multi_block_metadata_sptr;
120 
121 std::ostream &operator<<(std::ostream &s, bstm_multi_block_metadata &metadata);
122 
123 //: Binary write boxm_update_bit_scene_manager scene to stream
124 void vsl_b_write(vsl_b_ostream &os, bstm_multi_block_metadata const &scene);
125 void vsl_b_write(vsl_b_ostream &os, const bstm_multi_block_metadata *&p);
126 void vsl_b_write(vsl_b_ostream &os, bstm_multi_block_metadata_sptr &sptr);
127 void vsl_b_write(vsl_b_ostream &os, bstm_multi_block_metadata_sptr const &sptr);
128 
129 //: Binary load boxm_update_bit_scene_manager scene from stream.
130 void vsl_b_read(vsl_b_istream &is, bstm_multi_block_metadata &scene);
131 void vsl_b_read(vsl_b_istream &is, bstm_multi_block_metadata *p);
132 void vsl_b_read(vsl_b_istream &is, bstm_multi_block_metadata_sptr &sptr);
133 void vsl_b_read(vsl_b_istream &is, bstm_multi_block_metadata_sptr const &sptr);
134 
135 // Compares spatial sizes of smallest regions of space representable by these
136 // two blocks.
137 bool voxel_resolutions_match(const bstm_multi_block_metadata &metadata,
138                              const boxm2_block_metadata &boxm2_metadata);
139 
140 bool voxel_resolutions_match(const bstm_multi_block_metadata &metadata,
141                              const bstm_block_metadata &bstm_metadata);
142 
143 #endif
144