func_common.inl 26.9 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1 2 3
///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
Robert Menzel's avatar
Robert Menzel committed
4
/// Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
Robert Menzel's avatar
Robert Menzel committed
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
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
/// 
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
/// 
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref core
/// @file glm/core/func_common.inl
/// @date 2008-08-03 / 2011-06-15
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////

namespace glm{
namespace detail
{
	template <typename genFIType, bool /*signed*/>
	struct Abs_
	{};

	template <typename genFIType>
	struct Abs_<genFIType, true>
	{
Robert Menzel's avatar
Robert Menzel committed
39
		GLM_FUNC_QUALIFIER static genFIType get(genFIType const & x)
Robert Menzel's avatar
Robert Menzel committed
40 41 42 43 44 45 46 47 48 49 50 51
		{
			GLM_STATIC_ASSERT(
				detail::type<genFIType>::is_float || 
				detail::type<genFIType>::is_int, "'abs' only accept floating-point and integer inputs");
			return x >= genFIType(0) ? x : -x;
			// TODO, perf comp with: *(((int *) &x) + 1) &= 0x7fffffff;
		}
	};

	template <typename genFIType>
	struct Abs_<genFIType, false>
	{
Robert Menzel's avatar
Robert Menzel committed
52
		GLM_FUNC_QUALIFIER static genFIType get(genFIType const & x)
Robert Menzel's avatar
Robert Menzel committed
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
		{
			GLM_STATIC_ASSERT(
					detail::type<genFIType>::is_uint, "'abs' only accept floating-point and integer inputs");
			return x;
		}
	};
}//namespace detail

	// abs
	template <typename genFIType>
	GLM_FUNC_QUALIFIER genFIType abs
	(
		genFIType const & x
	)
	{
		return detail::Abs_<genFIType, std::numeric_limits<genFIType>::is_signed>::get(x);
	}

	VECTORIZE_VEC(abs)

	// sign
	//Try something like based on x >> 31 to get the sign bit
	template <typename genFIType> 
	GLM_FUNC_QUALIFIER genFIType sign
	(
		genFIType const & x
	)
	{
		GLM_STATIC_ASSERT(
			detail::type<genFIType>::is_float || 
			detail::type<genFIType>::is_int, "'sign' only accept signed inputs");

		genFIType result;
		if(x > genFIType(0))
			result = genFIType(1);
		else if(x < genFIType(0))
			result = genFIType(-1);
		else
			result = genFIType(0);
		return result;
	}
	
	VECTORIZE_VEC(sign)

	// floor
	template <>
	GLM_FUNC_QUALIFIER detail::half floor<detail::half>(detail::half const & x)
	{
		return detail::half(::std::floor(float(x)));
	}

	template <typename genType>
	GLM_FUNC_QUALIFIER genType floor(genType const & x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'floor' only accept floating-point inputs");

		return ::std::floor(x);
	}

	VECTORIZE_VEC(floor)

	// trunc
	template <typename genType>
	GLM_FUNC_QUALIFIER genType trunc(genType const & x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'trunc' only accept floating-point inputs");
		return x < 0 ? -floor(-x) : floor(x);
	}

	VECTORIZE_VEC(trunc)

	// round
	template <typename genType>
	GLM_FUNC_QUALIFIER genType round(genType const& x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'round' only accept floating-point inputs");

		if(x < 0)
			return genType(int(x - genType(0.5)));
		return genType(int(x + genType(0.5)));
	}

	VECTORIZE_VEC(round)

/*
	// roundEven
	template <typename genType>
	GLM_FUNC_QUALIFIER genType roundEven(genType const& x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'roundEven' only accept floating-point inputs");

		return genType(int(x + genType(int(x) % 2)));
	}
*/
	
	// roundEven
	template <typename genType>
	GLM_FUNC_QUALIFIER genType roundEven(genType const & x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'roundEven' only accept floating-point inputs");
		
		int Integer = int(x);
		genType IntegerPart = genType(Integer);
		genType FractionalPart = fract(x);

		if(FractionalPart > genType(0.5) || FractionalPart < genType(0.5))
		{
			return round(x);
		}
		else if((Integer % 2) == 0)
		{
			return IntegerPart;
		}
		else if(x <= genType(0)) // Work around... 
		{
			return IntegerPart - 1;
		}
		else
		{
			return IntegerPart + 1;
		}
		//else // Bug on MinGW 4.5.2
		//{
		//	return mix(IntegerPart + genType(-1), IntegerPart + genType(1), x <= genType(0));
		//}
	}
	
	VECTORIZE_VEC(roundEven)

	// ceil
	template <typename genType>
	GLM_FUNC_QUALIFIER genType ceil(genType const & x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'ceil' only accept floating-point inputs");

		return ::std::ceil(x);
	}

	VECTORIZE_VEC(ceil)

	// fract
	template <typename genType>
	GLM_FUNC_QUALIFIER genType fract
	(
		genType const & x
	)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'fract' only accept floating-point inputs");

		return x - ::std::floor(x);
	}

	VECTORIZE_VEC(fract)

	// mod
	template <typename genType>
	GLM_FUNC_QUALIFIER genType mod
	(
		genType const & x, 
		genType const & y
	)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'mod' only accept floating-point inputs");

		return x - y * floor(x / y);
	}

	VECTORIZE_VEC_SCA(mod)
	VECTORIZE_VEC_VEC(mod)

	// modf
	template <typename genType>
	GLM_FUNC_QUALIFIER genType modf
	(
		genType const & x, 
		genType & i
	)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'modf' only accept floating-point inputs");

		return std::modf(x, &i);
	}

	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec2<valType> modf
	(
		detail::tvec2<valType> const & x, 
		detail::tvec2<valType> & i
	)
	{
		return detail::tvec2<valType>(
			modf(x.x, i.x),
			modf(x.y, i.y));
	}

	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec3<valType> modf
	(
		detail::tvec3<valType> const & x, 
		detail::tvec3<valType> & i
	)
	{
		return detail::tvec3<valType>(
			modf(x.x, i.x),
			modf(x.y, i.y),
			modf(x.z, i.z));
	}

	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec4<valType> modf
	(
		detail::tvec4<valType> const & x, 
		detail::tvec4<valType> & i
	)
	{
		return detail::tvec4<valType>(
			modf(x.x, i.x),
			modf(x.y, i.y),
			modf(x.z, i.z),
			modf(x.w, i.w));
	}

	//// Only valid if (INT_MIN <= x-y <= INT_MAX)
	//// min(x,y)
	//r = y + ((x - y) & ((x - y) >> (sizeof(int) *
Robert Menzel's avatar
Robert Menzel committed
278
	//CHAR_BIT - 1)));
Robert Menzel's avatar
Robert Menzel committed
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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	//// max(x,y)
	//r = x - ((x - y) & ((x - y) >> (sizeof(int) *
	//CHAR_BIT - 1)));

	// min
	template <typename genType>
	GLM_FUNC_QUALIFIER genType min
	(
		genType const & x, 
		genType const & y
	)
	{
		GLM_STATIC_ASSERT(
			detail::type<genType>::is_float || 
			detail::type<genType>::is_int ||
			detail::type<genType>::is_uint, "'min' only accept numbers");

		return x < y ? x : y;
	}

	VECTORIZE_VEC_SCA(min)
	VECTORIZE_VEC_VEC(min)

	// max
	template <typename genType>
	GLM_FUNC_QUALIFIER genType max
	(
		genType const & x, 
		genType const & y
	)
	{
		GLM_STATIC_ASSERT(
			detail::type<genType>::is_float || 
			detail::type<genType>::is_int ||
			detail::type<genType>::is_uint, "'max' only accept numbers");

		return x > y ? x : y;
	}

	VECTORIZE_VEC_SCA(max)
	VECTORIZE_VEC_VEC(max)

	// clamp
	template <typename valType>
	GLM_FUNC_QUALIFIER valType clamp
	(
		valType const & x, 
		valType const & minVal, 
		valType const & maxVal
	)
	{
		GLM_STATIC_ASSERT(
			detail::type<valType>::is_float || 
			detail::type<valType>::is_int ||
			detail::type<valType>::is_uint, "'clamp' only accept numbers");
		
		return min(maxVal, max(minVal, x));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> clamp
	(
		detail::tvec2<T> const & x, 
		typename detail::tvec2<T>::value_type const & minVal, 
		typename detail::tvec2<T>::value_type const & maxVal
	)
	{
		return detail::tvec2<T>(
			clamp(x.x, minVal, maxVal),
			clamp(x.y, minVal, maxVal));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> clamp
	(
		detail::tvec3<T> const & x, 
		typename detail::tvec3<T>::value_type const & minVal, 
		typename detail::tvec3<T>::value_type const & maxVal
	)
	{
		return detail::tvec3<T>(
			clamp(x.x, minVal, maxVal),
			clamp(x.y, minVal, maxVal),
			clamp(x.z, minVal, maxVal));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> clamp
	(
		detail::tvec4<T> const & x, 
		typename detail::tvec4<T>::value_type const & minVal, 
		typename detail::tvec4<T>::value_type const & maxVal
	)
	{
		return detail::tvec4<T>(
			clamp(x.x, minVal, maxVal),
			clamp(x.y, minVal, maxVal),
			clamp(x.z, minVal, maxVal),
			clamp(x.w, minVal, maxVal));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> clamp
	(
		detail::tvec2<T> const & x, 
		detail::tvec2<T> const & minVal, 
		detail::tvec2<T> const & maxVal
	)
	{
		return detail::tvec2<T>(
			clamp(x.x, minVal.x, maxVal.x),
			clamp(x.y, minVal.y, maxVal.y));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> clamp
	(
		detail::tvec3<T> const & x, 
		detail::tvec3<T> const & minVal, 
		detail::tvec3<T> const & maxVal
	)
	{
		return detail::tvec3<T>(
			clamp(x.x, minVal.x, maxVal.x),
			clamp(x.y, minVal.y, maxVal.y),
			clamp(x.z, minVal.z, maxVal.z));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> clamp
	(
		detail::tvec4<T> const & x, 
		detail::tvec4<T> const & minVal, 
		detail::tvec4<T> const & maxVal
	)
	{
		return detail::tvec4<T>(
			clamp(x.x, minVal.x, maxVal.x),
			clamp(x.y, minVal.y, maxVal.y),
			clamp(x.z, minVal.z, maxVal.z),
			clamp(x.w, minVal.w, maxVal.w));
	}

	// mix
Robert Menzel's avatar
Robert Menzel committed
423 424
	template <typename genType>
	GLM_FUNC_QUALIFIER genType mix
Robert Menzel's avatar
Robert Menzel committed
425
	(
Robert Menzel's avatar
Robert Menzel committed
426 427 428
		genType const & x, 
		genType const & y, 
		genType const & a
Robert Menzel's avatar
Robert Menzel committed
429 430
	)
	{
Robert Menzel's avatar
Robert Menzel committed
431
		GLM_STATIC_ASSERT(detail::type<genType>::is_float , "'genType' is not floating-point type");
Robert Menzel's avatar
Robert Menzel committed
432

Robert Menzel's avatar
Robert Menzel committed
433
		return x + a * (y - x);
Robert Menzel's avatar
Robert Menzel committed
434 435
	}

Robert Menzel's avatar
Robert Menzel committed
436 437
	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec2<valType> mix
Robert Menzel's avatar
Robert Menzel committed
438
	(
Robert Menzel's avatar
Robert Menzel committed
439 440 441
		detail::tvec2<valType> const & x, 
		detail::tvec2<valType> const & y, 
		valType const & a
Robert Menzel's avatar
Robert Menzel committed
442 443
	)
	{
Robert Menzel's avatar
Robert Menzel committed
444 445 446
		GLM_STATIC_ASSERT(detail::type<valType>::is_float , "'genType' is not floating-point type");

		return x + a * (y - x);
Robert Menzel's avatar
Robert Menzel committed
447 448
	}

Robert Menzel's avatar
Robert Menzel committed
449 450
	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec3<valType> mix
Robert Menzel's avatar
Robert Menzel committed
451
	(
Robert Menzel's avatar
Robert Menzel committed
452 453 454
		detail::tvec3<valType> const & x, 
		detail::tvec3<valType> const & y, 
		valType const & a
Robert Menzel's avatar
Robert Menzel committed
455 456
	)
	{
Robert Menzel's avatar
Robert Menzel committed
457
		return x + a * (y - x);
Robert Menzel's avatar
Robert Menzel committed
458 459
	}

Robert Menzel's avatar
Robert Menzel committed
460 461
	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec4<valType> mix
Robert Menzel's avatar
Robert Menzel committed
462
	(
Robert Menzel's avatar
Robert Menzel committed
463 464 465
		detail::tvec4<valType> const & x, 
		detail::tvec4<valType> const & y, 
		valType const & a
Robert Menzel's avatar
Robert Menzel committed
466 467
	)
	{
Robert Menzel's avatar
Robert Menzel committed
468
		return x + a * (y - x);
Robert Menzel's avatar
Robert Menzel committed
469 470
	}

Robert Menzel's avatar
Robert Menzel committed
471 472
	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec2<valType> mix
Robert Menzel's avatar
Robert Menzel committed
473
	(
Robert Menzel's avatar
Robert Menzel committed
474 475 476
		detail::tvec2<valType> const & x, 
		detail::tvec2<valType> const & y, 
		detail::tvec2<valType> const & a
Robert Menzel's avatar
Robert Menzel committed
477 478
	)
	{
Robert Menzel's avatar
Robert Menzel committed
479
		return x + a * (y - x);
Robert Menzel's avatar
Robert Menzel committed
480 481
	}

Robert Menzel's avatar
Robert Menzel committed
482 483
	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec3<valType> mix
Robert Menzel's avatar
Robert Menzel committed
484
	(
Robert Menzel's avatar
Robert Menzel committed
485 486 487
		detail::tvec3<valType> const & x, 
		detail::tvec3<valType> const & y, 
		detail::tvec3<valType> const & a
Robert Menzel's avatar
Robert Menzel committed
488 489
	)
	{
Robert Menzel's avatar
Robert Menzel committed
490 491 492
		GLM_STATIC_ASSERT(detail::type<valType>::is_float , "'genType' is not floating-point type");

		return x + a * (y - x);
Robert Menzel's avatar
Robert Menzel committed
493 494
	}

Robert Menzel's avatar
Robert Menzel committed
495 496
	template <typename valType>
	GLM_FUNC_QUALIFIER detail::tvec4<valType> mix
Robert Menzel's avatar
Robert Menzel committed
497
	(
Robert Menzel's avatar
Robert Menzel committed
498 499 500
		detail::tvec4<valType> const & x, 
		detail::tvec4<valType> const & y, 
		detail::tvec4<valType> const & a
Robert Menzel's avatar
Robert Menzel committed
501 502
	)
	{
Robert Menzel's avatar
Robert Menzel committed
503
		return x + a * (y - x);
Robert Menzel's avatar
Robert Menzel committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
	}

	//template <typename genTypeT>
	//GLM_FUNC_QUALIFIER genTypeT mix
	//(
	//	genTypeT const & x, 
	//	genTypeT const & y, 
	//	float const & a
	//)
	//{
	//	// It could be a vector too
	//	//GLM_STATIC_ASSERT(
	//	//	detail::type<genTypeT>::is_float && 
	//	//	detail::type<genTypeU>::is_float);

	//	return x + a * (y - x);
	//}

Robert Menzel's avatar
Robert Menzel committed
522 523
	template <>
	GLM_FUNC_QUALIFIER float mix
Robert Menzel's avatar
Robert Menzel committed
524
	(
Robert Menzel's avatar
Robert Menzel committed
525 526
		float const & x, 
		float const & y, 
Robert Menzel's avatar
Robert Menzel committed
527 528 529
		bool const & a
	)
	{
Robert Menzel's avatar
Robert Menzel committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
		return a ? y : x;
	}

	template <>
	GLM_FUNC_QUALIFIER double mix
	(
		double const & x, 
		double const & y, 
		bool const & a
	)
	{
		return a ? y : x;
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> mix
	(
		detail::tvec2<T> const & x, 
		detail::tvec2<T> const & y, 
		bool a
	)
	{
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");

		return a ? y : x;
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> mix
	(
		detail::tvec3<T> const & x, 
		detail::tvec3<T> const & y, 
		bool a
	)
	{
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");

		return a ? y : x;
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> mix
	(
		detail::tvec4<T> const & x, 
		detail::tvec4<T> const & y, 
		bool a
	)
	{
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");
Robert Menzel's avatar
Robert Menzel committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

		return a ? y : x;
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> mix
	(
		detail::tvec2<T> const & x, 
		detail::tvec2<T> const & y, 
		typename detail::tvec2<T>::bool_type a
	)
	{
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");

		detail::tvec2<T> result;
		for
		(
			typename detail::tvec2<T>::size_type i = 0; 
Robert Menzel's avatar
Robert Menzel committed
597
			i < x.length(); ++i
Robert Menzel's avatar
Robert Menzel committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		)
		{
			result[i] = a[i] ? y[i] : x[i];
		}
		return result;
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> mix
	(
		detail::tvec3<T> const & x, 
		detail::tvec3<T> const & y, 
		typename detail::tvec3<T>::bool_type a
	)
	{
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");

		detail::tvec3<T> result;
		for
		(
			typename detail::tvec3<T>::size_type i = 0; 
Robert Menzel's avatar
Robert Menzel committed
619
			i < x.length(); ++i
Robert Menzel's avatar
Robert Menzel committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
		)
		{
			result[i] = a[i] ? y[i] : x[i];
		}
		return result;
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> mix
	(
		detail::tvec4<T> const & x, 
		detail::tvec4<T> const & y, 
		typename detail::tvec4<T>::bool_type a
	)
	{
		GLM_STATIC_ASSERT(detail::type<T>::is_float, "'mix' only accept floating-point inputs");

		detail::tvec4<T> result;
		for
		(
			typename detail::tvec4<T>::size_type i = 0; 
Robert Menzel's avatar
Robert Menzel committed
641
			i < x.length(); ++i
Robert Menzel's avatar
Robert Menzel committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
		)
		{
			result[i] = a[i] ? y[i] : x[i];
		}
		return result;
	}

	// step
	template <typename genType>
	GLM_FUNC_QUALIFIER genType step
	(
		genType const & edge, 
		genType const & x
	)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'step' only accept floating-point inputs");

		return x < edge ? genType(0) : genType(1);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> step
	(
		typename detail::tvec2<T>::value_type const & edge, 
		detail::tvec2<T> const & x
	)
	{
		return detail::tvec2<T>(
			x.x < edge ? T(0) : T(1),
			x.y < edge ? T(0) : T(1));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> step
	(
		typename detail::tvec3<T>::value_type const & edge, 
		detail::tvec3<T> const & x
	)
	{
		return detail::tvec3<T>(
			x.x < edge ? T(0) : T(1),
			x.y < edge ? T(0) : T(1),
			x.z < edge ? T(0) : T(1));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> step
	(
		typename detail::tvec4<T>::value_type const & edge, 
		detail::tvec4<T> const & x
	)
	{
		return detail::tvec4<T>(
			x.x < edge ? T(0) : T(1),
			x.y < edge ? T(0) : T(1),
			x.z < edge ? T(0) : T(1),
			x.w < edge ? T(0) : T(1));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> step
	(
		detail::tvec2<T> const & edge, 
		detail::tvec2<T> const & x
	)
	{
		return detail::tvec2<T>(
			x.x < edge.x ? T(0) : T(1),
			x.y < edge.y ? T(0) : T(1));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> step
	(
		detail::tvec3<T> const & edge, 
		detail::tvec3<T> const & x
	)
	{
		return detail::tvec3<T>(
			x.x < edge.x ? T(0) : T(1),
			x.y < edge.y ? T(0) : T(1),
			x.z < edge.z ? T(0) : T(1));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> step
	(
		detail::tvec4<T> const & edge, 
		detail::tvec4<T> const & x
	)
	{
		return detail::tvec4<T>(
			x.x < edge.x ? T(0) : T(1),
			x.y < edge.y ? T(0) : T(1),
			x.z < edge.z ? T(0) : T(1),
			x.w < edge.w ? T(0) : T(1));
	}

	// smoothstep
	template <typename genType>
	GLM_FUNC_QUALIFIER genType smoothstep
	(
		genType const & edge0, 
		genType const & edge1, 
		genType const & x
	)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'smoothstep' only accept floating-point inputs");

		genType tmp = clamp((x - edge0) / (edge1 - edge0), genType(0), genType(1));
		return tmp * tmp * (genType(3) - genType(2) * tmp);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> smoothstep
	(
		typename detail::tvec2<T>::value_type const & edge0, 
		typename detail::tvec2<T>::value_type const & edge1, 
		detail::tvec2<T> const & x
	)
	{
		return detail::tvec2<T>(
			smoothstep(edge0, edge1, x.x),
			smoothstep(edge0, edge1, x.y));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> smoothstep
	(
		typename detail::tvec3<T>::value_type const & edge0, 
		typename detail::tvec3<T>::value_type const & edge1, 
		detail::tvec3<T> const & x
	)
	{
		return detail::tvec3<T>(
			smoothstep(edge0, edge1, x.x),
			smoothstep(edge0, edge1, x.y),
			smoothstep(edge0, edge1, x.z));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> smoothstep
	(
		typename detail::tvec4<T>::value_type const & edge0, 
		typename detail::tvec4<T>::value_type const & edge1, 
		detail::tvec4<T> const & x
	)
	{
		return detail::tvec4<T>(
			smoothstep(edge0, edge1, x.x),
			smoothstep(edge0, edge1, x.y),
			smoothstep(edge0, edge1, x.z),
			smoothstep(edge0, edge1, x.w));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> smoothstep
	(
		detail::tvec2<T> const & edge0, 
		detail::tvec2<T> const & edge1, 
		detail::tvec2<T> const & x
	)
	{
		return detail::tvec2<T>(
			smoothstep(edge0.x, edge1.x, x.x),
			smoothstep(edge0.y, edge1.y, x.y));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> smoothstep
	(
		detail::tvec3<T> const & edge0, 
		detail::tvec3<T> const & edge1, 
		detail::tvec3<T> const & x
	)
	{
		return detail::tvec3<T>(
			smoothstep(edge0.x, edge1.x, x.x),
			smoothstep(edge0.y, edge1.y, x.y),
			smoothstep(edge0.z, edge1.z, x.z));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> smoothstep
	(
		detail::tvec4<T> const & edge0, 
		detail::tvec4<T> const & edge1, 
		detail::tvec4<T> const & x
	)
	{
		return detail::tvec4<T>(
			smoothstep(edge0.x, edge1.x, x.x),
			smoothstep(edge0.y, edge1.y, x.y),
			smoothstep(edge0.z, edge1.z, x.z),
			smoothstep(edge0.w, edge1.w, x.w));
	}

	// TODO: Not working on MinGW...
	template <typename genType> 
	GLM_FUNC_QUALIFIER bool isnan(genType const & x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'isnan' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
845
#		if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_INTEL))
Robert Menzel's avatar
Robert Menzel committed
846
			return _isnan(x) != 0;
Robert Menzel's avatar
Robert Menzel committed
847
#		elif(GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG))
Robert Menzel's avatar
Robert Menzel committed
848 849 850 851 852
#			if(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
				return _isnan(x) != 0;
#			else
				return std::isnan(x);
#			endif
Robert Menzel's avatar
Robert Menzel committed
853 854 855
#		elif(GLM_COMPILER & GLM_COMPILER_CUDA)
			return isnan(x) != 0;
#		else
Robert Menzel's avatar
Robert Menzel committed
856
			return std::isnan(x);
Robert Menzel's avatar
Robert Menzel committed
857
#		endif
Robert Menzel's avatar
Robert Menzel committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	}

	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec2<T>::bool_type isnan
	(
		detail::tvec2<T> const & x
	)
	{
		return typename detail::tvec2<T>::bool_type(
			isnan(x.x),
			isnan(x.y));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec3<T>::bool_type isnan
	(
		detail::tvec3<T> const & x
	)
	{
		return typename detail::tvec3<T>::bool_type(
			isnan(x.x),
			isnan(x.y),
			isnan(x.z));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec4<T>::bool_type isnan
	(
		detail::tvec4<T> const & x
	)
	{
		return typename detail::tvec4<T>::bool_type(
			isnan(x.x),
			isnan(x.y),
			isnan(x.z),
			isnan(x.w));
	}

	template <typename genType> 
	GLM_FUNC_QUALIFIER bool isinf(
		genType const & x)
	{
		GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'isinf' only accept floating-point inputs");

Robert Menzel's avatar
Robert Menzel committed
902
#		if(GLM_COMPILER & (GLM_COMPILER_INTEL | GLM_COMPILER_VC))
Robert Menzel's avatar
Robert Menzel committed
903
			return _fpclass(x) == _FPCLASS_NINF || _fpclass(x) == _FPCLASS_PINF;
Robert Menzel's avatar
Robert Menzel committed
904
#		elif(GLM_COMPILER & (GLM_COMPILER_GCC | GLM_COMPILER_CLANG))
Robert Menzel's avatar
Robert Menzel committed
905 906 907 908 909
#			if(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
				return _isinf(x) != 0;
#			else
				return std::isinf(x);
#			endif
Robert Menzel's avatar
Robert Menzel committed
910 911 912 913
#		elif(GLM_COMPILER & GLM_COMPILER_CUDA)
			// http://developer.download.nvidia.com/compute/cuda/4_2/rel/toolkit/docs/online/group__CUDA__MATH__DOUBLE_g13431dd2b40b51f9139cbb7f50c18fab.html#g13431dd2b40b51f9139cbb7f50c18fab
			return isinf(double(x)) != 0;
#		else
Robert Menzel's avatar
Robert Menzel committed
914
			return std::isinf(x);
Robert Menzel's avatar
Robert Menzel committed
915
#		endif
Robert Menzel's avatar
Robert Menzel committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	}

	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec2<T>::bool_type isinf
	(
		detail::tvec2<T> const & x
	)
	{
		return typename detail::tvec2<T>::bool_type(
			isinf(x.x),
			isinf(x.y));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec3<T>::bool_type isinf
	(
		detail::tvec3<T> const & x
	)
	{
		return typename detail::tvec3<T>::bool_type(
			isinf(x.x),
			isinf(x.y),
			isinf(x.z));
	}

	template <typename T>
	GLM_FUNC_QUALIFIER typename detail::tvec4<T>::bool_type isinf
	(
		detail::tvec4<T> const & x
	)
	{
		return typename detail::tvec4<T>::bool_type(
			isinf(x.x),
			isinf(x.y),
			isinf(x.z),
			isinf(x.w));
	}

	GLM_FUNC_QUALIFIER int floatBitsToInt(float const & value)
	{
		union
		{
			float f;
			int i;
		} fi;

		fi.f = value;
		return fi.i;
	}

	GLM_FUNC_QUALIFIER detail::tvec2<int> floatBitsToInt
	(
		detail::tvec2<float> const & value
	)
	{
		return detail::tvec2<int>(
			floatBitsToInt(value.x),
			floatBitsToInt(value.y));
	}

	GLM_FUNC_QUALIFIER detail::tvec3<int> floatBitsToInt
	(
		detail::tvec3<float> const & value
	)
	{
		return detail::tvec3<int>(
			floatBitsToInt(value.x),
			floatBitsToInt(value.y),
			floatBitsToInt(value.z));
	}

	GLM_FUNC_QUALIFIER detail::tvec4<int> floatBitsToInt
	(
		detail::tvec4<float> const & value
	)
	{
		return detail::tvec4<int>(
			floatBitsToInt(value.x),
			floatBitsToInt(value.y),
			floatBitsToInt(value.z),
			floatBitsToInt(value.w));
	}

	GLM_FUNC_QUALIFIER uint floatBitsToUint(float const & value)
	{
		union
		{
			float f;
			uint u;
		} fu;

		fu.f = value;
		return fu.u;
	}

	GLM_FUNC_QUALIFIER detail::tvec2<uint> floatBitsToUint
	(
		detail::tvec2<float> const & value
	)
	{
		return detail::tvec2<uint>(
			floatBitsToUint(value.x),
			floatBitsToUint(value.y));
	}

	GLM_FUNC_QUALIFIER detail::tvec3<uint> floatBitsToUint
	(
		detail::tvec3<float> const & value
	)
	{
		return detail::tvec3<uint>(
			floatBitsToUint(value.x),
			floatBitsToUint(value.y),
			floatBitsToUint(value.z));
	}

	GLM_FUNC_QUALIFIER detail::tvec4<uint> floatBitsToUint
	(
		detail::tvec4<float> const & value
	)
	{
		return detail::tvec4<uint>(
			floatBitsToUint(value.x),
			floatBitsToUint(value.y),
			floatBitsToUint(value.z),
			floatBitsToUint(value.w));
	}

	GLM_FUNC_QUALIFIER float intBitsToFloat(int const & value)
	{
		union
		{
			float f;
			int i;
		} fi;

		fi.i = value;
		return fi.f;
	}

	GLM_FUNC_QUALIFIER detail::tvec2<float> intBitsToFloat

	(
		detail::tvec2<int> const & value
	)
	{
		return detail::tvec2<float>(
			intBitsToFloat(value.x),
			intBitsToFloat(value.y));
	}

	GLM_FUNC_QUALIFIER detail::tvec3<float> intBitsToFloat
	(
		detail::tvec3<int> const & value
	)
	{
		return detail::tvec3<float>(
			intBitsToFloat(value.x),
			intBitsToFloat(value.y),
			intBitsToFloat(value.z));
	}

	GLM_FUNC_QUALIFIER detail::tvec4<float> intBitsToFloat
	(
		detail::tvec4<int> const & value
	)
	{
		return detail::tvec4<float>(
			intBitsToFloat(value.x),
			intBitsToFloat(value.y),
			intBitsToFloat(value.z),
			intBitsToFloat(value.w));
	}

	GLM_FUNC_QUALIFIER float uintBitsToFloat(uint const & value)
	{
		union
		{
			float f;
			uint u;
		} fu;

		fu.u = value;
		return fu.f;
	}

	GLM_FUNC_QUALIFIER detail::tvec2<float> uintBitsToFloat
	(
		detail::tvec2<uint> const & value
	)
	{
		return detail::tvec2<float>(
			uintBitsToFloat(value.x),
			uintBitsToFloat(value.y));
	}

	GLM_FUNC_QUALIFIER detail::tvec3<float> uintBitsToFloat
	(
		detail::tvec3<uint> const & value
	)
	{
		return detail::tvec3<float>(
			uintBitsToFloat(value.x),
			uintBitsToFloat(value.y),
			uintBitsToFloat(value.z));
	}

	GLM_FUNC_QUALIFIER detail::tvec4<float> uintBitsToFloat
	(
		detail::tvec4<uint> const & value
	)
	{
		return detail::tvec4<float>(
			uintBitsToFloat(value.x),
			uintBitsToFloat(value.y),
			uintBitsToFloat(value.z),
			uintBitsToFloat(value.w));
	}

	template <typename genType>
	GLM_FUNC_QUALIFIER genType fma
	(
		genType const & a, 
		genType const & b, 
		genType const & c
	)
	{
		return a * b + c;
	}

	template <typename genType>
	GLM_FUNC_QUALIFIER genType frexp
	(
		genType const & x, 
		int & exp
	)
	{
		return std::frexp(x, exp);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> frexp
	(
		detail::tvec2<T> const & x, 
		detail::tvec2<int> & exp
	)
	{
		return std::frexp(x, exp);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> frexp
	(
		detail::tvec3<T> const & x, 
		detail::tvec3<int> & exp
	)
	{
		return std::frexp(x, exp);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> frexp
	(
		detail::tvec4<T> const & x, 
		detail::tvec4<int> & exp
	)
	{
		return std::frexp(x, exp);
	}

	template <typename genType>
	GLM_FUNC_QUALIFIER genType ldexp
	(
		genType const & x, 
		int const & exp
	)
	{
		return std::frexp(x, exp);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec2<T> ldexp
	(
		detail::tvec2<T> const & x, 
		detail::tvec2<int> const & exp
	)
	{
		return std::frexp(x, exp);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec3<T> ldexp
	(
		detail::tvec3<T> const & x, 
		detail::tvec3<int> const & exp
	)
	{
		return std::frexp(x, exp);
	}

	template <typename T>
	GLM_FUNC_QUALIFIER detail::tvec4<T> ldexp
	(
		detail::tvec4<T> const & x, 
		detail::tvec4<int> const & exp
	)
	{
		return std::frexp(x, exp);
	}

}//namespace glm