[8d6683]: contrib / brl / bbas / volm / volm_io_tools.cxx Maximize Restore History

Download this file

volm_io_tools.cxx    796 lines (718 with data), 30.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#include "volm_io_tools.h"
//:
// \file
#include <vul/vul_file.h>
#include <vul/vul_file_iterator.h>
#include <vpgl/vpgl_utm.h>
#include <bkml/bkml_write.h>
#include <vil/vil_load.h>
#include <vil/vil_image_view.h>
#include <vil/vil_crop.h>
#include <vpgl/vpgl_lvcs.h>
#include <vpgl/vpgl_lvcs_sptr.h>
#include <vcl_algorithm.h>
#include <vgl/algo/vgl_fit_lines_2d.h>
#include <vgl/vgl_intersection.h>
#include <vgl/vgl_line_segment_3d.h>
unsigned int volm_io_tools::northing = 0; // WARNING: north hard-coded
static double eps = 1.0e-5;
inline bool near_zero(double x) { return x < eps && x > -eps; }
inline bool near_equal(double x, double y) { return near_zero(x-y); }
bool near_eq_pt(vgl_point_2d<double> a, vgl_point_2d<double> b)
{
return (near_equal(a.x(),b.x()) && near_equal(a.y(), b.y()));
}
void volm_img_info::save_box_kml(vcl_string out_name) {
vcl_ofstream ofs(out_name.c_str());
bkml_write::open_document(ofs);
bkml_write::write_box(ofs, name, "", bbox);
bkml_write::close_document(ofs);
}
bool read_box(vcl_string bbox_file, vgl_box_2d<double>& bbox) {
char buffer[1000];
vcl_ifstream ifs(bbox_file.c_str());
if (!ifs.is_open()) {
vcl_cerr << " cannot open: " << bbox_file << "!\n";
return false;
}
vcl_string dummy; double top_lat, bottom_lat, left_lon, right_lon;
for (unsigned kk = 0; kk < 22; kk++)
ifs.getline(buffer, 1000);
// top
ifs.getline(buffer, 1000);
vcl_stringstream top_edge_line(buffer);
top_edge_line >> dummy; top_edge_line >> dummy; top_edge_line >> dummy;
top_edge_line >> top_lat;
// bottom
ifs.getline(buffer, 1000);
vcl_stringstream bot_edge_line(buffer);
bot_edge_line >> dummy; bot_edge_line >> dummy; bot_edge_line >> dummy;
bot_edge_line >> bottom_lat;
// left
ifs.getline(buffer, 1000);
vcl_stringstream left_edge_line(buffer);
left_edge_line >> dummy; left_edge_line >> dummy; left_edge_line >> dummy;
left_edge_line >> left_lon;
// right
ifs.getline(buffer, 1000);
vcl_stringstream right_edge_line(buffer);
right_edge_line >> dummy; right_edge_line >> dummy; right_edge_line >> dummy;
right_edge_line >> right_lon;
vgl_point_2d<double> lower_left(left_lon, bottom_lat);
vgl_point_2d<double> upper_right(right_lon, top_lat);
bbox = vgl_box_2d<double>(lower_left, upper_right);
//vcl_cout << "bbox: " << bbox << vcl_endl;
return true;
}
bool volm_io_tools::load_naip_img(vcl_string const& img_folder, vcl_string const& name, vpgl_lvcs_sptr& lvcs, volm_img_info& info, bool load_resource)
{
vcl_string filename = img_folder + "\\" + name;
vcl_string img_name = filename + "\\" + name + ".tif";
vcl_string tfw_name = filename + "\\" + name + ".tfw";
if (!vul_file::exists(tfw_name) || !vul_file::exists(img_name))
return false;
info.name = name; info.img_name = img_name;
vcl_string bbox_file = filename + "\\output_parameters.txt";
if (!read_box(bbox_file, info.bbox)) {
vcl_cerr << " cannot find: " << bbox_file << vcl_endl;
return false;
}
//vcl_cout << "NAIP bbox: " << info.bbox << vcl_endl;
// figure out utm zone
vpgl_utm utm; int utm_zone, zone_max; double xx, yy;
utm.transform(info.bbox.min_point().y(), info.bbox.min_point().x(), xx, yy, utm_zone);
utm.transform(info.bbox.max_point().y(), info.bbox.max_point().x(), xx, yy, zone_max);
if (utm_zone != zone_max) {
vcl_cout << "!!!!!!!!!!!!!!!!!!!!!!!!!WARNING! img: " << img_name << " has min and max points in different UTM zones, using zone of min point!\n";
}
vpgl_geo_camera *cam = 0;
if (!vpgl_geo_camera::init_geo_camera(tfw_name, lvcs, utm_zone, northing, cam))
return false;
info.cam = cam;
if (load_resource) {
vil_image_resource_sptr img = vil_load_image_resource(img_name.c_str());
vcl_cout << "ni: " << img->ni() <<" nj: " << img->nj() <<vcl_endl;
info.ni = img->ni(); info.nj = img->nj();
}
return true;
}
bool volm_io_tools::load_naip_imgs(vcl_string const& img_folder, vcl_vector<volm_img_info>& imgs, bool load_resource) {
vpgl_lvcs_sptr lvcs = new vpgl_lvcs; // just the default, no concept of local coordinate system here, so won't be used
vcl_string in_dir = img_folder + "*";
for (vul_file_iterator fn = in_dir.c_str(); fn; ++fn) {
vcl_string filename = fn();
//vcl_cout << "filename: " << filename << vcl_endl;
vcl_string file = vul_file::strip_directory(filename);
volm_img_info info;
if (load_naip_img(img_folder, file, lvcs, info, load_resource))
imgs.push_back(info);
}
return true;
}
int volm_io_tools::load_lidar_img(vcl_string img_file, volm_img_info& info, bool load_image_resource) {
vpgl_lvcs_sptr lvcs = new vpgl_lvcs; // just the default, no concept of local coordinate system here, so won't be used
if (load_image_resource) {
info.img_r = vil_load(img_file.c_str());
info.ni = info.img_r->ni(); info.nj = info.img_r->nj();
} else {
vil_image_view_base_sptr img_sptr = vil_load(img_file.c_str());
info.ni = img_sptr->ni(); info.nj = img_sptr->nj();
}
info.name = vul_file::strip_directory(vul_file::strip_extension(img_file));
info.img_name = img_file;
vpgl_geo_camera *cam;
vpgl_geo_camera::init_geo_camera(img_file, info.ni, info.nj, lvcs, cam);
info.cam = cam;
// obtain the bounding box of current image
vcl_string name = vul_file::strip_directory(img_file);
name = name.substr(name.find_first_of('_')+1, name.size());
vcl_string n_coords = name.substr(0, name.find_first_of('_'));
vcl_string hemisphere, direction;
vcl_size_t n = n_coords.find("N");
if (n < n_coords.size())
hemisphere = "N";
else
hemisphere = "S";
n = n_coords.find("E");
if (n < n_coords.size())
direction = "E";
else
direction = "W";
double lat, lon;
cam->img_to_global(0.0, info.nj-1, lon, lat);
if (direction == "W")
lon = -lon;
if (hemisphere == "S")
lat = -lat;
vgl_point_2d<double> lower_left(lon, lat);
vpgl_utm utm; int utm_zone; double x,y;
utm.transform(lat, lon, x, y, utm_zone);
vcl_cout << " zone of lidar img: " << img_file << ": " << utm_zone << " from lower left corner!\n";
cam->img_to_global(info.ni-1, 0.0, lon, lat);
if (direction == "W")
lon = -lon;
if (hemisphere == "S")
lat = -lat;
vgl_point_2d<double> upper_right(lon, lat);
vgl_box_2d<double> bbox(lower_left, upper_right);
//vcl_cout << "bbox: " << bbox << vcl_endl;
info.bbox = bbox;
return utm_zone;
}
void volm_io_tools::load_lidar_imgs(vcl_string const& folder, vcl_vector<volm_img_info>& infos) {
vcl_string in_dir = folder + "*.tif";
for (vul_file_iterator fn = in_dir.c_str(); fn; ++fn) {
vcl_string filename = fn();
volm_img_info info;
load_lidar_img(filename, info);
infos.push_back(info);
}
}
void volm_io_tools::load_nlcd_imgs(vcl_string const& folder, vcl_vector<volm_img_info>& infos)
{
vcl_string in_dir = folder + "*.tif*"; // sometimes .tif is written .tiff
for (vul_file_iterator fn = in_dir.c_str(); fn; ++fn) {
vcl_string filename = fn();
volm_img_info info;
load_lidar_img(filename, info, true);
infos.push_back(info);
}
}
void volm_io_tools::load_imgs(vcl_string const& folder, vcl_vector<volm_img_info>& infos, bool load_image_resource)
{
vcl_string in_dir = folder + "*.tif*"; // sometimes .tif is written .tiff
for (vul_file_iterator fn = in_dir.c_str(); fn; ++fn) {
vcl_string filename = fn();
volm_img_info info;
load_lidar_img(filename, info, load_image_resource);
infos.push_back(info);
}
}
bool volm_io_tools::get_location_nlcd(vcl_vector<volm_img_info>& NLCD_imgs, double lat, double lon, double elev, unsigned char& label)
{
bool found_it = false;
for (unsigned i = 0; i < NLCD_imgs.size(); i++) {
if (NLCD_imgs[i].bbox.contains(lon, lat)) {
vil_image_view<vxl_byte> img(NLCD_imgs[i].img_r);
// get the land type of the location
double u, v;
// NLCD imgs are loaded via load_lidar_img, using the init_cam from filename
// therefore tranfer everything using abs(lon) and abs(lat)
double abs_lat, abs_lon;
abs_lat = lat;
if (lat < 0) abs_lat = -lat;
abs_lon = lon;
if (lon < 0) abs_lon = -lon;
NLCD_imgs[i].cam->global_to_img(abs_lon, abs_lat, elev, u, v);
//NLCD_imgs[i].cam->global_to_img(-lon, lat, elev, u, v);
unsigned uu = (unsigned)vcl_floor(u + 0.5);
unsigned vv = (unsigned)vcl_floor(v + 0.5);
if (uu > 0 && vv > 0 && uu < NLCD_imgs[i].ni && vv < NLCD_imgs[i].nj) {
label = img(uu, vv);
found_it = true;
break;
}
}
}
return found_it;
}
bool volm_io_tools::expend_line(vcl_vector<vgl_point_2d<double> > line, double const& w, vgl_polygon<double>& poly)
{
vcl_vector<vgl_point_2d<double> > sheet;
vcl_vector<vgl_point_2d<double> > pts_u;
vcl_vector<vgl_point_2d<double> > pts_d;
unsigned n_pts = line.size();
for (unsigned i = 0; i < n_pts-1; i++) {
vgl_point_2d<double> s, e;
s = line[i]; e = line[i+1];
if (near_eq_pt(s,e))
continue;
vgl_line_2d<double> seg(s, e);
vgl_vector_2d<double> n = seg.normal();
vgl_point_2d<double> su, sd, eu, ed;
su = s + 0.5*w*n; sd = s - 0.5*w*n;
pts_u.push_back(su); pts_d.push_back(sd);
if (i == n_pts-2) {
eu = e + 0.5*w*n; ed = e - 0.5*w*n;
pts_u.push_back(eu); pts_d.push_back(ed);
}
}
// rearrange the point list
for (unsigned i = 0; i < pts_u.size(); i++)
sheet.push_back(pts_u[i]);
for (int i = pts_d.size()-1; i >=0; i--)
sheet.push_back(pts_d[i]);
poly.push_back(sheet);
return true;
}
#include <volm/volm_geo_index2.h>
#include <volm/volm_osm_objects.h>
// a method to read the binary osm object file and also contstruct the volm_geo_index2, the method returns the root of the tree
volm_geo_index2_node_sptr volm_io_tools::read_osm_data_and_tree(vcl_string geoindex_filename_pre, vcl_string osm_bin_filename, volm_osm_objects& osm_objs, double& min_size)
{
vcl_string filename = geoindex_filename_pre + ".txt";
volm_geo_index2_node_sptr root = volm_geo_index2::read_and_construct<volm_osm_object_ids_sptr>(filename, min_size);
// obtain all leaves
vcl_vector<volm_geo_index2_node_sptr> leaves;
volm_geo_index2::get_leaves(root, leaves);
// load the content for valid leaves
for (unsigned l_idx = 0; l_idx < leaves.size(); l_idx++) {
vcl_string bin_file = leaves[l_idx]->get_label_name(geoindex_filename_pre, "osm");
if (!vul_file::exists(bin_file))
continue;
volm_geo_index2_node<volm_osm_object_ids_sptr>* ptr = dynamic_cast<volm_geo_index2_node<volm_osm_object_ids_sptr>* >(leaves[l_idx].ptr());
ptr->contents_ = new volm_osm_object_ids(bin_file);
}
// load the osm bin file to get real location date with unit of lon and lat, associated with ids stored in geo_index2
if (!vul_file::exists(osm_bin_filename)) {
vcl_cout << "ERROR: can not find osm binary: " << osm_bin_filename << vcl_endl;
return 0;
}
vsl_b_ifstream is(osm_bin_filename.c_str());
if (!is) {
vcl_cerr << "In volm_osm_object::volm_osm_object() -- cannot open: " << osm_bin_filename << vcl_endl;
return 0;
}
osm_objs.b_read(is);
is.close();
#if 0
if (is_kml()) {
vcl_stringstream kml_pts, kml_roads, kml_regions;
kml_pts << out_pre() << "/p1b_wr" << world_id() << "_tile_" << tile_id() << "_osm_pts.kml";
kml_roads << out_pre() << "/p1b_wr" << world_id() << "_tile_" << tile_id() << "_osm_roads.kml";
kml_regions << out_pre() << "/p1b_wr" << world_id() << "_tile_" << tile_id() << "_osm_regions.kml";
osm_objs.write_pts_to_kml(kml_pts.str());
osm_objs.write_lines_to_kml(kml_roads.str());
osm_objs.write_polys_to_kml(kml_regions.str());
}
#endif
return root;
}
//: load a geotiff file with .tif file and read its ortho camera info from its header, puts a dummy lvcs to vpgl_geo_cam object so lvcs is not valid
// even though it reads the camera from filename with N/S and W/E distinction, it constructs a camera in global WGS84 coordinates, so the global coordinates should be used to fetch pixels, (i.e. no need to make them always positive)
void volm_io_tools::load_geotiff_image(vcl_string filename, volm_img_info& info, bool load_cam_from_name)
{
info.img_name = filename;
info.name = vul_file::strip_directory(info.img_name);
info.name = vul_file::strip_extension(info.name);
info.img_r = vil_load(info.img_name.c_str());
info.ni = info.img_r->ni(); info.nj = info.img_r->nj();
vcl_cout << "image ni: " << info.ni << " nj: " << info.nj << vcl_endl;
vpgl_geo_camera *cam;
vpgl_lvcs_sptr lvcs_dummy = new vpgl_lvcs;
if (load_cam_from_name) {
vil_image_resource_sptr img_res = vil_load_image_resource(info.img_name.c_str());
vpgl_geo_camera::init_geo_camera_from_filename(filename, info.ni, info.nj, lvcs_dummy, cam); // constructs in global WGS84 (no distinction of N/S or W/E)
vcl_cout << cam->trans_matrix() << vcl_endl;
} else {
vil_image_resource_sptr img_res = vil_load_image_resource(info.img_name.c_str());
vpgl_geo_camera::init_geo_camera(img_res, lvcs_dummy, cam);
vcl_cout << cam->trans_matrix() << vcl_endl;
}
info.cam = cam;
double lat, lon;
cam->img_to_global(0.0, info.nj-1, lon, lat);
vgl_point_2d<double> lower_left(lon, lat);
vpgl_utm utm; int utm_zone; double x,y;
utm.transform(lat, lon, x, y, utm_zone);
vcl_cout << " zone of ASTER DEM img: " << info.name << ": " << utm_zone << " from lower left corner!\n";
cam->img_to_global(info.ni-1, 0.0, lon, lat);
vgl_point_2d<double> upper_right(lon, lat);
vgl_box_2d<double> bbox(lower_left, upper_right);
vcl_cout << "bbox: " << bbox << vcl_endl;
info.bbox = bbox;
}
void volm_io_tools::load_aster_dem_imgs(vcl_string const& folder, vcl_vector<volm_img_info>& infos)
{
vcl_string file_glob = folder + "//ASTGTM2_*.tif";
for (vul_file_iterator fn = file_glob.c_str(); fn; ++fn) {
volm_img_info info;
volm_io_tools::load_geotiff_image(fn(), info);
infos.push_back(info);
//volm_img_info info;
//vcl_string folder = fn();
//vcl_string file_glob2 = folder + "//" + "*_dem*.tif";
//for (vul_file_iterator fn2 = file_glob2.c_str(); fn2; ++fn2) {
// volm_io_tools::load_geotiff_image(fn2(), info);
// infos.push_back(info);
//}
}
}
void volm_io_tools::load_geocover_imgs(vcl_string const& folder, vcl_vector<volm_img_info>& infos)
{
vcl_string file_glob = folder + "//Geocover_*.tif";
for (vul_file_iterator fn = file_glob.c_str(); fn; ++fn) {
volm_img_info info;
volm_io_tools::load_geotiff_image(fn(), info, true); // last argument true so load camera from the file name
infos.push_back(info);
}
}
void volm_io_tools::load_urban_imgs(vcl_string const& folder, vcl_vector<volm_img_info>& infos)
{
vcl_string file_glob = folder + "//Urextent_*.tif";
for (vul_file_iterator fn = file_glob.c_str(); fn; ++fn) {
volm_img_info info;
volm_io_tools::load_geotiff_image(fn(), info, true);
infos.push_back(info);
}
}
void crop_and_find_min_max(vcl_vector<volm_img_info>& infos, unsigned img_id, int i0, int j0, int crop_ni, int crop_nj, double& min, double& max)
{
#if 0
vil_image_view<vxl_int_16> img(infos[img_id].img_r);
vil_image_view<vxl_int_16> img_crop = vil_crop(img, i0, crop_ni, j0, crop_nj);
for (unsigned ii = 0; ii < img_crop.ni(); ii++)
for (unsigned jj = 0; jj < img_crop.nj(); jj++) {
if (min > img_crop(ii, jj)) min = img_crop(ii, jj);
if (max < img_crop(ii, jj)) max = img_crop(ii, jj);
}
#endif
if (vil_image_view<vxl_int_16>* img = dynamic_cast<vil_image_view<vxl_int_16>*>(infos[img_id].img_r.ptr())) {
vil_image_view<vxl_int_16> img_crop = vil_crop(*img, i0, crop_ni, j0, crop_nj);
for (unsigned ii = 0; ii < img_crop.ni(); ii++)
for (unsigned jj = 0; jj < img_crop.nj(); jj++) {
if (min > img_crop(ii, jj)) min = img_crop(ii, jj);
if (max < img_crop(ii, jj)) max = img_crop(ii, jj);
}
}
else if (vil_image_view<float>* img = dynamic_cast<vil_image_view<float>*>(infos[img_id].img_r.ptr()) ) {
vil_image_view<float> img_crop = vil_crop(*img, i0, crop_ni, j0, crop_nj);
for (unsigned ii = 0; ii < img_crop.ni(); ii++)
for (unsigned jj = 0; jj < img_crop.nj(); jj++) {
if (min > img_crop(ii, jj)) min = img_crop(ii, jj);
if (max < img_crop(ii, jj)) max = img_crop(ii, jj);
}
}
}
bool volm_io_tools::find_min_max_height(vgl_point_2d<double>& lower_left, vgl_point_2d<double>& upper_right, vcl_vector<volm_img_info>& infos, double& min, double& max)
{
// find the image of all four corners
vcl_vector<vcl_pair<unsigned, vcl_pair<int, int> > > corners;
vcl_vector<vgl_point_2d<double> > pts;
pts.push_back(vgl_point_2d<double>(lower_left.x(), upper_right.y()));
pts.push_back(vgl_point_2d<double>(upper_right.x(), lower_left.y()));
pts.push_back(lower_left);
pts.push_back(upper_right);
for (unsigned k = 0; k < pts.size(); k++) {
// find the image
for (unsigned j = 0; j < infos.size(); j++) {
double u, v;
infos[j].cam->global_to_img(pts[k].x(), pts[k].y(), 0, u, v);
int uu = (int)vcl_floor(u+0.5);
int vv = (int)vcl_floor(v+0.5);
if (uu < 0 || vv < 0 || uu >= infos[j].ni || vv >= infos[j].nj)
continue;
vcl_pair<unsigned, vcl_pair<int, int> > pp(j, vcl_pair<int, int>(uu, vv));
corners.push_back(pp);
break;
}
}
if (corners.size() != 4) {
vcl_cerr << "Cannot locate all 4 corners among these DEM tiles!\n";
return false;
}
// case 1: all corners are in the same image
if (corners[0].first == corners[1].first) {
// crop the image
int i0 = corners[0].second.first;
int j0 = corners[0].second.second;
int crop_ni = corners[1].second.first-corners[0].second.first+1;
int crop_nj = corners[1].second.second-corners[0].second.second+1;
crop_and_find_min_max(infos, corners[0].first, i0, j0, crop_ni, crop_nj, min, max);
return true;
}
// case 2: two corners are in the same image
if (corners[0].first == corners[2].first && corners[1].first == corners[3].first) {
// crop the first image
int i0 = corners[0].second.first;
int j0 = corners[0].second.second;
int crop_ni = infos[corners[0].first].ni - corners[0].second.first;
int crop_nj = corners[2].second.second-corners[0].second.second+1;
crop_and_find_min_max(infos, corners[0].first, i0, j0, crop_ni, crop_nj, min, max);
// crop the second image
i0 = 0;
j0 = corners[3].second.second;
crop_ni = corners[3].second.first + 1;
crop_nj = corners[1].second.second-corners[3].second.second+1;
crop_and_find_min_max(infos, corners[1].first, i0, j0, crop_ni, crop_nj, min, max);
return true;
}
// case 3: two corners are in the same image
if (corners[0].first == corners[3].first && corners[1].first == corners[2].first) {
// crop the first image
int i0 = corners[0].second.first;
int j0 = corners[0].second.second;
int crop_ni = corners[3].second.first - corners[0].second.first + 1;
int crop_nj = infos[corners[0].first].nj - corners[0].second.second;
crop_and_find_min_max(infos, corners[0].first, i0, j0, crop_ni, crop_nj, min, max);
// crop the second image
i0 = corners[2].second.first;
j0 = 0;
crop_ni = corners[1].second.first - corners[2].second.first + 1;
crop_nj = corners[2].second.second + 1;
crop_and_find_min_max(infos, corners[1].first, i0, j0, crop_ni, crop_nj, min, max);
return true;
}
// case 4: all corners are in a different image
// crop the first image, image of corner 0
int i0 = corners[0].second.first;
int j0 = corners[0].second.second;
int crop_ni = infos[corners[0].first].ni - corners[0].second.first;
int crop_nj = infos[corners[0].first].nj - corners[0].second.second;
crop_and_find_min_max(infos, corners[0].first, i0, j0, crop_ni, crop_nj, min, max);
// crop the second image, image of corner 1
i0 = 0;
j0 = 0;
crop_ni = corners[1].second.first + 1;
crop_nj = corners[1].second.second + 1;
crop_and_find_min_max(infos, corners[1].first, i0, j0, crop_ni, crop_nj, min, max);
// crop the third image, image of corner 2
i0 = corners[2].second.first;
j0 = 0;
crop_ni = infos[corners[2].first].ni - corners[2].second.first;
crop_nj = corners[2].second.second + 1;
crop_and_find_min_max(infos, corners[2].first, i0, j0, crop_ni, crop_nj, min, max);
// crop the fourth image, image of corner 3
i0 = 0;
j0 = corners[3].second.second;
crop_ni = corners[3].second.first + 1;
crop_nj = infos[corners[3].first].nj - corners[3].second.second;
crop_and_find_min_max(infos, corners[3].first, i0, j0, crop_ni, crop_nj, min, max);
return true;
}
//: use the following method to get the multiplier for conversion of meters to degrees, uses vpgl_lvcs internally
double volm_io_tools::meter_to_seconds(double lat, double lon)
{
vpgl_lvcs_sptr lvcs = new vpgl_lvcs(lat, lon, 0, vpgl_lvcs::wgs84, vpgl_lvcs::DEG, vpgl_lvcs::METERS);
double lon1, lat1, lon2, lat2, gz;
lvcs->local_to_global(0, 0, 0, vpgl_lvcs::wgs84, lon1, lat1, gz);
lvcs->local_to_global(1, 0, 0, vpgl_lvcs::wgs84, lon2, lat2, gz);
double dif_lon = lon2-lon1;
double dif_lat = lat2-lat1;
double dif = vcl_sqrt(dif_lon*dif_lon + dif_lat*dif_lat); // 1 meter is this many degrees in this area
return dif;
}
bool find_intersect(vgl_box_2d<double> const& bbox, vgl_point_2d<double> const& s, vgl_point_2d<double> e, vgl_point_2d<double>& pt)
{
vgl_line_2d<double> line(s, e);
vgl_point_2d<double> pi0, pi1;
if (!vgl_intersection(bbox, line, pi0, pi1))
return false;
double x1 = s.x(), y1 = s.y();
double x2 = e.x(), y2 = e.y();
double xp = pi0.x(), yp = pi0.y();
double d1p = (xp-x1)*(xp-x1) + (yp-y1)*(yp-y1);
double d2p = (xp-x2)*(xp-x2) + (yp-y2)*(yp-y2);
double d12 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
double diff = vcl_sqrt(d1p) + vcl_sqrt(d2p) - vcl_sqrt(d12);
if (diff < 1E-5) {
pt = pi0;
return true;
}
xp = pi1.x(); yp = pi1.y();
d1p = (xp-x1)*(xp-x1) + (yp-y1)*(yp-y1);
d2p = (xp-x2)*(xp-x2) + (yp-y2)*(yp-y2);
d12 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
diff = vcl_sqrt(d1p) + vcl_sqrt(d2p) - vcl_sqrt(d12);
if (diff < 1E-5) {
pt = pi1;
return true;
}
return false;
}
bool volm_io_tools::line_inside_the_box(vgl_box_2d<double> const& bbox, vcl_vector<vgl_point_2d<double> >& line, vcl_vector<vgl_point_2d<double> >& road)
{
// obtain points that lie inside the bounding box
vcl_vector<vgl_point_2d<double> > line_in = vgl_intersection(line, bbox);
if (line_in.empty())
return false;
for (unsigned i = 0; i < line_in.size(); i++)
road.push_back(line_in[i]);
// find the intersection points
for (unsigned i = 0; i < line_in.size(); i++) {
vgl_point_2d<double> curr_pt = line_in[i];
vcl_vector<vgl_point_2d<double> >::iterator vit = vcl_find(line.begin(), line.end(), curr_pt);
if (vit == line.begin() ) {
vgl_point_2d<double> next = *(vit+1);
if (bbox.contains(next))
continue;
else {
vgl_point_2d<double> intersect_pt;
if (!find_intersect(bbox, curr_pt, next, intersect_pt))
return false;
// insert the intersect after current point
vcl_vector<vgl_point_2d<double> >::iterator it = vcl_find(road.begin(), road.end(), curr_pt);
road.insert(it+1, intersect_pt);
}
}
else if (vit == line.end()-1) {
vgl_point_2d<double> prev = *(vit-1);
if (bbox.contains(prev))
continue;
else {
vgl_point_2d<double> intersect_pt;
if (!find_intersect(bbox, prev, curr_pt,intersect_pt))
return false;
// insert the intersect point before current point
vcl_vector<vgl_point_2d<double> >::iterator it = vcl_find(road.begin(), road.end(), curr_pt);
road.insert(it, intersect_pt);
}
}
else if (vit != line.end()) {
vgl_point_2d<double> prev = *(vit-1);
vgl_point_2d<double> next = *(vit+1);
if (bbox.contains(next) && bbox.contains(prev))
continue;
else if (bbox.contains(next)) {
vgl_point_2d<double> intersect_pt;
if (!find_intersect(bbox, prev, curr_pt, intersect_pt))
return false;
vcl_vector<vgl_point_2d<double> >::iterator it = vcl_find(road.begin(), road.end(), curr_pt);
road.insert(it, intersect_pt);
}
else if (bbox.contains(prev)) {
vgl_point_2d<double> intersect_pt;
if (!find_intersect(bbox, curr_pt, next, intersect_pt))
return false;
// insert the intersect after current point
vcl_vector<vgl_point_2d<double> >::iterator it = vcl_find(road.begin(), road.end(), curr_pt);
road.insert(it+1, intersect_pt);
}
else {
vgl_point_2d<double> intersect_pt;
if (!find_intersect(bbox, curr_pt, next, intersect_pt))
return false;
// find and insert the intersection after current point
vcl_vector<vgl_point_2d<double> >::iterator it = vcl_find(road.begin(), road.end(), curr_pt);
road.insert(it+1, intersect_pt);
// find and insert the intersection before current point
if (!find_intersect(bbox, prev, curr_pt,intersect_pt))
return false;
it = vcl_find(road.begin(), road.end(), curr_pt);
road.insert(it, intersect_pt);
}
}
else
return false;
}
return true;
}
void form_line_segment_from_pts(vcl_vector<vgl_point_2d<double> > const& road, vcl_vector<vgl_line_segment_2d<double> >& road_seg)
{
unsigned num_pts = road.size();
unsigned num_segs = num_pts - 1;
for (unsigned i = 0; i < num_segs; i++) {
vgl_point_2d<double> s = road[i]; vgl_point_2d<double> e = road[i+1];
if (near_eq_pt(s,e))
continue;
road_seg.push_back(vgl_line_segment_2d<double>(s, e));
}
#if 0
// define a 2d line fit
unsigned min_pts = 3;
double tol = 5.0; // in pixel unit
vgl_fit_lines_2d<double> fitter(min_pts, tol);
unsigned num_pts = road.size();
unsigned num_segs = num_pts - 1;
if (num_pts <= min_pts) {
for (unsigned i = 0; i < num_segs; i++) {
vgl_point_2d<double> s = road[i]; vgl_point_2d<double> e = road[i+1];
road_seg.push_back(vgl_line_segment_2d<double>(s, e));
}
}
else {
fitter.add_curve(road);
fitter.fit();
road_seg = fitter.get_line_segs();
if (road_seg.empty()) {
// fitting failed form the segment directly
road_seg.clear();
for (unsigned i = 0; i < num_segs; i++) {
vgl_point_2d<double> s = road[i]; vgl_point_2d<double> e = road[i+1];
road_seg.push_back(vgl_line_segment_2d<double>(s, e));
}
}
}
fitter.clear();
#endif
}
void find_junctions(vgl_line_segment_2d<double> const& seg,
volm_land_layer const& seg_prop,
vcl_vector<vgl_line_segment_2d<double> > const& lines,
volm_land_layer const& line_prop,
vcl_vector<vgl_point_2d<double> >& cross_pts,
vcl_vector<volm_land_layer>& cross_prop)
{
vgl_line_segment_3d<double> l1(vgl_point_3d<double>(seg.point1().x(), seg.point1().y(), 0.0), vgl_point_3d<double>(seg.point2().x(), seg.point2().y(), 0.0));
unsigned n_seg = lines.size();
for (unsigned i = 0; i < n_seg; i++) {
vgl_line_segment_3d<double> l2(vgl_point_3d<double>(lines[i].point1().x(), lines[i].point1().y(), 0.0),
vgl_point_3d<double>(lines[i].point2().x(), lines[i].point2().y(), 0.0));
vgl_point_3d<double> pt;
if (!vgl_intersection(l1, l2, pt))
continue;
cross_pts.push_back(vgl_point_2d<double>(pt.x(), pt.y()));
vcl_pair<int,int> key(seg_prop.id_, line_prop.id_);
cross_prop.push_back(volm_osm_category_io::road_junction_table[key]);
}
}
unsigned count_line_start_from_cross(vgl_point_2d<double> const& cross_pt,
vcl_vector<vgl_point_2d<double> > const& rd,
vcl_vector<vcl_vector<vgl_point_2d<double> > > const& net)
{
unsigned cnt = 0;
// check current road first
vgl_point_2d<double> s = *(rd.begin()); vgl_point_2d<double> e = *(rd.end()-1);
if ( near_eq_pt(cross_pt, s) || near_eq_pt(cross_pt, e))
cnt++;
for (unsigned i = 0; i < net.size(); i++) {
s = *(net[i].begin()); e = *(net[i].end()-1);
if ( near_eq_pt(cross_pt, s) || near_eq_pt(cross_pt, e))
cnt++;
}
return cnt;
}
bool volm_io_tools::search_junctions(vcl_vector<vgl_point_2d<double> > const& road, volm_land_layer const& road_prop,
vcl_vector<vcl_vector<vgl_point_2d<double> > > net, vcl_vector<volm_land_layer> net_props,
vcl_vector<vgl_point_2d<double> >& cross_pts, vcl_vector<volm_land_layer>& cross_props)
{
unsigned n_rds = net.size();
// form the line segment for each road in the network
vcl_vector<vgl_line_segment_2d<double> > road_seg;
form_line_segment_from_pts(road, road_seg);
vcl_vector<vcl_vector<vgl_line_segment_2d<double> > > net_segs;
for (unsigned r_idx = 0; r_idx < n_rds; r_idx++) {
vcl_vector<vgl_line_segment_2d<double> > seg;
form_line_segment_from_pts(net[r_idx], seg);
net_segs.push_back(seg);
}
// find the cross for each segment
unsigned n_seg = road_seg.size();
for (unsigned s_idx = 0; s_idx < n_seg; s_idx++) {
vgl_line_segment_2d<double> curr_seg = road_seg[s_idx];
for (unsigned r_idx = 0; r_idx < n_rds; r_idx++) {
vcl_vector<vgl_line_segment_2d<double> > curr_net_seg = net_segs[r_idx];
vcl_vector<vgl_point_2d<double> > pt;
vcl_vector<volm_land_layer> prop;
find_junctions(curr_seg, road_prop, curr_net_seg, net_props[r_idx], pt, prop);
if (pt.empty())
continue;
if (prop.size() != pt.size())
return false;
for (unsigned p_idx = 0; p_idx < pt.size(); p_idx++)
if (vcl_find(cross_pts.begin(), cross_pts.end(), pt[p_idx]) == cross_pts.end()) {
cross_pts.push_back(pt[p_idx]); cross_props.push_back(prop[p_idx]);
}
}
}
//check whether the cross pt are T_section
//principle, count the number of roads whose end points are on the cross_pt
//if there is 1 or 3 lines start from this cross pt, the cross is a T_section
for (unsigned c_idx = 0; c_idx < cross_pts.size(); c_idx++) {
unsigned num_lines = count_line_start_from_cross(cross_pts[c_idx], road, net);
if (num_lines == 1 || num_lines == 3) {
cross_props[c_idx] = volm_osm_category_io::volm_land_table[241];
}
}
return true;
}