可视化场景内任意绘制线段并测量距离

[复制链接]
4916|1
 楼主| 森友小锘 发表于 2021-8-16 14:33 | 显示全部楼层 |阅读模式
IO, TI, TE, ST, point, AN
#申请开发板# #技术资源# #申请原创# #每日话题# #有奖活动#
数字孪生可视化场景中,可能会遇到这个问题,即需要测量数字孪生可视化场景中的不同目标之间的距离。通过这个测量,可以明确的知道可视化场景中各个目标的位置以及各个目标之间的距离,便于做出合理的规划。这个需求并不难,我们需要做的是确定需要测量的对象的坐标点起点和终点位置。

运行效果如下:





在ThingJS中要知道场景中两点间的空间距离可以通过调用三维空间内所有坐标点,计算两个坐标点的距离去量算出两点之间的空间距离,需要通过鼠标点击才能获取到两点之间的空间距离。比如我要知道场景中某两个场景距离有多长,就可以通过鼠标点击两个甚至多个场景位置,来计算三维场景中任意三维点的空间距离。下面一起看一下操作步骤:

1、添加注册事件,注册测量详情界面拖拽事件,设置可拖拽范围不能超出屏幕。

  1. registerEvent() {
  2. var _this = this;
  3. // 注册测量详情界面关闭按钮点击事件
  4. $('#dataDetails .tj-close').on('click', function() {
  5. $('#dataDetails').css('display', 'none');
  6. });

  7. // 注册测量详情界面拖拽事件,设置可拖拽范围,不能超出屏幕
  8. $('#dataDetails .tj-title').on('mousedown', function(ev) {
  9. ev.preventDefault();
  10. var domEle = $('#dataDetails .tj-panel');
  11. var spacX = ev.pageX - domEle[0].offsetLeft;
  12. var spacY = ev.pageY - domEle[0].offsetTop;
  13. $(document).bind('mousemove', function(event) {
  14. var x = event.pageX - spacX;
  15. var y = event.pageY - spacY;
  16. if (event.pageX < 0 || event.pageX > $(window).width() || event.pageY < 0 || event.pageY > $(window).height()) {
  17. $(document).unbind('mousemove');
  18. }
  19. if (x <= 0) x = 0;
  20. if (x > ($(window).width() - domEle.width())) {
  21. x = $(window).width() - domEle.width();
  22. }
  23. if (y <= 0) y = 0;
  24. if (y > ($(window).height() - domEle.height())) {
  25. y = $(window).height() - domEle.height();
  26. }
  27. domEle.css('left', x + 'px').css('top', y + 'px');
  28. });
  29. }).on('mouseup', function() {
  30. $(document).unbind('mousemove');
  31. });

  32. // 注册单击事件,创建测距线段实例
  33. app.on(THING.EventType.SingleClick, '*', function(e) {
  34. if (e.button == 0) {
  35. _this.lineNum++;
  36. let line = new DrawLine({
  37. app: app,
  38. modelNum: _this.lineNum,
  39. currPosition: e.pickedPosition
  40. })
  41. app.pauseEvent(THING.EventType.SingleClick, '*', '创建测距线');
  42. }
  43. }, "创建测距线");
  44. }
  45. }


2、ThingJS使用 Constructor () 作为对象构造器函数,用来构造一种“对象类型”,即创建相同类型的对象框架。Constructor () 构造器为对象的属性赋初始值,JS中可以任意扩展构造参数option,实现动态绑定。绘制测量线的构造参数创建如下:

  1. class DrawLine {
  2. /**
  3. * 构造器
  4. * @param {JSON} option - 构造参数
  5. */
  6. constructor(option) {
  7. this.opts = option;
  8. this.pointsArr = [this.opts.currPosition]; // 鼠标移动中坐标点的集合
  9. this.coordinatesArr = [this.opts.currPosition]; // 存储鼠标点击后坐标点的集合
  10. this.ePosition = null; // 存储触发事件后鼠标的位置
  11. this.lineCoor = [this.opts.currPosition]; // 存储当前两个坐标点
  12. this.disArr = []; // 存储所有坐标点与坐标点间的距离
  13. this.numIndex = 0; // 自增变量
  14. this.reSetDistance; // 存储两点间的距离
  15. this.lastStatus = false; // 判断是否绘制结束值为false为未结束true为结束
  16. this.pointsObjArr = [];
  17. this.rianleyDom = $('#marker'); // 跟随鼠标的提示
  18. this.pointCardDom = $('#pointMarker'); // 鼠标移动至节点的提示
  19. this.init(); // 初始化
  20. this.appClick(); // 调用方法
  21. }






3、单击鼠标左键添加点位,双击或单击鼠标右键结束。如果是多线段测量,移动鼠标可以持续绘制。

  1. appClick() {
  2. var _this = this;
  3. // 点击左键添加节点右键结束绘制
  4. _this.opts.app.on('SingleClick', function(e) {
  5. if (e.button == 0) {
  6. if (!e.picked) return;
  7. _this.numIndex++;
  8. _this.ePosition = e.pickedPosition;
  9. _this.createPoint(_this.ePosition);
  10. _this.coordinatesArr.push(_this.ePosition);
  11. _this.lineCoor.push(_this.ePosition);
  12. _this.createLine(_this.coordinatesArr);
  13. _this.getDistance();
  14. _this.template =
  15. `<div id="line` + _this.opts.modelNum + _this.numIndex + `" class="card-label card-line` + _this.opts.modelNum + `">
  16. <span class="text">`;
  17. if (_this.lineDistanceAll != null) {
  18. _this.template += _this.lineDistanceAll + `米`;
  19. } else {
  20. _this.template += `<span style="color:#f45905; border-right: 1px solid #ccc;margin-right: 5px">` + _this.lineId + `</span> 起点`
  21. }
  22. _this.template +=
  23. `</span>
  24. <span><img id="linePoints` + _this.opts.modelNum + _this.numIndex + `" src="/guide/examples/images/measure/remove.png"></span>
  25. </div>`;
  26. _this.boardId = 'line' + _this.opts.modelNum + _this.numIndex;
  27. _this.createCard(_this.regionPoint);
  28. _this.pointsObj = {
  29. id: 'linePoints' + _this.opts.modelNum + _this.numIndex,
  30. parent: 'line' + _this.opts.modelNum + _this.numIndex,
  31. coor: _this.ePosition,
  32. distance: _this.lineDistance
  33. }
  34. _this.pointsObjArr.push(_this.pointsObj);
  35. _this.cardClick();
  36. } else {
  37. if (_this.coordinatesArr.length < 2) {
  38. _this.destroyAll();
  39. _this.rianleyDom.css('display', 'none');
  40. return;
  41. };
  42. _this.end();
  43. }
  44. _this.rianleyDom.css('display', 'none');
  45. }, '点击');

  46. // 鼠标移动持续绘制测量线段
  47. _this.opts.app.on('MouseMove', function(e) {
  48. if (e.picked) {
  49. _this.ePosition = e.pickedPosition;
  50. _this.pointsArr = [..._this.coordinatesArr, _this.ePosition];
  51. _this.createLine(_this.pointsArr);
  52. _this.line.style.color = '#f88020';
  53. if (_this.pointsArr.length >= 2) {
  54. _this.moveDistance = THING.Math.getDistance(_this.pointsArr[_this.pointsArr.length - 1], _this.pointsArr[_this.pointsArr.length - 2]);
  55. let countNum = 0;
  56. _this.disArr.forEach(v => {
  57. countNum += parseFloat(v);
  58. });
  59. countNum = 1 * parseFloat(countNum).toFixed(2) + 1 * parseFloat(_this.moveDistance).toFixed(2);
  60. _this.rianleyDom.css('display', 'block');
  61. _this.rianleyDom.find('span.value').text(countNum.toFixed(2));
  62. _this.rianleyDom.css('left', e.clientX + 10 + 'px');
  63. _this.rianleyDom.css('top', e.clientY + 'px');
  64. }
  65. }
  66. }, '移动');

  67. // 结束绘制当前测量线段
  68. _this.opts.app.on('DBLClick', function(ev) {
  69. if (_this.coordinatesArr.length < 2) {
  70. _this.destroyAll();
  71. _this.rianleyDom.css('display', 'none');
  72. return;
  73. };
  74. _this.end();
  75. }, '双击');
  76. }

  77. /**
  78. * 创建节点
  79. * @param {Array} ePosition - 坐标点
  80. */
  81. createPoint(ePosition) {
  82. var _this = this;
  83. _this.regionPoint = _this.opts.app.create({
  84. type: 'Sphere',
  85. id: 'linePoints' + _this.opts.modelNum + _this.numIndex,
  86. name: 'linePoints' + _this.opts.modelNum,
  87. radius: 0.2, // 半径
  88. widthSegments: 16,
  89. heightSegments: 16,
  90. position: ePosition, // 球体坐标
  91. style: {
  92. color: '#c10000',
  93. roughness: 50,
  94. opacity: 0.8
  95. }
  96. });
  97. }


4、创建节点、线段和节点顶牌这些基本元素,确定起点、各个节点的坐标。其中线段属于所有鼠标点击后的坐标点集合,即测量的总距离。

  1. createPoint(ePosition) {
  2. var _this = this;
  3. _this.regionPoint = _this.opts.app.create({
  4. type: 'Sphere',
  5. id: 'linePoints' + _this.opts.modelNum + _this.numIndex,
  6. name: 'linePoints' + _this.opts.modelNum,
  7. radius: 0.2, // 半径
  8. widthSegments: 16,
  9. heightSegments: 16,
  10. position: ePosition, // 球体坐标
  11. style: {
  12. color: '#c10000',
  13. roughness: 50,
  14. opacity: 0.8
  15. }
  16. });
  17. }

  18. /**
  19. * 创建线段
  20. * @param {Array} coordinates - 所有鼠标点击后的坐标点集合
  21. */
  22. createLine(coordinates) {
  23. let id = this.opts.modelNum >= 10 ? this.opts.modelNum : '0' + this.opts.modelNum;
  24. if (this.line) {
  25. this.line.destroy();
  26. }
  27. this.lineId = 'Line' + id;
  28. this.line = this.opts.app.create({
  29. type: 'PolygonLine',
  30. name: 'line',
  31. id: 'Line' + id,
  32. width: 0.05,
  33. points: coordinates,
  34. style: {
  35. color: '#f45905',
  36. roughness: 5,
  37. opacity: 0.9
  38. }
  39. });
  40. }

  41. /**
  42. * 计算两个坐标点间的距离
  43. */
  44. getDistance() {
  45. if (this.lineCoor.length < 2) return;
  46. if (this.coordinatesArr.length > 2) {
  47. this.lineCoor.shift();
  48. }
  49. this.lineDistance = THING.Math.getDistance(this.lineCoor[0], this.lineCoor[1]);
  50. this.lineDistance = this.lineDistance.toFixed(2);
  51. this.disArr.push(this.lineDistance);
  52. let countNum = 0;
  53. this.disArr.forEach(v => {
  54. countNum += parseFloat(v);
  55. });
  56. this.lineDistanceAll = countNum.toFixed(2);
  57. }

  58. /**
  59. * 创建节点顶牌
  60. * @param {Object} parent - 顶牌父物体
  61. */
  62. createCard(parent) {
  63. $('#div3d').append(this.template);
  64. this.srcElem = document.getElementById(this.boardId);
  65. this.imgElem = document.getElementById('linePoints' + this.opts.modelNum + this.numIndex);
  66. this.ui = this.opts.app.create({
  67. type: 'UIAnchor',
  68. parent: parent,
  69. element: this.srcElem,
  70. localPosition: [0.4, 0.3, 0.4],
  71. pivotPixel: [0, 0]
  72. });
  73. }






通过以上的操作,可以实现多点线段绘制并计算出多点线段之间的距离。上图展示了多点线段长度的测试结果。快来试试看吧!




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
manbo789 发表于 2021-8-16 14:54 | 显示全部楼层
兄弟你发错版块了吧,
您需要登录后才可以回帖 登录 | 注册

本版积分规则

107

主题

107

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部