| VRS - The Virtual Rendering System |
| version 3.3 |
00001 /********************************************************************** 00002 VRS - The Virtual Rendering System 00003 Copyright (C) 2001 Computer Graphics Systems Group at the 00004 Hasso-Plattner-Institute, Potsdam, Germany. 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 This library is distributed in the hope that it will be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 See the GNU Lesser General Public License for more details. 00013 You should have received a copy of the GNU Lesser+ General Public 00014 License along with this library; if not, write to the FreeSoftware 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA, 02111-1307, USA. 00016 **********************************************************************/ 00017 // $Id: linearmapper.h 6457 2008-08-18 13:17:08Z Tassilo_Glander $ 00018 // $Date: 2008-08-18 15:17:08 +0200 (Mon, 18 Aug 2008) $ 00019 // $Revision: 6457 $ 00020 // $State$ 00021 // $Author: Tassilo_Glander $ 00022 // 00023 // $Log$ 00024 // Revision 1.19 2005/01/03 01:43:08 klimetschek 00025 // - modified win32 project layout, splitted vrs.vcproj into vrs_container, vrs_core, vrs_sg, vrs_image, vrs_opengl and vrs_io 00026 // - removed vrs.vcproj 00027 // minor things: 00028 // - added all glutexamples to vrs4glut.sln 00029 // - fixed some problems in glutexamples 00030 // - removed all project references from all VS projects (using solution wide project dependencies instead) 00031 // 00032 // Revision 1.18 2004/11/05 13:58:09 basch 00033 // Class comment changed (Doxygen style) 00034 // 00035 // Revision 1.17 2004/03/12 16:28:38 baumann 00036 // macros VRS_NAMESPACE_BEGIN/_END expanded and removed 00037 // 00038 // Revision 1.16 2004/02/12 08:29:07 gerber 00039 // added macro VRS_SERIALIZATION_PARENT_CLASS for parent serialization 00040 // 00041 // Revision 1.15 2004/01/28 15:26:03 wendland 00042 // updated method serialize(), with attribute names now 00043 // 00044 // Revision 1.14 2004/01/19 11:43:57 baumann 00045 // serialization mechanism improved 00046 // 00047 // Revision 1.13 2003/11/07 14:19:24 kirsch 00048 // removed leading underscore from include guards 00049 // 00050 // Revision 1.12 2002/10/29 15:03:34 baumann 00051 // bug fix for the interpolation calculation 00052 // 00053 // Revision 1.11 2002/08/06 16:24:01 baumann 00054 // VRS_CORE_API removed for template classes 00055 // 00056 // Revision 1.10 2002/07/11 14:21:53 kersting 00057 // added version for serialize method 00058 // 00059 // Revision 1.9 2002/04/25 08:12:05 kirsch 00060 // added typename where necessary 00061 // 00062 // Revision 1.8 2002/03/03 21:44:24 kosta 00063 // VRS_TYPEINFO-macro rewritten 00064 // 00065 // Revision 1.7 2002/02/19 10:34:09 kosta 00066 // undone all changes since 2002-02-15 00067 // 00068 // Revision 1.4 2002/02/14 09:37:20 kosta 00069 // automatic serialization registration for template classes implemented 00070 // 00071 // Revision 1.3 2002/02/05 07:48:25 kosta 00072 // new persistency macros: 00073 // VRS_SERIALIZABLE(CLASS_NAME); 00074 // VRS_SERIALIZABLE_ABSTRACT_CLASS(CLASS_NAME); 00075 // VRS_SERIALIZABLE_NO_SO_CLASS(CLASS_NAME) 00076 // 00077 // Revision 1.2 2002/02/04 13:07:19 kosta 00078 // VRS_SERIALIZABLE macros completely rewritten 00079 // 00080 // Revision 1.1 2002/01/27 12:22:45 kosta 00081 // initial version 00082 // 00083 00084 #ifndef VRS_LINEARMAPPER_H 00085 #define VRS_LINEARMAPPER_H 00086 00087 #include <vrs/mapper.h> 00088 00089 #include <map> 00090 00091 namespace VRS { 00092 00099 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00100 class LinearMapper : public Mapper<TARGET_TYPE, SOURCE_TYPE> { 00101 00102 00103 public: 00104 LinearMapper() { } 00106 00107 bool add(SOURCE_TYPE source, TARGET_TYPE target); 00112 bool remove(SOURCE_TYPE source) { return (valueMap_.erase(source) > 0); } 00117 virtual TARGET_TYPE map(SOURCE_TYPE source); 00134 VRS_TYPEINFO(LinearMapper, VRS_TEMPLATE_ARGS_2(TARGET_TYPE, SOURCE_TYPE, Mapper)); 00135 VRS_SERIALIZABLE(LinearMapper); 00136 00137 private: 00138 typedef std::map<SOURCE_TYPE, TARGET_TYPE> ValueMap; 00139 ValueMap valueMap_; 00140 }; 00141 00142 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00143 bool LinearMapper<TARGET_TYPE, SOURCE_TYPE>::add(SOURCE_TYPE source, TARGET_TYPE target) { 00144 return valueMap_.insert(ValueMap::value_type(source, target)).second; 00145 } 00146 00147 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00148 TARGET_TYPE LinearMapper<TARGET_TYPE, SOURCE_TYPE>::map(SOURCE_TYPE source) { 00149 VRS_Assertion(valueMap_.size() > 0, "no mapping defined"); 00150 00151 typename ValueMap::const_iterator iter1, iter2; 00152 iter1 = iter2 = valueMap_.upper_bound(source); 00153 00154 if(iter1 == valueMap_.begin()) { 00155 // case: source < first_element 00156 return (*iter1).second; 00157 } 00158 --iter1; // this is save now 00159 if(iter2 == valueMap_.end()) { 00160 // case: last_element <= source 00161 return (*iter1).second; 00162 } 00163 00164 // case: element1 <= source < element2 00165 00166 // linear interpolation of the target values (based on the source values) 00167 const double t = double(source - (*iter1).first) / double((*iter2).first - (*iter1).first); 00168 const TARGET_TYPE result = ((1.0 - t) * (*iter1).second) + (t * (*iter2).second); 00169 return result; 00170 } 00171 00172 template<typename TARGET_TYPE, typename SOURCE_TYPE> 00173 void LinearMapper<TARGET_TYPE, SOURCE_TYPE>::serialize(SerializationManager& doc) { 00174 UINT8 version = 0; 00175 doc.classVersion(version); 00176 00177 // typedef, because the serialization macro doesn't like templates with 2 template parameter 00178 typedef Mapper<TARGET_TYPE, SOURCE_TYPE> Parent; 00179 VRS_SERIALIZATION_PARENT_CLASS(doc, Parent); 00180 00181 typename ValueMap::size_type size = valueMap_.size(); 00182 serialization(doc, "size", size); 00183 00184 if (doc.isReading()) { 00185 SOURCE_TYPE source; 00186 TARGET_TYPE target; 00187 00188 valueMap_.clear(); 00189 for(typename ValueMap::size_type i = 0; i < size; ++i) { 00190 serialization(doc, "source", source); 00191 serialization(doc, "target", target); 00192 valueMap_.insert(ValueMap::value_type(source, target)); 00193 } 00194 } else { 00195 for(typename ValueMap::const_iterator iter = valueMap_.begin(), iterEnd = valueMap_.end(); iter != iterEnd; ++iter) { 00196 SOURCE_TYPE source = (*iter).first; 00197 TARGET_TYPE target = (*iter).second; 00198 serialization(doc, "source", source); 00199 serialization(doc, "target", target); 00200 } 00201 } 00202 } 00203 00204 VRS_SERIALIZATION_REGISTRATION_TEMPLATE_2(LinearMapper); 00205 00206 } // namespace VRS 00207 00208 #endif // VRS_LINEARMAPPER_H