| | 1 | | using Newtonsoft.Json; |
| | 2 | | using System; |
| | 3 | |
|
| | 4 | | namespace MorphoGeometry |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Ray class. |
| | 8 | | /// </summary> |
| | 9 | | public class Ray : IEquatable<Ray> |
| | 10 | | { |
| | 11 | | [JsonProperty(Required = Required.Always)] |
| | 12 | | /// <summary> |
| | 13 | | /// Ray origin. |
| | 14 | | /// </summary> |
| | 15 | | public Vector origin; |
| | 16 | |
|
| | 17 | | [JsonProperty(Required = Required.Always)] |
| | 18 | | /// <summary> |
| | 19 | | /// Direction of the ray. |
| | 20 | | /// </summary> |
| | 21 | | public Vector direction; |
| | 22 | |
|
| | 23 | | [JsonConstructor] |
| | 24 | | /// <summary> |
| | 25 | | /// Create a new Ray. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="origin">Origin of the ray.</param> |
| | 28 | | /// <param name="direction">Direction of the ray.</param> |
| 3 | 29 | | public Ray(Vector origin, |
| 3 | 30 | | Vector direction) |
| 3 | 31 | | { |
| 3 | 32 | | this.origin = origin; |
| 3 | 33 | | this.direction = direction; |
| 3 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// String representation of the ray. |
| | 38 | | /// </summary> |
| | 39 | | /// <returns>String representation.</returns> |
| | 40 | | public override String ToString() |
| 0 | 41 | | { |
| 0 | 42 | | return string.Format("Ray::{0}", direction); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | public string Serialize() |
| 1 | 46 | | { |
| 1 | 47 | | return JsonConvert.SerializeObject(this); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | public static Ray Deserialize(string json) |
| 2 | 51 | | { |
| | 52 | | try |
| 2 | 53 | | { |
| 2 | 54 | | return JsonConvert.DeserializeObject<Ray>(json); |
| | 55 | | } |
| 1 | 56 | | catch (Exception e) |
| 1 | 57 | | { |
| 1 | 58 | | throw new Exception(e.Message); |
| | 59 | | } |
| 1 | 60 | | } |
| | 61 | |
|
| | 62 | | public bool Equals(Ray other) |
| 1 | 63 | | { |
| 1 | 64 | | if (other == null) |
| 0 | 65 | | return false; |
| | 66 | |
|
| 1 | 67 | | if (other != null |
| 1 | 68 | | && other.origin == this.origin |
| 1 | 69 | | && other.direction == this.direction) |
| 1 | 70 | | return true; |
| | 71 | | else |
| 0 | 72 | | return false; |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | public override bool Equals(Object obj) |
| 2 | 76 | | { |
| 2 | 77 | | if (obj == null) |
| 0 | 78 | | return false; |
| | 79 | |
|
| 2 | 80 | | var rayObj = obj as Ray; |
| 2 | 81 | | if (rayObj == null) |
| 2 | 82 | | return false; |
| | 83 | | else |
| 0 | 84 | | return Equals(rayObj); |
| 2 | 85 | | } |
| | 86 | |
|
| | 87 | | public override int GetHashCode() |
| 0 | 88 | | { |
| | 89 | | unchecked |
| 0 | 90 | | { |
| 0 | 91 | | int hash = 17; |
| 0 | 92 | | hash = hash * 23 + origin.GetHashCode(); |
| 0 | 93 | | hash = hash * 23 + direction.GetHashCode(); |
| 0 | 94 | | return hash; |
| | 95 | | } |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public static bool operator ==(Ray ray1, Ray ray2) |
| 4 | 99 | | { |
| 4 | 100 | | if (((object)ray1) == null || ((object)ray2) == null) |
| 4 | 101 | | return Object.Equals(ray1, ray2); |
| | 102 | |
|
| 0 | 103 | | return ray1.Equals(ray2); |
| 4 | 104 | | } |
| | 105 | |
|
| | 106 | | public static bool operator !=(Ray ray1, Ray ray2) |
| 1 | 107 | | { |
| 1 | 108 | | return !(ray1 == ray2); |
| 1 | 109 | | } |
| | 110 | | } |
| | 111 | | } |