forked from Dronee/vector3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector3.cpp
191 lines (164 loc) · 5.98 KB
/
vector3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// vector3.cpp
// vector3
//
// Created by Maksim Piriyev on 4/28/18.
// Copyright © 2018 Dronee. All rights reserved.
//
#include "vector3.h"
#include <float.h>
namespace vector3{
const Vector2 Vector2::Zero(0,0);
const Vector2 Vector2::X(1,0);
const Vector2 Vector2::Y(0,1);
const Vector2 Vector2::One(1,1);
const Vector3 Vector3::Zero(0,0,0);
const Vector3 Vector3::X(1,0,0);
const Vector3 Vector3::Y(0,1,0);
const Vector3 Vector3::Z(0,0,1);
const Vector3 Vector3::One(1,1,1);// = Vector3(1,1,1).normalized();
const Vector3 Vector3::Max(DBL_MAX,DBL_MAX,DBL_MAX);
const Vector3 Vector3::Min(DBL_MIN,DBL_MIN,DBL_MIN);
#ifdef MOBILE
const Vector3 Vector3::Head(0,1,0);
const Vector2 Vector2::Head(0,1);
#else
const Vector3 Vector3::Head(1,0,0);
const Vector2 Vector2::Head(1,0);
#endif
const Quaternion Quaternion::Zero(0,0,0,0);
const Quaternion Quaternion::Identity(0,0,0,1);
Vector2::operator Vector3()const { return Vector3(x,y,0); }
scalar Vector3::pitch() const{
// return atan2(z,yz().length());
// return atan2 (y,xz().length()) ;
//Vector3 surface = xz();
//return surface.angle(*this);
//return xz().angle();
return Vector3::Head.rotation(*this).pitch();
}
scalar Vector3::roll()const {
// return atan (x/yz().length());
return Vector3::Head.rotation(*this).roll();
}
scalar Vector3::yaw() const{
//#ifdef MOBILE
// return xy().angle();
//#else
// return -xy().angle();
//#endif
// return atan (y/xz().length()) ;
// return atan (z/yz().length());
//return -xy().angle();
return Vector3::Head.rotation(*this).yaw();
}
// Quaternion Vector3::rotation(const Vector3& to)const {
// auto a = cross(to);
// auto w = sqrt(lengthSquared() * to.lengthSquared()) + dot(to);
// auto q = Quaternion(a.x,a.y,a.z,w);
// // auto ll = to.length();
// // auto l = q.length();
// return q.normalized();
// }
Quaternion Vector3::rotation(const Vector3& to)const {
auto t = to.normalized();
auto f = (*this).normalized();
auto a = cross(t);
auto w =f.dot(t);
if(t == -f){
return Quaternion(orthogonal(),rad(180));
}
//auto q = Quaternion(a,acos(w));
// auto ll = to.length();
// auto l = q.length();
return Quaternion(a,acos(w));//q;//.normalized();
}
//quat quat::fromtwovectors(vec3 u, vec3 v)
//{
// float cos_theta = dot(normalize(u), normalize(v));
// float angle = acos(cos_theta);
// vec3 w = normalize(cross(u, v));
// return quat::fromaxisangle(angle, w);
//}
//Quaternion Vector3::rotation(Vector3& to)const {
// auto a = cross(to.normalized()).normalized();
// auto w = dot(to);
// auto angle = acos(w)/2;
// w = cos(angle);
// a = a * sin(angle);
// auto q = Quaternion(a.x,a.y,a.z,w);
// auto ll = to.length();
// auto l = q.length();
// return q.normalized();
//}
Quaternion Vector3::rotation(const Vector3& to,const Vector3& axis) {
auto uv1 = axis.cross(*this);
auto uv2 = axis.cross(to);
auto w = (uv1+uv2).normalized().dot(uv1);
return Quaternion(axis.x,axis.y,axis.z,w);
}
Vector3 Vector3::rotated(scalar angle,const Vector3& axis) {
auto q = Quaternion(axis, angle);
return (*this) * q;
}
vector<scalar> mid(vector<scalar>& a,vector<scalar>& b,int i,scalar m){
vector<scalar> r(a.size());
for(int j=0;j<a.size();j++) {
r[j] = (a[j]+b[j])/2;
}
r[i] = m;
return r;
}
vector<scalar> ternary(vector<scalar> start, vector<scalar> end, function<scalar(vector<scalar>)> eval){
auto l = start, r = end;
auto sum = [&](){
auto rtn = 0.0;
for(int i = 0;i<l.size();i++) rtn += r[i]-l[i];
return rtn;
};
while(sum() > l.size()*eps){
for(int j=0;j<start.size();j++) {
auto l1 = (l[j]*2+r[j])/3;
auto l2 = (l[j]+2*r[j])/3;
if (eval(mid(l,r,j,l1)) < eval(mid(l,r,j,l2)) ){
r[j] = l2;
}else{
l[j] = l1;
}
}
}
return l;
}
// vector<scalar> ternary(vector<scalar> start, vector<scalar> end, function<scalar(vector<scalar>)> eval){
// auto l = start, r = end;
// auto prevEval = eval(l);
// for(int i=0;i<100*start.size();i++){
// for(int j=0;j<start.size();j++) {
// auto l1 = (l[j]*2+r[j])/3;
// auto l2 = (l[j]+2*r[j])/3;
// if (eval(mid(l,r,j,l1)) < eval(mid(l,r,j,l2)) ){
// r[j] = l2;
// }else{
// l[j] = l1;
// }
//
// }
// }
// return l;
//
// }
std::vector<Vector3> operator+(const std::vector<Vector3> &A, const std::vector<Vector3> &B)
{
std::vector<Vector3> AB;
AB.reserve( A.size() + B.size() ); // preallocate memory
AB.insert( AB.end(), A.begin(), A.end() ); // add A;
AB.insert( AB.end(), B.begin(), B.end() ); // add B;
return AB;
}
std::vector<Vector3> &operator+=(std::vector<Vector3> &A, const std::vector<Vector3> &B)
{
A.reserve( A.size() + B.size() ); // preallocate memory without erase original data
A.insert( A.end(), B.begin(), B.end() ); // add B;
return A; // here A could be named AB
}
}