-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSofaBoxAPI.cs
More file actions
232 lines (188 loc) · 8.27 KB
/
SofaBoxAPI.cs
File metadata and controls
232 lines (188 loc) · 8.27 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using UnityEngine;
using System;
using System.Runtime.InteropServices;
namespace SofaUnityAPI
{
/// <summary>
/// Class used to handle bindings to the Sofa Cube object, using a RegularGrid.
/// </summary>
public class SofaBoxAPI : SofaBaseObjectAPI
{
/// <summary>
/// Default constructor
/// </summary>
/// <param name="simu">Pointer to the SofaPhysicsAPI</param>
/// <param name="nameID">Name of this Object</param>
/// <param name="isRigid">Type rigid or deformable</param>
public SofaBoxAPI(IntPtr simu, string nameID, string parentName, bool isRigid)
: base(simu, nameID, parentName, isRigid)
{
}
/// Destructor
~SofaBoxAPI()
{
Dispose(false);
}
/// 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 the cube
int res = sofaPhysicsAPI_addCube(m_simu, m_name, m_parentName, m_isRigid);
m_name += "_node";
if (res != 0)
{
Debug.LogError("SofaBoxAPI::createObject cube creation method return error: " + SofaDefines.msg_error[res] + " for object " + m_name);
return false;
}
if (displayLog)
Debug.Log("cube Added! " + m_name);
m_hasObject = true;
return true;
}
return false;
}
/// Method to create the triangulation from Sofa topology to Unity buffers
public override int[] createTriangulation()
{
//if (!m_hasObject)
//{
return new int[0];
//}
//int nbrQuads = sofaPhysics3DObject_getNbQuads(m_simu, m_name);
//if (nbrQuads < 0)
//{
// Debug.LogError("createTriangulation failed, method returns: " + SofaDefines.msg_error[nbrQuads]);
// return new int[0];
//}
////Debug.Log("NbrQuad: " + nbrQuads);
//int nbrIndices = nbrQuads * 4;
//int[] quads = new int[nbrIndices];
//sofaPhysics3DObject_getQuads(m_simu, m_name, quads);
//int resX = 5;
//int resY = 5;
//int resZ = 5;
//int nbrTri = (resX - 1) * (resY - 1) * 4 + (resX - 1) * (resZ - 1) * 4 + (resY - 1) * (resZ - 1) * 4;
////int nbrTri = 2;
//int[] tris = new int[nbrTri*3];
////Debug.Log("nbrTri: " + (resX - 1) * (resY - 1) * 2 * 3);
//int cpt = 0;
//for (int k = 0; k < 2; ++k)
//{
// int face = k * resX * resY * (resZ - 1);
// for (int j = 0; j < resY - 1; ++j)
// {
// int lvl = face + j * resX;
// for (int i = 0; i < resX - 1; ++i)
// {
// int id1 = cpt * 6 + 1;
// int id2 = cpt * 6 + 2;
// if (k != 0)
// {
// id1 = cpt * 6 + 2;
// id2 = cpt * 6 + 1;
// }
// tris[cpt * 6] = lvl + i;
// tris[id1] = lvl + i + resX;
// tris[id2] = lvl + i + resX + 1;
// id1 += 3;
// id2 += 3;
// tris[cpt * 6 + 3] = lvl + i;
// tris[id1] = lvl + i + resX + 1;
// tris[id2] = lvl + i + 1;
// cpt++;
// }
// }
//}
//for (int i = 0; i < 2; ++i)
//{
// int face = i * (resX-1);
// for (int k = 0; k < resZ - 1; ++k)
// {
// int lvl1 = face + k * resX * resY;
// int lvl2 = face + (k + 1) * resX * resY;
// for (int j = 0; j < resY - 1; ++j)
// {
// int id1 = cpt * 6 + 1;
// int id2 = cpt * 6 + 2;
// if (i != 0)
// {
// id1 = cpt * 6 + 2;
// id2 = cpt * 6 + 1;
// }
// tris[cpt * 6] = lvl2 + j * resX;
// tris[id1] = lvl2 + (j + 1) * resX;
// tris[id2] = lvl1 + (j + 1) * resX;
// id1 += 3;
// id2 += 3;
// tris[cpt * 6 + 3] = lvl2 + j * resX;
// tris[id1] = lvl1 + (j + 1) * resX;
// tris[id2] = lvl1 + j * resX;
// cpt++;
// }
// }
//}
//for (int j = 0; j < 2; ++j)
//{
// int face = j * resX * (resY - 1);
// for (int k = 0; k < resZ - 1; ++k)
// {
// int lvl1 = face + k * resX * resY;
// int lvl2 = face + (k + 1) * resX * resY;
// for (int i = 0; i < resX - 1; ++i)
// {
// int id1 = cpt * 6 + 1;
// int id2 = cpt * 6 + 2;
// if (j != 0)
// {
// id1 = cpt * 6 + 2;
// id2 = cpt * 6 + 1;
// }
// tris[cpt * 6] = lvl2 + i;
// tris[id1] = lvl1 + i;
// tris[id2] = lvl1 + (i + 1);
// id1 += 3;
// id2 += 3;
// tris[cpt * 6 + 3] = lvl2 + i;
// tris[id1] = lvl1 + (i + 1);
// tris[id2] = lvl2 + (i + 1);
// cpt++;
// }
// }
//}
//return tris;
}
/// Method to recompute the Tex coords according to mesh position and geometry.
public override void recomputeTexCoords(Mesh mesh)
{
//Vector3[] verts = mesh.vertices;
//Vector2[] uvs = new Vector2[verts.Length];
//this.computeBoundingBox(mesh);
//float rangeX = 1 / (m_max.x - m_min.x);
//float rangeY = 1 / (m_max.y - m_min.y);
//float rangeZ = 1 / (m_max.z - m_min.z);
//for (int i = 0; i < verts.Length; i++)
//{
// if (verts[i].z == m_max.z)
// uvs[i] = new Vector2((verts[i].x - m_min.x) * rangeX, (verts[i].y - m_min.y) * rangeY);
// else if (verts[i].z == m_min.z)
// uvs[i] = new Vector2((m_max.x - verts[i].x) * rangeX, (verts[i].y - m_min.y) * rangeY);
// else if (verts[i].x == m_max.x)
// uvs[i] = new Vector2((verts[i].z - m_min.z) * rangeZ, (verts[i].y - m_min.y) * rangeY);
// else if (verts[i].x == m_min.x)
// uvs[i] = new Vector2((m_max.z - verts[i].z) * rangeZ, (verts[i].y - m_min.y) * rangeY);
// else if (verts[i].y == m_max.y)
// uvs[i] = new Vector2((m_max.x - verts[i].x) * rangeX, (verts[i].z - m_min.z) * rangeZ);
// else if (verts[i].y == m_min.y)
// uvs[i] = new Vector2((verts[i].x - m_min.x) * rangeX, (verts[i].z - m_min.z) * rangeZ);
//}
//mesh.uv = uvs;
}
/////////////////////////////////////////////////////////////////////////////////////////
//////////// Communication API to sofaPhysicsAdvanceAPI ////////////////
/////////////////////////////////////////////////////////////////////////////////////////
[DllImport("SofaVerseAPI", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int sofaPhysicsAPI_addCube(IntPtr obj, string nodeName, string parentNodeName, bool isRigid);
}
}