Entity.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*global define*/
  2. define([
  3. '../Core/Cartesian3',
  4. '../Core/createGuid',
  5. '../Core/defaultValue',
  6. '../Core/defined',
  7. '../Core/defineProperties',
  8. '../Core/DeveloperError',
  9. '../Core/Event',
  10. '../Core/Matrix3',
  11. '../Core/Matrix4',
  12. '../Core/Quaternion',
  13. '../Core/Transforms',
  14. './createPropertyDescriptor',
  15. './Property'
  16. ], function(
  17. Cartesian3,
  18. createGuid,
  19. defaultValue,
  20. defined,
  21. defineProperties,
  22. DeveloperError,
  23. Event,
  24. Matrix3,
  25. Matrix4,
  26. Quaternion,
  27. Transforms,
  28. createPropertyDescriptor,
  29. Property) {
  30. "use strict";
  31. /**
  32. * Entity instances are the primary data store for processed data.
  33. * They are used primarily by the visualizers to create and maintain graphic
  34. * primitives that represent the Entity's properties at a specific time.
  35. * @alias Entity
  36. * @constructor
  37. *
  38. * @param {String} [id] A unique identifier for this object. If no id is provided, a GUID is generated.
  39. *
  40. * @see Property
  41. * @see EntityCollection
  42. */
  43. var Entity = function(id) {
  44. if (!defined(id)) {
  45. id = createGuid();
  46. }
  47. this._availability = undefined;
  48. this._id = id;
  49. this._definitionChanged = new Event();
  50. this._name = undefined;
  51. this._parent = undefined;
  52. this._propertyNames = ['billboard', 'box', 'corridor', 'cylinder', 'description', 'ellipse', //
  53. 'ellipsoid', 'label', 'model', 'orientation', 'path', 'point', 'polygon', //
  54. 'polyline', 'position', 'rectangle', 'viewFrom', 'wall'];
  55. this._billboard = undefined;
  56. this._billboardSubscription = undefined;
  57. this._box = undefined;
  58. this._boxSubscription = undefined;
  59. this._corridor = undefined;
  60. this._corridorSubscription = undefined;
  61. this._cylinder = undefined;
  62. this._cylinderSubscription = undefined;
  63. this._description = undefined;
  64. this._descriptionSubscription = undefined;
  65. this._ellipse = undefined;
  66. this._ellipseSubscription = undefined;
  67. this._ellipsoid = undefined;
  68. this._ellipsoidSubscription = undefined;
  69. this._label = undefined;
  70. this._labelSubscription = undefined;
  71. this._model = undefined;
  72. this._modelSubscription = undefined;
  73. this._orientation = undefined;
  74. this._orientationSubscription = undefined;
  75. this._path = undefined;
  76. this._pathSubscription = undefined;
  77. this._point = undefined;
  78. this._pointSubscription = undefined;
  79. this._polygon = undefined;
  80. this._polygonSubscription = undefined;
  81. this._polyline = undefined;
  82. this._polylineSubscription = undefined;
  83. this._position = undefined;
  84. this._positionSubscription = undefined;
  85. this._rectangle = undefined;
  86. this._rectangleSubscription = undefined;
  87. this._viewFrom = undefined;
  88. this._viewFromSubscription = undefined;
  89. this._wall = undefined;
  90. this._wallSubscription = undefined;
  91. };
  92. defineProperties(Entity.prototype, {
  93. /**
  94. * The availability, if any, associated with this object.
  95. * If availability is undefined, it is assumed that this object's
  96. * other properties will return valid data for any provided time.
  97. * If availability exists, the objects other properties will only
  98. * provide valid data if queried within the given interval.
  99. * @memberof Entity.prototype
  100. * @type {TimeIntervalCollection}
  101. */
  102. availability : createPropertyDescriptor('availability'),
  103. /**
  104. * Gets the unique ID associated with this object.
  105. * @memberof Entity.prototype
  106. * @type {String}
  107. */
  108. id : {
  109. get : function() {
  110. return this._id;
  111. }
  112. },
  113. /**
  114. * Gets the event that is raised whenever a new property is assigned.
  115. * @memberof Entity.prototype
  116. *
  117. * @type {Event}
  118. * @readonly
  119. */
  120. definitionChanged : {
  121. get : function() {
  122. return this._definitionChanged;
  123. }
  124. },
  125. /**
  126. * Gets or sets the name of the object. The name is intended for end-user
  127. * consumption and does not need to be unique.
  128. * @memberof Entity.prototype
  129. * @type {String}
  130. */
  131. name : {
  132. configurable : false,
  133. get : function() {
  134. return this._name;
  135. },
  136. set : function(value) {
  137. var oldValue = this._name;
  138. if (oldValue !== value) {
  139. this._name = value;
  140. this._definitionChanged.raiseEvent(this, 'name', value, oldValue);
  141. }
  142. }
  143. },
  144. /**
  145. * Gets or sets the parent object.
  146. * @memberof Entity.prototype
  147. * @type {Entity}
  148. */
  149. parent : createPropertyDescriptor('parent'),
  150. /**
  151. * Gets the names of all properties registed on this instance.
  152. * @memberof Entity.prototype
  153. * @type {Event}
  154. */
  155. propertyNames : {
  156. get : function() {
  157. return this._propertyNames;
  158. }
  159. },
  160. /**
  161. * Gets or sets the billboard.
  162. * @memberof Entity.prototype
  163. * @type {BillboardGraphics}
  164. */
  165. billboard : createPropertyDescriptor('billboard'),
  166. /**
  167. * Gets or sets the box.
  168. * @memberof Entity.prototype
  169. * @type {BoxGraphics}
  170. */
  171. box : createPropertyDescriptor('box'),
  172. /**
  173. * Gets or sets the corridor.
  174. * @memberof Entity.prototype
  175. * @type {CorridorGraphics}
  176. */
  177. corridor : createPropertyDescriptor('corridor'),
  178. /**
  179. * Gets or sets the cylinder.
  180. * @memberof Entity.prototype
  181. * @type {CylinderGraphics}
  182. */
  183. cylinder : createPropertyDescriptor('cylinder'),
  184. /**
  185. * Gets or sets the description.
  186. * @memberof Entity.prototype
  187. * @type {Property}
  188. */
  189. description : createPropertyDescriptor('description'),
  190. /**
  191. * Gets or sets the ellipse.
  192. * @memberof Entity.prototype
  193. * @type {EllipseGraphics}
  194. */
  195. ellipse : createPropertyDescriptor('ellipse'),
  196. /**
  197. * Gets or sets the ellipsoid.
  198. * @memberof Entity.prototype
  199. * @type {EllipsoidGraphics}
  200. */
  201. ellipsoid : createPropertyDescriptor('ellipsoid'),
  202. /**
  203. * Gets or sets the label.
  204. * @memberof Entity.prototype
  205. * @type {LabelGraphics}
  206. */
  207. label : createPropertyDescriptor('label'),
  208. /**
  209. * Gets or sets the model.
  210. * @memberof Entity.prototype
  211. * @type {LabelGraphics}
  212. */
  213. model : createPropertyDescriptor('model'),
  214. /**
  215. * Gets or sets the orientation.
  216. * @memberof Entity.prototype
  217. * @type {Property}
  218. */
  219. orientation : createPropertyDescriptor('orientation'),
  220. /**
  221. * Gets or sets the path.
  222. * @memberof Entity.prototype
  223. * @type {PathGraphics}
  224. */
  225. path : createPropertyDescriptor('path'),
  226. /**
  227. * Gets or sets the point graphic.
  228. * @memberof Entity.prototype
  229. * @type {PointGraphics}
  230. */
  231. point : createPropertyDescriptor('point'),
  232. /**
  233. * Gets or sets the polygon.
  234. * @memberof Entity.prototype
  235. * @type {PolygonGraphics}
  236. */
  237. polygon : createPropertyDescriptor('polygon'),
  238. /**
  239. * Gets or sets the polyline.
  240. * @memberof Entity.prototype
  241. * @type {PolylineGraphics}
  242. */
  243. polyline : createPropertyDescriptor('polyline'),
  244. /**
  245. * Gets or sets the position.
  246. * @memberof Entity.prototype
  247. * @type {PositionProperty}
  248. */
  249. position : createPropertyDescriptor('position'),
  250. /**
  251. * Gets or sets the rectangle.
  252. * @memberof Entity.prototype
  253. * @type {RectangleGraphics}
  254. */
  255. rectangle : createPropertyDescriptor('rectangle'),
  256. /**
  257. * Gets or sets the suggested initial offset for viewing this object
  258. * with the camera. The offset is defined in the east-north-up reference frame.
  259. * @memberof Entity.prototype
  260. * @type {Cartesian3}
  261. */
  262. viewFrom : createPropertyDescriptor('viewFrom'),
  263. /**
  264. * Gets or sets the wall.
  265. * @memberof Entity.prototype
  266. * @type {WallGraphics}
  267. */
  268. wall : createPropertyDescriptor('wall')
  269. });
  270. /**
  271. * Given a time, returns true if this object should have data during that time.
  272. *
  273. * @param {JulianDate} time The time to check availability for.
  274. * @returns true if the object should have data during the provided time, false otherwise.
  275. */
  276. Entity.prototype.isAvailable = function(time) {
  277. //>>includeStart('debug', pragmas.debug);
  278. if (!defined(time)) {
  279. throw new DeveloperError('time is required.');
  280. }
  281. //>>includeEnd('debug');
  282. var availability = this._availability;
  283. return !defined(availability) || availability.contains(time);
  284. };
  285. /**
  286. * Adds a property to this object. Once a property is added, it can be
  287. * observed with {@link Entity#definitionChanged} and composited
  288. * with {@link CompositeEntityCollection}
  289. *
  290. * @param {String} propertyName The name of the property to add.
  291. *
  292. * @exception {DeveloperError} "propertyName" is a reserved property name.
  293. * @exception {DeveloperError} "propertyName" is already a registered property.
  294. */
  295. Entity.prototype.addProperty = function(propertyName) {
  296. var propertyNames = this._propertyNames;
  297. //>>includeStart('debug', pragmas.debug);
  298. if (!defined(propertyName)) {
  299. throw new DeveloperError('propertyName is required.');
  300. }
  301. if (propertyNames.indexOf(propertyName) !== -1) {
  302. throw new DeveloperError(propertyName + ' is already a registered property.');
  303. }
  304. if (propertyName in this) {
  305. throw new DeveloperError(propertyName + ' is a reserved property name.');
  306. }
  307. //>>includeEnd('debug');
  308. propertyNames.push(propertyName);
  309. Object.defineProperty(this, propertyName, createPropertyDescriptor(propertyName, true));
  310. };
  311. /**
  312. * Removed a property previously added with addProperty.
  313. *
  314. * @param {String} propertyName The name of the property to remove.
  315. *
  316. * @exception {DeveloperError} "propertyName" is a reserved property name.
  317. * @exception {DeveloperError} "propertyName" is not a registered property.
  318. */
  319. Entity.prototype.removeProperty = function(propertyName) {
  320. var propertyNames = this._propertyNames;
  321. //>>includeStart('debug', pragmas.debug);
  322. if (!defined(propertyName)) {
  323. throw new DeveloperError('propertyName is required.');
  324. }
  325. if (propertyNames.indexOf(propertyName) === -1) {
  326. throw new DeveloperError(propertyName + ' is not a registered property.');
  327. }
  328. //>>includeEnd('debug');
  329. this._propertyNames.push(propertyName);
  330. delete this[propertyName];
  331. };
  332. /**
  333. * Assigns each unassigned property on this object to the value
  334. * of the same property on the provided source object.
  335. *
  336. * @param {Entity} source The object to be merged into this object.
  337. */
  338. Entity.prototype.merge = function(source) {
  339. //>>includeStart('debug', pragmas.debug);
  340. if (!defined(source)) {
  341. throw new DeveloperError('source is required.');
  342. }
  343. //>>includeEnd('debug');
  344. //Name and availability are not Property objects and are currently handled differently.
  345. this.name = defaultValue(this.name, source.name);
  346. this.availability = defaultValue(source.availability, this.availability);
  347. var propertyNames = this._propertyNames;
  348. var sourcePropertyNames = source._propertyNames;
  349. var propertyNamesLength = sourcePropertyNames.length;
  350. for (var i = 0; i < propertyNamesLength; i++) {
  351. var name = sourcePropertyNames[i];
  352. var targetProperty = this[name];
  353. var sourceProperty = source[name];
  354. //Custom properties that are registered on the source entity must also
  355. //get registered on this entity.
  356. if (!defined(targetProperty) && propertyNames.indexOf(name) === -1) {
  357. this.addProperty(name);
  358. }
  359. if (defined(sourceProperty)) {
  360. if (defined(targetProperty)) {
  361. if (defined(targetProperty.merge)) {
  362. targetProperty.merge(sourceProperty);
  363. }
  364. } else if (defined(sourceProperty.merge) && defined(sourceProperty.clone)) {
  365. this[name] = sourceProperty.clone();
  366. } else {
  367. this[name] = sourceProperty;
  368. }
  369. }
  370. }
  371. };
  372. var matrix3Scratch = new Matrix3();
  373. var positionScratch = new Cartesian3();
  374. var orientationScratch = new Quaternion();
  375. /**
  376. * @private
  377. */
  378. Entity.prototype._getModelMatrix = function(time, result) {
  379. var position = Property.getValueOrUndefined(this._position, time, positionScratch);
  380. if (!defined(position)) {
  381. return undefined;
  382. }
  383. var orientation = Property.getValueOrUndefined(this._orientation, time, orientationScratch);
  384. if (!defined(orientation)) {
  385. result = Transforms.eastNorthUpToFixedFrame(position, undefined, result);
  386. } else {
  387. result = Matrix4.fromRotationTranslation(Matrix3.fromQuaternion(orientation, matrix3Scratch), position, result);
  388. }
  389. return result;
  390. };
  391. return Entity;
  392. });