00001
00002
00003
00004 #ifndef __UIUC_CHARM_SURFACEMESH_H
00005 #define __UIUC_CHARM_SURFACEMESH_H
00006
00007 #include <stdio.h>
00008 #include "ckvector3d.h"
00009 #include <vector>
00010
00011
00012 class TriangleSurfaceMesh {
00013 public:
00014 enum {nodePer=3};
00015
00017 class conn_t {
00018 public:
00019 int nodes[TriangleSurfaceMesh::nodePer];
00020 conn_t() {nodes[0]=-1;}
00021 conn_t(int a,int b,int c)
00022 {nodes[0]=a; nodes[1]=b; nodes[2]=c;}
00023 };
00024
00026 TriangleSurfaceMesh() { }
00028 TriangleSurfaceMesh(int nt,int np) { allocate(nt,np); }
00029 virtual ~TriangleSurfaceMesh() { }
00030
00032 virtual void allocate(int nt,int np) {
00033 tris.resize(nt);
00034 pts.resize(np);
00035 }
00036
00038 inline int getTriangles(void) const {return tris.size();}
00040 inline int *getTriangle(int t) {return &(tris[t].nodes[0]);}
00041 inline const int *getTriangle(int t) const {return &(tris[t].nodes[0]);}
00042 inline int *getTriangleConn(void) {return getTriangle(0);}
00043 inline const int *getTriangleConn(void) const {return getTriangle(0);}
00044
00045 inline const double getArea(int t) const {
00046 CkVector3d n1_coord = pts[tris[t].nodes[0]];
00047 CkVector3d n2_coord = pts[tris[t].nodes[1]];
00048 CkVector3d n3_coord = pts[tris[t].nodes[2]];
00049 double area=0.0;
00050 double aLen, bLen, cLen, sLen, d, ds_sum;
00051
00052 ds_sum = 0.0;
00053 for (int i=0; i<3; i++) {
00054 d = n1_coord[i] - n2_coord[i];
00055 ds_sum += d*d;
00056 }
00057 aLen = sqrt(ds_sum);
00058 ds_sum = 0.0;
00059 for (int i=0; i<3; i++) {
00060 d = n2_coord[i] - n3_coord[i];
00061 ds_sum += d*d;
00062 }
00063 bLen = sqrt(ds_sum);
00064 ds_sum = 0.0;
00065 for (int i=0; i<3; i++) {
00066 d = n3_coord[i] - n1_coord[i];
00067 ds_sum += d*d;
00068 }
00069 cLen = sqrt(ds_sum);
00070 sLen = (aLen+bLen+cLen)/2;
00071 return (sqrt(sLen*(sLen-aLen)*(sLen-bLen)*(sLen-cLen)));
00072 }
00073
00075 inline int getPoints(void) const {return pts.size();}
00077 inline CkVector3d &getPoint(int p) {return pts[p];}
00078 inline const CkVector3d &getPoint(int p) const {return pts[p];}
00079 CkVector3d *getPointArray(void);
00080 const CkVector3d *getPointArray(void) const;
00081
00082 void cleanup() {
00083 pts.erase(pts.begin(), pts.end());
00084 std::vector<CkVector3d>(pts).swap(pts);
00085 tris.erase(tris.begin(), tris.end());
00086 std::vector<conn_t>(tris).swap(tris);
00087 }
00088
00090 int addTriangle(const conn_t &c) {tris.push_back(c); nonGhostTri++; return tris.size()-1;}
00091 int addPoint(const CkVector3d &pt) {pts.push_back(pt); nonGhostPt++; return pts.size()-1;}
00092
00093 int nonGhostTri, nonGhostPt;
00094 void writeToTecplot(char *fname) {
00095 FILE *file = fopen(fname, "w");
00096
00097 fprintf (file, "TITLE=\"%s\"\n", fname);
00098 fprintf (file, "ZONE N=%d E=%d ET=TRIANGLE F=FEPOINT\n",
00099 pts.size(), tris.size());
00100
00101 int i,n;
00102 n=pts.size();
00103 for (i=0; i<n; ++i) {
00104 fprintf(file,"%lf %lf %lf\n",pts[i][0],pts[i][1],pts[i][2]);
00105 }
00106
00107 n=tris.size();
00108 for (i=0; i<n; ++i) {
00109 fprintf(file,"%d %d %d\n", tris[i].nodes[0]+1, tris[i].nodes[1]+1,
00110 tris[i].nodes[2]+1);
00111 }
00112 fclose(file);
00113 }
00114
00115
00116 private:
00117 std::vector<conn_t> tris;
00118 std::vector<CkVector3d> pts;
00119 };
00120 #endif