rect.h

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 /***************************************************************************
00023 
00024     Rectangle intersection code copied and modified from the guichan 0.4
00025     source, which is released under the BSD license.
00026 
00027     Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved.
00028 
00029     * Redistribution and use in source and binary forms, with or without modification,
00030       are permitted provided that the following conditions are met:
00031       Redistributions of source code must retain the above copyright notice,
00032       this list of conditions and the following disclaimer.
00033 
00034     * Redistributions in binary form must reproduce the above copyright notice,
00035       this list of conditions and the following disclaimer in the documentation
00036       and/or other materials provided with the distribution.
00037 
00038     * Neither the name of the Guichan nor the names of its contributors may be used
00039       to endorse or promote products derived from this software without specific
00040       prior written permission.
00041 
00042  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
00043  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
00044  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00045  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
00046  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00047  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00048  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00049  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00050  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00051 
00052     For more Information about guichan see: http://guichan.sourceforge.net
00053 
00054 ****************************************************************************/
00055 
00056 #ifndef FIFE_VIDEO_RECT_H
00057 #define FIFE_VIDEO_RECT_H
00058 
00059 // Standard C++ library includes
00060 #include <iostream>
00061 
00062 // 3rd party library includes
00063 
00064 // FIFE includes
00065 // These includes are split up in two parts, separated by one empty line
00066 // First block: files included from the FIFE root src directory
00067 // Second block: files included from the same folder
00068 #include "point.h"
00069 
00070 namespace FIFE {
00071 
00079     template <typename T>
00080     class RectType {
00081         public:
00084             T x;
00087             T y;
00090             T w;
00093             T h;
00094 
00099             explicit RectType(T x = 0, T y = 0, T w = 0, T h = 0) : x(x), y(y), w(w), h(h) {
00100             }
00101 
00106             template<typename U>
00107             explicit RectType(const RectType<U>& r)
00108                 : x(static_cast<T>(r.x)), 
00109                 y(static_cast<T>(r.y)), 
00110                 w(static_cast<T>(r.w)), 
00111                 h(static_cast<T>(r.h)) {
00112             }
00113 
00116             T right() const;
00117 
00120             T bottom() const;
00121 
00127             bool operator==(const RectType<T>& rect ) const;
00128 
00134             bool contains( const PointType2D<T>& point ) const;
00135 
00143             bool intersects( const RectType<T>& rect ) const;
00144 
00151             bool intersectInplace( const RectType<T>& rect );
00152     };
00153 
00159     template<typename T>
00160     std::ostream& operator<<(std::ostream& os, const RectType<T>& r) {
00161         return
00162             os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")";
00163     }
00164 
00166 
00167     template<typename T>
00168     inline T RectType<T>::right() const {
00169         return x + w;
00170     }
00171 
00172     template<typename T>
00173     inline T RectType<T>::bottom() const {
00174         return y + h;
00175     }
00176 
00177     template<typename T>
00178     inline bool RectType<T>::operator==(const RectType<T>& rect ) const {
00179         return
00180             x == rect.x && y == rect.y && w == rect.w && h == rect.h;
00181     }
00182 
00183     template<typename T>
00184     inline bool RectType<T>::contains( const PointType2D<T>& point ) const {
00185         return
00186             (((point.x >= x) && (point.x <= x + w))
00187                 && ((point.y >= y) && (point.y <= y + h)));
00188     }
00189 
00190 
00191     template<typename T>
00192     inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) {
00193         x = x - rectangle.x;
00194         y = y - rectangle.y;
00195 
00196 
00197         if (x < 0) {
00198             w += x;
00199             x = 0;
00200         }
00201 
00202         if (y < 0) {
00203             h += y;
00204             y = 0;
00205         }
00206 
00207         if (x + w > rectangle.w) {
00208             w = rectangle.w - x;
00209         }
00210 
00211         if (y + h > rectangle.h) {
00212             h = rectangle.h - y;
00213         }
00214 
00215         x += rectangle.x;
00216         y += rectangle.y;
00217 
00218         if (w <= 0 || h <= 0) {
00219             h = 0;
00220             w = 0;
00221             return false;
00222         }
00223         return true;
00224     }
00225 
00226     template<typename T>
00227     inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const {
00228         T _x = x - rectangle.x;
00229         T _y = y - rectangle.y;
00230         T _w = w;
00231         T _h = h;
00232 
00233 
00234         if (_x < 0) {
00235             _w += _x;
00236             _x = 0;
00237         }
00238 
00239         if (_y < 0) {
00240             _h += _y;
00241             _y = 0;
00242         }
00243 
00244         if (_x + _w > rectangle.w) {
00245             _w = rectangle.w - _x;
00246         }
00247 
00248         if (_y + _h > rectangle.h) {
00249             _h = rectangle.h - _y;
00250         }
00251 
00252         if (_w <= 0 || _h <= 0) {
00253             return false;
00254         }
00255         return true;
00256     }
00257 
00258     typedef RectType<int32_t> Rect;
00259     typedef RectType<float> FloatRect;
00260     typedef RectType<double> DoubleRect;
00261 
00262 
00263 }
00264 
00265 #endif