Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 61.54 KB | None | 0 0
  1. #include <float.h>
  2. #include <assert.h>
  3. #include "meshEdit.h"
  4. #include "mutablePriorityQueue.h"
  5. #include "error_dialog.h"
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. namespace CS248 {
  11.  
  12. VertexIter HalfedgeMesh::splitEdge(EdgeIter e0) {
  13. // TODO: (meshEdit)
  14. // This method should split the given edge and return an iterator to the
  15. // newly inserted vertex. The halfedge of this vertex should point along
  16. // the edge that was split, rather than the new edges.
  17. // Halfedges
  18. if(!e0->isBoundary()) {
  19. Vector3D oldMidpoint = e0->centroid();
  20. HalfedgeIter h0 = e0->halfedge();
  21. HalfedgeIter h1 = h0->next();
  22. HalfedgeIter h2 = h1->next();
  23. HalfedgeIter h3 = h0->twin();
  24. HalfedgeIter h4 = h3->next();
  25. HalfedgeIter h5 = h4->next();
  26. HalfedgeIter h6 = h1->twin();
  27. HalfedgeIter h7 = h2->twin();
  28. HalfedgeIter h8 = h4->twin();
  29. HalfedgeIter h9 = h5->twin();
  30.  
  31. // Vertices
  32. VertexIter v0 = h0->vertex();
  33. VertexIter v1 = h3->vertex();
  34. VertexIter v2 = h2->vertex();
  35. VertexIter v3 = h5->vertex();
  36.  
  37. // Edges
  38. EdgeIter e1 = h1->edge();
  39. EdgeIter e2 = h2->edge();
  40. EdgeIter e3 = h4->edge();
  41. EdgeIter e4 = h5->edge();
  42.  
  43. // Faces
  44. FaceIter f0 = h0->face();
  45. FaceIter f1 = h3->face();
  46.  
  47. // New Vertice
  48. VertexIter v4 = this->newVertex();
  49.  
  50. // New Halfedges
  51. HalfedgeIter h10 = this->newHalfedge();
  52. HalfedgeIter h11 = this->newHalfedge();
  53. HalfedgeIter h12 = this->newHalfedge();
  54. HalfedgeIter h13 = this->newHalfedge();
  55. HalfedgeIter h14 = this->newHalfedge();
  56. HalfedgeIter h15 = this->newHalfedge();
  57.  
  58. // New Faces
  59. FaceIter f2 = this->newFace();
  60. FaceIter f3 = this->newFace();
  61.  
  62. // New Edges
  63. EdgeIter e5 = this->newEdge();
  64. EdgeIter e6 = this->newEdge();
  65. EdgeIter e7 = this->newEdge();
  66.  
  67. // UPDATES
  68. this->deleteEdge(e0); // remove edge
  69. e0 = this->newEdge(); // create new edge
  70.  
  71. // Halfedges
  72. h0->next() = h1;
  73. h0->twin() = h3;
  74. h0->vertex() = v4; // new
  75. h0->edge() = e0;
  76. h0->face() = f0;
  77.  
  78. h1->next() = h11; // new
  79. h1->twin() = h6;
  80. h1->vertex() = v1;
  81. h1->edge() = e1;
  82. h1->face() = f0;
  83.  
  84. h2->next() = h13; // new
  85. h2->twin() = h7;
  86. h2->vertex() = v2;
  87. h2->edge() = e2;
  88. h2->face() = f2; // new
  89.  
  90. h3->next() = h10; // new
  91. h3->twin() = h0;
  92. h3->vertex() = v1;
  93. h3->edge() = e0;
  94. h3->face() = f1;
  95.  
  96. h4->next() = h15; // new
  97. h4->twin() = h8;
  98. h4->vertex() = v0;
  99. h4->edge() = e3;
  100. h4->face() = f3; // new
  101.  
  102. h5->next() = h3;
  103. h5->twin() = h9;
  104. h5->vertex() = v3;
  105. h5->edge() = e4;
  106. h5->face() = f1;
  107.  
  108. h6->next() = h6->next();
  109. h6->twin() = h1;
  110. h6->vertex() = v2;
  111. h6->edge() = e1;
  112. h6->face() = h6->face();
  113.  
  114. h7->next() = h7->next();
  115. h7->twin() = h2;
  116. h7->vertex() = v0;
  117. h7->edge() = e2;
  118. h7->face() = h7->face();
  119.  
  120. h8->next() = h8->next();
  121. h8->twin() = h4;
  122. h8->vertex() = v3;
  123. h8->edge() = e3;
  124. h8->face() = h8->face();
  125.  
  126. h9->next() = h9->next();
  127. h9->twin() = h5;
  128. h9->vertex() = v1;
  129. h9->edge() = e4;
  130. h9->face() = h9->face();
  131.  
  132. // NEW
  133. h10->next() = h5;
  134. h10->twin() = h15;
  135. h10->vertex() = v4;
  136. h10->edge() = e6;
  137. h10->face() = f1;
  138.  
  139. h11->next() = h0;
  140. h11->twin() = h12;
  141. h11->vertex() = v2;
  142. h11->edge() = e7;
  143. h11->face() = f0;
  144.  
  145. h12->next() = h2;
  146. h12->twin() = h11;
  147. h12->vertex() = v4;
  148. h12->edge() = e7;
  149. h12->face() = f2;
  150.  
  151. h13->next() = h12;
  152. h13->twin() = h14;
  153. h13->vertex() = v0;
  154. h13->edge() = e5;
  155. h13->face() = f2;
  156.  
  157. h14->next() = h4;
  158. h14->twin() = h13;
  159. h14->vertex() = v4;
  160. h14->edge() = e5;
  161. h14->face() = f3;
  162.  
  163. h15->next() = h14;
  164. h15->twin() = h10;
  165. h15->vertex() = v3;
  166. h15->edge() = e6;
  167. h15->face() = f3;
  168.  
  169. // Vertices
  170. v0->halfedge() = h13;
  171. v1->halfedge() = h3;
  172. v2->halfedge() = h11;
  173. v3->halfedge() = h15;
  174. v4->halfedge() = h0; // IMPORTANT!!
  175.  
  176. // Edges
  177. e0->halfedge() = h0;
  178. e1->halfedge() = h1;
  179. e2->halfedge() = h2;
  180. e3->halfedge() = h4;
  181. e4->halfedge() = h5;
  182. e5->halfedge() = h14;
  183. e6->halfedge() = h10;
  184. e7->halfedge() = h12;
  185.  
  186. // Faces
  187. f0->halfedge() = h0;
  188. f1->halfedge() = h3;
  189. f2->halfedge() = h13;
  190. f3->halfedge() = h14;
  191.  
  192. v4->position = oldMidpoint; // new position
  193. return v4;
  194. } else {
  195. Vector3D oldMidpoint = e0->centroid();
  196. HalfedgeIter h0 = e0->halfedge();
  197. if(h0->isBoundary()) {
  198. h0 = h0->twin();
  199. }
  200. HalfedgeIter h1 = h0->next();
  201. HalfedgeIter h2 = h1->next();
  202. HalfedgeIter h3 = h0->twin();
  203. HalfedgeIter h4 = h1->twin();
  204. HalfedgeIter h5 = h2->twin();
  205.  
  206. // Vertices
  207. VertexIter v0 = h0->vertex();
  208. VertexIter v1 = h1->vertex();
  209. VertexIter v2 = h2->vertex();
  210.  
  211. // Edges
  212. EdgeIter e1 = h1->edge();
  213. EdgeIter e2 = h2->edge();
  214.  
  215. // Faces
  216. FaceIter f0 = h0->face();
  217.  
  218. // New Vertice
  219. VertexIter v3 = this->newVertex();
  220.  
  221. // New Halfedges
  222. HalfedgeIter h6 = this->newHalfedge();
  223. HalfedgeIter h7 = this->newHalfedge();
  224. HalfedgeIter h8 = this->newHalfedge();
  225. HalfedgeIter h9 = this->newHalfedge();
  226.  
  227. // New Faces
  228. FaceIter f1 = this->newFace();
  229.  
  230. // New Edges
  231. EdgeIter e3 = this->newEdge();
  232. EdgeIter e4 = this->newEdge();
  233.  
  234. // UPDATES
  235. this->deleteEdge(e0); // remove edge
  236. e0 = this->newEdge(); // create new edge
  237.  
  238. h0->next() = h1;
  239. h0->twin() = h3;
  240. h0->vertex() = v3; // new
  241. h0->edge() = e0;
  242. h0->face() = f0;
  243.  
  244. h1->next() = h6; // new
  245. h1->twin() = h4;
  246. h1->vertex() = v1;
  247. h1->edge() = e1;
  248. h1->face() = f0;
  249.  
  250. h2->next() = h8; // new
  251. h2->twin() = h5;
  252. h2->vertex() = v2;
  253. h2->edge() = e2;
  254. h2->face() = f1; // new
  255.  
  256. HalfedgeIter h3OldNext = h3->next();
  257. h3->next() = h9; // new
  258. h3->twin() = h0;
  259. h3->vertex() = v1;
  260. h3->edge() = e0;
  261. h3->face() = h3->face();
  262.  
  263. h4->next() = h4->next(); // new
  264. h4->twin() = h1;
  265. h4->vertex() = v2;
  266. h4->edge() = e1;
  267. h4->face() = h4->face(); // new
  268.  
  269. h5->next() = h5->next();
  270. h5->twin() = h2;
  271. h5->vertex() = v0;
  272. h5->edge() = e2;
  273. h5->face() = h5->face();
  274.  
  275. h6->next() = h0;
  276. h6->twin() = h7;
  277. h6->vertex() = v2;
  278. h6->edge() = e4;
  279. h6->face() = f0;
  280.  
  281. h7->next() = h2;
  282. h7->twin() = h6;
  283. h7->vertex() = v3;
  284. h7->edge() = e4;
  285. h7->face() = f1;
  286.  
  287. h8->next() = h7;
  288. h8->twin() = h9;
  289. h8->vertex() = v0;
  290. h8->edge() = e3;
  291. h8->face() = f1;
  292.  
  293. h9->next() = h3OldNext;
  294. h9->twin() = h8;
  295. h9->vertex() = v3;
  296. h9->edge() = e3;
  297. h9->face() = h3->face();
  298.  
  299. v0->halfedge() = h8;
  300. v1->halfedge() = h1;
  301. v2->halfedge() = h2;
  302. v3->halfedge() = h0;
  303.  
  304. e0->halfedge() = h0;
  305. e1->halfedge() = h1;
  306. e2->halfedge() = h2;
  307. e3->halfedge() = h8;
  308. e4->halfedge() = h6;
  309.  
  310. f0->halfedge() = h0;
  311. f1->halfedge() = h8;
  312.  
  313. v3->position = oldMidpoint;
  314. return v3;
  315. }
  316.  
  317. }
  318.  
  319. VertexIter HalfedgeMesh::collapseEdge(EdgeIter e) {
  320. // *** Extra Credit ***
  321. // TODO: (meshEdit)
  322. // This method should collapse the given edge and return an iterator to
  323. // the new vertex created by the collapse.
  324. HalfedgeIter h = e->halfedge();
  325. HalfedgeIter twin = h->twin();
  326. FaceIter f0 = h->face();
  327. FaceIter f1 = twin->face();
  328. VertexIter v0 = h->vertex();
  329. VertexIter v1 = twin->vertex();
  330. // use degree to detect if face with edge e is a triangle or not
  331. // only two faces; one on each side
  332. // BELOW WORKS IF NEITHER IS A TRIANGLE
  333. HalfedgeIter h0 = h->next(); // placeholder value... change accordingly
  334. HalfedgeIter h1 = h0;
  335. HalfedgeIter h2 = twin->next();
  336. HalfedgeIter h3 = h2;
  337. cout << f0->degree() << endl;
  338. cout << f1->degree() << endl;
  339. if (f0->degree() == 3) {
  340. this->eraseEdge(h0->edge());
  341. h0 = h->next();
  342. h1 = h0;
  343. cout << "erase 1" << endl;
  344. }
  345. if (f1->degree() == 3) {
  346. this->eraseEdge(h2->edge());
  347. h2 = twin->next();
  348. h3 = h2;
  349. cout << "erase 2" << endl;
  350. }
  351.  
  352. cout << "erase edge worked!" << endl;
  353.  
  354. while (h1->next() != h) {
  355. h1 = h1->next();
  356. }
  357. h1->next() = h0;
  358. f0->halfedge() = h0;
  359. v1->halfedge() = h0;
  360. this->deleteHalfedge(h);
  361. cout << "erased one halfedge!" << endl;
  362.  
  363. while (h3->next() != twin) {
  364. h3 = h3->next();
  365. }
  366. h3->next() = h2;
  367. f1->halfedge() = h2;
  368. v0->halfedge() = h2;
  369. this->deleteHalfedge(twin);
  370.  
  371. cout << "erased the other!" << endl;
  372.  
  373. this->deleteEdge(e);
  374. // now merge two vertices together by removing v0
  375. HalfedgeIter step = h1;
  376. HalfedgeIter stop;
  377. while (step != h3) {
  378. step = step->twin();
  379. stop = step;
  380. step->vertex() = v1; // set vertex for each halfedge going out of v0 to be v1 instead
  381. while (step->next() != stop) {
  382. step = step->next();
  383. }
  384. }
  385.  
  386. cout << "DONE" << endl;
  387. this->deleteVertex(v0);
  388. return v1;
  389. }
  390.  
  391. VertexIter HalfedgeMesh::collapseFace(FaceIter f) {
  392. // *** Extra Credit ***
  393. // TODO: (meshEdit)
  394. // This method should collapse the given face and return an iterator to
  395. // the new vertex created by the collapse.
  396. HalfedgeIter h = f->halfedge();
  397. EdgeIter e = h->edge();
  398. HalfedgeIter hnext = h;
  399. VertexIter v;
  400. Size total = f->degree();
  401. cout << total << endl;
  402. do {
  403. e = hnext->edge();
  404. hnext = hnext->next();
  405. f->halfedge() = hnext;
  406. if (f->degree() == 3) {
  407. cout << "3!!!" << endl;
  408. HalfedgeIter h = e->halfedge();
  409. HalfedgeIter twin = h->twin();
  410. FaceIter f0 = h->face();
  411. FaceIter f1 = twin->face();
  412. VertexIter v0 = h->vertex();
  413. VertexIter v1 = twin->vertex();
  414. // use degree to detect if face with edge e is a triangle or not
  415. // only two faces; one on each side
  416. // BELOW WORKS IF NEITHER IS A TRIANGLE
  417. HalfedgeIter h0 = h->next(); // placeholder value... change accordingly
  418. HalfedgeIter h1 = h0;
  419. HalfedgeIter h2 = twin->next();
  420. HalfedgeIter h3 = h2;
  421. while (h1->next() != h) {
  422. h1 = h1->next();
  423. }
  424. h1->next() = h0;
  425. f0->halfedge() = h0;
  426. v1->halfedge() = h0;
  427. this->deleteHalfedge(h);
  428.  
  429. while (h3->next() != twin) {
  430. h3 = h3->next();
  431. }
  432. h3->next() = h2;
  433. f1->halfedge() = h2;
  434. v0->halfedge() = h2;
  435. this->deleteHalfedge(twin);
  436.  
  437. this->deleteEdge(e);
  438. // now merge two vertices together by removing v0
  439. HalfedgeIter step = h1;
  440. HalfedgeIter stop;
  441. while (step != h3) {
  442. step = step->twin();
  443. stop = step;
  444. step->vertex() = v1; // set vertex for each halfedge going out of v0 to be v1 instead
  445. while (step->next() != stop) {
  446. step = step->next();
  447. }
  448. }
  449. this->deleteVertex(v0);
  450. }
  451. else {
  452. cout << "edge collapse" << endl;
  453. v = this->collapseEdge(e);
  454. }
  455. total--;
  456. } while (total > 1);
  457. cout << "done!" << endl;
  458. return v;
  459. }
  460.  
  461. FaceIter HalfedgeMesh::eraseVertex(VertexIter v) {
  462. HalfedgeIter h = v->halfedge(); // starting halfedge
  463. FaceIter f; // face to be kept
  464. // iterate over all the halfedges of faces that go around the vertice to be deleted
  465. HalfedgeIter hnext;
  466. EdgeIter e;
  467. int degree = v->degree();
  468. for(int i = 0; i < degree; i++) {
  469. hnext = h->twin()->next();
  470. e = h->edge();
  471. f = this->eraseEdge(e);
  472. h = hnext;
  473. }
  474. this->deleteVertex(v);
  475.  
  476. return f;
  477. }
  478.  
  479. FaceIter HalfedgeMesh::eraseEdge(EdgeIter e) {
  480. // *** Extra Credit ***
  481. HalfedgeIter h0 = e->halfedge();
  482. h0->name = "h0";
  483. HalfedgeIter h1 = h0->twin();
  484. h1->name = "h1";
  485. VertexIter v0 = h0->vertex();
  486. VertexIter v1 = h1->vertex();
  487. HalfedgeIter v0newNext = h1->next();
  488. v0newNext->name = "v0nn";
  489. HalfedgeIter v1newNext = h0->next();
  490. v1newNext->name = "v1nn";
  491. v0->halfedge() = v0newNext;
  492. v1->halfedge() = v1newNext;
  493.  
  494. HalfedgeIter current = v0newNext->twin();
  495. while(current->next() != h0) {
  496. current = current->next()->twin();
  497. }
  498. current->next() = v0newNext;
  499. current->name = "v0nc";
  500.  
  501.  
  502. current = v1newNext->twin();
  503. while(current->next() != h1) {
  504. current = current->next()->twin();
  505. }
  506. current->next() = v1newNext;
  507. current->name = "v1nc";
  508.  
  509. FaceIter mergedFace = v0newNext->face();
  510. FaceIter faceToErase = h0->face();
  511. current = v0newNext;
  512. if(v0newNext == h0) {
  513. cout << "Making sure..." << endl;
  514. mergedFace = v1newNext->face();
  515. faceToErase = h1->face();
  516. current = v1newNext;
  517. }
  518. mergedFace->halfedge() = current;
  519. //cout << current->name << endl;
  520. current = current->next();
  521. while(current != mergedFace->halfedge()) {
  522. current->face() = mergedFace;
  523. //cout << current->name << endl;
  524. current = current->next();
  525. }
  526.  
  527. this->deleteHalfedge(h0);
  528. this->deleteHalfedge(h1);
  529. if(faceToErase != mergedFace) {
  530. this->deleteFace(faceToErase);
  531. }
  532.  
  533. this->deleteEdge(e);
  534. return mergedFace;
  535. }
  536.  
  537. EdgeIter HalfedgeMesh::flipEdge(EdgeIter e0) {
  538. if (e0->isBoundary())
  539. return e0;
  540. // don't flip if tetrahedron...
  541. // need to make it able to do n-gon
  542. // more edge cases?
  543. // make sure number of faces stays same
  544.  
  545. // PHASE I
  546. // Halfedges
  547. HalfedgeIter h0 = e0->halfedge();
  548. HalfedgeIter h1 = h0->next();
  549. HalfedgeIter h2 = h1->next();
  550. HalfedgeIter h3 = h0->twin();
  551. HalfedgeIter h4 = h3->next();
  552. HalfedgeIter h5 = h4->next();
  553. HalfedgeIter h6 = h1->twin();
  554. HalfedgeIter h7 = h2->twin();
  555. HalfedgeIter h8 = h4->twin();
  556. HalfedgeIter h9 = h5->twin();
  557.  
  558. // Vertices
  559. VertexIter v0 = h0->vertex();
  560. VertexIter v1 = h3->vertex();
  561. VertexIter v2 = h2->vertex();
  562. VertexIter v3 = h5->vertex();
  563.  
  564. // Edges
  565. EdgeIter e1 = h1->edge();
  566. EdgeIter e2 = h2->edge();
  567. EdgeIter e3 = h4->edge();
  568. EdgeIter e4 = h5->edge();
  569.  
  570. // Faces
  571. FaceIter f0 = h0->face();
  572. FaceIter f1 = h3->face();
  573.  
  574. // PHASE III
  575. // Halfedges
  576. h0->next() = h1;
  577. h0->twin() = h3;
  578. h0->vertex() = v3;
  579. h0->edge() = e0;
  580. h0->face() = f0;
  581.  
  582. h1->next() = h2;
  583. h1->twin() = h7;
  584. h1->vertex() = v2;
  585. h1->edge() = e2;
  586. h1->face() = f0;
  587.  
  588. h2->next() = h0;
  589. h2->twin() = h8;
  590. h2->vertex() = v0;
  591. h2->edge() = e3;
  592. h2->face() = f0;
  593.  
  594. h3->next() = h4;
  595. h3->twin() = h0;
  596. h3->vertex() = v2;
  597. h3->edge() = e0;
  598. h3->face() = f1;
  599.  
  600. h4->next() = h5;
  601. h4->twin() = h9;
  602. h4->vertex() = v3;
  603. h4->edge() = e4;
  604. h4->face() = f1;
  605.  
  606. h5->next() = h3;
  607. h5->twin() = h6;
  608. h5->vertex() = v1;
  609. h5->edge() = e1;
  610. h5->face() = f1;
  611.  
  612. h6->next() = h6->next();
  613. h6->twin() = h5;
  614. h6->vertex() = v2;
  615. h6->edge() = e1;
  616. h6->face() = h6->face();
  617.  
  618. h7->next() = h7->next();
  619. h7->twin() = h1;
  620. h7->vertex() = v0;
  621. h7->edge() = e2;
  622. h7->face() = h7->face();
  623.  
  624. h8->next() = h8->next();
  625. h8->twin() = h2;
  626. h8->vertex() = v3;
  627. h8->edge() = e3;
  628. h8->face() = h8->face();
  629.  
  630. h9->next() = h9->next();
  631. h9->twin() = h4;
  632. h9->vertex() = v1;
  633. h9->edge() = e4;
  634. h9->face() = h9->face();
  635.  
  636. // Vertices
  637. v0->halfedge() = h2;
  638. v1->halfedge() = h5;
  639. v2->halfedge() = h3;
  640. v3->halfedge() = h0;
  641.  
  642. // Edges
  643. e0->halfedge() = h0;
  644. e1->halfedge() = h5;
  645. e2->halfedge() = h1;
  646. e3->halfedge() = h2;
  647. e4->halfedge() = h4;
  648.  
  649. // Faces
  650. f0->halfedge() = h3;
  651. f1->halfedge() = h0;
  652. return e0;
  653. }
  654.  
  655. void HalfedgeMesh::subdivideQuad(bool useCatmullClark) {
  656. // Unlike the local mesh operations (like bevel or edge flip), we will perform
  657. // subdivision by splitting *all* faces into quads "simultaneously." Rather
  658. // than operating directly on the halfedge data structure (which as you've
  659. // seen
  660. // is quite difficult to maintain!) we are going to do something a bit nicer:
  661. //
  662. // 1. Create a raw list of vertex positions and faces (rather than a full-
  663. // blown halfedge mesh).
  664. //
  665. // 2. Build a new halfedge mesh from these lists, replacing the old one.
  666. //
  667. // Sometimes rebuilding a data structure from scratch is simpler (and even
  668. // more
  669. // efficient) than incrementally modifying the existing one. These steps are
  670. // detailed below.
  671.  
  672. // TODO Step I: Compute the vertex positions for the subdivided mesh. Here
  673. // we're
  674. // going to do something a little bit strange: since we will have one vertex
  675. // in
  676. // the subdivided mesh for each vertex, edge, and face in the original mesh,
  677. // we
  678. // can nicely store the new vertex *positions* as attributes on vertices,
  679. // edges,
  680. // and faces of the original mesh. These positions can then be conveniently
  681. // copied into the new, subdivided mesh.
  682. // [See subroutines for actual "TODO"s]
  683. if (useCatmullClark) {
  684. computeCatmullClarkPositions();
  685. } else {
  686. computeLinearSubdivisionPositions();
  687. }
  688.  
  689. //cout << "divisions worked!" << endl;
  690. // TODO Step II: Assign a unique index (starting at 0) to each vertex, edge,
  691. // and
  692. // face in the original mesh. These indices will be the indices of the
  693. // vertices
  694. // in the new (subdivided mesh). They do not have to be assigned in any
  695. // particular
  696. // order, so long as no index is shared by more than one mesh element, and the
  697. // total number of indices is equal to V+E+F, i.e., the total number of
  698. // vertices
  699. // plus edges plus faces in the original mesh. Basically we just need a
  700. // one-to-one
  701. // mapping between original mesh elements and subdivided mesh vertices.
  702. // [See subroutine for actual "TODO"s]
  703. assignSubdivisionIndices();
  704. //cout << "indices worked!" << endl;
  705.  
  706. // TODO Step III: Build a list of quads in the new (subdivided) mesh, as
  707. // tuples of
  708. // the element indices defined above. In other words, each new quad should be
  709. // of
  710. // the form (i,j,k,l), where i,j,k and l are four of the indices stored on our
  711. // original mesh elements. Note that it is essential to get the orientation
  712. // right
  713. // here: (i,j,k,l) is not the same as (l,k,j,i). Indices of new faces should
  714. // circulate in the same direction as old faces (think about the right-hand
  715. // rule).
  716. // [See subroutines for actual "TODO"s]
  717. vector<vector<Index> > subDFaces;
  718. vector<Vector3D> subDVertices;
  719. buildSubdivisionFaceList(subDFaces);
  720. //cout << "faces worked!" << endl;
  721. buildSubdivisionVertexList(subDVertices);
  722. //cout << "vertices worked!" << endl;
  723. // TODO Step IV: Pass the list of vertices and quads to a routine that clears
  724. // the
  725. // internal data for this halfedge mesh, and builds new halfedge data from
  726. // scratch,
  727. // using the two lists.
  728. rebuild(subDFaces, subDVertices);
  729. //cout << "rebuild worked! All done!" << endl;
  730. }
  731.  
  732. /**
  733. * Compute new vertex positions for a mesh that splits each polygon
  734. * into quads (by inserting a vertex at the face midpoint and each
  735. * of the edge midpoints). The new vertex positions will be stored
  736. * in the members Vertex::newPosition, Edge::newPosition, and
  737. * Face::newPosition. The values of the positions are based on
  738. * simple linear interpolation, e.g., the edge midpoints and face
  739. * centroids.
  740. */
  741. void HalfedgeMesh::computeLinearSubdivisionPositions() {
  742. // TODO For each vertex, assign Vertex::newPosition to
  743. // its original position, Vertex::position.
  744. for (VertexIter v = this->verticesBegin(); v != this->verticesEnd(); v++) {
  745. v->newPosition = v->position;
  746. }
  747. // TODO For each edge, assign the midpoint of the two original
  748. // positions to Edge::newPosition.
  749. for (EdgeIter e = this->edgesBegin(); e != this->edgesEnd(); e++) {
  750. e->newPosition = e->centroid();
  751. }
  752. // TODO For each face, assign the centroid (i.e., arithmetic mean)
  753. // of the original vertex positions to Face::newPosition. Note
  754. // that in general, NOT all faces will be triangles!
  755. for (FaceIter f = this->facesBegin(); f != this->facesEnd(); f++) {
  756. f->newPosition = f->centroid();
  757. }
  758.  
  759. //showError("computeLinearSubdivisionPositions() not implemented.");
  760. }
  761.  
  762. /**
  763. * Compute new vertex positions for a mesh that splits each polygon
  764. * into quads (by inserting a vertex at the face midpoint and each
  765. * of the edge midpoints). The new vertex positions will be stored
  766. * in the members Vertex::newPosition, Edge::newPosition, and
  767. * Face::newPosition. The values of the positions are based on
  768. * the Catmull-Clark rules for subdivision.
  769. */
  770. void HalfedgeMesh::computeCatmullClarkPositions() {
  771. // TODO The implementation for this routine should be
  772. // a lot like HalfedgeMesh::computeLinearSubdivisionPositions(),
  773. // except that the calculation of the positions themsevles is
  774. // slightly more involved, using the Catmull-Clark subdivision
  775. // rules. (These rules are outlined in the Developer Manual.)
  776.  
  777. // TODO face
  778. for (FaceIter f = this->facesBegin(); f != this->facesEnd(); f++) {
  779. f->newPosition = f->centroid();
  780. }
  781.  
  782. // TODO edges
  783. for (EdgeIter e = this->edgesBegin(); e != this->edgesEnd(); e++) {
  784. if(e->isBoundary()) {
  785. e->newPosition = e->centroid();
  786. } else {
  787. e->newPosition = ((*e).halfedge()->face()->newPosition + (*e).halfedge()->twin()->face()->newPosition + (*e).halfedge()->vertex()->position + (*e).halfedge()->twin()->vertex()->position) / 4;
  788. }
  789.  
  790. }
  791.  
  792. // TODO vertices
  793. Vector3D q, r;
  794. Size n;
  795. HalfedgeIter h;
  796. for (VertexIter v = this->verticesBegin(); v != this->verticesEnd(); v++) {
  797.  
  798. if(v->isBoundary()) {
  799. HalfedgeIter h = v->halfedge();
  800. EdgeIter e1, e2;
  801. int foundEdges = 0;
  802. do {
  803. if(h->edge()->isBoundary()) {
  804. if(foundEdges == 0) {
  805. e1 = h->edge();
  806. } else {
  807. e2 = h->edge();
  808. }
  809. foundEdges++;
  810. }
  811. h = h->twin()->next();
  812. }while(foundEdges < 2);
  813. v->newPosition = (3.0 / 4.0) * v->position + (1.0 / 8.0) * e1->newPosition + (1.0 / 8.0) * e2->newPosition;
  814. } else {
  815. n = v->degree(); // number of faces/edges touching vertex
  816. h = v->halfedge(); // get halfedge of vertex
  817. q = Vector3D();
  818. r = Vector3D();
  819. do {
  820. if (!h->face()->isBoundary()) { // check if face is a hole
  821. q += (*h).face()->newPosition;
  822. r += (*h).edge()->newPosition; // okay to be a boundary edge?
  823. }
  824. h = h->twin()->next();
  825. } while (h != v->halfedge());
  826. q *= 1.0 / n;
  827. r *= 1.0 / n;
  828. ////cout << "q: " << q << endl;
  829. ////cout << "r: " << r << endl;
  830. ////cout << "v: " << v->position << endl;
  831. v->newPosition = (q + 2 * r + (n - 3) * v->position) / n;
  832. ////cout << "vert: " << v->newPosition << endl;
  833. }
  834.  
  835. }
  836. ////cout << "vertices worked!" << endl;
  837. }
  838.  
  839. /**
  840. * Assign a unique integer index to each vertex, edge, and face in
  841. * the mesh, starting at 0 and incrementing by 1 for each element.
  842. * These indices will be used as the vertex indices for a mesh
  843. * subdivided using Catmull-Clark (or linear) subdivision.
  844. */
  845. void HalfedgeMesh::assignSubdivisionIndices() {
  846. // TODO Start a counter at zero; if you like, you can use the
  847. // "Index" type (defined in halfedgeMesh.h)
  848. Index pos = 0;
  849. // TODO Iterate over vertices, assigning values to Vertex::index
  850. for (VertexIter v = this->verticesBegin(); v != this->verticesEnd(); v++) {
  851. v->index = pos;
  852. pos++;
  853. }
  854. // TODO Iterate over edges, assigning values to Edge::index
  855. for (EdgeIter e = this->edgesBegin(); e != this->edgesEnd(); e++) {
  856. e->index = pos;
  857. pos++;
  858. }
  859. // TODO Iterate over faces, assigning values to Face::index
  860. for (FaceIter f = this->facesBegin(); f != this->facesEnd(); f++) {
  861. f->index = pos;
  862. pos++;
  863. }
  864. //showError("assignSubdivisionIndices() not implemented.");
  865. }
  866.  
  867. /**
  868. * Build a flat list containing all the vertex positions for a
  869. * Catmull-Clark (or linear) subdivison of this mesh. The order of
  870. * vertex positions in this list must be identical to the order
  871. * of indices assigned to Vertex::newPosition, Edge::newPosition,
  872. * and Face::newPosition.
  873. */
  874. void HalfedgeMesh::buildSubdivisionVertexList(vector<Vector3D>& subDVertices) {
  875. // TODO Resize the vertex list so that it can hold all the vertices.
  876.  
  877. // TODO Iterate over vertices, assigning Vertex::newPosition to the
  878. // appropriate location in the new vertex list.
  879. for (VertexIter v = this->verticesBegin(); v != this->verticesEnd(); v++) {
  880. subDVertices.push_back(v->newPosition);
  881. }
  882.  
  883. // TODO Iterate over edges, assigning Edge::newPosition to the appropriate
  884. // location in the new vertex list.
  885. for (EdgeIter e = this->edgesBegin(); e != this->edgesEnd(); e++) {
  886. subDVertices.push_back(e->newPosition);
  887. }
  888.  
  889. // TODO Iterate over faces, assigning Face::newPosition to the appropriate
  890. // location in the new vertex list.
  891. for (FaceIter f = this->facesBegin(); f != this->facesEnd(); f++) {
  892. subDVertices.push_back(f->newPosition);
  893. }
  894. //showError("buildSubdivisionVertexList() not implemented.");
  895. }
  896.  
  897. /**
  898. * Build a flat list containing all the quads in a Catmull-Clark
  899. * (or linear) subdivision of this mesh. Each quad is specified
  900. * by a vector of four indices (i,j,k,l), which come from the
  901. * members Vertex::index, Edge::index, and Face::index. Note that
  902. * the ordering of these indices is important because it determines
  903. * the orientation of the new quads; it is also important to avoid
  904. * "bowties." For instance, (l,k,j,i) has the opposite orientation
  905. * of (i,j,k,l), and if (i,j,k,l) is a proper quad, then (i,k,j,l)
  906. * will look like a bowtie.
  907. */
  908. void HalfedgeMesh::buildSubdivisionFaceList(vector<vector<Index> >& subDFaces) {
  909. // TODO This routine is perhaps the most tricky step in the construction of
  910. // a subdivision mesh (second, perhaps, to computing the actual Catmull-Clark
  911. // vertex positions). Basically what you want to do is iterate over faces,
  912. // then for each for each face, append N quads to the list (where N is the
  913. // degree of the face). For this routine, it may be more convenient to simply
  914. // append quads to the end of the list (rather than allocating it ahead of
  915. // time), though YMMV. You can of course iterate around a face by starting
  916. // with its first halfedge and following the "next" pointer until you get
  917. // back to the beginning. The tricky part is making sure you grab the right
  918. // indices in the right order---remember that there are indices on vertices,
  919. // edges, AND faces of the original mesh. All of these should get used. Also
  920. // remember that you must have FOUR indices per face, since you are making a
  921. // QUAD mesh!
  922.  
  923. // TODO iterate over faces
  924. // TODO loop around face
  925. // TODO build lists of four indices for each sub-quad
  926. // TODO append each list of four indices to face list
  927. vector<Index> quad(4);
  928. HalfedgeIter h, hNext;
  929. Size n;
  930. for (FaceIter f = this->facesBegin(); f != this->facesEnd(); f++) {
  931. h = f->halfedge();
  932. hNext = f->halfedge();
  933. n = f->degree() - 1;
  934. do {
  935. if (!f->isBoundary()) { // check if face is a hole
  936. quad[0] = (*h).vertex()->index; // original vertice
  937. ////cout << "vert: " << (*h).vertex()->index << endl;
  938. quad[1] = (*h).edge()->index; // edge vertice
  939. ////cout << "edge 1: " << (*h).edge()->index << endl;
  940. quad[2] = f->index; // face vertice
  941. ////cout << "face: " << f->index << endl;
  942. while (n > 0) {
  943. hNext = hNext->next();
  944. n--;
  945. }
  946. quad[3] = (*hNext).edge()->index; // edge vertice
  947. ////cout << "edge 2: " << (*hNext).edge()->index << endl;
  948. subDFaces.push_back(quad);
  949. n = f->degree();
  950. }
  951. h = h->next();
  952. hNext = hNext->next();
  953. } while (h != f->halfedge()); // keep looping until come back to first halfedge
  954. }
  955. }
  956.  
  957. FaceIter HalfedgeMesh::bevelVertex(VertexIter v) {
  958. // *** Extra Credit ***
  959. // TODO This method should replace the vertex v with a face, corresponding to
  960. // a bevel operation. It should return the new face. NOTE: This method is
  961. // responsible for updating the *connectivity* of the mesh only---it does not
  962. // need to update the vertex positions. These positions will be updated in
  963. // HalfedgeMesh::bevelVertexComputeNewPositions (which you also have to
  964. // implement!)
  965.  
  966. FaceIter newFace = this->newFace();
  967. HalfedgeIter h0 = v->halfedge();
  968. HalfedgeIter current = h0;
  969. bool assigned = false;
  970. HalfedgeIter prevNewHalfedge;
  971. int n = v->degree();
  972. for(int i = 0; i < n; i++) {
  973. HalfedgeIter twin = current->twin();
  974. HalfedgeIter next = twin->next();
  975.  
  976. VertexIter ov0 = twin->vertex();
  977. FaceIter of0 = current->face();
  978. FaceIter of1 = twin->face();
  979.  
  980. HalfedgeIter nh0 = this->newHalfedge();
  981. HalfedgeIter nh1 = this->newHalfedge();
  982. HalfedgeIter nh2 = this->newHalfedge();
  983. HalfedgeIter nh3 = this->newHalfedge();
  984.  
  985. VertexIter nv0 = this->newVertex();
  986.  
  987. EdgeIter ne0 = this->newEdge();
  988. EdgeIter ne1 = this->newEdge();
  989.  
  990. HalfedgeIter intonh1 = current;
  991. while(intonh1->next() != twin) {
  992. intonh1 = intonh1->next()->twin();
  993. }
  994.  
  995. nh0->next() = current->next();
  996. nh0->twin() = nh1;
  997. nh0->vertex() = nv0;
  998. nh0->edge() = ne0;
  999. nh0->face() = of0;
  1000.  
  1001. nh1->next() = nh2;
  1002. nh1->twin() = nh0;
  1003. nh1->vertex() = ov0;
  1004. nh1->edge() = ne0;
  1005. nh1->face() = of1;
  1006.  
  1007. intonh1->next() = nh1;
  1008.  
  1009. if(assigned) {
  1010. prevNewHalfedge->next() = nh0;
  1011. }
  1012. nh2->twin() = nh3;
  1013. nh2->vertex() = nv0;
  1014. nh2->edge() = ne1;
  1015. nh2->face() = of1;
  1016.  
  1017. if(assigned) {
  1018. nh3->next() = prevNewHalfedge->twin();
  1019. }
  1020. nh3->twin() = nh2;
  1021. if(assigned) {
  1022. prevNewHalfedge->twin()->vertex() = nv0;
  1023. }
  1024. nh3->edge() = ne1;
  1025. nh3->face() = newFace;
  1026.  
  1027. ne0->halfedge() = nh0;
  1028. ne1->halfedge() = nh2;
  1029.  
  1030. nv0->halfedge() = nh0;
  1031. ov0->halfedge() = nh1;
  1032.  
  1033. of0->halfedge() = nh0;
  1034.  
  1035. prevNewHalfedge = nh2;
  1036. assigned = true;
  1037.  
  1038. this->deleteEdge(current->edge());
  1039. this->deleteHalfedge(current);
  1040. this->deleteHalfedge(twin);
  1041.  
  1042. current = next;
  1043. }
  1044.  
  1045. HalfedgeIter nh0 = prevNewHalfedge->face()->halfedge();
  1046. HalfedgeIter nh3 = nh0->twin()->next()->twin();
  1047. prevNewHalfedge->next() = nh0;
  1048. nh3->next() = prevNewHalfedge->twin();
  1049. prevNewHalfedge->twin()->vertex() = nh0->vertex();
  1050. newFace->halfedge() = prevNewHalfedge->twin();
  1051.  
  1052. this->deleteVertex(v);
  1053. return newFace;
  1054. }
  1055.  
  1056. FaceIter HalfedgeMesh::bevelEdge(EdgeIter e) {
  1057. VertexIter mv0 = e->halfedge()->vertex();
  1058. VertexIter mv1 = e->halfedge()->twin()->vertex();
  1059.  
  1060. Vector3D mv0pos = mv0->position;
  1061. Vector3D mv1pos = mv1->position;
  1062.  
  1063. VertexIter v = mv0;
  1064. FaceIter newFace = this->newFace();
  1065. HalfedgeIter h0 = v->halfedge();
  1066. HalfedgeIter current = h0;
  1067. bool assigned = false;
  1068. HalfedgeIter prevNewHalfedge;
  1069. int n1 = mv0->degree();
  1070. int n2 = mv1->degree();
  1071. for(int i = 0; i < n1; i++) {
  1072. HalfedgeIter twin = current->twin();
  1073. HalfedgeIter next = twin->next();
  1074.  
  1075. VertexIter ov0 = twin->vertex();
  1076. FaceIter of0 = current->face();
  1077. FaceIter of1 = twin->face();
  1078.  
  1079. HalfedgeIter nh0 = this->newHalfedge();
  1080. HalfedgeIter nh1 = this->newHalfedge();
  1081. HalfedgeIter nh2 = this->newHalfedge();
  1082. HalfedgeIter nh3 = this->newHalfedge();
  1083.  
  1084. VertexIter nv0 = this->newVertex();
  1085. nv0->position = mv0pos;
  1086.  
  1087. EdgeIter ne0 = this->newEdge();
  1088. EdgeIter ne1 = this->newEdge();
  1089.  
  1090. if(ov0 == mv1) {
  1091. mv0 = nv0;
  1092. }
  1093.  
  1094. HalfedgeIter intonh1 = current;
  1095. while(intonh1->next() != twin) {
  1096. intonh1 = intonh1->next()->twin();
  1097. }
  1098.  
  1099. nh0->next() = current->next();
  1100. nh0->twin() = nh1;
  1101. nh0->vertex() = nv0;
  1102. nh0->edge() = ne0;
  1103. nh0->face() = of0;
  1104.  
  1105. nh1->next() = nh2;
  1106. nh1->twin() = nh0;
  1107. nh1->vertex() = ov0;
  1108. nh1->edge() = ne0;
  1109. nh1->face() = of1;
  1110.  
  1111. intonh1->next() = nh1;
  1112.  
  1113. if(assigned) {
  1114. prevNewHalfedge->next() = nh0;
  1115. }
  1116. nh2->twin() = nh3;
  1117. nh2->vertex() = nv0;
  1118. nh2->edge() = ne1;
  1119. nh2->face() = of1;
  1120.  
  1121. if(assigned) {
  1122. nh3->next() = prevNewHalfedge->twin();
  1123. }
  1124. nh3->twin() = nh2;
  1125. if(assigned) {
  1126. prevNewHalfedge->twin()->vertex() = nv0;
  1127. }
  1128. nh3->edge() = ne1;
  1129. nh3->face() = newFace;
  1130.  
  1131. ne0->halfedge() = nh0;
  1132. ne1->halfedge() = nh2;
  1133.  
  1134. nv0->halfedge() = nh0;
  1135. ov0->halfedge() = nh1;
  1136.  
  1137. of0->halfedge() = nh0;
  1138.  
  1139. prevNewHalfedge = nh2;
  1140. assigned = true;
  1141.  
  1142. this->deleteEdge(current->edge());
  1143. this->deleteHalfedge(current);
  1144. this->deleteHalfedge(twin);
  1145.  
  1146. current = next;
  1147. }
  1148.  
  1149. HalfedgeIter nh0 = prevNewHalfedge->face()->halfedge();
  1150. HalfedgeIter nh3 = nh0->twin()->next()->twin();
  1151. prevNewHalfedge->next() = nh0;
  1152. nh3->next() = prevNewHalfedge->twin();
  1153. prevNewHalfedge->twin()->vertex() = nh0->vertex();
  1154. newFace->halfedge() = prevNewHalfedge->twin();
  1155.  
  1156. this->deleteVertex(v);
  1157.  
  1158. v = mv1;
  1159. h0 = v->halfedge();
  1160. current = h0;
  1161. assigned = false;
  1162. for(int i = 0; i < n2; i++) {
  1163. HalfedgeIter twin = current->twin();
  1164. HalfedgeIter next = twin->next();
  1165.  
  1166. VertexIter ov0 = twin->vertex();
  1167. FaceIter of0 = current->face();
  1168. FaceIter of1 = twin->face();
  1169.  
  1170. HalfedgeIter nh0 = this->newHalfedge();
  1171. HalfedgeIter nh1 = this->newHalfedge();
  1172. HalfedgeIter nh2 = this->newHalfedge();
  1173. HalfedgeIter nh3 = this->newHalfedge();
  1174.  
  1175. VertexIter nv0 = this->newVertex();
  1176. nv0->position = mv1pos;
  1177.  
  1178. EdgeIter ne0 = this->newEdge();
  1179. EdgeIter ne1 = this->newEdge();
  1180.  
  1181. if(ov0 == mv0) {
  1182. mv1 = nv0;
  1183. }
  1184.  
  1185. HalfedgeIter intonh1 = current;
  1186. while(intonh1->next() != twin) {
  1187. intonh1 = intonh1->next()->twin();
  1188. }
  1189.  
  1190. nh0->next() = current->next();
  1191. nh0->twin() = nh1;
  1192. nh0->vertex() = nv0;
  1193. nh0->edge() = ne0;
  1194. nh0->face() = of0;
  1195.  
  1196. nh1->next() = nh2;
  1197. nh1->twin() = nh0;
  1198. nh1->vertex() = ov0;
  1199. nh1->edge() = ne0;
  1200. nh1->face() = of1;
  1201.  
  1202. intonh1->next() = nh1;
  1203.  
  1204. if(assigned) {
  1205. prevNewHalfedge->next() = nh0;
  1206. }
  1207. nh2->twin() = nh3;
  1208. nh2->vertex() = nv0;
  1209. nh2->edge() = ne1;
  1210. nh2->face() = of1;
  1211.  
  1212. if(assigned) {
  1213. nh3->next() = prevNewHalfedge->twin();
  1214. }
  1215. nh3->twin() = nh2;
  1216. if(assigned) {
  1217. prevNewHalfedge->twin()->vertex() = nv0;
  1218. }
  1219. nh3->edge() = ne1;
  1220. nh3->face() = newFace;
  1221.  
  1222. ne0->halfedge() = nh0;
  1223. ne1->halfedge() = nh2;
  1224.  
  1225. nv0->halfedge() = nh0;
  1226. ov0->halfedge() = nh1;
  1227.  
  1228. of0->halfedge() = nh0;
  1229.  
  1230. prevNewHalfedge = nh2;
  1231. assigned = true;
  1232.  
  1233. this->deleteEdge(current->edge());
  1234. this->deleteHalfedge(current);
  1235. this->deleteHalfedge(twin);
  1236.  
  1237. current = next;
  1238. }
  1239.  
  1240. nh0 = prevNewHalfedge->face()->halfedge();
  1241. nh3 = nh0->twin()->next()->twin();
  1242. prevNewHalfedge->next() = nh0;
  1243. nh3->next() = prevNewHalfedge->twin();
  1244. prevNewHalfedge->twin()->vertex() = nh0->vertex();
  1245. newFace->halfedge() = prevNewHalfedge->twin();
  1246.  
  1247. this->deleteVertex(v);
  1248.  
  1249. HalfedgeIter bh0 = mv0->halfedge();
  1250. while(bh0->twin()->vertex() != mv1) {
  1251. bh0 = bh0->twin()->next();
  1252. }
  1253. HalfedgeIter bh1 = bh0->twin();
  1254.  
  1255. HalfedgeIter ref0 = bh1->next()->twin();
  1256. HalfedgeIter oh0 = ref0;
  1257. while(oh0->next()->vertex() != ref0->vertex()) {
  1258. oh0 = oh0->next();
  1259. }
  1260.  
  1261. HalfedgeIter oh1 = bh1->next()->twin()->next()->next()->twin()->next()->twin();
  1262. HalfedgeIter oh2 = bh0->next()->twin()->next()->next()->twin()->next()->twin();
  1263. HalfedgeIter ref3 = bh0->next()->twin();
  1264. HalfedgeIter oh3 = ref3;
  1265.  
  1266. while(oh3->next()->vertex() != ref3->vertex()) {
  1267. oh3 = oh3->next();
  1268. }
  1269.  
  1270. HalfedgeIter oh4 = bh1->next()->next();
  1271. HalfedgeIter oh5 = bh0->next()->next();
  1272. HalfedgeIter oh6 = bh1->next()->twin()->next()->next();
  1273. HalfedgeIter oh7 = bh0->next()->twin()->next()->next();
  1274.  
  1275. EdgeIter etd0 = bh0->edge();
  1276. EdgeIter etd1 = bh0->next()->edge();
  1277. EdgeIter etd2 = bh0->next()->twin()->next()->edge();
  1278. EdgeIter etd3 = bh1->next()->edge();
  1279. EdgeIter etd4 = bh1->next()->twin()->next()->edge();
  1280.  
  1281.  
  1282. this->deleteHalfedge(etd0->halfedge()->twin());
  1283. this->deleteHalfedge(etd0->halfedge());
  1284. this->deleteEdge(etd0);
  1285.  
  1286. this->deleteHalfedge(etd1->halfedge()->twin());
  1287. this->deleteHalfedge(etd1->halfedge());
  1288. this->deleteEdge(etd1);
  1289.  
  1290. this->deleteHalfedge(etd2->halfedge()->twin());
  1291. this->deleteHalfedge(etd2->halfedge());
  1292. this->deleteEdge(etd2);
  1293.  
  1294. this->deleteHalfedge(etd3->halfedge()->twin());
  1295. this->deleteHalfedge(etd3->halfedge());
  1296. this->deleteEdge(etd3);
  1297.  
  1298. this->deleteHalfedge(etd4->halfedge()->twin());
  1299. this->deleteHalfedge(etd4->halfedge());
  1300. this->deleteEdge(etd4);
  1301.  
  1302. this->deleteVertex(mv0);
  1303. this->deleteVertex(mv1);
  1304.  
  1305. EdgeIter ned0 = this->newEdge();
  1306. EdgeIter ned1 = this->newEdge();
  1307.  
  1308. HalfedgeIter nhe0 = this->newHalfedge();
  1309. HalfedgeIter nhe1 = this->newHalfedge();
  1310. HalfedgeIter nhe2 = this->newHalfedge();
  1311. HalfedgeIter nhe3 = this->newHalfedge();
  1312.  
  1313. oh0->next() = nhe1;
  1314. oh1->next() = nhe3;
  1315. oh2->next() = nhe0;
  1316. oh3->next() = nhe2;
  1317.  
  1318. nhe0->next() = oh4;
  1319. nhe0->twin() = nhe1;
  1320. nhe0->vertex() = oh2->twin()->vertex();
  1321. nhe0->edge() = ned0;
  1322. nhe0->face() = oh2->face();
  1323.  
  1324. nhe1->next() = oh7;
  1325. nhe1->twin() = nhe0;
  1326. nhe1->vertex() = oh0->twin()->vertex();//
  1327. nhe1->edge() = ned0;
  1328. nhe1->face() = newFace;
  1329.  
  1330. nhe2->next() = oh6;
  1331. nhe2->twin() = nhe3;
  1332. nhe2->vertex() = oh3->twin()->vertex();//
  1333. nhe2->edge() = ned1;
  1334. nhe2->face() = newFace;
  1335.  
  1336. nhe3->next() = oh5;
  1337. nhe3->twin() = nhe2;
  1338. nhe3->vertex() = oh1->twin()->vertex();
  1339. nhe3->edge() = ned1;
  1340. nhe3->face() = oh1->face();
  1341.  
  1342. oh4->face()->halfedge() = oh4;
  1343. oh2->twin()->face()->halfedge() = oh2->twin();
  1344. oh5->face()->halfedge() = oh5;
  1345. oh1->twin()->face()->halfedge() = oh1->twin();
  1346.  
  1347. ned0->halfedge() = nhe2;
  1348. ned1->halfedge() = nhe1;
  1349.  
  1350. newFace->halfedge() = oh0;
  1351. return newFace;
  1352. }
  1353.  
  1354. FaceIter HalfedgeMesh::bevelFace(FaceIter f) {
  1355. // *** Extra Credit ***
  1356. // TODO This method should replace the face f with an additional, inset face
  1357. // (and ring of faces around it), corresponding to a bevel operation. It
  1358. // should return the new face. NOTE: This method is responsible for updating
  1359. // the *connectivity* of the mesh only---it does not need to update the vertex
  1360. // positions. These positions will be updated in
  1361. // HalfedgeMesh::bevelFaceComputeNewPositions (which you also have to
  1362. // implement!)
  1363.  
  1364.  
  1365. vector<HalfedgeIter> newHalfEdges;
  1366. FaceIter newFace = this->newFace();
  1367.  
  1368. HalfedgeIter h0 = f->halfedge();
  1369. HalfedgeIter current = h0;
  1370.  
  1371. bool assigned = false;
  1372. HalfedgeIter prevNewHalfedge;
  1373. VertexIter prevNewVertex;
  1374. do {
  1375. HalfedgeIter next = current->next();
  1376.  
  1377.  
  1378. HalfedgeIter nh0 = this->newHalfedge();
  1379. HalfedgeIter nh1 = this->newHalfedge();
  1380. HalfedgeIter nh2 = this->newHalfedge();
  1381. HalfedgeIter nh3 = this->newHalfedge();
  1382.  
  1383. VertexIter nv0 = this->newVertex();
  1384.  
  1385. EdgeIter ne0 = this->newEdge();
  1386. EdgeIter ne1 = this->newEdge();
  1387.  
  1388. FaceIter nf0 = this->newFace();
  1389.  
  1390. nh0->next() = nh1;
  1391. nh0->twin() = nh3;
  1392. nh0->vertex() = next->vertex();
  1393. nh0->edge() = ne0;
  1394. nh0->face() = nf0;
  1395.  
  1396. if(assigned) {
  1397. nh1->next() = prevNewHalfedge;
  1398. }
  1399. nh1->twin() = nh2;
  1400. nh1->vertex() = nv0;
  1401. nh1->edge() = ne1;
  1402. nh1->face() = nf0;
  1403.  
  1404. nh2->twin() = nh1;
  1405. if(assigned) {
  1406. nh2->vertex() = prevNewVertex;
  1407. }
  1408. nh2->edge() = ne1;
  1409. nh2->face() = newFace;
  1410.  
  1411. if(assigned) {
  1412. prevNewHalfedge->next() = current;
  1413. prevNewHalfedge->face() = nf0;
  1414. }
  1415.  
  1416. nh3->twin() = nh0;
  1417. nh3->vertex() = nv0;
  1418. nh3->edge() = ne0;
  1419.  
  1420. current->next() = nh0;
  1421. current->face() = nf0;
  1422.  
  1423. nv0->halfedge() = nh3;
  1424.  
  1425. ne0->halfedge() = nh0;
  1426. ne1->halfedge() = nh1;
  1427.  
  1428. if(assigned) {
  1429. nf0->halfedge() = prevNewHalfedge;
  1430. }
  1431.  
  1432.  
  1433. prevNewVertex = nv0;
  1434. prevNewHalfedge = nh3;
  1435.  
  1436. current = next;
  1437. if(assigned) {
  1438. nh1->next()->twin()->next()->twin()->next() = nh2;
  1439. }
  1440. assigned = true;
  1441. }while(current != h0);
  1442. HalfedgeIter nh0 = current->next();
  1443. nh0->next()->next() = prevNewHalfedge;
  1444. nh0->next()->twin()->vertex() = prevNewVertex;
  1445. prevNewHalfedge->next() = current;
  1446. prevNewHalfedge->face() = nh0->face();
  1447. nh0->next()->next()->twin()->next()->twin()->next() = nh0->next()->twin();
  1448. nh0->face()->halfedge() = prevNewHalfedge;
  1449. newFace->halfedge() = nh0->next()->twin();
  1450. this->deleteFace(f);
  1451. return newFace;
  1452. }
  1453.  
  1454.  
  1455. void HalfedgeMesh::bevelFaceComputeNewPositions(
  1456. vector<Vector3D>& originalVertexPositions,
  1457. vector<HalfedgeIter>& newHalfedges, double normalShift,
  1458. double tangentialInset) {
  1459. // *** Extra Credit ***
  1460. // TODO Compute new vertex positions for the vertices of the beveled face.
  1461. //
  1462. // These vertices can be accessed via newHalfedges[i]->vertex()->position for
  1463. // i = 1, ..., newHalfedges.size()-1.
  1464. //
  1465. // The basic strategy here is to loop over the list of outgoing halfedges,
  1466. // and use the preceding and next vertex position from the original mesh
  1467. // (in the originalVertexPositions array) to compute an offset vertex
  1468. // position.
  1469. //
  1470. // Note that there is a 1-to-1 correspondence between halfedges in
  1471. // newHalfedges and vertex positions
  1472. // in orig. So, you can write loops of the form
  1473. //
  1474. // for( int i = 0; i < newHalfedges.size(); hs++ )
  1475. // {
  1476. // Vector3D pi = originalVertexPositions[i]; // get the original vertex
  1477. // position correponding to vertex i
  1478. // }
  1479. //
  1480. // Get the number of vertices in the new polygon
  1481. int N = newHalfedges.size();
  1482.  
  1483. // Assuming we're looking at vertex i, compute the indices
  1484. // of the next and previous elements in the list using
  1485. // modular arithmetic---note that to get the previous index,
  1486. // we can't just subtract 1 because the mod operation in C++
  1487. // doesn't behave quite how you might expect for negative
  1488. // values!
  1489.  
  1490.  
  1491. // Get the actual 3D vertex coordinates at these vertices
  1492.  
  1493. for(int i = 0; i < N; i++) {
  1494. int a = (i+N-1) % N;
  1495. int b = i;
  1496. int c = (i+1) % N;
  1497. Vector3D pa = originalVertexPositions[a];
  1498. Vector3D pb = originalVertexPositions[b];
  1499. Vector3D pc = originalVertexPositions[c];
  1500.  
  1501. if(newHalfedges[a]->vertex()->position == newHalfedges[b]->vertex()->position && newHalfedges[b]->vertex()->position == newHalfedges[c]->vertex()->position && newHalfedges[a]->vertex()->position == newHalfedges[c]->vertex()->position) {
  1502. for(int j = 0; j < N; j++) {
  1503. int d = (j+N-1) % N;
  1504. int e = j;
  1505. int f = (j+1) % N;
  1506. Vector3D pd = originalVertexPositions[d];
  1507. Vector3D pe = originalVertexPositions[e];
  1508. Vector3D pf = originalVertexPositions[f];
  1509. newHalfedges[j]->vertex()->position = pe + (((pd - pe) + (pf - pe)) * .1f);
  1510. }
  1511. }
  1512. newHalfedges[i]->vertex()->position += newHalfedges[i]->twin()->next()->twin()->face()->normal() * normalShift;
  1513. }
  1514.  
  1515. }
  1516.  
  1517. void HalfedgeMesh::bevelVertexComputeNewPositions(
  1518. Vector3D originalVertexPosition, vector<HalfedgeIter>& newHalfedges,
  1519. double tangentialInset) {
  1520. int N = newHalfedges.size();
  1521.  
  1522. double t = .1;
  1523.  
  1524. // Assuming we're looking at vertex i, compute the indices
  1525. // of the next and previous elements in the list using
  1526. // modular arithmetic---note that to get the previous index,
  1527. // we can't just subtract 1 because the mod operation in C++
  1528. // doesn't behave quite how you might expect for negative
  1529. // values!
  1530.  
  1531.  
  1532. // Get the actual 3D vertex coordinates at these vertices
  1533. bool same = true;
  1534. for(int i = 1; i < N; i++) {
  1535. if(!(newHalfedges[i]->vertex()->position == newHalfedges[i-1]->vertex()->position)) {
  1536. same = false;
  1537. break;
  1538. }
  1539. }
  1540. if(same) {
  1541. for(int i = 0; i < N; i++) {
  1542. newHalfedges[i]->vertex()->position = originalVertexPosition;
  1543. }
  1544. }
  1545.  
  1546. for(int i = 0; i < N; i++) {
  1547. Vector3D dto = (originalVertexPosition - newHalfedges[i]->vertex()->position);
  1548. double distanceMagnitude = sqrt(dto.x * dto.x + dto.y * dto.y + dto.z * dto.z);
  1549. Vector3D dtr = (originalVertexPosition - newHalfedges[i]->twin()->vertex()->position);
  1550. double referenceMagnitude = sqrt(dtr.x * dtr.x + dtr.y * dtr.y + dtr.z * dtr.z);
  1551. double t = distanceMagnitude / referenceMagnitude;
  1552. t -= tangentialInset;
  1553. if(t > 1) {
  1554. t = 1;
  1555. }
  1556. if(t < 0) {
  1557. t = 0;
  1558. }
  1559. newHalfedges[i]->vertex()->position = originalVertexPosition * (1 - t) + newHalfedges[i]->twin()->vertex()->position * t;
  1560. }
  1561.  
  1562. }
  1563.  
  1564. void HalfedgeMesh::bevelEdgeComputeNewPositions(
  1565. vector<Vector3D>& originalVertexPositions,
  1566. vector<HalfedgeIter>& newHalfedges, double tangentialInset) {
  1567.  
  1568. // *** Extra Credit ***
  1569. // TODO Compute new vertex positions for the vertices of the beveled edge.
  1570. //
  1571. // These vertices can be accessed via newHalfedges[i]->vertex()->position for
  1572. // i = 1, ..., newHalfedges.size()-1.
  1573. //
  1574. // The basic strategy here is to loop over the list of outgoing halfedges,
  1575. // and use the preceding and next vertex position from the original mesh
  1576. // (in the orig array) to compute an offset vertex position.
  1577. //
  1578. // Note that there is a 1-to-1 correspondence between halfedges in
  1579. // newHalfedges and vertex positions
  1580. // in orig. So, you can write loops of the form
  1581. //
  1582. // for( int i = 0; i < newHalfedges.size(); i++ )
  1583. // {
  1584. // Vector3D pi = originalVertexPositions[i]; // get the original vertex
  1585. // position correponding to vertex i
  1586. // }
  1587. //V
  1588. int N = newHalfedges.size();
  1589. for(int i = 0; i < N; i++) {
  1590. Vector3D originalVertexPosition = originalVertexPositions[i];
  1591. Vector3D dto = (originalVertexPosition - newHalfedges[i]->vertex()->position);
  1592. double distanceMagnitude = sqrt(dto.x * dto.x + dto.y * dto.y + dto.z * dto.z);
  1593. Vector3D dtr = (originalVertexPosition - newHalfedges[i]->twin()->vertex()->position);
  1594. double referenceMagnitude = sqrt(dtr.x * dtr.x + dtr.y * dtr.y + dtr.z * dtr.z);
  1595. double t = distanceMagnitude / referenceMagnitude;
  1596. t -= tangentialInset;
  1597. if(t > 1) {
  1598. t = 1;
  1599. }
  1600. if(t < 0) {
  1601. t = 0;
  1602. }
  1603. newHalfedges[i]->vertex()->position = originalVertexPosition * (1 - t) + newHalfedges[i]->twin()->vertex()->position * t;
  1604. }
  1605. }
  1606.  
  1607. void HalfedgeMesh::splitPolygons(vector<FaceIter>& fcs) {
  1608. for (auto f : fcs) splitPolygon(f);
  1609. }
  1610.  
  1611. void HalfedgeMesh::splitPolygon(FaceIter f) {
  1612. if(f->degree() <= 3) {
  1613. return;
  1614. } else {
  1615. HalfedgeIter h = f->halfedge();
  1616. HalfedgeIter next = h->next();
  1617. HalfedgeIter prev = next;
  1618. while(prev->next() != h) {
  1619. prev = prev->next();
  1620. }
  1621. EdgeIter ne0 = this->newEdge();
  1622. HalfedgeIter nh0 = this->newHalfedge();
  1623. HalfedgeIter nh1 = this->newHalfedge();
  1624. FaceIter nf0 = this->newFace();
  1625. nf0->halfedge() = h;
  1626. ne0->halfedge() = nh0;
  1627. f->halfedge() = next->next();
  1628.  
  1629. nh0->next() = h;
  1630. nh0->twin() = nh1;
  1631. nh0->vertex() = next->next()->vertex();
  1632. nh0->edge() = ne0;
  1633. nh0->face() = nf0;
  1634.  
  1635. nh1->next() = next->next();
  1636. nh1->twin() = nh0;
  1637. nh1->vertex() = h->vertex();
  1638. nh1->edge() = ne0;
  1639. nh1->face() = f;
  1640.  
  1641. prev->next() = nh1;
  1642. next->next() = nh0;
  1643. splitPolygon(f);
  1644.  
  1645. }
  1646. }
  1647.  
  1648. EdgeRecord::EdgeRecord(EdgeIter& _edge) : edge(_edge) {
  1649. // *** Extra Credit ***
  1650. // TODO: (meshEdit)
  1651. // Compute the combined quadric from the edge endpoints.
  1652. // -> Build the 3x3 linear system whose solution minimizes the quadric error
  1653. // associated with these two endpoints.
  1654. // -> Use this system to solve for the optimal position, and store it in
  1655. // EdgeRecord::optimalPoint.
  1656. // -> Also store the cost associated with collapsing this edg in
  1657. // EdgeRecord::Cost.
  1658. Matrix4x4 K_ij = _edge->halfedge()->vertex()->quadric + _edge->halfedge()->twin()->vertex()->quadric;
  1659. cout << "K_ij\n: " << K_ij << endl;
  1660. Matrix3x3 A;
  1661. Vector3D b;
  1662. for(int i = 0; i < 3; i++) {
  1663. for(int j = 0; j < 3; j++) {
  1664. A(i, j) = K_ij(i, j);
  1665. }
  1666. }
  1667. /*
  1668. b.x = K_ij(0, 3);
  1669. b.y = K_ij(1, 3);
  1670. b.z = K_ij(2, 3);
  1671. */
  1672. b.x = K_ij(0, 3);
  1673. b.y = K_ij(1, 3);
  1674. b.z = K_ij(2, 3);
  1675.  
  1676. cout << "A\n: " << A << endl;
  1677. cout << "b: " << b << endl;
  1678. Vector3D x = A.inv() * b;
  1679.  
  1680. cout << "A Det: " << A.det() << endl;
  1681. cout << "A inv:\n " << A.inv() << endl;
  1682. cout << "x:\n " << x << endl;
  1683.  
  1684. Vector4D u;
  1685. u.x = x.x;
  1686. u.y = x.y;
  1687. u.z = x.z;
  1688. u.w = 1;
  1689.  
  1690. Matrix4x4 u_T;
  1691. u_T.zero(0);
  1692. u_T.column(3) = u;
  1693. u_T = u_T.T();
  1694.  
  1695. Vector4D prod = (u_T * K_ij).T().column(3);
  1696. cout << "STUFF: " << endl;
  1697. cout << u_T << "\n" << K_ij << "\n" << u << endl;
  1698.  
  1699. double cost = prod.x * u.x + prod.y * u.y + prod.z * u.z + prod.w * u.w;
  1700. cout << "New Cost: " << cost << endl;
  1701. this->optimalPoint = x;
  1702. this->score = cost;
  1703. _edge->record = *this;
  1704. }
  1705.  
  1706. void MeshResampler::upsample(HalfedgeMesh& mesh)
  1707. // This routine should increase the number of triangles in the mesh using Loop
  1708. // subdivision.
  1709. {
  1710. // TODO: (meshEdit)
  1711. // Compute new positions for all the vertices in the input mesh, using
  1712. // the Loop subdivision rule, and store them in Vertex::newPosition.
  1713. // -> At this point, we also want to mark each vertex as being a vertex of the
  1714. // original mesh.
  1715.  
  1716. /*
  1717. map<HalfedgeIter, double> vertices;
  1718. map<HalfedgeIter, double>::iterator index;
  1719. Vector3D s;
  1720. */
  1721. for (EdgeIter e = mesh.edgesBegin(); e != mesh.edgesEnd(); e++) {
  1722. e->isNew = false;
  1723. if(e->isBoundary()) {
  1724. e->newPosition = e->centroid();
  1725. } else {
  1726. Vector3D s;
  1727. // original
  1728. s += e->halfedge()->vertex()->position * (3.0 / 8.0);
  1729. s += e->halfedge()->twin()->vertex()->position * (3.0 / 8.0);
  1730. s += e->halfedge()->next()->next()->vertex()->position * (1.0 / 8.0);
  1731. s += e->halfedge()->twin()->next()->next()->vertex()->position * (1.0 / 8.0);
  1732. e->newPosition = s;
  1733. }
  1734.  
  1735. }
  1736. //cout << "edge positions done!" << endl;
  1737.  
  1738. for (VertexIter v = mesh.verticesBegin(); v != mesh.verticesEnd(); v++) {
  1739.  
  1740. v->isNew = false; // original
  1741. if(v->isBoundary()) {
  1742. HalfedgeIter h = v->halfedge();
  1743. EdgeIter e1, e2;
  1744. int foundEdges = 0;
  1745. do {
  1746. if(h->edge()->isBoundary()) {
  1747. if(foundEdges == 0) {
  1748. e1 = h->edge();
  1749. } else {
  1750. e2 = h->edge();
  1751. }
  1752. foundEdges++;
  1753. }
  1754. h = h->twin()->next();
  1755. }while(foundEdges < 2);
  1756. v->newPosition = (3.0 / 4.0) * v->position + (1.0 / 8.0) * e1->newPosition + (1.0 / 8.0) * e2->newPosition;
  1757. } else {
  1758. Size n;
  1759. double u;
  1760. Vector3D s;
  1761. n = v->degree();
  1762. if (n == 3)
  1763. u = 3.0 / 16;
  1764. else {
  1765. u = 3.0 / (8 * n);
  1766. }
  1767. //v->getNeighborhood(vertices); // gets all neighboring vertices
  1768. HalfedgeIter firstEdge = v->halfedge();
  1769. HalfedgeIter currentEdge = firstEdge;
  1770. do {
  1771. currentEdge = currentEdge->twin();
  1772. s += currentEdge->vertex()->position;
  1773. currentEdge = currentEdge->next();
  1774. }while(currentEdge != firstEdge);
  1775. v->newPosition = ((1 - (n * u)) * v->position) + (u * s);
  1776. }
  1777. //cout << v->newPosition << endl;
  1778. }
  1779. //cout << "new vertices done!" << endl;
  1780. // -> Next, compute the updated vertex positions associated with edges, and
  1781. // store it in Edge::newPosition.
  1782.  
  1783. // -> Next, we're going to split every edge in the mesh, in any order. For
  1784. // future reference, we're also going to store some information about which
  1785. // subdivided edges come from splitting an edge in the original mesh, and
  1786. // which edges are new, by setting the flat Edge::isNew. Note that in this
  1787. // loop, we only want to iterate over edges of the original mesh.
  1788. // Otherwise, we'll end up splitting edges that we just split (and the
  1789. // loop will never end!)
  1790. /*int num = mesh.nEdges();
  1791. EdgeIter edg = mesh.edgesBegin();
  1792. VertexIter vNew;
  1793. EdgeIter nextEdge;
  1794. for (int i = 0; i < num; i++) {
  1795. if (!vertices.empty())
  1796. vertices.clear();
  1797. // get the next edge NOW!
  1798. nextEdge = edg;
  1799. nextEdge++;
  1800.  
  1801. if (!edg->isNew) {
  1802. vNew = mesh.splitEdge(edg); // not working........?
  1803. vNew->isNew = true;
  1804. vNew->position = edg->newPosition;
  1805. //cout << "edg: " << (*edg).newPosition << endl;
  1806. //cout << "vNew: " << (*vNew).position << endl;
  1807. }
  1808.  
  1809. // mark each edge as new
  1810. vNew->getNeighborhood(vertices);
  1811. for (index = vertices.begin(); index != vertices.end(); index++) { // iterate over neighboring vertices
  1812. (*index).first->edge()->isNew = true;
  1813. }
  1814. edg = nextEdge;
  1815. }*/
  1816. int n = mesh.nEdges();
  1817. EdgeIter e = mesh.edgesBegin();
  1818. for (int i = 0; i < n; i++) {
  1819.  
  1820. // get the next edge NOW!
  1821. EdgeIter nextEdge = e;
  1822. nextEdge++;
  1823.  
  1824. // now, even if splitting the edge deletes it...
  1825. if (!e->isNew) {
  1826. if(!e->isBoundary()) {
  1827. Vector3D newPos = e->newPosition;
  1828. VertexIter newV = mesh.splitEdge(e);
  1829. newV->halfedge()->edge()->isNew = false;
  1830. newV->halfedge()->twin()->next()->edge()->isNew = true;
  1831. newV->halfedge()->twin()->next()->twin()->next()->edge()->isNew = false;
  1832. newV->halfedge()->twin()->next()->twin()->next()->twin()->next()->edge()->isNew = true;
  1833. newV->isNew = true;
  1834. newV->newPosition = newPos;
  1835. } else {
  1836. Vector3D newPos = e->newPosition;
  1837. VertexIter newV = mesh.splitEdge(e);
  1838. HalfedgeIter newEdge = newV->halfedge();
  1839. do {
  1840. if(newEdge->edge()->isBoundary()) {
  1841. newEdge->edge()->isNew = false;
  1842. } else {
  1843. newEdge->edge()->isNew = true;
  1844. }
  1845. newEdge = newEdge->twin()->next();
  1846. }while(newEdge != newV->halfedge());
  1847. newV->isNew = true;
  1848. newV->newPosition = newPos;
  1849. }
  1850.  
  1851. }
  1852.  
  1853. // ...we still have a valid reference to the next edge.
  1854. e = nextEdge;
  1855. }
  1856.  
  1857. ////cout << "edge split done!" << endl;
  1858.  
  1859. for (VertexIter v = mesh.verticesBegin(); v != mesh.verticesEnd(); v++) {
  1860. ////cout << "Position: " << v->position << "New: " << v->isNew << endl;
  1861. }
  1862.  
  1863. for (EdgeIter ed = mesh.edgesBegin(); ed != mesh.edgesEnd(); ed++) {
  1864. ////cout << "Edge: " << ed->centroid() << "New: " << ed->isNew << endl;
  1865. }
  1866. // -> Now flip any new edge that connects an old and new vertex.
  1867. // call edge flip: mesh.flipEdge(edge); if one vertice = new and other is old
  1868.  
  1869. for (EdgeIter ed = mesh.edgesBegin(); ed != mesh.edgesEnd(); ed++) {
  1870. if (ed->halfedge()->vertex()->isNew != ed->halfedge()->twin()->vertex()->isNew && ed->isNew) {
  1871. //cout << "Flipped edge that is " << ed->isNew << "connecting " << ed->halfedge()->vertex()->position << " which is " << ed->halfedge()->vertex()->isNew << " and " << ed->halfedge()->twin()->vertex()->position << " which is " << ed->halfedge()->twin()->vertex()->isNew << endl;
  1872. mesh.flipEdge(ed);
  1873. }
  1874. }
  1875. ////cout << "edge flip done!" << endl;
  1876. // -> Finally, copy the new vertex positions into final Vertex::position.
  1877. // v->position = newposition;
  1878. for (VertexIter vert = mesh.verticesBegin(); vert != mesh.verticesEnd(); vert++) {
  1879. vert->position = vert->newPosition;
  1880. }
  1881. ////cout << "All done!" << endl;
  1882. // Each vertex and edge of the original surface can be associated with a
  1883. // vertex in the new (subdivided) surface.
  1884. // Therefore, our strategy for computing the subdivided vertex locations is to
  1885. // *first* compute the new positions
  1886. // using the connectity of the original (coarse) mesh; navigating this mesh
  1887. // will be much easier than navigating
  1888. // the new subdivided (fine) mesh, which has more elements to traverse. We
  1889. // will then assign vertex positions in
  1890. // the new mesh based on the values we computed for the original mesh.
  1891.  
  1892. // Compute updated positions for all the vertices in the original mesh, using
  1893. // the Loop subdivision rule.
  1894.  
  1895. // Next, compute the updated vertex positions associated with edges.
  1896.  
  1897. // Next, we're going to split every edge in the mesh, in any order. For
  1898. // future
  1899. // reference, we're also going to store some information about which
  1900. // subdivided
  1901. // edges come from splitting an edge in the original mesh, and which edges are
  1902. // new.
  1903. // In this loop, we only want to iterate over edges of the original
  1904. // mesh---otherwise,
  1905. // we'll end up splitting edges that we just split (and the loop will never
  1906. // end!)
  1907.  
  1908. // Finally, flip any new edge that connects an old and new vertex.
  1909.  
  1910. // Copy the updated vertex positions to the subdivided mesh.
  1911. //showError("upsample() not implemented.");
  1912. }
  1913.  
  1914. void MeshResampler::downsample(HalfedgeMesh& mesh) {
  1915. // *** Extra Credit ***
  1916. // TODO: (meshEdit)
  1917. // Compute initial quadrics for each face by simply writing the plane equation
  1918. // for the face in homogeneous coordinates. These quadrics should be stored
  1919. // in Face::quadric
  1920. // -> Compute an initial quadric for each vertex as the sum of the quadrics
  1921. // associated with the incident faces, storing it in Vertex::quadric
  1922. // -> Build a priority queue of edges according to their quadric error cost,
  1923. // i.e., by building an EdgeRecord for each edge and sticking it in the
  1924. // queue.
  1925. // -> Until we reach the target edge budget, collapse the best edge. Remember
  1926. // to remove from the queue any edge that touches the collapsing edge
  1927. // BEFORE it gets collapsed, and add back into the queue any edge touching
  1928. // the collapsed vertex AFTER it's been collapsed. Also remember to assign
  1929. // a quadric to the collapsed vertex, and to pop the collapsed edge off the
  1930. // top of the queue.
  1931. int numFaces = mesh.nFaces();
  1932. for(FaceIter f = mesh.facesBegin(); f != mesh.facesEnd(); f++) {
  1933. Vector4D v;
  1934. Vector3D normal = f->normal();
  1935. Vector3D p = f->halfedge()->vertex()->position;
  1936. v.x = normal.x;
  1937. v.y = normal.y;
  1938. v.z = normal.z;
  1939. v.w = -1 * (normal.x * p.x + normal.y * p.y + normal.z * p.z);
  1940. //cout << "V: " << v << endl;
  1941. Matrix4x4 _K = outer(v, v);
  1942. //cout << "K: \n" << _K << endl;
  1943. f->quadric = _K;
  1944. }
  1945. for(VertexIter v = mesh.verticesBegin(); v != mesh.verticesEnd(); v++) {
  1946. Matrix4x4 K_i;
  1947. HalfedgeIter h = v->halfedge();
  1948. HalfedgeIter current = h;
  1949. do {
  1950. K_i += current->face()->quadric;
  1951. h = h->twin()->next();
  1952. }while(current != h);
  1953. v->quadric = K_i;
  1954. }
  1955. MutablePriorityQueue<EdgeRecord> queue;
  1956. for(EdgeIter e = mesh.edgesBegin(); e != mesh.edgesEnd(); e++) {
  1957. EdgeRecord myRecord(e);
  1958. queue.insert(myRecord);
  1959. }
  1960. while(mesh.nFaces() > numFaces - 1) {
  1961.  
  1962. //cout << "Alive1" << endl;
  1963. EdgeRecord cheapest = queue.top();
  1964. //cout << "Alive1" << endl;
  1965. queue.pop();
  1966. //cout << "Alive1" << endl;
  1967. Matrix4x4 newQuadric = cheapest.edge->halfedge()->vertex()->quadric + cheapest.edge->halfedge()->twin()->vertex()->quadric;
  1968. //cout << "Alive1" << endl;
  1969.  
  1970. HalfedgeIter start = cheapest.edge->halfedge();
  1971. //cout << "Alive1" << endl;
  1972. HalfedgeIter current = start;
  1973. do {
  1974. queue.remove(current->edge()->record);
  1975. current = current->twin()->next();
  1976. //cout << "Finiteness Verification 1" << endl;
  1977. }while(current != start);
  1978.  
  1979. start = cheapest.edge->halfedge()->twin();
  1980. current = start;
  1981. do {
  1982. queue.remove(current->edge()->record);
  1983. current = current->twin()->next();
  1984. //cout << "Finiteness Verification 2" << endl;
  1985. }while(current != start);
  1986. int b4 = mesh.nFaces();
  1987. Vector3D artificialEstimate = (cheapest.edge->halfedge()->vertex()->position + cheapest.edge->halfedge()->twin()->vertex()->position) / 2.;
  1988. cout << "Boutta collapse an edge connecting " << cheapest.edge->halfedge()->vertex()->position << " and " << cheapest.edge->halfedge()->twin()->vertex()->position << endl;
  1989. //cout << "Test: " << cheapest.edge->halfedge()->edge()->halfedge()->vertex()->position << endl;
  1990. VertexIter newV = mesh.collapseEdge(cheapest.edge);
  1991. cout << "Cost: " << cheapest.score << endl;
  1992. //cout << " COLLAPSED ONE EDGE CONNECTING " << cheapest.edge->halfedge()->vertex()->position << " and " << cheapest.edge->halfedge()->twin()->vertex()->position << endl;
  1993. newV->quadric = newQuadric;
  1994. //newV->position = cheapest.optimalPoint;
  1995. newV->position = artificialEstimate;
  1996.  
  1997. start = newV->halfedge();
  1998. current = start;
  1999. do {
  2000. queue.insert(EdgeRecord(current->edge()));
  2001. current = current->twin()->next();
  2002. //cout << "Finiteness Verification 3" << endl;
  2003. }while(current != start);
  2004. //cout << "Finished." << endl;
  2005. }
  2006. }
  2007.  
  2008. void MeshResampler::resample(HalfedgeMesh& mesh) {
  2009. double mean = 0;
  2010. for (EdgeIter e = mesh.edgesBegin(); e != mesh.edgesEnd(); e++) {
  2011. mean += e->length() / mesh.nEdges();
  2012. }
  2013. cout << "average edge: " << mean << endl;
  2014. // Repeat the four main steps for 5 or 6 iterations
  2015. // -> Split edges much longer than the target length (being careful about
  2016. // how the loop is written!)
  2017. int n = mesh.nEdges();
  2018. EdgeIter e = mesh.edgesBegin();
  2019. EdgeIter nextEdge;
  2020. for (int i = 0; i < n; i++) {
  2021. nextEdge = e;
  2022. nextEdge++;
  2023. if (e->length() > 4 * mean / 3) {
  2024. mesh.splitEdge(e);
  2025. }
  2026. e = nextEdge;
  2027. }
  2028. cout << "edge splits done" << endl;
  2029.  
  2030. // -> Collapse edges much shorter than the target length. Here we need to
  2031. // be EXTRA careful about advancing the loop, because many edges may have
  2032. // been destroyed by a collapse (which ones?)
  2033. // iterate over all edges in the mesh
  2034.  
  2035. /*n = mesh.nEdges();
  2036. e = mesh.edgesBegin();
  2037. HalfedgeIter h;
  2038. FaceIter f0, f1;
  2039. for (int i = 0; i < n; i++) {
  2040. nextEdge = e;
  2041. nextEdge++;
  2042. if (e->length() < 4 * mean / 5) {
  2043. cout << "collapse executing!" << endl;
  2044. h = e->halfedge();
  2045. f0 = h->face();
  2046. f1 = h->twin()->face();
  2047. mesh.collapseEdge(e);
  2048. if (f0->degree() == 3)
  2049. i--; // extra edge eliminated
  2050. if (f1->degree() == 3)
  2051. i--; // extra edge eliminated
  2052. }
  2053. e = nextEdge;
  2054. }*/
  2055.  
  2056. cout << "edge collapse done" << endl;
  2057. // -> Now flip each edge if it improves vertex degree
  2058. int before, after;
  2059. int a1, a2, b1, b2;
  2060. for (EdgeIter e = mesh.edgesBegin(); e != mesh.edgesEnd(); e++) {
  2061. a1 = e->halfedge()->vertex()->degree();
  2062. a2 = e->halfedge()->twin()->vertex()->degree();
  2063. b1 = e->halfedge()->next()->next()->vertex()->degree();
  2064. b2 = e->halfedge()->twin()->next()->next()->vertex()->degree();
  2065. before = abs(a1 - 6) + abs(a2 - 6) + abs(b1 - 6) + abs(b2 - 6);
  2066. after = abs(a1 - 7) + abs(a2 - 7) + abs(b1 - 5) + abs(b2 - 5);
  2067. if (before > after) {
  2068. mesh.flipEdge(e);
  2069. }
  2070. }
  2071. cout << "edge flip done" << endl;
  2072.  
  2073. Vector3D dir;
  2074. // -> Finally, apply some tangential smoothing to the vertex positions
  2075. for (VertexIter v = mesh.verticesBegin(); v != mesh.verticesEnd(); v++) {
  2076. v->newPosition = v->neighborhoodCentroid();
  2077. }
  2078.  
  2079. cout << "setting new position done" << endl;
  2080.  
  2081. for (VertexIter vert = mesh.verticesBegin(); vert != mesh.verticesEnd(); vert++) {
  2082. dir = vert->newPosition - vert->position;
  2083. // adjusting v...
  2084. dir = dir - dot(vert->normal(), dir) * vert->normal();
  2085. vert->position = vert->position + 0.2 * dir;
  2086. }
  2087.  
  2088. cout << "moving to new position done" << endl;
  2089. }
  2090.  
  2091. } // namespace CS248
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement