chili3d筆記23 正交投影3d重建筆記4 點到線2

從俯視圖到主視圖就這兩條線有問題,比想象的效果好

原圖

兩條斜線變成了4條橫線

經典少一根線

好了但是不知道為什么好了

import { Logger, PubSub } from "chili-core";
import DxfParser, { ILineEntity } from 'dxf-parser';
class Cluster {lines: [number, number, number, number][];min_x: number;max_x: number;min_y: number;max_y: number;constructor(lines: [number, number, number, number][] = []) {this.lines = [...lines];this.min_x = Infinity;this.max_x = -Infinity;this.min_y = Infinity;this.max_y = -Infinity;if (lines.length > 0) {this.updateBounds();}}updateBounds(): void {this.min_x = Math.min(...this.lines.flatMap(line => [line[0], line[2]]));this.max_x = Math.max(...this.lines.flatMap(line => [line[0], line[2]]));this.min_y = Math.min(...this.lines.flatMap(line => [line[1], line[3]]));this.max_y = Math.max(...this.lines.flatMap(line => [line[1], line[3]]));}get lengthX(): number {return parseFloat((this.max_x - this.min_x).toFixed(1));}get lengthY(): number {return parseFloat((this.max_y - this.min_y).toFixed(1));}
}
function clusterLines(lines: [number, number, number, number][], expandDistance: number = 5): Cluster[] {const clusters: Cluster[] = [];const remainingLines = [...lines];while (remainingLines.length > 0) {const seed = remainingLines.shift()!;const currentCluster = new Cluster([seed]);currentCluster.updateBounds();let changed = true;while (changed) {changed = false;const expandedMinX = currentCluster.min_x - expandDistance;const expandedMaxX = currentCluster.max_x + expandDistance;const expandedMinY = currentCluster.min_y - expandDistance;const expandedMaxY = currentCluster.max_y + expandDistance;const toAdd: [number, number, number, number][] = [];for (const line of [...remainingLines]) {const [x1, y1, x2, y2] = line;const inBound =(x1 >= expandedMinX && x1 <= expandedMaxX && y1 >= expandedMinY && y1 <= expandedMaxY) ||(x2 >= expandedMinX && x2 <= expandedMaxX && y2 >= expandedMinY && y2 <= expandedMaxY);if (inBound) {toAdd.push(line);changed = true;}}for (const line of toAdd) {currentCluster.lines.push(line);remainingLines.splice(remainingLines.indexOf(line), 1);}currentCluster.updateBounds();}// 合并完全覆蓋的聚類for (let i = 0; i < clusters.length; i++) {const cluster = clusters[i];if (currentCluster.min_x <= cluster.min_x &&currentCluster.min_y <= cluster.min_y &&currentCluster.max_x >= cluster.max_x &&currentCluster.max_y >= cluster.max_y) {currentCluster.lines.push(...cluster.lines);clusters.splice(i, 1);break;}}clusters.push(currentCluster);}return clusters;
}
export function rebuild3D(document: Document) {const fileInput = document.createElement("input");fileInput.type = "file";fileInput.accept = ".dxf";fileInput.style.display = "none";fileInput.addEventListener("change", async (event) => {const target = event.target as HTMLInputElement;if (!target.files || target.files.length === 0) return;const file = target.files[0];Logger.info(`Selected file: ${file.name}`);try {const reader = new FileReader();reader.onload = () => {const dxfText = reader.result as string;const parser = new DxfParser();const dxf = parser.parseSync(dxfText);const inputlines: [number, number, number, number][] = [];if (dxf && dxf.entities) {dxf.entities.forEach(entity => {if (entity.type === 'LINE') {const lineEntity = entity as ILineEntity;const start = lineEntity.vertices[0];const end = lineEntity.vertices[1];if (start && end) {inputlines.push([start.x, start.y, end.x, end.y]);}}});}// 執行聚類const clusters = clusterLines(inputlines, 5);const lines3d:[number,number,number,number,number,number,number][]=[]const { mostMinX, mostMinY } = getMostFrequentMinXY(clusters);const mostFrequentClusters = clusters.filter(cluster => {return cluster.min_x === mostMinX.value && cluster.min_y === mostMinY.value;
});
const topclusters = clusters.filter(cluster => {return cluster.min_x === mostMinX.value && cluster.min_y > mostMinY.value;
});
;
const rightclusters = clusters.filter(cluster => {return cluster.min_x > mostMinX.value && cluster.min_y === mostMinY.value;
});
const bottomclusters = clusters.filter(cluster => {return cluster.min_x === mostMinX.value && cluster.min_y < mostMinY.value;
});
const leftclusters = clusters.filter(cluster => {return cluster.min_x < mostMinX.value && cluster.min_y === mostMinY.value;
});const mostFrequentCluster= mostFrequentClusters[0];const topcluauster= topclusters[0];const rightcluster= rightclusters[0];const bottomcluster= bottomclusters[0];const leftcluster= leftclusters[0];const seen = new Set<string>(); // 用于記錄已經添加過的線段function addUniqueLine(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number,color:number) {const key = `${x1},${y1},${z1},${x2},${y2},${z2}`;if (!seen.has(key)) {seen.add(key);lines3d.push([x1, y1, z1, x2, y2, z2,color]);}
} 
const clusterMinY = topcluauster.min_y;// 構建點存在性查詢的 Map
const pointToLinesMap = new Map<string, boolean>();
topcluauster.lines.forEach(([tx1, ty1, tx2, ty2]) => {pointToLinesMap.set(`${tx1},${ty1}`, true);pointToLinesMap.set(`${tx2},${ty2}`, true);
});// 判斷某點是否存在于 topcluauster 中
function isPointInTopCluster(x: number, y: number): boolean {return pointToLinesMap.has(`${x},${y}`);
}// 主處理邏輯
for (const [x1, y1, x2, y2] of mostFrequentCluster.lines) {for (const [tx1, ty1, tx2, ty2] of topcluauster.lines) {// 判斷 (x1, ty1) 和 (x2, ty2) 是否在 topcluauster 的線段中const p1Exists = isPointInTopCluster(x1, ty1);const p2Exists = isPointInTopCluster(x2, ty2);const offset1 = ty1 - clusterMinY;const offset2 = ty2 - clusterMinY;if (p1Exists && p2Exists) {if(x1==x2&&x1==tx1){addUniqueLine(x1, y1, offset1, x2, y2, offset1, 1);}else if(x1==x2&&x1==tx2){addUniqueLine(x1, y1, offset2, x2, y2, offset2, 1);}else if(x1==tx1&&x2==tx2){ addUniqueLine(x1, y1, offset1, x2, y2, offset2, 1);}}if (ty1 !== ty2 && (x1 === tx1 && tx1 === tx2 )) {addUniqueLine(x1, y1, offset1, x1, y1, offset2, 1);}if (ty1 !== ty2 && ( x2 === tx1 && tx1 === tx2)) {addUniqueLine(x2, y2, offset1, x2, y2, offset2, 1);}}
}
Logger.info(`lines3d completed with ${lines3d.length} lines3d`);lines3d.forEach(line => {PubSub.default.pub("njsgcs_makeline", line[0], line[1],  line[2], line[3], line[4], line[5],1); })///// let i =0;// // 發送每個線段給 njsgcs_makeline// clusters.forEach(cluster => {//     i++;//     cluster.lines.forEach(line => {//         const [x1, y1, x2, y2] = line;//         PubSub.default.pub("njsgcs_makeline", x1, y1, 0, x2, y2, 0,i); // z=0 假設為俯視圖//     });// });///Logger.info(`Clustering completed with ${clusters.length} clusters`);};reader.readAsText(file);} catch (error) {Logger.error("Error reading file:", error);}});fileInput.click();
}
function getMostFrequentMinXY(clusters: Cluster[]) {const minXCounts: Record<number, number> = {};const minYCounts: Record<number, number> = {};let maxXCount = 0, mostX = clusters[0]?.min_x;let maxYCount = 0, mostY = clusters[0]?.min_y;for (const cluster of clusters) {const x = cluster.min_x;const y = cluster.min_y;minXCounts[x] = (minXCounts[x] || 0) + 1;if (minXCounts[x] > maxXCount) {maxXCount = minXCounts[x];mostX = x;}minYCounts[y] = (minYCounts[y] || 0) + 1;if (minYCounts[y] > maxYCount) {maxYCount = minYCounts[y];mostY = y;}}return {mostMinX: { value: mostX, count: maxXCount },mostMinY: { value: mostY, count: maxYCount }};
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/86518.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/86518.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/86518.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

LDO的自放電功能

LDO&#xff08;低壓差線性穩壓器&#xff09;的自放電功能&#xff08;Discharge Function 或 Active Discharge&#xff09;是一種在關閉輸出時主動釋放輸出端殘留電荷的機制。以下是其關鍵點&#xff1a; 1. 自放電功能的作用 快速放電&#xff1a;當LDO被禁用&#xff08;如…

Ingress-Nginx簡介和配置樣例

Ingress-Nginx 是 Kubernetes 中一個基于 Nginx 的 Ingress 控制器&#xff0c;用于管理對集群內服務的 HTTP/HTTPS 訪問。它是 Kubernetes Ingress 資源的實現之一&#xff0c;通過配置 Nginx 反向代理和負載均衡器&#xff0c;提供路由規則、SSL/TLS 終止、路徑重寫等高級功能…

Java+LangChain實戰入門:深度剖析開發大語言模型應用!

在人工智能飛速發展的今天&#xff0c;大語言模型&#xff08;如GPT系列&#xff09;正改變著我們構建應用的方式。但如何將這些先進模型無縫集成到企業級Java應用中&#xff1f;這正是LangChain框架的強項——它簡化了語言模型的調用、鏈式處理和上下文管理&#xff0c;讓開發…

論文筆記:Large language model augmented narrative driven recommendations

RecSys 2023 代碼&#xff1a;iesl/narrative-driven-rec-mint: Mint: A data augmentation method for narrative driven recommendation. 1 intro 盡管基于歷史交互的數據能夠有效地提供推薦&#xff0c;但用戶在請求推薦時&#xff0c;往往只是對目標物品有一個模糊的概念…

興達易控Modbus TCP轉Profibus DP網關與安科瑞多功能電表的快速通訊

興達易控Modbus TCP轉Profibus DP網關與安科瑞多功能電表的快速通訊 在工業自動化領域&#xff0c;不同設備之間的通信連接至關重要。興達易控Modbus TCP轉Profibus DP網關接APM810/MCE安科瑞多功能電表與300plc通訊&#xff0c;這一過程涉及到多個關鍵技術和環節&#xff0c;…

epoll實現理解

根據前文高性能網絡設計推演中&#xff0c;epoll作為一個“大殺器”為網絡開發提供強大的支持。Linux系統上IO多路復用方案有select、poll、epoll。其中epoll的性能表現最優&#xff0c;且支持的并發量最大。本文大概介紹epoll的底層實現。 一、示例引入 了解epoll開發&#…

協議轉換賦能光伏制造:DeviceNET轉PROFINET網關的通信質檢實踐

協議轉換賦能光伏制造&#xff1a;DeviceNET轉PROFINET網關的通信質檢實踐 某光伏電池片生產線創新性地將網關作為計算節點&#xff0c;通過搭載DeviceNET-PROFINET智能網關-穩聯技術WL-PN-DVNM&#xff0c;在協議轉換層直接運行AI質檢模型。DeviceNET端采集的高清圖像數據經網…

學習永無止境

已掌握以下每個&#xff0c;有屬于自己的一套架構方式&#xff1a; vue.element-ui&#xff1a;后臺管理 vue.uni-app&#xff1a;H5&#xff0c;小程序&#xff0c;Android&#xff0c;IOS php&#xff1a;??RESTful&#xff0c;服務&#xff0c;業務邏輯&#xff08;如電商…

永磁無刷電機旋轉原理

目錄 1. 磁場的基本知識 2. 角速度&#xff0c;線速度&#xff0c;工程轉速 3.力和力矩 4. 慣量&#xff0c;轉動慣量 5. 電機的四種狀態 5.1 空載 5.2 帶載 5.3 滿載 5.4 堵轉 6. 功和功率 1. 磁場的基本知識 無頭無尾&#xff0c;轉了一圈&#xff0c;就叫有旋…

Ubuntu 物理桌面遠程訪問教程(基于 RealVNC / mstsc)

Ubuntu 物理桌面遠程訪問教程&#xff08;基于 RealVNC / mstsc&#xff09; 適用對象&#xff1a;任意安裝了 GNOME GDM 的 Ubuntu 系統 目標&#xff1a;遠程連接系統默認物理桌面 :0&#xff0c;無虛擬桌面、無 Xfce&#xff0c;真實 GNOME 桌面環境 1. 準備條件 Ubuntu 系…

Vue3 工程化實戰

Vue3 工程化實戰 引言&#xff1a;構建工具的演進與選擇 在前端工程化領域&#xff0c;構建工具的選擇直接影響開發效率與項目性能。隨著Vue3的普及&#xff0c;構建工具生態也發生了顯著變化&#xff1a;傳統vue-cli逐漸進入維護模式&#xff0c;而新一代構建工具Vite憑借其…

調用phantomjs(前端)插件生成ECharts圖片

package com.demo.common.utils; //json格式化工具,可以其他工具類 import cn.hutool.json.JSONUtil; import lombok.extern. public class FileUtil { /** * 調用phantomjs(前端)插件生成ECharts圖片 * @param path 根路徑 * @param option ECharts配置J…

React Hooks詳解

React Hooks 常考內容 React Hooks 是 React 16.8 引入的重要特性&#xff0c;用于在函數組件中使用狀態和其他 React 特性。以下是面試中常考的核心內容&#xff1a; 基礎 Hook useState: 用于管理組件內部狀態&#xff0c;返回狀態變量和更新狀態的函數。useEffect: 處理副…

c++17標準std::filesystem常用函數

std::filesystem 是 C17 引入的標準庫&#xff0c;用于處理文件系統操作&#xff0c;提供了跨平臺的文件和目錄操作能力。以下是一些常用的函數和類&#xff1a; 一、路徑操作&#xff08;std::filesystem::path&#xff09; cpp 運行 #include <filesystem> namespa…

非結構化文檔的自動化敏感標識方法技術解析

在數字化時代&#xff0c;企業與組織面臨的數據形態正發生深刻變革。據統計&#xff0c;非結構化數據占企業數據總量的 80% 以上&#xff0c;涵蓋文本、郵件、PDF、日志、社交媒體內容等多種形式。這些數據中往往蘊含著大量敏感信息&#xff0c;如個人身份信息、商業機密、醫療…

c語言中的字符類型

字符類型 char char是一種整數&#xff0c;也是一種特殊的類型&#xff1a;字符。 #include <stdio.h> int main(){char c,d;c 1; //把整數1賦值給變量cd 1; //把字符‘1’賦值給變量dif (c d){printf("相等");}else{printf("不相等\n");…

Cribl stream 管道對時間的改變時區

先說一下時區的重要性&#xff0c;要是cribl 時區是UTC&#xff0c;但是過來數據是GTM8 就是中國時區&#xff0c;那么數據過來&#xff0c;就可能在后端的Splunk 沒有顯示&#xff0c;那么解決這個問題&#xff0c;cribl 管道引入了auto timestamp 的功能&#xff1a; 注意到&…

深度學習:PyTorch卷積神經網絡(1)

本文目錄&#xff1a; 一、CNN概述二、CNN日常應用三、CNN的卷積層&#xff08;一 &#xff09;基本介紹&#xff08;二&#xff09;卷積層計算1.對輸入數據的要求2.卷積核核心參數3.計算過程4.特征圖尺寸計算5.1、多通道卷積計算5.2、多卷積核計算6.PyTorch卷積層API 前言&…

linux網絡編程socket套接字

套接字概念 Socket本身有“插座”的意思&#xff0c;在Linux環境下&#xff0c;用于表示進程間網絡通信的特殊文件類型。本質為內核借助緩沖區形成的偽文件。 既然是文件&#xff0c;那么理所當然的&#xff0c;我們可以使用文件描述符引用套接字。與管道類似的&#xff0c;L…

Python 數據分析與可視化 Day 5 - 數據可視化入門(Matplotlib Seaborn)

&#x1f3af; 今日目標 掌握 Matplotlib 的基本繪圖方法&#xff08;折線圖、柱狀圖、餅圖&#xff09;掌握 Seaborn 的高級繪圖方法&#xff08;分類圖、分布圖、箱線圖&#xff09;熟悉圖像美化&#xff08;標題、標簽、顏色、風格&#xff09;完成一組學生成績數據的可視化…