00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef ORIENTED_BOX_H_
00026 #define ORIENTED_BOX_H_
00027
00028 #include <Core/Primitive/Vector3.h>
00029 #include <Core/Primitive/Matrix33.h>
00030 #include <Core/Primitive/Matrix34.h>
00031 #include <Core/Primitive/Matrix44.h>
00032
00033 namespace Lamp{
00034
00035 class AxisAlignedBox;
00036 class Capsule;
00037 class Cone;
00038 class Line;
00039 class Plane;
00040 class Ray;
00041 class Segment;
00042 class Sphere;
00043 class Triangle;
00044
00045
00046
00047
00048
00049
00050
00051 class OrientedBox{
00052 public:
00053
00054
00055
00056
00057 static const OrientedBox zero;
00058
00059
00060 static const OrientedBox unit;
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 inline OrientedBox(){}
00071
00072
00073
00074
00075
00076
00077
00078 inline OrientedBox(const Matrix33& rotationMatrix,
00079 const Vector3& center, const Vector3& extent) :
00080 rotationMatrix_(rotationMatrix), center_(center), extent_(extent){
00081 Assert((extent_.x >= 0.f) && (extent_.y >= 0.f) && (extent_.z >= 0.f));
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 inline OrientedBox(
00103 float rotation00, float rotation01, float rotation02,
00104 float rotation10, float rotation11, float rotation12,
00105 float rotation20, float rotation21, float rotation22,
00106 float centerX, float centerY, float centerZ,
00107 float extentX, float extentY, float extentZ) :
00108 rotationMatrix_(rotation00, rotation01, rotation02,
00109 rotation10, rotation11, rotation12,
00110 rotation20, rotation21, rotation22),
00111 center_(centerX, centerY, centerZ),
00112 extent_(extentX, extentY, extentZ){
00113 Assert((extent_.x >= 0.f) && (extent_.y >= 0.f) && (extent_.z >= 0.f));
00114 }
00115
00116
00117
00118
00119
00120 inline explicit OrientedBox(const float* const source) :
00121 rotationMatrix_(source[0], source[1], source[2],
00122 source[3], source[4], source[5],
00123 source[6], source[7], source[8]),
00124 center_(source[9], source[10], source[11]),
00125 extent_(source[12], source[13], source[14]){
00126 Assert((extent_.x >= 0.f) && (extent_.y >= 0.f) && (extent_.z >= 0.f));
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 inline void set(const Matrix33& rotationMatrix,
00139 const Vector3& center, const Vector3& extent){
00140 rotationMatrix_ = rotationMatrix;
00141 center_ = center;
00142 extent_ = extent;
00143 Assert((extent_.x >= 0.f) && (extent_.y >= 0.f) && (extent_.z >= 0.f));
00144 }
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 inline void set(
00165 float rotation00, float rotation01, float rotation02,
00166 float rotation10, float rotation11, float rotation12,
00167 float rotation20, float rotation21, float rotation22,
00168 float centerX, float centerY, float centerZ,
00169 float extentX, float extentY, float extentZ){
00170 rotationMatrix_.set(rotation00, rotation01, rotation02,
00171 rotation10, rotation11, rotation12,
00172 rotation20, rotation21, rotation22);
00173 center_.set(centerX, centerY, centerZ);
00174 extent_.set(extentX, extentY, extentZ);
00175 Assert((extent_.x >= 0.f) && (extent_.y >= 0.f) && (extent_.z >= 0.f));
00176 }
00177
00178
00179
00180
00181
00182 inline void set(const float* const source){
00183 rotationMatrix_.set(source[0], source[1], source[2],
00184 source[3], source[4], source[5],
00185 source[6], source[7], source[8]);
00186 center_.set(source[9], source[10], source[11]);
00187 extent_.set(source[12], source[13], source[14]);
00188 Assert((extent_.x >= 0.f) && (extent_.y >= 0.f) && (extent_.z >= 0.f));
00189 }
00190
00191
00192
00193
00194
00195
00196 inline void setRotationMatrix(const Matrix33& rotationMatrix){
00197 rotationMatrix_ = rotationMatrix;
00198 }
00199
00200
00201
00202
00203
00204 inline void setRotationXYZ(const Vector3 rotationXYZ){
00205 rotationMatrix_.setRotationXYZ(rotationXYZ);
00206 }
00207
00208
00209
00210
00211
00212 inline void setRotationQuaternion(const Quaternion& rotationQuaternion){
00213 rotationMatrix_.setRotationQuaternion(rotationQuaternion);
00214 }
00215
00216
00217
00218
00219
00220
00221 inline void setCenter(const Vector3& center){ center_ = center; }
00222
00223
00224
00225
00226
00227
00228 inline void setExtent(const Vector3& extent){
00229 extent_ = extent;
00230 Assert((extent_.x >= 0.f) && (extent_.y >= 0.f) && (extent_.z >= 0.f));
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240 inline const Matrix33& getRotationMatrix() const{ return rotationMatrix_; }
00241
00242
00243
00244
00245
00246 inline const Vector3& getCenter() const{ return center_; }
00247
00248
00249
00250
00251
00252 inline const Vector3& getExtent() const{ return extent_; }
00253
00254
00255
00256
00257
00258 inline Vector3 getSize() const{ return (extent_ * 2.f); }
00259
00260
00261
00262
00263
00264
00265
00266 inline Vector3 getAxis(int index) const{
00267 Assert((index >= 0) && (index < 3));
00268 return Vector3(rotationMatrix_.m[index][0],
00269 rotationMatrix_.m[index][1], rotationMatrix_.m[index][2]);
00270 }
00271
00272
00273
00274
00275
00276 inline Vector3 getAxisX() const{
00277 return Vector3(
00278 rotationMatrix_.m00, rotationMatrix_.m01, rotationMatrix_.m02);
00279 }
00280
00281
00282
00283
00284
00285 inline Vector3 getAxisY() const{
00286 return Vector3(
00287 rotationMatrix_.m10, rotationMatrix_.m11, rotationMatrix_.m12);
00288 }
00289
00290
00291
00292
00293
00294 inline Vector3 getAxisZ() const{
00295 return Vector3(
00296 rotationMatrix_.m20, rotationMatrix_.m21, rotationMatrix_.m22);
00297 }
00298
00299
00300
00301
00302
00303
00304
00305 inline Vector3 getExtendedAxis(int index) const{
00306 Assert((index >= 0) && (index < 3));
00307 float extent = extent_.array[index];
00308 return Vector3(rotationMatrix_.m[index][0] * extent,
00309 rotationMatrix_.m[index][1] * extent,
00310 rotationMatrix_.m[index][2] * extent);
00311 }
00312
00313
00314
00315
00316
00317 inline Vector3 getExtendedAxisX() const{
00318 return Vector3(rotationMatrix_.m00 * extent_.x,
00319 rotationMatrix_.m01 * extent_.x, rotationMatrix_.m02 * extent_.x);
00320 }
00321
00322
00323
00324
00325
00326 inline Vector3 getExtendedAxisY() const{
00327 return Vector3(rotationMatrix_.m10 * extent_.y,
00328 rotationMatrix_.m11 * extent_.y, rotationMatrix_.m12 * extent_.y);
00329 }
00330
00331
00332
00333
00334
00335 inline Vector3 getExtendedAxisZ() const{
00336 return Vector3(rotationMatrix_.m20 * extent_.z,
00337 rotationMatrix_.m21 * extent_.z, rotationMatrix_.m22 * extent_.z);
00338 }
00339
00340
00341
00342
00343
00344
00345
00346 inline float getEffectiveDiameter(const Vector3& direction) const{
00347 return (Math::abs(direction.dotProduct(getExtendedAxisX())) +
00348 Math::abs(direction.dotProduct(getExtendedAxisY())) +
00349 Math::abs(direction.dotProduct(getExtendedAxisZ())));
00350 }
00351
00352
00353
00354
00355
00356
00357 inline float getEffectiveRadius(const Vector3& direction) const{
00358 return getEffectiveDiameter(direction) * 0.5f;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 inline Vector3 getCorner(int index) const{
00383 Assert((index >= 0) && (index < 8));
00384 Vector3 axisX = getExtendedAxisX();
00385 Vector3 axisY = getExtendedAxisY();
00386 Vector3 axisZ = getExtendedAxisZ();
00387 if(index == 0){
00388 return center_ - axisX - axisY - axisZ;
00389 }else if(index == 1){
00390 return center_ - axisX + axisY - axisZ;
00391 }else if(index == 2){
00392 return center_ + axisX + axisY - axisZ;
00393 }else if(index == 3){
00394 return center_ + axisX - axisY - axisZ;
00395 }else if(index == 4){
00396 return center_ + axisX + axisY + axisZ;
00397 }else if(index == 5){
00398 return center_ - axisX + axisY + axisZ;
00399 }else if(index == 6){
00400 return center_ - axisX - axisY + axisZ;
00401 }else if(index == 7){
00402 return center_ + axisX - axisY + axisZ;
00403 }
00404 ErrorOut("OrientedBox::getCorner() Out of index");
00405 return center_;
00406 }
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 inline void getCornerArray(Vector3 corner[8]) const{
00428 const Matrix33& matrix = rotationMatrix_;
00429 Vector3 axisX = getExtendedAxisX();
00430 Vector3 axisY = getExtendedAxisY();
00431 Vector3 axisZ = getExtendedAxisZ();
00432 corner[0] = center_ - axisX - axisY - axisZ;
00433 corner[1] = center_ - axisX + axisY - axisZ;
00434 corner[2] = center_ + axisX + axisY - axisZ;
00435 corner[3] = center_ + axisX - axisY - axisZ;
00436 corner[4] = center_ + axisX + axisY + axisZ;
00437 corner[5] = center_ - axisX + axisY + axisZ;
00438 corner[6] = center_ - axisX - axisY + axisZ;
00439 corner[7] = center_ + axisX - axisY + axisZ;
00440 }
00441
00442
00443
00444
00445
00446
00447
00448
00449 inline bool isZero() const{
00450 return extent_.epsilonEquals(Vector3::zero, Math::epsilon);
00451 }
00452
00453
00454
00455
00456
00457 inline bool isUnit() const{
00458 return extent_.epsilonEquals(Vector3(0.5f, 0.5f, 0.5f), Math::epsilon);
00459 }
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469 inline OrientedBox transform(const Matrix33& matrix) const{
00470 OrientedBox result;
00471 result.rotationMatrix_ = matrix * rotationMatrix_;
00472 result.center_ = matrix * center_;
00473 result.extent_ = extent_;
00474 return result;
00475 }
00476
00477
00478
00479
00480
00481
00482 inline OrientedBox transform(const Matrix34& matrix) const{
00483 OrientedBox result;
00484 Matrix33 matrix33;
00485 matrix33.set(matrix);
00486 result.rotationMatrix_ = matrix33 * rotationMatrix_;
00487 result.center_ = matrix * center_;
00488 result.extent_ = extent_;
00489 return result;
00490 }
00491
00492
00493
00494
00495
00496
00497 inline OrientedBox transform(const Matrix44& matrix) const{
00498 OrientedBox result;
00499 Matrix33 matrix33;
00500 matrix33.set(matrix);
00501 result.rotationMatrix_ = matrix33 * rotationMatrix_;
00502 result.center_ = matrix * center_;
00503 result.extent_ = extent_;
00504 return result;
00505 }
00506
00507
00508
00509
00510
00511
00512
00513 AxisAlignedBox scaledTransform(const Matrix33& matrix) const;
00514
00515
00516
00517
00518
00519
00520 AxisAlignedBox scaledTransform(const Matrix34& matrix) const;
00521
00522
00523
00524
00525
00526
00527 AxisAlignedBox scaledTransform(const Matrix44& matrix) const;
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 float getDistance(const Vector3& point) const{
00538 return Math::sqrt(getSquaredDistance(point));
00539 }
00540
00541
00542
00543
00544
00545
00546 float getSquaredDistance(const Vector3& point) const;
00547
00548
00549
00550
00551
00552
00553
00554 float getDistance(const AxisAlignedBox& axisAlignedBox) const{
00555 return Math::sqrt(getSquaredDistance(axisAlignedBox));
00556 }
00557
00558
00559
00560
00561
00562
00563 float getSquaredDistance(const AxisAlignedBox& axisAlignedBox) const;
00564
00565
00566
00567
00568
00569
00570
00571 float getDistance(const Capsule& capsule) const{
00572 return Math::sqrt(getSquaredDistance(capsule));
00573 }
00574
00575
00576
00577
00578
00579
00580 float getSquaredDistance(const Capsule& capsule) const;
00581
00582
00583
00584
00585
00586
00587
00588 float getDistance(const Cone& cone) const{
00589 return Math::sqrt(getSquaredDistance(cone));
00590 }
00591
00592
00593
00594
00595
00596
00597 float getSquaredDistance(const Cone& cone) const;
00598
00599
00600
00601
00602
00603
00604
00605 float getDistance(const Line& line) const{
00606 return Math::sqrt(getSquaredDistance(line));
00607 }
00608
00609
00610
00611
00612
00613
00614 float getSquaredDistance(const Line& line) const;
00615
00616
00617
00618
00619
00620
00621
00622 float getDistance(const OrientedBox& orientedBox) const{
00623 return Math::sqrt(getSquaredDistance(orientedBox));
00624 }
00625
00626
00627
00628
00629
00630
00631 float getSquaredDistance(const OrientedBox& orientedBox) const;
00632
00633
00634
00635
00636
00637
00638
00639 float getDistance(const Plane& plane) const;
00640
00641
00642
00643
00644
00645
00646 float getSquaredDistance(const Plane& plane) const{
00647 float distance = getDistance(plane);
00648 return (distance * distance);
00649 }
00650
00651
00652
00653
00654
00655
00656
00657 float getDistance(const Ray& ray) const{
00658 return Math::sqrt(getSquaredDistance(ray));
00659 }
00660
00661
00662
00663
00664
00665
00666 float getSquaredDistance(const Ray& ray) const;
00667
00668
00669
00670
00671
00672
00673
00674 float getDistance(const Segment& segment) const{
00675 return Math::sqrt(getSquaredDistance(segment));
00676 }
00677
00678
00679
00680
00681
00682
00683 float getSquaredDistance(const Segment& segment) const;
00684
00685
00686
00687
00688
00689
00690
00691 float getDistance(const Sphere& sphere) const{
00692 return Math::sqrt(getSquaredDistance(sphere));
00693 }
00694
00695
00696
00697
00698
00699
00700 float getSquaredDistance(const Sphere& sphere) const;
00701
00702
00703
00704
00705
00706
00707
00708 float getDistance(const Triangle& triangle) const{
00709 return Math::sqrt(getSquaredDistance(triangle));
00710 }
00711
00712
00713
00714
00715
00716
00717 float getSquaredDistance(const Triangle& triangle) const;
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727 bool intersect(const Vector3& point) const;
00728
00729
00730
00731
00732
00733
00734
00735 bool intersect(const AxisAlignedBox& axisAlignedBox) const;
00736
00737
00738
00739
00740
00741
00742
00743 bool intersect(const Capsule& capsule) const;
00744
00745
00746
00747
00748
00749
00750
00751 bool intersect(const Cone& cone) const;
00752
00753
00754
00755
00756
00757
00758
00759 bool intersect(const Line& line) const;
00760
00761
00762
00763
00764
00765
00766
00767 bool intersect(const OrientedBox& orientedBox) const;
00768
00769
00770
00771
00772
00773
00774
00775 bool intersect(const Plane& plane) const;
00776
00777
00778
00779
00780
00781
00782
00783 bool intersect(const Ray& ray) const;
00784
00785
00786
00787
00788
00789
00790
00791 bool intersect(const Segment& segment) const;
00792
00793
00794
00795
00796
00797
00798
00799 bool intersect(const Sphere& sphere) const;
00800
00801
00802
00803
00804
00805
00806
00807 bool intersect(const Triangle& triangle) const;
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817 inline bool operator ==(const OrientedBox& target) const{
00818 return ((rotationMatrix_ == target.rotationMatrix_) &&
00819 (center_ == target.center_) && (extent_ == target.extent_));
00820 }
00821
00822
00823
00824
00825
00826
00827
00828 inline bool epsilonEquals(
00829 const OrientedBox& target, float epsilon) const{
00830 Assert(epsilon >= 0.f);
00831 return (
00832 rotationMatrix_.epsilonEquals(target.rotationMatrix_, epsilon) &&
00833 center_.epsilonEquals(target.center_, epsilon) &&
00834 extent_.epsilonEquals(target.extent_, epsilon));
00835 }
00836
00837
00838
00839
00840
00841
00842 inline bool operator !=(const OrientedBox& target) const{
00843 return ((rotationMatrix_ != target.rotationMatrix_) ||
00844 (center_ != target.center_) || (extent_ != target.extent_));
00845 }
00846
00847
00848
00849
00850
00851
00852
00853 inline bool notEpsilonEquals(
00854 const OrientedBox& target, float epsilon) const{
00855 Assert(epsilon >= 0.f);
00856 return (
00857 rotationMatrix_.notEpsilonEquals(target.rotationMatrix_, epsilon) ||
00858 center_.notEpsilonEquals(target.center_, epsilon) ||
00859 extent_.notEpsilonEquals(target.extent_, epsilon));
00860 }
00861
00862
00863
00864
00865
00866
00867
00868
00869 inline String toString() const{
00870 String returnString;
00871 returnString.format("{ { ( %.8f, %.8f, %.8f ) "
00872 "( %.8f, %.8f, %.8f ) ( %.8f, %.8f, %.8f ) } "
00873 "( %.8f, %.8f, %.8f ) ( %.8f, %.8f, %.8f ) }",
00874 rotationMatrix_.m00, rotationMatrix_.m01, rotationMatrix_.m02,
00875 rotationMatrix_.m10, rotationMatrix_.m11, rotationMatrix_.m12,
00876 rotationMatrix_.m20, rotationMatrix_.m21, rotationMatrix_.m22,
00877 center_.x, center_.y, center_.z, extent_.x, extent_.y, extent_.z);
00878 return returnString;
00879 }
00880
00881 private:
00882
00883
00884
00885
00886 Matrix33 rotationMatrix_;
00887
00888 Vector3 center_;
00889
00890 Vector3 extent_;
00891
00892 };
00893
00894
00895 }
00896 #endif // End of ORIENTED_BOX_H_
00897