00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef PBRT_GEOMETRY_H
00013 #define PBRT_GEOMETRY_H
00014
00015 #include <algorithm>
00016 #include <iostream>
00017 #include <cstdlib>
00018 #include <cstdio>
00019 #include <cassert>
00020 #include <cmath>
00021
00022
00023 namespace gk {
00024
00025 #define RAY_EPSILON 0.0001f
00026 #define EPSILON 0.00001f
00027
00028
00029 class Point;
00030 class Normal;
00031 class Vector;
00032
00033
00034 class Point2
00035 {
00036 public:
00037
00038
00039
00040 Point2( const float _x = 0.f, const float _y = 0.f )
00041 :
00042 x( _x ), y( _y )
00043 {}
00044
00045
00046 Point2 operator+( const Point2 &v ) const
00047 {
00048 return Point2( x + v.x, y + v.y );
00049 }
00050
00051
00052 Point2& operator+=( const Point2 &v )
00053 {
00054 x += v.x;
00055 y += v.y;
00056
00057 return *this;
00058 }
00059
00060
00061 Point2 operator-( const Point2 &v ) const
00062 {
00063 return Point2( x - v.x, y - v.y );
00064 }
00065
00066
00067 Point2& operator-=( const Point2 &v )
00068 {
00069 x -= v.x;
00070 y -= v.y;
00071
00072 return *this;
00073 }
00074
00075
00076 bool operator==( const Point2 &v ) const
00077 {
00078 return (x == v.x && y == v.y);
00079 }
00080
00081
00082 Point2 operator*( const float f ) const
00083 {
00084 return Point2( f*x, f*y );
00085 }
00086
00087
00088 Point2 &operator*=( const float f )
00089 {
00090 x *= f;
00091 y *= f;
00092
00093 return *this;
00094 }
00095
00096
00097 Point2 operator/( const float f ) const
00098 {
00099 assert( f != 0 );
00100 float inv = 1.f / f;
00101 return Point2( x * inv, y * inv );
00102 }
00103
00104
00105 Point2 &operator/=( const float f )
00106 {
00107 assert( f != 0 );
00108 float inv = 1.f / f;
00109 x *= inv;
00110 y *= inv;
00111
00112 return *this;
00113 }
00114
00115
00116 Point2 operator-( ) const
00117 {
00118 return Point2( -x, -y );
00119 }
00120
00121
00122 const float& operator[]( const unsigned int i ) const
00123 {
00124 return ( &x )[i];
00125 }
00126
00127
00128 float &operator[]( const unsigned int i )
00129 {
00130 return ( &x )[i];
00131 }
00132
00133
00134 float LengthSquared() const
00135 {
00136 return x*x + y*y;
00137 }
00138
00139
00140 float Length() const
00141 {
00142 return sqrtf( LengthSquared() );
00143 }
00144
00145
00146
00147 float x, y;
00148 };
00149
00150
00151
00152 class Vector
00153 {
00154 public:
00155
00156
00157 Vector( const float _x = 0.f, const float _y = 0.f, const float _z = 0.f )
00158 :
00159 x( _x ), y( _y ), z( _z )
00160 {}
00161
00162
00163 explicit Vector( const Point &p );
00164
00165
00166 explicit Vector( const Normal &n );
00167
00168
00169 Vector( const Point& p, const Point& q );
00170
00171
00172 void print( ) const
00173 {
00174 printf("% -.8f % -.8f % -.8f\n", x, y, z);
00175 }
00176
00177
00178 Vector operator+( const Vector &v ) const
00179 {
00180 return Vector( x + v.x, y + v.y, z + v.z );
00181 }
00182
00183
00184 Vector& operator+=( const Vector &v )
00185 {
00186 x += v.x;
00187 y += v.y;
00188 z += v.z;
00189
00190 return *this;
00191 }
00192
00193
00194 Vector operator-( const Vector &v ) const
00195 {
00196 return Vector( x - v.x, y - v.y, z - v.z );
00197 }
00198
00199
00200 Vector& operator-=( const Vector &v )
00201 {
00202 x -= v.x;
00203 y -= v.y;
00204 z -= v.z;
00205
00206 return *this;
00207 }
00208
00209
00210 bool operator==( const Vector &v ) const
00211 {
00212 return (x == v.x && y == v.y && z == v.z);
00213 }
00214
00215
00216 Vector operator*( const float f ) const
00217 {
00218 return Vector( f*x, f*y, f*z );
00219 }
00220
00221
00222 Vector &operator*=( const float f )
00223 {
00224 x *= f;
00225 y *= f;
00226 z *= f;
00227
00228 return *this;
00229 }
00230
00231
00232 Vector operator/( const float f ) const
00233 {
00234 assert( f != 0 );
00235 float inv = 1.f / f;
00236 return Vector( x * inv, y * inv, z * inv );
00237 }
00238
00239
00240 Vector &operator/=( const float f )
00241 {
00242 assert( f != 0 );
00243 float inv = 1.f / f;
00244 x *= inv;
00245 y *= inv;
00246 z *= inv;
00247
00248 return *this;
00249 }
00250
00251
00252 Vector operator-( ) const
00253 {
00254 return Vector( -x, -y, -z );
00255 }
00256
00257
00258 const float& operator[]( const unsigned int i ) const
00259 {
00260 return ( &x )[i];
00261 }
00262
00263
00264 float &operator[]( const unsigned int i )
00265 {
00266 return ( &x )[i];
00267 }
00268
00269
00270 float LengthSquared() const
00271 {
00272 return x*x + y*y + z*z;
00273 }
00274
00275
00276 float Length() const
00277 {
00278 return sqrtf( LengthSquared() );
00279 }
00280
00281
00282
00283 float x, y, z;
00284 };
00285
00286
00287 typedef Vector Vector3;
00288
00289
00290 class Color
00291 {
00292 public:
00293
00294
00295 Color( )
00296 :
00297 r(0.f), g(0.f), b(0.f), a(1.f)
00298 {}
00299
00300 Color( const float _r, const float _g, const float _b, const float _a= 1.f )
00301 :
00302 r( _r ), g( _g ), b( _b ), a( _a )
00303 {}
00304
00305
00306 Color( const float _v, const float _a= 1.f )
00307 :
00308 r( _v ), g( _v ), b( _v ), a( _a )
00309 {}
00310
00311
00312 void print( ) const
00313 {
00314 printf("% -.6f % -.6f % -.6f % -.6f\n", r, g, b, a);
00315 }
00316
00317
00318 Color operator+( const Color &v ) const
00319 {
00320 return Color( r + v.r, g + v.g, b + v.b, a + v.a );
00321 }
00322
00323
00324 Color& operator+=( const Color &v )
00325 {
00326 r += v.r;
00327 g += v.g;
00328 b += v.b;
00329 a+= v.a;
00330
00331 return *this;
00332 }
00333
00334
00335 Color operator-( const Color &v ) const
00336 {
00337 return Color( r - v.r, g - v.g, b - v.b, a - v.a );
00338 }
00339
00340
00341 Color& operator-=( const Color &v )
00342 {
00343 r -= v.r;
00344 g -= v.g;
00345 b -= v.b;
00346 a -= v.a;
00347
00348 return *this;
00349 }
00350
00351 Color operator*( const Color& v ) const
00352 {
00353 return Color( r * v.r, g * v.g, b * v.b, a * v.a );
00354 }
00355
00356 Color& operator*=( const Color& v)
00357 {
00358 r *= v.r;
00359 g *= v.g;
00360 b *= v.b;
00361 a *= v.a;
00362
00363 return *this;
00364 }
00365
00366
00367 bool operator==( const Color &v ) const
00368 {
00369 return (r == v.r && g == v.g && b == v.b && a == v.a );
00370 }
00371
00372
00373 Color operator*( const float f ) const
00374 {
00375 return Color( f*r, f*g, f*b, f*a );
00376 }
00377
00378
00379 Color &operator*=( const float f )
00380 {
00381 r *= f;
00382 g *= f;
00383 b *= f;
00384 a *= f;
00385
00386 return *this;
00387 }
00388
00389
00390 Color operator/( const float f ) const
00391 {
00392 assert( f != 0 );
00393 float inv = 1.f / f;
00394 return Color( r * inv, g * inv, b * inv, a * inv );
00395 }
00396
00397
00398 Color &operator/=( const float f )
00399 {
00400 assert( f != 0 );
00401 float inv = 1.f / f;
00402 r *= inv;
00403 g *= inv;
00404 b *= inv;
00405 a *= inv;
00406
00407 return *this;
00408 }
00409
00410
00411 Color operator-( ) const
00412 {
00413 return Color( -r, -g, -b, -a );
00414 }
00415
00416
00417 float power( ) const
00418 {
00419 return (r + g + b) / 3.f;
00420 }
00421
00422
00423 const float& operator[]( const unsigned int i ) const
00424 {
00425 return ( &r )[i];
00426 }
00427
00428
00429 float &operator[]( const unsigned int i )
00430 {
00431 return ( &r )[i];
00432 }
00433
00434
00435
00436 float r, g, b, a;
00437 };
00438
00439
00440 typedef Color Energy;
00441
00442
00443
00444
00445 class Point
00446 {
00447 public:
00448
00449 Point( const float _x = 0.f, const float _y = 0.f, const float _z = 0.f )
00450 :
00451 x( _x ), y( _y ), z( _z )
00452 {}
00453
00454 explicit Point( const Vector &v )
00455 :
00456 x( v.x ), y( v.y ), z( v.z )
00457 {}
00458
00459
00460 void print( ) const
00461 {
00462 printf("%.10f %.10f %.10f\n", x, y, z);
00463 }
00464
00465
00466 Point operator+( const Vector &v ) const
00467 {
00468 return Point( x + v.x, y + v.y, z + v.z );
00469 }
00470
00471
00472 Point &operator+=( const Vector &v )
00473 {
00474 x += v.x;
00475 y += v.y;
00476 z += v.z;
00477
00478 return *this;
00479 }
00480
00481
00482 Vector operator-( const Point &q ) const
00483 {
00484 return Vector( x - q.x, y - q.y, z - q.z );
00485 }
00486
00487
00488 Point operator-( const Vector &v ) const
00489 {
00490 return Point( x - v.x, y - v.y, z - v.z );
00491 }
00492
00493
00494 Point &operator-=( const Vector &v )
00495 {
00496 x -= v.x;
00497 y -= v.y;
00498 z -= v.z;
00499
00500 return *this;
00501 }
00502
00503
00504 Point &operator+=( const Point &q )
00505 {
00506 x += q.x;
00507 y += q.y;
00508 z += q.z;
00509
00510 return *this;
00511 }
00512
00513 Point operator+( const Point &q ) const
00514 {
00515 return Point( x + q.x, y + q.y, z + q.z );
00516 }
00517
00518 Point operator*( const float f ) const
00519 {
00520 return Point( f*x, f*y, f*z );
00521 }
00522
00523 Point &operator*=( const float f )
00524 {
00525 x *= f;
00526 y *= f;
00527 z *= f;
00528
00529 return *this;
00530 }
00531
00532 Point operator/ ( const float f ) const
00533 {
00534 float inv = 1.f / f;
00535 return Point( inv*x, inv*y, inv*z );
00536 }
00537
00538 Point &operator/=( const float f )
00539 {
00540 float inv = 1.f / f;
00541 x *= inv;
00542 y *= inv;
00543 z *= inv;
00544
00545 return *this;
00546 }
00547
00548 const float& operator[]( const unsigned int i ) const
00549 {
00550 return ( &x )[i];
00551 }
00552
00553 float &operator[]( const unsigned int i )
00554 {
00555 return ( &x )[i];
00556 }
00557
00558
00559 float x, y, z;
00560 };
00561
00562
00563 class HPoint
00564 {
00565 public:
00566
00567
00568 HPoint( const float _x = 0.f, const float _y = 0.f, const float _z = 0.f, const float _w= 1.f )
00569 :
00570 x( _x ), y( _y ), z( _z ), w( _w )
00571 {}
00572
00573
00574 HPoint( const Point& p )
00575 :
00576 x(p.x), y(p.y), z(p.z), w(1.f)
00577 {}
00578
00579
00580 Point project( ) const
00581 {
00582 assert( w != 0.f );
00583 return Point(x / w, y / w, z / w);
00584 }
00585
00586
00587 bool isVisible( ) const
00588 {
00589 return
00590 (-w < x && x < w
00591 && -w < y && y < w
00592 && -w < z && z < w);
00593 }
00594
00595
00596 bool isCulled( ) const
00597 {
00598 return !isVisible();
00599 }
00600
00601
00602 const float& operator[]( const unsigned int i ) const
00603 {
00604 return ( &x )[i];
00605 }
00606
00607
00608 float &operator[]( const unsigned int i )
00609 {
00610 return ( &x )[i];
00611 }
00612
00613
00614 float x, y, z, w;
00615 };
00616
00617
00618 class Normal
00619 {
00620 public:
00621
00622 Normal( const float _x = 0.f, const float _y = 0.f, const float _z = 0.f )
00623 :
00624 x( _x ), y( _y ), z( _z )
00625 {}
00626
00627 Normal operator-( ) const
00628 {
00629 return Normal( -x, -y, -z );
00630 }
00631
00632 Normal operator+ ( const Normal &v ) const
00633 {
00634 return Normal( x + v.x, y + v.y, z + v.z );
00635 }
00636
00637 Normal& operator+=( const Normal &v )
00638 {
00639 x += v.x;
00640 y += v.y;
00641 z += v.z;
00642
00643 return *this;
00644 }
00645
00646 Normal operator- ( const Normal &v ) const
00647 {
00648 return Normal( x - v.x, y - v.y, z - v.z );
00649 }
00650
00651 Normal& operator-=( const Normal &v )
00652 {
00653 x -= v.x;
00654 y -= v.y;
00655 z -= v.z;
00656
00657 return *this;
00658 }
00659
00660 Normal operator*( const float f ) const
00661 {
00662 return Normal( f*x, f*y, f*z );
00663 }
00664
00665 Normal &operator*=( const float f )
00666 {
00667 x *= f;
00668 y *= f;
00669 z *= f;
00670
00671 return *this;
00672 }
00673
00674 Normal operator/ ( const float f ) const
00675 {
00676 float inv = 1.f / f;
00677 return Normal( x * inv, y * inv, z * inv );
00678 }
00679
00680 Normal &operator/=( float f )
00681 {
00682 float inv = 1.f / f;
00683 x *= inv;
00684 y *= inv;
00685 z *= inv;
00686
00687 return *this;
00688 }
00689
00690 float LengthSquared() const
00691 {
00692 return x*x + y*y + z*z;
00693 }
00694
00695 float Length() const
00696 {
00697 return sqrtf( LengthSquared() );
00698 }
00699
00700 explicit Normal( const Vector &v )
00701 :
00702 x( v.x ), y( v.y ), z( v.z )
00703 {}
00704
00705 const float& operator[]( const unsigned int i ) const
00706 {
00707 return ( &x )[i];
00708 }
00709
00710 float &operator[]( const unsigned int i )
00711 {
00712 return ( &x )[i];
00713 }
00714
00715
00716 float x, y, z;
00717 };
00718
00719 typedef Normal Tangent;
00720
00721
00722
00723 struct Ray
00724 {
00725
00726 Ray()
00727 :
00728 tmin( RAY_EPSILON ), tmax( HUGE_VAL ),
00729 dnear(0.f), radius(0.f)
00730 {}
00731
00732
00733 Ray( const Point &origin, const Vector &direction,
00734 const float start = RAY_EPSILON, const float end = HUGE_VAL )
00735 :
00736 o( origin ), d( direction ), tmin( start ), tmax( end ),
00737 dnear(0.f), radius(0.f)
00738 {
00739 inv_d= Vector(1.f / d.x, 1.f / d.y, 1.f / d.z);
00740 sign_d[0]= (inv_d[0] < 0.f) ? 1 : 0;
00741 sign_d[1]= (inv_d[1] < 0.f) ? 1 : 0;
00742 sign_d[2]= (inv_d[2] < 0.f) ? 1 : 0;
00743 sign_d[3]= 0;
00744 }
00745
00746
00747 Ray( const Point &origin, const Point& destination,
00748 const float start = RAY_EPSILON, const float end = 1.f - RAY_EPSILON )
00749 :
00750 o( origin ), d( origin, destination ), tmin( start ), tmax( end ),
00751 dnear(0.f), radius(0.f)
00752 {
00753 inv_d= Vector(1.f / d.x, 1.f / d.y, 1.f / d.z);
00754 sign_d[0]= (inv_d[0] < 0.f) ? 1 : 0;
00755 sign_d[1]= (inv_d[1] < 0.f) ? 1 : 0;
00756 sign_d[2]= (inv_d[2] < 0.f) ? 1 : 0;
00757 sign_d[3]= 0;
00758 }
00759
00760
00761 Point operator( )( const float t ) const
00762 {
00763 return o + d * t;
00764 }
00765
00766
00767 bool isBackward( const int axis ) const
00768 {
00769 return (sign_d[axis]);
00770 }
00771
00772
00773 Point o;
00774 Vector d;
00775 Vector inv_d;
00776 unsigned char sign_d[4];
00777 float tmin, tmax;
00778 float dnear;
00779 float radius;
00780 };
00781
00782
00783 struct Hit
00784 {
00785 Hit( )
00786 :
00787 tmin(RAY_EPSILON),
00788 t(HUGE_VAL),
00789 object_id(-1),
00790 node_id(-1),
00791 child_id(-1),
00792 user_data(0.f)
00793 {}
00794
00795 Hit( const Ray& ray )
00796 :
00797 tmin(ray.tmin),
00798 t(ray.tmax),
00799 object_id(-1),
00800 node_id(-1),
00801 child_id(-1),
00802 user_data(0.f)
00803 {}
00804
00805 ~Hit( ) {}
00806
00807
00808 Point p;
00809 Normal n;
00810 Tangent t1, t2;
00811 float tmin;
00812 float t;
00813 float u, v;
00814 int object_id;
00815 int node_id;
00816 int child_id;
00817 float user_data;
00818 };
00819
00820
00821
00822 class BBox
00823 {
00824 public:
00825
00826
00827 BBox()
00828 {
00829 clear();
00830 }
00831
00832
00833 void clear( )
00834 {
00835 pMin = Point( HUGE_VAL, HUGE_VAL, HUGE_VAL );
00836 pMax = Point( -HUGE_VAL, -HUGE_VAL, -HUGE_VAL );
00837 }
00838
00839
00840 BBox( const Point &p )
00841 :
00842 pMin( p ), pMax( p )
00843 { }
00844
00845
00846 BBox( const Point &p1, const Point &p2 )
00847 {
00848 pMin = Point(
00849 std::min( p1.x, p2.x ),
00850 std::min( p1.y, p2.y ),
00851 std::min( p1.z, p2.z ) );
00852
00853 pMax = Point(
00854 std::max( p1.x, p2.x ),
00855 std::max( p1.y, p2.y ),
00856 std::max( p1.z, p2.z ) );
00857 }
00858
00859 friend inline std::ostream &
00860 operator<<( std::ostream &os, const BBox &b );
00861
00862 void print( ) const
00863 {
00864 printf("[ %.10f %.10f %.10f ] x [ %.10f %.10f %.10f ] ",
00865 pMin.x, pMin.y, pMin.z,
00866 pMax.x, pMax.y, pMax.z);
00867
00868 Vector d(pMin, pMax);
00869 printf("extents (%.10f %.10f %.10f)\n",
00870 d.x, d.y, d.z);
00871 }
00872
00873
00874 friend BBox Union( const BBox &b, const Point &p );
00875
00876
00877 friend BBox Union( const BBox &b, const BBox &b2 );
00878
00879
00880 void Union( const BBox& bbox )
00881 {
00882 #if 0
00883 pMin.x= std::min(pMin.x, bbox.pMin.x);
00884 pMin.y= std::min(pMin.y, bbox.pMin.y);
00885 pMin.z= std::min(pMin.z, bbox.pMin.z);
00886 pMax.x= std::max(pMax.x, bbox.pMax.x);
00887 pMax.y= std::max(pMax.y, bbox.pMax.y);
00888 pMax.z= std::max(pMax.z, bbox.pMax.z);
00889 #else
00890
00891 if(bbox.pMin.x < pMin.x)
00892 pMin.x= bbox.pMin.x;
00893 if(bbox.pMin.y < pMin.y)
00894 pMin.y= bbox.pMin.y;
00895 if(bbox.pMin.z < pMin.z)
00896 pMin.z= bbox.pMin.z;
00897
00898 if(bbox.pMax.x > pMax.x)
00899 pMax.x= bbox.pMax.x;
00900 if(bbox.pMax.y > pMax.y)
00901 pMax.y= bbox.pMax.y;
00902 if(bbox.pMax.z > pMax.z)
00903 pMax.z= bbox.pMax.z;
00904 #endif
00905 }
00906
00907
00908 void Union( const Point& p )
00909 {
00910 #if 0
00911 pMin.x= std::min(pMin.x, p.x);
00912 pMin.y= std::min(pMin.y, p.y);
00913 pMin.z= std::min(pMin.z, p.z);
00914 pMax.x= std::max(pMax.x, p.x);
00915 pMax.y= std::max(pMax.y, p.y);
00916 pMax.z= std::max(pMax.z, p.z);
00917 #else
00918
00919 if(p.x < pMin.x)
00920 pMin.x= p.x;
00921 if(p.y < pMin.y)
00922 pMin.y= p.y;
00923 if(p.z < pMin.z)
00924 pMin.z= p.z;
00925
00926 if(p.x > pMax.x)
00927 pMax.x= p.x;
00928 if(p.y > pMax.y)
00929 pMax.y= p.y;
00930 if(p.z > pMax.z)
00931 pMax.z= p.z;
00932 #endif
00933 }
00934
00935
00936 bool Overlaps( const BBox &b ) const
00937 {
00938 bool x = ( pMax.x >= b.pMin.x ) && ( pMin.x <= b.pMax.x );
00939 bool y = ( pMax.y >= b.pMin.y ) && ( pMin.y <= b.pMax.y );
00940 bool z = ( pMax.z >= b.pMin.z ) && ( pMin.z <= b.pMax.z );
00941 return ( x && y && z );
00942 }
00943
00944
00945 bool Inside( const Point &pt ) const
00946 {
00947 return ( pt.x >= pMin.x && pt.x <= pMax.x
00948 && pt.y >= pMin.y && pt.y <= pMax.y
00949 && pt.z >= pMin.z && pt.z <= pMax.z );
00950 }
00951
00952
00953 void Expand( float delta )
00954 {
00955 pMin -= Vector( delta, delta, delta );
00956 pMax += Vector( delta, delta, delta );
00957 }
00958
00959
00960 const Point getCenter( ) const
00961 {
00962 return (pMin + pMax) * .5f;
00963 }
00964
00965
00966 void getCenter( Point& center ) const
00967 {
00968 center= getCenter();
00969 }
00970
00971
00972 float Volume( ) const
00973 {
00974 const Vector d(pMin, pMax);
00975 return d.x * d.y * d.z;
00976 }
00977
00978
00979 float SurfaceArea( ) const
00980 {
00981 const Vector d(pMin, pMax);
00982 const float area= 2.f * d.x * d.y + 2.f * d.x * d.z + 2.f * d.y * d.z;
00983 return area;
00984 }
00985
00986
00987 int MaximumExtent( ) const
00988 {
00989 const Vector d(pMin, pMax);
00990 if ( d.x > d.y && d.x > d.z )
00991 return 0;
00992 if ( d.y > d.z )
00993 return 1;
00994 else
00995 return 2;
00996 }
00997
00998
00999 void BoundingSphere( Point &c, float &r ) const;
01000
01001
01002
01003
01004
01005
01006 bool Intersect( const Ray &ray, const float htmax,
01007 float &rtmin, float &rtmax ) const
01008 {
01009 float tmin= ((*this)[ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01010 float tmax= ((*this)[1 - ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01011
01012
01013 const float tymin= ((*this)[ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01014 const float tymax= ((*this)[1 - ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01015
01016 if((tmin > tymax) || (tymin > tmax))
01017 return false;
01018 if(tymin > tmin)
01019 tmin= tymin;
01020 if(tymax < tmax)
01021 tmax= tymax;
01022
01023
01024 const float tzmin= ((*this)[ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01025 const float tzmax= ((*this)[1 - ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01026
01027 if((tmin > tzmax) || (tzmin > tmax))
01028 return false;
01029 if(tzmin > tmin)
01030 tmin= tzmin;
01031 if(tzmax < tmax)
01032 tmax= tzmax;
01033
01034
01035 rtmin= tmin;
01036 rtmax= tmax;
01037 return ((tmin < htmax) && (tmax > ray.tmin));
01038 }
01039
01040 bool Occluded( const Ray& ray ) const
01041 {
01042 float rtmin;
01043 float rtmax;
01044 return Intersect(ray, ray.tmax, rtmin, rtmax);
01045 }
01046
01047
01048 bool Intersect( const Ray &ray, const float htmax,
01049 float &rtmin, float &rtmax, Normal& rn ) const
01050 {
01051 Normal n(-1.f, 0.f, 0.f);
01052 float tmin= ((*this)[ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01053 float tmax= ((*this)[1 - ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01054
01055
01056 const float tymin= ((*this)[ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01057 const float tymax= ((*this)[1 - ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01058
01059 if((tmin > tymax) || (tymin > tmax))
01060 return false;
01061 if(tymin > tmin)
01062 {
01063 tmin= tymin;
01064 n= Normal(0.f, -1.f, 0.f);
01065 }
01066 if(tymax < tmax)
01067 tmax= tymax;
01068
01069
01070 const float tzmin= ((*this)[ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01071 const float tzmax= ((*this)[1 - ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01072
01073 if((tmin > tzmax) || (tzmin > tmax))
01074 return false;
01075 if(tzmin > tmin)
01076 {
01077 tmin= tzmin;
01078 n= Normal(0.f, 0.f, -1.f);
01079 }
01080 if(tzmax < tmax)
01081 tmax= tzmax;
01082
01083
01084 rtmin= tmin;
01085 rtmax= tmax;
01086 rn= n;
01087 return ((tmin < htmax) && (tmax > ray.tmin));
01088 }
01089
01090
01091
01092 bool Intersect( const float *origin, const float *direction,
01093 const float tmin, const float tmax, float *hit0, float *hit1 ) const
01094 {
01095 assert(hit0 != NULL);
01096 assert(hit1 != NULL);
01097
01098 Ray ray( Point(origin[0], origin[1], origin[2]), Vector(direction[0], direction[1], direction[2]) );
01099 return Intersect(ray, tmax, *hit0, *hit1);
01100 }
01101
01102
01103 Point& operator[] ( const int id )
01104 {
01105 assert(id == 0 || id == 1);
01106 return (&pMin)[id];
01107 }
01108
01109
01110 const Point& operator[] ( const int id ) const
01111 {
01112 assert(id == 0 || id == 1);
01113 return (&pMin)[id];
01114 }
01115
01116
01117
01118 Point pMin, pMax;
01119 };
01120
01121
01122 inline
01123 Vector::Vector( const Point &p )
01124 :
01125 x( p.x ), y( p.y ), z( p.z )
01126 {}
01127
01128 inline
01129 Vector::Vector( const Point& p, const Point& q )
01130 :
01131 x( q.x - p.x ), y( q.y - p.y ), z( q.z - p.z )
01132 {}
01133
01134 inline
01135 std::ostream &operator<<( std::ostream &os, const Vector &v )
01136 {
01137 os << v.x << ", " << v.y << ", " << v.z;
01138 return os;
01139 }
01140
01141 inline
01142 Vector operator*( float f, const Vector &v )
01143 {
01144 return v*f;
01145 }
01146
01147
01148 inline
01149 float Dot( const Vector &v1, const Vector &v2 )
01150 {
01151 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
01152 }
01153
01154
01155 inline
01156 float AbsDot( const Vector &v1, const Vector &v2 )
01157 {
01158 return fabsf( Dot( v1, v2 ) );
01159 }
01160
01161
01162 inline
01163 float ZeroDot( const Vector &v1, const Vector &v2 )
01164 {
01165 return std::max( 0.f, Dot( v1, v2 ) );
01166 }
01167
01168
01169 inline
01170 Vector Cross( const Vector &v1, const Vector &v2 )
01171 {
01172 return Vector(
01173 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01174 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01175 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01176 }
01177
01178
01179 inline
01180 Vector Cross( const Vector &v1, const Normal &v2 )
01181 {
01182 return Vector(
01183 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01184 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01185 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01186 }
01187
01188
01189 inline
01190 Vector Cross( const Normal &v1, const Vector &v2 )
01191 {
01192 return Vector(
01193 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01194 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01195 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01196 }
01197
01198
01199 inline
01200 Vector Normalize( const Vector &v )
01201 {
01202 return v / v.Length();
01203 }
01204
01205
01206 inline
01207 void CoordinateSystem( const Vector &v1, Vector *v2, Vector *v3 )
01208 {
01209 if ( fabsf( v1.x ) > fabsf( v1.y ) )
01210 {
01211 float invLen = 1.f / sqrtf( v1.x * v1.x + v1.z * v1.z );
01212 *v2 = Vector( -v1.z * invLen, 0.f, v1.x * invLen );
01213 }
01214 else
01215 {
01216 float invLen = 1.f / sqrtf( v1.y * v1.y + v1.z * v1.z );
01217 *v2 = Vector( 0.f, v1.z * invLen, -v1.y * invLen );
01218 }
01219
01220 *v3 = Cross( v1, *v2 );
01221
01222 }
01223
01224
01225 inline
01226 float Distance( const Point &p1, const Point &p2 )
01227 {
01228 return ( p1 - p2 ).Length();
01229 }
01230
01231
01232 inline
01233 float DistanceSquared( const Point &p1, const Point &p2 )
01234 {
01235 return ( p1 - p2 ).LengthSquared();
01236 }
01237
01238 inline
01239 std::ostream &operator<<( std::ostream &os, const Point &v )
01240 {
01241 os << v.x << ", " << v.y << ", " << v.z;
01242 return os;
01243 }
01244
01245 inline
01246 Point operator*( float f, const Point &p )
01247 {
01248 return p*f;
01249 }
01250
01251 inline
01252 Normal operator*( float f, const Normal &n )
01253 {
01254 return Normal( f*n.x, f*n.y, f*n.z );
01255 }
01256
01257
01258 inline
01259 Normal Normalize( const Normal &n )
01260 {
01261 return n / n.Length();
01262 }
01263
01264 inline
01265 Vector::Vector( const Normal &n )
01266 :
01267 x( n.x ), y( n.y ), z( n.z )
01268 {}
01269
01270
01271 inline
01272 float Dot( const Normal &n1, const Vector &v2 )
01273 {
01274 return n1.x * v2.x + n1.y * v2.y + n1.z * v2.z;
01275 }
01276
01277
01278 inline
01279 float Dot( const Vector &v1, const Normal &n2 )
01280 {
01281 return v1.x * n2.x + v1.y * n2.y + v1.z * n2.z;
01282 }
01283
01284
01285 inline
01286 float Dot( const Normal &n1, const Normal &n2 )
01287 {
01288 return n1.x * n2.x + n1.y * n2.y + n1.z * n2.z;
01289 }
01290
01291
01292 inline
01293 float AbsDot( const Normal &n1, const Vector &v2 )
01294 {
01295 return fabsf( n1.x * v2.x + n1.y * v2.y + n1.z * v2.z );
01296 }
01297
01298
01299 inline
01300 float AbsDot( const Vector &v1, const Normal &n2 )
01301 {
01302 return fabsf( v1.x * n2.x + v1.y * n2.y + v1.z * n2.z );
01303 }
01304
01305
01306 inline
01307 float AbsDot( const Normal &n1, const Normal &n2 )
01308 {
01309 return fabsf( n1.x * n2.x + n1.y * n2.y + n1.z * n2.z );
01310 }
01311
01312
01313 inline
01314 float ZeroDot( const Normal &v1, const Vector &v2 )
01315 {
01316 return std::max( 0.f, Dot( v1, v2 ) );
01317 }
01318
01319
01320 inline
01321 float ZeroDot( const Vector &v1, const Normal &v2 )
01322 {
01323 return std::max( 0.f, Dot( v1, v2 ) );
01324 }
01325
01326
01327 inline
01328 float ZeroDot( const Normal &v1, const Normal &v2 )
01329 {
01330 return std::max( 0.f, Dot( v1, v2 ) );
01331 }
01332
01333 inline
01334 std::ostream &operator<<( std::ostream &os, const Normal &v )
01335 {
01336 os << v.x << ", " << v.y << ", " << v.z;
01337 return os;
01338 }
01339
01340 inline
01341 std::ostream &operator<<( std::ostream &os, Ray &r )
01342 {
01343 os << "org: " << r.o << "dir: " << r.d << " range [" <<
01344 r.tmin << "," << r.tmax << "] ";
01345 return os;
01346 }
01347
01348 inline
01349 std::ostream &operator<<( std::ostream &os, const BBox &b )
01350 {
01351 os << b.pMin << " -> " << b.pMax;
01352 return os;
01353 }
01354
01355
01356 inline
01357 float Lerp( const float t, const float v1, const float v2 )
01358 {
01359 return (1.f - t) * v1 + t * v2;
01360 }
01361
01362
01363 inline
01364 float Clamp( const float value, const float low, const float high)
01365 {
01366 if(value < low)
01367 return low;
01368 else if (value > high)
01369 return high;
01370 else
01371 return value;
01372 }
01373
01374
01375 inline
01376 int Clamp( const int value , const int low, const int high )
01377 {
01378 if(value < low)
01379 return low;
01380 else if (value > high)
01381 return high;
01382 else
01383 return value;
01384 }
01385
01386
01387 inline
01388 float Radians( const float deg )
01389 {
01390 return ( (float) M_PI / 180.f) * deg;
01391 }
01392
01393
01394 inline
01395 float Degrees( const float rad )
01396 {
01397 return (180.f / (float) M_PI) * rad;
01398 }
01399
01400
01401 inline
01402 Vector SphericalDirection( float sintheta, float costheta, float phi )
01403 {
01404 return Vector(
01405 sintheta * cosf( phi ),
01406 sintheta * sinf( phi ),
01407 costheta );
01408 }
01409
01410
01411 inline
01412 Vector SphericalDirection( float sintheta, float costheta, float phi,
01413 const Vector &x, const Vector &y, const Vector &z )
01414 {
01415 return sintheta * cosf( phi ) * x
01416 + sintheta * sinf( phi ) * y
01417 + costheta * z;
01418 }
01419
01420
01421 inline
01422 float SphericalTheta( const Vector &v )
01423 {
01424 return acosf( Clamp( v.z, -1.f, 1.f ) );
01425 }
01426
01427
01428 inline
01429 float SphericalPhi( const Vector &v )
01430 {
01431 float p = atan2f( v.y, v.x );
01432 return ( p < 0.f ) ? p + 2.f*M_PI : p;
01433 }
01434
01435 }
01436
01437 #endif // PBRT_GEOMETRY_H