| 1 | // | 
|---|
| 2 | // polysphere.cc | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of the SC Toolkit. | 
|---|
| 10 | // | 
|---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU Library General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful, | 
|---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 19 | // GNU Library General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU Library General Public License | 
|---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to | 
|---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 24 | // | 
|---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7. | 
|---|
| 26 | // | 
|---|
| 27 | // This is loosely based on a program by Jon Leech. | 
|---|
| 28 |  | 
|---|
| 29 | #include <stdlib.h> | 
|---|
| 30 | #include <iostream> | 
|---|
| 31 | #include <math.h> | 
|---|
| 32 |  | 
|---|
| 33 | #include <util/render/polysphere.h> | 
|---|
| 34 | #include <util/render/polygons.h> | 
|---|
| 35 |  | 
|---|
| 36 | using namespace std; | 
|---|
| 37 | using namespace sc; | 
|---|
| 38 |  | 
|---|
| 39 | static inline void | 
|---|
| 40 | switch2(int& i, int& j) | 
|---|
| 41 | { | 
|---|
| 42 | int tmp = i; | 
|---|
| 43 | i = j; | 
|---|
| 44 | j = tmp; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | class edge { | 
|---|
| 48 | int vertex[2]; | 
|---|
| 49 | public: | 
|---|
| 50 | void set(int i, int j) { | 
|---|
| 51 | if (i == j) { | 
|---|
| 52 | ExEnv::errn() << "edge: bad nodes" << endl; | 
|---|
| 53 | abort(); | 
|---|
| 54 | } | 
|---|
| 55 | vertex[0] = i; vertex[1] = j; | 
|---|
| 56 | } | 
|---|
| 57 | int& v(int i) { return vertex[i]; } | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | class triangle { | 
|---|
| 61 | int edge_[3]; | 
|---|
| 62 | int orientation[3]; | 
|---|
| 63 | public: | 
|---|
| 64 | void set(int e0, int o0, | 
|---|
| 65 | int e1, int o1, | 
|---|
| 66 | int e2, int o2) { | 
|---|
| 67 | edge_[0] = e0; orientation[0] = o0; | 
|---|
| 68 | edge_[1] = e1; orientation[1] = o1; | 
|---|
| 69 | edge_[2] = e2; orientation[2] = o2; | 
|---|
| 70 | } | 
|---|
| 71 | void set(int e0, int o0, | 
|---|
| 72 | int e1, int o1, | 
|---|
| 73 | int e2, int o2, edge* edges) { | 
|---|
| 74 | edge& E0 = edges[e0]; | 
|---|
| 75 | edge& E1 = edges[e1]; | 
|---|
| 76 | edge& E2 = edges[e2]; | 
|---|
| 77 |  | 
|---|
| 78 | if (  ((o0==0? E0.v(1): E0.v(0)) != (o1==0? E1.v(0): E1.v(1))) | 
|---|
| 79 | ||((o1==0? E1.v(1): E1.v(0)) != (o2==0? E2.v(0): E2.v(1))) | 
|---|
| 80 | ||((o2==0? E2.v(1): E2.v(0)) != (o0==0? E0.v(0): E0.v(1)))) { | 
|---|
| 81 | ExEnv::errn() << "triangle: bad edges or orientations" << endl; | 
|---|
| 82 | abort(); | 
|---|
| 83 | } | 
|---|
| 84 | edge_[0] = e0; orientation[0] = o0; | 
|---|
| 85 | edge_[1] = e1; orientation[1] = o1; | 
|---|
| 86 | edge_[2] = e2; orientation[2] = o2; | 
|---|
| 87 | } | 
|---|
| 88 | int& e(int i) { return edge_[i]; } | 
|---|
| 89 | int& o(int i) { return orientation[i]; } | 
|---|
| 90 | }; | 
|---|
| 91 |  | 
|---|
| 92 | static void | 
|---|
| 93 | subdivide(int level, int maxlevel, | 
|---|
| 94 | edge* edges, triangle* triangles, | 
|---|
| 95 | int nv, int ne, int nf, const Ref<RenderedPolygons>& poly) | 
|---|
| 96 | { | 
|---|
| 97 | int i; | 
|---|
| 98 |  | 
|---|
| 99 | if (level >= maxlevel) { | 
|---|
| 100 | // fill in poly | 
|---|
| 101 | for (i=0; i<nf; i++) { | 
|---|
| 102 | edge& e0 = edges[triangles[i].e(0)]; | 
|---|
| 103 | edge& e1 = edges[triangles[i].e(1)]; | 
|---|
| 104 | int v0 = (triangles[i].o(0)==0? e0.v(0): e0.v(1)); | 
|---|
| 105 | int v1 = (triangles[i].o(0)==0? e0.v(1): e0.v(0)); | 
|---|
| 106 | int v2 = (triangles[i].o(1)==0? e1.v(1): e1.v(0)); | 
|---|
| 107 | if (v0 == v1 || v1 == v2 || v2 == v0) { | 
|---|
| 108 | ExEnv::errn() << "bad triangle" << endl; | 
|---|
| 109 | abort(); | 
|---|
| 110 | } | 
|---|
| 111 | poly->set_face(i, v0, v1, v2); | 
|---|
| 112 | } | 
|---|
| 113 | return; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | int nv2 = nv + ne; | 
|---|
| 117 | int ne2 = 3*nf + 2*ne; | 
|---|
| 118 | int nf2 = 4*nf; | 
|---|
| 119 |  | 
|---|
| 120 | edge* newedges = new edge[ne2]; | 
|---|
| 121 | triangle* newtriangles = new triangle[nf2]; | 
|---|
| 122 |  | 
|---|
| 123 | // split the edges and compute the new points | 
|---|
| 124 | int ipoint = nv; | 
|---|
| 125 | int inewedge = 0; | 
|---|
| 126 | for (i=0; i<ne; i++) { | 
|---|
| 127 | // split the edges | 
|---|
| 128 | newedges[inewedge].v(0) = edges[i].v(0); | 
|---|
| 129 | newedges[inewedge].v(1) = ipoint; | 
|---|
| 130 | inewedge++; | 
|---|
| 131 | newedges[inewedge].v(0) = ipoint; | 
|---|
| 132 | newedges[inewedge].v(1) = edges[i].v(1); | 
|---|
| 133 | inewedge++; | 
|---|
| 134 |  | 
|---|
| 135 | // find the midpoint and normalize to unit length | 
|---|
| 136 | double v[3]; | 
|---|
| 137 | v[0] = poly->vertex(edges[i].v(0), 0) + poly->vertex(edges[i].v(1), 0); | 
|---|
| 138 | v[1] = poly->vertex(edges[i].v(0), 1) + poly->vertex(edges[i].v(1), 1); | 
|---|
| 139 | v[2] = poly->vertex(edges[i].v(0), 2) + poly->vertex(edges[i].v(1), 2); | 
|---|
| 140 | double norm = 1.0/sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); | 
|---|
| 141 | v[0] *= norm; | 
|---|
| 142 | v[1] *= norm; | 
|---|
| 143 | v[2] *= norm; | 
|---|
| 144 | poly->set_vertex(ipoint, v[0], v[1], v[2]); | 
|---|
| 145 |  | 
|---|
| 146 | ipoint++; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | // form the new triangles and the edges crossing the old triangles | 
|---|
| 150 | int inewtriangle = 0; | 
|---|
| 151 | for (i=0; i<nf; i++) { | 
|---|
| 152 | // the edges of the old triangles | 
|---|
| 153 | int e0 = triangles[i].e(0); | 
|---|
| 154 | int e1 = triangles[i].e(1); | 
|---|
| 155 | int e2 = triangles[i].e(2); | 
|---|
| 156 |  | 
|---|
| 157 | // the orientations of the old triangles | 
|---|
| 158 | int o0 = triangles[i].o(0); | 
|---|
| 159 | int o1 = triangles[i].o(1); | 
|---|
| 160 | int o2 = triangles[i].o(2); | 
|---|
| 161 |  | 
|---|
| 162 | // find the points in the centers of the old triangle's edges | 
|---|
| 163 | int p0 = nv + e0; | 
|---|
| 164 | int p1 = nv + e1; | 
|---|
| 165 | int p2 = nv + e2; | 
|---|
| 166 |  | 
|---|
| 167 | // the edges obtained by dividing the old edges | 
|---|
| 168 | int e00 = 2*e0; | 
|---|
| 169 | int e01 = 2*e0 + 1; | 
|---|
| 170 | int e10 = 2*e1; | 
|---|
| 171 | int e11 = 2*e1 + 1; | 
|---|
| 172 | int e20 = 2*e2; | 
|---|
| 173 | int e21 = 2*e2 + 1; | 
|---|
| 174 |  | 
|---|
| 175 | // connect the points to form new edges | 
|---|
| 176 | int ne0 = inewedge; newedges[inewedge++].set(p0, p1); | 
|---|
| 177 | int ne1 = inewedge; newedges[inewedge++].set(p1, p2); | 
|---|
| 178 | int ne2 = inewedge; newedges[inewedge++].set(p2, p0); | 
|---|
| 179 |  | 
|---|
| 180 | // if original edges had reversed orientation, switch | 
|---|
| 181 | // ordering of split edges | 
|---|
| 182 | if (o0) switch2(e00, e01); | 
|---|
| 183 | if (o1) switch2(e10, e11); | 
|---|
| 184 | if (o2) switch2(e20, e21); | 
|---|
| 185 |  | 
|---|
| 186 | // form the new triangles | 
|---|
| 187 | newtriangles[inewtriangle++].set(e00, o0, | 
|---|
| 188 | ne2, 1, | 
|---|
| 189 | e21, o2, | 
|---|
| 190 | newedges); | 
|---|
| 191 | newtriangles[inewtriangle++].set(e10, o1, | 
|---|
| 192 | ne0, 1, | 
|---|
| 193 | e01, o0, | 
|---|
| 194 | newedges); | 
|---|
| 195 | newtriangles[inewtriangle++].set(e20, o2, | 
|---|
| 196 | ne1, 1, | 
|---|
| 197 | e11, o1, | 
|---|
| 198 | newedges); | 
|---|
| 199 | newtriangles[inewtriangle++].set(ne0, 0, | 
|---|
| 200 | ne1, 0, | 
|---|
| 201 | ne2, 0, | 
|---|
| 202 | newedges); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | subdivide(level+1,maxlevel,newedges,newtriangles,nv2,ne2,nf2,poly); | 
|---|
| 206 | delete[] newedges; | 
|---|
| 207 | delete[] newtriangles; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | void | 
|---|
| 211 | sc::polysphere(int level, const Ref<RenderedPolygons>& poly) | 
|---|
| 212 | { | 
|---|
| 213 | int i; | 
|---|
| 214 |  | 
|---|
| 215 | // compute the number of vertices, edges, and faces | 
|---|
| 216 | int nf = 8; | 
|---|
| 217 | int ne = 12; | 
|---|
| 218 | int nv = 6; | 
|---|
| 219 | for (i=2; i<=level; i++) { | 
|---|
| 220 | nv = nv + ne; | 
|---|
| 221 | ne = 3*nf + 2*ne; | 
|---|
| 222 | nf = 4*nf; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | poly->initialize(nv,nf); | 
|---|
| 226 |  | 
|---|
| 227 | // Fill in the first level, an octahedron. | 
|---|
| 228 |  | 
|---|
| 229 | // the vertices | 
|---|
| 230 | poly->set_vertex(0, 1.0, 0.0, 0.0); | 
|---|
| 231 | poly->set_vertex(1,-1.0, 0.0, 0.0); | 
|---|
| 232 | poly->set_vertex(2, 0.0, 1.0, 0.0); | 
|---|
| 233 | poly->set_vertex(3, 0.0,-1.0, 0.0); | 
|---|
| 234 | poly->set_vertex(4, 0.0, 0.0, 1.0); | 
|---|
| 235 | poly->set_vertex(5, 0.0, 0.0,-1.0); | 
|---|
| 236 |  | 
|---|
| 237 | edge *edges = new edge[12]; | 
|---|
| 238 | edges[0].set(0, 5); | 
|---|
| 239 | edges[1].set(0, 2); | 
|---|
| 240 | edges[2].set(0, 4); | 
|---|
| 241 | edges[3].set(0, 3); | 
|---|
| 242 | edges[4].set(1, 5); | 
|---|
| 243 | edges[5].set(1, 2); | 
|---|
| 244 | edges[6].set(1, 4); | 
|---|
| 245 | edges[7].set(1, 3); | 
|---|
| 246 | edges[8].set(5, 2); | 
|---|
| 247 | edges[9].set(2, 4); | 
|---|
| 248 | edges[10].set(4, 3); | 
|---|
| 249 | edges[11].set(3, 5); | 
|---|
| 250 |  | 
|---|
| 251 | triangle *triangles = new triangle[8]; | 
|---|
| 252 | triangles[0].set(0, 0, 8, 0, 1, 1); | 
|---|
| 253 | triangles[1].set(1, 0, 9, 0, 2, 1); | 
|---|
| 254 | triangles[2].set(2, 0,10, 0, 3, 1); | 
|---|
| 255 | triangles[3].set(3, 0,11, 0, 0, 1); | 
|---|
| 256 | triangles[4].set(4, 0,11, 1, 7, 1); | 
|---|
| 257 | triangles[5].set(5, 0, 8, 1, 4, 1); | 
|---|
| 258 | triangles[6].set(6, 0, 9, 1, 5, 1); | 
|---|
| 259 | triangles[7].set(7, 0,10, 1, 6, 1); | 
|---|
| 260 |  | 
|---|
| 261 | nf = 8; | 
|---|
| 262 | ne = 12; | 
|---|
| 263 | nv = 6; | 
|---|
| 264 |  | 
|---|
| 265 | subdivide(1,level,edges,triangles,nv,ne,nf,poly); | 
|---|
| 266 |  | 
|---|
| 267 | delete[] edges; | 
|---|
| 268 | delete[] triangles; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 272 |  | 
|---|
| 273 | // Local Variables: | 
|---|
| 274 | // mode: c++ | 
|---|
| 275 | // c-file-style: "CLJ" | 
|---|
| 276 | // End: | 
|---|