Advertisement
Manuzor

Untitled

Feb 13th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.81 KB | None | 0 0
  1. struct Vec2
  2. {
  3.   float X, Y;
  4.  
  5.   Vec2 opDispatch(string Members)()
  6.     if(Members.length == 2)
  7.   {
  8.     mixin(`return typeof(return)(this.` ~ Members[0] ~ `, this.` ~ Members[1] ~`);`);
  9.   }
  10.  
  11.   Vec3 opDispatch(string Members)()
  12.     if(Members.length == 3)
  13.   {
  14.     mixin(`return typeof(return)(this.` ~ Members[0] ~ `, this.` ~ Members[1] ~ `, this.` ~ Members[2] ~`);`);
  15.   }
  16.  
  17.   Vec4 opDispatch(string Members)()
  18.     if(Members.length == 4)
  19.   {
  20.     mixin(`return typeof(return)(this.` ~ Members[0] ~ `, this.` ~ Members[1] ~ `, this.` ~ Members[2] ~ `, this.` ~ Members[3] ~`);`);
  21.   }
  22. }
  23.  
  24. struct Vec3
  25. {
  26.   float X, Y, Z;
  27.  
  28.   this(float X, float Y, float Z)
  29.   {
  30.     this.X = X;
  31.     this.Y = Y;
  32.     this.Z = Z;
  33.   }
  34.  
  35.   this(Vec2 V, float Z)
  36.   {
  37.     this.X = V.X;
  38.     this.Y = V.Y;
  39.     this.Z = Z;
  40.   }
  41.  
  42.   this(float X, Vec2 V)
  43.   {
  44.     this.X = X;
  45.     this.Y = V.X;
  46.     this.Z = V.Y;
  47.   }
  48. }
  49.  
  50. struct Vec4
  51. {
  52.   union
  53.   {
  54.     struct
  55.     {
  56.       float X, Y, Z, W;
  57.     }
  58.  
  59.     float[4] Data;
  60.   }
  61.  
  62.   this(float X, float Y, float Z, float W)
  63.   {
  64.     this.X = X;
  65.     this.Y = Y;
  66.     this.Z = Z;
  67.     this.W = W;
  68.   }
  69.  
  70.   this(Vec2 V, float Z, float W)
  71.   {
  72.     this.X = V.X;
  73.     this.Y = V.Y;
  74.     this.Z = Z;
  75.     this.W = W;
  76.   }
  77.  
  78.   this(float X, Vec2 V, float W)
  79.   {
  80.     this.X = X;
  81.     this.Y = V.X;
  82.     this.Z = V.Y;
  83.     this.W = W;
  84.   }
  85.  
  86.   this(float X, float Y, Vec2 V)
  87.   {
  88.     this.X = X;
  89.     this.Y = Y;
  90.     this.Z = V.X;
  91.     this.W = V.Y;
  92.   }
  93.  
  94.   this(Vec2 V1, Vec2 V2)
  95.   {
  96.     this.X = V1.X;
  97.     this.Y = V1.Y;
  98.     this.Z = V2.X;
  99.     this.W = V2.Y;
  100.   }
  101.  
  102.   this(Vec3 V, float W)
  103.   {
  104.     this.X = V.X;
  105.     this.Y = V.Y;
  106.     this.Z = V.Z;
  107.     this.W = W;
  108.   }
  109.  
  110.   this(float X, Vec3 V)
  111.   {
  112.     this.X = X;
  113.     this.Y = V.X;
  114.     this.Z = V.Y;
  115.     this.W = V.Z;
  116.   }
  117. }
  118.  
  119. // Vec2
  120. unittest
  121. {
  122.   assert(Vec2(1, 2).X == 1);
  123.   assert(Vec2(1, 2).Y == 2);
  124.  
  125.   assert(Vec2(1, 2).YX   == Vec2(2, 1));
  126.   assert(Vec2(1, 2).XYX  == Vec3(1, 2, 1));
  127.   assert(Vec2(1, 2).XYXY == Vec4(1, 2, 1, 2));
  128.  
  129.   static assert(!__traits(compiles, Vec2(1, 2).Foo), "Swizzling is only supposed to work with value members of " ~ Vec2.stringof ~ ".");
  130.   static assert(!__traits(compiles, Vec2(1, 2).XXXXX), "Swizzling output dimension is limited to 4.");
  131. }
  132.  
  133. // Vec3
  134. unittest
  135. {
  136.   assert(Vec3(1, 2, 3).X == 1);
  137.   assert(Vec3(1, 2, 3).Y == 2);
  138.   assert(Vec3(1, 2, 3).Z == 3);
  139.  
  140.   assert(Vec3(Vec2(1, 2), 3).X == 1);
  141.   assert(Vec3(Vec2(1, 2), 3).Y == 2);
  142.   assert(Vec3(Vec2(1, 2), 3).Z == 3);
  143.  
  144.   assert(Vec3(1, Vec2(2, 3)).X == 1);
  145.   assert(Vec3(1, Vec2(2, 3)).Y == 2);
  146.   assert(Vec3(1, Vec2(2, 3)).Z == 3);
  147. }
  148.  
  149. // Vec4
  150. unittest
  151. {
  152.   assert(Vec4(1, 2, 3, 4).X == 1);
  153.   assert(Vec4(1, 2, 3, 4).Y == 2);
  154.   assert(Vec4(1, 2, 3, 4).Z == 3);
  155.   assert(Vec4(1, 2, 3, 4).W == 4);
  156.  
  157.   assert(Vec4(Vec2(1, 2), 3, 4).X == 1);
  158.   assert(Vec4(Vec2(1, 2), 3, 4).Y == 2);
  159.   assert(Vec4(Vec2(1, 2), 3, 4).Z == 3);
  160.   assert(Vec4(Vec2(1, 2), 3, 4).W == 4);
  161.  
  162.   assert(Vec4(1, Vec2(2, 3), 4).X == 1);
  163.   assert(Vec4(1, Vec2(2, 3), 4).Y == 2);
  164.   assert(Vec4(1, Vec2(2, 3), 4).Z == 3);
  165.   assert(Vec4(1, Vec2(2, 3), 4).W == 4);
  166.  
  167.   assert(Vec4(1, 2, Vec2(3, 4)).X == 1);
  168.   assert(Vec4(1, 2, Vec2(3, 4)).Y == 2);
  169.   assert(Vec4(1, 2, Vec2(3, 4)).Z == 3);
  170.   assert(Vec4(1, 2, Vec2(3, 4)).W == 4);
  171.  
  172.   assert(Vec4(Vec2(1, 2), Vec2(3, 4)).X == 1);
  173.   assert(Vec4(Vec2(1, 2), Vec2(3, 4)).Y == 2);
  174.   assert(Vec4(Vec2(1, 2), Vec2(3, 4)).Z == 3);
  175.   assert(Vec4(Vec2(1, 2), Vec2(3, 4)).W == 4);
  176.  
  177.   assert(Vec4(Vec3(1, 2, 3), 4).X == 1);
  178.   assert(Vec4(Vec3(1, 2, 3), 4).Y == 2);
  179.   assert(Vec4(Vec3(1, 2, 3), 4).Z == 3);
  180.   assert(Vec4(Vec3(1, 2, 3), 4).W == 4);
  181.  
  182.   assert(Vec4(1, Vec3(2, 3, 4)).X == 1);
  183.   assert(Vec4(1, Vec3(2, 3, 4)).Y == 2);
  184.   assert(Vec4(1, Vec3(2, 3, 4)).Z == 3);
  185.   assert(Vec4(1, Vec3(2, 3, 4)).W == 4);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement