-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSofaCustomMeshAPI.cs
More file actions
140 lines (106 loc) · 5.58 KB
/
Copy pathSofaCustomMeshAPI.cs
File metadata and controls
140 lines (106 loc) · 5.58 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using UnityEngine;
using System;
using System.Runtime.InteropServices;
namespace SofaUnityAPI
{
/// <summary>
/// Class of a SofaCustomMesh. This is the base class that will create a physical node on the SofaAdvancePhysicsAPI.
/// It will consist into a MechanicalObject and a collisionSphereModel
/// It will connect to the SofaPhysicsAPI.
/// </summary>
public class SofaCustomMeshAPI : SofaBaseObjectAPI
{
/// <summary> Default constructor, will call impl method: @see createObject() </summary>
/// <param name="simu">Pointer to the SofaPhysicsAPI</param>
/// <param name="nameID">Name of this Object</param>
public SofaCustomMeshAPI(IntPtr simu, string parentName, string nameID)
: base(simu, nameID, parentName, true)
{
}
protected string m_dofName = "";
protected string m_collisionName = "";
/// Implicit method to really create object and link to Sofa object. Called by SofaBaseObject constructor
protected override bool createObject()
{
if (m_hasObject == false) // first time create object only
{
// Create a Node in Sofa simulation tree and add a mechanicalObject into it
int res = sofaPhysicsAPI_addSphereCollisionsObject(m_simu, m_name, m_parentName);
m_name += "_node";
if (res != 0)
{
Debug.LogError("SofaCustomMesh::createObject creation method return error: " + SofaDefines.msg_error[res] + " for object " + m_name);
return false;
}
m_hasObject = true;
return true;
}
return false;
}
public void SetMeshNameID(string _name)
{
m_dofName = _name;
}
public void SetCollisionNameID(string _name)
{
m_collisionName = _name;
}
/// <summary> Method to set the number of vertices to this 3D Object. </summary>
/// <param name="nbr"> Number of vertices </param>
public void SetNumberOfVertices(int nbr)
{
if (!m_isCreated)
return;
int res = sofaMeshAPI_setNbVertices(m_simu, m_dofName, nbr);
if (res < 0)
Debug.LogError("mechanicalObject size: " + m_dofName + " " + SofaDefines.msg_error[res]);
else
sofaComponentAPI_reinitComponent(m_simu, m_dofName);
}
/// <summary> Method to update the mesh position from unity position.</summary>
/// <param name="trans"> Local GameObject Transform to get Unity world position.</param>
/// <param name="vertices"> List of vertices from Unity GameObject.</param>
/// <param name="scaleUnityToSofa"> scale to transform Unity to Sofa positions.</param>
public void UpdateMesh(Transform trans, Vector3[] vertices, Transform sofaCTransform)
{
if (!m_isCreated)
return;
int nbrV = vertices.Length;
float[] val = new float[(nbrV) * 3];
for (int i = 0; i < nbrV; i++)
{
Vector3 vert = trans.TransformPoint(vertices[i]);
Vector3 vertS = sofaCTransform.InverseTransformPoint(vert);
val[i * 3] = -vertS.x; // left to right coordinate system conversion
val[i * 3 + 1] = vertS.y;
val[i * 3 + 2] = vertS.z;
}
int resUpdate = sofaMeshAPI_setVertices(m_simu, m_dofName, val);
if (resUpdate < 0)
Debug.LogError("SofaCustomMesh updateMesh: " + m_dofName + " return error: " + SofaDefines.msg_error[resUpdate]);
}
public void SetFloatValue(string dataName, float value)
{
if (!m_isCreated)
return;
int res = sofaComponentAPI_setDoubleValue(m_simu, m_collisionName, dataName, (double)value);
if (res != 0)
Debug.LogError("Method setFloatValue of Data: " + dataName + " of object: " + m_collisionName + " returns error: " + SofaDefines.msg_error[res]);
else
sofaComponentAPI_reinitComponent(m_simu, m_collisionName);
}
/////////////////////////////////////////////////////////////////////////////////////////
//////////// Communication API to sofaPhysicsAdvanceAPI ////////////////
/////////////////////////////////////////////////////////////////////////////////////////
[DllImport("SofaVerseAPI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int sofaPhysicsAPI_addSphereCollisionsObject(IntPtr obj, string name, string parentNodeName);
[DllImport("SofaVerseAPI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int sofaMeshAPI_setNbVertices(IntPtr obj, string name, int nbrV);
[DllImport("SofaVerseAPI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int sofaMeshAPI_setVertices(IntPtr obj, string name, float[] arr);
[DllImport("SofaVerseAPI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int sofaComponentAPI_setDoubleValue(IntPtr obj, string componentName, string dataName, double value);
[DllImport("SofaVerseAPI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int sofaComponentAPI_reinitComponent(IntPtr obj, string componentName);
}
}