c++ 扫描线填充算法实现,对 DEM 数据进行置平

ScanlineFilling.h
#include <iostream>
#include <vector>
class ScanlineFilling
{
	public:
		class Edge {
		public:
			int x1, y1, x2, y2, dx, dy;
			Edge(int x1, int y1, int x2, int y2);
		};

	private:
		std::vector<Edge> edgeTable;
		int  getYmin();
		int  getYmax();
		bool  isActive(Edge edge, int currentY);
		void  updateActiveEdges(std::vector<Edge>& activeEdges, int y);
		void  findIntersections(std::vector<int>& intersections, std::vector <Edge> activeEdges, int currentY);
	public:
		// 多边形填充
		// 参数:matrix:		要填充的二维数组,
		//		x,y:		数组的行列,
		//		position:	多边形的坐标[x,y],
		//		len:		多边形坐标的个数(从起点到终点的个数,其中起点==终点),
		//		fillValue:	填充的值
		void fill(int** matrix, int x, int y, int** position, int len, int fillValue);
};

ScanlineFilling.cpp
#include "ScanlineFilling.h"
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

ScanlineFilling::Edge::Edge(int x1, int y1, int x2, int y2) {
	this->x1 = x1;
	this->y1 = y1;
	this->x2 = x2;
	this->y2 = y2;
	this->dx = x2 - x1;
	this->dy = y2 - y1;
}

int ScanlineFilling::getYmin() {
	int yMin = edgeTable.front().y1;
	for (Edge edge : edgeTable) {
		if (edge.y1 < yMin)
			yMin = edge.y1;
	}
	return yMin;
}

int ScanlineFilling::getYmax() {
	int yMax = edgeTable.front().y1;
	for (Edge edge : edgeTable) {
		if (edge.y1 > yMax)
			yMax = edge.y1;
	}
	return yMax;
}

bool ScanlineFilling::isActive(Edge edge, int currentY) {
	return edge.y1 < currentY && currentY <= edge.y2 || edge.y1 >= currentY && currentY > edge.y2;
}

void ScanlineFilling::updateActiveEdges(std::vector<Edge>& activeEdges, int y) {
	for (Edge edge : edgeTable) {
		if (isActive(edge, y)) {
			activeEdges.push_back(edge);
		}
	}
}

void ScanlineFilling::findIntersections(std::vector<int>& intersections, std::vector<Edge> activeEdges, int currentY) {
	for (Edge edge : activeEdges) {
		int x = edge.x1 + ((currentY - edge.y1) * (edge.dx)) / (edge.dy);
		intersections.push_back(x);
	}
}

// 多边形填充
// 参数:matrix:		要填充的二维数组,
//		x,y:		数组的行列,
//		position:	多边形的坐标[x,y],
//		len:		多边形坐标的个数(从起点到终点的个数,其中起点==终点),
//		fillValue:	填充的值
void ScanlineFilling::fill(int** matrix, int x, int y, int** position, int len,int fillValue) {
	vector<Edge> activeEdges;
	vector<int> intersections;
	int firstX, firstY;
	edgeTable.clear();
	// 获取要填充的多边形的边
	for (int i = 0; i < len; i++) {
		if (i == 0) {
			firstX = position[i][0];
			firstY = position[i][1];
			continue;
		}
		edgeTable.push_back(*(new Edge(firstX, firstY, position[i][0], position[i][1])));
		firstX = position[i][0];
		firstY = position[i][1];
	}
	int yMin = getYmin();
	int yMax = getYmax();
	for (int y = yMin; y < yMax; y++) {
		activeEdges.clear();
		intersections.clear();
		updateActiveEdges(activeEdges, y);
		findIntersections(intersections, activeEdges, y);
		// 升序
		sort(intersections.begin(),intersections.end());
		for (int i = 0; i < intersections.size(); i += 2) {
			int x1 = intersections[i];
			if ((i + 1) >= intersections.size()) {
				break;
			}
			int x2 = intersections[i + 1];
			for (int k = x1; k < x2; k++) {
				matrix[k][y] = fillValue;
			}
		}
	}
}

二维矩阵填充测试:

int main() {

	int** matrix = new int* [10];
	for (int i = 0; i < 10; i++) {
		matrix[i] = new int[10];
		for (int j = 0; j < 10; j++) {
			matrix[i][j] = 0;
		}
	}

	int** position = new int* [4];
	position[0] = new int[2];
	position[1] = new int[2];
	position[2] = new int[2];
	position[3] = new int[2];
	// 起点
	position[0][0] = 2;
	position[0][1] = 2;
	// 中间点
	position[1][0] = 2;
	position[1][1] = 8;
	position[2][0] = 8;
	position[2][1] = 7;
	// 终点
	position[3][0] = 2;
	position[3][1] = 2;
	ScanlineFilling* sc = new ScanlineFilling();
	sc->fill(matrix, 10, 10, position, 4, 1);
	// 打印填充结果
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			cout << matrix[i][j] << "   ";
		}
		cout << endl;
	}
	return 0;
}

在这里插入图片描述

对 DEM 数据进行置平

ScanlineFilling* sc = new ScanlineFilling();
// greyValue: dem 高程值二维矩阵
// X,Y:		   dem 高程值二维矩阵在 x(lat),y(lon)方向上的像素个数
// position:   要填充的多边形的坐标(第一个点和最后一个点相同),
//			   用 shp 文件置平,经纬度要转换为 dem 高程值的索引位置
// pointCount: 多边形坐标个数, position.length
// minGreyValue: 要填充的高程值
sc->fill(greyValue, X, Y, position, pointCount, minGreyValue);

填充结果:

原始数据

填充的 shp 位置
填充结果
填充结果三维显示

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
填方挖方算法是指根据地表高程数据(如DEM数据)计算地形表面被填方或挖方的体积。以下是一个利用 C++DEM 数据进行填方挖方算法的基本步骤: 1. 读取 DEM 数据文件并解析高程数据。 2. 定义填方挖方的高度阈值,计算出 DEM 数据每个像素点的填方或挖方高度(即 DEM 数据该点高度减去阈值)。 3. 对于每个像素点,根据其填方或挖方高度计算其对总体积的贡献。 4. 对所有像素点的贡献值进行累加,得到填方或挖方的总体积。 以下是一个 C++ 示例代码,用于计算 DEM 数据的填方挖方体积: ```c++ #include <iostream> #include <fstream> #include <vector> using namespace std; const double THRESHOLD = 10.0; // 高度阈值 int main() { // 读取 DEM 数据文件 ifstream fin("dem.txt"); int rows, cols; fin >> rows >> cols; vector<vector<double>> data(rows, vector<double>(cols)); for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { fin >> data[i][j]; } } fin.close(); // 计算填方挖方体积 double fill_volume = 0.0, cut_volume = 0.0; for (int i = 0; i < rows - 1; ++i) { for (int j = 0; j < cols - 1; ++j) { double height1 = data[i][j] - THRESHOLD; double height2 = data[i][j+1] - THRESHOLD; double height3 = data[i+1][j+1] - THRESHOLD; double height4 = data[i+1][j] - THRESHOLD; double avg_height = (height1 + height2 + height3 + height4) / 4.0; double volume = (avg_height >= 0.0 ? 1.0 : -1.0) * avg_height; if (avg_height >= 0.0) { fill_volume += volume; } else { cut_volume += volume; } } } // 输出结果 cout << "Fill volume: " << fill_volume << endl; cout << "Cut volume: " << cut_volume << endl; return 0; } ``` 在该示例代码,我们首先从 `dem.txt` 文件读取 DEM 数据,并将其存储在二维向量 `data` 。然后,我们遍历 DEM 数据的每个像素点,计算出其填方或挖方高度,并根据其高度贡献计算出填方或挖方的总体积。最后,我们输出填方和挖方的总体积。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值