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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user