Skip to content
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

Cartesian3 vector projection #6093

Merged
merged 1 commit into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `Cartesian3.vectorProjection` for projecting one vector to another [#6093](https://github.com/AnalyticalGraphicsInc/cesium/pull/6093)

### 1.41 - 2018-01-02

Expand Down
18 changes: 18 additions & 0 deletions Source/Core/Cartesian3.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,24 @@ define([
return result;
};

/**
* Projects vector a onto vector b
* @param {Cartesian3} a The vector that needs projecting
* @param {Cartesian3} b The vector to project onto
* @param {Cartesian3} result The result cartesian
* @returns {Cartesian3} The modified result parameter
*/
Cartesian3.vectorProjection = function(a, b, result) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to projectVector or projectVectors?

In general, try to use verbs for function names since they are actions.

//>>includeStart('debug', pragmas.debug);
Check.defined('a', a);
Check.defined('b', b);
Check.defined('result', result);
//>>includeEnd('debug');

var scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
return Cartesian3.multiplyByScalar(b, scalar, result);
};

/**
* Compares the provided Cartesians componentwise and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
Expand Down
24 changes: 24 additions & 0 deletions Specs/Core/Cartesian3Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,30 @@ defineSuite([
}).toThrowDeveloperError();
});

it('projects vector a onto vector b', function() {
var a = new Cartesian3(0.0, 1.0, 0.0);
var b = new Cartesian3(1.0, 0.0, 0.0);
var result = Cartesian3.vectorProjection(a, b, new Cartesian3());
expect(result).toEqual(new Cartesian3(0.0, 0.0, 0.0));

a = new Cartesian3(1.0, 1.0, 0.0);
b = new Cartesian3(1.0, 0.0, 0.0);
result = Cartesian3.vectorProjection(a, b, new Cartesian3());
expect(result).toEqual(new Cartesian3(1.0, 0.0, 0.0));
});

it('vectorProjection throws when missing parameters', function() {
expect(function() {
return Cartesian3.vectorProjection(undefined, new Cartesian3(), new Cartesian3());
}).toThrowDeveloperError();
expect(function() {
return Cartesian3.vectorProjection(new Cartesian3(), undefined, new Cartesian3());
}).toThrowDeveloperError();
expect(function() {
return Cartesian3.vectorProjection(new Cartesian3(), new Cartesian3(), undefined);
}).toThrowDeveloperError();
});

createPackableSpecs(Cartesian3, new Cartesian3(1, 2, 3), [1, 2, 3]);
createPackableArraySpecs(Cartesian3, [new Cartesian3(1, 2, 3), new Cartesian3(4, 5, 6)], [1, 2, 3, 4, 5, 6]);
});