Optimized, updated

This commit is contained in:
Segey Lapin
2021-10-15 18:22:02 +03:00
parent ea4c8a5731
commit ccee4fb686
5 changed files with 230 additions and 166 deletions

View File

@@ -1,10 +1,11 @@
#ifndef ROAD_GRID_H
#define ROAD_GRID_H
#include <unordered_map>
#include <core/object.h>
#include <core/reference.h>
#include <scene/resources/curve.h>
#include <core/math/random_number_generator.h>
#include <modules/opensimplex/open_simplex_noise.h>
#include <modules/voxel/util/noise/fast_noise_lite.h>
class CanvasItem;
@@ -44,7 +45,6 @@ protected:
int spread, int dim);
HashMap<int, int> class_sizes;
struct half_edge;
HashMap<int, HashMap<int, List<struct half_edge *> > > hedge_grid;
Rect2 bounds;
void set_class_size(int cl, int sz)
{
@@ -141,15 +141,38 @@ protected:
rect.size.x = grid_height;
return rect;
}
inline void insert_hedge_to_grid_cell(int x, int y, struct half_edge *hedge)
{
if (hedge_grid.has(x) && hedge_grid[x].has(y))
hedge_grid[x][y].push_back(hedge);
else {
List<struct half_edge *> items;
items.push_back(hedge);
class hg {
typedef Vector<struct half_edge *> tvalue;
std::unordered_map<int, std::unordered_map<int, Vector<struct half_edge *> > > hedge_grid;
public:
inline const tvalue get(int x, int y) const
{
return hedge_grid.at(x).at(y);
}
inline bool has(int x, int y) const
{
if (hedge_grid.find(x) != hedge_grid.end() &&
hedge_grid.at(x).find(y) != hedge_grid.at(x).end())
return true;
return false;
}
inline void set(int x, int y, struct half_edge *hedge)
{
Vector<struct half_edge *> items;
if (has(x, y))
items = get(x, y);
items.resize(items.size() + 1);
items.write[items.size() - 1] = hedge;
hedge_grid[x][y] = items;
}
};
class hg hedge_grid;
inline void insert_hedge_to_grid_cell(int x, int y, struct half_edge *hedge)
{
static int count = 0;
count++;
hedge_grid.set(x, y, hedge);
printf("count: %d\n", count);
}
inline void add_hedge_to_grid(struct half_edge *hedge)
{
@@ -186,12 +209,44 @@ protected:
return (int)(y / grid_height);
}
float grid_width, grid_height;
friend class Roads;
PoolVector<Vector3> vertices;
public:
void build(Ref<Curve> curve, Ref<OpenSimplexNoise> noise);
struct vshape {
AABB area;
int instance;
int e1, e2;
int site;
Vector3 p1, p2, p3;
};
protected:
PoolVector<struct vshape> vshapes;
void sort_angle(Vector<int> &sort_data);
public:
void build(Ref<Curve> curve, Ref<FastNoiseLite> noise);
void draw_debug(Node *drawable, int size_x, int size_y) const;
int find_edge(int a, int b);
void setup_vshapes();
inline const PoolVector<struct vshape> &get_vshapes() const
{
const PoolVector<struct vshape> &ret = vshapes;
return ret;
}
inline PoolVector<struct vshape> &get_vshapes()
{
PoolVector<struct vshape> &ret = vshapes;
return ret;
}
Vector2 get_influence(int x, int y, float radius) const;
RoadGrid();
~RoadGrid();
inline int get_diagram_vertex_count() const
{
return diagram_vertices.size();
}
inline int get_map_hedges_count() const
{
return map_hedges.size();
}
void build_building_positions();
};
#endif