Makefile: clean-all
[summerhack] / src / 3dengfx / libs / lib3ds / tcb.c
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
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 
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
13  * or  FITNESS FOR A  PARTICULAR PURPOSE.  See the  GNU Lesser General Public  
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: tcb.c,v 1.9 2001/07/07 19:05:30 jeh Exp $
21  */
22 #define LIB3DS_EXPORT
23 #include <lib3ds/tcb.h>
24 #include <lib3ds/io.h>
25 #include <math.h>
26
27
28 /*!
29  * \defgroup tcb Tension/Continuity/Bias Splines
30  *
31  * \author J.E. Hoffmann <je-h@gmx.net>
32  */
33
34
35 /*!
36  * \ingroup tcb 
37  */
38 void
39 lib3ds_tcb(Lib3dsTcb *p, Lib3dsTcb *pc, Lib3dsTcb *c, Lib3dsTcb *nc, Lib3dsTcb *n,
40   Lib3dsFloat *ksm, Lib3dsFloat *ksp, Lib3dsFloat *kdm, Lib3dsFloat *kdp)
41 {
42   Lib3dsFloat tm,cm,cp,bm,bp,tmcm,tmcp,cc;
43   Lib3dsFloat dt,fp,fn;
44
45   if (!pc) {
46     pc=c;
47   }
48   if (!nc) {
49     nc=c;
50   }
51   
52   fp=fn=1.0f;
53   if (p&&n) {
54     dt=0.5f*(Lib3dsFloat)(pc->frame-p->frame+n->frame-nc->frame);
55     fp=((Lib3dsFloat)(pc->frame-p->frame))/dt;
56     fn=((Lib3dsFloat)(n->frame-nc->frame))/dt;
57     cc=(Lib3dsFloat)fabs(c->cont);
58     fp=fp+cc-cc*fp;
59     fn=fn+cc-cc*fn;
60   }
61
62   cm=1.0f-c->cont;
63   tm=0.5f*(1.0f-c->tens);
64   cp=2.0f-cm;
65   bm=1.0f-c->bias;
66   bp=2.0f-bm;      
67   tmcm=tm*cm;
68   tmcp=tm*cp;
69   *ksm=tmcm*bp*fp;
70   *ksp=tmcp*bm*fp;
71   *kdm=tmcp*bp*fn;
72   *kdp=tmcm*bm*fn;
73 }
74
75
76 /*!
77  * \ingroup tcb 
78  */
79 Lib3dsBool
80 lib3ds_tcb_read(Lib3dsTcb *tcb, Lib3dsIo *io)
81 {
82   Lib3dsWord flags;
83   
84   tcb->frame=lib3ds_io_read_intd(io);
85   tcb->flags=flags=lib3ds_io_read_word(io);
86   if (flags&LIB3DS_USE_TENSION) {
87     tcb->tens=lib3ds_io_read_float(io);
88   }
89   if (flags&LIB3DS_USE_CONTINUITY) {
90     tcb->cont=lib3ds_io_read_float(io);
91   }
92   if (flags&LIB3DS_USE_BIAS) {
93     tcb->bias=lib3ds_io_read_float(io);
94   }
95   if (flags&LIB3DS_USE_EASE_TO) {
96     tcb->ease_to=lib3ds_io_read_float(io);
97   }
98   if (flags&LIB3DS_USE_EASE_FROM) {
99     tcb->ease_from=lib3ds_io_read_float(io);
100   }
101   if (lib3ds_io_error(io)) {
102     return(LIB3DS_FALSE);
103   }
104   return(LIB3DS_TRUE);
105 }
106
107
108 /*!
109  * \ingroup tcb 
110  */
111 Lib3dsBool
112 lib3ds_tcb_write(Lib3dsTcb *tcb, Lib3dsIo *io)
113 {
114   lib3ds_io_write_intd(io, tcb->frame);
115   lib3ds_io_write_word(io, tcb->flags);
116   if (tcb->flags&LIB3DS_USE_TENSION) {
117     lib3ds_io_write_float(io, tcb->tens);
118   }
119   if (tcb->flags&LIB3DS_USE_CONTINUITY) {
120     lib3ds_io_write_float(io, tcb->cont);
121   }
122   if (tcb->flags&LIB3DS_USE_BIAS) {
123     lib3ds_io_write_float(io, tcb->bias);
124   }
125   if (tcb->flags&LIB3DS_USE_EASE_TO) {
126     lib3ds_io_write_float(io, tcb->ease_to);
127   }
128   if (tcb->flags&LIB3DS_USE_EASE_FROM) {
129     lib3ds_io_write_float(io, tcb->ease_from);
130   }
131   if (lib3ds_io_error(io)) {
132     return(LIB3DS_FALSE);
133   }
134   return(LIB3DS_TRUE);
135 }
136
137
138
139