00001
00002
00003
00004 #ifndef _INTERPOL_H_
00005 #define _INTERPOL_H_
00006
00007 #include <stdio.h>
00008
00009 #include "point3d.h"
00010 #include "reallocablearray.h"
00011 #include "space.h"
00012 #include "socket.h"
00013
00014 class point {
00015
00016 public :
00017
00018 Point3D p;
00019 Point3D f;
00020
00021 point(const Point3D np, const Point3D nf) : p(np), f(nf)
00022 {
00023 }
00024 point(const Point3D *np, const Point3D *nf)
00025 {
00026 p = *np;
00027 f = *nf;
00028 }
00029 point() : p(), f()
00030 {
00031 }
00032
00033 point(const point *cp)
00034 {
00035 p = cp->p;
00036 f = cp->f;
00037 }
00038
00039 ~point()
00040 {
00041 }
00042
00043 long gethash()
00044 {
00045 long res;
00046
00047 res = hashdouble(p.x);
00048 res ^= hashdouble(p.y);
00049 res ^= hashdouble(p.z);
00050 res ^= hashdouble(f.x);
00051 res ^= hashdouble(f.y);
00052 res ^= hashdouble(f.z);
00053 return res;
00054 }
00055
00056 void print_datas()
00057 {
00058 fprintf(stderr, "Point class.\np: %f,%f,%f\nf: %f,%f,%f\n",
00059 p.x,p.y,p.z,f.x,f.y,f.z);
00060 }
00061
00062 void receive_data(char *buf)
00063 {
00064 int j=0;
00065
00066 receive_float_from_buf(buf + j, &(p.x));
00067 j+=4;
00068 receive_float_from_buf(buf + j, &(p.y));
00069 j+=4;
00070 receive_float_from_buf(buf + j, &(p.z));
00071 j+=4;
00072 receive_float_from_buf(buf + j, &(f.x));
00073 j+=4;
00074 receive_float_from_buf(buf + j, &(f.y));
00075 j+=4;
00076 receive_float_from_buf(buf + j, &(f.z));
00077 j+=4;
00078 }
00079
00080 void data_to_send(char *buf)
00081 {
00082 int j=0;
00083
00084 copy_float_to_buf(buf + j, p.x);
00085 j+=4;
00086 copy_float_to_buf(buf + j, p.y);
00087 j+=4;
00088 copy_float_to_buf(buf + j, p.z);
00089 j+=4;
00090 copy_float_to_buf(buf + j, f.x);
00091 j+=4;
00092 copy_float_to_buf(buf + j, f.y);
00093 j+=4;
00094 copy_float_to_buf(buf + j, f.z);
00095 j+=4;
00096 }
00097
00098 };
00099
00100
00101 typedef ReallocableArray<point *> point_l;
00102
00103 class space_i : public space {
00104 public:
00105 point_l my_points;
00106
00107 space_i() { }
00108
00109 virtual ~space_i() { erase_all(); }
00110
00111 virtual point *isthere(Point3D *newpoint)
00112 {
00113 for(int i=0; i<my_points.NumElements(); i++) {
00114
00115 point* cur = my_points[i];
00116 if(cur->p == *newpoint)
00117 return (cur);
00118 }
00119 return 0;
00120 }
00121
00122 void add_new_point(point *p)
00123 {
00124 my_points.Append(p);
00125 }
00126
00127 virtual void print_datas()
00128 {
00129 fprintf(stderr, "Space of interpolation.\n");
00130 for(int i=0; i<my_points.NumElements(); i++) {
00131 point* cur = my_points[i];
00132 cur->print_datas();
00133 }
00134 }
00135
00136 virtual void send_datas(Socket *s)
00137 {
00138 unsigned short size;
00139 int j;
00140 char *buf;
00141 us_t l;
00142
00143 size = 24 * my_points.NumElements();
00144 buf = (char *) malloc(sizeof(char) * (3 + size));
00145 if(buf == NULL)
00146 return;
00147 l.s = htons(size);
00148 buf[0] = TYPE_INTERPOL;
00149 for(int k=0; k<2; k++)
00150 buf[k+1] = l.c[k];
00151 j=0;
00152 for(int i=0; i<my_points.NumElements(); i++) {
00153 point* cur = my_points[i];
00154 cur->data_to_send(buf + 3 + (24*j));
00155 j++;
00156 }
00157 s->Send(buf, 3 + size);
00158 free(buf);
00159 }
00160
00161 virtual void use_new_datas(char *buf, int size)
00162 {
00163 int i,nb;
00164 point *p;
00165
00166 nb = size / 24;
00167
00168 erase_all();
00169
00170 for(i=0;i<nb; i++)
00171 {
00172 p = new point();
00173 p->receive_data(buf);
00174 add_new_point(p);
00175 buf+=24;
00176 }
00177 }
00178
00179 virtual void erase_all()
00180 {
00181 #if 0
00182 for(int i=0; i<my_points.NumElements(); i++) {
00183 point* cur = my_points[i];
00184 delete cur;
00185 }
00186 #endif
00187 my_points.DeleteAll();
00188 }
00189
00190 long gethash()
00191 {
00192 long res = 0;
00193
00194 for(int i=0; i<my_points.NumElements(); i++) {
00195 point* cur = my_points[i];
00196 res ^= (cur->gethash());
00197 }
00198
00199 return res;
00200 }
00201
00202 virtual Point3D *get_force(Point3D *pos, Point3D* res)
00203 {
00204 Point3D *tmp1,*tmp2;
00205 double distance, sumdistance = 0;
00206
00207 if(res == NULL)
00208 res = new Point3D();
00209
00210 *res = Point3D(0,0,0);
00211 for(int i=0; i<my_points.NumElements(); i++) {
00212 point* cur = my_points[i];
00213 tmp1 = &(cur)->p;
00214 distance = sqrt((*pos - *tmp1).Squared());
00215 if(distance == 0)
00216 {
00217 *res = *tmp1;
00218 return res;
00219 }
00220 tmp2 = &(cur)->f;
00221 *res += (1.0/distance * (*tmp2));
00222 sumdistance += (1.0/distance);
00223 }
00224
00225 *res /= sumdistance;
00226
00227 return res;
00228 }
00229
00230 virtual int istouching()
00231 {
00232 return 1;
00233 }
00234
00235 };
00236
00237 #endif