VelocityOrientationProperty.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import Cartesian3 from '../Core/Cartesian3.js';
  2. import defaultValue from '../Core/defaultValue.js';
  3. import defined from '../Core/defined.js';
  4. import defineProperties from '../Core/defineProperties.js';
  5. import Ellipsoid from '../Core/Ellipsoid.js';
  6. import Event from '../Core/Event.js';
  7. import Matrix3 from '../Core/Matrix3.js';
  8. import Quaternion from '../Core/Quaternion.js';
  9. import Transforms from '../Core/Transforms.js';
  10. import Property from './Property.js';
  11. import VelocityVectorProperty from './VelocityVectorProperty.js';
  12. /**
  13. * A {@link Property} which evaluates to a {@link Quaternion} rotation
  14. * based on the velocity of the provided {@link PositionProperty}.
  15. *
  16. * @alias VelocityOrientationProperty
  17. * @constructor
  18. *
  19. * @param {Property} [position] The position property used to compute the orientation.
  20. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid used to determine which way is up.
  21. *
  22. * @example
  23. * //Create an entity with position and orientation.
  24. * var position = new Cesium.SampledProperty();
  25. * position.addSamples(...);
  26. * var entity = viewer.entities.add({
  27. * position : position,
  28. * orientation : new Cesium.VelocityOrientationProperty(position)
  29. * }));
  30. */
  31. function VelocityOrientationProperty(position, ellipsoid) {
  32. this._velocityVectorProperty = new VelocityVectorProperty(position, true);
  33. this._subscription = undefined;
  34. this._ellipsoid = undefined;
  35. this._definitionChanged = new Event();
  36. this.ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  37. var that = this;
  38. this._velocityVectorProperty.definitionChanged.addEventListener(function() {
  39. that._definitionChanged.raiseEvent(that);
  40. });
  41. }
  42. defineProperties(VelocityOrientationProperty.prototype, {
  43. /**
  44. * Gets a value indicating if this property is constant.
  45. * @memberof VelocityOrientationProperty.prototype
  46. *
  47. * @type {Boolean}
  48. * @readonly
  49. */
  50. isConstant : {
  51. get : function() {
  52. return Property.isConstant(this._velocityVectorProperty);
  53. }
  54. },
  55. /**
  56. * Gets the event that is raised whenever the definition of this property changes.
  57. * @memberof VelocityOrientationProperty.prototype
  58. *
  59. * @type {Event}
  60. * @readonly
  61. */
  62. definitionChanged : {
  63. get : function() {
  64. return this._definitionChanged;
  65. }
  66. },
  67. /**
  68. * Gets or sets the position property used to compute orientation.
  69. * @memberof VelocityOrientationProperty.prototype
  70. *
  71. * @type {Property}
  72. */
  73. position : {
  74. get : function() {
  75. return this._velocityVectorProperty.position;
  76. },
  77. set : function(value) {
  78. this._velocityVectorProperty.position = value;
  79. }
  80. },
  81. /**
  82. * Gets or sets the ellipsoid used to determine which way is up.
  83. * @memberof VelocityOrientationProperty.prototype
  84. *
  85. * @type {Property}
  86. */
  87. ellipsoid : {
  88. get : function() {
  89. return this._ellipsoid;
  90. },
  91. set : function(value) {
  92. var oldValue = this._ellipsoid;
  93. if (oldValue !== value) {
  94. this._ellipsoid = value;
  95. this._definitionChanged.raiseEvent(this);
  96. }
  97. }
  98. }
  99. });
  100. var positionScratch = new Cartesian3();
  101. var velocityScratch = new Cartesian3();
  102. var rotationScratch = new Matrix3();
  103. /**
  104. * Gets the value of the property at the provided time.
  105. *
  106. * @param {JulianDate} [time] The time for which to retrieve the value.
  107. * @param {Quaternion} [result] The object to store the value into, if omitted, a new instance is created and returned.
  108. * @returns {Quaternion} The modified result parameter or a new instance if the result parameter was not supplied.
  109. */
  110. VelocityOrientationProperty.prototype.getValue = function(time, result) {
  111. var velocity = this._velocityVectorProperty._getValue(time, velocityScratch, positionScratch);
  112. if (!defined(velocity)) {
  113. return undefined;
  114. }
  115. Transforms.rotationMatrixFromPositionVelocity(positionScratch, velocity, this._ellipsoid, rotationScratch);
  116. return Quaternion.fromRotationMatrix(rotationScratch, result);
  117. };
  118. /**
  119. * Compares this property to the provided property and returns
  120. * <code>true</code> if they are equal, <code>false</code> otherwise.
  121. *
  122. * @param {Property} [other] The other property.
  123. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  124. */
  125. VelocityOrientationProperty.prototype.equals = function(other) {
  126. return this === other ||//
  127. (other instanceof VelocityOrientationProperty &&
  128. Property.equals(this._velocityVectorProperty, other._velocityVectorProperty) &&
  129. (this._ellipsoid === other._ellipsoid ||
  130. this._ellipsoid.equals(other._ellipsoid)));
  131. };
  132. export default VelocityOrientationProperty;