All Classes Namespaces Files Functions Variables Typedefs Pages
Proximity2D.h
Go to the documentation of this file.
1 
2 // ----------------------------------------------------------------------------
3 // Copyright (c) 2002-2003, Sony Computer Entertainment America
4 // Original author: Craig Reynolds <craig_reynolds@playstation.sony.com>
5 //
6 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
7 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
9 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
10 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
11 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
12 // DEALINGS IN THE SOFTWARE.
13 //
14 // ----------------------------------------------------------------------------
15 
21 #pragma once
22 
23 #include <vector>
24 #include "lq2D.h"
25 #include "ProximityDatabaseItem.h"
26 
27 
28 namespace TTC {
29 
30 // ----------------------------------------------------------------------------
31  // A ProximityDatabase-style wrapper for the LQ bin lattice system
32  class LQProximityDatabase2D
33  {
34  public:
35 
36  // constructor
37  LQProximityDatabase2D (const Vector2D& center,
38  const Vector2D& dimensions,
39  const Vector2D& divisions)
40  {
41  const Vector2D halfsize (dimensions * 0.5f);
42  const Vector2D origin (center - halfsize);
43  _origin = origin;
44  _dimensions = dimensions;
45  _divisions = divisions;
46 
47  lq = lqCreateDatabase2D (origin.x, origin.y,
48  dimensions.x, dimensions.y,
49  (int) floor(0.5f + divisions.x),
50  (int) floor(0.5f + divisions.y));
51  }
52 
53  // destructor
54  virtual ~LQProximityDatabase2D ()
55  {
56  lqDeleteDatabase2D (lq);
57  lq = NULL;
58  }
59 
60  // "token" to represent objects stored in the database
61  class tokenType
62  {
63  public:
64 
65  // constructor
66  tokenType (ProximityDatabaseItem* parentObject, LQProximityDatabase2D& lqsd)
67  {
68  lqInitClientProxy2D (&proxy, parentObject);
69  lq = lqsd.lq;
70  }
71 
72  // destructor
73  virtual ~tokenType (void)
74  {
75  lqRemoveFromBin (&proxy);
76  }
77 
78  // the client object calls this each time its position changes
79  void updateForNewPosition (const Vector2D& p)
80  {
81  lqUpdateForNewLocation (lq, &proxy, p.x, p.y);
82  }
83 
84  // find all neighbors within the given sphere (as center and radius)
85  void findNeighbors (const Vector2D& center,
86  const Vector2D& facingDirection,
87  const float radius,
88  std::vector<ProximityDatabaseItem*>& results,
89  bool restrictedView=false)
90  {
91  lqMapOverAllObjectsInLocality (lq,
92  center.x, center.y,
93  facingDirection.x, facingDirection.y,
94  radius,
95  restrictedView,
96  perNeighborCallBackFunction,
97  (void*)&results);
98  }
99 
100  // called by LQ for each clientObject in the specified neighborhood:
101  // push that clientObject onto the ContentType vector in void*
102  // clientQueryState
103  // (parameter names commented out to prevent compiler warning from "-W")
104  static void perNeighborCallBackFunction (void* clientObject,
105  float /*distanceSquared*/,
106  void* clientQueryState)
107  {
108  typedef std::vector<ProximityDatabaseItem*> ctv;
109  ctv& results = *((ctv*) clientQueryState);
110  results.push_back ((ProximityDatabaseItem*) clientObject);
111  }
112 
113  private:
114  lqClientProxy2D proxy;
115  lqInternalDB2D* lq;
116  };
117 
118  // allocate a token to represent a given client object in this database
119  tokenType* allocateToken (ProximityDatabaseItem* parentObject)
120  {
121  return new tokenType (parentObject, *this);
122  }
123 
124  // count the number of tokens currently in the database
125  int getPopulation (void)
126  {
127  int count = 0;
128  lqMapOverAllObjects (lq, counterCallBackFunction, &count);
129  return count;
130  }
131 
132  // (parameter names commented out to prevent compiler warning from "-W")
133  static void counterCallBackFunction (void* /*clientObject*/,
134  float /*distanceSquared*/,
135  void* clientQueryState)
136  {
137  int& counter = *(int*)clientQueryState;
138  counter++;
139  }
140 
141  Vector2D getOrigin (void) {return _origin;}
142  Vector2D getDivisions (void) {return _divisions;}
143  Vector2D getDimensions (void) {return _dimensions;}
144 
145  private:
146  lqInternalDB2D* lq;
147  Vector2D _origin;
148  Vector2D _divisions;
149  Vector2D _dimensions;
150  };
151 
153  typedef LQProximityDatabase2D SpatialProximityDatabase;
154 
156  typedef SpatialProximityDatabase::tokenType ProximityToken;
157 
158 }
LQProximityDatabase2D SpatialProximityDatabase
The spatial proximity database.
Definition: Proximity2D.h:153
This is a C++ modified version of Opensteer's original spatial database.
Declares the virtual interface inherited by the objects of the proximity database.
SpatialProximityDatabase::tokenType ProximityToken
An object in the proximity database.
Definition: Proximity2D.h:156