Advertisement
Guest User

Point.d

a guest
Feb 9th, 2013
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 8.17 KB | None | 0 0
  1. /**
  2. Copyright (c) 2013, w0rp <moebiuspesona@gmail.com>
  3.  
  4. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8.  
  9. 1. Redistributions of source code must retain the above copyright notice, this
  10.    list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  
  26. */
  27.  
  28. module geometry.Point;
  29.  
  30. enum Axis: byte
  31. {
  32.    X,
  33.    Y,
  34.    Z
  35. }
  36.  
  37. struct Point
  38. {
  39.     public real x = 0.0;
  40.     public real y = 0.0;
  41.     public real z = 0.0;
  42.  
  43.     /**
  44.         @brief Compute the square of the norm.
  45.  
  46.         Compute the norm without applying the square root operation.
  47.  
  48.         @param The square of the norm.
  49.      */
  50.     public @property const pure nothrow
  51.     real sq_norm()
  52.     {
  53.         return this.x ^^ 2 + this.y ^^ 2 + this.z ^^ 2;
  54.     }
  55.  
  56.     /**
  57.         @brief Compute the Euclidean norm of this point.
  58.  
  59.         @return The norm.
  60.      */
  61.     public @property const pure nothrow
  62.     real norm()
  63.     {
  64.         return sqrt(this.sq_norm);
  65.     }
  66.  
  67.     /**
  68.         @brief Normalize this vector to get a unit vector.
  69.  
  70.         @return The normalized vector.
  71.      */
  72.     public @property const pure nothrow
  73.     Point normalize()
  74.     {
  75.         return this / this.norm;
  76.     }
  77.  
  78.     public pure const nothrow
  79.     Point opUnary(string s)() if (s == "-")
  80.     {
  81.         return Point(-this.x, -this.y, -this.z);
  82.     }
  83.  
  84.     unittest
  85.     {
  86.         Point p = {1, 2, 3};
  87.         Point q = -p;
  88.  
  89.         assert(-p.x == q.x);
  90.         assert(-p.y == q.y);
  91.         assert(-p.z == q.z);
  92.     }
  93.  
  94.     public pure const nothrow
  95.     bool opEquals(in Point other)
  96.     {
  97.         return approxEqual(this.x, other.x)
  98.             && approxEqual(this.y, other.y)
  99.             && approxEqual(this.z, other.z);
  100.     }
  101.  
  102.     unittest
  103.     {
  104.         Point p = {1, 2, 3};
  105.         Point q = {1, 2, 3};
  106.  
  107.         assert(p == q);
  108.  
  109.         Point r = {2, 3, 4};
  110.  
  111.         assert (p != r);
  112.     }
  113.  
  114.     public pure const nothrow
  115.     Point opBinary(string s)(in Point other) if (s == "+")
  116.     {
  117.         return Point(this.x + other.x, this.y + other.y, this.z + other.z);
  118.     }
  119.  
  120.     unittest
  121.     {
  122.         Point p = {1, 2, 3};
  123.         Point q = {1, 2, 3};
  124.  
  125.         Point r = p + q;
  126.  
  127.         assert(r.x == 2);
  128.         assert(r.y == 4);
  129.         assert(r.z == 6);
  130.     }
  131.  
  132.     public pure const nothrow
  133.     Point opBinary(string s)(in Point other) if (s == "-")
  134.     {
  135.         return Point(this.x - other.x, this.y - other.y, this.z - other.z);
  136.     }
  137.  
  138.     unittest
  139.     {
  140.         Point p = {1, 2, 3};
  141.         Point q = {1, 2, 3};
  142.  
  143.         Point r = p - q;
  144.  
  145.         assert(r.x == 0);
  146.         assert(r.y == 0);
  147.         assert(r.z == 0);
  148.     }
  149.  
  150.     // Scalar multiplication.
  151.     public pure const nothrow
  152.     Point opBinary(string s)(in real scalar) if (s == "*")
  153.     {
  154.         return Point(this.x * scalar, this.y * scalar, this.z * scalar);
  155.     }
  156.  
  157.     public pure const nothrow
  158.     Point opBinaryRight(string s)(in real scalar) if (s == "*")
  159.     {
  160.         return this * scalar;
  161.     }
  162.  
  163.     unittest
  164.     {
  165.         Point p = {1, 2, 3};
  166.  
  167.         Point q = p * 2;
  168.         Point r = 2 * p;
  169.  
  170.         assert(q.x == 2);
  171.         assert(q.y == 4);
  172.         assert(q.z == 6);
  173.         assert(q.x == r.x);
  174.         assert(q.y == r.y);
  175.         assert(q.z == r.z);
  176.     }
  177.  
  178.     // Dot product.
  179.     public pure const nothrow
  180.     real opBinary(string s)(in Point other) if (s == "*")
  181.     {
  182.         return this.x * other.x + this.y * other.y + this.z * other.z;
  183.     }
  184.  
  185.     unittest
  186.     {
  187.         Point p = {1, 2, 3};
  188.         Point q = {4, 5, 6};
  189.  
  190.         assert(p * q == 32);
  191.     }
  192.  
  193.     // Scalar divison.
  194.     public pure const nothrow
  195.     Point opBinary(string s)(in real scalar) if (s == "/")
  196.     {
  197.         return Point(this.x / scalar, this.y / scalar, this.z / scalar);
  198.     }
  199.  
  200.     unittest
  201.     {
  202.         Point p = {2, 4, 6};
  203.  
  204.         Point q = p / 2;
  205.  
  206.         assert(q.x == 1);
  207.         assert(q.y == 2);
  208.         assert(q.z == 3);
  209.     }
  210.  
  211.     // Scalar exponentiation.
  212.     public pure const nothrow
  213.     Point opBinary(string s)(in real scalar) if (s == "^^")
  214.     {
  215.         return Point(this.x ^^ scalar, this.y ^^ scalar, this.z ^^ scalar);
  216.     }
  217.  
  218.     unittest
  219.     {
  220.         Point p = {1, 2, 3};
  221.  
  222.         Point q = p ^^ 2;
  223.  
  224.         assert(q.x == 1);
  225.         assert(q.y == 4);
  226.         assert(q.z == 9);
  227.     }
  228.  
  229.     public pure nothrow
  230.     void opOpAssign(string s)(in real scalar) if (s == "+")
  231.     {
  232.         this.x += scalar;
  233.         this.y += scalar;
  234.         this.z += scalar;
  235.     }
  236.  
  237.     unittest
  238.     {
  239.         Point p = {1, 2, 3};
  240.  
  241.         p += 1;
  242.  
  243.         assert(p.x == 2);
  244.         assert(p.y == 3);
  245.         assert(p.z == 4);
  246.     }
  247.  
  248.     public pure nothrow
  249.     void opOpAssign(string s)(in Point other) if (s == "+")
  250.     {
  251.         this.x += other.x;
  252.         this.y += other.y;
  253.         this.z += other.z;
  254.     }
  255.  
  256.     unittest
  257.     {
  258.         Point p = {1, 2, 3};
  259.         Point q = {4, 5, 6};
  260.  
  261.         p += q;
  262.  
  263.         assert(p.x == 5);
  264.         assert(p.y == 7);
  265.         assert(p.z == 9);
  266.     }
  267.  
  268.     public pure nothrow
  269.     void opOpAssign(string s)(in real scalar) if (s == "-")
  270.     {
  271.         this.x -= scalar;
  272.         this.y -= scalar;
  273.         this.z -= scalar;
  274.     }
  275.  
  276.     unittest
  277.     {
  278.         Point p = {1, 2, 3};
  279.  
  280.         p -= 1;
  281.  
  282.         assert(p.x == 0);
  283.         assert(p.y == 1);
  284.         assert(p.z == 2);
  285.     }
  286.  
  287.     public pure nothrow
  288.     void opOpAssign(string s)(in Point other) if (s == "-")
  289.     {
  290.         this.x -= other.x;
  291.         this.y -= other.y;
  292.         this.z -= other.z;
  293.     }
  294.  
  295.     unittest
  296.     {
  297.         Point p = {2, 4, 6};
  298.         Point q = {1, 2, 3};
  299.  
  300.         p -= q;
  301.  
  302.         assert(p.x == 1);
  303.         assert(p.y == 2);
  304.         assert(p.z == 3);
  305.     }
  306.  
  307.     public pure nothrow
  308.     void opOpAssign(string s)(in real scalar) if (s == "*")
  309.     {
  310.         this.x *= scalar;
  311.         this.y *= scalar;
  312.         this.z *= scalar;
  313.     }
  314.  
  315.     unittest
  316.     {
  317.         Point p = {1, 2, 3};
  318.  
  319.         p *= 2;
  320.  
  321.         assert(p.x == 2);
  322.         assert(p.y == 4);
  323.         assert(p.z == 6);
  324.     }
  325.  
  326.     public pure nothrow
  327.     void opOpAssign(string s)(in real scalar) if (s == "/")
  328.     {
  329.         this.x /= scalar;
  330.         this.y /= scalar;
  331.         this.z /= scalar;
  332.     }
  333.  
  334.     unittest
  335.     {
  336.         Point p = {2, 4, 6};
  337.  
  338.         p /= 2;
  339.  
  340.         assert(p.x == 1);
  341.         assert(p.y == 2);
  342.         assert(p.z == 3);
  343.     }
  344.  
  345.     public pure nothrow
  346.     void opOpAssign(string s)(in real scalar) if (s == "^^")
  347.     {
  348.         this.x ^^= scalar;
  349.         this.y ^^= scalar;
  350.         this.z ^^= scalar;
  351.     }
  352.  
  353.     unittest
  354.     {
  355.         Point p = {1, 2, 3};
  356.  
  357.         p ^^= 2;
  358.  
  359.         assert(p.x == 1);
  360.         assert(p.y == 4);
  361.         assert(p.z == 9);
  362.     }
  363. }
  364.  
  365. /**
  366.     @brief Compute the cross product of this point and another.
  367.  
  368.     @param other The second point.
  369.  
  370.     @return The product.
  371.  */
  372. pure nothrow
  373. Point cross(in Point left, in Point right)
  374. {
  375.     Point p;
  376.  
  377.     p.x = left.y * right.z - left.z * right.y;
  378.     p.y = left.z * right.x - left.x * right.z;
  379.     p.z = left.x * right.y - left.y * right.x;
  380.  
  381.     return p;
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement