File: UserLocation.cpp

package info (click to toggle)
dasher 4.11%2Bgit20130508.adc653-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 40,248 kB
  • ctags: 5,158
  • sloc: xml: 185,479; cpp: 32,301; sh: 11,207; makefile: 828; ansic: 483
file content (268 lines) | stat: -rw-r--r-- 7,475 bytes parent folder | download | duplicates (10)
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

#include "../Common/Common.h"

#include "UserLocation.h"

#include <sys/timeb.h>

// Track memory leaks on Windows to the line that new'd the memory
#ifdef _WIN32
#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#endif

// Construct a new location at the current point in time and at the specified coordinates.
// This version only stores the integer coordinate data.
CUserLocation::CUserLocation(int iX, int iY, float dNats)
{
  //CFunctionLogger f1("CUserLocation::CUserLocation(1)", gLogger);

  InitMemeberVars();

  m_strTime               = CTimeSpan::GetTimeStamp();
  m_iLocationX            = iX;
  m_iLocationY            = iY;
  m_bHasInteger           = true;
  m_dNats                 = dNats;
}

// Stores only the normalized floating point data.
CUserLocation::CUserLocation(float iX, float iY, float dNats)
{
  //CFunctionLogger f1("CUserLocation::CUserLocation(2)", gLogger);

  InitMemeberVars();

  m_strTime               = CTimeSpan::GetTimeStamp();
  m_dNormalizedLocationX  = iX;
  m_dNormalizedLocationY  = iY;
  m_bHasNormalized        = true;
  m_dNats                 = dNats;
}

// Stores only the normalized floating point data. 
// This version calculates the normalization itself.
CUserLocation::CUserLocation(int iX, 
                             int iY, 
                             int iTop, 
                             int iLeft, 
                             int iBottom, 
                             int iRight, 
                             bool bStoreIntegerRep, 
                             float dNats)
{
  //CFunctionLogger f1("CUserLocation::CUserLocation(3)", gLogger);

  InitMemeberVars();

  m_strTime               = CTimeSpan::GetTimeStamp();
  m_dNormalizedLocationX  = (float) ComputeNormalizedX(iX, iLeft, iRight);
  m_dNormalizedLocationY  = (float) ComputeNormalizedY(iY, iTop, iBottom);
  m_bHasNormalized        = true;
  m_dNats                 = dNats;

  if (bStoreIntegerRep)
  {
    m_bHasInteger         = true;
    m_iLocationX          = iX;
    m_iLocationY          = iY;
  }
  else
    m_bHasInteger         = false;
}

// We want both the integer representation and the normalized.
CUserLocation::CUserLocation(int iX1, int iY1, float iX2, float iY2, float dNats)
{
  //CFunctionLogger f1("CUserLocation::CUserLocation(4)", gLogger);

  InitMemeberVars();

  m_strTime               = CTimeSpan::GetTimeStamp();
  m_iLocationX            = iX1;
  m_iLocationY            = iY1;
  m_dNormalizedLocationX  = iX2;
  m_dNormalizedLocationY  = iY2;
  m_bHasNormalized        = true;
  m_bHasInteger           = true;
  m_dNats                 = dNats;
}

CUserLocation::~CUserLocation()
{
  //CFunctionLogger f1("CUserLocation::~CUserLocation", gLogger);
}

string CUserLocation::GetXML(const string& strPrefix)
{
  //CFunctionLogger f1("CUserLocation::GetXML", gLogger);

  string strResult = "";

  strResult += strPrefix;
  strResult += "<Pos>\n";

  strResult += strPrefix;
  strResult += "\t<Time>";
  strResult += m_strTime;
  strResult += "</Time>\n";

  char strNum[256];
  if (m_bHasInteger)
  {
    strResult += strPrefix;
    strResult += "\t<X>";
    sprintf(strNum, "%d", m_iLocationX);
    strResult += strNum;
    strResult += "</X>\n";

    strResult += strPrefix;
    strResult += "\t<Y>";
    sprintf(strNum, "%d", m_iLocationY);
    strResult += strNum;
    strResult += "</Y>\n";
  }
  if (m_bHasNormalized)
  {
    strResult += strPrefix;
    strResult += "\t<XNorm>";
    sprintf(strNum, "%0.4f", m_dNormalizedLocationX);
    strResult += strNum;
    strResult += "</XNorm>\n";

    strResult += strPrefix;
    strResult += "\t<YNorm>";
    sprintf(strNum, "%0.4f", m_dNormalizedLocationY);
    strResult += strNum;
    strResult += "</YNorm>\n";    
  }

  strResult += strPrefix;
  strResult += "\t<Bits>";
  sprintf(strNum, "%0.3f", m_dNats / log(2.0));	
  strResult += strNum;
  strResult += "</Bits>\n";

  strResult += strPrefix;
  strResult += "</Pos>\n";

  return strResult;
}

// Static helper method for computing normalized X coordinate
double CUserLocation::ComputeNormalizedX(int iX, int iLeft, int iRight)
{
  //CFunctionLogger f1("CUserLocation::ComputeNormalizedX", gLogger);

  return (double) (iX - iLeft) / (double) abs(iRight - iLeft);
}

// Static helper method for computing normalized Y coordinate
double CUserLocation::ComputeNormalizedY(int iY, int iTop, int iBottom)
{
  //CFunctionLogger f1("CUserLocation::ComputeNormalizedY", gLogger);

  return (double) (iY - iTop) / (double) abs(iBottom - iTop);
}

void CUserLocation::InitMemeberVars()
{
  //CFunctionLogger f1("CUserLocation::InitMemeberVars", gLogger);

  m_strTime               = "";
  m_iLocationX            = 0;
  m_iLocationY            = 0;
  m_dNormalizedLocationX  = 0.0;
  m_dNormalizedLocationY  = 0.0;
  m_bHasNormalized        = false;
  m_bHasInteger           = false;
  m_dNats                 = 0.0;
}

// Construct based on some XML like:
//  <Pos>
//  	<Time>15:49:10.203</Time>
//  	<X>807</X>
//  	<Y>382</Y>
//  	<XNorm>0.7274</XNorm>
//  	<YNorm>0.1853</YNorm>
//  	<Bits>0.555</Bits>
//  </Pos>
CUserLocation::CUserLocation(const string& strXML)
{
  //CFunctionLogger f1("CUserLocation::CUserLocation(XML)", gLogger);

  InitMemeberVars();

  bool bFoundNormX        = false;
  bool bFoundNormY        = false;
  bool bFoundX            = false;
  bool bFoundY            = false;

  m_strTime               = XMLUtil::GetElementString("Time", strXML, true);    
  m_iLocationX            = XMLUtil::GetElementInt("X", strXML, &bFoundX);
  m_iLocationY            = XMLUtil::GetElementInt("Y", strXML, &bFoundY);
  m_dNormalizedLocationX  = (float) XMLUtil::GetElementFloat("XNorm", strXML, &bFoundNormX);
  m_dNormalizedLocationY  = (float) XMLUtil::GetElementFloat("YNorm", strXML, &bFoundNormY);

  // Convert the bits back to dNats
  m_dNats                 = (float) ((double) XMLUtil::GetElementFloat("Bits", strXML) * (double) log(2.0));

  // If there weren't X, Y elements, we want them set to 0 and mark
  // ourselves as not having them.
  if ((!bFoundX) && (!bFoundY))
  {
    m_iLocationX  = 0;
    m_iLocationY  = 0;
    m_bHasInteger = false;
  }
  else
    m_bHasInteger = true;

  // Require that we find both XNorm and YNorm in order to count
  if ((!bFoundNormX) || (!bFoundNormY))
  {
    m_dNormalizedLocationX  = 0.0;
    m_dNormalizedLocationY  = 0.0;
    m_bHasNormalized        = false;
  }
  else
    m_bHasNormalized        = true;

}

// Returns a tab delimited version of this location's X & Y coordinate
string CUserLocation::GetTabMouseXY(bool bReturnNormalized)
{
  //CFunctionLogger f1("CUserLocation::GetTabMouseXY", gLogger);

  string strResult = "";
  char szNum[256];

  if (bReturnNormalized)
    sprintf(szNum, "%0.4f\t%0.4f\n", m_dNormalizedLocationX, m_dNormalizedLocationY);
  else
    sprintf(szNum, "%0.4f\t%0.4f\n", (double)m_iLocationX, (double)m_iLocationY);

  strResult += szNum;

  return strResult;
}

// Figure out what grid location this normalized mouse coordinate should go.
void CUserLocation::GetMouseGridLocation(int iGridSize, int* pRow, int* pCol)
{
  //CFunctionLogger f1("CUserLocation::GetMouseGridLocation", gLogger);

  if ((pRow == NULL) || (pCol == NULL))
    return;

  *pRow = (int) (m_dNormalizedLocationX * (double) iGridSize);
  *pCol = (int) (m_dNormalizedLocationY * (double) iGridSize);
}