File: type.h

package info (click to toggle)
mathgl 8.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 248,044 kB
  • sloc: cpp: 87,365; ansic: 3,299; javascript: 3,284; pascal: 1,562; python: 52; sh: 51; makefile: 47; f90: 22
file content (336 lines) | stat: -rw-r--r-- 13,489 bytes parent folder | download | duplicates (2)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/***************************************************************************
 * type.h is part of Math Graphic Library
 * Copyright (C) 2007-2016 Alexey Balakin <mathgl.abalakin@gmail.ru>       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License  as       *
 *   published by the Free Software Foundation; either version 3 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef _MGL_TYPE_H_
#define _MGL_TYPE_H_

#include "mgl2/define.h"
//-----------------------------------------------------------------------------
const mreal mglPi = M_PI;
const mreal mglNaN = NAN;
const mreal mglInf = INFINITY;
const mreal mgl_min_a = 1./256;
//-----------------------------------------------------------------------------
#define MGL_SET_XYZ(p,xx,yy,zz)		{p.x=(xx);p.y=(yy);p.z=(zz);}
#define MGL_SET_XY(p,xx,yy)			{p.x=(xx);p.y=(yy);p.z=0;}
#define MGL_SET_RGBA(p,rr,gg,bb,aa)	{p.r=(rr);p.g=(gg);p.b=(bb);p.a=(aa);}
#define MGL_SET_RGB(p,rr,gg,bb)		{p.r=(rr);p.g=(gg);p.b=(bb);}
//-----------------------------------------------------------------------------
/// Class for point in 3D space
struct MGL_EXPORT mglPoint
{
	mreal x,y,z,c;
	mglPoint(mreal X=0,mreal Y=0,mreal Z=0,mreal C=0):x(X),y(Y),z(Z),c(C) {}
	mglPoint(const mglPoint &d):x(d.x),y(d.y),z(d.z),c(d.c) {}
#if MGL_HAVE_RVAL
	mglPoint(mglPoint &&d):x(d.x),y(d.y),z(d.z),c(d.c)	{}
#endif
	inline void Set(mreal X=0,mreal Y=0,mreal Z=0,mreal C=0)	{x=X;y=Y;z=Z;c=C;}
	inline bool IsNAN() const		{	return (x!=x || y!=y || z!=z || c!=c);	}
	inline mreal val(int i) const	{	mreal dat[4]={x,y,z,c};	return dat[i];	}
	inline mreal norm() const		{	return sqrt(x*x+y*y+z*z);	}
	inline void Normalize()	{	mreal v=norm();	x/=v;	y/=v;	z/=v;	}
	inline bool same(const mglPoint &a) const	// NOTE: exact comparison is used here
	{	return (a.x-x)*(a.x-x)+(a.y-y)*(a.y-y)==0;	}

	inline const mglPoint &operator=(const mglPoint &p)
	{	x=p.x;	y=p.y;	z=p.z;	c=p.c;	return p;	}
	inline void operator+=(const mglPoint &a)	{	x+=a.x;	y+=a.y;	z+=a.z;	c+=a.c;	}
	inline void operator-=(const mglPoint &a)	{	x-=a.x;	y-=a.y;	z-=a.z;	c-=a.c;	}
	inline void operator+=(mreal a)	{	x+=a;	y+=a;	z+=a;	}
	inline void operator-=(mreal a)	{	x-=a;	y-=a;	z-=a;	}
	inline void operator*=(mreal a)	{	x*=a;	y*=a;	z*=a;	}
	inline void operator/=(mreal a)	{	x/=a;	y/=a;	z/=a;	}
};
#ifndef SWIG
inline mglPoint operator+(const mglPoint &a, const mglPoint &b)
{	return mglPoint(a.x+b.x, a.y+b.y, a.z+b.z, a.c+b.c);	}
inline mglPoint operator-(const mglPoint &a, const mglPoint &b)
{	return mglPoint(a.x-b.x, a.y-b.y, a.z-b.z, a.c-b.c);	}
inline mglPoint operator-(const mglPoint &a)
{	return mglPoint(-a.x, -a.y, -a.z, -a.c);	}
inline mglPoint operator*(mreal b, const mglPoint &a)
{	return mglPoint(a.x*b, a.y*b, a.z*b, a.c*b);	}
inline mglPoint operator*(const mglPoint &a, mreal b)
{	return mglPoint(a.x*b, a.y*b, a.z*b, a.c*b);	}
inline mglPoint operator/(const mglPoint &a, mreal b)
{	return mglPoint(a.x/b, a.y/b, a.z/b);	}
inline mreal operator*(const mglPoint &a, const mglPoint &b)
{	return a.x*b.x+a.y*b.y+a.z*b.z;	}
inline mglPoint operator/(const mglPoint &a, const mglPoint &b)
{	return mglPoint(a.x*b.x, a.y*b.y, a.z*b.z);	}
inline mglPoint operator&(const mglPoint &a, const mglPoint &b)
{	return a - b*((a*b)/(b*b));	}
inline mglPoint operator|(const mglPoint &a, const mglPoint &b)
{	return b*((a*b)/(b*b));	}
inline mglPoint operator^(const mglPoint &a, const mglPoint &b)
{	return mglPoint(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);	}
inline mglPoint operator!(const mglPoint &a)
{	mreal f=mgl_hypot(a.x,a.y);	return f==0?mglPoint(0.,1.,0.):mglPoint(-a.y/f, a.x/f, 0);	}
inline bool operator==(const mglPoint &a, const mglPoint &b)	// NOTE: exact comparison is used here
{	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)+(a.c-b.c)*(a.c-b.c)==0;	}
//{	return !memcmp(&a, &b, sizeof(mglPoint));	}
inline bool operator!=(const mglPoint &a, const mglPoint &b)	// NOTE: exact comparison is used here
{	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)+(a.c-b.c)*(a.c-b.c)!=0;	}
//{	return memcmp(&a, &b, sizeof(mglPoint));	}
inline bool operator<(const mglPoint &a, const mglPoint &b)
{	return a.x<=b.x && a.y<=b.y && a.z<=b.z;	}
inline bool operator>(const mglPoint &a, const mglPoint &b)
{	return a.x>=b.x && a.y>=b.y && a.z>=b.z;	}
inline mreal mgl_norm(const mglPoint &p)
{	return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);	}
inline mreal mgl_anorm(const mglPoint &p)
{	return fabs(p.x)+fabs(p.y)+fabs(p.z);	}
#endif
//-----------------------------------------------------------------------------
/// Class for RGBA color
struct MGL_EXPORT mglColor
{
	float r;	///< Red component of color
	float g;	///< Green component of color
	float b;	///< Blue component of color
	float a;	///< Alpha component of color

	/// Constructor for RGB components manually
	mglColor(float R,float G,float B, float A=1):r(R),g(G),b(B),a(A)	{}
	/// Constructor for RGB components manually
	mglColor(const unsigned char *c, float A=1):r(c[0]/255.f),g(c[1]/255.f),b(c[2]/255.f),a(A)	{}
	/// Constructor set default color
	mglColor():r(0),g(0),b(0),a(1)	{}
	/// Constructor set color from character id
	mglColor(char c, float bright=1)		{	Set(c,bright);	}
	/// Copy constructor
	mglColor(const mglColor &d):r(d.r),g(d.g),b(d.b),a(d.a)	{}
#if MGL_HAVE_RVAL
	mglColor(mglColor &&d):r(d.r),g(d.g),b(d.b),a(d.a)	{}
#endif
	/// Set color as Red, Green, Blue values
	void Set(float R,float G,float B,float A=1)	{	r=R;	g=G;	b=B;	a=A;	}
	/// Set color as Red, Green, Blue values
	void Set(mglColor c, float bright=1)
	{
		if(bright<0)	bright=0;
		if(bright>2.f)	bright=2.f;
		if(bright<=1)
		{	r=c.r*bright;	g=c.g*bright;	b=c.b*bright;	a = 1;	}
		else
		{	r=1-(1-c.r)*(2-bright);	g=1-(1-c.g)*(2-bright);	b=1-(1-c.b)*(2-bright);	a=1;	}
	}
	/// Check if color is valid
	inline bool Valid() const
	{	return ((r>=0) & (r<=1) & (g>=0) & (g<=1) & (b>=0) & (b<=1) & (a>=0) & (a<=1));	}
	/// Get maximal spectral component
	inline float Norm() const
	{	return r>g ? r : (g>b ? g : b);	}
	inline float NormS() const
	{	return r*r+g*g+b*b;	}
	/// Set color from symbolic id
	inline void Set(char p, float bright=1)
	{
		float rgb[3];	mgl_chrrgb(p,rgb);
		Set(mglColor(rgb[0],rgb[1],rgb[2]),bright);
	}
	inline const mglColor &operator=(const mglColor &p)
	{	r=p.r;	g=p.g;	b=p.b;	a=p.a;	return p;	}
	/// Copy color from other one
	inline bool operator==(const mglColor &c) const	// NOTE: exact comparison is used here
	{	return (r-c.r)*(r-c.r)+(g-c.g)*(g-c.g)+(b-c.b)*(b-c.b)+(a-c.a)*(a-c.a)==0;	}
//	{	return !memcmp(this, &c, sizeof(mglColor));	}
	inline bool operator!=(const mglColor &c) const	// NOTE: exact comparison is used here
	{	return (r-c.r)*(r-c.r)+(g-c.g)*(g-c.g)+(b-c.b)*(b-c.b)+(a-c.a)*(a-c.a)!=0;	}
//	{	return memcmp(this, &c, sizeof(mglColor));		}
	inline bool operator<(const mglColor &c) const
	{	return memcmp(this, &c, sizeof(mglColor))<0;	}
	// transparency still the same
	inline void operator*=(float v)				{	r*=v;	g*=v;	b*=v;	a*=v;	}
	inline void operator+=(const mglColor &c)	{	r+=c.r;	g+=c.g;	b+=c.b;	a+=c.a;	}
	inline void operator-=(const mglColor &c)	{	r-=c.r;	g-=c.g;	b-=c.b;	a-=c.a;	}
};
#ifndef SWIG
inline mglColor operator+(const mglColor &a, const mglColor &b)
{	return mglColor(a.r+b.r, a.g+b.g, a.b+b.b, a.a+b.a);	}
inline mglColor operator-(const mglColor &a, const mglColor &b)
{	return mglColor(a.r-b.r, a.g-b.g, a.b-b.b, a.a-b.a);	}
inline mglColor operator*(const mglColor &a, float b)
{	return mglColor(a.r*b, a.g*b, a.b*b, a.a*b);	}
inline mglColor operator*(float b, const mglColor &a)
{	return mglColor(a.r*b, a.g*b, a.b*b, a.a*b);	}
inline float operator*(const mglColor &b, const mglColor &a)
{	return a.r*b.r+a.g*b.g+a.b*b.b;	}
inline mglColor operator/(const mglColor &a, float b)
{	return mglColor(a.r/b, a.g/b, a.b/b, a.a/b);	}
inline mglColor operator!(const mglColor &a)
{	return mglColor(1-a.r, 1-a.g, 1-a.b, a.a);	}
#endif
//-----------------------------------------------------------------------------
#ifndef SWIG
/// Class for Unicode string.
/** NOTE: mglString accept multi-byte char* string for converting to wchar_t*. But it keep only single-byte char*!!! */
struct MGL_EXPORT mglString
{
	char *s;
	wchar_t *w;
	mglString()	{	s=new char[1];	w=new wchar_t[1];	*s=*w=0;	}
	mglString(const mglString &str)
	{
		size_t ls = wcslen(str.w)+1;
		s = new char[ls];		memcpy(s,str.s,ls);
		w = new wchar_t[ls];	memcpy(w,str.w,ls*sizeof(wchar_t));
	}
#if MGL_HAVE_RVAL
	mglString(mglString &&d):s(d.s),w(d.w)	{	d.s=NULL;	d.w=NULL;	}
#endif
	mglString(wchar_t ch)
	{
		s=new char[2];	w=new wchar_t[2];
		s[0] = char(ch&0xff);	w[0] = ch;
		s[1] = w[1] = 0;
	}
	mglString(const char *str)
	{
		if(str)
		{
			size_t ls=mbstowcs(0,str,0);
			w = new wchar_t[ls+1];	mbstowcs(w,str,ls); w[ls]=0;
			s = new char[ls+1];	for(size_t i=0;i<=ls;i++)	s[i]=w[i];
		}
		else	{	s=new char[1];	w=new wchar_t[1];	*s=*w=0;	}
	}
	mglString(const wchar_t *str)
	{
		if(str)
		{
			size_t len=wcslen(str);
			w = new wchar_t[len+1];	s = new char[len+1];
			for(size_t i=0;i<=len;i++)	s[i]=w[i]=str[i];
		}
		else	{	s=new char[1];	w=new wchar_t[1];	*s=*w=0;	}
	}
	mglString(const std::string &str)
	{
		size_t ls=mbstowcs(0,str.c_str(),0);
		w = new wchar_t[ls+1];	mbstowcs(w,str.c_str(),ls); w[ls]=0;
		s = new char[ls+1];	for(size_t i=0;i<=ls;i++)	s[i]=w[i];
	}
	mglString(const std::wstring &str)
	{
		size_t len=str.length();
		w = new wchar_t[len+1];	s = new char[len+1];
		for(size_t i=0;i<=len;i++)	s[i]=w[i]=str[i];
	}
	~mglString()	{	if(w)	{	delete []s;	delete []w;	}	}
	/// String length
	size_t length() const
	{	return wcslen(w);	}
	/// Crop string (like std::string::substr())
	void crop(size_t pos, size_t len=size_t(-1))
	{
		if(pos)	for(size_t i=0;i<len;i++)
			s[i] = w[i] = w[i+pos];
		s[len] = w[len] = 0;
	}
	/// Find the position of symbol
	size_t find(wchar_t ch)
	{	const wchar_t *p = wcschr(w,ch);	return p?p-w:size_t(-1);	}
	/// Find the position of string
	size_t find(const wchar_t *str)
	{	const wchar_t *p = wcsstr(w,str);	return p?p-w:size_t(-1);	}
	size_t find(const char *str)
	{	const char *p = strstr(s,str);		return p?p-s:size_t(-1);	}
	/// Access to i-th symbol
	wchar_t operator[](size_t i) const	{	return w[i];	}
	wchar_t &operator[](size_t i)		{	return w[i];	}
	/// Comparison operators
	bool operator==(const char *str) const	{	return	!strcmp(s,str);	}
	bool operator==(const wchar_t *str) const{	return	!wcscmp(w,str);	}
	bool operator==(const std::string &str) const	{	return	str==s;	}
	bool operator==(const std::wstring &str) const	{	return	str==w;	}
	/// Set operators
	const mglString &operator=(const mglString &str)
	{
		delete []s;	delete []w;
		size_t ls = wcslen(str.w)+1;
		s = new char[ls];		memcpy(s,str.s,ls);
		w = new wchar_t[ls];	memcpy(w,str.w,ls*sizeof(wchar_t));
		return str;
	}
	wchar_t operator=(wchar_t ch)
	{
		delete []s;	delete []w;
		w = new wchar_t[2];	s = new char[2];
		s[0]=w[0]=ch;	s[1]=w[1]=0;
		return ch;
	}
	const std::string &operator=(const std::string &str)
	{
		delete []s;	delete []w;
		size_t ls=mbstowcs(0,str.c_str(),0);
		w = new wchar_t[ls+1];	mbstowcs(w,str.c_str(),ls); w[ls]=0;
		s = new char[ls+1];	for(size_t i=0;i<=ls;i++)	s[i]=w[i];
		return str;
	}
	const char *operator=(const char *str)
	{
		delete []s;	delete []w;
		if(str)
		{
			size_t ls=mbstowcs(0,str,0);
			w = new wchar_t[ls+1];	mbstowcs(w,str,ls); w[ls]=0;
			s = new char[ls+1];	for(size_t i=0;i<=ls;i++)	s[i]=w[i];
		}
		else	{	s=new char[1];	w=new wchar_t[1];	*s=*w=0;	}
		return str;
	}
	const std::wstring &operator=(const std::wstring &str)
	{
		delete []s;	delete []w;
		size_t len=str.length();
		w = new wchar_t[len+1];	s = new char[len+1];
		for(size_t i=0;i<=len;i++)	s[i]=w[i]=str[i];
		return str;
	}
	const wchar_t *operator=(const wchar_t *str)
	{
		delete []s;	delete []w;
		if(str)
		{
			size_t len=wcslen(str);
			w = new wchar_t[len+1];	s = new char[len+1];
			for(size_t i=0;i<=len;i++)	s[i]=w[i]=str[i];
		}
		else	{	s=new char[1];	w=new wchar_t[1];	*s=*w=0;	}
		return str;
	}
	/// Append operator
	void operator+=(const wchar_t *str)
	{
		if(str && *str)
		{
			wchar_t *t=w;	delete []s;
			size_t l1=wcslen(w), l2=wcslen(str);
			w = new wchar_t[l1+l2+1];	s = new char[l1+l2+1];
			for(size_t i=0;i<l1;i++)	s[i]=w[i]=t[i];
			for(size_t i=0;i<=l2;i++)	s[i+l1]=w[i+l1]=str[i];
			delete []t;
		}
	}
};
#endif
//-----------------------------------------------------------------------------
#endif