1 #include "volm_spherical_region.h"
2 
3 #define stringify( name ) # name
is_attribute(spherical_region_attributes att)4 bool volm_spherical_region::is_attribute(spherical_region_attributes att)
5 {
6     if (attributes_.find(att)!=attributes_.end())
7         return true;
8     else
9         return false;
10 }
11 
attribute_value(spherical_region_attributes att,unsigned char & value)12 bool volm_spherical_region::attribute_value(spherical_region_attributes att, unsigned char & value)
13 {
14     if (this->is_attribute(att))
15     {
16         value = attributes_[att];
17         return true;
18     }
19     else
20     {
21         value = 255;
22         return false;
23     }
24 }
25 
print(std::ostream & os)26 void volm_spherical_region::print(std::ostream& os)
27 {
28     os<<"Box "<<box_<<' ';
29     auto iter = attributes_.begin();
30     for (;iter!= attributes_.end();iter++)
31     {
32         os<<'('<<iter->first<<", "<<(int)iter->second<<") " ;
33     }
34     os<<'\n';
35 }
36 
attribute_types()37 std::vector<spherical_region_attributes> volm_spherical_region::attribute_types()
38 {
39     std::vector<spherical_region_attributes> attributes;
40     auto iter = attributes_.begin();
41     for (;iter!= attributes_.end();iter++)
42     {
43         attributes.push_back(iter->first);
44     }
45     return attributes;
46 }
47 
add_region(const volm_spherical_region & region)48 void volm_spherical_regions_layer::add_region(const volm_spherical_region& region)
49 {
50     regions_.push_back(region);
51     this->update_attribute_map(regions_.size()-1);
52 }
53 
54 
55 std::map<unsigned char,std::vector<unsigned int > >
attributed_regions_by_type(spherical_region_attributes att)56 volm_spherical_regions_layer::attributed_regions_by_type(spherical_region_attributes att)
57 {
58     if ( attributed_regions_.find(att)!= attributed_regions_.end())
59     {
60         return  attributed_regions_[att];
61     }
62     else // return an empty vector
63     {
64         std::map<unsigned char, std::vector<unsigned int> > ids;
65         return ids;
66     }
67 }
68 
69 std::vector<unsigned int >
attributed_regions_by_type_only(spherical_region_attributes att)70 volm_spherical_regions_layer::attributed_regions_by_type_only(spherical_region_attributes att)
71 {
72     if ( attributed_regions_.find(att)!= attributed_regions_.end())
73     {
74         auto iter = attributed_regions_[att].begin();
75         std::vector<unsigned int>  sids;
76         for (;iter!=attributed_regions_[att].end();iter++)
77             sids.insert(sids.begin(),iter->second.begin(),iter->second.end());
78         return  sids;
79     }
80     else // return an empty vector
81     {
82          std::vector<unsigned int>  ids;
83         return ids;
84     }
85 }
86 
87 std::vector<unsigned int>
attributed_regions_by_value(spherical_region_attributes att,unsigned char & val)88     volm_spherical_regions_layer::attributed_regions_by_value(spherical_region_attributes att,unsigned char & val)
89 {
90     std::map<unsigned char,std::vector<unsigned int > > temp = this->attributed_regions_by_type(att);
91 
92     if ( temp.find(val)!= temp.end())
93     {
94         return temp[val];
95     }
96     std::vector<unsigned int> ids;
97     return ids;
98 }
99 
update_attribute_map(int id)100 void volm_spherical_regions_layer::update_attribute_map(int id)
101 {
102     std::vector<spherical_region_attributes> attribute_types = regions_[id].attribute_types();
103     for (auto & attribute_type : attribute_types)
104     {
105         unsigned char val =255;
106         if (regions_[id].attribute_value(attribute_type,val))
107             attributed_regions_[attribute_type][val].push_back(id);
108     }
109 }
110