Reverted planemap
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
#define CELL_INVALID 0
|
||||
|
||||
// Round a float and return as integer. Clamp result to the specified range.
|
||||
static int round_and_clamp(float x, int lo, int hi) {
|
||||
static int round_and_clamp(double x, int lo, int hi) {
|
||||
x = round(x);
|
||||
if (x < lo) return lo;
|
||||
if (x > hi) return hi;
|
||||
@@ -38,7 +38,7 @@ struct CellRange {
|
||||
// the map: in that case, returns exactly the valid cells and not the
|
||||
// invalid ones.
|
||||
//
|
||||
static CellRange rect_cell_range(float x1, float y1, float x2, float y2) {
|
||||
static CellRange rect_cell_range(double x1, double y1, double x2, double y2) {
|
||||
CellRange result;
|
||||
result.xlo = round_and_clamp(x1 / CELL_SCALE, -CELL_LIMIT, CELL_LIMIT + 1);
|
||||
result.ylo = round_and_clamp(y1 / CELL_SCALE, -CELL_LIMIT, CELL_LIMIT + 1);
|
||||
@@ -54,9 +54,9 @@ static int64_t cell_id(int64_t cellx, int64_t celly) {
|
||||
}
|
||||
|
||||
// Get the cell ID of the specified point, or CELL_INVALID if the point is off the map.
|
||||
static int64_t point_cell_id(float x, float y) {
|
||||
float cellx = round(x / CELL_SCALE);
|
||||
float celly = round(y / CELL_SCALE);
|
||||
static int64_t point_cell_id(double x, double y) {
|
||||
double cellx = round(x / CELL_SCALE);
|
||||
double celly = round(y / CELL_SCALE);
|
||||
if ((cellx < -CELL_LIMIT) || (celly < -CELL_LIMIT) || (cellx > CELL_LIMIT) || (celly > CELL_LIMIT)) {
|
||||
return CELL_INVALID;
|
||||
}
|
||||
@@ -101,10 +101,10 @@ void PlaneMap::insert(const std::string &plane, int64_t cellid, PlaneItem *clien
|
||||
l.push_back(client);
|
||||
}
|
||||
|
||||
void PlaneItem::set_pos(const std::string &plane, const util::XYZ &xyz) {
|
||||
int64_t old_cell = point_cell_id(xyz_.x, xyz_.y);
|
||||
int64_t new_cell = point_cell_id(xyz.x, xyz.y);
|
||||
|
||||
void PlaneItem::set_pos(const std::string &plane, double x, double y, double z) {
|
||||
int64_t old_cell = point_cell_id(x_, y_);
|
||||
int64_t new_cell = point_cell_id(x, y);
|
||||
|
||||
// Update the grid.
|
||||
if (pmap_ != 0) {
|
||||
if ((plane_ != plane) || (old_cell != new_cell)) {
|
||||
@@ -115,12 +115,14 @@ void PlaneItem::set_pos(const std::string &plane, const util::XYZ &xyz) {
|
||||
|
||||
// Update the client position.
|
||||
plane_ = plane;
|
||||
xyz_ = xyz;
|
||||
x_ = x;
|
||||
y_ = y;
|
||||
z_ = z;
|
||||
}
|
||||
|
||||
void PlaneItem::untrack() {
|
||||
if (pmap_ != 0) {
|
||||
pmap_->remove(plane_, point_cell_id(xyz_.x, xyz_.y), this);
|
||||
pmap_->remove(plane_, point_cell_id(x_, y_), this);
|
||||
pmap_ = 0;
|
||||
}
|
||||
}
|
||||
@@ -133,7 +135,7 @@ void PlaneMap::track(PlaneItem *item) {
|
||||
}
|
||||
}
|
||||
|
||||
PlaneItem::PlaneItem() : pmap_(NULL), xyz_(0,0,0) {
|
||||
PlaneItem::PlaneItem() : pmap_(NULL), x_(0.0), y_(0.0), z_(0.0) {
|
||||
}
|
||||
|
||||
PlaneMap::PlaneMap() {
|
||||
@@ -174,13 +176,13 @@ int PlaneMap::total_cells() const {
|
||||
return total;
|
||||
}
|
||||
|
||||
PlaneMap::EltVec PlaneMap::scan_radius(const std::string &plane, float x, float y, float radius) const {
|
||||
PlaneMap::EltVec PlaneMap::scan_radius(const std::string &plane, double x, double y, double radius) const {
|
||||
PlaneMap::EltVec result;
|
||||
auto piter = planes_.find(plane);
|
||||
if (piter != planes_.end()) {
|
||||
const Plane &p = piter->second;
|
||||
CellRange range = rect_cell_range(x - radius, y - radius, x + radius, y + radius);
|
||||
float radsq = radius*radius;
|
||||
double radsq = radius*radius;
|
||||
for (int cy = range.ylo; cy <= range.yhi; cy++) {
|
||||
for (int cx = range.xlo; cx <= range.xhi; cx++) {
|
||||
auto liter = p.find(cell_id(cx, cy));
|
||||
@@ -198,8 +200,8 @@ PlaneMap::EltVec PlaneMap::scan_radius(const std::string &plane, float x, float
|
||||
}
|
||||
|
||||
LuaDefine(unittests_planemap, "c") {
|
||||
float SC = CELL_SCALE;
|
||||
float E = CELL_SCALE * 0.4;
|
||||
double SC = CELL_SCALE;
|
||||
double E = CELL_SCALE * 0.4;
|
||||
int LO = -CELL_LIMIT;
|
||||
int HI = CELL_LIMIT;
|
||||
PlaneMap pm;
|
||||
@@ -312,15 +314,5 @@ LuaDefine(unittests_planemap, "c") {
|
||||
elts = pm.scan_radius("bar", 1000.0, 1000.0, 100.0);
|
||||
LuaAssert(L, elts.size() == 2);
|
||||
|
||||
// Test static_field_cast.
|
||||
struct MyTangible {
|
||||
int x, y, z;
|
||||
PlaneItem plane_item_;
|
||||
};
|
||||
MyTangible tan;
|
||||
PlaneItem *pip = &tan.plane_item_;
|
||||
MyTangible *tanp = pip->static_field_cast<MyTangible>();
|
||||
LuaAssert(L, tanp == &tan);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -74,10 +74,8 @@
|
||||
#define PLANEMAP_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "util.hpp"
|
||||
|
||||
class PlaneMap;
|
||||
|
||||
@@ -87,38 +85,20 @@ class PlaneItem {
|
||||
private:
|
||||
PlaneMap *pmap_;
|
||||
std::string plane_;
|
||||
util::XYZ xyz_;
|
||||
double x_, y_, z_;
|
||||
|
||||
public:
|
||||
PlaneItem();
|
||||
~PlaneItem();
|
||||
|
||||
const std::string &plane() const { return plane_; }
|
||||
const util::XYZ &xyz() const { return xyz_; }
|
||||
const float x() const { return xyz_.x; }
|
||||
const float y() const { return xyz_.y; }
|
||||
const float z() const { return xyz_.z; }
|
||||
const double x() const { return x_; }
|
||||
const double y() const { return y_; }
|
||||
const double z() const { return z_; }
|
||||
|
||||
void untrack();
|
||||
void set_pos(const std::string &plane, const util::XYZ &xyz);
|
||||
void set_pos(const std::string &plane, float x, float y, float z) { set_pos(plane, util::XYZ(x,y,z)); }
|
||||
void set_xyz(const util::XYZ &xyz) { set_pos(plane_, xyz); }
|
||||
void set_xyz(float x, float y, float z) { set_pos(plane_, util::XYZ(x, y, z)); }
|
||||
|
||||
// Assume that some other object has methods 'get_plane' and 'get_xyz'.
|
||||
// Get the plane and XYZ from that object and store it in this PlaneItem.
|
||||
template<class T>
|
||||
void set_pos_from(const T &obj) {
|
||||
set_pos(obj.get_plane(), obj.get_xyz());
|
||||
}
|
||||
|
||||
// Assume that the PlaneItem is a field in an object of class T.
|
||||
// Static cast the PlaneItem pointer to a type T pointer.
|
||||
template<class T>
|
||||
T *static_field_cast() {
|
||||
const std::size_t offset = offsetof(T, plane_item_);
|
||||
return reinterpret_cast<T*>(reinterpret_cast<char*>(this) - offset);
|
||||
}
|
||||
void set_pos(const std::string &plane, double x, double y, double z);
|
||||
void set_xyz(double x, double y, double z) { set_pos(plane_, x, y, z); }
|
||||
};
|
||||
|
||||
class PlaneMap {
|
||||
@@ -134,7 +114,7 @@ public:
|
||||
PlaneMap();
|
||||
~PlaneMap();
|
||||
void track(PlaneItem *item);
|
||||
EltVec scan_radius(const std::string &plane, float x, float y, float radius) const;
|
||||
EltVec scan_radius(const std::string &plane, double x, double y, double radius) const;
|
||||
|
||||
private:
|
||||
// unit testing stuff.
|
||||
|
||||
Reference in New Issue
Block a user