Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

MAPSJS-2660: Support off-center projection in getFitBoundsDistance() #2294

Merged
merged 3 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 69 additions & 70 deletions @here/harp-mapview/lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,78 +944,77 @@ export namespace MapViewUtils {
worldTarget: THREE.Vector3,
camera: THREE.PerspectiveCamera
): number {
const cameraRotationMatrix = new THREE.Matrix4();
cameraRotationMatrix.extractRotation(camera.matrixWorld);
const screenUpVector = new THREE.Vector3(0, 1, 0).applyMatrix4(cameraRotationMatrix);
const screenSideVector = new THREE.Vector3(1, 0, 0).applyMatrix4(cameraRotationMatrix);
const screenVertMidPlane = new THREE.Plane().setFromCoplanarPoints(
camera.position,
worldTarget,
worldTarget.clone().add(screenUpVector)
);
const screenHorzMidPlane = new THREE.Plane().setFromCoplanarPoints(
camera.position,
worldTarget,
worldTarget.clone().add(screenSideVector)
);

const cameraPos = cache.vector3[0];
cameraPos.copy(camera.position);

const halfVertFov = THREE.MathUtils.degToRad(camera.fov / 2);
// TODO: Support off-center projections.
const halfHorzFov = calculateHorizontalFovByVerticalFov(halfVertFov * 2, camera.aspect) / 2;

// tan(fov/2)
const halfVertFovTan = 1 / Math.tan(halfVertFov);
const halfHorzFovTan = 1 / Math.tan(halfHorzFov);

const cameraToTarget = cache.vector3[1];
cameraToTarget.copy(cameraPos).sub(worldTarget).negate();

const cameraToTargetNormalized = new THREE.Vector3().copy(cameraToTarget).normalize();

const offsetVector = new THREE.Vector3();

const cameraToPointOnRefPlane = new THREE.Vector3();
const pointOnRefPlane = new THREE.Vector3();

function checkAngle(
point: THREE.Vector3,
referencePlane: THREE.Plane,
maxAngle: number,
fovFactor: number
) {
referencePlane.projectPoint(point, pointOnRefPlane);
cameraToPointOnRefPlane.copy(cameraPos).sub(pointOnRefPlane).negate();

const viewAngle = cameraToTarget.angleTo(cameraToPointOnRefPlane);

if (viewAngle <= maxAngle) {
return;
}

const cameraToPointLen = cameraToPointOnRefPlane.length();
const cameraToTargetLen = cameraToTarget.length();

const newCameraDistance =
cameraToPointLen * (Math.sin(viewAngle) * fovFactor - Math.cos(viewAngle)) +
cameraToTargetLen;

offsetVector
.copy(cameraToTargetNormalized)
.multiplyScalar(cameraToTargetLen - newCameraDistance);

cameraPos.add(offsetVector);
cameraToTarget.sub(offsetVector);
}

// camY
// targetDist^
// |<-------->| Ps
// constD pEyeZ| /| ^
// |<-->|<--->| / | |
// | | | / | | |ndcY-O.y|*h/2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document what h is to remove any potential ambiguity

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// | | | / | |
// <---T----P'----C0----O v
// camZ |_| /| |
// | / |<--->|
// PcamY| / f
// | / (focal length)
// |/
// P
// camY
// constD newPEyeZ ^ Ps
// |<-->|<--------------->| _-`| ^
// | | | _-` | | |sign(ndcY)-O.y|h/2
// | | | _-` | |
// <---T----P'----C0----------C1---------O v
// camZ |_| _-`| | C0 - Initial camera position
// | _-` |<-------->| C1 - New camera position
// PcamY| _-` f T - Camera target
// | _-` (focal length) P - Bounds point (world space)
// | _-` Ps - P projected on screen.
// P-` O - Principal point.
//
// Diagram showing how to calculate the camera distance on the camera YZ plane so that a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand the diagrams till I had read the description, and even then it wasn't fully clear.

They should have some title, and the second diagram should have some description that highlights that we are computing the factor to move C from C0 to C1. Also, it should be highlighted that Ps is on the edge of the screen. I know it is described later, but it would be good to have it documented here too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// point P is projected on the screen edge (similar for XZ plane). P is between target and
// initial camera position, but calculations are equivalent for points beyond the target
// (pEyeZ negative) or behind the camera (constD negative).
// Right triangles PP'C0 and PsOC0 are equivalent, as well as PP'C1 and Ps0C1, that means:
// |ndcY-O.y|*h/(2*f) = PcamY / |pEyeZ| (1) (ndcY-O.y,pEyeZ may be negative, take abs vals).
// |sign(ndcY)-O.y|h/(2*f) = PcamY / newPEyeZ (2)
// Dividing (1) by (2) and solving for newPEyeZ we get:
// newPEyeZ = | pEyeZ || ndcY - O.y | / |sign(ndcY)-O.y|
// The target distance to project P at the top/bottom border of the viewport is then:
// constD + newPEyeZ = targetDist - pEyeZ + |pEyeZ||ndcY-O.y| / |sign(ndcY)-O.y|
// The target distance to project P at the left/right border of the viewport is similarly:
// targetDist - pEyeZ + |pEyeZ||ndcX-O.x| / |sign(ndcX)-O.x|
// Take the largest of both distances to ensure the point is inside the viewport:
// newDistance = targetDist - pEyeZ +
// max(| ndcX - O.x | /|sign(ndcX)-O.x|, |ndcY-O.y|/sign(ndcY) - O.y |) *| pEyeZ |

const targetDist = cache.vector3[0].copy(worldTarget).sub(camera.position).length();
const ppalPoint = CameraUtils.getPrincipalPoint(camera);
let newDistance = targetDist;

const getDistanceFactor = (pointNDC: number, ppNDC: number) => {
// Use as maximum NDC a value slightly smaller than 1 to ensure the point is visible
// with the final camera distance. Otherwise any precision loss might leave it just
// outside of the viewport.
const maxNDC = 0.99;
return Math.abs(pointNDC) > 1
? Math.abs((pointNDC - ppNDC) / (maxNDC * Math.sign(pointNDC) - ppNDC))
: 1;
};
for (const point of points) {
checkAngle(point, screenVertMidPlane, halfVertFov, halfVertFovTan);
checkAngle(point, screenHorzMidPlane, halfHorzFov, halfHorzFovTan);
const pEyeZ = -cache.vector3[0].copy(point).applyMatrix4(camera.matrixWorldInverse).z;
const pointNDC = cache.vector3[0].applyMatrix4(camera.projectionMatrix);
const maxFactor = Math.max(
getDistanceFactor(pointNDC.x, ppalPoint.x),
getDistanceFactor(pointNDC.y, ppalPoint.y)
);
if (maxFactor > 1) {
const constDist = targetDist - pEyeZ;
const newPEyeZ = Math.abs(pEyeZ) * maxFactor + constDist;
newDistance = Math.max(newDistance, newPEyeZ);
}
}

return cameraToTarget.length();
return newDistance;
}

/**
Expand Down
Loading