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( (float) HUGE_VAL, (float) HUGE_VAL, (float) HUGE_VAL );
00836 pMax = Point( (float) -HUGE_VAL, (float) -HUGE_VAL, (float) -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 void Intersection( const BBox& b )
00945 {
00946 pMin.x= std::max(pMin.x, b.pMin.x);
00947 pMin.y= std::max(pMin.y, b.pMin.y);
00948 pMin.z= std::max(pMin.z, b.pMin.z);
00949 pMax.x= std::min(pMax.x, b.pMax.x);
00950 pMax.y= std::min(pMax.y, b.pMax.y);
00951 pMax.z= std::min(pMax.z, b.pMax.z);
00952 }
00953
00954 friend BBox Intersection( const BBox& a, const BBox& b );
00955
00956 bool isEmpty( ) const
00957 {
00958 bool x = ( pMax.x < pMin.x );
00959 bool y = ( pMax.y < pMin.y );
00960 bool z = ( pMax.z < pMin.z );
00961 return ( x && y && z );
00962 }
00963
00964
00965 bool Inside( const Point &pt ) const
00966 {
00967 return ( pt.x >= pMin.x && pt.x <= pMax.x
00968 && pt.y >= pMin.y && pt.y <= pMax.y
00969 && pt.z >= pMin.z && pt.z <= pMax.z );
00970 }
00971
00972
00973 void Expand( float delta )
00974 {
00975 pMin -= Vector( delta, delta, delta );
00976 pMax += Vector( delta, delta, delta );
00977 }
00978
00979
00980 const Point getCenter( ) const
00981 {
00982 return (pMin + pMax) * .5f;
00983 }
00984
00985
00986 void getCenter( Point& center ) const
00987 {
00988 center= getCenter();
00989 }
00990
00991
00992 float Volume( ) const
00993 {
00994 const Vector d(pMin, pMax);
00995 return d.x * d.y * d.z;
00996 }
00997
00998
00999 float SurfaceArea( ) const
01000 {
01001 const Vector d(pMin, pMax);
01002 const float area= 2.f * d.x * d.y + 2.f * d.x * d.z + 2.f * d.y * d.z;
01003 return area;
01004 }
01005
01006
01007 int MaximumExtent( ) const
01008 {
01009 const Vector d(pMin, pMax);
01010 if ( d.x > d.y && d.x > d.z )
01011 return 0;
01012 if ( d.y > d.z )
01013 return 1;
01014 else
01015 return 2;
01016 }
01017
01018
01019 void BoundingSphere( Point &c, float &r ) const;
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042 bool Intersect( const Ray &ray, const float htmax,
01043 float &rtmin, float &rtmax ) const
01044 {
01045 float tmin= ((*this)[ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01046 float tmax= ((*this)[1 - ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01047
01048
01049 const float tymin= ((*this)[ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01050 const float tymax= ((*this)[1 - ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01051
01052 if((tmin > tymax) || (tymin > tmax))
01053 return false;
01054 if(tymin > tmin)
01055 tmin= tymin;
01056 if(tymax < tmax)
01057 tmax= tymax;
01058
01059
01060 const float tzmin= ((*this)[ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01061 const float tzmax= ((*this)[1 - ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01062
01063 if((tmin > tzmax) || (tzmin > tmax))
01064 return false;
01065 if(tzmin > tmin)
01066 tmin= tzmin;
01067 if(tzmax < tmax)
01068 tmax= tzmax;
01069
01070
01071 rtmin= tmin;
01072 rtmax= tmax;
01073 return ((tmin < htmax) && (tmax > ray.tmin));
01074 }
01075
01076 bool Occluded( const Ray& ray ) const
01077 {
01078 float rtmin;
01079 float rtmax;
01080 return Intersect(ray, ray.tmax, rtmin, rtmax);
01081 }
01082
01083 bool Occluded( const Ray& ray, const float tmax ) const
01084 {
01085 float rtmin;
01086 float rtmax;
01087 return Intersect(ray, tmax, rtmin, rtmax);
01088 }
01089
01090
01091 bool Intersect( const Ray &ray, const float htmax,
01092 float &rtmin, float &rtmax, Normal& rn ) const
01093 {
01094 Normal n(-1.f, 0.f, 0.f);
01095 float tmin= ((*this)[ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01096 float tmax= ((*this)[1 - ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01097
01098
01099 const float tymin= ((*this)[ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01100 const float tymax= ((*this)[1 - ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01101
01102 if((tmin > tymax) || (tymin > tmax))
01103 return false;
01104 if(tymin > tmin)
01105 {
01106 tmin= tymin;
01107 n= Normal(0.f, -1.f, 0.f);
01108 }
01109 if(tymax < tmax)
01110 tmax= tymax;
01111
01112
01113 const float tzmin= ((*this)[ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01114 const float tzmax= ((*this)[1 - ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01115
01116 if((tmin > tzmax) || (tzmin > tmax))
01117 return false;
01118 if(tzmin > tmin)
01119 {
01120 tmin= tzmin;
01121 n= Normal(0.f, 0.f, -1.f);
01122 }
01123 if(tzmax < tmax)
01124 tmax= tzmax;
01125
01126
01127 rtmin= tmin;
01128 rtmax= tmax;
01129 rn= n;
01130 return ((tmin < htmax) && (tmax > ray.tmin));
01131 }
01132
01133
01134
01135 Point& operator[] ( const int id )
01136 {
01137 assert(id == 0 || id == 1);
01138 return (&pMin)[id];
01139 }
01140
01141
01142 const Point& operator[] ( const int id ) const
01143 {
01144 assert(id == 0 || id == 1);
01145 return (&pMin)[id];
01146 }
01147
01148
01149
01150 Point pMin, pMax;
01151 };
01152
01153
01154 inline
01155 Vector::Vector( const Point &p )
01156 :
01157 x( p.x ), y( p.y ), z( p.z )
01158 {}
01159
01160 inline
01161 Vector::Vector( const Point& p, const Point& q )
01162 :
01163 x( q.x - p.x ), y( q.y - p.y ), z( q.z - p.z )
01164 {}
01165
01166 inline
01167 std::ostream &operator<<( std::ostream &os, const Vector &v )
01168 {
01169 os << v.x << ", " << v.y << ", " << v.z;
01170 return os;
01171 }
01172
01173 inline
01174 Vector operator*( float f, const Vector &v )
01175 {
01176 return v*f;
01177 }
01178
01179
01180 inline
01181 float Dot( const Vector &v1, const Vector &v2 )
01182 {
01183 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
01184 }
01185
01186
01187 inline
01188 float AbsDot( const Vector &v1, const Vector &v2 )
01189 {
01190 return fabsf( Dot( v1, v2 ) );
01191 }
01192
01193
01194 inline
01195 float ZeroDot( const Vector &v1, const Vector &v2 )
01196 {
01197 return std::max( 0.f, Dot( v1, v2 ) );
01198 }
01199
01200
01201 inline
01202 Vector Cross( const Vector &v1, const Vector &v2 )
01203 {
01204 return Vector(
01205 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01206 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01207 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01208 }
01209
01210
01211 inline
01212 Vector Cross( const Vector &v1, const Normal &v2 )
01213 {
01214 return Vector(
01215 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01216 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01217 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01218 }
01219
01220
01221 inline
01222 Vector Cross( const Normal &v1, const Vector &v2 )
01223 {
01224 return Vector(
01225 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01226 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01227 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01228 }
01229
01230
01231 inline
01232 Vector Normalize( const Vector &v )
01233 {
01234 return v / v.Length();
01235 }
01236
01237
01238 inline
01239 void CoordinateSystem( const Vector &v1, Vector *v2, Vector *v3 )
01240 {
01241 if ( fabsf( v1.x ) > fabsf( v1.y ) )
01242 {
01243 float invLen = 1.f / sqrtf( v1.x * v1.x + v1.z * v1.z );
01244 *v2 = Vector( -v1.z * invLen, 0.f, v1.x * invLen );
01245 }
01246 else
01247 {
01248 float invLen = 1.f / sqrtf( v1.y * v1.y + v1.z * v1.z );
01249 *v2 = Vector( 0.f, v1.z * invLen, -v1.y * invLen );
01250 }
01251
01252 *v3 = Cross( v1, *v2 );
01253
01254 }
01255
01256
01257 inline
01258 float Distance( const Point &p1, const Point &p2 )
01259 {
01260 return ( p1 - p2 ).Length();
01261 }
01262
01263
01264 inline
01265 float DistanceSquared( const Point &p1, const Point &p2 )
01266 {
01267 return ( p1 - p2 ).LengthSquared();
01268 }
01269
01270 inline
01271 std::ostream &operator<<( std::ostream &os, const Point &v )
01272 {
01273 os << v.x << ", " << v.y << ", " << v.z;
01274 return os;
01275 }
01276
01277 inline
01278 Point operator*( float f, const Point &p )
01279 {
01280 return p*f;
01281 }
01282
01283 inline
01284 Normal operator*( float f, const Normal &n )
01285 {
01286 return Normal( f*n.x, f*n.y, f*n.z );
01287 }
01288
01289
01290 inline
01291 Normal Normalize( const Normal &n )
01292 {
01293 return n / n.Length();
01294 }
01295
01296 inline
01297 Vector::Vector( const Normal &n )
01298 :
01299 x( n.x ), y( n.y ), z( n.z )
01300 {}
01301
01302
01303 inline
01304 float Dot( const Normal &n1, const Vector &v2 )
01305 {
01306 return n1.x * v2.x + n1.y * v2.y + n1.z * v2.z;
01307 }
01308
01309
01310 inline
01311 float Dot( const Vector &v1, const Normal &n2 )
01312 {
01313 return v1.x * n2.x + v1.y * n2.y + v1.z * n2.z;
01314 }
01315
01316
01317 inline
01318 float Dot( const Normal &n1, const Normal &n2 )
01319 {
01320 return n1.x * n2.x + n1.y * n2.y + n1.z * n2.z;
01321 }
01322
01323
01324 inline
01325 float AbsDot( const Normal &n1, const Vector &v2 )
01326 {
01327 return fabsf( n1.x * v2.x + n1.y * v2.y + n1.z * v2.z );
01328 }
01329
01330
01331 inline
01332 float AbsDot( const Vector &v1, const Normal &n2 )
01333 {
01334 return fabsf( v1.x * n2.x + v1.y * n2.y + v1.z * n2.z );
01335 }
01336
01337
01338 inline
01339 float AbsDot( const Normal &n1, const Normal &n2 )
01340 {
01341 return fabsf( n1.x * n2.x + n1.y * n2.y + n1.z * n2.z );
01342 }
01343
01344
01345 inline
01346 float ZeroDot( const Normal &v1, const Vector &v2 )
01347 {
01348 return std::max( 0.f, Dot( v1, v2 ) );
01349 }
01350
01351
01352 inline
01353 float ZeroDot( const Vector &v1, const Normal &v2 )
01354 {
01355 return std::max( 0.f, Dot( v1, v2 ) );
01356 }
01357
01358
01359 inline
01360 float ZeroDot( const Normal &v1, const Normal &v2 )
01361 {
01362 return std::max( 0.f, Dot( v1, v2 ) );
01363 }
01364
01365 inline
01366 std::ostream &operator<<( std::ostream &os, const Normal &v )
01367 {
01368 os << v.x << ", " << v.y << ", " << v.z;
01369 return os;
01370 }
01371
01372 inline
01373 std::ostream &operator<<( std::ostream &os, Ray &r )
01374 {
01375 os << "org: " << r.o << "dir: " << r.d << " range [" <<
01376 r.tmin << "," << r.tmax << "] ";
01377 return os;
01378 }
01379
01380 inline
01381 std::ostream &operator<<( std::ostream &os, const BBox &b )
01382 {
01383 os << b.pMin << " -> " << b.pMax;
01384 return os;
01385 }
01386
01387
01388 inline
01389 float Lerp( const float t, const float v1, const float v2 )
01390 {
01391 return (1.f - t) * v1 + t * v2;
01392 }
01393
01394
01395 inline
01396 float Clamp( const float value, const float low, const float high)
01397 {
01398 if(value < low)
01399 return low;
01400 else if (value > high)
01401 return high;
01402 else
01403 return value;
01404 }
01405
01406
01407 inline
01408 int Clamp( const int value , const int low, const int high )
01409 {
01410 if(value < low)
01411 return low;
01412 else if (value > high)
01413 return high;
01414 else
01415 return value;
01416 }
01417
01418
01419 inline
01420 float Radians( const float deg )
01421 {
01422 return ( (float) M_PI / 180.f) * deg;
01423 }
01424
01425
01426 inline
01427 float Degrees( const float rad )
01428 {
01429 return (180.f / (float) M_PI) * rad;
01430 }
01431
01432
01433 inline
01434 Vector SphericalDirection( float sintheta, float costheta, float phi )
01435 {
01436 return Vector(
01437 sintheta * cosf( phi ),
01438 sintheta * sinf( phi ),
01439 costheta );
01440 }
01441
01442
01443 inline
01444 Vector SphericalDirection( float sintheta, float costheta, float phi,
01445 const Vector &x, const Vector &y, const Vector &z )
01446 {
01447 return sintheta * cosf( phi ) * x
01448 + sintheta * sinf( phi ) * y
01449 + costheta * z;
01450 }
01451
01452
01453 inline
01454 float SphericalTheta( const Vector &v )
01455 {
01456 return acosf( Clamp( v.z, -1.f, 1.f ) );
01457 }
01458
01459
01460 inline
01461 float SphericalPhi( const Vector &v )
01462 {
01463 float p = atan2f( v.y, v.x );
01464 return ( p < 0.f ) ? p + 2.f*M_PI : p;
01465 }
01466
01467 }
01468
01469 #endif // PBRT_GEOMETRY_H