Skip to content

Commit

Permalink
Return reference to Node object from GetNode() as opposed to a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
faisal-bhuiyan committed Jul 30, 2024
1 parent 80fb2de commit 7e74bf8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/restruct_poc/model/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class Model {
}

/// Return a node by ID - const/read-only version
std::shared_ptr<const Node> 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<Node> 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<std::shared_ptr<Node>>& GetNodes() const { return this->nodes_; }
Expand Down
36 changes: 18 additions & 18 deletions tests/unit_tests/restruct_poc/model/test_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 7e74bf8

Please sign in to comment.