Skip to content

Commit 4787847

Browse files
[src] Add BeamAdapterTracker script. First draft: working but not optimized at all (#259)
* [src] Add BeamAdapterTracker script. First draft: working but not optimized at all * generalised script, update scene with it --------- Co-authored-by: PierreFonda3D <pierre.fonda@infinytech3d.com>
1 parent f971e23 commit 4787847

4 files changed

Lines changed: 5705 additions & 4509 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using SofaUnity;
5+
6+
namespace SofaUnity
7+
{
8+
[System.Serializable]
9+
public class BeamToolEntry
10+
{
11+
public SofaComponent m_tool = null;
12+
public GameObject m_slave = null;
13+
14+
// Runtime (not shown in inspector)
15+
[HideInInspector] public SofaIntData d_tool_id = null;
16+
public Vector3 m_position = Vector3.zero;
17+
}
18+
19+
public class BeamAdapterTracker : MonoBehaviour
20+
{
21+
/// Pointer to the unique sofa mesh
22+
public SofaMesh m_sofaMesh = null;
23+
24+
[SerializeField]
25+
public List<BeamToolEntry> m_tools = new List<BeamToolEntry>();
26+
27+
private bool m_isReady = false;
28+
private int m_nbrDofs = 0;
29+
30+
void Start()
31+
{
32+
// Connect wireTipIndex for each tool
33+
foreach (BeamToolEntry entry in m_tools)
34+
{
35+
if (entry.m_tool != null)
36+
{
37+
Debug.Log("BeamAdapterTracker: Tool found. Connecting wireTipIndex");
38+
entry.d_tool_id = (SofaIntData)entry.m_tool.m_dataArchiver.GetSofaIntData("wireTipIndex");
39+
}
40+
}
41+
42+
if (m_sofaMesh == null)
43+
{
44+
Debug.LogError("BeamAdapterTracker: SofaMesh not set. Can't track the wire tip positions.");
45+
m_isReady = false;
46+
}
47+
else
48+
{
49+
m_sofaMesh.AddListener(); // TODO m_sofaMesh.RemoveListener(); when exit?
50+
m_nbrDofs = m_sofaMesh.NbVertices();
51+
m_isReady = true;
52+
}
53+
}
54+
55+
// TODO for later: track orientation in addition to the position.
56+
private void Update()
57+
{
58+
if (!m_isReady) return;
59+
60+
foreach (BeamToolEntry entry in m_tools)
61+
{
62+
if (entry.d_tool_id == null) continue;
63+
64+
int id = entry.d_tool_id.Value;
65+
if (id >= 0 && id < m_nbrDofs)
66+
{
67+
// TODO: check why we need to do a transform point. The position should be world position already no?
68+
entry.m_position = m_sofaMesh.GetPosition(id);
69+
if (entry.m_slave != null)
70+
entry.m_slave.transform.position = m_sofaMesh.m_sofaContext.transform.TransformPoint(entry.m_position);
71+
}
72+
}
73+
}
74+
}
75+
} // namespace SofaUnity

Core/Scripts/Modules/Tools/BeamAdapterTracker.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)