forked from Dan-Piker/K2Goals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClampLength.cs
69 lines (64 loc) · 2.05 KB
/
ClampLength.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
69
using System.Collections.Generic;
using Rhino.Geometry;
namespace KangarooSolver.Goals
{
/// <summary>
/// Keep the length of a line between some upper and lower bounds
/// When it is between these lengths no force is applied
/// </summary>
public class ClampLength : GoalObject
{
public double Upper;
public double Lower;
public double Stiffness;
public ClampLength()
{
}
public ClampLength(int S, int E, double U, double L, double k)
{
PIndex = new int[2] { S, E };
Move = new Vector3d[2];
Weighting = new double[2];
Upper = U;
Lower = L;
Stiffness = k;
}
public ClampLength(Point3d S, Point3d E, double U, double L, double k)
{
PPos = new Point3d[2] { S, E };
Move = new Vector3d[2];
Weighting = new double[2]{k,k};
Upper = U;
Lower = L;
Stiffness = k;
}
public override void Calculate(List<KangarooSolver.Particle> p)
{
Vector3d current = p[PIndex[1]].Position - p[PIndex[0]].Position;
double LengthNow = current.Length;
if (LengthNow > Upper)
{
double stretchfactor = 1.0 - Upper / LengthNow;
Vector3d SpringMove = 0.5 * current * stretchfactor;
Move[0] = SpringMove;
Move[1] = -SpringMove;
Weighting[0] = Stiffness;
Weighting[1] = Stiffness;
}
else if (LengthNow < Lower)
{
double stretchfactor = 1.0 - Lower / LengthNow;
Vector3d SpringMove = 0.5 * current * stretchfactor;
Move[0] = SpringMove;
Move[1] = -SpringMove;
Weighting[0] = Stiffness;
Weighting[1] = Stiffness;
}
else
{
Move[0] = Vector3d.Zero;
Move[1] = Vector3d.Zero;
}
}
}
}