00001 /***************************** License ********************************** 00002 00003 Copyright (C) 2008 by Communicative Machines 00004 http://www.cmlabs.com All rights reserved. 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 00020 ************************************************************************/ 00021 00022 00023 #include "thirdparty/ObjectTrack.h" 00024 00025 namespace cmlabs { 00026 00027 ObjectObservation::ObjectObservation() { 00028 id = Object::createUniqueID("ObjectObservation"); 00029 image = NULL; 00030 } 00031 00032 ObjectObservation::ObjectObservation(const JString& xml) { 00033 id = Object::createUniqueID("ObjectObservation"); 00034 image = NULL; 00035 tracks.noDelete(); 00036 if (!fromXML(xml)) { 00037 } 00038 } 00039 00040 ObjectObservation::ObjectObservation(XMLNode* node) { 00041 id = Object::createUniqueID("ObjectObservation"); 00042 image = NULL; 00043 tracks.noDelete(); 00044 if (!fromXML(node)) { 00045 } 00046 } 00047 00048 ObjectObservation::~ObjectObservation() { 00049 delete(image); 00050 tracks.noDelete(); 00051 image = NULL; 00052 } 00053 00054 Object* ObjectObservation::clone() const { 00055 ObjectObservation* obs = new ObjectObservation(); 00056 obs->frame = frame; 00057 obs->id = id; 00058 obs->scene = scene; 00059 obs->timestamp = timestamp; 00060 obs->box = box; 00061 if (image != NULL) 00062 obs->image = (Bitmap*) image->clone(); 00063 obs->properties.copyAll(properties); 00064 obs->tracks.addAll(tracks); 00065 return obs; 00066 } 00067 00068 JString ObjectObservation::print() { 00069 JString str = JString::format("\n"); 00070 return str; 00071 } 00072 00073 JString ObjectObservation::toXML() { 00074 return JString::format("<ObjectTrack>\n</ObjectTrack>"); 00075 } 00076 00077 bool ObjectObservation::fromXML(const JString& xml) { 00078 00079 if (xml.length() == 0) 00080 return false; 00081 00082 XMLParser* xmlParser = new XMLParser(); 00083 xmlParser->parse(xml); 00084 bool ret = fromXML(xmlParser->getRootNode()); 00085 delete(xmlParser); 00086 return ret; 00087 } 00088 00089 bool ObjectObservation::fromXML(XMLNode* node) { 00090 if (node == NULL) { 00091 return false; 00092 } 00093 if (!node->getTag().equalsIgnoreCase("ObjectTrack")) 00094 return false; 00095 00096 //XMLNode* xmlNode = node->getChildNode("collection"); 00097 //if (xmlNode == NULL) 00098 // return false; 00099 //if (!list.fromXML(xmlNode)) 00100 // return false; 00101 00102 return true; 00103 } 00104 00105 JString ObjectObservation::get(const JString& key) { 00106 Object* obj = properties.get(key); 00107 if (obj == NULL) return ""; 00108 return obj->print(); 00109 } 00110 00111 bool ObjectObservation::set(const JString& key, const JString& value) { 00112 return properties.put(key, value.clone()); 00113 } 00114 00115 Object* ObjectObservation::getObject(const JString& key) { 00116 return properties.get(key); 00117 } 00118 00119 bool ObjectObservation::setObject(const JString& key, Object* value) { 00120 return properties.put(key, value); 00121 } 00122 00123 bool ObjectObservation::associateWithTrack(const JString& trackID) { 00124 if (tracks.contains(trackID)) 00125 return true; 00126 return tracks.add(trackID); 00127 } 00128 00129 bool ObjectObservation::disassociateWithTrack(const JString& trackID) { 00130 return tracks.remove(trackID); 00131 } 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 ObjectTrackEntry::ObjectTrackEntry() { 00154 observations.noDelete(); 00155 } 00156 00157 ObjectTrackEntry::ObjectTrackEntry(ObjectObservation* obs) { 00158 observations.noDelete(); 00159 if (obs != NULL) { 00160 timestamp = obs->timestamp; 00161 frame = obs->frame; 00162 scene = obs->scene; 00163 associateObservation(obs); 00164 } 00165 } 00166 00167 ObjectTrackEntry::ObjectTrackEntry(const JString& xml) { 00168 observations.noDelete(); 00169 if (!fromXML(xml)) { 00170 } 00171 } 00172 00173 ObjectTrackEntry::ObjectTrackEntry(XMLNode* node) { 00174 observations.noDelete(); 00175 if (!fromXML(node)) { 00176 } 00177 } 00178 00179 ObjectTrackEntry::~ObjectTrackEntry() {} 00180 00181 Object* ObjectTrackEntry::clone() const { 00182 ObjectTrackEntry* entry = new ObjectTrackEntry(); 00183 entry->timestamp = timestamp; 00184 entry->frame = frame; 00185 entry->scene = scene; 00186 entry->observations.copyAll(observations); 00187 return entry; 00188 } 00189 00190 JString ObjectTrackEntry::print() { 00191 JString str = JString::format("\n"); 00192 return str; 00193 } 00194 00195 JString ObjectTrackEntry::toXML() { 00196 return JString::format("<ObjectTrack>\n</ObjectTrack>"); 00197 } 00198 00199 bool ObjectTrackEntry::fromXML(const JString& xml) { 00200 00201 if (xml.length() == 0) 00202 return false; 00203 00204 XMLParser* xmlParser = new XMLParser(); 00205 xmlParser->parse(xml); 00206 bool ret = fromXML(xmlParser->getRootNode()); 00207 delete(xmlParser); 00208 return ret; 00209 } 00210 00211 bool ObjectTrackEntry::fromXML(XMLNode* node) { 00212 if (node == NULL) { 00213 return false; 00214 } 00215 if (!node->getTag().equalsIgnoreCase("ObjectTrack")) 00216 return false; 00217 00218 //XMLNode* xmlNode = node->getChildNode("collection"); 00219 //if (xmlNode == NULL) 00220 // return false; 00221 //if (!list.fromXML(xmlNode)) 00222 // return false; 00223 00224 return true; 00225 } 00226 00227 bool ObjectTrackEntry::associateObservation(ObjectObservation* obs) { 00228 if (obs == NULL) return false; 00229 if (frame != obs->frame) return false; 00230 return observations.put(obs->id, obs); 00231 } 00232 00233 bool ObjectTrackEntry::disassociateObservation(ObjectObservation* obs) { 00234 if (obs == NULL) return false; 00235 if (frame != obs->frame) return false; 00236 return observations.remove(obs->id); 00237 } 00238 00239 Box ObjectTrackEntry::getBoundingBox() { 00240 00241 Box box; 00242 if (observations.getCount() == 0) 00243 return box; 00244 00245 ObjectObservation* obs = (ObjectObservation*) observations.getFirst(); 00246 if (obs == NULL) 00247 return box; 00248 00249 box = obs->box; 00250 for (int n=1; n<observations.getCount(); n++) { 00251 if ( (obs = (ObjectObservation*) observations.get(n)) != NULL) { 00252 box.growToBoundingBox(obs->box); 00253 } 00254 } 00255 return box; 00256 } 00257 00258 00259 00260 00261 00262 00263 00264 00265 00266 00267 00268 00269 00270 00271 00272 00273 00274 00275 ObjectTrack::ObjectTrack() { 00276 id = Object::createUniqueID("ObjectTrack"); 00277 } 00278 00279 ObjectTrack::ObjectTrack(const JString& xml) { 00280 id = Object::createUniqueID("ObjectTrack"); 00281 if (!fromXML(xml)) { 00282 } 00283 } 00284 00285 ObjectTrack::ObjectTrack(XMLNode* node) { 00286 id = Object::createUniqueID("ObjectTrack"); 00287 if (!fromXML(node)) { 00288 } 00289 } 00290 00291 ObjectTrack::~ObjectTrack() {} 00292 00293 Object* ObjectTrack::clone() const { 00294 ObjectTrack* track = new ObjectTrack(); 00295 track->id = id; 00296 track->entries.copyAll(entries); 00297 return track; 00298 } 00299 00300 JString ObjectTrack::print() { 00301 JString str = JString::format("\n"); 00302 return str; 00303 } 00304 00305 JString ObjectTrack::toXML() { 00306 return JString::format("<ObjectTrack>\n</ObjectTrack>"); 00307 } 00308 00309 bool ObjectTrack::fromXML(const JString& xml) { 00310 00311 if (xml.length() == 0) 00312 return false; 00313 00314 XMLParser* xmlParser = new XMLParser(); 00315 xmlParser->parse(xml); 00316 bool ret = fromXML(xmlParser->getRootNode()); 00317 delete(xmlParser); 00318 return ret; 00319 } 00320 00321 bool ObjectTrack::fromXML(XMLNode* node) { 00322 if (node == NULL) { 00323 return false; 00324 } 00325 if (!node->getTag().equalsIgnoreCase("ObjectTrack")) 00326 return false; 00327 00328 //XMLNode* xmlNode = node->getChildNode("collection"); 00329 //if (xmlNode == NULL) 00330 // return false; 00331 //if (!list.fromXML(xmlNode)) 00332 // return false; 00333 00334 return true; 00335 } 00336 00337 00338 ObjectTrackEntry* ObjectTrack::get(unsigned long long frame) { 00339 return (ObjectTrackEntry*) entries.get(JString(frame)); 00340 } 00341 00342 ObjectTrackEntry* ObjectTrack::getFirst() { 00343 return (ObjectTrackEntry*) entries.getFirst(); 00344 } 00345 00346 ObjectTrackEntry* ObjectTrack::getLast() { 00347 return (ObjectTrackEntry*) entries.getLast(); 00348 } 00349 00350 JTime ObjectTrack::getFirstActivityTime() { 00351 ObjectTrackEntry* entry = getFirst(); 00352 if (entry == NULL) return JTime::createInvalid(); 00353 return entry->timestamp; 00354 } 00355 00356 unsigned long long ObjectTrack::getFirstActivityFrame() { 00357 ObjectTrackEntry* entry = getFirst(); 00358 if (entry == NULL) return 0; 00359 return entry->frame; 00360 } 00361 00362 JTime ObjectTrack::getLastActivityTime() { 00363 ObjectTrackEntry* entry = getLast(); 00364 if (entry == NULL) return JTime::createInvalid(); 00365 return entry->timestamp; 00366 } 00367 00368 unsigned long long ObjectTrack::getLastActivityFrame() { 00369 ObjectTrackEntry* entry = getLast(); 00370 if (entry == NULL) return 0; 00371 return entry->frame; 00372 } 00373 00374 bool ObjectTrack::associateObservation(ObjectObservation* obs) { 00375 if (obs == NULL) return false; 00376 unsigned long long obsFrame = obs->frame; 00377 if (obsFrame < 0) return false; 00378 ObjectTrackEntry* entry = (ObjectTrackEntry*) entries.get(JString(obsFrame)); 00379 if (entry == NULL) { 00380 entry = new ObjectTrackEntry(obs); 00381 entries.put(JString(obsFrame), entry); 00382 } 00383 return obs->associateWithTrack(this->id); 00384 } 00385 00386 bool ObjectTrack::disassociateObservation(ObjectObservation* obs) { 00387 if (obs == NULL) return false; 00388 unsigned long long obsFrame = obs->frame; 00389 if (obsFrame < 0) return false; 00390 ObjectTrackEntry* entry = (ObjectTrackEntry*) entries.get(JString(obsFrame)); 00391 if (entry != NULL) 00392 entry->disassociateObservation(obs); 00393 return obs->disassociateWithTrack(this); 00394 } 00395 00396 00397 00398 00399 00400 00401 00402 } // namespace cmlabs 00403