<template></template>
<script setup>
import { ref, onMounted, reactive } from "vue";
import { RouterView, RouterLink } from "vue-router";
import { request, uploadFile } from "@/utils/request";
import { vtkmodel } from "@/control/vtkModel.js";
import { throttle } from "@kitware/vtk.js/macros";
import vtkSphereSource from "@kitware/vtk.js/Filters/Sources/SphereSource";
import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor";
import vtkMapper from "@kitware/vtk.js/Rendering/Core/Mapper";
import vtkLineSource from '@kitware/vtk.js/Filters/Sources/LineSource';
import vtkPointSource from '@kitware/vtk.js/Filters/Sources/PointSource';

onMounted(() => {
  readNodeAndPipe();
  vtkmodel.clearJgAddMode();
});
const nodes = ref([]); //节点数据
const pipes = ref([]); //管道数据
const delNodes = ref([]); //无效节点
const validNodes = ref([]); //有效节点




async function readNodeAndPipe() {
  const params = {
    transCode: "D00000",
    count: 1000,
    page: 1,
  };
  await request(params)
    .then((res) => {
      if (res.returnCode == "000000000") {
        nodes.value = res.rows;
        readePipe();
      } else {
        ElMessage({
          message: res.returnMsg,
          type: "error",
        });
      }
    })
    .catch((err) => {});
}
async function readePipe() {
  const params = {
    transCode: "D00001",
    count: 1000,
    page: 1,
  };
  await request(params)
    .then((res) => {
      if (res.returnCode == "000000000") {
        pipes.value = res.rows;
        lineShow();
      } else {
        ElMessage({
          message: res.returnMsg,
          type: "error",
        });
      }
    })
    .catch((err) => {});
}


/**
 * 在管道中已存在的点才可以使用
 */
function nodeIsValid(node) {
  let isValid = false;
  pipes.value.forEach((pipe) => {
    if (node.id == pipe.snId || node.id == pipe.enId) {
      isValid = true;
      return isValid;
    }
  });
  return isValid;
}
/**
 * 根据id 获取对应的编号
 */
function indexIdByPipeNodeId(nid) {
  for (let index = 0; index < validNodes.value.length; index++) {
    const node = validNodes.value[index];
    if (node.id == nid) {
      return index;
    }
  }
  return 0;
}
function pointByPipeNodeId(nid) {
  for (let index = 0; index < validNodes.value.length; index++) {
    const node = validNodes.value[index];
    if (node.id == nid) {
      return [parseFloat(node.x), parseFloat(node.y), parseFloat(node.z)];
    }
  }
  return 0;
}

function lineShow() {
  console.log("模板加载..");
  const points = vtkmodel.polyData.getPoints();
  const lines = vtkmodel.polyData.getLines();

  //判断NODE 是否在管道中使用
  nodes.value.forEach((node) => {
    if (!nodeIsValid(node)) {
      delNodes.value.push(node);
    } else {
      validNodes.value.push(node);
    }
  });
  // validNodes.value.forEach((node) => {
  //   // console.log(node);
  //   points.insertNextPoint(parseFloat(node.x), parseFloat(node.y), parseFloat(node.z));
  // });
  // pipes.value.forEach((pipe) => {
  //   let sid = indexIdByPipeNodeId(pipe.snId);
  //   let eid = indexIdByPipeNodeId(pipe.enId);
  //   // console.log(pipe.snId-1, pipe.enId-1,":",sid, eid);
  //   lines.insertNextCell([sid, eid]);
  // });

  // points.insertNextPoint(1, 0, 0);
  // points.insertNextPoint(0, 1, 0);
  // points.insertNextPoint(0, 0, 1);
  // lines.insertNextCell([0, 1]);
  // lines.insertNextCell([1, 2]);
  // lines.insertNextCell([2, 0]);

  const sphereSource = vtkSphereSource.newInstance({
    center: [0, 0, 0],
    radius: 4.0,
  });
  // sphereSource.setThetaResolution(64);
  // sphereSource.setPhiResolution(32);

  const mapper = vtkMapper.newInstance();
  mapper.setInputConnection(sphereSource.getOutputPort());
  validNodes.value.forEach((node) => {
     const actor = vtkActor.newInstance();
    actor.setMapper(mapper);
    actor.setPosition(parseFloat(node.x), parseFloat(node.y), parseFloat(node.z));
    vtkmodel.renderer.addActor(actor);
    const nodeActor={};
    nodeActor.node =node;
    nodeActor.actor=actor;
    vtkmodel.nodeActors.push(nodeActor);
  });

  pipes.value.forEach((pipe) => {
    const lineSource = vtkLineSource.newInstance();
    let point1 = pointByPipeNodeId(pipe.snId);
    let point2 = pointByPipeNodeId(pipe.enId);
    //  console.log(point1,":",point2);
    // lines.insertNextCell([sid, eid]);
    lineSource.setPoint1(point1);
    lineSource.setPoint2(point2);
    lineSource.setResolution(12);
    const actor = vtkActor.newInstance();
    const mapper = vtkMapper.newInstance();
    actor.setMapper(mapper);
    actor.getProperty().setLineWidth(3);//设置线宽
    // actor.getProperty().setColor(WHITE);//设置线宽

    mapper.setInputConnection(lineSource.getOutputPort());
    vtkmodel.renderer.addActor(actor);

    const pipeActor={};
    pipeActor.pipe =pipe;
    pipeActor.actor=actor;
    vtkmodel.pipeActors.push(pipeActor);
  });

  vtkmodel.renderer.resetCamera();
  vtkmodel.renderWindow.render();
}
</script>
<style scoped>
.vtk {
  position: absolute;
  z-index: 150;
  top: 70px;
}
span {
  position: absolute;
  top: 2px;
}
</style>
