How to calculate whether the point is online?

I have a line, How to calculate whether any point on the line overlaps with the line?
The coordinates are:

let coord = [-122.42166597493569, 37.66877558477902, -122.4188713733115, 37.669448382889094, -122.41642921573572, 37.66916144609525, -122.4146827046525, 37.66834407434515]
viewer.entities.add({
        name: "test",
        id: 'line_1',
        polyline: {
          positions: Cesium.Cartesian3.fromDegreesArray(coord),
          width: 13,
          clampToGround: true,
          material: Cesium.Color.RED,
        }
});

Hi @mcck,

Welcome to the community! Thank you for bringing us your question! It seems like you have created a polyline. How do you intend to add points to the scene?

-Sam

I want to determine whether the created point overlaps the line。
Add point:

	var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
	handler.setInputAction(function(movement){
		
		let cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(movement.position), viewer.scene);
		
		let point = viewer.entities.add({
			position: cartesian,
			point: {
			  color: Cesium.Color.WHITE,
			  pixelSize: 5,
			  heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
			  outlineColor: Cesium.Color.BLACK,
			  outlineWidth: 1
			},
		  });
		  
		if( xxx ){ // Do points and lines overlap
			// overlap
		} else {
			// No overlap
		}
		
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

Something like this?

	var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
	handler.setInputAction(function(movement){
		
        var feature = scene.pick(movement.endPosition);
		let cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(movement.position), viewer.scene);
		
		let point = viewer.entities.add({
			position: cartesian,
			point: {
			  color: Cesium.Color.WHITE,
			  pixelSize: 5,
			  heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
			  outlineColor: Cesium.Color.BLACK,
			  outlineWidth: 1
			},
		  });
		  
		if( feature == myPolylineEntity ){ // Do points and lines overlap
			// overlap
		} else {
			// No overlap
		}
		
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

If lots of entities end up in the near vicinity of each other, you move to scene.drillPick() and sift through the resulting array for your polyline entity instead.

Cheers,

Alex

2 Likes

thank you,If I don’t have windows position and only have Cartesian 3, how can I judge

I think in your example I modified, movement.endPosition should be movement.position. You’re using the screen position there to create the ray that returns the Cartesian3, so in a sense you’ve got both.

Cheers,

Alex

Maybe my expression is wrong. I only have cartesian3 now. I want to judge whether cartesian3 overlaps with polyline, windowPosition is just a test

Hello @mcck,
I have a similar problem (only to determine if a point is in a given area) and I thought about it a lot - I think that using a corridor instead of a polyline can help you - it has a height -
https://cesium.com/learn/cesiumjs/ref-doc/CorridorGeometry.html?classFilter=cor
this is how intersection tests can be used
https://cesium.com/learn/cesiumjs/ref-doc/IntersectionTests.html
here you can fantasize:
1 you can direct a ray from a point and determine its intersection with a primitive
2, you can define the intersection of the corridor primitive and the dimension sphere of the entity with the geometry of the point

  • there are a lot of pitfalls - I will not say exactly from memory now - however, there are definitely opportunities - when I have time, I will also return to the implementation of my tool and perhaps write more accurately with code examples. So far, I am only suggesting the direction of a possible solution.
    Hope this helps you.
2 Likes

Maybe I should change the way. I thought about it. I don’t have to use polyline to realize this. Thank you very much for your suggestion

1 Like