File: vector.c

package info (click to toggle)
boson 0.13-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 12,992 kB
  • ctags: 29,287
  • sloc: cpp: 203,902; ansic: 9,959; makefile: 75; sh: 64; sed: 47
file content (377 lines) | stat: -rw-r--r-- 6,691 bytes parent folder | download | duplicates (2)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * The 3D Studio File Format Library
 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
 * All rights reserved.
 *
 * This program is  free  software;  you can redistribute it and/or modify it
 * under the terms of the  GNU Lesser General Public License  as published by 
 * the  Free Software Foundation;  either version 2.1 of the License,  or (at 
 * your option) any later version.
 *
 * This  program  is  distributed in  the  hope that it will  be useful,  but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public  
 * License for more details.
 *
 * You should  have received  a copy of the GNU Lesser General Public License
 * along with  this program;  if not, write to the  Free Software Foundation,
 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: vector.c,v 1.9 2004/11/16 07:41:44 efalk Exp $
 */
#define LIB3DS_EXPORT
#include <lib3ds/vector.h>
#include <math.h>


/*!
 * \defgroup vector Vector Mathematics
 *
 * \author J.E. Hoffmann <je-h@gmx.net>
 */
/*!
 * \typedef Lib3dsVector
 *   \ingroup vector
 */


/*!
 * Clear a vector to zero.
 *
 * \param c Vector to clear.
 *
 * \ingroup vector
 */
void
lib3ds_vector_zero(Lib3dsVector c)
{
  int i;
  for (i=0; i<3; ++i) {
    c[i]=0.0f;
  }
}


/*!
 * Copy a vector.
 *
 * \param dest Destination vector.
 * \param src Source vector.
 *
 * \ingroup vector
 */
void
lib3ds_vector_copy(Lib3dsVector dest, Lib3dsVector src)
{
  int i;
  for (i=0; i<3; ++i) {
    dest[i]=src[i];
  }
}


/*!
 * Negate a vector.
 *
 * \param c Vector to negate.
 *
 * \ingroup vector
 */
void
lib3ds_vector_neg(Lib3dsVector c)
{
  int i;
  for (i=0; i<3; ++i) {
    c[i]=-c[i];
  }
}


/*!
 * Add two vectors.
 *
 * \param c Result.
 * \param a First addend.
 * \param b Second addend.
 *
 * \ingroup vector
 */
void
lib3ds_vector_add(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
{
  int i;
  for (i=0; i<3; ++i) {
    c[i]=a[i]+b[i];
  }
}


/*!
 * Subtract two vectors.
 *
 * \param c Result.
 * \param a Addend.
 * \param b Minuend.
 *
 * \ingroup vector
 */
void
lib3ds_vector_sub(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
{
  int i;
  for (i=0; i<3; ++i) {
    c[i]=a[i]-b[i];
  }
}


/*!
 * Multiply a vector by a scalar.
 *
 * \param c Vector to be multiplied.
 * \param k Scalar.
 *
 * \ingroup vector
 */
void
lib3ds_vector_scalar(Lib3dsVector c, Lib3dsFloat k)
{
  int i;
  for (i=0; i<3; ++i) {
    c[i]*=k;
  }
}


/*!
 * Compute cross product.
 *
 * \param c Result.
 * \param a First vector.
 * \param b Second vector.
 *
 * \ingroup vector
 */
void
lib3ds_vector_cross(Lib3dsVector c, Lib3dsVector a, Lib3dsVector b)
{
  c[0]=a[1]*b[2] - a[2]*b[1];
  c[1]=a[2]*b[0] - a[0]*b[2];
  c[2]=a[0]*b[1] - a[1]*b[0];
}


/*!
 * Compute dot product.
 *
 * \param a First vector.
 * \param b Second vector.
 *
 * \return Dot product.
 *
 * \ingroup vector
 */
Lib3dsFloat
lib3ds_vector_dot(Lib3dsVector a, Lib3dsVector b)
{
  return(a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
}


/*!
 * Compute square of vector.
 *
 * Computes x*x + y*y + z*z.
 *
 * \param c Vector to square.
 *
 * \return Square of vector.
 *
 * \ingroup vector
 */
Lib3dsFloat
lib3ds_vector_squared(Lib3dsVector c)
{
  return(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]);
}


/*!
 * Compute length of vector.
 *
 * Computes |c| = sqrt(x*x + y*y + z*z)
 *
 * \param c Vector to compute.
 *
 * \return Length of vector.
 *
 * \ingroup vector
 */
Lib3dsFloat
lib3ds_vector_length(Lib3dsVector c)
{
  return((Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]));
}


/*!
 * Normalize a vector.
 *
 * Scales a vector so that its length is 1.0.
 *
 * \param c Vector to normalize.
 *
 * \ingroup vector
 */
void
lib3ds_vector_normalize(Lib3dsVector c)
{
  Lib3dsFloat l,m;

  l=(Lib3dsFloat)sqrt(c[0]*c[0] + c[1]*c[1] + c[2]*c[2]);
  if (fabs(l)<LIB3DS_EPSILON) {
    if ((c[0]>=c[1]) && (c[0]>=c[2])) {
      c[0]=1.0f;
      c[1]=c[2]=0.0f;
    }
    else
    if (c[1]>=c[2]) {
      c[1]=1.0f;
      c[0]=c[2]=0.0f;
    }
    else {
      c[2]=1.0f;
      c[0]=c[1]=0.0f;
    }
  }
  else {
    m=1.0f/l;
    c[0]*=m;
    c[1]*=m;
    c[2]*=m;
  }
}


/*!
 * Compute a vector normal to two line segments.
 *
 * Computes the normal vector to the lines b-a and b-c.
 *
 * \param n Returned normal vector.
 * \param a Endpoint of first line.
 * \param b Base point of both lines.
 * \param c Endpoint of second line.
 *
 * \ingroup vector
 */
void
lib3ds_vector_normal(Lib3dsVector n, Lib3dsVector a, Lib3dsVector b, Lib3dsVector c)
{
  Lib3dsVector p,q;

  lib3ds_vector_sub(p,c,b);
  lib3ds_vector_sub(q,a,b);
  lib3ds_vector_cross(n,p,q);
  lib3ds_vector_normalize(n);
}


/*!
 * Multiply a point by a transformation matrix.
 *
 * Applies the given transformation matrix to the given point.  With some
 * transformation matrices, a vector may also be transformed.
 *
 * \param c Result.
 * \param m Transformation matrix.
 * \param a Input point.
 *
 * \ingroup vector
 */
void
lib3ds_vector_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a)
{
  c[0]= m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2] + m[3][0];
  c[1]= m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2] + m[3][1];
  c[2]= m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2] + m[3][2];
}


/*!
 * Compute a point on a cubic spline.
 *
 * Computes a point on a parametric Bezier spline.
 *
 * \param c Result.
 * \param a First endpoint of the spline.
 * \param p First tangent vector of the spline.
 * \param q Second tangent vector of the spline.
 * \param b Second endpoint of the spline.
 * \param t Spline parameter [0. 1.]
 *
 * \ingroup vector
 */
void
lib3ds_vector_cubic(Lib3dsVector c, Lib3dsVector a, Lib3dsVector p, Lib3dsVector q,
  Lib3dsVector b, Lib3dsFloat t)
{
  Lib3dsDouble x,y,z,w;   

  x=2*t*t*t - 3*t*t + 1;
  y=-2*t*t*t + 3*t*t;
  z=t*t*t - 2*t*t + t;
  w=t*t*t - t*t;
  c[0]=(Lib3dsFloat)(x*a[0] + y*b[0] + z*p[0] + w*q[0]);
  c[1]=(Lib3dsFloat)(x*a[1] + y*b[1] + z*p[1] + w*q[1]);
  c[2]=(Lib3dsFloat)(x*a[2] + y*b[2] + z*p[2] + w*q[2]);
}


/*!
 * c[i] = min(c[i], a[i]);
 *
 * Computes minimum values of x,y,z independently.
 *
 * \ingroup vector
 */
void 
lib3ds_vector_min(Lib3dsVector c, Lib3dsVector a)
{
  int i;
  for (i=0; i<3; ++i) {
    if (a[i]<c[i]) {
      c[i] = a[i];
    }
  }
}


/*!
 * c[i] = max(c[i], a[i]);
 *
 * Computes maximum values of x,y,z independently.
 *
 * \ingroup vector
 */
void 
lib3ds_vector_max(Lib3dsVector c, Lib3dsVector a)
{
  int i;
  for (i=0; i<3; ++i) {
    if (a[i]>c[i]) {
      c[i] = a[i];
    }
  }
}


/*!
 * \ingroup vector
 */
void
lib3ds_vector_dump(Lib3dsVector c)
{
  fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]);
}