如何使用 C# 識別 2D 點雲中的凸孔並將其多邊形化?
此程式碼示範了一種在一組 2d 點中尋找凸孔的方法。此方法包括建立點雲位圖、計算位圖中每個單元的資料密度、建立未使用區域的清單(map[][] = 0 或map[][]
這是此演算法的 C# 實作提供:
using System; using System.Collections.Generic; using System.Linq; using System.Drawing; namespace HoleFinder { class Program { // Define a cell structure for the bitmap public struct Cell { public double x0, x1, y0, y1; // Bounding box of points inside the cell public int count; // Number of points inside the cell } // Define a line structure for representing hole boundaries public struct Line { public double x0, y0, x1, y1; // Line edge points public int id; // Id of the hole to which the line belongs for segmentation/polygonization public int i0, i1, j0, j1; // Index in map[][] } // Compute the bounding box of the point cloud public static (double x0, double x1, double y0, double y1) ComputeBoundingBox(List<Point> points) { double x0 = points[0].X; double x1 = points[0].X; double y0 = points[0].Y; double y1 = points[0].Y; foreach (var point in points) { if (point.X < x0) x0 = point.X; if (point.X > x1) x1 = point.X; if (point.Y < y0) y0 = point.Y; if (point.Y > y1) y1 = point.Y; } return (x0, x1, y0, y1); } // Create a bitmap of the point cloud public static int[,] CreateBitmap(List<Point> points, (double x0, double x1, double y0, double y1) boundingBox, int N) { // Create a 2D array to represent the bitmap int[,] bitmap = new int[N, N]; // Compute the scale factors for converting point coordinates to bitmap indices double mx = N / (boundingBox.x1 - boundingBox.x0); double my = N / (boundingBox.y1 - boundingBox.y0); // Iterate over the points and increment the corresponding cells in the bitmap foreach (var point in points) { int i = (int)Math.Round((point.X - boundingBox.x0) * mx); int j = (int)Math.Round((point.Y - boundingBox.y0) * my); if (i >= 0 && i < N && j >= 0 && j < N) bitmap[i, j]++; } return bitmap; } // Compute the data density for each cell in the bitmap public static void ComputeDataDensity(int[,] bitmap, Cell[] map) { for (int i = 0; i < map.Length; i++) { map[i].count = 0; } for (int i = 0; i < bitmap.GetLength(0); i++) { for (int j = 0; j < bitmap.GetLength(1); j++) { map[i * bitmap.GetLength(1) + j].count += bitmap[i, j]; } } } // Create a list of unused areas (map[][] = 0 or map[][] <= treshold) public static List<(int i0, int i1, int j0, int j1)> FindUnusedAreasHorizontalVertical(Cell[] map, int N, int treshold = 0) { List<(int i0, int i1, int j0, int j1)> unusedAreas = new List<(int, int, int, int)>(); // Scan horizontally for (int j = 0; j < N; j++) { int i0 = -1; int i1 = -1; for (int i = 0; i < N; i++) { if (map[i * N + j].count == 0 || map[i * N + j].count <= treshold) { if (i0 < 0) i0 = i; } else { if (i0 >= 0) { unusedAreas.Add((i0, i1, j, j)); i0 = -1; i1 = -1; } } } if (i0 >= 0) unusedAreas.Add((i0, i1, j, j)); } // Scan vertically for (int i = 0; i < N; i++) { int j0 = -1; int j1 = -1; for (int j = 0; j < N; j++) { if (map[i * N + j].count == 0 || map[i * N + j].count <= treshold) { if (j0 < 0) j0 = j; } else { if (j0 >= 0) { unusedAreas.Add((i, i, j0, j1)); j0 = -1; j1 = -1; } } } if (j0 >= 0) unusedAreas.Add((i, i, j0, j1)); } return unusedAreas; } // Segment the list of unused areas into groups of connected components public static List<List<(int i0, int i1, int j0, int j1)>> SegmentUnusedAreas(List<(int i0, int i1, int j0, int j1)> unusedAreas) { // Initialize each unused area as a separate group List<List<(int i0, int i1, int j0, int j1)>> segments = new List<List<(int i0, int i1, int j0, int j1)>>(); foreach (var unusedArea in unusedAreas) { segments.Add(new List<(int i0, int i1, int j0, int j1)> { unusedArea }); } // Iterate until no more segments can be joined bool joined = true; while (joined) { joined = false; // Check if any two segments intersect or are adjacent for (int i = 0; i < segments.Count; i++) { for (int j = i + 1; j < segments.Count; j++) { // Check for intersection bool intersects = false; foreach (var unusedArea1 in segments[i]) { foreach (var unusedArea2 in segments[j]) { if (unusedArea1.i0 <= unusedArea2.i1 && unusedArea1.i1 >= unusedArea2.i0 && unusedArea1.j0 <= unusedArea2.j1 && unusedArea1.j1 >= unusedArea2.j0) { intersects = true; break; } } if (intersects) break; } // Check for adjacency bool adjacent = false; if (!intersects) { foreach (var unusedArea1 in segments[i]) { foreach (var unusedArea2 in segments[j]) { if (unusedArea1.i0 == unusedArea2.i0 && unusedArea1.i1 == unusedArea2.i1 && ((unusedArea1.j1 == unusedArea2.j0 && Math.Abs(unusedArea1.j0 - unusedArea2.j1) == 1) || (unusedArea1.j0 == unusedArea2.j1 && Math.Abs(unusedArea1.j1 - unusedArea2.j0) == 1))) { adjacent = true; break; } if (unusedArea1.j0 == unusedArea2.j0 && unusedArea1.j1 == unusedArea2.j1
以上是如何使用 C# 識別 2D 點雲中的凸孔並將其多邊形化?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

C#和C 的学习曲线和开发者体验有显著差异。1)C#的学习曲线较平缓,适合快速开发和企业级应用。2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C 學習者和開發者可以從StackOverflow、Reddit的r/cpp社區、Coursera和edX的課程、GitHub上的開源項目、專業諮詢服務以及CppCon等會議中獲得資源和支持。 1.StackOverflow提供技術問題的解答;2.Reddit的r/cpp社區分享最新資訊;3.Coursera和edX提供正式的C 課程;4.GitHub上的開源項目如LLVM和Boost提陞技能;5.專業諮詢服務如JetBrains和Perforce提供技術支持;6.CppCon等會議有助於職業

C 通過第三方庫(如TinyXML、Pugixml、Xerces-C )與XML交互。 1)使用庫解析XML文件,將其轉換為C 可處理的數據結構。 2)生成XML時,將C 數據結構轉換為XML格式。 3)在實際應用中,XML常用於配置文件和數據交換,提升開發效率。

靜態分析在C 中的應用主要包括發現內存管理問題、檢查代碼邏輯錯誤和提高代碼安全性。 1)靜態分析可以識別內存洩漏、雙重釋放和未初始化指針等問題。 2)它能檢測未使用變量、死代碼和邏輯矛盾。 3)靜態分析工具如Coverity能發現緩衝區溢出、整數溢出和不安全API調用,提升代碼安全性。

C 在現代編程中仍然具有重要相關性。 1)高性能和硬件直接操作能力使其在遊戲開發、嵌入式系統和高性能計算等領域佔據首選地位。 2)豐富的編程範式和現代特性如智能指針和模板編程增強了其靈活性和效率,儘管學習曲線陡峭,但其強大功能使其在今天的編程生態中依然重要。

使用C 中的chrono庫可以讓你更加精確地控制時間和時間間隔,讓我們來探討一下這個庫的魅力所在吧。 C 的chrono庫是標準庫的一部分,它提供了一種現代化的方式來處理時間和時間間隔。對於那些曾經飽受time.h和ctime折磨的程序員來說,chrono無疑是一個福音。它不僅提高了代碼的可讀性和可維護性,還提供了更高的精度和靈活性。讓我們從基礎開始,chrono庫主要包括以下幾個關鍵組件:std::chrono::system_clock:表示系統時鐘,用於獲取當前時間。 std::chron

C 的未來將專注於並行計算、安全性、模塊化和AI/機器學習領域:1)並行計算將通過協程等特性得到增強;2)安全性將通過更嚴格的類型檢查和內存管理機制提升;3)模塊化將簡化代碼組織和編譯;4)AI和機器學習將促使C 適應新需求,如數值計算和GPU編程支持。
