diff options
author | Adam Frisby | 2008-05-26 21:39:01 +0000 |
---|---|---|
committer | Adam Frisby | 2008-05-26 21:39:01 +0000 |
commit | de06c85259f45f900ca7457fae388c1a8ab871fd (patch) | |
tree | 96c63e9e4329b6eff8f1aa06c7c4aa90500ce53e /OpenSim/Region | |
parent | Thank you kindly, Melanie for a patch for script reset (diff) | |
download | opensim-SC-de06c85259f45f900ca7457fae388c1a8ab871fd.zip opensim-SC-de06c85259f45f900ca7457fae388c1a8ab871fd.tar.gz opensim-SC-de06c85259f45f900ca7457fae388c1a8ab871fd.tar.bz2 opensim-SC-de06c85259f45f900ca7457fae388c1a8ab871fd.tar.xz |
* Patch from jhurliman - Implements a binary search in the LLRAW exporter which dramatically speeds up exports.
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/LLRAW.cs | 70 |
1 files changed, 44 insertions, 26 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/LLRAW.cs b/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/LLRAW.cs index 1f6208c..505fae3 100644 --- a/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/LLRAW.cs +++ b/OpenSim/Region/Environment/Modules/World/Terrain/FileLoaders/LLRAW.cs | |||
@@ -33,6 +33,40 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders | |||
33 | { | 33 | { |
34 | public class LLRAW : ITerrainLoader | 34 | public class LLRAW : ITerrainLoader |
35 | { | 35 | { |
36 | public struct HeightmapLookupValue : IComparable<HeightmapLookupValue> | ||
37 | { | ||
38 | public int Index; | ||
39 | public double Value; | ||
40 | |||
41 | public HeightmapLookupValue(int index, double value) | ||
42 | { | ||
43 | Index = index; | ||
44 | Value = value; | ||
45 | } | ||
46 | |||
47 | public int CompareTo(HeightmapLookupValue val) | ||
48 | { | ||
49 | return Value.CompareTo(val.Value); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | /// <summary>Lookup table to speed up terrain exports</summary> | ||
54 | HeightmapLookupValue[] LookupHeightTable; | ||
55 | |||
56 | public LLRAW() | ||
57 | { | ||
58 | LookupHeightTable = new HeightmapLookupValue[256 * 256]; | ||
59 | |||
60 | for (int i = 0; i < 256; i++) | ||
61 | { | ||
62 | for (int j = 0; j < 256; j++) | ||
63 | { | ||
64 | LookupHeightTable[i + (j * 256)] = new HeightmapLookupValue(i + (j * 256), ((double)i * ((double)j / 127.0d))); | ||
65 | } | ||
66 | } | ||
67 | Array.Sort<HeightmapLookupValue>(LookupHeightTable); | ||
68 | } | ||
69 | |||
36 | #region ITerrainLoader Members | 70 | #region ITerrainLoader Members |
37 | 71 | ||
38 | public ITerrainChannel LoadFile(string filename) | 72 | public ITerrainChannel LoadFile(string filename) |
@@ -70,37 +104,21 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders | |||
70 | FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write); | 104 | FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write); |
71 | BinaryWriter binStream = new BinaryWriter(s); | 105 | BinaryWriter binStream = new BinaryWriter(s); |
72 | 106 | ||
73 | // Generate a smegging big lookup table to speed the operation up (it needs it) | ||
74 | double[] lookupHeightTable = new double[65536]; | ||
75 | int i; | ||
76 | int y; | ||
77 | for (i = 0; i < 256; i++) | ||
78 | { | ||
79 | int j; | ||
80 | for (j = 0; j < 256; j++) | ||
81 | { | ||
82 | lookupHeightTable[i + (j * 256)] = (i * (j / 127.0)); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | // Output the calculated raw | 107 | // Output the calculated raw |
87 | for (y = 0; y < map.Height; y++) | 108 | for (int y = 0; y < map.Height; y++) |
88 | { | 109 | { |
89 | int x; | 110 | for (int x = 0; x < map.Width; x++) |
90 | for (x = 0; x < map.Width; x++) | ||
91 | { | 111 | { |
92 | double t = map[x, y]; | 112 | double t = map[x, y]; |
93 | double min = double.MaxValue; | ||
94 | int index = 0; | 113 | int index = 0; |
95 | 114 | ||
96 | for (i = 0; i < 65536; i++) | 115 | // The lookup table is pre-sorted, so we either find an exact match or |
97 | { | 116 | // the next closest (smaller) match with a binary search |
98 | if (Math.Abs(t - lookupHeightTable[i]) < min) | 117 | index = Array.BinarySearch<HeightmapLookupValue>(LookupHeightTable, new HeightmapLookupValue(0, t)); |
99 | { | 118 | if (index < 0) |
100 | min = Math.Abs(t - lookupHeightTable[i]); | 119 | index = ~index - 1; |
101 | index = i; | 120 | |
102 | } | 121 | index = LookupHeightTable[index].Index; |
103 | } | ||
104 | 122 | ||
105 | byte red = (byte) (index & 0xFF); | 123 | byte red = (byte) (index & 0xFF); |
106 | byte green = (byte) ((index >> 8) & 0xFF); | 124 | byte green = (byte) ((index >> 8) & 0xFF); |
@@ -149,4 +167,4 @@ namespace OpenSim.Region.Environment.Modules.World.Terrain.FileLoaders | |||
149 | return "LL/SL RAW"; | 167 | return "LL/SL RAW"; |
150 | } | 168 | } |
151 | } | 169 | } |
152 | } \ No newline at end of file | 170 | } |