Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix fishnet error when cell_size is not an index of 2 #579

Merged
merged 7 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 56 additions & 43 deletions cpp/src/arrow/render_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,81 +454,94 @@ template <typename T>
std::pair<uint8_t*, int64_t> render_fishnetmap(const std::vector<std::string>& points,
const std::vector<T>& arr_c,
const std::string& conf) {
auto data = AggHandler::weight_agg<T>(points, arr_c);
auto num_point = data.first.size();

std::vector<uint32_t> input_x(num_point);
std::vector<uint32_t> input_y(num_point);
std::vector<T> input_c(num_point);

rapidjson::Document document;
document.Parse(conf.c_str());
rapidjson::Value mark_enter;
mark_enter = document["marks"][0]["encode"]["enter"];
auto agg = mark_enter["aggregation_type"]["value"].GetString();
int cell_size = mark_enter["cell_size"]["value"].GetDouble();
int cell_spacing = mark_enter["cell_spacing"]["value"].GetDouble();
auto region_size = cell_size + cell_spacing;
AggHandler::AggType type_agg = AggHandler::agg_type(agg);

const auto& result_wkb = data.first;
const auto& result_weight = data.second;
auto data = AggHandler::region_agg<T>(points, arr_c, region_size);
auto num_point = data.size();

std::vector<uint32_t> input_x(num_point);
std::vector<uint32_t> input_y(num_point);
std::vector<T> input_c(num_point);
int i = 0;

switch (type_agg) {
case AggHandler::AggType::MAX: {
for (int i = 0; i < num_point; i++) {
input_x[i] = result_wkb[i]->toPoint()->getX();
input_y[i] = result_wkb[i]->toPoint()->getY();
input_c[i] = *max_element(result_weight[i].begin(), result_weight[i].end());
OGRGeometryFactory::destroyGeometry(result_wkb[i]);
for (auto iter = data.begin(); iter != data.end(); iter++) {
auto result_point = iter->first;
auto result_weight = iter->second;
input_x[i] = result_point.first;
input_y[i] = result_point.second;
input_c[i] = *max_element(result_weight.begin(), result_weight.end());
i++;
}
break;
}
case AggHandler::AggType::MIN: {
for (int i = 0; i < num_point; i++) {
input_x[i] = result_wkb[i]->toPoint()->getX();
input_y[i] = result_wkb[i]->toPoint()->getY();
input_c[i] = *min_element(result_weight[i].begin(), result_weight[i].end());
OGRGeometryFactory::destroyGeometry(result_wkb[i]);
for (auto iter = data.begin(); iter != data.end(); iter++) {
auto result_point = iter->first;
auto result_weight = iter->second;
input_x[i] = result_point.first;
input_y[i] = result_point.second;
input_c[i] = *min_element(result_weight.begin(), result_weight.end());
i++;
}
break;
}
case AggHandler::AggType::COUNT: {
for (int i = 0; i < num_point; i++) {
input_x[i] = result_wkb[i]->toPoint()->getX();
input_y[i] = result_wkb[i]->toPoint()->getY();
input_c[i] = result_weight[i].size();
OGRGeometryFactory::destroyGeometry(result_wkb[i]);
for (auto iter = data.begin(); iter != data.end(); iter++) {
auto result_point = iter->first;
auto result_weight = iter->second;
input_x[i] = result_point.first;
input_y[i] = result_point.second;
input_c[i] = result_weight.size();
i++;
}
break;
}
case AggHandler::AggType::SUM: {
for (int i = 0; i < num_point; i++) {
input_x[i] = result_wkb[i]->toPoint()->getX();
input_y[i] = result_wkb[i]->toPoint()->getY();
input_c[i] = accumulate(result_weight[i].begin(), result_weight[i].end(), 0);
OGRGeometryFactory::destroyGeometry(result_wkb[i]);
for (auto iter = data.begin(); iter != data.end(); iter++) {
auto result_point = iter->first;
auto result_weight = iter->second;
input_x[i] = result_point.first;
input_y[i] = result_point.second;
input_c[i] = accumulate(result_weight.begin(), result_weight.end(), 0);
i++;
}
break;
}
case AggHandler::AggType::STDDEV: {
for (int i = 0; i < num_point; i++) {
input_x[i] = result_wkb[i]->toPoint()->getX();
input_y[i] = result_wkb[i]->toPoint()->getY();
T sum = accumulate(result_weight[i].begin(), result_weight[i].end(), 0);
T mean = sum / result_weight[i].size();
for (auto iter = data.begin(); iter != data.end(); iter++) {
auto result_point = iter->first;
auto result_weight = iter->second;
input_x[i] = result_point.first;
input_y[i] = result_point.second;
T sum = accumulate(result_weight.begin(), result_weight.end(), 0);
T mean = sum / result_weight.size();
T accum = 0;
std::for_each(std::begin(result_weight[i]), std::end(result_weight[i]),
std::for_each(std::begin(result_weight), std::end(result_weight),
[&](const T d) { accum += (d - mean) * (d - mean); });
input_c[i] = sqrt(accum / result_weight[i].size());
OGRGeometryFactory::destroyGeometry(result_wkb[i]);
input_c[i] = sqrt(accum / result_weight.size());
i++;
}
break;
}
case AggHandler::AggType::AVG: {
for (int i = 0; i < num_point; i++) {
input_x[i] = result_wkb[i]->toPoint()->getX();
input_y[i] = result_wkb[i]->toPoint()->getY();
T sum_data = accumulate(result_weight[i].begin(), result_weight[i].end(), 0);
input_c[i] = sum_data / result_weight[i].size();
OGRGeometryFactory::destroyGeometry(result_wkb[i]);
for (auto iter = data.begin(); iter != data.end(); iter++) {
auto result_point = iter->first;
auto result_weight = iter->second;
input_x[i] = result_point.first;
input_y[i] = result_point.second;
T sum_data = accumulate(result_weight.begin(), result_weight.end(), 0);
input_c[i] = sum_data / result_weight.size();
i++;
}
break;
}
Expand Down
77 changes: 32 additions & 45 deletions cpp/src/render/2d/fishnet_map/fishnet_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
Expand Down Expand Up @@ -57,8 +58,6 @@ FishNetMap<T>::FishNetMap(uint32_t* input_x, uint32_t* input_y, T* count,

template <typename T>
FishNetMap<T>::~FishNetMap() {
free(vertices_x_);
free(vertices_y_);
free(colors_);
}

Expand All @@ -71,6 +70,7 @@ void set_colors(float* colors, uint32_t* input_x, uint32_t* input_y, T* input_c,
int64_t window_size = width * height;
int cell_size = vega_fishnet_map.cell_size();
int cell_spacing = vega_fishnet_map.cell_spacing();
int block_size = cell_size + cell_spacing;

std::vector<T> weights(num);
memcpy(&weights[0], input_c, num * sizeof(T));
Expand All @@ -83,23 +83,15 @@ void set_colors(float* colors, uint32_t* input_x, uint32_t* input_y, T* input_c,
color_gradient.createDefaultHeatMapGradient();

for (int i = 0; i < num; i++) {
if (input_y[i] * block_size >= height || input_x[i] * block_size >= width) continue;
float value = input_c[i] > max_pix ? 1.0f : (input_c[i] - min_pix) / count_range;
float color_r, color_g, color_b;
color_gradient.getColorAtValue(value, color_r, color_g, color_b);

if (input_y[i] * cell_size >= height || input_x[i] * cell_size >= width) {
continue;
}
int index = cell_size * input_y[i] * width + input_x[i] * cell_size;
for (int m = 0; m < cell_size - cell_spacing; m++) {
for (int n = 0; n < cell_size - cell_spacing; n++) {
int index_in = (index + m * width + n) * 4;
colors[index_in++] = color_r;
colors[index_in++] = color_g;
colors[index_in++] = color_b;
colors[index_in++] = vega_fishnet_map.opacity();
}
}
auto index = i * 4;
colors[index++] = color_r;
colors[index++] = color_g;
colors[index++] = color_b;
colors[index++] = vega_fishnet_map.opacity();
}
}

Expand All @@ -110,50 +102,45 @@ void FishNetMap<T>::DataInit() {
int64_t height = window_params.height();
int64_t window_size = width * height;

colors_ = (float*)malloc(window_size * 4 * sizeof(float));
colors_ = (float*)malloc(num_vertices_ * 4 * sizeof(float));
set_colors(colors_, vertices_x_, vertices_y_, count_, num_vertices_, fishnet_vega_);
vertices_x_ = (uint32_t*)malloc(window_size * sizeof(uint32_t));
vertices_y_ = (uint32_t*)malloc(window_size * sizeof(uint32_t));
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
vertices_x_[i * width + j] = j;
vertices_y_[i * width + j] = i;
}
}
num_vertices_ = window_size;
}

template <typename T>
void FishNetMap<T>::Draw() {
// glEnable(GL_PROGRAM_POINT_SIZE);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);

glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
glLineWidth(20);
#ifdef USE_GPU
glDrawArrays(GL_POINTS, 0, num_vertices_);
glFlush();

glDeleteVertexArrays(1, &VAO_);
glDeleteBuffers(2, VBO_);
#else
glOrtho(0, window()->window_params().width(), 0, window()->window_params().height(), -1,
1);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

int offset = 0;
std::vector<int32_t> vertices(num_vertices_ * 2);
for (auto i = 0; i < num_vertices_; i++) {
vertices[offset++] = vertices_x_[i];
vertices[offset++] = vertices_y_[i];
int width = window()->window_params().width();
int height = window()->window_params().height();
int cell_size = fishnet_vega_.cell_size();
int cell_spacing = fishnet_vega_.cell_spacing();
int block_size = cell_size + cell_spacing;
double spacing = (double)cell_spacing / 2;

glOrtho(0, width, 0, height, -1, 1);
for (int i = 0; i < num_vertices_; i++) {
if (vertices_x_[i] * block_size >= width || vertices_y_[i] * block_size >= height)
continue;
glColor4f(colors_[i * 4], colors_[i * 4 + 1], colors_[i * 4 + 2], colors_[i * 4 + 3]);
glBegin(GL_POLYGON);
double x = vertices_x_[i] * block_size + spacing;
double y = vertices_y_[i] * block_size + spacing;
glVertex2d(x + (double)cell_size, y + (double)cell_size);
glVertex2d(x + (double)cell_size, y);
glVertex2d(x, y);
glVertex2d(x, y + (double)cell_size);
glEnd();
}
glColorPointer(4, GL_FLOAT, 0, colors_);
glVertexPointer(2, GL_INT, 0, &vertices[0]);

glDrawArrays(GL_POINTS, 0, num_vertices_);
glFlush();
glFinish();
#endif
}

Expand Down
37 changes: 37 additions & 0 deletions cpp/src/render/utils/agg/agg_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@ class AggHandler {

static AggType agg_type(std::string type);

struct hash_pair {
template <class T1, class T2>
size_t operator()(const std::pair<T1, T2>& point) const {
auto hash_x = std::hash<T1>{}(point.first);
auto hash_y = std::hash<T2>{}(point.second);
return hash_x ^ hash_y;
}
};

template <typename T>
static std::unordered_map<std::pair<uint32_t, uint32_t>, std::vector<T>, hash_pair>
region_agg(const std::vector<std::string>& wkb_arr, const std::vector<T>& arr_c,
int region_size) {
assert(wkb_arr.size() == arr_c.size());

std::unordered_map<std::pair<uint32_t, uint32_t>, std::vector<T>, hash_pair> result;
for (size_t i = 0; i < wkb_arr.size(); i++) {
std::string wkb = wkb_arr[i];
OGRGeometry* geo;
CHECK_GDAL(OGRGeometryFactory::createFromWkb(wkb.c_str(), nullptr, &geo));
uint32_t x = geo->toPoint()->getX() / region_size;
uint32_t y = geo->toPoint()->getY() / region_size;
OGRGeometryFactory::destroyGeometry(geo);
auto point = std::make_pair(x, y);
if (result.find(point) == result.end()) {
std::vector<T> weight;
weight.emplace_back(arr_c[i]);
result[point] = weight;
} else {
auto& weight = result[point];
weight.emplace_back(arr_c[i]);
}
}

return result;
}

static std::pair<std::vector<OGRGeometry*>, std::vector<int>> weight_agg(
const std::shared_ptr<arrow::Array>& geos) {
auto geo_arr = std::static_pointer_cast<arrow::BinaryArray>(geos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ void VegaFishNetMap::Parse(const std::string& json) {
!JsonTypeCheck(mark_enter["cell_size"]["value"], rapidjson::Type::kNumberType)) {
return;
}
cell_size_ = mark_enter["cell_size"]["value"].GetInt();
cell_size_ = mark_enter["cell_size"]["value"].GetDouble();

if (!JsonLabelCheck(mark_enter, "cell_spacing") ||
!JsonLabelCheck(mark_enter["cell_spacing"], "value") ||
!JsonTypeCheck(mark_enter["cell_spacing"]["value"], rapidjson::Type::kNumberType)) {
return;
}
cell_spacing_ = mark_enter["cell_spacing"]["value"].GetInt();
cell_spacing_ = mark_enter["cell_spacing"]["value"].GetDouble();

// parse opacity
if (!JsonLabelCheck(mark_enter, "opacity") ||
Expand Down
Loading