| 1 | // |
| 2 | // Copyright (c) 2009-2010 Mikko Mononen [email protected] |
| 3 | // |
| 4 | // This software is provided 'as-is', without any express or implied |
| 5 | // warranty. In no event will the authors be held liable for any damages |
| 6 | // arising from the use of this software. |
| 7 | // Permission is granted to anyone to use this software for any purpose, |
| 8 | // including commercial applications, and to alter it and redistribute it |
| 9 | // freely, subject to the following restrictions: |
| 10 | // 1. The origin of this software must not be misrepresented; you must not |
| 11 | // claim that you wrote the original software. If you use this software |
| 12 | // in a product, an acknowledgment in the product documentation would be |
| 13 | // appreciated but is not required. |
| 14 | // 2. Altered source versions must be plainly marked as such, and must not be |
| 15 | // misrepresented as being the original software. |
| 16 | // 3. This notice may not be removed or altered from any source distribution. |
| 17 | // |
| 18 | |
| 19 | #include "ChunkyTriMesh.h" |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <math.h> |
| 23 | |
| 24 | struct BoundsItem |
| 25 | { |
| 26 | float bmin[2]; |
| 27 | float bmax[2]; |
| 28 | int i; |
| 29 | }; |
| 30 | |
| 31 | static int compareItemX(const void* va, const void* vb) |
| 32 | { |
| 33 | const BoundsItem* a = (const BoundsItem*)va; |
| 34 | const BoundsItem* b = (const BoundsItem*)vb; |
| 35 | if (a->bmin[0] < b->bmin[0]) |
| 36 | return -1; |
| 37 | if (a->bmin[0] > b->bmin[0]) |
| 38 | return 1; |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static int compareItemY(const void* va, const void* vb) |
| 43 | { |
| 44 | const BoundsItem* a = (const BoundsItem*)va; |
| 45 | const BoundsItem* b = (const BoundsItem*)vb; |
| 46 | if (a->bmin[1] < b->bmin[1]) |
| 47 | return -1; |
| 48 | if (a->bmin[1] > b->bmin[1]) |
| 49 | return 1; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static void calcExtends(const BoundsItem* items, const int /*nitems*/, |
| 54 | const int imin, const int imax, |
| 55 | float* bmin, float* bmax) |
| 56 | { |
| 57 | bmin[0] = items[imin].bmin[0]; |
| 58 | bmin[1] = items[imin].bmin[1]; |
| 59 | |
| 60 | bmax[0] = items[imin].bmax[0]; |
| 61 | bmax[1] = items[imin].bmax[1]; |
| 62 | |
| 63 | for (int i = imin+1; i < imax; ++i) |
| 64 | { |
| 65 | const BoundsItem& it = items[i]; |
| 66 | if (it.bmin[0] < bmin[0]) bmin[0] = it.bmin[0]; |
| 67 | if (it.bmin[1] < bmin[1]) bmin[1] = it.bmin[1]; |
| 68 | |
| 69 | if (it.bmax[0] > bmax[0]) bmax[0] = it.bmax[0]; |
| 70 | if (it.bmax[1] > bmax[1]) bmax[1] = it.bmax[1]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | inline int longestAxis(float x, float y) |
| 75 | { |
| 76 | return y > x ? 1 : 0; |
| 77 | } |
| 78 | |
| 79 | static void subdivide(BoundsItem* items, int nitems, int imin, int imax, int trisPerChunk, |
| 80 | int& curNode, rcChunkyTriMeshNode* nodes, const int maxNodes, |
| 81 | int& curTri, int* outTris, const int* inTris) |
| 82 | { |
| 83 | int inum = imax - imin; |
| 84 | int icur = curNode; |
| 85 | |
| 86 | if (curNode >= maxNodes) |
| 87 | return; |
| 88 | |
| 89 | rcChunkyTriMeshNode& node = nodes[curNode++]; |
| 90 | |
| 91 | if (inum <= trisPerChunk) |
| 92 | { |
| 93 | // Leaf |
| 94 | calcExtends(items, nitems, imin, imax, node.bmin, node.bmax); |
| 95 | |
| 96 | // Copy triangles. |
| 97 | node.i = curTri; |
| 98 | node.n = inum; |
| 99 | |
| 100 | for (int i = imin; i < imax; ++i) |
| 101 | { |
| 102 | const int* src = &inTris[items[i].i*3]; |
| 103 | int* dst = &outTris[curTri*3]; |
| 104 | curTri++; |
| 105 | dst[0] = src[0]; |
| 106 | dst[1] = src[1]; |
| 107 | dst[2] = src[2]; |
| 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | // Split |
| 113 | calcExtends(items, nitems, imin, imax, node.bmin, node.bmax); |
| 114 | |
| 115 | int axis = longestAxis(node.bmax[0] - node.bmin[0], |
| 116 | node.bmax[1] - node.bmin[1]); |
| 117 | |
| 118 | if (axis == 0) |
| 119 | { |
| 120 | // Sort along x-axis |
| 121 | qsort(items+imin, static_cast<size_t>(inum), sizeof(BoundsItem), compareItemX); |
| 122 | } |
| 123 | else if (axis == 1) |
| 124 | { |
| 125 | // Sort along y-axis |
| 126 | qsort(items+imin, static_cast<size_t>(inum), sizeof(BoundsItem), compareItemY); |
| 127 | } |
| 128 | |
| 129 | int isplit = imin+inum/2; |
| 130 | |
| 131 | // Left |
| 132 | subdivide(items, nitems, imin, isplit, trisPerChunk, curNode, nodes, maxNodes, curTri, outTris, inTris); |
| 133 | // Right |
| 134 | subdivide(items, nitems, isplit, imax, trisPerChunk, curNode, nodes, maxNodes, curTri, outTris, inTris); |
| 135 | |
| 136 | int iescape = curNode - icur; |
| 137 | // Negative index means escape. |
| 138 | node.i = -iescape; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | bool rcCreateChunkyTriMesh(const float* verts, const int* tris, int ntris, |
| 143 | int trisPerChunk, rcChunkyTriMesh* cm) |
| 144 | { |
| 145 | int nchunks = (ntris + trisPerChunk-1) / trisPerChunk; |
| 146 | |
| 147 | cm->nodes = new rcChunkyTriMeshNode[nchunks*4]; |
| 148 | if (!cm->nodes) |
| 149 | return false; |
| 150 | |
| 151 | cm->tris = new int[ntris*3]; |
| 152 | if (!cm->tris) |
| 153 | return false; |
| 154 | |
| 155 | cm->ntris = ntris; |
| 156 | |
| 157 | // Build tree |
| 158 | BoundsItem* items = new BoundsItem[ntris]; |
| 159 | if (!items) |
| 160 | return false; |
| 161 | |
| 162 | for (int i = 0; i < ntris; i++) |
| 163 | { |
| 164 | const int* t = &tris[i*3]; |
| 165 | BoundsItem& it = items[i]; |
| 166 | it.i = i; |
| 167 | // Calc triangle XZ bounds. |
| 168 | it.bmin[0] = it.bmax[0] = verts[t[0]*3+0]; |
| 169 | it.bmin[1] = it.bmax[1] = verts[t[0]*3+2]; |
| 170 | for (int j = 1; j < 3; ++j) |
| 171 | { |
| 172 | const float* v = &verts[t[j]*3]; |
| 173 | if (v[0] < it.bmin[0]) it.bmin[0] = v[0]; |
| 174 | if (v[2] < it.bmin[1]) it.bmin[1] = v[2]; |
| 175 | |
| 176 | if (v[0] > it.bmax[0]) it.bmax[0] = v[0]; |
| 177 | if (v[2] > it.bmax[1]) it.bmax[1] = v[2]; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | int curTri = 0; |
| 182 | int curNode = 0; |
| 183 | subdivide(items, ntris, 0, ntris, trisPerChunk, curNode, cm->nodes, nchunks*4, curTri, cm->tris, tris); |
| 184 | |
| 185 | delete [] items; |
| 186 | |
| 187 | cm->nnodes = curNode; |
| 188 | |
| 189 | // Calc max tris per node. |
| 190 | cm->maxTrisPerChunk = 0; |
| 191 | for (int i = 0; i < cm->nnodes; ++i) |
| 192 | { |
| 193 | rcChunkyTriMeshNode& node = cm->nodes[i]; |
| 194 | const bool isLeaf = node.i >= 0; |
| 195 | if (!isLeaf) continue; |
| 196 | if (node.n > cm->maxTrisPerChunk) |
| 197 | cm->maxTrisPerChunk = node.n; |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | inline bool checkOverlapRect(const float amin[2], const float amax[2], |
| 205 | const float bmin[2], const float bmax[2]) |
| 206 | { |
| 207 | bool overlap = true; |
| 208 | overlap = (amin[0] > bmax[0] || amax[0] < bmin[0]) ? false : overlap; |
| 209 | overlap = (amin[1] > bmax[1] || amax[1] < bmin[1]) ? false : overlap; |
| 210 | return overlap; |
| 211 | } |
| 212 | |
| 213 | int rcGetChunksOverlappingRect(const rcChunkyTriMesh* cm, |
| 214 | float bmin[2], float bmax[2], |
| 215 | int* ids, const int maxIds) |
| 216 | { |
| 217 | // Traverse tree |
| 218 | int i = 0; |
| 219 | int n = 0; |
| 220 | while (i < cm->nnodes) |
| 221 | { |
| 222 | const rcChunkyTriMeshNode* node = &cm->nodes[i]; |
| 223 | const bool overlap = checkOverlapRect(bmin, bmax, node->bmin, node->bmax); |
| 224 | const bool isLeafNode = node->i >= 0; |
| 225 | |
| 226 | if (isLeafNode && overlap) |
| 227 | { |
| 228 | if (n < maxIds) |
| 229 | { |
| 230 | ids[n] = i; |
| 231 | n++; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (overlap || isLeafNode) |
| 236 | i++; |
| 237 | else |
| 238 | { |
| 239 | const int escapeIndex = -node->i; |
| 240 | i += escapeIndex; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return n; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | static bool checkOverlapSegment(const float p[2], const float q[2], |
| 250 | const float bmin[2], const float bmax[2]) |
| 251 | { |
| 252 | static const float EPSILON = 1e-6f; |
| 253 | |
| 254 | float tmin = 0; |
| 255 | float tmax = 1; |
| 256 | float d[2]; |
| 257 | d[0] = q[0] - p[0]; |
| 258 | d[1] = q[1] - p[1]; |
| 259 | |
| 260 | for (int i = 0; i < 2; i++) |
| 261 | { |
| 262 | if (fabsf(d[i]) < EPSILON) |
| 263 | { |
| 264 | // Ray is parallel to slab. No hit if origin not within slab |
| 265 | if (p[i] < bmin[i] || p[i] > bmax[i]) |
| 266 | return false; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | // Compute intersection t value of ray with near and far plane of slab |
| 271 | float ood = 1.0f / d[i]; |
| 272 | float t1 = (bmin[i] - p[i]) * ood; |
| 273 | float t2 = (bmax[i] - p[i]) * ood; |
| 274 | if (t1 > t2) { float tmp = t1; t1 = t2; t2 = tmp; } |
| 275 | if (t1 > tmin) tmin = t1; |
| 276 | if (t2 < tmax) tmax = t2; |
| 277 | if (tmin > tmax) return false; |
| 278 | } |
| 279 | } |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | int rcGetChunksOverlappingSegment(const rcChunkyTriMesh* cm, |
| 284 | float p[2], float q[2], |
| 285 | int* ids, const int maxIds) |
| 286 | { |
| 287 | // Traverse tree |
| 288 | int i = 0; |
| 289 | int n = 0; |
| 290 | while (i < cm->nnodes) |
| 291 | { |
| 292 | const rcChunkyTriMeshNode* node = &cm->nodes[i]; |
| 293 | const bool overlap = checkOverlapSegment(p, q, node->bmin, node->bmax); |
| 294 | const bool isLeafNode = node->i >= 0; |
| 295 | |
| 296 | if (isLeafNode && overlap) |
| 297 | { |
| 298 | if (n < maxIds) |
| 299 | { |
| 300 | ids[n] = i; |
| 301 | n++; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if (overlap || isLeafNode) |
| 306 | i++; |
| 307 | else |
| 308 | { |
| 309 | const int escapeIndex = -node->i; |
| 310 | i += escapeIndex; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return n; |
| 315 | } |