1 /*
2  * The 3D Studio File Format Library
3  * Copyright (C) 1996-2007 by Jan Eric Kyprianidis <www.kyprianidis.com>
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.11 2007/06/15 09:33:19 jeh Exp $
21  */
22 #include <lib3ds/tcb.h>
23 #include <lib3ds/io.h>
24 #include <math.h>
25 
26 
27 /*!
28  * \defgroup tcb Tension/Continuity/Bias Splines
29  */
30 
31 
32 /*!
33  * \ingroup tcb
34  */
35 void
lib3ds_tcb(Lib3dsTcb * p,Lib3dsTcb * pc,Lib3dsTcb * c,Lib3dsTcb * nc,Lib3dsTcb * n,Lib3dsFloat * ksm,Lib3dsFloat * ksp,Lib3dsFloat * kdm,Lib3dsFloat * kdp)36 lib3ds_tcb(Lib3dsTcb *p, Lib3dsTcb *pc, Lib3dsTcb *c, Lib3dsTcb *nc, Lib3dsTcb *n,
37   Lib3dsFloat *ksm, Lib3dsFloat *ksp, Lib3dsFloat *kdm, Lib3dsFloat *kdp)
38 {
39   Lib3dsFloat tm,cm,cp,bm,bp,tmcm,tmcp,cc;
40   Lib3dsFloat dt,fp,fn;
41 
42   if (!pc) {
43     pc=c;
44   }
45   if (!nc) {
46     nc=c;
47   }
48 
49   fp=fn=1.0f;
50   if (p&&n) {
51     dt=0.5f*(Lib3dsFloat)(pc->frame-p->frame+n->frame-nc->frame);
52     fp=((Lib3dsFloat)(pc->frame-p->frame))/dt;
53     fn=((Lib3dsFloat)(n->frame-nc->frame))/dt;
54     cc=(Lib3dsFloat)fabs(c->cont);
55     fp=fp+cc-cc*fp;
56     fn=fn+cc-cc*fn;
57   }
58 
59   cm=1.0f-c->cont;
60   tm=0.5f*(1.0f-c->tens);
61   cp=2.0f-cm;
62   bm=1.0f-c->bias;
63   bp=2.0f-bm;
64   tmcm=tm*cm;
65   tmcp=tm*cp;
66   *ksm=tmcm*bp*fp;
67   *ksp=tmcp*bm*fp;
68   *kdm=tmcp*bp*fn;
69   *kdp=tmcm*bm*fn;
70 }
71 
72 
73 /*!
74  * \ingroup tcb
75  */
76 Lib3dsBool
lib3ds_tcb_read(Lib3dsTcb * tcb,Lib3dsIo * io)77 lib3ds_tcb_read(Lib3dsTcb *tcb, Lib3dsIo *io)
78 {
79   Lib3dsWord flags;
80 
81   tcb->frame=lib3ds_io_read_intd(io);
82   tcb->flags=flags=lib3ds_io_read_word(io);
83   if (flags&LIB3DS_USE_TENSION) {
84     tcb->tens=lib3ds_io_read_float(io);
85   }
86   if (flags&LIB3DS_USE_CONTINUITY) {
87     tcb->cont=lib3ds_io_read_float(io);
88   }
89   if (flags&LIB3DS_USE_BIAS) {
90     tcb->bias=lib3ds_io_read_float(io);
91   }
92   if (flags&LIB3DS_USE_EASE_TO) {
93     tcb->ease_to=lib3ds_io_read_float(io);
94   }
95   if (flags&LIB3DS_USE_EASE_FROM) {
96     tcb->ease_from=lib3ds_io_read_float(io);
97   }
98   if (lib3ds_io_error(io)) {
99     return(LIB3DS_FALSE);
100   }
101   return(LIB3DS_TRUE);
102 }
103 
104 
105 /*!
106  * \ingroup tcb
107  */
108 Lib3dsBool
lib3ds_tcb_write(Lib3dsTcb * tcb,Lib3dsIo * io)109 lib3ds_tcb_write(Lib3dsTcb *tcb, Lib3dsIo *io)
110 {
111   lib3ds_io_write_intd(io, tcb->frame);
112   lib3ds_io_write_word(io, tcb->flags);
113   if (tcb->flags&LIB3DS_USE_TENSION) {
114     lib3ds_io_write_float(io, tcb->tens);
115   }
116   if (tcb->flags&LIB3DS_USE_CONTINUITY) {
117     lib3ds_io_write_float(io, tcb->cont);
118   }
119   if (tcb->flags&LIB3DS_USE_BIAS) {
120     lib3ds_io_write_float(io, tcb->bias);
121   }
122   if (tcb->flags&LIB3DS_USE_EASE_TO) {
123     lib3ds_io_write_float(io, tcb->ease_to);
124   }
125   if (tcb->flags&LIB3DS_USE_EASE_FROM) {
126     lib3ds_io_write_float(io, tcb->ease_from);
127   }
128   if (lib3ds_io_error(io)) {
129     return(LIB3DS_FALSE);
130   }
131   return(LIB3DS_TRUE);
132 }
133 
134 
135 
136 
137