PolyVox  0.2.1
Open source voxel management library
Interpolation.h
Go to the documentation of this file.
1 /*******************************************************************************
2 Copyright (c) 2005-2009 David Williams
3 
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7 
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16 
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19 
20  3. This notice may not be removed or altered from any source
21  distribution.
22 *******************************************************************************/
23 
24 #ifndef __PolyVox_Interpolation_H__
25 #define __PolyVox_Interpolation_H__
26 
27 #include <cassert>
28 
29 namespace PolyVox
30 {
31  template <typename Type>
32  Type lerp(
33  const Type& v0,const Type& v1,
34  const float x)
35  {
36  assert((x >= 0.0f) && (x <= 1.0f));
37 
38  //Interpolate along X
39  Type v0_1 = v0 + x * (v1 - v0);
40 
41  return v0_1;
42  }
43 
44  template <typename Type>
45  Type bilerp(
46  const Type& v00,const Type& v10,const Type& v01,const Type& v11,
47  const float x, const float y)
48  {
49  assert((x >= 0.0f) && (y >= 0.0f) &&
50  (x <= 1.0f) && (y <= 1.0f));
51 
52  // Linearly interpolate along x
53  Type v00_10 = lerp(v00, v10, x);
54  Type v01_11 = lerp(v01, v11, x);
55 
56  // And linearly interpolate the results along y
57  Type v00_10__v01_11 = lerp(v00_10, v01_11, y);
58 
59  return v00_10__v01_11;
60  }
61 
62  template <typename Type>
63  Type trilerp(
64  const Type& v000,const Type& v100,const Type& v010,const Type& v110,
65  const Type& v001,const Type& v101,const Type& v011,const Type& v111,
66  const float x, const float y, const float z)
67  {
68  assert((x >= 0.0f) && (y >= 0.0f) && (z >= 0.0f) &&
69  (x <= 1.0f) && (y <= 1.0f) && (z <= 1.0f));
70 
71  // Bilinearly interpolate along Y
72  Type v000_v100__v010_v110 = bilerp(v000, v100, v010, v110, x, y);
73  Type v001_v101__v011_v111 = bilerp(v001, v101, v011, v111, x, y);
74 
75  // And linearly interpolate the results along z
76  Type v000_v100__v010_v110____v001_v101__v011_v111 = lerp(v000_v100__v010_v110, v001_v101__v011_v111, z);
77 
78  return v000_v100__v010_v110____v001_v101__v011_v111;
79  }
80 }
81 
82 #endif //__PolyVox_Interpolation_H__