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