forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line3.cs
131 lines (108 loc) · 3.85 KB
/
Line3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Representation of a 3D line
/// </summary>
[Serializable]
public struct Line3 : IEquatable<Line3>, IFormattable
{
public Vector3 origin;
public Vector3 direction;
public static Line3 xAxis => new Line3(Vector3.zero, Vector3.right);
public static Line3 yAxis => new Line3(Vector3.zero, Vector3.up);
public static Line3 zAxis => new Line3(Vector3.zero, Vector3.forward);
public Line3(Ray ray)
{
origin = ray.origin;
direction = ray.direction;
}
public Line3(Vector3 origin, Vector3 direction)
{
this.origin = origin;
this.direction = direction;
}
/// <summary>
/// Returns a point at <paramref name="distance"/> units from origin along the line
/// </summary>
public Vector3 GetPoint(float distance)
{
return origin + direction*distance;
}
/// <summary>
/// Linearly interpolates between two lines
/// </summary>
public static Line3 Lerp(Line3 a, Line3 b, float t)
{
t = Mathf.Clamp01(t);
return new Line3(a.origin + (b.origin - a.origin)*t, a.direction + (b.direction - a.direction)*t);
}
/// <summary>
/// Linearly interpolates between two lines without clamping the interpolant
/// </summary>
public static Line3 LerpUnclamped(Line3 a, Line3 b, float t)
{
return new Line3(a.origin + (b.origin - a.origin)*t, a.direction + (b.direction - a.direction)*t);
}
#region Casting operators
public static explicit operator Line3(Ray ray)
{
return new Line3(ray);
}
public static explicit operator Ray(Line3 line)
{
return new Ray(line.origin, line.direction);
}
public static explicit operator Ray2D(Line3 line)
{
return new Ray2D((Vector2) line.origin, (Vector2) line.direction);
}
public static explicit operator Line2(Line3 line)
{
return new Line2((Vector2) line.origin, (Vector2) line.direction);
}
#endregion Casting operators
public static Line3 operator +(Line3 line, Vector3 vector)
{
return new Line3(line.origin + vector, line.direction);
}
public static Line3 operator -(Line3 line, Vector3 vector)
{
return new Line3(line.origin - vector, line.direction);
}
public static bool operator ==(Line3 a, Line3 b)
{
return a.origin == b.origin && a.direction == b.direction;
}
public static bool operator !=(Line3 a, Line3 b)
{
return !(a == b);
}
public override int GetHashCode()
{
return origin.GetHashCode() ^ (direction.GetHashCode() << 2);
}
public override bool Equals(object other)
{
return other is Line3 && Equals((Line3) other);
}
public bool Equals(Line3 other)
{
return origin.Equals(other.origin) && direction.Equals(other.direction);
}
public override string ToString()
{
return string.Format("Line3(origin: {0}, direction: {1})", origin, direction);
}
public string ToString(string format)
{
return string.Format("Line3(origin: {0}, direction: {1})", origin.ToString(format), direction.ToString(format));
}
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format("Line3(origin: {0}, direction: {1})", origin.ToString(format, formatProvider),
direction.ToString(format, formatProvider));
}
}
}