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