Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AutoControl.cpp
1 #include <Base/Math/Vector3.hh>
2 #include <Base/Geometry/RMatrixBase.hh>
3 #include <Base/Geometry/Quaternion.hh>
4 #include "AutoControl.hh"
5 
6 #include <Gui/biasgl.h>
7 
8 using namespace BIAS;
9 using namespace BIAS;
10 using namespace std;
11 
13 {
14  NumberOfSteps_ = 10;
15  Step_ = 0;
16  Sector_ = 0;
17  InitializedSector_ = -1;
18  Active_ = false;
19 }
20 
22 {
23  if (PoseList_.size() > 1)
24  {
25  InitializedSector_ = Sector_;
26  Vector3<double> fromC = PoseList_[Sector_].C;
27  Vector3<double> fromUp = PoseList_[Sector_].up;
28  Vector3<double> fromPoI = PoseList_[Sector_].PoI;
29 
30  Vector3<double> toC = PoseList_[Sector_ + 1].C;
31  Vector3<double> toUp = PoseList_[Sector_ + 1].up;
32  Vector3<double> toPoI = PoseList_[Sector_ + 1].PoI;
33 
34  // Calculate direction vectors and stepSize
35  VecC_ = toC - fromC;
36  StepSizeC_ = VecC_.Length() / NumberOfSteps_;
37  VecUp_ = toUp - fromUp;
38  StepSizeUp_ = VecUp_.Length() / NumberOfSteps_;
39  VecPoI_ = toPoI - fromPoI;
40  StepSizePoI_ = VecPoI_.Length() / NumberOfSteps_;
41  }
42 }
43 
45 {
46  if (PoseList_.size() <= 1) {
47  BIASERR("Not enough camera poses defined.")
48  return -1;
49  }
50 
51  if (Sector_ + 2 >= PoseList_.size() && Step_ >= NumberOfSteps_){
52  //cout<<"Animation finished"<<endl;
53  return -1;
54  }
55 
56 
57  if (Step_ < NumberOfSteps_-1) { // Is the current sector finished
58  Step_++;
59  } else {
60  // Go to next sector if there is one left.
61  if (Sector_ < PoseList_.size() - 2) {
62  Step_ = 0;
63  Sector_++;
64  } else if (Sector_ + 2 == PoseList_.size() ) {
65  // goto the last pose by calling
66  // Goto(NumberOfSteps, NumPoses-2)
67  //cout << "last pose\n";
68  Step_++;
69  }
70  }
71  // cout << "sector "<<Sector_<<"\tstep "<<Step_<<endl;
72  return GoTo(Step_, Sector_);
73 }
74 
75 
76 int AutoControl::GoTo(unsigned int step, unsigned int sector)
77 {
78  if (PoseList_.size() ==0) return -1;
79  if (step > NumberOfSteps_) {
80  BIASERR("Specified parameter out of range: Step " << step);
81  return -1;
82  }
83  if (sector >= PoseList_.size() - 1) {
84  BIASERR("Specified parameter out of range: Sector " << sector);
85  return -1;
86  }
87  Step_ = step;
88  Sector_ = sector;
89  //cout << "step = "<<step<<"\tsector = "<<sector<<endl;
90  if ((int)Sector_ != InitializedSector_) {
91  //cout << "calling init\n";
92  Init();
93  }
94  Vector3<double> fromC = PoseList_[Sector_].C;
95  Vector3<double> fromUp = PoseList_[Sector_].up;
96  Vector3<double> fromPoI = PoseList_[Sector_].PoI;
97  Vector3<double> delta;
98  //cout << "C: "<<fromC<<"\nUp: "<<fromUp<<"\nPOI: "<<fromPoI<<endl;
99  // Compute new camera Vector3<double> C, up; pose.
100  if (!VecC_.IsZero()) {
101  delta = VecC_;
102  delta.Normalize();
103  delta *= (StepSizeC_ * Step_);
104  fromC += delta;
105  }
106 
107  if (!VecPoI_.IsZero()) {
108  delta = VecPoI_;
109  delta.Normalize();
110  delta *= (StepSizePoI_ * Step_);
111  fromPoI += delta;
112  }
113 
114  if (!VecUp_.IsZero()) {
115  delta = VecUp_;
116  delta.Normalize();
117  delta *= (StepSizeUp_ * Step_);
118  fromUp += delta;
119  }
120  // update camera pose and redraw scene.
121  //cout << "setting extrinsigs to "<<fromC<<", "<<fromPoI<<", "<<fromUp<<endl;
122  int res = controlledObject_->SetExtrinsics(fromC, fromPoI, fromUp);
123  Context_->Redisplay();
124  return res;
125 }
126 
128 {
129  if ((int)(PoseList_.size()) < pose+1) {
130  BIASERR("Specified parameter is out of range!");
131  return -1;
132  }
133 
134  // update camera pose and redraw scene.
135  //cout << "setting extrinsigs to "<<PoseList_[pose].C<<", "<<PoseList_[pose].PoI<<", "<<PoseList_[pose].up<<endl;
136  int res = controlledObject_->SetExtrinsics(PoseList_[pose].C,
137  PoseList_[pose].PoI,
138  PoseList_[pose].up);
139  Context_->Redisplay();
140  return res;
141 }
142 
143 void AutoControl::
145 {
146  CameraPose newPose;
147  newPose.C = C;
148  newPose.PoI = PoI;
149  newPose.up = up;
150  PoseList_.push_back(newPose);
151 
152 // cout<<"--------- Added Pose:"<<endl;
153 // cout<<"C: "<<C<<endl;
154 // cout<<"PoI: "<<PoI<<endl;
155 // cout<<"up: "<<up<<endl;
156 
157  if (PoseList_.size() == 2)
158  {
159  Init();
160  }
161 }
162 
163 void AutoControl::
165 {
166  Vector3<double> C,A,up;
167  controlledObject_->GetExtrinsics(C, up);
168  RMatrix R = controlledObject_->GetMyselfAsProjectionParameterBase()->GetR();
169  A[0] = R[0][2];
170  A[1] = R[1][2];
171  A[2] = R[2][2];
172  AddPose(C,A+C,up);
173 }
174 
175 
176 
178 {
179  if (PoseList_.size() <2) return;
180  Step_ = 0;
181  Sector_ = 0;
182  InitializedSector_ = -1;
183  Init();
184 }
185 
187 {
188  if (PoseList_.size() == 0)
189  {
190  BIASERR("No poses in list.")
191  return;
192  }
193 
194  PoseList_.pop_back();
195  Sector_ = 0;
196  Step_ = 0;
197  Init();
198 }
199 
201 {
202  if (PoseList_.size() == 0)
203  {
204  BIASERR("No poses in list.")
205  return;
206  }
207 
208  // get pose and erase from PoseList_
209  vector<CameraPose>::iterator iter=PoseList_.begin();
210  for(int i=0; i<pose; i++) {
211  iter++;
212  }
213  PoseList_.erase(iter);
214  Sector_ = 0;
215  Step_ = 0;
216  Init();
217 }
218 
220 {
221  PoseList_.clear();
222 }
223 
224 bool AutoControl::
226 {
227  Vector3<double> C, up;
228  switch (key)
229  {
230  case KEY_F1:
231  AddPose();
232  break;
233  case KEY_F2:
234  cout<<"NextStep"<<endl;
235  NextStep();
236  break;
237 
238  case KEY_F3:
239  cout<<"Rewind"<<endl;
240  Rewind();
241  break;
242 
243  case KEY_F4:
244  cout<<"Last added pose deleted."<<endl;
245  DeleteLast();
246  break;
247  }
248  return true;
249 }
250 
251 
252 
253 void AutoControl::
255 {
256  Context_ = context;
257 }
258 
259 int AutoControl::SaveToFile(const std::string &file)
260 {
261  ofstream ofs(file.c_str());
262  if (!ofs) {
263  perror(file.c_str());
264  return -1;
265  }
266  Vector3<double> C,A,up;
267  for (unsigned int i=0;i<PoseList_.size(); i++) {
268  C = PoseList_[i].C;
269  A = PoseList_[i].PoI;
270  up = PoseList_[i].up;
271  ofs << C[0]<<" "<<C[1]<<" "<<C[2]<<" ";
272  ofs << A[0]<<" "<<A[1]<<" "<<A[2]<<" ";
273  ofs << up[0]<<" "<<up[1]<<" "<<up[2]<<endl;
274 
275  }
276 
277  ofs.close();
278  return 0;
279 }
280 
281 
282 int AutoControl::LoadFromFile(const std::string &file)
283 {
284  PoseList_.clear();
285  InitializedSector_ = -1;
286  ifstream ifs(file.c_str());
287  if (!ifs) {
288  perror(file.c_str());
289  return -1;
290  }
291  Vector3<double> C,A,up;
292 
293  while (ifs) {
294  ifs >> C[0]>>C[1]>>C[2];
295  ifs >> A[0]>>A[1]>>A[2];
296  ifs >> up[0]>>up[1]>>up[2];
297  if (ifs) AddPose(C,A,up);
298 
299  }
300  // cout <<"Found "<<PoseList_.size()<<" poses"<<endl;
301  Step_ = 0;
302  Sector_ = 0;
303  InitializedSector_ = -1;
304  Active_ = false;
305  Init();
306  return PoseList_.size();
307 }
308 
310 {
311  bool didsomething = false;
312  // cout <<"AutoControl::TimerExpired()"<<endl;
313  if (Active_) {
314  int res = NextStep();
315  didsomething = true;
316  if (res<0) Active_ = false;
317  }
318 
319  return didsomething;
320 }
double Length() const
returns the Euclidean Length of the Vector
Definition: Vector3.hh:193
int NextStep()
Goes to the next calculated pose.
Definition: AutoControl.cpp:44
BIAS::Vector3< double > C
Definition: AutoControl.hh:104
void SetContext(RenderContextBase *context)
void DeleteLast()
Deletes the last added pose and resets the autopilot F4.
void AddPose()
Adds the current pose of ControldObject to the autopilot at the end of PoseList_ F1.
void DeletePose(int pose)
Deletes the defined pose and resets the autopilot F4.
3D rotation matrix
Definition: RMatrix.hh:49
int SaveToFile(const std::string &file)
int LoadFromFile(const std::string &file)
void Init()
Initialisizes the autopilot.
Definition: AutoControl.cpp:21
bool TimerExpired()
this function is called by RenderContextBase when timer is expired Overwrite and implement to do some...
int GoToPose(int pose)
Goes to the defined pose.
int GoTo(unsigned int step, unsigned int sector)
Goes to the defined step and sector.
Definition: AutoControl.cpp:76
void DeleteAll()
Deletes all poses in PoseList_ and resets autopilot.
void Rewind()
Sets the autopilot to start point.
BIAS::Vector3< double > up
Definition: AutoControl.hh:106
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
BIAS::Vector3< double > PoI
Definition: AutoControl.hh:105
bool SpecialKeyPressed(int key)
Handles special key events.
Base for all classes creating interface between GL and &quot;window manager&quot;.