| | 1 | | using ReFlex.Core.Common.Interfaces; |
| | 2 | |
|
| | 3 | | namespace ReFlex.Core.Common.Components |
| | 4 | | { |
| | 5 | | /// <inheritdoc /> |
| | 6 | | /// <summary> |
| | 7 | | /// A three dimensional point. |
| | 8 | | /// @todo: checks and fallback values for null references for operators and methods |
| | 9 | | /// </summary> |
| | 10 | | /// <seealso cref="IBase3" /> |
| | 11 | | public class Point3 : IBase3 |
| | 12 | | { |
| | 13 | | /// <inheritdoc /> |
| | 14 | | /// <summary> |
| | 15 | | /// Gets or sets the x value. |
| | 16 | | /// </summary> |
| | 17 | | /// <value> |
| | 18 | | /// The x value. |
| | 19 | | /// </value> |
| 34722951 | 20 | | public float X { get; set; } |
| | 21 | |
|
| | 22 | | /// <inheritdoc /> |
| | 23 | | /// <summary> |
| | 24 | | /// Gets or sets the y value. |
| | 25 | | /// </summary> |
| | 26 | | /// <value> |
| | 27 | | /// The y value. |
| | 28 | | /// </value> |
| 34721392 | 29 | | public float Y { get; set; } |
| | 30 | |
|
| | 31 | | /// <inheritdoc /> |
| | 32 | | /// <summary> |
| | 33 | | /// Gets or sets the z value. |
| | 34 | | /// </summary> |
| | 35 | | /// <value> |
| | 36 | | /// The z value. |
| | 37 | | /// </value> |
| 34806732 | 38 | | public float Z { get; set; } |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Returns true if ... is valid. |
| | 42 | | /// </summary> |
| | 43 | | /// <value> |
| | 44 | | /// <c>true</c> if this instance is valid; otherwise, <c>false</c>. |
| | 45 | | /// </value> |
| 19711510 | 46 | | public bool IsValid { get; set; } = true; |
| | 47 | |
|
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Returns true if the point is excluded by a filter. |
| | 51 | | /// </summary> |
| | 52 | | /// <value> |
| | 53 | | /// <c>true</c> if this instance is removed by a filtered, otherwise <c>false</c>. |
| | 54 | | /// </value> |
| 19630156 | 55 | | public bool IsFiltered { get; set; } = false; |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Initializes a new instance of the <see cref="Point3"/> class. |
| | 59 | | /// </summary> |
| 21520712 | 60 | | public Point3() => Set(0, 0, 0); |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Initializes a new instance of the <see cref="Point3"/> class. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="x">The x.</param> |
| | 66 | | /// <param name="y">The y.</param> |
| | 67 | | /// <param name="z">The z.</param> |
| 8591232 | 68 | | public Point3(float x, float y, float z) => Set(x, y, z); |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Initializes a new instance of the <see cref="Point3"/> class. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="base3">The base3.</param> |
| 6 | 74 | | public Point3(IBase3 base3) => Set(base3); |
| | 75 | |
|
| | 76 | | /// <inheritdoc /> |
| | 77 | | /// <summary> |
| | 78 | | /// Copies the values from the input to [this] instance. |
| | 79 | | /// </summary> |
| | 80 | | /// <param name="base3">The base3 input.</param> |
| 4396708 | 81 | | public void Set(IBase3 base3) => Set(base3.X, base3.Y, base3.Z); |
| | 82 | |
|
| | 83 | | /// <inheritdoc /> |
| | 84 | | /// <summary> |
| | 85 | | /// Copies this instance. |
| | 86 | | /// </summary> |
| | 87 | | /// <returns>a copy of this instance</returns> |
| 2 | 88 | | public IBase3 Copy() => new Point3(this) { IsValid = IsValid, IsFiltered = IsFiltered }; |
| | 89 | |
|
| | 90 | | /// <inheritdoc /> |
| | 91 | | /// <summary> |
| | 92 | | /// Sets the x, y and z value. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="x">The x value.</param> |
| | 95 | | /// <param name="y">The y value.</param> |
| | 96 | | /// <param name="z">The z value.</param> |
| | 97 | | public void Set(float x, float y, float z) |
| 19452680 | 98 | | { |
| 19452680 | 99 | | X = x; |
| 19452680 | 100 | | Y = y; |
| 19452680 | 101 | | Z = z; |
| 19452680 | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Direction from start to end. |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="start">The start.</param> |
| | 108 | | /// <param name="end">The end.</param> |
| | 109 | | /// <returns>A <see cref="Vector3"/> which points from start to end.</returns> |
| | 110 | | public static Vector3 Direction(Point3 start, Point3 end) => |
| 4 | 111 | | new Vector3(end.X - start.X, end.Y - start.Y, end.Z - start.Z); |
| | 112 | |
|
| | 113 | | /// <summary> |
| | 114 | | /// Interpolates between point1 and point2. |
| | 115 | | /// </summary> |
| | 116 | | /// <param name="point1">The point1.</param> |
| | 117 | | /// <param name="point2">The point2.</param> |
| | 118 | | /// <param name="weight">The weight.</param> |
| | 119 | | /// <returns>Interpolated point.</returns> |
| | 120 | | public static Point3 Interpolate(Point3 point1, Point3 point2, float weight) |
| 3 | 121 | | { |
| 3 | 122 | | var x = (1 - weight) * point1.X + weight * point2.X; |
| 3 | 123 | | var y = (1 - weight) * point1.Y + weight * point2.Y; |
| 3 | 124 | | var z = (1 - weight) * point1.Z + weight * point2.Z; |
| 3 | 125 | | return new Point3(x, y, z); |
| 3 | 126 | | } |
| | 127 | |
|
| | 128 | | public static Point3 operator +(Point3 a, Point3 b) |
| 1 | 129 | | { |
| 1 | 130 | | return new Point3(a.X + b.X, a.Y + b.Y, a.Z + b.Z); |
| 1 | 131 | | } |
| | 132 | |
|
| | 133 | | public static Point3 operator -(Point3 a, Point3 b) |
| 1 | 134 | | { |
| 1 | 135 | | return new Point3(a.X - b.X, a.Y - b.Y, a.Z - b.Z); |
| 1 | 136 | | } |
| | 137 | |
|
| | 138 | | public static Point3 operator *(Point3 a, float scalar) |
| 1 | 139 | | { |
| 1 | 140 | | return new Point3(a.X * scalar, a.Y * scalar, a.Z * scalar); |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | public static Point3 operator /(Point3 a, float scalar) |
| 2 | 144 | | { |
| 2 | 145 | | if (System.Math.Abs(scalar) <= float.Epsilon) |
| 1 | 146 | | scalar = scalar >= 0 ? float.Epsilon : -float.Epsilon; |
| | 147 | |
|
| 2 | 148 | | var rez = 1f / scalar; |
| 2 | 149 | | return new Point3(a.X * rez, a.Y * rez, a.Z * rez); |
| 2 | 150 | | } |
| | 151 | |
|
| | 152 | | /// <summary> |
| | 153 | | /// Calculates the distance between two points. |
| | 154 | | /// </summary> |
| | 155 | | /// <param name="point1">The point1.</param> |
| | 156 | | /// <param name="point2">The point2.</param> |
| | 157 | | /// <returns>The distance between two points.</returns> |
| | 158 | | public static float Distance(Point3 point1, Point3 point2) |
| 1 | 159 | | { |
| 1 | 160 | | var lenX = point2.X - point1.X; |
| 1 | 161 | | var lenY = point2.Y - point1.Y; |
| 1 | 162 | | var lenZ = point2.Z - point1.Z; |
| | 163 | |
|
| 1 | 164 | | return (float)System.Math.Sqrt((lenX * lenX) + (lenY * lenY) + (lenZ * lenZ)); |
| 1 | 165 | | } |
| | 166 | |
|
| | 167 | | public static float Squared2DDistance(Point3 point1, Point3 point2) |
| 655 | 168 | | { |
| 655 | 169 | | var lenX = point2.X - point1.X; |
| 655 | 170 | | var lenY = point2.Y - point1.Y; |
| | 171 | |
|
| 655 | 172 | | return System.Math.Abs(lenX) + System.Math.Abs(lenY); |
| 655 | 173 | | } |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Compares [this] instance with the input. |
| | 177 | | /// </summary> |
| | 178 | | /// <param name="base3">The base3 to compare with.</param> |
| | 179 | | /// <returns> |
| | 180 | | /// <c>true</c> if the values of the input and [this] instance are equal. |
| | 181 | | /// </returns> |
| | 182 | | public bool Equals(IBase3 base3) |
| 21 | 183 | | { |
| 21 | 184 | | return base3 != null && |
| 21 | 185 | | Equals(X, base3.X) && |
| 21 | 186 | | Equals(Y, base3.Y) && |
| 21 | 187 | | Equals(Z, base3.Z); |
| 21 | 188 | | } |
| | 189 | |
|
| | 190 | | /// <summary> |
| | 191 | | /// Returns a <see cref="string" /> that represents [this] instance. |
| | 192 | | /// </summary> |
| | 193 | | /// <returns> |
| | 194 | | /// A <see cref="string" /> that represents [this] instance. |
| | 195 | | /// </returns> |
| 4 | 196 | | public override string ToString() => IsValid ? $"X: {X}; Y: {Y}; Z: {Z}" : "invalid"; |
| | 197 | | } |
| | 198 | | } |