| 1 | /************************************************************************* |
| 2 | * * |
| 3 | * Vega FEM Simulation Library Version 3.1 * |
| 4 | * * |
| 5 | * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Code authors: Jernej Barbic, Christopher Twigg, Daniel Schroeder * |
| 9 | * http://www.jernejbarbic.com/code * |
| 10 | * * |
| 11 | * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, * |
| 12 | * Doug L. James, Jovan Popovic * |
| 13 | * * |
| 14 | * Funding: National Science Foundation, Link Foundation, * |
| 15 | * Singapore-MIT GAMBIT Game Lab, * |
| 16 | * Zumberge Research and Innovation Fund at USC * |
| 17 | * * |
| 18 | * This library is free software; you can redistribute it and/or * |
| 19 | * modify it under the terms of the BSD-style license that is * |
| 20 | * included with this library in the file LICENSE.txt * |
| 21 | * * |
| 22 | * This library is distributed in the hope that it will be useful, * |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file * |
| 25 | * LICENSE.TXT for more details. * |
| 26 | * * |
| 27 | *************************************************************************/ |
| 28 | |
| 29 | // Bounding Box |
| 30 | // Author: Jernej Barbic, CMU |
| 31 | |
| 32 | #include "openGL-headers.h" |
| 33 | #include <float.h> |
| 34 | #include <vector> |
| 35 | #include <iostream> |
| 36 | using namespace std; |
| 37 | |
| 38 | #include "boundingBox.h" |
| 39 | #include "triangle.h" |
| 40 | |
| 41 | |
| 42 | BoundingBox::BoundingBox(const vector<Vec3d> & vertices) |
| 43 | { |
| 44 | // set bmin_, bmax_ |
| 45 | bmin_ = Vec3d(+DBL_MAX, +DBL_MAX, +DBL_MAX); |
| 46 | bmax_ = Vec3d(-DBL_MAX, -DBL_MAX, -DBL_MAX); |
| 47 | for(size_t i = 0; i < vertices.size(); i++) |
| 48 | { |
| 49 | const Vec3d& p = vertices[i]; |
| 50 | if (p[0] < bmin_[0]) |
| 51 | bmin_[0] = p[0]; |
| 52 | if (p[0] > bmax_[0]) |
| 53 | bmax_[0] = p[0]; |
| 54 | |
| 55 | if (p[1] < bmin_[1]) |
| 56 | bmin_[1] = p[1]; |
| 57 | if (p[1] > bmax_[1]) |
| 58 | bmax_[1] = p[1]; |
| 59 | |
| 60 | if (p[2] < bmin_[2]) |
| 61 | bmin_[2] = p[2]; |
| 62 | if (p[2] > bmax_[2]) |
| 63 | bmax_[2] = p[2]; |
| 64 | } |
| 65 | updateData(); |
| 66 | } |
| 67 | |
| 68 | template<class Triangle> |
| 69 | BoundingBox::BoundingBox(const vector<Triangle> & tripool) |
| 70 | { |
| 71 | // set bmin_, bmax_ |
| 72 | bmin_ = Vec3d(+DBL_MAX, +DBL_MAX, +DBL_MAX); |
| 73 | bmax_ = Vec3d(-DBL_MAX, -DBL_MAX, -DBL_MAX); |
| 74 | for(unsigned int i=0; i < tripool.size(); i++) // over all vertices |
| 75 | { |
| 76 | Vec3d p = tripool[i].first(); |
| 77 | if (p[0] < bmin_[0]) |
| 78 | bmin_[0] = p[0]; |
| 79 | if (p[0] > bmax_[0]) |
| 80 | bmax_[0] = p[0]; |
| 81 | |
| 82 | if (p[1] < bmin_[1]) |
| 83 | bmin_[1] = p[1]; |
| 84 | if (p[1] > bmax_[1]) |
| 85 | bmax_[1] = p[1]; |
| 86 | |
| 87 | if (p[2] < bmin_[2]) |
| 88 | bmin_[2] = p[2]; |
| 89 | if (p[2] > bmax_[2]) |
| 90 | bmax_[2] = p[2]; |
| 91 | |
| 92 | p = tripool[i].second(); |
| 93 | |
| 94 | if (p[0] < bmin_[0]) |
| 95 | bmin_[0] = p[0]; |
| 96 | if (p[0] > bmax_[0]) |
| 97 | bmax_[0] = p[0]; |
| 98 | |
| 99 | if (p[1] < bmin_[1]) |
| 100 | bmin_[1] = p[1]; |
| 101 | if (p[1] > bmax_[1]) |
| 102 | bmax_[1] = p[1]; |
| 103 | |
| 104 | if (p[2] < bmin_[2]) |
| 105 | bmin_[2] = p[2]; |
| 106 | if (p[2] > bmax_[2]) |
| 107 | bmax_[2] = p[2]; |
| 108 | |
| 109 | p = tripool[i].third(); |
| 110 | |
| 111 | if (p[0] < bmin_[0]) |
| 112 | bmin_[0] = p[0]; |
| 113 | if (p[0] > bmax_[0]) |
| 114 | bmax_[0] = p[0]; |
| 115 | |
| 116 | if (p[1] < bmin_[1]) |
| 117 | bmin_[1] = p[1]; |
| 118 | if (p[1] > bmax_[1]) |
| 119 | bmax_[1] = p[1]; |
| 120 | |
| 121 | if (p[2] < bmin_[2]) |
| 122 | bmin_[2] = p[2]; |
| 123 | if (p[2] > bmax_[2]) |
| 124 | bmax_[2] = p[2]; |
| 125 | } |
| 126 | |
| 127 | updateData(); |
| 128 | } |
| 129 | |
| 130 | void BoundingBox::regularize() |
| 131 | { |
| 132 | Vec3d center_ = 0.5 * (bmin_ + bmax_); |
| 133 | Vec3d halfside_ = 0.5* (bmax_ - bmin_); |
| 134 | |
| 135 | double maxHalf = halfside_[0]; |
| 136 | if (halfside_[1] > maxHalf) |
| 137 | maxHalf = halfside_[1]; |
| 138 | if (halfside_[2] > maxHalf) |
| 139 | maxHalf = halfside_[2]; |
| 140 | |
| 141 | Vec3d cubeHalf_ = Vec3d(maxHalf,maxHalf,maxHalf); |
| 142 | |
| 143 | bmin_ = center_ - cubeHalf_; |
| 144 | bmax_ = center_ + cubeHalf_; |
| 145 | |
| 146 | updateData(); |
| 147 | } |
| 148 | |
| 149 | void BoundingBox::updateData() |
| 150 | { |
| 151 | center_ = 0.5 * (bmin_ + bmax_); |
| 152 | halfSides_ = 0.5 * (bmax_ - bmin_); |
| 153 | } |
| 154 | |
| 155 | void BoundingBox::verifyBox() const |
| 156 | { |
| 157 | if (!((bmin_[0] <= bmax_[0]) && (bmin_[1] <= bmax_[1]) && (bmin_[2] <= bmax_[2]))) |
| 158 | printf("Error: inconsistent bounding box.\n"); |
| 159 | } |
| 160 | |
| 161 | bool BoundingBox::checkInside(const Vec3d & p) const |
| 162 | { |
| 163 | return bmin_[0] <= p[0] && p[0] <= bmax_[0] && bmin_[1] <= p[1] && p[1] <= bmax_[1] && bmin_[2] <= p[2] && p[2] <= bmax_[2]; |
| 164 | } |
| 165 | |
| 166 | void BoundingBox::render() const |
| 167 | { |
| 168 | // render the bounding box |
| 169 | Vec3d p0(bmin_[0],bmin_[1],bmin_[2]); |
| 170 | Vec3d p1(bmax_[0],bmin_[1],bmin_[2]); |
| 171 | Vec3d p2(bmax_[0],bmax_[1],bmin_[2]); |
| 172 | Vec3d p3(bmin_[0],bmax_[1],bmin_[2]); |
| 173 | |
| 174 | Vec3d p4(bmin_[0],bmin_[1],bmax_[2]); |
| 175 | Vec3d p5(bmax_[0],bmin_[1],bmax_[2]); |
| 176 | Vec3d p6(bmax_[0],bmax_[1],bmax_[2]); |
| 177 | Vec3d p7(bmin_[0],bmax_[1],bmax_[2]); |
| 178 | |
| 179 | #define VTX(i) (i)[0],(i)[1],(i)[2] |
| 180 | glBegin(GL_LINES); |
| 181 | glVertex3f(VTX(p0)); |
| 182 | glVertex3f(VTX(p1)); |
| 183 | glVertex3f(VTX(p1)); |
| 184 | glVertex3f(VTX(p2)); |
| 185 | glVertex3f(VTX(p2)); |
| 186 | glVertex3f(VTX(p3)); |
| 187 | glVertex3f(VTX(p3)); |
| 188 | glVertex3f(VTX(p0)); |
| 189 | |
| 190 | glVertex3f(VTX(p0)); |
| 191 | glVertex3f(VTX(p4)); |
| 192 | glVertex3f(VTX(p1)); |
| 193 | glVertex3f(VTX(p5)); |
| 194 | glVertex3f(VTX(p2)); |
| 195 | glVertex3f(VTX(p6)); |
| 196 | glVertex3f(VTX(p3)); |
| 197 | glVertex3f(VTX(p7)); |
| 198 | |
| 199 | glVertex3f(VTX(p4)); |
| 200 | glVertex3f(VTX(p5)); |
| 201 | glVertex3f(VTX(p5)); |
| 202 | glVertex3f(VTX(p6)); |
| 203 | glVertex3f(VTX(p6)); |
| 204 | glVertex3f(VTX(p7)); |
| 205 | glVertex3f(VTX(p7)); |
| 206 | glVertex3f(VTX(p4)); |
| 207 | glEnd(); |
| 208 | |
| 209 | #undef VTX |
| 210 | } |
| 211 | |
| 212 | // should this be turned into a self-modifying function? |
| 213 | void BoundingBox::expand(double expansionFactor) |
| 214 | { |
| 215 | bmin_ = center_ - expansionFactor * halfSides_; |
| 216 | bmax_ = center_ + expansionFactor * halfSides_; |
| 217 | |
| 218 | updateData(); |
| 219 | } |
| 220 | |
| 221 | bool BoundingBox::lineSegmentIntersection(const Vec3d & segmentStart, const Vec3d & segmentEnd, Vec3d * intersection) const |
| 222 | { |
| 223 | /* |
| 224 | Jernej Barbic, CMU, 2005 |
| 225 | adapted from: |
| 226 | |
| 227 | Fast Ray-Box Intersection |
| 228 | by Andrew Woo |
| 229 | from "Graphics Gems", Academic Press, 1990 |
| 230 | */ |
| 231 | |
| 232 | #define NUMDIM 3 |
| 233 | #define RIGHT 0 |
| 234 | #define LEFT 1 |
| 235 | #define MIDDLE 2 |
| 236 | |
| 237 | Vec3d dir = segmentEnd - segmentStart; |
| 238 | bool inside = true; |
| 239 | char quadrant[NUMDIM]; |
| 240 | register int i; |
| 241 | int whichPlane; |
| 242 | double maxT[NUMDIM]; |
| 243 | double candidatePlane[NUMDIM]; |
| 244 | |
| 245 | /* Find candidate planes; this loop can be avoided if |
| 246 | rays cast all from the eye(assume perpsective view) */ |
| 247 | for (i=0; i<NUMDIM; i++) |
| 248 | if(segmentStart[i] < bmin_[i]) |
| 249 | { |
| 250 | quadrant[i] = LEFT; |
| 251 | candidatePlane[i] = bmin_[i]; |
| 252 | inside = false; |
| 253 | } |
| 254 | else if (segmentStart[i] > bmax_[i]) |
| 255 | { |
| 256 | quadrant[i] = RIGHT; |
| 257 | candidatePlane[i] = bmax_[i]; |
| 258 | inside = false; |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | quadrant[i] = MIDDLE; |
| 263 | } |
| 264 | |
| 265 | /* Ray origin inside bounding box */ |
| 266 | if(inside) |
| 267 | { |
| 268 | *intersection = segmentStart; |
| 269 | return (true); |
| 270 | } |
| 271 | |
| 272 | /* Calculate T distances to candidate planes */ |
| 273 | for (i = 0; i < NUMDIM; i++) |
| 274 | if (quadrant[i] != MIDDLE && dir[i] !=0.) |
| 275 | maxT[i] = (candidatePlane[i]-segmentStart[i]) / dir[i]; |
| 276 | else |
| 277 | maxT[i] = -1.; |
| 278 | |
| 279 | /* Get largest of the maxT's for final choice of intersection */ |
| 280 | whichPlane = 0; |
| 281 | for (i = 1; i < NUMDIM; i++) |
| 282 | if (maxT[whichPlane] < maxT[i]) |
| 283 | whichPlane = i; |
| 284 | |
| 285 | /* Check final candidate actually inside box */ |
| 286 | if (maxT[whichPlane] < 0.0) |
| 287 | return (false); |
| 288 | if (maxT[whichPlane] > 1.0) |
| 289 | return (false); // remove this for ray |
| 290 | |
| 291 | for (i = 0; i < NUMDIM; i++) |
| 292 | { |
| 293 | if (whichPlane != i) |
| 294 | { |
| 295 | (*intersection)[i] = segmentStart[i] + maxT[whichPlane] *dir[i]; |
| 296 | if ((*intersection)[i] < bmin_[i] || (*intersection)[i] > bmax_[i]) |
| 297 | return (false); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | (*intersection)[i] = candidatePlane[i]; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return (true); /* ray hits box */ |
| 306 | |
| 307 | #undef NUMDIM |
| 308 | #undef RIGHT |
| 309 | #undef LEFT |
| 310 | #undef MIDDLE |
| 311 | } |
| 312 | |
| 313 | double BoundingBox::distanceToPoint(const Vec3d & point) const |
| 314 | { |
| 315 | double distance = 0.0; |
| 316 | for(int dim=0; dim<3; dim++) |
| 317 | { |
| 318 | if (point[dim] < bmin_[dim]) |
| 319 | distance += (bmin_[dim] - point[dim]) * (bmin_[dim] - point[dim]); |
| 320 | if (point[dim] > bmax_[dim]) |
| 321 | distance += (point[dim] - bmax_[dim]) * (point[dim] - bmax_[dim]); |
| 322 | } |
| 323 | return sqrt(distance); |
| 324 | } |
| 325 | |
| 326 | void BoundingBox::print() const |
| 327 | { |
| 328 | cout << bmin_ << " " << bmax_ << endl; |
| 329 | } |
| 330 | |
| 331 | template BoundingBox::BoundingBox(const vector<TriangleBasic> & tripool); |
| 332 | template BoundingBox::BoundingBox(const vector<TriangleWithCollisionInfo> & tripool); |
| 333 | template BoundingBox::BoundingBox(const vector<TriangleWithCollisionInfoAndPseudoNormals> & tripool); |