MeshKit
1.0
|
00001 #include "std.h" 00002 #include <stdlib.h> 00003 #include <limits> 00004 00005 #include "3D.h" 00007 // 00008 // class: Bounds 00009 // 00010 00011 void Bounds::reset() 00012 { 00013 min[X] = min[Y] = min[Z] = std::numeric_limits<double>::max(); 00014 max[X] = max[Y] = max[Z] = std::numeric_limits<double>::min(); 00015 00016 center[X] = center[Y] = center[Z] = 0.0; 00017 radius = 0.0; 00018 00019 points = 0; 00020 } 00021 00022 void Bounds::addPoint(const Vec3& v) 00023 { 00024 if( v[X] < min[X] ) min[X] = v[X]; 00025 if( v[Y] < min[Y] ) min[Y] = v[Y]; 00026 if( v[Z] < min[Z] ) min[Z] = v[Z]; 00027 00028 if( v[X] > max[X] ) max[X] = v[X]; 00029 if( v[Y] > max[Y] ) max[Y] = v[Y]; 00030 if( v[Z] > max[Z] ) max[Z] = v[Z]; 00031 00032 00033 center += v; 00034 00035 points++; 00036 } 00037 00038 void Bounds::complete() 00039 { 00040 center /= (double)points; 00041 00042 Vec3 R1 = max-center; 00043 Vec3 R2 = min-center; 00044 double r1=length(R1); 00045 double r2=length(R2); 00046 radius = (r1>r2)?r1:r2; // max (r1, r2) 00047 } 00048 00049 00050 00052 // 00053 // class: Plane 00054 // 00055 00056 void Plane::calcFrom(const Vec3& p1, const Vec3& p2, const Vec3& p3) 00057 { 00058 Vec3 v1 = p2-p1; 00059 Vec3 v2 = p3-p1; 00060 00061 n = v1 ^ v2; 00062 unitize(n); 00063 00064 d = -n*p1; 00065 } 00066 00067 void Plane::calcFrom(const array<Vec3>& verts) 00068 { 00069 n[X] = n[Y] = n[Z] = 0.0; 00070 00071 int i; 00072 for(i=0; i<verts.length()-1; i++) 00073 { 00074 const Vec3& cur = verts[i]; 00075 const Vec3& next = verts[i+1]; 00076 00077 n[X] += (cur[Y] - next[Y]) * (cur[Z] + next[Z]); 00078 n[Y] += (cur[Z] - next[Z]) * (cur[X] + next[X]); 00079 n[Z] += (cur[X] - next[X]) * (cur[Y] + next[Y]); 00080 } 00081 00082 const Vec3& cur = verts[verts.length()-1]; 00083 const Vec3& next = verts[0]; 00084 n[X] += (cur[Y] - next[Y]) * (cur[Z] + next[Z]); 00085 n[Y] += (cur[Z] - next[Z]) * (cur[X] + next[X]); 00086 n[Z] += (cur[X] - next[X]) * (cur[Y] + next[Y]); 00087 00088 unitize(n); 00089 00090 d = -n*verts[0]; 00091 } 00092 00093