Tanoda
SphericalSection.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 Leap.Unity.Infix;
10using UnityEngine;
11
12namespace Leap.Unity.Geometry {
13
14 public struct SphericalSection {
15
16 public float angle;
17 public float angleRad { get { return angle * Mathf.Deg2Rad; }}
18 public float radius;
19 public float minRadius;
20 public Transform transform;
21 public Matrix4x4? overrideMatrix;
22
23 public Matrix4x4 matrix { get {
24 if (overrideMatrix != null) {
25 return overrideMatrix.Value;
26 }
27 if (transform != null) {
28 return transform.WorldFromLocal();
29 }
30 return Matrix4x4.identity;
31 }}
32
33 public SphericalSection(float angle, float radius, float minRadius = 0,
34 Transform transform = null)
35 {
36 this.angle = angle; this.radius = radius; this.minRadius = minRadius;
37 this.transform = transform;
38 this.overrideMatrix = null;
39 }
40
46 public Vector3 SampleFromUnitCube(Vector3 unitCubePoint) {
47 var circlePoint = Swizzle.Swizzle.xy(unitCubePoint).Square2Circle();
48 var circlePointMag = circlePoint.magnitude;
49
50 var zFrac = unitCubePoint.z.Map(-1f, 1f, 0f, 1f);
51 var rForward = zFrac.Map(0f, 1f, minRadius, radius) * Vector3.forward;
52
53 var rot = Quaternion.identity;
54 var circleAngle = 0f;
55 if (circlePointMag > 0f) {
56 var circleDir = circlePoint / circlePointMag;
57 circleAngle = Vector3.up.SignedAngle(circleDir, -Vector3.forward);
58 }
59 rot = Quaternion.AngleAxis(circleAngle, -Vector3.forward) * rot;
60
61 var coneAngle = circlePointMag.Map(0f, 1f, 0f, angle / 2f);
62 rot = Quaternion.AngleAxis(coneAngle, rot.GetRight()) * rot;
63
64 return matrix.MultiplyPoint3x4(rot * rForward);
65 }
66
67 public void Draw(Drawer drawer, Color? color = null,
68 Matrix4x4? overrideMatrix = null)
69 {
70 var useMatrix = overrideMatrix ?? this.transform.WorldFromLocal();
71 var section = this;
72 section.overrideMatrix = useMatrix;
73
74 var unitBox = Box.unit;
75 var useColor = drawer.color = color ?? Color.white;
76 var alpha = useColor.a;
77 unitBox.DrawLines(divisions: 16, drawLineFunc: (a, b) => {
78 var a2 = section.SampleFromUnitCube(a);
79 var b2 = section.SampleFromUnitCube(b);
80 drawer.Line(a2, b2);
81 });
82
83 var rect = new Rect(center: Vector3.forward, radii: Vector3.one);
84 var prevR = 1f;
85 for (var r = 0.8f; r > 0f; r -= 0.15f) {
86 drawer.color = useColor.WithAlpha(r * r * alpha);
87 rect.radii = Vector3.one * r;
88 rect.DrawLines(divisions: 16, drawLineFunc: (a, b) => {
89 var a2 = section.SampleFromUnitCube(a);
90 var b2 = section.SampleFromUnitCube(b);
91 drawer.Line(a2, b2);
92 });
93 for (var n0 = -1; n0 <= 1; n0 += 1) {
94 for (var n1 = -1; n1 <= 1; n1 += 1) {
95 var s = new Vector3(n0 * prevR, n1 * prevR, 1f);
96 var t = new Vector3(n0 * r, n1 * r, 1f);
97 drawer.Line(section.SampleFromUnitCube(s),
98 section.SampleFromUnitCube(t));
99 }
100 }
101 prevR = r;
102 }
103 }
104
105 }
106
107}
UnityEngine.Color Color
Definition: TestScript.cs:32
Simple drawing interface abstraction (intended for debug drawing, not production!) with statically-ac...
Definition: Drawer.cs:19
void Line(Vector3 a, Vector3 b)
Definition: Drawer.cs:140
Color color
Calls the setColor delegate.
Definition: Drawer.cs:26
static Box unit
Definition: Box.cs:53
void Draw(Drawer drawer, Color? color=null, Matrix4x4? overrideMatrix=null)
Vector3 SampleFromUnitCube(Vector3 unitCubePoint)
Converts a sample point from the unit cube (XYZ [-1, 1]) to a sample point in the spherical section....
SphericalSection(float angle, float radius, float minRadius=0, Transform transform=null)