Skip to content

Commit

Permalink
#143: Fix issues with move paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ziggi committed Oct 7, 2018
1 parent 6cd7906 commit 4e5c74d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 63 deletions.
76 changes: 17 additions & 59 deletions src/CMovePath.cpp
Expand Up @@ -15,26 +15,14 @@ int CMovePath::Create()
int id = 0;

// find free path id
for (auto const &value : m_vPathID) {
if (value != id) {
for (auto const &value : m_mMovePath) {
if (value.first != id) {
break;
}
id++;
}

try {
if (static_cast<size_t>(id) == m_vPath.size()) {
m_vPath.push_back(std::vector<CVector>());
m_vPointsID.push_back(std::vector<int>());
m_vPathID.push_back(id);
} else {
m_vPath.insert(m_vPath.begin() + id, std::vector<CVector>());
m_vPointsID.insert(m_vPointsID.begin() + id, std::vector<int>());
m_vPathID.insert(m_vPathID.begin() + id, id);
}
} catch (...) {
return INVALID_MOVEPATH_ID;
}
m_mMovePath.insert({ id, std::map<int, CVector>()});
return id;
}

Expand All @@ -43,30 +31,19 @@ bool CMovePath::Destroy(int iPathId)
if (!IsPathValid(iPathId)) {
return false;
}
m_vPath.erase(m_vPath.begin() + iPathId);
m_vPointsID.erase(m_vPointsID.begin() + iPathId);
m_vPathID.erase(m_vPathID.begin() + iPathId);

m_mMovePath.erase(iPathId);
return true;
}

bool CMovePath::IsPathValid(int iPathId)
{
// is not in range
if (m_vPathID.size() == 0) {
return false;
}

if (iPathId < 0 || iPathId > m_vPathID.back()) {
return false;
}

// try to find the path
return std::find(m_vPathID.begin(), m_vPathID.end(), iPathId) != m_vPathID.end();
return m_mMovePath.count(iPathId) > 0;
}

std::vector<CVector> *CMovePath::GetPoints(int iPathId)
std::map<int, CVector> *CMovePath::GetPoints(int iPathId)
{
return &m_vPath[iPathId];
return &m_mMovePath[iPathId];
}

int CMovePath::AddPoint(int iPathId, const CVector &vecPoint)
Expand All @@ -77,25 +54,15 @@ int CMovePath::AddPoint(int iPathId, const CVector &vecPoint)

int id = 0;

// find free point id
for (auto const &value : m_vPointsID[iPathId]) {
if (value != id) {
// find free path id
for (auto const &value : m_mMovePath[iPathId]) {
if (value.first != id) {
break;
}
id++;
}

try {
if (static_cast<size_t>(id) == m_vPath.size()) {
m_vPath[iPathId].push_back(vecPoint);
m_vPointsID[iPathId].push_back(id);
} else {
m_vPath[iPathId].insert(m_vPath[iPathId].begin() + id, vecPoint);
m_vPointsID[iPathId].insert(m_vPointsID[iPathId].begin() + id, id);
}
} catch (...) {
return -1;
}
m_mMovePath[iPathId].insert({ id, vecPoint });
return id;
}

Expand All @@ -104,8 +71,8 @@ bool CMovePath::RemovePoint(int iPathId, int iPointId)
if (IsPointValid(iPathId, iPointId)) {
return false;
}
m_vPath[iPathId].erase(m_vPath[iPathId].begin() + iPointId);
m_vPointsID[iPathId].erase(m_vPointsID[iPathId].begin() + iPointId);

m_mMovePath[iPathId].erase(iPointId);
return true;
}

Expand All @@ -115,23 +82,14 @@ bool CMovePath::IsPointValid(int iPathId, int iPointId)
return false;
}

// is not in range
if (m_vPointsID[iPathId].size() == 0) {
return false;
}

if (iPointId < 0 || iPointId > m_vPointsID[iPathId].back()) {
return false;
}

// try to find the path point
return std::find(m_vPointsID[iPathId].begin(), m_vPointsID[iPathId].end(), iPathId) != m_vPointsID[iPathId].end();
return m_mMovePath[iPathId].count(iPointId) > 0;
}

CVector *CMovePath::GetPoint(int iPathId, int iPointId)
{
if (!IsPointValid(iPathId, iPointId)) {
return NULL;
}
return &m_vPath[iPathId].at(iPointId);

return &m_mMovePath[iPathId].at(iPointId);
}
6 changes: 2 additions & 4 deletions src/CMovePath.hpp
Expand Up @@ -17,17 +17,15 @@ class CMovePath
int Create();
bool Destroy(int iPathId);
bool IsPathValid(int iPathId);
std::vector<CVector> *GetPoints(int iPathId);
std::map<int, CVector> *GetPoints(int iPathId);

int AddPoint(int iPathId, const CVector &vecPoint);
bool RemovePoint(int iPathId, int iPointId);
bool IsPointValid(int iPathId, int iPointId);
CVector *GetPoint(int iPathId, int iPointId);

private:
std::vector<std::vector<CVector>> m_vPath;
std::vector<std::vector<int>> m_vPointsID;
std::vector<int> m_vPathID;
std::map<int, std::map<int, CVector>> m_mMovePath;
};

#endif

0 comments on commit 4e5c74d

Please sign in to comment.