-
Notifications
You must be signed in to change notification settings - Fork 3
/
WeArtHapticObject.cpp
133 lines (116 loc) · 3.72 KB
/
WeArtHapticObject.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
/**
* WEART - HapticObject component
* https://www.weart.it/
*/
#include "WeArtHapticObject.h"
#include "WeArtController.h"
#include <algorithm>
WeArtHapticObject::WeArtHapticObject(WeArtClient* client) {
weArtclient = client;
}
void WeArtHapticObject::AddEffect(WeArtEffect* effect) {
if (!ContainsEffect(effect)) {
activeEffects.push_back(effect);
UpdateEffects();
}
}
void WeArtHapticObject::RemoveEffect(WeArtEffect* effect) {
if (ContainsEffect(effect)) {
activeEffects.erase(std::remove(activeEffects.begin(), activeEffects.end(), effect), activeEffects.end());
UpdateEffects();
}
}
bool WeArtHapticObject::ContainsEffect(WeArtEffect* effect) {
return std::find(activeEffects.begin(), activeEffects.end(), effect) != activeEffects.end();
}
void WeArtHapticObject::UpdateEffects(void) {
if (activeEffects.empty()) {
weArtForce.value(WeArtConstants::defaultForce);
weArtForce.active = false;
weArtTemperature.value(WeArtConstants::defaultTemperature);
weArtTemperature.active = false;
weArtTexture.textureType((TextureType)WeArtConstants::defaultTextureIndex);
weArtTexture.textureVelocity(WeArtTexture::DefaultVelocity);
weArtTexture.active = false;
StopTemperatureMessage msg1;
SendMessage(&msg1);
StopForceMessage msg2;
SendMessage(&msg2);
StopTextureMessage msg3;
SendMessage(&msg3);
} else {
// Update temperature and send relevant messages
WeArtEffect* tempEffect = nullptr;
for (std::vector<WeArtEffect*>::reverse_iterator i = activeEffects.rbegin(); i != activeEffects.rend(); ++i) {
if ((*i)->getTemperature().active) {
tempEffect = *i;
break;
}
}
WeArtTemperature newTemp = tempEffect != nullptr ? tempEffect->getTemperature() : WeArtTemperature();
if (!(newTemp == weArtTemperature)) {
if (!newTemp.active) {
StopTemperatureMessage msg;
SendMessage(&msg);
} else {
SetTemperatureMessage msg(newTemp.value());
SendMessage(&msg);
}
weArtTemperature = newTemp;
}
// Update force and set relevant messages
WeArtEffect* forceEffect = nullptr;
for (std::vector<WeArtEffect*>::reverse_iterator i = activeEffects.rbegin(); i != activeEffects.rend(); ++i) {
if ((*i)->getForce().active) {
forceEffect = *i;
break;
}
}
WeArtForce newForce = forceEffect != nullptr ? forceEffect->getForce() : WeArtForce();
if (!(newForce == weArtForce)) {
if (!newForce.active) {
StopForceMessage msg;
SendMessage(&msg);
} else {
const float fValue[3] = { newForce.value(), 0.0, 0.0 };
SetForceMessage msg(fValue);
SendMessage(&msg);
}
weArtForce = newForce;
}
// Update texture and set relevant messages
WeArtEffect* texEffect = nullptr;
for (std::vector<WeArtEffect*>::reverse_iterator i = activeEffects.rbegin(); i != activeEffects.rend(); ++i) {
if ((*i)->getTexture().active) {
texEffect = *i;
break;
}
}
WeArtTexture newTex = texEffect != nullptr ? texEffect->getTexture() : WeArtTexture();
if (!(newTex == weArtTexture)) {
if (!newTex.active) {
StopTextureMessage msg;
SendMessage(&msg);
} else {
SetTextureMessage msg((int)newTex.textureType(), newTex.textureVelocity(), newTex.volume());
SendMessage(&msg);
}
weArtTexture = newTex;
}
}
}
void WeArtHapticObject::SendMessage(WeArtMessage* msg) {
const HandSide hand_sides[] = { HandSide::Left, HandSide::Right };
const ActuationPoint actuation_points[] = { ActuationPoint::Thumb, ActuationPoint::Index, ActuationPoint::Middle, ActuationPoint::Palm };
for (HandSide hs : hand_sides) {
if ((uint32_t)hs & handSideFlag) {
for (ActuationPoint ap : actuation_points) {
if ((uint32_t)ap & actuationPointFlag) {
msg->setHandSide(hs);
msg->setActuationPoint(ap);
weArtclient->SendMessage(msg);
}
}
}
}
}