Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "Brush.h" 21 : 22 : #include "lib/debug.h" 23 : #include "maths/BoundingBoxAligned.h" 24 : #include "maths/Frustum.h" 25 : #include "maths/Plane.h" 26 : 27 5 : CBrush::CBrush() = default; 28 : 29 : /////////////////////////////////////////////////////////////////////////////// 30 : // Convert the given bounds into a brush 31 3 : CBrush::CBrush(const CBoundingBoxAligned& bounds) 32 : { 33 3 : m_Vertices.resize(8); 34 : 35 27 : for(size_t i = 0; i < 8; ++i) 36 : { 37 24 : m_Vertices[i][0] = bounds[(i & 1) ? 1 : 0][0]; // X 38 24 : m_Vertices[i][1] = bounds[(i & 2) ? 1 : 0][1]; // Y 39 24 : m_Vertices[i][2] = bounds[(i & 4) ? 1 : 0][2]; // Z 40 : } 41 : 42 : // construct cube face indices, 5 vertex indices per face (start vertex included twice) 43 : 44 3 : m_Faces.resize(30); 45 : 46 3 : m_Faces[0] = 0; m_Faces[1] = 1; m_Faces[2] = 3; m_Faces[3] = 2; m_Faces[4] = 0; // Z = min 47 3 : m_Faces[5] = 4; m_Faces[6] = 5; m_Faces[7] = 7; m_Faces[8] = 6; m_Faces[9] = 4; // Z = max 48 : 49 3 : m_Faces[10] = 0; m_Faces[11] = 2; m_Faces[12] = 6; m_Faces[13] = 4; m_Faces[14] = 0; // X = min 50 3 : m_Faces[15] = 1; m_Faces[16] = 3; m_Faces[17] = 7; m_Faces[18] = 5; m_Faces[19] = 1; // X = max 51 : 52 3 : m_Faces[20] = 0; m_Faces[21] = 1; m_Faces[22] = 5; m_Faces[23] = 4; m_Faces[24] = 0; // Y = min 53 3 : m_Faces[25] = 2; m_Faces[26] = 3; m_Faces[27] = 7; m_Faces[28] = 6; m_Faces[29] = 2; // Y = max 54 3 : } 55 : 56 : 57 : /////////////////////////////////////////////////////////////////////////////// 58 : // Calculate bounds of this brush 59 0 : void CBrush::Bounds(CBoundingBoxAligned& result) const 60 : { 61 0 : result.SetEmpty(); 62 : 63 0 : for(size_t i = 0; i < m_Vertices.size(); ++i) 64 0 : result += m_Vertices[i]; 65 0 : } 66 : 67 : 68 : /////////////////////////////////////////////////////////////////////////////// 69 : // Cut the brush according to a given plane 70 : 71 : /// Holds information about what happens to a single vertex in a brush during a slicing operation. 72 : struct SliceOpVertexInfo 73 : { 74 : float planeDist; ///< Signed distance from this vertex to the slicing plane. 75 : size_t resIdx; ///< Index of this vertex in the resulting brush (or NO_VERTEX if cut away) 76 : }; 77 : 78 : /// Holds information about a newly introduced vertex on an edge in a brush as the result of a slicing operation. 79 : struct SliceOpNewVertexInfo 80 : { 81 : /// Indices of adjacent edge vertices in original brush 82 : size_t edgeIdx1, edgeIdx2; 83 : /// Index of newly introduced vertex in resulting brush 84 : size_t resIdx; 85 : 86 : /** 87 : * Index into SliceOpInfo.nvInfo; hold the indices of this new vertex's direct neighbours in the slicing plane face, 88 : * with no consistent winding direction around the face for either field (e.g., the neighb1 of X can point back to 89 : * X with either its neighb1 or neighb2). 90 : */ 91 : size_t neighbIdx1, neighbIdx2; 92 : }; 93 : 94 : /// Holds support information during a CBrush/CPlane slicing operation. 95 4 : struct SliceOpInfo 96 : { 97 : CBrush* result; 98 : const CBrush* original; 99 : 100 : /** 101 : * Holds information about what happens to each vertex in the original brush after the slice operation. 102 : * Same size as m_Vertices of the brush getting sliced. 103 : */ 104 : std::vector<SliceOpVertexInfo> ovInfo; 105 : 106 : /// Holds information about newly inserted vertices during a slice operation. 107 : std::vector<SliceOpNewVertexInfo> nvInfo; 108 : 109 : /** 110 : * Indices into nvInfo; during the execution of the slicing algorithm, holds the previously inserted new vertex on 111 : * one of the edges of the face that's currently being evaluated for slice points, or NO_VERTEX if no such vertex 112 : * exists. 113 : */ 114 : size_t thisFaceNewVertexIdx; 115 : }; 116 : 117 : struct CBrush::Helper 118 : { 119 : /** 120 : * Creates a new vertex between the given two vertices (indexed into the original brush). 121 : * Returns the index of the new vertex in the resulting brush. 122 : */ 123 : static size_t SliceNewVertex(SliceOpInfo& sliceInfo, size_t v1, size_t v2); 124 : }; 125 : 126 8 : size_t CBrush::Helper::SliceNewVertex(SliceOpInfo& sliceOp, size_t edgeIdx1, size_t edgeIdx2) 127 : { 128 : // check if a new vertex has already been inserted on this edge 129 8 : size_t idx; 130 20 : for(idx = 0; idx < sliceOp.nvInfo.size(); ++idx) 131 : { 132 16 : if ((sliceOp.nvInfo[idx].edgeIdx1 == edgeIdx1 && sliceOp.nvInfo[idx].edgeIdx2 == edgeIdx2) || 133 14 : (sliceOp.nvInfo[idx].edgeIdx1 == edgeIdx2 && sliceOp.nvInfo[idx].edgeIdx2 == edgeIdx1)) 134 : break; 135 : } 136 : 137 8 : if (idx >= sliceOp.nvInfo.size()) 138 : { 139 : // no previously inserted new vertex found on this edge; insert a new one 140 4 : SliceOpNewVertexInfo nvi; 141 4 : CVector3D newPos; 142 : 143 : // interpolate between the two vertices based on their distance from the plane 144 4 : float inv = 1.0 / (sliceOp.ovInfo[edgeIdx1].planeDist - sliceOp.ovInfo[edgeIdx2].planeDist); 145 4 : newPos = sliceOp.original->m_Vertices[edgeIdx2] * ( sliceOp.ovInfo[edgeIdx1].planeDist * inv) + 146 4 : sliceOp.original->m_Vertices[edgeIdx1] * (-sliceOp.ovInfo[edgeIdx2].planeDist * inv); 147 : 148 4 : nvi.edgeIdx1 = edgeIdx1; 149 4 : nvi.edgeIdx2 = edgeIdx2; 150 4 : nvi.resIdx = sliceOp.result->m_Vertices.size(); 151 4 : nvi.neighbIdx1 = NO_VERTEX; 152 4 : nvi.neighbIdx2 = NO_VERTEX; 153 : 154 4 : sliceOp.result->m_Vertices.push_back(newPos); 155 4 : sliceOp.nvInfo.push_back(nvi); 156 : } 157 : 158 : // at this point, 'idx' is the index into nvInfo of the vertex inserted onto the edge 159 : 160 8 : if (sliceOp.thisFaceNewVertexIdx != NO_VERTEX) 161 : { 162 : // a vertex has been previously inserted onto another edge of this face; link them together as neighbours 163 : // (using whichever one of the neighbIdx1 or -2 links is still available) 164 : 165 4 : if (sliceOp.nvInfo[sliceOp.thisFaceNewVertexIdx].neighbIdx1 == NO_VERTEX) 166 2 : sliceOp.nvInfo[sliceOp.thisFaceNewVertexIdx].neighbIdx1 = idx; 167 : else 168 2 : sliceOp.nvInfo[sliceOp.thisFaceNewVertexIdx].neighbIdx2 = idx; 169 : 170 4 : if (sliceOp.nvInfo[idx].neighbIdx1 == NO_VERTEX) 171 2 : sliceOp.nvInfo[idx].neighbIdx1 = sliceOp.thisFaceNewVertexIdx; 172 : else 173 2 : sliceOp.nvInfo[idx].neighbIdx2 = sliceOp.thisFaceNewVertexIdx; 174 : 175 : // a plane should slice a face only in two locations, so reset for the next face 176 4 : sliceOp.thisFaceNewVertexIdx = NO_VERTEX; 177 : } 178 : else 179 : { 180 : // store the index of the inserted vertex on this edge, so that we can retrieve it when the plane slices 181 : // this face again in another edge 182 4 : sliceOp.thisFaceNewVertexIdx = idx; 183 : } 184 : 185 8 : return sliceOp.nvInfo[idx].resIdx; 186 : } 187 : 188 4 : void CBrush::Slice(const CPlane& plane, CBrush& result) const 189 : { 190 4 : ENSURE(&result != this); 191 : 192 8 : SliceOpInfo sliceOp; 193 : 194 4 : sliceOp.original = this; 195 4 : sliceOp.result = &result; 196 4 : sliceOp.thisFaceNewVertexIdx = NO_VERTEX; 197 4 : sliceOp.ovInfo.resize(m_Vertices.size()); 198 4 : sliceOp.nvInfo.reserve(m_Vertices.size() / 2); 199 : 200 4 : result.m_Vertices.resize(0); // clear any left-overs 201 4 : result.m_Faces.resize(0); 202 4 : result.m_Vertices.reserve(m_Vertices.size() + 2); 203 4 : result.m_Faces.reserve(m_Faces.size() + 5); 204 : 205 : // Copy vertices that weren't sliced away by the plane to the resulting brush. 206 28 : for(size_t i = 0; i < m_Vertices.size(); ++i) 207 : { 208 24 : const CVector3D& vtx = m_Vertices[i]; // current vertex 209 24 : SliceOpVertexInfo& vtxInfo = sliceOp.ovInfo[i]; // slicing operation info about current vertex 210 : 211 24 : vtxInfo.planeDist = plane.DistanceToPlane(vtx); 212 24 : if (vtxInfo.planeDist >= 0.0) 213 : { 214 : // positive side of the plane; not sliced away 215 12 : vtxInfo.resIdx = result.m_Vertices.size(); 216 12 : result.m_Vertices.push_back(vtx); 217 : } 218 : else 219 : { 220 : // other side of the plane; sliced away 221 12 : vtxInfo.resIdx = NO_VERTEX; 222 : } 223 : } 224 : 225 : // Transfer faces. (Recall how faces are specified; see CBrush::m_Faces). The idea is to examine each face separately, 226 : // and see where its edges cross the slicing plane (meaning that exactly one of the vertices of that edge was cut away). 227 : // On those edges, new vertices are introduced where the edge intersects the plane, and the resulting brush's m_Faces 228 : // array is updated to refer to the newly inserted vertices instead of the original one that got cut away. 229 : 230 : size_t currentFaceStartIdx = NO_VERTEX; // index of the first vertex of the current face in the original brush 231 : size_t resultFaceStartIdx = NO_VERTEX; // index of the first vertex of the current face in the resulting brush 232 : 233 94 : for(size_t i = 0; i < m_Faces.size(); ++i) 234 : { 235 90 : if (currentFaceStartIdx == NO_VERTEX) 236 : { 237 : // starting a new face 238 18 : ENSURE(sliceOp.thisFaceNewVertexIdx == NO_VERTEX); 239 : 240 18 : currentFaceStartIdx = m_Faces[i]; 241 18 : resultFaceStartIdx = result.m_Faces.size(); 242 18 : continue; 243 18 : } 244 : 245 72 : size_t prevIdx = m_Faces[i-1]; // index of previous vertex in this face list 246 72 : size_t curIdx = m_Faces[i]; // index of current vertex in this face list 247 : 248 72 : if (sliceOp.ovInfo[prevIdx].resIdx == NO_VERTEX) 249 : { 250 : // previous face vertex got sliced away by the plane; see if the edge (prev,current) crosses the slicing plane 251 36 : if (sliceOp.ovInfo[curIdx].resIdx != NO_VERTEX) 252 : { 253 : // re-entering the front side of the plane; insert vertex on intersection of plane and (prev,current) edge 254 4 : result.m_Faces.push_back(Helper::SliceNewVertex(sliceOp, prevIdx, curIdx)); 255 4 : result.m_Faces.push_back(sliceOp.ovInfo[curIdx].resIdx); 256 : } 257 : } 258 : else 259 : { 260 : // previous face vertex didn't get sliced away; see if the edge (prev,current) crosses the slicing plane 261 36 : if (sliceOp.ovInfo[curIdx].resIdx != NO_VERTEX) 262 : { 263 : // perfectly normal edge; doesn't cross the plane 264 32 : result.m_Faces.push_back(sliceOp.ovInfo[curIdx].resIdx); 265 : } 266 : else 267 : { 268 : // leaving the front side of the plane; insert vertex on intersection of plane and edge (prev, current) 269 4 : result.m_Faces.push_back(Helper::SliceNewVertex(sliceOp, prevIdx, curIdx)); 270 : } 271 : } 272 : 273 : // if we're back at the first vertex of the current face, then we've completed the face 274 72 : if (curIdx == currentFaceStartIdx) 275 : { 276 : // close the index loop 277 18 : if (result.m_Faces.size() > resultFaceStartIdx) 278 11 : result.m_Faces.push_back(result.m_Faces[resultFaceStartIdx]); 279 : 280 : currentFaceStartIdx = NO_VERTEX; // start a new face 281 : } 282 : } 283 : 284 4 : ENSURE(currentFaceStartIdx == NO_VERTEX); 285 : 286 : // Create the face that lies in the slicing plane. Remember, all the intersections of the slicing plane with face 287 : // edges of the brush have been stored in sliceOp.nvInfo by the SliceNewVertex function, and refer to their direct 288 : // neighbours in the slicing plane face using the neighbIdx1 and neighbIdx2 fields (in no consistent winding order). 289 : 290 4 : if (sliceOp.nvInfo.size()) 291 : { 292 : // push the starting vertex 293 1 : result.m_Faces.push_back(sliceOp.nvInfo[0].resIdx); 294 : 295 : // At this point, there is no consistent winding order in the neighbX fields, so at each vertex we need to figure 296 : // out whether neighb1 or neighb2 points 'onwards' along the face, according to an initially chosen winding direction. 297 : // (or, equivalently, which one points back to the one we were just at). At each vertex, we then set neighb1 to be the 298 : // one to point onwards, deleting any pointers which we no longer need to complete the trace. 299 : 300 1 : size_t idx; 301 1 : size_t prev = 0; 302 : 303 1 : idx = sliceOp.nvInfo[0].neighbIdx2; // pick arbitrary starting direction 304 1 : sliceOp.nvInfo[0].neighbIdx2 = NO_VERTEX; 305 : 306 4 : while(idx != 0) 307 : { 308 3 : ENSURE(idx < sliceOp.nvInfo.size()); 309 3 : if (idx >= sliceOp.nvInfo.size()) 310 : break; 311 : 312 3 : if (sliceOp.nvInfo[idx].neighbIdx1 == prev) 313 : { 314 : // neighb1 is pointing the wrong way; we want to normalize it to point onwards in the direction 315 : // we initially chose, so swap it with neighb2 and delete neighb2 (no longer needed) 316 1 : sliceOp.nvInfo[idx].neighbIdx1 = sliceOp.nvInfo[idx].neighbIdx2; 317 1 : sliceOp.nvInfo[idx].neighbIdx2 = NO_VERTEX; 318 : } 319 : else 320 : { 321 : // neighb1 isn't pointing to the previous vertex, so neighb2 must be (otherwise a pair of vertices failed to 322 : // get paired properly during face/plane slicing). 323 2 : ENSURE(sliceOp.nvInfo[idx].neighbIdx2 == prev); 324 2 : sliceOp.nvInfo[idx].neighbIdx2 = NO_VERTEX; 325 : } 326 : 327 3 : result.m_Faces.push_back(sliceOp.nvInfo[idx].resIdx); 328 : 329 : // move to next vertex; neighb1 has been normalized to point onward 330 3 : prev = idx; 331 3 : idx = sliceOp.nvInfo[idx].neighbIdx1; 332 3 : sliceOp.nvInfo[prev].neighbIdx1 = NO_VERTEX; // no longer needed, we've moved on 333 : } 334 : 335 : // push starting vertex again to close the shape 336 1 : result.m_Faces.push_back(sliceOp.nvInfo[0].resIdx); 337 : } 338 4 : } 339 : 340 : 341 : 342 : /////////////////////////////////////////////////////////////////////////////// 343 : // Intersect with frustum by repeated slicing 344 0 : void CBrush::Intersect(const CFrustum& frustum, CBrush& result) const 345 : { 346 0 : ENSURE(&result != this); 347 : 348 0 : if (!frustum.GetNumPlanes()) 349 : { 350 0 : result = *this; 351 0 : return; 352 : } 353 : 354 0 : CBrush buf; 355 0 : const CBrush* prev = this; 356 0 : CBrush* next; 357 : 358 : // Repeatedly slice this brush with each plane of the frustum, alternating between 'result' and 'buf' to 359 : // save intermediate results. Set up the starting brush so that the final version always ends up in 'result'. 360 : 361 0 : if (frustum.GetNumPlanes() & 1) 362 : next = &result; 363 : else 364 0 : next = &buf; 365 : 366 0 : for(size_t i = 0; i < frustum.GetNumPlanes(); ++i) 367 : { 368 0 : prev->Slice(frustum[i], *next); 369 0 : prev = next; 370 0 : if (prev == &buf) 371 : next = &result; 372 : else 373 0 : next = &buf; 374 : } 375 : 376 0 : ENSURE(prev == &result); 377 : } 378 : 379 19 : const std::vector<CVector3D>& CBrush::GetVertices() const 380 : { 381 19 : return m_Vertices; 382 : } 383 : 384 13 : void CBrush::GetFaces(std::vector<std::vector<size_t>>& out) const 385 : { 386 : // split the back-to-back faces into separate face vectors, so that they're in a 387 : // user-friendlier format than the back-to-back vertex index array 388 : // i.e. split 'x--xy------yz----z' into 'x--x', 'y-------y', 'z---z' 389 : 390 13 : size_t faceStartIdx = 0; 391 85 : while (faceStartIdx < m_Faces.size()) 392 : { 393 : // start new face 394 72 : std::vector<size_t> singleFace; 395 72 : singleFace.push_back(m_Faces[faceStartIdx]); 396 : 397 : // step over all the values in the face until we hit the starting value again (which closes the face) 398 72 : size_t j = faceStartIdx + 1; 399 288 : while (j < m_Faces.size() && m_Faces[j] != m_Faces[faceStartIdx]) 400 : { 401 216 : singleFace.push_back(m_Faces[j]); 402 216 : j++; 403 : } 404 : 405 : // each face must be closed by the same value that started it 406 72 : ENSURE(m_Faces[faceStartIdx] == m_Faces[j]); 407 : 408 72 : singleFace.push_back(m_Faces[j]); 409 72 : out.push_back(singleFace); 410 : 411 72 : faceStartIdx = j + 1; 412 72 : } 413 13 : }