forked from Dan-Piker/K2Goals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollidePointMesh.cs
68 lines (57 loc) · 1.94 KB
/
CollidePointMesh.cs
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
using System;
using System.Collections.Generic;
using Rhino.Geometry;
namespace KangarooSolver.Goals
{
/// <summary>
/// 2 way collision between points and mesh
/// </summary>
public class CollidePointMesh : GoalObject
{
public double Strength;
public bool Inside;
public Mesh M;
public CollidePointMesh(Point3d[] P, Mesh m, bool inside, double K)
{
var mPoints = m.Vertices.ToPoint3dArray();
var size = P.Length + mPoints.Length;
PPos = new Point3d[size];
Move = new Vector3d[size];
Weighting = new double[size];
for (int i = 0; i < mPoints.Length; i++)
{
PPos[i] = mPoints[i];
Weighting[i] = K;
}
for (int i = mPoints.Length; i < PPos.Length; i++)
{
PPos[i] = P[i - mPoints.Length];
Weighting[i] = K;
}
M = m;
Inside = inside;
}
public override void Calculate(List<KangarooSolver.Particle> p)
{
for (int i = 0; i < M.Vertices.Count; i++)
{
M.Vertices.SetVertex(i, p[PIndex[i]].Position);
Move[i] = Vector3d.Zero;
}
for (int i = M.Vertices.Count; i < PIndex.Length; i++)
{
Point3d ThisPt = p[PIndex[i]].Position;
if (M.IsPointInside(ThisPt, 0.01, true)!=Inside)
{
var MP = M.ClosestMeshPoint(ThisPt, 1000);
var Push = 0.5 * (MP.Point - ThisPt);
Move[i] = Push;
Move[M.Faces[MP.FaceIndex].A] -= Push * MP.T[0];
Move[M.Faces[MP.FaceIndex].B] -= Push * MP.T[1];
Move[M.Faces[MP.FaceIndex].C] -= Push * MP.T[2];
}
else { Move[i] = Vector3d.Zero; }
}
}
}
}