1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 MartiƱo Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#ifndef _SHARED_GRAPHICS_MATHUTIL_H_
#define _SHARED_GRAPHICS_MATHUTIL_H_
#include "math_wrapper.h"
#include "vec.h"
#include "data_types.h"
#include "leak_dumper.h"
using namespace std;
using namespace Shared::Platform;
namespace Shared{ namespace Graphics{
const float pi= 3.1415926f;
const float sqrt2= 1.41421356f;
const float zero= 1e-6f;
const float infinity= 1e6f;
// =====================================================
// class Rect
// =====================================================
// 0 +-+
// | |
// +-+ 1
template<typename T>
class Rect2{
public:
Vec2<T> p[2];
public:
Rect2(){
};
Rect2(const Vec2<T> &p0, const Vec2<T> &p1){
this->p[0]= p0;
this->p[1]= p1;
}
Rect2(T p0x, T p0y, T p1x, T p1y){
p[0].x= p0x;
p[0].y= p0y;
p[1].x= p1x;
p[1].y= p1y;
}
Rect2<T> operator*(T scalar){
return Rect2<T>(
p[0]*scalar,
p[1]*scalar);
}
Rect2<T> operator/(T scalar){
return Rect2<T>(
p[0]/scalar,
p[1]/scalar);
}
bool isInside(const Vec2<T> &p) const{
return
p.x>=this->p[0].x &&
p.y>=this->p[0].y &&
p.x<this->p[1].x &&
p.y<this->p[1].y;
}
void clamp(T minX, T minY,T maxX, T maxY){
for(int i=0; i<2; ++i){
if(p[i].x<minX){
p[i].x= minX;
}
if(p[i].y<minY){
p[i].y= minY;
}
if(p[i].x>maxX){
p[i].x= maxX;
}
if(p[i].y>maxY){
p[i].y= maxY;
}
}
}
std::string getString() const {
std::ostringstream streamOut;
streamOut << "#1: " << this->p[0].getString();
streamOut << "#2: " << this->p[1].getString();
std::string result = streamOut.str();
streamOut.str(std::string());
return result;
}
};
typedef Rect2<int> Rect2i;
typedef Rect2<char> Rect2c;
typedef Rect2<float> Rect2f;
typedef Rect2<double> Rect2d;
// =====================================================
// class Quad
// =====================================================
// 0 +-+ 2
// | |
// 1 +-+ 3
template<typename T>
class Quad2{
public:
Vec2<T> p[4];
public:
Quad2(){
};
Quad2(const Vec2<T> &p0, const Vec2<T> &p1, const Vec2<T> &p2, const Vec2<T> &p3){
this->p[0]= p0;
this->p[1]= p1;
this->p[2]= p2;
this->p[3]= p3;
}
explicit Quad2(const Rect2<T> &rect){
this->p[0]= rect.p[0];
this->p[1]= Vec2<T>(rect.p[0].x, rect.p[1].y);
this->p[2]= rect.p[1];
this->p[3]= Vec2<T>(rect.p[1].x, rect.p[0].y);
}
Quad2<T> operator*(T scalar){
return Quad2<T>(
p[0]*scalar,
p[1]*scalar,
p[2]*scalar,
p[3]*scalar);
}
Quad2<T> operator/(T scalar){
return Quad2<T>(
p[0]/scalar,
p[1]/scalar,
p[2]/scalar,
p[3]/scalar);
}
bool operator <(const Quad2<T> &v) const {
if(p[0] < v.p[0]) {
return true;
}
if(p[1] < v.p[1]) {
return true;
}
if(p[2] < v.p[2]) {
return true;
}
if(p[3] < v.p[3]) {
return true;
}
return false;
}
bool operator !=(const Quad2<T> &v) const {
if(p[0] != v.p[0]) {
return true;
}
if(p[1] != v.p[1]) {
return true;
}
if(p[2] != v.p[2]) {
return true;
}
if(p[3] != v.p[3]) {
return true;
}
return false;
}
Rect2<T> computeBoundingRect() const{
return Rect2i(
#ifdef WIN32
min(p[0].x, p[1].x),
min(p[0].y, p[2].y),
max(p[2].x, p[3].x),
max(p[1].y, p[3].y));
#else
std::min(p[0].x, p[1].x),
std::min(p[0].y, p[2].y),
std::max(p[2].x, p[3].x),
std::max(p[1].y, p[3].y));
#endif
}
bool isInside(const Vec2<T> &pt) const{
if(!computeBoundingRect().isInside(pt))
return false;
bool left[4];
left[0]= (pt.y - p[0].y)*(p[1].x - p[0].x) - (pt.x - p[0].x)*(p[1].y - p[0].y) < 0;
left[1]= (pt.y - p[1].y)*(p[3].x - p[1].x) - (pt.x - p[1].x)*(p[3].y - p[1].y) < 0;
left[2]= (pt.y - p[3].y)*(p[2].x - p[3].x) - (pt.x - p[3].x)*(p[2].y - p[3].y) < 0;
left[3]= (pt.y - p[2].y)*(p[0].x - p[2].x) - (pt.x - p[2].x)*(p[0].y - p[2].y) < 0;
return left[0] && left[1] && left[2] && left[3];
}
void clamp(T minX, T minY, T maxX, T maxY){
for(int i=0; i<4; ++i){
if(p[i].x<minX){
p[i].x= minX;
}
if(p[i].y<minY){
p[i].y= minY;
}
if(p[i].x>maxX){
p[i].x= maxX;
}
if(p[i].y>maxY){
p[i].y= maxY;
}
}
}
float area() {
Vec2i v0= p[3]-p[0];
Vec2i v1= p[1]-p[2];
return 0.5f * ((v0.x * v1.y) - (v0.y * v1.x));
}
std::string getString() const {
std::ostringstream streamOut;
streamOut << "#1: " << this->p[0].getString();
streamOut << "#2: " << this->p[1].getString();
streamOut << "#3: " << this->p[2].getString();
streamOut << "#4: " << this->p[3].getString();
std::string result = streamOut.str();
streamOut.str(std::string());
return result;
}
};
typedef Quad2<int> Quad2i;
typedef Quad2<char> Quad2c;
typedef Quad2<float> Quad2f;
typedef Quad2<double> Quad2d;
// =====================================================
// Misc
// =====================================================
inline int next2Power(int n){
int i;
for (i=1; i<n; i*=2);
return i;
}
template<typename T>
inline T degToRad(T deg){
return (deg*2*pi)/360;
}
template<typename T>
inline T radToDeg(T rad){
return (rad*360)/(2*pi);
}
// ====================================================================================================================
// ====================================================================================================================
// Inline implementation
// ====================================================================================================================
// ====================================================================================================================
//#if _xs_BigEndian_
// #define _xs_iexp_ 0
// #define _xs_iman_ 1
//#else
// #define _xs_iexp_ 1 //intel is little endian
// #define _xs_iman_ 0
//#endif //BigEndian_
//
////#define finline __forceinline
//#define finline inline
//
//#ifndef _xs_DEFAULT_CONVERSION
//#define _xs_DEFAULT_CONVERSION 0
//#endif
//
////typedef long int32;
//typedef double real64;
//const real64 _xs_doublemagic = real64 (6755399441055744.0); //2^52 * 1.5, uses limited precisicion to floor
//
//finline int32 xs_CRoundToInt(real64 val, real64 dmr = _xs_doublemagic) {
//#if _xs_DEFAULT_CONVERSION==0
// val = val + dmr;
// return ((int32*)&val)[_xs_iman_];
// //return 0;
//#else
// return int32(floor(val+.5));
//#endif
//}
}}//end namespace
#endif
|