00001
00002 #include "strk_art_model_display_process.h"
00003 #include <vcl_iostream.h>
00004 #include <vcl_vector.h>
00005 #include <vgl/vgl_point_2d.h>
00006 #include <vtol/vtol_topology_object.h>
00007 #include <vtol/vtol_vertex.h>
00008 #include <vtol/vtol_edge.h>
00009 #include <vtol/vtol_vertex_2d.h>
00010 #include <vtol/vtol_face_2d.h>
00011
00012 strk_art_model_display_process::strk_art_model_display_process()
00013 {
00014 failure_ = false;
00015 first_frame_ = true;
00016 }
00017
00018 strk_art_model_display_process::~strk_art_model_display_process()
00019 {
00020 }
00021
00022 static void construct_face_and_cog(const int n_verts,
00023 const int frame_index,
00024 vnl_matrix<double> const& cogs,
00025 vnl_matrix<double> const& X,
00026 vnl_matrix<double> const& Y,
00027 vtol_face_2d_sptr & face,
00028 vtol_vertex_2d_sptr& cog)
00029 {
00030 vcl_vector<vtol_vertex_sptr> verts;
00031 for (int i = 0; i<n_verts; i++)
00032 {
00033 vtol_vertex* v2d = new vtol_vertex_2d(X[frame_index][i], Y[frame_index][i]);
00034 verts.push_back(v2d);
00035 }
00036 face = new vtol_face_2d(verts);
00037 vgl_point_2d<double> p(cogs[frame_index][0], cogs[frame_index][1]);
00038 vtol_vertex_2d_sptr temp = new vtol_vertex_2d(p.x(), p.y());
00039 cog = temp;
00040 }
00041
00042
00043 bool strk_art_model_display_process::input_tracked_models(vcl_ifstream & str)
00044 {
00045 int n_frames = 0;
00046 vtol_face_2d_sptr stem;
00047 vtol_face_2d_sptr long_arm;
00048 vtol_face_2d_sptr short_arm;
00049 int stem_n_verts =0, long_arm_n_verts = 0, short_arm_n_verts =0;
00050
00051 vcl_string sf, nf, nv;
00052 str >> sf;
00053 if (sf!="START_FRAME:")
00054 return false;
00055 str >> start_frame_;
00056 str >> nf;
00057 if (nf!="N_FRAMES:")
00058 return false;
00059 str >> n_frames;
00060
00061 vcl_string st, cg, la, sa, x, y;
00062
00063 str >> st;
00064 if (st!="STEM:")
00065 return false;
00066 str >> nv;
00067 if (nv!="N_VERTS:")
00068 return false;
00069 str >> stem_n_verts;
00070 str >> cg;
00071 if (cg!="COG:")
00072 return false;
00073 vnl_matrix<double> cog_stem(n_frames, 2);
00074 vnl_matrix<double> stem_X(n_frames, stem_n_verts);
00075 vnl_matrix<double> stem_Y(n_frames, stem_n_verts);
00076 str >> cog_stem;
00077 str >> x;
00078 if (x!="X:")
00079 return false;
00080 str >> stem_X;
00081 str >> y;
00082 if (y!="Y:")
00083 return false;
00084 str >> stem_Y;
00085
00086 str >> la;
00087 if (la!="LONG_ARM:")
00088 return false;
00089 str >> nv;
00090 if (nv!="N_VERTS:")
00091 return false;
00092 str >> long_arm_n_verts;
00093 str >> cg;
00094 if (cg!="COG:")
00095 return false;
00096 vnl_matrix<double> cog_long_arm(n_frames, 2);
00097 vnl_matrix<double> long_arm_X(n_frames, long_arm_n_verts);
00098 vnl_matrix<double> long_arm_Y(n_frames, long_arm_n_verts);
00099 str >> cog_long_arm;
00100 str >> x;
00101 if (x!="X:")
00102 return false;
00103 str >> long_arm_X;
00104 str >> y;
00105 if (y!="Y:")
00106 return false;
00107 str >> long_arm_Y;
00108
00109
00110 str >> sa;
00111 if (sa!="SHORT_ARM:")
00112 return false;
00113 str >> nv;
00114 if (nv!="N_VERTS:")
00115 return false;
00116 str >> short_arm_n_verts;
00117 str >> cg;
00118 if (cg!="COG:")
00119 return false;
00120 vnl_matrix<double> cog_short_arm(n_frames, 2);
00121 vnl_matrix<double> short_arm_X(n_frames, short_arm_n_verts);
00122 vnl_matrix<double> short_arm_Y(n_frames, short_arm_n_verts);
00123 str >> cog_short_arm;
00124 str >> x;
00125 if (x!="X:")
00126 return false;
00127 str >> short_arm_X;
00128 str >> y;
00129 if (y!="Y:")
00130 return false;
00131 str >> short_arm_Y;
00132
00133
00134
00135
00136 for (int i = 0; i<n_frames; i++)
00137 {
00138 vtol_vertex_2d_sptr c;
00139 construct_face_and_cog(stem_n_verts, i,
00140 cog_stem, stem_X, stem_Y,
00141 stem, c);
00142
00143 construct_face_and_cog(long_arm_n_verts, i,
00144 cog_long_arm, long_arm_X, long_arm_Y,
00145 long_arm, c);
00146
00147 construct_face_and_cog(short_arm_n_verts, i,
00148 cog_short_arm, short_arm_X, short_arm_Y,
00149 short_arm, c);
00150 vcl_vector<vtol_face_2d_sptr> mod;
00151 mod.push_back(stem); mod.push_back(long_arm); mod.push_back(short_arm);
00152 tracked_models_.push_back(mod);
00153 }
00154 return true;
00155 }
00156
00157 bool strk_art_model_display_process::execute()
00158 {
00159 if (failure_)
00160 return false;
00161 if (this->get_N_input_images()!=1)
00162 {
00163 vcl_cout << "In strk_art_model_display_process::execute() -"
00164 << " not exactly one input image\n";
00165 failure_ = true;
00166 return false;
00167 }
00168 output_topo_objs_.clear();
00169 input_images_.clear();
00170 static int n_models = 0;
00171 if (first_frame_)
00172 {
00173 first_frame_ = false;
00174
00175 vcl_ifstream str(track_file_.c_str());
00176 if (!str)
00177 {
00178 vcl_cout << "In strk_art_model_display_process::execute() -"
00179 << " could not open file " << track_file_ << '\n';
00180 failure_ = true;
00181 return false;
00182 }
00183
00184 this->input_tracked_models(str);
00185 n_models = tracked_models_.size();
00186 if (!n_models)
00187 {
00188 first_frame_ = false;
00189 const char* file = track_file_.c_str();
00190 vcl_ifstream str(file);
00191 if (!str)
00192 {
00193 vcl_cout << "In strk_art_model_display_process::execute() -"
00194 << " could not open file " << track_file_ << '\n';
00195 failure_ = true;
00196 return false;
00197 }
00198
00199 this->input_tracked_models(str);
00200 n_models = tracked_models_.size();
00201 if (!n_models)
00202 {
00203 vcl_cout << "In strk_art_model_display_process::execute() -"
00204 << " no models found in track file\n";
00205 failure_ = true;
00206 }
00207 }
00208 }
00209 int frame_index = this->frame_index();
00210 int offset = frame_index-start_frame_;
00211 if (offset<0||offset>=n_models)
00212 return false;
00213 vcl_vector<vtol_face_2d_sptr> tracked_faces = tracked_models_[offset];
00214 for (vcl_vector<vtol_face_2d_sptr>::iterator fit = tracked_faces.begin();
00215 fit != tracked_faces.end(); fit++)
00216 {
00217 vcl_vector<vtol_edge_sptr> edges;
00218 (*fit)->edges(edges);
00219 for (vcl_vector<vtol_edge_sptr>::iterator eit = edges.begin();
00220 eit != edges.end(); eit++)
00221 {
00222 vtol_topology_object_sptr to = (*eit)->cast_to_edge();
00223 output_topo_objs_.push_back(to);
00224 }
00225 }
00226 return true;
00227 }
00228
00229 bool strk_art_model_display_process::set_input_file(vcl_string const& file_name)
00230 {
00231 track_file_ = file_name;
00232 if (track_file_=="")
00233 return false;
00234 vcl_ifstream str(track_file_.c_str());
00235 if (!str)
00236 {
00237 vcl_cout << "In strk_art_model_display_process::set_input_file() -"
00238 << " could not open file " << track_file_ << '\n';
00239 return false;
00240 }
00241 str.close();
00242 return true;
00243 }