File: PositionTracker.h

package info (click to toggle)
bzflag 2.0.13.20080902-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 27,564 kB
  • ctags: 34,716
  • sloc: cpp: 139,842; ansic: 14,510; sh: 10,715; makefile: 2,454; perl: 477; php: 428; python: 345; objc: 243; xml: 24
file content (132 lines) | stat: -rw-r--r-- 4,825 bytes parent folder | download
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
/* bzflag
 * Copyright (c) 1993 - 2008 Tim Riker
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named LICENSE that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef __POSITIONTRACKER_H__
#define __POSTIIONTRACKER_H__

// common header first
#include "common.h"

/* system interface headers */
#include <string>
#include <vector>
#include <map>
#include <limits.h>

/* common interface headers */
#include "TimeKeeper.h"


/** PositionTracker keeps track of where things are in a 3D Cartesian
 * coordinate space.  It can give introspective information like where
 * things are and how close other tracked objects are.  Objects may be
 * grouped into separate categories for subsearching.  All objects are
 * labeled with either a string or numeric identifier.
 */
class PositionTracker
{
public:
  typedef enum idtype { UNSET_ID, INT_ID, STR_ID } idtype_t;

private:

  /** something that we are tracking (uniquely identified by either an
   * integer or a string)
   */
  typedef struct item {
    idtype_t id;
    long int intID;
    std::string strID;
    TimeKeeper lastUpdate;
    double position[3]; // position in Cartesian 3-space coodinates
    bool forgotten;
  } item_t;

  typedef std::vector<item_t *> TrackedItemVector;

  /** the actual collection of things being tracked are categorized
   *  into some provided group name.  using that group name (which is
   *  "" if not provided), we get back a set of tracked objects.
   */
  std::map<std::string, TrackedItemVector> _trackedItem;

  /** waypoint distance matrix
   */
  std::map<std::pair<unsigned short int, unsigned short int>, double> _waypointDistance;

protected:

public:

  PositionTracker();
  PositionTracker(const PositionTracker& tracker);
  ~PositionTracker();

  /** inform the tracker that there is intent to track something new.
   * tracking items may be categorized using a provided group name.  a
   * token is returned that must be used for subsequent updates.
   */
  unsigned short track(const std::string id, std::string group = std::string(""));
  unsigned short track(long int id, std::string group = std::string(""));

  /** update the position of a tracked item.  returns truthfully
   * whether the update could be performed (i.e. whether the id
   * existed).  the token and string id are both required for
   * validation.
   */
  bool update(unsigned short int token, const std::string id, const double position[3], std::string group = std::string(""));
  bool update(unsigned short int token, const std::string id, const float position[3], std::string group = std::string(""));
  bool update(unsigned short int token, long int id, const double position[3], std::string group = std::string(""));
  bool update(unsigned short int token, long int id, const float position[3], std::string group = std::string(""));

  /** add a waypoint to the tracker so that it can calculate shortest
   * paths better.  A negative distance will cause the real distance
   * to be computed and stored, otherwise a non-negative distance will
   * indicate how "far" the two points are.  Setting the distance to
   * zero or near zero is akin to a teleporter.
   */
  bool addWaypoint(const double from[3], const double to[3], double distance=-1.0);
  bool addWaypoint(const float from[3], const float to[3], double distance=-1.0);

  /** stop tracking something if it was being tracked.  the token and
   * string id are both required for validation.
   */
  bool forget(unsigned short int token, const std::string id, std::string group = std::string(""));
  bool forget(unsigned short int token, long int id, std::string group = std::string(""));

  /** compute the simple distance between two tracked items given their tokens.
   */
  double distanceBetween(unsigned short int fromToken, unsigned short int toToken, std::string fromGroup=std::string(""), std::string toGroup=std::string("")) const;

  /** compute the shortest distance between two tracked items,
   * utilizing available waypoint shortcuts.
   */
  double waypointDistance(unsigned short int fromToken, unsigned short int toToken, std::string fromGroup=std::string(""), std::string toGroup=std::string("")) const;

  /** returns a count of how many objects are being tracked in a
   * particular group.
   */
  unsigned short int trackedCount(std::string group = std::string("")) const;

};

#else
class PositionTracker;
#endif

// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
// c-basic-offset: 2 ***
// indent-tabs-mode: t ***
// End: ***
// ex: shiftwidth=2 tabstop=8