From 4d302261bd13de807dec0930fab7f6c4fa3c745e Mon Sep 17 00:00:00 2001 From: minggo Date: Wed, 1 Jun 2022 17:37:07 +0800 Subject: [PATCH] add copy constructor and operator= functions (#11318) * add copy constructor and operator= functions --- native/cocos/core/geometry/Frustum.cpp | 37 ++++++++++++++++++++ native/cocos/core/geometry/Frustum.h | 47 +++++++++++++------------- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/native/cocos/core/geometry/Frustum.cpp b/native/cocos/core/geometry/Frustum.cpp index f65560d1a2..a5444eeb3f 100644 --- a/native/cocos/core/geometry/Frustum.cpp +++ b/native/cocos/core/geometry/Frustum.cpp @@ -211,5 +211,42 @@ void Frustum::updatePlanes() { planes[5]->define(vertices[7], vertices[5], vertices[6]); } +Frustum::Frustum() { + init(); +} + +Frustum::Frustum(const Frustum &rhs) : ShapeBase(rhs) { + init(); + *this = rhs; +} + +Frustum::~Frustum() { + for (auto *plane : planes) { + plane->release(); + } +} + +Frustum &Frustum::operator=(const Frustum &rhs) { + if (this == &rhs) { + return *this; + } + + vertices = rhs.vertices; + + for (size_t i = 0; i < planes.size(); ++i) { // NOLINT(modernize-loop-convert) + Plane::copy(planes[i], *rhs.planes[i]); + } + + return *this; +} + +void Frustum::init() { + setType(ShapeEnum::SHAPE_FRUSTUM); + for (size_t i = 0; i < planes.size(); ++i) { // NOLINT(modernize-loop-convert) + planes[i] = ccnew Plane(); + planes[i]->addRef(); + } +} + } // namespace geometry } // namespace cc diff --git a/native/cocos/core/geometry/Frustum.h b/native/cocos/core/geometry/Frustum.h index 06e4fd4fc8..5b590c5c35 100644 --- a/native/cocos/core/geometry/Frustum.h +++ b/native/cocos/core/geometry/Frustum.h @@ -122,29 +122,14 @@ public: return out; } - /** - * @en - * Set whether to use accurate intersection testing function on this frustum. - * @zh - * 设置是否在此截锥体上使用精确的相交测试函数。 - */ - void setAccurate(bool accurate) { - setType(accurate ? ShapeEnum::SHAPE_FRUSTUM_ACCURATE : ShapeEnum::SHAPE_FRUSTUM); - } + Frustum(); + Frustum(const Frustum &rhs); + Frustum(Frustum &&rhs) = delete; + ~Frustum() override; - Frustum() { - setType(ShapeEnum::SHAPE_FRUSTUM); - for (size_t i = 0; i < planes.size(); ++i) { // NOLINT(modernize-loop-convert) - planes[i] = ccnew Plane(); - planes[i]->addRef(); - } - } - - ~Frustum() override { - for (auto *plane : planes) { - plane->release(); - } - } + // Can remove these operator override functions if not using Plane* in planes array. + Frustum &operator=(const Frustum &rhs); + Frustum &operator=(Frustum &&rhs) = delete; /** * @en @@ -155,12 +140,26 @@ public: */ void transform(const Mat4 &); - ccstd::array vertices; - ccstd::array planes; void createOrtho(float width, float height, float near, float far, const Mat4 &transform); void split(float start, float end, float aspect, float fov, const Mat4 &transform); void updatePlanes(); void update(const Mat4 &m, const Mat4 &inv); + + /** + * @en + * Set whether to use accurate intersection testing function on this frustum. + * @zh + * 设置是否在此截锥体上使用精确的相交测试函数。 + */ + inline void setAccurate(bool accurate) { + setType(accurate ? ShapeEnum::SHAPE_FRUSTUM_ACCURATE : ShapeEnum::SHAPE_FRUSTUM); + } + + ccstd::array vertices; + ccstd::array planes; + +private: + void init(); }; } // namespace geometry