# Mesh Simplification / MeshSimplification / decimation.cpp

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283``` ```#include "decimation.h" //============================================================ void save_last_boundary(vector& boundary) { int sz; sz = boundary.size(); last_boundary.clear(); for (int i = 0 ; i < sz; i++) last_boundary.push_back(*boundary[i]); } void save_last_decimation(vector& boundary) { int sz; sz = boundary.size(); last_decimated.clear(); for (int i = 0 ; i < sz; i++) last_decimated.push_back(*boundary[i]); } //============================================================ int decimate(HE_vert* vert, map& del_verts, bool save_boundary) { vector boundary; vector new_faces; vector, int> > new_faces_points; int ret; int error_count = 0; ret = construct_boundary(vert, boundary); if (save_boundary) { save_last_decimation(boundary); } ret = delete_vertex(vert, del_verts); ret = retriangulate_boundary(vert, boundary, &new_faces, &new_faces_points); vector::iterator nfiter = new_faces.begin(); vector, int> >::iterator nfpiter = new_faces_points.begin(); for (; nfiter != new_faces.end() && nfpiter != new_faces_points.end(); nfiter++, nfpiter++) { if (!test_face(*nfiter, nfpiter->first.first, nfpiter->first.second, nfpiter->second)) { error_count--; } } if (error_count < 0) { return error_count; } return ret; } //============================================================ int delete_face(HE_face* f) { HE_edge* e = f->edge; HE_edge* en = e->next; for (int i = 0; i < 3; i++) { untie_edge(e, UVRT | UPAR); // untie from verts and pairs dele(e); e = en; en = en->next; } delf(f); return 0; } //============================================================ int delete_vertex(HE_vert* vert, map& del_verts) { HE_edge *e = vert->edge, *e_tmp; static map del_faces; map::iterator it; // delete faces around the vertex del_faces[e->face] = 1; for (e_tmp = e->next->pair; e_tmp!=e; e_tmp = e_tmp->next->pair) { if (e_tmp->vert != vert) { throw ITERATION_ERROR; } del_faces[e_tmp->face] = 1; } it = del_faces.begin(); for (;it != del_faces.end(); it++) { it->second = !delete_face(it->first); } del_faces.clear(); // delete vertex del_verts[vert->index] = vert; return 1; } //============================================================ int construct_boundary(HE_vert* vert, vector& boundary) { HE_edge *e = vert->edge, *e_tmp; if (e->vert != vert) { return ITERATION_ERROR; } // find shortest edge // construct boundary e_tmp = e->next->pair; boundary.push_back(e->vert2); for (; e_tmp != e; e_tmp = e_tmp->next->pair) { boundary.push_back(e_tmp->vert2); } return boundary.size(); } //============================================================ int retriangulate_boundary(HE_vert* vert, vector& boundary, vector* new_faces, vector, int> >* new_faces_points) { pair e1, e2, e3; int sz = boundary.size(); int last = sz - 1, s_last = sz - 2; HE_face* f; if (sz < 3) { return 0; } // retriangulate one triangle f = create_face(boundary[0]->index, boundary[last]->index, boundary[s_last]->index); if (new_faces != NULL) { new_faces->push_back(f); } if (new_faces_points != NULL) { pair p; pair, int> pp; p.first = boundary[0]->index; p.second = boundary[last]->index; pp.first = p; pp.second = boundary[s_last]->index; new_faces_points->push_back(pp); } boundary.pop_back(); if (sz == 3) { return 1; } else { return retriangulate_boundary(vert, boundary) + 1; } } //============================================================ double local_curvature(HE_vert* vert, int type) { double val = 0.0; switch (type) { case NORMAL_DOT_PRODUCT: val = normal_dot_product(vert); break; case AVERAGE_POINT: val = dist_to_avg_pt(vert); break; default: val = normal_dot_product(vert); break; } return val; } //============================================================ double normal_dot_product(HE_vert* vert) { HE_edge *e = vert->edge; Vector3 pv, p1, p2, n1, n2; double i = 0.0, small_len_2 = 0.0; pv = getVector(vert); p1 = getVector(e->pair->vert); p2 = getVector(e->next->vert); e = e->next->pair; n1 = (p2^p1).unit(); do { if (e->pair == NULL || e->pair->next == NULL) { throw ITERATION_ERROR; } p1 = p2; p2 = getVector(e->next->vert); e = e->next->pair; n2 = (p2^p1).unit(); small_len_2 = n1*n2; n1 = n2; } while (e != vert->edge); return small_len_2; } //============================================================ double dist_to_avg_pt(HE_vert* vert) { HE_edge *e = vert->edge; Vector3 v, vp, p, avgp(0.0, 0.0, 0.0); double i = 0.0, avg_len = 0.0, curviture; vp = getVector(vert); do { p = getVector(e->pair->vert); avgp += p; avg_len += (p - vp).length2(); i+=1.0; if (e->pair == NULL || e->pair->next == NULL) { throw ITERATION_ERROR; } e = e->pair->next->next; } while (e != vert->edge); avg_len /= i; avgp *= (1.0/i); v = vp - avgp; curviture = v.length2(); return (curviture*curviture)/avg_len; } //============================================================ map del_verts; void clean_decimation_structures() { map::iterator j = del_verts.begin(); for (; j != del_verts.end(); j++) { delv(j->second); } } int decimate(map::iterator& i, double threshold, int test_type, bool save_boundary) { if (decimation_test(i, threshold, test_type)) { decimate(i->second, del_verts, save_boundary); return 1; } return 0; } int decimation_test(map::iterator& i, double threshold, int test_type) { double test = 0, tc2 = threshold; tc2*=tc2; test = local_curvature(i->second, test_type); return test < tc2; } void decimate(double threshold, int test_type, bool save_boundary) { int total = 0, successful = 0, failed = 0, invalid1 = 0, invalid2 = 0; double res = 0; map::iterator i = verts.begin(); cout << "Applying vertex decimation algorithm... " << endl; for (; i != verts.end(); i++) { if (del_verts.count(i->first)) continue; try { total += decimate(i, threshold, test_type, save_boundary); } catch (int i) { if (i == ITERATION_ERROR) { cout << "Iteration error occured in local_curvature() function." << endl; break; } } } clean_decimation_structures(); cout << "Vertices decimated " << total << " of " << nv << endl; } ```