1 //:
2 // \file
3 // \brief Parses the configuration file for bwm tool.
4 //
5 #include <sstream>
6 #include <cstring>
7 #include <iostream>
8 #include <cstdio>
9 #include "bstm_scene_parser.h"
10 
11 #ifdef _MSC_VER
12 #  include "vcl_msvc_warnings.h"
13 #endif
14 
15 // --------------
16 // --- PARSER ---
17 // --------------
18 template <typename T>
convert(const char * t,T & d)19 void convert(const char* t, T& d)
20 {
21   std::stringstream strm(t);
22   strm >> d;
23 }
24 
bstm_scene_parser()25 bstm_scene_parser::bstm_scene_parser()
26 {
27   init_params();
28 }
29 
lvcs(vpgl_lvcs & lvcs)30 bool bstm_scene_parser::lvcs(vpgl_lvcs& lvcs)
31 {
32   vpgl_lvcs::cs_names cs_name = vpgl_lvcs::str_to_enum(lvcs_cs_name_.data());
33   vpgl_lvcs::LenUnits len_unit;
34   vpgl_lvcs::AngUnits geo_unit;
35    if (std::strcmp(lvcs_XYZ_unit_.data(), "feet")==0) {
36      len_unit = vpgl_lvcs::FEET;
37    }
38    else if (std::strcmp(lvcs_XYZ_unit_.data(), "meters")==0) {
39      len_unit = vpgl_lvcs::METERS;
40    }
41    else {
42      std::cout << "LVCS Length Unit " << lvcs_XYZ_unit_ << " is not valid\n";
43      return false;
44    }
45 
46    if (std::strcmp(lvcs_geo_angle_unit_.data(), "radians")==0) {
47      geo_unit = vpgl_lvcs::RADIANS;
48    }
49    else if (std::strcmp(lvcs_geo_angle_unit_.data(), "degrees")==0) {
50      geo_unit = vpgl_lvcs::DEG;
51    }
52    else {
53      std::cout << "LVCS Geo Angle Unit " << lvcs_geo_angle_unit_ << " is not valid\n";
54      return false;
55    }
56 
57    lvcs = vpgl_lvcs(lvcs_origin_lat_,lvcs_origin_lon_,lvcs_origin_elev_, cs_name,
58                     lvcs_lat_scale_, lvcs_lon_scale_, geo_unit, len_unit,
59                     lvcs_local_origin_x_, lvcs_local_origin_y_, lvcs_theta_);
60   return true;
61 }
62 
init_params()63 void bstm_scene_parser::init_params()
64 {
65   lvcs_cs_name_="";
66   lvcs_origin_lon_=0;
67   lvcs_origin_lat_=0;
68   lvcs_origin_elev_=0;
69   lvcs_lon_scale_=0;
70   lvcs_lat_scale_=0;
71   lvcs_XYZ_unit_="";
72   lvcs_geo_angle_unit_="";
73   lvcs_local_origin_x_=0;
74   lvcs_local_origin_y_=0;
75   lvcs_theta_=0;
76   // world origin
77   origin_ = vgl_point_3d<double>(0,0,0);
78   path_="";
79   name_="";
80   version_ = 1;
81 }
82 
83 
84 //-----------------------------------------------------------------------------
85 //: Start Element needs to parse the following tags
86 // - scene level metadata
87 //   * LVCS_TAG "lvcs"
88 //   * LOCAL_ORIGIN_TAG "local_origin"
89 //   * SCENE_PATHS_TAG "scene_paths"
90 // - block level metadata
91 //   * BLOCK_TAG "block"
92 //   * BLOCK_ID_TAG "block_id"
93 //   * BLOCK_ORIGIN_TAG "block_origin"
94 //   * SUB_BLOCK_DIMENSIONS_TAG "sub_block_dimensions"
95 //   * TREE_INIT_LEVEL_TAG "tree_init_level"
96 //   * TREE_MAX_LEVEL_TAG "tree_max_level"
97 //   * P_INIT_TAG "p_init"
98 //   * MAX_MB_TAG "max_mb"
99 void
startElement(const char * name,const XML_Char ** atts)100 bstm_scene_parser::startElement(const char* name, const XML_Char** atts)
101 {
102 #if 0
103   std::cout<< "element=" << name << '\n'
104           << "  Attr=" << atts[i] << "->" << atts[i+1] << std::endl;
105 #endif
106 
107   //LVCS tag
108   if (std::strcmp(name, LVCS_TAG) == 0) {
109     for (int i=0; atts[i]; i+=2) {
110       if (std::strcmp(atts[i], "cs_name") == 0)
111         convert(atts[i+1], lvcs_cs_name_);
112       else if (std::strcmp(atts[i], "origin_lon") == 0)
113         convert(atts[i+1], lvcs_origin_lon_);
114       else if (std::strcmp(atts[i], "origin_lat") == 0)
115         convert(atts[i+1], lvcs_origin_lat_);
116       else if (std::strcmp(atts[i], "origin_elev") == 0)
117         convert(atts[i+1], lvcs_origin_elev_);
118       else if (std::strcmp(atts[i], "lon_scale") == 0)
119         convert(atts[i+1], lvcs_lon_scale_);
120       else if (std::strcmp(atts[i], "lat_scale") == 0)
121         convert(atts[i+1], lvcs_lat_scale_);
122       else if (std::strcmp(atts[i], "local_XYZ_unit") == 0)
123         convert(atts[i+1], lvcs_XYZ_unit_);
124       else if (std::strcmp(atts[i], "geo_angle_unit") == 0)
125         convert(atts[i+1], lvcs_geo_angle_unit_);
126       else if (std::strcmp(atts[i], "local_origin_x") == 0)
127         convert(atts[i+1], lvcs_local_origin_x_);
128       else if (std::strcmp(atts[i], "local_origin_y") == 0)
129         convert(atts[i+1], lvcs_local_origin_y_);
130       else if (std::strcmp(atts[i], "theta_") == 0)
131         convert(atts[i+1], lvcs_theta_);
132     }
133   }
134 
135   //Local Origin Tag
136   else if (std::strcmp(name,LOCAL_ORIGIN_TAG)== 0) {
137     double x,y,z;
138     for (int i=0; atts[i]; i+=2) {
139       if (std::strcmp(atts[i], "x") == 0)
140         convert(atts[i+1], x);
141       else if (std::strcmp(atts[i], "y") == 0)
142         convert(atts[i+1], y);
143       else if (std::strcmp(atts[i], "z") == 0)
144         convert(atts[i+1], z);
145     }
146     origin_ = vgl_point_3d<double>(x,y,z);
147   }
148 
149   //SCENE PATHS TAG
150   else if (std::strcmp(name,SCENE_PATHS_TAG)== 0) {
151     for (int i=0; atts[i]; i+=2) {
152       if (std::strcmp(atts[i], "path") == 0)
153         convert(atts[i+1], path_);
154     }
155   }
156 
157   //----------- APPEARANCE TAG -------------------------------------------------
158   else if (std::strcmp(name, APM_TAG) == 0) {
159     for (int i=0; atts[i]; i+=2) {
160       std::string buff;
161       if (std::strcmp(atts[i], "apm") == 0) {
162         convert(atts[i+1], buff);
163         appearances_.push_back(buff);
164       }
165     }
166   }
167   else if (std::strcmp(name,VERSION_TAG) == 0){
168     for (int i=0; atts[i]; i+=2) {
169       if (std::strcmp(atts[i], "number") == 0)
170         convert(atts[i+1], version_);
171     }
172   }
173   //---------- BLOCK TAG -------------------------------------------------------
174   else if (std::strcmp(name, BLOCK_TAG) == 0) {
175     bstm_block_metadata metadata;
176     int idi, idj, idk, idt;
177     double ox, oy, oz,ot;
178     double dim_x, dim_y, dim_z,dim_t;
179     unsigned num_x, num_y, num_z,num_t;
180 
181     //iterate over attributes...
182     for (int i=0; atts[i]; i+=2) {
183 
184       if (std::strcmp(atts[i], "id_i") == 0)
185         convert(atts[i+1], idi);
186       else if (std::strcmp(atts[i], "id_j") == 0)
187         convert(atts[i+1], idj);
188       else if (std::strcmp(atts[i], "id_k") == 0)
189         convert(atts[i+1], idk);
190       else if (std::strcmp(atts[i], "id_t") == 0)
191         convert(atts[i+1], idt);
192       else if (std::strcmp(atts[i], "origin_x") == 0)
193         convert(atts[i+1], ox);
194       else if (std::strcmp(atts[i], "origin_y") == 0)
195         convert(atts[i+1], oy);
196       else if (std::strcmp(atts[i], "origin_z") == 0)
197         convert(atts[i+1], oz);
198       else if (std::strcmp(atts[i], "origin_t") == 0)
199         convert(atts[i+1], ot);
200       else if (std::strcmp(atts[i], "dim_x") == 0)
201         convert(atts[i+1], dim_x);
202       else if (std::strcmp(atts[i], "dim_y") == 0)
203         convert(atts[i+1], dim_y);
204       else if (std::strcmp(atts[i], "dim_z") == 0)
205         convert(atts[i+1], dim_z);
206       else if (std::strcmp(atts[i], "dim_t") == 0)
207         convert(atts[i+1], dim_t);
208       else if (std::strcmp(atts[i], "num_x") == 0)
209         convert(atts[i+1], num_x);
210       else if (std::strcmp(atts[i], "num_y") == 0)
211         convert(atts[i+1], num_y);
212       else if (std::strcmp(atts[i], "num_z") == 0)
213         convert(atts[i+1], num_z);
214       else if (std::strcmp(atts[i], "num_t") == 0)
215         convert(atts[i+1], num_t);
216       else if (std::strcmp(atts[i], "init_level")==0)
217         convert(atts[i+1], metadata.init_level_);
218       else if (std::strcmp(atts[i], "init_level_t")==0)
219         convert(atts[i+1], metadata.init_level_t_);
220       else if (std::strcmp(atts[i], "max_level")==0)
221         convert(atts[i+1], metadata.max_level_);
222       else if (std::strcmp(atts[i], "max_level_t")==0)
223         convert(atts[i+1], metadata.max_level_t_);
224       else if (std::strcmp(atts[i], "max_mb")==0)
225         convert(atts[i+1], metadata.max_mb_);
226       else if (std::strcmp(atts[i], "p_init")==0)
227         convert(atts[i+1], metadata.p_init_);
228     }
229     metadata.id_ = bstm_block_id(idi, idj, idk, idt);
230     metadata.local_origin_ = vgl_point_3d<double>(ox, oy, oz);
231     metadata.local_origin_t_ = ot;
232     metadata.sub_block_dim_ = vgl_vector_3d<double>(dim_x, dim_y, dim_z);
233     metadata.sub_block_dim_t_ = dim_t;
234     metadata.sub_block_num_ = vgl_vector_3d<unsigned>(num_x, num_y, num_z);
235     metadata.sub_block_num_t_ = num_t;
236     metadata.version_ = version_;
237     blocks_[metadata.id_] = metadata;
238   }
239 }
240