vtkModel.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. import * as d3 from "d3-scale";
  2. import { formatDefaultLocale } from "d3-format";
  3. import "@kitware/vtk.js/Rendering/Profiles/Geometry";
  4. import vtkFullScreenRenderWindow from "@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow";
  5. import vtkGenericRenderWindow from '@kitware/vtk.js/Rendering/Misc/GenericRenderWindow';
  6. import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor";
  7. import vtkMapper from "@kitware/vtk.js/Rendering/Core/Mapper";
  8. import vtkScalarBarActor from "@kitware/vtk.js/Rendering/Core/ScalarBarActor";
  9. import vtkColorTransferFunction from "@kitware/vtk.js/Rendering/Core/ColorTransferFunction";
  10. import vtkLookupTable from '@kitware/vtk.js/Common/Core/LookupTable';
  11. import { Representation } from "@kitware/vtk.js/Rendering/Core/Property/Constants";
  12. import vtkPolyData from "@kitware/vtk.js/Common/DataModel/PolyData";
  13. import vtkLight from "@kitware/vtk.js/Rendering/Core/Light";
  14. import { throttle } from '@kitware/vtk.js/macros';
  15. import {
  16. FieldDataTypes,
  17. FieldAssociations,
  18. } from '@kitware/vtk.js/Common/DataModel/DataSet/Constants';
  19. import vtkSphereSource from '@kitware/vtk.js/Filters/Sources/SphereSource';
  20. import vtkLineSource from '@kitware/vtk.js/Filters/Sources/LineSource';
  21. import vtkSphereMapper from '@kitware/vtk.js/Rendering/Core/SphereMapper';
  22. import vtk2DShape from '@kitware/vtk.js/Filters/Sources/Arrow2DSource';
  23. import vtkArrowSource from '@kitware/vtk.js/Filters/Sources/ArrowSource';
  24. import vtkArrow2DSource from '@kitware/vtk.js/Filters/Sources/Arrow2DSource';
  25. import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource';
  26. import vtkAxesActor from '@kitware/vtk.js/Rendering/Core/AxesActor';
  27. import vtkAnnotatedCubeActor from '@kitware/vtk.js/Rendering/Core/AnnotatedCubeActor';
  28. import vtkOrientationMarkerWidget from '@kitware/vtk.js/Interaction/Widgets/OrientationMarkerWidget';
  29. import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera';
  30. import vtkPLYReader from '@kitware/vtk.js/IO/Geometry/PLYReader';
  31. import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture';
  32. import vtkMatrixBuilder from '@kitware/vtk.js/Common/Core/MatrixBuilder';
  33. import axios from 'axios';
  34. export class VtkModel {
  35. constructor() {
  36. //当前显示 模版 还是结果
  37. this.isJg = false;
  38. /**
  39. * 选择点线
  40. */
  41. this.isSelectNode = false;
  42. /**
  43. * 选择管道
  44. */
  45. this.isSelectPipe = false;
  46. //管道节点选择对象
  47. this.selectObj = null;
  48. //X,Y
  49. this.epageX=null;
  50. this.epageY=null;
  51. //结果选择的点数据
  52. this.selectJgPointId = 0;
  53. this.validNodes = [];//节点数据
  54. this.pipes = [];//管道数据
  55. this.renderWindowWith = null;
  56. this.renderer = null;
  57. this.renderWindow = null;
  58. //选取
  59. this.lastActor = null;
  60. this.nodeActors = []; //节点actor 数据
  61. this.pipeActors = []; //管道actor 数据
  62. this.interactor = null;
  63. this.apiSpecificRenderWindow = null;
  64. this.hardwareSelector = null;
  65. //模板数据
  66. this.polyData = vtkPolyData.newInstance();
  67. this.mapper = vtkMapper.newInstance();
  68. this.actor = vtkActor.newInstance();
  69. this.actor.getProperty().setRepresentation(Representation.SURFACE); //线
  70. this.actor.getProperty().setColor(WHITE);
  71. this.mapper.setInputData(this.polyData);
  72. this.actor.setMapper(this.mapper);
  73. //点数据
  74. this.actor2 = vtkActor.newInstance();
  75. this.actor2.getProperty().setRepresentation(Representation.POINTS); //点
  76. this.actor2.getProperty().setColor(WHITE);
  77. this.actor2.getProperty().setPointSize(2);
  78. this.actor2.setMapper(this.mapper);
  79. //结果数据渲染
  80. this.jgActor = vtkActor.newInstance();
  81. this.jgMapper = vtkMapper.newInstance();
  82. this.jgActor.getProperty().setRepresentation(Representation.SURFACE); //面
  83. this.scalarBarActor = vtkScalarBarActor.newInstance();
  84. this.scalarBarActor.setGenerateTicks(this.generateTicks(10));
  85. this.scalarBarActor.setDrawAboveRangeSwatch(true);
  86. this.scalarBarActor.setDrawNanAnnotation(false);
  87. this.scalarBarActor.setDrawAboveRangeSwatch(false);
  88. this.scalarBarActor.setBoxPosition([1, 0]);
  89. // 修改设条颜色
  90. const ctf = vtkColorTransferFunction.newInstance();
  91. ctf.addRGBPoint(0.0,0, 1, 154/255.0);
  92. ctf.addRGBPoint(1.0, 1.0, 165.0/255.0, 0.0);
  93. ctf.addRGBPoint(2.0, 230/255.0, 0.0, 92.0/225.0);
  94. // ctf.addRGBPoint(0.0, 0.0, 0.0, 1.0);
  95. // ctf.addRGBPoint(1.0, 0.0, 1.0, 0.5);
  96. // ctf.addRGBPoint(2.0, 0.0, 1.0, 0.0);
  97. // ctf.addRGBPoint(3.0, 1.0, 0.5, 0.0);
  98. // ctf.addRGBPoint(4.0, 1.0, 0.0, 0.0);
  99. this.jgMapper.setLookupTable(ctf);
  100. this.jgActor.setMapper(this.jgMapper);
  101. const lut = this.jgMapper.getLookupTable();
  102. this.scalarBarActor.setScalarsToColors(lut);
  103. //告警数据渲染
  104. this.gjActor = vtkActor.newInstance();
  105. this.gjMapper = vtkMapper.newInstance();
  106. this.gjActor.getProperty().setRepresentation(Representation.SURFACE); //面
  107. this.gjscalarBarActor = vtkScalarBarActor.newInstance();
  108. this.gjscalarBarActor.setGenerateTicks(this.generateTicks(2));
  109. this.gjscalarBarActor.setDrawAboveRangeSwatch(true);
  110. this.gjscalarBarActor.setDrawNanAnnotation(false);
  111. this.gjscalarBarActor.setDrawAboveRangeSwatch(false);
  112. this.gjscalarBarActor.setBoxPosition([1, 0]);
  113. this.gjMapper.setLookupTable(vtkLookupTable.newInstance());
  114. const gjlut = this.gjMapper.getLookupTable();
  115. gjlut.setNumberOfColors(parseInt(2, 10));
  116. // hueRange: [0.0, 0.66667], //色调范围
  117. // saturationRange: [1.0, 1.0],//饱和度
  118. // valueRange: [1.0, 1.0],
  119. // alphaRange: [1.0, 1.0], //透明度
  120. gjlut.setAlphaRange([1,1]);
  121. gjlut.setValueRange([0.5,1]);
  122. // gjlut.setHueRange([0.66667,0.0]);//蓝色到红色
  123. gjlut.setHueRange([0.3375,0.0]);//绿色到红色
  124. // this.gjscalarBarActor.setScalarsToColors(gjlut);
  125. this.gjMapper.setInterpolateScalarsBeforeMapping(true);//有无渐变
  126. this.gjActor.setMapper(this.gjMapper);
  127. this.ways = [];
  128. this.wayActors = [];
  129. this.wayspath = [];
  130. this.wayspathActors = [];
  131. this.sensorobj={};
  132. this.actorpipsites=[];
  133. this.jcdActors = []; //检测点数据
  134. this.jcreader = vtkPLYReader.newInstance();
  135. fetchJSFileAsArrayBuffer('static//3d/jiancd.ply').then(arrayBuffer => {
  136. // 读取PLY数据
  137. this.jcreader.parseAsArrayBuffer(arrayBuffer);
  138. });
  139. this.pnameActors =[];
  140. this.soureActors =[];
  141. this.soureReader = vtkPLYReader.newInstance();
  142. fetchJSFileAsArrayBuffer('static//3d/fire.ply').then(arrayBuffer => {
  143. // 读取PLY数据
  144. this.soureReader.parseAsArrayBuffer(arrayBuffer);
  145. });
  146. this.pmpActors=[];
  147. this.cgqActors=[];
  148. this.cgqreader = vtkPLYReader.newInstance();
  149. fetchJSFileAsArrayBuffer('static//3d/jiancd.ply').then(arrayBuffer => {
  150. // 读取PLY数据
  151. this.cgqreader.parseAsArrayBuffer(arrayBuffer);
  152. });
  153. }
  154. xyz_back(){
  155. let cam =this.renderer.getActiveCamera();
  156. cam.setViewUp([0,1,0]);
  157. cam.setPosition(495544.15625, 5403554.25, 9989.55990464236);//相机位置;
  158. this.renderer.resetCamera();
  159. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  160. this.renderWindow.render()
  161. }
  162. x_up(){
  163. let cam =this.renderer.getActiveCamera();
  164. cam.setViewUp([0,0,1]);
  165. cam.setPosition(495544.15625+5000, 5403554.25, 467.62999725341797);//相机位置;
  166. this.renderer.resetCamera();
  167. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  168. this.renderWindow.render();
  169. }
  170. x_down(){
  171. let cam =this.renderer.getActiveCamera();
  172. cam.setViewUp([0,0,1]);
  173. cam.setPosition(495544.15625+-5000, 5403554.25, 467.62999725341797);//相机位置
  174. this.renderer.resetCamera();
  175. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  176. this.renderWindow.render();
  177. }
  178. y_up(){
  179. let cam =this.renderer.getActiveCamera();
  180. cam.setViewUp([0,0,1]);
  181. cam.setPosition(495544.15625, 5403554.25+5000, 467.62999725341797);//相机位置
  182. this.renderer.resetCamera();
  183. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  184. this.renderWindow.render();
  185. }
  186. y_down(){
  187. let cam =this.renderer.getActiveCamera();
  188. cam.setViewUp([0,0,1]);
  189. cam.setPosition(495544.15625, 5403554.25-5000, 467.62999725341797);//相机位置
  190. this.renderer.resetCamera();
  191. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  192. this.renderWindow.render();
  193. }
  194. z_up(){
  195. let cam =this.renderer.getActiveCamera();
  196. cam.setViewUp([0,1,0]);
  197. cam.setPosition(495544.15625, 5403554.25, 467.62999725341797+5000);//相机位置
  198. this.renderer.resetCamera();
  199. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  200. this.renderWindow.render();
  201. }
  202. z_down(){
  203. let cam =this.renderer.getActiveCamera();
  204. cam.setViewUp([0,1,0]);
  205. cam.setPosition(495544.15625, 5403554.25, 467.62999725341797-5000);//相机位置
  206. this.renderer.resetCamera();
  207. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  208. this.renderWindow.render();
  209. }
  210. //巷道名称
  211. initPname() {
  212. fetch('static//3d/pipname.json')
  213. .then(response => response.json()).then(data => {
  214. let pipnames = data;
  215. pipnames.forEach((pipname)=>{
  216. let pnameReader = vtkPLYReader.newInstance();
  217. fetchJSFileAsArrayBuffer('static//3d/'+pipname.name+'.ply').then(arrayBuffer => {
  218. // 读取PLY数据
  219. pnameReader.parseAsArrayBuffer(arrayBuffer);
  220. });
  221. const mapper = vtkMapper.newInstance();
  222. mapper.setInputConnection(pnameReader.getOutputPort());
  223. const actor = vtkActor.newInstance();
  224. actor.setPosition(pipname.x,pipname.y,pipname.z);
  225. actor.getProperty().setColor(TEXTCOLOR);
  226. actor.setMapper(mapper);
  227. actor.setScale(20,20,20);//放大
  228. this.pnameActors.push(actor);
  229. // this.renderer.addActor(actor);
  230. });
  231. }).catch(error => {
  232. console.error('Error fetching JSON:', error);
  233. });
  234. }
  235. //显示巷道名称
  236. showPname(){
  237. this.pnameActors.forEach((actor)=>{
  238. this.renderer.addActor(actor);
  239. });
  240. this.renderWindow.render();
  241. }
  242. //隐藏巷道名称
  243. removePname(){
  244. this.pnameActors.forEach((actor)=>{
  245. this.renderer.removeActor(actor);
  246. });
  247. this.renderWindow.render();
  248. }
  249. //清空灾源
  250. delSoures(){
  251. this.removeSoure();
  252. this.soureActors=[];
  253. }
  254. //水泵/风门、风机添加
  255. addpump(pid,site){
  256. let pipe =this.pipeByPid(pid,site);
  257. let point1 = this.pointByPipeNodeId(pipe.snId);
  258. let point2 = this.pointByPipeNodeId(pipe.enId);
  259. let py = site/(1-site);
  260. let pipsite_point=[
  261. (point1[0]+py*point2[0])/(1+py),
  262. (point1[1]+py*point2[1])/(1+py),
  263. (point1[2]+py*point2[2])/(1+py),
  264. ]
  265. console.log(pipsite_point)
  266. const mapper = vtkMapper.newInstance();
  267. mapper.setInputConnection(this.cgqreader.getOutputPort());
  268. const actor = vtkActor.newInstance();
  269. actor.setPosition(pipsite_point[0],pipsite_point[1],pipsite_point[2]);
  270. actor.getProperty().setColor(SOURECOLOR);
  271. actor.setMapper(mapper);
  272. actor.setScale(5,5,5);//放大
  273. // 旋转actor
  274. actor.rotateY(90);// 绕y轴旋转
  275. this.pmpActors.push(actor);
  276. this.pmpActors.forEach((actor)=>{
  277. this.renderer.addActor(actor);
  278. });
  279. this.renderWindow.render();
  280. }
  281. showpump(){
  282. this.pmpActors.forEach((actor)=>{
  283. this.renderer.addActor(actor);
  284. });
  285. this.renderWindow.render();
  286. }
  287. removepump(){
  288. this.pmpActors.forEach((actor)=>{
  289. this.renderer.removeActor(actor);
  290. });
  291. this.renderWindow.render();
  292. }
  293. delpump(){
  294. this.removepump();
  295. this.pmpActors=[];
  296. }
  297. addSensor(pid,site){
  298. let pipe =this.pipeByPid(pid,site);
  299. let point1 = this.pointByPipeNodeId(pipe.snId);
  300. let point2 = this.pointByPipeNodeId(pipe.enId);
  301. let py = site/(1-site);
  302. let pipsite_point=[
  303. (point1[0]+py*point2[0])/(1+py),
  304. (point1[1]+py*point2[1])/(1+py),
  305. (point1[2]+py*point2[2])/(1+py),
  306. ]
  307. const mapper = vtkMapper.newInstance();
  308. mapper.setInputConnection(this.cgqreader.getOutputPort());
  309. const actor = vtkActor.newInstance();
  310. actor.setPosition(pipsite_point[0],pipsite_point[1],pipsite_point[2]);
  311. actor.getProperty().setColor(SOURECOLOR);
  312. actor.setMapper(mapper);
  313. actor.setScale(5,5,5);//放大
  314. // 旋转actor
  315. actor.rotateY(90);// 绕y轴旋转
  316. this.cgqActors.push(actor);
  317. }
  318. showSensor(){
  319. this.cgqActors.forEach((actor)=>{
  320. this.renderer.addActor(actor);
  321. });
  322. console.log('看看谁先执行')
  323. this.renderWindow.render();
  324. }
  325. removeSensor(){
  326. this.cgqActors.forEach((actor)=>{
  327. this.renderer.removeActor(actor);
  328. });
  329. this.renderWindow.render();
  330. }
  331. delSensor(){
  332. this.removeSensor();
  333. this.cgqActors=[];
  334. }
  335. //添加一个灾源点
  336. addSoures(pid,site){
  337. let pipe =this.pipeByPid(pid);
  338. let point1 = this.pointByPipeNodeId(pipe.snId);
  339. let point2 = this.pointByPipeNodeId(pipe.enId);
  340. let py = site/(1-site);
  341. let pipsite_point=[
  342. (point1[0]+py*point2[0])/(1+py),
  343. (point1[1]+py*point2[1])/(1+py),
  344. (point1[2]+py*point2[2])/(1+py),
  345. ]
  346. this.initSoure(pipsite_point[0],pipsite_point[1],pipsite_point[2]);
  347. }
  348. initSoure(x,y,z) {
  349. const mapper = vtkMapper.newInstance();
  350. mapper.setInputConnection(this.soureReader.getOutputPort());
  351. const actor = vtkActor.newInstance();
  352. actor.setPosition(x,y,z);
  353. actor.getProperty().setColor(REA);
  354. actor.setMapper(mapper);
  355. // 旋转actor
  356. actor.rotateZ(180);// 绕y轴旋转
  357. actor.setScale(0.3,0.3,0.3);//放大
  358. this.soureActors.push(actor);
  359. // this.renderer.addActor(actor);
  360. }
  361. //显示灾源点
  362. showSoure(){
  363. this.soureActors.forEach((actor)=>{
  364. this.renderer.addActor(actor);
  365. });
  366. this.renderWindow.render();
  367. }
  368. //隐藏灾源点
  369. removeSoure(){
  370. this.soureActors.forEach((actor)=>{
  371. this.renderer.removeActor(actor);
  372. });
  373. this.renderWindow.render();
  374. }
  375. //初始化监测点
  376. initCheckNodes(checkNodes){
  377. checkNodes.forEach((checkNode) => {
  378. let pipe =this.pipeByPid(checkNode.pid);
  379. let site =checkNode.site;
  380. let point1 = this.pointByPipeNodeId(pipe.snId);
  381. let point2 = this.pointByPipeNodeId(pipe.enId);
  382. let py = site/(1-site);
  383. let pipsite_point=[
  384. (point1[0]+py*point2[0])/(1+py),
  385. (point1[1]+py*point2[1])/(1+py),
  386. (point1[2]+py*point2[2])/(1+py),
  387. ]
  388. this.initjc(pipsite_point[0],pipsite_point[1],pipsite_point[2]);
  389. });
  390. }
  391. initjc(x,y,z) {
  392. const mapper = vtkMapper.newInstance();
  393. mapper.setInputConnection(this.jcreader.getOutputPort());
  394. const actor = vtkActor.newInstance();
  395. actor.setPosition(x,y,z);
  396. actor.getProperty().setColor(WHITE);
  397. actor.setMapper(mapper);
  398. actor.setScale(5,5,5);//放大
  399. this.jcdActors.push(actor);
  400. }
  401. //显示监测点
  402. showjc(){
  403. this.jcdActors.forEach((actor)=>{
  404. this.renderer.addActor(actor);
  405. });
  406. this.renderWindow.render();
  407. }
  408. //隐藏监测点
  409. removejc(){
  410. this.jcdActors.forEach((actor)=>{
  411. this.renderer.removeActor(actor);
  412. });
  413. this.renderWindow.render();
  414. }
  415. //显示所选择巷道位置
  416. addpipsite(pid,site){
  417. console.log(1111)
  418. let pipe =this.pipeByPid(pid);
  419. if(this.actorpipsites.length==0){
  420. const actorpipsite= vtkActor.newInstance();
  421. //管道位置数据
  422. const sphereSource = vtkSphereSource.newInstance({
  423. center: [0, 0, 0],
  424. radius: 4.0,
  425. });
  426. const pipsiteMapper = vtkMapper.newInstance();
  427. pipsiteMapper.setInputConnection(sphereSource.getOutputPort());
  428. actorpipsite.setMapper(pipsiteMapper);
  429. actorpipsite.getProperty().setColor(WHITE);
  430. this.actorpipsites.push(actorpipsite);
  431. }
  432. const actorpipsite =this.actorpipsites[0];
  433. let point1 = this.pointByPipeNodeId(pipe.snId);
  434. let point2 = this.pointByPipeNodeId(pipe.enId);
  435. let py = site/(1-site);
  436. let pipsite_point=[
  437. (point1[0]+py*point2[0])/(1+py),
  438. (point1[1]+py*point2[1])/(1+py),
  439. (point1[2]+py*point2[2])/(1+py),
  440. ]
  441. actorpipsite.setPosition(pipsite_point);
  442. this.renderer.addActor(actorpipsite);
  443. this.renderWindow.render();
  444. }
  445. //显示所选择巷道位置
  446. addpipsite2(pid,site){
  447. let pipe =this.pipeByPid(pid);
  448. if(this.actorpipsites.length==0){
  449. addpipsite(pid,site);
  450. }
  451. if(this.actorpipsites.length==1){
  452. const actorpipsite= vtkActor.newInstance();
  453. //管道位置数据
  454. const sphereSource = vtkSphereSource.newInstance({
  455. center: [0, 0, 0],
  456. radius: 4.0,
  457. });
  458. const pipsiteMapper = vtkMapper.newInstance();
  459. pipsiteMapper.setInputConnection(sphereSource.getOutputPort());
  460. actorpipsite.setMapper(pipsiteMapper);
  461. actorpipsite.getProperty().setColor(WHITE);
  462. this.actorpipsites.push(actorpipsite);
  463. }
  464. const actorpipsite =this.actorpipsites[1];
  465. let point1 = this.pointByPipeNodeId(pipe.snId);
  466. let point2 = this.pointByPipeNodeId(pipe.enId);
  467. let py = site/(1-site);
  468. let pipsite_point=[
  469. (point1[0]+py*point2[0])/(1+py),
  470. (point1[1]+py*point2[1])/(1+py),
  471. (point1[2]+py*point2[2])/(1+py),
  472. ]
  473. actorpipsite.setPosition(pipsite_point);
  474. this.renderer.addActor(actorpipsite);
  475. this.renderWindow.render();
  476. }
  477. //移除所选择的巷道位置
  478. clearpipsite(){
  479. this.actorpipsites.forEach((actor)=>{
  480. this.renderer.removeActor(actor);
  481. });
  482. }
  483. // 安全路径显示
  484. safetypath(rows){
  485. this.renderWindowWith.resize();
  486. this.wayspath=rows;
  487. let nodes1 = [];
  488. let nodes2 = [];
  489. this.wayspathActors=[];
  490. for (let index = 0; index < this.wayspath.length - 1; index++) {
  491. nodes1.push(this.wayspath[index]);
  492. }
  493. for (let index = 1; index < this.wayspath.length; index++) {
  494. nodes2.push(this.wayspath[index]);
  495. }
  496. for (let index = 0; index < nodes2.length; index++) {
  497. const lineSource = vtkLineSource.newInstance();
  498. let snode = nodes1[index];
  499. let enode = nodes2[index];
  500. let wizhi1 = this.pointByPipeNodeId(snode.nid);
  501. let wizhi2 = this.pointByPipeNodeId(enode.nid);
  502. let point1 = [parseFloat(wizhi1[0]), parseFloat(wizhi1[1]), parseFloat(wizhi1[2])];
  503. let point2 = [parseFloat(wizhi2[0]), parseFloat(wizhi2[1]), parseFloat(wizhi2[2])];
  504. lineSource.setPoint1(point1);
  505. lineSource.setPoint2(point2);
  506. lineSource.setResolution(12);
  507. const actor = vtkActor.newInstance();
  508. const mapper = vtkMapper.newInstance();
  509. actor.setMapper(mapper);
  510. actor.getProperty().setLineWidth(3);//设置线宽
  511. actor.getProperty().setColor(GREEN);
  512. const initialValues = {
  513. base: 0,
  514. center: point2,
  515. height: 9,
  516. radius: 5,
  517. resolution: 8,
  518. direction: [
  519. point2[0]-point1[0],
  520. point2[1]-point1[1],
  521. point2[2]-point1[2],
  522. ]
  523. };
  524. const vtk2d = vtkConeSource.newInstance(initialValues);
  525. const mapper2 = vtkMapper.newInstance();
  526. mapper2.setInputConnection(vtk2d.getOutputPort());
  527. const actor2 = vtkActor.newInstance();
  528. actor2.setMapper(mapper2);
  529. actor2.getProperty().setColor(GREEN);
  530. this.wayspathActors.push(actor2);
  531. mapper.setInputConnection(lineSource.getOutputPort());
  532. this.wayspathActors.push(actor);
  533. }
  534. this.wayspathActors.forEach((wayAcotor) => {
  535. this.renderer.addActor(wayAcotor);
  536. });
  537. // vtkmodel.renderer.resetCamera();
  538. this.renderWindow.render();
  539. }
  540. // 清空安全路径
  541. safetypathremove(){
  542. this.wayspathActors.forEach(actor => {
  543. this.renderer.removeActor(actor);
  544. });
  545. this.renderWindow.render();
  546. }
  547. waysInit(ways) {
  548. this.renderWindowWith.resize();
  549. this.ways = ways;
  550. this.ways.forEach((way) => {
  551. let nodes1 = [];
  552. let nodes2 = [];
  553. for (let index = 0; index < way.nrows.length - 1; index++) {
  554. nodes1.push(way.nrows[index]);
  555. }
  556. for (let index = 1; index < way.nrows.length; index++) {
  557. nodes2.push(way.nrows[index]);
  558. }
  559. for (let index = 0; index < nodes2.length; index++) {
  560. const lineSource = vtkLineSource.newInstance();
  561. let snode = nodes1[index];
  562. let enode = nodes2[index];
  563. let point1 = [parseFloat(snode.x), parseFloat(snode.y), parseFloat(snode.z)];
  564. let point2 = [parseFloat(enode.x), parseFloat(enode.y), parseFloat(enode.z)];
  565. lineSource.setPoint1(point1);
  566. lineSource.setPoint2(point2);
  567. lineSource.setResolution(12);
  568. const actor = vtkActor.newInstance();
  569. const mapper = vtkMapper.newInstance();
  570. actor.setMapper(mapper);
  571. actor.getProperty().setLineWidth(3);//设置线宽
  572. if(way.safety=="safety"){
  573. actor.getProperty().setColor(GREEN);
  574. }else if(way.safety=="danger"){
  575. actor.getProperty().setColor(REA);
  576. }
  577. const initialValues = {
  578. base: 0,
  579. center: point2,
  580. height: 9,
  581. radius: 5,
  582. resolution: 8,
  583. direction: [
  584. point2[0]-point1[0],
  585. point2[1]-point1[1],
  586. point2[2]-point1[2],
  587. ]
  588. };
  589. const vtk2d = vtkConeSource.newInstance(initialValues);
  590. const mapper2 = vtkMapper.newInstance();
  591. mapper2.setInputConnection(vtk2d.getOutputPort());
  592. const actor2 = vtkActor.newInstance();
  593. actor2.setMapper(mapper2);
  594. if(way.safety=="safety"){
  595. actor2.getProperty().setColor(GREEN);
  596. }else if(way.safety=="danger"){
  597. actor2.getProperty().setColor(REA);
  598. }
  599. this.wayActors.push(actor2);
  600. mapper.setInputConnection(lineSource.getOutputPort());
  601. this.wayActors.push(actor);
  602. }
  603. });
  604. this.wayActors.forEach((wayAcotor) => {
  605. this.renderer.addActor(wayAcotor);
  606. });
  607. // vtkmodel.renderer.resetCamera();
  608. vtkmodel.renderWindow.render();
  609. }
  610. initMode(rootContainer) {
  611. this.renderWindowWith = vtkGenericRenderWindow.newInstance();
  612. this.renderWindowWith.setContainer(rootContainer);
  613. this.renderer = this.renderWindowWith.getRenderer();
  614. this.renderer.setBackground(0.0, 0.0, 0.0, 0.0)
  615. this.renderWindow = this.renderWindowWith.getRenderWindow();
  616. //选取
  617. this.lastActor = null;
  618. this.nodeActors = []; //节点actor 数据
  619. this.pipeActors = []; //管道actor 数据
  620. this.interactor = this.renderer.getRenderWindow().getInteractor();
  621. this.apiSpecificRenderWindow = this.interactor.getView();
  622. this.hardwareSelector = this.apiSpecificRenderWindow.getSelector();
  623. this.hardwareSelector.setCaptureZValues(true);
  624. // this.hardwareSelector.setFieldAssociation(FieldAssociations.FIELD_ASSOCIATION_POINTS);
  625. this.hardwareSelector.setFieldAssociation(FieldAssociations.FIELD_ASSOCIATION_CELLS);
  626. // document.addEventListener("mousemove", throttleMouseHandler);
  627. document.addEventListener('contextmenu', throttleMouseHandler,function(e){
  628. e.preventDefault();
  629. // throttleMouseHandler
  630. })
  631. // //箭头
  632. this.arrowActors = [];
  633. // const viewColors = [
  634. // [1, 0, 0], // sagittal
  635. // [0, 1, 0], // coronal
  636. // [0, 0, 1], // axial
  637. // [0.5, 0.5, 0.5], // 3D
  638. // ];
  639. // // create axes
  640. // const axes = vtkAnnotatedCubeActor.newInstance();
  641. // axes.setDefaultStyle({
  642. // text: '+X',
  643. // fontStyle: 'bold',
  644. // fontFamily: 'Arial',
  645. // fontColor: 'black',
  646. // fontSizeScale: (res) => res / 2,
  647. // faceColor: createRGBStringFromRGBValues(viewColors[0]),
  648. // faceRotation: 0,
  649. // edgeThickness: 0.1,
  650. // edgeColor: 'black',
  651. // resolution: 400,
  652. // });
  653. // // axes.setXPlusFaceProperty({ text: '+X' });
  654. // axes.setXMinusFaceProperty({
  655. // text: '-X',
  656. // faceColor: createRGBStringFromRGBValues(viewColors[0]),
  657. // faceRotation: 90,
  658. // fontStyle: 'italic',
  659. // });
  660. // axes.setYPlusFaceProperty({
  661. // text: '+Y',
  662. // faceColor: createRGBStringFromRGBValues(viewColors[1]),
  663. // fontSizeScale: (res) => res / 4,
  664. // });
  665. // axes.setYMinusFaceProperty({
  666. // text: '-Y',
  667. // faceColor: createRGBStringFromRGBValues(viewColors[1]),
  668. // fontColor: 'white',
  669. // });
  670. // axes.setZPlusFaceProperty({
  671. // text: '+Z',
  672. // faceColor: createRGBStringFromRGBValues(viewColors[2]),
  673. // });
  674. // axes.setZMinusFaceProperty({
  675. // text: '-Z',
  676. // faceColor: createRGBStringFromRGBValues(viewColors[2]),
  677. // faceRotation: 45,
  678. // });
  679. // // const vtkAxesActor = vtkAxesActor.newInstance();
  680. // // create orientation widget
  681. // this.orientationWidget = vtkOrientationMarkerWidget.newInstance({
  682. // actor: axes,
  683. // interactor: this.renderWindow.getInteractor(),
  684. // });
  685. // this.orientationWidget.setEnabled(true);
  686. // this.orientationWidget.setViewportCorner(
  687. // vtkOrientationMarkerWidget.Corners.BOTTOM_LEFT
  688. // );
  689. // this.orientationWidget.setViewportSize(0.15);
  690. // this.orientationWidget.setMinPixelSize(100);
  691. // this.orientationWidget.setMaxPixelSize(300);
  692. }
  693. generateTicks(numberOfTicks) {
  694. return (helper) => {
  695. const lastTickBounds = helper.getLastTickBounds();
  696. // compute tick marks for axes
  697. const scale = d3
  698. .scaleLinear()
  699. .domain([0.0, 1.0])
  700. .range([lastTickBounds[0], lastTickBounds[1]]);
  701. const samples = scale.ticks(numberOfTicks);
  702. const ticks = samples.map((tick) => scale(tick));
  703. // Replace minus "\u2212" with hyphen-minus "\u002D" so that parseFloat() works
  704. formatDefaultLocale({ minus: "\u002D" });
  705. const format = scale.tickFormat(ticks[0], ticks[ticks.length - 1], numberOfTicks);
  706. const tickStrings = ticks
  707. .map(format)
  708. .map((tick) => Number(parseFloat(tick).toPrecision(12)).toPrecision()); // d3 sometimes adds unwanted whitespace
  709. helper.setTicks(ticks);
  710. helper.setTickStrings(tickStrings);
  711. };
  712. }
  713. //添加箭头
  714. addArrow(velocitys) {
  715. this.clearArrow();
  716. this.arrowActors = [];
  717. velocitys.forEach(velocity => {
  718. velocity.points.forEach(point => {
  719. // const initialValues = {
  720. // base:0,
  721. // shape: 'arrow4points',
  722. // center: [point.x,point.y,point.z],
  723. // direction:[
  724. // velocity.vector[2],
  725. // velocity.vector[0],
  726. // velocity.vector[1]
  727. // ],
  728. // height: 10.0,
  729. // thickness: 1.5,
  730. // width: 3,
  731. // };
  732. // const vtk2d = vtk2DShape.newInstance(initialValues);
  733. const initialValues = {
  734. // base: 0,
  735. center: [point.x, point.y, point.z],
  736. // height: 10,
  737. // radius: 3,
  738. // resolution: 6,
  739. height: 10.0,
  740. radius:2,
  741. resolution: 6,
  742. direction: [
  743. // velocity.vector[2],
  744. // velocity.vector[0],
  745. // velocity.vector[1]
  746. velocity.vector[0],
  747. velocity.vector[1],
  748. velocity.vector[2]
  749. ],
  750. capping: true,
  751. };
  752. const vtk2d = vtkConeSource.newInstance(initialValues);
  753. const mapper = vtkMapper.newInstance();
  754. mapper.setInputConnection(vtk2d.getOutputPort());
  755. const actor = vtkActor.newInstance();
  756. actor.setMapper(mapper);
  757. actor.getProperty().setColor(REA);
  758. this.arrowActors.push(actor);
  759. // this.renderer.addActor(actor);
  760. })
  761. });
  762. for (let index = 0; index < this.arrowActors.length; index=index+30) {
  763. let actor =this.arrowActors[index];
  764. this.renderer.addActor(actor);
  765. };
  766. this.renderWindow.render();
  767. }
  768. clearArrow() {
  769. if (this.arrowActors) {
  770. this.arrowActors.forEach(actor => {
  771. this.renderer.removeActor(actor);
  772. });
  773. }
  774. }
  775. modelInit(validNodes, pipes) {
  776. this.renderWindowWith.resize();
  777. this.validNodes = validNodes;
  778. this.pipes = pipes;
  779. this.modelCreate();
  780. }
  781. modelCreate() {
  782. console.log("modelInit..");
  783. const points = this.polyData.getPoints();
  784. const lines = this.polyData.getLines();
  785. //无节点actor
  786. this.validNodes.forEach((node) => {
  787. points.insertNextPoint(parseFloat(node.x), parseFloat(node.y), parseFloat(node.z));
  788. });
  789. //无管道actor
  790. this.pipes.forEach((pipe) => {
  791. let sid = this.indexIdByPipeNodeId(pipe.snId);
  792. let eid = this.indexIdByPipeNodeId(pipe.enId);
  793. lines.insertNextCell([sid, eid]);
  794. });
  795. //有节点actors
  796. const sphereSource = vtkSphereSource.newInstance({
  797. center: [0, 0, 0],
  798. radius: 4.0,
  799. });
  800. const mapper = vtkMapper.newInstance();
  801. mapper.setInputConnection(sphereSource.getOutputPort());
  802. this.validNodes.forEach((node) => {
  803. const actor = vtkActor.newInstance();
  804. actor.setMapper(mapper);
  805. actor.setPosition(parseFloat(node.x), parseFloat(node.y), parseFloat(node.z));
  806. actor.getProperty().setColor(WHITE);
  807. const nodeActor = {};
  808. nodeActor.node = node;
  809. nodeActor.actor = actor;
  810. this.nodeActors.push(nodeActor);
  811. });
  812. //有管道actors
  813. this.pipes.forEach((pipe) => {
  814. const lineSource = vtkLineSource.newInstance();
  815. let point1 = this.pointByPipeNodeId(pipe.snId);
  816. let point2 = this.pointByPipeNodeId(pipe.enId);
  817. lineSource.setPoint1(point1);
  818. lineSource.setPoint2(point2);
  819. lineSource.setResolution(12);
  820. const actor = vtkActor.newInstance();
  821. const mapper = vtkMapper.newInstance();
  822. actor.setMapper(mapper);
  823. actor.getProperty().setLineWidth(3);//设置线宽
  824. actor.getProperty().setColor(WHITE);
  825. mapper.setInputConnection(lineSource.getOutputPort());
  826. const pipeActor = {};
  827. pipeActor.pipe = pipe;
  828. pipeActor.actor = actor;
  829. this.pipeActors.push(pipeActor);
  830. });
  831. this.modelShow();
  832. this.renderer.resetCamera();
  833. const cam = this.renderer.getActiveCamera();
  834. console.log("透视或平行相机", cam.getParallelProjection());
  835. // cam.setParallelProjection(true);
  836. // console.log("透视或平行相机",cam.getParallelProjection());
  837. console.log("水平视角使用", cam.getUseHorizontalViewAngle());
  838. // cam.setUseHorizontalViewAngle(true);
  839. console.log("平行位置投影比例", cam.getParallelScale());
  840. // cam.setParallelScale(600);
  841. console.log("窗口中心位置", cam.getWindowCenter());//[0, 0]
  842. // cam.setWindowCenter([1,-1]);
  843. console.log("相机视角", cam.getViewAngle());//30
  844. // cam.setViewAngle(90);
  845. console.log("摄像头位置", cam.getPosition());//[495544.15625, 5403554.25, 9989.55990464236]
  846. // cam.setPosition(495544.15625, 5403554.25, 9989.55990464236);
  847. console.log("相机焦点", cam.getFocalPoint());// [495544.15625, 5403554.25, 467.62999725341797]
  848. cam.setFocalPoint(495544.15625, 5403554.25, 467.62999725341797);
  849. console.log("相机方向", cam.getViewUp());//[0,1,0]
  850. // cam.setViewUp([0,1,1]);
  851. console.log("近远平面", cam.getClippingRange());// [9141.491986233214, 9913.53967078369]
  852. // cam.setClippingRange(9241.491986233214,9300.53967078369);
  853. //设置环境光和光照
  854. const light = vtkLight.newInstance();
  855. light.setColor(1.0, 1.0, 1.0); // 白色环境光
  856. light.setIntensity(3.0); // 强度为1.0
  857. this.renderer.addLight(light);
  858. // this.renderer.resetCamera();
  859. // cam.setPosition(0, 0, 9989);
  860. //相机焦点 495544.15625 5403554.25 467.62999725341797
  861. // cam.setPosition( 495544.15625+500, 5403554.25, 467.62999725341797);
  862. // cam.setPosition( 495544.15625-500, 5403554.25, 467.62999725341797);
  863. // cam.setViewUp([0,0,1]);
  864. // this.renderer.updateCamera();
  865. }
  866. modelClearShow() {
  867. this.renderer.removeActor(this.actor);
  868. this.renderer.removeActor(this.actor2);
  869. this.nodeActors.forEach((nodeActor) => {
  870. this.renderer.removeActor(nodeActor.actor);
  871. });
  872. this.pipeActors.forEach((pipeActor) => {
  873. this.renderer.removeActor(pipeActor.actor);
  874. });
  875. }
  876. wayClearShow(){
  877. this.wayActors.forEach((wayAcotor) => {
  878. this.renderer.removeActor(wayAcotor);
  879. });
  880. }
  881. modelShow() {
  882. this.modelClearShow();
  883. if (!this.isSelectNode) {//不选择节点
  884. this.renderer.addActor(this.actor2);
  885. }
  886. if (!this.isSelectPipe) {//不选择管道
  887. this.renderer.addActor(this.actor);
  888. }
  889. if (this.isSelectNode) {//选择节点
  890. this.nodeActors.forEach((nodeActor) => {
  891. this.renderer.addActor(nodeActor.actor);
  892. });
  893. }
  894. if (this.isSelectPipe) {//选择管道
  895. this.pipeActors.forEach((pipeActor) => {
  896. this.renderer.addActor(pipeActor.actor);
  897. });
  898. }
  899. // this.addjc(495530.903,5403828.821,432.40);
  900. // this.addpname(495530.903,5403829.821,432.40);
  901. // this.addSoure(495530.903,5403829.821,432.40);
  902. // this.addpname(495530.903,5403829.821,432.40);
  903. this.initPname();
  904. this.renderer.addActor(this.axesActor);
  905. // this.rotateY();
  906. }
  907. selectNodes() {
  908. this.isSelectNode = true;
  909. this.isSelectPipe = false;
  910. this.modelShow();
  911. document.addEventListener("mousemove", throttleMouseHandler);
  912. this.renderWindow.render();
  913. }
  914. selectPipes() {
  915. this.isSelectNode = false;
  916. this.isSelectPipe = true;
  917. document.addEventListener("mousemove", throttleMouseHandler);
  918. this.modelShow();
  919. this.renderWindow.render();
  920. }
  921. selectNoting() {
  922. this.isSelectNode = false;
  923. this.isSelectPipe = false;
  924. this.modelShow();
  925. this.renderWindow.render();
  926. }
  927. /**
  928. * 根据id 获取对应的编号
  929. */
  930. indexIdByPipeNodeId(nid) {
  931. for (let index = 0; index < this.validNodes.length; index++) {
  932. const node = this.validNodes[index];
  933. if (node.id == nid) {
  934. return index;
  935. }
  936. }
  937. return 0;
  938. }
  939. pipeByPid(pid) {
  940. console.log("pipeByPid",this.pipes.length)
  941. for (let index = 0; index < this.pipes.length; index++) {
  942. const pipe = this.pipes[index];
  943. if (pipe.id == pid) {
  944. return pipe;
  945. }
  946. }
  947. }
  948. pointByPipeNodeId(nid) {
  949. for (let index = 0; index < this.validNodes.length; index++) {
  950. const node = this.validNodes[index];
  951. if (node.id == nid) {
  952. return [parseFloat(node.x), parseFloat(node.y), parseFloat(node.z)];
  953. }
  954. }
  955. return 0;
  956. }
  957. clearAllShowGj() {
  958. this.renderer.addActor(this.gjActor);
  959. this.renderer.addActor(this.gjscalarBarActor);
  960. this.renderer.removeActor(this.jgActor);
  961. this.renderer.removeActor(this.scalarBarActor);
  962. this.isSelectNode = false;
  963. this.isSelectPipe = false;
  964. this.wayClearShow();
  965. this.modelClearShow();
  966. this.isJg = true; //显示结果
  967. document.removeEventListener("mousemove", throttleMouseHandler);
  968. }
  969. clearModeAddJg() {
  970. this.renderer.addActor(this.jgActor);
  971. this.renderer.addActor(this.scalarBarActor);
  972. this.isSelectNode = false;
  973. this.isSelectPipe = false;
  974. this.renderer.removeActor(this.gjActor);
  975. this.renderer.removeActor(this.gjscalarBarActor)
  976. this.wayClearShow();
  977. this.modelClearShow();
  978. this.isJg = true; //显示结果
  979. document.removeEventListener("mousemove", throttleMouseHandler);
  980. }
  981. clearJgAddMode() {
  982. this.renderer.removeActor(this.scalarBarActor);
  983. this.renderer.removeActor(this.jgActor);
  984. this.renderer.removeActor(this.gjActor);
  985. this.renderer.removeActor(this.gjscalarBarActor)
  986. this.clearArrow();
  987. this.modelShow();
  988. this.wayClearShow();
  989. this.isJg = false; //不显示结果
  990. }
  991. clearAll() {
  992. this.renderer.removeActor(this.jgActor);
  993. this.renderer.removeActor(this.scalarBarActor);
  994. this.renderer.removeActor(this.gjActor);
  995. this.renderer.removeActor(this.gjscalarBarActor)
  996. this.clearArrow();
  997. this.modelClearShow();
  998. this.wayClearShow();
  999. this.renderWindow.render();
  1000. }
  1001. //选中指定点
  1002. selectByNodeId(nid) {
  1003. console.log("selectByNodeId", nid);
  1004. // if (this.isSelectNode) {
  1005. if (this.lastActor != null) {
  1006. this.renderer.removeActor(this.lastActor);
  1007. }
  1008. this.nodeActors.forEach(nodeActor => {
  1009. if (nid == nodeActor.node.id) {
  1010. nodeActor.actor.getProperty().setColor(GREEN);
  1011. this.lastActor = nodeActor.actor;
  1012. this.selectObj = nodeActor.node;
  1013. this.renderer.addActor(this.lastActor);
  1014. this.renderWindow.render();
  1015. return;
  1016. }
  1017. });
  1018. // }
  1019. }
  1020. //选中指定管道
  1021. selectByPipeId(pid) {
  1022. console.log("selectByPipeId", pid);
  1023. // if (this.isSelectPipe) {
  1024. if (this.lastActor != null) {
  1025. this.renderer.removeActor(this.lastActor);
  1026. }
  1027. this.pipeActors.forEach(pipeActor => {
  1028. if (pid == pipeActor.pipe.id) {
  1029. pipeActor.actor.getProperty().setColor(GREEN);
  1030. this.lastActor = pipeActor.actor;
  1031. this.selectObj = pipeActor.pipe;
  1032. this.renderer.addActor(this.lastActor);
  1033. this.renderWindow.render();
  1034. return;
  1035. }
  1036. });
  1037. }
  1038. // }
  1039. }
  1040. function createRGBStringFromRGBValues(rgb) {
  1041. if (rgb.length !== 3) {
  1042. return 'rgb(0, 0, 0)';
  1043. }
  1044. return `rgb(${(rgb[0] * 255).toString()}, ${(rgb[1] * 255).toString()}, ${(
  1045. rgb[2] * 255
  1046. ).toString()})`;
  1047. }
  1048. function pickOnMouseEvent(event) {
  1049. if (vtkmodel.interactor.isAnimating()) {
  1050. // We should not do picking when interacting with the scene
  1051. return;
  1052. }
  1053. const [x, y] = eventToWindowXY(event);
  1054. // console.log([x,y]);
  1055. // if(!vtkmodel.getPointData){//没有数据
  1056. // return ;
  1057. // }
  1058. // console.log(vtkmodel.hardwareSelector.getPointData());
  1059. // if(!vtkmodel.renderer.getActors().size==0){
  1060. // return;
  1061. // }
  1062. vtkmodel.hardwareSelector
  1063. .getSourceDataAsync(vtkmodel.renderer, x, y, x, y)
  1064. .then((result) => {
  1065. if (result) {
  1066. processSelections(result.generateSelection(x, y, x, y));
  1067. // processSelections(result.generateSelection(x, y, x, y));
  1068. } else {
  1069. // processSelections(null);
  1070. }
  1071. });
  1072. }
  1073. function eventToWindowXY(event) {
  1074. // We know we are full screen => window.innerXXX
  1075. // Otherwise we can use pixel device ratio or else...
  1076. var left = event.pageX ;
  1077. var top = event.pageY ;
  1078. vtkmodel.epageX=left;
  1079. vtkmodel.epageY=top;
  1080. const { clientX, clientY } = event;
  1081. const [width, height] = vtkmodel.apiSpecificRenderWindow.getSize();
  1082. const x = Math.round((width * clientX) / window.innerWidth);
  1083. const y = Math.round(height * (1 - clientY / window.innerHeight)); // Need to flip Y
  1084. // console.log(x,y)
  1085. return [x, y];
  1086. }
  1087. function processSelections(selections) {
  1088. if (!selections || selections.length === 0) {//没有选择
  1089. if (vtkmodel.lastActor == null) {
  1090. } else {
  1091. // vtkmodel.lastActor.getProperty().setColor(WHITE);
  1092. // vtkmodel.renderWindow.render();
  1093. }
  1094. // vtkmodel.lastActor = null;
  1095. return;
  1096. }
  1097. const {
  1098. worldPosition: rayHitWorldPosition,
  1099. compositeID,
  1100. prop,
  1101. propID,
  1102. attributeID,
  1103. } = selections[0].getProperties();
  1104. if (vtkmodel.lastActor != null) {
  1105. vtkmodel.lastActor.getProperty().setColor(WHITE);
  1106. vtkmodel.renderWindow.render();
  1107. }
  1108. vtkmodel.nodeActors.forEach((nodeActor) => {
  1109. if (prop == nodeActor.actor) {
  1110. vtkmodel.selectObj = nodeActor.node;
  1111. prop.getProperty().setColor(GREEN);
  1112. vtkmodel.lastActor = prop;
  1113. // console.log(vtkmodel.selectObj );
  1114. }
  1115. });
  1116. vtkmodel.pipeActors.forEach((pipeActor) => {
  1117. if (prop == pipeActor.actor) {
  1118. vtkmodel.selectObj = pipeActor.pipe;
  1119. prop.getProperty().setColor(GREEN);
  1120. vtkmodel.lastActor = prop;
  1121. // console.log(vtkmodel.selectObj );
  1122. }
  1123. });
  1124. if (vtkmodel.isJg) {//结果数据选择cell
  1125. if (vtkmodel.hardwareSelector.getFieldAssociation() ===
  1126. FieldAssociations.FIELD_ASSOCIATION_POINTS) {//选择点
  1127. } else {
  1128. //选择Cell
  1129. const input = prop.getMapper().getInputData();
  1130. if (!input.getCells()) {
  1131. input.buildCells();
  1132. // return;
  1133. }
  1134. const cellPoints = input.getCellPoints(attributeID);
  1135. if (cellPoints) {
  1136. const pointIds = cellPoints.cellPointIds;
  1137. // Find the closest cell point, and use that as cursor position
  1138. const points = Array.from(pointIds).map((pointId) =>
  1139. input.getPoints().getPoint(pointId)
  1140. );
  1141. const scalarDataArray = input.getPointData().getScalars().getData();
  1142. console.log(scalarDataArray[pointIds[0]]);//节点标量数据
  1143. // pointIds.forEach(pointId=>{
  1144. // console.log(input.getPointData().getScalars().getName());
  1145. // console.log(scalarDataArray[pointId]);//节点标量数据
  1146. // });
  1147. vtkmodel.selectJgPointId = pointIds[0];
  1148. }
  1149. }
  1150. } else {
  1151. vtkmodel.renderWindow.render();
  1152. }
  1153. }
  1154. // 异步函数,用于读取JS文件并转换为ArrayBuffer
  1155. async function fetchJSFileAsArrayBuffer(url) {
  1156. try {
  1157. // 使用fetch API异步加载JS文件
  1158. const response = await fetch(url, {
  1159. method: 'GET',
  1160. responseType: 'arraybuffer' // 指定响应类型为arraybuffer
  1161. });
  1162. // 确保响应成功
  1163. if (!response.ok) {
  1164. throw new Error(`HTTP error! status: ${response.status}`);
  1165. }
  1166. // 获取ArrayBuffer对象
  1167. const arrayBuffer = await response.arrayBuffer();
  1168. // 可选:验证转换后的ArrayBuffer内容
  1169. const decoder = new TextDecoder(); // 使用TextDecoder将ArrayBuffer解码为字符串
  1170. const content = decoder.decode(arrayBuffer);
  1171. // console.log('File content:', content);
  1172. return arrayBuffer;
  1173. } catch (error) {
  1174. console.error('Error fetching the file:', error);
  1175. throw error; // 重新抛出错误以便上层处理
  1176. }
  1177. }
  1178. const throttleMouseHandler = throttle(pickOnMouseEvent, 20);
  1179. const WHITE = [1, 0.2, 1];
  1180. const GREEN = [0.1, 0.8, 0.1];
  1181. const REA = [1, 0, 0];
  1182. const JCDCOLOR = [0.5, 0, 0];
  1183. const SOURECOLOR = [0.5, 0.5, 0];
  1184. const TEXTCOLOR = [1, 1, 1];
  1185. const vtkmodel = new VtkModel();
  1186. export { vtkmodel };