31 Scene::Scene(std::string dbName, std::string sceneName,
int rankingMethodEntry,
double alphaEntry,
double betaEntry,
double gammaEntry)
45 sqlite3_open(
DbName.c_str(), &database);
46 sqlite3_stmt *statement;
47 std::stringstream sqlReq;
48 sqlReq <<
"SELECT type,px,py,pz,setId,observedId FROM recorded_objects " 49 <<
"INNER JOIN recorded_patterns on recorded_sets.patternId = recorded_patterns.id " 50 <<
"INNER JOIN recorded_sets on recorded_objects.setId = recorded_sets.id " 51 <<
"WHERE recorded_patterns.name = '" 54 const std::string& tmp = sqlReq.str();
55 const char* SQLREQ = tmp.c_str();
56 int returnPrepare = sqlite3_prepare_v2(database, SQLREQ, -1,&statement, 0);
57 ROS_DEBUG_STREAM(
"Scene::getObjectFromDb prepare database returned code : " << returnPrepare);
58 if(returnPrepare == SQLITE_OK)
60 int cols = sqlite3_column_count(statement);
64 result = sqlite3_step(statement);
67 ROS_ERROR_STREAM(
"Scene::getObjectFromDb something went terribly wrong when recovering objects for scene : " << SceneName);
70 if(result == SQLITE_ROW)
73 std::string name = (
char*)sqlite3_column_text(statement, 0);
74 std::string X = (
char*)sqlite3_column_text(statement, 1);
75 std::string Y = (
char*)sqlite3_column_text(statement, 2);
76 std::string Z = (
char*)sqlite3_column_text(statement, 3);
77 std::string setId = (
char*)sqlite3_column_text(statement, 4);
78 std::string observedId = (
char*)sqlite3_column_text(statement, 5);
79 std::string objectMetaName = name + observedId;
84 <<
" with observed Id " << observedId
85 <<
" in scene " << SceneName);
87 objectPoint->setX(boost::lexical_cast<double>(X));
88 objectPoint->setY(boost::lexical_cast<double>(Y));
89 objectPoint->setZ(boost::lexical_cast<double>(Z));
90 objectPoint->setSetId(boost::lexical_cast<int>(setId));
91 this->
ObjectMap[objectMetaName]->AddPoint(objectPoint);
99 sqlite3_finalize(statement);
101 sqlite3_close(database);
102 ROS_ERROR_COND(
ObjectMap.size() == 0,
"Scene::getObjectFromDb Could not get any Object. Please check the database location and the pattern name");
111 std::stringstream sqlReq;
112 sqlReq <<
"SELECT COUNT(DISTINCT recorded_sets.id) FROM recorded_sets " 113 <<
"INNER JOIN recorded_patterns on recorded_sets.patternId = recorded_patterns.id " 114 <<
"WHERE recorded_patterns.name = '" 117 const std::string& tmp = sqlReq.str();
118 const char* SQLREQ = tmp.c_str();
120 sqlite3_open(
DbName.c_str(), &database);
121 sqlite3_stmt *statement;
122 if(sqlite3_prepare_v2(database, SQLREQ, -1, &statement, 0) == SQLITE_OK)
124 int cols = sqlite3_column_count(statement);
125 int result = sqlite3_step(statement);
126 if(result == SQLITE_ROW)
128 for(
int col = 0; col < cols; col++)
130 std::string
s = (
char*)sqlite3_column_text(statement, col);
134 sqlite3_finalize(statement);
136 sqlite3_close(database);
145 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
147 objectIterator->second->CalcAveragePos();
148 objectIterator->second->CalcPosVar();
154 bool safetyTest =
false;
156 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
158 for(
int point=0; point<objectIterator->second->getObjetsCount(); point++)
162 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator2=
ObjectMap.begin(); objectIterator2!=
ObjectMap.end(); ++objectIterator2)
164 if(objectIterator2->first != objectIterator->first)
167 for(
int k=0; k<objectIterator2->second->getObjetsCount(); k++)
169 if((objectIterator2->second->getPoint(k))->getSetId() == (objectIterator->second->getPoint(point))->getSetId())
172 distance += (objectIterator->second->getPoint(point))->calcDistance(*(objectIterator2->second->getPoint(k)));
178 if(safetyTest==
true){objectIterator->second->AddDistance(distance/counter);safetyTest=
false;}
180 objectIterator->second->AverageDistance();
187 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
189 double presenceInScene = (boost::lexical_cast<
double>(objectIterator->second->getObjetsCount())/boost::lexical_cast<double>(this->
SceneObjectsCount));
190 ROS_DEBUG_STREAM(
"Scene::calcPresenceInSceneForEachObject Object occurence in scene : " 191 << objectIterator->second->getObjetsCount()
193 <<
", presence in scene : " << presenceInScene
194 <<
", for object : " << objectIterator->second->getObjectName()
195 <<
" with observed Id " << objectIterator->second->getObservedId());
196 objectIterator->second->setPresenceInScene(presenceInScene);
204 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
206 objectIterator->second->DisplayStats();
213 std::function<bool(const std::pair<std::string,ObjectSharedPointer>,
const std::pair<std::string,ObjectSharedPointer>)> compare_distance_min =
214 [](
const std::pair<std::string,ObjectSharedPointer>& i,
const std::pair<std::string,ObjectSharedPointer>& j)
215 {
return i.second->getAverageDistanceToOtherObjects() < j.second->getAverageDistanceToOtherObjects();};
217 std::function<bool(const std::pair<std::string,ObjectSharedPointer>,
const std::pair<std::string,ObjectSharedPointer>)> compare_position_min =
218 [](
const std::pair<std::string,ObjectSharedPointer>& i,
const std::pair<std::string,ObjectSharedPointer>& j)
219 {
return i.second->getPosVar() < j.second->getPosVar();};
222 double minValueDist = (std::min_element(
ObjectMap.begin(),
ObjectMap.end(),compare_distance_min))->second->getAverageDistanceToOtherObjects();
223 double minValuePos = (std::min_element(
ObjectMap.begin(),
ObjectMap.end(),compare_position_min))->second->getPosVar();
225 ROS_DEBUG_STREAM(
"Scene::normalize minimum average distance value found " << minValueDist);
226 ROS_DEBUG_STREAM(
"Scene::normalize minimum average position value found " << minValuePos);
228 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
230 double bufferMin = (objectIterator->second->getAverageDistanceToOtherObjects()-minValueDist);
232 << objectIterator->second->getObjectName() <<
" : " << bufferMin);
233 (objectIterator->second)->setNormalizedAverageDistanceToOtherObjects(bufferMin);
234 (objectIterator->second)->setNormalizedPosVar((objectIterator->second->getPosVar()-minValuePos));
237 std::function<bool(const std::pair<std::string,ObjectSharedPointer>,
const std::pair<std::string,ObjectSharedPointer>)> compare_distance_max =
238 [](
const std::pair<std::string,ObjectSharedPointer>& i,
const std::pair<std::string,ObjectSharedPointer>& j)
239 {
return i.second->getNormalizedAverageDistanceToOtherObjects() < j.second->getNormalizedAverageDistanceToOtherObjects();};
241 std::function<bool(const std::pair<std::string,ObjectSharedPointer>,
const std::pair<std::string,ObjectSharedPointer>)> compare_position_max =
242 [](
const std::pair<std::string,ObjectSharedPointer>& i,
const std::pair<std::string,ObjectSharedPointer>& j)
243 {
return i.second->getNormalizedPosVar() < j.second->getNormalizedPosVar();};
246 double maxValueDist = (std::max_element(
ObjectMap.begin(),
ObjectMap.end(),compare_distance_max))->second->getNormalizedAverageDistanceToOtherObjects();
247 double maxValuePos = (std::max_element(
ObjectMap.begin(),
ObjectMap.end(),compare_position_max))->second->getNormalizedPosVar();
249 ROS_DEBUG_STREAM(
"Scene::normalize max average distance value found " << maxValueDist);
250 ROS_DEBUG_STREAM(
"Scene::normalize max average position value found " << maxValuePos);
252 double valueDistCoef = 1/maxValueDist;
253 double posVarCoef = 1/maxValuePos;
255 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
257 objectIterator->second->setNormalizedAverageDistanceToOtherObjects(objectIterator->second->getNormalizedAverageDistanceToOtherObjects()*valueDistCoef+0.1);
258 objectIterator->second->setNormalizedPosVar(objectIterator->second->getNormalizedPosVar()*posVarCoef+0.1);
265 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
267 objectIterator->second->rank();
273 std::ofstream file(filePath, std::ios::out | std::ios::app);
276 file << std::endl << std::endl <<
"Scene " << this->
SceneName << std::endl;
279 for (std::unordered_map<std::string,ObjectSharedPointer>::iterator objectIterator=
ObjectMap.begin(); objectIterator!=
ObjectMap.end(); ++objectIterator)
281 objectIterator->second->publishLogs(filePath);
#define ROS_DEBUG_STREAM_COND(cond, args)
#define ROS_ERROR_STREAM_COND(cond, args)
void getObjectAverageDistance()
#define ROS_ERROR_COND(cond,...)
void calcAveragePositionForEachObject()
void calcPresenceInSceneForEachObject()
std::shared_ptr< Object > ObjectSharedPointer
#define ROS_DEBUG_STREAM(args)
std::shared_ptr< ObjectPoint > ObjectPointSharedPointer
std::unordered_map< std::string, ObjectSharedPointer > ObjectMap
#define ROS_ERROR_STREAM(args)
void publishLogs(std::string filePath)