CellModules
rotation.h
Go to the documentation of this file.
1#ifndef MECACELL_ROTATION_H
2#define MECACELL_ROTATION_H
3#include <cmath>
4using namespace std;
5namespace MecaCell {
6
7template <typename V> struct Rotation {
8 V n = V(0, 1, 0);
9 double teta = 0;
10
12
13 Rotation(const V& v, const double& f) : n(v), teta(f) {}
14
15 void randomize() {
16 n.random();
17 n.normalize();
18 teta = 1.0;
19 }
20
21 Rotation operator+(const V& v) const {
22 Rotation R(n, teta);
23 V::addAsAngularVelocity(v, R);
24 R.compress();
25 return R;
26 }
27
28 Rotation rotated(const Rotation& r) const { return V::rotateRotation(*this, r); }
29
30 void compress() { // teta will be < pi
31 if (teta > M_PI) {
32 if (teta > M_PI * 2.0) teta = fmod(teta, M_PI * 2.0);
33 n = -n;
34 teta = (2.0 * M_PI) - teta;
35 }
36 }
37
39 Rotation res(n, teta);
40 res.compress();
41 return res;
42 }
43
44 Rotation inverted() const { return Rotation(-n, teta); }
45
46 Rotation operator+(const Rotation& R2) const {
47 Rotation R = V::addRotations(R2, *this);
48 R.compress();
49 return R;
50 }
51 Rotation operator-(const Rotation& R2) const {
52 Rotation R = V::addRotations(-R2, *this);
53 R.compress();
54 return R;
55 }
56};
57}
58
59#endif
this file contains various miscellanious utility functions & helpers *
Provides common mathematical functions and vector operations.
Definition: std.hpp:4
double fmod(double x, double y)
Computes the remainder of the division of two numbers.
Definition: std.hpp:205
Rotation rotated(const Rotation &r) const
Definition: rotation.h:28
Rotation compressed() const
Definition: rotation.h:38
Rotation operator+(const Rotation &R2) const
Definition: rotation.h:46
Rotation operator-(const Rotation &R2) const
Definition: rotation.h:51
Rotation operator+(const V &v) const
Definition: rotation.h:21
Rotation inverted() const
Definition: rotation.h:44
Rotation(const V &v, const double &f)
Definition: rotation.h:13
void randomize()
Definition: rotation.h:15