1 /****************************************************************************
2  *                  mechsim.h
3  *
4  *  Author: Christoph Hormann <chris_hormann@gmx.de>
5  *
6  *  Mechanics simulation
7  *
8  *  Copyright 2002-2005 Christoph Hormann
9  *
10  * from Persistence of Vision(tm) Ray Tracer version 3.6.
11  * Copyright 1991-2003 Persistence of Vision Team
12  * Copyright 2003-2004 Persistence of Vision Raytracer Pty. Ltd.
13  *---------------------------------------------------------------------------
14  * NOTICE: This source code file is provided so that users may experiment
15  * with enhancements to POV-Ray and to port the software to platforms other
16  * than those supported by the POV-Ray developers. There are strict rules
17  * regarding how you are permitted to use this file. These rules are contained
18  * in the distribution and derivative versions licenses which should have been
19  * provided with this file.
20  *
21  * These licences may be found online, linked from the end-user license
22  * agreement that is located at http://www.povray.org/povlegal.html
23  *---------------------------------------------------------------------------
24  * This program is based on the popular DKB raytracer version 2.12.
25  * DKBTrace was originally written by David K. Buck.
26  * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
27  *---------------------------------------------------------------------------
28  *
29  *===========================================================================
30  * This file is part of MegaPOV, a modified and unofficial version of POV-Ray
31  * For more information on MegaPOV visit our website:
32  * http://megapov.inetart.net/
33  *===========================================================================
34  *
35  * $RCSfile: mechsim.h,v $
36  * $Revision: 1.13 $
37  * $Author: chris $
38  *
39  *****************************************************************************/
40 
41 #ifndef MECHSIM_H
42 #define MECHSIM_H
43 BEGIN_POV_NAMESPACE
44 
45 /*****************************************************************************
46 * Global preprocessor defines
47 ******************************************************************************/
48 
49 #ifndef MECHSIM_DEBUG
50 #define MECHSIM_DEBUG 0
51 #endif
52 
53 #define MECHSIM_METHOD_EULER 1
54 #define MECHSIM_METHOD_HEUN 2
55 #define MECHSIM_METHOD_RUNGE_KUTTA4 3
56 #define MECHSIM_METHOD_GRADIENT 4
57 
58 #define MECHSIM_FLAG_FIXED 1
59 
60 #define MECHSIM_COLLISION_NONE 0
61 #define MECHSIM_COLLISION_ALL 1
62 #define MECHSIM_COLLISION_GROUP 2
63 
64 #define MECHSIM_ENV_METHOD_FORCE 1
65 #define MECHSIM_ENV_METHOD_IMPACT 2
66 
67 #define MECHSIM_FACE_BOUNDING_AUTO 0
68 #define MECHSIM_FACE_BOUNDING_NO 1
69 #define MECHSIM_FACE_BOUNDING_BBOX 2
70 #define MECHSIM_FACE_BOUNDING_HASH 3
71 
72 /*****************************************************************************
73 * Global typedefs
74 ******************************************************************************/
75 
76 typedef int INT_VECT[3];
77 
78 /* ------------------------------------------------------ */
79 typedef struct mechsim_state_struct MECHSIM_STATE;
80 
81 struct mechsim_state_struct {
82   VECTOR Position;
83   VECTOR Velocity;
84 };
85 
86 /* ------------------------------------------------------ */
87 typedef struct mechsim_mass_struct MECHSIM_MASS;
88 
89 struct mechsim_mass_struct {
90 
91   int Index;
92 
93   VECTOR Position;
94   VECTOR Velocity;
95 	// Yes, we don't need them during the simulation but after it
96 	// (for being able to access them from SDL)
97   VECTOR Acceleration;
98   DBL Mass;
99   DBL Radius;
100   int Flag;
101   int Attach;
102   int Force;
103 
104   int Group;
105 };
106 
107 /* ------------------------------------------------------ */
108 typedef struct mechsim_connect_struct MECHSIM_CONNECT;
109 
110 struct mechsim_connect_struct {
111 
112   int Index;
113 
114   int Idx1;
115   int Idx2;
116 
117   DBL Length;
118   DBL Stiffness;
119   DBL Damping;
120 
121   int Group;
122 };
123 
124 /* ------------------------------------------------------ */
125 typedef struct mechsim_face_struct MECHSIM_FACE;
126 
127 struct mechsim_face_struct {
128 
129   int Index;
130 
131   int Idx1;
132   int Idx2;
133   int Idx3;
134 
135   int Group;
136 };
137 
138 /* ------------------------------------------------------ */
139 typedef struct mechsim_group_struct MECHSIM_GROUP;
140 
141 struct mechsim_group_struct {
142 
143   int max_masses;
144   int max_connects;
145   int max_faces;
146 
147   int mass_count;
148   int connect_count;
149   int faces_count;
150 
151   MECHSIM_MASS **Masses;
152   MECHSIM_CONNECT **Connects;
153   MECHSIM_FACE **Faces;
154 };
155 
156 /* ------------------------------------------------------ */
157 
158 typedef struct mechsim_ve_element_struct MECHSIM_VE_ELEMENT;
159 
160 struct mechsim_ve_element_struct {
161 
162   DBL Y;
163   DBL Stiffness;
164   DBL Damping;
165 };
166 
167 typedef struct mechsim_viscoelastic_struct MECHSIM_VISCOELASTIC;
168 
169 struct mechsim_viscoelastic_struct {
170 
171   int Index;
172 
173   int Idx1;
174   int Idx2;
175 
176   DBL Length;
177   DBL Stiffness0;
178 	int element_count;
179 	int Accuracy;
180   MECHSIM_VE_ELEMENT *Element;
181 };
182 
183 
184 /* ------------------------------------------------------ */
185 typedef struct bounding_hashtable_item BOUNDING_HASHTABLE_ITEM;
186 
187 struct bounding_hashtable_item {
188   int time_stamp; // time stamp of this item
189   int size; // size of the index list
190   int used; // used size of the index list
191   int *index; // index list
192 };
193 
194 /* ------------------------------------------------------ */
195 typedef struct mechsim_bounding_data MECHSIM_BOUNDING_DATA;
196 
197 struct mechsim_bounding_data {
198   int time_stamp; // current time stamp for faster updating of the hashing list
199   SNGL_VECT Unit; // unit size of the grid
200   int intersect_list_size; // size of the intersection list for hashing
201   int intersect_list_index; // current index of the intersection list for hasing
202   int* intersect_list; // the intersection list for hashing
203   int bitfield_size; // size of the bit field
204   unsigned int* bitfield; // bit field
205   int bounding_hashtable_size; // size of the hashing list
206   BOUNDING_HASHTABLE_ITEM* bounding_hashtable; // hashing list
207   BBOX* Faces_BBox_Array; // array of bounding boxes used for faces
208   BBOX* Face_Groups_BBox_Array; // array of bounding boxes used for groups, bounded objects are faces
209   BBOX* Mass_Groups_BBox_Array; // array of bounding boxes used for groups, bounded objects are masses
210   BBOX* Connect_Groups_BBox_Array; // array of bounding boxes used for groups, bounded objects are connections
211   BBOX* Connects_BBox_Array; // array of bounding boxes used for connections
212 };
213 
214 /* ------------------------------------------------------ */
215 typedef struct mechsim_data_struct MECHSIM_DATA;
216 
217 struct mechsim_data_struct {
218 
219   int max_masses;
220   int max_connects;
221   int max_faces;
222   int max_groups;
223 
224   int max_viscoelastics;
225 
226   int mass_count;
227   int connect_count;
228   int faces_count;
229   int group_count;
230 
231   int ve_count;
232 
233   MECHSIM_MASS *Masses;
234   MECHSIM_CONNECT *Connects;
235   MECHSIM_FACE *Faces;
236   MECHSIM_GROUP *Groups;
237 
238 	MECHSIM_VISCOELASTIC *Viscoelastics;
239 
240   OBJECT *Faces_Object;
241 };
242 
243 /* ------------------------------------------------------ */
244 typedef struct mechsim_environment_struct MECHSIM_ENVIRONMENT;
245 
246 struct mechsim_environment_struct {
247 
248   FUNCTION_PTR Function;
249   OBJECT *Object;
250   DBL Stiffness;
251   DBL Damping;
252   DBL Friction;
253   DBL Friction_Excess;
254   unsigned int Method;
255 
256   MECHSIM_ENVIRONMENT *Next;
257 };
258 
259 /* ------------------------------------------------------ */
260 typedef struct mechsim_fields_struct MECHSIM_FIELDS;
261 
262 struct mechsim_fields_struct {
263 
264   FUNCTION_PTR *Function;
265   int count;
266   int max_count;
267 };
268 
269 /* ------------------------------------------------------ */
270 typedef struct mechsim_forces_struct MECHSIM_FORCES;
271 
272 struct mechsim_forces_struct {
273 
274   FUNCTION_PTR *Function;
275   int count;
276   int max_count;
277 };
278 
279 /* ------------------------------------------------------ */
280 typedef struct mechsim_interactions_struct MECHSIM_INTERACTIONS;
281 
282 struct mechsim_interactions_struct {
283 
284   FUNCTION_PTR *Function;
285   int count;
286   int max_count;
287 };
288 
289 /* ------------------------------------------------------ */
290 typedef struct mechsim_attachments_struct MECHSIM_ATTACHMENTS;
291 
292 struct mechsim_attachments_struct {
293 
294   FUNCTION_PTR *Function;
295 	INT_VECT *Fixed;
296   int count;
297   int max_count;
298 };
299 
300 /* ------------------------------------------------------ */
301 typedef struct mechsim_options_struct MECHSIM_OPTIONS;
302 
303 struct mechsim_options_struct {
304 
305   unsigned int Method;
306   int Step_Count;
307   DBL Time_Step;
308   DBL Start_Time;
309   DBL End_Time;
310 	bool Timing_Given;
311 	bool Timing_Loaded;
312 
313 	// for adaptive time stepping
314   DBL Accuracy;
315   DBL Time_Step_Min;
316 
317   // For message output
318   int Steps;
319   DBL Time;
320 	DBL TS_Min;
321 	DBL TS_Max;
322 
323   VECTOR gravity;
324   DBL Damping;
325 
326   MECHSIM_ENVIRONMENT *Environment;
327 	int Env_max_count;
328 	int Env_count;
329 
330   DBL Collision_Stiffness;
331   DBL Collision_Damping;
332   DBL Collision_Friction;
333   DBL Collision_Friction_Excess;
334   int Calc_Collision;
335   int Calc_Collision_Faces;
336   int Calc_Collision_Connections;
337 
338   char* Load_File_Name;
339   char* Save_File_Name;
340 
341   bool Save_File;
342   int Save_File_Type;
343 
344   bool Enabled;
345 
346   MECHSIM_DATA Data;
347 
348   MECHSIM_INTERACTIONS Interactions;
349   MECHSIM_ATTACHMENTS Attachments;
350   MECHSIM_FIELDS Fields;
351   MECHSIM_FORCES Forces;
352 
353   unsigned int Bounding;
354 	INT_VECT Fixed;
355 };
356 
357 /*****************************************************************************
358 * Global variables
359 ******************************************************************************/
360 
361 extern MECHSIM_OPTIONS MechsimOptions;
362 
363 /*****************************************************************************
364 * Global functions
365 ******************************************************************************/
366 
367 DBL Mechsim_Connect_Evaluate(int Idx);
368 DBL Mechsim_Viscoelastic_Evaluate(int Idx);
369 
370 bool Mechsim_read_file (IStream * fd, int Group);
371 
372 void Initialize_MechSim(void);
373 void Deinitialize_MechSim(void);
374 
375 void Mechsim_Simulate(void);
376 
377 
378 END_POV_NAMESPACE
379 
380 #endif
381