Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[1fab1b]: src / hugin_base / panodata / PanoramaOptions.cpp Maximize Restore History

Download this file

PanoramaOptions.cpp    532 lines (460 with data), 15.3 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
// -*- c-basic-offset: 4 -*-
/** @file PanoramaOptions.cpp
*
* @brief implementation of PanoramaMemento 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
*
Revised 16JAN2010 by TKSharpless
to support default projection parameters and dynamic FOV limits.
libpano APIs
panoProjectionFeaturesQuery( proj, &features ) returns constant
values, including min, max, and default parameter values
and absolute max FOV limits.
queryFOVLimits(proj, &parms, &fovs ) returns dynamic hFOV and vFOV
limits calculated for a given set of projection parameters.
A PanoramaOptions holds a copy of the pano_projection_features from
panoProjectionFeaturesQuery(). Original implementation updated
that oftener than necessary (it can change only when the projection
does) possibly with the hope of tracking dynamic changes.
Revised implementation loads the full pano_projection_features only
when projection changes, and updates its max FOV values only when
parameter values change. Member fns getMaxHFOV() and getMaxVFOV()
now return those local values.
setProjection() now posts default projection parameters that come
from libpano in the pano_projection_features block. The default
projection parameter values formerly set here are now in libpano
(queryfeature.c)
*/
#include <hugin_config.h>
#include "PanoramaOptions.h"
#include <hugin_utils/utils.h>
#include <hugin_math/hugin_math.h>
#include <panotools/PanoToolsInterface.h>
namespace HuginBase {
using namespace hugin_utils;
using namespace vigra;
const std::string & PanoramaOptions::getFormatName(FileFormat f)
{
assert((int)f <= (int)FILEFORMAT_NULL);
return fileformatNames[(int) f];
}
const std::string & PanoramaOptions::getOutputExtension() const
{
assert((int)outputFormat < (int)FILEFORMAT_NULL);
return fileformatExt[(int) outputFormat];
}
PanoramaOptions::FileFormat PanoramaOptions::getFormatFromName(const std::string & name)
{
int max = (int) FILEFORMAT_NULL;
int i;
for (i=0; i<max; i++) {
if (name == fileformatNames[i]) {
break;
}
}
if (i+1 == max) {
DEBUG_ERROR("could not parse format " << name );
return TIFF_m;
}
return (FileFormat) i;
}
void PanoramaOptions::printScriptLine(std::ostream & o, bool forPTOptimizer) const
{
o << "p f" << m_projectionFormat << " w" << getWidth()<< " h" << getHeight()
<< " v" << getHFOV() << " ";
if (! forPTOptimizer) {
switch (colorCorrection) {
case NONE:
break;
case BRIGHTNESS_COLOR:
o << " k" << colorReferenceImage;
break;
case BRIGHTNESS:
o << " b" << colorReferenceImage;
break;
case COLOR:
o << " d" << colorReferenceImage;
break;
}
// the new exposure options
o << " E" << outputExposureValue;
o << " R" << outputMode;
if (outputPixelType.size() > 0) {
o << " T" << outputPixelType;
}
if (m_roi != vigra::Rect2D(m_size)) {
o << " S" << m_roi.left() << "," << m_roi.right() << "," << m_roi.top() << "," << m_roi.bottom();
}
}
if (m_projectionParams.size() > 0) {
o << " P\"";
for (int i=0; i < (int) m_projectionParams.size(); i++) {
o << m_projectionParams[i];
if (i+1 < (int)m_projectionParams.size())
o << " ";
}
o << "\"";
}
o << " n\"" << getFormatName(outputFormat);
if ( outputFormat == JPEG ) {
o << " q" << quality;
} else if ( outputFormat == TIFF ||
outputFormat == TIFF_m ||
outputFormat == TIFF_mask ||
outputFormat == TIFF_multilayer ||
outputFormat == TIFF_multilayer_mask)
{
o << " c:" << tiffCompression;
if (tiff_saveROI) {
o << " r:CROP";
}
}
o << "\"";
o << std::endl;
// misc options
o << "m g" << gamma << " i" << interpolator;
switch (remapAcceleration) {
case NO_SPEEDUP:
break;
case MAX_SPEEDUP:
o << " f0";
break;
case MEDIUM_SPEEDUP:
o << " f1";
}
o << " m" << huberSigma;
// options for photometric estimation.
o << " p" << photometricHuberSigma;
if (photometricSymmetricError)
o << " s1";
o << std::endl;
}
void PanoramaOptions::setProjection(ProjectionFormat f)
{
// post new projection type
if ((int) f >= panoProjectionFormatCount()) {
// reset to equirect if this projection is not known
f = EQUIRECTANGULAR;
}
m_projectionFormat = f;
/* Load constant features of the new projection */
panoProjectionFeaturesQuery(f, &m_projFeatures);
/* post default projection parameters and corresponding FOV limits */
m_projectionParams.resize(m_projFeatures.numberOfParameters);
// reset projection parameters to default, determine also new fov limits, clip current fovs...
resetProjectionParameters();
}
void PanoramaOptions::setProjectionParameters(const std::vector<double> & params)
{
assert(m_projFeatures.numberOfParameters == (int) params.size());
// check if the parameters are good.
if (m_projFeatures.numberOfParameters == (int) params.size()) {
m_projectionParams = params;
// enforce limits.
for (size_t i=0; i < params.size(); i++) {
if (m_projectionParams[i] > m_projFeatures.parm[i].maxValue) {
m_projectionParams[i] = m_projFeatures.parm[i].maxValue;
}
if (m_projectionParams[i] < m_projFeatures.parm[i].minValue) {
m_projectionParams[i] = m_projFeatures.parm[i].minValue;
}
}
}
/* get dynamic FOV limits corresponding to the new parameters,
clip current fovs to those limits, and post the results
*/
double parms[PANO_PROJECTION_MAX_PARMS];
double fovs[2];
int i;
for( i = 0; i < m_projFeatures.numberOfParameters; i++){
parms[i] = m_projectionParams[i];
}
if( queryFOVLimits((int)m_projectionFormat, parms, fovs )){
m_projFeatures.maxHFOV = fovs[0];
m_projFeatures.maxVFOV = fovs[1];
}
setHFOV( m_hfov, false );
setVFOV( getVFOV() );
}
void PanoramaOptions::resetProjectionParameters()
{
std::vector<double> defParam(m_projFeatures.numberOfParameters);
for(int i = 0; i < m_projFeatures.numberOfParameters; i++)
{
defParam[i] = m_projFeatures.parm[i].defValue;
};
setProjectionParameters(defParam);
};
bool PanoramaOptions::fovCalcSupported(ProjectionFormat f) const
{
/* Ideally this attribute should come from libpano */
return ( f == RECTILINEAR
|| f == CYLINDRICAL
|| f == EQUIRECTANGULAR
|| f == MERCATOR
|| f == SINUSOIDAL
|| f == MILLER_CYLINDRICAL
|| f == PANINI
|| f == ARCHITECTURAL
|| f == EQUI_PANINI
|| f == BIPLANE
|| f == TRIPLANE
|| f == GENERAL_PANINI
);
}
void PanoramaOptions::setWidth(unsigned int w, bool keepView)
{
if (m_projectionFormat == EQUIRECTANGULAR || m_projectionFormat == SINUSOIDAL) {
if (w%2 == 1) {
w = w+1;
}
}
bool nocrop = (m_roi == vigra::Rect2D(m_size));
double scale = w / (double) m_size.x;
m_size.x = w;
if (nocrop) {
m_roi = vigra::Rect2D(m_size);
} else {
// for now, do a simple proportional scaling
m_roi.setUpperLeft(vigra::Point2D(roundi(scale*m_roi.left()), m_roi.top()));
m_roi.setLowerRight(vigra::Point2D(roundi(scale*m_roi.right()), m_roi.bottom()));
// ensure ROI is inside the panorama
m_roi &= vigra::Rect2D(m_size);
}
if (keepView) {
m_size.y = hugin_utils::roundi(m_size.y*scale);
if (nocrop) {
m_roi = vigra::Rect2D(m_size);
} else {
m_roi.setUpperLeft(vigra::Point2D(m_roi.left(), roundi(scale*m_roi.top())));
m_roi.setLowerRight(vigra::Point2D(m_roi.right(), roundi(scale*m_roi.bottom())));
// ensure ROI is inside the panorama
m_roi &= Rect2D(m_size);
}
if (fovCalcSupported(m_projectionFormat)) {
if (getVFOV() > getMaxVFOV()) {
setVFOV(getMaxVFOV());
}
}
}
DEBUG_DEBUG(" HFOV: " << m_hfov << " size: " << m_size << " roi: " << m_roi << " => vfov: " << getVFOV());
}
void PanoramaOptions::setHeight(unsigned int h)
{
bool nocrop = (m_roi == vigra::Rect2D(m_size));
if (h == 0) {
h = 1;
}
int dh = h - m_size.y;
m_size.y = h;
if (nocrop) {
m_roi = vigra::Rect2D(m_size);
} else {
// move ROI
m_roi.moveBy(0,dh/2);
m_roi &= vigra::Rect2D(m_size);
}
DEBUG_DEBUG(" HFOV: " << m_hfov << " size: " << m_size << " roi:" << m_roi << " => vfov: " << getVFOV() );
}
void PanoramaOptions::setHFOV(double h, bool keepView)
{
if (keepView && !fovCalcSupported(m_projectionFormat)) {
DEBUG_NOTICE("Ignoring keepView");
keepView = false;
}
if (h <= 0) {
h = 1;
}
double vfov;
if (keepView) {
vfov = getVFOV();
}
m_hfov = std::min(h, getMaxHFOV());
if (keepView) {
setVFOV(std::min(vfov, getMaxVFOV()));
}
}
void PanoramaOptions::setVFOV(double VFOV)
{
VFOV = std::min(VFOV, getMaxVFOV());
if (! fovCalcSupported(m_projectionFormat)) {
return;
}
bool nocrop = (m_roi == vigra::Rect2D(m_size));
if (VFOV <= 0) {
VFOV = 1;
}
// TODO: create transform from equirect to target projection and
// set additional
PTools::Transform transf;
SrcPanoImage src;
src.setProjection(SrcPanoImage::EQUIRECTANGULAR);
src.setHFOV(360);
src.setSize(vigra::Size2D(360,180));
// set m_mosaicPanoFlag member of transf:
// are we in pano or mosaic mode?
// are describing a forward or inverse transform inside transf
// transf.m_mosaicPanoFlag =
if (PO_isMosaicNotPano()) {
transf.setTransformFlag(7); // mosaic/inverse transform
}
else {
transf.setTransformFlag(3); // pano/inverse transform
}
transf.createInvTransform(src, *this);
FDiff2D pmiddle;
if (VFOV>180 && getMaxVFOV() > 180) {
// we have crossed the pole
transf.transform(pmiddle, FDiff2D(180, 180-VFOV/2 - 0.01));
} else {
transf.transform(pmiddle, FDiff2D(0, VFOV/2));
}
// Dev: print pmiddle (before and after transf)
// check (0,400) on rectilinear maps to (0,90) on equirec
FDiff2D pmiddleCurrent = FDiff2D(0, m_size.y/2.0);
//std::cout << "Dev: in PanoramaOptions::setVFOV(), top middle coord of current pano = " << pmiddleCurrent << endl;
//std::cout << "Dev: in PanoramaOptions::setVFOV(), pmiddle = " << pmiddle << endl;
// try to keep the same ROI
vigra::Size2D oldSize = m_size;
m_size.y = abs(hugin_utils::roundi(2*pmiddle.y));
if (nocrop) {
m_roi = vigra::Rect2D(m_size);
} else {
// adjust ROI to stay in previous position
int dh = m_size.y - oldSize.y;
m_roi.moveBy(0, dh/2);
// ensure ROI is visible
m_roi &= vigra::Rect2D(m_size);
}
DEBUG_DEBUG(" HFOV: " << m_hfov << " size: " << m_size << " roi: " << m_roi << " => vfov: " << VFOV);
}
double PanoramaOptions::getVFOV() const
{
// calcuale VFOV based on current panorama
PTools::Transform transf;
SrcPanoImage src;
src.setProjection(SrcPanoImage::EQUIRECTANGULAR);
src.setHFOV(360);
src.setSize(vigra::Size2D(360,180));
// set m_mosaicPanoFlag member of transf:
// are we in pano or mosaic mode?
// are describing a forward or inverse transform inside transf
//transf.m_mosaicPanoFlag =
if (PO_isMosaicNotPano()) {
transf.setTransformFlag(5); // mosaic/forward transform
}
else {
transf.setTransformFlag(1); // pano/forward transform
}
transf.createTransform(src, *this);
FDiff2D pmiddle;
FDiff2D pcorner;
transf.transform(pmiddle, FDiff2D(0, m_size.y/2.0));
// Dev:
// Dev: print pmiddle (before and after transf)
// check (0,90) on equirec maps to (0,400) on rectilinear
FDiff2D pmiddleCurrent = FDiff2D(0, m_size.y/2.0);
//std::cout << "Dev: in PanoramaOptions::getVFOV(), top middle coord of current pano = " << pmiddleCurrent << endl;
//std::cout << "Dev: in PanoramaOptions::getVFOV(), pmiddle = " << pmiddle << endl;
// transf.transform(pcorner, FDiff2D(m_size.x/2.0, m_size.y/2.0));
double VFOV;
if (pmiddle.x > 90 ||pmiddle.y < -90) {
// the pole has been crossed
VFOV = 2*(180-pmiddle.y);
} else {
VFOV = 2*pmiddle.y;
}
//double VFOV = 2.0*std::max(pcorner.y, pmiddle.y);
/*
double VFOV;
switch (m_projectionFormat) {
case PanoramaOptions::RECTILINEAR:
VFOV = 2.0 * atan( (double)m_height * tan(DEG_TO_RAD(m_hfov)/2.0) / m_width);
VFOV = RAD_TO_DEG(VFOV);
break;
case PanoramaOptions::CYLINDRICAL:
{
// equations: w = f * v (f: focal length, in pixel)
double f = m_width / DEG_TO_RAD(m_hfov);
VFOV = 2*atan(m_height/(2.0*f));
VFOV = RAD_TO_DEG(VFOV);
break;
}
case PanoramaOptions::EQUIRECTANGULAR:
// FIXME: This is wrong!
case TRANSVERSE_MERCATOR:
case MERCATOR:
VFOV = m_hfov * m_height / m_width;
break;
case PanoramaOptions::FULL_FRAME_FISHEYE:
VFOV = m_hfov * m_height / m_width;
break;
}
*/
DEBUG_DEBUG(" HFOV: " << m_hfov << " size: " << m_size << " roi: " << m_roi << " => vfov: " << VFOV);
return VFOV;
}
const std::string PanoramaOptions::fileformatNames[] =
{
"JPEG",
"PNG",
"TIFF",
"TIFF_m",
"TIFF_mask",
"TIFF_multilayer",
"TIFF_multilayer_mask",
"PICT",
"PSD",
"PSD_m",
"PSD_mask",
"PAN",
"IVR",
"IVR_java",
"VRML",
"QTVR",
"HDR",
"HDR_m",
"EXR",
"EXR_m"
};
const std::string PanoramaOptions::fileformatExt[] =
{
"jpg",
"png",
"tif",
"tif",
"tif",
"tif",
"tif",
"pict",
"psd",
"psd",
"psd",
"pan",
"ivr",
"IVR_java",
"wrl",
"mov",
"hdr",
"hdr",
"exr",
"exr"
};
} //namespace