Skip to content

Commit 939d952

Browse files
authoredJan 5, 2018
Merge pull request #6093 from AnalyticalGraphicsInc/vectorProjection
Cartesian3 vector projection
2 parents f9bfe99 + 07c0173 commit 939d952

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
 

‎CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Change Log
77
*
88
* Added `ClippingPlaneCollection.isSupported` function for checking if rendering with clipping planes is supported.
99
* Improved CZML Custom Properties sandcastle example [#6086](https://github.com/AnalyticalGraphicsInc/cesium/pull/6086)
10+
* Added `Cartesian3.vectorProjection` for projecting one vector to another [#6093](https://github.com/AnalyticalGraphicsInc/cesium/pull/6093)
1011

1112
### 1.41 - 2018-01-02
1213

‎Source/Core/Cartesian3.js

+18
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,24 @@ define([
682682
return result;
683683
};
684684

685+
/**
686+
* Projects vector a onto vector b
687+
* @param {Cartesian3} a The vector that needs projecting
688+
* @param {Cartesian3} b The vector to project onto
689+
* @param {Cartesian3} result The result cartesian
690+
* @returns {Cartesian3} The modified result parameter
691+
*/
692+
Cartesian3.vectorProjection = function(a, b, result) {
693+
//>>includeStart('debug', pragmas.debug);
694+
Check.defined('a', a);
695+
Check.defined('b', b);
696+
Check.defined('result', result);
697+
//>>includeEnd('debug');
698+
699+
var scalar = Cartesian3.dot(a, b) / Cartesian3.dot(b, b);
700+
return Cartesian3.multiplyByScalar(b, scalar, result);
701+
};
702+
685703
/**
686704
* Compares the provided Cartesians componentwise and returns
687705
* <code>true</code> if they are equal, <code>false</code> otherwise.

‎Specs/Core/Cartesian3Spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,30 @@ defineSuite([
12411241
}).toThrowDeveloperError();
12421242
});
12431243

1244+
it('projects vector a onto vector b', function() {
1245+
var a = new Cartesian3(0.0, 1.0, 0.0);
1246+
var b = new Cartesian3(1.0, 0.0, 0.0);
1247+
var result = Cartesian3.vectorProjection(a, b, new Cartesian3());
1248+
expect(result).toEqual(new Cartesian3(0.0, 0.0, 0.0));
1249+
1250+
a = new Cartesian3(1.0, 1.0, 0.0);
1251+
b = new Cartesian3(1.0, 0.0, 0.0);
1252+
result = Cartesian3.vectorProjection(a, b, new Cartesian3());
1253+
expect(result).toEqual(new Cartesian3(1.0, 0.0, 0.0));
1254+
});
1255+
1256+
it('vectorProjection throws when missing parameters', function() {
1257+
expect(function() {
1258+
return Cartesian3.vectorProjection(undefined, new Cartesian3(), new Cartesian3());
1259+
}).toThrowDeveloperError();
1260+
expect(function() {
1261+
return Cartesian3.vectorProjection(new Cartesian3(), undefined, new Cartesian3());
1262+
}).toThrowDeveloperError();
1263+
expect(function() {
1264+
return Cartesian3.vectorProjection(new Cartesian3(), new Cartesian3(), undefined);
1265+
}).toThrowDeveloperError();
1266+
});
1267+
12441268
createPackableSpecs(Cartesian3, new Cartesian3(1, 2, 3), [1, 2, 3]);
12451269
createPackableArraySpecs(Cartesian3, [new Cartesian3(1, 2, 3), new Cartesian3(4, 5, 6)], [1, 2, 3, 4, 5, 6]);
12461270
});

0 commit comments

Comments
 (0)
Please sign in to comment.