From a5917ef15aa3fae0094ad84951787b6cc6a24dd2 Mon Sep 17 00:00:00 2001 From: hpinkos Date: Fri, 5 Jan 2018 13:36:39 -0500 Subject: [PATCH 1/3] project point onto plane --- CHANGES.md | 1 + Source/Core/Plane.js | 54 +++++++++++++++++++++++++++++------------ Specs/Core/PlaneSpec.js | 20 +++++++++++++++ 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ad81c175673f..30156d29c7f9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ Change Log * * Added `ClippingPlaneCollection.isSupported` function for checking if rendering with clipping planes is supported. * Improved CZML Custom Properties sandcastle example [#6086](https://github.com/AnalyticalGraphicsInc/cesium/pull/6086) +* Added `Plane.projectPointOntoPlane` for projecting a `Cartesian3` position onto a `Plane` [#6092](https://github.com/AnalyticalGraphicsInc/cesium/pull/6092) ### 1.41 - 2018-01-02 diff --git a/Source/Core/Plane.js b/Source/Core/Plane.js index 421e19512baf..d46f81462521 100644 --- a/Source/Core/Plane.js +++ b/Source/Core/Plane.js @@ -1,19 +1,19 @@ define([ - './Cartesian3', - './Check', - './defined', - './DeveloperError', - './freezeObject', - './Math', - './Matrix4' - ], function( - Cartesian3, - Check, - defined, - DeveloperError, - freezeObject, - CesiumMath, - Matrix4) { + './Cartesian3', + './Check', + './defined', + './DeveloperError', + './freezeObject', + './Math', + './Matrix4' +], function( + Cartesian3, + Check, + defined, + DeveloperError, + freezeObject, + CesiumMath, + Matrix4) { 'use strict'; /** @@ -156,6 +156,30 @@ define([ return Cartesian3.dot(plane.normal, point) + plane.distance; }; + var scratchCartesian = new Cartesian3(); + /** + * Projects a point onto the plane. + * @param {Plane} plane The plane to project the point onto + * @param {Cartesian3} point The point to project onto the plane + * @param {Cartesian3} [result] The result point. If undefined, a new Cartesian3 will be created. + */ + Plane.projectPointOntoPlane = function(plane, point, result) { + //>>includeStart('debug', pragmas.debug); + Check.typeOf.object('plane', plane); + Check.typeOf.object('point', point); + //>>includeEnd('debug'); + + if (!defined(result)) { + result = new Cartesian3(); + } + + // projectedPoint = point - (normal.point + scale) * normal + var pointDistance = Plane.getPointDistance(plane, point); + var scaledNormal = Cartesian3.multiplyByScalar(plane.normal, pointDistance, scratchCartesian); + + return Cartesian3.subtract(point, scaledNormal, result); + }; + var scratchPosition = new Cartesian3(); /** * Transforms the plane by the given transformation matrix. diff --git a/Specs/Core/PlaneSpec.js b/Specs/Core/PlaneSpec.js index 5d258c5c18b3..ee120c64d27c 100644 --- a/Specs/Core/PlaneSpec.js +++ b/Specs/Core/PlaneSpec.js @@ -129,6 +129,26 @@ defineSuite([ }).toThrowDeveloperError(); }); + it('projects a point onto the plane', function() { + var plane = new Plane(Cartesian3.UNIT_X, 0.0); + var point = new Cartesian3(1.0, 1.0, 0.0); + var result = Plane.projectPointOntoPlane(plane, point); + expect(result).toEqual(new Cartesian3(0.0, 1.0, 0.0)); + + plane = new Plane(Cartesian3.UNIT_Y, 0.0); + result = Plane.projectPointOntoPlane(plane, point); + expect(result).toEqual(new Cartesian3(1.0, 0.0, 0.0)); + }); + + it('projectPointOntoPlane uses result parameter', function() { + var plane = new Plane(Cartesian3.UNIT_X, 0.0); + var point = new Cartesian3(1.0, 1.0, 0.0); + var result = new Cartesian3(); + var returnedResult = Plane.projectPointOntoPlane(plane, point, result); + expect(result).toBe(returnedResult); + expect(result).toEqual(new Cartesian3(0.0, 1.0, 0.0)); + }); + it('clone throws without a plane', function() { expect(function() { Plane.clone(undefined); From 23f51bf11e2a202ae237c40b61633cdccfe65782 Mon Sep 17 00:00:00 2001 From: hpinkos Date: Fri, 5 Jan 2018 13:51:16 -0500 Subject: [PATCH 2/3] more specs --- Specs/Core/PlaneSpec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Specs/Core/PlaneSpec.js b/Specs/Core/PlaneSpec.js index ee120c64d27c..01f9aed36084 100644 --- a/Specs/Core/PlaneSpec.js +++ b/Specs/Core/PlaneSpec.js @@ -149,6 +149,16 @@ defineSuite([ expect(result).toEqual(new Cartesian3(0.0, 1.0, 0.0)); }); + it('projectPointOntoPlane requires the plane and point parameters', function() { + expect(function() { + return Plane.projectPointOntoPlane(new Plane(Cartesian3.UNIT_X, 0), undefined); + }).toThrowDeveloperError(); + + expect(function() { + return Plane.projectPointOntoPlane(undefined, new Cartesian3()); + }).toThrowDeveloperError(); + }); + it('clone throws without a plane', function() { expect(function() { Plane.clone(undefined); From 57b75cba9db56f3645c952c4f35158f4f8581fec Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 5 Jan 2018 16:04:59 -0500 Subject: [PATCH 3/3] Whitespace. --- Source/Core/Plane.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Source/Core/Plane.js b/Source/Core/Plane.js index d46f81462521..1a688eced55d 100644 --- a/Source/Core/Plane.js +++ b/Source/Core/Plane.js @@ -1,19 +1,19 @@ define([ - './Cartesian3', - './Check', - './defined', - './DeveloperError', - './freezeObject', - './Math', - './Matrix4' -], function( - Cartesian3, - Check, - defined, - DeveloperError, - freezeObject, - CesiumMath, - Matrix4) { + './Cartesian3', + './Check', + './defined', + './DeveloperError', + './freezeObject', + './Math', + './Matrix4' + ], function( + Cartesian3, + Check, + defined, + DeveloperError, + freezeObject, + CesiumMath, + Matrix4) { 'use strict'; /**