00001
00002 #ifndef GDHECLIENT_HPP
00003 #define GDHECLIENT_HPP
00004
00005 #include <string>
00006 #include <iostream>
00007 #include <sstream>
00008
00009 namespace gdhe {
00010 #include <gdhe/GDHE.h>
00011 }
00012 #include "ddf/definitions.hpp"
00013
00014 const char OBJ_BASE_NAME[6] = "GDHE_";
00015
00019 class GdheClient
00020 {
00021 static unsigned short m_robot_id;
00022
00023 public:
00024
00025 GdheClient(char *server_name);
00026 ~GdheClient();
00027
00028 void SendCommand(char *cmd){ gdhe::eval_expression(cmd); }
00029 void SendCommand(std::string &str) { gdhe::eval_expression(const_cast<char *> (str.c_str())); }
00030 void Redraw() { SendCommand((char *)"redrawAllWindows"); }
00031 unsigned short CreateFrame(float dim, unsigned char r = 255, unsigned char g = 255, unsigned char b = 255, std::string obj_name = "");
00032 void SetBkGroundColor(unsigned char r = 255, unsigned char g = 255, unsigned char b = 255);
00033 unsigned short CreateGrid(float xs, float ys, float xf, float yf, float step, unsigned char r = 255, unsigned char g = 255, unsigned char b = 255, std::string obj_name = "");
00034 unsigned short CreateSphere(double diameter, unsigned char r = 255, unsigned char g = 255, unsigned char b = 255, std::string obj_name = "");
00035 unsigned short CreateEllipsoid(double dx, double dy, double dz, unsigned char r = 255, unsigned char g = 255, unsigned char b = 255, std::string obj_name = "");
00036
00037 void SetPos(std::string & str_obj_name, double x = 0, double y = 0, double z = 0);
00038 void SetPos(char *obj_name, double x = 0, double y = 0, double z = 0) { std::string str(obj_name); SetPos(str,x,y,z); }
00039 void SetPos(unsigned short id, double x = 0, double y = 0, double z = 0);
00040 void DeleteObject(std::string & obj_name);
00041 void DeleteObject(char *obj_name) { std::string tmp(obj_name); DeleteObject(tmp); }
00042 void DeleteObject(unsigned short obj);
00043 void CleanAll() { SendCommand("unset -nocomplain robots"); SendCommand("unset -nocomplain pos"); }
00044 void GenName(std::string const& name, std::string & str) {
00045 std::ostringstream toadd;
00046 if (name == "") toadd << OBJ_BASE_NAME << ++m_robot_id;
00047 else toadd << name;
00048 str = toadd.str();
00049 }
00050
00051 void PreparePolyline(std::ostringstream & toadd, unsigned short nbpoints, unsigned char r, unsigned char g, unsigned char b, std::string & obj_name, std::string & str_obj_name)
00052 {
00053 std::string tmp;
00054 toadd.precision(3);
00055 toadd.setf(std::ios::fixed, std::ios::floatfield);
00056
00057 GenName(obj_name, str_obj_name);
00058
00059 toadd << "set robots(" << str_obj_name << ") {" "color " << (int)r << " " << (int)g << " " << (int)b << ";polyline " << nbpoints;
00060 }
00061
00062 short FinishPolyline(std::ostringstream & toadd, std::string & obj_name, std::string & str_obj_name)
00063 {
00064 std::string tmp;
00065 toadd << " }";
00066
00067 tmp = toadd.str();
00068 SendCommand(tmp);
00069
00070 SetPos(str_obj_name);
00071
00072 if(obj_name == "") return m_robot_id;
00073 else return -1;
00074 }
00075
00076 template<typename T>
00077 short CreatePolyline(T data[][3], unsigned short nbpoints, unsigned char r = 255, unsigned char g = 255, unsigned char b = 255, std::string obj_name = "")
00078 {
00079 std::ostringstream toadd;
00080 std::string str_obj_name;
00081
00082 PreparePolyline(toadd, nbpoints,r,g,b,obj_name,str_obj_name);
00083 for (unsigned short i = 0; i < nbpoints; i++)
00084 {
00085 toadd << " " << data[i][0] << " " << data[i][1] << " " << data[i][2];
00086 }
00087 return FinishPolyline(toadd, obj_name, str_obj_name);
00088 }
00089
00090 typedef std::vector<jafar::ddf::VEC> PolyLine;
00091 short CreatePolyline(PolyLine traj, unsigned short nbpoints, unsigned char r = 255, unsigned char g = 255, unsigned char b = 255, std::string obj_name = "")
00092 {
00093 std::ostringstream toadd;
00094 std::string str_obj_name;
00095
00096 PreparePolyline(toadd, nbpoints,r,g,b,obj_name,str_obj_name);
00097
00098 for (unsigned short i = 0; i < traj.size(); i++)
00099 {
00100 toadd << " " << traj[i][0] << " " << traj[i][1] << " " << traj[i][2];
00101 }
00102
00103 return FinishPolyline(toadd, obj_name, str_obj_name);
00104 }
00105
00106 };
00107
00108 #endif