-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoplanarPolygonGeometry and CoplanarPolygonOutlineGeometry #6769
Conversation
@hpinkos, thanks for the pull request! Maintainers, we have a signed CLA from @hpinkos, so you can review this at any time. I am a bot who helps you make Cesium awesome! Contributions to my configuration are welcome. 🌍 🌎 🌏 |
This doesn't connect anything to the Entity API layer. I was planning to figure out if and how we want to do that in a separate PR. |
@ggetz can you review? |
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> | ||
<meta name="description" content="Draw the outline of a cylinder."> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update description.
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> | ||
<meta name="description" content="Draw an ellipsoid."> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update description.
|
||
var i; | ||
// If all the points are on a line, just remove one of the zero dimensions | ||
if ((xMag === 0 && (yMag === 0 || zMag === 0)) || (yMag === 0 && zMag === 0)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be some sort of error or warning rather than the geometry just not being created?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess to answer my own question, that is the behavior when less than 3 positions are supplied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, we decided a while ago to just return undefined and not render invalid geometry instead of throwing an error. This is mostly because there's a lot of garbage data in KML and GeoJSON files that are automatically generated and we don't want to crash the whole file because we don't know how to render one of the polygons
var zMag = Cartesian3.magnitude(zAxis); | ||
var min = Math.min(xMag, yMag, zMag); | ||
|
||
var i; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declare i
down where it's being used.
|
||
/** | ||
* A description of a polygon composed of arbitrary coplanar positions. | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we mention in the docs that if the positions are not coplanar, it will re-project them to a plane?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't. It will attempt to draw whatever positions are passed in, but for best results the positions should be coplanar
* | ||
* @param {Object} options Object with the following properties: | ||
* @param {Cartesian3[]} options.positions The positions of the polygon | ||
* @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also have stRotation
as an option? PolygonGeometry
has this as an option. Besides the polygon hierarchy, all the other options aren't applicable here AFAIK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, trying to keep the options as minimal as possible for this one. We can expand it later if someone asks or it.
if (positions.length < 3) { | ||
return; | ||
} | ||
var bs = BoundingSphere.fromPoints(positions); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the coding guide, I believe we avoid abbreviations.
|
||
var attributes = new GeometryAttributes(); | ||
|
||
if (vertexFormat.position) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we always have position?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically yes, but someone could technically define whatever they want for vertexFormat
Check.defined('positionsResult', positionsResult); | ||
//>>includeEnd('debug'); | ||
|
||
var obb = OrientedBoundingBox.fromPoints(positions, obbScratch); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here about abbreviations.
var obbScratch = new OrientedBoundingBox(); | ||
|
||
// call after removeDuplicates | ||
PolygonGeometryLibrary.projectTo2D = function(positions, positionsResult, normalResult, tangentResult, bitangentResult) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a spec for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's covered between CoplanarPolygonGeometry
and CoplanarPolygonOutlineGeometry
. We don't have specs for any of the other *GeometryLibrary
static classes.
@hpinkos Can you clarify what projectTo2D is doing in cases where the geometry is not coplanar, like in this case? |
|
var planeYAxis; | ||
var origin = Cartesian3.clone(center, scratchOrigin); | ||
var normal; | ||
if (min === xMag) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be more simple. Once you find the vectors with the minimum and maximum length:
z = cross(x, y); // normal
y = cross(z, x);
// normalize x, y, and z
It doesn't matter if you use z or -z for the normal. It also doesn't matter if you use +/-x or +/-y as long as they are orthogonal in the plane.
var normal; | ||
if (min === xMag) { | ||
if (min !== 0) { | ||
origin = Cartesian3.add(origin, xAxis, origin); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why move the origin from the bounding box center?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because all the points are already in a plane and I need a new plane to project them onto =P
There's definitely a better way to do this. I hadn't given this logic any thought since I wrote it at our first code sprint down the shore. Thanks for pointing that out!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no wait! It's because I want the intersection plane to be where the OBB box face is instead of at the center of the box.
for (i = 0; i < positions.length; i++) { | ||
ray.origin = Cartesian3.clone(positions[i], ray.origin); | ||
|
||
var intersectionPoint = IntersectionTests.rayPlane(ray, plane, scratchIntersectionPoint); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to intersect the ray with the plane. You just need this:
v = subtract(positions[i], origin);
x = dot(xAxis, v);
y = dot(yAxis, v);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that's the case when the positions are actually coplanar. But I need it to work with positions that are mostly coplanar and to "try it's best" for a set of arbitrary points so I think I still need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I guess you're right though, the results come out the same for both. Thanks!
return; | ||
} | ||
var center = orientedBoundingBox.center; | ||
var origin = Cartesian3.clone(center, scratchOrigin); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need this anymore. Just subtract from center below.
👍 Looks good to me, just that one last comment. @ggetz can merge when ready. |
@ggetz ready |
👍 Thanks @hpinkos! |
how do things render for this example? |
@bmckilligan all fixed! |
Adds support for drawing polygons composed of arbitrary coplanar positions. (It doesn't crash if the positions are not coplanar, but the resulting triangulation is unpredictable and probably won't work as desired)
Related #3349 (adds support for vertical polygons but doesn't fix that KML file)
Test code: