File: muscle.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (213 lines) | stat: -rw-r--r-- 5,545 bytes parent folder | download | duplicates (3)
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
/* ==========================================================================
                               MUSCLE_C
=============================================================================

    FUNCTION NAMES

    float VecLen 	-- calculates a vector length.
    float CosAng 	-- compute the cosine angle between two vectors.
    activate_muscle     -- activate a muscle.
    act_muscles 	-- activate muscles.
    reset_muscles 	-- reset all the muscles.

    C SPECIFICATIONS

    float VecLen 	( float *v )
    float CosAng 	( float *v1, float *v2 )
    activate_muscle 	( HEAD *face, float *vt, float *vh, 
                          float fstart, float fin, float ang, float val )
    act_muscles 	( HEAD *face ) 
    reset_muscles 	( HEAD *face ) 

    DESCRIPTION
	
	This module is where all the muscle action takes place.  This module 
	comes as is with no warranties.  

    SIDE EFFECTS
	Unknown.
   
    HISTORY
	Created 16-Dec-94  Keith Waters at DEC's Cambridge Research Lab.

============================================================================ */

#include <math.h>
#include <stdio.h>
#include "head.h"

#ifdef _WIN32
#pragma warning (disable:4244)	/* Disable bogus conversion warnings. */
#pragma warning (disable:4305)  /* VC++ 5.0 version of above warning. */
#endif

/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define DTOR(deg) ((deg)*0.017453292)	/* degrees to radians   */
#define RTOD(rad) ((rad)*57.29577951)   /* radians to degrees   */
#define RADF 180.0 / M_PI 


/* ======================================================================== */
/* float VecLen ( vec )							    */
/* ======================================================================== */
/*
** Caculates the length of the vector.
*/

float VecLen ( float *v )
{
  return (float) sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}

/* ======================================================================== */
/* float CosAng ( v1, v2 )						    */
/* ======================================================================== */
/*
** Rotates the facial muscles 90.0 degrees.
*/

float CosAng ( float *v1, float *v2 )
{
  float ang, a,b ;
  
  a = VecLen ( v1 ) ;
  b = VecLen ( v2 ) ;

  ang = ((v1[0]*v2[0]) + (v1[1]*v2[1] ) + (v1[2]*v2[2])) / (a*b) ;

  return ( ang ) ;
}

/* ======================================================================== */
/* activate_muscle ( face, vt, vh, fstart, fin, ang, val )		    */
/* ======================================================================== */
/*
** activate the muscle.
*/

void
activate_muscle (HEAD *face, float *vt, float *vh, float fstart,  float fin,  float ang,  float val)
{
  float newp[3], va[3], vb[3] ;
  int i,j,k,l ;
  float valen, vblen ;
  float cosa, cosv, dif, tot, percent, thet, newv, the, radf ;
  
  radf  = 180.0/ M_PI ;
  the   = ang / radf ; ;
  thet  = cos ( the ) ;

  cosa = 0.0 ;

  /* find the length of the muscle */
  for (i=0; i<3; i++)
    va[i] = vt[i] - vh[i] ;
  valen = VecLen ( va ) ;

  /* loop for all polygons */
  for (i=0; i<face->npolygons; i++) {

    /* loop for all vertices */
    for (j=0; j<3; j++) {

      /* find the length of the muscle head to the mesh node */
      for (k=0; k<3; k++)
	vb[k] = face->polygon[i]->vertex[j]->xyz[k] - vh[k] ;
      vblen = VecLen ( vb ) ;

      if ( valen > 0.0 && vblen > 0.0) {
	cosa = CosAng ( va, vb ) ;

	if ( cosa >= thet ) {
	  if ( vblen <= fin ) {
	    cosv = val * ( 1.0 - (cosa/thet) ) ;

	    if ( vblen >= fstart && vblen <= fin) {
	      dif       = vblen - fstart ;
	      tot       = fin - fstart ;
	      percent   = dif/tot ;
	      newv      = cos ( DTOR(percent*90.0) ) ;

	      for ( l=0; l<3; l++)
		newp[l] = (vb[l] * cosv) * newv ;
	    }
	    else {
	      for ( l=0; l<3; l++)
		newp[l] = vb[l] * cosv ;

	    }   /* endif vblen>fin */

	    for (l=0; l<3; l++)
	      face->polygon[i]->vertex[j]->xyz[l] += newp[l] ;
		  
	  }  /* endif vblen>fin    */
	}   /* endif cosa>thet    */
      }    /* endif mlen&&tlen   */
    }     /* end for j vertices */
  }      /* end for i polygon  */
}

/* ======================================================================== */
/* act_muscles ( face ) 						    */
/* ======================================================================== */
/*
** activate the muscles
*/

void
act_muscles ( HEAD *face ) 
{
  int i ;

  /* 
   * Loop all the muscles.             
   */ 
  for (i=0; i<face->nmuscles; i++) {

    /*
     * Check to see if the muscle is active.                        
    */
    if (face->muscle[i]->active) {

      activate_muscle ( face, face->muscle[i]->head,
		              face->muscle[i]->tail,
		              face->muscle[i]->fs,
		              face->muscle[i]->fe,
		              face->muscle[i]->zone,
 		              face->muscle[i]->mval ) ;

      /* 
      * Reset the muscle activity.     
      */
      face->muscle[i]->active = 1 ;

    }
  } 
}

/* ======================================================================== */
/* reset_muscles ( face ) 						    */
/* ======================================================================== */
/*
** Resets the muscles of the face.  This is achieved by reversing 
** the muscle contraction.
*/

void
reset_muscles ( HEAD *face ) 
{
  int i,j,k ;

  for ( i=0; i<face->npolygons; i++ ) {
    for ( j=0; j<3; j++ ) {

      for ( k=0; k<3; k++ )
	face->polygon[i]->vertex[j]->xyz[k] = 
	  face->polygon[i]->vertex[j]->nxyz[k] ;

    } /* end for j */
  } /* end for i */
}