1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 /** \file
23  * \ingroup bke
24  */
25 
26 struct Depsgraph;
27 struct ID;
28 struct ListBase;
29 struct Object;
30 struct Scene;
31 struct bConstraint;
32 struct bConstraintTarget;
33 struct bPoseChannel;
34 
35 /* ---------------------------------------------------------------------------- */
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /* special struct for use in constraint evaluation */
41 typedef struct bConstraintOb {
42   /** to get evaluated armature. */
43   struct Depsgraph *depsgraph;
44   /** for system time, part of deglobalization, code nicer later with local time (ton) */
45   struct Scene *scene;
46   /** if pchan, then armature that it comes from, otherwise constraint owner */
47   struct Object *ob;
48   /** pose channel that owns the constraints being evaluated */
49   struct bPoseChannel *pchan;
50 
51   /** matrix where constraints are accumulated + solved */
52   float matrix[4][4];
53   /** original matrix (before constraint solving) */
54   float startmat[4][4];
55 
56   /** type of owner  */
57   short type;
58   /** rotation order for constraint owner (as defined in eEulerRotationOrders in BLI_math.h) */
59   short rotOrder;
60 } bConstraintOb;
61 
62 /* ---------------------------------------------------------------------------- */
63 
64 /* Callback format for performing operations on ID-pointers for Constraints */
65 typedef void (*ConstraintIDFunc)(struct bConstraint *con,
66                                  struct ID **idpoin,
67                                  bool is_reference,
68                                  void *userdata);
69 
70 /* ....... */
71 
72 /**
73  * Constraint Type-Info (shorthand in code = cti):
74  * This struct provides function pointers for runtime, so that functions can be
75  * written more generally (with fewer/no special exceptions for various constraints).
76  *
77  * Callers of these functions must check that they actually point to something useful,
78  * as some constraints don't define some of these.
79  *
80  * Warning:
81  * it is not too advisable to reorder order of members of this struct,
82  * as you'll have to edit quite a few #NUM_CONSTRAINT_TYPES of these
83  * structs.
84  */
85 typedef struct bConstraintTypeInfo {
86   /* admin/ident */
87   /** CONSTRAINT_TYPE_### */
88   short type;
89   /** size in bytes of the struct */
90   short size;
91   /** name of constraint in interface */
92   char name[32];
93   /** name of struct for SDNA */
94   char structName[32];
95 
96   /* data management function pointers - special handling */
97   /** free any data that is allocated separately (optional) */
98   void (*free_data)(struct bConstraint *con);
99   /** run the provided callback function on all the ID-blocks linked to the constraint */
100   void (*id_looper)(struct bConstraint *con, ConstraintIDFunc func, void *userdata);
101   /** copy any special data that is allocated separately (optional) */
102   void (*copy_data)(struct bConstraint *con, struct bConstraint *src);
103   /**
104    * Set settings for data that will be used for #bConstraint.data
105    * (memory already allocated using #MEM_callocN).
106    */
107   void (*new_data)(void *cdata);
108 
109   /* target handling function pointers */
110   /**
111    * For multi-target constraints: return that list;
112    * otherwise make a temporary list (returns number of targets).
113    */
114   int (*get_constraint_targets)(struct bConstraint *con, struct ListBase *list);
115   /**
116    * For single-target constraints only:
117    * flush data back to source data, and the free memory used.
118    */
119   void (*flush_constraint_targets)(struct bConstraint *con, struct ListBase *list, bool no_copy);
120 
121   /* evaluation */
122   /** set the ct->matrix for the given constraint target (at the given ctime) */
123   void (*get_target_matrix)(struct Depsgraph *depsgraph,
124                             struct bConstraint *con,
125                             struct bConstraintOb *cob,
126                             struct bConstraintTarget *ct,
127                             float ctime);
128   /** evaluate the constraint for the given time */
129   void (*evaluate_constraint)(struct bConstraint *con,
130                               struct bConstraintOb *cob,
131                               struct ListBase *targets);
132 } bConstraintTypeInfo;
133 
134 /* Function Prototypes for bConstraintTypeInfo's */
135 const bConstraintTypeInfo *BKE_constraint_typeinfo_get(struct bConstraint *con);
136 const bConstraintTypeInfo *BKE_constraint_typeinfo_from_type(int type);
137 
138 /* ---------------------------------------------------------------------------- */
139 
140 /* Constraint function prototypes */
141 void BKE_constraint_unique_name(struct bConstraint *con, struct ListBase *list);
142 
143 struct bConstraint *BKE_constraint_duplicate_ex(struct bConstraint *src,
144                                                 const int flag,
145                                                 const bool do_extern);
146 
147 struct bConstraint *BKE_constraint_copy_for_pose(struct Object *ob,
148                                                  struct bPoseChannel *pchan,
149                                                  struct bConstraint *src);
150 struct bConstraint *BKE_constraint_copy_for_object(struct Object *ob, struct bConstraint *src);
151 
152 void BKE_constraints_free(struct ListBase *list);
153 void BKE_constraints_free_ex(struct ListBase *list, bool do_id_user);
154 void BKE_constraints_copy(struct ListBase *dst, const struct ListBase *src, bool do_extern);
155 void BKE_constraints_copy_ex(struct ListBase *dst,
156                              const struct ListBase *src,
157                              const int flag,
158                              bool do_extern);
159 void BKE_constraints_id_loop(struct ListBase *list, ConstraintIDFunc func, void *userdata);
160 void BKE_constraint_free_data(struct bConstraint *con);
161 void BKE_constraint_free_data_ex(struct bConstraint *con, bool do_id_user);
162 
163 bool BKE_constraint_target_uses_bbone(struct bConstraint *con, struct bConstraintTarget *ct);
164 
165 /* Constraint API function prototypes */
166 struct bConstraint *BKE_constraints_active_get(struct ListBase *list);
167 void BKE_constraints_active_set(ListBase *list, struct bConstraint *con);
168 struct bConstraint *BKE_constraints_find_name(struct ListBase *list, const char *name);
169 
170 struct bConstraint *BKE_constraint_find_from_target(struct Object *ob,
171                                                     struct bConstraintTarget *tgt,
172                                                     struct bPoseChannel **r_pchan);
173 
174 struct bConstraint *BKE_constraint_add_for_object(struct Object *ob, const char *name, short type);
175 struct bConstraint *BKE_constraint_add_for_pose(struct Object *ob,
176                                                 struct bPoseChannel *pchan,
177                                                 const char *name,
178                                                 short type);
179 
180 bool BKE_constraint_remove_ex(ListBase *list,
181                               struct Object *ob,
182                               struct bConstraint *con,
183                               bool clear_dep);
184 bool BKE_constraint_remove(ListBase *list, struct bConstraint *con);
185 
186 /* Constraints + Proxies function prototypes */
187 void BKE_constraints_proxylocal_extract(struct ListBase *dst, struct ListBase *src);
188 bool BKE_constraints_proxylocked_owner(struct Object *ob, struct bPoseChannel *pchan);
189 
190 /* Constraint Evaluation function prototypes */
191 struct bConstraintOb *BKE_constraints_make_evalob(struct Depsgraph *depsgraph,
192                                                   struct Scene *scene,
193                                                   struct Object *ob,
194                                                   void *subdata,
195                                                   short datatype);
196 void BKE_constraints_clear_evalob(struct bConstraintOb *cob);
197 
198 void BKE_constraint_mat_convertspace(struct Object *ob,
199                                      struct bPoseChannel *pchan,
200                                      float mat[4][4],
201                                      short from,
202                                      short to,
203                                      const bool keep_scale);
204 
205 void BKE_constraint_target_matrix_get(struct Depsgraph *depsgraph,
206                                       struct Scene *scene,
207                                       struct bConstraint *con,
208                                       int index,
209                                       short ownertype,
210                                       void *ownerdata,
211                                       float mat[4][4],
212                                       float ctime);
213 void BKE_constraint_targets_for_solving_get(struct Depsgraph *depsgraph,
214                                             struct bConstraint *con,
215                                             struct bConstraintOb *ob,
216                                             struct ListBase *targets,
217                                             float ctime);
218 void BKE_constraints_solve(struct Depsgraph *depsgraph,
219                            struct ListBase *conlist,
220                            struct bConstraintOb *cob,
221                            float ctime);
222 
223 #ifdef __cplusplus
224 }
225 #endif
226