Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.cpp
More file actions
Latest commit
161 lines (139 loc) · 5.07 KB
/
Copy pathExample.cpp
File metadata and controls
161 lines (139 loc) · 5.07 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include"../include/AugmentaClientSDK.hpp"
#include<vector>
#include<array>
#include<iostream>
// Sample data types. Replace with your own types !
using Vector3f = std::array<float, 3>;
using PointCloud = std::vector<Vector3f>;
structCluster {
Vector3f position;
};
using String = std::string;
structExampleWebSocketClient
{
Augmenta::Client augmentaClient;
// 1 -----
// Before anything else we need to initialize the client with the desired options
voidInitialize()
{
Augmenta::ProtocolOptions options;
options.usePolling = false;
// Specify the options you need
augmentaClient.initialize("my name", options);
}
// 2 -----
// When the client is notified that a connection was opened, we send a registry request
voidOnConnectionOpened()
{
// Use the lib to generate the message according to your options
std::string requestMessage = augmentaClient.getRegisterMessage();
SendMessageToServer(requestMessage);
}
// 3 ------
// The lib can be used to parse a data blob or messages to a more developer-friendly format
voidOnMessageReceived(const std::string &message)
{
constauto &parsedMessage = augmentaClient.parseControlMessage(message.c_str());
if (parsedMessage.isSetup())
{
if (parsedMessage.getStatus() == Augmenta::ControlMessage::Status::Error)
{
std::cout << "An error occured: " << parsedMessage.getErrorMessage() << std::endl;
return;
}
constauto &world = parsedMessage.getRootObject();
TraverseObjectHierarchy(&world);
}
elseif (parsedMessage.isUpdate())
{
// Same thing on update, save the data you need !
TraverseObjectHierarchy(&parsedMessage.getRootObject());
}
}
// 4 (cont.) ------
voidOnDataBlobReceived(const std::vector<std::byte> &dataBlob)
{
auto parsedData = augmentaClient.parseDataBlob(dataBlob.data(), dataBlob.size());
// Scene info
constauto &sceneInfo = parsedData.getSceneInfo();
String scenePath;
scenePath.resize(sceneInfo.getAddressLength());
sceneInfo.getAddress(scenePath.data());
// Objects
for (auto &object : parsedData.getObjects())
{
constauto &objectID = object.getID();
if (object.hasCluster())
{
auto &clusterInfo = object.getCluster();
if (clusterInfo.getState() == Augmenta::ClusterState::Entered)
{
// This is a new cluster, add it to our list
}
if (clusterInfo.getState() == Augmenta::ClusterState::WillLeave)
{
// Clean up leaving clusters
}
}
if (object.hasPointCloud())
{
auto &pcInfo = object.getPointCloud();
// Do something with the data
// For example you can copy the point data to your own data structure.
PointCloud pc;
pc.resize(pcInfo.getPointCount());
pcInfo.getPointsData(pc.data());
}
}
// Zones
for (constauto &zone : parsedData.getZones())
{
for (constauto &property : zone.getProperties())
{
switch (property.getType())
{
case Augmenta::DataBlob::ZoneEventPacket::Property::Type::Slider:
{
constauto &sliderData = property.getSliderParameters();
auto value = sliderData->getValue();
}
case Augmenta::DataBlob::ZoneEventPacket::Property::Type::XYPad:
{
constauto &xyData = property.getXYPadParameters();
auto x = xyData->getX();
auto y = xyData->getY();
}
// ...
}
}
}
}
voidSendMessageToServer(const std::string &message)
{
// ... do your thing
}
// Helper: Recursively traverse the object hierarchy (received on setup or update) and get the data you need
voidTraverseObjectHierarchy(const Augmenta::ControlMessage::Container *container)
{
if (container->isScene())
{
constauto &sceneParameters = container->getSceneParameters();
constauto &sceneSize = sceneParameters->size;
// Maybe save that somewhere ?
}
if (container->isZone())
{
constauto &zoneParameters = container->getZoneParameters();
if (zoneParameters->isBox())
{
constauto &boxParameters = zoneParameters->getBoxShapeParameters();
boxParameters->size;
// You might want to do something with this ?
}
}
for (constauto &child : container->getChildren())
{
TraverseObjectHierarchy(&child);
}
}
};