Commit 6e9f2939 authored by Nicolas Carion's avatar Nicolas Carion

[Timeline2][Model] Implement snappoint ignoring + tests

parent 23e9c4d3
......@@ -66,3 +66,24 @@ int SnapModel::getClosestPoint(int position)
return (int)next;
}
void SnapModel::ignore(const std::vector<int>& pts)
{
for (int pt : pts) {
auto it = m_snaps.find(pt);
if (it != m_snaps.end()) {
m_ignore.push_back(*it);
m_snaps.erase(it);
}
}
}
void SnapModel::unIgnore()
{
for (const auto& pt : m_ignore) {
if (m_snaps.count(pt.first) == 0) {
m_snaps[pt.first] = 0;
}
m_snaps[pt.first] += pt.second;
}
m_ignore.clear();
}
......@@ -23,6 +23,7 @@
#define SNAPMODEL_H
#include <map>
#include <vector>
/** @brief This class represents the snap points of the timeline.
Basically, one can add or remove snap points, and query the closest snap point to a given location
......@@ -43,8 +44,21 @@ public:
/* @brief Retrieves closest point. Returns -1 if there is no snappoint available */
int getClosestPoint(int position);
/* @brief Ignores the given positions until unIgnore() is called
You can make several call to this before unIgnoring
Note that you cannot remove ignored points.
@param points list of point to ignore
*/
void ignore(const std::vector<int>& pts);
/* @brief Revert ignoring
*/
void unIgnore();
private:
std::map<int, int> m_snaps; //This represents the snappoints internally. The keys are the positions and the values are the number of elements at this position. Note that it is important that the datastructure is ordered. QMap is NOT ordered, and therefore not suitable.
std::vector<std::pair<int, int> > m_ignore;
};
#endif
......@@ -7,65 +7,127 @@ TEST_CASE("Snap points model test", "[SnapModel]")
{
SnapModel snap;
// empty
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
snap.addPoint(10);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(999) == 10);
snap.addPoint(10);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(999) == 10);
snap.addPoint(15);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(12) == 10);
REQUIRE(snap.getClosestPoint(13) == 15);
REQUIRE(snap.getClosestPoint(15) == 15);
REQUIRE(snap.getClosestPoint(16) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
snap.removePoint(10);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(12) == 10);
REQUIRE(snap.getClosestPoint(13) == 15);
REQUIRE(snap.getClosestPoint(15) == 15);
REQUIRE(snap.getClosestPoint(16) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
snap.removePoint(10);
REQUIRE(snap.getClosestPoint(0) == 15);
REQUIRE(snap.getClosestPoint(10) == 15);
REQUIRE(snap.getClosestPoint(11) == 15);
REQUIRE(snap.getClosestPoint(9) == 15);
REQUIRE(snap.getClosestPoint(12) == 15);
REQUIRE(snap.getClosestPoint(13) == 15);
REQUIRE(snap.getClosestPoint(15) == 15);
REQUIRE(snap.getClosestPoint(16) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
snap.removePoint(15);
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
REQUIRE(snap.getClosestPoint(11) == -1);
REQUIRE(snap.getClosestPoint(9) == -1);
REQUIRE(snap.getClosestPoint(12) == -1);
REQUIRE(snap.getClosestPoint(13) == -1);
REQUIRE(snap.getClosestPoint(15) == -1);
REQUIRE(snap.getClosestPoint(16) == -1);
REQUIRE(snap.getClosestPoint(999) == -1);
SECTION("Basic test") {
// empty
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
snap.addPoint(10);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(999) == 10);
snap.addPoint(10);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(999) == 10);
snap.addPoint(15);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(12) == 10);
REQUIRE(snap.getClosestPoint(13) == 15);
REQUIRE(snap.getClosestPoint(15) == 15);
REQUIRE(snap.getClosestPoint(16) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
snap.removePoint(10);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(12) == 10);
REQUIRE(snap.getClosestPoint(13) == 15);
REQUIRE(snap.getClosestPoint(15) == 15);
REQUIRE(snap.getClosestPoint(16) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
snap.removePoint(10);
REQUIRE(snap.getClosestPoint(0) == 15);
REQUIRE(snap.getClosestPoint(10) == 15);
REQUIRE(snap.getClosestPoint(11) == 15);
REQUIRE(snap.getClosestPoint(9) == 15);
REQUIRE(snap.getClosestPoint(12) == 15);
REQUIRE(snap.getClosestPoint(13) == 15);
REQUIRE(snap.getClosestPoint(15) == 15);
REQUIRE(snap.getClosestPoint(16) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
snap.removePoint(15);
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
REQUIRE(snap.getClosestPoint(11) == -1);
REQUIRE(snap.getClosestPoint(9) == -1);
REQUIRE(snap.getClosestPoint(12) == -1);
REQUIRE(snap.getClosestPoint(13) == -1);
REQUIRE(snap.getClosestPoint(15) == -1);
REQUIRE(snap.getClosestPoint(16) == -1);
REQUIRE(snap.getClosestPoint(999) == -1);
}
SECTION("Snappoint Ignoring") {
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
snap.addPoint(10);
auto state = [&]() {
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(999) == 10);
};
state();
snap.ignore({0});
state();
snap.ignore({10});
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
snap.unIgnore();
state();
snap.addPoint(10);
state();
snap.ignore({10});
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
snap.unIgnore();
state();
snap.addPoint(15);
REQUIRE(snap.getClosestPoint(0) == 10);
REQUIRE(snap.getClosestPoint(10) == 10);
REQUIRE(snap.getClosestPoint(11) == 10);
REQUIRE(snap.getClosestPoint(9) == 10);
REQUIRE(snap.getClosestPoint(12) == 10);
REQUIRE(snap.getClosestPoint(13) == 15);
REQUIRE(snap.getClosestPoint(15) == 15);
REQUIRE(snap.getClosestPoint(16) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
snap.ignore({15});
state();
snap.removePoint(10);
state();
snap.removePoint(10);
REQUIRE(snap.getClosestPoint(0) == -1);
REQUIRE(snap.getClosestPoint(10) == -1);
snap.unIgnore();
REQUIRE(snap.getClosestPoint(0) == 15);
REQUIRE(snap.getClosestPoint(10) == 15);
REQUIRE(snap.getClosestPoint(11) == 15);
REQUIRE(snap.getClosestPoint(9) == 15);
REQUIRE(snap.getClosestPoint(999) == 15);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment