Started on continus line drawing

This commit is contained in:
Trygve 2024-05-03 20:33:28 +02:00
parent 77467594bd
commit 9d8df3256d
2 changed files with 63 additions and 45 deletions

View File

@ -45,6 +45,8 @@ class Point
int mscase;
Point * next;
Point * previous;
bool endpoint = false;
bool allocated = false;
Point(int x, int y, int mscase){
this -> x = x;
this -> y = y;

View File

@ -52,30 +52,38 @@ std::vector<std::vector<Point>> vector_cellmap(HeightMap* heightmap, int interva
for (int i = 1; i <= num_contours; i++)
{
auto points = produce_cellmap(heightmap, heightmap->min + interval*i);
for (int j = 0; j < points.size(); j++){
Point current = points[j];
Point* current = &points[j];
//std::cout << points_allocated << " " << points.size() << "\n";
for (int k = 0; k < points.size(); k++){
Point candidate = points[k];
if (candidate.next != nullptr) {
if (candidate.x +1 == current.x) {
current.next = &candidate;
}
else if (candidate.x -1 == current.x) {
current.next = &candidate;
}
else if (candidate.y +1 == current.x) {
current.next = &candidate;
}
else if (candidate.y -1 == current.x) {
current.next = &candidate;
}
Point* candidate = &points[k];
if (candidate->previous == nullptr and candidate != current->previous) {
if (candidate->x +1 == current->x && candidate->y == current->y) {
current->next = candidate;
candidate->previous = current;
}
else if (candidate->x -1 == current->x && candidate->y == current->y) {
current->next = candidate;
candidate->previous = current;
}
else if (candidate->y +1 == current->y && candidate->x == current->x) {
current->next = candidate;
candidate->previous = current;
}
else if (candidate->y -1 == current->y && candidate->x == current->x) {
current->next = candidate;
candidate->previous = current;
}
else {
current->endpoint = true;
}
}
}
}
vec_private.push_back(points);
}
#pragma omp critical
@ -162,47 +170,55 @@ void write_output_file(std::vector<std::vector<Point>> all_points, const char *f
for (int j = 0; j < all_points.size(); j++)
{
OGRFeature *feature;
OGRLineString *geometry = new OGRLineString();
feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
feature->SetField( "Name", j );
OGRFeature *feature;
OGRLineString *geometry = new OGRLineString();
feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
feature->SetField( "Name", j );
std::vector<Point> points = all_points[j];
for (int i = 0; i < points.size(); i++)
Point* current = &points[0];
while (true)
{
//int x_raw = i%width;
//int y_raw = i/height;
int x_raw = points[i].x;
int y_raw = points[i].y;
int x_raw = current->x;
int y_raw = current->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_raw << ", " << y_raw << " ";std::cout.flush();
//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_raw ,y_raw );
current->allocated = true;
current = current->previous;
//current sometimes becomes random pointer?????
if (current == nullptr)
{
for (int k = 0; k<points.size(); k++)
{
if (points[k].endpoint and !points[k].allocated)
{
current = &points[k];
}
}
if (current == nullptr){std::cout << "🫠"; break; }
}
}
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
{
printf( "Failed to set geometry.\n" );
exit( 1 );
}
OGRGeometryFactory::destroyGeometry(geometry);
if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
}
OGRFeature::DestroyFeature( feature );
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
{
printf( "Failed to set geometry.\n" );
exit( 1 );
}
OGRGeometryFactory::destroyGeometry(geometry);
if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
}
OGRFeature::DestroyFeature( feature );
break;
}
GDALClose( poDS );