Switch to unified view

a b/Tools/AnyMocap/CreateConstraintClass.any
1
#ifndef _ANYMOCAP_CREATE_CONSTRAINT_CLASS_ANY_
2
#define _ANYMOCAP_CREATE_CONSTRAINT_CLASS_ANY_
3
/*
4
---
5
group: MoCap
6
topic: Parameter Identification
7
descr: |
8
  Collection of class templates for adding constraints between 
9
  design variables in Parameter optimization studies.
10
---
11
12
To use these drivers import the file:
13
#include "<AMMR_TOOLS>/AnyMoCap/CreateConstraintClass.any"
14
15
See the individual classes for more information.
16
17
*/
18
19
20
21
// Add a constraint to AnyMoCap models between two design variables 
22
// in the parameter identification study.
23
// By default the Design variables are linked to be equal by adding a constraint 
24
// `Coef1*DesVar1 - Coef2*DesVar2 = 0`
25
//
26
// It is also possible change the link by setting `Coef1` and `Coef2`, and the
27
// `Constraint_Type` argument. 
28
// 
29
// :::{rubric} Example:
30
// :::
31
// ```{code-block} AnyScriptDoc
32
//  LinkDesignVars EqualThighLength (
33
//    Desvar1=RightThighLength,
34
//    Desvar2=LeftThighLength,
35
//  ) = {};
36
//```
37
// The constraint can be changed by changing the `Constraint_Type` argument.
38
//
39
#class_template LinkDesignVars (
40
    DesVar1,
41
    DesVar2,
42
    Constraint_Type = EqualToZero,
43
    Coef1 = 1,
44
    Coef2= 1,
45
    PARAMETER_OPT_STUDY = Main.Studies.ParameterIdentification
46
)
47
{
48
    // Arguments
49
    // ------------
50
    // LinkDesignVars#DesVar1
51
    //   The first design variable to link.
52
    // LinkDesignVars#DesVar2
53
    //   The second design variable to link.
54
    // LinkDesignVars#Constraint_Type
55
    //   The type of constraint to add between the design variables.
56
    //   Possible values are: EqualToZero, LessThanZero, GreaterThanZero
57
    // LinkDesignVars#Coef1
58
    //   The coefficient of the first design variable in the constraint equation.
59
    // LinkDesignVars#Coef2
60
    //   The coefficient of the second design variable in the constraint equation.
61
    // LinkDesignVars#PARAMETER_OPT_STUDY
62
    //   The parameter optimization study to add the constraint to.
63
64
    Main.ModelSetup.ParameterIdentification = {
65
       AnyDesMeasure LinkedDesignVars_##DesVar1##_##DesVar2 = {
66
           Type = Constraint_Type;
67
           Val = (Coef1) *.DesVar1.Val - (Coef2)*.DesVar2.Val;
68
       }; 
69
    };
70
    
71
    PARAMETER_OPT_STUDY = {
72
        AnyDesMeasure &LinkedDesignVars_##DesVar1##_##DesVar2 = Main.ModelSetup.ParameterIdentification.LinkedDesignVars_##DesVar1##_##DesVar2 ;  
73
    };  
74
}; 
75
76
77
// Small convenience class to quickly add an `AnyDesMeasure` constraint to 
78
// the `Main.ModelSetup.ParameterIdentification` folder and link it 
79
// to the parameter optimization study.
80
//
81
// :::{rubric} Example:
82
// :::
83
// Here is an example of adding a constraints which forces the pelvis width
84
// to follow the overall body scale.
85
// 
86
// :::{note} The following assumes the pelvis width is a design variable, and a model which 
87
//        uses the `ScaleXYZ` scaling law. 
88
// :::
89
//
90
// ```{code-block} AnyScriptDoc
91
// CreateConstraint ScaleHeadWithBody(
92
//   Constraint_Type = EqualToZero,
93
// ) = 
94
//{
95
//   Value = Main.HumanModel.Anthropometrics.SegmentScaleFactors.BodyScale 
96
//          - Main.HumanModel.Anthropometrics.SegmentScaleFactors.Pelvis.WidthScale;
97
// };
98
//```
99
//
100
#class_template CreateConstraint (
101
   NAME=__NOT_DEFINED__,
102
   Constraint_Type = EqualToZero, 
103
   PARAMETER_OPT_STUDY = Main.Studies.ParameterIdentification)
104
{
105
    // Arguments
106
    // CreateConstraint#Constraint_Type
107
    //  The type of constraint to add between the design variables.
108
    //  Possible values are: EqualToZero, LessThanZero, GreaterThanZero
109
    // CreateConstraint#PARAMETER_OPT_STUDY
110
    // The parameter optimization study to add the constraint to.
111
112
113
    // CreateConstraint
114
    /// The value used in constraint. This is usually set to some expression between 
115
    /// other variables. 
116
    #var AnyFloat Value;
117
  
118
    Main.ModelSetup.ParameterIdentification = {
119
    #if NAME == __NOT_DEFINED__
120
        AnyDesMeasure __NAME__##Constraint_Type =
121
    #else
122
        AnyDesMeasure NAME##Constraint_Type =
123
    #endif
124
        {
125
            Type = Constraint_Type;
126
            Val = ....Value;
127
        };  
128
  };
129
  
130
  PARAMETER_OPT_STUDY = {
131
    #if NAME == __NOT_DEFINED__
132
        AnyDesMeasure &__NAME__##Constraint_Type = Main.ModelSetup.ParameterIdentification.__NAME__##Constraint_Type ;
133
    #else
134
        AnyDesMeasure &NAME##Constraint_Type = Main.ModelSetup.ParameterIdentification.NAME##Constraint_Type ;  
135
    #endif
136
   };
137
    
138
}; 
139
140
141
// Convenience class to quickly add an constraints to markers in a cluster.
142
// The constraints ensures the markers remain in a rectangular shape.
143
//
144
// :::{note} Adding such a constraint is often not be necessary and may 
145
//     make it more difficult for the optimizer.
146
// :::
147
// 
148
// :::{rubric} Example:
149
// :::
150
//
151
// ```{code-block} AnyScriptDoc
152
// Create4clusterRectangleConstraint ThighClusterConstraint(
153
//   marker1 = RThigh1,
154
//   marker2 = RThigh2,
155
//   marker3 = RThigh3,
156
//   marker4 = RThigh4,
157
// ) = {};
158
//
159
//```
160
//
161
#class_template Create4clusterRectangleConstraint (
162
    marker1,
163
    marker2,
164
    marker3,
165
    marker4,
166
    PARAMETER_OPT_STUDY = Main.Studies.ParameterIdentification,
167
){
168
    // Arguments
169
    // ------------
170
    // Create4clusterRectangleConstraint#marker1
171
    //   The first marker in the cluster.
172
    // Create4clusterRectangleConstraint#marker2
173
    //   The second marker in the cluster.
174
    // Create4clusterRectangleConstraint#marker3
175
    //   The third marker in the cluster.
176
    // Create4clusterRectangleConstraint#marker4
177
    //   The fourth marker in the cluster.
178
    // Create4clusterRectangleConstraint#PARAMETER_OPT_STUDY
179
    //   The parameter optimization study to add the constraint to.
180
181
182
  PARAMETER_OPT_STUDY = {
183
     // equal opposing sides in the rectangular cluster
184
    AnyDesMeasure __NAME__##_SideConstraint1 = {
185
         Type = EqualToZero;
186
         Val = vnorm(marker1.sRelOptEdit-marker2.sRelOptEdit) - vnorm(marker3.sRelOptEdit-marker4.sRelOptEdit);
187
     };
188
     // equal opposing sides in the rectangular cluster
189
     AnyDesMeasure __NAME__##_SideConstraint2= {
190
         Type = EqualToZero;
191
         Val = vnorm(marker2.sRelOptEdit-marker3.sRelOptEdit) - vnorm(marker4.sRelOptEdit-marker1.sRelOptEdit);
192
     };
193
     // equal diagonals in the cluster
194
     AnyDesMeasure __NAME__##_DiagonalConstraint= {
195
         Type = EqualToZero;
196
         Val = vnorm(marker1.sRelOptEdit-marker3.sRelOptEdit) - vnorm(marker2.sRelOptEdit-marker4.sRelOptEdit);
197
     };
198
  };
199
  
200
  
201
};
202
203
#class_template Create4clusterParallelogramConstraint (name, marker1, marker2, marker3, marker4,
204
PARAMETER_OPT_STUDY = Main.Studies.ParameterIdentification){
205
  PARAMETER_OPT_STUDY = {
206
     // equal opposing sides in the rectangular cluster
207
    AnyDesMeasure name##_ParallelogramConstraint = {
208
         Type = EqualToZero;
209
         Val = vnorm(marker4.sRelOptEdit+ marker2.sRelOptEdit - marker1.sRelOptEdit - marker3.sRelOptEdit);
210
     };
211
  };
212
  
213
  
214
};
215
216
#endif