1 /+ 2 The MIT License (MIT) 3 4 Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)> 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 +/ 24 25 module des.math.linear.triangle; 26 27 import std.traits; 28 import des.math.linear.vector; 29 import des.math.linear.matrix; 30 import des.math.linear.ray; 31 import des.math.basic; 32 33 /// 34 struct Triangle(T) if( isFloatingPoint!T ) 35 { 36 /// 37 alias Vector3!T vectype; 38 39 /// 40 vectype[3] pnt; 41 42 pure: 43 44 /// 45 this( in vectype P0, in vectype P1, in vectype P2 ) 46 { 47 pnt[0] = P0; 48 pnt[1] = P1; 49 pnt[2] = P2; 50 } 51 52 @property 53 { 54 /// 55 vectype perp() const { return cross( pnt[1]-pnt[0], pnt[2]-pnt[0] ); } 56 /// 57 vectype norm() const { return perp.e; } 58 /// 59 T area() const { return perp.len / 2.0; } 60 /// 61 vectype center() const { return (pnt[0] + pnt[1] + pnt[2]) / 3.0f; } 62 } 63 64 /// affine transform 65 auto tr(X)( in Matrix!(4,4,X) mtr ) const 66 { 67 return Triangle!T( (mtr * vec!(4,T,"x y z w")( pnt[0], 1 )).xyz, 68 (mtr * vec!(4,T,"x y z w")( pnt[1], 1 )).xyz, 69 (mtr * vec!(4,T,"x y z w")( pnt[2], 1 )).xyz ); 70 } 71 72 /// 73 Ray!(T)[3] toRays() const 74 { 75 alias Ray!T st; 76 return [ st.fromPoints( pnt[0], pnt[1] ), 77 st.fromPoints( pnt[1], pnt[2] ), 78 st.fromPoints( pnt[2], pnt[0] ) ]; 79 } 80 81 /+ высота проведённая из точки это отрезок, 82 соединяющий проекцию точки на плоскость и 83 саму точку (Ray) +/ 84 /// 85 auto altitude( in vectype pp ) const 86 { 87 auto n = norm; 88 auto dst = n * dot( n, pp-pnt[0] ); 89 return Ray!T( pp - dst, dst ); 90 } 91 92 /// 93 auto project(F)( in Ray!F seg ) const 94 { 95 auto n = norm; 96 auto dst1 = dot( n, seg.pos-pnt[0] ); 97 auto dst2 = dot( n, seg.end-pnt[0] ); 98 auto diff = dst1 - dst2; 99 return Ray!T( seg.png - n * dst1, 100 seg.dir + n * diff ); 101 } 102 103 /// 104 auto intersect(F)( in Ray!F seg ) const 105 { return seg.intersect( project(seg) ); } 106 } 107 108 /// 109 alias Triangle!float fTriangle; 110 /// 111 alias Triangle!double dTriangle; 112 /// 113 alias Triangle!real rTriangle; 114 115 /// 116 unittest 117 { 118 auto poly = fTriangle( vec3(0,0,0), vec3(1,0,0), vec3(0,1,0) ); 119 assert( poly.area == 0.5f ); 120 assert( poly.norm == vec3(0,0,1) ); 121 122 auto pnt = vec3( 2,2,2 ); 123 auto a = poly.altitude( pnt ); 124 assert( a.pos == vec3(2,2,0) ); 125 assert( a.dir == vec3(0,0,2) ); 126 }