sdk.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import cover from "./cover/index";
  2. import { createLoadPack, loadLib } from "@/utils";
  3. import {
  4. FuseModelAttrs,
  5. FuseModel,
  6. GuidePath,
  7. MeasureType,
  8. Measure as StoreMeasure,
  9. MeasurePosition,
  10. SceneType,
  11. scenes,
  12. Scene,
  13. } from "@/store";
  14. import type { Emitter } from "mitt";
  15. import { TaggingPositionType } from "@/api";
  16. export enum SettingResourceType {
  17. map = "map",
  18. color = "color",
  19. envImage = "img",
  20. bottomImage = "bimg",
  21. icon = "icon",
  22. }
  23. type SceneModelAttrs = FuseModelAttrs & { select: boolean };
  24. export type SceneModel = ToChangeAPI<SceneModelAttrs> & {
  25. bus: Emitter<
  26. Pick<SceneModelAttrs, "select"> & {
  27. loadError: void;
  28. loadDone: void;
  29. loadProgress: number;
  30. changeSelect: boolean;
  31. transformChanged: {
  32. position?: SceneLocalPos;
  33. scale?: number;
  34. rotation?: SceneLocalPos;
  35. bottom?: number;
  36. };
  37. }
  38. >;
  39. destroy: () => void;
  40. enterRotateMode: () => void;
  41. enterMoveMode: () => void;
  42. leaveTransform: () => void;
  43. getDefaultRotation: () => SceneLocalPos
  44. enterAlignment: () => void;
  45. leaveAlignment: () => void;
  46. enterScaleSet: () => ScaleSet;
  47. leaveScaleSet: () => void;
  48. supportPano: () => boolean;
  49. flyInPano: () => void;
  50. flyOutPano: () => void;
  51. };
  52. export interface ScaleSet {
  53. setLength: (length: number) => void;
  54. startMeasure: () => void;
  55. }
  56. export type ModelAttrRange = {
  57. [key in "opacity" | "bottom" | "scale" as `${key}Range`]: {
  58. min: number;
  59. max: number;
  60. step: number;
  61. };
  62. };
  63. export type AddModelProps = Pick<FuseModel, "url" | "id"> &
  64. FuseModelAttrs & {
  65. type: string;
  66. isDynamicAdded: boolean;
  67. mode: "many" | "single";
  68. fromType: any;
  69. } & ModelAttrRange;
  70. export type SceneGuidePath = Pick<GuidePath, "speed" | "time"> & Pose;
  71. export interface SceneGuide {
  72. bus: Emitter<{ changePoint: number; playComplete: void }>;
  73. play: () => void;
  74. pause: () => void;
  75. clear: () => void;
  76. }
  77. export type ScenePos = { localPos: SceneLocalPos; modelId: FuseModel["id"] };
  78. export type ScreenPos = {
  79. trueSide: boolean;
  80. pos: ScreenLocalPos;
  81. modelId: FuseModel["id"];
  82. };
  83. export interface CameraComeToProps {
  84. position: SceneLocalPos;
  85. target?: SceneLocalPos;
  86. dur?: number;
  87. modelId?: FuseModel["id"];
  88. distance?: 1 | 2 | 3;
  89. }
  90. export type CalcPathProps = [
  91. [SceneGuidePath, SceneGuidePath],
  92. Partial<Pick<SceneGuidePath, "time" | "speed">>
  93. ];
  94. export interface MeasureBase {
  95. destroy?: () => void;
  96. show: () => void;
  97. hide: () => void;
  98. fly: () => void;
  99. bus: Emitter<{
  100. update: [MeasurePosition["point"][], MeasurePosition["modelId"][]];
  101. highlight: boolean;
  102. }>;
  103. changeSelect: (isHight: boolean) => void;
  104. setPositions: (
  105. points: MeasurePosition["point"][],
  106. modelIds: MeasurePosition["modelId"][]
  107. ) => void;
  108. }
  109. export type Measure<T extends StoreMeasure["type"] = StoreMeasure["type"]> =
  110. MeasureBase &
  111. (T extends MeasureType.area
  112. ? { getArea: () => { value: number } }
  113. : { getDistance: () => { value: number } });
  114. export type StartMeasure<T extends StoreMeasure["type"]> = Measure<T> & {
  115. bus: Emitter<{
  116. submit: [MeasurePosition["point"][], MeasurePosition["modelId"][]];
  117. cancel: void;
  118. invalidPoint: string;
  119. }>;
  120. };
  121. export type Pose =
  122. | {
  123. position: SceneLocalPos;
  124. target: SceneLocalPos;
  125. }
  126. | {
  127. panoId: any;
  128. model: SceneModel;
  129. posInModel: SceneLocalPos;
  130. rotInModel: SceneLocalPos;
  131. position: SceneLocalPos;
  132. target: SceneLocalPos;
  133. };
  134. export interface SDK {
  135. layout: HTMLDivElement;
  136. sceneBus: Emitter<{
  137. cameraChange: SceneLocalPos;
  138. panoModelChange: SceneModel;
  139. modeChange: { mode: "pano" | "fuse"; active: SceneModel };
  140. }>;
  141. setBackdrop: (
  142. drop: string,
  143. type: SettingResourceType,
  144. tb: { scale?: number; rotate?: number }
  145. ) => void;
  146. switchScene: (
  147. scene: { type: SceneType; num: string } | null
  148. ) => Promise<void>;
  149. startAddSth: () => void
  150. endAddSth: () => void
  151. addModel: (props: AddModelProps) => SceneModel;
  152. setCameraFov: (fov: number) => void;
  153. enableMap(dom: HTMLDivElement, latlng: number[]): void;
  154. switchMapType: (type: string) => void;
  155. showGrid: () => void;
  156. compassVisibility: (visibility: boolean) => void;
  157. canTurnToPanoMode: () => { model: SceneModel };
  158. hideGrid: () => void;
  159. calcPathInfo: (
  160. paths: CalcPathProps[0],
  161. info: CalcPathProps[1]
  162. ) => Required<CalcPathProps[1]>;
  163. getPositionByScreen: (
  164. screenPos: ScreenLocalPos,
  165. modelId?: FuseModel["id"]
  166. ) => (ScenePos & { worldPos: SceneLocalPos, localNormal: SceneLocalPos }) | null;
  167. getScreenByPosition: (
  168. localPos: SceneLocalPos,
  169. modelId?: FuseModel["id"]
  170. ) => ScreenPos | null;
  171. screenshot: (width: number, height: number) => Promise<string>;
  172. getPose: () => Pose;
  173. comeTo: (pos: CameraComeToProps) => void;
  174. enterSceneGuide: (data: SceneGuidePath[]) => SceneGuide;
  175. drawMeasure<T extends StoreMeasure["type"]>(
  176. type: T,
  177. points: MeasurePosition["point"][],
  178. modelIds: MeasurePosition["modelId"][]
  179. ): Measure<T>;
  180. startMeasure<T extends StoreMeasure["type"]>(type: T): StartMeasure<T>;
  181. createTagging: (props: Tagging3DProps) => Tagging3D
  182. createPath: (props: PathProps) => Path
  183. }
  184. export type PathProps = {
  185. // 线段名称
  186. name: string,
  187. // 是否显示名称,
  188. showName: boolean,
  189. // 文字大小
  190. fontSize: number,
  191. // 是否显示方向,
  192. showDirection: boolean,
  193. // 方向是否反向
  194. reverseDirection: boolean,
  195. line: {
  196. width: number,
  197. color: string,
  198. altitudeAboveGround: number
  199. position?: SceneLocalPos,
  200. modelId?: string
  201. },
  202. points: {
  203. // 点位名称
  204. name: string,
  205. position: SceneLocalPos,
  206. modelId: string,
  207. }[]
  208. }
  209. export type Path = {
  210. bus: Emitter<{
  211. activePoint: number
  212. // 标注点击事件
  213. click: void;
  214. // 鼠标移入标注事件
  215. enter: void;
  216. // 鼠标移出标注事件
  217. leave: void;
  218. // 线段坐标更改事件
  219. linePositionChange: {
  220. pos: SceneLocalPos,
  221. modelId: string
  222. }
  223. focus: boolean
  224. // 路径点位置变更
  225. changePoints: PathProps['points']
  226. drawed: void
  227. }>;
  228. changeDirection: ( show: boolean, reverse: boolean ) => void
  229. changeFontSize: (fontSize: number) => void
  230. focus: (f: boolean) => void
  231. changeVisibilityRange: (range: number) => void
  232. changePointName: (index: number, name: string) => void
  233. // 飞向路径
  234. fly: () => void,
  235. // 更改路径点
  236. changePathPoints: (points: Omit<PathProps['points'][number], 'name'>[]) => void
  237. // 播放路径,相机沿着路径运动,传入播放完毕回调
  238. play: (playedHandler: () => void) => void
  239. // 停止播放路径
  240. pause: () => void
  241. // 是否可编辑
  242. changeCanEdit: (canMove: boolean) => void
  243. deletePoint: (index: number) => void
  244. changeEditMode: (editMode: boolean) => void
  245. // 可见性
  246. visibility: (visibility: boolean) => void
  247. // 气泡是否可见
  248. visibilityName: (visibility: boolean) => void
  249. // 更改标题气泡属性
  250. changeLine: (props: Partial<PathProps['line']>) => void
  251. // 更改标题气泡属性
  252. changeName: (name: string) => void
  253. // 线段销毁
  254. destroy: () => void
  255. }
  256. export type Tagging3DProps = {
  257. lineHeight: number
  258. fontSize: number,
  259. // 标题
  260. title: string
  261. // 标注类型 2d | 3d
  262. type: TaggingPositionType,
  263. // 模型id
  264. modelId: string,
  265. // 贴地射线获取的位置
  266. position: SceneLocalPos
  267. normal: SceneLocalPos
  268. // 是否可以移动
  269. canMove: boolean,
  270. // 贴地图片url
  271. image: string
  272. // 贴地图片的变换
  273. mat: {
  274. scale: number,
  275. rotation: number,
  276. }
  277. }
  278. export type Tagging3D = {
  279. bus: Emitter<{
  280. // 标注点击事件
  281. click: void;
  282. // 鼠标移入标注事件
  283. enter: void;
  284. // 鼠标移出标注事件
  285. leave: void;
  286. // 位置变更
  287. changePosition: {pos: SceneLocalPos, modelId: string, normal: SceneLocalPos}
  288. }>;
  289. changePosition: (position: {
  290. modelId: string,
  291. // 贴地射线获取的位置
  292. position: SceneLocalPos
  293. normal: SceneLocalPos
  294. }) => void
  295. changeFontSize: (fontSize: number) => void
  296. changeLineHeight: (lineHeight: number) => void
  297. // 设置标题
  298. changeTitle: (title: string) => void
  299. // 标题是否可见
  300. visibilityTitle: (visibility: boolean) => void
  301. // 更改可拖拽移动
  302. changeCanMove: (canMove: boolean) => void
  303. // 获取图标中心三维坐标
  304. getImageCenter: () => SceneLocalPos
  305. // 更改图标
  306. changeImage: (url: string) => void
  307. // 标注可见性
  308. visibility: (visibility: boolean) => void
  309. // 标注图片变换,传入变换
  310. changeMat: (props: Tagging3DProps['mat']) => void
  311. // 更改热点类型
  312. changeType: (val: TaggingPositionType) => void
  313. // 距离相机位置
  314. getCameraDisSquared: () => number
  315. // 标注销毁
  316. destory: () => void
  317. }
  318. export let sdk: SDK;
  319. export type InialSDKProps = {
  320. laserRoot?: string
  321. ossRoot?: string
  322. laserOSSRoot?: string
  323. panoOSSRoot?: string
  324. layout: HTMLDivElement;
  325. scenes: Scene[];
  326. lonlat?: number[];
  327. };
  328. export let initialed = false;
  329. export const initialSDK = async (props: InialSDKProps) => {
  330. if (initialed) return sdk;
  331. initialed = true;
  332. const libs = [
  333. `./lib/proj4/proj4.js`,
  334. `./lib/jquery/jquery-3.1.1.min.js`,
  335. `./lib/other/BinaryHeap.js`,
  336. `./lib/tween/tween.min.js`,
  337. `./lib/plasio/js/laslaz.js`,
  338. `./lib/plasio/vendor/bluebird.js`,
  339. `./lib/plasio/workers/laz-loader-worker.js`,
  340. `./lib/plasio/workers/laz-perf.js`,
  341. `./lib/Cesium/Cesium.js`,
  342. `./lib/shapefile/shapefile.js`,
  343. ];
  344. await Promise.all(libs.map(loadLib));
  345. await loadLib(`./lib/potree/potree.js`);
  346. console.log(props);
  347. const localSdk = cover({
  348. ...props,
  349. dom: props.layout,
  350. isLocal: false,
  351. scenes: props.scenes,
  352. lonlat: props.lonlat,
  353. mapDom: null,
  354. } as any) as unknown as SDK;
  355. (window as any).sdk = sdk = localSdk;
  356. sdk.layout = props.layout;
  357. return sdk;
  358. };
  359. export default sdk!;