Debian Codegraph @

external/cubicvr2/math/vec3.h

raw
1 //
2 // vec3.h
3 // CubicVR2
4 //
5 // Created by Charles J. Cliffe on 2013-02-21.
6 // Copyright (c) 2013 Charles J. Cliffe. All rights reserved.
7 //
8
9 #ifndef __CubicVR2__vec3__
10 #define __CubicVR2__vec3__
11
12 #include <iostream>
13 #include "cubic_types.h"
14 #include <cmath>
15 #include <stddef.h>
16
17 namespace CubicVR {
18
19 #define vec3SG(c,x,y) \
20 vec3 COMBINE(get,x)() { return y; } \
21 c & COMBINE(set,x)(vec3 value) { y = value; return *this; }
22
23
24 struct vec3 {
25 __float x,y,z;
26
27 __float& r() { return x; }
28 __float& g() { return y; }
29 __float& b() { return z; }
30
31 //access as-array:
32 inline __float& operator [] (size_t i) {
33 __float* as_array = (__float*)this;
34 return (as_array[i]);
35 }
36
37 inline const __float& operator [] (size_t i) const {
38 __float* as_array = (__float*)this;
39 return (as_array[i]);
40 }
41
42 vec3 (__float xi,__float yi,__float zi) { x = xi; y = yi; z = zi; }
43 vec3 () { x = y = z = 0.0f; }
44
45 vec3 operator*(__float v) { return vec3(x*v, y*v, z*v); }
46 vec3 operator*(vec3 v) { return vec3::cross(*this,v); }
47 vec3 operator+(vec3 v) { return vec3::add(*this,v); }
48 vec3 operator-(vec3 v) { return vec3::subtract(*this,v); }
49
50
51 static __float length(vec3 pta, vec3 ptb) {
52 __float a,b,c;
53 a = ptb[0]-pta[0];
54 b = ptb[1]-pta[1];
55 c = ptb[2]-pta[2];
56 return sqrtf((a*a) + (b*b) + (c*c));
57 };
58 static __float length(vec3 pta) {
59 __float a,b,c;
60 a = pta[0];
61 b = pta[1];
62 c = pta[2];
63 return sqrtf((a*a) + (b*b) + (c*c));
64 };
65 static vec3 normalize(vec3 pt) {
66 __float a = pt[0], b = pt[1], c = pt[2],
67 d = sqrtf((a*a) + (b*b) + (c*c));
68 if (d) {
69 pt[0] = pt[0]/d;
70 pt[1] = pt[1]/d;
71 pt[2] = pt[2]/d;
72
73 return pt;
74 }
75
76 pt = vec3(0.0f,0.0f,0.0f);
77
78 return pt;
79 };
80 static __float dot(vec3 v1, vec3 v2) {
81 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
82 };
83 static __float angle(vec3 v1, vec3 v2) {
84 __float a = acosf((v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]) / (sqrtf(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2]) * sqrtf(v2[0] * v2[0] + v2[1] * v2[1] + v2[2] * v2[2])));
85
86 return a;
87 };
88 static vec3 cross(vec3 vectA, vec3 vectB) {
89 return vec3(
90 vectA[1] * vectB[2] - vectB[1] * vectA[2], vectA[2] * vectB[0] - vectB[2] * vectA[0], vectA[0] * vectB[1] - vectB[0] * vectA[1]
91 );
92 };
93 static vec3 multiply(vec3 vectA, __float constB) {
94 return vec3(vectA[0] * constB, vectA[1] * constB, vectA[2] * constB);
95 };
96 static vec3 add(vec3 vectA, vec3 vectB) {
97 return vec3(vectA[0] + vectB[0], vectA[1] + vectB[1], vectA[2] + vectB[2]);
98 };
99
100 static vec3 subtract(vec3 vectA, vec3 vectB) {
101 return vec3(vectA[0] - vectB[0], vectA[1] - vectB[1], vectA[2] - vectB[2]);
102 };
103
104 static bool equal(vec3 a, vec3 b, __float epsilon = 0.0000001f) {
105 return ((fabs(a[0] - b[0]) < epsilon) && (fabs(a[1] - b[1]) < epsilon) && (fabs(a[2] - b[2]) < epsilon));
106 };
107
108 static vec3 moveViewRelative(vec3 position, vec3 target, __float xdelta, __float zdelta) {
109 __float ang = atan2f(zdelta, xdelta);
110 __float cam_ang = atan2f(target[2] - position[2], target[0] - position[0]);
111 __float mag = sqrtf(xdelta * xdelta + zdelta * zdelta);
112
113 __float move_ang = cam_ang + ang + (float)M_PI/2.0f;
114
115 // if (typeof(alt_source) === 'object') {
116 // return [alt_source[0] + mag * Math.cos(move_ang), alt_source[1], alt_source[2] + mag * Math.sin(move_ang)];
117 // }
118
119 return vec3(position[0] + mag * cosf(move_ang), position[1], position[2] + mag * sinf(move_ang));
120 };
121
122 static vec3 trackTarget(vec3 position, vec3 target, __float trackingSpeed, __float safeDistance) {
123 vec3 camv = vec3::subtract(target, position);
124 vec3 dist = camv;
125 __float fdist = vec3::length(dist);
126 vec3 motionv = camv;
127
128 motionv = vec3::normalize(motionv);
129 motionv = vec3::multiply(motionv, trackingSpeed * (1.0f / (1.0f / (fdist - safeDistance))));
130
131 vec3 ret_pos;
132
133 if (fdist > safeDistance) {
134 ret_pos = vec3::add(position, motionv);
135 } else if (fdist < safeDistance) {
136 motionv = camv;
137 motionv = vec3::normalize(motionv);
138 motionv = vec3::multiply(motionv, trackingSpeed * (1.0f / (1.0f / (fabsf(fdist - safeDistance)))));
139 ret_pos = vec3::subtract(position, motionv);
140 } else {
141 ret_pos = vec3(position[0], position[1] + motionv[2], position[2]);
142 }
143
144 return ret_pos;
145 };
146
147 static vec3 getClosestTo(vec3 ptA, vec3 ptB, vec3 ptTest) {
148 vec3 S, T, U;
149
150 S = vec3::subtract(ptB, ptA);
151 T = vec3::subtract(ptTest, ptA);
152 U = vec3::add(vec3::multiply(S, vec3::dot(S, T) / vec3::dot(S, S)), ptA);
153
154 return U;
155 };
156
157 // linePlaneIntersect: function(normal, point_on_plane, segment_start, segment_end)
158 // {
159 // // form a plane from normal and point_on_plane and test segment start->end to find intersect point
160 // var denom,mu;
161 //
162 // var d = - normal[0] * point_on_plane[0] - normal[1] * point_on_plane[1] - normal[2] * point_on_plane[2];
163 //
164 // // calculate position where the plane intersects the segment
165 // denom = normal[0] * (segment_end[0] - segment_start[0]) + normal[1] * (segment_end[1] - segment_start[1]) + normal[2] * (segment_end[2] - segment_start[2]);
166 // if (Math.fabs(denom) < 0.001) return false;
167 //
168 // mu = - (d + normal[0] * segment_start[0] + normal[1] * segment_start[1] + normal[2] * segment_start[2]) / denom;
169 // return [
170 // (segment_start[0] + mu * (segment_end[0] - segment_start[0])),
171 // (segment_start[1] + mu * (segment_end[1] - segment_start[1])),
172 // (segment_start[2] + mu * (segment_end[2] - segment_start[2]))
173 // ];
174 // }
175 };
176
177 }
178 #endif /* defined(__CubicVR2__vec3__) */