-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from lihqi/lhq-dev-3.5.0
feat(lb-components): When pointCloudData update filter preResult by PCD
- Loading branch information
Showing
9 changed files
with
277 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,16 @@ | |
* @author Ron <[email protected]> | ||
*/ | ||
|
||
// eslint-disable-next-line | ||
import GenerateIndexWorker from 'web-worker:./generateIndexWorker.js'; | ||
import { PCDLoader } from './PCDLoader'; | ||
|
||
type TCacheInfo = { | ||
src: string; | ||
}; | ||
|
||
export type TIndexMap = Map<string, { x: number; y: number; z: number }[]>; | ||
|
||
export class PointCloudCache { | ||
public pcdLoader: PCDLoader; | ||
|
||
|
@@ -19,6 +23,8 @@ export class PointCloudCache { | |
|
||
private colorMap: Map<string, Float32Array>; | ||
|
||
private cacheIndexMap: Map<string, TIndexMap>; | ||
|
||
private cacheList: Array<TCacheInfo> = []; | ||
|
||
private static instance: PointCloudCache; | ||
|
@@ -29,7 +35,7 @@ export class PointCloudCache { | |
this.pcdLoader = new PCDLoader(); | ||
this.pointsMap = new Map(); | ||
this.colorMap = new Map(); | ||
|
||
this.cacheIndexMap = new Map(); | ||
this.cache2DHighlightIndex = new Map(); | ||
} | ||
|
||
|
@@ -95,4 +101,40 @@ export class PointCloudCache { | |
public clearCache2DHighlightIndex() { | ||
this.cache2DHighlightIndex.clear(); | ||
} | ||
|
||
/** | ||
* Loads index map from cache or generates it in a worker. | ||
* | ||
* @param src The path to the source image. | ||
* @param points The points of the source image. | ||
* @returns A promise that resolves to the index map. | ||
*/ | ||
public loadIndexMap = async (src: string, points: Float32Array) => { | ||
const currentCacheIndexMap = this.cacheIndexMap.get(src); | ||
|
||
return new Promise((resolve) => { | ||
if (currentCacheIndexMap) { | ||
return resolve(currentCacheIndexMap); | ||
} | ||
if (window.Worker) { | ||
const generateIndexWorker = new GenerateIndexWorker({ type: 'module' }); | ||
generateIndexWorker.postMessage({ | ||
points, | ||
}); | ||
|
||
generateIndexWorker.onmessage = (e: any) => { | ||
const { indexMap } = e.data; | ||
this.cacheIndexMap.set(src, indexMap); | ||
// 按照缓存一个 1.8M PCD(包含 points.length:360804)文件需要占用 2.8MB 内存粗略估算,缓存 50 个 pcd 文件大概需要 140MB 内存 | ||
if (this.cacheIndexMap.size > this.MAX_SIZE) { | ||
const firstKey = Array.from(this.cacheIndexMap.keys())[0]; | ||
this.cacheIndexMap.delete(firstKey); | ||
} | ||
|
||
resolve(indexMap); | ||
generateIndexWorker.terminate(); | ||
}; | ||
} | ||
}); | ||
}; | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/lb-annotation/src/core/pointCloud/generateIndexWorker.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
onmessage = function onmessage(e) { | ||
const { points } = e.data; | ||
|
||
const indexMap = new Map(); | ||
|
||
for (let i = 0; i < points.length; i += 3) { | ||
const x = points[i]; | ||
const y = points[i + 1]; | ||
const z = points[i + 2]; | ||
|
||
const key = `${Math.ceil(x)}@${Math.ceil(y)}@${Math.ceil(z)}`; | ||
|
||
if (!indexMap.has(key)) { | ||
indexMap.set(key, []); | ||
} | ||
indexMap.get(key).push({ | ||
x, | ||
y, | ||
z, | ||
}); | ||
} | ||
|
||
this.postMessage({ indexMap }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.