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 SPHERE_H_
00026 #define SPHERE_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 Intersection;
00036 class AxisAlignedBox;
00037 class Capsule;
00038 class Cone;
00039 class Line;
00040 class OrientedBox;
00041 class Plane;
00042 class Ray;
00043 class Segment;
00044 class Triangle;
00045
00046
00047
00048
00049
00050
00051
00052 class Sphere{
00053 public:
00054
00055
00056
00057
00058 static const Sphere zero;
00059
00060
00061 static const Sphere unit;
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 inline Sphere(){}
00072
00073
00074
00075
00076
00077
00078
00079 inline Sphere(const Vector3& center, float radius) :
00080 center_(center), radius_(radius){
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 inline Sphere(float centerX, float centerY, float centerZ, float radius) :
00092 center_(centerX, centerY, centerZ), radius_(radius){
00093 }
00094
00095
00096
00097
00098
00099 inline explicit Sphere(const float* const source) :
00100 center_(source[0], source[1], source[2]), radius_(source[3]){
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 inline void set(const Vector3& center, float radius){
00112 center_ = center;
00113 radius_ = radius;
00114 }
00115
00116
00117
00118
00119
00120
00121
00122
00123 inline void set(float centerX, float centerY, float centerZ, float radius){
00124 center_.set(centerX, centerY, centerZ);
00125 radius_ = radius;
00126 }
00127
00128
00129
00130
00131
00132 inline void set(const float* const source){
00133 center_.set(source[0], source[1], source[2]);
00134 radius_ = source[3];
00135 }
00136
00137
00138
00139
00140
00141
00142
00143 inline void setCenter(float centerX, float centerY, float centerZ){
00144 center_.set(centerX, centerY, centerZ);
00145 }
00146
00147
00148
00149
00150
00151 inline void setCenter(const Vector3& center){ center_ = center; }
00152
00153
00154
00155
00156
00157 inline void setRadius(float radius){ radius_ = radius; }
00158
00159
00160
00161
00162
00163
00164
00165
00166 inline const Vector3& getCenter() const{ return center_; }
00167
00168
00169
00170
00171
00172 inline float getRadius() const{ return radius_; }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 inline const Sphere& merge(const Sphere& target){
00183 Vector3 direction = target.center_ - center_;
00184
00185 if(direction.isZero()){
00186 if(target.radius_ > radius_){ radius_ = target.radius_; }
00187 return *this;
00188 }
00189 float distance = direction.getLength();
00190
00191 if(radius_ >= target.radius_ + distance){ return *this; }
00192
00193 if(target.radius_ >= radius_ + distance){
00194 *this = target;
00195 return *this;
00196 }
00197
00198 direction *= (1.f / distance);
00199 center_ = ((center_ - direction * radius_) +
00200 (target.center_ + direction * target.radius_)) * 0.5f;
00201 radius_ = (radius_ + target.radius_ + distance) * 0.5f;
00202 return *this;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 inline const Sphere& merge(const Vector3& target){
00214 Vector3 direction = target - center_;
00215
00216 if(direction.isZero()){ return *this; }
00217 float distance = direction.getLength();
00218
00219 if(distance <= radius_){ return *this; }
00220
00221 direction *= (1.f / distance);
00222 center_ = ((center_ - direction * radius_) + target) * 0.5f;
00223 radius_ = (radius_ + distance) * 0.5f;
00224 return *this;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234 inline const Sphere& append(const Vector3& target){
00235 Vector3 direction = target - center_;
00236
00237 if(direction.isZero()){ return *this; }
00238 float distance = direction.getLength();
00239
00240 if(distance > radius_){ radius_ = distance; }
00241 return *this;
00242 }
00243
00244
00245
00246
00247
00248
00249 inline bool isZero() const{
00250 return (Math::abs(radius_) <= Math::epsilon);
00251 }
00252
00253
00254
00255
00256
00257 inline bool isUnit() const{
00258 return (Math::abs(radius_ - 1.f) <= Math::epsilon);
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269 inline Sphere transform(const Matrix33& matrix) const{
00270 return Sphere(matrix * center_, radius_);
00271 }
00272
00273
00274
00275
00276
00277
00278 inline Sphere transform(const Matrix34& matrix) const{
00279 return Sphere(matrix * center_, radius_);
00280 }
00281
00282
00283
00284
00285
00286
00287 inline Sphere transform(const Matrix44& matrix) const{
00288 return Sphere(matrix * center_, radius_);
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 inline Sphere scaledTransform(const Matrix33& matrix) const{
00300
00301 float maxSquardLength = 0.f;
00302 for(int i = 0; i < 3; i++){
00303 float squaredLength =
00304 matrix.m[0][i] * matrix.m[0][i] +
00305 matrix.m[1][i] * matrix.m[1][i] +
00306 matrix.m[2][i] * matrix.m[2][i];
00307 if(squaredLength > maxSquardLength){
00308 maxSquardLength = squaredLength;
00309 }
00310 }
00311 return Sphere(matrix * center_, radius_ * Math::sqrt(maxSquardLength));
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321 inline Sphere scaledTransform(const Matrix34& matrix) const{
00322
00323 float maxSquardLength = 0.f;
00324 for(int i = 0; i < 3; i++){
00325 float squaredLength =
00326 matrix.m[0][i] * matrix.m[0][i] +
00327 matrix.m[1][i] * matrix.m[1][i] +
00328 matrix.m[2][i] * matrix.m[2][i];
00329 if(squaredLength > maxSquardLength){
00330 maxSquardLength = squaredLength;
00331 }
00332 }
00333 return Sphere(matrix * center_, radius_ * Math::sqrt(maxSquardLength));
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343 inline Sphere scaledTransform(const Matrix44& matrix) const{
00344
00345 float maxSquardLength = 0.f;
00346 for(int i = 0; i < 3; i++){
00347 float squaredLength =
00348 matrix.m[0][i] * matrix.m[0][i] +
00349 matrix.m[1][i] * matrix.m[1][i] +
00350 matrix.m[2][i] * matrix.m[2][i];
00351 if(squaredLength > maxSquardLength){
00352 maxSquardLength = squaredLength;
00353 }
00354 }
00355 return Sphere(matrix * center_, radius_ * Math::sqrt(maxSquardLength));
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 float getDistance(const Vector3& point) const{
00367 return Math::sqrt(getSquaredDistance(point));
00368 }
00369
00370
00371
00372
00373
00374
00375 float getSquaredDistance(const Vector3& point) const;
00376
00377
00378
00379
00380
00381
00382
00383 float getDistance(const AxisAlignedBox& axisAlignedBox) const{
00384 return Math::sqrt(getSquaredDistance(axisAlignedBox));
00385 }
00386
00387
00388
00389
00390
00391
00392 float getSquaredDistance(const AxisAlignedBox& axisAlignedBox) const;
00393
00394
00395
00396
00397
00398
00399
00400 float getDistance(const Capsule& capsule) const{
00401 return Math::sqrt(getSquaredDistance(capsule));
00402 }
00403
00404
00405
00406
00407
00408
00409 float getSquaredDistance(const Capsule& capsule) const;
00410
00411
00412
00413
00414
00415
00416
00417 float getDistance(const Cone& cone) const{
00418 return Math::sqrt(getSquaredDistance(cone));
00419 }
00420
00421
00422
00423
00424
00425
00426 float getSquaredDistance(const Cone& cone) const;
00427
00428
00429
00430
00431
00432
00433
00434 float getDistance(const Line& line) const{
00435 return Math::sqrt(getSquaredDistance(line));
00436 }
00437
00438
00439
00440
00441
00442
00443 float getSquaredDistance(const Line& line) const;
00444
00445
00446
00447
00448
00449
00450
00451 float getDistance(const OrientedBox& orientedBox) const{
00452 return Math::sqrt(getSquaredDistance(orientedBox));
00453 }
00454
00455
00456
00457
00458
00459
00460 float getSquaredDistance(const OrientedBox& orientedBox) const;
00461
00462
00463
00464
00465
00466
00467
00468 float getDistance(const Plane& plane) const;
00469
00470
00471
00472
00473
00474
00475 float getSquaredDistance(const Plane& plane) const{
00476 float distance = getDistance(plane);
00477 return (distance * distance);
00478 }
00479
00480
00481
00482
00483
00484
00485
00486 float getDistance(const Ray& ray) const{
00487 return Math::sqrt(getSquaredDistance(ray));
00488 }
00489
00490
00491
00492
00493
00494
00495 float getSquaredDistance(const Ray& ray) const;
00496
00497
00498
00499
00500
00501
00502
00503 float getDistance(const Segment& segment) const{
00504 return Math::sqrt(getSquaredDistance(segment));
00505 }
00506
00507
00508
00509
00510
00511
00512 float getSquaredDistance(const Segment& segment) const;
00513
00514
00515
00516
00517
00518
00519
00520 float getDistance(const Sphere& sphere) const{
00521 return Math::sqrt(getSquaredDistance(sphere));
00522 }
00523
00524
00525
00526
00527
00528
00529 float getSquaredDistance(const Sphere& sphere) const;
00530
00531
00532
00533
00534
00535
00536
00537 float getDistance(const Triangle& triangle) const{
00538 return Math::sqrt(getSquaredDistance(triangle));
00539 }
00540
00541
00542
00543
00544
00545
00546 float getSquaredDistance(const Triangle& triangle) const;
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 bool intersect(const Vector3& point) const;
00557
00558
00559
00560
00561
00562
00563
00564 bool intersect(const AxisAlignedBox& axisAlignedBox) const;
00565
00566
00567
00568
00569
00570
00571
00572 bool intersect(const Capsule& capsule) const;
00573
00574
00575
00576
00577
00578
00579
00580 bool intersect(const Cone& cone) const;
00581
00582
00583
00584
00585
00586
00587
00588 bool intersect(const Line& line) const;
00589
00590
00591
00592
00593
00594
00595
00596 bool intersect(const OrientedBox& orientedBox) const;
00597
00598
00599
00600
00601
00602
00603
00604 bool intersect(const Plane& plane) const;
00605
00606
00607
00608
00609
00610
00611
00612 bool intersect(const Ray& ray) const;
00613
00614
00615
00616
00617
00618
00619
00620 bool intersect(const Segment& segment) const;
00621
00622
00623
00624
00625
00626
00627
00628 bool intersect(const Sphere& sphere) const;
00629
00630
00631
00632
00633
00634
00635
00636 bool intersect(Intersection* intersection, const Sphere& sphere) const;
00637
00638
00639
00640
00641
00642
00643
00644 bool intersect(const Triangle& triangle) const;
00645
00646
00647
00648
00649
00650
00651
00652 bool intersect(Intersection* intersection, const Triangle& triangle) const;
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662 inline bool operator ==(const Sphere& target) const{
00663 return ((center_ == target.center_) && (radius_ == target.radius_));
00664 }
00665
00666
00667
00668
00669
00670
00671
00672 inline bool epsilonEquals(
00673 const Sphere& target, float epsilon) const{
00674 Assert(epsilon >= 0.f);
00675 return (center_.epsilonEquals(target.center_, epsilon) &&
00676 (Math::abs(radius_ - target.radius_) <= epsilon));
00677 }
00678
00679
00680
00681
00682
00683
00684 inline bool operator !=(const Sphere& target) const{
00685 return ((center_ != target.center_) || (radius_ != target.radius_));
00686 }
00687
00688
00689
00690
00691
00692
00693
00694 inline bool notEpsilonEquals(
00695 const Sphere& target, float epsilon) const{
00696 Assert(epsilon >= 0.f);
00697 return (center_.notEpsilonEquals(target.center_, epsilon) ||
00698 (Math::abs(radius_ - target.radius_) > epsilon));
00699 }
00700
00701
00702
00703
00704
00705
00706
00707
00708 inline String toString() const{
00709 String returnString;
00710 returnString.format("{ ( %.8f, %.8f, %.8f ) %.8f }",
00711 center_.x, center_.y, center_.z, radius_);
00712 return returnString;
00713 }
00714
00715 private:
00716
00717
00718
00719
00720 Vector3 center_;
00721
00722 float radius_;
00723
00724 };
00725
00726
00727 }
00728 #endif // End of SPHERE_H_
00729