Rotation of a polygon

235 views
Skip to first unread message

amitb...@gmail.com

unread,
Dec 17, 2018, 6:15:10 AM12/17/18
to cesium-dev
I have added a polygon and tried rotating it mouse movement. I tried stRotation but it was not working. Is there any other way to rotate the polygon ?

Omar Shehata

unread,
Dec 17, 2018, 3:52:52 PM12/17/18
to cesium-dev
Can you share the code you've tried? Perhaps as a Sandcastle (https://cesiumjs.org/Cesium/Build/Apps/Sandcastle/) example that I can also run?

This GitHub issue has an example of rotating a polygon:

amitb...@gmail.com

unread,
Dec 18, 2018, 12:16:19 AM12/18/18
to cesium-dev

Omar Shehata

unread,
Dec 18, 2018, 1:48:14 PM12/18/18
to cesium-dev
Ah, I see what you mean now. The stRotation only works for the texture or material on the polygon, not the actual geometry.

Unfortunately, CesiumJS does not expose a modelMatrix on entities, which we could have constructed here to apply a local transformation to rotate it. What you can do is rotate the points themselves with a callback property. The easiest way to do that is probably using Turf.js's rotate function:


Since you can't include JavaScript libraries in Sandcastle I put together a demo on Glitch:


You can see the code for it here:


And just in case I accidentally delete that example, here's the full code using Turf to rotate the polygon:

var viewer = new Cesium.Viewer('cesiumContainer');

    var rotation ;
    var lastRotation;
    var lastPoints;

    function getRotationValue() {
      rotation =  document.getElementById("rotate_ent").value;
      rotation = parseFloat(rotation);
      if (rotation === lastRotation) {
        return lastPoints;
      }

      lastRotation = rotation;
      var poly = turf.polygon([[ [-75.9607370, 37.1804856], [-75.9578855, 37.1861531], [-75.9542902, 37.1852787], [-75.9546776, 37.1816414], [-75.9578841, 37.1823743], [-75.9584821, 37.1800066], [-75.9607370, 37.1804856] ]]);
      var rotatedPoly = turf.transformRotate(poly, rotation);

      var pointsArray = [];
      for(let point of rotatedPoly.geometry.coordinates[0]) {
        pointsArray.push(point[0]);
        pointsArray.push(point[1]);
      }

      lastPoints = Cesium.Cartesian3.fromDegreesArray(pointsArray);
      return lastPoints;
    }


    viewer.entities.add({
        polygon: {
            hierarchy : new Cesium.CallbackProperty(getRotationValue, false),
            material : Cesium.Color.BLUE.withAlpha(0.5),
        extrudedHeight : 300.0,
            classificationType : Cesium.ClassificationType.TERRAIN
        }
    });

    viewer.zoomTo(viewer.entities);

Hope this helps!

amitb...@gmail.com

unread,
Dec 19, 2018, 4:04:29 AM12/19/18
to cesium-dev
It's working. Thank You so much.

But now how can I rotate the polygon with mouse movement instead of rotating it from the input of the range bar ?

Omar Shehata

unread,
Dec 19, 2018, 9:33:09 AM12/19/18
to cesium-dev
You can get the mouse position with regular JavaScript or with Cesium's event handlers:

var handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
handler
.setInputAction(function(movement) {
    console
.log(movement);
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

You can see examples of using this on Sandcastle, here's one that lets you draw with the mouse:


You might also need a way to convert from screen coordinates to 3d world coordinates and back. If so, check out the functions I describe in this post:

amitb...@gmail.com

unread,
Feb 26, 2019, 6:32:13 AM2/26/19
to cesium-dev
Hi,

Here for a single polygon, this is rotating is working properly. But how to rotate the selected polygon when there are multiple polygons are there on the map ?

The issue which I'm facing is that when using turf.polygon([coordinates]) while trying to rotate a polygon, all the polygons which are present on the map are merging into single polygon.

Omar Shehata

unread,
Feb 26, 2019, 6:50:01 PM2/26/19
to cesium-dev
I think you'll need to keep them in an array/list and run turf.polygon() on each list of coordinates separately. 
Reply all
Reply to author
Forward
0 new messages