Source: Core/CorridorGeometry.js

  1. /*global define*/
  2. define([
  3. './arrayRemoveDuplicates',
  4. './BoundingSphere',
  5. './Cartesian3',
  6. './Cartographic',
  7. './ComponentDatatype',
  8. './CornerType',
  9. './CorridorGeometryLibrary',
  10. './defaultValue',
  11. './defined',
  12. './defineProperties',
  13. './DeveloperError',
  14. './Ellipsoid',
  15. './Geometry',
  16. './GeometryAttribute',
  17. './GeometryAttributes',
  18. './IndexDatatype',
  19. './Math',
  20. './PolygonPipeline',
  21. './PrimitiveType',
  22. './Rectangle',
  23. './VertexFormat'
  24. ], function(
  25. arrayRemoveDuplicates,
  26. BoundingSphere,
  27. Cartesian3,
  28. Cartographic,
  29. ComponentDatatype,
  30. CornerType,
  31. CorridorGeometryLibrary,
  32. defaultValue,
  33. defined,
  34. defineProperties,
  35. DeveloperError,
  36. Ellipsoid,
  37. Geometry,
  38. GeometryAttribute,
  39. GeometryAttributes,
  40. IndexDatatype,
  41. CesiumMath,
  42. PolygonPipeline,
  43. PrimitiveType,
  44. Rectangle,
  45. VertexFormat) {
  46. 'use strict';
  47. var cartesian1 = new Cartesian3();
  48. var cartesian2 = new Cartesian3();
  49. var cartesian3 = new Cartesian3();
  50. var cartesian4 = new Cartesian3();
  51. var cartesian5 = new Cartesian3();
  52. var cartesian6 = new Cartesian3();
  53. var scratch1 = new Cartesian3();
  54. var scratch2 = new Cartesian3();
  55. function addNormals(attr, normal, left, front, back, vertexFormat) {
  56. var normals = attr.normals;
  57. var tangents = attr.tangents;
  58. var binormals = attr.binormals;
  59. var forward = Cartesian3.normalize(Cartesian3.cross(left, normal, scratch1), scratch1);
  60. if (vertexFormat.normal) {
  61. CorridorGeometryLibrary.addAttribute(normals, normal, front, back);
  62. }
  63. if (vertexFormat.binormal) {
  64. CorridorGeometryLibrary.addAttribute(binormals, left, front, back);
  65. }
  66. if (vertexFormat.tangent) {
  67. CorridorGeometryLibrary.addAttribute(tangents, forward, front, back);
  68. }
  69. }
  70. function combine(computedPositions, vertexFormat, ellipsoid) {
  71. var positions = computedPositions.positions;
  72. var corners = computedPositions.corners;
  73. var endPositions = computedPositions.endPositions;
  74. var computedLefts = computedPositions.lefts;
  75. var computedNormals = computedPositions.normals;
  76. var attributes = new GeometryAttributes();
  77. var corner;
  78. var leftCount = 0;
  79. var rightCount = 0;
  80. var i;
  81. var indicesLength = 0;
  82. var length;
  83. for (i = 0; i < positions.length; i += 2) {
  84. length = positions[i].length - 3;
  85. leftCount += length; //subtracting 3 to account for duplicate points at corners
  86. indicesLength += length*2;
  87. rightCount += positions[i + 1].length - 3;
  88. }
  89. leftCount += 3; //add back count for end positions
  90. rightCount += 3;
  91. for (i = 0; i < corners.length; i++) {
  92. corner = corners[i];
  93. var leftSide = corners[i].leftPositions;
  94. if (defined(leftSide)) {
  95. length = leftSide.length;
  96. leftCount += length;
  97. indicesLength += length;
  98. } else {
  99. length = corners[i].rightPositions.length;
  100. rightCount += length;
  101. indicesLength += length;
  102. }
  103. }
  104. var addEndPositions = defined(endPositions);
  105. var endPositionLength;
  106. if (addEndPositions) {
  107. endPositionLength = endPositions[0].length - 3;
  108. leftCount += endPositionLength;
  109. rightCount += endPositionLength;
  110. endPositionLength /= 3;
  111. indicesLength += endPositionLength * 6;
  112. }
  113. var size = leftCount + rightCount;
  114. var finalPositions = new Float64Array(size);
  115. var normals = (vertexFormat.normal) ? new Float32Array(size) : undefined;
  116. var tangents = (vertexFormat.tangent) ? new Float32Array(size) : undefined;
  117. var binormals = (vertexFormat.binormal) ? new Float32Array(size) : undefined;
  118. var attr = {
  119. normals : normals,
  120. tangents : tangents,
  121. binormals : binormals
  122. };
  123. var front = 0;
  124. var back = size - 1;
  125. var UL, LL, UR, LR;
  126. var normal = cartesian1;
  127. var left = cartesian2;
  128. var rightPos, leftPos;
  129. var halfLength = endPositionLength / 2;
  130. var indices = IndexDatatype.createTypedArray(size / 3, indicesLength);
  131. var index = 0;
  132. if (addEndPositions) { // add rounded end
  133. leftPos = cartesian3;
  134. rightPos = cartesian4;
  135. var firstEndPositions = endPositions[0];
  136. normal = Cartesian3.fromArray(computedNormals, 0, normal);
  137. left = Cartesian3.fromArray(computedLefts, 0, left);
  138. for (i = 0; i < halfLength; i++) {
  139. leftPos = Cartesian3.fromArray(firstEndPositions, (halfLength - 1 - i) * 3, leftPos);
  140. rightPos = Cartesian3.fromArray(firstEndPositions, (halfLength + i) * 3, rightPos);
  141. CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
  142. CorridorGeometryLibrary.addAttribute(finalPositions, leftPos, undefined, back);
  143. addNormals(attr, normal, left, front, back, vertexFormat);
  144. LL = front / 3;
  145. LR = LL + 1;
  146. UL = (back - 2) / 3;
  147. UR = UL - 1;
  148. indices[index++] = UL;
  149. indices[index++] = LL;
  150. indices[index++] = UR;
  151. indices[index++] = UR;
  152. indices[index++] = LL;
  153. indices[index++] = LR;
  154. front += 3;
  155. back -= 3;
  156. }
  157. }
  158. var posIndex = 0;
  159. var compIndex = 0;
  160. var rightEdge = positions[posIndex++]; //add first two edges
  161. var leftEdge = positions[posIndex++];
  162. finalPositions.set(rightEdge, front);
  163. finalPositions.set(leftEdge, back - leftEdge.length + 1);
  164. left = Cartesian3.fromArray(computedLefts, compIndex, left);
  165. var rightNormal;
  166. var leftNormal;
  167. length = leftEdge.length - 3;
  168. for (i = 0; i < length; i += 3) {
  169. rightNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(rightEdge, i, scratch1), scratch1);
  170. leftNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(leftEdge, length - i, scratch2), scratch2);
  171. normal = Cartesian3.normalize(Cartesian3.add(rightNormal, leftNormal, normal), normal);
  172. addNormals(attr, normal, left, front, back, vertexFormat);
  173. LL = front / 3;
  174. LR = LL + 1;
  175. UL = (back - 2) / 3;
  176. UR = UL - 1;
  177. indices[index++] = UL;
  178. indices[index++] = LL;
  179. indices[index++] = UR;
  180. indices[index++] = UR;
  181. indices[index++] = LL;
  182. indices[index++] = LR;
  183. front += 3;
  184. back -= 3;
  185. }
  186. rightNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(rightEdge, length, scratch1), scratch1);
  187. leftNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(leftEdge, length, scratch2), scratch2);
  188. normal = Cartesian3.normalize(Cartesian3.add(rightNormal, leftNormal, normal), normal);
  189. compIndex += 3;
  190. for (i = 0; i < corners.length; i++) {
  191. var j;
  192. corner = corners[i];
  193. var l = corner.leftPositions;
  194. var r = corner.rightPositions;
  195. var pivot;
  196. var start;
  197. var outsidePoint = cartesian6;
  198. var previousPoint = cartesian3;
  199. var nextPoint = cartesian4;
  200. normal = Cartesian3.fromArray(computedNormals, compIndex, normal);
  201. if (defined(l)) {
  202. addNormals(attr, normal, left, undefined, back, vertexFormat);
  203. back -= 3;
  204. pivot = LR;
  205. start = UR;
  206. for (j = 0; j < l.length / 3; j++) {
  207. outsidePoint = Cartesian3.fromArray(l, j * 3, outsidePoint);
  208. indices[index++] = pivot;
  209. indices[index++] = start - j - 1;
  210. indices[index++] = start - j;
  211. CorridorGeometryLibrary.addAttribute(finalPositions, outsidePoint, undefined, back);
  212. previousPoint = Cartesian3.fromArray(finalPositions, (start - j - 1) * 3, previousPoint);
  213. nextPoint = Cartesian3.fromArray(finalPositions, pivot * 3, nextPoint);
  214. left = Cartesian3.normalize(Cartesian3.subtract(previousPoint, nextPoint, left), left);
  215. addNormals(attr, normal, left, undefined, back, vertexFormat);
  216. back -= 3;
  217. }
  218. outsidePoint = Cartesian3.fromArray(finalPositions, pivot * 3, outsidePoint);
  219. previousPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, (start) * 3, previousPoint), outsidePoint, previousPoint);
  220. nextPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, (start - j) * 3, nextPoint), outsidePoint, nextPoint);
  221. left = Cartesian3.normalize(Cartesian3.add(previousPoint, nextPoint, left), left);
  222. addNormals(attr, normal, left, front, undefined, vertexFormat);
  223. front += 3;
  224. } else {
  225. addNormals(attr, normal, left, front, undefined, vertexFormat);
  226. front += 3;
  227. pivot = UR;
  228. start = LR;
  229. for (j = 0; j < r.length / 3; j++) {
  230. outsidePoint = Cartesian3.fromArray(r, j * 3, outsidePoint);
  231. indices[index++] = pivot;
  232. indices[index++] = start + j;
  233. indices[index++] = start + j + 1;
  234. CorridorGeometryLibrary.addAttribute(finalPositions, outsidePoint, front);
  235. previousPoint = Cartesian3.fromArray(finalPositions, pivot * 3, previousPoint);
  236. nextPoint = Cartesian3.fromArray(finalPositions, (start + j) * 3, nextPoint);
  237. left = Cartesian3.normalize(Cartesian3.subtract(previousPoint, nextPoint, left), left);
  238. addNormals(attr, normal, left, front, undefined, vertexFormat);
  239. front += 3;
  240. }
  241. outsidePoint = Cartesian3.fromArray(finalPositions, pivot * 3, outsidePoint);
  242. previousPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, (start + j) * 3, previousPoint), outsidePoint, previousPoint);
  243. nextPoint = Cartesian3.subtract(Cartesian3.fromArray(finalPositions, start * 3, nextPoint), outsidePoint, nextPoint);
  244. left = Cartesian3.normalize(Cartesian3.negate(Cartesian3.add(nextPoint, previousPoint, left), left), left);
  245. addNormals(attr, normal, left, undefined, back, vertexFormat);
  246. back -= 3;
  247. }
  248. rightEdge = positions[posIndex++];
  249. leftEdge = positions[posIndex++];
  250. rightEdge.splice(0, 3); //remove duplicate points added by corner
  251. leftEdge.splice(leftEdge.length - 3, 3);
  252. finalPositions.set(rightEdge, front);
  253. finalPositions.set(leftEdge, back - leftEdge.length + 1);
  254. length = leftEdge.length - 3;
  255. compIndex += 3;
  256. left = Cartesian3.fromArray(computedLefts, compIndex, left);
  257. for (j = 0; j < leftEdge.length; j += 3) {
  258. rightNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(rightEdge, j, scratch1), scratch1);
  259. leftNormal = ellipsoid.geodeticSurfaceNormal(Cartesian3.fromArray(leftEdge, length - j, scratch2), scratch2);
  260. normal = Cartesian3.normalize(Cartesian3.add(rightNormal, leftNormal, normal), normal);
  261. addNormals(attr, normal, left, front, back, vertexFormat);
  262. LR = front / 3;
  263. LL = LR - 1;
  264. UR = (back - 2) / 3;
  265. UL = UR + 1;
  266. indices[index++] = UL;
  267. indices[index++] = LL;
  268. indices[index++] = UR;
  269. indices[index++] = UR;
  270. indices[index++] = LL;
  271. indices[index++] = LR;
  272. front += 3;
  273. back -= 3;
  274. }
  275. front -= 3;
  276. back += 3;
  277. }
  278. normal = Cartesian3.fromArray(computedNormals, computedNormals.length - 3, normal);
  279. addNormals(attr, normal, left, front, back, vertexFormat);
  280. if (addEndPositions) { // add rounded end
  281. front += 3;
  282. back -= 3;
  283. leftPos = cartesian3;
  284. rightPos = cartesian4;
  285. var lastEndPositions = endPositions[1];
  286. for (i = 0; i < halfLength; i++) {
  287. leftPos = Cartesian3.fromArray(lastEndPositions, (endPositionLength - i - 1) * 3, leftPos);
  288. rightPos = Cartesian3.fromArray(lastEndPositions, i * 3, rightPos);
  289. CorridorGeometryLibrary.addAttribute(finalPositions, leftPos, undefined, back);
  290. CorridorGeometryLibrary.addAttribute(finalPositions, rightPos, front);
  291. addNormals(attr, normal, left, front, back, vertexFormat);
  292. LR = front / 3;
  293. LL = LR - 1;
  294. UR = (back - 2) / 3;
  295. UL = UR + 1;
  296. indices[index++] = UL;
  297. indices[index++] = LL;
  298. indices[index++] = UR;
  299. indices[index++] = UR;
  300. indices[index++] = LL;
  301. indices[index++] = LR;
  302. front += 3;
  303. back -= 3;
  304. }
  305. }
  306. attributes.position = new GeometryAttribute({
  307. componentDatatype : ComponentDatatype.DOUBLE,
  308. componentsPerAttribute : 3,
  309. values : finalPositions
  310. });
  311. if (vertexFormat.st) {
  312. var st = new Float32Array(size / 3 * 2);
  313. var rightSt;
  314. var leftSt;
  315. var stIndex = 0;
  316. if (addEndPositions) {
  317. leftCount /= 3;
  318. rightCount /= 3;
  319. var theta = Math.PI / (endPositionLength + 1);
  320. leftSt = 1 / (leftCount - endPositionLength + 1);
  321. rightSt = 1 / (rightCount - endPositionLength + 1);
  322. var a;
  323. var halfEndPos = endPositionLength / 2;
  324. for (i = halfEndPos + 1; i < endPositionLength + 1; i++) { // lower left rounded end
  325. a = CesiumMath.PI_OVER_TWO + theta * i;
  326. st[stIndex++] = rightSt * (1 + Math.cos(a));
  327. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  328. }
  329. for (i = 1; i < rightCount - endPositionLength + 1; i++) { // bottom edge
  330. st[stIndex++] = i * rightSt;
  331. st[stIndex++] = 0;
  332. }
  333. for (i = endPositionLength; i > halfEndPos; i--) { // lower right rounded end
  334. a = CesiumMath.PI_OVER_TWO - i * theta;
  335. st[stIndex++] = 1 - rightSt * (1 + Math.cos(a));
  336. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  337. }
  338. for (i = halfEndPos; i > 0; i--) { // upper right rounded end
  339. a = CesiumMath.PI_OVER_TWO - theta * i;
  340. st[stIndex++] = 1 - leftSt * (1 + Math.cos(a));
  341. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  342. }
  343. for (i = leftCount - endPositionLength; i > 0; i--) { // top edge
  344. st[stIndex++] = i * leftSt;
  345. st[stIndex++] = 1;
  346. }
  347. for (i = 1; i < halfEndPos + 1; i++) { // upper left rounded end
  348. a = CesiumMath.PI_OVER_TWO + theta * i;
  349. st[stIndex++] = leftSt * (1 + Math.cos(a));
  350. st[stIndex++] = 0.5 * (1 + Math.sin(a));
  351. }
  352. } else {
  353. leftCount /= 3;
  354. rightCount /= 3;
  355. leftSt = 1 / (leftCount - 1);
  356. rightSt = 1 / (rightCount - 1);
  357. for (i = 0; i < rightCount; i++) { // bottom edge
  358. st[stIndex++] = i * rightSt;
  359. st[stIndex++] = 0;
  360. }
  361. for (i = leftCount; i > 0; i--) { // top edge
  362. st[stIndex++] = (i - 1) * leftSt;
  363. st[stIndex++] = 1;
  364. }
  365. }
  366. attributes.st = new GeometryAttribute({
  367. componentDatatype : ComponentDatatype.FLOAT,
  368. componentsPerAttribute : 2,
  369. values : st
  370. });
  371. }
  372. if (vertexFormat.normal) {
  373. attributes.normal = new GeometryAttribute({
  374. componentDatatype : ComponentDatatype.FLOAT,
  375. componentsPerAttribute : 3,
  376. values : attr.normals
  377. });
  378. }
  379. if (vertexFormat.tangent) {
  380. attributes.tangent = new GeometryAttribute({
  381. componentDatatype : ComponentDatatype.FLOAT,
  382. componentsPerAttribute : 3,
  383. values : attr.tangents
  384. });
  385. }
  386. if (vertexFormat.binormal) {
  387. attributes.binormal = new GeometryAttribute({
  388. componentDatatype : ComponentDatatype.FLOAT,
  389. componentsPerAttribute : 3,
  390. values : attr.binormals
  391. });
  392. }
  393. return {
  394. attributes : attributes,
  395. indices : indices
  396. };
  397. }
  398. function extrudedAttributes(attributes, vertexFormat) {
  399. if (!vertexFormat.normal && !vertexFormat.binormal && !vertexFormat.tangent && !vertexFormat.st) {
  400. return attributes;
  401. }
  402. var positions = attributes.position.values;
  403. var topNormals;
  404. var topBinormals;
  405. if (vertexFormat.normal || vertexFormat.binormal) {
  406. topNormals = attributes.normal.values;
  407. topBinormals = attributes.binormal.values;
  408. }
  409. var size = attributes.position.values.length / 18;
  410. var threeSize = size * 3;
  411. var twoSize = size * 2;
  412. var sixSize = threeSize * 2;
  413. var i;
  414. if (vertexFormat.normal || vertexFormat.binormal || vertexFormat.tangent) {
  415. var normals = (vertexFormat.normal) ? new Float32Array(threeSize * 6) : undefined;
  416. var binormals = (vertexFormat.binormal) ? new Float32Array(threeSize * 6) : undefined;
  417. var tangents = (vertexFormat.tangent) ? new Float32Array(threeSize * 6) : undefined;
  418. var topPosition = cartesian1;
  419. var bottomPosition = cartesian2;
  420. var previousPosition = cartesian3;
  421. var normal = cartesian4;
  422. var tangent = cartesian5;
  423. var binormal = cartesian6;
  424. var attrIndex = sixSize;
  425. for (i = 0; i < threeSize; i += 3) {
  426. var attrIndexOffset = attrIndex + sixSize;
  427. topPosition = Cartesian3.fromArray(positions, i, topPosition);
  428. bottomPosition = Cartesian3.fromArray(positions, i + threeSize, bottomPosition);
  429. previousPosition = Cartesian3.fromArray(positions, (i + 3) % threeSize, previousPosition);
  430. bottomPosition = Cartesian3.subtract(bottomPosition, topPosition, bottomPosition);
  431. previousPosition = Cartesian3.subtract(previousPosition, topPosition, previousPosition);
  432. normal = Cartesian3.normalize(Cartesian3.cross(bottomPosition, previousPosition, normal), normal);
  433. if (vertexFormat.normal) {
  434. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndexOffset);
  435. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndexOffset + 3);
  436. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndex);
  437. CorridorGeometryLibrary.addAttribute(normals, normal, attrIndex + 3);
  438. }
  439. if (vertexFormat.tangent || vertexFormat.binormal) {
  440. binormal = Cartesian3.fromArray(topNormals, i, binormal);
  441. if (vertexFormat.binormal) {
  442. CorridorGeometryLibrary.addAttribute(binormals, binormal, attrIndexOffset);
  443. CorridorGeometryLibrary.addAttribute(binormals, binormal, attrIndexOffset + 3);
  444. CorridorGeometryLibrary.addAttribute(binormals, binormal, attrIndex);
  445. CorridorGeometryLibrary.addAttribute(binormals, binormal, attrIndex + 3);
  446. }
  447. if (vertexFormat.tangent) {
  448. tangent = Cartesian3.normalize(Cartesian3.cross(binormal, normal, tangent), tangent);
  449. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndexOffset);
  450. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndexOffset + 3);
  451. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndex);
  452. CorridorGeometryLibrary.addAttribute(tangents, tangent, attrIndex + 3);
  453. }
  454. }
  455. attrIndex += 6;
  456. }
  457. if (vertexFormat.normal) {
  458. normals.set(topNormals); //top
  459. for (i = 0; i < threeSize; i += 3) { //bottom normals
  460. normals[i + threeSize] = -topNormals[i];
  461. normals[i + threeSize + 1] = -topNormals[i + 1];
  462. normals[i + threeSize + 2] = -topNormals[i + 2];
  463. }
  464. attributes.normal.values = normals;
  465. } else {
  466. attributes.normal = undefined;
  467. }
  468. if (vertexFormat.binormal) {
  469. binormals.set(topBinormals); //top
  470. binormals.set(topBinormals, threeSize); //bottom
  471. attributes.binormal.values = binormals;
  472. } else {
  473. attributes.binormal = undefined;
  474. }
  475. if (vertexFormat.tangent) {
  476. var topTangents = attributes.tangent.values;
  477. tangents.set(topTangents); //top
  478. tangents.set(topTangents, threeSize); //bottom
  479. attributes.tangent.values = tangents;
  480. }
  481. }
  482. if (vertexFormat.st) {
  483. var topSt = attributes.st.values;
  484. var st = new Float32Array(twoSize * 6);
  485. st.set(topSt); //top
  486. st.set(topSt, twoSize); //bottom
  487. var index = twoSize * 2;
  488. for ( var j = 0; j < 2; j++) {
  489. st[index++] = topSt[0];
  490. st[index++] = topSt[1];
  491. for (i = 2; i < twoSize; i += 2) {
  492. var s = topSt[i];
  493. var t = topSt[i + 1];
  494. st[index++] = s;
  495. st[index++] = t;
  496. st[index++] = s;
  497. st[index++] = t;
  498. }
  499. st[index++] = topSt[0];
  500. st[index++] = topSt[1];
  501. }
  502. attributes.st.values = st;
  503. }
  504. return attributes;
  505. }
  506. function addWallPositions(positions, index, wallPositions) {
  507. wallPositions[index++] = positions[0];
  508. wallPositions[index++] = positions[1];
  509. wallPositions[index++] = positions[2];
  510. for ( var i = 3; i < positions.length; i += 3) {
  511. var x = positions[i];
  512. var y = positions[i + 1];
  513. var z = positions[i + 2];
  514. wallPositions[index++] = x;
  515. wallPositions[index++] = y;
  516. wallPositions[index++] = z;
  517. wallPositions[index++] = x;
  518. wallPositions[index++] = y;
  519. wallPositions[index++] = z;
  520. }
  521. wallPositions[index++] = positions[0];
  522. wallPositions[index++] = positions[1];
  523. wallPositions[index++] = positions[2];
  524. return wallPositions;
  525. }
  526. function computePositionsExtruded(params, vertexFormat) {
  527. var topVertexFormat = new VertexFormat({
  528. position : vertexFormat.positon,
  529. normal : (vertexFormat.normal || vertexFormat.binormal),
  530. tangent : vertexFormat.tangent,
  531. binormal : (vertexFormat.normal || vertexFormat.binormal),
  532. st : vertexFormat.st
  533. });
  534. var ellipsoid = params.ellipsoid;
  535. var computedPositions = CorridorGeometryLibrary.computePositions(params);
  536. var attr = combine(computedPositions, topVertexFormat, ellipsoid);
  537. var height = params.height;
  538. var extrudedHeight = params.extrudedHeight;
  539. var attributes = attr.attributes;
  540. var indices = attr.indices;
  541. var positions = attributes.position.values;
  542. var length = positions.length;
  543. var newPositions = new Float64Array(length * 6);
  544. var extrudedPositions = new Float64Array(length);
  545. extrudedPositions.set(positions);
  546. var wallPositions = new Float64Array(length * 4);
  547. positions = PolygonPipeline.scaleToGeodeticHeight(positions, height, ellipsoid);
  548. wallPositions = addWallPositions(positions, 0, wallPositions);
  549. extrudedPositions = PolygonPipeline.scaleToGeodeticHeight(extrudedPositions, extrudedHeight, ellipsoid);
  550. wallPositions = addWallPositions(extrudedPositions, length * 2, wallPositions);
  551. newPositions.set(positions);
  552. newPositions.set(extrudedPositions, length);
  553. newPositions.set(wallPositions, length * 2);
  554. attributes.position.values = newPositions;
  555. length /= 3;
  556. var i;
  557. var iLength = indices.length;
  558. var twoLength = length + length;
  559. var newIndices = IndexDatatype.createTypedArray(newPositions.length / 3, iLength * 2 + twoLength * 3);
  560. newIndices.set(indices);
  561. var index = iLength;
  562. for (i = 0; i < iLength; i += 3) { // bottom indices
  563. var v0 = indices[i];
  564. var v1 = indices[i + 1];
  565. var v2 = indices[i + 2];
  566. newIndices[index++] = v2 + length;
  567. newIndices[index++] = v1 + length;
  568. newIndices[index++] = v0 + length;
  569. }
  570. attributes = extrudedAttributes(attributes, vertexFormat);
  571. var UL, LL, UR, LR;
  572. for (i = 0; i < twoLength; i += 2) { //wall indices
  573. UL = i + twoLength;
  574. LL = UL + twoLength;
  575. UR = UL + 1;
  576. LR = LL + 1;
  577. newIndices[index++] = UL;
  578. newIndices[index++] = LL;
  579. newIndices[index++] = UR;
  580. newIndices[index++] = UR;
  581. newIndices[index++] = LL;
  582. newIndices[index++] = LR;
  583. }
  584. return {
  585. attributes : attributes,
  586. indices : newIndices
  587. };
  588. }
  589. var scratchCartesian1 = new Cartesian3();
  590. var scratchCartesian2 = new Cartesian3();
  591. var scratchCartographic = new Cartographic();
  592. function computeOffsetPoints(position1, position2, ellipsoid, halfWidth, min, max) {
  593. // Compute direction of offset the point
  594. var direction = Cartesian3.subtract(position2, position1, scratchCartesian1);
  595. Cartesian3.normalize(direction, direction);
  596. var normal = ellipsoid.geodeticSurfaceNormal(position1, scratchCartesian2);
  597. var offsetDirection = Cartesian3.cross(direction, normal, scratchCartesian1);
  598. Cartesian3.multiplyByScalar(offsetDirection, halfWidth, offsetDirection);
  599. var minLat = min.latitude;
  600. var minLon = min.longitude;
  601. var maxLat = max.latitude;
  602. var maxLon = max.longitude;
  603. // Compute 2 offset points
  604. Cartesian3.add(position1, offsetDirection, scratchCartesian2);
  605. ellipsoid.cartesianToCartographic(scratchCartesian2, scratchCartographic);
  606. var lat = scratchCartographic.latitude;
  607. var lon = scratchCartographic.longitude;
  608. minLat = Math.min(minLat, lat);
  609. minLon = Math.min(minLon, lon);
  610. maxLat = Math.max(maxLat, lat);
  611. maxLon = Math.max(maxLon, lon);
  612. Cartesian3.subtract(position1, offsetDirection, scratchCartesian2);
  613. ellipsoid.cartesianToCartographic(scratchCartesian2, scratchCartographic);
  614. lat = scratchCartographic.latitude;
  615. lon = scratchCartographic.longitude;
  616. minLat = Math.min(minLat, lat);
  617. minLon = Math.min(minLon, lon);
  618. maxLat = Math.max(maxLat, lat);
  619. maxLon = Math.max(maxLon, lon);
  620. min.latitude = minLat;
  621. min.longitude = minLon;
  622. max.latitude = maxLat;
  623. max.longitude = maxLon;
  624. }
  625. var scratchCartesianOffset = new Cartesian3();
  626. var scratchCartesianEnds = new Cartesian3();
  627. var scratchCartographicMin = new Cartographic();
  628. var scratchCartographicMax = new Cartographic();
  629. function computeRectangle(positions, ellipsoid, width, cornerType) {
  630. var cleanPositions = arrayRemoveDuplicates(positions, Cartesian3.equalsEpsilon);
  631. var length = cleanPositions.length - 1;
  632. if (length === 0 || width === 0) {
  633. return new Rectangle();
  634. }
  635. var halfWidth = width * 0.5;
  636. scratchCartographicMin.latitude = Number.POSITIVE_INFINITY;
  637. scratchCartographicMin.longitude = Number.POSITIVE_INFINITY;
  638. scratchCartographicMax.latitude = Number.NEGATIVE_INFINITY;
  639. scratchCartographicMax.longitude = Number.NEGATIVE_INFINITY;
  640. var lat, lon;
  641. if (cornerType === CornerType.ROUNDED) {
  642. // Compute start cap
  643. var first = cleanPositions[0];
  644. Cartesian3.subtract(first, cleanPositions[1], scratchCartesianOffset);
  645. Cartesian3.normalize(scratchCartesianOffset, scratchCartesianOffset);
  646. Cartesian3.multiplyByScalar(scratchCartesianOffset, halfWidth, scratchCartesianOffset);
  647. Cartesian3.add(first, scratchCartesianOffset, scratchCartesianEnds);
  648. ellipsoid.cartesianToCartographic(scratchCartesianEnds, scratchCartographic);
  649. lat = scratchCartographic.latitude;
  650. lon = scratchCartographic.longitude;
  651. scratchCartographicMin.latitude = Math.min(scratchCartographicMin.latitude, lat);
  652. scratchCartographicMin.longitude = Math.min(scratchCartographicMin.longitude, lon);
  653. scratchCartographicMax.latitude = Math.max(scratchCartographicMax.latitude, lat);
  654. scratchCartographicMax.longitude = Math.max(scratchCartographicMax.longitude, lon);
  655. }
  656. // Compute the rest
  657. for (var i = 0; i < length; ++i) {
  658. computeOffsetPoints(cleanPositions[i], cleanPositions[i+1], ellipsoid, halfWidth,
  659. scratchCartographicMin, scratchCartographicMax);
  660. }
  661. // Compute ending point
  662. var last = cleanPositions[length];
  663. Cartesian3.subtract(last, cleanPositions[length-1], scratchCartesianOffset);
  664. Cartesian3.normalize(scratchCartesianOffset, scratchCartesianOffset);
  665. Cartesian3.multiplyByScalar(scratchCartesianOffset, halfWidth, scratchCartesianOffset);
  666. Cartesian3.add(last, scratchCartesianOffset, scratchCartesianEnds);
  667. computeOffsetPoints(last, scratchCartesianEnds, ellipsoid, halfWidth,
  668. scratchCartographicMin, scratchCartographicMax);
  669. if (cornerType === CornerType.ROUNDED) {
  670. // Compute end cap
  671. ellipsoid.cartesianToCartographic(scratchCartesianEnds, scratchCartographic);
  672. lat = scratchCartographic.latitude;
  673. lon = scratchCartographic.longitude;
  674. scratchCartographicMin.latitude = Math.min(scratchCartographicMin.latitude, lat);
  675. scratchCartographicMin.longitude = Math.min(scratchCartographicMin.longitude, lon);
  676. scratchCartographicMax.latitude = Math.max(scratchCartographicMax.latitude, lat);
  677. scratchCartographicMax.longitude = Math.max(scratchCartographicMax.longitude, lon);
  678. }
  679. var rectangle = new Rectangle();
  680. rectangle.north = scratchCartographicMax.latitude;
  681. rectangle.south = scratchCartographicMin.latitude;
  682. rectangle.east = scratchCartographicMax.longitude;
  683. rectangle.west = scratchCartographicMin.longitude;
  684. return rectangle;
  685. }
  686. /**
  687. * A description of a corridor. Corridor geometry can be rendered with both {@link Primitive} and {@link GroundPrimitive}.
  688. *
  689. * @alias CorridorGeometry
  690. * @constructor
  691. *
  692. * @param {Object} options Object with the following properties:
  693. * @param {Cartesian3[]} options.positions An array of positions that define the center of the corridor.
  694. * @param {Number} options.width The distance between the edges of the corridor in meters.
  695. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  696. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  697. * @param {Number} [options.height=0] The distance in meters between the ellipsoid surface and the positions.
  698. * @param {Number} [options.extrudedHeight] The distance in meters between the ellipsoid surface and the extruded face.
  699. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  700. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  701. *
  702. * @see CorridorGeometry.createGeometry
  703. * @see Packable
  704. *
  705. * @demo {@link http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Corridor.html|Cesium Sandcastle Corridor Demo}
  706. *
  707. * @example
  708. * var corridor = new Cesium.CorridorGeometry({
  709. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY,
  710. * positions : Cesium.Cartesian3.fromDegreesArray([-72.0, 40.0, -70.0, 35.0]),
  711. * width : 100000
  712. * });
  713. */
  714. function CorridorGeometry(options) {
  715. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  716. var positions = options.positions;
  717. var width = options.width;
  718. //>>includeStart('debug', pragmas.debug);
  719. if (!defined(positions)) {
  720. throw new DeveloperError('options.positions is required.');
  721. }
  722. if (!defined(width)) {
  723. throw new DeveloperError('options.width is required.');
  724. }
  725. //>>includeEnd('debug');
  726. this._positions = positions;
  727. this._ellipsoid = Ellipsoid.clone(defaultValue(options.ellipsoid, Ellipsoid.WGS84));
  728. this._vertexFormat = VertexFormat.clone(defaultValue(options.vertexFormat, VertexFormat.DEFAULT));
  729. this._width = width;
  730. this._height = defaultValue(options.height, 0);
  731. this._extrudedHeight = defaultValue(options.extrudedHeight, this._height);
  732. this._cornerType = defaultValue(options.cornerType, CornerType.ROUNDED);
  733. this._granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  734. this._workerName = 'createCorridorGeometry';
  735. this._rectangle = computeRectangle(positions, this._ellipsoid, width, this._cornerType);
  736. /**
  737. * The number of elements used to pack the object into an array.
  738. * @type {Number}
  739. */
  740. this.packedLength = 1 + positions.length * Cartesian3.packedLength + Ellipsoid.packedLength + VertexFormat.packedLength + Rectangle.packedLength + 5;
  741. }
  742. /**
  743. * Stores the provided instance into the provided array.
  744. *
  745. * @param {CorridorGeometry} value The value to pack.
  746. * @param {Number[]} array The array to pack into.
  747. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  748. *
  749. * @returns {Number[]} The array that was packed into
  750. */
  751. CorridorGeometry.pack = function(value, array, startingIndex) {
  752. //>>includeStart('debug', pragmas.debug);
  753. if (!defined(value)) {
  754. throw new DeveloperError('value is required');
  755. }
  756. if (!defined(array)) {
  757. throw new DeveloperError('array is required');
  758. }
  759. //>>includeEnd('debug');
  760. startingIndex = defaultValue(startingIndex, 0);
  761. var positions = value._positions;
  762. var length = positions.length;
  763. array[startingIndex++] = length;
  764. for (var i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  765. Cartesian3.pack(positions[i], array, startingIndex);
  766. }
  767. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  768. startingIndex += Ellipsoid.packedLength;
  769. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  770. startingIndex += VertexFormat.packedLength;
  771. Rectangle.pack(value._rectangle, array, startingIndex);
  772. startingIndex += Rectangle.packedLength;
  773. array[startingIndex++] = value._width;
  774. array[startingIndex++] = value._height;
  775. array[startingIndex++] = value._extrudedHeight;
  776. array[startingIndex++] = value._cornerType;
  777. array[startingIndex] = value._granularity;
  778. return array;
  779. };
  780. var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  781. var scratchVertexFormat = new VertexFormat();
  782. var scratchRectangle = new Rectangle();
  783. var scratchOptions = {
  784. positions : undefined,
  785. ellipsoid : scratchEllipsoid,
  786. vertexFormat : scratchVertexFormat,
  787. width : undefined,
  788. height : undefined,
  789. extrudedHeight : undefined,
  790. cornerType : undefined,
  791. granularity : undefined
  792. };
  793. /**
  794. * Retrieves an instance from a packed array.
  795. *
  796. * @param {Number[]} array The packed array.
  797. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  798. * @param {CorridorGeometry} [result] The object into which to store the result.
  799. * @returns {CorridorGeometry} The modified result parameter or a new CorridorGeometry instance if one was not provided.
  800. */
  801. CorridorGeometry.unpack = function(array, startingIndex, result) {
  802. //>>includeStart('debug', pragmas.debug);
  803. if (!defined(array)) {
  804. throw new DeveloperError('array is required');
  805. }
  806. //>>includeEnd('debug');
  807. startingIndex = defaultValue(startingIndex, 0);
  808. var length = array[startingIndex++];
  809. var positions = new Array(length);
  810. for (var i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  811. positions[i] = Cartesian3.unpack(array, startingIndex);
  812. }
  813. var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  814. startingIndex += Ellipsoid.packedLength;
  815. var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
  816. startingIndex += VertexFormat.packedLength;
  817. var rectangle = Rectangle.unpack(array, startingIndex, scratchRectangle);
  818. startingIndex += Rectangle.packedLength;
  819. var width = array[startingIndex++];
  820. var height = array[startingIndex++];
  821. var extrudedHeight = array[startingIndex++];
  822. var cornerType = array[startingIndex++];
  823. var granularity = array[startingIndex];
  824. if (!defined(result)) {
  825. scratchOptions.positions = positions;
  826. scratchOptions.width = width;
  827. scratchOptions.height = height;
  828. scratchOptions.extrudedHeight = extrudedHeight;
  829. scratchOptions.cornerType = cornerType;
  830. scratchOptions.granularity = granularity;
  831. return new CorridorGeometry(scratchOptions);
  832. }
  833. result._positions = positions;
  834. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  835. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  836. result._width = width;
  837. result._height = height;
  838. result._extrudedHeight = extrudedHeight;
  839. result._cornerType = cornerType;
  840. result._granularity = granularity;
  841. result._rectangle = Rectangle.clone(rectangle);
  842. return result;
  843. };
  844. /**
  845. * Computes the geometric representation of a corridor, including its vertices, indices, and a bounding sphere.
  846. *
  847. * @param {CorridorGeometry} corridorGeometry A description of the corridor.
  848. * @returns {Geometry|undefined} The computed vertices and indices.
  849. */
  850. CorridorGeometry.createGeometry = function(corridorGeometry) {
  851. var positions = corridorGeometry._positions;
  852. var height = corridorGeometry._height;
  853. var width = corridorGeometry._width;
  854. var extrudedHeight = corridorGeometry._extrudedHeight;
  855. var extrude = (height !== extrudedHeight);
  856. var cleanPositions = arrayRemoveDuplicates(positions, Cartesian3.equalsEpsilon);
  857. if ((cleanPositions.length < 2) || (width <= 0)) {
  858. return;
  859. }
  860. var ellipsoid = corridorGeometry._ellipsoid;
  861. var vertexFormat = corridorGeometry._vertexFormat;
  862. var params = {
  863. ellipsoid : ellipsoid,
  864. positions : cleanPositions,
  865. width : width,
  866. cornerType : corridorGeometry._cornerType,
  867. granularity : corridorGeometry._granularity,
  868. saveAttributes: true
  869. };
  870. var attr;
  871. if (extrude) {
  872. var h = Math.max(height, extrudedHeight);
  873. extrudedHeight = Math.min(height, extrudedHeight);
  874. height = h;
  875. params.height = height;
  876. params.extrudedHeight = extrudedHeight;
  877. attr = computePositionsExtruded(params, vertexFormat);
  878. } else {
  879. var computedPositions = CorridorGeometryLibrary.computePositions(params);
  880. attr = combine(computedPositions, vertexFormat, ellipsoid);
  881. attr.attributes.position.values = PolygonPipeline.scaleToGeodeticHeight(attr.attributes.position.values, height, ellipsoid);
  882. }
  883. var attributes = attr.attributes;
  884. var boundingSphere = BoundingSphere.fromVertices(attributes.position.values, undefined, 3);
  885. if (!vertexFormat.position) {
  886. attr.attributes.position.values = undefined;
  887. }
  888. return new Geometry({
  889. attributes : attributes,
  890. indices : attr.indices,
  891. primitiveType : PrimitiveType.TRIANGLES,
  892. boundingSphere : boundingSphere
  893. });
  894. };
  895. /**
  896. * @private
  897. */
  898. CorridorGeometry.createShadowVolume = function(corridorGeometry, minHeightFunc, maxHeightFunc) {
  899. var granularity = corridorGeometry._granularity;
  900. var ellipsoid = corridorGeometry._ellipsoid;
  901. var minHeight = minHeightFunc(granularity, ellipsoid);
  902. var maxHeight = maxHeightFunc(granularity, ellipsoid);
  903. return new CorridorGeometry({
  904. positions : corridorGeometry._positions,
  905. width : corridorGeometry._width,
  906. cornerType : corridorGeometry._cornerType,
  907. ellipsoid : ellipsoid,
  908. granularity : granularity,
  909. extrudedHeight : minHeight,
  910. height : maxHeight,
  911. vertexFormat : VertexFormat.POSITION_ONLY
  912. });
  913. };
  914. defineProperties(CorridorGeometry.prototype, {
  915. /**
  916. * @private
  917. */
  918. rectangle : {
  919. get : function() {
  920. return this._rectangle;
  921. }
  922. }
  923. });
  924. return CorridorGeometry;
  925. });