1import brl_init
2import bvxm_batch as batch
3dbvalue = brl_init.register_batch(batch)
4import os
5#############################################################################
6# PROVIDES higher level python functions to make bvxm_batch
7# code more readable/refactored
8#############################################################################
9
10# loads/creates scene
11
12
13def create_scene(scene_params_xml):
14    batch.init_process("bvxmCreateVoxelWorldProcess")
15    # "./bvxmCreateVoxelWorldProcess.xml"
16    batch.set_params_process(scene_params_xml)
17    status = batch.run_process()
18    if status != 1:
19        raise Exception('Error creating scene from ' + scene_params_xml)
20    (world_id, world_type) = batch.commit_output(0)
21    world = dbvalue(world_id, world_type)
22    return world
23
24# create scene xml
25
26
27def create_scene_xml(scene_xml, world_dir, lvcs, lvcs_file, dim_x, dim_y, dim_z, voxel_size=1.0, corner_x=0.0, corner_y=0.0, corner_z=0.0,
28                     min_ocp_prob=0.001, max_ocp_prob=0.999, max_scale=1):
29    batch.init_process("bvxmCreateSceneXmlProcess")
30    batch.set_input_string(0, scene_xml)
31    batch.set_input_string(1, world_dir)
32    batch.set_input_float(2,  corner_x)
33    batch.set_input_float(3,  corner_y)
34    batch.set_input_float(4,  corner_z)
35    batch.set_input_unsigned(5, dim_x)
36    batch.set_input_unsigned(6, dim_y)
37    batch.set_input_unsigned(7, dim_z)
38    batch.set_input_float(8, voxel_size)
39    batch.set_input_from_db(9, lvcs)
40    batch.set_input_string(10, lvcs_file)
41    batch.set_input_float(11, min_ocp_prob)
42    batch.set_input_float(12, max_ocp_prob)
43    batch.set_input_unsigned(13, max_scale)
44    return batch.run_process()
45
46
47def create_scene_large_scale(roi_kml, scene_root, world_dir, dem_folder, world_size=500.0, voxel_size=1.0, height_diff=120.0, height_sub=25.0, extension = 500.0, land_folder=""):
48    batch.init_process("bvxmCreateSceneXmlLargeScaleProcess")
49    batch.set_input_string(0, roi_kml)
50    batch.set_input_string(1, scene_root)
51    batch.set_input_string(2, world_dir)
52    batch.set_input_string(3, dem_folder)
53    batch.set_input_string(4, land_folder)
54    batch.set_input_float(5, world_size)
55    batch.set_input_float(6, voxel_size)
56    batch.set_input_float(7, height_diff)
57    batch.set_input_float(8, height_sub)
58    batch.set_input_float(9, extension)
59    status = batch.run_process()
60    if status:
61        (id, type) = batch.commit_output(0)
62        n_scenes = batch.get_output_unsigned(id)
63        batch.remove_data(id)
64        return n_scenes
65    else:
66        return 0
67
68
69def scene_box(scene):
70    batch.init_process("bvxmSceneBoxProcess")
71    batch.set_input_from_db(0, scene)
72    batch.run_process()
73    (id, type) = batch.commit_output(0)
74    lower_left_lon = batch.get_output_double(id)
75    batch.remove_data(id)
76    (id, type) = batch.commit_output(1)
77    lower_left_lat = batch.get_output_double(id)
78    batch.remove_data(id)
79    (id, type) = batch.commit_output(2)
80    lower_left_elev = batch.get_output_double(id)
81    batch.remove_data(id)
82    (id, type) = batch.commit_output(3)
83    upper_right_lon = batch.get_output_double(id)
84    batch.remove_data(id)
85    (id, type) = batch.commit_output(4)
86    upper_right_lat = batch.get_output_double(id)
87    batch.remove_data(id)
88    (id, type) = batch.commit_output(5)
89    upper_right_elev = batch.get_output_double(id)
90    batch.remove_data(id)
91    return lower_left_lon, lower_left_lat, lower_left_elev, upper_right_lon, upper_right_lat, upper_right_elev
92
93# changed to always draw a filled polygon, default alpha value (a) is set to 0 so that the box is completely transparent though so it will look as an empty polygon
94# set a to e.g. 100 for a semi-transparent polygon, set a to 255 for an
95# opaque polygon
96
97
98def write_scene_kml(scene, kml_filename, is_overwrite=True, r=255, g=255, b=255, a=0, name=""):
99    batch.init_process("bvxmSceneKmlProcess")
100    batch.set_input_from_db(0, scene)
101    batch.set_input_string(1, kml_filename)
102    batch.set_input_bool(2, is_overwrite)
103    batch.set_input_unsigned(3, r)
104    batch.set_input_unsigned(4, g)
105    batch.set_input_unsigned(5, b)
106    batch.set_input_unsigned(6, a)
107    batch.set_input_string(7, name)
108    batch.run_process()
109
110# check whether the scene is intersects with a given kml polygon
111
112
113def check_scene_poly_overlap(scene, kml_file):
114    batch.init_process("bvxmScenePolyOverlapProcess")
115    batch.set_input_from_db(0, scene)
116    batch.set_input_string(1, kml_file)
117    status = batch.run_process()
118    return status
119
120
121def scene_origin(scene):
122    batch.init_process("bvxmSceneOriginProcess")
123    batch.set_input_from_db(0, scene)
124    batch.run_process()
125    (id, type) = batch.commit_output(0)
126    lower_left_lon = batch.get_output_double(id)
127    batch.remove_data(id)
128    (id, type) = batch.commit_output(1)
129    lower_left_lat = batch.get_output_double(id)
130    batch.remove_data(id)
131    (id, type) = batch.commit_output(2)
132    lower_left_z = batch.get_output_double(id)
133    batch.remove_data(id)
134    return lower_left_lon, lower_left_lat, lower_left_z
135
136# return the scene bbox and voxel size in local coordinates
137
138
139def scene_local_box(scene):
140    batch.init_process("bvxmSceneLocalBoxProcess")
141    batch.set_input_from_db(0, scene)
142    batch.run_process()
143    (id, type) = batch.commit_output(0)
144    lower_left_x = batch.get_output_double(id)
145    batch.remove_data(id)
146    (id, type) = batch.commit_output(1)
147    lower_left_y = batch.get_output_double(id)
148    batch.remove_data(id)
149    (id, type) = batch.commit_output(2)
150    upper_right_x = batch.get_output_double(id)
151    batch.remove_data(id)
152    (id, type) = batch.commit_output(3)
153    upper_right_y = batch.get_output_double(id)
154    batch.remove_data(id)
155    (id, type) = batch.commit_output(4)
156    voxel_size = batch.get_output_double(id)
157    batch.remove_data(id)
158    (id, type) = batch.commit_output(5)
159    lower_left_z = batch.get_output_double(id)  # min z
160    batch.remove_data(id)
161    (id, type) = batch.commit_output(6)
162    upper_right_z = batch.get_output_double(id)  # max z
163    batch.remove_data(id)
164    return lower_left_x, lower_left_y, upper_right_x, upper_right_y, voxel_size, lower_left_z, upper_right_z
165
166# return the scene world directory
167
168
169def model_dir(scene):
170    batch.init_process("bvxmSceneModelDirProcess")
171    batch.set_input_from_db(0, scene)
172    batch.run_process()
173    (id, type) = batch.commit_output(0)
174    model_dir = batch.get_output_string(id)
175    batch.remove_data(id)
176    return model_dir
177
178
179def roi_init(image_filename, cam, world, roi_init_params_xml, is_all_bits=False):
180    batch.init_process("bvxmRoiInitProcess")
181    batch.set_input_string(0, image_filename)
182    batch.set_input_from_db(1, cam)
183    batch.set_input_from_db(2, world)
184    batch.set_input_bool(3, is_all_bits)
185    # "bvxmRoiInitProcess.xml")
186    batch.set_params_process(roi_init_params_xml)
187    statuscode = batch.run_process()
188    if statuscode:
189        (cropped_cam_id, cropped_cam_type) = batch.commit_output(0)
190        cropped_cam = dbvalue(cropped_cam_id, cropped_cam_type)
191        (cropped_image_id, cropped_image_type) = batch.commit_output(1)
192        cropped_image = dbvalue(cropped_image_id, cropped_image_type)
193        (uncertainty_id, uncertainty_type) = batch.commit_output(2)
194        uncertainty = dbvalue(uncertainty_id, uncertainty_type)
195        return statuscode, cropped_cam, cropped_image, uncertainty
196    else:
197        return statuscode, dbvalue(0, ""), dbvalue(0, ""), dbvalue(0, "")
198
199
200def bvxm_detect_edges(cropped_image, edge_params_xml):
201    batch.init_process("bvxmDetectEdgesProcess")
202    batch.set_input_from_db(0, cropped_image)
203    batch.set_params_process(edge_params_xml)
204    batch.run_process()
205    (cropped_edge_image_id, cropped_edge_image) = batch.commit_output(0)
206    cropped_edge_image = dbvalue(cropped_edge_image_id, cropped_edge_image)
207    return cropped_edge_image
208
209
210def update_edges(world, cropped_cam, cropped_edge_image, edge_prob_mask_size=21, edge_prob_mask_sigma=1.0, scale=0):
211    batch.init_process("bvxmUpdateEdgesProcess")
212    batch.set_input_from_db(0, world)
213    batch.set_input_from_db(1, cropped_cam)
214    batch.set_input_from_db(2, cropped_edge_image)
215    batch.set_input_unsigned(3, 0)
216    batch.set_input_int(4, edge_prob_mask_size)
217    batch.set_input_float(5, edge_prob_mask_sigma)
218# batch.set_params_process(update_params_xml); #
219# "./bvxmUpdateEdgesProcess.xml");
220    batch.run_process()
221
222
223def rpc_registration(world, cropped_cam, cropped_edge_image, uncertainty, shift_3d_flag=0, scale=0, is_uncertainty_float=0):
224    batch.init_process("bvxmRpcRegistrationProcess")
225    batch.set_input_from_db(0, world)
226    batch.set_input_from_db(1, cropped_cam)
227    batch.set_input_from_db(2, cropped_edge_image)
228    batch.set_input_bool(3, shift_3d_flag)
229    if is_uncertainty_float == 1:
230        print("uncertainty = ", uncertainty)
231        batch.set_input_float(4, uncertainty)
232    else:
233        batch.set_input_from_db(4, uncertainty)
234    batch.set_input_unsigned(5, scale)
235    batch.run_process()
236    (cam_id, cam_type) = batch.commit_output(0)
237    cam = dbvalue(cam_id, cam_type)
238    (expected_edge_image_id, expected_edge_image_type) = batch.commit_output(1)
239    expected_edge_image = dbvalue(
240        expected_edge_image_id, expected_edge_image_type)
241    (offset_u_id, offset_u_type) = batch.commit_output(2)
242    offset_u = batch.get_output_double(offset_u_id)
243    (offset_v_id, offset_v_type) = batch.commit_output(3)
244    offset_v = batch.get_output_double(offset_v_id)
245    return cam, expected_edge_image, offset_u, offset_v
246
247
248def render_height_map(world):
249    print("Rendering height map")
250    batch.init_process("bvxmHeightmapOrthoProcess")
251    batch.set_input_from_db(0, world)
252    batch.run_process()
253    (id, type) = batch.commit_output(0)
254    out_d_img = dbvalue(id, type)
255    (id, type) = batch.commit_output(1)
256    out_h_img = dbvalue(id, type)
257    (id, type) = batch.commit_output(2)
258    out_conf_img = dbvalue(id, type)
259    return out_h_img, out_d_img, out_conf_img
260
261
262def render_height_map_with_cam(world, input_cam, ni, nj, is_negate=False):
263    batch.init_process("bvxmHeightmapProcess")
264    batch.set_input_from_db(0, input_cam)
265    batch.set_input_unsigned(1, ni)
266    batch.set_input_unsigned(2, nj)
267    batch.set_input_from_db(3, world)
268    batch.set_input_bool(4, is_negate)
269    batch.run_process()
270    (id, type) = batch.commit_output(0)
271    out_d_img = dbvalue(id, type)
272    return out_d_img
273
274
275def render_height_map_expected_with_cam(world, input_cam, ni, nj):
276    batch.init_process("bvxmHeightmapExpectedProcess")
277    batch.set_input_from_db(0, world)
278    batch.set_input_from_db(1, input_cam)
279    batch.set_input_unsigned(2, ni)
280    batch.set_input_unsigned(3, nj)
281    batch.run_process()
282    (id, type) = batch.commit_output(0)
283    out_h_img = dbvalue(id, type)
284    (id, type) = batch.commit_output(1)
285    out_var_img = dbvalue(id, type)
286    return out_h_img, out_var_img
287
288
289def render_uncertainty_img(input_cam, ni, nj, world):
290    batch.init_process("bvxmUncertaintyProcess")
291    batch.set_input_from_db(0, world)
292    batch.set_input_from_db(1, input_cam)
293    batch.set_input_unsigned(2, ni)
294    batch.set_input_unsigned(3, nj)
295    batch.run_process()
296    (id, type) = batch.commit_output(0)
297    out_img = dbvalue(id, type)
298    return out_img
299
300
301def render_ortho_edgemap(world, scale=0):
302    print("Rendering ortho edge map")
303    batch.init_process("bvxmEdgemapOrthoProcess")
304    batch.set_input_from_db(0, world)
305    batch.set_input_unsigned(1, 0)  # scale
306    batch.run_process()
307    (id, type) = batch.commit_output(0)
308    out_e_img = dbvalue(id, type)
309    (id, type) = batch.commit_output(1)
310    out_e_img_byte = dbvalue(id, type)
311    (id, type) = batch.commit_output(2)
312    out_h_img = dbvalue(id, type)
313    (id, type) = batch.commit_output(3)
314    ortho_cam = dbvalue(id, type)
315    return out_e_img, out_e_img_byte, out_h_img, ortho_cam
316
317
318def create_ortho_camera(world, is_utm=False):
319    batch.init_process("bvxmCreateOrthoCameraProcess")
320    batch.set_input_from_db(0, world)
321    batch.set_input_bool(1, is_utm)
322    batch.run_process()
323    (id, type) = batch.commit_output(0)
324    ortho_cam = dbvalue(id, type)
325    return ortho_cam
326
327
328def render_exp_image(cam, ni, nj, world, app_model, bin_index=0, scale_index=0):
329    batch.init_process("bvxmRenderExpectedImageProcess")
330    batch.set_input_from_db(0, cam)
331    batch.set_input_unsigned(1, ni)
332    batch.set_input_unsigned(2, nj)
333    batch.set_input_from_db(3, world)
334    batch.set_input_string(4, app_model)
335    # set bin index to be 0 for all images
336    batch.set_input_unsigned(5, bin_index)
337    batch.set_input_unsigned(6, scale_index)
338    batch.run_process()
339    (id, type) = batch.commit_output(0)
340    out_img = dbvalue(id, type)
341    (id, type) = batch.commit_output(1)
342    out_conf_img = dbvalue(id, type)
343    return out_img, out_conf_img
344
345
346def render_exp_edge_img(cam, ni, nj, world, scale=0):
347    batch.init_process("bvxmExpectedEdgeImageProcess")
348    batch.set_input_from_db(0, world)
349    batch.set_input_from_db(1, cam)
350    batch.set_input_unsigned(2, ni)
351    batch.set_input_unsigned(3, nj)
352    batch.set_input_unsigned(4, scale)
353    batch.run_process()
354    (id, type) = batch.commit_output(0)
355    exp_img = dbvalue(id, type)
356    (id, type) = batch.commit_output(1)
357    exp_img_byte = dbvalue(id, type)
358    return exp_img, exp_img_byte
359
360
361def update_appearance(img, cam, world, app_type, bin_index=0, scale_index=0, use_cache=0):
362    batch.init_process("bvxmUpdateProcess")
363    batch.set_input_from_db(0, img)
364    batch.set_input_from_db(1, cam)
365    batch.set_input_from_db(2, world)
366    batch.set_input_string(3, app_type)
367    # set bin index to be 0 for all images
368    batch.set_input_unsigned(4, bin_index)
369    batch.set_input_unsigned(5, scale_index)
370    batch.set_input_unsigned(6, use_cache)
371    batch.run_process()
372    (id, type) = batch.commit_output(0)
373    density_img = dbvalue(id, type)
374    (id, type) = batch.commit_output(1)
375    density_mask_img = dbvalue(id, type)
376    return density_img, density_mask_img
377
378
379def save_occupancy_raw(world, filename, app_model, scale=0):
380    batch.init_process("bvxmSaveOccupancyRawProcess")
381    batch.set_input_from_db(0, world)
382    batch.set_input_string(1, filename)
383    batch.set_input_unsigned(2, scale)
384    batch.set_input_string(3, app_model)
385    batch.run_process()
386
387
388def orthorectify(world, ortho_height_map, ortho_height_map_camera, input_image, input_image_camera):
389    batch.init_process("bvxmOrthorectifyProcess")
390    batch.set_input_from_db(0, ortho_height_map)
391    batch.set_input_from_db(1, ortho_height_map_camera)
392    batch.set_input_from_db(2, input_image)
393    batch.set_input_from_db(3, input_image_camera)
394    batch.set_input_from_db(4, world)
395    batch.run_process()
396    (id, type) = batch.commit_output(0)
397    out_ortho_img = dbvalue(id, type)
398    return out_ortho_img
399
400############## some utilities, put here for now ##########
401
402
403def image_to_vrml_points(out_e_img, out_h_img, output_filename, prob_thres, max_scene_height):
404    batch.init_process("bvrmlImageToPointsProcess")
405    batch.set_input_from_db(0, out_e_img)
406    batch.set_input_from_db(1, out_h_img)
407    batch.set_input_string(2, output_filename)
408    batch.set_input_float(3, prob_thres)
409    batch.set_input_float(4, max_scene_height)
410    batch.run_process()
411    (id, type) = batch.commit_output(0)
412    out_img = dbvalue(id, type)
413    return out_img
414
415
416def name_suffix_for_camera(lower_left_lon, lower_left_lat, upper_right_lon, upper_right_lat):
417    if (lower_left_lat < 0):
418        name = "S" + str(abs(lower_left_lat))
419    else:
420        name = "N" + str(lower_left_lat)
421    if (lower_left_lon < 0):
422        name = name + "W" + str(abs(lower_left_lon))
423    else:
424        name = name + "E" + str(lower_left_lon)
425    name = name + "_S" + str(abs(upper_right_lat - lower_left_lat)) + \
426        "x" + str(abs(upper_right_lon - lower_left_lon))
427    return name
428
429# Create x y z images for bvxm_scene from dem
430
431
432def generate_xyz_from_dem(world, geotiff_dem, geoid_height, geocam=0, fill_in_value=-1.0):
433    batch.init_process("bvxmDemToXYZProcess")
434    batch.set_input_from_db(0, world)
435    batch.set_input_string(1, geotiff_dem)
436    batch.set_input_double(2, geoid_height)
437    batch.set_input_from_db(3, geocam)
438    batch.set_input_float(4, fill_in_value)
439    result = batch.run_process()
440    if result:
441        (xi_id, xi_type) = batch.commit_output(0)
442        x_img = dbvalue(xi_id, xi_type)
443        (yi_id, yi_type) = batch.commit_output(1)
444        y_img = dbvalue(yi_id, yi_type)
445        (zi_id, zi_type) = batch.commit_output(2)
446        z_img = dbvalue(zi_id, zi_type)
447    else:
448        x_img = 0
449        y_img = 0
450        z_img = 0
451    return x_img, y_img, z_img
452
453# Create x y z images for bvxm_scene from multiple dem image in image folder
454# Assuming the camera are loaded from geotiff image header
455
456
457def generate_xyz_from_dem_multi(world, img_folder, geoid_height, fill_in_value=-1.0):
458    batch.init_process("bvxmDemToXYZProcess2")
459    batch.set_input_from_db(0, world)
460    batch.set_input_string(1, img_folder)
461    batch.set_input_double(2, geoid_height)
462    batch.set_input_float(3, fill_in_value)
463    result = batch.run_process()
464    if result:
465        (xi_id, xi_type) = batch.commit_output(0)
466        x_img = dbvalue(xi_id, xi_type)
467        (yi_id, yi_type) = batch.commit_output(1)
468        y_img = dbvalue(yi_id, yi_type)
469        (zi_id, zi_type) = batch.commit_output(2)
470        z_img = dbvalue(zi_id, zi_type)
471    else:
472        x_img = 0
473        y_img = 0
474        z_img = 0
475    return x_img, y_img, z_img
476
477
478def create_land_map(world, geo_folder, urban_folder, osm_folder, is_osm_pt, is_osm_region, is_osm_line, is_convert=True):
479    batch.init_process("bvxmCreateLandMapProcess")
480    batch.set_input_from_db(0, world)
481    batch.set_input_string(1, geo_folder)
482    batch.set_input_string(2, urban_folder)
483    batch.set_input_string(3, osm_folder)
484    batch.set_input_bool(4, is_osm_pt)
485    batch.set_input_bool(5, is_osm_line)
486    batch.set_input_bool(6, is_osm_region)
487    batch.set_input_bool(7, is_convert)
488    result = batch.run_process()
489    if result:
490        (img_id, img_type) = batch.commit_output(0)
491        land_img = dbvalue(img_id, img_type)
492    else:
493        land_img = 0
494    return land_img
495
496
497def scene_lvcs(world):
498    batch.init_process("bvxmSceneLvcsProcess")
499    batch.set_input_from_db(0, world)
500    status = batch.run_process()
501    if status:
502        (id, type) = batch.commit_output(0)
503        lvcs = dbvalue(id, type)
504        return lvcs
505    else:
506        return 0
507