Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CameraViewController.cpp
1 #ifdef WIN32
2 # pragma warning (disable: 4005) // for VS8 + WX
3 #endif
4 
5 #include "CameraViewController.hh"
6 #include "biasgl.h"
7 #include <iostream>
8 
9 using namespace std;
10 using namespace BIAS;
11 
12 
13 // JW
14 CameraViewController::~CameraViewController(){
15  delete p_externals; p_externals=NULL;
16  delete p_internals; p_internals=NULL;
17 }
18 
19 
20 CameraViewController::CameraViewController()
21 : p_externals(NULL), p_internals(NULL)
22 {
23  InitMembers();
24 }
25 
26 
28  //mode_ = ExamineTrackballmode ; //InspectionMoveCAbsolute
29 
30  if (p_externals==NULL){
32  } else
34 
35  if (p_internals==NULL)
37  else
39 
40  size.Set(100,100);
41  startpos.SetZero();
44 }
45 
46 
50 }
51 
54 }
55 
58 }
59 
60 
61 std::ostream &
62 CameraViewController::Print(std::ostream & os ){
63  os<<"CameraViewController : "<<endl;
64  GetInternals().Print(os);
65  GetExternals().Print(os);
66  return os;
67 }
68 
70  startpos=pos;
71  BIASASSERT(p_externals!=NULL);
72  BIASASSERT(p_internals!=NULL);
75 }
76 
77 
79 {
80  BIASASSERT(p_externals!=NULL);
81 
82  Vector2<double> middle = size /2.0;
83  //cout<<"size: "<<size<<" middle: "<<middle<<endl;
84 
85  // tangential directions mapping to motion delta
86  Vector3<double> v0(1,0,0);
87  Vector3<double> v1(0,1,0);
88  Vector3<double> offset(0,0,0);
89 
90  //cout<<deltaN<<endl;
91 
92  Vector3<double> displaceCart = GetExternals().C - GetExternals().viewAt;
93  Vector3<double> displacePolar = displaceCart.CartesianToPolar();
94  //cout<<displaceCart<<" is polar "<<displacePolar<<endl;
95 
96  v1 = GetExternals().up;
97  v1.Normalize();
98  v0 = v1.CrossProduct(displaceCart);
99  v0.Normalize();
100  //cout<<v0<<" v1 "<<v1<<endl;
101 
102 
103  //offset = 0.01 * Vector3<double>(deltaN[0], deltaN[1], 0.0);
104  const double scale=0.1;
105  offset += scale * deltaN[0] * v0;
106  offset += scale * deltaN[1] * v1;
107 
108  GetExternals().C += offset;
109 }
110 
111 
113 {
114  BIASASSERT(p_externals!=NULL);
115 
116  Vector3<double> v0(1,0,0);
118  Vector3<double> offset(0,0,0);
119 
120  Vector3<double> displaceCart = GetExternals().C - GetExternals().viewAt;
121 
122  v0 = v1.CrossProduct(displaceCart);
123 
124  v1.Normalize();
125  v0.Normalize();
126 
127  //offset = 0.01 * Vector3<double>(deltaN[0], deltaN[1], 0.0);
128  const double scale = displaceCart.Length()*2.0;
129  offset += scale * deltaN[0] * v0;
130  offset += scale * deltaN[1] * v1;
131 
132  GetExternals().C += offset;
133  GetExternals().viewAt += offset;
134 }
135 
136 
137 void CameraViewController::MoveToCenter(const double & fraction)
138 {
139  if (abs(fraction)>10.0)
140  {
141  BIASERR("fraction should be a normalized value, usually between -1.0 and +1.0. skipping motion");
142  BIASBREAK;
143  return;
144  }
145  BIASASSERT(p_externals!=NULL);
147  direction *= fraction;
148  p_externals->C += direction;
149 }
150 
152 {
153  const Vector2<double> delta = pos - this->startpos;
154  const double fraction = delta.GetY() / size.GetY();
155  MoveToCenter(fraction);
156  this->startpos = pos;
157 }
158 
159 
161 {
162  Vector2<double> delta = pos - this->startpos;
163  delta.DivideIP(size);
164  delta *= 20.0;
165  MoveOrbit(delta);
166 
167  this->startpos = pos; // new center
168 }
169 
171 {
172  Vector2<double> delta = pos - this->startpos;
173  delta.DivideIP(size);
174  MoveCoplanar(delta);
175  this->startpos = pos; // new center
176 }
177 
178 
179 
180 // helper
181 //#define square(arg) ((arg) * (arg))
182 
184  // Trackball rotation
185  BIASERR("incorrectly implemented");
186  // TODO
187 
188  // offset from start of dragging
189  Vector2<double> middle=size/2.;
190  //cout<<"DBG size="<<size<<" middle="<<middle<<endl;
191 
192  /*
193  // current pos. on unit 1 sphere, determine z from x,y
194  Vector2<double> deltaPosAct = ((pos -middle) / size);
195  Vector3<double> vAct(deltaPosAct[0], -deltaPosAct[1], 0 ); // projection to XY plane
196  vAct[2] = sqrt(1 - square(vAct[0]) - square(vAct[1]) ); // minus y for lower left origin
197  cout<<" vAct="<<vAct<<" length: "<<vAct.Length()<<endl;
198 
199  // startpos on unit 1 sphere
200  Vector2<double> deltaPosStart = (startpos -middle) / size;
201  Vector3<double> vStart(deltaPosStart[0], -deltaPosStart[1], 0);
202  vStart[2] = sqrt(1 - square(vStart[0]) - square(vStart[1]) );
203  cout<<" vStart="<<vStart<<" length: "<<vStart.Length()<<endl;
204  */
205 
206 
207  // normalize length
208 
209 
210  // offset from viewcenter in polar form:
211  //static BIAS::Vector3<double> PolarCoords = (startC -viewAt_).CoordEuclideanToSphere();
212 
213  /*
214  float dtheta = (float)(mouseX - x)*TWOPI/(float)screenWidth;
215  float dphi = (float)(y - mouseY)*TWOPI/(float)screenHeight;
216  if (p == &Eye.from) {
217  p->theta -= dtheta;
218  p->phi -= dphi;
219  } else {
220  p->theta += dtheta;
221  p->phi += dphi;
222  }
223  if (p->theta < -TWOPI) p->theta += TWOPI;
224  else if (p->theta > TWOPI) p->theta -= TWOPI;
225  if (p->phi > M_PI) p->phi = M_PI;
226  else if (p->phi < 0.) p->phi = 0.;
227  */
228 
229  //// set new "rotated" eye center
230  //C_ = viewAt_ +p->CoordSphereToEuclidean();
231  //// adapt up vector
232  //cout<<"DBG C_="<<C_<<" "<<C_.NormL2()<<endl;
233 
234 
235  // polarToCartesian(Eye.from, eyeCoords);
236  //gluLookAt(eyeCoords[0], eyeCoords[1], eyeCoords[2],
237  // Eye.target[0], Eye.target[1], Eye.target[2],
238  // -cos(Eye.from.phi)*sin(Eye.from.theta),
239  // -cos(Eye.from.phi)*cos(Eye.from.theta),
240  // sin(Eye.from.phi));
241 }
242 
243 
244 //void CameraViewController::OnMotion( wxMouseEvent &event ){
245 //
246 // //CALLINFO;
247 // if (event.LeftIsDown()){
248 // wxPoint pt= event.GetPosition();
249 // Vector2<double> diff= Vector2<double>(pt.x, pt.y) -startpos;
250 // //cout<<"diff: "<<diff<<endl;
251 // Motion(diff[0], diff[1]);
252 // }
253 //}
254 
255 
256 //void CameraViewController::OnLeftDown( wxMouseEvent &event ){
257 // //CALLINFO;
258 // wxPoint pos= event.GetPosition();
259 // this->startpos.Set(pos.x, pos.y);
260 // this->startC=C_;
261 //}
262 
void MotionTranslationCoplanar(const BIAS::Vector2< double > &pos)
void MoveOrbit(const BIAS::Vector2< double > &deltaN)
BIAS::CameraViewExternals * p_externals
double Length() const
returns the Euclidean Length of the Vector
Definition: Vector3.hh:193
std::ostream & Print(std::ostream &os=std::cout)
map update of one axis (mouse wheel, delta, key...) maps delta (e.g.
BIAS::Vector2< double > startpos
BIAS::CameraViewInternals * p_internals
std::ostream & Print(std::ostream &os=std::cout)
BIAS::Vector3< double > up
virtual void InitMembers()
any constructor should call this function to initialize all data members (e.g.
void MoveToCenter(const double &fraction)
void Set(const T &scalar)
set all elements to a scalar value
Definition: Vector2.hh:190
void CrossProduct(const Vector3< T > &argvec, Vector3< T > &destvec) const
cross product of two vectors destvec = this x argvec
Definition: Vector3.hh:594
void SetStartPos(const BIAS::Vector2< double > &pos)
toplevel entry for all motion controllers
CameraViewExternals GetExternals() const
CameraViewInternals GetInternals() const
void DivideIP(const T &scalar)
Division (in place) of an scalar.
Definition: Vector2.hh:549
T & GetY()
Definition: Vector2.hh:292
void MotionTranslationForward(const BIAS::Vector2< double > &pos)
describes internal params of an (OpenGL) camera view
BIAS::Vector3< double > viewAt
BIAS::Vector3< double > C
void MotionTranslation(const BIAS::Vector2< double > &pos)
simple translation
std::ostream & Print(std::ostream &os=std::cout) const
BIAS::Vector2< double > size
void MotionTrackballRotation(const BIAS::Vector2< double > &pos)
void MoveCoplanar(const BIAS::Vector2< double > &deltaN)
BIAS::Vector3< T > CartesianToPolar() const
Definition: Vector3.hh:422
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
describes external params of a camera view Used e.g.
void SetZero()
Definition: Vector2.hh:196