当前位置: 首页 > news >正文

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.lryc.cn/news/575385.html

相关文章:

  • 【NLP】使用 LangGraph 构建 RAG 的Research Multi-Agent
  • house of apple2
  • Linux系统(信号篇):信号的产生
  • 【Pandas】pandas DataFrame shift
  • Ubuntu下布署mediasoup-demo
  • 黑马JVM解析笔记(四):Javap图解指令流程,深入理解Java字节码执行机制
  • Redis 为什么选用跳跃表,而不是红黑树
  • 《聊一聊ZXDoc》之汽车标定、台架标定、三高标定
  • 【STM32】外部中断
  • 【C++11】右值引用和移动语义
  • gRPC 使用(python 版本)
  • 2025学年湖北省职业院校技能大赛 “信息安全管理与评估”赛项 样题卷(五)
  • Axure版TDesign 组件库-免费版
  • MQTT 和 HTTP 有什么本质区别?
  • 如何将 Memfault 固件 SDK 集成到使用 Nordic 的 nRF Connect SDK(NCS)的项目中
  • 数据结构进阶 - 第四,五章 串、数组和广义表
  • Docker 入门教程(一):从概念到第一个容器
  • 水质指数预测模型R²偏低的原因分析与优化策略
  • 2-深度学习挖短线股-1-股票范围选择
  • uniapp微信小程序:editor组件placeholder字体样式修改
  • vue3 + elementPlus 封装hook,检测form表单数据修改变更;示例用 script setup 语法使用
  • SpringBoot项目快速开发框架JeecgBoot——Web处理!
  • 一次开发,多端适配!全面掌握Dioxus跨平台开发框架!
  • 远程玩3A大作要多少帧?ToDesk、向日葵、UU远程性能对决
  • 面试破局:告别流水账,用“故事思维”重塑自我介绍
  • rocketmq中broker和namesrv的区别和联系?
  • 川翔云电脑全新上线:三维行业高效云端算力新选择
  • 智能化监管:微算法科技(NASDAQ:MLGO)比特币社区分类器助力加密货币市场规范发展
  • CRON表达式编辑器与定时任务实现技术文档
  • 阿里云ACP-检索分析服务