forked from gradientspace/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScalarMap.cs
74 lines (62 loc) · 1.97 KB
/
ScalarMap.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
using System;
using System.Collections.Generic;
namespace g3
{
/// <summary>
/// Scalar version of a ColorMap (ie interpolate between sample points)
/// [TODO] could we make this a template?
/// </summary>
public class ScalarMap
{
struct Sample
{
public double t;
public double value;
}
List<Sample> points = new List<Sample>();
Interval1d validRange;
public ScalarMap()
{
validRange = Interval1d.Empty;
}
public void AddPoint(double t, double value)
{
Sample cp = new Sample() { t = t, value = value };
if ( points.Count == 0 ) {
points.Add(cp);
validRange.Contain(t);
} else if ( t < points[0].t ) {
points.Insert(0, cp);
validRange.Contain(t);
} else {
for ( int k = 0; k < points.Count; ++k ) {
if ( points[k].t == t ) {
points[k] = cp;
return;
} else if ( points[k].t > t ) {
points.Insert(k, cp);
return;
}
}
points.Add(cp);
validRange.Contain(t);
}
}
public double Linear(double t)
{
if (t <= points[0].t)
return points[0].value;
int N = points.Count;
if (t >= points[N - 1].t)
return points[N - 1].value;
for ( int k = 1; k < points.Count; ++k ) {
if ( points[k].t > t ) {
Sample prev = points[k - 1], next = points[k];
double a = (t - prev.t) / (next.t - prev.t);
return (1.0f - a) * prev.value + (a) * next.value;
}
}
return points[N - 1].value; // should never get here...
}
}
}