-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBeamAdapterTracker.cs
More file actions
75 lines (65 loc) · 2.4 KB
/
Copy pathBeamAdapterTracker.cs
File metadata and controls
75 lines (65 loc) · 2.4 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SofaUnity;
namespace SofaUnity
{
[System.Serializable]
public class BeamToolEntry
{
public SofaComponent m_tool = null;
public GameObject m_slave = null;
// Runtime (not shown in inspector)
[HideInInspector] public SofaIntData d_tool_id = null;
public Vector3 m_position = Vector3.zero;
}
public class BeamAdapterTracker : MonoBehaviour
{
/// Pointer to the unique sofa mesh
public SofaMesh m_sofaMesh = null;
[SerializeField]
public List<BeamToolEntry> m_tools = new List<BeamToolEntry>();
private bool m_isReady = false;
private int m_nbrDofs = 0;
void Start()
{
// Connect wireTipIndex for each tool
foreach (BeamToolEntry entry in m_tools)
{
if (entry.m_tool != null)
{
Debug.Log("BeamAdapterTracker: Tool found. Connecting wireTipIndex");
entry.d_tool_id = (SofaIntData)entry.m_tool.m_dataArchiver.GetSofaIntData("wireTipIndex");
}
}
if (m_sofaMesh == null)
{
Debug.LogError("BeamAdapterTracker: SofaMesh not set. Can't track the wire tip positions.");
m_isReady = false;
}
else
{
m_sofaMesh.AddListener(); // TODO m_sofaMesh.RemoveListener(); when exit?
m_nbrDofs = m_sofaMesh.NbVertices();
m_isReady = true;
}
}
// TODO for later: track orientation in addition to the position.
private void Update()
{
if (!m_isReady) return;
foreach (BeamToolEntry entry in m_tools)
{
if (entry.d_tool_id == null) continue;
int id = entry.d_tool_id.Value;
if (id >= 0 && id < m_nbrDofs)
{
// TODO: check why we need to do a transform point. The position should be world position already no?
entry.m_position = m_sofaMesh.GetPosition(id);
if (entry.m_slave != null)
entry.m_slave.transform.position = m_sofaMesh.m_sofaContext.transform.TransformPoint(entry.m_position);
}
}
}
}
} // namespace SofaUnity