FIFE  2008.0
angles.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <iostream>
00024 
00025 // 3rd party library includes
00026 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "angles.h"
00032 
00033 namespace FIFE {
00034     int getIndexByAngle(int angle, const type_angle2id& angle2id, int& closestMatchingAngle) {
00035         int wangle = (360 + angle) % 360;
00036 
00037         if (angle2id.size() == 0) {
00038             return -1;
00039         }
00040         if (angle2id.size() == 1) {
00041             closestMatchingAngle = angle2id.begin()->first;
00042             return angle2id.begin()->second;
00043         }
00044 
00045         type_angle2id::const_iterator u(angle2id.upper_bound(wangle));
00046         type_angle2id::const_iterator tmp;
00047 
00048         // take care of the forward wrapping case
00049         if (u == angle2id.end()) {
00050             int ud = wangle - (--u)->first;
00051             int ld = 360 - wangle + angle2id.begin()->first;
00052             if (ud > ld) {
00053                 // wrapped value (first)
00054                 closestMatchingAngle = angle2id.begin()->first;
00055                 return angle2id.begin()->second;
00056             }
00057             // non-wrapped value
00058             closestMatchingAngle = u->first;
00059             return u->second;
00060         }
00061 
00062         // take care of the backward wrapping case
00063         if (u == angle2id.begin()) {
00064             tmp = angle2id.end();
00065             tmp--;
00066             int ld = u->first - wangle;
00067             int ud = 360 - tmp->first + wangle;
00068             if (ud > ld) {
00069                 // non-wrapped value (first)
00070                 closestMatchingAngle = angle2id.begin()->first;
00071                 return angle2id.begin()->second;
00072             }
00073             // wrapped value (last)
00074             closestMatchingAngle = tmp->first;
00075             return tmp->second;
00076         }
00077 
00078         // value in the middle...
00079         int ud = u->first - wangle;
00080         int ucm = u->first;
00081         int ui = u->second;
00082         u--;
00083         int ld = wangle - u->first;
00084         int lcm = u->first;
00085         int li = u->second;
00086 
00087         if (ud <= ld) {
00088             closestMatchingAngle = ucm;
00089             return ui;
00090         }
00091         closestMatchingAngle = lcm;
00092         return li;
00093     }
00094 }