1 static assert( Vector!(3,float).isStatic == true ); 2 static assert( Vector!(3,float).isDynamic == false ); 3 4 static assert( Vector!(0,float).isStatic == false ); 5 static assert( Vector!(0,float).isDynamic == true ); 6 7 static assert( isVector!(Vector!(3,float)) ); 8 static assert( isVector!(Vector!(0,float)) ); 9 10 static assert( Vector!(3,float).sizeof == float.sizeof * 3 ); 11 static assert( Vector!(0,float).sizeof == (float[]).sizeof ); 12 13 static assert( Vector!(3,float).length == 3 );
1 assert( eq( Vector!(3,float)(1,2,3), [1,2,3] ) ); 2 3 auto a = Vector!(3,float)(1,2,3); 4 assert( eq( Vector!(5,int)(0,a,4), [0,1,2,3,4] ) ); 5 6 static assert( !__traits(compiles, { auto v = Vector!(2,int)(1,2,3); } ) ); 7 8 { auto v = Vector!(0,int)(1,2,3); } // no exception 9 { auto v = Vector!(3,int)(1); } // no exception 10 11 auto b = Vector!(0,float)(1,2,3); 12 assert( eq( b.length, 3 ) ); 13 14 auto c = Vector!(3,float)(1); 15 assert( eq( c, [1,1,1] ) ); 16 auto d = c; 17 assert( eq( c, d ) );
1 static struct Test1 { float x,y,z; } 2 static assert( !__traits(compiles,Vector!(3,float)(Test1.init)) ); 3 4 static struct Test2 { float[3] data; alias data this; } 5 static assert( __traits(compiles,Vector!(3,float)(Test2.init)) );
convert vectors
1 auto a = ivec2(1,2); 2 auto b = vec2(a); 3 assert( eq( a, b ) ); 4 auto c = ivec2(b); 5 assert( eq( a, c ) );
1 auto a = Vector!(3,int)(1,2,3); 2 assert( eq( a.x, a.r ) ); 3 assert( eq( a.y, a.g ) ); 4 assert( eq( a.z, a.b ) ); 5 assert( eq( a.x, a.u ) ); 6 assert( eq( a.y, a.v ) ); 7 assert( eq( a.z, a.t ) );
1 auto a = vec3(1,2,3); 2 3 assert( eq( a.opDispatch!"x", 1 ) ); 4 assert( eq( a.y, 2 ) ); 5 assert( eq( a.z, 3 ) ); 6 7 a.opDispatch!"x" = 2; 8 a.x = 2; 9 assert( eq( a.x, 2 ) );
1 auto a = vec3(1,2,3); 2 3 auto b = a.opDispatch!"xy"; 4 auto c = a.xx; 5 auto d = a.xxxyyzyx; 6 7 static assert( is(typeof(b) == Vector!(2,float) ) ); 8 static assert( is(typeof(c) == Vector!(2,float) ) ); 9 static assert( is(typeof(d) == Vector!(8,float) ) ); 10 11 assert( eq( b, [1,2] ) ); 12 assert( eq( c, [1,1] ) ); 13 assert( eq( d, [1,1,1,2,2,3,2,1] ) );
1 auto a = vec3(1,2,3); 2 auto b = dvec4(4,5,6,7); 3 auto c = vecD( 9, 10 ); 4 a.opDispatch!"xz"( b.yw ); 5 assert( eq( a, [5,2,7] ) ); 6 a.zy = c; 7 assert( eq( a, [5,10,9] ) ); 8 static assert( !__traits(compiles, a.xy=vec3(1,2,3)) ); 9 static assert( !__traits(compiles, a.xx=vec2(1,2)) ); 10 auto d = a.zxy = b.wyx; 11 static assert( is( d.datatype == double ) ); 12 assert( eq( d, [ 7,5,4 ] ) ); 13 assert( eq( a, [ 5,4,7 ] ) ); 14 a.yzx = a.zxz; 15 assert( eq( a, [ 7,7,5 ] ) );
1 auto a = vec3(1,2,3); 2 auto b = vecD(1,2,3); 3 auto c = a + b; 4 assert( is( typeof(c) == vec3 ) ); 5 auto d = b + a; 6 assert( is( typeof(d) == vecD ) ); 7 assert( eq( c, d ) ); 8 auto f = ivec3(1,2,3); 9 auto c1 = a + f; 10 assert( is( typeof(c1) == vec3 ) ); 11 auto d1 = ivec3(f) + ivec3(a); 12 assert( is( typeof(d1) == ivec3 ) ); 13 assert( eq( c1, d ) ); 14 assert( eq( c, d1 ) ); 15 16 a *= 2; 17 b *= 2; 18 auto e = b *= 2; 19 assert( eq( a, [2,4,6] ) ); 20 assert( eq( b, a*2 ) ); 21 22 auto x = 2 * a; 23 assert( eq( x, [4,8,12] ) ); 24 25 assert( !!x ); 26 x[0] = float.nan; 27 assert( !x );
auto a = vec3(2,4,6); a /= 2; assert( eq( a, [1,2,3] ) );
1 auto a = vecD(1,2,3); 2 3 auto b = vec3(a); 4 auto c = vecD(b); 5 6 assert( eq( a, b ) ); 7 assert( eq( a, c ) );
auto a = vec3(2,2,1); assert( eq( a.rebase(vec3(2,0,0),vec3(0,2,0),vec3(0,0,2)), [1,1,.5] ) );
1 auto a = vec3(1,2,3); 2 auto b = ivec3(1,2,3); 3 auto k = a.len2; 4 assert( is( typeof(k) == float ) ); 5 6 auto l = b.len2; 7 assert( is( typeof(l) == int ) ); 8 9 auto m = b.len; 10 assert( is( typeof(m) == float ) ); 11 12 auto n = b.len!real; 13 assert( is( typeof(n) == real ) ); 14 15 assert( is( typeof( vec3( 1, 2, 3 ).e ) == vec3 ) ); 16 assert( eq( a.e.len, 1 ) );
1 alias Vector!(3,cfloat) cvec3; 2 3 auto a = cvec3( 1-1i, 2+0i, 0+3i ); 4 static assert( __traits(compiles, a.e) ); 5 6 { auto k = a.e; } // no exception
1 alias Vector!(3,Vector!(3,float)) mat3; 2 auto a = mat3( vec3(1,0,0), vec3(0,1,0), vec3(0,0,1) ); 3 4 a *= 2; 5 a += a; 6 7 assert( eq( a[0][0], 4 ) ); 8 assert( eq( a[1][1], 4 ) ); 9 assert( eq( a[2][2], 4 ) ); 10 11 assert( eq( a[0][1], 0 ) ); 12 assert( eq( a[1][2], 0 ) ); 13 assert( eq( a[2][1], 0 ) ); 14 15 a ^^= 2; 16 17 assert( eq( a[0][0], 16 ) ); 18 assert( eq( a[1][1], 16 ) ); 19 assert( eq( a[2][2], 16 ) ); 20 21 auto b = -a; 22 23 assert( eq( b[0][0], -16) ); 24 assert( eq( b[1][1], -16) ); 25 assert( eq( b[2][2], -16) );