babylon.meshSimplification.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. module BABYLON {
  2. /**
  3. * A simplifier interface for future simplification implementations.
  4. */
  5. export interface ISimplifier {
  6. /**
  7. * Simplification of a given mesh according to the given settings.
  8. * Since this requires computation, it is assumed that the function runs async.
  9. * @param settings The settings of the simplification, including quality and distance
  10. * @param successCallback A callback that will be called after the mesh was simplified.
  11. * @param errorCallback in case of an error, this callback will be called. optional.
  12. */
  13. simplify(settings: ISimplificationSettings, successCallback: (simplifiedMeshes: Mesh) => void, errorCallback?: () => void): void;
  14. }
  15. /**
  16. * Expected simplification settings.
  17. * Quality should be between 0 and 1 (1 being 100%, 0 being 0%);
  18. */
  19. export interface ISimplificationSettings {
  20. quality: number;
  21. distance: number;
  22. }
  23. export class SimplificationSettings implements ISimplificationSettings {
  24. constructor(public quality: number, public distance: number) {
  25. }
  26. }
  27. export interface ISimplificationTask {
  28. settings: Array<ISimplificationSettings>;
  29. simplificationType: SimplificationType;
  30. mesh: Mesh;
  31. successCallback? : () => void;
  32. submesh?: number;
  33. parallelProcessing: boolean;
  34. }
  35. export class SimplificationQueue {
  36. private _simplificationArray: Array<ISimplificationTask>;
  37. public running;
  38. constructor() {
  39. this.running = false;
  40. this._simplificationArray = [];
  41. }
  42. public executeNext() {
  43. var task = this._simplificationArray.pop();
  44. if (task) {
  45. this.running = true;
  46. this.runSimplification(task);
  47. } else {
  48. this.running = false;
  49. }
  50. }
  51. public runSimplification(task: ISimplificationTask) {
  52. if (task.parallelProcessing) {
  53. //parallel simplifier
  54. task.settings.forEach((setting) => {
  55. var simplifier = this.getSimplifier(task);
  56. simplifier.simplify(setting,(newMesh) => {
  57. task.mesh.addLODLevel(setting.distance, newMesh);
  58. //check if it is the last
  59. if (setting.quality === task.settings[task.settings.length - 1].quality && task.successCallback) {
  60. //all done, run the success callback.
  61. task.successCallback();
  62. }
  63. this.executeNext();
  64. });
  65. });
  66. } else {
  67. //single simplifier.
  68. var simplifier = this.getSimplifier(task);
  69. var runDecimation = (setting: ISimplificationSettings, callback: () => void) => {
  70. simplifier.simplify(setting,(newMesh) => {
  71. task.mesh.addLODLevel(setting.distance, newMesh);
  72. //run the next quality level
  73. callback();
  74. });
  75. }
  76. AsyncLoop.Run(task.settings.length,(loop: AsyncLoop) => {
  77. runDecimation(task.settings[loop.index],() => {
  78. loop.executeNext();
  79. });
  80. },() => {
  81. //execution ended, run the success callback.
  82. if (task.successCallback) {
  83. task.successCallback();
  84. }
  85. this.executeNext();
  86. });
  87. }
  88. }
  89. private getSimplifier(task: ISimplificationTask) : ISimplifier {
  90. switch (task.simplificationType) {
  91. case SimplificationType.QUADRATIC:
  92. default:
  93. return new QuadraticErrorSimplification(task.mesh);
  94. }
  95. }
  96. }
  97. /**
  98. * The implemented types of simplification.
  99. * At the moment only Quadratic Error Decimation is implemented.
  100. */
  101. export enum SimplificationType {
  102. QUADRATIC
  103. }
  104. export class DecimationTriangle {
  105. public normal: Vector3;
  106. public error: Array<number>;
  107. public deleted: boolean;
  108. public isDirty: boolean;
  109. public borderFactor: number;
  110. constructor(public vertices: Array<number>) {
  111. this.error = new Array<number>(4);
  112. this.deleted = false;
  113. this.isDirty = false;
  114. this.borderFactor = 0;
  115. }
  116. }
  117. export class DecimationVertex {
  118. public q: QuadraticMatrix;
  119. public isBorder: boolean;
  120. public triangleStart: number;
  121. public triangleCount: number;
  122. //if color is present instead of uvs.
  123. public color: Color4;
  124. constructor(public position: Vector3, public normal: Vector3, public uv: Vector2, public id) {
  125. this.isBorder = true;
  126. this.q = new QuadraticMatrix();
  127. this.triangleCount = 0;
  128. this.triangleStart = 0;
  129. }
  130. }
  131. export class QuadraticMatrix {
  132. public data: Array<number>;
  133. constructor(data?: Array<number>) {
  134. this.data = new Array(10);
  135. for (var i = 0; i < 10; ++i) {
  136. if (data && data[i]) {
  137. this.data[i] = data[i];
  138. } else {
  139. this.data[i] = 0;
  140. }
  141. }
  142. }
  143. public det(a11, a12, a13, a21, a22, a23, a31, a32, a33) {
  144. var det = this.data[a11] * this.data[a22] * this.data[a33] + this.data[a13] * this.data[a21] * this.data[a32] +
  145. this.data[a12] * this.data[a23] * this.data[a31] - this.data[a13] * this.data[a22] * this.data[a31] -
  146. this.data[a11] * this.data[a23] * this.data[a32] - this.data[a12] * this.data[a21] * this.data[a33];
  147. return det;
  148. }
  149. public addInPlace(matrix: QuadraticMatrix) {
  150. for (var i = 0; i < 10; ++i) {
  151. this.data[i] += matrix.data[i];
  152. }
  153. }
  154. public addArrayInPlace(data: Array<number>) {
  155. for (var i = 0; i < 10; ++i) {
  156. this.data[i] += data[i];
  157. }
  158. }
  159. public add(matrix: QuadraticMatrix): QuadraticMatrix {
  160. var m = new QuadraticMatrix();
  161. for (var i = 0; i < 10; ++i) {
  162. m.data[i] = this.data[i] + matrix.data[i];
  163. }
  164. return m;
  165. }
  166. public static FromData(a: number, b: number, c: number, d: number): QuadraticMatrix {
  167. return new QuadraticMatrix(QuadraticMatrix.DataFromNumbers(a, b, c, d));
  168. }
  169. //returning an array to avoid garbage collection
  170. public static DataFromNumbers(a: number, b: number, c: number, d: number) {
  171. return [a * a, a * b, a * c, a * d, b * b, b * c, b * d, c * c, c * d, d * d];
  172. }
  173. }
  174. export class Reference {
  175. constructor(public vertexId: number, public triangleId: number) { }
  176. }
  177. /**
  178. * An implementation of the Quadratic Error simplification algorithm.
  179. * Original paper : http://www1.cs.columbia.edu/~cs4162/html05s/garland97.pdf
  180. * Ported mostly from QSlim and http://voxels.blogspot.de/2014/05/quadric-mesh-simplification-with-source.html to babylon JS
  181. * @author RaananW
  182. */
  183. export class QuadraticErrorSimplification implements ISimplifier {
  184. private triangles: Array<DecimationTriangle>;
  185. private vertices: Array<DecimationVertex>;
  186. private references: Array<Reference>;
  187. private initialised: boolean = false;
  188. public syncIterations = 5000;
  189. public aggressiveness: number;
  190. public decimationIterations: number;
  191. public boundingBoxEpsilon: number;
  192. constructor(private _mesh: Mesh) {
  193. this.aggressiveness = 7;
  194. this.decimationIterations = 100;
  195. this.boundingBoxEpsilon = Engine.Epsilon;
  196. }
  197. public simplify(settings: ISimplificationSettings, successCallback: (simplifiedMeshes: Mesh) => void) {
  198. this.initWithMesh(this._mesh,() => {
  199. this.runDecimation(settings, successCallback);
  200. });
  201. }
  202. private isTriangleOnBoundingBox(triangle: DecimationTriangle): boolean {
  203. var gCount = 0;
  204. triangle.vertices.forEach((vId) => {
  205. var count = 0;
  206. var vPos = this.vertices[vId].position;
  207. var bbox = this._mesh.getBoundingInfo().boundingBox;
  208. if (bbox.maximum.x - vPos.x < this.boundingBoxEpsilon|| vPos.x - bbox.minimum.x > this.boundingBoxEpsilon)
  209. ++count;
  210. if (bbox.maximum.y == vPos.y || vPos.y == bbox.minimum.y)
  211. ++count;
  212. if (bbox.maximum.z == vPos.z || vPos.z == bbox.minimum.z)
  213. ++count;
  214. if (count > 1) {
  215. ++gCount;
  216. };
  217. });
  218. if (gCount > 1) {
  219. console.log(triangle, gCount);
  220. }
  221. return gCount > 1;
  222. }
  223. private runDecimation(settings: ISimplificationSettings, successCallback: (simplifiedMeshes: Mesh) => void) {
  224. var targetCount = ~~(this.triangles.length * settings.quality);
  225. var deletedTriangles = 0;
  226. var triangleCount = this.triangles.length;
  227. var iterationFunction = (iteration: number, callback) => {
  228. setTimeout(() => {
  229. if (iteration % 5 === 0) {
  230. this.updateMesh(iteration === 0);
  231. }
  232. for (var i = 0; i < this.triangles.length; ++i) {
  233. this.triangles[i].isDirty = false;
  234. }
  235. var threshold = 0.000000001 * Math.pow((iteration + 3), this.aggressiveness);
  236. var trianglesIterator = (i) => {
  237. var tIdx = ~~(((this.triangles.length / 2) + i) % this.triangles.length);
  238. var t = this.triangles[tIdx];
  239. if (!t) return;
  240. if (t.error[3] > threshold || t.deleted || t.isDirty) { return }
  241. for (var j = 0; j < 3; ++j) {
  242. if (t.error[j] < threshold) {
  243. var deleted0: Array<boolean> = [];
  244. var deleted1: Array<boolean> = [];
  245. var i0 = t.vertices[j];
  246. var i1 = t.vertices[(j + 1) % 3];
  247. var v0 = this.vertices[i0];
  248. var v1 = this.vertices[i1];
  249. if (v0.isBorder !== v1.isBorder) continue;
  250. var p = Vector3.Zero();
  251. var n = Vector3.Zero();
  252. var uv = Vector2.Zero();
  253. var color = new Color4(0, 0, 0, 1);
  254. this.calculateError(v0, v1, p, n, uv, color);
  255. var delTr = [];
  256. if (this.isFlipped(v0, i1, p, deleted0, t.borderFactor, delTr)) continue;
  257. if (this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr)) continue;
  258. if (delTr.length == 2 || delTr[0] === delTr[1]) {
  259. continue;
  260. }
  261. v0.normal = n;
  262. if (v0.uv)
  263. v0.uv = uv;
  264. else if (v0.color)
  265. v0.color = color;
  266. v0.q = v1.q.add(v0.q);
  267. if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0) continue;
  268. if (p.equals(v0.position)) continue;
  269. v0.position = p;
  270. var tStart = this.references.length;
  271. deletedTriangles = this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
  272. deletedTriangles = this.updateTriangles(v0.id, v1, deleted1, deletedTriangles);
  273. var tCount = this.references.length - tStart;
  274. if (tCount <= v0.triangleCount) {
  275. if (tCount) {
  276. for (var c = 0; c < tCount; c++) {
  277. this.references[v0.triangleStart + c] = this.references[tStart + c];
  278. }
  279. }
  280. } else {
  281. v0.triangleStart = tStart;
  282. }
  283. v0.triangleCount = tCount;
  284. break;
  285. }
  286. }
  287. };
  288. AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, trianglesIterator, callback,() => { return (triangleCount - deletedTriangles <= targetCount) });
  289. }, 0);
  290. };
  291. AsyncLoop.Run(this.decimationIterations,(loop: AsyncLoop) => {
  292. if (triangleCount - deletedTriangles <= targetCount) loop.breakLoop();
  293. else {
  294. iterationFunction(loop.index,() => {
  295. loop.executeNext();
  296. });
  297. }
  298. },() => {
  299. setTimeout(() => {
  300. successCallback(this.reconstructMesh());
  301. }, 0);
  302. });
  303. }
  304. private initWithMesh(mesh: Mesh, callback: Function) {
  305. if (!mesh) return;
  306. this.vertices = [];
  307. this.triangles = [];
  308. this._mesh = mesh;
  309. //It is assumed that a mesh has positions, normals and either uvs or colors.
  310. var positionData = this._mesh.getVerticesData(VertexBuffer.PositionKind);
  311. var normalData = this._mesh.getVerticesData(VertexBuffer.NormalKind);
  312. var uvs = this._mesh.getVerticesData(VertexBuffer.UVKind);
  313. var colorsData = this._mesh.getVerticesData(VertexBuffer.ColorKind);
  314. var indices = mesh.getIndices();
  315. var vertexInit = (i) => {
  316. var vertex = new DecimationVertex(Vector3.FromArray(positionData, i * 3), Vector3.FromArray(normalData, i * 3), null, i);
  317. if (this._mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  318. vertex.uv = Vector2.FromArray(uvs, i * 2);
  319. } else if (this._mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  320. vertex.color = Color4.FromArray(colorsData, i * 4);
  321. }
  322. this.vertices.push(vertex);
  323. };
  324. var totalVertices = mesh.getTotalVertices();
  325. AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit,() => {
  326. var indicesInit = (i) => {
  327. var pos = i * 3;
  328. var i0 = indices[pos + 0];
  329. var i1 = indices[pos + 1];
  330. var i2 = indices[pos + 2];
  331. var triangle = new DecimationTriangle([this.vertices[i0].id, this.vertices[i1].id, this.vertices[i2].id]);
  332. this.triangles.push(triangle);
  333. };
  334. AsyncLoop.SyncAsyncForLoop(indices.length / 3, this.syncIterations, indicesInit,() => {
  335. this.init(callback);
  336. });
  337. });
  338. }
  339. private init(callback: Function) {
  340. var triangleInit1 = (i) => {
  341. var t = this.triangles[i];
  342. t.normal = Vector3.Cross(this.vertices[t.vertices[1]].position.subtract(this.vertices[t.vertices[0]].position), this.vertices[t.vertices[2]].position.subtract(this.vertices[t.vertices[0]].position)).normalize();
  343. for (var j = 0; j < 3; j++) {
  344. this.vertices[t.vertices[j]].q.addArrayInPlace(QuadraticMatrix.DataFromNumbers(t.normal.x, t.normal.y, t.normal.z, -(Vector3.Dot(t.normal, this.vertices[t.vertices[0]].position))));
  345. }
  346. };
  347. AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit1,() => {
  348. var triangleInit2 = (i) => {
  349. var t = this.triangles[i];
  350. for (var j = 0; j < 3; ++j) {
  351. t.error[j] = this.calculateError(this.vertices[t.vertices[j]], this.vertices[t.vertices[(j + 1) % 3]]);
  352. }
  353. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  354. };
  355. AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit2,() => {
  356. this.initialised = true;
  357. callback();
  358. });
  359. });
  360. }
  361. private reconstructMesh(): Mesh {
  362. var newTriangles: Array<DecimationTriangle> = [];
  363. var i: number;
  364. for (i = 0; i < this.vertices.length; ++i) {
  365. this.vertices[i].triangleCount = 0;
  366. }
  367. var t: DecimationTriangle;
  368. var j: number;
  369. for (i = 0; i < this.triangles.length; ++i) {
  370. if (!this.triangles[i].deleted) {
  371. t = this.triangles[i];
  372. for (j = 0; j < 3; ++j) {
  373. this.vertices[t.vertices[j]].triangleCount = 1;
  374. }
  375. newTriangles.push(t);
  376. }
  377. }
  378. var newVerticesOrder = [];
  379. //compact vertices, get the IDs of the vertices used.
  380. var dst = 0;
  381. for (i = 0; i < this.vertices.length; ++i) {
  382. if (this.vertices[i].triangleCount) {
  383. this.vertices[i].triangleStart = dst;
  384. this.vertices[dst].position = this.vertices[i].position;
  385. this.vertices[dst].normal = this.vertices[i].normal;
  386. this.vertices[dst].uv = this.vertices[i].uv;
  387. this.vertices[dst].color = this.vertices[i].color;
  388. newVerticesOrder.push(i);
  389. dst++;
  390. }
  391. }
  392. for (i = 0; i < newTriangles.length; ++i) {
  393. t = newTriangles[i];
  394. for (j = 0; j < 3; ++j) {
  395. t.vertices[j] = this.vertices[t.vertices[j]].triangleStart;
  396. }
  397. }
  398. this.vertices = this.vertices.slice(0, dst);
  399. var newPositionData = [];
  400. var newNormalData = [];
  401. var newUVsData = [];
  402. var newColorsData = [];
  403. for (i = 0; i < newVerticesOrder.length; ++i) {
  404. newPositionData.push(this.vertices[i].position.x);
  405. newPositionData.push(this.vertices[i].position.y);
  406. newPositionData.push(this.vertices[i].position.z);
  407. newNormalData.push(this.vertices[i].normal.x);
  408. newNormalData.push(this.vertices[i].normal.y);
  409. newNormalData.push(this.vertices[i].normal.z);
  410. if (this.vertices[i].uv) {
  411. newUVsData.push(this.vertices[i].uv.x);
  412. newUVsData.push(this.vertices[i].uv.y);
  413. } else if (this.vertices[i].color) {
  414. newColorsData.push(this.vertices[i].color.r);
  415. newColorsData.push(this.vertices[i].color.g);
  416. newColorsData.push(this.vertices[i].color.b);
  417. newColorsData.push(this.vertices[i].color.a);
  418. }
  419. }
  420. var newIndicesArray: Array<number> = [];
  421. for (i = 0; i < newTriangles.length; ++i) {
  422. newIndicesArray.push(newTriangles[i].vertices[0]);
  423. newIndicesArray.push(newTriangles[i].vertices[1]);
  424. newIndicesArray.push(newTriangles[i].vertices[2]);
  425. }
  426. //not cloning, to avoid geometry problems. Creating a whole new mesh.
  427. var newMesh = new Mesh(this._mesh.name + "Decimated", this._mesh.getScene());
  428. newMesh.material = this._mesh.material;
  429. newMesh.parent = this._mesh.parent;
  430. newMesh.setIndices(newIndicesArray);
  431. newMesh.setVerticesData(VertexBuffer.PositionKind, newPositionData);
  432. newMesh.setVerticesData(VertexBuffer.NormalKind, newNormalData);
  433. if (newUVsData.length > 0)
  434. newMesh.setVerticesData(VertexBuffer.UVKind, newUVsData);
  435. if (newColorsData.length > 0)
  436. newMesh.setVerticesData(VertexBuffer.ColorKind, newColorsData);
  437. //preparing the skeleton support
  438. if (this._mesh.skeleton) {
  439. //newMesh.skeleton = this._mesh.skeleton.clone("", "");
  440. //newMesh.getScene().beginAnimation(newMesh.skeleton, 0, 100, true, 1.0);
  441. }
  442. return newMesh;
  443. }
  444. private isFlipped(vertex1: DecimationVertex, index2: number, point: Vector3, deletedArray: Array<boolean>, borderFactor: number, delTr: Array<DecimationTriangle>): boolean {
  445. for (var i = 0; i < vertex1.triangleCount; ++i) {
  446. var t = this.triangles[this.references[vertex1.triangleStart + i].triangleId];
  447. if (t.deleted) continue;
  448. var s = this.references[vertex1.triangleStart + i].vertexId;
  449. var id1 = t.vertices[(s + 1) % 3];
  450. var id2 = t.vertices[(s + 2) % 3];
  451. if ((id1 === index2 || id2 === index2)/* && !this.isTriangleOnBoundingBox(t)*/) {
  452. deletedArray[i] = true;
  453. delTr.push(t);
  454. continue;
  455. }
  456. var d1 = this.vertices[id1].position.subtract(point);
  457. d1 = d1.normalize();
  458. var d2 = this.vertices[id2].position.subtract(point);
  459. d2 = d2.normalize();
  460. if (Math.abs(Vector3.Dot(d1, d2)) > 0.999) return true;
  461. var normal = Vector3.Cross(d1, d2).normalize();
  462. deletedArray[i] = false;
  463. if (Vector3.Dot(normal, t.normal) < 0.2) return true;
  464. }
  465. return false;
  466. }
  467. private updateTriangles(vertexId: number, vertex: DecimationVertex, deletedArray: Array<boolean>, deletedTriangles: number): number {
  468. var newDeleted = deletedTriangles;
  469. for (var i = 0; i < vertex.triangleCount; ++i) {
  470. var ref = this.references[vertex.triangleStart + i];
  471. var t = this.triangles[ref.triangleId];
  472. if (t.deleted) continue;
  473. if (deletedArray[i]) {
  474. t.deleted = true;
  475. newDeleted++;
  476. continue;
  477. }
  478. t.vertices[ref.vertexId] = vertexId;
  479. t.isDirty = true;
  480. t.error[0] = this.calculateError(this.vertices[t.vertices[0]], this.vertices[t.vertices[1]]) + (t.borderFactor / 2);
  481. t.error[1] = this.calculateError(this.vertices[t.vertices[1]], this.vertices[t.vertices[2]]) + (t.borderFactor / 2);
  482. t.error[2] = this.calculateError(this.vertices[t.vertices[2]], this.vertices[t.vertices[0]]) + (t.borderFactor / 2);
  483. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  484. this.references.push(ref);
  485. }
  486. return newDeleted;
  487. }
  488. private identifyBorder() {
  489. for (var i = 0; i < this.vertices.length; ++i) {
  490. var vCount: Array<number> = [];
  491. var vId: Array<number> = [];
  492. var v = this.vertices[i];
  493. var j: number;
  494. for (j = 0; j < v.triangleCount; ++j) {
  495. var triangle = this.triangles[this.references[v.triangleStart + j].triangleId];
  496. for (var ii = 0; ii < 3; ii++) {
  497. var ofs = 0;
  498. var id = triangle.vertices[ii];
  499. while (ofs < vCount.length) {
  500. if (vId[ofs] === id) break;
  501. ++ofs;
  502. }
  503. if (ofs === vCount.length) {
  504. vCount.push(1);
  505. vId.push(id);
  506. } else {
  507. vCount[ofs]++;
  508. }
  509. }
  510. }
  511. for (j = 0; j < vCount.length; ++j) {
  512. if (vCount[j] === 1) {
  513. this.vertices[vId[j]].isBorder = true;
  514. } else {
  515. this.vertices[vId[j]].isBorder = false;
  516. }
  517. }
  518. }
  519. }
  520. private updateMesh(identifyBorders: boolean = false) {
  521. var i: number;
  522. if (!identifyBorders) {
  523. var newTrianglesVector: Array<DecimationTriangle> = [];
  524. for (i = 0; i < this.triangles.length; ++i) {
  525. if (!this.triangles[i].deleted) {
  526. newTrianglesVector.push(this.triangles[i]);
  527. }
  528. }
  529. this.triangles = newTrianglesVector;
  530. }
  531. for (i = 0; i < this.vertices.length; ++i) {
  532. this.vertices[i].triangleCount = 0;
  533. this.vertices[i].triangleStart = 0;
  534. }
  535. var t: DecimationTriangle;
  536. var j: number;
  537. var v: DecimationVertex;
  538. for (i = 0; i < this.triangles.length; ++i) {
  539. t = this.triangles[i];
  540. for (j = 0; j < 3; ++j) {
  541. v = this.vertices[t.vertices[j]];
  542. v.triangleCount++;
  543. }
  544. }
  545. var tStart = 0;
  546. for (i = 0; i < this.vertices.length; ++i) {
  547. this.vertices[i].triangleStart = tStart;
  548. tStart += this.vertices[i].triangleCount;
  549. this.vertices[i].triangleCount = 0;
  550. }
  551. var newReferences: Array<Reference> = new Array(this.triangles.length * 3);
  552. for (i = 0; i < this.triangles.length; ++i) {
  553. t = this.triangles[i];
  554. for (j = 0; j < 3; ++j) {
  555. v = this.vertices[t.vertices[j]];
  556. newReferences[v.triangleStart + v.triangleCount] = new Reference(j, i);
  557. v.triangleCount++;
  558. }
  559. }
  560. this.references = newReferences;
  561. if (identifyBorders) {
  562. this.identifyBorder();
  563. }
  564. }
  565. private vertexError(q: QuadraticMatrix, point: Vector3): number {
  566. var x = point.x;
  567. var y = point.y;
  568. var z = point.z;
  569. return q.data[0] * x * x + 2 * q.data[1] * x * y + 2 * q.data[2] * x * z + 2 * q.data[3] * x + q.data[4] * y * y
  570. + 2 * q.data[5] * y * z + 2 * q.data[6] * y + q.data[7] * z * z + 2 * q.data[8] * z + q.data[9];
  571. }
  572. private calculateError(vertex1: DecimationVertex, vertex2: DecimationVertex, pointResult?: Vector3, normalResult?: Vector3, uvResult?: Vector2, colorResult?: Color4): number {
  573. var q = vertex1.q.add(vertex2.q);
  574. var border = vertex1.isBorder && vertex2.isBorder;
  575. var error: number = 0;
  576. var qDet = q.det(0, 1, 2, 1, 4, 5, 2, 5, 7);
  577. if (qDet !== 0 && !border) {
  578. if (!pointResult) {
  579. pointResult = Vector3.Zero();
  580. }
  581. pointResult.x = -1 / qDet * (q.det(1, 2, 3, 4, 5, 6, 5, 7, 8));
  582. pointResult.y = 1 / qDet * (q.det(0, 2, 3, 1, 5, 6, 2, 7, 8));
  583. pointResult.z = -1 / qDet * (q.det(0, 1, 3, 1, 4, 6, 2, 5, 8));
  584. error = this.vertexError(q, pointResult);
  585. //TODO this should be correctly calculated
  586. if (normalResult) {
  587. normalResult.copyFrom(vertex1.normal);
  588. if (vertex1.uv)
  589. uvResult.copyFrom(vertex1.uv);
  590. else if (vertex1.color)
  591. colorResult.copyFrom(vertex1.color);
  592. }
  593. } else {
  594. var p3 = (vertex1.position.add(vertex2.position)).divide(new Vector3(2, 2, 2));
  595. //var norm3 = (vertex1.normal.add(vertex2.normal)).divide(new Vector3(2, 2, 2)).normalize();
  596. var error1 = this.vertexError(q, vertex1.position);
  597. var error2 = this.vertexError(q, vertex2.position);
  598. var error3 = this.vertexError(q, p3);
  599. error = Math.min(error1, error2, error3);
  600. if (error === error1) {
  601. if (pointResult) {
  602. pointResult.copyFrom(vertex1.position);
  603. normalResult.copyFrom(vertex1.normal);
  604. if (vertex1.uv)
  605. uvResult.copyFrom(vertex1.uv);
  606. else if (vertex1.color)
  607. colorResult.copyFrom(vertex1.color);
  608. }
  609. } else if (error === error2) {
  610. if (pointResult) {
  611. pointResult.copyFrom(vertex2.position);
  612. normalResult.copyFrom(vertex2.normal);
  613. if (vertex2.uv)
  614. uvResult.copyFrom(vertex2.uv);
  615. else if (vertex2.color)
  616. colorResult.copyFrom(vertex2.color);
  617. }
  618. } else {
  619. if (pointResult) {
  620. pointResult.copyFrom(p3);
  621. normalResult.copyFrom(vertex1.normal);
  622. if (vertex1.uv)
  623. uvResult.copyFrom(vertex1.uv);
  624. else if (vertex1.color)
  625. colorResult.copyFrom(vertex1.color);
  626. }
  627. }
  628. }
  629. return error;
  630. }
  631. }
  632. }