forked from bardia16/CPS_Android_HW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
movementdatabase.cpp
145 lines (129 loc) · 4.33 KB
/
movementdatabase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "movementdatabase.h"
#include <cmath>
#include <QDebug>
#define min_distance 0.2
MovementDatabase::MovementDatabase(QObject *parent)
: QObject(parent), currentMovement(new Movement(this))
{
m_movements.append(currentMovement);
}
void MovementDatabase::handleNewAcceleration(double x, double y, double velocityX, double velocityY, double xBias, double yBias)
{
if (std::abs(x) <= min_acceleration && std::abs(y) <= min_acceleration && (std::abs(velocityX) <= min_velocity && std::abs(velocityY) <= min_velocity))
{
if(currentMovement->calculateDistanceTraveled() >= min_distance)
{
createNewMovement();
qDebug() << "Creating new movements";
}
else if(currentMovement->calculateDistanceTraveled() < min_distance && currentMovement->calculateDistanceTraveled() > 0.0)
{
currentMovement->accelerations.clear();
currentMovement->angleChanges.clear(); // no need to store because the movement was rotation
qDebug() << "Movement cleared";
}
}
if (std::abs(x) >= min_acceleration || std::abs(y) >= min_acceleration) {
currentMovement->addAcceleration(x, y);
}
//qDebug() << m_movements;
}
void MovementDatabase::handleNewAngle(double alpha)
{
currentMovement->addAngleChange(alpha);
emit angleUpdated(currentMovement->getCurrentAngle());
}
void MovementDatabase::reset()
{
currentMovement = new Movement(this);
m_movements.clear();
m_movements.append(currentMovement);
//emit movementsUpdated(0.0, 0.0, 0.0, "Reset");
qDebug() << "MovementDatabase reset.";
}
void MovementDatabase::createNewMovement()
{
QVector3D lastPosition;
qreal lastAngle;
Movement* newMovement = new Movement(this);
Movement* lastMovement = m_movements.last();
newMovement->setStartPosition(0.0, 0.0);
newMovement->setStartAngle(0.0);
lastPosition = QVector3D(0.0, 0.0, 0.0);
lastAngle = 0.0;
if (!m_movements.isEmpty()) {
lastMovement->findDirection();
lastPosition = lastMovement->getCurrentPosition();
lastAngle = lastMovement->getCurrentAngle();
newMovement->setStartPosition(lastPosition.x(), lastPosition.y());
newMovement->setStartAngle(lastAngle);
}
emit movementsUpdated(lastPosition.x(), lastPosition.y(), lastAngle, lastMovement->getDirection());
m_movements.append(newMovement);
currentMovement = newMovement;
}
/*QString MovementDatabase::getDirection()
{
qreal distanceX = currentMovement->calculateDistanceTraveledX();
qreal distanceY = currentMovement->calculateDistanceTraveledY();
bool main_direction_is_X = std::abs(distanceX) > std::abs(distanceY);
qreal angle = currentMovement->getCurrentAngle();
QString direction = "Not assigned";
//logic
if(main_direction_is_X && distanceX > 0)
{
if(angle == 0)
direction = "Right";
else if(angle == 90)
direction = "Up";
else if(angle == -90)
direction = "Bottom";
else if(angle == 180)
direction = "Left";
}
else if(main_direction_is_X && distanceX < 0)
{
if(angle == 0)
direction = "Left";
else if(angle == 90)
direction = "Bottom";
else if(angle == -90)
direction = "Up";
else if(angle == 180)
direction = "Right";
}
if(!main_direction_is_X && distanceY > 0)
{
if(angle == 0)
direction = "Up";
else if(angle == 90)
direction = "Left";
else if(angle == -90)
direction = "Right";
else if(angle == 180)
direction = "Down";
}
else if(!main_direction_is_X && distanceY < 0)
{
if(angle == 0)
direction = "Down";
else if(angle == 90)
direction = "Right";
else if(angle == -90)
direction = "Left";
else if(angle == 180)
direction = "Up";
}
return direction;
}*/
void MovementDatabase::createNewPattern(bool isAttempt)
{
// Create a new Pattern instance with the current list of movements
Pattern* pattern = new Pattern(m_movements, this);
if(!isAttempt)
emit newPattern(pattern);
else
emit newAttempt(pattern);
// Optionally, clear the current list of movements if needed
reset();
}