File: math_util.h

package info (click to toggle)
glest 3.1.2-1
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 3,496 kB
  • ctags: 6,107
  • sloc: cpp: 30,065; sh: 8,293; makefile: 48
file content (211 lines) | stat: -rw-r--r-- 4,114 bytes parent folder | download
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
// ==============================================================
//	This file is part of Glest Shared Library (www.glest.org)
//
//	Copyright (C) 2001-2008 Martio 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 <cmath>

#include "vec.h"

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;
			}
		}
	}
};

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);
	}

	Rect2<T> computeBoundingRect() const{
		return Rect2i(
			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));
	}

	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;
			}
		}
	}
};

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);
}

}}//end namespace

#endif