From 258ea03d0855d765743ae443030edbd757a5045c Mon Sep 17 00:00:00 2001 From: shiquanyu Date: Fri, 12 Mar 2021 00:14:20 +0800 Subject: [PATCH 1/3] Add 2D transform functions --- src/CMakeLists.txt | 2 + src/transform.cpp | 160 +++++++++++++++++++++++++++++++++++++++++++++ src/transform.h | 46 +++++++++++++ 3 files changed, 208 insertions(+) create mode 100644 src/transform.cpp create mode 100644 src/transform.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e15ee4be..51db56258 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,6 +29,7 @@ set(ncnn_SRCS mat_pixel_affine.cpp mat_pixel_resize.cpp mat_pixel_rotate.cpp + transform.cpp modelbin.cpp net.cpp option.cpp @@ -374,6 +375,7 @@ if(NCNN_INSTALL_SDK) layer_shader_type.h layer_type.h mat.h + transform.h modelbin.h net.h option.h diff --git a/src/transform.cpp b/src/transform.cpp new file mode 100644 index 000000000..ac21eb119 --- /dev/null +++ b/src/transform.cpp @@ -0,0 +1,160 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include + +#include "transform.h" + +namespace ncnn { + +namespace transform { + +Mat combine(const Mat& m1, const Mat& m2) +{ + Mat M(3, 2, sizeof(float)); + float row3[] = {0, 0, 1}; + float* p = M; + const float* p1 = m1; + const float* p2 = m2; + for (int row = 0; row < 2; ++row) + { + for (int col = 0; col < 3; ++col) + { + p[row * 3 + col] = p1[row * 3 + 0] * p2[0 * 3 + col] + + p1[row * 3 + 1] * p2[1 * 3 + col] + + p1[row * 3 + 2] * row3[col]; + } + } + return M; +} + +Mat rotation(const float degree, const float* center) +{ + int ndim = 2; + Mat M(3, 2, sizeof(float)); + float *mat = M; + float rad = degree * 3.14159265358979323846 / 180.0; + float c = std::cos(rad); + float s = std::sin(rad); + + M.fill(0); + mat[0] = c; + mat[1] = -s; + mat[3] = s; + mat[4] = c; + + if (center) + { + for (int d = 0; d < ndim; d++) + { + mat[d*3+ndim] = center[d] - center[0] * mat[d*3+0] - center[1] * mat[d*3+1]; + } + } + + return M; +} + +Mat shear(const float degree, float* center) +{ + int ndim = 2; + Mat M(3, 2, sizeof(float)); + float *mat = M; + float rad = degree * 3.14159265358979323846 / 180.0; + float s = std::tan(rad); + + M.fill(0); + mat[0] = 1; + mat[1] = s; + mat[3] = s; + mat[4] = 1; + + if (center) + { + for (int d = 0; d < ndim; d++) + { + mat[d*3+ndim] = center[d] - center[0] * mat[d*3+0] - center[1] * mat[d*3+1]; + } + } + + return M; +} + +Mat shear(const float* degrees, const float* center) +{ + int ndim = 2; + Mat M(3, 2, sizeof(float)); + float *mat = M; + float rad1 = degrees[0] * 3.14159265358979323846 / 180.0; + float s1 = std::tan(rad1); + float rad2 = degrees[1] * 3.14159265358979323846 / 180.0; + float s2 = std::tan(rad2); + + M.fill(0); + mat[0] = 1; + mat[1] = s1; + mat[3] = s2; + mat[4] = 1; + + if (center) + { + for (int d = 0; d < ndim; d++) + { + mat[d*3+ndim] = center[d] - center[0] * mat[d*3+0] - center[1] * mat[d*3+1]; + } + } + + return M; +} + +Mat scale(const float* scale, const float* center) +{ + int ndim = 2; + Mat M(3, 2, sizeof(float)); + M.fill(0); + float *mat = M; + + for (int d = 0; d < ndim; d++) + { + mat[d * 3 + d] = scale[d]; + } + if (center) + { + for (int d = 0; d < ndim; d++) + { + mat[d*3+ndim] = center[d] * (1 - scale[d]); + } + } + + return M; +} + +Mat translation(const float* offsets) +{ + int ndim = 2; + Mat M(3, 2, sizeof(float)); + float *mat = M; + + M.fill(0); + mat[0] = 1; + mat[4] = 1; + for (int d = 0; d < ndim; d++) { + mat[d * 3 + ndim] = offsets[d]; + } + + return M; +} + +} // namespace transform + +} // namespace ncnn diff --git a/src/transform.h b/src/transform.h new file mode 100644 index 000000000..a04a24836 --- /dev/null +++ b/src/transform.h @@ -0,0 +1,46 @@ +// Tencent is pleased to support the open source community by making ncnn available. +// +// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. +// +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// https://opensource.org/licenses/BSD-3-Clause +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef NCNN_TRANSFORM_H +#define NCNN_TRANSFORM_H + +#include "mat.h" + +namespace ncnn { + +namespace transform { + +Mat combine(const Mat& m1, const Mat& m2); + +template +Mat combine(const Mat& m1, const Mat& m2, const Args&... mats) +{ + return combine(combine(m1, m2), mats...); +} + +Mat rotation(const float degree, const float* center = nullptr); + +Mat shear(const float degree, float* center = nullptr); + +Mat shear(const float* degrees, const float* center = nullptr); + +Mat scale(const float* scale, const float* center = nullptr); + +Mat translation(const float* offsets); + +} // namespace transform + +} // namespace ncnn + +#endif // NCNN_TRANSFORM_H From 3121e63be41183899862593269c05e45498aa518 Mon Sep 17 00:00:00 2001 From: shiquanyu Date: Fri, 12 Mar 2021 00:52:31 +0800 Subject: [PATCH 2/3] fixed error in some platform --- src/transform.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transform.cpp b/src/transform.cpp index ac21eb119..ce8a7f09a 100644 --- a/src/transform.cpp +++ b/src/transform.cpp @@ -12,7 +12,7 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. -#include +#include #include "transform.h" From cb63b3c90ef0f0ce4f424f310a0dc11edd410544 Mon Sep 17 00:00:00 2001 From: shiquanyu Date: Sat, 13 Mar 2021 15:07:02 +0800 Subject: [PATCH 3/3] format code style; using c head math.h instead of cmath --- src/transform.cpp | 39 +++++++++++++++++++-------------------- src/transform.h | 8 ++++---- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/transform.cpp b/src/transform.cpp index ce8a7f09a..368c5d3d7 100644 --- a/src/transform.cpp +++ b/src/transform.cpp @@ -12,7 +12,7 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. -#include +#include #include "transform.h" @@ -31,9 +31,7 @@ Mat combine(const Mat& m1, const Mat& m2) { for (int col = 0; col < 3; ++col) { - p[row * 3 + col] = p1[row * 3 + 0] * p2[0 * 3 + col] + - p1[row * 3 + 1] * p2[1 * 3 + col] + - p1[row * 3 + 2] * row3[col]; + p[row * 3 + col] = p1[row * 3 + 0] * p2[0 * 3 + col] + p1[row * 3 + 1] * p2[1 * 3 + col] + p1[row * 3 + 2] * row3[col]; } } return M; @@ -43,10 +41,10 @@ Mat rotation(const float degree, const float* center) { int ndim = 2; Mat M(3, 2, sizeof(float)); - float *mat = M; + float* mat = M; float rad = degree * 3.14159265358979323846 / 180.0; - float c = std::cos(rad); - float s = std::sin(rad); + float c = cos(rad); + float s = sin(rad); M.fill(0); mat[0] = c; @@ -58,20 +56,20 @@ Mat rotation(const float degree, const float* center) { for (int d = 0; d < ndim; d++) { - mat[d*3+ndim] = center[d] - center[0] * mat[d*3+0] - center[1] * mat[d*3+1]; + mat[d * 3 + ndim] = center[d] - center[0] * mat[d * 3 + 0] - center[1] * mat[d * 3 + 1]; } } return M; } -Mat shear(const float degree, float* center) +Mat shear(const float degree, float* center) { int ndim = 2; Mat M(3, 2, sizeof(float)); - float *mat = M; + float* mat = M; float rad = degree * 3.14159265358979323846 / 180.0; - float s = std::tan(rad); + float s = tan(rad); M.fill(0); mat[0] = 1; @@ -83,7 +81,7 @@ Mat shear(const float degree, float* center) { for (int d = 0; d < ndim; d++) { - mat[d*3+ndim] = center[d] - center[0] * mat[d*3+0] - center[1] * mat[d*3+1]; + mat[d * 3 + ndim] = center[d] - center[0] * mat[d * 3 + 0] - center[1] * mat[d * 3 + 1]; } } @@ -94,11 +92,11 @@ Mat shear(const float* degrees, const float* center) { int ndim = 2; Mat M(3, 2, sizeof(float)); - float *mat = M; + float* mat = M; float rad1 = degrees[0] * 3.14159265358979323846 / 180.0; - float s1 = std::tan(rad1); + float s1 = tan(rad1); float rad2 = degrees[1] * 3.14159265358979323846 / 180.0; - float s2 = std::tan(rad2); + float s2 = tan(rad2); M.fill(0); mat[0] = 1; @@ -110,7 +108,7 @@ Mat shear(const float* degrees, const float* center) { for (int d = 0; d < ndim; d++) { - mat[d*3+ndim] = center[d] - center[0] * mat[d*3+0] - center[1] * mat[d*3+1]; + mat[d * 3 + ndim] = center[d] - center[0] * mat[d * 3 + 0] - center[1] * mat[d * 3 + 1]; } } @@ -122,7 +120,7 @@ Mat scale(const float* scale, const float* center) int ndim = 2; Mat M(3, 2, sizeof(float)); M.fill(0); - float *mat = M; + float* mat = M; for (int d = 0; d < ndim; d++) { @@ -132,7 +130,7 @@ Mat scale(const float* scale, const float* center) { for (int d = 0; d < ndim; d++) { - mat[d*3+ndim] = center[d] * (1 - scale[d]); + mat[d * 3 + ndim] = center[d] * (1 - scale[d]); } } @@ -143,12 +141,13 @@ Mat translation(const float* offsets) { int ndim = 2; Mat M(3, 2, sizeof(float)); - float *mat = M; + float* mat = M; M.fill(0); mat[0] = 1; mat[4] = 1; - for (int d = 0; d < ndim; d++) { + for (int d = 0; d < ndim; d++) + { mat[d * 3 + ndim] = offsets[d]; } diff --git a/src/transform.h b/src/transform.h index a04a24836..34ef819d5 100644 --- a/src/transform.h +++ b/src/transform.h @@ -29,13 +29,13 @@ Mat combine(const Mat& m1, const Mat& m2, const Args&... mats) return combine(combine(m1, m2), mats...); } -Mat rotation(const float degree, const float* center = nullptr); +Mat rotation(const float degree, const float* center = NULL); -Mat shear(const float degree, float* center = nullptr); +Mat shear(const float degree, float* center = NULL); -Mat shear(const float* degrees, const float* center = nullptr); +Mat shear(const float* degrees, const float* center = NULL); -Mat scale(const float* scale, const float* center = nullptr); +Mat scale(const float* scale, const float* center = NULL); Mat translation(const float* offsets);