2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2021 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2021-08-23 15:30:01 +02:00
|
|
|
|
|
|
|
|
#include "COM_TransformOperation.h"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_rotation.h"
|
2021-08-23 15:30:01 +02:00
|
|
|
#include "COM_RotateOperation.h"
|
|
|
|
|
#include "COM_ScaleOperation.h"
|
|
|
|
|
|
|
|
|
|
namespace blender::compositor {
|
|
|
|
|
|
|
|
|
|
TransformOperation::TransformOperation()
|
|
|
|
|
{
|
2021-10-13 23:01:15 +02:00
|
|
|
add_input_socket(DataType::Color, ResizeMode::None);
|
|
|
|
|
add_input_socket(DataType::Value, ResizeMode::None);
|
|
|
|
|
add_input_socket(DataType::Value, ResizeMode::None);
|
|
|
|
|
add_input_socket(DataType::Value, ResizeMode::None);
|
|
|
|
|
add_input_socket(DataType::Value, ResizeMode::None);
|
|
|
|
|
add_output_socket(DataType::Color);
|
2021-08-23 15:30:01 +02:00
|
|
|
translate_factor_x_ = 1.0f;
|
|
|
|
|
translate_factor_y_ = 1.0f;
|
|
|
|
|
convert_degree_to_rad_ = false;
|
|
|
|
|
sampler_ = PixelSampler::Bilinear;
|
|
|
|
|
invert_ = false;
|
2023-11-26 12:14:35 +01:00
|
|
|
|
|
|
|
|
flags_.can_be_constant = true;
|
2021-09-28 19:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-23 15:30:01 +02:00
|
|
|
void TransformOperation::init_data()
|
|
|
|
|
{
|
|
|
|
|
|
2021-09-28 19:33:06 +02:00
|
|
|
translate_x_ = get_input_operation(X_INPUT_INDEX)->get_constant_value_default(0.0f) *
|
|
|
|
|
translate_factor_x_;
|
|
|
|
|
translate_y_ = get_input_operation(Y_INPUT_INDEX)->get_constant_value_default(0.0f) *
|
|
|
|
|
translate_factor_y_;
|
2021-08-23 15:30:01 +02:00
|
|
|
|
2021-09-28 19:33:06 +02:00
|
|
|
const float degree = get_input_operation(DEGREE_INPUT_INDEX)->get_constant_value_default(0.0f);
|
2022-09-25 18:33:28 +10:00
|
|
|
const double rad = convert_degree_to_rad_ ? DEG2RAD(double(degree)) : degree;
|
2021-08-23 15:30:01 +02:00
|
|
|
rotate_cosine_ = cos(rad);
|
|
|
|
|
rotate_sine_ = sin(rad);
|
2021-09-28 19:33:06 +02:00
|
|
|
|
|
|
|
|
scale_ = get_input_operation(SCALE_INPUT_INDEX)->get_constant_value_default(1.0f);
|
2021-08-23 15:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TransformOperation::get_area_of_interest(const int input_idx,
|
|
|
|
|
const rcti &output_area,
|
|
|
|
|
rcti &r_input_area)
|
|
|
|
|
{
|
|
|
|
|
switch (input_idx) {
|
|
|
|
|
case IMAGE_INPUT_INDEX: {
|
2021-09-28 19:33:06 +02:00
|
|
|
NodeOperation *image_op = get_input_operation(IMAGE_INPUT_INDEX);
|
|
|
|
|
const rcti &image_canvas = image_op->get_canvas();
|
|
|
|
|
if (invert_) {
|
|
|
|
|
/* Scale -> Rotate -> Translate. */
|
|
|
|
|
r_input_area = output_area;
|
|
|
|
|
BLI_rcti_translate(&r_input_area, -translate_x_, -translate_y_);
|
|
|
|
|
RotateOperation::get_rotation_area_of_interest(scale_canvas_,
|
|
|
|
|
rotate_canvas_,
|
|
|
|
|
rotate_sine_,
|
|
|
|
|
rotate_cosine_,
|
|
|
|
|
r_input_area,
|
|
|
|
|
r_input_area);
|
|
|
|
|
ScaleOperation::get_scale_area_of_interest(
|
|
|
|
|
image_canvas, scale_canvas_, scale_, scale_, r_input_area, r_input_area);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Translate -> Rotate -> Scale. */
|
|
|
|
|
ScaleOperation::get_scale_area_of_interest(
|
|
|
|
|
rotate_canvas_, scale_canvas_, scale_, scale_, output_area, r_input_area);
|
|
|
|
|
RotateOperation::get_rotation_area_of_interest(translate_canvas_,
|
|
|
|
|
rotate_canvas_,
|
|
|
|
|
rotate_sine_,
|
|
|
|
|
rotate_cosine_,
|
|
|
|
|
r_input_area,
|
|
|
|
|
r_input_area);
|
|
|
|
|
BLI_rcti_translate(&r_input_area, -translate_x_, -translate_y_);
|
|
|
|
|
}
|
2021-08-23 15:30:01 +02:00
|
|
|
expand_area_for_sampler(r_input_area, sampler_);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case X_INPUT_INDEX:
|
|
|
|
|
case Y_INPUT_INDEX:
|
2021-09-28 19:33:06 +02:00
|
|
|
case DEGREE_INPUT_INDEX:
|
2021-08-23 15:30:01 +02:00
|
|
|
case SCALE_INPUT_INDEX: {
|
2021-09-28 19:33:06 +02:00
|
|
|
r_input_area = COM_CONSTANT_INPUT_AREA_OF_INTEREST;
|
2021-08-23 15:30:01 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TransformOperation::update_memory_buffer_partial(MemoryBuffer *output,
|
|
|
|
|
const rcti &area,
|
|
|
|
|
Span<MemoryBuffer *> inputs)
|
|
|
|
|
{
|
|
|
|
|
const MemoryBuffer *input_img = inputs[IMAGE_INPUT_INDEX];
|
2021-09-28 19:33:06 +02:00
|
|
|
BuffersIterator<float> it = output->iterate_with({}, area);
|
2021-08-23 15:30:01 +02:00
|
|
|
if (invert_) {
|
|
|
|
|
transform_inverted(it, input_img);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
transform(it, input_img);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 19:33:06 +02:00
|
|
|
void TransformOperation::determine_canvas(const rcti &preferred_area, rcti &r_area)
|
|
|
|
|
{
|
|
|
|
|
const bool image_determined =
|
2021-10-13 23:01:15 +02:00
|
|
|
get_input_socket(IMAGE_INPUT_INDEX)->determine_canvas(preferred_area, r_area);
|
2021-09-28 19:33:06 +02:00
|
|
|
if (image_determined) {
|
|
|
|
|
rcti image_canvas = r_area;
|
2021-11-29 19:23:43 +01:00
|
|
|
rcti unused = COM_AREA_NONE;
|
2021-10-13 23:01:15 +02:00
|
|
|
get_input_socket(X_INPUT_INDEX)->determine_canvas(image_canvas, unused);
|
|
|
|
|
get_input_socket(Y_INPUT_INDEX)->determine_canvas(image_canvas, unused);
|
|
|
|
|
get_input_socket(DEGREE_INPUT_INDEX)->determine_canvas(image_canvas, unused);
|
|
|
|
|
get_input_socket(SCALE_INPUT_INDEX)->determine_canvas(image_canvas, unused);
|
2021-09-28 19:33:06 +02:00
|
|
|
|
|
|
|
|
init_data();
|
|
|
|
|
if (invert_) {
|
|
|
|
|
/* Scale -> Rotate -> Translate. */
|
|
|
|
|
scale_canvas_ = image_canvas;
|
|
|
|
|
ScaleOperation::scale_area(scale_canvas_, scale_, scale_);
|
|
|
|
|
|
|
|
|
|
RotateOperation::get_rotation_canvas(
|
|
|
|
|
scale_canvas_, rotate_sine_, rotate_cosine_, rotate_canvas_);
|
|
|
|
|
|
|
|
|
|
translate_canvas_ = rotate_canvas_;
|
|
|
|
|
BLI_rcti_translate(&translate_canvas_, translate_x_, translate_y_);
|
|
|
|
|
|
|
|
|
|
r_area = translate_canvas_;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Translate -> Rotate -> Scale. */
|
|
|
|
|
translate_canvas_ = image_canvas;
|
|
|
|
|
BLI_rcti_translate(&translate_canvas_, translate_x_, translate_y_);
|
|
|
|
|
|
|
|
|
|
RotateOperation::get_rotation_canvas(
|
|
|
|
|
translate_canvas_, rotate_sine_, rotate_cosine_, rotate_canvas_);
|
|
|
|
|
|
|
|
|
|
scale_canvas_ = rotate_canvas_;
|
|
|
|
|
ScaleOperation::scale_area(scale_canvas_, scale_, scale_);
|
|
|
|
|
|
|
|
|
|
r_area = scale_canvas_;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 15:30:01 +02:00
|
|
|
void TransformOperation::transform(BuffersIterator<float> &it, const MemoryBuffer *input_img)
|
|
|
|
|
{
|
2021-09-28 19:33:06 +02:00
|
|
|
float rotate_center_x, rotate_center_y;
|
|
|
|
|
RotateOperation::get_rotation_center(translate_canvas_, rotate_center_x, rotate_center_y);
|
|
|
|
|
float rotate_offset_x, rotate_offset_y;
|
|
|
|
|
RotateOperation::get_rotation_offset(
|
|
|
|
|
translate_canvas_, rotate_canvas_, rotate_offset_x, rotate_offset_y);
|
|
|
|
|
|
|
|
|
|
const float scale_center_x = BLI_rcti_size_x(&rotate_canvas_) / 2.0f;
|
|
|
|
|
const float scale_center_y = BLI_rcti_size_y(&rotate_canvas_) / 2.0f;
|
|
|
|
|
float scale_offset_x, scale_offset_y;
|
|
|
|
|
ScaleOperation::get_scale_offset(rotate_canvas_, scale_canvas_, scale_offset_x, scale_offset_y);
|
|
|
|
|
|
2021-08-23 15:30:01 +02:00
|
|
|
for (; !it.is_end(); ++it) {
|
2021-09-28 19:33:06 +02:00
|
|
|
float x = ScaleOperation::scale_coord_inverted(it.x + scale_offset_x, scale_center_x, scale_);
|
|
|
|
|
float y = ScaleOperation::scale_coord_inverted(it.y + scale_offset_y, scale_center_y, scale_);
|
|
|
|
|
|
|
|
|
|
x = rotate_offset_x + x;
|
|
|
|
|
y = rotate_offset_y + y;
|
2021-08-23 15:30:01 +02:00
|
|
|
RotateOperation::rotate_coords(
|
2021-09-28 19:33:06 +02:00
|
|
|
x, y, rotate_center_x, rotate_center_y, rotate_sine_, rotate_cosine_);
|
|
|
|
|
|
|
|
|
|
input_img->read_elem_sampled(x - translate_x_, y - translate_y_, sampler_, it.out);
|
2021-08-23 15:30:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TransformOperation::transform_inverted(BuffersIterator<float> &it,
|
|
|
|
|
const MemoryBuffer *input_img)
|
|
|
|
|
{
|
2021-09-28 19:33:06 +02:00
|
|
|
const rcti &image_canvas = get_input_operation(IMAGE_INPUT_INDEX)->get_canvas();
|
|
|
|
|
const float scale_center_x = BLI_rcti_size_x(&image_canvas) / 2.0f - translate_x_;
|
|
|
|
|
const float scale_center_y = BLI_rcti_size_y(&image_canvas) / 2.0f - translate_y_;
|
|
|
|
|
float scale_offset_x, scale_offset_y;
|
|
|
|
|
ScaleOperation::get_scale_offset(image_canvas, scale_canvas_, scale_offset_x, scale_offset_y);
|
|
|
|
|
|
|
|
|
|
float rotate_center_x, rotate_center_y;
|
|
|
|
|
RotateOperation::get_rotation_center(translate_canvas_, rotate_center_x, rotate_center_y);
|
|
|
|
|
rotate_center_x -= translate_x_;
|
|
|
|
|
rotate_center_y -= translate_y_;
|
|
|
|
|
float rotate_offset_x, rotate_offset_y;
|
|
|
|
|
RotateOperation::get_rotation_offset(
|
|
|
|
|
scale_canvas_, rotate_canvas_, rotate_offset_x, rotate_offset_y);
|
|
|
|
|
|
2021-08-23 15:30:01 +02:00
|
|
|
for (; !it.is_end(); ++it) {
|
2021-09-28 19:33:06 +02:00
|
|
|
float x = rotate_offset_x + (it.x - translate_x_);
|
|
|
|
|
float y = rotate_offset_y + (it.y - translate_y_);
|
2021-08-23 15:30:01 +02:00
|
|
|
RotateOperation::rotate_coords(
|
2021-09-28 19:33:06 +02:00
|
|
|
x, y, rotate_center_x, rotate_center_y, rotate_sine_, rotate_cosine_);
|
|
|
|
|
|
|
|
|
|
x = ScaleOperation::scale_coord_inverted(x + scale_offset_x, scale_center_x, scale_);
|
|
|
|
|
y = ScaleOperation::scale_coord_inverted(y + scale_offset_y, scale_center_y, scale_);
|
|
|
|
|
|
2021-08-23 15:30:01 +02:00
|
|
|
input_img->read_elem_sampled(x, y, sampler_, it.out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace blender::compositor
|