How can I get the world coordinates of the node of the gltf model in cesium, such as a plane.gltf, I want to get the world coordinates of the wheel - cesiumjs

How can I get the world coordinates of the node of the gltf model in cesium, such as a plane.gltf, I want to get the world coordinates of the wheel?
(1)load glb
cesium.primitiveFighter = cesium.viewer.scene.primitives.add(
Cesium.Model.fromGltf({
url: "f18_v13.glb",
modelMatrix: modelMatrix,
scale: 20.0,
minimumPixelSize: 16,
maximumScale: 300,
})
);
(2) get node
var node = model.getNode("wheel");
(3) matrix transformaton
node.matrix =
Cesium.Matrix4.fromTranslationQuaternionRotationScale(
new Cesium.Cartesian3(
element.cannonBody.position.x,
element.cannonBody.position.y,
element.cannonBody.position.z
), // translation
element.cannonBody.quaternion, // rotation
new Cesium.Cartesian3(1.0, 1.0, 1.0), // scale
element.cesiumNode.matrix
);
(4)
How to get the world coordinates after node transformation?
My idea is to get the mesh corresponding to the node, but it seems that I can't get it

Related

How to move the camera in a forge viewer to face the true north direction, using the euler angles

I understand that forge viewer uses three.js extensively, I have a couple of questions
I want to point my forge viewer camera to the north direction (true north) and further synchronise the rotation based on the north values.
Also is it possible to set the bounds ?
I'm trying to synchronise forge viewer based on a set of euler angles (pitch, yaw and roll) available at my hand
I'm using the forge viewer version 7.
Fareed also asked this via email, so I'm copying & pasting my replies here.
Not sure which source model format you used, so suppose it's Revit (RVT).
In Revit model metadata, two attributes can help calculate the north rotation to the true north.
metadata['world north vector']['XYZ']: The project north vector of the Revit view.
metadata['custom values']['angleToTrueNorth']: The angel from the project north to true north of Revit view.
// Calculate project north angle
const projectNorthVector = new THREE.Vector3().fromArray( model.getData().metadata['world north vector']['XYZ'] );
const autoCam = viewer.autocam;
const frontDirection = autoCam.sceneFrontDirection.clone(); //!<<< viewer world north
const upVector = autoCam.sceneUpDirection.clone();
let crossVector = new THREE.Vector3();
crossVector.crossVectors( frontDirection, projectNorthVector );
const projectNorthAngle = projectNorthVector.angleTo( frontDirection ) * ( crossVector.dot( upVector ) < 0 ? -1 : 1 );
// Calculate true north angle
let trueNorthAngle = metadata['custom values']['angleToTrueNorth'] * (Math.PI / 180);
// Final rotation angle from viewer world north to true north
const finalRotationAngle = projectNorthAngle + trueNorthAngle;
// and then rotate your vector by Z.
Sorry, I'm not familiar with the Euler angles (pitch, yaw, and roll), but from three.js documentation, I can see it uses intrinsic Tait-Bryan angles.
Three.js uses intrinsic Tait-Bryan angles. This means that rotations are performed with respect to the local coordinate system. That is, for order 'XYZ', the rotation is first around the local-X axis (which is the same as the world-X axis), then around local-Y (which may now be different from the world Y-axis), then local-Z (which may be different from the world Z-axis).
So, probably, you can try to get that with the either way below:
Use three.js API to get Euler Tait-Bryan angles from quaternion
const quaternion = viewer.getCamera().quaternion.clone();
const rotation = new THREE.Euler().setFromQuaternion( quaternion, 'XYZ' );
Or get it from the camera's rotation
const { rotation } = viewer.getCamera();
const eulerOrder = rotation.order;
Or refer to the Navisworks approach: https://adndevblog.typepad.com/aec/2019/07/get-roll-value-of-edit-current-viewpoint.html
viewer.navigation.setCameraUpVector( new THREE.Vector3(0,1,0), true );
const quaternion = viewer.getCamera().quaternion.clone();
let { x, y, z, w } = quaternion;
let roll = Math.atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z);
let pitch = Math.atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z);
let yaw = Math.asin(2*x*y + 2*z*w);
To set Euler angles to camera, here is an approach, but I think you will need to change the Euler order if it's not XYZ.
const euler = new THREE.Euler(..., ..., ..., 'XYZ');
viewer.getCamera().quaternion..setFromEuler(euler);

GLTF file not well positioned by Cesium

I want to display a hurricane (big isosurface object) in Cesium. For this I converted an OBJ file with longitude, latitude, altitude columns for each vertex of the isosurface representing the hurricane, in a new OBJ file reprojected in ECEF (Earth Centered) projection.So the final OBJ file contains now X,Y,Z for each vertex instead of longitude, latitude, altitude. After final reformat by obj2gltf, I try to display the GLTF "hurricane" file in Cesium.JS using the code below:
console.log('loading hurricane.gltf';
var mymodel = viewer.scene.primitives.add(Cesium.Model.fromGltf({
url : 'data/hurricane.gltf',
modelMatrix : Cesium.Matrix4.IDENTITY,
asynchronous: false
}));
I can see my hurricane on the earth, but not at the good position. I suspect a problem of matrix. IDENTITY matrix seems not to be the good one. I could try to make a new matrix but I can't find enough informations about the axes orientation used by Cesium.
I verified the X,Y,Z ECEF coordinates, they are good. Does anyone already meet this problem ?
If your glTF model origin is at the center of the hurricane, you can place it using a Cesium Entity, something like this:
// Longitude degrees, Latitude degrees, height in meters
var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, height);
var heading = Cesium.Math.toRadians(0);
var pitch = 0;
var roll = 0;
var hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);
var orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);
var entity = viewer.entities.add({
name : 'Hurricane',
position : position,
orientation : orientation,
model : {
uri : 'data/hurricane.gltf'
}
});
viewer.trackedEntity = entity;
There are more complete working demos of this on Sandcastle.
But, if your hurricane is visible on the surface of the Earth using the identity matrix, that likely means that the origin of that model is nowhere near the center of the hurricane. You may need to edit the glTF file, to make sure that the model is centered on its own origin, and does not have some fixed Earth location pre-baked into the model's internal transformations.

Random GPS coordinates in a non rectangular space

I am working on an user interface that shows many pins on a map.
During the development I am randomly generating 1500 map pins just to be placed on the map to test look/feel/performance etc. issues.
The code which does that looks like this:
for (var i = 0; i <= 1500; i += 1) {
$scope.mapPins.push({
latitude: (Math.random() * 2) + 51,
longitude: (Math.random() * 4) + 3,
icon: themeImages[Math.floor(Math.random() * themeImages.length)],
title: 'Sample title',
infoContent: 'Sample content'
});
}
Naturally the area of the pins covered is a rectangle for latitudes 51-53 and longitudes 3-7. For those who are wondering where it is, it is the area roughly around Netherlands.
Now, there's a little problem that the Netherlands is not a rectangular area and a lot of these coordinates fall over the sea and I would like my coordinates to be only on the land.
Is there a witty mathematical way how I can pool coordinates from a non-rectangular area?
Of course I could make a google.maps polygon object that covers a nonrectangular shape and then via google api test every random generated pin whether it falls within the bounds of this shape etc, but that would be an overkill for UI design phase. Basically my question is whether there is a neat mathematical trick that would allow me to randomly generate coordinates from a non-rectangular space.
Leave your code as it is, the rectangle is the bounding box over your area of interest.
Then add a line
if (isPointInpolygon(polygon, longitudeOrX, latitudeOrY) {
// use this location
}
now you only need to search for a point in polygon function, which is easy to find.
you can directly use the coordinates in (long, lat) order, longitude is related to x coordinate, lat to y.
The polygon has to be filled with the coordinates of the country not insode the water.
If you have islands, then maybe you need multiple such polygons, then iterate over all.
Not to be a stickler but you're actually generating 1501 map pins :)
It is very unlikely that you'll find a simpler solution than using a simple pointinpolygon check.
Use the Google Maps Drawing library (https://developers.google.com/maps/documentation/javascript/drawing#using_the_library) to draw a polygon around the boundary of the Netherlands and save it however you want (e.g., in database, or just copy the string that defines the boundary's coordinates).
Then in your script above, define the google maps polygon (similar to what is done here in the official docs: https://developers.google.com/maps/documentation/javascript/shapes#polygons), then use the containsLocation method in the Google Maps Geometry library (https://developers.google.com/maps/documentation/javascript/examples/poly-containsLocation) to check if your random map pins lie within the boundaries of the Netherlands before adding them to the map.
For example:
var netherlandsCoords = [
// comma-separated list of coordinates defining the Netherlands boundary
];
var netherlandsBoundary = new google.maps.Polygon({
path: netherlandsCoords
});
for (var i = 0; i <= 1500; i += 1) {
var lat = (Math.random() * 2) + 51;
var lng = (Math.random() * 4) + 3;
var latlng = new google.maps.LatLng(lat, lng);
if (google.maps.geometry.poly.containsLocation(latlng, netherlandsBoundary)) {
$scope.mapPins.push({
latitude: lat,
longitude: lng,
icon: themeImages[Math.floor(Math.random() * themeImages.length)],
title: 'Sample title',
infoContent: 'Sample content'
});
}
}

WebGL Three.js : Texture alingment on a geometry face

I would like to write text on each faces of an IcosahedronGeometry
I'm able to generate the textures and apply the textures to all the faces :
for ( var i = 0; i < geometry.faces.length; i ++ ) {
geometry.faces[i].materialIndex = i;
materials.push( new THREE.MeshBasicMaterial( { overdraw: true, map: getTexture(i), wireframe: true, wireframeLinewidth: 1} ) );
}
// 3D element
element = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial(materials) );
However each textures are overwriting the other ... And I can't align them correctly
http://jsfiddle.net/jzbf7/
Any idea ?
You need to understand how the UVs are set up for IcosahedronGeometry -- they are very similar to the UVs for SphereGeometry, in which a map of the world will cover the entire sphere.
This is very different from the UVs for CubeGeometry, where the texture maps to each face.
Experiment with the updated fiddle to see for yourself: http://jsfiddle.net/jzbf7/2/
(If the sphere renders too dark, render it again -- the colors are random.)
Also, there is a bug in the IcosahedronGeometry UV map. This can be seen at the "seam".
three.js r.56

Google Maps Flash - Polygon with Holes, Crossing the meridian

I have a problem with creating an overlay with a polygon that contains
multiple polylines.
I want to create a polygon with holes cut out for certain countries (to show
which areas are off limits). I do this by creating a polygone with multiple
polylines.
The first polyline was created to cover the entire visible area of the map.
i started off with this:
To get around only spanning one region-180/180
var outer:Array = [ new LatLng(90, -180),
new LatLng(90, 180),
new LatLng(-90, 180),
new LatLng(-90, -180),
new LatLng(90, -180)]
donut = new Polygon([],
new PolygonOptions({
strokeStyle: new StrokeStyle({
thickness: 0}),
fillStyle: new FillStyle({
color: 0x000000,
alpha: 0.5})
}));
donut.setPolyline(0, outer)
map.addOverlay(donut);
Obviously this has a problem with only spanning one lot of lat/lng. And when
zoomed out, this doesnt work. To get around this i tried getting the bounds
of the map, toSpan() and getNorth(),getSouth() and so on. These didnt return
a true span of the visible map - as it maxed out at 180/360.
My solution was to use fromViewportToLatLng() of the pixel points of the
map, and set the opt_nowrap to 'true'
var pnw:LatLng = map.fromViewportToLatLng(new Point(0, 0), true)
var pse:LatLng = map.fromViewportToLatLng(new Point(map.width, map.height),
true)
This produced results similar to this
Lat Lng North West (89.77386689378173, -614.3294000000001)
LatLng South East (-89.07253945829217, 370.0455999999999)
From here i can then correctly set the polyline (on map move and zoom
events) to cover the entire map, no matter how many spans of longitude
occur:
outer = [
new LatLng(pnw.lat(), pnw.lng(), true),
new LatLng(pnw.lat(), pse.lng(), true),
new LatLng(pse.lat(), pse.lng(), true),
new LatLng(pse.lat(), pnw.lng(), true),
new LatLng(pnw.lat(), pnw.lng(), true)
]
donut.setPolyline(0, outer)
*So far so good. *
Now, i want to add the donut holes to my polygon, again adding polylines to
the donut polygon.
private var australia:Array = [
new LatLng(-9.7716, 143.0241),
new LatLng(-23.4610, 158.1852),
new LatLng(-45.1338, 147.1549),
new LatLng(-35.2615, 111.5153),
new LatLng(-20.6921, 113.0094),
new LatLng(-10.0746, 130.3239),
new LatLng(-9.7716, 143.0241)
]
private var nz:Array = [
new LatLng(-33.5951, 165.8254),
new LatLng(-33.5951, 179.7341),
new LatLng(-48.3845, 179.7341),
new LatLng(-48.3845, 165.8254),
new LatLng(-33.5951, 165.8254)
]
private var hawaii:Array = [
new LatLng(21.8000, -160.4347),
new LatLng(22.5477, -159.7975),
new LatLng(21.4067, -156.3533),
new LatLng(19.5336, -154.4197),
new LatLng(18.6511, -155.6392),
new LatLng(20.6633, -157.8639),
new LatLng(21.8000, -160.4347)
]
donut.setPolyline(1, hawaii)
donut.setPolyline(2, nz)
donut.setPolyline(3, australia)
The problem lies when the meridian line is visible in the map viewport. This
problem is more evident when zommed in, as the donut hole polylines jump
from the visible viewport to the other side of a lat long span.
Sometimes the outer array doesnt even correctly render, causing the donut
polygon to essentialy invert (polygons over the countries, rather than
cutouts)
I realise this is a tough one to visualise. I have attached the .as + .fla
file in case anyone wants to test it out.
http://www.digital.leskiwis.com/maps/MapTest.zip
So you're trying to make a big polygon with holes cut out for Australia, nz, Hawaii,...? Your code for adding the polylines seems wrong to me. I'd start with the big square:
var outer:Array = [ new LatLng(90, -180),
new LatLng(90, 180),
new LatLng(-90, 180),
new LatLng(-90, -180),
new LatLng(90, -180)]
donut = new Polygon([outer],
new PolygonOptions({
strokeStyle: new StrokeStyle({
thickness: 0}),
fillStyle: new FillStyle({
color: 0x000000,
alpha: 0.5})
}));
Then add your holes
//define au, nz, hi
//...
//the 0th polyline is already the outer edge...
donut.setPolyline(1, hi);
donut.setPolyline(2, nz);
donut.setPolyline(3, au);
Now you dounut is a big rectangle with 3 holes.
Then add it to the map:
map.addOverlay(donut);
I've worked with lots of polygons that crossed hemispheres and meridians in Google Maps before, you don't have to mess with the viewport or zoom region or anything like that. As long as you're defining the polygon the way you think you are, GM will draw it correctly in any bounding rect (even if you are zoomed in so far that no edge of the polygon is visible).
The other thing I see is that the giant polygon you're making to cover the whole world is a little ambiguous: For example 90,-180 and 90,180 are the same point (the north pole) a line connecting those coordinates would have 0 length. Doing stuff at the poles is kinda effed in Mercator. Any possibility of cutting it off a 89° N? I think that would make your outer polygon more happy.