Camera orientation and Frustum intersection

Hello folks.

My Goal:

I have a camera orientation data from photogrammetry software: frustum parameters and transformation matrix. What i need is to check if a given point is inside each camera frustum or outside it.

My problems

  1. I do not understand how to set Cesum.Camera() orientation the same way as i set orientation for Cesium.FrustumGeometry

  2. Is cullingVolume.computeVisibility the right approach to check if the point is visible by camera?

First problem description:

Lets say i have each camera defined as:

camera = {
    label: 'label'
    transform : Cesium.Matrix4,
    sensor : {
	    width: W,
	    height: H
	    f : F
    }
};

I calculate position and orientation from Matrix4

var matrix3 = Cesium.Matrix4.getMatrix3(transform, new Cesium.Matrix3());
var rotationMatrix = Cesium.Matrix3.getRotation(matrix3,new Cesium.Matrix3())
var rotationQuaternion = Cesium.Quaternion.fromRotationMatrix(rotationMatrix, new Cesium.Quaternion())
var positionOnEllipsoid = Cesium.Matrix4.multiplyByPoint(camera.transform, new Cesium.Cartesian3(), new Cesium.Cartesian3())

I create frustum geometry:

var frustumGeometry = new Cesium.FrustumGeometry({
		frustum : frustum,
		origin : positionOnEllipsoid,
		orientation : frustumOrientation,
		
	});
	
var frustumGeometryInstance = new Cesium.GeometryInstance({
	geometry : frustumGeometry,
	attributes : {
		color :Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.fromCssColorString('#498AD6'))
	},
	id : camera.label
});

	
var frustumPrimitive = camerasPrimitives.add(new Cesium.Primitive({
	geometryInstances : frustumGeometryInstance,
	appearance : new Cesium.PerInstanceColorAppearance({
		closed: true,
		grid:true,
		flat : true
	})
}));

And it works. Frustum geometries are located in the right position and oriented the right way.

To set the Cesium.Camera i calculate Cesium.HeadingPitchRoll:

var rotationQuaternion = Cesium.Quaternion.fromRotationMatrix(rotationMatrix, new         Cesium.Quaternion())
var hpr = Cesium.HeadingPitchRoll.fromQuaternion(rotationQuaternion, new Cesium.HeadingPitchRoll())

Then I create a new Cesium.Camera() as:

var cameraToBeChecked =  new Cesium.Camera(scene);
cameraToBeChecked.setView({
    destination : cartesianPosition,
    orientation: {
        heading : hpr.heading, 
        pitch : hpr.pitch,    
        roll : hpr.roll                            
    }
});

And, the camera orientation is wrong. What I am doing wrong? How to orient new Cesium.Camera() properly with rotationMatrix or Quatrneion or HeadingPitchRoll ?

Second problem description:

To check if point given by user is Inside or Outside the camera frustum i use:

var userDefinedPoint = new Cesium.Cartesian3(.....)
var cullingVolume = frustum.computeCullingVolume(cameraToBeChecked.position,cameraToBeChecked.direction, cameraToBeChecked.up)
if (cullingVolume.computeVisibility(new Cesium.BoundingSphere(userDefinedPoint, 0.0)) === Cesium.Intersect.INSIDE || Cesium.Intersect.INTERSECTING ) {
	console.log(camera.label + ' is INSIDE')		
} else {
	console.log(camera.label + ' is OUTSIDE')
};

Is this a right way do do that?

Best regards,
Ilia Shevelev

Hi!

Have the same problem with Cesium.Camera() orientation, can anyone please help us guys?

1 Like

Can you reproduce the orientation issue in a Sandcastle? See: How to share custom Sandcastle examples

For the visibility check, yes this should work as long as you’re interested in just knowing whether the point is inside the frustum, see: How to answer the question "Can the camera see point (X,Y,Z) right now?"

1 Like

Thank you for your response, here is Sandcastle link

Hello @omar and community,

I still can’t figure out the solution myself and would appreciate any assistance.

Best regards

Hi @Ilia_Shevelev, for aligning the camera using the transformation matrix, I notice the first three columns of your matrix, which represents X, Y, and Z axis, aren’t normalized, so it may create a strange behavior for the camera. Another problem is that the Z axis which is the 3rd column of the matrix also points toward the earth. It should be negated as the camera direction is always opposite to the axis. Also I’m curious if the transformation matrix is constructed using the left-handed system by any chance? When I switch the column 0 and column 1 (X and Y axis), it works correctly after that. This is the Sandcastle example for the fix. The transformation matrix in the example is extracted from the camera DSC00230 in your Sandcastle. Please let me know if it helps.