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.poly; 26 27 import std.traits; 28 import des.math.linear.vector; 29 import des.math.linear.matrix; 30 import des.math.linear.segment; 31 import des.math.basic; 32 33 struct Poly(T) if( isFloatingPoint!T ) 34 { 35 alias Vector!(3,T,"x y z") vectype; 36 vectype[3] pnt; 37 38 pure this( in vectype P0, in vectype P1, in vectype P2 ) 39 { 40 pnt[0] = P0; 41 pnt[1] = P1; 42 pnt[2] = P2; 43 } 44 45 @property 46 { 47 vectype perp() const { return cross( pnt[1]-pnt[0], pnt[2]-pnt[0] ); } 48 vectype norm() const { return perp.e; } 49 T area() const { return perp.len / 2.0; } 50 vectype center() const { return (pnt[0] + pnt[1] + pnt[2]) / 3.0f; } 51 } 52 53 auto tr(X)( in Matrix!(4,4,X) mtr ) const 54 { 55 return Poly!T( (mtr * vec!(4,T,"x y z w")( pnt[0], 1 )).xyz, 56 (mtr * vec!(4,T,"x y z w")( pnt[1], 1 )).xyz, 57 (mtr * vec!(4,T,"x y z w")( pnt[2], 1 )).xyz ); 58 } 59 60 Segment!(T)[3] toSegments() const 61 { 62 alias Segment!T st; 63 return [ st.fromPoints( pnt[0], pnt[1] ), 64 st.fromPoints( pnt[1], pnt[2] ), 65 st.fromPoints( pnt[2], pnt[0] ) ]; 66 } 67 68 /+ высота проведённая из точки это отрезок, 69 соединяющий проекцию точки на плоскость и 70 саму точку (Segment) +/ 71 auto altitude( in vectype pp ) const 72 { 73 auto n = norm; 74 auto dst = n * dot( n, pp-pnt[0] ); 75 return Segment!T( pp - dst, dst ); 76 } 77 78 auto project(F)( in Segment!F seg ) const 79 { 80 auto n = norm; 81 auto dst1 = dot( n, seg.pnt-pnt[0] ); 82 auto dst2 = dot( n, seg.end-pnt[0] ); 83 auto diff = dst1 - dst2; 84 return Segment!T( seg.png - n * dst1, 85 seg.dir + n * diff ); 86 } 87 88 auto intersect(F)( in Segment!F seg ) const 89 { return seg.intersect( project(seg) ); } 90 } 91 92 alias Poly!float fPoly; 93 alias Poly!double dPoly; 94 alias Poly!real rPoly; 95 96 unittest 97 { 98 auto poly = fPoly( vec3(0,0,0), vec3(1,0,0), vec3(0,1,0) ); 99 assert( poly.area == 0.5f ); 100 assert( poly.norm == vec3(0,0,1) ); 101 102 auto pnt = vec3( 2,2,2 ); 103 auto a = poly.altitude( pnt ); 104 assert( a.pnt == vec3(2,2,0) ); 105 assert( a.dir == vec3(0,0,2) ); 106 }