Tanoda
Sphere.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using Leap.Unity.Infix;
12using UnityEngine;
13
14namespace Leap.Unity.Geometry {
15
16 using UnityRect = UnityEngine.Rect;
17
18 [System.Serializable]
19 public struct Sphere {
20
21 public Transform transform;
22 public Vector3 center;
23 public float radius;
24 public Matrix4x4? overrideMatrix;
25
26 #region Constructors
27
28 public Sphere(LocalSphere localSphere, Transform withTransform)
29 : this(localSphere.center, localSphere.radius, withTransform) { }
30
31 public Sphere(float radius = 0.10f, Component transformSource = null)
32 : this(default(Vector3), radius, transformSource) { }
33
34 public Sphere(float radius = 0.10f)
35 : this(default(Vector3), radius, null) { }
36
37 public Sphere(float radius = 0.10f, Matrix4x4? overrideMatrix = null)
38 : this(default(Vector3), radius, null, overrideMatrix) { }
39
40 public Sphere(Vector3 center = default(Vector3), float radius = 0.10f) :
41 this(center, radius, (Component)null) { }
42
43 public Sphere(Vector3 center = default(Vector3), float radius = 0.10f,
44 Component transformSource = null, Matrix4x4? overrideMatrix = null)
45 {
46 this.transform = (transformSource == null ? null : transformSource.transform);
47 this.center = center;
48 this.radius = radius;
49 this.overrideMatrix = overrideMatrix;
50 }
51
52 public Sphere(Vector3 center = default(Vector3), float radius = 0.10f,
53 Transform transform = null) : this(center, radius, (Component)transform) { }
54
55 public Sphere(Sphere other) : this(other.center, other.radius, other.transform) { }
56
57 #endregion
58
59 #region Accessors
60
64 public Matrix4x4 matrix {
65 get {
66 if (overrideMatrix != null) {
67 return overrideMatrix.Value * Matrix4x4.Translate(center);
68 }
69 if (transform == null) {
70 return Matrix4x4.Translate(center);
71 }
72 return transform.localToWorldMatrix * Matrix4x4.Translate(center);
73 }
74 }
75
81 public Vector3 position {
82 get {
83 return this.matrix.MultiplyPoint3x4(Vector3.zero);
84 }
85 }
86
87 #endregion
88
89 #region Chain Calls
90
91 public Sphere WithCenter(Vector3 center) {
92 var copy = new Sphere(this); copy.center = center; return copy;
93 }
94
95 #endregion
96
97 #region Collision
98
99 public bool Overlaps(Box box) {
100 return Collision.DoesOverlap(this, box);
101 }
102
107 public float DistanceTo(Rect rect) {
108 return Collision.DistanceBetween(this, rect);
109 }
110
111 public bool Overlaps(Rect rect) {
112 return Collision.DoesOverlap(this, rect);
113 }
114
115 #endregion
116
117 #region Debug Rendering
118
119 public void DrawLines(Action<Vector3, Vector3> lineDrawingFunc,
120 int latitudinalDivisions = 5,
121 int longitudinalDivisions = 5,
122 int numCircleSegments = 7,
123 Matrix4x4? matrixOverride = null) {
124 Matrix4x4 m = Matrix4x4.identity;
125 if (transform != null) {
126 m = transform.localToWorldMatrix;
127 }
128 if (matrixOverride.HasValue) {
129 m = matrixOverride.Value;
130 }
131
132 // Vector3 center = m.MultiplyPoint3x4(this.center);
133 // float radius = m.MultiplyPoint3x4(Vector3.right).magnitude * this.radius;
134 // Vector3 x = m.MultiplyVector(Vector3.right);
135 // Vector3 y = m.MultiplyVector(Vector3.up);
136 //Vector3 z = m.MultiplyVector(Vector3.forward); // unused
137 var center = this.center;
138 var radius = this.radius;
139 var x = Vector3.right;
140 var y = Vector3.up;
141
142 // Wire lat-long sphere
143 int latDiv = latitudinalDivisions;
144 float latAngle = 180f / latDiv; float accumLatAngle = 0f;
145 int lonDiv = longitudinalDivisions;
146 float lonAngle = 180f / lonDiv;
147 Quaternion lonRot = Quaternion.AngleAxis(lonAngle, y);
148 Vector3 lonNormal = x;
149 for (int i = 0; i < latDiv; i++) {
150 accumLatAngle += latAngle;
152 center: center + y * Mathf.Cos(accumLatAngle * Mathf.Deg2Rad) * radius,
153 normal: y,
154 radius: Mathf.Sin(accumLatAngle * Mathf.Deg2Rad) * radius,
155 numCircleSegments: numCircleSegments,
156 lineDrawingFunc: lineDrawingFunc,
157 fractionOfCircleToDraw: 1.0f,
158 radialStartDirection: x,
159 matrix: m
160 );
161 }
162 for (int i = 0; i < lonDiv; i++) {
164 center: center,
165 normal: lonNormal,
166 radius: radius,
167 numCircleSegments: numCircleSegments,
168 lineDrawingFunc: lineDrawingFunc,
169 fractionOfCircleToDraw: 1.0f,
170 radialStartDirection: y,
171 matrix: m
172 );
173 lonNormal = lonRot * lonNormal;
174 }
175 }
176
178 Matrix4x4 m = Matrix4x4.identity;
179 if (transform != null) {
180 m = transform.localToWorldMatrix;
181 }
182
183 var origDrawerColor = drawer.color;
184
185 Vector3 center = m.MultiplyPoint3x4(this.center);
186 float radius = m.MultiplyPoint3x4(Vector3.right).magnitude * this.radius;
187 Vector3 x = m.MultiplyVector(Vector3.right);
188 Vector3 y = m.MultiplyVector(Vector3.up);
189 //Vector3 z = m.MultiplyVector(Vector3.forward); // unused
190
191 // Sphere
192 drawer.color = drawer.color.WithAlpha(origDrawerColor.a * 0.05f);
193 drawer.DrawSphere(center, radius);
194
195 // Wire lat-long sphere
196 drawer.color = drawer.color.WithAlpha(origDrawerColor.a * 0.2f);
197 int latDiv = 6;
198 float latAngle = 180f / latDiv; float accumLatAngle = 0f;
199 int lonDiv = 6;
200 float lonAngle = 180f / lonDiv;
201 Quaternion lonRot = Quaternion.AngleAxis(lonAngle, y);
202 Vector3 lonNormal = x;
203 for (int i = 0; i < latDiv; i++) {
204 accumLatAngle += latAngle;
205 drawer.DrawWireArc(center: center + y * Mathf.Cos(accumLatAngle * Mathf.Deg2Rad) * radius,
206 normal: y,
207 radialStartDirection: x,
208 radius: Mathf.Sin(accumLatAngle * Mathf.Deg2Rad) * radius,
209 fractionOfCircleToDraw: 1.0f,
210 numCircleSegments: 22);
211 }
212 for (int i = 0; i < latDiv; i++) {
213 drawer.DrawWireArc(center: center,
214 normal: lonNormal,
215 radialStartDirection: y,
216 radius: radius,
217 fractionOfCircleToDraw: 1.0f,
218 numCircleSegments: 22);
219 lonNormal = lonRot * lonNormal;
220 }
221
222 drawer.color = origDrawerColor;
223 }
224
225 #endregion
226
227 }
228
229 public static class SphereExtensions {
230
234 public static Sphere ToSphere(this Vector3 vec3, float radius) {
235 return new Sphere(vec3, radius);
236 }
237
238 }
239
240}
UnityEngine.Component Component
void DrawWireArc(Vector3 center, Vector3 normal, Vector3 radialStartDirection, float radius, float fractionOfCircleToDraw, int numCircleSegments=16)
Color color
Sets or gets the color for the gizmos that will be drawn next.
void DrawSphere(Vector3 center, float radius)
Draws a filled gizmo sphere at the given position with the given radius.
UnityEngine.Rect UnityRect
Definition: LeapGrid.cs:14
static void DrawWireArc(Vector3 center, Vector3 normal, float radius, int numCircleSegments, Action< Vector3, Vector3 > lineDrawingFunc, float fractionOfCircleToDraw=1.0f, Matrix4x4? matrix=null, Vector3? radialStartDirection=null)
void DrawRuntimeGizmos(RuntimeGizmoDrawer drawer)
Definition: Sphere.cs:177
bool Overlaps(Box box)
Definition: Sphere.cs:99
Sphere(Vector3 center=default(Vector3), float radius=0.10f, Component transformSource=null, Matrix4x4? overrideMatrix=null)
Definition: Sphere.cs:43
Matrix4x4? overrideMatrix
Definition: Sphere.cs:24
Matrix4x4 matrix
Local-to-world matrix for this Sphere.
Definition: Sphere.cs:64
Sphere(float radius=0.10f, Component transformSource=null)
Definition: Sphere.cs:31
Sphere(Vector3 center=default(Vector3), float radius=0.10f, Transform transform=null)
Definition: Sphere.cs:52
Vector3 position
The world position of the center of this sphere (read only). This is dependent on the state of its Tr...
Definition: Sphere.cs:81
Sphere(float radius=0.10f, Matrix4x4? overrideMatrix=null)
Definition: Sphere.cs:37
Sphere WithCenter(Vector3 center)
Definition: Sphere.cs:91
Sphere(float radius=0.10f)
Definition: Sphere.cs:34
Sphere(LocalSphere localSphere, Transform withTransform)
Definition: Sphere.cs:28
void DrawLines(Action< Vector3, Vector3 > lineDrawingFunc, int latitudinalDivisions=5, int longitudinalDivisions=5, int numCircleSegments=7, Matrix4x4? matrixOverride=null)
Definition: Sphere.cs:119
Sphere(Vector3 center=default(Vector3), float radius=0.10f)
Definition: Sphere.cs:40
float DistanceTo(Rect rect)
Returns the distance between the closest points on the Rect and the Sphere, or 0 if the two overlap.
Definition: Sphere.cs:107
bool Overlaps(Rect rect)
Definition: Sphere.cs:111
Sphere(Sphere other)
Definition: Sphere.cs:55