Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4.  
  5.  
  6. namespace Common.Geom
  7. {
  8. /**
  9.  * vector of three floats with some vector functions (right handed), which can safely passed by value to a native library
  10.  * (if passed as pointer it must also be pinned in memory)
  11.  */
  12. [StructLayout(LayoutKind.Sequential)]
  13. public struct Vector3f : IEquatable<Vector3f>
  14. {
  15.     private static Vector3f _zero = new Vector3f(0f,0f,0f);
  16.     public static Vector3f ZERO
  17.     {
  18.         get
  19.         {
  20.             return _zero;
  21.         }
  22.     }
  23.     private static Vector3f _one = new Vector3f(1f, 1f, 1f);
  24.     public static Vector3f ONE
  25.     {
  26.         get
  27.         {
  28.             return _one;
  29.         }
  30.     }
  31.  
  32.     public float x, y, z;
  33.  
  34.     public Vector3f(float x = 0.0f, float y = 0.0f, float z = 0.0f)
  35.     {
  36.         this.x = x;
  37.         this.y = y;
  38.         this.z = z;
  39.     }
  40.  
  41.     public Vector3f(Vector3f other)
  42.     {
  43.         this.x = other.x;
  44.         this.y = other.y;
  45.         this.z = other.z;
  46.     }
  47.  
  48.     public Vector3f(UnityEngine.Vector3 vec3)
  49.     {
  50.         this.x = vec3.x;
  51.         this.y = vec3.z;
  52.         this.z = vec3.y;
  53.     }
  54.  
  55.     public Vector3f(UnityEngine.Vector2 vec2)
  56.     {
  57.         this.x = vec2.x;
  58.         this.y = vec2.y;
  59.         this.z = 0;
  60.     }
  61.     public void set(UnityEngine.Vector3 other)
  62.     {
  63.         this.x = other.x;
  64.         this.y = other.z;
  65.         this.z = other.y;
  66.     }
  67.  
  68.     public void set(Vector3f other) {
  69.         this.x = other.x;
  70.         this.y = other.y;
  71.         this.z = other.z;
  72.     }
  73.  
  74.     public void set(float x, float y, float z)
  75.     {
  76.         this.x = x;
  77.         this.y = y;
  78.         this.z = z;
  79.     }
  80.     public bool isZero()
  81.     {
  82.         return x == 0f && y == 0f && z == 0f;
  83.     }
  84.     public bool isZero(float epsilon) {
  85.         float epsSquare = epsilon*epsilon;
  86.         return x * x <= epsSquare && y * y <= epsSquare && z * z <= epsSquare;
  87.     }
  88.     public void setToZero()
  89.     {
  90.         this.x = this.y = this.z = 0.0f;
  91.     }
  92.  
  93.     public void setToAbsoluteValues()
  94.     {
  95.         if (x < 0.0f) x = -x;
  96.         if (y < 0.0f) y = -y;
  97.         if (z < 0.0f) z = -z;
  98.     }
  99.  
  100.     public void add(ref Vector3f other) {
  101.         x += other.x;
  102.         y += other.y;
  103.         z += other.z;
  104.     }
  105.  
  106.     public void add(Vector3f other)
  107.     {
  108.         add(ref other);
  109.     }
  110.  
  111.     public void subtract(ref Vector3f other) {
  112.         x -= other.x;
  113.         y -= other.y;
  114.         z -= other.z;
  115.     }
  116.     public void subtract(Vector3f other)
  117.     {
  118.         subtract(ref other);
  119.     }
  120.  
  121.     public void scale(float scale) {
  122.         this.x *= scale;
  123.         this.y *= scale;
  124.         this.z *= scale;
  125.     }
  126.  
  127.     public void scaleAdd(float scale, ref Vector3f other)
  128.     {
  129.         this.x += scale * other.x;
  130.         this.y += scale * other.y;
  131.         this.z += scale * other.z;
  132.     }
  133.  
  134.     public void scaleAdd(float scale, Vector3f other)
  135.     {
  136.         scaleAdd(scale, ref other);  
  137.     }
  138.  
  139.     public void divide(float divisor)
  140.     {
  141.         this.x /= divisor;
  142.         this.y /= divisor;
  143.         this.z /= divisor;
  144.     }
  145.  
  146.     /**
  147.      * scalar product
  148.      */
  149.     public float dot(ref Vector3f other)
  150.     {
  151.         return this.x * other.x + this.y * other.y + this.z * other.z;
  152.     }
  153.  
  154.     /**
  155.      * scalar product
  156.      */
  157.     public float dot(Vector3f other)
  158.     {
  159.         return this.x * other.x + this.y * other.y + this.z * other.z;
  160.     }
  161.  
  162.     /**
  163.      * cross product stored in this vector
  164.      */
  165.     public void crossWith(ref Vector3f other)
  166.     {
  167.         float nx = this.y * other.z - this.z * other.y;
  168.         float ny = this.z * other.x - this.x * other.z;
  169.         float nz = this.x * other.y - this.y * other.x;
  170.         this.x = nx;
  171.         this.y = ny;
  172.         this.z = nz;
  173.     }
  174.     public void crossWith(Vector3f other)
  175.     {
  176.         crossWith(ref other);
  177.     }
  178.  
  179.     public bool normalize()
  180.     {
  181.         float l = (float)length();
  182.         if (l == 0.0f) return false;
  183.         x = x/l; y = y/l; z = z/l;
  184.         return true;
  185.     }
  186.  
  187.     public float lengthSquared() {
  188.         return x * x  +  y * y  +  z * z;
  189.     }
  190.     public double length() {
  191.         return Math.Sqrt(lengthSquared());
  192.     }
  193.     public float lengthf()
  194.     {
  195.         return (float)Math.Sqrt(lengthSquared());
  196.     }
  197.  
  198.     public bool setLength(float newLength)
  199.     {
  200.         float l = (float)length();
  201.         if (l == 0.0) return false;
  202.         x = (x / l) * newLength;
  203.         y = (y / l) * newLength;
  204.         z = (z / l) * newLength;
  205.         return true;
  206.     }
  207.  
  208.     public float distanceSquared(ref Vector3f other) {
  209.         float dx = other.x - x;
  210.         float dy = other.y - y;
  211.         float dz = other.z - z;
  212.         return dx * dx + dy * dy + dz * dz;
  213.     }
  214.     public float distanceSquared(Vector3f other)
  215.     {
  216.         return distanceSquared(ref other); ;
  217.     }
  218.     public double distance(ref Vector3f other) {
  219.         return Math.Sqrt(distanceSquared(ref other));
  220.     }
  221.     public double distance(Vector3f other)
  222.     {
  223.         return distance(ref other);
  224.     }
  225.     public float getMaxComponent() {
  226.         return x >= y ? (x >= z ? x : z) : (y >= z ? y : z);
  227.     }
  228.  
  229.     public void getLefthanded(ref UnityEngine.Vector3 lhVector3)
  230.     {
  231.         lhVector3.x = x;
  232.         lhVector3.y = z;
  233.         lhVector3.z = y;
  234.     }
  235.  
  236.     public static implicit operator UnityEngine.Vector3(Vector3f v)
  237.     {
  238.         return new UnityEngine.Vector3(v.x, v.z, v.y);
  239.     }
  240.     public static implicit operator Vector3f(UnityEngine.Vector3 v)
  241.     {
  242.         return new Vector3f(v);
  243.     }
  244.  
  245.     public static implicit operator Vector3f(UnityEngine.Vector2 v)
  246.     {
  247.         return new Vector3f(v);
  248.     }
  249.  
  250.     public Vector3f Rotate(float angleDegree)
  251.     {
  252.         float rad = angleDegree * UnityEngine.Mathf.Deg2Rad;
  253.         float rx = (x * UnityEngine.Mathf.Cos(rad)) - (y * UnityEngine.Mathf.Sin(rad));
  254.         float ry = (y * UnityEngine.Mathf.Cos(rad)) + (x * UnityEngine.Mathf.Sin(rad));
  255.         return new Vector3f(rx,ry);
  256.     }
  257.  
  258.     public static int Side(Vector3f line, Vector3f p)
  259.     {
  260.         Vector3f perp = new Vector3f(-line.y, line.x);
  261.         float d = p.dot(ref perp);
  262.         return Math.Sign(d);
  263.     }
  264.  
  265.     public Vector3f Ground()
  266.     {
  267.         return new Vector3f(this.x, this.y, 0);
  268.     }
  269.  
  270.     public override bool Equals(object obj)
  271.     {
  272.         // If parameter is null return false.
  273.         if (obj == null)
  274.         {
  275.             return false;
  276.         }
  277.  
  278.         // If parameter cannot be cast to Point return false.
  279.         Vector3f? v = obj as Vector3f?;
  280.         if ((System.Object)v == null)
  281.         {
  282.             return false;
  283.         }
  284.         return this == v;
  285.     }
  286.     public bool Equals(Vector3f other)
  287.     {
  288.         return this == other;
  289.     }
  290.  
  291.     public bool Equals(ref Vector3f other)
  292.     {
  293.         return this == other;
  294.     }
  295.  
  296.     public override int GetHashCode()
  297.     {
  298.        
  299.         return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
  300.     }
  301.  
  302.     /**
  303.      * indexing operator
  304.      */
  305.     public float this[int i] {
  306.         get
  307.         {
  308.             switch (i)
  309.             {
  310.                 case 0: return x;
  311.                 case 1: return y;
  312.                 case 2: return z;
  313.                 default: throw new IndexOutOfRangeException("Index " + i + " is out of " + GetType() + " Range");
  314.             }
  315.         }
  316.         set {
  317.             switch (i)
  318.             {
  319.                 case 0: x = value; break;
  320.                 case 1: y = value; break;
  321.                 case 2: z = value; break;
  322.                 default: throw new IndexOutOfRangeException("Index " + i + " is out of " + GetType() + " Range");
  323.             }
  324.         }
  325.     }
  326.    
  327.     public static Vector3f operator + (Vector3f a, Vector3f b)
  328.     {
  329.           return new Vector3f(a.x + b.x, a.y + b.y, a.z + b.z);
  330.     }
  331.  
  332.     public static Vector3f operator + (Vector3f a, float offset)
  333.     {
  334.         return new Vector3f(a.x + offset, a.y + offset, a.z + offset);
  335.     }
  336.  
  337.     public static Vector3f operator + (float offset, Vector3f a)
  338.     {
  339.         return new Vector3f(offset + a.x, offset + a.y, offset + a.z);
  340.     }
  341.  
  342.     public static Vector3f operator - (Vector3f a, Vector3f b)
  343.     {
  344.         return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);
  345.     }
  346.  
  347.     public static Vector3f operator - (Vector3f a, float offset)
  348.     {
  349.         return new Vector3f(a.x - offset, a.y - offset, a.z - offset);
  350.     }
  351.  
  352.     public static Vector3f operator - (float offset, Vector3f a)
  353.     {
  354.         return new Vector3f(offset - a.x, offset - a.y, offset - a.z);
  355.     }
  356.  
  357.     public static Vector3f operator * (Vector3f a, float scale)
  358.     {
  359.         return new Vector3f(a.x * scale, a.y * scale, a.z * scale);
  360.     }
  361.  
  362.     public static Vector3f operator * (float scale, Vector3f a)
  363.     {
  364.         return a * scale;
  365.     }
  366.  
  367.     public static Vector3f operator / (Vector3f a, float scale)
  368.     {
  369.         return new Vector3f(a.x / scale, a.y / scale, a.z / scale);
  370.     }
  371.  
  372.     /**
  373.      * cross product
  374.      */
  375.     public static Vector3f operator *(Vector3f a, Vector3f b)
  376.     {
  377.         return new Vector3f(
  378.             a.y * b.z - a.z * b.y,
  379.             a.z * b.x - a.x * b.z,
  380.             a.x * b.y - a.y * b.x
  381.         );
  382.     }
  383.  
  384.  
  385.     public static bool operator == (Vector3f a, Vector3f b)
  386.     {
  387.         return a.x == b.x && a.y == b.y && a.z == b.z;
  388.     }
  389.     public static bool operator != (Vector3f a, Vector3f b)
  390.     {
  391.         return !(a == b);
  392.     }
  393.     public static bool operator < (Vector3f a, Vector3f b)
  394.     {
  395.         return a.lengthSquared() < b.lengthSquared();
  396.     }
  397.     public static bool operator <= (Vector3f a, Vector3f b)
  398.     {
  399.         return a.lengthSquared() <= b.lengthSquared();
  400.     }
  401.     public static bool operator > (Vector3f a, Vector3f b)
  402.     {
  403.         return a.lengthSquared() > b.lengthSquared();
  404.     }
  405.  
  406.     public static bool operator >= (Vector3f a, Vector3f b)
  407.     {
  408.         return a.lengthSquared() >= b.lengthSquared();
  409.     }
  410.  
  411.     public override string ToString() {
  412.         return GetType().Name + "( " + x + " ; " + y + " ; " + z + " )";
  413.  
  414.     }
  415. }
  416.  
  417. public static class Vector3Ext
  418. {
  419.     public static bool TryParse(this UnityEngine.Vector2 vec, string arg)
  420.     {
  421.         if (arg == null || arg == "" || !arg.Contains(";"))
  422.         {
  423.             vec = default(UnityEngine.Vector2);
  424.             return false;
  425.         }
  426.  
  427.         string[] strVals = arg.Split(';');
  428.  
  429.         if (strVals == null || strVals.Length != 2)
  430.         {
  431.             vec = default(UnityEngine.Vector2);
  432.             return false;
  433.         }
  434.  
  435.         float vX, vY;
  436.         float.TryParse(strVals[0], out vX);
  437.         float.TryParse(strVals[1], out vY);
  438.  
  439.         vec = new UnityEngine.Vector2();
  440.         vec.x = vX;
  441.         vec.y = vY;
  442.  
  443.         return true;
  444.     }
  445.     public static bool TryParse(this UnityEngine.Vector3 vec, string arg)
  446.     {
  447.         if (arg == null || arg == "" || !arg.Contains(";"))
  448.             return false;
  449.  
  450.         string[] strVals = arg.Split(';');
  451.  
  452.         if(strVals == null || strVals.Length != 3)
  453.             return false;
  454.  
  455.         float vX, vY, vZ;
  456.         if(float.TryParse(strVals[0], out vX))
  457.             return false;
  458.         if(float.TryParse(strVals[1], out vY))
  459.             return false;
  460.         if(float.TryParse(strVals[2], out vZ))
  461.             return false;
  462.  
  463.         vec = new UnityEngine.Vector3();
  464.         vec.x = vX;
  465.         vec.y = vY;
  466.         vec.z = vZ;
  467.  
  468.         return true;
  469.     }
  470. }
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement