Skip to content

Commit

Permalink
feat(pointcloud): Change 3DView camera from perspective to orthographic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerwin-L committed Feb 13, 2023
1 parent 71dd3b5 commit 6be1167
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 14 deletions.
7 changes: 6 additions & 1 deletion packages/lb-annotation/src/core/pointCloud/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ export class PointCloud {
};
}

public updateCameraZoom(zoom: number) {
this.camera.zoom = zoom;
this.camera.updateProjectionMatrix();
}

/**
* Update Camera position & target
* @param boxParams
Expand Down Expand Up @@ -392,7 +397,7 @@ export class PointCloud {
* Reset camera to center-top
*/
public resetCamera() {
this.updateCamera({ x: -1, y: 0, z: 500 }, { x: 0, y: 0, z: 0 });
this.updateCamera(this.DEFAULT_INIT_CAMERA_POSITION, { x: 0, y: 0, z: 0 });
}

public createThreeMatrix4(matrix4: TMatrix4Tuple) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const PointCloud3DContext = React.createContext<{
reset3DView: () => void;
}>({
isActive: false,
setTarget3DView: () => {},
reset3DView: () => {},
setTarget3DView: () => { },
reset3DView: () => { },
});

const PointCloudViewIcon = ({
Expand Down Expand Up @@ -95,7 +95,7 @@ const PointCloud3D: React.FC<IA2MapStateProps> = ({ currentData, config }) => {
if (!ptCtx.mainViewInstance) {
return;
}
initPointCloud3d?.();
initPointCloud3d?.(size);
}, [size]);
const { selectedBox } = useSingleBox();

Expand All @@ -111,15 +111,29 @@ const PointCloud3D: React.FC<IA2MapStateProps> = ({ currentData, config }) => {
ptCtx.mainViewInstance?.resetCamera();
};

/**
* Listen for data changes.
*/
useEffect(() => {
if (ref.current && currentData?.url) {
let pointCloud = ptCtx.mainViewInstance;
if (!pointCloud) {
if (!pointCloud && size.width) {
const orthographicParams = {
left: -size.width / 2,
right: size.width / 2,
top: size.height / 2,
bottom: -size.height / 2,
near: 100,
far: -100,
}

// Need to be showed
pointCloud = new PointCloud({
container: ref.current,
backgroundColor: '#4c4c4c',
config,
isOrthographicCamera: true,
orthographicParams,
});
ptCtx.setMainViewInstance(pointCloud);
}

if (currentData.result) {
Expand All @@ -140,9 +154,8 @@ const PointCloud3D: React.FC<IA2MapStateProps> = ({ currentData, config }) => {
ptCtx.setPointCloudValid(jsonParser(currentData.result)?.valid);
}

ptCtx.setMainViewInstance(pointCloud);
}
}, [currentData]);
}, [currentData, size]);

/**
* Observe selectedID and reset camera to target top-view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const PointCloudTopView: React.FC<IProps> = ({

const { pointCloud2dOperation: TopView2dOperation } = ptCtx.topViewInstance;

TopView2dOperation.singleOn('polygonCreated', (polygon: IPolygonData) => {
TopView2dOperation.singleOn('polygonCreated', (polygon: IPolygonData, zoom: number) => {
if (TopView2dOperation.pattern === EPolygonPattern.Normal || !currentData?.url) {
/**
* Notice. The Polygon need to be converted to pointCloud coordinate system for storage.
Expand All @@ -216,6 +216,7 @@ const PointCloudTopView: React.FC<IProps> = ({
size,
imgList,
trackConfigurable: config.trackConfigurable,
zoom
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,13 @@ export const usePointCloudViews = () => {
size,
imgList,
trackConfigurable,
zoom,
}: {
newPolygon: any;
size: ISize;
imgList: IFileItem[];
trackConfigurable?: boolean;
zoom: number;
}) => {
const newImgList = imgList as any[];

Expand Down Expand Up @@ -423,7 +425,7 @@ export const usePointCloudViews = () => {

polygonOperation.setSelectedIDs([newPolygon.id]);
setSelectedIDs(boxParams.id);
syncPointCloudViews(PointCloudView.Top, newPolygon, boxParams);
syncPointCloudViews(PointCloudView.Top, newPolygon, boxParams, zoom);
addPointCloudBox(boxParams);
addHistory({ newBoxParams: boxParams });
};
Expand Down Expand Up @@ -528,7 +530,12 @@ export const usePointCloudViews = () => {
* @param polygon
* @param boxParams
*/
const syncPointCloudViews = (omitView: string, polygon: any, boxParams: IPointCloudBox) => {
const syncPointCloudViews = (
omitView: string,
polygon: any,
boxParams: IPointCloudBox,
zoom?: number,
) => {
const dataUrl = currentData?.url;

const viewToBeUpdated = {
Expand All @@ -550,6 +557,10 @@ export const usePointCloudViews = () => {
viewToBeUpdated[key]();
}
});
if (zoom) {
mainViewInstance?.updateCameraZoom(zoom);
}

mainViewGenBox(boxParams);
mainViewInstance?.highlightOriginPointCloud(boxParams);
};
Expand All @@ -559,12 +570,21 @@ export const usePointCloudViews = () => {
mainViewInstance?.generateBoxes(newBoxes);
};

const initPointCloud3d = () => {
const initPointCloud3d = (size: ISize) => {
if (!mainViewInstance) {
return;
}

mainViewInstance.initPerspectiveCamera();
const orthographicParams = {
left: -size.width / 2,
right: size.width / 2,
top: size.height / 2,
bottom: -size.height / 2,
near: 100,
far: -100,
}

mainViewInstance.initOrthographicCamera(orthographicParams);
mainViewInstance.initRenderer();
mainViewInstance.render();
};
Expand Down

0 comments on commit 6be1167

Please sign in to comment.