Repository navigation
Expand file tree
/
Copy pathSession.h
More file actions
299 lines (250 loc) · 9.77 KB
/
Copy pathSession.h
File metadata and controls
299 lines (250 loc) · 9.77 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/***************************************************************************
# Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/
#pragma once
#include <G3D/G3D.h>
#include "ConfigFiles.h"
#include <ctime>
class FPSciApp;
class PlayerEntity;
class TargetEntity;
class FPSciLogger;
// Simple timer for measuring time offsets
class Timer
{
public:
std::chrono::steady_clock::time_point startTime;
void startTimer() { startTime = std::chrono::steady_clock::now(); };
float getTime()
{
auto now = std::chrono::steady_clock::now();
int t = std::chrono::duration_cast<std::chrono::duration<int, std::milli>>(now - startTime).count();
return ((float)t) / 1000.0f;
};
};
struct FrameInfo {
FILETIME time;
//float idt = 0.0f;
float sdt = 0.0f;
FrameInfo() {};
FrameInfo(FILETIME t, float simDeltaTime) {
time = t;
sdt = simDeltaTime;
}
};
struct TargetLocation {
FILETIME time;
String name = "";
Point3 position = Point3::zero();
TargetLocation() {};
TargetLocation(FILETIME t, String targetName, Point3 targetPosition) {
time = t;
name = targetName;
position = targetPosition;
}
};
enum PlayerActionType{
None,
Aim,
Invalid,
Nontask,
Miss,
Hit,
Destroy
};
struct PlayerAction {
FILETIME time;
Point2 viewDirection = Point2::zero();
Point3 position = Point3::zero();
PlayerActionType action = PlayerActionType::None;
String targetName = "";
PlayerAction() {};
PlayerAction(FILETIME t, Point2 playerViewDirection, Point3 playerPosition, PlayerActionType playerAction, String name) {
time = t;
viewDirection = playerViewDirection;
position = playerPosition;
action = playerAction;
targetName = name;
}
};
class Session : public ReferenceCountedObject {
protected:
FPSciApp* m_app = nullptr; ///< Pointer to the app
Scene* m_scene = nullptr; ///< Pointer to the scene
shared_ptr<SessionConfig> m_config; ///< The session this experiment will run
shared_ptr<FPSciLogger> m_logger; ///< Output results logger
shared_ptr<PlayerEntity> m_player; ///< Player entity
shared_ptr<Camera> m_camera; ///< Camera entity
// Experiment management
int m_remainingTargets; ///< Number of remaining targets (calculated at the end of the session)
int m_destroyedTargets = 0; ///< Number of destroyed target
int m_clickCount = 0; ///< Count of total clicks in this trial
bool m_hasSession; ///< Flag indicating whether psych helper has loaded a valid session
String m_feedbackMessage; ///< Message to show when trial complete
// Target management
Table<String, Array<shared_ptr<ArticulatedModel>>>* m_targetModels;
int m_modelScaleCount;
int m_lastUniqueID = 0; ///< Counter for creating unique names for various entities
Array<shared_ptr<TargetEntity>> m_targetArray; ///< Array of drawn targets
int m_currTrialIdx; ///< Current trial
int m_currQuestionIdx = -1; ///< Current question index
Array<int> m_remainingTrials; ///< Completed flags
Array<Array<shared_ptr<TargetConfig>>> m_targetConfigs; ///< Target configurations by trial
// Time-based parameters
RealTime m_taskExecutionTime; ///< Task completion time for the most recent trial
String m_taskStartTime; ///< Recorded task start timestamp
String m_taskEndTime; ///< Recorded task end timestamp
RealTime m_totalRemainingTime = 0; ///< Time remaining in the trial
RealTime m_scoreboardDuration = 10.0; ///< Show the score for at least this amount of seconds.
RealTime m_lastFireAt = 0.f; ///< Time of the last shot
Timer m_timer; ///< Timer used for timing tasks
// Could move timer above to stopwatch in future
//Stopwatch stopwatch;
// Target parameters
const float m_targetDistance = 1.0f; ///< Actual distance to target
Session(FPSciApp* app, shared_ptr<SessionConfig> config) : m_app(app), m_config(config)
{
m_hasSession = notNull(m_config);
}
Session(FPSciApp* app) : m_app(app)
{
m_hasSession = false;
}
~Session(){
clearTargets(); // Clear the targets when the session is done
}
/** Creates a random target with motion based on parameters
@param motionDuration time in seconds to produce a motion path for
@param motionDecisionPeriod time in seconds when new motion direction is chosen
@param speed world-space velocity (m/s) of target
@param radius world-space distance to target
@param scale size of target TODO: is this radius or diameter in meters?*/
//void spawnParameterizedRandomTarget(float motionDuration, float motionDecisionPeriod, float speed, float radius, float scale);
/** Creates a random target in front of the player */
//void spawnRandomTarget();
/** Creates a spinning target */
//shared_ptr<FlyingEntity> spawnTarget(const Point3& position, float scale, bool spinLeft = true, const Color3& color = Color3::red(), String modelName = "model/target/target.obj");
/** Insert a target into the target array/scene */
inline void insertTarget(shared_ptr<TargetEntity> target);
shared_ptr<TargetEntity> spawnDestTarget(
shared_ptr<TargetConfig> config,
const Point3& position,
const Color3& color,
const int paramIdx,
const String& name = "");
shared_ptr<FlyingEntity> spawnReferenceTarget(
const Point3& position,
const Point3& orbitCenter,
const float size,
const Color3& color
);
shared_ptr<FlyingEntity> spawnFlyingTarget(
shared_ptr<TargetConfig> config,
const Point3& position,
const Point3& orbitCenter,
const Color3& color,
const int paramIdx,
const String& name = ""
);
shared_ptr<JumpingEntity> spawnJumpingTarget(
shared_ptr<TargetConfig> config,
const Point3& position,
const Point3& orbitCenter,
const Color3& color,
const float targetDistance,
const int paramIdx,
const String& name = ""
);
inline Point2 getViewDirection()
{ // returns (azimuth, elevation), where azimuth is 0 deg when straightahead and + for right, - for left.
Point3 view_cartesian = m_camera->frame().lookVector();
float az = atan2(-view_cartesian.z, -view_cartesian.x) * 180 / pif();
float el = atan2(view_cartesian.y, sqrtf(view_cartesian.x * view_cartesian.x + view_cartesian.z * view_cartesian.z)) * 180 / pif();
return Point2(az, el);
}
inline Point3 getPlayerLocation()
{
return m_camera->frame().translation;
}
public:
float initialHeadingRadians = 0.0f;
static shared_ptr<Session> create(FPSciApp* app) {
return createShared<Session>(app);
}
static shared_ptr<Session> create(FPSciApp* app, shared_ptr<SessionConfig> config) {
return createShared<Session>(app, config);
}
void randomizePosition(const shared_ptr<TargetEntity>& target) const;
void initTargetAnimation();
float weaponCooldownPercent() const;
RealTime lastFireTime() const {
return m_lastFireAt;
}
int remainingAmmo() const;
bool isComplete() const;
void nextCondition();
void endLogging();
/** randomly returns either +1 or -1 **/
static float randSign() {
if (Random::common().uniform() > 0.5) {
return 1;
} else {
return -1;
}
}
void updatePresentationState();
void onInit(String filename, String description);
void onSimulation(RealTime rdt, SimTime sdt, SimTime idt);
void processResponse();
void recordTrialResponse(int destroyedTargets, int totalTargets);
void accumulateTrajectories();
void accumulateFrameInfo(RealTime rdt, float sdt, float idt);
void countDestroy() {
m_destroyedTargets += 1;
}
/** Destroy a target from the targets array */
void destroyTarget(int index);
void destroyTarget(shared_ptr<TargetEntity> target);
/** clear all targets (used when clearing remaining targets at the end of a trial) */
void clearTargets();
float getRemainingTrialTime();
float getProgress();
int getScore();
String getFeedbackMessage();
/** queues action with given name to insert into database when trial completes
@param action - one of "aim" "hit" "miss" or "invalid (shots limited by fire rate)" */
void accumulatePlayerAction(PlayerActionType action, String target="");
bool canFire();
bool setupTrialParams(Array<Array<shared_ptr<TargetConfig>>> trials);
bool moveOn = false; ///< Flag indicating session is complete
enum PresentationState presentationState; ///< Current presentation state
/** result recording */
void countClick() { m_clickCount++; }
const Array<shared_ptr<TargetEntity>>& targetArray() const {
return m_targetArray;
}
};