From 693e3c66c83c799e326fa465c15b4ed528054cc2 Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Wed, 13 Mar 2024 04:47:51 -0700 Subject: [PATCH] Add copy constructors and assignments to user_objects. All of the constructors and assignments operators are declared by this CL, but only those that are currently used are implemented here. PiperOrigin-RevId: 615369559 Change-Id: I9f8cc4a7cd2070362baba04f2af4c5567a696596 --- src/user/user_mesh.cc | 79 +++++++++++++++++++++++- src/user/user_objects.cc | 126 ++++++++++++++++++++++++++++++++------- src/user/user_objects.h | 95 ++++++++++++++++++++++------- 3 files changed, 256 insertions(+), 44 deletions(-) diff --git a/src/user/user_mesh.cc b/src/user/user_mesh.cc index 7e536c84..552cdb62 100644 --- a/src/user/user_mesh.cc +++ b/src/user/user_mesh.cc @@ -168,7 +168,6 @@ mjCMesh::mjCMesh(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->mesh.CopyFromSpec(); *this = _def->mesh; } @@ -179,12 +178,70 @@ mjCMesh::mjCMesh(mjCModel* _model, mjCDef* _def) { // in case this body is not compiled CopyFromSpec(); - // point to local (needs to be after defaults) + // point to local PointToLocal(); } +mjCMesh::mjCMesh(const mjCMesh& other) { + *this = other; +} + + + +mjCMesh& mjCMesh::operator=(const mjCMesh& other) { + if (this != &other) { + this->spec = other.spec; + *static_cast(this) = static_cast(other); + *static_cast(this) = static_cast(other); + if (other.vert_) { + size_t nvert = 3*other.nvert_*sizeof(float); + this->vert_ = (float*)mju_malloc(nvert); + memcpy(this->vert_, other.vert_, nvert); + } + if (other.normal_) { + size_t nnormal = 3*other.nnormal_*sizeof(float); + this->normal_ = (float*)mju_malloc(nnormal); + memcpy(this->normal_, other.normal_, nnormal); + } + if (other.center_) { + size_t ncenter = 3*other.nface_*sizeof(double); + this->center_ = (double*)mju_malloc(ncenter); + memcpy(this->center_, other.center_, ncenter); + } + if (other.texcoord_) { + size_t ntexcoord = 2*other.ntexcoord_*sizeof(float); + this->texcoord_ = (float*)mju_malloc(ntexcoord); + memcpy(this->texcoord_, other.texcoord_, ntexcoord); + } + if (other.face_) { + size_t nface = 3*other.nface_*sizeof(int); + this->face_ = (int*)mju_malloc(nface); + memcpy(this->face_, other.face_, nface); + } + if (other.facenormal_) { + size_t nfacenormal = 3*other.nface_*sizeof(int); + this->facenormal_ = (int*)mju_malloc(nfacenormal); + memcpy(this->facenormal_, other.facenormal_, nfacenormal); + } + if (other.facetexcoord_) { + size_t nfacetexcoord = 3*other.nface_*sizeof(int); + this->facetexcoord_ = (int*)mju_malloc(nfacetexcoord); + memcpy(this->facetexcoord_, other.facetexcoord_, nfacetexcoord); + } + if (other.graph_) { + size_t szgraph = szgraph_*sizeof(int); + this->graph_ = (int*)mju_malloc(szgraph); + memcpy(this->graph_, other.graph_, szgraph); + } + } + PointToLocal(); + return *this; +} + + + void mjCMesh::PointToLocal() { spec.element = (mjElement)this; spec.name = (mjString)&name; @@ -1873,7 +1930,7 @@ mjCSkin::mjCSkin(mjCModel* _model) { bodyid.clear(); matid = -1; - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this camera is not compiled @@ -2262,6 +2319,22 @@ mjCFlex::mjCFlex(mjCModel* _model) { } +mjCFlex::mjCFlex(const mjCFlex& other) { + *this = other; +} + + +mjCFlex& mjCFlex::operator=(const mjCFlex& other) { + if (this != &other) { + this->spec = other.spec; + *static_cast(this) = static_cast(other); + *static_cast(this) = static_cast(other); + } + PointToLocal(); + return *this; +} + + void mjCFlex::PointToLocal() { spec.element = (mjElement)this; spec.name = (mjString)&name; diff --git a/src/user/user_objects.cc b/src/user/user_objects.cc index 0def64a1..c3d81935 100644 --- a/src/user/user_objects.cc +++ b/src/user/user_objects.cc @@ -506,6 +506,8 @@ mjCDef::mjCDef(const mjCDef& other) { // compiler void mjCDef::Compile(const mjCModel* model) { + CopyFromSpec(); + // enforce length of all default userdata arrays joint.userdata_.resize(model->nuser_jnt); geom.userdata_.resize(model->nuser_geom); @@ -1425,7 +1427,6 @@ mjCJoint::mjCJoint(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->joint.CopyFromSpec(); *this = _def->joint; } @@ -1433,7 +1434,7 @@ mjCJoint::mjCJoint(mjCModel* _model, mjCDef* _def) { model = _model; def = (_def ? _def : (_model ? _model->defaults[0] : 0)); - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this joint is not compiled @@ -1637,7 +1638,6 @@ mjCGeom::mjCGeom(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->geom.CopyFromSpec(); *this = _def->geom; } @@ -1645,7 +1645,7 @@ mjCGeom::mjCGeom(mjCModel* _model, mjCDef* _def) { model = _model; def = (_def ? _def : (_model ? _model->defaults[0] : 0)); - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this geom is not compiled @@ -2256,11 +2256,10 @@ mjCSite::mjCSite(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->site.CopyFromSpec(); *this = _def->site; } - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this site is not compiled @@ -2416,7 +2415,6 @@ mjCCamera::mjCCamera(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->camera.CopyFromSpec(); *this = _def->camera; } @@ -2424,7 +2422,7 @@ mjCCamera::mjCCamera(mjCModel* _model, mjCDef* _def) { model = _model; def = (_def ? _def : (_model ? _model->defaults[0] : 0)); - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this camera is not compiled @@ -2571,7 +2569,6 @@ mjCLight::mjCLight(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->light.CopyFromSpec(); *this = _def->light; } @@ -2913,7 +2910,7 @@ mjCTexture::mjCTexture(mjCModel* _model) { // clear internal variables rgb = 0; - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this camera is not compiled @@ -3637,7 +3634,6 @@ mjCMaterial::mjCMaterial(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->material.CopyFromSpec(); *this = _def->material; } @@ -3645,7 +3641,7 @@ mjCMaterial::mjCMaterial(mjCModel* _model, mjCDef* _def) { model = _model; def = (_def ? _def : (_model ? _model->defaults[0] : 0)); - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this camera is not compiled @@ -3654,6 +3650,24 @@ mjCMaterial::mjCMaterial(mjCModel* _model, mjCDef* _def) { +mjCMaterial::mjCMaterial(const mjCMaterial& other) { + *this = other; +} + + + +mjCMaterial& mjCMaterial::operator=(const mjCMaterial& other) { + if (this != &other) { + this->spec = other.spec; + *static_cast(this) = static_cast(other); + *static_cast(this) = static_cast(other); + } + PointToLocal(); + return *this; +} + + + void mjCMaterial::PointToLocal() { spec.element = (mjElement)this; spec.name = (mjString)&name; @@ -3696,7 +3710,6 @@ mjCPair::mjCPair(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->pair.CopyFromSpec(); *this = _def->pair; } @@ -3704,7 +3717,7 @@ mjCPair::mjCPair(mjCModel* _model, mjCDef* _def) { model = _model; def = (_def ? _def : (_model ? _model->defaults[0] : 0)); - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this camera is not compiled @@ -3713,6 +3726,26 @@ mjCPair::mjCPair(mjCModel* _model, mjCDef* _def) { +mjCPair::mjCPair(const mjCPair& other) { + *this = other; +} + + + +mjCPair& mjCPair::operator=(const mjCPair& other) { + if (this != &other) { + this->spec = other.spec; + *static_cast(this) = static_cast(other); + *static_cast(this) = static_cast(other); + this->geom1 = other.geom1; + this->geom2 = other.geom2; + } + PointToLocal(); + return *this; +} + + + void mjCPair::PointToLocal() { spec.element = (mjElement)this; spec.name = (mjString)&name; @@ -3956,7 +3989,6 @@ mjCEquality::mjCEquality(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->equality.CopyFromSpec(); *this = _def->equality; } @@ -3964,7 +3996,7 @@ mjCEquality::mjCEquality(mjCModel* _model, mjCDef* _def) { model = _model; def = (_def ? _def : (_model ? _model->defaults[0] : 0)); - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this camera is not compiled @@ -3972,6 +4004,25 @@ mjCEquality::mjCEquality(mjCModel* _model, mjCDef* _def) { } + +mjCEquality::mjCEquality(const mjCEquality& other) { + *this = other; +} + + + +mjCEquality& mjCEquality::operator=(const mjCEquality& other) { + if (this != &other) { + this->spec = other.spec; + *static_cast(this) = static_cast(other); + *static_cast(this) = static_cast(other); + } + PointToLocal(); + return *this; +} + + + void mjCEquality::PointToLocal() { spec.element = (mjElement)this; spec.name = (mjString)&name; @@ -4082,7 +4133,6 @@ mjCTendon::mjCTendon(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->tendon.CopyFromSpec(); *this = _def->tendon; } @@ -4090,7 +4140,7 @@ mjCTendon::mjCTendon(mjCModel* _model, mjCDef* _def) { model = _model; def = (_def ? _def : (_model ? _model->defaults[0] : 0)); - // point to local (needs to be after defaults) + // point to local PointToLocal(); // in case this camera is not compiled @@ -4098,6 +4148,25 @@ mjCTendon::mjCTendon(mjCModel* _model, mjCDef* _def) { } + +mjCTendon::mjCTendon(const mjCTendon& other) { + *this = other; +} + + + +mjCTendon& mjCTendon::operator=(const mjCTendon& other) { + if (this != &other) { + this->spec = other.spec; + *static_cast(this) = static_cast(other); + *static_cast(this) = static_cast(other); + } + PointToLocal(); + return *this; +} + + + bool mjCTendon::is_limited() const { return islimited(limited, range); } @@ -4467,7 +4536,6 @@ mjCActuator::mjCActuator(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - _def->actuator.CopyFromSpec(); *this = _def->actuator; } @@ -4478,12 +4546,30 @@ mjCActuator::mjCActuator(mjCModel* _model, mjCDef* _def) { // in case this actuator is not compiled CopyFromSpec(); - // point to local (needs to be after defaults) + // point to local PointToLocal(); } +mjCActuator::mjCActuator(const mjCActuator& other) { + *this = other; +} + + + +mjCActuator& mjCActuator::operator=(const mjCActuator& other) { + if (this != &other) { + this->spec = other.spec; + *static_cast(this) = static_cast(other); + *static_cast(this) = static_cast(other); + } + PointToLocal(); + return *this; +} + + + bool mjCActuator::is_ctrllimited() const { return islimited(ctrllimited, ctrlrange); } bool mjCActuator::is_forcelimited() const { return islimited(forcelimited, forcerange); } bool mjCActuator::is_actlimited() const { return islimited(actlimited, actrange); } diff --git a/src/user/user_objects.h b/src/user/user_objects.h index 7e9b84df..79ab83cb 100644 --- a/src/user/user_objects.h +++ b/src/user/user_objects.h @@ -660,6 +660,9 @@ class mjCFlex: public mjCFlex_, private mjmFlex { private: mjCFlex(mjCModel* = 0); + mjCFlex(const mjCFlex& other); // copy constructor + mjCFlex& operator=(const mjCFlex& other); // copy assignment + void Compile(const mjVFS* vfs); // compiler void CreateBVH(void); // create flex BVH void CreateShellPair(void); // create shells and evpairs @@ -735,6 +738,8 @@ class mjCMesh: public mjCMesh_, private mjmMesh { friend class mjXWriter; public: mjCMesh(mjCModel* = 0, mjCDef* = 0); + mjCMesh(const mjCMesh& other); // copy constructor + mjCMesh& operator=(const mjCMesh& other); // copy assignment ~mjCMesh(); mjmMesh spec; @@ -890,7 +895,10 @@ class mjCSkin: public mjCSkin_, private mjmSkin { private: mjCSkin(mjCModel* = 0); // constructor + mjCSkin(const mjCSkin& other); // copy constructor + mjCSkin& operator=(const mjCSkin& other); // copy assignment ~mjCSkin(); // destructor + void Compile(const mjVFS* vfs); // compiler void LoadSKN(mjResource* resource); // load skin in SKN BIN format }; @@ -929,8 +937,11 @@ class mjCHField : public mjCHField_, private mjmHField { std::vector& get_userdata() { return userdata_; } private: - mjCHField(mjCModel* model); // constructor - ~mjCHField(); // destructor + mjCHField(mjCModel* model); // constructor + mjCHField(const mjCHField& other); // copy constructor + mjCHField& operator=(const mjCHField& other); // copy assignment + ~mjCHField(); // destructor + float* data; // elevation data, row-major format void Compile(const mjVFS* vfs); // compiler @@ -974,7 +985,10 @@ class mjCTexture : public mjCTexture_, private mjmTexture { std::vector get_cubefiles() const { return cubefiles_; } private: - mjCTexture(mjCModel*); // constructor + mjCTexture(mjCModel*); // constructor + mjCTexture(const mjCTexture& other); // copy constructor + mjCTexture& operator=(const mjCTexture& other); // copy assignment + void Compile(const mjVFS* vfs); // compiler void Builtin2D(void); // make builtin 2D @@ -1027,7 +1041,10 @@ class mjCMaterial : public mjCMaterial_, private mjmMaterial { void del_texture() { texture_.clear(); } private: - mjCMaterial(mjCModel* = 0, mjCDef* = 0); // constructor + mjCMaterial(mjCModel* = 0, mjCDef* = 0); // constructor + mjCMaterial(const mjCMaterial& other); // copy constructor + mjCMaterial& operator=(const mjCMaterial& other); // copy assignment + void Compile(void); // compiler }; @@ -1068,7 +1085,10 @@ class mjCPair : public mjCPair_, private mjmPair { } private: - mjCPair(mjCModel* = 0, mjCDef* = 0); // constructor + mjCPair(mjCModel* = 0, mjCDef* = 0); // constructor + mjCPair(const mjCPair& other); // copy constructor + mjCPair& operator=(const mjCPair& other); // copy assignment + void Compile(void); // compiler mjCGeom* geom1; // geom1 @@ -1112,7 +1132,10 @@ class mjCBodyPair : public mjCBodyPair_, private mjmExclude { } private: - mjCBodyPair(mjCModel*); // constructor + mjCBodyPair(mjCModel*); // constructor + mjCBodyPair(const mjCBodyPair& other); // copy constructor + mjCBodyPair& operator=(const mjCBodyPair& other); // copy assignment + void Compile(void); // compiler }; @@ -1147,7 +1170,10 @@ class mjCEquality : public mjCEquality_, private mjmEquality { void PointToLocal(); private: - mjCEquality(mjCModel* = 0, mjCDef* = 0); // constructor + mjCEquality(mjCModel* = 0, mjCDef* = 0); // constructor + mjCEquality(const mjCEquality& other); // copy constructor + mjCEquality& operator=(const mjCEquality& other); // copy assignment + void Compile(void); // compiler }; @@ -1203,8 +1229,11 @@ class mjCTendon : public mjCTendon_, private mjmTendon { bool is_limited() const; private: - mjCTendon(mjCModel* = 0, mjCDef* = 0); // constructor - ~mjCTendon(); // destructor + mjCTendon(mjCModel* = 0, mjCDef* = 0); // constructor + mjCTendon(const mjCTendon& other); // copy constructor + mjCTendon& operator=(const mjCTendon& other); // copy assignment + ~mjCTendon(); // destructor + void Compile(void); // compiler }; @@ -1233,7 +1262,10 @@ class mjCWrap : public mjCWrap_, private mjmWrap { mjCBase* obj; // wrap object pointer private: - mjCWrap(mjCModel*, mjCTendon*); // constructor + mjCWrap(mjCModel*, mjCTendon*); // constructor + mjCWrap(const mjCWrap& other); // copy constructor + mjCWrap& operator=(const mjCWrap& other); // copy assignment + void Compile(void); // compiler mjCTendon* tendon; // tendon owning this wrap @@ -1263,7 +1295,10 @@ class mjCPlugin : public mjCPlugin_ { mjCBase* parent; // parent object (only used when generating error message) private: - mjCPlugin(mjCModel*); // constructor + mjCPlugin(mjCModel*); // constructor + mjCPlugin(const mjCPlugin& other); // copy constructor + mjCPlugin& operator=(const mjCPlugin& other); // copy assignment + void Compile(void); // compiler }; @@ -1311,7 +1346,10 @@ class mjCActuator : public mjCActuator_, private mjmActuator { bool is_actlimited() const; private: - mjCActuator(mjCModel* = 0, mjCDef* = 0); // constructor + mjCActuator(mjCModel* = 0, mjCDef* = 0); // constructor + mjCActuator(const mjCActuator& other); // copy constructor + mjCActuator& operator=(const mjCActuator& other); // copy assignment + void Compile(void); // compiler void CopyFromSpec(); void PointToLocal(); @@ -1354,7 +1392,10 @@ class mjCSensor : public mjCSensor_, private mjmSensor { const std::string& get_refname() { return spec_refname_; } private: - mjCSensor(mjCModel*); // constructor + mjCSensor(mjCModel*); // constructor + mjCSensor(const mjCSensor& other); // copy constructor + mjCSensor& operator=(const mjCSensor& other); // copy assignment + void Compile(void); // compiler void CopyFromSpec(); void MakePointerLocal(); @@ -1386,8 +1427,11 @@ class mjCNumeric : public mjCNumeric_, private mjmNumeric { void CopyFromSpec(); private: - mjCNumeric(mjCModel*); // constructor - ~mjCNumeric(); // destructor + mjCNumeric(mjCModel*); // constructor + mjCNumeric(const mjCNumeric& other); // copy constructor + mjCNumeric& operator=(const mjCNumeric& other); // copy assignment + ~mjCNumeric(); // destructor + void Compile(void); // compiler }; @@ -1415,8 +1459,11 @@ class mjCText : public mjCText_, private mjmText { void CopyFromSpec(); private: - mjCText(mjCModel*); // constructor - ~mjCText(); // destructor + mjCText(mjCModel*); // constructor + mjCText(const mjCText& other); // copy constructor + mjCText& operator=(const mjCText& other); // copy assignment + ~mjCText(); // destructor + void Compile(void); // compiler }; @@ -1449,8 +1496,11 @@ class mjCTuple : public mjCTuple_, private mjmTuple { void CopyFromSpec(); private: - mjCTuple(mjCModel*); // constructor - ~mjCTuple(); // destructor + mjCTuple(mjCModel*); // constructor + mjCTuple(const mjCTuple& other); // copy constructor + mjCTuple& operator=(const mjCTuple& other); // copy assignment + ~mjCTuple(); // destructor + void Compile(void); // compiler }; @@ -1488,8 +1538,11 @@ class mjCKey : public mjCKey_, private mjmKey { void CopyFromSpec(); private: - mjCKey(mjCModel*); // constructor - ~mjCKey(); // destructor + mjCKey(mjCModel*); // constructor + mjCKey(const mjCKey& other); // copy constructor + mjCKey& operator=(const mjCKey& other); // copy assignment + ~mjCKey(); // destructor + void Compile(const mjModel* m); // compiler };