Hugin trunk 0.1
Loading...
Searching...
No Matches
PanoCommand.cpp
Go to the documentation of this file.
1// -*- c-basic-offset: 4 -*-
6/*
7* This is free software; you can redistribute it and/or
8* modify it under the terms of the GNU General Public
9* License as published by the Free Software Foundation; either
10* version 2 of the License, or (at your option) any later version.
11*
12* This software is distributed in the hope that it will be useful,
13* but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* Lesser General Public License for more details.
16*
17* You should have received a copy of the GNU General Public
18* License along with this software. If not, see
19* <http://www.gnu.org/licenses/>.
20*
21*/
22
23#include "PanoCommand.h"
24#include <fstream>
26
33#include <panodata/ParseExp.h>
34
35namespace PanoCommand
36{
38 {
39 for (std::vector<PanoCommand*>::iterator it = commands.begin(); it != commands.end(); ++it)
40 {
41 delete *it;
42 }
43 }
44
46 {
47 bool result = true;
48 for (std::vector<PanoCommand*>::iterator it = commands.begin(); it != commands.end(); ++it)
49 {
50 result &= (**it).processPanorama(pano);
51 }
53 return result;
54 };
55
57 {
58 pano.reset();
59 return true;
60 }
61
63 {
64 std::vector<HuginBase::SrcPanoImage>::const_iterator it;
65 for (it = imgs.begin(); it != imgs.end(); ++it)
66 {
67 pano.addImage(*it);
68 }
69 return true;
70 }
71
73 {
74 pano.removeImage(imgNr);
75 return true;
76 }
77
79 {
80 for (HuginBase::UIntSet::reverse_iterator it = imgNrs.rbegin();
81 it != imgNrs.rend(); ++it)
82 {
83 pano.removeImage(*it);
84 }
85 return true;
86 }
87
94
96 {
97 HuginBase::CPVector::const_iterator it;
98 unsigned int i = 0;
99 for (it = cps.begin(); it != cps.end(); ++it, i++)
100 {
101 pano.changeControlPoint(i, *it);
102 }
103 if (updateCPError)
104 {
106 };
107
108 return true;
109 }
110
112 {
113 pano.updateVariables(vars);
115 return true;
116 }
117
125
132
134 {
135 HuginBase::UIntSet::iterator it;
136 HuginBase::VariableMapVector::const_iterator v_it = vars.begin();
137 for (it = change.begin(); it != change.end(); ++it)
138 {
139 pano.updateVariables(*it, *v_it);
140 ++v_it;
141 }
143 return true;
144 }
145
147 {
148 if (!m_expression.empty())
149 {
152 return true;
153 }
154 else
155 {
156 return false;
157 };
158 }
159
165
166
172
178
180 {
181 HuginBase::UIntSet::iterator it;
182 for (it = images.begin(); it != images.end(); ++it)
183 {
184 pano.updateVariable(*it, var);
185 }
187
188 return true;
189 }
190
192 {
194 // adjust canvas size
196 fitPano.run();
198 opts.setHFOV(fitPano.getResultHorizontalFOV());
200 pano.setOptions(opts);
202
203 return true;
204 }
205
207 {
209
211 if (opts.getHFOV()<360)
212 {
213 // center non 360 deg panos
215 };
216 // adjust canvas size
218 fitPano.run();
219 opts.setHFOV(fitPano.getResultHorizontalFOV());
221 pano.setOptions(opts);
223
224 return true;
225 }
226
228 {
230 imgs.insert(point.image1Nr);
231 imgs.insert(point.image2Nr);
234 // copy only the given cp to the duplicated pano
235 cpVector.push_back(point);
237 // create subpano with only images for the single cp
239 // finally calc cp error
241 point.error = subPano.getCtrlPoint(0).error;
242 }
243
245 {
247 {
248 // if the first image number is bigger than the second, mirror the point
249 point.mirror();
250 };
251 // calculate cp error only for newly added control point
253 pano.addCtrlPoint(point);
254 return true;
255 }
256
258 {
260 for (HuginBase::CPVector::iterator it = cps.begin(); it != cps.end(); ++it)
261 {
262 if ((*it).image1Nr > (*it).image2Nr)
263 {
264 (*it).mirror();
265 };
266 imgs.insert(it->image1Nr);
267 imgs.insert(it->image2Nr);
268 }
269 // calculate cp error only for newly added control point
270 // create subpano with only images for the new cp
274 // finally calc cp error
276 for (auto it = cps.begin(); it != cps.end(); ++it)
277 {
278 it->error = subPano.getCtrlPoint(it - cps.begin()).error;
279 pano.addCtrlPoint(*it);
280 };
281 return true;
282 }
283
285 {
287 return true;
288 }
289
291 {
292 for (HuginBase::UIntSet::reverse_iterator it = m_points.rbegin(); it != m_points.rend(); ++it)
293 {
294 pano.removeCtrlPoint(*it);
295 }
296 return true;
297 }
298
300 {
301 // calculate cp error only for the changed control point
303 // finally change the point
305 return true;
306 }
307
309 {
310 HuginBase::UIntSet::iterator it;
311 for (unsigned int i = 0; i < pano.getNrOfImages(); i++)
312 {
313 if (set_contains(m_active, i))
314 {
315 pano.activateImage(i, true);
316 }
317 else
318 {
319 pano.activateImage(i, false);
320 };
321 }
322 return true;
323 }
324
326 {
327 pano.swapImages(m_i1, m_i2);
328 return true;
329 }
330
332 {
333 pano.moveImage(m_i1, m_i2);
334 return true;
335 }
336
343
350
352 {
353 int i = 0;
354 for (HuginBase::UIntSet::iterator it = imgNrs.begin(); it != imgNrs.end(); ++it)
355 {
356 pano.setSrcImage(*it, imgs[i]);
357 i++;
358 }
360 return true;
361 }
362
369
370 LoadPTProjectCmd::LoadPTProjectCmd(HuginBase::Panorama & p, const std::string & filename, const std::string & prefix)
371 : PanoCommand(p), filename(filename), prefix(prefix)
372 {
373 m_clearDirty = true;
374 }
375
377 {
378 if (!pano.ReadPTOFile(filename))
379 {
380 return false;
381 }
382 return true;
383 }
384
391
398
405
407 {
408 //search all image with the same lens, otherwise the crop factor is updated via links,
409 //but not the hfov if the image is not the given HuginBase::UIntSet
414 for (HuginBase::UIntSet::const_iterator it = imgNrs.begin(); it != imgNrs.end(); ++it)
415 {
416 allImgWithSameLens.insert(*it);
417 unsigned int lensNr = lenses.getPartNumber(*it);
419 {
420 continue;
421 };
422 testedLens.insert(lensNr);
423 for (unsigned int i = 0; i<pano.getNrOfImages(); i++)
424 {
425 if (lenses.getPartNumber(i) == lensNr)
426 {
427 allImgWithSameLens.insert(i);
428 };
429 };
430 };
433 return true;
434 }
435
437 {
438 // it might change as we are setting them
441 for (HuginBase::UIntSet::iterator it = image_numbers.begin(); it != image_numbers.end(); ++it)
442 {
443 group.switchParts(*it, new_new_part_number);
444 // update the lens number if it changes.
445 new_new_part_number = group.getPartNumber(*it);
446 }
447 return true;
448 }
449
451 {
454 {
455 for (HuginBase::UIntSet::iterator imageIt = image_numbers.begin(); imageIt != image_numbers.end(); ++imageIt)
456 {
457 // link the variables
458 for (std::set<HuginBase::ImageVariableGroup::ImageVariableEnum>::iterator variableIt = changeVariables.begin();
460 {
461 group.linkVariableImage(*variableIt, *imageIt);
462 }
463 }
464 }
465 else
466 {
467 for (HuginBase::UIntSet::iterator imageIt = image_numbers.begin(); imageIt != image_numbers.end(); ++imageIt)
468 {
469 // unlink the variable
470 for (std::set<HuginBase::ImageVariableGroup::ImageVariableEnum>::iterator variableIt = changeVariables.begin();
472 {
473 group.unlinkVariableImage(*variableIt, *imageIt);
474 group.updatePartNumbers();
475 }
476 }
477 }
478 return true;
479 }
480
482 {
485 std::set<HuginBase::ImageVariableGroup::ImageVariableEnum>::iterator it;
486 for (it = variables.begin(); it != variables.end(); ++it)
487 {
489 }
490 return true;
491 }
492
494#define image_variable( name, type, default_value )\
495 bool ChangeImage##name##Cmd::processPanorama(HuginBase::Panorama& pano)\
496 {\
497 for (HuginBase::UIntSet::iterator it = image_numbers.begin(); it != image_numbers.end(); it++)\
498 {\
499 HuginBase::SrcPanoImage img = pano.getSrcImage(*it);\
500 img.set##name(value);\
501 pano.setSrcImage(*it, img);\
502 }\
503 return true;\
504 };
506#undef image_variable
507
509 {
510 // unlink all the variables in the first image.
511 DEBUG_ASSERT(!image_numbers.empty());
512 unsigned int image_index = *image_numbers.begin();
513 for (std::set<HuginBase::ImageVariableGroup::ImageVariableEnum>::iterator it = vars.begin(); it != vars.end(); ++it)
514 {
515 switch (*it)
516 {
517#define image_variable( name, type, default_value )\
518 case HuginBase::ImageVariableGroup::IVE_##name:\
519 pano.unlinkImageVariable##name(image_index);\
520 break;
522#undef image_variable
523 }
524 }
525 // now the first image should have a new part in the group.
526 // we want to switch the rest of the images to the new part.
528 for (HuginBase::UIntSet::iterator it = ++image_numbers.begin(); it != image_numbers.end(); ++it)
529 {
530 std::size_t part_number = group.getPartNumber(image_index);
531 group.switchParts(*it, part_number);
532 }
533 return true;
534 }
535
541
543 {
545 return true;
546 }
547
555
557 {
558 const size_t nrImages = pano.getNrOfImages();
559 if (nrImages>0)
560 {
561 const HuginBase::SrcPanoImage& img = pano.getImage(0);
562 const double hfov = img.getHFOV();
563 size_t imgsPerRow;
564 //distribute all images
565 //for rectilinear images calculate number of rows
566 if (img.getProjection() == HuginBase::SrcPanoImage::RECTILINEAR)
567 {
568 imgsPerRow = std::max(3, int(360 / (0.8*hfov)));
569 imgsPerRow = std::min(imgsPerRow, nrImages);
570 }
571 else
572 {
573 //all other images do in one row to prevent cluttered images with fisheye images and the like
574 imgsPerRow = nrImages;
575 };
576 double offset = 0.75*hfov;
577 if ((imgsPerRow - 1.0)*offset>360)
578 {
579 offset = 360 / (imgsPerRow - 1.0);
580 };
581 double yaw = -(imgsPerRow - 1.0) / 2.0*offset;
582 double pitch = 0;
583 if (imgsPerRow<nrImages)
584 {
585 pitch = (-(std::ceil(double(nrImages) / double(imgsPerRow)) - 1.0) / 2.0*offset);
586 };
588 size_t counter = 0;
589 for (size_t i = 0; i<nrImages; i++)
590 {
591 HuginBase::VariableMap::iterator it = varsVec[i].find("y");
592 if (it != varsVec[i].end())
593 {
594 it->second.setValue(yaw);
595 };
596 it = varsVec[i].find("p");
597 if (it != varsVec[i].end())
598 {
599 it->second.setValue(pitch);
600 };
601 yaw += offset;
602 counter++;
603 if (counter == imgsPerRow)
604 {
605 counter = 0;
606 pitch += offset;
607 yaw = -(imgsPerRow - 1.0) / 2.0*offset;
608 };
609 };
611 };
612 return true;
613 }
614
615} // namespace PanoCommand
Utility calls into PanoTools using CPP interface.
function to parse expressions from strings
virtual double getResultHorizontalFOV()
Definition FitPanorama.h:68
virtual double getResultHeight()
Definition FitPanorama.h:75
static double calcMeanExposure(const PanoramaData &pano)
unsigned int getPartNumber(unsigned int imageNr) const
Get a part number from an image number.
represents a control point
void mirror()
swap (image1Nr,x1,y1) with (image2Nr,x2,y2)
Same as above, but use a non const panorama.
void linkVariablePart(ImageVariableEnum variable, unsigned int partNr)
link one of the variables across a given part
virtual void run()
runs the algorithm.
Panorama image options.
void setHFOV(double h, bool keepView=true)
set the horizontal field of view.
void setHeight(unsigned int h)
set panorama height
Model for a panorama.
Definition Panorama.h:153
const SrcPanoImage & getImage(std::size_t nr) const
get a panorama image, counting starts with 0
Definition Panorama.h:211
const PanoramaOptions & getOptions() const
returns the options for this panorama
Definition Panorama.h:481
void updateMasksForImage(unsigned int imgNr, MaskPolygonVector newMasks)
set complete mask list for image with number
Definition Panorama.cpp:882
void setOptimizeVector(const OptimizeVector &optvec)
set optimize setting
Definition Panorama.cpp:297
virtual void UpdateCropFactor(UIntSet imgs, double newCropFactor)
updates the crop factor, try to keep focal length constant
Definition Panorama.cpp:254
virtual void updateWhiteBalance(double redFactor, double blueFactor)
update the global white balace of the panorama by multiplying the red and blue factor of each image w...
void removeImage(unsigned int nr)
creates an image, from filename, and a Lens, if needed
Definition Panorama.cpp:329
void setCtrlPoints(const CPVector &points)
set all control points (Ippei: Is this supposed to be 'add' method?)
Definition Panorama.cpp:449
bool ReadPTOFile(const std::string &filename, const std::string &prefix="")
read pto file from the given filename into Panorama object it does some checks on the file and issues...
void mergePanorama(const Panorama &newPano)
merges the panorama with the given pano
unsigned int addCtrlPoint(const ControlPoint &point)
add a new control point.
Definition Panorama.cpp:381
virtual void updateVariable(unsigned int imgNr, const Variable &var)
update a single variable
Definition Panorama.cpp:200
virtual void UpdateFocalLength(UIntSet imgs, double newFocalLength)
updates the focal length by changing hfov
Definition Panorama.cpp:227
unsigned int addImage(const SrcPanoImage &img)
the the number for a specific image
Definition Panorama.cpp:319
void swapImages(unsigned int img1, unsigned int img2)
swap images.
void setOptions(const PanoramaOptions &opt)
set new output settings This is not used directly for optimizing/stiching, but it can be feed into ru...
void markAsOptimized(bool optimized=true)
Definition Panorama.h:618
void setSrcImage(unsigned int nr, const SrcPanoImage &img)
set input image parameters
void activateImage(unsigned int imgNr, bool active=true)
mark an image as active or inactive.
void moveImage(size_t img1, size_t img2)
moves images.
VariableMapVector getVariables() const
get variables of this panorama
Definition Panorama.cpp:118
virtual void updateVariables(const VariableMapVector &vars)
Set the variables.
Definition Panorama.cpp:171
Panorama getSubset(const UIntSet &imgs) const
get a subset of the panorama
void changeControlPoint(unsigned int pNr, const ControlPoint &point)
change a control Point.
Definition Panorama.cpp:434
void updateCtrlPointErrors(const CPVector &controlPoints)
update control points distances.
Definition Panorama.cpp:160
Panorama duplicate() const
duplicate the panorama
void setPhotometricOptimizerSwitch(const int newSwitch)
sets the photometric optimizer master switch
Definition Panorama.cpp:311
void reset()
clear the internal state.
Definition Panorama.cpp:69
void removeCtrlPoint(unsigned int pNr)
remove a control point.
Definition Panorama.cpp:391
void setOptimizerSwitch(const int newSwitch)
set optimizer master switch
Definition Panorama.cpp:303
std::size_t getNrOfImages() const
number of images.
Definition Panorama.h:205
All variables of a source image.
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::ControlPoint point
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::CPVector cps
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
std::vector< HuginBase::SrcPanoImage > imgs
Definition PanoCommand.h:72
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::ControlPoint point
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
std::set< HuginBase::ImageVariableGroup::ImageVariableEnum > groupVariables
std::set< HuginBase::ImageVariableGroup::ImageVariableEnum > changeVariables
std::set< HuginBase::ImageVariableGroup::ImageVariableEnum > variables
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::UIntSet image_numbers
std::vector< PanoCommand * > commands
Definition PanoCommand.h:51
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
std::set< HuginBase::ImageVariableGroup::ImageVariableEnum > variables
LoadPTProjectCmd(HuginBase::Panorama &p, const std::string &filename, const std::string &prefix="")
const std::string & filename
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::Panorama newPano
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
std::set< HuginBase::ImageVariableGroup::ImageVariableEnum > vars
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::UIntSet image_numbers
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::UIntSet imgNrs
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::PanoramaOptions options
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::Variable var
HuginBase::UIntSet images
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::CPVector cps
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::VariableMapVector vars
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::MaskPolygonVector m_mask
HuginBase::OptimizeVector m_optvec
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::SrcPanoImage img
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
std::vector< HuginBase::SrcPanoImage > imgs
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::VariableMapVector vars
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::VariableMapVector vars
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
HuginBase::VariableMapVector vars
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
virtual bool processPanorama(HuginBase::Panorama &pano)
Called by execute().
#define DEBUG_ASSERT(cond)
Definition utils.h:80
This file specifies what image variables SrcPanoImg should have.
void calcCtrlPointErrors(PanoramaData &pano)
Update the Ctrl Point errors without optimizing.
std::vector< VariableMap > VariableMapVector
std::vector< ControlPoint > CPVector
std::set< unsigned int > UIntSet
void UpdateSingleCtrlPointError(const HuginBase::Panorama &pano, HuginBase::ControlPoint &point)
void PanoParseExpression(HuginBase::Panorama &pano, const std::string &expression, std::ostream &statusStream, std::ostream &errorStream)
parses the given expression and apply the changes to the Panorama
Definition ParseExp.cpp:768
int roundi(T x)
Definition hugin_math.h:73
bool set_contains(const _Container &c, const typename _Container::key_type &key)
Definition stl_utils.h:74
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41