aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Common.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Common.cs')
-rw-r--r--OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Common.cs228
1 files changed, 228 insertions, 0 deletions
diff --git a/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Common.cs b/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Common.cs
new file mode 100644
index 0000000..438359c
--- /dev/null
+++ b/OpenSim/Region/Terrain.BasicTerrain/libTerrainBSD/Channel/Common.cs
@@ -0,0 +1,228 @@
1/*
2Redistribution and use in source and binary forms, with or without
3modification, are permitted provided that the following conditions are
4met:
5
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13
14 * Neither the name of libTerrain nor the names of
15 its contributors may be used to endorse or promote products
16 derived from this software without specific prior written
17 permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*/
31
32
33using System;
34using System.Collections.Generic;
35using System.Text;
36
37namespace libTerrain
38{
39 public partial class Channel
40 {
41 public int getWidth()
42 {
43 return w;
44 }
45 public int getHeight()
46 {
47 return h;
48 }
49
50 public Channel copy()
51 {
52 Channel x = new Channel(w, h);
53 x.map = (double[,])this.map.Clone();
54 return x;
55 }
56
57 public void set(int x, int y, double val)
58 {
59 if (x >= w)
60 throw new Exception("Bounds error while setting pixel (width)");
61 if (y >= h)
62 throw new Exception("Bounds error while setting pixel (height)");
63 if (x < 0)
64 throw new Exception("Bounds error while setting pixel (width)");
65 if (y < 0)
66 throw new Exception("Bounds error while setting pixel (height)");
67
68 map[x, y] = val;
69 }
70
71 public void setClip(int x, int y, double val)
72 {
73 if (x >= w)
74 throw new Exception("Bounds error while setting pixel (width)");
75 if (y >= h)
76 throw new Exception("Bounds error while setting pixel (height)");
77 if (x < 0)
78 throw new Exception("Bounds error while setting pixel (width)");
79 if (y < 0)
80 throw new Exception("Bounds error while setting pixel (height)");
81
82 if (val > 1.0)
83 val = 1.0;
84 if (val < 0.0)
85 val = 0.0;
86
87 map[x, y] = val;
88 }
89
90 private double getBilinearInterpolate(double x, double y)
91 {
92 if (x > w - 2.0)
93 x = w - 2.0;
94 if (y > h - 2.0)
95 y = h - 2.0;
96 if (x < 0.0)
97 x = 0.0;
98 if (y < 0.0)
99 y = 0.0;
100
101 int STEP_SIZE = 1;
102 double h00 = get((int)x, (int)y);
103 double h10 = get((int)x + STEP_SIZE, (int)y);
104 double h01 = get((int)x, (int)y + STEP_SIZE);
105 double h11 = get((int)x + STEP_SIZE, (int)y + STEP_SIZE);
106 double h1 = h00;
107 double h2 = h10;
108 double h3 = h01;
109 double h4 = h11;
110 double a00 = h1;
111 double a10 = h2 - h1;
112 double a01 = h3 - h1;
113 double a11 = h1 - h2 - h3 + h4;
114 double partialx = x - (int)x;
115 double partialz = y - (int)y;
116 double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz);
117 return hi;
118 }
119
120 public double get(int x, int y)
121 {
122 if (x >= w)
123 x = w - 1;
124 if (y >= h)
125 y = h - 1;
126 if (x < 0)
127 x = 0;
128 if (y < 0)
129 y = 0;
130 return map[x, y];
131 }
132
133 public void setWrap(int x, int y, double val)
134 {
135 map[x % w, y % h] = val;
136 }
137
138 public void setWrapClip(int x, int y, double val)
139 {
140 if (val > 1.0)
141 val = 1.0;
142 if (val < 0.0)
143 val = 0.0;
144
145 map[x % w, y % h] = val;
146 }
147
148 public void fill(double val)
149 {
150 int x, y;
151 for (x = 0; x < w; x++)
152 {
153 for (y = 0; y < h; y++)
154 {
155 map[x, y] = val;
156 }
157 }
158 }
159
160 public void fill(double min, double max, double val)
161 {
162 int x, y;
163 for (x = 0; x < w; x++)
164 {
165 for (y = 0; y < h; y++)
166 {
167 if (map[x, y] >= min && map[x, y] <= max)
168 map[x, y] = val;
169 }
170 }
171 }
172
173 public double findMax()
174 {
175 int x, y;
176 double max = double.MinValue;
177
178 for (x = 0; x < w; x++)
179 {
180 for (y = 0; y < h; y++)
181 {
182 if (map[x, y] > max)
183 max = map[x, y];
184 }
185 }
186
187 return max;
188 }
189
190 public double findMin()
191 {
192 int x, y;
193 double min = double.MaxValue;
194
195 for (x = 0; x < w; x++)
196 {
197 for (y = 0; y < h; y++)
198 {
199 if (map[x, y] < min)
200 min = map[x, y];
201 }
202 }
203
204 return min;
205 }
206
207 public double sum()
208 {
209 int x, y;
210 double sum = 0.0;
211
212 for (x = 0; x < w; x++)
213 {
214 for (y = 0; y < h; y++)
215 {
216 sum += map[x, y];
217 }
218 }
219
220 return sum;
221 }
222
223 public double avg()
224 {
225 return sum() / (w * h);
226 }
227 }
228}