Homepage Demos Overview Downloads Tutorials Reference
Credits

VRmixin.cc

Go to the documentation of this file.
00001 #include <sstream>
00002 
00003 #include "Vision/RawCameraGenerator.h"
00004 #include "Vision/RLEGenerator.h"
00005 #include "Vision/RegionGenerator.h"
00006 #include "Vision/SegmentedColorGenerator.h"
00007 
00008 #include "SketchData.h"
00009 #include "ShapeBlob.h"
00010 #include "Sketch.h"
00011 
00012 #include "Lookout.h"
00013 #include "MapBuilder.h"
00014 #include "ParticleFilter.h"
00015 #include "Pilot.h"
00016 
00017 #include "ViewerConnection.h"  // for port numbers and buffer sizes
00018 #include "VRmixin.h"
00019 
00020 namespace DualCoding {
00021 
00022 //----------------------------------------------------------------
00023 
00024 VRmixin* VRmixin::theOne=NULL;
00025 
00026 //! static function allows us to specify intialization order because static within a function isn't created until the function is called
00027 template<ReferenceFrameType_t _refFrameType,int const init_id, size_t const _width, size_t const _height>
00028 static SketchSpace& createStaticSkS(const std::string& _name) {
00029   static SketchSpace SkS(_name,_refFrameType,init_id,_width,_height);
00030   //  cout << _name << " space is constructed\n";
00031   return SkS;
00032 }
00033 
00034 SketchSpace& VRmixin::getCamSkS() {
00035   return createStaticSkS<camcentric,10000,CameraResolutionX,CameraResolutionY>("cam");
00036 }
00037 SketchSpace& VRmixin::getLocalSkS() {
00038   return createStaticSkS<egocentric,20000,WORLD_WIDTH,WORLD_HEIGHT>("local"); 
00039 }
00040 SketchSpace& VRmixin::getWorldSkS() {
00041   return createStaticSkS<allocentric,30000,WORLD_WIDTH,WORLD_HEIGHT>("world");
00042 }
00043 ShapeSpace& VRmixin::getGroundShS() {
00044   static ShapeSpace ShS(&VRmixin::getCamSkS(),90000,"ground",egocentric);
00045   return ShS;
00046 }
00047 
00048 MapBuilder& VRmixin::getMapBuilder() {
00049   static MapBuilder mapbuilder;
00050   return mapbuilder;
00051 }
00052 
00053 SketchSpace& VRmixin::camSkS=VRmixin::getCamSkS();
00054 ShapeSpace& VRmixin::camShS=VRmixin::getCamSkS().getDualSpace();
00055 
00056 ShapeSpace& VRmixin::groundShS=VRmixin::getGroundShS();
00057 
00058 SketchSpace& VRmixin::localSkS=VRmixin::getLocalSkS();
00059 ShapeSpace& VRmixin::localShS=VRmixin::getLocalSkS().getDualSpace();
00060 
00061 SketchSpace& VRmixin::worldSkS=VRmixin::getWorldSkS();
00062 ShapeSpace& VRmixin::worldShS=VRmixin::getWorldSkS().getDualSpace();
00063 
00064 Shape<AgentData> VRmixin::theAgent;
00065 
00066 Socket *VRmixin::camDialogSock=NULL;
00067 Socket *VRmixin::camRleSock=NULL;
00068 Socket *VRmixin::localDialogSock=NULL;
00069 Socket *VRmixin::localRleSock=NULL;
00070 Socket *VRmixin::worldDialogSock=NULL;
00071 Socket *VRmixin::worldRleSock=NULL;
00072 
00073 MapBuilder& VRmixin::mapBuilder=VRmixin::getMapBuilder();
00074 Pilot VRmixin::pilot;
00075 Lookout VRmixin::lookout;
00076 ParticleFilter VRmixin::filter(VRmixin::getLocalSkS().getDualSpace(),VRmixin::getWorldSkS().getDualSpace());
00077 
00078 
00079 unsigned int VRmixin::instanceCount=0;
00080 unsigned int VRmixin::crewCount=0;
00081 
00082 VRmixin::VRmixin() {
00083   if(instanceCount++==0) {
00084     // only want to do the following once
00085     //cout << "Initializing VRmixin statics" << endl;
00086     if (theOne != NULL) {
00087       cerr << "VRmixin statics already constructed!?!?!" << endl;
00088       return;
00089     }
00090     theOne=this;
00091     camSkS.requireIdx();
00092     localSkS.requireIdx();
00093     worldSkS.requireIdx();
00094     
00095     camDialogSock=wireless->socket(SocketNS::SOCK_STREAM, 1024, DIALOG_BUFFER_SIZE);
00096     camRleSock=wireless->socket(SocketNS::SOCK_STREAM, 1024, RLE_BUFFER_SIZE);
00097     worldDialogSock=wireless->socket(SocketNS::SOCK_STREAM, 1024, DIALOG_BUFFER_SIZE);
00098     worldRleSock=wireless->socket(SocketNS::SOCK_STREAM, 1024, RLE_BUFFER_SIZE);
00099     localDialogSock=wireless->socket(SocketNS::SOCK_STREAM, 1024, DIALOG_BUFFER_SIZE);
00100     localRleSock=wireless->socket(SocketNS::SOCK_STREAM, 1024, RLE_BUFFER_SIZE);
00101     
00102     wireless->setReceiver(camDialogSock->sock, &camDialogSockCallback);
00103     wireless->setReceiver(worldDialogSock->sock, &worldDialogSockCallback);
00104     wireless->setReceiver(localDialogSock->sock, &localDialogSockCallback);
00105     
00106     wireless->setDaemon(camDialogSock,   true);
00107     wireless->setDaemon(camRleSock,      true);
00108     wireless->setDaemon(worldDialogSock, true);
00109     wireless->setDaemon(worldRleSock,    true);
00110     wireless->setDaemon(localDialogSock, true);
00111     wireless->setDaemon(localRleSock,    true);
00112     
00113     wireless->listen(camDialogSock,   CAM_DIALOG_PORT);
00114     wireless->listen(camRleSock,      CAM_RLE_PORT);
00115     wireless->listen(worldDialogSock, WORLD_DIALOG_PORT);
00116     wireless->listen(worldRleSock,    WORLD_RLE_PORT);
00117     wireless->listen(localDialogSock, LOCAL_DIALOG_PORT);
00118     wireless->listen(localRleSock,    LOCAL_RLE_PORT);
00119     
00120     camSkS.viewer->setDialogSocket(camDialogSock,     CAM_DIALOG_PORT);
00121     camSkS.viewer->setRleSocket(camRleSock,           CAM_RLE_PORT);
00122     localSkS.viewer->setDialogSocket(localDialogSock, LOCAL_DIALOG_PORT);
00123     localSkS.viewer->setRleSocket(localRleSock,       LOCAL_RLE_PORT);
00124     worldSkS.viewer->setDialogSocket(worldDialogSock, WORLD_DIALOG_PORT);
00125     worldSkS.viewer->setRleSocket(worldRleSock,       WORLD_RLE_PORT);
00126 
00127     theAgent = Shape<AgentData>(worldShS,Point());
00128 
00129     mapBuilder.SetAutoDelete(false);
00130     pilot.SetAutoDelete(false);
00131     lookout.SetAutoDelete(false);
00132   }
00133 }
00134 
00135 VRmixin::~VRmixin() {
00136   if(--instanceCount==0) {
00137     // cout << "Destructing VRmixin statics" << endl;
00138     if (theOne == NULL) {
00139       cerr << "VRmixin statics already destructed!?!?!" << endl;
00140       return;
00141     }
00142     
00143     wireless->setDaemon(camDialogSock,  false);
00144     wireless->setDaemon(camRleSock,     false);
00145     wireless->setDaemon(localDialogSock,false);
00146     wireless->setDaemon(localRleSock,   false);
00147     wireless->setDaemon(worldDialogSock,false);
00148     wireless->setDaemon(worldRleSock,   false);
00149     
00150     wireless->close(camRleSock->sock);
00151     wireless->close(camDialogSock->sock);
00152     wireless->close(localRleSock->sock);
00153     wireless->close(localDialogSock->sock);
00154     wireless->close(worldRleSock->sock);
00155     wireless->close(worldDialogSock->sock);
00156     
00157     theOne=NULL;
00158     
00159     // clear ShapeSpace first because it may contain rendering links to SketchSpace
00160     camShS.clear();
00161     camSkS.bumpRefreshCounter(); // release visible sketches
00162     camSkS.clear();
00163     
00164     localShS.clear();
00165     localSkS.bumpRefreshCounter(); // release visible sketches
00166     localSkS.clear();
00167     
00168     worldShS.clear();
00169     worldSkS.bumpRefreshCounter(); // release visible sketches
00170     worldSkS.clear();
00171     
00172     filter.reinitialize();
00173     
00174     camSkS.freeIndexes();
00175     localSkS.freeIndexes();
00176     worldSkS.freeIndexes();
00177   }
00178 }
00179 
00180 void VRmixin::startCrew() {
00181   if(crewCount++==0) {
00182     //cout << "Starting crew" << endl;
00183     mapBuilder.SetAutoDelete(false);
00184     mapBuilder.DoStart();
00185     lookout.SetAutoDelete(false);
00186     lookout.DoStart();
00187     pilot.SetAutoDelete(false);
00188     pilot.DoStart();
00189   }
00190 }
00191 
00192 void VRmixin::stopCrew() {
00193   if(--crewCount==0) {
00194     //cout << "Stopping crew" << endl;
00195     pilot.DoStop();
00196     lookout.DoStop();
00197     mapBuilder.DoStop();
00198   }
00199 }
00200 
00201 void VRmixin::projectToGround(const NEWMAT::Matrix& camToBase,
00202             const NEWMAT::ColumnVector& ground_plane) {
00203   groundShS.clear();
00204   groundShS.importShapes(camShS.allShapes());
00205   const vector<ShapeRoot> &groundShapes_vec = groundShS.allShapes();
00206   for(size_t i = 0; i < groundShapes_vec.size(); i++)
00207     groundShapes_vec[i]->projectToGround(camToBase, ground_plane);
00208 }
00209 
00210 int VRmixin::camDialogSockCallback(char *buf, int bytes) {
00211   static std::string incomplete;
00212   dialogCallback(buf, bytes, incomplete, theOne->camSkS, theOne->camShS);
00213   return 0;
00214 }
00215 
00216 int VRmixin::localDialogSockCallback(char *buf, int bytes) {
00217   static std::string incomplete;
00218   dialogCallback(buf, bytes, incomplete, theOne->localSkS, theOne->localShS);
00219   return 0;
00220 }
00221 
00222 int VRmixin::worldDialogSockCallback(char *buf, int bytes) {
00223   static std::string incomplete;
00224   dialogCallback(buf, bytes, incomplete, theOne->worldSkS, theOne->worldShS);
00225   return 0;
00226 }
00227 
00228 void VRmixin::dialogCallback(char* buf, int bytes, std::string& incomplete,
00229            SketchSpace& SkS, ShapeSpace& ShS) {
00230   std::string s(buf,bytes);
00231   while(s.size()>0) {
00232     size_t endline=s.find('\n');
00233     if(endline==std::string::npos) {
00234       incomplete+=s;
00235       return;
00236     }
00237     else {
00238       incomplete+=s.substr(0,endline);
00239       theOne->processSketchRequest(incomplete,SkS,ShS);
00240       incomplete.erase();
00241       s=s.substr(endline+1);
00242     }
00243   }
00244   return;
00245 }
00246 
00247 bool VRmixin::rleEncodeSketch(const SketchDataRoot& image)
00248 {
00249   unsigned int avail = RLE_BUFFER_SIZE-1;
00250   Socket* rleSock = image.getSpace().viewer->getRleSocket();
00251   char* buf=(char*)rleSock->getWriteBuffer(avail);
00252   ASSERTRETVAL(buf!=NULL,"could not get buffer",false);
00253   unsigned int used = image.saveBuffer(buf, avail);
00254   rleSock->write(used);
00255   return true;
00256 }
00257 
00258 //! Import a color-segmented image as a Sketch<uchar>
00259 Sketch<uchar> VRmixin::sketchFromSeg() {
00260   Sketch<uchar> cam(camSkS, "camimage");
00261   cam->setColorMap(segMap);
00262   size_t const npixels = cam->getNumPixels();
00263   cmap_t* seg_image = ProjectInterface::defSegmentedColorGenerator->getImage(CAM_LAYER,CAM_CHANNEL);
00264   for(size_t i = 0; i < npixels; i++)
00265     cam[i] = seg_image[i];
00266   return cam;
00267 }
00268 
00269 //! Import channel n as a Sketch<uchar>
00270 Sketch<uchar> VRmixin::sketchFromChannel(RawCameraGenerator::channel_id_t chan) {
00271   if ( chan < 0 || chan >= RawCameraGenerator::NUM_CHANNELS)
00272     chan = RawCameraGenerator::CHAN_Y;
00273   Sketch<uchar> cam(camSkS,"sketchFromChannel");
00274   cam->setColorMap(grayMap);
00275   uchar* campixels = cam->getRawPixels();
00276   int const incr = ProjectInterface::defRawCameraGenerator->getIncrement(CAM_LAYER);
00277   int const skip = ProjectInterface::defRawCameraGenerator->getSkip(CAM_LAYER);
00278   uchar* chan_ptr = ProjectInterface::defRawCameraGenerator->getImage(CAM_LAYER,chan);
00279   if(chan_ptr==NULL) {
00280     for (unsigned int row = 0; row < cam->getHeight(); row++)
00281       for (unsigned int col = 0; col < cam->getWidth(); col++)
00282         *campixels++ = 0;
00283   } else {
00284     chan_ptr -= incr;  // back up by one pixel to prepare for loop
00285     for (unsigned int row = 0; row < cam->getHeight(); row++) {
00286       for (unsigned int col = 0; col < cam->getWidth(); col++)
00287         *campixels++ = *(chan_ptr += incr);
00288       chan_ptr += skip;
00289     }
00290   }
00291   return cam;
00292 }
00293 
00294 Sketch<uchar> VRmixin::sketchFromRawY() {
00295   return sketchFromChannel(RawCameraGenerator::CHAN_Y);
00296 }
00297 
00298 
00299 //! Import the results of the region generator as a vector of Shape<BlobData>
00300 vector<Shape<BlobData> >
00301 VRmixin::getBlobsFromRegionGenerator(int color, int minarea,
00302              BlobData::BlobOrientation_t orient, int maxblobs) {
00303   vector<Shape<BlobData> > result;
00304   const CMVision::run<uchar> *rle_buffer = reinterpret_cast<const CMVision::run<uchar>*>
00305     (ProjectInterface::defRLEGenerator->getImage(CAM_LAYER,CAM_CHANNEL));
00306   const CMVision::color_class_state* ccs = reinterpret_cast<const CMVision::color_class_state*>
00307     (ProjectInterface::defRegionGenerator->getImage(CAM_LAYER,CAM_CHANNEL));
00308   //  cout << "Color " << color << " name '" << ccs[color].name 
00309   //   << "' has " << ccs[color].num << " regions." << endl;
00310   const rgb rgbvalue = ProjectInterface::getColorRGB(color);
00311   const CMVision::region* list_head = ccs[color].list;
00312   for (int i=0; list_head!=NULL && i<maxblobs && list_head->area >= minarea; list_head = list_head->next, i++) {
00313     BlobData* blobdat = BlobData::new_blob(camShS,*list_head, rle_buffer, orient, rgbvalue);
00314     result.push_back(Shape<BlobData>(blobdat));
00315   }
00316   return result;
00317 }
00318 
00319 void VRmixin::processSketchRequest(const std::string &line,
00320            SketchSpace& SkS, 
00321            ShapeSpace& ShS)
00322 {
00323   Socket* dialogSock = SkS.viewer->getDialogSocket();
00324   if(line.compare(0,strlen("size"),"size")==0) {
00325     dialogSock->printf("size begin\n");
00326     dialogSock->printf("width %d\nheight %d\n",int(SkS.getWidth()),int(SkS.getHeight()));
00327     dialogSock->printf("size end\n");
00328   }
00329   else if(line.compare(0,strlen("list"),"list")==0) {
00330     dialogSock->printf("list begin\n");
00331     SkS.viewer->writeBigString(SkS.getTmatForGUI());  
00332     SkS.viewer->writeBigString(SkS.getSketchListForGUI());  
00333     SkS.viewer->writeBigString(ShS.getShapeListForGUI()); 
00334     dialogSock->printf("list end\n");
00335   } else if(line.compare(0,strlen("get"),"get")==0) {
00336     dialogSock->printf("get begin\n");
00337     std::string tempstring = line.substr(strlen("get"),
00338            line.length()-strlen("get"));
00339     std::istringstream ist(tempstring);
00340     int requested_id = -1;
00341     ist >> requested_id;
00342     dialogSock->printf("get read:%d\n",requested_id);
00343     SketchDataRoot* sketchptr=(SkS.retrieveSketch(requested_id));
00344     if(sketchptr != NULL)
00345       rleEncodeSketch(*sketchptr);
00346     dialogSock->printf("get end\n");
00347   } else {
00348     dialogSock->printf("Invalid command\n");
00349   }
00350 }
00351 
00352 } // namespace

DualCoding 3.0beta
Generated Wed Oct 4 00:01:54 2006 by Doxygen 1.4.7