[8f0f49]: src / Panorama / Panorama.cpp Maximize Restore History

Download this file

Panorama.cpp    1724 lines (1526 with data), 57.7 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
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
// -*- c-basic-offset: 4 -*-
/** @file Panorama.cpp
*
* @brief implementation of Panorama Class
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <config.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <iterator>
#include <algorithm>
#include <locale.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <vigra/impex.hxx>
#include <common/stl_utils.h>
#include <PT/Panorama.h>
#include <PT/PanoToolsInterface.h>
#include <PT/RemappedPanoImage.h>
//#include "panoinc.h"
using namespace PT;
using namespace std;
using namespace vigra;
using namespace utils;
//const Map::data_type & map_get(const Map &m, const Map::key_type & key)
const Variable & const_map_get(const VariableMap &m, const string & key)
{
VariableMap::const_iterator it = m.find(key);
if (it != m.end()) {
return (*it).second;
} else {
DEBUG_WARN("could not find " << key);
throw std::out_of_range("No such element in vector");
}
}
// Map::data_type & map_get( Map &m, Map::key_type & key)
Variable & map_get( VariableMap &m, const std::string & key)
{
VariableMap::iterator it = m.find(key);
if (it != m.end()) {
return (*it).second;
} else {
DEBUG_WARN("could not find " << key);
throw std::out_of_range("No such element in vector");
}
}
// Map::data_type & map_get( Map &m, Map::key_type & key)
PT::LensVariable & map_get(PT::LensVarMap &m, const std::string & key)
{
LensVarMap::iterator it = m.find(key);
if (it != m.end()) {
return (*it).second;
} else {
DEBUG_WARN("could not find " << key);
throw std::out_of_range("No such element in vector");
}
}
// Map::data_type & map_get( Map &m, Map::key_type & key)
const PT::LensVariable & const_map_get(const PT::LensVarMap &m, const std::string & key)
{
LensVarMap::const_iterator it = m.find(key);
if (it != m.end()) {
return (*it).second;
} else {
DEBUG_WARN("could not find " << key);
throw std::out_of_range("No such element in vector");
}
}
/// helper functions for parsing of a script line
bool PT::getPTParam(std::string & output, const std::string & line, const std::string & parameter)
{
std::string::size_type p;
if ((p=line.find(std::string(" ") + parameter)) == std::string::npos) {
DEBUG_INFO("could not find param " << parameter
<< " in line: " << line);
return false;
}
p += parameter.length() + 1;
std::string::size_type p2 = line.find(' ',p);
output = line.substr(p, p2-p);
// DEBUG_DEBUG("string idex: " << p <<"," << p2 << " string: \"" << output << "\"");
return true;
}
bool PT::getPTStringParam(std::string & output, const std::string & line, const std::string & parameter)
{
std::string::size_type p;
if ((p=line.find(std::string(" ") + parameter + "\"")) == std::string::npos) {
DEBUG_INFO("could not find string param " << parameter
<< " in line: " << line);
return false;
}
p += parameter.length() + 2;
std::string::size_type e = line.find("\"",p);
DEBUG_DEBUG("p:" << p << " e:" << e);
output = line.substr(p,e-p);
DEBUG_DEBUG("output: ##" << output << "##");
return true;
}
bool PT::getPTStringParamColon(std::string & output, const std::string & line, const std::string & parameter)
{
std::string::size_type p;
if ((p=line.find(std::string(" ") + parameter + ":")) == std::string::npos) {
DEBUG_INFO("could not find string param " << parameter
<< " in line: " << line);
return false;
}
p += parameter.length() + 2;
std::string::size_type e = line.find(" ",p);
DEBUG_DEBUG("p:" << p << " e:" << e);
output = line.substr(p,e-p);
DEBUG_DEBUG("output: ##" << output << "##");
return true;
}
bool PT::getDoubleParam(double & d, const std::string & line, const std::string & name)
{
std::string s;
if (!getPTParam(s, line, name)) {
return false;
}
return stringToDouble(s, d);
}
bool PT::getPTDoubleParam(double & value, int & link,
const std::string & line, const std::string & var)
{
string val;
if (getPTParam(val,line, var)) {
DEBUG_ASSERT(line.size() > 0);
DEBUG_DEBUG(var << ":" <<val);
if (val[0] == '=') {
link = utils::lexical_cast<int>(val.substr(1));
} else {
link = -1;
if (!stringToDouble(val, value)) {
return false;
}
}
} else {
return false;
}
return true;
}
bool PT::readVar(Variable & var, int & link, const std::string & line)
{
string val;
if (getPTParam(val,line, var.getName())) {
DEBUG_ASSERT(line.size() > 0);
DEBUG_DEBUG(var.getName() << ":" <<val);
if (val[0] == '=') {
link = utils::lexical_cast<int>(val.substr(1));
} else {
link = -1;
double dest = 0;
if (!stringToDouble(val, dest)) {
return false;
}
var.setValue(dest);
}
} else {
return false;
}
return true;
}
//=========================================================================
//=========================================================================
Panorama::Panorama()
: currentProcess(NO_PROCESS),
optimizerExe("PTOptimizer"),
stitcherExe("PTStitcher"),
PTScriptFile("PT_script.txt"),
m_forceImagesUpdate(false),
dirty(false)
{
// init map with ptoptimizer variables.
m_ptoptimizerVarNames.insert("a");
m_ptoptimizerVarNames.insert("b");
m_ptoptimizerVarNames.insert("c");
m_ptoptimizerVarNames.insert("d");
m_ptoptimizerVarNames.insert("e");
m_ptoptimizerVarNames.insert("g");
m_ptoptimizerVarNames.insert("t");
m_ptoptimizerVarNames.insert("v");
m_ptoptimizerVarNames.insert("r");
m_ptoptimizerVarNames.insert("p");
m_ptoptimizerVarNames.insert("y");
cerr << "Panorama obj created" << std::endl;
/*
settings.setPath("dangelo","PanoAssistant");
readSettings();
process.setCommunication(QProcess::Stdin|
QProcess::Stdout|
QProcess::Stderr|
QProcess::DupStderr);
connect(&process, SIGNAL(processExited()), this, SLOT(processExited()));
*/
}
Panorama::~Panorama()
{
DEBUG_TRACE("dtor");
reset();
changeFinished();
DEBUG_TRACE("dtor about to finish");
}
void Panorama::reset()
{
//
// imageChanged(0);
// delete all images and control points.
state.ctrlPoints.clear();
state.lenses.clear();
state.images.clear();
state.variables.clear();
state.options.reset();
}
#if 0
QDomElement Panorama::toXML(QDomDocument & doc)
{
QDomElement root = doc.createElement("panorama");
// serialize global options:
root.appendChild(options.toXML(doc));
// serialize image
QDomElement images_ = doc.createElement("images");
for (ImageVector::iterator it = images.begin(); it != images.end(); ++it) {
images_.appendChild(it->toXML(doc));
}
root.appendChild(images_);
// control points
QDomElement cps = doc.createElement("control_points");
for (CPVector::iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
cps.appendChild(it->toXML(doc));
}
root.appendChild(cps);
// lenses
QDomElement lenses_ = doc.createElement("lenses");
for (std::vector<Lens>::iterator it = lenses.begin(); it != lenses.end(); ++it) {
lenses_.appendChild(it->toXML(doc));
}
root.appendChild(lenses_);
return root;
}
void Panorama::setFromXML(const QDomNode & elem)
{
DEBUG_DEBUG("Panorama::setFromXML");
clear();
Q_ASSERT(elem.nodeName() == "panorama");
// read global options
QDomNode n = elem.namedItem("output");
Q_ASSERT(!n.isNull);
options.setFromXML(n);
n = elem.namedItem("images");
Q_ASSERT(!n.isNull);
n = n.firstChild();
while( !n.isNull() ) {
QDomElement e = n.toElement(); // try to convert the node to an element.
Q_ASSERT((!e.isNull()) && e.tagName() == "image" );
images.push_back(PanoImage(*this, e));
reportAddedImage(images.size() -1);
n = n.nextSibling();
}
n = elem.namedItem("control_points");
Q_ASSERT(!n.isNull());
n = n.firstChild();
while( !n.isNull() ) {
QDomElement e = n.toElement();// try to convert the node to an element.
Q_ASSERT((!e.isNull()) && e.tagName() == "control_point" );
controlPoints.push_back(ControlPoint(*this, e));
reportAddedCtrlPoint(controlPoints.size() -1);
n = n.nextSibling();
}
}
#endif
std::vector<unsigned int> Panorama::getCtrlPointsForImage(unsigned int imgNr) const
{
std::vector<unsigned int> result;
unsigned int i = 0;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if ((it->image1Nr == imgNr) || (it->image2Nr == imgNr)) {
result.push_back(i);
}
i++;
}
return result;
}
const VariableMapVector & Panorama::getVariables() const
{
return state.variables;
}
const VariableMap & Panorama::getImageVariables(unsigned int imgNr) const
{
assert(imgNr < state.images.size());
return state.variables[imgNr];
}
FDiff2D Panorama::calcFOV() const
{
Size2D panoSize(360,180);
// remap into minature pano.
PanoramaOptions opts;
opts.setHFOV(360);
opts.setProjection(PanoramaOptions::EQUIRECTANGULAR);
opts.setWidth(360);
opts.setHeight(180);
// remap image
// DGSW - make sure the type is correct
vigra::BImage panoAlpha(panoSize.x, panoSize.y,static_cast< unsigned char >(0));
// vigra::BImage panoAlpha(panoSize.x, panoSize.y,0);
RemappedPanoImage<vigra::BImage, vigra::BImage> remapped;
UIntSet activeImgs = getActiveImages();
for (UIntSet::iterator it = activeImgs.begin(); it != activeImgs.end(); ++it) {
// for (unsigned int imgNr=0; imgNr < getNrOfImages(); imgNr++) {
const PanoImage & img = getImage(*it);
remapped.setPanoImage(getSrcImage(*it), opts.getDestImage());
//remapped.setPanoImage(*this, *it, vigra::Size2D(img.getWidth(), img.getHeight()), opts);
// calculate alpha channel
remapped.calcAlpha();
// copy into global alpha channel.
vigra::copyImageIf(vigra_ext::applyRect(remapped.boundingBox(),
vigra_ext::srcMaskRange(remapped)),
vigra_ext::applyRect(remapped.boundingBox(),
vigra_ext::srcMask(remapped)),
vigra_ext::applyRect(remapped.boundingBox(),
destImage(panoAlpha)));
// vigra::ImageExportInfo imge2("c:/hugin_calcfov_alpha.png");
// exportImage(vigra::srcImageRange(panoAlpha), imge2);
}
// get field of view
FDiff2D ul,lr;
bool found = false;
ul.x = DBL_MAX;
ul.y = DBL_MAX;
lr.x = -DBL_MAX;
lr.y = -DBL_MAX;
for (int v=0; v< 180; v++) {
for (int h=0; h < 360; h++) {
if (panoAlpha(h,v)) {
// pixel is valid
if ( ul.x > h ) {
found=true;
ul.x = h;
}
if ( ul.y > v ) {
found=true;
ul.y = v;
}
if ( lr.x < h) {
found=true;
lr.x = h;
}
if ( lr.y < v) {
found=true;
lr.y = v;
}
}
}
}
if (!found) {
// if nothing found, return current fov
return FDiff2D(state.options.getHFOV(), state.options.getVFOV());
}
ul.x = ul.x - 180;
ul.y = ul.y - 90;
lr.x = lr.x - 180;
lr.y = lr.y - 90;
FDiff2D fov (2*max(fabs(ul.x), fabs(lr.x)), 2*max(fabs(ul.y), fabs(lr.y)));
return fov;
}
void Panorama::centerHorizontically()
{
Size2D panoSize(360,180);
// remap into minature pano.
PanoramaOptions opts;
opts.setHFOV(360);
opts.setProjection(PanoramaOptions::EQUIRECTANGULAR);
opts.setWidth(360);
opts.setHeight(180);
// remap image
vigra::BImage panoAlpha(panoSize);
RemappedPanoImage<vigra::BImage, vigra::BImage> remapped;
// use selected images.
UIntSet activeImgs = getActiveImages();
for (UIntSet::iterator it = activeImgs.begin(); it != activeImgs.end(); ++it) {
// for (unsigned int imgNr=0; imgNr < getNrOfImages(); imgNr++) {
// const PanoImage & img = getImage(*it);
// Size2D sz(img.getWidth(), img.getHeight());
// remapped.setPanoImage(*this, *it, sz, opts);
remapped.setPanoImage(getSrcImage(*it), opts.getDestImage());
// calculate alpha channel
remapped.calcAlpha();
// copy into global alpha channel.
vigra::copyImageIf(vigra_ext::applyRect(remapped.boundingBox(),
vigra_ext::srcMaskRange(remapped)),
vigra_ext::applyRect(remapped.boundingBox(),
vigra_ext::srcMask(remapped)),
vigra_ext::applyRect(remapped.boundingBox(),
destImage(panoAlpha)));
}
// vigra::ImageExportInfo imge("c:/hugin_calcfov_alpha.png");
// exportImage(vigra::srcImageRange(panoAlpha), imge);
// get field of view
std::vector<int> borders;
bool colOccupied = false;
for (int h=0; h < 360; h++) {
bool curColOccupied = false;
for (int v=0; v< 180; v++) {
if (panoAlpha(h,v)) {
// pixel is valid
curColOccupied = true;
}
}
if (colOccupied && (! curColOccupied) ||
(!colOccupied) && curColOccupied )
{
// change in position, save point.
borders.push_back(h-180);
colOccupied = curColOccupied;
}
}
size_t lastidx = borders.size() -1;
if (lastidx == -1) {
// empty pano
return;
}
if (colOccupied) {
// we have reached the right border, and the pano is still valid
// shift right fragments by 360 deg
// |11 2222| -> | 222211 |
std::vector<int> newBorders;
newBorders.push_back(borders[lastidx]);
for (size_t i = 0; i < lastidx; i++) {
newBorders.push_back(borders[i]+360);
}
borders = newBorders;
}
double dYaw=(borders[0] + borders[lastidx])/2;
// apply yaw shift
unsigned int nImg = getNrOfImages();
for (unsigned int i=0; i < nImg; i++) {
Variable & v = map_get(state.variables[i], "y");
double yaw = v.getValue();
yaw = yaw - dYaw;
while (yaw < 180) {
yaw += 360;
}
while (yaw > 180) {
yaw -= 360;
}
v.setValue(yaw);
imageChanged(i);
}
}
void Panorama::updateCtrlPointErrors(const CPVector & cps)
{
assert(cps.size() == state.ctrlPoints.size());
unsigned int nrp = cps.size();
for (unsigned int i = 0; i < nrp ; i++) {
imageChanged(state.ctrlPoints[i].image1Nr);
imageChanged(state.ctrlPoints[i].image2Nr);
state.ctrlPoints[i].error = cps[i].error;
}
}
void Panorama::updateVariables(const VariableMapVector & vars)
{
assert(vars.size() == state.images.size());
unsigned int i = 0;
for (VariableMapVector::const_iterator it = vars.begin(); it != vars.end(); ++it) {
updateVariables(i, *it);
i++;
}
}
void Panorama::updateVariables(unsigned int imgNr, const VariableMap & var)
{
assert(imgNr < state.images.size());
for (VariableMap::const_iterator it = var.begin(); it != var.end() ; ++it) {
updateVariable(imgNr,it->second);
}
}
void Panorama::updateVariable(unsigned int imgNr, const Variable &var)
{
// DEBUG_TRACE("image " << imgNr << " variable: " << var.getName());
DEBUG_ASSERT(imgNr < state.images.size());
// update a single variable
// check corrosponding lens if we have to update some other images
// as well.
unsigned int lensNr = state.images[imgNr].getLensNr();
DEBUG_ASSERT(lensNr < state.lenses.size());
// update value for this image
// state.variables[imgNr][var.getName()].setValue(var.getValue());
const std::string & s = var.getName();
Variable & tvar = map_get(state.variables[imgNr], s);
tvar.setValue(var.getValue());
bool lensVar = set_contains(state.lenses[lensNr].variables, var.getName());
if (lensVar) {
// special handling for lens variables.
// if they are inherited, update the value in the lens, and all
// image variables that use this lens.
LensVariable & lv = map_get(state.lenses[lensNr].variables,var.getName());
if (lv.isLinked()) {
// DEBUG_DEBUG("updating image variable, lens var is linked");
lv.setValue(var.getValue());
updateLensVariable(lensNr,lv);
}
}
imageChanged(imgNr);
}
void Panorama::setOptimizeVector(const OptimizeVector & optvec)
{
DEBUG_ASSERT(optvec.size() == state.images.size());
state.optvec = optvec;
}
unsigned int Panorama::addImage(const PanoImage &img, const VariableMap & vars)
{
// the lens must have been already created!
bool ok = img.getLensNr() < state.lenses.size();
DEBUG_ASSERT(ok);
unsigned int nr = state.images.size();
state.images.push_back(img);
state.variables.push_back(vars);
copyLensVariablesToImage(nr);
imageChanged(nr);
return nr;
}
#if 0
unsigned int Panorama::addImage(const std::string & filename)
{
// create a lens if we don't have one.
if (state.lenses.size() < 1) {
state.lenses.push_back(Lens());
}
// read lens spec from image, if possible
// FIXME use a lens database (for example the one from PTLens)
// FIXME to initialize a,b,c etc.
// searches for the new image for an unused lens , if found takes this
// if no free lens is available creates a new one
int unsigned lensNr (0);
bool lens_belongs_to_image = false;
int unused_lens = -1;
while ( unused_lens < 0 ) {
lens_belongs_to_image = false;
for (ImageVector::iterator it = state.images.begin();
it != state.images.end() ; ++it) {
if ((*it).getLens() == lensNr)
lens_belongs_to_image = true;
}
if ( lens_belongs_to_image == false )
unused_lens = lensNr;
else
lensNr++;
}
bool lens_allready_inside = false;
for ( lensNr = 0 ; lensNr < state.lenses.size(); ++lensNr) {
if ( (int)lensNr == unused_lens )
lens_allready_inside = true;
}
// DEBUG_INFO ( "lens_allready_inside= "<< lens_allready_inside <<" new lensNr: " << unused_lens <<"/"<< state.lenses.size() )
Lens l;
if ( lens_allready_inside )
l = state.lenses[unused_lens];
l.readEXIF(filename);
if ( lens_allready_inside ) {
state.lenses[unused_lens] = l;
} else {
state.lenses.push_back(l);
unused_lens = state.lenses.size() - 1;
}
unsigned int nr = state.images.size();
state.images.push_back(PanoImage(filename));
ImageOptions opts = state.images.back().getOptions();
opts.lensNr = unused_lens;
state.images.back().setOptions(opts);
state.variables.push_back(ImageVariables());
updateLens(nr);
adjustVarLinks();
imageChanged(nr);
DEBUG_INFO ( "new lensNr: " << unused_lens <<"/"<< state.lenses.size() )
return nr;
}
#endif
void Panorama::removeImage(unsigned int imgNr)
{
DEBUG_DEBUG("Panorama::removeImage(" << imgNr << ")");
assert(imgNr < state.images.size());
// remove control points
CPVector::iterator it = state.ctrlPoints.begin();
while (it != state.ctrlPoints.end()) {
if ((it->image1Nr == imgNr) || (it->image2Nr == imgNr)) {
// remove point that refernce to imgNr
it = state.ctrlPoints.erase(it);
} else {
// correct point references
if (it->image1Nr > imgNr) it->image1Nr--;
if (it->image2Nr > imgNr) it->image2Nr--;
++it;
}
}
// remove Lens if needed
bool removeLens = true;
unsigned int lens = state.images[imgNr].getLensNr();
unsigned int i = 0;
for (ImageVector::iterator it = state.images.begin(); it != state.images.end(); ++it) {
if ((*it).getLensNr() == lens && imgNr != i) {
removeLens = false;
}
i++;
}
if (removeLens) {
DEBUG_TRACE("removing lens " << lens);
for (ImageVector::iterator it = state.images.begin(); it != state.images.end(); ++it) {
if((*it).getLensNr() >= lens) {
(*it).setLensNr((*it).getLensNr() - 1);
imageChanged(it - state.images.begin());
}
}
state.lenses.erase(state.lenses.begin() + lens);
}
DEBUG_TRACE("Remove variables and image from panorama state")
state.variables.erase(state.variables.begin() + imgNr);
state.images.erase(state.images.begin() + imgNr);
// check if reference image has been moved
if (state.options.optimizeReferenceImage >= state.images.size()) {
state.options.optimizeReferenceImage = 0;
imageChanged(state.options.optimizeReferenceImage);
}
if (state.options.colorReferenceImage >= state.images.size()) {
state.options.colorReferenceImage = 0;
imageChanged(state.options.colorReferenceImage);
}
// change all other (moved) images
DEBUG_TRACE("flag moved images as dirty");
for (unsigned int i=imgNr; i < state.images.size(); i++) {
imageChanged(i);
}
m_forceImagesUpdate = true;
}
void Panorama::setImageFilename(unsigned int i, const std::string & fname)
{
DEBUG_ASSERT(i < state.images.size());
state.images[i].setFilename(fname);
imageChanged(i);
m_forceImagesUpdate = true;
}
void Panorama::setImageOptions(unsigned int i, const ImageOptions & opts)
{
DEBUG_ASSERT(i < state.images.size());
state.images[i].setOptions(opts);
imageChanged(i);
m_forceImagesUpdate = true;
}
unsigned int Panorama::addCtrlPoint(const ControlPoint & point )
{
unsigned int nr = state.ctrlPoints.size();
state.ctrlPoints.push_back(point);
imageChanged(point.image1Nr);
imageChanged(point.image2Nr);
return nr;
}
void Panorama::removeCtrlPoint(unsigned int pNr)
{
DEBUG_ASSERT(pNr < state.ctrlPoints.size());
ControlPoint & point = state.ctrlPoints[pNr];
unsigned int i1 = point.image1Nr;
unsigned int i2 = point.image2Nr;
state.ctrlPoints.erase(state.ctrlPoints.begin() + pNr);
imageChanged(i1);
imageChanged(i2);
}
void Panorama::changeControlPoint(unsigned int pNr, const ControlPoint & point)
{
assert(pNr < state.ctrlPoints.size());
// change notify for all involved images
imageChanged(state.ctrlPoints[pNr].image1Nr);
imageChanged(state.ctrlPoints[pNr].image2Nr);
imageChanged(point.image1Nr);
imageChanged(point.image2Nr);
state.ctrlPoints[pNr] = point;
}
void Panorama::printPanoramaScript(ostream & o,
const OptimizeVector & optvars,
const PanoramaOptions & output,
const UIntSet & imgs,
bool forPTOptimizer,
const std::string & stripPrefix)
{
#ifdef __unix__
// set numeric locale to C, for correct number output
char * t = setlocale(LC_NUMERIC,NULL);
char * old_locale = (char*) malloc(strlen(t)+1);
strcpy(old_locale, t);
setlocale(LC_NUMERIC,"C");
#endif
if (forPTOptimizer) {
o << "# PTOptimizer script, written by hugin" << std::endl
<< std::endl;
} else {
o << "# hugin project file, version 1" << std::endl;
}
// output options..
output.printScriptLine(o);
std::map<unsigned int, unsigned int> linkAnchors;
// map from script img nr -> pano image nr
std::map<unsigned int, unsigned int> imageNrMap;
o << std::endl
<< "# image lines" << std::endl;
unsigned int ic = 0;
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
++imgNrIt)
{
unsigned int imgNr = *imgNrIt;
imageNrMap[imgNr] = ic;
PanoImage & img = state.images[imgNr];
unsigned int lensNr = img.getLensNr();
Lens & lens = state.lenses[lensNr];
const VariableMap & vars = state.variables[imgNr];
o << "i w" << img.getWidth() << " h" << img.getHeight()
<<" f" << lens.getProjection() << " ";
// print variables with links
for (VariableMap::const_iterator vit = vars.begin();
vit != vars.end(); ++vit)
{
bool ptoptvar = set_contains(m_ptoptimizerVarNames,vit->first);
if (!ptoptvar && forPTOptimizer) {
continue;
}
// print links if needed
if (set_contains(lens.variables,vit->first)
&& map_get(lens.variables, vit->first).isLinked())
{
if (set_contains(linkAnchors, lensNr)
&& linkAnchors[lensNr] != imageNrMap[imgNr])
{
// print link
DEBUG_DEBUG("printing link: " << vit->first);
// print link, anchor variable was already printed
map_get(lens.variables,vit->first).printLink(o,linkAnchors[lensNr]) << " ";
} else {
DEBUG_DEBUG("printing value for linked var " << vit->first);
// first time, print value
linkAnchors[lensNr] = imageNrMap[imgNr];
if ( ( (vit->first == "a" && set_contains(optvars[imgNr], "a") )||
(vit->first == "b" && set_contains(optvars[imgNr], "b") )||
(vit->first == "c" && set_contains(optvars[imgNr], "c") )
)
&& forPTOptimizer && vit->second.getValue() == 0.0)
{
// work around a bug in PTOptimizer, a,b,c values will only be
// optmized if nonzero
o << vit->first << 1e-5 << " ";
} else {
vit->second.print(o) << " ";
}
}
} else {
if (( (vit->first == "a" && set_contains(optvars[imgNr], "a") )||
(vit->first == "b" && set_contains(optvars[imgNr], "b") )||
(vit->first == "c" && set_contains(optvars[imgNr], "c") )
)
&& forPTOptimizer && vit->second.getValue() == 0.0)
{
// work around a bug in PTOptimizer, a,b,c values will only be optmized
// if nonzero
o << vit->first << 1e-5 << " ";
} else {
vit->second.print(o) << " ";
}
}
}
ImageOptions iopts = img.getOptions();
if (iopts.docrop) {
// print crop parameters
vigra::Rect2D c = iopts.cropRect;
o << " S" << c.left() << "," << c.right() << "," << c.top() << "," << c.bottom();
}
if (!forPTOptimizer) {
if (iopts.m_vigCorrMode != ImageOptions::VIGCORR_NONE) {
o << " Vm" << iopts.m_vigCorrMode;
}
if (iopts.m_flatfield.size() > 0) {
o << " Vf\"" << iopts.m_flatfield << "\"";
}
}
o << " u" << output.featherWidth
<< (img.getOptions().morph ? " o" : "");
string fname = img.getFilename();
if (stripPrefix.size() > 0) {
// strip prefix from image names.
// check if the prefix is acutally the same
string tmp = fname.substr(0,stripPrefix.size());
if (tmp.compare(stripPrefix) == 0) {
DEBUG_DEBUG("striping " << stripPrefix << " from " << fname);
fname = fname.erase(0,stripPrefix.size());
DEBUG_DEBUG("after stripping: " << fname);
} else {
DEBUG_DEBUG(stripPrefix << " does not match " << fname);
}
}
o << " n\"" << fname << "\"" << std::endl;
ic++;
}
o << std::endl << std::endl
<< "# specify variables that should be optimized" << std::endl
<< "v ";
int optVarCounter=0;
// be careful. linked variables should not be specified multiple times.
vector<set<string> > linkvars(state.lenses.size());
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
++imgNrIt)
{
unsigned int i = *imgNrIt;
unsigned int lensNr = state.images[i].getLensNr();
const Lens & lens = state.lenses[lensNr];
const set<string> & optvar = optvars[i];
for (set<string>::const_iterator sit = optvar.begin();
sit != optvar.end(); ++sit )
{
if (set_contains(lens.variables,*sit)) {
// it is a lens variable
if (const_map_get(lens.variables,*sit).isLinked()) {
if (! set_contains(linkvars[lensNr], *sit))
{
// print only once
o << *sit << imageNrMap[i] << " ";
linkvars[lensNr].insert(*sit);
optVarCounter++;
}
} else {
// unlinked lens variable, print as usual
o << *sit << imageNrMap[i] << " ";
optVarCounter++;
}
} else {
// not a lens variable, print multiple times
o << *sit << imageNrMap[i] << " ";
optVarCounter++;
}
}
// insert line break after 10 variables
if (optVarCounter > 0) {
o << std::endl << "v ";
optVarCounter = 0;
}
}
o << std::endl << std::endl
<< "# control points" << std::endl;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
o << "c n" << imageNrMap[it->image1Nr]
<< " N" << imageNrMap[it->image2Nr]
<< " x" << it->x1 << " y" << it->y1
<< " X" << it->x2 << " Y" << it->y2
<< " t" << it->mode << std::endl;
}
}
o << std::endl;
// special line with hugins options.
o << "#hugin_options r" << output.optimizeReferenceImage;
switch (output.blendMode) {
case PanoramaOptions::NO_BLEND:
o << " e0";
break;
case PanoramaOptions::WEIGHTED_BLEND:
o << " e1";
break;
case PanoramaOptions::SPLINE_BLEND:
o << " e2";
break;
case PanoramaOptions::CHESSBOARD_BLEND:
o << " e3";
break;
}
o << std::endl;
#ifdef __unix__
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
#endif
}
void Panorama::printStitcherScript(ostream & o,
const PanoramaOptions & target,
const UIntSet & imgs) const
{
#ifdef __unix__
// set numeric locale to C, for correct number output
char * t = setlocale(LC_NUMERIC,NULL);
char * old_locale = (char*) malloc(strlen(t)+1);
strcpy(old_locale, t);
setlocale(LC_NUMERIC,"C");
#endif
o << "# PTStitcher script, written by hugin" << std::endl
<< std::endl;
// output options..
target.printScriptLine(o);
o << std::endl
<< "# output image lines" << std::endl;
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end(); ++imgNrIt) {
unsigned int imgNr = *imgNrIt;
const PanoImage & img = state.images[imgNr];
unsigned int lensNr = img.getLensNr();
const Lens & lens = state.lenses[lensNr];
const VariableMap & vars = state.variables[imgNr];
o << "o w" << img.getWidth() << " h" << img.getHeight()
<<" f" << state.lenses[lensNr].getProjection() << " ";
// print variables, without links
VariableMap::const_iterator vit;
for(vit = vars.begin(); vit != vars.end(); ++vit)
{
if (!set_contains(m_ptoptimizerVarNames,vit->first)) {
continue;
}
vit->second.print(o) << " ";
}
o << " u" << target.featherWidth << " m" << img.getOptions().ignoreFrameWidth
<< (img.getOptions().morph ? " o" : "")
<< " n\"" << img.getFilename() << "\"";
if (img.getOptions().docrop) {
// print crop parameters
vigra::Rect2D c = img.getOptions().cropRect;
o << " C" << c.left() << "," << c.right() << "," << c.top() << "," << c.bottom();
}
o << std::endl;
}
o << std::endl;
#ifdef __unix__
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
#endif
}
void Panorama::readOptimizerOutput(const UIntSet & imgs, VariableMapVector & vars, CPVector & ctrlPoints) const
{
std::ifstream script(PTScriptFile.c_str());
if (!script.good()) {
DEBUG_ERROR("Could not open " << PTScriptFile);
// throw execption
return;
}
parseOptimizerScript(script, imgs, vars, ctrlPoints);
}
void Panorama::parseOptimizerScript(istream & i, const UIntSet & imgs,
VariableMapVector & imgVars, CPVector & CPs) const
{
DEBUG_TRACE("");
#ifdef __unix__
// set numeric locale to C, for correct number output
char * t = setlocale(LC_NUMERIC,NULL);
char * old_locale = (char*) malloc(strlen(t)+1);
strcpy(old_locale, t);
setlocale(LC_NUMERIC,"C");
#endif
unsigned int ic=0;
std::map<unsigned int, unsigned int> script2ImgMap;
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
++imgNrIt)
{
unsigned int imgNr = *imgNrIt;
script2ImgMap[ic] = imgNr;
ic++;
}
ic = 0;
unsigned int sc = 0;
std::map<unsigned int, unsigned int> script2CPMap;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
script2CPMap[sc] = ic;
sc++;
}
ic++;
}
// 0 = read output (image lines), 1 = read control point distances
int state = 0;
string line;
unsigned int lineNr = 0;
unsigned int scriptImgCounter = 0;
unsigned int scriptCPCounter = 0;
// VariableMapVector::iterator varIt = imgVars.begin();
// CPVector::iterator pointIt = CPs.begin();
int pnr=0;
while (!i.eof()) {
std::getline(i, line);
lineNr++;
switch (state) {
case 0:
{
// we are reading the output lines:
// o f3 r0 p0 y0 v89.2582 a-0.027803 b0.059851 c-0.073115 d10.542470 e16.121145 u10 -buf
if ((line.compare("# Control Points: Distance between desired and fitted Position") == 0 )
|| (line.compare("# Control Points: Distance between desired and fitted Position (in Pixels)") == 0 )
|| (line.compare("# Control Points: Distance between desired and fitted Position (in \"Pixels\")") == 0 )) {
// switch to reading the control point distance
if (scriptImgCounter != imgs.size()) {
DEBUG_ERROR("Read only " << scriptImgCounter << " images from PTOptimizer file");
}
DEBUG_DEBUG("Changing state to read control point distances");
state = 1;
break;
}
if (line[0] != 'o') continue;
// select variables of the image
VariableMap & var = imgVars[script2ImgMap[scriptImgCounter]];
DEBUG_DEBUG("reading image variables for image:" << scriptImgCounter);
// read position variables
int link;
readVar(map_get(var, "r"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "p"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "y"), link, line);
DEBUG_ASSERT(link == -1);
DEBUG_DEBUG("yaw: " << map_get(var, "y").getValue()
<< " pitch " << map_get(var, "p").getValue()
<< " roll " << map_get(var, "r").getValue());
// read lens variables
for (char **c = Lens::variableNames; *c != 0; ++c) {
Variable & curVar = map_get(var, *c);
if (!readVar(curVar, link, line)) {
DEBUG_ERROR("Could not read "<< *c << " at script line " << lineNr);
}
// linking in output forbidden
DEBUG_ASSERT(link == -1);
}
scriptImgCounter++;
break;
}
case 1:
{
// read ctrl point distances:
// # Control Point No 0: 0.428994
if (line[0] == 'C') {
// DEBUG_DEBUG(CPs.size() << " points, read: " << pnr);
state = 2;
break;
}
if (line.find("# Control Point No") != 0) continue;
DEBUG_DEBUG("reading cp dist line: " << line);
string::size_type p;
if ((p=line.find(':')) == string::npos) assert(0);
p++;
DEBUG_DEBUG("parsing point " << scriptCPCounter << " (idx:" << p << "): " << line.substr(p));
double err = -1;
utils::stringToDouble(line.substr(p), err);
CPs[script2CPMap[scriptCPCounter]].error = err;
DEBUG_DEBUG("read CP distance " << err);
scriptCPCounter++;
break;
}
default:
// ignore line..
break;
}
}
#ifdef __unix__
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
#endif
}
void Panorama::changeFinished(bool keepDirty)
{
if (state.images.size() == 0) {
// force an empty update if all images have been
// removed
DEBUG_DEBUG("forcing images update, with no images");
m_forceImagesUpdate = true;
}
// remove change notification for nonexisting images from set.
UIntSet::iterator uB = changedImages.lower_bound(state.images.size());
changedImages.erase(uB,changedImages.end());
stringstream t;
copy(changedImages.begin(), changedImages.end(),
ostream_iterator<unsigned int>(t, " "));
DEBUG_TRACE("changed image(s) " << t.str() << " begin");
std::set<PanoramaObserver *>::iterator it;
for(it = observers.begin(); it != observers.end(); ++it) {
DEBUG_TRACE("notifying listener");
if (changedImages.size() > 0 || m_forceImagesUpdate) {
(*it)->panoramaImagesChanged(*this, changedImages);
}
(*it)->panoramaChanged(*this);
}
// reset changed images
changedImages.clear();
m_forceImagesUpdate = false;
if (!keepDirty) {
dirty = true;
}
DEBUG_TRACE("end");
}
const Lens & Panorama::getLens(unsigned int lensNr) const
{
assert(lensNr < state.lenses.size());
return state.lenses[lensNr];
}
void Panorama::updateLens(unsigned int lensNr, const Lens & lens)
{
DEBUG_TRACE(lensNr << " contains " << lens.variables.size() << " variables");
assert(lensNr < state.lenses.size());
state.lenses[lensNr].update(lens);
// DEBUG_DEBUG("after update: " << lens.variables.size() << " variables");
// copy changes to images
for (LensVarMap::const_iterator it = state.lenses[lensNr].variables.begin();
it != state.lenses[lensNr].variables.end();
++it)
{
DEBUG_DEBUG("updating " << it->second.getName() << " (key: " << it->first << ")");
updateLensVariable(lensNr, it->second);
}
}
void Panorama::updateLensVariable(unsigned int lensNr, const LensVariable &var)
{
DEBUG_TRACE("lens " << lensNr << " variable: " << var.getName());
DEBUG_ASSERT(lensNr < state.lenses.size());
std::string varname = var.getName();
LensVariable & realvar = map_get(state.lenses[lensNr].variables, varname);
realvar = var;
unsigned int nImages = state.images.size();
for (unsigned int i=0; i<nImages; i++) {
if (state.images[i].getLensNr() == lensNr) {
// FIXME check for if really changed?
imageChanged(i);
map_get(state.variables[i], var.getName()).setValue(var.getValue());
// check if the crop area should be automatically centered
if ( var.getName() == "d" ) {
ImageOptions opts = state.images[i].getOptions();
if (opts.docrop && opts.autoCenterCrop) {
// horizontally center crop area.
double center = state.images[i].getWidth() / 2.0 + var.getValue();
int left = roundi(center - opts.cropRect.width() / 2.0);
int right = roundi(center + opts.cropRect.width() / 2.0);
opts.cropRect.setUpperLeft(Point2D(left, opts.cropRect.top()));
opts.cropRect.setLowerRight(Point2D(right, opts.cropRect.bottom()));
state.images[i].setOptions(opts);
}
}
if ( var.getName() == "e" ) {
ImageOptions opts = state.images[i].getOptions();
if (opts.docrop && opts.autoCenterCrop) {
// horizontally center crop area.
double center = state.images[i].getHeight() / 2.0 + var.getValue();
int top = roundi(center - opts.cropRect.height() / 2.0);
int bottom = roundi(center + opts.cropRect.height() / 2.0);
opts.cropRect.setUpperLeft(Point2D(opts.cropRect.left(), top));
opts.cropRect.setLowerRight(Point2D(opts.cropRect.right(), bottom));
state.images[i].setOptions(opts);
}
}
}
}
}
void Panorama::setLens(unsigned int imgNr, unsigned int lensNr)
{
DEBUG_TRACE("img: " << imgNr << " lens:" << lensNr);
assert(lensNr < state.lenses.size());
assert(imgNr < state.images.size());
state.images[imgNr].setLensNr(lensNr);
imageChanged(imgNr);
// copy the whole lens settings into the image
copyLensVariablesToImage(imgNr);
removeUnusedLenses();
}
void Panorama::swapImages(unsigned int img1, unsigned int img2)
{
DEBUG_TRACE("swapping images " << img1 << ", " << img2);
DEBUG_ASSERT(img1 < state.images.size());
DEBUG_ASSERT(img2 < state.images.size());
// first, swap image struct
PanoImage pimg1 = state.images[img1];
state.images[img1] = state.images[img2];
state.images[img2] = pimg1;
// swap variables
VariableMap vars1 = state.variables[img1];
state.variables[img1] = state.variables[img2];
state.variables[img2] = vars1;
// update control points
for (CPVector::iterator it=state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
int n1 = (*it).image1Nr;
int n2 = (*it).image2Nr;
if ((*it).image1Nr == img1) {
n1 = img2;
} else if ((*it).image1Nr == img2) {
n1 = img1;
}
if ((*it).image2Nr == img1) {
n2 = img2;
} else if ((*it).image2Nr == img2) {
n2 = img1;
}
(*it).image1Nr = n1;
(*it).image2Nr = n2;
}
// update panorama options
if (state.options.colorReferenceImage == img1) {
state.options.colorReferenceImage = img2;
} else if (state.options.colorReferenceImage == img2) {
state.options.colorReferenceImage = img1;
}
if (state.options.optimizeReferenceImage == img1) {
state.options.optimizeReferenceImage = img2;
} else if (state.options.optimizeReferenceImage == img2) {
state.options.optimizeReferenceImage = img1;
}
imageChanged(img1);
imageChanged(img2);
}
void Panorama::removeUnusedLenses()
{
for (unsigned int lNr=0; lNr < state.lenses.size(); lNr++) {
// check if this lens is lonely
int n=0;
for (unsigned int iNr=0; iNr < state.images.size(); iNr++) {
if (state.images[iNr].getLensNr() == lNr) {
n++;
}
}
if (n == 0) {
// not used by any image, remove from vector
LensVector::iterator it = state.lenses.begin();
it = it + lNr;
state.lenses.erase(it);
// adjust lens numbers inside images
for (unsigned int iNr=0; iNr < state.images.size(); iNr++) {
unsigned int imgLensNr = state.images[iNr].getLensNr();
assert(imgLensNr != lNr);
if ( imgLensNr > lNr) {
state.images[iNr].setLensNr(imgLensNr-1);
imageChanged(iNr);
}
}
}
}
}
void Panorama::removeLens(unsigned int lensNr)
{
DEBUG_ASSERT(lensNr < state.lenses.size());
// it is an error to remove all lenses.
DEBUG_ASSERT(state.images.size() == 0 || lensNr > 0);
for (unsigned int i = 0; i < state.images.size(); i++) {
if (state.images[i].getLensNr() == lensNr) {
state.images[i].setLensNr(0);
copyLensVariablesToImage(i);
imageChanged(i);
}
}
}
unsigned int Panorama::addLens(const Lens & lens)
{
state.lenses.push_back(lens);
return state.lenses.size() - 1;
}
void Panorama::setMemento(PanoramaMemento & memento)
{
DEBUG_TRACE("");
// remove old content.
reset();
DEBUG_DEBUG("nr of images in memento:" << memento.images.size());
state = memento;
unsigned int nNewImages = state.images.size();
DEBUG_DEBUG("nNewImages:" << nNewImages);
// send changes for all images
for (unsigned int i = 0; i < nNewImages; i++) {
imageChanged(i);
}
}
PanoramaMemento Panorama::getMemento(void) const
{
return PanoramaMemento(state);
}
void Panorama::setOptions(const PanoramaOptions & opt)
{
if (state.options.optimizeReferenceImage != opt.optimizeReferenceImage) {
imageChanged(opt.optimizeReferenceImage);
imageChanged(state.options.optimizeReferenceImage);
}
if (state.options.colorReferenceImage != opt.colorReferenceImage) {
imageChanged(opt.colorReferenceImage);
imageChanged(state.options.colorReferenceImage);
}
state.options = opt;
}
int Panorama::addImageAndLens(const std::string & filename, double HFOV)
{
// load image
vigra::ImageImportInfo img(filename.c_str());
Lens lens;
lens.setImageSize(vigra::Size2D(img.width(), img.height()));
map_get(lens.variables,"v").setValue(HFOV);
double cropFactor = 0;
lens.initFromFile(filename, cropFactor);
int matchingLensNr=-1;
for (unsigned int lnr=0; lnr < getNrOfLenses(); lnr++) {
const Lens & l = getLens(lnr);
// use a lens if hfov and ratio are the same
// should add a check for exif camera information as
// well.
if ((l.getAspectRatio() == lens.getAspectRatio()) &&
(const_map_get(l.variables,"v").getValue() == const_map_get(lens.variables,"v").getValue()) &&
(l.getSensorSize() == lens.getSensorSize()))
{
matchingLensNr= lnr;
}
}
if (matchingLensNr == -1) {
matchingLensNr = addLens(lens);
}
VariableMap vars;
fillVariableMap(vars);
DEBUG_ASSERT(matchingLensNr >= 0);
PanoImage pimg(filename, img.width(), img.height(), (unsigned int) matchingLensNr);
return addImage(pimg, vars);
}
void Panorama::addObserver(PanoramaObserver * o)
{
observers.insert(o);
}
bool Panorama::removeObserver(PanoramaObserver * o)
{
return observers.erase(o) > 0;
}
void Panorama::clearObservers()
{
observers.clear();
}
void Panorama::imageChanged(unsigned int imgNr)
{
// DEBUG_TRACE("adding image " << imgNr);
changedImages.insert(imgNr);
assert(changedImages.find(imgNr) != changedImages.end());
}
void Panorama::activateImage(unsigned int imgNr, bool active)
{
assert(imgNr < state.images.size());
ImageOptions o = getImage(imgNr).getOptions();
if (o.active != active) {
o.active = active;
state.images[imgNr].setOptions(o);
imageChanged(imgNr);
}
}
UIntSet Panorama::getActiveImages() const
{
UIntSet activeImgs;
for (unsigned int i = 0; i < state.images.size(); i++) {
if (state.images[i].getOptions().active) {
activeImgs.insert(i);
}
}
return activeImgs;
}
//==== internal function for variable & lens management
// update the variables of a Lens, when it has been changed.
void Panorama::copyLensVariablesToImage(unsigned int imgNr)
{
unsigned int nImages = state.images.size();
unsigned int nLenses = state.lenses.size();
const PanoImage &img = state.images[imgNr];
unsigned int lensNr = img.getLensNr();
DEBUG_DEBUG("imgNr: " << imgNr << " of " << nImages);
DEBUG_DEBUG("img lens nr: " << img.getLensNr() << " nr of lenses: " << state.lenses.size());
DEBUG_ASSERT(imgNr < state.images.size());
DEBUG_ASSERT(lensNr < nLenses);
const Lens & lens = state.lenses[lensNr];
for (LensVarMap::const_iterator it = lens.variables.begin();
it != lens.variables.end();++it)
{
map_get(state.variables[imgNr], it->first).setValue(it->second.getValue());
}
}
SrcPanoImage Panorama::getSrcImage(unsigned imgNr) const
{
DEBUG_ASSERT(imgNr < state.images.size());
const PanoImage & img = state.images[imgNr];
const ImageOptions & opts = img.getOptions();
const Lens & lens = state.lenses[img.getLensNr()];
const VariableMap & vars = getImageVariables(imgNr);
SrcPanoImage ret(img.getFilename(), Size2D(img.getWidth(), img.getHeight()));
ret.setProjection((SrcPanoImage::Projection) lens.getProjection());
ret.setHFOV(const_map_get(vars,"v").getValue());
ret.setRoll(const_map_get(vars,"r").getValue());
ret.setPitch(const_map_get(vars,"p").getValue());
ret.setYaw(const_map_get(vars,"y").getValue());
// geometrical distortion correction
std::vector<double> radialDist(4);
radialDist[0] = const_map_get(vars,"a").getValue();
radialDist[1] = const_map_get(vars,"b").getValue();
radialDist[2] = const_map_get(vars,"c").getValue();
radialDist[3] = 1 - radialDist[0] - radialDist[1] - radialDist[2];
ret.setRadialDistortion(radialDist);
FDiff2D t;
t.x = const_map_get(vars,"d").getValue();
t.y = const_map_get(vars,"e").getValue();
ret.setRadialDistortionCenterShift(t);
t.x = const_map_get(vars,"g").getValue();
t.y = const_map_get(vars,"t").getValue();
ret.setShear(t);
// vignetting
ret.setVigCorrMode(opts.m_vigCorrMode);
ret.setFlatfieldFilename(opts.m_flatfield);
std::vector<double> vigCorrCoeff(4);
vigCorrCoeff[0] = const_map_get(vars,"Va").getValue();
vigCorrCoeff[1] = const_map_get(vars,"Vb").getValue();
vigCorrCoeff[2] = const_map_get(vars,"Vc").getValue();
vigCorrCoeff[3] = const_map_get(vars,"Vd").getValue();
ret.setRadialVigCorrCoeff(vigCorrCoeff);
t.x = const_map_get(vars,"Vx").getValue();
t.y = const_map_get(vars,"Vy").getValue();
ret.setRadialVigCorrCenterShift(t);
std::vector<double> k(3);
k[0] = const_map_get(vars,"K0a").getValue();
k[1] = const_map_get(vars,"K1a").getValue();
k[2] = const_map_get(vars,"K2a").getValue();
ret.setBrightnessFactor(k);
k[0] = const_map_get(vars,"K0b").getValue();
k[1] = const_map_get(vars,"K1b").getValue();
k[2] = const_map_get(vars,"K2b").getValue();
ret.setBrightnessOffset(k);
// crop
if (!opts.docrop) {
ret.setCropMode(SrcPanoImage::NO_CROP);
} else if (ret.getProjection() == SrcPanoImage::CIRCULAR_FISHEYE) {
ret.setCropMode(SrcPanoImage::CROP_CIRCLE);
ret.setCropRect(opts.cropRect);
} else {
ret.setCropMode(SrcPanoImage::CROP_RECTANGLE);
ret.setCropRect(opts.cropRect);
}
ret.setGamma(state.options.gamma);
return ret;
}
void Panorama::setSrcImg(unsigned int imgNr, const SrcPanoImage & img)
{
// get variable map vector
VariableMap vars;
DEBUG_ASSERT(imgNr < state.images.size());
PanoImage & pimg = state.images[imgNr];
ImageOptions opts = pimg.getOptions();
Lens & lens = state.lenses[pimg.getLensNr()];
// fill variable map
// position
vars.insert(make_pair("v", Variable("v", img.getHFOV())));
vars.insert(make_pair("r", Variable("r", img.getRoll())));
vars.insert(make_pair("p", Variable("p", img.getPitch())));
vars.insert(make_pair("y", Variable("y", img.getYaw())));
// distortion
vars.insert(make_pair("a", Variable("a", img.getRadialDistortion()[0])));
vars.insert(make_pair("b", Variable("b", img.getRadialDistortion()[1])));
vars.insert(make_pair("c", Variable("c", img.getRadialDistortion()[2])));
vars.insert(make_pair("d", Variable("d", img.getRadialDistortionCenterShift().x)));
vars.insert(make_pair("e", Variable("e", img.getRadialDistortionCenterShift().y)));
vars.insert(make_pair("g", Variable("g", img.getShear().x)));
vars.insert(make_pair("t", Variable("t", img.getShear().x)));
// vignetting correction
vars.insert(make_pair("Va", Variable("Va", img.getRadialVigCorrCoeff()[0])));
vars.insert(make_pair("Vb", Variable("Vb", img.getRadialVigCorrCoeff()[1])));
vars.insert(make_pair("Vc", Variable("Vc", img.getRadialVigCorrCoeff()[2])));
vars.insert(make_pair("Vd", Variable("Vd", img.getRadialVigCorrCoeff()[3])));
vars.insert(make_pair("Vx", Variable("Vx", img.getRadialVigCorrCenterShift().x)));
vars.insert(make_pair("Vy", Variable("Vy", img.getRadialVigCorrCenterShift().y)));
// brightness correction
vars.insert(make_pair("K0a", Variable("K0a", img.getBrightnessFactor()[0])));
vars.insert(make_pair("K1a", Variable("K1a", img.getBrightnessFactor()[1])));
vars.insert(make_pair("K2a", Variable("K2a", img.getBrightnessFactor()[2])));
vars.insert(make_pair("K0b", Variable("K0b", img.getBrightnessOffset()[0])));
vars.insert(make_pair("K1b", Variable("K1b", img.getBrightnessOffset()[1])));
vars.insert(make_pair("K2b", Variable("K2b", img.getBrightnessOffset()[2])));
// set variables
updateVariables(imgNr, vars);
// update lens
lens.setProjection((Lens::LensProjectionFormat)img.getProjection());
lens.setImageSize(img.getSize());
// update image
pimg.setFilename(img.getFilename());
pimg.setSize(img.getSize());
// update image options
if (img.getCropMode() == SrcPanoImage::NO_CROP) {
opts.docrop = false;
} else {
opts.docrop = true;
}
opts.cropRect = img.getCropRect();
opts.m_vigCorrMode = img.getVigCorrMode();
opts.m_flatfield = img.getFlatfieldFilename();
setImageOptions(imgNr, opts);
}
unsigned int PT::calcOptimalPanoWidth(const PanoramaOptions & opt,
const PanoImage & img,
double v,
Lens::LensProjectionFormat imgProj,
vigra::Size2D imgSize)
{
// calculate average pixel density of each image
// and use the highest one to calculate the width
double density=0;
double w = imgSize.x;
switch (imgProj) {
case Lens::RECTILINEAR:
density = 1/RAD_TO_DEG(atan(2*tan(DEG_TO_RAD(v)/2)/w));
break;
case Lens::CIRCULAR_FISHEYE:
case Lens::FULL_FRAME_FISHEYE:
// if we assume the linear fisheye model: r = f * theta
// then we get the same pixel density as for cylindrical and equirect
case Lens::EQUIRECTANGULAR:
case Lens::PANORAMIC:
density = w / v;
break;
}
// TODO: use density properly based on the output projection.
return roundi(density * opt.getHFOV());
}