1 #ifndef __GNUC__
2 #pragma once
3 #endif
4 #ifndef __XR_MESH_UTILS_H__
5 #define __XR_MESH_UTILS_H__
6 
7 #include "xr_vector2.h"
8 #include "xr_vector3.h"
9 
10 namespace xray_re {
11 
calc_perimeter(const _vector3<T> & p0,const _vector3<T> & p1,const _vector3<T> & p2)12 template<typename T> static inline T calc_perimeter(const _vector3<T>& p0, const _vector3<T>& p1, const _vector3<T>& p2)
13 {
14 	return p0.distance(p1) + p1.distance(p2) + p2.distance(p0);
15 }
16 
calc_perimeter(const _vector2<T> & p0,const _vector2<T> & p1,const _vector2<T> & p2)17 template<typename T> static inline T calc_perimeter(const _vector2<T>& p0, const _vector2<T>& p1, const _vector2<T>& p2)
18 {
19 	return p0.distance(p1) + p1.distance(p2) + p2.distance(p0);
20 }
21 
calc_signed_area(const _vector3<T> & p0,const _vector3<T> & p1,const _vector3<T> & p2)22 template<typename T> static inline T calc_signed_area(const _vector3<T>& p0, const _vector3<T>& p1, const _vector3<T>& p2)
23 {
24 	// i = 0, j = 1
25 	T x = p0.y*p1.z - p0.z*p1.y;
26 	T y = p0.z*p1.x - p0.x*p1.z;
27 	T z = p0.x*p1.y - p0.y*p1.x;
28 
29 	// i = 1, j = 2
30 	x += p1.y*p2.z - p1.z*p2.y;
31 	y += p1.z*p2.x - p1.x*p2.z;
32 	z += p1.x*p2.y - p1.y*p2.x;
33 
34 	// i = 2, j = 0
35 	x += p2.y*p0.z - p2.z*p0.y;
36 	y += p2.z*p0.x - p2.x*p0.z;
37 	z += p2.x*p0.y - p2.y*p0.x;
38 
39 	return T(0.5)*_vector3<T>().calc_normal(p0, p1, p2).dot_product(_vector3<T>().set(x, y, z));
40 }
41 
calc_area(const _vector3<T> & p0,const _vector3<T> & p1,const _vector3<T> & p2)42 template<typename T> static inline T calc_area(const _vector3<T>& p0, const _vector3<T>& p1, const _vector3<T>& p2)
43 {
44 	return std::abs(calc_signed_area(p0, p1, p2));
45 }
46 
calc_area_xz(const _vector3<T> & p0,const _vector3<T> & p1,const _vector3<T> & p2)47 template<typename T> static inline T calc_area_xz(const _vector3<T>& p0, const _vector3<T>& p1, const _vector3<T>& p2)
48 {
49 	return T(0.5)*(p0.z*p1.x - p0.x*p1.z + p1.z*p2.x - p1.x*p2.z + p2.z*p0.x - p2.x*p0.z);
50 }
51 
52 } // end of namespace xray_re
53 
54 #endif
55