Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/handlers/handler.cppGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 // @author Brett Ponsler (bponsler (at) wpi (dot) edu) 00035 #include "handler.h" 00036 #include "../recognizers/recognizer.h" 00037 #include "../lib/engagement_params.h" 00038 #include "../events/event_factory.h" 00039 #include "../actors/actor_manager.h" 00040 00041 Handler::~Handler() 00042 { 00043 // Iterate through all directed gaze recognizers and delete them all 00044 std::map<std::string, Recognizer*>::iterator iter; 00045 00046 // Delete all of the known directed gaze recognizers 00047 for(iter = this->dg_recognizers_.begin(); iter != this->dg_recognizers_.end(); 00048 iter++) 00049 delete iter->second; 00050 00051 // Delete the mutual facial gaze recognizer 00052 if (this->mfg_recognizer_ != NULL) 00053 delete this->mfg_recognizer_; 00054 00055 // Delete the adjacency pair recognizer 00056 if (this->ap_recognizer_ != NULL) 00057 delete this->ap_recognizer_; 00058 00059 // Delete the backchannel recognizer 00060 if (this->bc_recognizer_ != NULL) 00061 delete this->bc_recognizer_; 00062 } 00063 00064 Handler::Handler() : 00065 EventSink() 00066 { 00067 // Set the logger for this object 00068 this->setLogger(""); 00069 00070 // Create a default vector of Recognizers 00071 this->mfg_recognizer_ = NULL; 00072 this->ap_recognizer_ = NULL; 00073 this->bc_recognizer_ = NULL; 00074 00075 // Set the actor to null 00076 this->setActor(""); 00077 } 00078 00079 Handler::Handler(std::string actor, EventSink *parent) : 00080 EventSink(parent) 00081 { 00082 // Set the logger for this object 00083 this->setLogger(parent->getLogger()); 00084 00085 // Create a default vector of Recognizers 00086 this->mfg_recognizer_ = NULL; 00087 this->ap_recognizer_ = NULL; 00088 this->bc_recognizer_ = NULL; 00089 00090 // Set the actor to given values 00091 this->setActor(actor); 00092 } 00093 00094 void Handler::received(std::string topic, ros::Message *message) 00095 { 00096 // Iterate through all directed gaze recognizers and delete them all 00097 std::map<std::string, Recognizer*>::iterator iter; 00098 00099 // Traverse through all the known recognizers 00100 for(iter = this->dg_recognizers_.begin(); iter != this->dg_recognizers_.end(); 00101 iter++) 00102 { 00103 // Send the message to the directed gaze recognizer 00104 iter->second->received(topic, message); 00105 } 00106 00107 // Send the message to the mutual facial gaze recognizer 00108 if (this->mfg_recognizer_ != NULL) 00109 this->mfg_recognizer_->received(topic, message); 00110 00111 // Send the message to the adjacency pair recognizer 00112 if (this->ap_recognizer_ != NULL) 00113 this->ap_recognizer_->received(topic, message); 00114 00115 // Send the message to the backchannel recognizer 00116 if (this->bc_recognizer_ != NULL) 00117 this->bc_recognizer_->received(topic, message); 00118 } 00119 00120 void Handler::setActor(std::string actor) 00121 { 00122 if (actor == "") 00123 this->warn("Handler initialized to a null actor."); 00124 00125 // Set the actor field for this Handler 00126 this->actor_ = actor; 00127 } 00128 00129 std::string Handler::getActor() 00130 { 00131 // Return the actor field for this Handler 00132 return this->actor_; 00133 } 00134 00135 void Handler::addDirectedGazeRecognizer(Recognizer *rec) 00136 { 00137 // Make sure the recognizer exists 00138 if (rec != NULL) 00139 this->dg_recognizers_[rec->getObject()] = rec; 00140 } 00141 00142 void Handler::setMutualFacialGazeRecognizer(Recognizer *rec) 00143 { 00144 if (this->mfg_recognizer_ != NULL) 00145 delete this->mfg_recognizer_; 00146 this->mfg_recognizer_ = rec; 00147 } 00148 00149 void Handler::setAdjacencyPairRecognizer(Recognizer *rec) 00150 { 00151 if (this->ap_recognizer_ != NULL) 00152 delete this->ap_recognizer_; 00153 this->ap_recognizer_ = rec; 00154 } 00155 00156 void Handler::setBackchannelRecognizer(Recognizer *rec) 00157 { 00158 if (this->bc_recognizer_ != NULL) 00159 delete this->bc_recognizer_; 00160 this->bc_recognizer_ = rec; 00161 } 00162 00163 void Handler::addEvent(Event event) 00164 { 00165 Actor *actor = ActorManager::findActor(this->actor_); 00166 00167 if (actor) 00168 { 00169 // Parse the success and failed events 00170 if (EventFactory::isCompleted(event)) 00171 { 00172 // Grab the additional data from the event 00173 std::vector<std::string> event_additional = event.getAdditional(); 00174 00175 // Make sure we have all the information 00176 if (event_additional.size() == 8) 00177 { 00178 // Grab the information from the event 00179 std::string type = event_additional.at(0); 00180 00181 // Grab the start, delay, duration and success of the event 00182 std::string str_start = event_additional.at(3); 00183 std::string str_delay = event_additional.at(4); 00184 std::string str_duration = event_additional.at(5); 00185 std::string str_success = event_additional.at(6); 00186 std::string str_data = event_additional.at(7); 00187 00188 // Determine the success of the connection event 00189 bool success = (str_success == EventFactory::getSuccessName() ? true 00190 : false); 00191 00192 // Determine the duration of the connection event 00193 unsigned long start = atof(str_start.c_str()); 00194 unsigned long delay = atof(str_delay.c_str()); 00195 unsigned long duration = atof(str_duration.c_str()); 00196 00197 // Add the event to this actor's timeline 00198 actor->addEvent(type, success, start, delay, duration, str_data); 00199 } 00200 } 00201 } 00202 } 00203 00204 Recognizer *Handler::getDirectedGazeRecognizer(std::string object) 00205 { 00206 return this->dg_recognizers_[object]; 00207 } 00208 00209 Recognizer *Handler::getMutualFacialGazeRecognizer() 00210 { 00211 return this->mfg_recognizer_; 00212 } 00213 00214 Recognizer *Handler::getAdjacencyPairRecognizer() 00215 { 00216 return this->ap_recognizer_; 00217 } 00218 00219 Recognizer *Handler::getBackchannelRecognizer() 00220 { 00221 return this->bc_recognizer_; 00222 } |