PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
transformation.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "transformation.h"
12#include <cmath> // definition of sin, cosine etc
13
14std::vector<double>
15util::rotateCW2D(const std::vector<double> &x,
16 const double &theta) {
17
18 return std::vector<double>{x[0] * std::cos(theta) + x[1] * std::sin(theta),
19 -x[0] * std::sin(theta) + x[1] * std::cos(theta),
20 0.0};
21}
22
24 const double &theta) {
25
26 return {x.d_x * std::cos(theta) + x.d_y * std::sin(theta),
27 -x.d_x * std::sin(theta) + x.d_y * std::cos(theta), 0.0};
28}
29
30std::vector<double>
31util::rotateACW2D(const std::vector<double> &x,
32 const double &theta) {
33
34 return rotateCW2D(x, -theta);
35}
36
38 const double &theta) {
39
40 return rotateCW2D(x, -theta);
41}
42
43
44std::vector<double>
45util::rotate2D(const std::vector<double> &x,
46 const double &theta) {
47
48 return std::vector<double>{x[0] * std::cos(theta) - x[1] * std::sin(theta),
49 x[0] * std::sin(theta) + x[1] * std::cos(theta),
50 0.0};
51}
52
54 const double &theta) {
55
56 return {x.d_x * std::cos(theta) - x.d_y * std::sin(theta),
57 x.d_x * std::sin(theta) + x.d_y * std::cos(theta), 0.0};
58}
59
61 const double &theta) {
62
63 return {-x.d_x * std::sin(theta) - x.d_y * std::cos(theta),
64 x.d_x * std::cos(theta) - x.d_y * std::sin(theta), 0.0};
65}
66
67util::Point util::rotate(const util::Point &p, const double &theta, const util::Point &axis) {
68
69 auto ct = std::cos(theta);
70 auto st = std::sin(theta);
71
72 // dot
73 double p_dot_n = p * axis;
74
75 // cross
76 util::Point n_cross_p = axis.cross(p);
77
78 return (1. - ct) * p_dot_n * axis + ct * p + st * n_cross_p;
79}
80
82
83 if ((a - b).lengthSq() < 1.0E-12)
84 return 0.;
85
86 // since we do not know which side of plane given by normal
87 // a x b / |a x b| is +ve, we compute the angle using cosine
88 return std::acos(b * a / (b.length() * a.length()));
89}
90
91double util::angle(util::Point a, util::Point b, util::Point axis, bool is_axis) {
92
93 if ((a - b).lengthSq() < 1.0E-12)
94 return 0.;
95
96 if (is_axis) {
97
98 // normal to plane of rotation
99 util::Point n = axis / axis.length();
100
101 util::Point na = n.cross(a);
102
103 double theta = std::atan(b * na / (a * b - (b * n) * (a * n)));
104 if (theta < 0.)
105 theta += M_PI;
106
107 if (b * na < 0.)
108 theta = M_PI + theta;
109
110 return theta;
111 } else {
112
113 auto theta = angle(a, b);
114
115 // TODO below only works in specific cases such as when vectors in xy
116 // plane and vector x gives the positive plane direction, i.e. whether
117 // (0, 0, 1) is +ve or (0, 0, -1) is +ve. Similar is true for yz, zx
118 // planes.
119
120 // normal to a and b
121 util::Point n_ab = a.cross(b);
122
123 double orient = axis * n_ab;
124 if (orient < 0.)
125 return 2. * M_PI - theta;
126 else
127 return theta;
128 }
129}
std::vector< double > rotate2D(const std::vector< double > &x, const double &theta)
Rotates a vector in xy-plane assuming ACW convention.
double angle(util::Point a, util::Point b)
Computes angle between two vectors.
util::Point derRotate2D(const util::Point &x, const double &theta)
Computes derivative of rotation wrt to time.
std::vector< double > rotateACW2D(const std::vector< double > &x, const double &theta)
Rotates a vector in xy-plane in anti-clockwise direction.
std::vector< double > rotateCW2D(const std::vector< double > &x, const double &theta)
Rotates a vector in xy-plane in clockwise direction.
util::Point rotate(const util::Point &p, const double &theta, const util::Point &axis)
Returns the vector after rotating by desired angle.
A structure to represent 3d vectors.
Definition point.h:30
double d_y
the y coordinate
Definition point.h:36
double length() const
Computes the Euclidean length of the vector.
Definition point.h:118
Point cross(const Point &b) const
Computes the cross product between this vector and given vector.
Definition point.h:151
double d_x
the x coordinate
Definition point.h:33