3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
25 #include "irrlichtwrapper.h"
28 TimeTaker::TimeTaker(const char *name, u32 *result)
33 m_time1 = getTimeMs();
36 u32 TimeTaker::stop(bool quiet)
40 u32 time2 = getTimeMs();
41 u32 dtime = time2 - m_time1;
49 std::cout<<m_name<<" took "<<dtime<<"ms"<<std::endl;
57 const v3s16 g_26dirs[26] =
59 // +right, +top, +back
60 v3s16( 0, 0, 1), // back
61 v3s16( 0, 1, 0), // top
62 v3s16( 1, 0, 0), // right
63 v3s16( 0, 0,-1), // front
64 v3s16( 0,-1, 0), // bottom
65 v3s16(-1, 0, 0), // left
67 v3s16(-1, 1, 0), // top left
68 v3s16( 1, 1, 0), // top right
69 v3s16( 0, 1, 1), // top back
70 v3s16( 0, 1,-1), // top front
71 v3s16(-1, 0, 1), // back left
72 v3s16( 1, 0, 1), // back right
73 v3s16(-1, 0,-1), // front left
74 v3s16( 1, 0,-1), // front right
75 v3s16(-1,-1, 0), // bottom left
76 v3s16( 1,-1, 0), // bottom right
77 v3s16( 0,-1, 1), // bottom back
78 v3s16( 0,-1,-1), // bottom front
80 v3s16(-1, 1, 1), // top back-left
81 v3s16( 1, 1, 1), // top back-right
82 v3s16(-1, 1,-1), // top front-left
83 v3s16( 1, 1,-1), // top front-right
84 v3s16(-1,-1, 1), // bottom back-left
85 v3s16( 1,-1, 1), // bottom back-right
86 v3s16(-1,-1,-1), // bottom front-left
87 v3s16( 1,-1,-1), // bottom front-right
91 static unsigned long next = 1;
93 /* RAND_MAX assumed to be 32767 */
96 next = next * 1103515245 + 12345;
97 return((unsigned)(next/65536) % 32768);
100 void mysrand(unsigned seed)
105 // Float with distance
112 float PointAttributeList::getInterpolatedFloat(v3s16 p)
114 const u32 near_wanted_count = 5;
115 // Last is nearest, first is farthest
116 core::list<DFloat> near;
118 for(core::list<PointWithAttr>::Iterator
119 i = m_points.begin();
120 i != m_points.end(); i++)
122 PointWithAttr &pwa = *i;
123 u32 d = pwa.p.getDistanceFrom(p);
126 df.v = pwa.attr.getFloat();
129 // If near list is empty, add directly and continue
136 // Get distance of farthest in near list
140 core::list<DFloat>::Iterator i = near.begin();
145 If point is closer than the farthest in the near list or
146 there are not yet enough points on the list
148 if(d < near_d || near.size() < near_wanted_count)
150 // Find the right place in the near list and put it there
152 // Go from farthest to near in the near list
153 core::list<DFloat>::Iterator i = near.begin();
154 for(; i != near.end(); i++)
156 // Stop when i is at the first nearer node
160 // Add df to before i
164 near.insert_before(i, df);
166 // Keep near list at right size
167 if(near.size() > near_wanted_count)
169 core::list<DFloat>::Iterator j = near.begin();
175 // Return if no values found
180 20:58:29 < tejeez> joka pisteelle a += arvo / etäisyys^6; b += 1 / etäisyys^6; ja
181 lopuks sit otetaan a/b
186 for(core::list<DFloat>::Iterator i = near.begin();
187 i != near.end(); i++)
192 //float dd = pow((float)i->d, 6);
193 float dd = pow((float)i->d, 5);
195 //dstream<<"dd="<<dd<<", v="<<v<<std::endl;
204 float PointAttributeList::getInterpolatedFloat(v3s16 p)
206 const u32 near_wanted_count = 2;
207 const u32 nearest_wanted_count = 2;
209 core::list<DFloat> near;
211 for(core::list<PointWithAttr>::Iterator
212 i = m_points.begin();
213 i != m_points.end(); i++)
215 PointWithAttr &pwa = *i;
216 u32 d = pwa.p.getDistanceFrom(p);
219 df.v = pwa.attr.getFloat();
222 // If near list is empty, add directly and continue
229 // Get distance of farthest in near list
233 core::list<DFloat>::Iterator i = near.begin();
238 If point is closer than the farthest in the near list or
239 there are not yet enough points on the list
241 if(d < near_d || near.size() < near_wanted_count)
243 // Find the right place in the near list and put it there
245 // Go from farthest to near in the near list
246 core::list<DFloat>::Iterator i = near.begin();
247 for(; i != near.end(); i++)
249 // Stop when i is at the first nearer node
253 // Add df to before i
257 near.insert_before(i, df);
259 // Keep near list at right size
260 if(near.size() > near_wanted_count)
262 core::list<DFloat>::Iterator j = near.begin();
268 // Return if no values found
276 u32 nearest_count = nearest_wanted_count;
277 if(nearest_count > near.size())
278 nearest_count = near.size();
279 core::list<DFloat> nearest;
281 core::list<DFloat>::Iterator i = near.getLast();
282 for(u32 j=0; j<nearest_count; j++)
284 nearest.push_front(*i);
291 20:58:29 < tejeez> joka pisteelle a += arvo / etäisyys^6; b += 1 / etäisyys^6; ja
292 lopuks sit otetaan a/b
296 Get total distance to nearest points
299 float nearest_d_sum = 0;
300 for(core::list<DFloat>::Iterator i = nearest.begin();
301 i != nearest.end(); i++)
303 nearest_d_sum += (float)i->d;
307 Interpolate a value between the first ones
310 dstream<<"nearest.size()="<<nearest.size()<<std::endl;
312 float interpolated = 0;
314 for(core::list<DFloat>::Iterator i = nearest.begin();
315 i != nearest.end(); i++)
318 if(nearest_d_sum > 0.001)
319 weight = (float)i->d / nearest_d_sum;
321 weight = 1. / nearest.size();
322 /*dstream<<"i->d="<<i->d<<" nearest_d_sum="<<nearest_d_sum
323 <<" weight="<<weight<<std::endl;*/
324 interpolated += weight * i->v;