mirror of
				https://gitlab.com/Trygve/contour-creator.git
				synced 2025-11-04 10:50:44 +00:00 
			
		
		
		
	Compare commits
	
		
			No commits in common. "ec0a31db5d34985f3bf90874b2293182e8bdbeab" and "77467594bd879c868efbf289568b79aa1f6d528e" have entirely different histories.
		
	
	
		
			ec0a31db5d
			...
			77467594bd
		
	
		
										
											Binary file not shown.
										
									
								
							| 
		 Before Width: | Height: | Size: 1.5 MiB  | 
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
		 Before Width: | Height: | Size: 88 KiB  | 
@ -5,31 +5,16 @@ author:
 | 
				
			|||||||
- Trygve og Esther
 | 
					- Trygve og Esther
 | 
				
			||||||
 | 
					
 | 
				
			||||||
---
 | 
					---
 | 
				
			||||||
 | 
					# What are contours
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Output of our program:
 | 
					# Output of our program
 | 
				
			||||||

 | 
					pretty pictures
 | 
				
			||||||
 | 
					 | 
				
			||||||
# Marching squares
 | 
					 | 
				
			||||||

 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# The logical flow of our program
 | 
					# The logical flow of our program
 | 
				
			||||||
```cpp
 | 
					flowchart
 | 
				
			||||||
int main(int argc, const char* argv[])
 | 
					
 | 
				
			||||||
{
 | 
					# GDAL
 | 
				
			||||||
    const char* filepath = argv[1];
 | 
					What is it?
 | 
				
			||||||
    HeightMap map(filepath); 
 | 
					 | 
				
			||||||
    map.blur(0.8)
 | 
					 | 
				
			||||||
    auto lines = create_lines(&map, 5);
 | 
					 | 
				
			||||||
    write_output_file(cellmaps, "out.geojson", &map);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Performance
 | 
					# Performance
 | 
				
			||||||
|                    |       Time         |
 | 
					show the benchmarks
 | 
				
			||||||
|--------------------|--------------------|
 | 
					 | 
				
			||||||
|  12 threads        |     41s            |
 | 
					 | 
				
			||||||
|   1 thread         |     116s           |
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Problems we encountered
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@ -43,10 +43,13 @@ class Point
 | 
				
			|||||||
        int x;
 | 
					        int x;
 | 
				
			||||||
        int y;
 | 
					        int y;
 | 
				
			||||||
        int mscase;
 | 
					        int mscase;
 | 
				
			||||||
        bool allocated = false;
 | 
					        Point * next;
 | 
				
			||||||
 | 
					        Point * previous;
 | 
				
			||||||
        Point(int x, int y, int mscase){
 | 
					        Point(int x, int y, int mscase){
 | 
				
			||||||
            this -> x = x;
 | 
					            this -> x = x;
 | 
				
			||||||
            this -> y = y;
 | 
					            this -> y = y;
 | 
				
			||||||
            this -> mscase = mscase;
 | 
					            this -> mscase = mscase;
 | 
				
			||||||
 | 
					            this -> next = nullptr;
 | 
				
			||||||
 | 
					            this -> previous = nullptr;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										151
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										151
									
								
								src/main.cpp
									
									
									
									
									
								
							@ -5,6 +5,7 @@
 | 
				
			|||||||
#include <gdal/ogr_feature.h>
 | 
					#include <gdal/ogr_feature.h>
 | 
				
			||||||
#include <gdal/ogr_geometry.h>
 | 
					#include <gdal/ogr_geometry.h>
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					#include <iterator>
 | 
				
			||||||
#include <ostream>
 | 
					#include <ostream>
 | 
				
			||||||
#include <vector>
 | 
					#include <vector>
 | 
				
			||||||
#include <cstdint>
 | 
					#include <cstdint>
 | 
				
			||||||
@ -21,21 +22,28 @@ std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
 | 
				
			|||||||
    for (int i = 0; i<length; i++) {
 | 
					    for (int i = 0; i<length; i++) {
 | 
				
			||||||
        int y = i/(heightmap->width-1);
 | 
					        int y = i/(heightmap->width-1);
 | 
				
			||||||
        int x = i%(heightmap->width-1);
 | 
					        int x = i%(heightmap->width-1);
 | 
				
			||||||
        uint8_t result = (heightmap->get_pixel(x,y)>z)*8 + 
 | 
					        uint8_t result = (heightmap->get_pixel(x,y)>z) + 
 | 
				
			||||||
        (heightmap->get_pixel(x+1,y)>z)*4 +
 | 
					        (heightmap->get_pixel(x+1,y)>z)*2 +
 | 
				
			||||||
        (heightmap->get_pixel(x+1,y+1)>z)*2 +
 | 
					        (heightmap->get_pixel(x+1,y+1)>z)*4 +
 | 
				
			||||||
        (heightmap->get_pixel(x,y+1)>z);
 | 
					        (heightmap->get_pixel(x,y+1)>z)*8;
 | 
				
			||||||
        if (result != 0 and result != 15 ) {
 | 
					        if (result != 0 and result != 15 ) {
 | 
				
			||||||
            points.push_back(Point(x, y, result));
 | 
					            points.push_back(Point(x, y, result));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    return points;
 | 
					    return points;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval)
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::vector<std::vector<Point>> vector_cellmap(HeightMap* heightmap, int interval)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int num_contours = (heightmap->max - heightmap->min)/interval;
 | 
					    int num_contours = (heightmap->max - heightmap->min)/interval;
 | 
				
			||||||
    std::vector<std::vector<Point>> vector_contours;
 | 
					    std::vector<std::vector<Point>> vector_contours;
 | 
				
			||||||
 | 
					    omp_set_num_threads(12);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #pragma omp parallel
 | 
					    #pragma omp parallel
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@ -44,67 +52,27 @@ std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval)
 | 
				
			|||||||
        for (int i = 1; i <= num_contours; i++) 
 | 
					        for (int i = 1; i <= num_contours; i++) 
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            auto points = 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 j = 0; j < points.size(); j++){
 | 
				
			||||||
 | 
					                Point current = points[j];
 | 
				
			||||||
 | 
					                //std::cout << points_allocated << " " << points.size() << "\n";
 | 
				
			||||||
                for (int k = 0; k < points.size(); k++){
 | 
					                for (int k = 0; k < points.size(); k++){
 | 
				
			||||||
                    Point* candidate = &points[k];
 | 
					                    Point candidate = points[k];
 | 
				
			||||||
                    if (!candidate->allocated) {
 | 
					                    if (candidate.next != nullptr) {
 | 
				
			||||||
                        if (candidate->x == x +1 && candidate->y == y) {
 | 
					                        if (candidate.x +1 == current.x) {
 | 
				
			||||||
                            candidate->allocated = true;
 | 
					                            current.next = &candidate;
 | 
				
			||||||
                            x = candidate->x;
 | 
					 | 
				
			||||||
                            y = candidate->y;
 | 
					 | 
				
			||||||
                            line.push_back(*candidate);
 | 
					 | 
				
			||||||
                            points_allocated++;
 | 
					 | 
				
			||||||
                            break;
 | 
					 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        else if (candidate->x == x -1 && candidate->y == y) {
 | 
					                        else if (candidate.x -1 == current.x) {
 | 
				
			||||||
                            x = candidate->x;
 | 
					                            current.next = &candidate;
 | 
				
			||||||
                            y = candidate->y;
 | 
					 | 
				
			||||||
                            candidate->allocated = true;
 | 
					 | 
				
			||||||
                            line.push_back(*candidate);
 | 
					 | 
				
			||||||
                            points_allocated++;
 | 
					 | 
				
			||||||
                            break;
 | 
					 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        else if (candidate->x == x && candidate->y == y + 1) {
 | 
					                        else if (candidate.y +1 == current.x) {
 | 
				
			||||||
                            x = candidate->x;
 | 
					                            current.next = &candidate;
 | 
				
			||||||
                            y = candidate->y;
 | 
					 | 
				
			||||||
                            candidate->allocated = true;
 | 
					 | 
				
			||||||
                            line.push_back(*candidate);
 | 
					 | 
				
			||||||
                            points_allocated++;
 | 
					 | 
				
			||||||
                            break;
 | 
					 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                        else if (candidate->x == x && candidate->y == y - 1) {
 | 
					                        else if (candidate.y -1 == current.x) {
 | 
				
			||||||
                            x = candidate->x;
 | 
					                            current.next = &candidate;
 | 
				
			||||||
                            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;
 | 
					 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					                        
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -112,6 +80,7 @@ std::vector<std::vector<Point>> create_lines(HeightMap* heightmap, int interval)
 | 
				
			|||||||
        #pragma omp critical
 | 
					        #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;
 | 
					        return vector_contours;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -131,7 +100,7 @@ constexpr std::tuple<double, double, double, double> marching_squares_lookup(int
 | 
				
			|||||||
    switch (mcase) {
 | 
					    switch (mcase) {
 | 
				
			||||||
        case 1: return {0, 0.5, 0.5, 0};
 | 
					        case 1: return {0, 0.5, 0.5, 0};
 | 
				
			||||||
        case 2: return {1, 0.5, 0.5, 0};
 | 
					        case 2: return {1, 0.5, 0.5, 0};
 | 
				
			||||||
        case 3: return {0, 0.5, 1, 0.5};
 | 
					        case 3: return {0, 0.5, 1, 0};
 | 
				
			||||||
        case 4: return {0.5, 1, 1, 0.5};
 | 
					        case 4: return {0.5, 1, 1, 0.5};
 | 
				
			||||||
        case 5: return {0, 0, 0, 0}; //FIXME
 | 
					        case 5: return {0, 0, 0, 0}; //FIXME
 | 
				
			||||||
        case 6: return {0.5, 1, 0.5, 0};
 | 
					        case 6: return {0.5, 1, 0.5, 0};
 | 
				
			||||||
@ -171,7 +140,7 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    OGRLayer *poLayer;
 | 
					    OGRLayer *poLayer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    poLayer = poDS->CreateLayer( "contours", &heightmap->reference_system, wkbLineString, NULL );
 | 
					    poLayer = poDS->CreateLayer( "contours", /*&(cellmaps[0]).reference_system*/ NULL, wkbLineString, NULL );
 | 
				
			||||||
    if( poLayer == NULL )
 | 
					    if( poLayer == NULL )
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        printf( "Layer creation failed.\n" );
 | 
					        printf( "Layer creation failed.\n" );
 | 
				
			||||||
@ -193,35 +162,49 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    for (int j = 0; j < all_points.size(); j++)
 | 
					    for (int j = 0; j < all_points.size(); j++)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        OGRFeature *feature;
 | 
					            OGRFeature *feature;
 | 
				
			||||||
        OGRLineString *geometry = new OGRLineString();
 | 
					            OGRLineString *geometry = new OGRLineString();
 | 
				
			||||||
        feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
 | 
					            feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
 | 
				
			||||||
        feature->SetField( "Name", j );
 | 
					            feature->SetField( "Name", j );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::vector<Point> points = all_points[j];
 | 
					        std::vector<Point> points = all_points[j];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (int k = 0; k < points.size(); k++)
 | 
					        for (int i = 0; i < points.size(); i++) 
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            int x_raw = points[k].x;
 | 
					
 | 
				
			||||||
            int y_raw = points[k].y;
 | 
					            //int x_raw = i%width;
 | 
				
			||||||
 | 
					            //int y_raw = i/height;
 | 
				
			||||||
 | 
					            int x_raw = points[i].x;
 | 
				
			||||||
 | 
					            int y_raw = points[i].y;
 | 
				
			||||||
            auto [x, y] = local_to_projected(heightmap->geotransform, x_raw, y_raw);
 | 
					            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) );
 | 
					            //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 ); 
 | 
				
			||||||
            geometry->setPoint(geometry->getNumPoints(),x ,y );
 | 
					            geometry->setPoint(geometry->getNumPoints(),x ,y );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ( feature->SetGeometry(geometry) != OGRERR_NONE)
 | 
					                    if ( feature->SetGeometry(geometry) != OGRERR_NONE)
 | 
				
			||||||
        {
 | 
					            {
 | 
				
			||||||
            printf( "Failed to set geometry.\n" );
 | 
					                printf( "Failed to set geometry.\n" );
 | 
				
			||||||
            exit( 1 );
 | 
					                exit( 1 );
 | 
				
			||||||
        }
 | 
					            }
 | 
				
			||||||
        OGRGeometryFactory::destroyGeometry(geometry);
 | 
					            OGRGeometryFactory::destroyGeometry(geometry);
 | 
				
			||||||
        if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
 | 
					            if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
 | 
				
			||||||
        {
 | 
					            {
 | 
				
			||||||
            printf( "Failed to create feature in shapefile.\n" );
 | 
					                printf( "Failed to create feature in shapefile.\n" );
 | 
				
			||||||
            exit( 1 );
 | 
					                exit( 1 );
 | 
				
			||||||
        }
 | 
					            }
 | 
				
			||||||
        OGRFeature::DestroyFeature( feature );
 | 
					            OGRFeature::DestroyFeature( feature );  
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    GDALClose( poDS );
 | 
					    GDALClose( poDS );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -235,6 +218,8 @@ int main(int argc, const char* argv[])
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    map.blur(0.8);
 | 
					    map.blur(0.8);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    auto lines = create_lines(&map, 5);
 | 
					    auto cellmap = produce_cellmap(&map, 40);
 | 
				
			||||||
    write_output_file(lines, "out.geojson", &map);
 | 
					    
 | 
				
			||||||
 | 
					    auto cellmaps = vector_cellmap(&map, 5);
 | 
				
			||||||
 | 
					    write_output_file(cellmaps, "out.geojson", &map);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user