root/OpenSceneGraph/trunk/src/osgPlugins/lib3ds/vector.cpp @ 1563

Revision 1563, 4.4 kB (checked in by robert, 12 years ago)

Added an #ifdef to osgGLUT/Window.cpp to handle the case of compiling against
older versions of GLUT.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[8]1/*
2 * The 3D Studio File Format Library
3 * Copyright (C) 1996-2001 by J.E. Hoffmann <je-h@gmx.net>
4 * All rights reserved.
5 *
6 * This program is  free  software;  you can redistribute it and/or modify it
[151]7 * under the terms of the  GNU Lesser General Public License  as published by
8 * the  Free Software Foundation;  either version 2.1 of the License,  or (at
[8]9 * your option) any later version.
10 *
11 * This  program  is  distributed in  the  hope that it will  be useful,  but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
[151]13 * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public 
[8]14 * License for more details.
15 *
16 * You should  have received  a copy of the GNU Lesser General Public License
17 * along with  this program;  if not, write to the  Free Software Foundation,
18 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id$
21 */
22#define LIB3DS_EXPORT
[1563]23#include "vector.h"
[8]24#include <math.h>
25
[151]26
[8]27/*!
28 * \defgroup vector Vector Mathematics
29 *
30 * \author J.E. Hoffmann <je-h@gmx.net>
31 */
32/*!
33 * \typedef Lib3dsVector
34 *   \ingroup vector
35 */
36
[151]37
[8]38/*!
39 * \ingroup vector
40 */
41void
42lib3ds_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 * \ingroup vector
53 */
54void
55lib3ds_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 * \ingroup vector
66 */
67void
68lib3ds_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 * \ingroup vector
79 */
80void
81lib3ds_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 * \ingroup vector
92 */
93void
94lib3ds_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 * \ingroup vector
105 */
106void
107lib3ds_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 * \ingroup vector
118 */
119void
120lib3ds_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 * \ingroup vector
130 */
131Lib3dsFloat
132lib3ds_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 * \ingroup vector
140 */
141Lib3dsFloat
142lib3ds_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 * \ingroup vector
150 */
151Lib3dsFloat
152lib3ds_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 * \ingroup vector
160 */
161void
162lib3ds_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 * \ingroup vector
191 */
192void
193lib3ds_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 * \ingroup vector
206 */
207void
208lib3ds_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 * \ingroup vector
218 */
219void
220lib3ds_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 * c[i] = min(c[i], a[i]);
[8]237 * \ingroup vector
238 */
[151]239void 
240lib3ds_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 * c[i] = max(c[i], a[i]);
253 * \ingroup vector
254 */
255void 
256lib3ds_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 * \ingroup vector
269 */
[8]270void
271lib3ds_vector_dump(Lib3dsVector c)
272{
[151]273  fprintf(stderr, "%f %f %f\n", c[0], c[1], c[2]);
[8]274}
Note: See TracBrowser for help on using the browser.