19 Commits

Author SHA1 Message Date
43f8b78830 Implemented cli 2024-05-07 13:29:49 +02:00
08255875c1 Added the argh library as a subproject 2024-05-07 11:40:33 +02:00
ec0a31db5d Small typo 2024-05-07 11:00:49 +02:00
esther
32a0b18f8e Merge branch 'main' of gitlab.com:Trygve/contour-creator 2024-05-07 10:58:15 +02:00
esther
ab96f9477e added some comments 2024-05-07 10:48:48 +02:00
790cf44f8b bugfix 2024-05-07 10:35:31 +02:00
df7cac1095 cleanup 2024-05-07 10:34:29 +02:00
93fcbc6a39 Merge branch 'drawing-lines' 2024-05-07 10:25:01 +02:00
95649f005b ITS WORKING (somewhat) 2024-05-06 23:50:11 +02:00
1ec3e8c778 slides 2024-05-06 14:16:04 +02:00
c535eec958 Debugging without omp 2024-05-06 11:37:10 +02:00
4f709d3b07 Use linked list to store the order of the points 2024-05-05 16:53:00 +02:00
4cd4f773bf Fixed stupid mistake in for loop 2024-05-03 20:54:45 +02:00
9d8df3256d Started on continus line drawing 2024-05-03 20:33:28 +02:00
esther
77467594bd improved vector_cellmap function 2024-05-02 21:02:48 +02:00
0188b9e29d Merge branch 'main' into HEAD 2024-04-30 15:12:48 +02:00
b800ceaf98 Changed file wrriting to use list of points 2024-04-30 15:11:44 +02:00
1dcfb0e737 Changed file wrriting to use list of points 2024-04-30 15:09:18 +02:00
esther
228945a767 tried to implement finding contour lines 2024-04-30 15:07:04 +02:00
9 changed files with 3266 additions and 75 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "extern/argh"]
path = extern/argh
url = https://github.com/adishavit/argh.git

View File

@@ -1,15 +1,22 @@
cmake_minimum_required(VERSION 3.20)
project(
contour-creator
LANGUAGES CXX)
find_package(GDAL CONFIG REQUIRED)
add_executable(${PROJECT_NAME}
src/HeightMap.cpp src/CellMap.cpp src/main.cpp
)
# Argh is a simple argrument parser
add_subdirectory(extern/argh)
target_link_libraries(${PROJECT_NAME} PRIVATE argh)
# Gdal is used for geodata IO
find_package(GDAL CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE GDAL::GDAL)
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif()
target_link_libraries(${PROJECT_NAME} GDAL::GDAL)

View File

@@ -3,4 +3,4 @@ rm -rf build
mkdir build
cmake -DCMAKE_BUILD_TYPE=Debug -B build
cmake --build build --parallel
build/contour-creator 'example_files/crop.tif'
build/contour-creator --interval=1 --blur example_files/crop.tif

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 88 KiB

View File

@@ -5,16 +5,31 @@ author:
- Trygve og Esther
---
# What are contours
# Output of our program
pretty pictures
# Output of our program:
![Contour map of Ås](kurver_ås.png)
# Marching squares
![By Nicoguaro; Adroitwhiz - File:Marching_squares_algorithm.svg, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=143418710](ms_wikipedia.svg)
# The logical flow of our program
flowchart
# GDAL
What is it?
```cpp
int main(int argc, const char* argv[])
{
const char* filepath = argv[1];
HeightMap map(filepath);
map.blur(0.8)
auto lines = create_lines(&map, 5);
write_output_file(cellmaps, "out.geojson", &map);
}
```
# Performance
show the benchmarks
| | Time |
|--------------------|--------------------|
| 12 threads | 41s |
| 1 thread | 116s |
# Problems we encountered

1
extern/argh vendored Submodule

Submodule extern/argh added at 431bf323ac

View File

@@ -43,10 +43,10 @@ class Point
int x;
int y;
int mscase;
bool allocated = false;
Point(int x, int y, int mscase){
this -> x = x;
this -> y = y;
this -> mscase = mscase;
};
};

View File

@@ -12,35 +12,31 @@
#include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h>
#include <omp.h>
#include "argh.h"
std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
{
int length = (heightmap->width-1)*(heightmap->height-1);
int length = (heightmap->width-1)*(heightmap->height-1); // Defining length of vector
uint8_t *cells = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length);
std::vector<Point> points;
std::vector<Point> points; // Initiating a vector of points
for (int i = 0; i<length; i++) {
int y = i/(heightmap->width-1);
int x = i%(heightmap->width-1);
uint8_t result = (heightmap->get_pixel(x,y)>z) +
(heightmap->get_pixel(x+1,y)>z)*2 +
(heightmap->get_pixel(x+1,y+1)>z)*4 +
(heightmap->get_pixel(x,y+1)>z)*8;
uint8_t result = (heightmap->get_pixel(x,y)>z)*8 +
(heightmap->get_pixel(x+1,y)>z)*4 +
(heightmap->get_pixel(x+1,y+1)>z)*2 +
(heightmap->get_pixel(x,y+1)>z);
if (result != 0 and result != 15 ) {
points.push_back(Point(x, y, result));
};
}
return points;
}
std::vector<std::vector<Point>> vector_cellmap(HeightMap* heightmap, int interval)
std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval)
{
int num_contours = (heightmap->max - heightmap->min)/interval;
std::vector<std::vector<Point>> vector_contours;
omp_set_num_threads(12);
#pragma omp parallel
{
@@ -48,11 +44,75 @@ std::vector<std::vector<Point>> vector_cellmap(HeightMap* heightmap, int interva
#pragma omp for
for (int i = 1; i <= num_contours; i++)
{
vec_private.push_back(produce_cellmap(heightmap, heightmap->min + interval*i));
auto points = produce_cellmap(heightmap, heightmap->min + interval*i);
std::vector<Point> line;
int x;
int y;
int points_allocated = 0;
x = points[0].x;
y = points[0].y;
points[0].allocated = true;
line.push_back(points[0]);
for (int j = 0; j < points.size(); j++){
for (int k = 0; k < points.size(); k++){
Point* candidate = &points[k];
if (!candidate->allocated) {
if (candidate->x == x +1 && candidate->y == y) {
candidate->allocated = true;
x = candidate->x;
y = candidate->y;
line.push_back(*candidate);
points_allocated++;
break;
}
else if (candidate->x == x -1 && candidate->y == y) {
x = candidate->x;
y = candidate->y;
candidate->allocated = true;
line.push_back(*candidate);
points_allocated++;
break;
}
else if (candidate->x == x && candidate->y == y + 1) {
x = candidate->x;
y = candidate->y;
candidate->allocated = true;
line.push_back(*candidate);
points_allocated++;
break;
}
else if (candidate->x == x && candidate->y == y - 1) {
x = candidate->x;
y = candidate->y;
candidate->allocated = true;
line.push_back(*candidate);
points_allocated++;
break;
}
}
}
if (j > points_allocated)
{
vec_private.push_back(line);
line.clear();
//std::cout << points.size() << " ";
for (int k = 0; k < points.size(); k++){
Point* candidate = &points[k];
if (!candidate->allocated)
{
line.push_back(*candidate);
x = candidate->x;
y = candidate->y;
points_allocated++;
break;
}
}
}
}
}
#pragma omp critical
vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end());
vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end());
}
return vector_contours;
}
@@ -72,7 +132,7 @@ constexpr std::tuple<double, double, double, double> marching_squares_lookup(int
switch (mcase) {
case 1: return {0, 0.5, 0.5, 0};
case 2: return {1, 0.5, 0.5, 0};
case 3: return {0, 0.5, 1, 0};
case 3: return {0, 0.5, 1, 0.5};
case 4: return {0.5, 1, 1, 0.5};
case 5: return {0, 0, 0, 0}; //FIXME
case 6: return {0.5, 1, 0.5, 0};
@@ -86,7 +146,7 @@ constexpr std::tuple<double, double, double, double> marching_squares_lookup(int
case 14: return {0, 0.5, 0.5, 0};
};
}
void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
void write_output_file(std::vector<std::vector<Point>> all_points, const char *filepath, HeightMap* heightmap)
{
const char *pszDriverName = "GeoJSON";
@@ -112,7 +172,7 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "contours", &(cellmaps[0]).reference_system, wkbLineString, NULL );
poLayer = poDS->CreateLayer( "contours", &heightmap->reference_system, wkbLineString, NULL );
if( poLayer == NULL )
{
printf( "Layer creation failed.\n" );
@@ -128,26 +188,27 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
printf( "Creating Name field failed.\n" );
exit( 1 );
}
for (int j = 0; j < cellmaps.size(); j++)
{
CellMap* cellmap = &cellmaps[j];
int width = heightmap->width -1;
int height = heightmap->height -1;
int size = width * height;
for (int i = 0; i < cellmap->height*cellmap->width; i++)
{
if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15)
for (int j = 0; j < all_points.size(); j++)
{
OGRFeature *feature;
OGRLineString *geometry = new OGRLineString();
feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
feature->SetField( "Name", j );
int x_raw = i%cellmap->width;
int y_raw = i/cellmap->width;
auto [x, y] = local_to_projected(cellmap->geotransform, x_raw, y_raw);
auto [x_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) );
//std::cout << x << ", " << y << " ";
geometry->setPoint( geometry->getNumPoints(), x+x_tr*0.25, y+y_tr*0.25 );
geometry->setPoint( geometry->getNumPoints(), x+x_bl*0.25, y+y_bl*0.25 );
std::vector<Point> points = all_points[j];
for (int k = 0; k < points.size(); k++)
{
int x_raw = points[k].x;
int y_raw = points[k].y;
auto [x, y] = local_to_projected(heightmap->geotransform, x_raw, y_raw);
//auto [x_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) );
geometry->setPoint(geometry->getNumPoints(),x ,y );
}
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
{
@@ -162,27 +223,39 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
}
OGRFeature::DestroyFeature( feature );
}
}
}
GDALClose( poDS );
}
int main(int argc, const char* argv[])
{
const char* filepath = argv[1];
HeightMap map(filepath);
argh::parser cmdl(argv);
std::cout << "x: " << map.width << " y: " << map.height << "\n";
std::cout << "max: " << map.max << " min: " << map.min << "\n";
map.blur(0.8);
auto cellmap = produce_cellmap(&map, 40);
auto cellmaps = vector_cellmap(&map, 5);
//write_output_file(cellmaps, "out.geojson");
if (cmdl[{ "-h", "--help" }])
{
std::cout << "Usage:\n"
<< "contour_creator [OPTIONS] <input_file>\n"
<< "-o; --output <FILENAME.geojson> - File to write output to (Default: contours.geojson)\n"
<< "-i; --interval <int> - Set the interval between contours (Default: 5)\n"
<< "-b; --blur - Blur the image\n"
<< "--stats - Print statistical information about the heightmap\n";
exit(0);
}
int interval;
cmdl({"-i", "--interval"}, 5) >> interval;
if (interval <= 0)
std::cerr << "Interval must be valid positive integer!" << "\n";
std::string output_file;
cmdl({"-o", "--output"}, "contours.geojson") >> output_file;
std::string input_file;
cmdl(1) >> input_file;
HeightMap map(input_file.c_str());
if (cmdl[{"-b", "--blur"}])
map.blur(0.8);
auto lines = create_lines(&map, interval);
write_output_file(lines, output_file.c_str(), &map);
std::cout << "Contours written to " << output_file << " 🗺️\n";
}