You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求无框架纯JavaScript实现IDW插值算法(高程点云转网格)

Vanilla JavaScript IDW Interpolation for Point Cloud to Grid Conversion

Hey there! I’ve got a straightforward vanilla JavaScript implementation of the Inverse Distance Weighted (IDW) interpolation algorithm that’s perfect for converting unstructured point cloud elevation data into a grid—no Node.js or external frameworks required. Let me break this down for you:

Core IDW Function

This function takes your point cloud data, a target grid point, and optional parameters to calculate the interpolated elevation:

function idwInterpolation(targetPoint, pointCloud, power = 2, distanceThreshold = Infinity) {
    let totalWeight = 0;
    let weightedSum = 0;

    for (const point of pointCloud) {
        // Calculate Euclidean distance between target and point cloud point
        const dx = targetPoint.x - point.x;
        const dy = targetPoint.y - point.y;
        const distance = Math.sqrt(dx * dx + dy * dy);

        // Skip points beyond threshold or exactly at target (avoid division by zero)
        if (distance > distanceThreshold || distance === 0) {
            continue;
        }

        // Calculate weight (inverse of distance raised to the power)
        const weight = 1 / Math.pow(distance, power);
        weightedSum += point.z * weight;
        totalWeight += weight;
    }

    // If no valid points found, return null or handle as needed
    return totalWeight > 0 ? weightedSum / totalWeight : null;
}

How to Generate a Full Grid

To convert your point cloud into a structured grid, you’ll need to define your grid bounds and resolution, then run the IDW function for each grid cell:

function generateElevationGrid(pointCloud, minX, maxX, minY, maxY, gridSize) {
    const grid = [];
    const cellSizeX = (maxX - minX) / gridSize;
    const cellSizeY = (maxY - minY) / gridSize;

    for (let y = 0; y < gridSize; y++) {
        const row = [];
        const gridY = minY + y * cellSizeY + cellSizeY / 2; // Sample at cell center

        for (let x = 0; x < gridSize; x++) {
            const gridX = minX + x * cellSizeX + cellSizeX / 2;
            const elevation = idwInterpolation(
                { x: gridX, y: gridY },
                pointCloud,
                2, // Power parameter (adjust based on your data)
                50 // Optional distance threshold (ignore points farther than 50 units)
            );
            row.push(elevation ?? NaN); // Replace null with NaN if preferred
        }

        grid.push(row);
    }

    return grid;
}

Usage Example

Here’s how you’d use these functions with sample point cloud data:

// Sample elevation point cloud (replace with your actual data)
const pointCloud = [
    { x: 0, y: 0, z: 10 },
    { x: 0, y: 10, z: 15 },
    { x: 10, y: 0, z: 12 },
    { x: 10, y: 10, z: 8 }
];

// Generate a 5x5 grid covering the area from (0,0) to (10,10)
const elevationGrid = generateElevationGrid(pointCloud, 0, 10, 0, 10, 5);

// Log the result
console.log("Generated Elevation Grid:", elevationGrid);

Key Notes

  • Power Parameter: The power value controls how much influence distant points have. A value of 2 is standard, but you can increase it (e.g., 3-5) to give closer points more weight, or decrease it (e.g., 1) to make distant points more impactful.
  • Distance Threshold: Use distanceThreshold to ignore points that are too far from the target grid cell—this can speed up computation and reduce noise from outliers.
  • Edge Cases: The function returns null if no valid points are found for a grid cell; you can adjust this to return a default value (like the average elevation) if needed.

内容的提问来源于stack exchange,提问作者Jay Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:00:57