| [8] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| [151] | 7 | |
|---|
| 8 | |
|---|
| [8] | 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| [151] | 13 | |
|---|
| [8] | 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #define LIB3DS_EXPORT |
|---|
| [1563] | 23 | #include "vector.h" |
|---|
| [8] | 24 | #include <math.h> |
|---|
| 25 | |
|---|
| [151] | 26 | |
|---|
| [8] | 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| [151] | 37 | |
|---|
| [8] | 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | void |
|---|
| 42 | lib3ds_vector_zero(Lib3dsVector c) |
|---|
| 43 | { |
|---|
| [151] | 44 | int i; |
|---|
| 45 | for (i=0; i<3; ++i) { |
|---|
| 46 | c[i]=0.0f; |
|---|
| 47 | } |
|---|
| [8] | 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | void |
|---|
| 55 | lib3ds_vector_copy(Lib3dsVector dest, Lib3dsVector src) |
|---|
| 56 | { |
|---|
| [151] | 57 | int i; |
|---|
| 58 | for (i=0; i<3; ++i) { |
|---|
| 59 | dest[i]=src[i]; |
|---|
| 60 | } |
|---|
| [8] | 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | void |
|---|
| 68 | lib3ds_vector_neg(Lib3dsVector c) |
|---|
| 69 | { |
|---|
| [151] | 70 | int i; |
|---|
| 71 | for (i=0; i<3; ++i) { |
|---|
| 72 | c[i]=-c[i]; |
|---|
| 73 | } |
|---|
| [8] | 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | void |
|---|
| 81 | lib3ds_vector_add(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) |
|---|
| 82 | { |
|---|
| [151] | 83 | int i; |
|---|
| 84 | for (i=0; i<3; ++i) { |
|---|
| 85 | c[i]=a[i]+b[i]; |
|---|
| 86 | } |
|---|
| [8] | 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | void |
|---|
| 94 | lib3ds_vector_sub(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) |
|---|
| 95 | { |
|---|
| [151] | 96 | int i; |
|---|
| 97 | for (i=0; i<3; ++i) { |
|---|
| 98 | c[i]=a[i]-b[i]; |
|---|
| 99 | } |
|---|
| [8] | 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | void |
|---|
| 107 | lib3ds_vector_scalar(Lib3dsVector c, Lib3dsFloat k) |
|---|
| 108 | { |
|---|
| [151] | 109 | int i; |
|---|
| 110 | for (i=0; i<3; ++i) { |
|---|
| 111 | c[i]*=k; |
|---|
| 112 | } |
|---|
| [8] | 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | void |
|---|
| 120 | lib3ds_vector_cross(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b) |
|---|
| 121 | { |
|---|
| [151] | 122 | c[0]=a[1]*b[2] - a[2]*b[1]; |
|---|
| 123 | c[1]=a[2]*b[0] - a[0]*b[2]; |
|---|
| 124 | c[2]=a[0]*b[1] - a[1]*b[0]; |
|---|
| [8] | 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | Lib3dsFloat |
|---|
| 132 | lib3ds_vector_dot(Lib3dsVector a, Lib3dsVector b) |
|---|
| 133 | { |
|---|
| [151] | 134 | return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]); |
|---|
| [8] | 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | Lib3dsFloat |
|---|
| 142 | lib3ds_vector_squared(Lib3dsVector c) |
|---|
| 143 | { |
|---|
| [151] | 144 | return(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]); |
|---|
| [8] | 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | Lib3dsFloat |
|---|
| 152 | lib3ds_vector_length(Lib3dsVector c) |
|---|
| 153 | { |
|---|
| [151] | 154 | return((Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2])); |
|---|
| [8] | 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | void |
|---|
| 162 | lib3ds_vector_normalize(Lib3dsVector c) |
|---|
| 163 | { |
|---|
| [151] | 164 | Lib3dsFloat l,m; |
|---|
| [8] | 165 | |
|---|
| [151] | 166 | l=(Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]); |
|---|
| 167 | if (fabs(l)<LIB3DS_EPSILON) { |
|---|
| 168 | c[0]=c[1]=c[2]=0.0f; |
|---|
| 169 | if ((c[0]>=c[1]) && (c[0]>=c[2])) { |
|---|
| 170 | c[0]=1.0f; |
|---|
| [8] | 171 | } |
|---|
| 172 | else |
|---|
| [151] | 173 | if (c[1]>=c[2]) { |
|---|
| 174 | c[1]=1.0f; |
|---|
| [8] | 175 | } |
|---|
| [151] | 176 | else { |
|---|
| 177 | c[2]=1.0f; |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | else { |
|---|
| 181 | m=1.0f/l; |
|---|
| 182 | c[0]*=m; |
|---|
| 183 | c[1]*=m; |
|---|
| 184 | c[2]*=m; |
|---|
| 185 | } |
|---|
| [8] | 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | void |
|---|
| 193 | lib3ds_vector_normal(Lib3dsVector n, Lib3dsVector a, Lib3dsVector b, Lib3dsVector c) |
|---|
| 194 | { |
|---|
| [151] | 195 | Lib3dsVector p,q; |
|---|
| [8] | 196 | |
|---|
| [151] | 197 | lib3ds_vector_sub(p,c,b); |
|---|
| 198 | lib3ds_vector_sub(q,a,b); |
|---|
| 199 | lib3ds_vector_cross(n,p,q); |
|---|
| 200 | lib3ds_vector_normalize(n); |
|---|
| [8] | 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | void |
|---|
| 208 | lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a) |
|---|
| 209 | { |
|---|
| [151] | 210 | c[0]= m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2] + m[3][0]; |
|---|
| 211 | c[1]= m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2] + m[3][1]; |
|---|
| 212 | c[2]= m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2] + m[3][2]; |
|---|
| [8] | 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | void |
|---|
| 220 | lib3ds_vector_cubic(Lib3dsVector c, Lib3dsVector a, Lib3dsVector p, Lib3dsVector q, |
|---|
| [151] | 221 | Lib3dsVector b, Lib3dsFloat t) |
|---|
| [8] | 222 | { |
|---|
| [151] | 223 | Lib3dsDouble x,y,z,w; |
|---|
| [8] | 224 | |
|---|
| [151] | 225 | x=2*t*t*t - 3*t*t + 1; |
|---|
| 226 | y=-2*t*t*t + 3*t*t; |
|---|
| 227 | z=t*t*t - 2*t*t + t; |
|---|
| 228 | w=t*t*t - t*t; |
|---|
| 229 | c[0]=(Lib3dsFloat)(x*a[0] + y*b[0] + z*p[0] + w*q[0]); |
|---|
| 230 | c[1]=(Lib3dsFloat)(x*a[1] + y*b[1] + z*p[1] + w*q[1]); |
|---|
| 231 | c[2]=(Lib3dsFloat)(x*a[2] + y*b[2] + z*p[2] + w*q[2]); |
|---|
| [8] | 232 | } |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| [151] | 236 | |
|---|
| [8] | 237 | |
|---|
| 238 | |
|---|
| [151] | 239 | void |
|---|
| 240 | lib3ds_vector_min(Lib3dsVector c, Lib3dsVector a) |
|---|
| 241 | { |
|---|
| 242 | int i; |
|---|
| 243 | for (i=0; i<3; ++i) { |
|---|
| 244 | if (a[i]<c[i]) { |
|---|
| 245 | c[i] = a[i]; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | void |
|---|
| 256 | lib3ds_vector_max(Lib3dsVector c, Lib3dsVector a) |
|---|
| 257 | { |
|---|
| 258 | int i; |
|---|
| 259 | for (i=0; i<3; ++i) { |
|---|
| 260 | if (a[i]>c[i]) { |
|---|
| 261 | c[i] = a[i]; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | |
|---|
| [8] | 270 | void |
|---|
| 271 | lib3ds_vector_dump(Lib3dsVector c) |
|---|
| 272 | { |
|---|
| [151] | 273 | fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]); |
|---|
| [8] | 274 | } |
|---|