-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOperatorInterfaces.cs
264 lines (201 loc) · 7.77 KB
/
OperatorInterfaces.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) Ryan Schmidt ([email protected]) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited. Proprietary and confidential.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using g3;
namespace gs
{
public delegate void OperatorModifiedHandler(ModelingOperator op);
public struct ModelingOpException
{
public ModelingOperator op;
public Exception e;
public ModelingOpException(ModelingOperator op, Exception e) { this.op = op; this.e = e; }
}
public delegate void OperatorExceptionHandler(ModelingOpException ex);
public interface ModelingOperator
{
event OperatorModifiedHandler OperatorModified;
event OperatorExceptionHandler OperatorException;
}
public abstract class BaseModelingOperator : ModelingOperator
{
public event OperatorModifiedHandler OperatorModified;
public event OperatorExceptionHandler OperatorException;
virtual protected void PostOnOperatorModified()
{
OperatorModified?.Invoke(this);
}
public void ForcePostOperatorModified()
{
PostOnOperatorModified();
}
virtual protected void PostOnOperatorException(Exception e)
{
OperatorException?.Invoke(new ModelingOpException(this, e));
}
}
/// <summary>
/// This class handles proper invalidation of an operator's output using a timestamp.
/// So, if an operator-modified is posted while the Update() is computing on a background
/// thread, we won't accidentally ignore it.
///
/// Usage is:
/// 1) when any inputs are modified, call invalidate()
/// 2) in any output functions, if requires_update() returns true, call your Update()
/// 3) at start of Update(), call begin_update()
/// 4) at end of Update(), call end_update()
/// 5) in any function that "consumes" the output, call result_consumed()
///
/// See MeshDeformationOp for a bare-bones example
///
/// [TODO] some kind of locking functionality, so that important data structures
/// cannot be modified while they are being used in Update() ?
///
/// </summary>
public abstract class BaseSingleOutputModelingOperator : BaseModelingOperator
{
bool result_valid = false;
int input_timestamp = 1;
int last_update_timestamp = 0;
/// <summary>
/// Is the current valid and ready to be consumed
/// </summary>
public bool IsResultValid {
get { return result_valid; }
}
/// <summary>
/// The Timestamp of the last client request. This value is incremented on invalidate() calls.
/// </summary>
public int CurrentInputTimestamp {
get { return input_timestamp; }
}
/// <summary>
/// The value of CurrentInputTimestamp the last time we began an update().
/// If this is == CurrentInputTimestamp, then the result is up-to-date
/// If this is < CurrentInputTimestamp, we need to Update()
/// </summary>
public int LastUpdateTimestamp {
get { return last_update_timestamp; }
}
/// <summary>
/// force an invalidation. Client may need to notify op about external change events,
/// for example requests to explicitly cancel current computation
/// </summary>
public virtual void ForceInvalidate()
{
invalidate();
}
protected virtual void invalidate()
{
result_valid = false;
input_timestamp++;
PostOnOperatorModified();
}
protected virtual void begin_update()
{
last_update_timestamp = input_timestamp;
}
protected virtual void complete_update()
{
result_valid = true;
}
protected virtual bool requires_update()
{
return (result_valid == false || input_timestamp != last_update_timestamp);
}
protected virtual bool is_invalidated()
{
return input_timestamp != last_update_timestamp;
}
protected virtual void result_consumed()
{
result_valid = false;
}
}
public interface IMeshSourceOp : ModelingOperator
{
IMesh GetIMesh();
bool HasSpatial { get; }
ISpatial GetSpatial();
}
public interface DMeshSourceOp : IMeshSourceOp
{
/// <summary>
/// returns DMesh that you should *not* be modifying
/// </summary>
DMesh3 GetDMeshUnsafe();
/// <summary>
/// assumption is that we can edit this DMesh3, ie the op no longer holds a reference!
/// </summary>
DMesh3 ExtractDMesh();
}
public interface ISampledCurve3dSourceOp : ModelingOperator
{
ISampledCurve3d GetICurve();
}
public interface DCurve3SourceOp : ISampledCurve3dSourceOp
{
/// <summary>
/// assumption is that we can edit this DCurve3, ie the op no longer holds a reference!
/// </summary>
DCurve3 ExtractDCurve();
}
public interface IIndexListSourceOp : ModelingOperator
{
IList<int> GetIndices();
}
public interface IVectorDisplacementSourceOp : ModelingOperator
{
IVectorDisplacement GetDisplacement();
}
public struct DMeshOutputStatus
{
public enum States {
Unavailable, Computing, Ready
}
public States State;
public DMesh3 Mesh;
public List<ModelingOpException> ComputeExceptions;
public bool IsErrorOutput() {
return Mesh == null || DMeshOpUtil.IsFailureMesh(Mesh);
}
public static DMeshOutputStatus Unavailable() { return new DMeshOutputStatus() { State = States.Unavailable }; }
public static DMeshOutputStatus Computing() { return new DMeshOutputStatus() { State = States.Computing }; }
public static DMeshOutputStatus Ready(DMesh3 mesh) { return new DMeshOutputStatus() { State = States.Ready, Mesh = mesh }; }
public static DMeshOutputStatus Ready(DMesh3 mesh, List<ModelingOpException> exceptions)
{ return new DMeshOutputStatus() { State = States.Ready, Mesh = mesh, ComputeExceptions = exceptions }; }
}
public interface DMeshComputeOp : ModelingOperator
{
// this function will return null if no new mesh is available yet
// (is this good? weird! but means we can do in separate thread safely...)
DMeshOutputStatus CheckForNewMesh();
}
public interface ResultSourceOp<T> : ModelingOperator
{
T ExtractResult();
}
public enum OpResultState
{
Unavailable, Computing, Ready
}
public struct OpResultOutputStatus<T>
{
public OpResultState State;
public T Result;
public List<ModelingOpException> ComputeExceptions;
public static OpResultOutputStatus<T> Unavailable() { return new OpResultOutputStatus<T>() { State = OpResultState.Unavailable }; }
public static OpResultOutputStatus<T> Computing() { return new OpResultOutputStatus<T>() { State = OpResultState.Computing }; }
public static OpResultOutputStatus<T> Ready(T result) { return new OpResultOutputStatus<T>() { State = OpResultState.Ready, Result = result}; }
public static OpResultOutputStatus<T> Ready(T result, List<ModelingOpException> exceptions)
{ return new OpResultOutputStatus<T>() { State = OpResultState.Ready, Result = result, ComputeExceptions = exceptions }; }
}
public interface ResultComputeOp<T> : ModelingOperator
{
OpResultOutputStatus<T> CheckForNewResult();
}
}