-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSofaPlaneAPI.cs
More file actions
112 lines (87 loc) · 3.73 KB
/
Copy pathSofaPlaneAPI.cs
File metadata and controls
112 lines (87 loc) · 3.73 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
using UnityEngine;
using System;
using System.Runtime.InteropServices;
namespace SofaUnityAPI
{
/// <summary>
/// Class used to handle bindings to the Sofa Plane object, using a Regular Grid topology in 2D.
/// </summary>
public class SofaPlaneAPI : 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 SofaPlaneAPI(IntPtr simu, string nameID, string parentName, bool isRigid)
: base(simu, nameID, parentName, isRigid)
{
}
/// Destructor
~SofaPlaneAPI()
{
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 plane
int res = sofaPhysicsAPI_addPlane(m_simu, m_name, m_parentName, m_isRigid);
m_name += "_node";
if (res != 0)
{
Debug.LogError("SofaPlaneAPI::createObject plane creation method return error: " + SofaDefines.msg_error[res] + " for object " + m_name);
return false;
}
if (displayLog)
Debug.Log("plane Added! " + m_name);
// Set created object to native pointer
m_hasObject = true;
return true;
}
return false;
}
/// Post processing method to recompute topology if needed.
public override void recomputeTopology(Mesh mesh)
{
// recompute triangles to face up.
int[] triangles = mesh.triangles;
int nbrTri = triangles.Length / 3;
for (int i = 0; i < nbrTri; i++)
{
int buff = triangles[i * 3 + 1];
triangles[i * 3 + 1] = triangles[i * 3 + 2];
triangles[i * 3 + 2] = buff;
}
mesh.triangles = triangles;
// recompute normals to face up.
Vector3[] norms = mesh.normals;
for (int i = 0; i < norms.Length; i++)
norms[i] = new Vector3(0.0f, 1.0f, 0.0f);
mesh.normals = norms;
}
/// 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);
// assume plane has normal on the Y value. To be done more generic in the future.
float rangeX = 1 / (m_max.x - m_min.x);
float rangeZ = 1 / (m_max.z - m_min.z);
for (int i = 0; i < verts.Length; i++)
{
uvs[i] = new Vector2((m_max.x - verts[i].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_addPlane(IntPtr obj, string name, string parentNodeName, bool isRigid);
}
}