Panda3D
 All Classes Functions Variables Enumerations
physxLinearInterpolationValues.cxx
00001 // Filename: physxLinearInterpolationValues.cxx
00002 // Created by:  enn0x (08Feb10)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "physxLinearInterpolationValues.h"
00016 
00017 ////////////////////////////////////////////////////////////////////
00018 //     Function: PhysxLinearInterpolationValues::clear
00019 //       Access: Public
00020 //  Description: 
00021 ////////////////////////////////////////////////////////////////////
00022 void PhysxLinearInterpolationValues::
00023 clear() {
00024 
00025   _map.clear();
00026 }
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: PhysxLinearInterpolationValues::insert
00030 //       Access: Public
00031 //  Description: 
00032 ////////////////////////////////////////////////////////////////////
00033 void PhysxLinearInterpolationValues::
00034 insert(float index, float value) {
00035 
00036   if (_map.empty()) {
00037     _min = _max = index;
00038   }
00039   else {
00040     _min = min(_min, index);
00041     _max = max(_max, index);
00042   }
00043   _map[index] = value;
00044 }
00045 
00046 ////////////////////////////////////////////////////////////////////
00047 //     Function: PhysxLinearInterpolationValues::is_valid
00048 //       Access: Public
00049 //  Description: 
00050 ////////////////////////////////////////////////////////////////////
00051 bool PhysxLinearInterpolationValues::
00052 is_valid(float number) const {
00053 
00054   return (number >= _min) && (number <= _max);
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: PhysxLinearInterpolationValues::get_size
00059 //       Access: Public
00060 //  Description: 
00061 ////////////////////////////////////////////////////////////////////
00062 unsigned int PhysxLinearInterpolationValues::
00063 get_size() const {
00064 
00065   return _map.size();
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: PhysxLinearInterpolationValues::get_value
00070 //       Access: Public
00071 //  Description: 
00072 ////////////////////////////////////////////////////////////////////
00073 float PhysxLinearInterpolationValues::
00074 get_value(float number) const {
00075 
00076   MapType::const_iterator lower = _map.begin();
00077   if (number < _min) {
00078     return lower->second;
00079   }
00080 
00081   MapType::const_iterator upper = _map.end();
00082   upper--;
00083   if (number > _max) {
00084     return upper->second;
00085   }
00086 
00087   upper = _map.lower_bound(number);
00088   if (upper == lower) {
00089     return upper->second;
00090   }
00091 
00092   lower = upper;
00093   lower--;
00094     
00095   float w1 = number - lower->first;
00096   float w2 = upper->first - number;
00097 
00098   return ((w2 * lower->second) + (w1 * upper->second)) / (w1 + w2);
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: PhysxLinearInterpolationValues::get_value_at_index
00103 //       Access: Public
00104 //  Description: 
00105 ////////////////////////////////////////////////////////////////////
00106 float PhysxLinearInterpolationValues::
00107 get_value_at_index(int index) const {
00108 
00109   MapType::const_iterator it = _map.begin();
00110 
00111   for (int i=0; i<index; i++) {
00112     ++it;
00113   }
00114 
00115   return it->second;
00116 }
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: PhysxLinearInterpolationValues::output
00120 //       Access: Public
00121 //  Description: 
00122 ////////////////////////////////////////////////////////////////////
00123 void PhysxLinearInterpolationValues::
00124 output(ostream &out) const {
00125 
00126   MapType::const_iterator it = _map.begin();
00127 
00128   for (; it != _map.end(); ++it) {
00129     cout << it->first << " -> " << it->second << "\n";
00130   }
00131 }
00132 
 All Classes Functions Variables Enumerations