SPH
ArcBall.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "gui/objects/Point.h"
9 #include "math/AffineMatrix.h"
10 #include "math/Quat.h"
11 
13 
21 class ArcBall {
22 private:
24  Vector start{ NAN };
25 
27  Pixel size;
28 
29 public:
30  ArcBall() {
31  size = Pixel(0, 0);
32  }
33 
34  explicit ArcBall(const Pixel size)
35  : size(size) {}
36 
37  void resize(const Pixel newSize) {
38  size = newSize;
39  }
40 
42  void click(const Pixel point) {
43  start = this->mapToSphere(point);
44  }
45 
51  AffineMatrix drag(const Pixel point, const Vector& pivot) {
52  SPH_ASSERT(isReal(start));
53  const Vector end = this->mapToSphere(point);
54  const Vector perp = cross(start, end);
55  if (getSqrLength(perp) > EPS) {
56  Quat q;
57  q[0] = perp[0];
58  q[1] = perp[1];
59  q[2] = perp[2];
60  q[3] = dot(start, end);
61 
63  result.translate(pivot);
64  result = result * q.convert();
65  result.translate(-pivot);
66  return result;
67  } else {
68  return AffineMatrix::identity();
69  }
70  }
71 
72 private:
73  Vector mapToSphere(const Pixel point) {
74  // rescale to <-1, 1> and invert y
75  SPH_ASSERT(size.x > 0 && size.y > 0);
76  const Vector p(2.f * float(point.x) / size.x - 1.f, 1.f - 2.f * float(point.y) / size.y, 0._f);
77 
78  const Float lengthSqr = getSqrLength(p);
79  if (lengthSqr > 1.f) {
80  const Float length = sqrt(lengthSqr);
81  return p / length;
82  } else {
83  return Vector(p[X], p[Y], sqrt(1.f - lengthSqr));
84  }
85  }
86 };
87 
Three-dimensional affine matrix.
INLINE bool isReal(const AntisymmetricTensor &t)
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr Float EPS
Definition: MathUtils.h:30
INLINE T sqrt(const T f)
Return a squared root of a value.
Definition: MathUtils.h:78
#define NAMESPACE_SPH_END
Definition: Object.h:12
Quaternion algebra.
INLINE Float getSqrLength(const Vector &v)
Definition: Vector.h:574
INLINE BasicVector< float > cross(const BasicVector< float > &v1, const BasicVector< float > &v2)
Cross product between two vectors.
Definition: Vector.h:559
BasicVector< Float > Vector
Definition: Vector.h:539
INLINE float dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Definition: Vector.h:548
@ Y
Definition: Vector.h:23
@ X
Definition: Vector.h:22
static AffineMatrix identity()
Definition: AffineMatrix.h:132
INLINE AffineMatrix & translate(const Vector &t)
Definition: AffineMatrix.h:58
Helper object providing rotation matrix based on mouse drag.
Definition: ArcBall.h:21
ArcBall(const Pixel size)
Definition: ArcBall.h:34
void resize(const Pixel newSize)
Definition: ArcBall.h:37
void click(const Pixel point)
Called on mouse click, starting the rotation.
Definition: ArcBall.h:42
AffineMatrix drag(const Pixel point, const Vector &pivot)
Called when mouse moves, rotating the object.
Definition: ArcBall.h:51
ArcBall()
Definition: ArcBall.h:30
Quaternion representing an axis of rotation and a (half of) rotation angle.
Definition: Quat.h:16
AffineMatrix convert() const
Converts the quaternion into a rotation matrix.
Definition: Quat.h:73
Simple 2D vector with integer coordinates. Provides conversion from and to wxPoint.
Definition: Point.h:101