move.service.ts 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
  2. import { ConfigService } from '@nestjs/config';
  3. import { readFileSync } from 'fs';
  4. import { join } from 'path';
  5. import { CacheService } from 'src/cache/cache.service';
  6. import { RotateService } from 'src/rotate/rotate.service';
  7. // import * as BreakPointIds from '../../ws/points-BreakPointId.json';
  8. // import { SceneService } from 'src/scene/scene.service';
  9. const seqExeAsyncFn = (asyncFn) => {
  10. let runPromise = null;
  11. return function seq(...args) {
  12. if (!runPromise) {
  13. //debugger;
  14. runPromise = asyncFn.apply(this, args);
  15. runPromise.then((data) => {
  16. //debugger;
  17. // console.log('seq result', data);
  18. });
  19. runPromise.then(() => (runPromise = null));
  20. return runPromise;
  21. } else {
  22. return runPromise.then(() => seq.apply(this, args));
  23. }
  24. };
  25. };
  26. @Injectable()
  27. export class MoveService implements OnModuleInit {
  28. constructor(
  29. private cacheService: CacheService,
  30. private rotateService: RotateService,
  31. private configService: ConfigService,
  32. ) {}
  33. private logger: Logger = new Logger('MoveService');
  34. private Actions = {
  35. Clicking: 1,
  36. Rotation: 1014,
  37. Joystick: 15,
  38. };
  39. public users = this.rotateService.users;
  40. private reply = {
  41. traceIds: [],
  42. vehicle: null,
  43. mediaSrc: null,
  44. moveOver:false,
  45. moveStart:false,
  46. newUserStates: [
  47. {
  48. userId: 'dcff36ae4fc1d',
  49. playerState: {
  50. roomTypeId: '',
  51. person: 0,
  52. avatarId: '',
  53. skinId: '',
  54. roomId: '',
  55. isHost: false,
  56. isFollowHost: false,
  57. skinDataVersion: '',
  58. avatarComponents: '',
  59. nickName: '',
  60. movingMode: 0,
  61. attitude: '',
  62. areaName: '',
  63. pathName: '',
  64. pathId: '',
  65. avatarSize: 1,
  66. extra: '',
  67. prioritySync: false,
  68. player: {
  69. position: { x: -700, y: 0, z: 0 },
  70. angle: {
  71. pitch: 0,
  72. yaw: 0,
  73. roll: 0,
  74. },
  75. },
  76. camera: {
  77. position: { x: -1145, y: 0, z: 160 },
  78. angle: {
  79. pitch: 0,
  80. yaw: 0,
  81. roll: 0,
  82. },
  83. },
  84. cameraCenter: { x: -700, y: 0, z: 0 },
  85. },
  86. renderInfo: {
  87. renderType: 0,
  88. videoFrame: null,
  89. cameraStateType: 3,
  90. isMoving: 1,
  91. needIfr: 0,
  92. isVideo: 0,
  93. stillFrame: 0,
  94. isRotating: 0,
  95. isFollowing: 0,
  96. clientPanoTitlesBitmap: [],
  97. clientPanoTreceId: '',
  98. prefetchVideoId: '',
  99. noMedia: false,
  100. },
  101. event: null,
  102. relation: 1,
  103. },
  104. ],
  105. actionResponses: [
  106. {
  107. actionType: 1,
  108. pointType: 100,
  109. extra: '',
  110. traceId: '',
  111. packetId: '',
  112. nps: [],
  113. peopleNum: 0,
  114. zoneId: '',
  115. echoMsg: '',
  116. reserveDetail: null,
  117. userWithAvatarList: [],
  118. newUserStates: [],
  119. code: 0,
  120. msg: '',
  121. },
  122. ],
  123. getStateType: 0,
  124. code: 0,
  125. msg: 'OK',
  126. };
  127. private breakPointInfo: any;
  128. private cameraInfos = [];
  129. public sendingFrameForJoystick = false;
  130. // eslint-disable-next-line @typescript-eslint/no-empty-function
  131. async onModuleInit() {
  132. const app_id = '0000000003';
  133. const prefix = '/mnt/metaverse/scene';
  134. let path;
  135. // let path: string;
  136. if (process.env.NODE_ENV === 'development') {
  137. path = join(__dirname, `../ws/${app_id}/points-${app_id}.json`);
  138. console.log('测试服JSON', path);
  139. }
  140. if (process.env.NODE_ENV === 'production') {
  141. path = join(`${prefix}/${app_id}/points-${app_id}.json`);
  142. console.log('正式服JSON', path);
  143. }
  144. this.loadJSON(path);
  145. }
  146. async loadJSON(path) {
  147. try {
  148. const data = await readFileSync(path);
  149. const BreakPointInfo = JSON.parse(Buffer.from(data).toString('utf-8'));
  150. this.breakPointInfo = BreakPointInfo;
  151. // console.log('BreakPointInfo', BreakPointInfo);
  152. } catch (error) {
  153. this.logger.error('load-json-error', error);
  154. }
  155. }
  156. init(app_id, userId) {
  157. const user = {
  158. appId: null,
  159. userId: null,
  160. breakPointId: null,
  161. roomId: null,
  162. player: {
  163. position: { x: -700, y: 0, z: 0 },
  164. angle: {
  165. pitch: 0,
  166. yaw: 0,
  167. roll: 0,
  168. },
  169. },
  170. camera: {
  171. position: { x: -1145, y: 0, z: 160 },
  172. angle: {
  173. pitch: 0,
  174. yaw: 0,
  175. roll: 0,
  176. },
  177. },
  178. rotateInfo: {
  179. frameIndex: 0,
  180. horizontal_move: 0,
  181. },
  182. moveInfo: {},
  183. // traceIds: [],
  184. // actionResponses:[]
  185. };
  186. user.appId = app_id;
  187. user.userId = userId;
  188. user.breakPointId = Number(this.configService.get('app.startPoint')) || 0;
  189. console.log('user-init', user);
  190. this.users[userId] = user;
  191. }
  192. async move(pathArray, actionRequest) {
  193. try {
  194. const userId = actionRequest['user_id'];
  195. const traceId = actionRequest['trace_id'];
  196. const actionType = actionRequest['action_type'];
  197. const user = this.users[userId];
  198. const appId = user.appId;
  199. const path = pathArray || [100, 101, 102]; //需要计算路径
  200. const angle = user.camera.angle.yaw % 45; //纠正需要
  201. const replys = [];
  202. const traceIds = [];
  203. traceIds.push(traceId);
  204. const checkReplys = await this.modeifyCameraAngle(
  205. angle,
  206. userId,
  207. traceId,
  208. actionType,
  209. );
  210. for (let i = 0; i < checkReplys.length; ++i) {
  211. checkReplys[i].actionResponses[0].actionType = actionType;
  212. }
  213. //replys['P' + user.breakPointId + 'T' + user.breakPointId] = checkReplys;
  214. replys.push(checkReplys);
  215. console.log('路径:' + path);
  216. //过渡传到缓存里
  217. this.reply.traceIds = traceIds;
  218. this.reply['newUserStates'][0].userId = userId;
  219. this.reply['actionResponses'][0].traceId = traceId;
  220. const index = Math.floor((user.camera.angle.yaw + 1) / 45); //过渡需要
  221. for (let i = 0; i < path.length - 1; ++i) {
  222. let pathReplys = [];
  223. const start_break_point_id = path[i];
  224. const end_break_point_id = path[i + 1];
  225. //读redis里的数据,按照frame_index的大小排序
  226. const key =
  227. 'moveframe:app_id:' +
  228. appId +
  229. ':start_break_point_id:' +
  230. start_break_point_id +
  231. ':end_break_point_id:' +
  232. end_break_point_id +
  233. ':angle:' +
  234. index;
  235. const moveFramesRes = await this.cacheService.get(key);
  236. if (moveFramesRes == null) {
  237. return replys;
  238. }
  239. const moveFrames = JSON.parse(moveFramesRes);
  240. pathReplys = this.createCacheReplys(
  241. appId,
  242. moveFrames,
  243. traceId,
  244. userId,
  245. start_break_point_id,
  246. end_break_point_id,
  247. false,
  248. );
  249. if (i == path.length - 2) {
  250. pathReplys[pathReplys.length - 1][
  251. 'newUserStates'
  252. ][0].renderInfo.isMoving = 0;
  253. }
  254. for (let j = 0; j < pathReplys.length; ++j) {
  255. pathReplys[j].actionResponses[0].actionType = actionType;
  256. }
  257. replys.push(pathReplys);
  258. //replys['P' + start_break_point_id + 'T' + end_break_point_id] =pathReplys;
  259. }
  260. return replys;
  261. } catch (error) {
  262. console.log('MoveService', error);
  263. }
  264. }
  265. createCacheReplys(
  266. appId,
  267. moveFrames,
  268. traceId,
  269. userId,
  270. startBreakPointId,
  271. endBreakPointId,
  272. isFromUser,
  273. ) {
  274. const replys = [];
  275. const startPosition = this.breakPointInfo[startBreakPointId].position;
  276. const endPosition = this.breakPointInfo[endBreakPointId].position;
  277. const angle = this.getAngle(
  278. startPosition,
  279. {
  280. x: startPosition.x + 1,
  281. y: startPosition.y,
  282. },
  283. endPosition,
  284. );
  285. const user = this.users[userId];
  286. let i;
  287. for (i = 1; i < moveFrames.length; i += 5) {
  288. const moveFrame = moveFrames[i];
  289. const reply = JSON.parse(JSON.stringify(this.reply));
  290. if (reply.traceIds.indexOf(traceId) == -1) {
  291. reply.traceIds.push(traceId);
  292. }
  293. reply['newUserStates'][0].userId = userId;
  294. if (!isFromUser) {
  295. reply['newUserStates'][0].playerState.player.position = {
  296. x:
  297. startPosition.x +
  298. ((endPosition.x - startPosition.x) / moveFrames.length) * i,
  299. y:
  300. startPosition.y +
  301. ((endPosition.y - startPosition.y) / moveFrames.length) * i,
  302. z:
  303. startPosition.z +
  304. ((endPosition.z - startPosition.z) / moveFrames.length) * i,
  305. };
  306. reply['newUserStates'][0].playerState.player.angle.yaw = angle;
  307. reply['newUserStates'][0].playerState.cameraCenter =
  308. reply['newUserStates'][0].playerState.player.position;
  309. } else {
  310. reply['newUserStates'][0].playerState.player.position = JSON.parse(
  311. JSON.stringify(user.player.position),
  312. );
  313. reply['newUserStates'][0].playerState.player.angle.yaw =
  314. user.player.angle.yaw;
  315. reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  316. JSON.stringify(user.player.position),
  317. );
  318. }
  319. reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  320. JSON.stringify(moveFrame.camera_position),
  321. );
  322. if (i == 1) {
  323. console.log('move-2' + moveFrame.camera_angle.yaw);
  324. }
  325. if (moveFrame.camera_angle.yaw < 0) {
  326. moveFrame.camera_angle.yaw += 360;
  327. } else if (moveFrame.camera_angle.yaw > 359) {
  328. moveFrame.camera_angle.yaw -= 360;
  329. }
  330. reply['newUserStates'][0].playerState.camera.angle =
  331. moveFrame.camera_angle;
  332. reply['newUserStates'][0].renderInfo.isMoving = 1;
  333. reply['actionResponses'][0].traceId = traceId;
  334. reply.mediaSrc =
  335. '/' +
  336. appId +
  337. '/' +
  338. startBreakPointId +
  339. '/' +
  340. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  341. '/' +
  342. moveFrame.file_name +
  343. '?m=' +
  344. new Date().getTime();
  345. if (moveFrame.file_name == '6_7_0.0031.h264') {
  346. debugger;
  347. }
  348. reply.startBreakPointId = startBreakPointId;
  349. reply.endBreakPointId = endBreakPointId;
  350. replys.push(reply);
  351. }
  352. if (i != moveFrames.length - 1) {
  353. i = moveFrames.length - 1;
  354. const moveFrame = moveFrames[i];
  355. const reply = JSON.parse(JSON.stringify(this.reply));
  356. reply.traceIds.push(traceId);
  357. reply['newUserStates'][0].userId = userId;
  358. if (!isFromUser) {
  359. reply['newUserStates'][0].playerState.player.position = {
  360. x:
  361. startPosition.x +
  362. ((endPosition.x - startPosition.x) / moveFrames.length) * i,
  363. y:
  364. startPosition.y +
  365. ((endPosition.y - startPosition.y) / moveFrames.length) * i,
  366. z:
  367. startPosition.z +
  368. ((endPosition.z - startPosition.z) / moveFrames.length) * i,
  369. };
  370. reply['newUserStates'][0].playerState.player.angle.yaw = angle;
  371. reply['newUserStates'][0].playerState.cameraCenter =
  372. reply['newUserStates'][0].playerState.player.position;
  373. } else {
  374. reply['newUserStates'][0].playerState.player.position = JSON.parse(
  375. JSON.stringify(user.player.position),
  376. );
  377. reply['newUserStates'][0].playerState.player.angle.yaw =
  378. user.player.angle.yaw;
  379. reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  380. JSON.stringify(user.player.position),
  381. );
  382. }
  383. reply['newUserStates'][0].playerState.camera.position =
  384. moveFrame.camera_position;
  385. if (moveFrame.camera_angle.yaw < 0) {
  386. moveFrame.camera_angle.yaw += 360;
  387. } else if (moveFrame.camera_angle.yaw > 359) {
  388. moveFrame.camera_angle.yaw -= 360;
  389. }
  390. reply['newUserStates'][0].playerState.camera.angle =
  391. moveFrame.camera_angle;
  392. reply['actionResponses'][0].traceId = traceId;
  393. reply.mediaSrc =
  394. '/' +
  395. appId +
  396. '/' +
  397. startBreakPointId +
  398. '/' +
  399. moveFrame.file_name.substring(0, moveFrame.file_name.indexOf('.')) +
  400. '/' +
  401. moveFrame.file_name +
  402. '?m=' +
  403. new Date().getTime();
  404. reply.startBreakPointId = startBreakPointId;
  405. reply.endBreakPointId = endBreakPointId;
  406. replys.push(reply);
  407. }
  408. return replys;
  409. }
  410. //需要通知user,人物和相机走到哪一个呼吸点位了
  411. updateUser(userId, breakPointId, lastReply) {
  412. const user = this.users[userId];
  413. user.breakPointId = breakPointId;
  414. console.log('handlejoystick:updateUser');
  415. if (lastReply.actionResponses[0].actionType != 15) {
  416. user.player.position =
  417. lastReply['newUserStates'][0].playerState.player.position;
  418. }
  419. user.player.angle = lastReply['newUserStates'][0].playerState.player.angle;
  420. user.camera.position =
  421. lastReply['newUserStates'][0].playerState.camera.position;
  422. user.camera.angle = lastReply['newUserStates'][0].playerState.camera.angle;
  423. }
  424. getBreakPoints(actionRequest) {
  425. const userId = actionRequest['user_id'];
  426. const traceId = actionRequest['trace_id'];
  427. const actionType = actionRequest['action_type'];
  428. const user = this.users[userId];
  429. const appId = user.appId;
  430. const breakPointId = user.breakPointId;
  431. const reply = {
  432. actionType: actionType,
  433. pointType: 100,
  434. extra: '',
  435. traceId: traceId,
  436. packetId: '',
  437. nps: [],
  438. peopleNum: 0,
  439. zoneId: '',
  440. echoMsg: '',
  441. reserveDetail: null,
  442. userWithAvatarList: [],
  443. newUserStates: [],
  444. code: 0,
  445. msg: '',
  446. };
  447. //const breakPoints = await this.cacheService.get('breakpoints:app_id:'+appId+':break_point_id:'+breakPointId);
  448. //获取redis表全部元素,'breakpoints:app_id:'+appId+':break_point_id:'开头的
  449. //const keys = await this.cacheService.keys(`breakpoints:app_id:${appId}*`);
  450. for (const key in this.breakPointInfo) {
  451. const breakPoint = this.breakPointInfo[key];
  452. //const breakPoint = JSON.parse(breakPointRes);
  453. //const position = breakPoint.position;
  454. reply['nps'].push({
  455. position: breakPoint.position,
  456. breakPointId: Number(key),
  457. });
  458. }
  459. // for (let i = 0; i < keys.length; ++i) {
  460. // const breakPointRes = await this.cacheService.get(keys[i]);
  461. // const breakPoint = JSON.parse(breakPointRes);
  462. // const position = breakPoint.position;
  463. // reply['nps'].push({
  464. // position: position,
  465. // breakPointId: breakPoint.breakPointId,
  466. // });
  467. // }
  468. return reply;
  469. }
  470. getAngle(point, point1, point2) {
  471. const x1 = point1.x - point.x;
  472. const y1 = point1.y - point.y;
  473. const x2 = point2.x - point.x;
  474. const y2 = point2.y - point.y;
  475. const dot = x1 * x2 + y1 * y2;
  476. const det = x1 * y2 - y1 * x2;
  477. const angle = (Math.atan2(det, dot) / Math.PI) * 180;
  478. return (angle + 360) % 360;
  479. }
  480. async stop(traceId, userId, breakPointId, cameraAngle, playerAngle) {
  481. //const breakPointId = movePointIds.substring(movePointIds.indexOf('-') + 1);
  482. const user = this.users[userId];
  483. const startBreakPointId = user.breakPointId;
  484. user.breakPointId = breakPointId;
  485. const appId = user.appId;
  486. const breakPoint = this.breakPointInfo[breakPointId];
  487. user.player.position = breakPoint.position;
  488. user.player.angle = playerAngle;
  489. const rotateKey =
  490. 'rotateframe:app_id:' +
  491. appId +
  492. ':frame_index:' +
  493. cameraAngle.yaw +
  494. ':break_point_id:' +
  495. breakPointId;
  496. const rotateDataRes = await this.cacheService.get(rotateKey);
  497. const rotateData = JSON.parse(rotateDataRes);
  498. user.camera.position = rotateData.cameraPosition;
  499. user.camera.angle = rotateData.cameraAngle;
  500. const reply = JSON.parse(JSON.stringify(this.reply));
  501. reply.traceIds.push(traceId);
  502. reply['newUserStates'][0].userId = userId;
  503. reply['newUserStates'][0].playerState.player.position = breakPoint.position;
  504. reply['newUserStates'][0].playerState.player.angle = playerAngle;
  505. reply['newUserStates'][0].playerState.camera.position =
  506. rotateData.cameraPosition;
  507. reply['newUserStates'][0].playerState.camera.angle = rotateData.cameraAngle;
  508. reply['newUserStates'][0].playerState.cameraCenter = breakPoint.position;
  509. reply['actionResponses'][0].traceId = traceId;
  510. reply.mediaSrc =
  511. '/' +
  512. appId +
  513. '/' +
  514. breakPointId +
  515. '/' +
  516. rotateData.directory +
  517. '/' +
  518. rotateData.fileName +
  519. '?m=' +
  520. new Date().getTime();
  521. reply.startBreakPointId = startBreakPointId;
  522. reply.endBreakPointId = breakPointId;
  523. reply['newUserStates'][0].renderInfo.isMoving = 0;
  524. return reply;
  525. }
  526. // 顺序旋转请求
  527. seqExeJoystick = seqExeAsyncFn(this.joystick);
  528. // async joystick(actionRequest) {
  529. // try {
  530. // const userId = actionRequest['user_id'];
  531. // const traceId = actionRequest['trace_id'];
  532. // const dir_action = actionRequest['dir_action'];
  533. // const actionType = actionRequest['action_type'];
  534. // const user = this.users[userId];
  535. // const breakPointId = user.breakPointId;
  536. // const appId = user.appId;
  537. // const replys = [];
  538. // const step = 0.3;
  539. // const closestDis = 85; //小于这个距离就跳到邻居呼吸点
  540. // const distance = step * dir_action.speed_level;
  541. // let angle = null;
  542. // let move_angle = dir_action.move_angle + user.camera.angle.yaw;
  543. // move_angle = move_angle % 360;
  544. // //TODO 临时增加断言
  545. // const playerPosition: Point = { x: 0, y: 0, z: 0 };
  546. // playerPosition.x =
  547. // user.player.position.x +
  548. // distance * Math.cos((move_angle / 360) * 2 * Math.PI);
  549. // playerPosition.y =
  550. // user.player.position.y +
  551. // distance * Math.sin((move_angle / 360) * 2 * Math.PI);
  552. // //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
  553. // let chooseBreakPointId = null;
  554. // const breakPoint = this.breakPointInfo[breakPointId];
  555. // const surroundPointIds = breakPoint.contact;
  556. // //const neighAngles = [];
  557. // const traceIds = [];
  558. // user.player.angle.yaw = move_angle;
  559. // traceIds.push(traceId);
  560. // this.reply.traceIds = traceIds;
  561. // this.reply['newUserStates'][0].userId = userId;
  562. // this.reply['actionResponses'][0].traceId = traceId;
  563. // this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
  564. // this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  565. // JSON.stringify(user.camera.angle),
  566. // );
  567. // this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  568. // JSON.stringify(user.camera.position),
  569. // );
  570. // this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  571. // JSON.stringify(breakPoint.position),
  572. // );
  573. // this.reply['newUserStates'][0].renderInfo.isMoving = 1;
  574. // this.reply['actionResponses'][0].traceId = traceId;
  575. // this.reply.mediaSrc = null;
  576. // if (surroundPointIds.length == 1) {
  577. // return await this.moveDirect(
  578. // playerPosition,
  579. // closestDis,
  580. // breakPointId,
  581. // surroundPointIds[0],
  582. // appId,
  583. // userId,
  584. // traceId,
  585. // actionType,
  586. // );
  587. // }
  588. // let count = 0;
  589. // const neighPoints = [];
  590. // //人在哪个角度
  591. // let _angle = this.getAngle(
  592. // breakPoint.position,
  593. // { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  594. // playerPosition,
  595. // );
  596. // if (_angle < 0) {
  597. // _angle += 360;
  598. // }
  599. // let singleInfo = null;
  600. // for (let i = 0; i < surroundPointIds.length; ++i) {
  601. // const neighPoint = this.breakPointInfo[surroundPointIds[i]];
  602. // neighPoint.breakPointId = surroundPointIds[i];
  603. // angle = this.getAngle(
  604. // breakPoint.position,
  605. // { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  606. // neighPoint.position,
  607. // );
  608. // if (angle < 0) {
  609. // angle += 360;
  610. // }
  611. // //if(angle<45&&angle!=0){
  612. // if (
  613. // // Math.abs(angle - move_angle) < 45 &&
  614. // // Math.abs(angle - move_angle) != 0
  615. // Math.abs(angle - _angle) < 45 &&
  616. // Math.abs(angle - _angle) != 0
  617. // ) {
  618. // neighPoint.angle = angle;
  619. // neighPoints.push(neighPoint);
  620. // ++count;
  621. // } else if (Math.abs(angle - move_angle) == 0) {
  622. // return await this.moveDirect(
  623. // playerPosition,
  624. // closestDis,
  625. // breakPointId,
  626. // surroundPointIds[i],
  627. // appId,
  628. // userId,
  629. // traceId,
  630. // actionType,
  631. // );
  632. // }
  633. // //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
  634. // if (angle == 0 && Math.abs(360 - _angle) < 45) {
  635. // neighPoint.angle = angle;
  636. // neighPoints.push(neighPoint);
  637. // ++count;
  638. // }
  639. // if(Math.abs(angle - move_angle)<45||Math.abs(angle+360 - move_angle)<45){
  640. // if(singleInfo == null){
  641. // singleInfo = {
  642. // angle:angle,
  643. // breakPointId:surroundPointIds[i]
  644. // }
  645. // }
  646. // }
  647. // }
  648. // if (count == 2) {
  649. // //人物移动
  650. // user.player.position = JSON.parse(JSON.stringify(playerPosition));
  651. // this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  652. // JSON.stringify(playerPosition),
  653. // );
  654. // }
  655. // else{
  656. // if(singleInfo != null){
  657. // return await this.moveDirect(
  658. // playerPosition,
  659. // closestDis,
  660. // breakPointId,
  661. // singleInfo.breakPointId,
  662. // appId,
  663. // userId,
  664. // traceId,
  665. // actionType,
  666. // );
  667. // }
  668. // else if (count == 1) {
  669. // return await this.moveDirect(
  670. // playerPosition,
  671. // closestDis,
  672. // breakPointId,
  673. // neighPoints[0].breakPointId,
  674. // appId,
  675. // userId,
  676. // traceId,
  677. // actionType,
  678. // );
  679. // }
  680. // else if (count == 0) {
  681. // this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  682. // JSON.stringify(user.player.position),
  683. // );
  684. // this.reply.actionResponses[0].actionType = actionType;
  685. // return this.reply;
  686. // }
  687. // }
  688. // //count == 2
  689. // //超出范围了
  690. // if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  691. // let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
  692. // if (neighPoints[0].angle == 0) {
  693. // offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
  694. // }
  695. // let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
  696. // if (neighPoints[1].angle == 0) {
  697. // offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
  698. // }
  699. // if (offsetAngle1 > offsetAngle2) {
  700. // chooseBreakPointId = neighPoints[1].breakPointId;
  701. // } else {
  702. // chooseBreakPointId = neighPoints[0].breakPointId;
  703. // }
  704. // return await this.moveCamera(
  705. // breakPointId,
  706. // chooseBreakPointId,
  707. // appId,
  708. // userId,
  709. // traceId,
  710. // actionType,
  711. // );
  712. // } else {
  713. // user.player.position = JSON.parse(JSON.stringify(playerPosition));
  714. // this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  715. // JSON.stringify(user.player.position),
  716. // );
  717. // this.reply.actionResponses[0].actionType = actionType;
  718. // return this.reply;
  719. // }
  720. // } catch (error) {
  721. // console.log('MoveService', error);
  722. // debugger;
  723. // return null;
  724. // }
  725. // }
  726. async joystick(actionRequest) {
  727. //try {
  728. if (this.sendingFrameForJoystick) {
  729. return null;
  730. }
  731. const userId = actionRequest['user_id'];
  732. const traceId = actionRequest['trace_id'];
  733. const dir_action = actionRequest['dir_action'];
  734. const actionType = actionRequest['action_type'];
  735. const user = this.users[userId];
  736. const breakPointId = user.breakPointId;
  737. const appId = user.appId;
  738. const step = 0.3;
  739. const closestDis = 85; //小于这个距离就跳到邻居呼吸点
  740. const distance = step * dir_action.speed_level;
  741. let angle = null;
  742. let move_angle = dir_action.move_angle + user.camera.angle.yaw;
  743. move_angle = move_angle % 360;
  744. //TODO 临时增加断言
  745. const playerPosition: Point = { x: 0, y: 0, z: 0 };
  746. playerPosition.x =
  747. user.player.position.x +
  748. distance * Math.cos((move_angle / 360) * 2 * Math.PI);
  749. playerPosition.y =
  750. user.player.position.y +
  751. distance * Math.sin((move_angle / 360) * 2 * Math.PI);
  752. //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
  753. let chooseBreakPointId = null;
  754. const breakPoint = this.breakPointInfo[breakPointId];
  755. const surroundPointIds = breakPoint.contact;
  756. const traceIds = [];
  757. user.player.angle.yaw = move_angle;
  758. traceIds.push(traceId);
  759. this.reply.traceIds = traceIds;
  760. this.reply['newUserStates'][0].userId = userId;
  761. this.reply['actionResponses'][0].traceId = traceId;
  762. this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
  763. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  764. JSON.stringify(user.camera.angle),
  765. );
  766. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  767. JSON.stringify(user.camera.position),
  768. );
  769. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  770. JSON.stringify(breakPoint.position),
  771. );
  772. this.reply['newUserStates'][0].renderInfo.isMoving = 1;
  773. this.reply['actionResponses'][0].traceId = traceId;
  774. this.reply.mediaSrc = null;
  775. console.log('joystickjoystick:' + this.cameraInfos.length);
  776. if (surroundPointIds.length == 1) {
  777. console.log(
  778. 'joystick校验--->' + breakPointId + '-' + surroundPointIds[0],
  779. );
  780. return await this.moveDirect(
  781. playerPosition,
  782. closestDis,
  783. breakPointId,
  784. surroundPointIds[0],
  785. appId,
  786. userId,
  787. traceId,
  788. actionType,
  789. );
  790. }
  791. let count = 0;
  792. const neighPoints = [];
  793. //人在哪个角度
  794. let _angle = this.getAngle(
  795. breakPoint.position,
  796. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  797. playerPosition,
  798. );
  799. if (_angle < 0) {
  800. _angle += 360;
  801. }
  802. let singleInfo = null;
  803. for (let i = 0; i < surroundPointIds.length; ++i) {
  804. const neighPoint = this.breakPointInfo[surroundPointIds[i]];
  805. neighPoint.breakPointId = surroundPointIds[i];
  806. angle = this.getAngle(
  807. breakPoint.position,
  808. { x: breakPoint.position.x + 1, y: breakPoint.position.y },
  809. neighPoint.position,
  810. );
  811. //if(angle<45&&angle!=0){
  812. if (
  813. // Math.abs(angle - move_angle) < 45 &&
  814. // Math.abs(angle - move_angle) != 0
  815. Math.abs(angle - _angle) < 45 &&
  816. Math.abs(angle - _angle) != 0
  817. ) {
  818. neighPoint.angle = angle;
  819. neighPoints.push(neighPoint);
  820. ++count;
  821. } else if (Math.abs(angle - move_angle) == 0) {
  822. console.log(
  823. 'joystick直走--->' + breakPointId + '-' + surroundPointIds[i],
  824. );
  825. return await this.moveDirect(
  826. playerPosition,
  827. closestDis,
  828. breakPointId,
  829. surroundPointIds[i],
  830. appId,
  831. userId,
  832. traceId,
  833. actionType,
  834. );
  835. }
  836. //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
  837. if (angle == 0 && Math.abs(360 - _angle) < 45) {
  838. neighPoint.angle = angle;
  839. neighPoints.push(neighPoint);
  840. ++count;
  841. }
  842. if (
  843. Math.abs(angle - move_angle) < 45 ||
  844. Math.abs(angle + 360 - move_angle) < 45
  845. ) {
  846. if (singleInfo == null) {
  847. singleInfo = {
  848. angle: angle,
  849. breakPointId: surroundPointIds[i],
  850. };
  851. }
  852. }
  853. }
  854. if (count == 2) {
  855. //人物移动
  856. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  857. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  858. JSON.stringify(playerPosition),
  859. );
  860. } else {
  861. if (singleInfo != null) {
  862. console.log(
  863. 'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
  864. );
  865. return await this.moveDirect(
  866. playerPosition,
  867. closestDis,
  868. breakPointId,
  869. singleInfo.breakPointId,
  870. appId,
  871. userId,
  872. traceId,
  873. actionType,
  874. );
  875. } else if (count == 1) {
  876. console.log(
  877. 'joystick校验--->' + breakPointId + '-' + neighPoints[0].breakPointId,
  878. );
  879. return await this.moveDirect(
  880. playerPosition,
  881. closestDis,
  882. breakPointId,
  883. neighPoints[0].breakPointId,
  884. appId,
  885. userId,
  886. traceId,
  887. actionType,
  888. );
  889. } else if (count == 0) {
  890. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  891. JSON.stringify(user.player.position),
  892. );
  893. this.reply.actionResponses[0].actionType = actionType;
  894. return this.reply;
  895. }
  896. }
  897. //count == 2
  898. //超出范围了
  899. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  900. let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
  901. if (neighPoints[0].angle == 0) {
  902. offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
  903. }
  904. let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
  905. if (neighPoints[1].angle == 0) {
  906. offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
  907. }
  908. if (offsetAngle1 > offsetAngle2) {
  909. chooseBreakPointId = neighPoints[1].breakPointId;
  910. } else {
  911. chooseBreakPointId = neighPoints[0].breakPointId;
  912. }
  913. console.log(
  914. 'joystick镜头过渡--->' + breakPointId + '-' + chooseBreakPointId,
  915. );
  916. return await this.moveCamera(
  917. breakPointId,
  918. chooseBreakPointId,
  919. appId,
  920. userId,
  921. traceId,
  922. actionType,
  923. );
  924. } else {
  925. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  926. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  927. JSON.stringify(user.player.position),
  928. );
  929. this.reply.actionResponses[0].actionType = actionType;
  930. const cameraInfo = this.getCameraInfo();
  931. if (cameraInfo != null) {
  932. console.log('joystick自由--->合并');
  933. this.reply['newUserStates'][0].playerState.camera.position =
  934. cameraInfo.camera_position;
  935. this.reply['newUserStates'][0].playerState.camera.angle =
  936. cameraInfo.camera_angle;
  937. if (cameraInfo.mediaSrc) {
  938. this.reply.mediaSrc = cameraInfo.mediaSrc;
  939. }
  940. if (this.cameraInfos.length == 0) {
  941. this.reply.moveOver = true;
  942. }
  943. this.sendingFrameForJoystick = true;
  944. } else {
  945. console.log('joystick自由--->不合并');
  946. }
  947. return this.reply;
  948. }
  949. // }
  950. // catch (error) {
  951. // console.log('MoveService', error);
  952. // debugger;
  953. // return null;
  954. // }
  955. }
  956. //沿着最合适的neighBreakPointId走
  957. async moveDirect(
  958. playerPosition,
  959. closestDis,
  960. breakPointId,
  961. neighBreakPointId,
  962. appId,
  963. userId,
  964. traceId,
  965. actionType,
  966. ) {
  967. const breakPoint = this.breakPointInfo[breakPointId];
  968. const user = this.users[userId];
  969. const player_Position = this.getTarget(
  970. playerPosition,
  971. breakPoint.position,
  972. this.breakPointInfo[neighBreakPointId].position,
  973. );
  974. if (player_Position != null) {
  975. playerPosition.x = player_Position.x;
  976. playerPosition.y = player_Position.y;
  977. user.player.position = JSON.parse(JSON.stringify(playerPosition));
  978. }
  979. if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
  980. return await this.moveCamera(
  981. breakPointId,
  982. neighBreakPointId,
  983. appId,
  984. userId,
  985. traceId,
  986. actionType,
  987. );
  988. } else {
  989. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  990. JSON.stringify(user.player.position),
  991. );
  992. this.reply.actionResponses[0].actionType = actionType;
  993. const cameraInfo = this.getCameraInfo();
  994. if (cameraInfo != null) {
  995. this.reply['newUserStates'][0].playerState.camera.position =
  996. cameraInfo.camera_position;
  997. this.reply['newUserStates'][0].playerState.camera.angle =
  998. cameraInfo.camera_angle;
  999. if (cameraInfo.mediaSrc) {
  1000. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1001. }
  1002. if (this.cameraInfos.length == 0) {
  1003. this.reply.moveOver = true;
  1004. }
  1005. this.sendingFrameForJoystick = true;
  1006. }
  1007. return this.reply;
  1008. }
  1009. }
  1010. async moveCamera(
  1011. breakPointId,
  1012. chooseBreakPointId,
  1013. appId,
  1014. userId,
  1015. traceId,
  1016. actionType,
  1017. ) {
  1018. const user = this.users[userId];
  1019. if (chooseBreakPointId == null) {
  1020. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1021. JSON.stringify(user.player.position),
  1022. );
  1023. this.reply.actionResponses[0].actionType = actionType;
  1024. return this.reply;
  1025. }
  1026. //判断人物离该邻接点的距离是否在最小路径内,如果是,跳到这个邻接点里
  1027. //相机纠正加过渡
  1028. else {
  1029. if (chooseBreakPointId == user.breakPointId) {
  1030. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1031. JSON.stringify(user.player.position),
  1032. );
  1033. return this.reply;
  1034. }
  1035. const angle = user.camera.angle.yaw % 45; //纠正需要
  1036. //通过user.camera.angle矫正相机的angle
  1037. const checkReplys = await this.modeifyCameraAngle(
  1038. angle,
  1039. userId,
  1040. traceId,
  1041. actionType,
  1042. );
  1043. for (let i = 0; i < checkReplys.length; ++i) {
  1044. //checkReplys[i].actionResponses[0].actionType = actionType;
  1045. this.addCameraInfo(
  1046. checkReplys[i]['newUserStates'][0].playerState.camera.position,
  1047. checkReplys[i]['newUserStates'][0].playerState.camera.angle,
  1048. checkReplys[i].mediaSrc,
  1049. );
  1050. }
  1051. //replys.push(checkReplys);
  1052. //读redis里的数据,按照frame_index的大小排序
  1053. const key =
  1054. 'moveframe:app_id:' +
  1055. appId +
  1056. ':start_break_point_id:' +
  1057. breakPointId +
  1058. ':end_break_point_id:' +
  1059. chooseBreakPointId +
  1060. ':angle:' +
  1061. Math.floor(user.camera.angle.yaw / 45);
  1062. const moveFramesRes = await this.cacheService.get(key);
  1063. const moveFrames = JSON.parse(moveFramesRes);
  1064. this.setCameraInfo(appId,moveFrames,breakPointId,chooseBreakPointId);
  1065. //user.breakPointId = chooseBreakPointId;
  1066. const cameraInfo = this.getCameraInfo();
  1067. if (cameraInfo != null) {
  1068. this.reply['newUserStates'][0].playerState.camera.position =
  1069. cameraInfo.camera_position;
  1070. this.reply['newUserStates'][0].playerState.camera.angle =
  1071. cameraInfo.camera_angle;
  1072. user.camera.position = JSON.parse(
  1073. JSON.stringify(cameraInfo.camera_position),
  1074. );
  1075. user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
  1076. if (cameraInfo.mediaSrc) {
  1077. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1078. }
  1079. this.reply.moveStart = true;
  1080. this.sendingFrameForJoystick = true;
  1081. }
  1082. return this.reply;
  1083. }
  1084. }
  1085. setCameraInfo(appId,moveFrames,startBreakPointId,endBreakPointId) {
  1086. for (let i = 0; i < moveFrames.length; i += 5) {
  1087. moveFrames[i].endBreakPointId = endBreakPointId;
  1088. moveFrames[i].mediaSrc = '/' + appId +'/' + startBreakPointId +'/' + moveFrames[i].file_name.substring( 0, moveFrames[i].file_name.indexOf('.'), ) + '/' +moveFrames[i].file_name + '?m=' +new Date().getTime();
  1089. this.cameraInfos.push(moveFrames[i]);
  1090. }
  1091. }
  1092. addCameraInfo(cameraPosition, cameraAngle, mediaSrc) {
  1093. this.cameraInfos.push({
  1094. camera_position: cameraPosition,
  1095. camera_angle: cameraAngle,
  1096. mediaSrc: mediaSrc,
  1097. });
  1098. }
  1099. getCameraInfo() {
  1100. if (this.cameraInfos.length > 0) {
  1101. const item = this.cameraInfos.shift();
  1102. return item;
  1103. } else {
  1104. return null;
  1105. }
  1106. }
  1107. complementFrame(userId){
  1108. if (this.cameraInfos.length > 0) {
  1109. const user = this.users[userId];
  1110. const cameraInfo = this.cameraInfos.shift();
  1111. this.reply.traceIds = [];
  1112. this.reply['newUserStates'][0].userId = userId;
  1113. this.reply['actionResponses'][0].traceId = null;
  1114. this.reply['newUserStates'][0].playerState.player.angle.yaw = user.player.angle.yaw;
  1115. this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
  1116. JSON.stringify(user.player.position),
  1117. );
  1118. this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
  1119. JSON.stringify(cameraInfo.camera_angle),
  1120. );
  1121. this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
  1122. JSON.stringify(cameraInfo.camera_position),
  1123. );
  1124. this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
  1125. JSON.stringify(user.player.position),
  1126. );
  1127. this.reply['newUserStates'][0].renderInfo.isMoving = 0;
  1128. this.reply['actionResponses'][0].traceId = null;
  1129. if (cameraInfo.mediaSrc) {
  1130. this.reply.mediaSrc = cameraInfo.mediaSrc;
  1131. }
  1132. return this.reply;
  1133. }
  1134. else {
  1135. return null;
  1136. }
  1137. }
  1138. async modeifyCameraAngle(angle, userId, traceId, actionType) {
  1139. const checkReplys = [];
  1140. if (angle > 22) {
  1141. angle = 45 - angle;
  1142. for (let i = 0; i < angle; ++i) {
  1143. // console.warn('矫正一次:' + i);
  1144. const reply = await this.rotateService.rotateForAngle(userId, 1);
  1145. // console.warn(
  1146. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1147. // );
  1148. reply.traceIds = [];
  1149. reply.traceIds.push(traceId);
  1150. const actionResponse = this.rotateService.createActionResponse(
  1151. actionType,
  1152. traceId,
  1153. );
  1154. reply.actionResponses = [];
  1155. reply.actionResponses.push(actionResponse);
  1156. checkReplys.push(reply);
  1157. }
  1158. } else if (angle != 0) {
  1159. for (let i = angle; i > -1; --i) {
  1160. // console.warn('矫正一次:' + i);
  1161. const reply = await this.rotateService.rotateForAngle(userId, -1);
  1162. // console.warn(
  1163. // '矫正:' + reply.newUserStates[0].playerState.camera.angle.yaw,
  1164. // );
  1165. reply.traceIds = [];
  1166. reply.traceIds.push(traceId);
  1167. const actionResponse = this.rotateService.createActionResponse(
  1168. actionType,
  1169. traceId,
  1170. );
  1171. reply.actionResponses = [];
  1172. reply.actionResponses.push(actionResponse);
  1173. checkReplys.push(reply);
  1174. }
  1175. }
  1176. return checkReplys;
  1177. }
  1178. getTarget(position, position1, position2) {
  1179. const line = this.createLine1(position1, position2);
  1180. const join = this.getJoinLinePoint(position, line);
  1181. // if (this.isContainForSegment(join, position1, position2)) {
  1182. // return join;
  1183. // } else {
  1184. // return null;
  1185. // }
  1186. return join;
  1187. }
  1188. getDistance(position1, position2) {
  1189. return Math.sqrt(
  1190. (position1.x - position2.x) * (position1.x - position2.x) +
  1191. (position1.y - position2.y) * (position1.y - position2.y),
  1192. );
  1193. }
  1194. isContainForSegment(point, startPoint, endPoint) {
  1195. const minDis = 0.1;
  1196. const dis1 =
  1197. this.getDistance(startPoint, point) + this.getDistance(endPoint, point);
  1198. const dis2 = this.getDistance(startPoint, endPoint);
  1199. if (Math.abs(dis1 - dis2) < minDis) {
  1200. return true;
  1201. } else {
  1202. return false;
  1203. }
  1204. }
  1205. //两条线的交点
  1206. getIntersectionPoint(parameter1, parameter2) {
  1207. if (this.isParallel(parameter1, parameter2)) {
  1208. return null;
  1209. }
  1210. if (
  1211. typeof parameter1.a == 'undefined' &&
  1212. typeof parameter2.a != 'undefined'
  1213. ) {
  1214. if (parameter1.x) {
  1215. return {
  1216. x: parameter1.x,
  1217. y: parameter2.a * parameter1.x + parameter2.b,
  1218. };
  1219. } else if (parameter1.y) {
  1220. return {
  1221. x: (parameter1.y - parameter2.b) / parameter2.a,
  1222. y: parameter1.y,
  1223. };
  1224. }
  1225. } else if (
  1226. typeof parameter2.a == 'undefined' &&
  1227. typeof parameter1.a != 'undefined'
  1228. ) {
  1229. if (parameter2.x) {
  1230. return {
  1231. x: parameter2.x,
  1232. y: parameter1.a * parameter2.x + parameter1.b,
  1233. };
  1234. } else if (parameter2.y) {
  1235. return {
  1236. x: (parameter2.y - parameter1.b) / parameter1.a,
  1237. y: parameter2.y,
  1238. };
  1239. }
  1240. } else if (
  1241. typeof parameter2.a == 'undefined' &&
  1242. typeof parameter1.a == 'undefined'
  1243. ) {
  1244. if (parameter1.hasOwnProperty('x') && parameter2.hasOwnProperty('y')) {
  1245. return { x: parameter1.x, y: parameter2.y };
  1246. } else if (
  1247. parameter1.hasOwnProperty('y') &&
  1248. parameter2.hasOwnProperty('x')
  1249. ) {
  1250. return { x: parameter2.x, y: parameter1.y };
  1251. } else {
  1252. return null;
  1253. }
  1254. }
  1255. if (parameter1.a == parameter2.a) {
  1256. return null;
  1257. }
  1258. const joinpointx =
  1259. (parameter2.b - parameter1.b) / (parameter1.a - parameter2.a);
  1260. const joinpointy =
  1261. (parameter1.a * parameter2.b - parameter2.a * parameter1.b) /
  1262. (parameter1.a - parameter2.a);
  1263. const point = { x: joinpointx, y: joinpointy };
  1264. return point;
  1265. }
  1266. // 垂直线
  1267. getVerticalLine(line, point) {
  1268. if (typeof line.a === 'undefined') {
  1269. if (line.hasOwnProperty('x')) {
  1270. return { y: point.y };
  1271. } else if (line.hasOwnProperty('y')) {
  1272. return { x: point.x };
  1273. } else {
  1274. return null;
  1275. }
  1276. } else if (line.a == 0) {
  1277. return { x: point.x };
  1278. } else {
  1279. const tl = {} as any as VerticalLineType;
  1280. tl.a = -1 / line.a;
  1281. const result = this.createLine2(tl, point);
  1282. return result;
  1283. }
  1284. }
  1285. // 经过point且与line垂直的直线,该直线与line的交点
  1286. getJoinLinePoint(point, line) {
  1287. const verticalLine = this.getVerticalLine(line, point);
  1288. const join = this.getIntersectionPoint(line, verticalLine);
  1289. return join;
  1290. }
  1291. // 与lineA平行并且point在线上
  1292. createLine2(lineA, point) {
  1293. const parameter = {} as any as {
  1294. x: number;
  1295. y: number;
  1296. a: number;
  1297. b: number;
  1298. };
  1299. if (typeof lineA.a === 'undefined') {
  1300. if (typeof lineA.x !== 'undefined') {
  1301. parameter.x = point.x;
  1302. } else if (typeof lineA.y !== 'undefined') {
  1303. parameter.y = point.y;
  1304. }
  1305. } else {
  1306. parameter.a = lineA.a;
  1307. parameter.b = point.y - point.x * lineA.a;
  1308. }
  1309. return parameter;
  1310. }
  1311. createLine1(point1, point2) {
  1312. if (point1.x == point2.x && point1.y == point2.y) {
  1313. return null;
  1314. } else if (this.getFixed(Math.abs(point1.x - point2.x)) == 0) {
  1315. return { x: point1.x };
  1316. } else if (this.getFixed(Math.abs(point1.y - point2.y)) == 0) {
  1317. return { y: point1.y };
  1318. }
  1319. const parametera = (point1.y - point2.y) / (point1.x - point2.x);
  1320. const parameterb =
  1321. (point1.x * point2.y - point2.x * point1.y) / (point1.x - point2.x);
  1322. if (this.getFixed(parametera) == 0) {
  1323. return { y: this.getFixed(parameterb) };
  1324. }
  1325. const parameter = {
  1326. a: this.getFixed(parametera),
  1327. b: this.getFixed(parameterb),
  1328. };
  1329. return parameter;
  1330. }
  1331. //返回true表示平行
  1332. isParallel(line1, line2) {
  1333. if (typeof line1.a == 'undefined' && typeof line2.a == 'undefined') {
  1334. if (line1.hasOwnProperty('x') && line2.hasOwnProperty('x')) {
  1335. return true;
  1336. } else if (line1.hasOwnProperty('y') && line2.hasOwnProperty('y')) {
  1337. return true;
  1338. } else {
  1339. return false;
  1340. }
  1341. } else if (typeof line1.a == 'undefined' || typeof line2.a == 'undefined') {
  1342. return false;
  1343. } else if (this.getFixed(line1.a) == this.getFixed(line2.a)) {
  1344. return true;
  1345. } else {
  1346. return false;
  1347. }
  1348. }
  1349. getFixed(num) {
  1350. const decimal = 2;
  1351. return parseFloat(num.toFixed(decimal));
  1352. }
  1353. isPointInPoly(position, breakPointIds) {
  1354. const x = position.x;
  1355. const y = position.y;
  1356. let inside = false;
  1357. for (
  1358. let i = 0, j = breakPointIds.length - 1;
  1359. i < breakPointIds.length;
  1360. j = i++
  1361. ) {
  1362. const pt1 = this.breakPointInfo[breakPointIds[i]];
  1363. const pt2 = this.breakPointInfo[breakPointIds[j]];
  1364. const xi = pt1.x;
  1365. const yi = pt1.y;
  1366. const xj = pt2.x;
  1367. const yj = pt2.y;
  1368. const intersect =
  1369. yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  1370. if (intersect) inside = !inside;
  1371. }
  1372. return inside;
  1373. }
  1374. }