Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for ACInfoModel #551

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions assets/ACModel/test.acim
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?xml version="1.0"?>
<acim version="0.1.0">
<timber id="24c4fb33-67fa-4c27-b42c-c0dc7ca5cd91">
<executed>NotDone</executed>
<state>NotDone</state>
<current>Cut#1</current>
<executed>NotDone</executed>
<bbox>
<corner id="0">-4.4408920985e-16 -1.33226762955e-15 -8.07965638975e-07</corner>
<corner id="1">1.85073895234 -1.33226762955e-15 -8.07965638975e-07</corner>
Expand Down
3 changes: 3 additions & 0 deletions src/AIAC/ACInfoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ namespace AIAC
///< Base Component
void TimberInfo::Component::SetAsCurrent() {
m_State = ACIMState::CURRENT;

#ifndef HEADLESS_TEST
AIAC_APP.GetLayer<LayerModel>()->GetACInfoModel().GetDoc().child("acim").child("timber").child("current").last_child().set_value(m_ID.c_str());
AIAC_APP.GetLayer<LayerModel>()->GetACInfoModel().Save();

AIAC_APP.GetRenderer()->SetGlobalViewToActivatedComponent(Renderer::StandardView::TOP);
#endif
}

void TimberInfo::Component::SetAsDone() {
Expand Down
10 changes: 7 additions & 3 deletions src/AIAC/ACInfoModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class TimberInfo{
class Component {
public:
Component(std::string type) : m_Type(type) {
#ifndef HEADLESS_TEST
m_Scale = AIAC::Config::Get<float>(AIAC::Config::SEC_AIAC, AIAC::Config::SCALE_FACTOR, 1.0f);
#endif
}
virtual void SetAsCurrent();
virtual void SetAsDone();
Expand Down Expand Up @@ -235,8 +237,8 @@ class TimberInfo{
};

inline bool IsSingleFace() const { return m_NonExposedFaceIDs.size() == 1; }
inline Face& GetFace(std::string id) { return m_Faces[id]; }
inline Edge& GetEdge(std::string id) { return m_Edges[id]; }
inline Face& GetFace(std::string id) { if(m_Faces.count(id) == 0) throw std::invalid_argument("Face ID does not exist."); return m_Faces[id]; }
inline Edge& GetEdge(std::string id) { if(m_Edges.count(id) == 0) throw std::invalid_argument("Edge ID does not exist."); return m_Edges[id]; }
inline std::map<std::string, Face>& GetAllFaces() { return m_Faces; }
inline std::map<std::string, Edge>& GetAllEdges() { return m_Edges; }
inline std::set<std::string>& GetAllNonExposedFaceIDs() { return m_NonExposedFaceIDs; }
Expand Down Expand Up @@ -358,7 +360,9 @@ class ACInfoModel
{
public:
ACInfoModel(){
#ifndef HEADLESS_TEST
m_Scale = AIAC::Config::Get<float>(AIAC::Config::SEC_AIAC, AIAC::Config::SCALE_FACTOR, 1.0f);
#endif
};
~ACInfoModel(){};

Expand Down Expand Up @@ -454,7 +458,7 @@ class ACInfoModel
float m_EdgeWeight = 1.1f;
float m_LabelSize = 0.75f;

float m_Scale;
float m_Scale = 1.0f;
float m_MeasuredBboxLength;

std::string m_FilePath;
Expand Down
2 changes: 1 addition & 1 deletion src/AIAC/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace AIAC
};
}

#ifdef SILENT_LOGGING
#if (defined SILENT_LOGGING && !defined HEADLESS_TEST)
#define AIAC_INFO(...) SPDLOG_LOGGER_INFO(AIAC::Log::GetLogger(), __VA_ARGS__)
#define AIAC_WARN(...) SPDLOG_LOGGER_WARN(AIAC::Log::GetLogger(), __VA_ARGS__)
#define AIAC_ERROR(...) SPDLOG_LOGGER_ERROR(AIAC::Log::GetLogger(), __VA_ARGS__)
Expand Down
87 changes: 87 additions & 0 deletions tests/unit_tests/InfoModelTest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <gtest/gtest.h>
#include "AIAC/ACInfoModel.h"

#include <glm/gtx/string_cast.hpp>
#include <set>

auto const MAX_ABS_ERROR = 1e-3f;

class InfoModelTest : public ::testing::Test {
protected:
void SetUp() override { }

void TearDown() override {}
};

TEST_F(InfoModelTest, FileIO) {
AIAC::ACInfoModel acim;

std::string path = "../assets/ACModel/test.acim";
acim.Load(path);

EXPECT_EQ(acim.GetFilePath(), path);
EXPECT_EQ(acim.GetName(), "test");
}

TEST_F(InfoModelTest, ContentParsing) {
AIAC::ACInfoModel acim;

std::string path = "../assets/ACModel/test.acim";
acim.Load(path);

auto& timberInfo = acim.GetTimberInfo();

// bounding box
auto bboxPts = timberInfo.GetBoundingBox();
EXPECT_EQ(bboxPts.size(), 8);

ASSERT_TRUE(glm::distance(glm::vec3(0, 0, -8.07965638975e-07f), bboxPts[0]) < MAX_ABS_ERROR);
ASSERT_TRUE(glm::distance(glm::vec3(1.85073895234f, 0, 0.130034050856f), bboxPts[5]) < MAX_ABS_ERROR);
ASSERT_TRUE(glm::distance(glm::vec3(0, 0.129643784514f, 0.130034050856f), bboxPts[7]) < MAX_ABS_ERROR);

// components
std::vector<std::string> allComponentsIDs = timberInfo.GetAllComponentsIDs();
std::unordered_set<std::string> allComponentsIDsSet(allComponentsIDs.begin(), allComponentsIDs.end());

std::vector<std::string> expectedIDs = {
"Hole#1", "Hole#2", "Hole#3", "Hole#4", "Hole#5", "Hole#6", "Hole#7", "Hole#8", "Hole#9", "Hole#10",
"Cut#1", "Cut#2", "Cut#3", "Cut#4", "Cut#5", "Cut#6", "Cut#7"
};

ASSERT_EQ(allComponentsIDsSet.size(), expectedIDs.size());
for (auto& id : expectedIDs) {
ASSERT_NE(allComponentsIDsSet.find(id), allComponentsIDsSet.end());
}

// holes
auto hole1 = static_cast<AIAC::TimberInfo::Hole*>(timberInfo.GetComponent("Hole#1"));
ASSERT_EQ(hole1->GetRadius(), 0.025f);
ASSERT_EQ(hole1->GetStartPointGO()->GetPosition(), glm::vec3(0.171164716736f, 0.0874013529763f, 0.130030809657f));
ASSERT_EQ(hole1->GetEndPointGO()->GetPosition(), glm::vec3(0.171164716736f, 0.0874013529763f, 6.34902733979e-16f));
ASSERT_EQ(hole1->GetCenter(), glm::vec3(0.171164716736f, 0.0874013529763f, 0.0650154048285f));

// cuts
auto cut1 = static_cast<AIAC::TimberInfo::Cut*>(timberInfo.GetComponent("Cut#1"));
ASSERT_EQ(cut1->GetCenter(), glm::vec3(0.543917917169f, 0.064821892257f, 0.10202644336f));
ASSERT_EQ(cut1->GetAllFaces().size(), 3); // 3 unexposed faces
ASSERT_EQ(cut1->GetAllEdges().size(), 12);

auto face1 = cut1->GetFace("1");
auto edges = face1.GetEdges();
ASSERT_EQ(edges.size(), 4);
auto edgeIdSet = std::unordered_set<std::string>(edges.begin(), edges.end());
for(auto& edgeId : {"0", "2", "5", "7"}) {
ASSERT_NE(edgeIdSet.find(edgeId), edgeIdSet.end());
}

auto corners = face1.GetCorners();
ASSERT_EQ(corners.size(), 4);
ASSERT_TRUE(glm::distance(corners[0], glm::vec3(0.650908f, 0.129637f, 0.074019f)) < MAX_ABS_ERROR);
ASSERT_TRUE(glm::distance(corners[1], glm::vec3(0.583188f, 0.000000f, 0.074019f)) < MAX_ABS_ERROR);
ASSERT_TRUE(glm::distance(corners[2], glm::vec3(0.583188f, -0.000000f, 0.130034f)) < MAX_ABS_ERROR);
ASSERT_TRUE(glm::distance(corners[3], glm::vec3(0.650908f, 0.129644f, 0.130034f)) < MAX_ABS_ERROR);

auto edge0 = cut1->GetEdge("0");
ASSERT_EQ(edge0.GetStartPt().GetPosition(), glm::vec3(0.583187494871f, -1.33226762955e-15f, 0.130034050856f));
ASSERT_EQ(edge0.GetEndPt().GetPosition(), glm::vec3(0.583187494871f, 6.66133814775e-16f, 0.0740188358647f));
}
Loading