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( const float _r = 0.f, const float _g = 0.f, const float _b = 0.f, const float _a= 1.f )
00296 :
00297 r( _r ), g( _g ), b( _b ), a( _a )
00298 {}
00299
00300
00301 void print( ) const
00302 {
00303 printf("% -.2f % -.2f % -.2f % -.2f\n", r, g, b, a);
00304 }
00305
00306
00307 Color operator+( const Color &v ) const
00308 {
00309 return Color( r + v.r, g + v.g, b + v.b, a + v.a );
00310 }
00311
00312
00313 Color& operator+=( const Color &v )
00314 {
00315 r += v.r;
00316 g += v.g;
00317 b += v.b;
00318 a+= v.a;
00319
00320 return *this;
00321 }
00322
00323
00324 Color operator-( const Color &v ) const
00325 {
00326 return Color( r - v.r, g - v.g, b - v.b, a - v.a );
00327 }
00328
00329
00330 Color& operator-=( const Color &v )
00331 {
00332 r -= v.r;
00333 g -= v.g;
00334 b -= v.b;
00335 a -= v.a;
00336
00337 return *this;
00338 }
00339
00340 Color operator*( const Color& v ) const
00341 {
00342 return Color( r * v.r, g * v.g, b * v.b, a * v.a );
00343 }
00344
00345 Color& operator*=( const Color& v)
00346 {
00347 r *= v.r;
00348 g *= v.g;
00349 b *= v.b;
00350 a *= v.a;
00351
00352 return *this;
00353 }
00354
00355
00356 bool operator==( const Color &v ) const
00357 {
00358 return (r == v.r && g == v.g && b == v.b && a == v.a );
00359 }
00360
00361
00362 Color operator*( const float f ) const
00363 {
00364 return Color( f*r, f*g, f*b, f*a );
00365 }
00366
00367
00368 Color &operator*=( const float f )
00369 {
00370 r *= f;
00371 g *= f;
00372 b *= f;
00373 a *= f;
00374
00375 return *this;
00376 }
00377
00378
00379 Color operator/( const float f ) const
00380 {
00381 assert( f != 0 );
00382 float inv = 1.f / f;
00383 return Color( r * inv, g * inv, b * inv, a * inv );
00384 }
00385
00386
00387 Color &operator/=( const float f )
00388 {
00389 assert( f != 0 );
00390 float inv = 1.f / f;
00391 r *= inv;
00392 g *= inv;
00393 b *= inv;
00394 a *= inv;
00395
00396 return *this;
00397 }
00398
00399
00400 Color operator-( ) const
00401 {
00402 return Color( -r, -g, -b, -a );
00403 }
00404
00405
00406 const float& operator[]( const unsigned int i ) const
00407 {
00408 return ( &r )[i];
00409 }
00410
00411
00412 float &operator[]( const unsigned int i )
00413 {
00414 return ( &r )[i];
00415 }
00416
00417
00418
00419 float r, g, b, a;
00420 };
00421
00422
00423 typedef Color Energy;
00424
00425
00426
00427
00428 class Point
00429 {
00430 public:
00431
00432 Point( const float _x = 0.f, const float _y = 0.f, const float _z = 0.f )
00433 :
00434 x( _x ), y( _y ), z( _z )
00435 {}
00436
00437 explicit Point( const Vector &v )
00438 :
00439 x( v.x ), y( v.y ), z( v.z )
00440 {}
00441
00442
00443 void print( ) const
00444 {
00445 printf("% -.8f % -.8f % -.8f\n", x, y, z);
00446 }
00447
00448
00449 Point operator+( const Vector &v ) const
00450 {
00451 return Point( x + v.x, y + v.y, z + v.z );
00452 }
00453
00454
00455 Point &operator+=( const Vector &v )
00456 {
00457 x += v.x;
00458 y += v.y;
00459 z += v.z;
00460
00461 return *this;
00462 }
00463
00464
00465 Vector operator-( const Point &q ) const
00466 {
00467 return Vector( x - q.x, y - q.y, z - q.z );
00468 }
00469
00470
00471 Point operator-( const Vector &v ) const
00472 {
00473 return Point( x - v.x, y - v.y, z - v.z );
00474 }
00475
00476
00477 Point &operator-=( const Vector &v )
00478 {
00479 x -= v.x;
00480 y -= v.y;
00481 z -= v.z;
00482
00483 return *this;
00484 }
00485
00486
00487 Point &operator+=( const Point &q )
00488 {
00489 x += q.x;
00490 y += q.y;
00491 z += q.z;
00492
00493 return *this;
00494 }
00495
00496 Point operator+( const Point &q ) const
00497 {
00498 return Point( x + q.x, y + q.y, z + q.z );
00499 }
00500
00501 Point operator*( const float f ) const
00502 {
00503 return Point( f*x, f*y, f*z );
00504 }
00505
00506 Point &operator*=( const float f )
00507 {
00508 x *= f;
00509 y *= f;
00510 z *= f;
00511
00512 return *this;
00513 }
00514
00515 Point operator/ ( const float f ) const
00516 {
00517 float inv = 1.f / f;
00518 return Point( inv*x, inv*y, inv*z );
00519 }
00520
00521 Point &operator/=( const float f )
00522 {
00523 float inv = 1.f / f;
00524 x *= inv;
00525 y *= inv;
00526 z *= inv;
00527
00528 return *this;
00529 }
00530
00531 const float& operator[]( const unsigned int i ) const
00532 {
00533 return ( &x )[i];
00534 }
00535
00536 float &operator[]( const unsigned int i )
00537 {
00538 return ( &x )[i];
00539 }
00540
00541
00542 float x, y, z;
00543 };
00544
00545
00546 class HPoint
00547 {
00548 public:
00549
00550
00551 HPoint( const float _x = 0.f, const float _y = 0.f, const float _z = 0.f, const float _w= 1.f )
00552 :
00553 x( _x ), y( _y ), z( _z ), w( _w )
00554 {}
00555
00556
00557 HPoint( const Point& p )
00558 :
00559 x(p.x), y(p.y), z(p.z), w(1.f)
00560 {}
00561
00562
00563 Point project( ) const
00564 {
00565 assert( w != 0.f );
00566 return Point(x / w, y / w, z / w);
00567 }
00568
00569
00570 bool isVisible( ) const
00571 {
00572 return
00573 (-w < x && x < w
00574 && -w < y && y < w
00575 && -w < z && z < w);
00576 }
00577
00578
00579 bool isCulled( ) const
00580 {
00581 return !isVisible();
00582 }
00583
00584
00585 const float& operator[]( const unsigned int i ) const
00586 {
00587 return ( &x )[i];
00588 }
00589
00590
00591 float &operator[]( const unsigned int i )
00592 {
00593 return ( &x )[i];
00594 }
00595
00596
00597 float x, y, z, w;
00598 };
00599
00600
00601 class Normal
00602 {
00603 public:
00604
00605 Normal( const float _x = 0.f, const float _y = 0.f, const float _z = 0.f )
00606 :
00607 x( _x ), y( _y ), z( _z )
00608 {}
00609
00610 Normal operator-( ) const
00611 {
00612 return Normal( -x, -y, -z );
00613 }
00614
00615 Normal operator+ ( const Normal &v ) const
00616 {
00617 return Normal( x + v.x, y + v.y, z + v.z );
00618 }
00619
00620 Normal& operator+=( const Normal &v )
00621 {
00622 x += v.x;
00623 y += v.y;
00624 z += v.z;
00625
00626 return *this;
00627 }
00628
00629 Normal operator- ( const Normal &v ) const
00630 {
00631 return Normal( x - v.x, y - v.y, z - v.z );
00632 }
00633
00634 Normal& operator-=( const Normal &v )
00635 {
00636 x -= v.x;
00637 y -= v.y;
00638 z -= v.z;
00639
00640 return *this;
00641 }
00642
00643 Normal operator*( const float f ) const
00644 {
00645 return Normal( f*x, f*y, f*z );
00646 }
00647
00648 Normal &operator*=( const float f )
00649 {
00650 x *= f;
00651 y *= f;
00652 z *= f;
00653
00654 return *this;
00655 }
00656
00657 Normal operator/ ( const float f ) const
00658 {
00659 float inv = 1.f / f;
00660 return Normal( x * inv, y * inv, z * inv );
00661 }
00662
00663 Normal &operator/=( float f )
00664 {
00665 float inv = 1.f / f;
00666 x *= inv;
00667 y *= inv;
00668 z *= inv;
00669
00670 return *this;
00671 }
00672
00673 float LengthSquared() const
00674 {
00675 return x*x + y*y + z*z;
00676 }
00677
00678 float Length() const
00679 {
00680 return sqrtf( LengthSquared() );
00681 }
00682
00683 explicit Normal( const Vector &v )
00684 :
00685 x( v.x ), y( v.y ), z( v.z )
00686 {}
00687
00688 const float& operator[]( const unsigned int i ) const
00689 {
00690 return ( &x )[i];
00691 }
00692
00693 float &operator[]( const unsigned int i )
00694 {
00695 return ( &x )[i];
00696 }
00697
00698
00699 float x, y, z;
00700 };
00701
00702 typedef Normal Tangent;
00703
00704
00705
00706 struct Ray
00707 {
00708
00709 Ray()
00710 :
00711 tmin( RAY_EPSILON ), tmax( HUGE_VAL ),
00712 dnear(0.f), radius(0.f)
00713 {}
00714
00715
00716 Ray( const Point &origin, const Vector &direction,
00717 const float start = RAY_EPSILON, const float end = HUGE_VAL )
00718 :
00719 o( origin ), d( direction ), tmin( start ), tmax( end ),
00720 dnear(0.f), radius(0.f)
00721 {
00722 inv_d= Vector(1.f / d.x, 1.f / d.y, 1.f / d.z);
00723 sign_d[0]= (inv_d[0] < 0.f) ? 1 : 0;
00724 sign_d[1]= (inv_d[1] < 0.f) ? 1 : 0;
00725 sign_d[2]= (inv_d[2] < 0.f) ? 1 : 0;
00726 }
00727
00728
00729 Ray( const Point &origin, const Point& destination,
00730 const float start = RAY_EPSILON, const float end = 1.f - RAY_EPSILON )
00731 :
00732 o( origin ), d( origin, destination ), tmin(start), tmax( end ),
00733 dnear(0.f), radius(0.f)
00734 {
00735 inv_d= Vector(1.f / d.x, 1.f / d.y, 1.f / d.z);
00736 sign_d[0]= (inv_d[0] < 0.f) ? 1 : 0;
00737 sign_d[1]= (inv_d[1] < 0.f) ? 1 : 0;
00738 sign_d[2]= (inv_d[2] < 0.f) ? 1 : 0;
00739 }
00740
00741
00742 Point operator( )( const float t ) const
00743 {
00744 return o + d * t;
00745 }
00746
00747
00748 bool isBackward( const int axis ) const
00749 {
00750 return (sign_d[axis]);
00751 }
00752
00753
00754 Point o;
00755 Vector d;
00756 Vector inv_d;
00757 unsigned char sign_d[4];
00758 float tmin, tmax;
00759 float dnear;
00760 float radius;
00761 };
00762
00763
00764 struct Hit
00765 {
00766 Hit( )
00767 :
00768 tmin(RAY_EPSILON),
00769 t(HUGE_VAL),
00770 object_id(-1),
00771 node_id(-1),
00772 child_id(-1),
00773 user_data(0.f)
00774 {}
00775
00776 Hit( const Ray& ray )
00777 :
00778 tmin(ray.tmin),
00779 t(ray.tmax),
00780 object_id(-1),
00781 node_id(-1),
00782 child_id(-1),
00783 user_data(0.f)
00784 {}
00785
00786 ~Hit( ) {}
00787
00788
00789 Point p;
00790 Normal n;
00791 Tangent t1, t2;
00792 float tmin;
00793 float t;
00794 float u, v;
00795 int object_id;
00796 int node_id;
00797 int child_id;
00798 float user_data;
00799 };
00800
00801
00802
00803 class BBox
00804 {
00805 public:
00806
00807
00808 BBox()
00809 {
00810 clear();
00811 }
00812
00813
00814 void clear( )
00815 {
00816 pMin = Point( HUGE_VAL, HUGE_VAL, HUGE_VAL );
00817 pMax = Point( -HUGE_VAL, -HUGE_VAL, -HUGE_VAL );
00818 }
00819
00820
00821 BBox( const Point &p )
00822 :
00823 pMin( p ), pMax( p )
00824 { }
00825
00826
00827 BBox( const Point &p1, const Point &p2 )
00828 {
00829 pMin = Point(
00830 std::min( p1.x, p2.x ),
00831 std::min( p1.y, p2.y ),
00832 std::min( p1.z, p2.z ) );
00833
00834 pMax = Point(
00835 std::max( p1.x, p2.x ),
00836 std::max( p1.y, p2.y ),
00837 std::max( p1.z, p2.z ) );
00838 }
00839
00840 friend inline std::ostream &
00841 operator<<( std::ostream &os, const BBox &b );
00842
00843 void print( ) const
00844 {
00845 printf("[ %f %f %f ] x [ %f %f %f ] ",
00846 pMin.x, pMin.y, pMin.z,
00847 pMax.x, pMax.y, pMax.z);
00848
00849 Vector d(pMin, pMax);
00850 printf("extents (%f %f %f) ",
00851 d.x, d.y, d.z);
00852 }
00853
00854
00855 friend BBox Union( const BBox &b, const Point &p );
00856
00857
00858 friend BBox Union( const BBox &b, const BBox &b2 );
00859
00860
00861 void Union( const BBox& bbox )
00862 {
00863 #if 0
00864 pMin.x= std::min(pMin.x, bbox.pMin.x);
00865 pMin.y= std::min(pMin.y, bbox.pMin.y);
00866 pMin.z= std::min(pMin.z, bbox.pMin.z);
00867 pMax.x= std::max(pMax.x, bbox.pMax.x);
00868 pMax.y= std::max(pMax.y, bbox.pMax.y);
00869 pMax.z= std::max(pMax.z, bbox.pMax.z);
00870 #else
00871
00872 if(bbox.pMin.x < pMin.x)
00873 pMin.x= bbox.pMin.x;
00874 if(bbox.pMin.y < pMin.y)
00875 pMin.y= bbox.pMin.y;
00876 if(bbox.pMin.z < pMin.z)
00877 pMin.z= bbox.pMin.z;
00878
00879 if(bbox.pMax.x > pMax.x)
00880 pMax.x= bbox.pMax.x;
00881 if(bbox.pMax.y > pMax.y)
00882 pMax.y= bbox.pMax.y;
00883 if(bbox.pMax.z > pMax.z)
00884 pMax.z= bbox.pMax.z;
00885 #endif
00886 }
00887
00888
00889 void Union( const Point& p )
00890 {
00891 #if 0
00892 pMin.x= std::min(pMin.x, p.x);
00893 pMin.y= std::min(pMin.y, p.y);
00894 pMin.z= std::min(pMin.z, p.z);
00895 pMax.x= std::max(pMax.x, p.x);
00896 pMax.y= std::max(pMax.y, p.y);
00897 pMax.z= std::max(pMax.z, p.z);
00898 #else
00899
00900 if(p.x < pMin.x)
00901 pMin.x= p.x;
00902 if(p.y < pMin.y)
00903 pMin.y= p.y;
00904 if(p.z < pMin.z)
00905 pMin.z= p.z;
00906
00907 if(p.x > pMax.x)
00908 pMax.x= p.x;
00909 if(p.y > pMax.y)
00910 pMax.y= p.y;
00911 if(p.z > pMax.z)
00912 pMax.z= p.z;
00913 #endif
00914 }
00915
00916
00917 bool Overlaps( const BBox &b ) const
00918 {
00919 bool x = ( pMax.x >= b.pMin.x ) && ( pMin.x <= b.pMax.x );
00920 bool y = ( pMax.y >= b.pMin.y ) && ( pMin.y <= b.pMax.y );
00921 bool z = ( pMax.z >= b.pMin.z ) && ( pMin.z <= b.pMax.z );
00922 return ( x && y && z );
00923 }
00924
00925
00926 bool Inside( const Point &pt ) const
00927 {
00928 return ( pt.x >= pMin.x && pt.x <= pMax.x
00929 && pt.y >= pMin.y && pt.y <= pMax.y
00930 && pt.z >= pMin.z && pt.z <= pMax.z );
00931 }
00932
00933
00934 void Expand( float delta )
00935 {
00936 pMin -= Vector( delta, delta, delta );
00937 pMax += Vector( delta, delta, delta );
00938 }
00939
00940
00941 const Point getCenter( ) const
00942 {
00943 return (pMin + pMax) / 2.f;
00944 }
00945
00946
00947 void getCenter( Point& center )
00948 {
00949 center= (pMin + pMax) / 2.f;
00950 }
00951
00952
00953 float Volume( ) const
00954 {
00955 const Vector d(pMin, pMax);
00956 return d.x * d.y * d.z;
00957 }
00958
00959
00960 float SurfaceArea( ) const
00961 {
00962 const Vector d(pMin, pMax);
00963 const float area= 2.f * d.x * d.y + 2.f * d.x * d.z + 2.f * d.y * d.z;
00964 return area;
00965 }
00966
00967
00968 int MaximumExtent( ) const
00969 {
00970 const Vector d(pMin, pMax);
00971 if ( d.x > d.y && d.x > d.z )
00972 return 0;
00973 if ( d.y > d.z )
00974 return 1;
00975 else
00976 return 2;
00977 }
00978
00979
00980 void BoundingSphere( Point &c, float &r ) const;
00981
00982
00983
00984
00985
00986
00987 bool Intersect( const Ray &ray,
00988 const float htmin, const float htmax,
00989 float &rtmin, float &rtmax ) const
00990 {
00991 float tmin= ((*this)[ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
00992 float tmax= ((*this)[1 - ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
00993
00994
00995 const float tymin= ((*this)[ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
00996 const float tymax= ((*this)[1 - ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
00997
00998 if((tmin > tymax) || (tymin > tmax))
00999 return false;
01000 if(tymin > tmin)
01001 tmin= tymin;
01002 if(tymax < tmax)
01003 tmax= tymax;
01004
01005
01006 const float tzmin= ((*this)[ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01007 const float tzmax= ((*this)[1 - ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01008
01009 if((tmin > tzmax) || (tzmin > tmax))
01010 return false;
01011 if(tzmin > tmin)
01012 tmin= tzmin;
01013 if(tzmax < tmax)
01014 tmax= tzmax;
01015
01016
01017 rtmin= tmin;
01018 rtmax= tmax;
01019 return ((tmin < htmax) && (tmax > htmin));
01020 }
01021
01022
01023 bool Intersect( const Ray &ray, const float htmin, const float htmax,
01024 float &rtmin, float &rtmax, Normal& rn ) const
01025 {
01026 Normal n(-1.f, 0.f, 0.f);
01027 float tmin= ((*this)[ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01028 float tmax= ((*this)[1 - ray.sign_d[0]].x - ray.o.x) * ray.inv_d.x;
01029
01030
01031 const float tymin= ((*this)[ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01032 const float tymax= ((*this)[1 - ray.sign_d[1]].y - ray.o.y) * ray.inv_d.y;
01033
01034 if((tmin > tymax) || (tymin > tmax))
01035 return false;
01036 if(tymin > tmin)
01037 {
01038 tmin= tymin;
01039 n= Normal(0.f, -1.f, 0.f);
01040 }
01041 if(tymax < tmax)
01042 tmax= tymax;
01043
01044
01045 const float tzmin= ((*this)[ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01046 const float tzmax= ((*this)[1 - ray.sign_d[2]].z - ray.o.z) * ray.inv_d.z;
01047
01048 if((tmin > tzmax) || (tzmin > tmax))
01049 return false;
01050 if(tzmin > tmin)
01051 {
01052 tmin= tzmin;
01053 n= Normal(0.f, 0.f, -1.f);
01054 }
01055 if(tzmax < tmax)
01056 tmax= tzmax;
01057
01058
01059 rtmin= tmin;
01060 rtmax= tmax;
01061 rn= n;
01062 return ((tmin < htmax) && (tmax > htmin));
01063 }
01064
01065
01066
01067 bool Intersect( const float *origin, const float *direction,
01068 const float tmin, const float tmax, float *hit0, float *hit1 )
01069 {
01070 assert(hit0 != NULL);
01071 assert(hit1 != NULL);
01072
01073 Ray ray( Point(origin[0], origin[1], origin[2]), Vector(direction[0], direction[1], direction[2]) );
01074 return Intersect(ray, tmin, tmax, *hit0, *hit1);
01075 }
01076
01077
01078 Point& operator[] ( const int id )
01079 {
01080 assert(id == 0 || id == 1);
01081 return (&pMin)[id];
01082 }
01083
01084
01085 const Point& operator[] ( const int id ) const
01086 {
01087 assert(id == 0 || id == 1);
01088 return (&pMin)[id];
01089 }
01090
01091
01092
01093 Point pMin, pMax;
01094 };
01095
01096
01097 inline
01098 Vector::Vector( const Point &p )
01099 :
01100 x( p.x ), y( p.y ), z( p.z )
01101 {}
01102
01103 inline
01104 Vector::Vector( const Point& p, const Point& q )
01105 :
01106 x( q.x - p.x ), y( q.y - p.y ), z( q.z - p.z )
01107 {}
01108
01109 inline
01110 std::ostream &operator<<( std::ostream &os, const Vector &v )
01111 {
01112 os << v.x << ", " << v.y << ", " << v.z;
01113 return os;
01114 }
01115
01116 inline
01117 Vector operator*( float f, const Vector &v )
01118 {
01119 return v*f;
01120 }
01121
01122
01123 inline
01124 float Dot( const Vector &v1, const Vector &v2 )
01125 {
01126 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
01127 }
01128
01129
01130 inline
01131 float AbsDot( const Vector &v1, const Vector &v2 )
01132 {
01133 return fabsf( Dot( v1, v2 ) );
01134 }
01135
01136
01137 inline
01138 float ZeroDot( const Vector &v1, const Vector &v2 )
01139 {
01140 return std::max( 0.f, Dot( v1, v2 ) );
01141 }
01142
01143
01144 inline
01145 Vector Cross( const Vector &v1, const Vector &v2 )
01146 {
01147 return Vector(
01148 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01149 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01150 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01151 }
01152
01153
01154 inline
01155 Vector Cross( const Vector &v1, const Normal &v2 )
01156 {
01157 return Vector(
01158 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01159 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01160 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01161 }
01162
01163
01164 inline
01165 Vector Cross( const Normal &v1, const Vector &v2 )
01166 {
01167 return Vector(
01168 ( v1.y * v2.z ) - ( v1.z * v2.y ),
01169 ( v1.z * v2.x ) - ( v1.x * v2.z ),
01170 ( v1.x * v2.y ) - ( v1.y * v2.x ) );
01171 }
01172
01173
01174 inline
01175 Vector Normalize( const Vector &v )
01176 {
01177 return v / v.Length();
01178 }
01179
01180
01181 inline
01182 void CoordinateSystem( const Vector &v1, Vector *v2, Vector *v3 )
01183 {
01184 if ( fabsf( v1.x ) > fabsf( v1.y ) )
01185 {
01186 float invLen = 1.f / sqrtf( v1.x * v1.x + v1.z * v1.z );
01187 *v2 = Vector( -v1.z * invLen, 0.f, v1.x * invLen );
01188 }
01189 else
01190 {
01191 float invLen = 1.f / sqrtf( v1.y * v1.y + v1.z * v1.z );
01192 *v2 = Vector( 0.f, v1.z * invLen, -v1.y * invLen );
01193 }
01194
01195 *v3 = Cross( v1, *v2 );
01196 }
01197
01198
01199 inline
01200 float Distance( const Point &p1, const Point &p2 )
01201 {
01202 return ( p1 - p2 ).Length();
01203 }
01204
01205
01206 inline
01207 float DistanceSquared( const Point &p1, const Point &p2 )
01208 {
01209 return ( p1 - p2 ).LengthSquared();
01210 }
01211
01212 inline
01213 std::ostream &operator<<( std::ostream &os, const Point &v )
01214 {
01215 os << v.x << ", " << v.y << ", " << v.z;
01216 return os;
01217 }
01218
01219 inline
01220 Point operator*( float f, const Point &p )
01221 {
01222 return p*f;
01223 }
01224
01225 inline
01226 Normal operator*( float f, const Normal &n )
01227 {
01228 return Normal( f*n.x, f*n.y, f*n.z );
01229 }
01230
01231
01232 inline
01233 Normal Normalize( const Normal &n )
01234 {
01235 return n / n.Length();
01236 }
01237
01238 inline
01239 Vector::Vector( const Normal &n )
01240 :
01241 x( n.x ), y( n.y ), z( n.z )
01242 {}
01243
01244
01245 inline
01246 float Dot( const Normal &n1, const Vector &v2 )
01247 {
01248 return n1.x * v2.x + n1.y * v2.y + n1.z * v2.z;
01249 }
01250
01251
01252 inline
01253 float Dot( const Vector &v1, const Normal &n2 )
01254 {
01255 return v1.x * n2.x + v1.y * n2.y + v1.z * n2.z;
01256 }
01257
01258
01259 inline
01260 float Dot( const Normal &n1, const Normal &n2 )
01261 {
01262 return n1.x * n2.x + n1.y * n2.y + n1.z * n2.z;
01263 }
01264
01265
01266 inline
01267 float AbsDot( const Normal &n1, const Vector &v2 )
01268 {
01269 return fabsf( n1.x * v2.x + n1.y * v2.y + n1.z * v2.z );
01270 }
01271
01272
01273 inline
01274 float AbsDot( const Vector &v1, const Normal &n2 )
01275 {
01276 return fabsf( v1.x * n2.x + v1.y * n2.y + v1.z * n2.z );
01277 }
01278
01279
01280 inline
01281 float AbsDot( const Normal &n1, const Normal &n2 )
01282 {
01283 return fabsf( n1.x * n2.x + n1.y * n2.y + n1.z * n2.z );
01284 }
01285
01286
01287 inline
01288 float ZeroDot( const Normal &v1, const Vector &v2 )
01289 {
01290 return std::max( 0.f, Dot( v1, v2 ) );
01291 }
01292
01293
01294 inline
01295 float ZeroDot( const Vector &v1, const Normal &v2 )
01296 {
01297 return std::max( 0.f, Dot( v1, v2 ) );
01298 }
01299
01300
01301 inline
01302 float ZeroDot( const Normal &v1, const Normal &v2 )
01303 {
01304 return std::max( 0.f, Dot( v1, v2 ) );
01305 }
01306
01307 inline
01308 std::ostream &operator<<( std::ostream &os, const Normal &v )
01309 {
01310 os << v.x << ", " << v.y << ", " << v.z;
01311 return os;
01312 }
01313
01314 inline
01315 std::ostream &operator<<( std::ostream &os, Ray &r )
01316 {
01317 os << "org: " << r.o << "dir: " << r.d << " range [" <<
01318 r.tmin << "," << r.tmax << "] ";
01319 return os;
01320 }
01321
01322 inline
01323 std::ostream &operator<<( std::ostream &os, const BBox &b )
01324 {
01325 os << b.pMin << " -> " << b.pMax;
01326 return os;
01327 }
01328
01329
01330 inline
01331 float Lerp( const float t, const float v1, const float v2 )
01332 {
01333 return (1.f - t) * v1 + t * v2;
01334 }
01335
01336
01337 inline
01338 float Clamp( const float value, const float low, const float high)
01339 {
01340 if(value < low)
01341 return low;
01342 else if (value > high)
01343 return high;
01344 else
01345 return value;
01346 }
01347
01348
01349 inline
01350 int Clamp( const int value , const int low, const int high )
01351 {
01352 if(value < low)
01353 return low;
01354 else if (value > high)
01355 return high;
01356 else
01357 return value;
01358 }
01359
01360
01361 inline
01362 float Radians( const float deg )
01363 {
01364 return ( (float) M_PI / 180.f) * deg;
01365 }
01366
01367
01368 inline
01369 float Degrees( const float rad )
01370 {
01371 return (180.f / (float) M_PI) * rad;
01372 }
01373
01374
01375 inline
01376 Vector SphericalDirection( float sintheta, float costheta, float phi )
01377 {
01378 return Vector(
01379 sintheta * cosf( phi ),
01380 sintheta * sinf( phi ),
01381 costheta );
01382 }
01383
01384
01385 inline
01386 Vector SphericalDirection( float sintheta, float costheta, float phi,
01387 const Vector &x, const Vector &y, const Vector &z )
01388 {
01389 return sintheta * cosf( phi ) * x
01390 + sintheta * sinf( phi ) * y
01391 + costheta * z;
01392 }
01393
01394
01395 inline
01396 float SphericalTheta( const Vector &v )
01397 {
01398 return acosf( Clamp( v.z, -1.f, 1.f ) );
01399 }
01400
01401
01402 inline
01403 float SphericalPhi( const Vector &v )
01404 {
01405 float p = atan2f( v.y, v.x );
01406 return ( p < 0.f ) ? p + 2.f*M_PI : p;
01407 }
01408
01409 }
01410
01411 #endif // PBRT_GEOMETRY_H