-
Notifications
You must be signed in to change notification settings - Fork 39
/
PicoGK_FieldMetadata.cs
373 lines (337 loc) · 13.4 KB
/
PicoGK_FieldMetadata.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//
// SPDX-License-Identifier: Apache-2.0
//
// PicoGK ("peacock") is a compact software kernel for computational geometry,
// specifically for use in Computational Engineering Models (CEM).
//
// For more information, please visit https://picogk.org
//
// PicoGK is developed and maintained by LEAP 71 - © 2023-2024 by LEAP 71
// https://leap71.com
//
// Computational Engineering will profoundly change our physical world in the
// years ahead. Thank you for being part of the journey.
//
// We have developed this library to be used widely, for both commercial and
// non-commercial projects alike. Therefore, we have released it under a
// permissive open-source license.
//
// The foundation of PicoGK is a thin layer on top of the powerful open-source
// OpenVDB project, which in turn uses many other Free and Open Source Software
// libraries. We are grateful to be able to stand on the shoulders of giants.
//
// LEAP 71 licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, THE SOFTWARE IS
// PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System.Diagnostics;
using System.Numerics;
using System.Text;
namespace PicoGK
{
public interface IFieldWithMetadata
{
public FieldMetadata oMetaData();
}
public partial class Voxels : IFieldWithMetadata
{
public FieldMetadata oMetaData() => m_oMetadata;
}
public partial class VectorField : IFieldWithMetadata
{
public FieldMetadata oMetaData() => m_oMetadata;
}
public partial class ScalarField : IFieldWithMetadata
{
public FieldMetadata oMetaData() => m_oMetadata;
}
/// <summary>
/// Metadata table containing parameters associated with field types
/// like Voxels, ScalarFields, VectorFields. The metadata contained in here
/// is saved and loaded from VDB files.
/// </summary>
public partial class FieldMetadata
{
/// <summary>
/// Type of the data items in the metadata table
/// </summary>
public enum EType
{
UNKNOWN = -1,
STRING = 0,
FLOAT,
VECTOR
};
/// <summary>
/// Number of items in the metadata table
/// </summary>
public int nCount()
{
return _nCount(m_hThis);
}
/// <summary>
/// Attempts to retrieve the name of the parameter at
/// the index supplied
/// </summary>
/// <param name="nIndex">Index value of the parameter</param>
/// <param name="strValueName">Name of the parameter at this
/// position.</param>
/// <returns></returns>
public bool bGetNameAt(int nIndex, out string strValueName)
{
int nLength = _nNameLengthAt(m_hThis, nIndex) + 1;
StringBuilder oBuilder = new StringBuilder(nLength);
if (!_bGetNameAt(m_hThis, nIndex, oBuilder, nLength))
{
strValueName = "";
return false;
}
strValueName = oBuilder.ToString();
return true;
}
/// <summary>
/// Returns the type of the value with the specified name
/// </summary>
/// <param name="strName">Name of the parameter to retrieve</param>
/// <returns>Type of the parameter</returns>
public EType eTypeAt(string strName)
{
int iType = _nTypeAt(m_hThis, strName);
if (iType > (int) EType.VECTOR)
{
Debug.Assert(false, "Invalid metadata type returned by PicoGKRuntime");
return EType.UNKNOWN;
}
return (EType) iType;
}
/// <summary>
/// Returns the human readable type of the parameter with the
/// specified name
/// </summary>
/// <param name="strName">Name of the parameter</param>
/// <returns>The human readable name of the type</returns>
public string strTypeAt(string strName)
{
return strTypeName(eTypeAt(strName));
}
/// <summary>
/// Translate the type enum to a string
/// </summary>
/// <param name="eType">Type to translate</param>
/// <returns>Human readable string</returns>
public string strTypeName(EType eType)
{
switch (eType)
{
case EType.UNKNOWN:
return "unknown";
case EType.STRING:
return "string";
case EType.FLOAT:
return "float";
case EType.VECTOR:
return "vector";
}
Debug.Assert(false, "Unknown type");
return "undefined";
}
/// <summary>
/// Try to get the value of a parameter
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="strValue">Value returned</param>
/// <returns>Returns false if value doesn't exist or has the
/// wrong type</returns>
public bool bGetValueAt( string strFieldName,
out string strValue)
{
int nLength = _nStringLengthAt(m_hThis, strFieldName) + 1;
StringBuilder oBuilder = new StringBuilder(nLength);
if (!_bGetStringAt(m_hThis, strFieldName, oBuilder, nLength))
{
strValue = "";
return false;
}
strValue = oBuilder.ToString();
return true;
}
/// <summary>
/// Try to get the value of a parameter
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="fValue">Value returned</param>
/// <returns>Returns false if value doesn't exist or has the
/// wrong type</returns>
public bool bGetValueAt(string strFieldName, out float fValue)
{
fValue = 0f;
return _bGetFloatAt(m_hThis, strFieldName, ref fValue);
}
/// <summary>
/// Try to get the value of a parameter
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="vecValue">Value returned</param>
/// <returns>Returns false if value doesn't exist or has the
/// wrong type</returns>
public bool bGetValueAt(string strFieldName, out Vector3 vecValue)
{
vecValue = Vector3.Zero;
return _bGetVectorAt(m_hThis, strFieldName, ref vecValue);
}
/// <summary>
/// Set string value in the metadata table
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="strValue">Value to set</param>
public void SetValue(string strFieldName, string strValue)
{
GuardInternalFields(strFieldName);
_SetValue(strFieldName, strValue);
}
/// <summary>
/// Set floating point value in the metadata table
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="fValue">Value to set</param>
public void SetValue(string strFieldName, float fValue)
{
GuardInternalFields(strFieldName);
_SetValue(strFieldName, fValue);
}
/// <summary>
/// Set a vector value in the metadata table
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="vecValue">Value to set</param>
public void SetValue(string strFieldName, Vector3 vecValue)
{
GuardInternalFields(strFieldName);
_SetValue(strFieldName, vecValue);
}
/// <summary>
/// Remove a value from the metadata table
/// </summary>
/// <param name="strFieldName">Name of the value</param>
public void RemoveValue(string strFieldName)
{
GuardInternalFields(strFieldName);
_RemoveValue(m_hThis, strFieldName);
}
/// <summary>
/// Converts the contents of the metadata table to a string
/// </summary>
/// <returns>String containing info about metadata</returns>
public override string? ToString()
{
string str = $"Metadata table with {nCount()} items\n";
for (int nMeta=0; nMeta < nCount(); nMeta++)
{
bGetNameAt(nMeta, out string strName);
str += $" {strName} ({strTypeAt(strName)}): ";
switch (eTypeAt(strName))
{
case EType.UNKNOWN:
break;
case EType.STRING:
bGetValueAt(strName, out string strValue);
str += "'" + strValue + "'";
break;
case EType.FLOAT:
bGetValueAt(strName, out float fValue);
str += fValue.ToString();
break;
case EType.VECTOR:
bGetValueAt(strName, out Vector3 vecValue);
str += vecValue.ToString();
break;
}
str += "\n";
}
return str;
}
/// <summary>
/// Internal constructor used by the Voxels, ScalarField and VectorField
/// accessor function. Do not construct FieldMetadata objects by
/// yourself
/// </summary>
/// <param name="hSource">This pointer</param>
public FieldMetadata(IntPtr hSource)
{
m_hThis = hSource;
// These values are added to every field we store from PicoGK
// this allows us to deal with future extensions, and also
// informs the user about the voxel size used in the field
// Note - all values we save to the metadata is in SI unites
// so we divide the voxel size in mm by 1000 to get the voxel size
// in meters
_SetValue("PicoGK.Library", Library.strName());
_SetValue("PicoGK.Version", Library.strVersion());
_SetValue("PicoGK.VoxelSize", Library.fVoxelSizeMM / 1000f);
}
/// <summary>
/// Set string value in the metadata table (internal, do not use)
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="strValue">Value to set</param>
internal void _SetValue(string strFieldName, string strValue)
{
_SetStringValue(m_hThis, strFieldName, strValue);
}
/// <summary>
/// Set floating point value in the metadata table (internal, do not use)
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="fValue">Value to set</param>
internal void _SetValue(string strFieldName, float fValue)
{
_SetFloatValue(m_hThis, strFieldName, fValue);
}
/// <summary>
/// Set a vector value in the metadata table (internal, do not use)
/// </summary>
/// <param name="strFieldName">Name of the parameter</param>
/// <param name="vecValue">Value to set</param>
internal void _SetValue(string strFieldName, Vector3 vecValue)
{
_SetVectorValue(m_hThis, strFieldName, vecValue);
}
/// <summary>
/// Remove a value from the metadata table (internal, do not use)
/// </summary>
/// <param name="strFieldName">Name of the value</param>
internal void _RemoveValue(string strFieldName)
{
_RemoveValue(m_hThis, strFieldName);
}
/// <summary>
/// This function tests whether you are attempting to set internal
/// metadata fields from your code — this can mess up openvdb and
/// internal PicoGK functionality
/// </summary>
/// <param name="strFieldName">Field name you are trying to set</param>
/// <exception cref="FieldAccessException"></exception>
protected void GuardInternalFields(string strFieldName)
{
if (strFieldName.StartsWith("PicoGK.", StringComparison.InvariantCultureIgnoreCase))
throw new FieldAccessException($"Fields starting with 'PicoGK.' are internal - do not set them from your code ('{strFieldName}').");
if ( strFieldName.Equals("class", StringComparison.InvariantCultureIgnoreCase) ||
strFieldName.Equals("name", StringComparison.InvariantCultureIgnoreCase))
{
throw new FieldAccessException($"Do not set openvdb-internal fields from your code ('{strFieldName}')");
}
if (strFieldName.StartsWith("file_", StringComparison.InvariantCultureIgnoreCase))
{
throw new FieldAccessException($"Field names starting with file_ are openvdb-internal - do not set from your code ('{strFieldName}')");
}
}
}
}