diff options
author | Manuel Castilla <manzanillawork@gmail.com> | 2021-05-24 15:31:17 +0300 |
---|---|---|
committer | Manuel Castilla <manzanillawork@gmail.com> | 2021-05-24 15:31:55 +0300 |
commit | 1318cb26fe07e221c8bc539f21a626f74de354d1 (patch) | |
tree | be1f372e5c2c2377435d88eea08c1f32861c0153 /source/blender | |
parent | 6c2c16b5f87e30a338d3f2e7259887403d736f50 (diff) |
Cleanup: Refactor PlaneTrack and PlaneDistort operations
Deduplicates code by introducing a PlaneDirtortBaseOperation for common logic.
Reviewed By: #compositing, jbakker
Differential Revision: https://developer.blender.org/D11273
Diffstat (limited to 'source/blender')
4 files changed, 81 insertions, 103 deletions
diff --git a/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.cc b/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.cc index 46ae00dee34..4edcc206f5b 100644 --- a/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.cc +++ b/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.cc @@ -31,6 +31,31 @@ namespace blender::compositor { +PlaneDistortBaseOperation::PlaneDistortBaseOperation() + : m_motion_blur_samples(1), m_motion_blur_shutter(0.5f) +{ +} + +void PlaneDistortBaseOperation::calculateCorners(const float corners[4][2], + bool normalized, + int sample) +{ + BLI_assert(sample < this->m_motion_blur_samples); + MotionSample *sample_data = &this->m_samples[sample]; + if (normalized) { + for (int i = 0; i < 4; i++) { + sample_data->frameSpaceCorners[i][0] = corners[i][0] * this->getWidth(); + sample_data->frameSpaceCorners[i][1] = corners[i][1] * this->getHeight(); + } + } + else { + for (int i = 0; i < 4; i++) { + sample_data->frameSpaceCorners[i][0] = corners[i][0]; + sample_data->frameSpaceCorners[i][1] = corners[i][1]; + } + } +} + /* ******** PlaneDistort WarpImage ******** */ BLI_INLINE void warpCoord(float x, float y, float matrix[3][3], float uv[2], float deriv[2][2]) @@ -46,13 +71,11 @@ BLI_INLINE void warpCoord(float x, float y, float matrix[3][3], float uv[2], flo deriv[1][1] = (matrix[1][1] - matrix[1][2] * uv[1]) / vec[2]; } -PlaneDistortWarpImageOperation::PlaneDistortWarpImageOperation() +PlaneDistortWarpImageOperation::PlaneDistortWarpImageOperation() : PlaneDistortBaseOperation() { this->addInputSocket(DataType::Color, ResizeMode::None); this->addOutputSocket(DataType::Color); this->m_pixelReader = nullptr; - this->m_motion_blur_samples = 1; - this->m_motion_blur_shutter = 0.5f; this->flags.complex = true; } @@ -60,24 +83,13 @@ void PlaneDistortWarpImageOperation::calculateCorners(const float corners[4][2], bool normalized, int sample) { - BLI_assert(sample < this->m_motion_blur_samples); + PlaneDistortBaseOperation::calculateCorners(corners, normalized, sample); + const int width = this->m_pixelReader->getWidth(); const int height = this->m_pixelReader->getHeight(); float frame_corners[4][2] = { {0.0f, 0.0f}, {(float)width, 0.0f}, {(float)width, (float)height}, {0.0f, (float)height}}; MotionSample *sample_data = &this->m_samples[sample]; - if (normalized) { - for (int i = 0; i < 4; i++) { - sample_data->frameSpaceCorners[i][0] = corners[i][0] * this->getWidth(); - sample_data->frameSpaceCorners[i][1] = corners[i][1] * this->getHeight(); - } - } - else { - for (int i = 0; i < 4; i++) { - sample_data->frameSpaceCorners[i][0] = corners[i][0]; - sample_data->frameSpaceCorners[i][1] = corners[i][1]; - } - } BKE_tracking_homography_between_two_quads( sample_data->frameSpaceCorners, frame_corners, sample_data->perspectiveMatrix); } @@ -147,34 +159,12 @@ bool PlaneDistortWarpImageOperation::determineDependingAreaOfInterest( /* ******** PlaneDistort Mask ******** */ -PlaneDistortMaskOperation::PlaneDistortMaskOperation() +PlaneDistortMaskOperation::PlaneDistortMaskOperation() : PlaneDistortBaseOperation() { addOutputSocket(DataType::Value); /* Currently hardcoded to 8 samples. */ m_osa = 8; - this->m_motion_blur_samples = 1; - this->m_motion_blur_shutter = 0.5f; -} - -void PlaneDistortMaskOperation::calculateCorners(const float corners[4][2], - bool normalized, - int sample) -{ - BLI_assert(sample < this->m_motion_blur_samples); - MotionSample *sample_data = &this->m_samples[sample]; - if (normalized) { - for (int i = 0; i < 4; i++) { - sample_data->frameSpaceCorners[i][0] = corners[i][0] * this->getWidth(); - sample_data->frameSpaceCorners[i][1] = corners[i][1] * this->getHeight(); - } - } - else { - for (int i = 0; i < 4; i++) { - sample_data->frameSpaceCorners[i][0] = corners[i][0]; - sample_data->frameSpaceCorners[i][1] = corners[i][1]; - } - } } void PlaneDistortMaskOperation::initExecution() diff --git a/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.h b/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.h index 95e5c86bd4d..cc6e4d00d71 100644 --- a/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.h +++ b/source/blender/compositor/operations/COM_PlaneDistortCommonOperation.h @@ -32,21 +32,43 @@ namespace blender::compositor { #define PLANE_DISTORT_MAX_SAMPLES 64 -class PlaneDistortWarpImageOperation : public NodeOperation { +class PlaneDistortBaseOperation : public NodeOperation { protected: struct MotionSample { float frameSpaceCorners[4][2]; /* Corners coordinates in pixel space. */ float perspectiveMatrix[3][3]; }; - SocketReader *m_pixelReader; MotionSample m_samples[PLANE_DISTORT_MAX_SAMPLES]; int m_motion_blur_samples; float m_motion_blur_shutter; public: + PlaneDistortBaseOperation(); + + void setMotionBlurSamples(int samples) + { + BLI_assert(samples <= PLANE_DISTORT_MAX_SAMPLES); + this->m_motion_blur_samples = samples; + } + void setMotionBlurShutter(float shutter) + { + this->m_motion_blur_shutter = shutter; + } + + virtual void calculateCorners(const float corners[4][2], bool normalized, int sample); + + private: + friend class PlaneTrackCommon; +}; + +class PlaneDistortWarpImageOperation : public PlaneDistortBaseOperation { + protected: + SocketReader *m_pixelReader; + + public: PlaneDistortWarpImageOperation(); - void calculateCorners(const float corners[4][2], bool normalized, int sample); + void calculateCorners(const float corners[4][2], bool normalized, int sample) override; void initExecution() override; void deinitExecution() override; @@ -56,47 +78,19 @@ class PlaneDistortWarpImageOperation : public NodeOperation { bool determineDependingAreaOfInterest(rcti *input, ReadBufferOperation *readOperation, rcti *output) override; - - void setMotionBlurSamples(int samples) - { - BLI_assert(samples <= PLANE_DISTORT_MAX_SAMPLES); - this->m_motion_blur_samples = samples; - } - void setMotionBlurShutter(float shutter) - { - this->m_motion_blur_shutter = shutter; - } }; -class PlaneDistortMaskOperation : public NodeOperation { +class PlaneDistortMaskOperation : public PlaneDistortBaseOperation { protected: - struct MotionSample { - float frameSpaceCorners[4][2]; /* Corners coordinates in pixel space. */ - }; int m_osa; - MotionSample m_samples[PLANE_DISTORT_MAX_SAMPLES]; float m_jitter[32][2]; - int m_motion_blur_samples; - float m_motion_blur_shutter; public: PlaneDistortMaskOperation(); - void calculateCorners(const float corners[4][2], bool normalized, int sample); - void initExecution() override; void executePixelSampled(float output[4], float x, float y, PixelSampler sampler) override; - - void setMotionBlurSamples(int samples) - { - BLI_assert(samples <= PLANE_DISTORT_MAX_SAMPLES); - this->m_motion_blur_samples = samples; - } - void setMotionBlurShutter(float shutter) - { - this->m_motion_blur_shutter = shutter; - } }; } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_PlaneTrackOperation.cc b/source/blender/compositor/operations/COM_PlaneTrackOperation.cc index 565bde6c945..0884f2ad979 100644 --- a/source/blender/compositor/operations/COM_PlaneTrackOperation.cc +++ b/source/blender/compositor/operations/COM_PlaneTrackOperation.cc @@ -41,6 +41,26 @@ PlaneTrackCommon::PlaneTrackCommon() this->m_planeTrackName[0] = '\0'; } +void PlaneTrackCommon::read_and_calculate_corners(PlaneDistortBaseOperation *distort_op) +{ + float corners[4][2]; + if (distort_op->m_motion_blur_samples == 1) { + readCornersFromTrack(corners, this->m_framenumber); + distort_op->calculateCorners(corners, true, 0); + } + else { + const float frame = (float)this->m_framenumber - distort_op->m_motion_blur_shutter; + const float frame_step = (distort_op->m_motion_blur_shutter * 2.0f) / + distort_op->m_motion_blur_samples; + float frame_iter = frame; + for (int sample = 0; sample < distort_op->m_motion_blur_samples; sample++) { + readCornersFromTrack(corners, frame_iter); + distort_op->calculateCorners(corners, true, sample); + frame_iter += frame_step; + } + } +} + void PlaneTrackCommon::readCornersFromTrack(float corners[4][2], float frame) { MovieTracking *tracking; @@ -84,21 +104,7 @@ void PlaneTrackCommon::determineResolution(unsigned int resolution[2], void PlaneTrackMaskOperation::initExecution() { PlaneDistortMaskOperation::initExecution(); - float corners[4][2]; - if (this->m_motion_blur_samples == 1) { - readCornersFromTrack(corners, this->m_framenumber); - calculateCorners(corners, true, 0); - } - else { - const float frame = (float)this->m_framenumber - this->m_motion_blur_shutter; - const float frame_step = (this->m_motion_blur_shutter * 2.0f) / this->m_motion_blur_samples; - float frame_iter = frame; - for (int sample = 0; sample < this->m_motion_blur_samples; sample++) { - readCornersFromTrack(corners, frame_iter); - calculateCorners(corners, true, sample); - frame_iter += frame_step; - } - } + PlaneTrackCommon::read_and_calculate_corners(this); } /* ******** PlaneTrackWarpImageOperation ******** */ @@ -106,22 +112,7 @@ void PlaneTrackMaskOperation::initExecution() void PlaneTrackWarpImageOperation::initExecution() { PlaneDistortWarpImageOperation::initExecution(); - /* TODO(sergey): De-duplicate with mask operation. */ - float corners[4][2]; - if (this->m_motion_blur_samples == 1) { - readCornersFromTrack(corners, this->m_framenumber); - calculateCorners(corners, true, 0); - } - else { - const float frame = (float)this->m_framenumber - this->m_motion_blur_shutter; - const float frame_step = (this->m_motion_blur_shutter * 2.0f) / this->m_motion_blur_samples; - float frame_iter = frame; - for (int sample = 0; sample < this->m_motion_blur_samples; sample++) { - readCornersFromTrack(corners, frame_iter); - calculateCorners(corners, true, sample); - frame_iter += frame_step; - } - } + PlaneTrackCommon::read_and_calculate_corners(this); } } // namespace blender::compositor diff --git a/source/blender/compositor/operations/COM_PlaneTrackOperation.h b/source/blender/compositor/operations/COM_PlaneTrackOperation.h index 95a7d536742..d240c8b06e9 100644 --- a/source/blender/compositor/operations/COM_PlaneTrackOperation.h +++ b/source/blender/compositor/operations/COM_PlaneTrackOperation.h @@ -40,7 +40,7 @@ class PlaneTrackCommon { /* note: this class is not an operation itself (to prevent virtual inheritance issues) * implementation classes must make wrappers to use these methods, see below. */ - void readCornersFromTrack(float corners[4][2], float frame); + void read_and_calculate_corners(PlaneDistortBaseOperation *distort_op); void determineResolution(unsigned int resolution[2], unsigned int preferredResolution[2]); public: @@ -62,6 +62,9 @@ class PlaneTrackCommon { { this->m_framenumber = framenumber; } + + private: + void readCornersFromTrack(float corners[4][2], float frame); }; class PlaneTrackMaskOperation : public PlaneDistortMaskOperation, public PlaneTrackCommon { |