Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfTimeEvent.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
25#ifndef _SOURCE_EVENT_TFTIMEEVENT_H_
26#define _SOURCE_EVENT_TFTIMEEVENT_H_
27
28#include "tfEventList.h"
29
30#include <limits>
31#include <unordered_map>
32
33
34namespace TissueForge::event {
35
36
37 struct TimeEvent;
38
39 using TimeEventMethod = EventMethodT<TimeEvent>;
40 using TimeEventNextTimeSetter = FloatP_t (*)(TimeEvent&, const FloatP_t&);
41
42 CAPI_FUNC(HRESULT) defaultTimeEventPredicateEval(const FloatP_t &next_time, const FloatP_t &start_time=-1, const FloatP_t &end_time=-1);
43
44 CAPI_FUNC(FloatP_t) timeEventSetNextTimeExponential(TimeEvent &event, const FloatP_t &time);
45 CAPI_FUNC(FloatP_t) timeEventSetNextTimeDeterministic(TimeEvent &event, const FloatP_t &time);
46
47 enum class TimeEventTimeSetterEnum : unsigned int {
48 DEFAULT = 0,
49 DETERMINISTIC,
50 EXPONENTIAL
51 };
52
53 typedef std::unordered_map<TimeEventTimeSetterEnum, TimeEventNextTimeSetter> TimeEventNextTimeSetterMapType;
54 static TimeEventNextTimeSetterMapType timeEventNextTimeSetterMap {
55 {TimeEventTimeSetterEnum::DETERMINISTIC, &timeEventSetNextTimeDeterministic},
56 {TimeEventTimeSetterEnum::EXPONENTIAL, &timeEventSetNextTimeExponential},
57 {TimeEventTimeSetterEnum::DEFAULT, &timeEventSetNextTimeDeterministic}
58 };
59
60 typedef std::unordered_map<std::string, TimeEventTimeSetterEnum> TimeEventNextTimeSetterNameMapType;
61 static TimeEventNextTimeSetterNameMapType timeEventNextTimeSetterNameMap {
62 {"deterministic", TimeEventTimeSetterEnum::DETERMINISTIC},
63 {"exponential", TimeEventTimeSetterEnum::EXPONENTIAL},
64 {"default", TimeEventTimeSetterEnum::DEFAULT}
65 };
66
67 CAPI_FUNC(TimeEventNextTimeSetter*) getTimeEventNextTimeSetter(TimeEventTimeSetterEnum setterEnum);
68 CAPI_FUNC(TimeEventNextTimeSetter*) getTimeEventNextTimeSetterN(std::string setterName);
69
70 struct CAPI_EXPORT TimeEvent : EventBase {
74 FloatP_t next_time;
75
79 FloatP_t period;
80
84 FloatP_t start_time;
85
89 FloatP_t end_time;
90
92 const FloatP_t &period,
93 TimeEventMethod *invokeMethod,
94 TimeEventMethod *predicateMethod=NULL,
95 TimeEventNextTimeSetter *nextTimeSetter=NULL,
96 const FloatP_t &start_time=0,
97 const FloatP_t &end_time=-1
98 ) :
99 EventBase(),
100 period(period),
101 invokeMethod(invokeMethod),
102 predicateMethod(predicateMethod),
103 nextTimeSetter(nextTimeSetter),
104 next_time(start_time),
105 start_time(start_time),
106 end_time(end_time > 0 ? end_time : std::numeric_limits<FloatP_t>::max())
107 {
108 if (nextTimeSetter == NULL) setTimeEventNextTimeSetter(TimeEventTimeSetterEnum::DEFAULT);
109 }
110 ~TimeEvent();
111
114 HRESULT eval(const FloatP_t &time);
115
116 protected:
117
118 FloatP_t getNextTime(const FloatP_t &current_time);
119 HRESULT setTimeEventNextTimeSetter(TimeEventTimeSetterEnum setterEnum);
120
121 private:
122
123 TimeEventMethod *invokeMethod;
124 TimeEventMethod *predicateMethod;
125 TimeEventNextTimeSetter *nextTimeSetter;
126 };
127
129
130
131 // Module entry points
132
145 const FloatP_t &period,
146 TimeEventMethod *invokeMethod,
147 TimeEventMethod *predicateMethod=NULL,
148 const unsigned int &nextTimeSetterEnum=0,
149 const FloatP_t &start_time=0,
150 const FloatP_t &end_time=-1
151 );
152
165 const FloatP_t &period,
166 TimeEventMethod *invokeMethod,
167 TimeEventMethod *predicateMethod=NULL,
168 const std::string &distribution="default",
169 const FloatP_t &start_time=0,
170 const FloatP_t &end_time=-1
171 );
172
173};
174
175#endif // _SOURCE_EVENT_TFTIMEEVENT_H_
Tissue Forge event system.
Definition tfEvent.h:34
TimeEvent * onTimeEventN(const FloatP_t &period, TimeEventMethod *invokeMethod, TimeEventMethod *predicateMethod=NULL, const std::string &distribution="default", const FloatP_t &start_time=0, const FloatP_t &end_time=-1)
Creates a time-dependent event using prescribed invoke and predicate functions.
TimeEvent * onTimeEvent(const FloatP_t &period, TimeEventMethod *invokeMethod, TimeEventMethod *predicateMethod=NULL, const unsigned int &nextTimeSetterEnum=0, const FloatP_t &start_time=0, const FloatP_t &end_time=-1)
Creates a time-dependent event using prescribed invoke and predicate functions.
Definition tfEvent.h:45
Definition tfEventList.h:59
Definition tfTimeEvent.h:70
FloatP_t start_time
Start time of evaluations.
Definition tfTimeEvent.h:84
FloatP_t end_time
End time of evaluations.
Definition tfTimeEvent.h:89
FloatP_t period
Period of evaluation.
Definition tfTimeEvent.h:79
FloatP_t next_time
Next time of evaluation.
Definition tfTimeEvent.h:74
int32_t HRESULT
Definition tf_port.h:255