From 7e74bf833457af31562124cb34cacaa99a192298 Mon Sep 17 00:00:00 2001 From: faisal-bhuiyan Date: Tue, 30 Jul 2024 15:11:55 -0600 Subject: [PATCH] Return reference to Node object from GetNode() as opposed to a pointer --- src/restruct_poc/model/model.hpp | 4 +-- .../restruct_poc/model/test_model.cpp | 36 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/restruct_poc/model/model.hpp b/src/restruct_poc/model/model.hpp index c36453c9..71656e3a 100644 --- a/src/restruct_poc/model/model.hpp +++ b/src/restruct_poc/model/model.hpp @@ -60,10 +60,10 @@ class Model { } /// Return a node by ID - const/read-only version - std::shared_ptr GetNode(int id) const { return this->nodes_[id]; } + const Node& GetNode(int id) const { return *this->nodes_[id]; } /// Return a node by ID - non-const version - std::shared_ptr GetNode(int id) { return this->nodes_[id]; } + Node& GetNode(int id) { return *this->nodes_[id]; } /// Returns a reference to the nodes in the model (as vector of shared pointers) const std::vector>& GetNodes() const { return this->nodes_; } diff --git a/tests/unit_tests/restruct_poc/model/test_model.cpp b/tests/unit_tests/restruct_poc/model/test_model.cpp index 6f5197eb..19c8d03b 100644 --- a/tests/unit_tests/restruct_poc/model/test_model.cpp +++ b/tests/unit_tests/restruct_poc/model/test_model.cpp @@ -55,17 +55,17 @@ TEST_F(ModelFixture, TranslateModelNode) { // Get the node and check the position auto node_0 = model.GetNode(0); - ASSERT_EQ(node_0->ID, 0); - ASSERT_EQ(node_0->x[0], pos[0]); // 0. - ASSERT_EQ(node_0->x[1], pos[1]); // 0. - ASSERT_EQ(node_0->x[2], pos[2]); // 0. + ASSERT_EQ(node_0.ID, 0); + ASSERT_EQ(node_0.x[0], pos[0]); // 0. + ASSERT_EQ(node_0.x[1], pos[1]); // 0. + ASSERT_EQ(node_0.x[2], pos[2]); // 0. // Now translate the node and check the new position Array_3 displacement = {1., 2., 3.}; - node_0->Translate(displacement); - ASSERT_EQ(node_0->x[0], pos[0] + displacement[0]); // 1. - ASSERT_EQ(node_0->x[1], pos[1] + displacement[1]); // 2. - ASSERT_EQ(node_0->x[2], pos[2] + displacement[2]); // 3. + node_0.Translate(displacement); + ASSERT_EQ(node_0.x[0], pos[0] + displacement[0]); // 1. + ASSERT_EQ(node_0.x[1], pos[1] + displacement[1]); // 2. + ASSERT_EQ(node_0.x[2], pos[2] + displacement[2]); // 3. } TEST_F(ModelFixture, RotateModelNode) { @@ -78,22 +78,22 @@ TEST_F(ModelFixture, RotateModelNode) { // Translate the node to {1., 0., 0.} auto node_0 = model.GetNode(0); - node_0->Translate({1., 0., 0.}); + node_0.Translate({1., 0., 0.}); // Now rotate the node 90 degrees around the z-axis - node_0->Rotate({0., 0., 1.}, M_PI / 2.); - ASSERT_NEAR(node_0->x[0], 0., 1e-12); - ASSERT_NEAR(node_0->x[1], 1., 1e-12); - ASSERT_NEAR(node_0->x[2], 0., 1e-12); + node_0.Rotate({0., 0., 1.}, M_PI / 2.); + ASSERT_NEAR(node_0.x[0], 0., 1e-12); + ASSERT_NEAR(node_0.x[1], 1., 1e-12); + ASSERT_NEAR(node_0.x[2], 0., 1e-12); // Return the node to {1., 0., 0.} - node_0->Translate({1., -1., 0.}); + node_0.Translate({1., -1., 0.}); // Now rotate the node 45 degrees around the z-axis using a quaternion - node_0->Rotate({0.92388, 0., 0., 0.382683}); - ASSERT_NEAR(node_0->x[0], 0.707107, 1e-6); - ASSERT_NEAR(node_0->x[1], 0.707107, 1e-6); - ASSERT_NEAR(node_0->x[2], 0., 1e-6); + node_0.Rotate({0.92388, 0., 0., 0.382683}); + ASSERT_NEAR(node_0.x[0], 0.707107, 1e-6); + ASSERT_NEAR(node_0.x[1], 0.707107, 1e-6); + ASSERT_NEAR(node_0.x[2], 0., 1e-6); } TEST_F(ModelFixture, AddBeamElementToModel) {