Tanoda
TransformCatmullRomSpline.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
11using System.Collections;
12using System.Collections.Generic;
13using UnityEngine;
14
15namespace Leap.Unity.Splines {
16
17 public class TransformCatmullRomSpline : MonoBehaviour, IRuntimeGizmoComponent {
18
19 private const int RESOLUTION = 12;
20
21 public Transform A;
22 public Transform B;
23 public Transform C;
24 public Transform D;
25
26 public GameObject poseEvaluationObj = null;
27 private GameObject[] _evalObjCopies = new GameObject[RESOLUTION + 1];
28
29 public bool fullPoseSpline = false;
30
31 public Color color = Color.white;
32
33 private HermiteSpline3? _spline = null;
34 private HermiteQuaternionSpline? _qSpline = null;
35
36 void Update() {
37 if (!fullPoseSpline) {
38 Vector3 a = A.position, b = B.position, c = C.position, d = D.position;
39 _spline = CatmullRom.ToCHS(a, b, c, d);
40 }
41 else {
42 Pose a = A.ToPose(), b = B.ToPose(), c = C.ToPose(), d = D.ToPose();
43 _spline = CatmullRom.ToCHS(a.position, b.position, c.position, d.position);
44 _qSpline = CatmullRom.ToQuaternionCHS(a.rotation, b.rotation,
45 c.rotation, d.rotation);
46
47 if (poseEvaluationObj != null) {
48 float incr = 1f / RESOLUTION;
49 var t = 0f;
50 _evalObjCopies[0] = poseEvaluationObj;
51 for (int i = 0; i <= RESOLUTION; i++) {
52 var obj = _evalObjCopies[i];
53
54 if (obj == null) {
55 obj = Instantiate(poseEvaluationObj);
56 obj.transform.parent = poseEvaluationObj.transform.parent;
57 _evalObjCopies[i] = obj;
58 }
59
60 obj.transform.position = _spline.Value.PositionAt(t);
61 obj.transform.rotation = _qSpline.Value.RotationAt(t);
62
63 t += incr;
64 }
65 }
66 }
67 }
68
70 drawer.color = color;
71
72 if (!_spline.HasValue || (fullPoseSpline && !_qSpline.HasValue)) return;
73
74 int resolution = 16;
75 float incr = 1f / resolution;
76 Vector3? lastPos = null;
77 for (float t = 0; t <= 1f; t += incr) {
78 var pos = _spline.Value.PositionAt(t);
79 if (fullPoseSpline) {
80 var rot = _qSpline.Value.RotationAt(t);
81
82 drawer.DrawPose(new Pose(pos, rot), 0.01f);
83 }
84
85 if (lastPos.HasValue) {
86 drawer.DrawLine(lastPos.Value, pos);
87 }
88
89 lastPos = pos;
90 }
91 }
92
93 }
94
95}
UnityEngine.Color Color
Definition: TestScript.cs:32
void DrawLine(Vector3 a, Vector3 b)
Draws a gizmo line that connects the two positions.
Color color
Sets or gets the color for the gizmos that will be drawn next.
Have your MonoBehaviour implement this interface to be able to draw runtime gizmos....
Represents a spline for the rotation of a rigid body from one orientation in space to another over a ...
Quaternion RotationAt(float t)
Gets the rotation at time t along this spline. The time is clamped within the t0 - t1 range.
Represents a spline that travels from one point in space to another over a specified time frame....
Vector3 PositionAt(float t)
Gets the position at time t along this spline. The time is clamped to t0, but can extrapolate beyon...
A position and rotation. You can multiply two poses; this acts like Matrix4x4 multiplication,...
Definition: Pose.cs:21
Quaternion rotation
Definition: Pose.cs:24
Vector3 position
Definition: Pose.cs:23