gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Camera.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 //
6 // Contributed by Gilles Marckmann <gilles.marckmann@ec-nantes.fr>
7 
8 #include <string>
9 #include <iostream>
10 #include <stdio.h>
11 #include <cmath>
12 #include <algorithm>
13 #include "Camera.h"
14 #include "GmshGlobal.h"
15 #include "GmshConfig.h"
16 #include "GmshMessage.h"
17 #include "Trackball.h"
18 #include "Context.h"
19 #include "drawContext.h"
20 
22  : on(false), stereoEnable(false), Lc(1.), glFnear(0.0001), glFfar(10000)
23 {
24 }
25 
27 
29 {
30  if(CTX::instance()->fileread) {
31  on = true;
32  }
36  alongZ();
37  this->lookAtCg();
38  eyesep = distance * eye_sep_ratio / 100.;
40  glFnear = .0001 * distance;
41  glFfar = 10000 * distance;
42  this->update();
43 }
44 
46 {
47  front.set(-1., 0., 0.);
48  up.set(0., 0., 1);
50  this->update();
51 }
52 
54 {
55  front.set(0., -1., 0.);
56  up.set(1., 0., 0);
58  this->update();
59 }
60 
62 {
63  front.set(0., 0., -1.);
64  up.set(0., 1., 0);
66  this->update();
67 }
68 
70 {
71  if(up.x > 0.) {
72  if(up.x != 1.) {
73  up.set(1., 0., 0.);
74  }
75  else {
76  up.set(-1., 0., 0.);
77  right = -right;
78  }
79  }
80  else {
81  if(up.x != -1.) {
82  up.set(-1., 0., 0.);
83  }
84  else {
85  up.set(1., 0., 0.);
86  right = -right;
87  }
88  }
89  front.x = up.y * right.z - up.z * right.y;
90  front.y = up.z * right.x - up.x * right.z;
91  front.z = up.x * right.y - up.y * right.x;
93  this->update();
94 }
95 
97 {
98  if(up.y > 0.) {
99  if(up.y != 1.) {
100  up.set(0., 1., 0.);
101  }
102  else {
103  up.set(0., -1., 0.);
104  right = -right;
105  }
106  }
107  else {
108  if(up.y != -1.) {
109  up.set(0., -1., 0.);
110  }
111  else {
112  up.set(0., 1., 0.);
113  right = -right;
114  }
115  }
116  front.x = up.y * right.z - up.z * right.y;
117  front.y = up.z * right.x - up.x * right.z;
118  front.z = up.x * right.y - up.y * right.x;
120  this->update();
121 }
122 
124 {
125  if(up.z > 0.) {
126  if(up.z != 1.) {
127  up.set(0., 0., 1.);
128  }
129  else {
130  up.set(0., 0., -1.);
131  right = -right;
132  }
133  }
134  else {
135  if(up.z != -1.) {
136  up.set(0., 0., -1.);
137  }
138  else {
139  up.set(0., 0., 1.);
140  right = -right;
141  }
142  }
143  front.x = up.y * right.z - up.z * right.y;
144  front.y = up.z * right.x - up.x * right.z;
145  front.z = up.x * right.y - up.y * right.x;
147  this->update();
148 }
149 
151 {
152  up = -1. * right;
153  update();
154 }
155 
157 {
158  up = right;
159  update();
160 }
161 
163 {
164  target.x = CTX::instance()->cg[0];
165  target.y = CTX::instance()->cg[1];
166  target.z = CTX::instance()->cg[2];
167  double W = CTX::instance()->max[0] - CTX::instance()->min[0];
168  double H = CTX::instance()->max[1] - CTX::instance()->min[1];
169  double P = CTX::instance()->max[2] - CTX::instance()->min[2];
170  Lc = sqrt(1. * W * W + 1. * H * H + 1. * P * P);
171  distance = .8 * fabs(.5 * Lc * 4. / 3. / tan(aperture * .01745329 / 2.));
173  this->update();
176  eyesep = focallength * eye_sep_ratio / 100.;
177 }
178 
179 void Camera::giveViewportDimension(const int &W, const int &H)
180 {
181  screenwidth = W;
182  screenheight = H;
183  screenratio = (double)W / (double)H;
184  glFleft = -screenratio * wd2;
186  glFtop = wd2;
187  glFbottom = -wd2;
188 }
189 
191 {
192  right.x = front.y * up.z - front.z * up.y;
193  right.y = front.z * up.x - front.x * up.z;
194  right.z = front.x * up.y - front.y * up.x;
195 
196  up.x = right.y * front.z - right.z * front.y;
197  up.y = right.z * front.x - right.x * front.z;
198  up.z = right.x * front.y - right.y * front.x;
199 
201  normalize(up);
202  normalize(right);
203  normalize(front);
208  eyesep = focallength * eye_sep_ratio / 100.;
209  radians = 0.0174532925 * aperture / 2.;
210  wd2 = glFnear * tan(radians);
212 }
213 
215 {
216  std::cout << " ------------ GENERAL PARAMETERS ------------" << std::endl;
217  std::cout << " CTX aperture " << CTX::instance()->camera_aperture
218  << std::endl;
219  std::cout << " CTX eyesep ratio " << CTX::instance()->eye_sep_ratio
220  << std::endl;
221  std::cout << " CTX focallength ratio " << CTX::instance()->focallength_ratio
222  << std::endl;
223  std::cout << " ------------ CAMERA PARAMETERS ------------" << std::endl;
224  std::cout << " position " << position.x << "," << position.y << ","
225  << position.z << std::endl;
226  std::cout << " front " << front.x << "," << front.y << "," << front.z
227  << std::endl;
228  std::cout << " up " << up.x << "," << up.y << "," << up.z << std::endl;
229  std::cout << " right " << right.x << "," << right.y << "," << right.z
230  << std::endl;
231  std::cout << " target " << target.x << "," << target.y << "," << target.z
232  << std::endl;
233  std::cout << " focallength_ratio " << focallength_ratio << std::endl;
234  std::cout << " focallength " << focallength << std::endl;
235  std::cout << " aperture " << aperture << std::endl;
236  std::cout << " eyesep_ratio " << eye_sep_ratio << std::endl;
237  std::cout << " eyesep " << eyesep << std::endl;
238  std::cout << " screenwidth " << screenwidth << std::endl;
239  std::cout << " screenheight " << screenheight << std::endl;
240  std::cout << " distance " << distance << std::endl;
241  std::cout << " ref_distance " << ref_distance << std::endl;
242  std::cout << " button_left_down " << button_left_down << std::endl;
243  std::cout << " button_middle_down " << button_middle_down << std::endl;
244  std::cout << " button_right_down " << button_right_down << std::endl;
245  std::cout << " stereoEnable " << stereoEnable << std::endl;
246  std::cout << " Lc " << Lc << std::endl;
247  std::cout << " eye_sep_ratio " << eye_sep_ratio << std::endl;
248  std::cout << " closeness " << closeness << std::endl;
249  std::cout << " glFnear " << glFnear << std::endl;
250  std::cout << " glFfar " << glFfar << std::endl;
251  std::cout << " radians " << radians << std::endl;
252  std::cout << " wd2 " << wd2 << std::endl;
253 }
254 
255 void Camera::moveRight(double &theta)
256 {
257  this->update();
258  position = position - distance * tan(theta) * right;
260  this->update();
261 }
262 
263 void Camera::moveUp(double &theta)
264 {
265  this->update();
266  position = position + distance * tan(theta) * up;
268  this->update();
269 }
270 
271 void Camera::zoom(double &factor)
272 {
273  distance = fabs(1. / factor * ref_distance);
275 }
276 
277 void Camera::rotate(double *q)
278 {
279  this->update();
280  // rotation projection in global coordinates
281  Quaternion omega;
282  omega.x = q[0] * right.x + q[1] * up.x - q[2] * front.x;
283  omega.y = q[0] * right.y + q[1] * up.y - q[2] * front.y;
284  omega.z = q[0] * right.z + q[1] * up.z - q[2] * front.z;
285  omega.w = q[3];
286  normalize(omega);
287  Quaternion conj = conjugate(omega);
288  front = omega * front * conj;
289  up = omega * up * conj;
290  right = omega * right * conj;
291  normalize(front);
292  normalize(up);
293  normalize(right);
294  // actualize camera position
296  this->update();
297 }
298 
299 void Camera::move_and_look(double _movfront, double _movright, double _movup,
300  double _thetafront, double _thetaright,
301  double _thetaup, double _azimut, double _elevation)
302 {
303  position = position + _movfront * front + _movright * right + _movup * up;
304  Quaternion omega_up;
305  omega_up.x = sin(_thetaup) * up.x;
306  omega_up.y = sin(_thetaup) * up.y;
307  omega_up.z = sin(_thetaup) * up.z;
308  omega_up.w = cos(_thetaup);
309  normalize(omega_up);
310  Quaternion omega_right;
311  omega_right.x = sin(_thetaright) * right.x;
312  omega_right.y = sin(_thetaright) * right.y;
313  omega_right.z = sin(_thetaright) * right.z;
314  omega_right.w = cos(_thetaright);
315  normalize(omega_right);
316  Quaternion omega_front;
317  omega_front.x = sin(_thetafront) * front.x;
318  omega_front.y = sin(_thetafront) * front.y;
319  omega_front.z = sin(_thetafront) * front.z;
320  omega_front.w = cos(_thetafront);
321  normalize(omega_front);
322  Quaternion omega;
323  omega = omega_up * omega_right * omega_front;
324  normalize(omega);
325  Quaternion conj = conjugate(omega);
326  front = omega * front * conj;
327  up = omega * up * conj;
328  right = omega * right * conj;
329  normalize(front);
330  normalize(up);
331  normalize(right);
332  this->update();
333  // in azimuthal coordinates
334  XYZ look, newlook;
335  _elevation = std::min(_elevation, 1.57);
336  _elevation = std::max(_elevation, -1.57);
337  look = cos(_azimut) * front - sin(_azimut) * right;
338  newlook = cos(_elevation) * look + sin(_elevation) * up;
339  target = position + distance * newlook;
340 }
341 
343 // QUATERNION and XYZ functions
345 
346 double length(Quaternion &q)
347 {
348  return sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
349 }
350 
351 double length(XYZ &p) { return sqrt(p.x * p.x + p.y * p.y + p.z * p.z); }
352 
354 {
355  double sina = sin(acos(q.w));
356  double l;
357  if(sina != 0.) {
358  l = (q.x * q.x + q.y * q.y + q.z * q.z) / (sina * sina);
359  l = sqrt(l);
360  }
361  else {
362  l = 0.;
363  }
364  if(l != 0.) {
365  q.x /= l;
366  q.y /= l;
367  q.z /= l;
368  }
369 }
371 {
372  double L = length(q);
373  q.x /= L;
374  q.y /= L;
375  q.z /= L;
376  q.w /= L;
377 }
378 
379 void normalize(XYZ &p)
380 {
381  double L = length(p);
382  p.x /= L;
383  p.y /= L;
384  p.z /= L;
385 }
386 
387 XYZ::XYZ(const Quaternion &R) : x(R.x), y(R.y), z(R.z) {}
388 
389 XYZ::XYZ(double _x, double _y, double _z) : x(_x), y(_y), z(_z) {}
390 
391 void XYZ::set(const double &_x, const double &_y, const double &_z)
392 {
393  x = _x;
394  y = _y;
395  z = _z;
396 }
397 
398 void rotate(const Quaternion &omega, XYZ axe)
399 {
400  XYZ new_axe;
401  Quaternion qaxe, new_qaxe;
402  qaxe.x = axe.x;
403  qaxe.y = axe.y;
404  qaxe.z = axe.z;
405  qaxe.w = 0.;
406  new_qaxe = mult(mult(omega, qaxe), conjugate(omega));
407  axe.x = new_qaxe.x;
408  axe.y = new_qaxe.y;
409  axe.z = new_qaxe.z;
410 }
411 
412 XYZ operator*(const double &a, const XYZ &T)
413 {
414  XYZ res(T);
415  res.x *= a;
416  res.y *= a;
417  res.z *= a;
418  return res;
419 }
420 
421 XYZ operator+(const XYZ &L, const XYZ &R)
422 {
423  XYZ res(L);
424  res.x += R.x;
425  res.y += R.y;
426  res.z += R.z;
427  return res;
428 }
429 XYZ operator-(const XYZ &L, const XYZ &R)
430 {
431  XYZ res(L);
432  res.x -= R.x;
433  res.y -= R.y;
434  res.z -= R.z;
435  return res;
436 }
437 XYZ operator-(const XYZ &R)
438 {
439  XYZ res;
440  res.x = -R.x;
441  res.y = -R.y;
442  res.z = -R.z;
443  return res;
444 }
445 
446 Quaternion::Quaternion(const XYZ &R) : x(R.x), y(R.y), z(R.z), w(0.) {}
447 
448 Quaternion::Quaternion(const XYZ &R, const double &A)
449 {
450  x = R.x * sin(A);
451  y = R.y * sin(A);
452  z = R.z * sin(A);
453  w = cos(A);
454 }
455 Quaternion::Quaternion() : x(0.), y(0.), z(0.), w(1.) {}
456 
458 
460 {
461  Quaternion C;
462  C.x = A.w * B.x + A.x * B.w + A.y * B.z - A.z * B.y;
463  C.y = A.w * B.y - A.x * B.z + A.y * B.w + A.z * B.x;
464  C.z = A.w * B.z + A.x * B.y - A.y * B.x + A.z * B.w;
465  C.w = A.w * B.w - A.x * B.x - A.y * B.y - A.z * B.z;
466  return C;
467 }
468 
470 {
471  return mult(A, B);
472 }
473 
475 {
476  quat.x = -quat.x;
477  quat.y = -quat.y;
478  quat.z = -quat.z;
479  return quat;
480 }
Camera::rotate
void rotate(double *q)
Definition: Camera.cpp:277
Quaternion::Quaternion
Quaternion()
Definition: Camera.cpp:455
Camera::screenratio
double screenratio
Definition: Camera.h:61
Camera::radians
double radians
Definition: Camera.h:64
operator*
XYZ operator*(const double &a, const XYZ &T)
Definition: Camera.cpp:412
Camera::up
XYZ up
Definition: Camera.h:53
Camera::ndfl
double ndfl
Definition: Camera.h:64
Camera::target
XYZ target
Definition: Camera.h:55
Camera::~Camera
~Camera()
Definition: Camera.cpp:26
Camera::ref_distance
double ref_distance
Definition: Camera.h:61
Camera::screenwidth
int screenwidth
Definition: Camera.h:60
Camera::glFleft
double glFleft
Definition: Camera.h:65
Camera::glFright
double glFright
Definition: Camera.h:65
Quaternion
Definition: Camera.h:31
Camera::update
void update()
Definition: Camera.cpp:190
Camera::tiltHeadRight
void tiltHeadRight()
Definition: Camera.cpp:156
Camera::glFtop
double glFtop
Definition: Camera.h:65
Camera::alongX
void alongX()
Definition: Camera.cpp:45
Camera::eyesep
double eyesep
Definition: Camera.h:59
Camera::right
XYZ right
Definition: Camera.h:54
CTX::eye_sep_ratio
double eye_sep_ratio
Definition: Context.h:238
Camera::affiche
void affiche()
Definition: Camera.cpp:214
XYZ::set
void set(const double &_x, const double &_y, const double &_z)
Definition: Camera.cpp:391
Quaternion::z
double z
Definition: Camera.h:33
Camera::position
XYZ position
Definition: Camera.h:51
Camera::lookAtCg
void lookAtCg()
Definition: Camera.cpp:162
GmshMessage.h
Camera::screenheight
int screenheight
Definition: Camera.h:60
Camera::glFbottom
double glFbottom
Definition: Camera.h:65
Camera::upX
void upX()
Definition: Camera.cpp:69
conjugate
Quaternion conjugate(Quaternion quat)
Definition: Camera.cpp:474
Camera::focallength
double focallength
Definition: Camera.h:56
Camera::focallength_ratio
double focallength_ratio
Definition: Camera.h:57
Camera::button_middle_down
bool button_middle_down
Definition: Camera.h:62
Camera::Camera
Camera()
Definition: Camera.cpp:21
CTX::instance
static CTX * instance()
Definition: Context.cpp:122
Camera::distance
double distance
Definition: Camera.h:61
Camera::alongZ
void alongZ()
Definition: Camera.cpp:61
Camera::on
bool on
Definition: Camera.h:50
Camera::glFfar
double glFfar
Definition: Camera.h:64
Camera::zoom
void zoom(double &factor)
Definition: Camera.cpp:271
rotate
void rotate(const Quaternion &omega, XYZ axe)
Definition: Camera.cpp:398
CTX::camera_aperture
double camera_aperture
Definition: Context.h:238
CTX::min
double min[3]
Definition: Context.h:229
CTX::max
double max[3]
Definition: Context.h:229
XYZ::XYZ
XYZ()
Definition: Camera.h:15
Camera::Lc
double Lc
Definition: Camera.h:64
CTX::focallength_ratio
double focallength_ratio
Definition: Context.h:238
CTX::cg
double cg[3]
Definition: Context.h:231
Camera::move_and_look
void move_and_look(double _movfront, double _movright, double _movup, double _rotfront, double _rotright, double _rotup, double _azimut, double _elevation)
Definition: Camera.cpp:299
Trackball.h
Camera::alongY
void alongY()
Definition: Camera.cpp:53
mult
Quaternion mult(const Quaternion &A, const Quaternion &B)
Definition: Camera.cpp:459
Camera::moveUp
void moveUp(double &theta)
Definition: Camera.cpp:263
Camera::upZ
void upZ()
Definition: Camera.cpp:123
Quaternion::y
double y
Definition: Camera.h:33
GmshGlobal.h
length
double length(Quaternion &q)
Definition: Camera.cpp:346
Camera::stereoEnable
bool stereoEnable
Definition: Camera.h:63
Camera.h
XYZ::x
double x
Definition: Camera.h:18
Camera::upY
void upY()
Definition: Camera.cpp:96
Camera::glFnear
double glFnear
Definition: Camera.h:64
Camera::button_left_down
bool button_left_down
Definition: Camera.h:62
Quaternion::~Quaternion
~Quaternion()
Definition: Camera.cpp:457
Context.h
Camera::tiltHeadLeft
void tiltHeadLeft()
Definition: Camera.cpp:150
z
const double z
Definition: GaussQuadratureQuad.cpp:56
Camera::eye_sep_ratio
double eye_sep_ratio
Definition: Camera.h:64
Camera::moveRight
void moveRight(double &theta)
Definition: Camera.cpp:255
XYZ::y
double y
Definition: Camera.h:18
XYZ::z
double z
Definition: Camera.h:18
Camera::giveViewportDimension
void giveViewportDimension(const int &W, const int &H)
Definition: Camera.cpp:179
XYZ
Definition: Camera.h:13
normalize
void normalize(Quaternion &q)
Definition: Camera.cpp:370
Quaternion::w
double w
Definition: Camera.h:33
operator-
XYZ operator-(const XYZ &L, const XYZ &R)
Definition: Camera.cpp:429
Camera::front
XYZ front
Definition: Camera.h:52
Camera::init
void init()
Definition: Camera.cpp:28
Camera::button_right_down
bool button_right_down
Definition: Camera.h:62
Camera::closeness
double closeness
Definition: Camera.h:64
Camera::aperture
double aperture
Definition: Camera.h:58
normalize_axe
void normalize_axe(Quaternion &q)
Definition: Camera.cpp:353
Quaternion::x
double x
Definition: Camera.h:33
Camera::wd2
double wd2
Definition: Camera.h:64
operator+
XYZ operator+(const XYZ &L, const XYZ &R)
Definition: Camera.cpp:421
drawContext.h