void rotateimage(std::shared_ptr<PrimitiveObject> src, std::shared_ptr<PrimitiveObject> & dst, float angle,bool balpha) {
// 计算旋转角度的弧度值
double radians = angle * 3.14159 / 180.0;
int width = src->userData().get2<int>("w");
int height = src->userData().get2<int>("h");
// 计算旋转中心点
int centerX = width / 2;
int centerY = height / 2;
// 计算旋转后的图像宽度和高度
int rotatedWidth = static_cast<int>(std::abs(width * cos(radians)) + std::abs(height * sin(radians)));
int rotatedHeight = static_cast<int>(std::abs(height * cos(radians)) + std::abs(width * sin(radians)));
dst->verts.resize(rotatedWidth * rotatedHeight);
dst->userData().set2("w", rotatedWidth);
dst->userData().set2("h", rotatedHeight);
if(src->verts.has_attr("alpha")){
dst->verts.add_attr<float>("alpha");
if(balpha){
for(int i = 0;i<dst->size();i++){
dst->verts.attr<float>("alpha")[i] = 0;
}
}
else{
for(int i = 0;i<dst->size();i++){
dst->verts.attr<float>("alpha")[i] = 1;
}
}
for (int y = 0; y < rotatedHeight; ++y) {
for (int x = 0; x < rotatedWidth; ++x) {
// 计算旋转前的坐标
int srcX = static_cast<int>((x - rotatedWidth / 2) * cos(-radians) - (y - rotatedHeight / 2) * sin(-radians) + centerX);
int srcY = static_cast<int>((x - rotatedWidth / 2) * sin(-radians) + (y - rotatedHeight / 2) * cos(-radians) + centerY);
// 检查坐标是否在旋转图像范围内
if (srcX >= 0 && srcX < width && srcY >= 0 && srcY < height) {
// 获取旋转前的像素值
dst->verts[y * rotatedWidth + x] = src->verts[srcY * width + srcX] ;
dst->verts.attr<float>("alpha")[y * rotatedWidth + x] = src->verts.attr<float>("alpha")[srcY * width + srcX];
}
}
}
}
else{
dst->verts.add_attr<float>("alpha");
if(balpha){
for(int i = 0;i<dst->size();i++){
dst->verts.attr<float>("alpha")[i] = 0;
}
}
else{
for(int i = 0;i<dst->size();i++){
dst->verts.attr<float>("alpha")[i] = 1;
}
}
for (int y = 0; y < rotatedHeight; ++y) {
for (int x = 0; x < rotatedWidth; ++x) {
// 计算旋转前的坐标
int srcX = static_cast<int>((x - rotatedWidth / 2) * cos(-radians) - (y - rotatedHeight / 2) * sin(-radians) + centerX);
int srcY = static_cast<int>((x - rotatedWidth / 2) * sin(-radians) + (y - rotatedHeight / 2) * cos(-radians) + centerY);
// 检查坐标是否在旋转图像范围内
if (srcX >= 0 && srcX < width && srcY >= 0 && srcY < height) {
// 获取旋转前的像素值
dst->verts[y * rotatedWidth + x] = src->verts[srcY * width + srcX] ;
dst->verts.attr<float>("alpha")[y * rotatedWidth + x] = 1;
}
}
}
}
// 遍历旋转后的图像像素
for (int y = 0; y < rotatedHeight; ++y) {
for (int x = 0; x < rotatedWidth; ++x) {
// 计算旋转前的坐标
int srcX = static_cast<int>((x - rotatedWidth / 2) * cos(-radians) - (y - rotatedHeight / 2) * sin(-radians) + centerX);
int srcY = static_cast<int>((x - rotatedWidth / 2) * sin(-radians) + (y - rotatedHeight / 2) * cos(-radians) + centerY);
// 检查坐标是否在旋转图像范围内
if (srcX >= 0 && srcX < width && srcY >= 0 && srcY < height) {
// 获取旋转前的像素值
dst->verts[y * rotatedWidth + x] = src->verts[srcY * width + srcX] ;
}
}
}
}
struct ImageRotate: INode {
void apply() override {
std::shared_ptr<PrimitiveObject> image = get_input<PrimitiveObject>("image");
auto balpha = get_input2<bool>("alpha");
float rotate = get_input2<float>("rotate");
auto &ud = image->userData();
int w = ud.get2<int>("w");
int h = ud.get2<int>("h");
auto image2 = std::make_shared<PrimitiveObject>();
image2->verts.resize(w * h);
image2->userData().set2("isImage", 1);
rotateimage(image,image2,rotate,balpha);
set_output("image", image2);
}
};
ZENDEFNODE(ImageRotate, {
{
{"image"},
{"float", "rotate", "0.0"},
{"bool", "alpha", "0"},
},
{
{"image"},
},
{},
{"comp"},
});