00001 #ifndef DB_PLANNER_PLANNER_H
00002 #define DB_PLANNER_PLANNER_H
00003
00004 #include <string>
00005 #include <vector>
00006 #include "aligner.h"
00007 #include "grasp.h"
00008 #include "grasp_ranker.h"
00009 #include "model.h"
00010 #include "neighbor_finder.h"
00011 #include "training_planner.h"
00012 using std::string;
00013 using std::vector;
00014
00015 namespace db_planner {
00016
00017 template <class Input>
00018 class Planner {
00019 private:
00020 const NeighborFinder<Input>& neighbor_finder_;
00021 const TrainingPlanner& training_planner_;
00022 const GraspRanker& grasp_selector_;
00023 const Aligner<Input>& aligner_;
00024 const int num_neighbors_;
00025 vector<Grasp> grasps_;
00026 vector<Grasp>::iterator current_grasp_;
00027 const Input& input_;
00028 public:
00029 Planner(const NeighborFinder<Input>& neighbor_finder,
00030 const TrainingPlanner& training_planner,
00031 const GraspRanker& grasp_ranker,
00032 const Aligner<Input>& aligner,
00033 const int num_neighbors)
00034 : neighbor_finder_(neighbor_finder),
00035 training_planner_(training_planner),
00036 grasp_ranker_(grasp_ranker),
00037 aligner_(aligner),
00038 num_neighbors_(num_neighbors) { }
00039
00040 bool CreatePlan(const Input& input) {
00041
00042 input_ = input;
00043
00044 vector<string> neighbors(num_neighbors_);
00045 if (!neighbor_finder_.Find(input_, num_neighbors_, &neighbors))
00046 return false;
00047
00048 grasps_.clear();
00049 for (vector<string>::const_iterator neighbor_name = neighbors.begin();
00050 neighbor_name != neighbors.end(); ++neighbor_name) {
00051 vector<Grasp> trained_grasps;
00052 training_planner_.TrainedGrasps(*neighbor_name, &trained_grasps);
00053 grasps_.insert(grasps_.end(),
00054 trained_grasps.begin(),
00055 trained_grasps.end());
00056 }
00057
00058 if (!grasp_ranker_.Rank(&grasps_))
00059 return false;
00060 current_grasp_ = grasps_.begin();
00061 return true;
00062 }
00063
00064
00065
00066
00067 bool NextGrasp(Grasp* grasp) {
00068 for ( ; current_grasp_ != grasps_.end(); current_grasp_++) {
00069 float alignemnt[16];
00070 if (aligner_.Align(*(current_grasp_->SourceModel()), input_, alignemnt)) {
00071 *grasp = current_grasp_->Transform(alignemnt);
00072 return true;
00073 }
00074 }
00075 return false;
00076 }
00077 };
00078
00079
00080 }
00081
00082
00083 #endif // DB_PLANNER_PLANNER_H