geometry.cpp

Go to the documentation of this file.
00001 
00002 /*
00003  * pbrt source code Copyright(c) 1998-2005 Matt Pharr and Greg Humphreys
00004  *
00005  * All Rights Reserved.
00006  * For educational use only; commercial use expressly forbidden.
00007  * NO WARRANTY, express or implied, for this software.
00008  * (See file License.txt for complete license)
00009  */
00010 
00011 // geometry.cpp*
00012 #include "geometry.h"
00013 // BBox Method Definitions
00014 COREDLL BBox Union(const BBox &b, const Point &p) {
00015         BBox ret = b;
00016         ret.pMin.x = min(b.pMin.x, p.x);
00017         ret.pMin.y = min(b.pMin.y, p.y);
00018         ret.pMin.z = min(b.pMin.z, p.z);
00019         ret.pMax.x = max(b.pMax.x, p.x);
00020         ret.pMax.y = max(b.pMax.y, p.y);
00021         ret.pMax.z = max(b.pMax.z, p.z);
00022         return ret;
00023 }
00024 COREDLL BBox Union(const BBox &b, const BBox &b2) {
00025         BBox ret;
00026         ret.pMin.x = min(b.pMin.x, b2.pMin.x);
00027         ret.pMin.y = min(b.pMin.y, b2.pMin.y);
00028         ret.pMin.z = min(b.pMin.z, b2.pMin.z);
00029         ret.pMax.x = max(b.pMax.x, b2.pMax.x);
00030         ret.pMax.y = max(b.pMax.y, b2.pMax.y);
00031         ret.pMax.z = max(b.pMax.z, b2.pMax.z);
00032         return ret;
00033 }
00034 void BBox::BoundingSphere(Point *c, float *rad) const {
00035         *c = .5f * pMin + .5f * pMax;
00036         *rad = Inside(*c) ? Distance(*c, pMax) : 0.f;
00037 }
00038 bool BBox::IntersectP(const Ray &ray, float *hitt0,
00039                 float *hitt1) const {
00040         float t0 = ray.mint, t1 = ray.maxt;
00041         for (int i = 0; i < 3; ++i) {
00042                 // Update interval for _i_th bounding box slab
00043                 float invRayDir = 1.f / ray.d[i];
00044                 float tNear = (pMin[i] - ray.o[i]) * invRayDir;
00045                 float tFar  = (pMax[i] - ray.o[i]) * invRayDir;
00046                 // Update parametric interval from slab intersection $t$s
00047                 if (tNear > tFar) swap(tNear, tFar);
00048                 t0 = tNear > t0 ? tNear : t0;
00049                 t1 = tFar  < t1 ? tFar  : t1;
00050                 if (t0 > t1) return false;
00051         }
00052         if (hitt0) *hitt0 = t0;
00053         if (hitt1) *hitt1 = t1;
00054         return true;
00055 }

Generated on Wed Sep 26 14:01:19 2007 for pbrt by  doxygen 1.5.1