00001
00002 #ifndef NODE_H
00003 #define NODE_H
00004
00005 #include <math.h>
00006 #include "ref.h"
00007 #include "charm.h"
00008
00009 class chunk;
00010
00012 class node {
00014 double coord[3];
00016
00018 int fixed;
00020
00022 int surface;
00024
00026 int reports;
00028
00031 double sumReports[3];
00033 nodeRef myRef;
00035 chunk *C;
00036 public:
00038
00039 node() {
00040 reports = 0;
00041 fixed = surface = -1;
00042 for (int i=0; i<3; i++) sumReports[i] = 0.0;
00043 myRef.reset();
00044 C = NULL;
00045 }
00047
00049 node(double x, double y, double z) {
00050 coord[0] = x; coord[1] = y; coord[2] = z;
00051 reports = 0;
00052 fixed = surface = -1;
00053 for (int i=0; i<3; i++) sumReports[i] = 0.0;
00054 myRef.reset();
00055 C = NULL;
00056 }
00058
00060 node(double inNode[3]) {
00061 reports = 0;
00062 fixed = surface = -1;
00063 for (int i=0; i<3; i++) {
00064 coord[i] = inNode[i];
00065 sumReports[i] = 0.0;
00066 }
00067 myRef.reset();
00068 C = NULL;
00069 }
00071
00074 void pup(PUP::er &p) {
00075 p|reports; p|fixed; p|surface;
00076 p(coord,3);
00077 p(sumReports,3);
00078 }
00080
00081 void set(int cid, int idx, chunk *cptr) { myRef.set(cid, idx); C = cptr; }
00083
00084 void set(double x, double y, double z) {
00085 coord[0] = x; coord[1] = y; coord[2] = z;
00086 }
00088
00089 void set(double inNode[3]) {
00090 for (int i=0; i<3; i++) coord[i] = inNode[i];
00091 }
00093
00094 void reset() {
00095 reports = 0;
00096 fixed = surface = -1;
00097 for (int i=0; i<3; i++) sumReports[i] = 0.0;
00098 myRef.reset();
00099 C = NULL;
00100 }
00102
00103 int operator==(const node& n) {
00104 return ((coord[0] == n.coord[0]) && (coord[1] == n.coord[1])
00105 && (coord[2] == n.coord[2])); }
00107
00108 node& operator=(const node& n) {
00109 fixed = n.fixed; surface = n.surface;
00110 for (int i=0; i<3; i++) {
00111 coord[i] = n.coord[i];
00112 }
00113 return *this;
00114 }
00116
00118 double getCoord(int d) {
00119 CmiAssert((d<=2) && (d>=0));
00120 return coord[d];
00121 }
00123 void fix() { fixed = 1; }
00125 int isFixed() { return(fixed); }
00127 void notFixed() { fixed = 0; }
00129 void setSurface() { surface = 1; }
00131 int onSurface() { return(surface); }
00133 void notSurface() { surface = 0; }
00135 double distance(const node& n) {
00136 double dx = n.coord[0] - coord[0], dy = n.coord[1] - coord[1],
00137 dz = n.coord[2] - coord[2];
00138 return (sqrt ((dx * dx) + (dy * dy) + (dz * dz)));
00139 }
00141 void midpoint(const node& n, node& result) {
00142 result.coord[0] = (coord[0] + n.coord[0]) / 2.0;
00143 result.coord[1] = (coord[1] + n.coord[1]) / 2.0;
00144 result.coord[2] = (coord[2] + n.coord[2]) / 2.0;
00145 }
00147 node midpoint(const node& n) {
00148 double x = (coord[0] + n.coord[0]) / 2.0;
00149 double y = (coord[1] + n.coord[1]) / 2.0;
00150 double z = (coord[2] + n.coord[2]) / 2.0;
00151 return node(x, y, z);
00152 }
00154
00156
00157
00158 void project(const node& n, node& result) {
00159 result.coord[0] = n.coord[0] + (n.coord[0] - coord[0]);
00160 result.coord[1] = n.coord[1] + (n.coord[1] - coord[1]);
00161 result.coord[2] = n.coord[2] + (n.coord[2] - coord[2]);
00162 }
00164
00166 void shortenLine(const node& n, double l, node& result) {
00167 double m = distance(n);
00168 result.coord[0] = (l/m)*(n.coord[0] - coord[0]) + coord[0];
00169 result.coord[1] = (l/m)*(n.coord[1] - coord[1]) + coord[1];
00170 result.coord[2] = (l/m)*(n.coord[2] - coord[2]) + coord[2];
00171 }
00173
00176 void relocateNode() {
00177
00178 if (fixed || (reports==0)) return;
00179 for (int i=0; i<3; i++) {
00180 coord[i] = sumReports[i]/reports;
00181 sumReports[i] = 0.0;
00182 }
00183 reports = 0;
00184 }
00186
00188 void relocationVote(const node& n) {
00189 for (int i=0; i<3; i++) sumReports[i] += n.coord[i];
00190 reports++;
00191 }
00192 };
00193
00194 #endif