HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SIM_Cache.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  */
7 
8 #ifndef __SIM_Cache_h__
9 #define __SIM_Cache_h__
10 
11 #include "SIM_API.h"
12 #include "SIM_CacheOptions.h"
13 #include "SIM_Object.h"
14 #include "SIM_ObjectArray.h"
15 #include "SIM_Utils.h"
16 #include <UT/UT_LinkList.h>
17 
18 class SIM_Engine;
19 class SIM_CacheEntry;
20 class SIM_SimulationState;
21 template <typename T> class UT_Array;
22 
23 /// This class is the cache for a SIM_Engine.
24 /// It holds all the actual objects for the simulation for the entire
25 /// cached time range.
27 {
28 public:
29  /// Constructor intializes this class with no object data.
30  /// A single cache entry is created to hold any new objects.
31  SIM_Cache();
32  /// Destructor destroys all objects and temp files.
33  ~SIM_Cache();
34 
35  /// Initialize this object with a SIM_Engine pointer. This must be
36  /// done as soon as the SIM_Cache is created.
37  void setEngine(SIM_Engine *engine);
38  /// Returns a reference to our caching option data.
39  const SIM_CacheOptions &getCacheOptions() const;
40  /// Sets the cache options.
41  void setCacheOptions(const SIM_CacheOptions &src);
42 
43  /// Caches the current object data and advances by one time step.
44  /// Basically this consists of creating a copy of all objects in the
45  /// simulation and setting the new current time. If the cache size
46  /// is exceeded, a cache entry will be deleted. A cache entry may
47  /// also get pushed to disk. If caching is disabled, this function
48  /// does nothing except advance the current time.
49  void cacheAndAdvance();
50  /// Tells the cache to move to a new current time.
51  /// Whatever cache entry is the best match for the specfied time will
52  /// be made the current cache entry. If the new time is earlier than any
53  /// cache entry, then the earliest cache entry is used. If the new time
54  /// is later than the latest cache entry, the last entry is used. Note
55  /// that this means the cache's current time won't necessarily be set
56  /// to the requested value. It is up to the SIM_Engine to perform
57  /// simulation steps to get to the actual desired time.
58  void setCurrentTime(const SIM_Time &time);
59  /// Deletes all cache entries after the specified time. The cache
60  /// entry for the specified time itself is also deleted. The cache
61  /// is moved to the entry just prior to the earliest deleted entry.
62  /// If there is insufficient cache to fulfil the request, then nothing
63  /// is changed and the function returns false. Otherwise it returns
64  /// true.
65  bool deleteAfterTime(const SIM_Time &time);
66  /// Clears all cache entries. A single empty cache entry is then
67  /// created to act as the current entry to shich new objects may be
68  /// added.
69  void clear();
70  /// Clears all cache entries. Does not create a cache entry
71  /// to act as a current entry, so the state is invalid and this
72  /// should be shortly deleted.
73  void clearForDestruction();
74  /// Attempts to re-load explicit cache entries from the last
75  /// loaded etnry
76  void preload();
77 
78  /// Returns the time of the current cache entry. This function is
79  /// called by the SIM_Engine::getSimulationTime() function.
80  const SIM_Time &getCurrentTime() const;
81  /// Returns the time of the earliest cache entry. This function is
82  /// called by the SIM_Engine::getEarliestCacheTime() function.
83  const SIM_Time &getEarliestTime() const;
84  const SIM_Time &getLatestTime() const;
85  /// Returns the objects that belong to the current cache entry.
86  /// This function is called by the SIM_Engine getSimulationObject()
87  /// and related functions.
88  SIM_SimulationState &getCurrentSimulationObjects() const;
89  /// Returns the live cached frames
90  void getCachedFrames(UT_Array<int> &frames,
91  bool memory) const;
92 
93  /// Returns the specified object at an earlier time step. If the object
94  /// at the exact specified time cannot be found, we create a new object
95  /// by interpolating between the previous and next available states of
96  /// the object.
97  const SIM_Object *getObjectAtTime(const SIM_Object &object,
98  const SIM_Time &time,
99  bool allowinterpolation);
100  /// Returns a non-const object from the specified earlier time. The
101  /// returned value is always a pointer to the passed in object, or a
102  /// substep object. If a time in some earlier timestep is specified,
103  /// the earlier object is copied as a substep object of the current
104  /// time. Otheriwse this function could be used to modify the past.
105  SIM_Object *getAffectorAtTime(SIM_Object &object,
106  const SIM_Time &time,
107  bool allowinterpolation);
108 
109  /// Before the call, 'past_objects' must be a valid array of valid arrays,
110  /// and 'past_times' must be a valid array.
111  /// 'past_objects' must have size 'max_past'
112  /// each element of 'past_objects' must be an array of SIM_Object* of
113  //// size 'num_objects'
114  /// 'past_times' must be an array of size 'max_past'
115  ///
116  /// Output:
117  /// The number of cached frames is returned.
118  /// The cached version of object[oi] at time past_times[i]
119  /// is stored in the array past_objects[i] at location oi,
120  /// for all 0 <= i < "the returned number of cached frames"
121  int getObjectsAtPastTimes(
122  const SIM_Object**const past_objects[],
123  SIM_Time past_times[],
124  const int num_objects,
125  const SIM_Object*const objects[],
126  const int max_past
127  );
128 
129  /// Equivalent of getObjectsAtPastTimes() for relationships.
130  int getRelationshipsAtPastTimes(
131  const SIM_Relationship**const past_rels[],
132  SIM_Time past_times[],
133  const int num_rels,
134  const SIM_Relationship*const rels[],
135  const int max_past
136  );
137 
138  /// Returns a substep version of the specified object. The substep
139  /// object is always created within the current simulation state
140  /// regardless of the time value. The reason the SIM_Cache is used
141  /// as an intermediary here is so that if we already have an
142  /// interpolated version of the object at the requested time, we
143  /// can convert the interpolated object into a substep (which is
144  /// really different only in the sense of whether the SIM_Cache or
145  /// the SIM_SimulationState owns it).
146  SIM_Object *createSubStepObject(const SIM_Object &object,
147  const SIM_Time &time);
148  /// Deletes any objects from the myInterpolatedObjects array that
149  /// match the provided object id. This function is called by the
150  /// engine just prior to solving the object. The idea is that the
151  /// object may have been used to create an interpolated object prior
152  /// to its solve for a given timestep. After its solve, all the
153  /// interpolation is incorrect, so leaving the interpolated objects
154  /// around is potentially misleading and harmful.
155  void clearInterpolatedObjects(
156  const SIM_ObjectArray &objects);
157  /// This performs the cleanup required after performing a simulation step.
158  /// All temporary interpolated objects are deleted, and any excess cache
159  /// entries in memory are flushed to disk.
160  void postSimulationStep();
161 
162 private:
163  /// Loads a cache entry from disk (if it's not already in memory).
164  void loadFromDisk(SIM_CacheEntry *entry);
165  /// Moves some cache entries from memory to disk if we have too many
166  /// in memory.
167  void ensureCacheCompliesWithOptions();
168  /// Interpolated objects use for display are stored outside of any
169  /// cache entry. This function clears out those interpolated objects.
170  void clearInterpolatedObjects(bool clearsubsteps);
171  /// Utility function used by getObjectAtTime() and getAffectorAtTime().
172  /// It finds the closest existing objects to the requested object.
173  void getBestObjects(const SIM_Object *object,
174  const SIM_Time &time,
175  bool allowinterpolation,
176  const SIM_Object *&matchobject,
177  SIM_Object *&nonconstmatchobject,
178  const SIM_Object *&bestbeforeobject,
179  SIM_Time &bestbeforetime,
180  const SIM_Object *&bestafterobject,
181  SIM_Time &bestaftertime);
182  /// Get the initial entry, which is the simulation state that shows
183  /// up for all times less then zero, and which is copied to be the
184  /// first timestep state when the cache is cleared.
185  SIM_CacheEntry *getInitialEntry();
186  /// Clears the initial entry.
187  void clearInitialEntry();
188  /// Get our current cache entry.
189  SIM_CacheEntry *getCurrentEntry() const;
190  /// Set our current cache entry.
191  void setCurrentEntry(SIM_CacheEntry *newentry);
192 
193  SIM_Engine *myEngine;
194  SIM_CacheOptions myCacheOptions;
195  UT_LinkList myCacheEntries;
196  SIM_CacheEntry *myCurrentEntry;
197  SIM_CacheEntry *myInitialEntry;
198  SIM_ObjectAtTimeMatrix myInterpolatedObjects;
199 
200  /// Do we have any temporary disk caches?
201  /// Does not count explicit entries.
202  /// We use this so we can determine if our getAllowSaveToDisk flag
203  /// needs to be enforced.
204  bool myHasDiskCache;
205 };
206 
207 #endif
208 
GLsizei GLenum GLsizei GLsizei GLuint memory
Definition: RE_OGL.h:202
GT_API const UT_StringHolder time
Holds pointers to a number of SIM_Object objects.
#define SIM_API
Definition: SIM_API.h:12
This class stores all the caching options used by the SIM_Cache.
GLenum src
Definition: glcorearb.h:1793