read coords
diff --git a/CMakeLists.txt b/CMakeLists.txt index 56054cd..78b1194 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -1,13 +1,15 @@ cmake_minimum_required(VERSION 3.0.0) project(delaunator VERSION 0.1.0) - +set (CMAKE_CXX_STANDARD 11) if (NOT EXISTS "${PROJECT_SOURCE_DIR}/includes") execute_process(COMMAND bash "-c" "(cd ${PROJECT_SOURCE_DIR} && ./fetch-includes.sh)") endif() # message("PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}") add_executable(delaunator src/main.cpp) +add_library(delaunator_lib src/delaunator.cpp) target_include_directories (delaunator PRIVATE "${PROJECT_SOURCE_DIR}/includes/rapidjson/include") +target_link_libraries(delaunator delaunator_lib) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
diff --git a/src/delaunator.cpp b/src/delaunator.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/delaunator.cpp
diff --git a/src/delaunator.h b/src/delaunator.h new file mode 100644 index 0000000..83e0f87 --- /dev/null +++ b/src/delaunator.h
@@ -0,0 +1,6 @@ +#pragma once + +class Delaunator { + template<int N> + Delaunator(const double (&coords)[N]); +};
diff --git a/src/main.cpp b/src/main.cpp index b4f3518..db79405 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -4,33 +4,62 @@ #include <fstream> #include <string> #include <exception> +#include <vector> +#include <initializer_list> -std::string read_file(const char* filename) { - std::ifstream input_file(filename); - if(input_file.good()) { - std::string json_str( - (std::istreambuf_iterator<char>(input_file)), - std::istreambuf_iterator<char>() - ); - return json_str; - } else { - std::printf("Error reading file %s", filename); - throw std::exception(); +namespace { + std::string read_file(const char* filename) { + std::ifstream input_file(filename); + if(input_file.good()) { + std::string json_str( + (std::istreambuf_iterator<char>(input_file)), + std::istreambuf_iterator<char>() + ); + return json_str; + } else { + std::printf("Error reading file %s", filename); + throw std::exception(); + } + } + + struct Points { + std::vector<double> x_vector; + std::vector<double> y_vector; + }; + + const Points get_geo_json_points(const std::string& json) { + rapidjson::Document document; + if(document.Parse(json.c_str()).HasParseError()) { + std::fprintf(stderr, "Cannot parse JSON"); + throw std::exception(); + } + const rapidjson::Value& features = document["features"]; + std::vector<double> x_vector; + std::vector<double> y_vector; + for(rapidjson::SizeType i = 0; i < features.Size(); i++) { + const rapidjson::Value& coordinates = features[i]["geometry"]["coordinates"]; + const double x = coordinates[0].GetDouble(); + const double y = coordinates[1].GetDouble(); + x_vector.push_back(std::move(x)); + y_vector.push_back(std::move(y)); + // std::printf("coordinates %f %f \n", x, y); + } + Points points = {.x_vector = x_vector, .y_vector = y_vector}; + return points; } } + int main(int, char* argv[]) { const char* filename = argv[1]; std::string json = read_file(filename); - rapidjson::Document document; - if(document.Parse(json.c_str()).HasParseError()) { - std::fprintf(stderr, "Cannot parse JSON"); - throw std::exception(); + const Points points = get_geo_json_points(json); + const int size = points.x_vector.size(); + double coords[size * 2]; + for(int i = 0; i < size; i++) + { + coords[i] = points.x_vector[i]; + coords[2 * i] = points.y_vector[i]; } - if(document.HasMember("features")) { - const rapidjson::Value& features = document["features"]; - for(rapidjson::SizeType i = 0; i < features.Size(); i++) { - std::printf("feature"); - } - } + }