projectdefinition.hpp 15.1 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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#pragma once

#include "mutableprojectdefinition.hpp"
#include "libvideostitch/inputDef.hpp"
#include "libvideostitch-gui/utils/inputlensenum.hpp"
#include "libvideostitch-gui/utils/inputformat.hpp"
#include "libvideostitch-gui/widgets/crop/cropshapeeditor.hpp"
#include "libvideostitch-base/lockingproxy.hpp"

#include <QObject>
#include <QString>
#include <sstream>
#include <QMutex>

class ProjectDefinition;

typedef VideoStitch::Helper::LockingProxy<VideoStitch::Core::PanoDefinition, const ProjectDefinition>
    PanoDefinitionLocked;
typedef VideoStitch::Helper::LockingProxy<VideoStitch::Core::AudioPipeDefinition, const ProjectDefinition>
    AudioPipeDefinitionLocked;
typedef VideoStitch::Helper::LockingProxy<VideoStitch::Core::ImageMergerFactory, const ProjectDefinition>
    ImageMergerFactoryLocked;
typedef VideoStitch::Helper::LockingProxy<VideoStitch::Core::ImageFlowFactory, const ProjectDefinition>
    ImageFlowFactoryLocked;
typedef VideoStitch::Helper::LockingProxy<VideoStitch::Core::ImageWarperFactory, const ProjectDefinition>
    ImageWarperFactoryLocked;
typedef VideoStitch::Helper::LockingProxy<VideoStitch::Core::StereoRigDefinition, const ProjectDefinition>
    StereoRigDefinitionLocked;

namespace VideoStitch {
namespace Core {
class ImageMergerFactory;
class ImageFlowFactory;
class ImageWarperFactory;
}  // namespace Core
}  // namespace VideoStitch

class SignalCompressionCaps;

/**
 * A class describing the project. Thread-safe.
 * Also hold a few runtime-parameters for the project,
 * so not stateless, compared to the underlying MutableProjectDefinition.
 */
class VS_GUI_EXPORT ProjectDefinition : public QObject {
  Q_OBJECT

 public:
  ProjectDefinition();
  virtual ~ProjectDefinition();

  /**
   * @brief Change the state of the project to @modified and notify using the hasBeenModified(bool) signal.
   */
  void setModified(const bool modified = true);

  /**
   * @brief Validates the panorama of the project.
   * @param errsink Stream which contains the error.
   * @returns True = the panorama is valid / false the panorama is invalid.
   */
  bool validatePanorama(std::stringstream &errsink);

  /**
   * @brief Adds a project from a Ptv::Value. If there is an existing project, it will be deleted.
   * @param value Input value.
   * @returns Returns true on success. false on error, back in a clean state.
   */
  bool load(const VideoStitch::Ptv::Value &value);

  /**
   * @brief Close the current project.
   */
  void close();

  /**
   * @brief isInit indicates if a project is successfully loaded.
   * @returns True if is initialized.
   */
  bool isInit() const;

  /**
   * @brief Serialize a GUI project definition.
   * @returns The serialized PTV.
   */
  VideoStitch::Ptv::Value *serialize() const;

  /**
   * Returns the pano definition for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  PanoDefinitionLocked getPano() const;

  /**
   * @brief Returns a const pano definition pointer for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  const PanoDefinitionLocked getPanoConst() const;

  /**
   * @brief Returns a const audio pipe definition pointer for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  AudioPipeDefinitionLocked getAudioPipe() const;

  /**
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  const AudioPipeDefinitionLocked getAudioPipeConst() const;

  /**
   * @brief Returns the stereo rig definition for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  StereoRigDefinitionLocked getStereoRig() const;

  /**
   * @brief Returns a const stereo rig definition pointer for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  const StereoRigDefinitionLocked getStereoRigConst() const;

  /**
   * @brief Returns the merger factory for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  ImageMergerFactoryLocked getImageMergerFactory() const;

  /**
   * @brief Gets the image blender type.
   * @returns Blender type name, if any. Otherwise the default one.
   */
  QString getBlender() const;

  /**
   * @brief Gets the image flow type.
   * @returns Flow type name, if any. Otherwise the default one.
   */
  QString getFlow() const;

  /**
   * @brief Gets the image warper type.
   * @returns Warper type name, if any. Otherwise the default one.
   */
  QString getWarper() const;

  /**
   * @brief Gets the feather values
   * @returns Returns the feather values if any. Default value, otherwise.
   */
  int getFeather() const;

  /**
   * @brief Gets the sphere scale value
   * @returns Returns the sphere scale value if any. Default value, otherwise.
   */
  double getSphereScale() const;

  /**
   * Lower limit to sphereScale parameter
   */
  double computeMinimumSphereScale() const;

  /**
   * Returns the flow factory for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  ImageFlowFactoryLocked getImageFlowFactory() const;

  /**
   * Returns the flow factory for the project.
   * @returns Non-NULL when isInit() returns true, NULL when isInit() returns false.
   */
  ImageWarperFactoryLocked getImageWarperFactory() const;

  /**
   * @brief Get the number of inputs.
   * @returns The number of inputs.
   */
  readerid_t getNumInputs() const;

  /**
   * @brief Get the list of input names.
   * @note Audio-only input is NOT included.
   * @returns The list of input names.
   */
  QStringList getInputNames() const;

  /**
   * @brief Get input type for video (we ignore the audio only inputs)
   * @returns The video input type.
   */
  VideoStitch::InputFormat::InputFormatEnum getVideoInputType() const;

  /**
   * @brief Checks if the inputs provides audio only
   * @returns True if has audio input
   */
  bool hasAnInputWithAudioOnly() const;

  /**
   * @brief Checks if the projects contains audio
   * @returns True if there is one audio pipe configured
   */
  bool hasAudio() const;

  /**
   * @brief Checks that the inputs are images or procedurals only (and not videos)
   */
  bool hasImagesOrProceduralsOnly() const;

  /**
   * @brief Checks that the inputs are several videos (at least 2 videos, no images and no procedurals)
   */
  bool hasSeveralVideos() const;

  /**
   * @brief Checks that there is at least 2 visual inputs (videos, images or procedurals)
   */
  bool hasSeveralVisualInputs() const;

  /**
   * @brief Get the project lens type for the UI
   * @return The lens type (Rectilinear, FullFrameFisheye, CircularFisheye, Equirectangular)
   */
  InputLensClass::LensType getProjectLensType() const;

  /**
   * @brief Checks if at least one input has a valid crop area
   */
  bool hasCroppedArea() const;

  /**
   * @brief Get the current projection name.
   * @returns The projection name.
   */
  QString getProjection() const;

  /**
   * @brief Get the panorama (uncropped) dimensions.
   */
  void getImageSize(unsigned &width, unsigned &height) const;

  /**
   * @brief Get the panorama cropped dimensions.
   **/
  void getCroppedImageSize(unsigned &width, unsigned &height) const;

  /**
   * @brief Get the horizontal field of view in arc degrees.
   * @return The FOV value.
   */
  double getHFOV() const;

  /**
   * @brief The project is compatible with the lib version.
   * @return True is it's compatible.
   */
  bool hasFileFormatChanged() const;

  /**
   * @brief Updates the project according to the lib version.
   */
  void updateFileFormat();

  /**
   * @brief isDrawingInputNumbers
   * @return The input numbers are shown.
   */
  bool isDrawingInputNumbers() const;

  /**
   * @brief setAudioPipe writes the @audio pipe definition in the project. Thread-safe.
   */
  void setAudioPipe(VideoStitch::Core::AudioPipeDefinition *audioPipeDefinition);

  /**
   * @brief setPano writes the @pano definition in the project. Thread-safe.
   */
  void setPano(VideoStitch::Core::PanoDefinition *pano);

  /**
   * @brief setMergerFactory writes the @mergerFactory definition in the project. Thread-safe.
   */
  void setMergerFactory(VideoStitch::Core::ImageMergerFactory *mergerFactory);

  /**
   * @brief setFlowFactory writes the @flowFactory definition in the project. Thread-safe.
   */
  void setFlowFactory(VideoStitch::Core::ImageFlowFactory *flowFactory);

  /**
   * @brief setWarperFactory writes the @warperFactory definition in the project. Thread-safe.
   */
  void setWarperFactory(VideoStitch::Core::ImageWarperFactory *warperFactory);

  /**
   * @brief setStereoRigDefinition writes the @rigDefinition definition in the project. Thread-safe.
   */
  void setStereoRigDefinition(VideoStitch::Core::StereoRigDefinition *rigDefinition);

  /**
   * @brief Sets the panorama width and height. Updates project data if needed.
   * @param width New panorama width.
   * @param height New panorama height.
   */
  virtual void updateSize(int width, int height);

  /**
   * @brief Sets the Horizontal field of view.
   * @param fov New horizontal field of view.
   */
  void setHFov(double fov);

  /**
   * @brief Sets the sphere scale value.
   * @param sphereScale New sphere scale.
   */
  void setSphereScale(double sphereScale);

  /**
   * @brief Draws the input numbers on the input image
   * @param draw True
   */
  void setDrawInputNumbers(bool draw);

  /**
   * @brief Sets the nex projection of the panorama.
   * @param projection New projection.
   */
  void setProjection(QString projection);

  /**
   * @brief markAsSaved resets the isModified flag of the class to indicate there is no
   * discrepancy between the current project and what you have saved last.
   */
  void markAsSaved();

  /**
   * @brief hasLocalModifications indicates if the current project has modifications since the last
   * markAsSaved() call.
   */
  bool hasLocalModifications() const;

  /**
   * @brief The orientation grid is displayed.
   * @return True if the orientation grid must be displayed
   */
  bool getDisplayOrientationGrid() const;

  /**
   * @brief The project has a rig configuration (Stereo)
   * @return True if has a rig configuration
   */
  bool hasRigConfiguration() const;

  /**
   * @brief Gets the audio delay from the first active audio input
   * @return The delay value
   */
  int getAudioDelay() const;

  /**
   * @brief Sets the crop values for an input.
   * @param input The input number
   * @param crop Crop values
   * @param lensType Project lens type
   * @param applyToAll Apply the same crop to all inputs.
   */
  void setInputCrop(const unsigned int input, const Crop &crop, const InputLensClass::LensType lensType,
                    const bool applyToAll);

  /**
   * @brief Sets a rig configuration to the project.
   * @param orientation The orientation
   * @param geometry The rig geometry
   * @param diameter The rig diamater
   * @param ipd The rig IPD
   * @param leftInputs List of left inputs
   * @param rightInputs List of right inputs
   */
  void setRigConfiguration(const VideoStitch::Core::StereoRigDefinition::Orientation orientation,
                           const VideoStitch::Core::StereoRigDefinition::Geometry geometry, const double diameter,
                           const double ipd, const QVector<int> leftInputs, const QVector<int> rightInputs);

  /**
   * @brief Sets the image blender type.
   * @param merger Merger type
   * @param feather The feather value
   */
  void changeBlendingParameters(QString merger, int feather);

  /**
   * @brief Sets the advanced blender type.
   * @param flow Image flow type
   * @param warper Image warper type
   */
  void changeAdvancedBlendingParameters(const QString &flow, const QString &warper);

  /**
   * @brief Get a list of audio input names.
   * @return List of audio input names.
   */
  QStringList getAudioInputNames() const;

  /**
   * @brief Get a list of video input names.
   * @return List of video input names.
   */
  QStringList getVideoInputNames() const;

 public slots:
  void changeProjection(const QString &projName, const double HFOV);
  void updateMasks();
  void setDisplayOrientationGrid(bool display, bool reqRestitch = true);
  void toggleInputNumberDrawing();
  void setInterPupillaryDistance(double ipd);
  void setAudioDelay(int delay_ms);

 signals:
  void reqReset(SignalCompressionCaps *);
  void reqResetRig(SignalCompressionCaps *);
  void reqUpdateMask(int index, unsigned char *maskData, int width, int height);
  void reqUpdateMask(int index, QImage *mask);
  void maskUpdateDone();
  void reqDisplayWarning(QString);
  void hasBeenModified(bool modified);
  void imagesOrProceduralsOnlyHasChanged(bool hasImagesOrProceduralsOnly);
  void severalVideosOnlyHasChanged(bool hasSeveralVideos);
  void severalVisualInputsHasChanged(bool hasSeveralVisualInputs);
  void reqToggleInputNumbers(bool draw);
  void reqDisplayOrientationGrid(bool display);
  void reqToggleControlPoints(bool display);
  void reqSetAudioDelay(int value);

 protected:
  /**
   * @brief mutex A mutex to protect access to this object.
   * The mutex is recursive in case the user accesses different objects at the same time.
   */
  mutable QMutex mutex;
  SignalCompressionCaps *signalCompressor;

  friend class VideoStitch::Helper::LockingProxy<VideoStitch::Core::PanoDefinition,
                                                 const ProjectDefinition>;  // PanoDefinitionLocked;
  friend class VideoStitch::Helper::LockingProxy<VideoStitch::Core::AudioPipeDefinition,
                                                 const ProjectDefinition>;  // AudioDefinitionLocked;
  friend class VideoStitch::Helper::LockingProxy<VideoStitch::Core::StereoRigDefinition,
                                                 const ProjectDefinition>;  // StereoRigDefinitionLocked;
  friend class VideoStitch::Helper::LockingProxy<VideoStitch::Core::ImageMergerFactory,
                                                 const ProjectDefinition>;  // ImageMergerFactoryLocked;
  friend class VideoStitch::Helper::LockingProxy<VideoStitch::Core::ImageFlowFactory,
                                                 const ProjectDefinition>;  // ImageFlowFactoryLocked;
  friend class VideoStitch::Helper::LockingProxy<VideoStitch::Core::ImageWarperFactory,
                                                 const ProjectDefinition>;  // ImageWarperFactoryLocked;

  /**
   * @brief lock Grab the local instance mutex.
   */
  void lock() const;

  /**
   * @brief lock Release the local instance mutex.
   */
  void unlock() const;

  virtual MutableProjectDefinition *getDelegate() const = 0;

  virtual void createDelegate(const VideoStitch::Ptv::Value &value) = 0;

  virtual void destroyDelegate() = 0;

  void setDefaultPtvValues(const VideoStitch::Ptv::Value &value);

 private:
  QString getInputName(int index) const;
  void copyValuesIntoVector(std::vector<VideoStitch::Ptv::Value *> &vector, const QVector<int> inputList);

 private:
  /**
   * @brief Tells if the project definition has been modified since it was saved (VideoStitcher:::savePTV()).
   */
  bool isModified;
  bool drawInputNumbers;
  bool displayOrientationGrid;
};