Switch to unified view

a b/Tools/AnyMocap/AutomaticInitialPositionOfBody.any
1
#ifndef _ANYMOCAP_AUTOMATIC_INITIAL_POSITION_OF_BODY_ANY_
2
#define _ANYMOCAP_AUTOMATIC_INITIAL_POSITION_OF_BODY_ANY_
3
4
#class_template AutoPelvisPos(
5
RASIS=RASI,
6
LASIS=LASI, 
7
BACK=RPSI,
8
DIRECT_PELVIS_POS=0,
9
C3D_OBJECT=Main.ModelSetup.C3DFileData,
10
TSTART = Main.Studies.InverseDynamicStudy.tStart,
11
MANNEQUIN_FOLDER = Main.HumanModel.Mannequin
12
) {
13
    #if DIRECT_PELVIS_POS == 0
14
      #var AnyVar tStart = TSTART;      
15
      
16
      // Calculate the index in the C3D data which corresponds to the tStart  variable.
17
      AnyFloat tStartC3D = C3D_OBJECT.Header.FirstFrameNo/C3D_OBJECT.Header.VideoFrameRate;
18
      AnyFloat tEndC3D = (C3D_OBJECT.Header.LastFrameNo+1)/C3D_OBJECT.Header.VideoFrameRate;
19
      AnyInt nC3d = C3D_OBJECT.Header.LastFrameNo-C3D_OBJECT.Header.FirstFrameNo+1;
20
      AnyFloat closest_idx = (nC3d)/(tEndC3D-tStartC3D)*(tStart -tStartC3D);
21
      AnyInt start_idx = round( max({0.0,  min({closest_idx, nC3d } )}));
22
      
23
      // construct the position vector and rotation matrix from the values in the C3D file.
24
      #var AnyVec3 Pelvis_RASIS = C3D_OBJECT.Points.Markers.RASIS.Pos[start_idx];
25
      #var AnyVec3 Pelvis_LASIS = C3D_OBJECT.Points.Markers.LASIS.Pos[start_idx];
26
      #var AnyVec3 Pelvis_Back = C3D_OBJECT.Points.Markers.BACK.Pos[start_idx];
27
    
28
      #var AnyVec3 PelvisPos = 0.5*(Pelvis_RASIS + Pelvis_LASIS) - 0.02*PelvisRotMat'[0] - 0.01*PelvisRotMat'[1];
29
      #var AnyMat33 PelvisRotMat = RotMat(Pelvis_LASIS, Pelvis_RASIS, Pelvis_Back)
30
                              * RotMat(-pi/2,x)
31
                              * RotMat(pi/2,y)*RotMat(11*pi/180,z);
32
    #else
33
      #var AnyVec3 PelvisPos;
34
      #var AnyMat33 PelvisRotMat;
35
      
36
    #endif
37
                            
38
    AnyFolder PelvisRotations = 
39
    {
40
       AnyVar yRot1 = asin(-.PelvisRotMat[2][0]);
41
       AnyVar yRot2 = pi - yRot1;
42
       
43
       AnyVar zRot1 = atan2(.PelvisRotMat[1][0]/cos(yRot1), .PelvisRotMat[0][0]/cos(yRot1));
44
       AnyVar zRot2 = atan2(.PelvisRotMat[1][0]/cos(yRot2), .PelvisRotMat[0][0]/cos(yRot2));
45
       
46
       AnyVar xRot1 = atan2(.PelvisRotMat[2][1]/cos(yRot1),.PelvisRotMat[2][2]/cos(yRot1));
47
       AnyVar xRot2 = atan2(.PelvisRotMat[2][1]/cos(yRot2),.PelvisRotMat[2][2]/cos(yRot2));
48
       AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
49
       AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
50
       
51
       AnyVar zRot = iffun(gtfun(abs1, abs2), zRot2, zRot1);
52
       AnyVar yRot = iffun(gtfun(abs1, abs2), yRot2, yRot1);
53
       AnyVar xRot = iffun(gtfun(abs1, abs2), xRot2, xRot1);
54
55
       // Check that the auto pos can reconstruct the rotation matrix.
56
       AnyFloat err = abs(.PelvisRotMat-RotMat(zRot,z)*RotMat(yRot,y)*RotMat(xRot,x));
57
       AnyInt AutoPosAssert = expect(ltfun(err,1e-5), "AutoPos failed to calculate the correct values");
58
     };
59
     
60
       
61
    AnyFolder &InitPosRef= MANNEQUIN_FOLDER.Posture;    
62
    
63
    InitPosRef= 
64
    {
65
      PelvisPosX = DesignVar(.PelvisPos[0]);
66
      PelvisPosY = DesignVar(.PelvisPos[1]);
67
      PelvisPosZ = DesignVar(.PelvisPos[2]);
68
      
69
      
70
      PelvisRotX = DesignVar(.PelvisRotations.xRot*180/pi);
71
      PelvisRotY = DesignVar(.PelvisRotations.yRot*180/pi);
72
      PelvisRotZ = DesignVar(.PelvisRotations.zRot*180/pi);
73
    };
74
};
75
76
#class_template AutomaticFullBodyInitialPosition (
77
MANNEQUIN_FOLDER = Main.HumanModel.Mannequin
78
) {
79
80
#var AnyVec3 RASIS = {-0.13,1,0};
81
#var AnyVec3 LASIS = {0.13,1,0};
82
#var AnyVec3 SACRUM = {0,1,-0.14} ;
83
84
 #var AnyVec3 PelvisPos = DesignVar((1/3)*( RASIS + LASIS + SACRUM ));
85
 #var AnyMat33 PelvisRotMat = RotMat(SACRUM , 
86
                                    0.5*(LASIS +RASIS ),
87
                                    LASIS )*RotMat(pi/2,x);  
88
89
90
#var AnyVec3 RKNEE = RightHip +{0,-0.5,0}*PelvisRotMat';
91
#var AnyVec3 LKNEE= LeftHip +{0,-0.5,0} *PelvisRotMat';
92
#var AnyVec3 RANKLE= RKNEE + {0,-0.5,0}*RightThighRotation';
93
#var AnyVec3 LANKLE= LKNEE + {0,-0.5,0}*LeftThighRotation';
94
#var AnyVec3 RTOE= RANKLE + {0.21,-0.05,0}*RightShankRotation';
95
#var AnyVec3 LTOE= LANKLE + {0.21,-0.05,0}*LeftShankRotation';
96
97
#var AnyVec3 RSHOULDER= PelvisPos +{0,0.6,0.15}*PelvisRotMat';
98
#var AnyVec3 LSHOULDER= PelvisPos +{0,0.6,-0.15}*PelvisRotMat';
99
  
100
101
102
103
104
  
105
                            
106
    AnyFolder PelvisRotations = 
107
    {
108
       AnyVar yRot1 = asin(-.PelvisRotMat[2][0]);
109
       AnyVar yRot2 = pi - yRot1;
110
       
111
       AnyVar zRot1 = atan2(.PelvisRotMat[1][0]/cos(yRot1), .PelvisRotMat[0][0]/cos(yRot1));
112
       AnyVar zRot2 = atan2(.PelvisRotMat[1][0]/cos(yRot2), .PelvisRotMat[0][0]/cos(yRot2));
113
       
114
       AnyVar xRot1 = atan2(.PelvisRotMat[2][1]/cos(yRot1),.PelvisRotMat[2][2]/cos(yRot1));
115
       AnyVar xRot2 = atan2(.PelvisRotMat[2][1]/cos(yRot2),.PelvisRotMat[2][2]/cos(yRot2));
116
       AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
117
       AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
118
       
119
       AnyVar zRot = iffun(gtfun(abs1, abs2), zRot2, zRot1);
120
       AnyVar yRot = iffun(gtfun(abs1, abs2), yRot2, yRot1);
121
       AnyVar xRot = iffun(gtfun(abs1, abs2), xRot2, xRot1);
122
123
       // Check that the auto pos can reconstruct the rotation matrix.
124
       AnyFloat err = abs(.PelvisRotMat-RotMat(zRot,z)*RotMat(yRot,y)*RotMat(xRot,x));
125
       AnyInt AutoPosAssert = expect(ltfun(err,1e-5), "AutoPos failed to calculate the correct values");
126
     };
127
     
128
     
129
      AnyVar PelvisPosX = PelvisPos[0];
130
      AnyVar PelvisPosY = PelvisPos[1];
131
      AnyVar PelvisPosZ = PelvisPos[2];
132
      // Inital Pelvis rotation 
133
      AnyVar PelvisRotX = DesignVar(PelvisRotations.xRot*180/pi);
134
      AnyVar PelvisRotY = DesignVar(PelvisRotations.yRot*180/pi);
135
      AnyVar PelvisRotZ = DesignVar(PelvisRotations.zRot*180/pi);
136
137
  
138
   AnyVec3 RightHip = PelvisPos + {0.08, -0.15, 0.12}*PelvisRotMat';
139
   AnyVec3 LeftHip = PelvisPos + {0.08, -0.15, -0.12}*PelvisRotMat';
140
                                      
141
   AnyMat33 RightThighRotation = RotMat( RKNEE, 
142
                                         RightHip , 
143
                                         RKNEE+{0,0,1}*PelvisRotMat')
144
                                        *RotMat(-pi/2,y)*RotMat(-pi/2,x);                             
145
   AnyMat33 LeftThighRotation = RotMat( LKNEE,
146
                                        LeftHip , 
147
                                        RKNEE+ {0,0,1}*PelvisRotMat')
148
                                        *RotMat(-pi/2,y)*RotMat(-pi/2,x);                             
149
   
150
   AnyMat33 RightShankRotation = RotMat(RANKLE, 
151
                                       RKNEE,
152
                                       RANKLE + {0,0,1}*RightThighRotation')
153
                                        *RotMat(-pi/2,y)*RotMat(-pi/2,x);                             
154
   AnyMat33 LeftShankRotation = RotMat( LANKLE, 
155
                                       LKNEE,
156
                                       LANKLE + {0,0,1}*LeftThighRotation')
157
                                        *RotMat(-pi/2,y)*RotMat(-pi/2,x);                             
158
   AnyMat33 RightFootRotation = RotMat(RANKLE, 
159
                                       RTOE,
160
                                       RKNEE);
161
   AnyMat33 LeftFootRotation = RotMat( LANKLE, 
162
                                       LTOE,
163
                                       LKNEE );
164
                                       
165
   // Calculation of Hip Angles
166
   AnyFolder RightHipRotations = {
167
     AnyMat33 RotRel = .PelvisRotMat'*.RightThighRotation ;
168
     AnyVar xRot1 = asin(RotRel[2][1]);
169
     AnyVar xRot2 = pi-xRot1;
170
     AnyVar yRot1 = atan2(-RotRel[2][0]/cos(xRot1 ),RotRel[2][2]/cos(xRot1));
171
     AnyVar yRot2 = atan2(-RotRel[2][0]/cos(xRot2 ),RotRel[2][2]/cos(xRot2));
172
     AnyVar zRot1 = atan2(-RotRel[0][1]/cos(yRot1),RotRel[1][1]/cos(yRot1));
173
     AnyVar zRot2 = atan2(-RotRel[0][1]/cos(yRot2),RotRel[1][1]/cos(yRot2));
174
     AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
175
     AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
176
     AnyVar zRot = iffun(gtfun(abs1,abs2), zRot2,zRot1);
177
     AnyVar yRot = iffun(gtfun(abs1,abs2), yRot2,yRot1);
178
     AnyVar xRot = iffun(gtfun(abs1,abs2), xRot2,xRot1) ;
179
   };
180
   AnyFolder LeftHipRotations = {
181
     AnyMat33 RotRel = .PelvisRotMat'*.LeftThighRotation ;
182
     AnyVar xRot1 = asin(RotRel[2][1]);
183
     AnyVar xRot2 = pi-xRot1;
184
     AnyVar yRot1 = atan2(-RotRel[2][0]/cos(xRot1 ),RotRel[2][2]/cos(xRot1));
185
     AnyVar yRot2 = atan2(-RotRel[2][0]/cos(xRot2 ),RotRel[2][2]/cos(xRot2));
186
     AnyVar zRot1 = atan2(-RotRel[0][1]/cos(yRot1),RotRel[1][1]/cos(yRot1));
187
     AnyVar zRot2 = atan2(-RotRel[0][1]/cos(yRot2),RotRel[1][1]/cos(yRot2));
188
     AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
189
     AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
190
     AnyVar zRot = iffun(gtfun(abs1,abs2), zRot2,zRot1);
191
     AnyVar yRot = iffun(gtfun(abs1,abs2), yRot2,yRot1);
192
     AnyVar xRot = iffun(gtfun(abs1,abs2), xRot2,xRot1) ;
193
   };
194
   AnyFolder RightKneeRotations = {
195
     AnyMat33 RotRel = .RightThighRotation' *.RightShankRotation ;
196
     AnyVar xRot1 = asin(RotRel[2][1]);
197
     AnyVar xRot2 = pi-xRot1;
198
     AnyVar yRot1 = atan2(-RotRel[2][0]/cos(xRot1 ),RotRel[2][2]/cos(xRot1));
199
     AnyVar yRot2 = atan2(-RotRel[2][0]/cos(xRot2 ),RotRel[2][2]/cos(xRot2));
200
     AnyVar zRot1 = atan2(-RotRel[0][1]/cos(yRot1),RotRel[1][1]/cos(yRot1));
201
     AnyVar zRot2 = atan2(-RotRel[0][1]/cos(yRot2),RotRel[1][1]/cos(yRot2));
202
     AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
203
     AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
204
     AnyVar zRot = iffun(gtfun(abs1,abs2), zRot2,zRot1);
205
     AnyVar yRot = iffun(gtfun(abs1,abs2), yRot2,yRot1);
206
     AnyVar xRot = iffun(gtfun(abs1,abs2), xRot2,xRot1) ;
207
   };
208
   AnyFolder LeftKneeRotations = {
209
     AnyMat33 RotRel = .LeftThighRotation' *.LeftShankRotation ;
210
     AnyVar xRot1 = asin(RotRel[2][1]);
211
     AnyVar xRot2 = pi-xRot1;
212
     AnyVar yRot1 = atan2(-RotRel[2][0]/cos(xRot1 ),RotRel[2][2]/cos(xRot1));
213
     AnyVar yRot2 = atan2(-RotRel[2][0]/cos(xRot2 ),RotRel[2][2]/cos(xRot2));
214
     AnyVar zRot1 = atan2(-RotRel[0][1]/cos(yRot1),RotRel[1][1]/cos(yRot1));
215
     AnyVar zRot2 = atan2(-RotRel[0][1]/cos(yRot2),RotRel[1][1]/cos(yRot2));
216
     AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
217
     AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
218
     AnyVar zRot = iffun(gtfun(abs1,abs2), zRot2,zRot1);
219
     AnyVar yRot = iffun(gtfun(abs1,abs2), yRot2,yRot1);
220
     AnyVar xRot = iffun(gtfun(abs1,abs2), xRot2,xRot1) ;
221
   };
222
   AnyFolder RightAnkleRotations = {
223
     AnyMat33 RotRel = .RightShankRotation' *.RightFootRotation ;
224
     AnyVar xRot1 = asin(RotRel[2][1]);
225
     AnyVar xRot2 = pi-xRot1;
226
     AnyVar yRot1 = atan2(-RotRel[2][0]/cos(xRot1 ),RotRel[2][2]/cos(xRot1));
227
     AnyVar yRot2 = atan2(-RotRel[2][0]/cos(xRot2 ),RotRel[2][2]/cos(xRot2));
228
     AnyVar zRot1 = atan2(-RotRel[0][1]/cos(yRot1),RotRel[1][1]/cos(yRot1));
229
     AnyVar zRot2 = atan2(-RotRel[0][1]/cos(yRot2),RotRel[1][1]/cos(yRot2));
230
     AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
231
     AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
232
     AnyVar zRot = iffun(gtfun(abs1,abs2), zRot2,zRot1);
233
     AnyVar yRot = iffun(gtfun(abs1,abs2), yRot2,yRot1);
234
     AnyVar xRot = iffun(gtfun(abs1,abs2), xRot2,xRot1) ;
235
   };
236
   AnyFolder LeftAnkleRotations = {
237
     AnyMat33 RotRel = .LeftShankRotation' *.LeftFootRotation ;
238
     AnyVar xRot1 = asin(RotRel[2][1]);
239
     AnyVar xRot2 = pi-xRot1;
240
     AnyVar yRot1 = atan2(-RotRel[2][0]/cos(xRot1 ),RotRel[2][2]/cos(xRot1));
241
     AnyVar yRot2 = atan2(-RotRel[2][0]/cos(xRot2 ),RotRel[2][2]/cos(xRot2));
242
     AnyVar zRot1 = atan2(-RotRel[0][1]/cos(yRot1),RotRel[1][1]/cos(yRot1));
243
     AnyVar zRot2 = atan2(-RotRel[0][1]/cos(yRot2),RotRel[1][1]/cos(yRot2));
244
     AnyVar abs1 = abs(zRot1)+abs(yRot1)+abs(zRot1);
245
     AnyVar abs2 = abs(zRot2)+abs(yRot2)+abs(zRot2);
246
     AnyVar zRot = iffun(gtfun(abs1,abs2), zRot2,zRot1);
247
     AnyVar yRot = iffun(gtfun(abs1,abs2), yRot2,yRot1);
248
     AnyVar xRot = iffun(gtfun(abs1,abs2), xRot2,xRot1) ;
249
   };   
250
   
251
                                       
252
   #var AnyVar PelvisThoraxExtension = 0;
253
   #var AnyVar PelvisThoraxLateralBending = 0;
254
   #var AnyVar PelvisThoraxRotation = 0;
255
   
256
   #var AnyVar NeckExtension = 0;
257
   #var AnyVar NeckLateralBending = 0;
258
   #var AnyVar NeckRotation = 0;
259
260
AnyFolder Right = {
261
  
262
  #var AnyVar HipFlexion = .RightHipRotations.zRot*180/pi;
263
  #var AnyVar HipAbduction = -.RightHipRotations.xRot*180/pi;
264
  #var AnyVar HipExternalRotation = 0;
265
  #var AnyVar KneeFlexion = -.RightKneeRotations.zRot*180/pi;
266
  #var AnyVar AnklePlantarFlexion = -.RightAnkleRotations.zRot*180/pi;
267
  #var AnyVar SubTalarEversion = 0;
268
  
269
  #var AnyVar GlenohumeralFlexion = 0;
270
  #var AnyVar GlenohumeralAbduction = 1;
271
  #var AnyVar GlenohumeralExternalRotation = 0;
272
  #var AnyVar ElbowFlexion = 0;
273
  #var AnyVar ElbowPronation = 0;
274
  #var AnyVar WristFlexion = 0;
275
  #var AnyVar WristAbduction = 0;
276
  
277
};
278
AnyFolder Left = {
279
  
280
  #var AnyVar HipFlexion = .LeftHipRotations.zRot*180/pi;
281
  #var AnyVar HipAbduction = .LeftHipRotations.xRot*180/pi;
282
  #var AnyVar HipExternalRotation = 0;
283
  #var AnyVar KneeFlexion = -.LeftKneeRotations.zRot*180/pi;
284
  #var AnyVar AnklePlantarFlexion = -.LeftAnkleRotations.zRot*180/pi;
285
  #var AnyVar SubTalarEversion = 0;
286
        
287
  #var AnyVar GlenohumeralFlexion = 0;
288
  #var AnyVar GlenohumeralAbduction = 1;
289
  #var AnyVar GlenohumeralExternalRotation = 0;
290
  #var AnyVar ElbowFlexion = 0;
291
  #var AnyVar ElbowPronation = 0;
292
  #var AnyVar WristFlexion = 0;
293
  #var AnyVar WristAbduction = 0;
294
295
  
296
};
297
298
299
AnyFolder &InitPosRef= MANNEQUIN_FOLDER.Posture;    
300
301
302
InitPosRef= {
303
                                         
304
305
PelvisPosX = .PelvisPosX;
306
PelvisPosY = .PelvisPosY;
307
PelvisPosZ = .PelvisPosZ;
308
// Inital Pelvis rotation 
309
PelvisRotX = .PelvisRotX;
310
PelvisRotY = .PelvisRotY;
311
PelvisRotZ = .PelvisRotZ;
312
313
PelvisThoraxExtension = .PelvisThoraxExtension;
314
PelvisThoraxLateralBending = .PelvisThoraxLateralBending ;
315
PelvisThoraxRotation = .PelvisThoraxRotation ;
316
317
NeckExtension = .NeckExtension ;
318
NeckLateralBending = .NeckLateralBending ;
319
NeckRotation = .NeckRotation ;
320
321
Right = {
322
  
323
  HipFlexion = ..Right.HipFlexion ;
324
  HipAbduction = ..Right.HipAbduction ;
325
  HipExternalRotation = ..Right.HipExternalRotation ;
326
  KneeFlexion = ..Right.KneeFlexion ;
327
  AnklePlantarFlexion = ..Right.AnklePlantarFlexion ;
328
  SubTalarEversion = ..Right.SubTalarEversion ;
329
  
330
  GlenohumeralFlexion = ..Right.GlenohumeralFlexion ;
331
  GlenohumeralAbduction = ..Right.GlenohumeralAbduction ;
332
  GlenohumeralExternalRotation = ..Right.GlenohumeralExternalRotation ;
333
  ElbowFlexion = ..Right.ElbowFlexion ;
334
  ElbowPronation = ..Right.ElbowPronation ;
335
  WristFlexion = ..Right.WristFlexion ;
336
  WristAbduction = ..Right.WristAbduction ;
337
  
338
};
339
Left = {
340
  
341
  HipFlexion = ..Left.HipFlexion ;
342
  HipAbduction = ..Left.HipAbduction ;
343
  HipExternalRotation = ..Left.HipExternalRotation ;
344
  KneeFlexion = ..Left.KneeFlexion ;
345
  AnklePlantarFlexion = ..Left.AnklePlantarFlexion ;
346
  SubTalarEversion = ..Left.SubTalarEversion ;
347
  
348
  GlenohumeralFlexion = ..Left.GlenohumeralFlexion ;
349
  GlenohumeralAbduction = ..Left.GlenohumeralAbduction ;
350
  GlenohumeralExternalRotation = ..Left.GlenohumeralExternalRotation ;
351
  ElbowFlexion = ..Left.ElbowFlexion ;
352
  ElbowPronation = ..Left.ElbowPronation ;
353
  WristFlexion = ..Left.WristFlexion ;
354
  WristAbduction = ..Left.WristAbduction ;
355
356
  
357
};
358
359
360
};// InitPosRef
361
362
//#var AnyVar ThighLengthIni = 0.5*(vnorm(RightHip-RKNEE)+vnorm(LeftHip-LKNEE));
363
//#var AnyVar ShankLengthIni = 0.5*(vnorm(RKNEE-RANKLE)+vnorm(LKNEE-LANKLE));
364
//
365
//
366
//AnyFolder &IniSegmentLength = Main.ModelSetup.SubjectSpecificData.Anthropometrics = {
367
//ThighLengthIni = .ThighLengthIni;
368
//ShankLengthIni = .ShankLengthIni;
369
//
370
//};// IniSegmentLength
371
//
372
373
374
};
375
376
#endif