00001
00002 #include <cassert>
00003 #include <climits>
00004 #include <ostream>
00005
00006
00007 #include <dtUtil/log.h>
00008
00009
00010 #include <controllers/innerSpace/Space3DUnit.h>
00011
00012 #ifdef WITH_VISUALISATION
00013 #include <linda/gnuplot_i.h>
00014 #endif
00015
00016 namespace srAlmende {
00017
00023 Space3DUnit::Space3DUnit() {
00024 Init();
00025 }
00026
00031 void Space3DUnit::Init() {
00032 assert (products.size() < 255);
00033 for (unsigned char i = 0; i < products.size(); i++) {
00034 products[i].id = i;
00035 products[i].quantity = 0;
00036 products[i].new_quantity = 0;
00037 }
00038 }
00039
00040 Space3DUnit::~Space3DUnit() {
00041 }
00042
00047 std::list<Space3DUnit*> Space3DUnit::getNeighbours() {
00048 std::list<Space3DUnit*> result;
00049 std::ostringstream msg; msg.clear(); msg.str("");
00050 msg << "Find neighbours for: " << location->toString() << ".";
00051 LOG_DEBUG(msg.str());
00052 for (unsigned char cnt = 0; cnt < 6; cnt++) {
00053 int ltestX = location->getX();
00054 int ltestY = location->getY();
00055 int ltestZ = location->getZ();
00056 switch (cnt) {
00057 case 0: case 1: {
00058 ltestX += (cnt << 1) - 1;
00059 if (ltestX > space->radius) continue;
00060 if (ltestX < -space->radius) continue;
00061 break;
00062 }
00063 case 2: case 3: {
00064 ltestY += (cnt << 1) - 5;
00065 if (ltestY > space->radius) continue;
00066 if (ltestY < -space->radius) continue;
00067 break;
00068 }
00069 case 4: case 5: {
00070 ltestZ += (cnt << 1) - 9;
00071 if (ltestZ > space->radius) continue;
00072 if (ltestZ < -space->radius) continue;
00073 break;
00074 }
00075 }
00076 msg.clear(); msg.str("");
00077 msg << "Add neighbour: [" << ltestX << "," << ltestY << "," << ltestZ << "].";
00078 LOG_DEBUG(msg.str());
00079 result.push_back(space->getSpace3DUnit(ltestX, ltestY, ltestZ));
00080 }
00081 return result;
00082 }
00083
00089 int Space3DUnit::sumProductQuantities() {
00090 int result = 0;
00091 for (unsigned int i = 0; i < products.size(); i++) {
00092 result += products[i].quantity;
00093
00094
00095
00096 #ifdef CHECK_PRODUCT_QUANTITY_LIMIT
00097 assert (result < (INT_MAX - CHAR_MAX));
00098 #endif
00099 }
00100 return result;
00101 }
00102
00103 #ifdef WITH_VISUALISATION
00104
00108 void Space3DUnit::DrawCell(gnuplot_ctrl *handle) {
00109
00110 int n = products[0].quantity;
00111 std::ostringstream msg; msg.clear(); msg.str("");
00112 msg << "Product quantity in " << location->toString() << " is " << n << ".";
00113 LOG_DEBUG(msg.str());
00114 if (!n) return;
00115
00116 gnuplot_cmd(handle, (char*)"set xrange [0:3]");
00117 gnuplot_cmd(handle, (char*)"set yrange [0:3]");
00118 gnuplot_cmd(handle, (char*)"set zrange [0:3]");
00119
00120 gnuplot_cmd(handle, (char*)"set xtic 0.5");
00121 gnuplot_cmd(handle, (char*)"set ytic 0.5");
00122 gnuplot_cmd(handle, (char*)"set ztic 0.5");
00123
00124 double *x_axis = (double*) calloc(n, sizeof(double));
00125 double *y_axis = (double*) calloc(n, sizeof(double));
00126 double *z_axis = (double*) calloc(n, sizeof(double));
00127
00128 int cube_resolution = 10;
00129 int ii = 0;
00130 for (ii = 0; ii < n; ii++) {
00131 x_axis[ii] = (rand() % cube_resolution) / 10.0 + (location->getX() + space->radius);
00132 y_axis[ii] = (rand() % cube_resolution) / 10.0 + (location->getY() + space->radius);
00133 z_axis[ii] = (rand() % cube_resolution) / 10.0 + (location->getZ() + space->radius);
00134 }
00135 gnuplot_splot(handle, x_axis, y_axis, z_axis, n, (char*)"");
00136 LOG_DEBUG("Plotted!");
00137 free(x_axis);
00138 free(y_axis);
00139 free(z_axis);
00140 }
00141 #endif
00142
00143 }