mirror of
				https://gitlab.com/Trygve/contour-creator.git
				synced 2025-10-31 17:00:46 +00:00 
			
		
		
		
	Compare commits
	
		
			No commits in common. "0188b9e29d5f52a2126e05a456c6d1ba47c9399f" and "d046e18c43d883eb7d60cf77aac60a8782044a6c" have entirely different histories.
		
	
	
		
			0188b9e29d
			...
			d046e18c43
		
	
		
							
								
								
									
										84
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										84
									
								
								src/main.cpp
									
									
									
									
									
								
							| @ -36,8 +36,6 @@ std::vector<Point> produce_cellmap(HeightMap* heightmap, float z) | |||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| std::vector<std::vector<Point>> vector_cellmap(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; | ||||||
| @ -50,44 +48,9 @@ std::vector<std::vector<Point>> vector_cellmap(HeightMap* heightmap, int interva | |||||||
|         #pragma omp for |         #pragma omp for | ||||||
|         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) ; |             vec_private.push_back(produce_cellmap(heightmap, heightmap->min + interval*i)); | ||||||
|             std::vector<Point> line; |  | ||||||
|             line.push_back(points.back()); |  | ||||||
|             points.pop_back(); |  | ||||||
| 
 |  | ||||||
|             while (points.size() != 0){ |  | ||||||
|                 Point current_point = points.back(); |  | ||||||
|                 for (int j=0; j < points.size(); j++){ |  | ||||||
|                     Point next = points[j]; |  | ||||||
|                     if (&next != &points.back()) { |  | ||||||
|                         if (next.x +1 == current_point.x) { |  | ||||||
|                             line.push_back(next); |  | ||||||
|                             points.erase(points.begin()+j); |  | ||||||
|                         } |  | ||||||
|                         else if (next.x -1 == current_point.x) { |  | ||||||
|                             line.push_back(next); |  | ||||||
|                             points.erase(points.begin()+j); |  | ||||||
|                         } |  | ||||||
|                         else if (next.y +1 == current_point.y) { |  | ||||||
|                             line.push_back(next); |  | ||||||
|                             points.erase(points.begin()+j); |  | ||||||
|                         } |  | ||||||
|                         else if (next.y -1 == current_point.y) { |  | ||||||
|                             line.push_back(next); |  | ||||||
|                             points.erase(points.begin()+j); |  | ||||||
|                         } |  | ||||||
|                         else { |  | ||||||
|                             vec_private.push_back(line); |  | ||||||
|                             std::vector<Point> line; |  | ||||||
|                             line.push_back(points.back()); |  | ||||||
|                             points.pop_back(); |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
|         #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()); | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| @ -123,7 +86,7 @@ constexpr std::tuple<double, double, double, double> marching_squares_lookup(int | |||||||
|         case 14: return {0, 0.5, 0.5, 0}; |         case 14: return {0, 0.5, 0.5, 0}; | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| void write_output_file(std::vector<std::vector<Point>> all_points, const char *filepath, HeightMap* heightmap) | void write_output_file(std::vector<CellMap> cellmaps, const char *filepath) | ||||||
| { | { | ||||||
| 
 | 
 | ||||||
|     const char *pszDriverName = "GeoJSON"; |     const char *pszDriverName = "GeoJSON"; | ||||||
| @ -149,7 +112,7 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f | |||||||
| 
 | 
 | ||||||
|     OGRLayer *poLayer; |     OGRLayer *poLayer; | ||||||
| 
 | 
 | ||||||
|     poLayer = poDS->CreateLayer( "contours", /*&(cellmaps[0]).reference_system*/ NULL, wkbLineString, NULL ); |     poLayer = poDS->CreateLayer( "contours", &(cellmaps[0]).reference_system, wkbLineString, NULL ); | ||||||
|     if( poLayer == NULL ) |     if( poLayer == NULL ) | ||||||
|     { |     { | ||||||
|         printf( "Layer creation failed.\n" ); |         printf( "Layer creation failed.\n" ); | ||||||
| @ -165,39 +128,27 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f | |||||||
|         printf( "Creating Name field failed.\n" ); |         printf( "Creating Name field failed.\n" ); | ||||||
|         exit( 1 ); |         exit( 1 ); | ||||||
|     } |     } | ||||||
|     int width = heightmap->width -1; |     for (int j = 0; j < cellmaps.size(); j++) | ||||||
|     int height = heightmap->height -1; |     { | ||||||
|     int size = width * height; |         CellMap* cellmap = &cellmaps[j]; | ||||||
| 
 | 
 | ||||||
|     for (int j = 0; j < all_points.size(); j++) |         for (int i = 0; i < cellmap->height*cellmap->width; i++)  | ||||||
|  |         { | ||||||
|  |             if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15) | ||||||
|             { |             { | ||||||
|                 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 ); | ||||||
| 
 |                 int x_raw = i%cellmap->width; | ||||||
| 
 |                 int y_raw = i/cellmap->width; | ||||||
|         std::vector<Point> points = all_points[j]; |                 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) ); | ||||||
|         for (int i = 0; i < points.size(); i++)  |  | ||||||
|         { |  | ||||||
| 
 |  | ||||||
|             //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_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) );
 |  | ||||||
|                 //std::cout << x << ", " << y << "  ";
 |                 //std::cout << x << ", " << y << "  ";
 | ||||||
|             //geometry->setPoint( geometry->getNumPoints(), x+x_tr*0.25, y+y_tr*0.25 ); 
 |                 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+x_bl*0.25, y+y_bl*0.25 );  | ||||||
|             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" ); | ||||||
| @ -210,6 +161,9 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f | |||||||
|                     exit( 1 ); |                     exit( 1 ); | ||||||
|                 } |                 } | ||||||
|                 OGRFeature::DestroyFeature( feature );   |                 OGRFeature::DestroyFeature( feature );   | ||||||
|  |                     } | ||||||
|  |              | ||||||
|  |         } | ||||||
|          |          | ||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| @ -230,5 +184,5 @@ int main(int argc, const char* argv[]) | |||||||
|     auto cellmap = produce_cellmap(&map, 40); |     auto cellmap = produce_cellmap(&map, 40); | ||||||
|      |      | ||||||
|     auto cellmaps = vector_cellmap(&map, 5); |     auto cellmaps = vector_cellmap(&map, 5); | ||||||
|     write_output_file(cellmaps, "out.geojson", &map); |     //write_output_file(cellmaps, "out.geojson");
 | ||||||
| } | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user