Panda3D
physxLinearInterpolationValues.cxx
1 // Filename: physxLinearInterpolationValues.cxx
2 // Created by: enn0x (08Feb10)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "physxLinearInterpolationValues.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: PhysxLinearInterpolationValues::clear
19 // Access: Public
20 // Description:
21 ////////////////////////////////////////////////////////////////////
22 void PhysxLinearInterpolationValues::
23 clear() {
24 
25  _map.clear();
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: PhysxLinearInterpolationValues::insert
30 // Access: Public
31 // Description:
32 ////////////////////////////////////////////////////////////////////
33 void PhysxLinearInterpolationValues::
34 insert(float index, float value) {
35 
36  if (_map.empty()) {
37  _min = _max = index;
38  }
39  else {
40  _min = min(_min, index);
41  _max = max(_max, index);
42  }
43  _map[index] = value;
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: PhysxLinearInterpolationValues::is_valid
48 // Access: Public
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 bool PhysxLinearInterpolationValues::
52 is_valid(float number) const {
53 
54  return (number >= _min) && (number <= _max);
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: PhysxLinearInterpolationValues::get_size
59 // Access: Public
60 // Description:
61 ////////////////////////////////////////////////////////////////////
62 unsigned int PhysxLinearInterpolationValues::
63 get_size() const {
64 
65  return _map.size();
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: PhysxLinearInterpolationValues::get_value
70 // Access: Public
71 // Description:
72 ////////////////////////////////////////////////////////////////////
73 float PhysxLinearInterpolationValues::
74 get_value(float number) const {
75 
76  MapType::const_iterator lower = _map.begin();
77  if (number < _min) {
78  return lower->second;
79  }
80 
81  MapType::const_iterator upper = _map.end();
82  upper--;
83  if (number > _max) {
84  return upper->second;
85  }
86 
87  upper = _map.lower_bound(number);
88  if (upper == lower) {
89  return upper->second;
90  }
91 
92  lower = upper;
93  lower--;
94 
95  float w1 = number - lower->first;
96  float w2 = upper->first - number;
97 
98  return ((w2 * lower->second) + (w1 * upper->second)) / (w1 + w2);
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: PhysxLinearInterpolationValues::get_value_at_index
103 // Access: Public
104 // Description:
105 ////////////////////////////////////////////////////////////////////
106 float PhysxLinearInterpolationValues::
107 get_value_at_index(int index) const {
108 
109  MapType::const_iterator it = _map.begin();
110 
111  for (int i=0; i<index; i++) {
112  ++it;
113  }
114 
115  return it->second;
116 }
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: PhysxLinearInterpolationValues::output
120 // Access: Public
121 // Description:
122 ////////////////////////////////////////////////////////////////////
123 void PhysxLinearInterpolationValues::
124 output(ostream &out) const {
125 
126  MapType::const_iterator it = _map.begin();
127 
128  for (; it != _map.end(); ++it) {
129  cout << it->first << " -> " << it->second << "\n";
130  }
131 }
132