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 #include "LampBasic.h"
00026 #include "Geometry/Mesh/DeformedMeshGeometry.h"
00027 #include "Geometry/System/IntersectionResult.h"
00028
00029 namespace Lamp{
00030
00031
00032
00033
00034
00035 DeformedMeshGeometry::DeformedMeshGeometry() : MeshGeometry(),
00036 boundingBox_(AxisAlignedBox::zero), boundingSphere_(Sphere::zero),
00037 triangles_(NULL), triangleCount_(0){
00038 }
00039
00040
00041 DeformedMeshGeometry::DeformedMeshGeometry(const DeformedMeshGeometry& copy) :
00042 MeshGeometry(copy), boundingBox_(AxisAlignedBox::zero),
00043 boundingSphere_(Sphere::zero), triangles_(NULL), triangleCount_(0){
00044 copyDeformedMeshGeometryData(copy);
00045 }
00046
00047
00048 const DeformedMeshGeometry& DeformedMeshGeometry::operator =(
00049 const DeformedMeshGeometry& copy){
00050
00051 if(this == ©){ return *this; }
00052 MeshGeometry::operator =(copy);
00053 copyDeformedMeshGeometryData(copy);
00054 return *this;
00055 }
00056
00057
00058 void DeformedMeshGeometry::copyDeformedMeshGeometryData(
00059 const DeformedMeshGeometry& copy){
00060 int triangleCount = copy.getTriangleCount();
00061 setTriangleCount(triangleCount);
00062 for(int i = 0; i < triangleCount; i++){
00063 setTriangle(i, copy.getTriangle(i));
00064 }
00065 setBoundingBox(copy.getBoundingBox());
00066 setBoundingSphere(copy.getBoundingSphere());
00067 }
00068
00069
00070 DeformedMeshGeometry::~DeformedMeshGeometry(){
00071 SafeArrayDelete(triangles_);
00072 }
00073
00074
00075
00076
00077 bool DeformedMeshGeometry::intersect(const Sphere& sphere) const{
00078
00079 if(!intersectBounding(sphere)){ return false; }
00080
00081 return intersectMesh(sphere);
00082 }
00083
00084
00085 void DeformedMeshGeometry::intersect(
00086 IntersectionResult* result, const Sphere& sphere) const{
00087
00088 if(!intersectBounding(sphere)){ return; }
00089
00090 intersectMesh(result, sphere);
00091 }
00092
00093
00094 bool DeformedMeshGeometry::intersectBounding(const Sphere& sphere) const{
00095
00096 if(!boundingSphere_.intersect(sphere)){ return false; }
00097
00098 if(!boundingBox_.intersect(sphere)){ return false; }
00099 return true;
00100 }
00101
00102
00103 bool DeformedMeshGeometry::intersectMesh(const Sphere& sphere) const{
00104 for(int i = 0; i < triangleCount_; i++){
00105 if(triangles_[i].intersect(sphere)){ return true; }
00106 }
00107 return false;
00108 }
00109
00110
00111 void DeformedMeshGeometry::intersectMesh(
00112 IntersectionResult* result, const Sphere& sphere) const{
00113 Intersection intersection;
00114 for(int i = 0; i < triangleCount_; i++){
00115 if(triangles_[i].intersect(&intersection, sphere)){
00116 result->add(intersection);
00117 }
00118 }
00119 }
00120
00121
00122
00123
00124 void DeformedMeshGeometry::setTriangleCount(int triangleCount){
00125 Assert(triangleCount >= 0);
00126 SafeArrayDelete(triangles_);
00127 triangleCount_ = triangleCount;
00128 if(triangleCount_ == 0){ return; }
00129 triangles_ = new Triangle[triangleCount_];
00130 }
00131
00132
00133
00134
00135 void DeformedMeshGeometry::calculateBounding(){
00136 if(triangleCount_ == 0){ return; }
00137
00138
00139 const Vector3& initializePosition = triangles_[0].getVertex(0);
00140 boundingBox_.set(initializePosition, initializePosition);
00141 for(int i = 0; i < triangleCount_; i++){
00142 const Triangle& triangle = triangles_[i];
00143 for(int j = 0; j < 3; j++){
00144 boundingBox_.merge(triangle.getVertex(j));
00145 }
00146 }
00147
00148
00149 boundingSphere_.set(boundingBox_.getCenter(), 0.f);
00150 for(int i = 0; i < triangleCount_; i++){
00151 const Triangle& triangle = triangles_[i];
00152 for(int j = 0; j < 3; j++){
00153 boundingSphere_.append(triangle.getVertex(j));
00154 }
00155 }
00156 }
00157
00158 }
00159