Skip to content
Snippets Groups Projects
Commit f70a78c8 authored by BlackAngle233's avatar BlackAngle233
Browse files

2021.5.10

parent af571a61
Branches
No related merge requests found
Showing
with 806 additions and 686 deletions
This diff is collapsed.
This diff is collapsed.
...@@ -35,40 +35,58 @@ public class Constraint ...@@ -35,40 +35,58 @@ public class Constraint
Object2 = obj2; Object2 = obj2;
constraintType = type; constraintType = type;
Line l1 = null;
Line l2 = null;
float length = 0;
switch (type) switch (type)
{ {
case ConstraintType.LineLength: case ConstraintType.LineLength:
Line l = (Line)obj1; l1 = (Line)obj1;
float length = (float)obj2; length = (float)obj2;
l.setConstraint(this); l1.setConstraint(this);
name = l.name + "=" + length.ToString(); name = l1.name + "=" + length.ToString();
break; break;
case ConstraintType.LineEqual: case ConstraintType.LineEqual:
Line l1 = (Line)obj1; l1 = (Line)obj1;
Line l2 = (Line)obj2; l2 = (Line)obj2;
l1.setConstraint(this); l1.setConstraint(this);
l2.setConstraint(this); l2.setConstraint(this);
name = l1.name + "=" + l2.name; name = l1.name + "=" + l2.name;
break; break;
case ConstraintType.LineNormal:
l1 = (Line)obj1;
l2 = (Line)obj2;
l1.setConstraint(this);
l2.setConstraint(this);
name = l1.name + "⊥" + l2.name;
break;
} }
} }
public float getConstraintValue() public float getConstraintValue()
{ {
float result = 0; float result = 0;
Line l1 = null;
Line l2 = null;
float length = 0;
switch (constraintType) switch (constraintType)
{ {
case ConstraintType.LineLength: case ConstraintType.LineLength:
Line l = (Line)Object1; l1 = (Line)Object1;
float length = (float)Object2; length = (float)Object2;
result = l.getLength() - length; result = l1.getLength() - length;
break; break;
case ConstraintType.LineEqual: case ConstraintType.LineEqual:
Line l1 = (Line)Object1; l1 = (Line)Object1;
Line l2 = (Line)Object2; l2 = (Line)Object2;
if (l1.lineState > l2.lineState) if (l1.lineState > l2.lineState)
{ {
...@@ -79,6 +97,12 @@ public class Constraint ...@@ -79,6 +97,12 @@ public class Constraint
result = l2.getLength() - l1.getLength(); result = l2.getLength() - l1.getLength();
} }
break; break;
case ConstraintType.LineNormal:
l1 = (Line)Object1;
l2 = (Line)Object2;
result = Vector3.Dot(l1.direction, l2.direction);
break;
} }
return result; return result;
...@@ -86,16 +110,21 @@ public class Constraint ...@@ -86,16 +110,21 @@ public class Constraint
public Gradient getGradient() public Gradient getGradient()
{ {
Line l1 = null;
Line l2 = null;
float length = 0;
switch (constraintType) switch (constraintType)
{ {
case ConstraintType.LineLength: case ConstraintType.LineLength:
Line l = (Line)Object1; l1 = (Line)Object1;
float length = (float)Object2; length = (float)Object2;
return l.getGradient(); return l1.getGradient();
case ConstraintType.LineEqual: case ConstraintType.LineEqual:
Line l1 = (Line)Object1; l1 = (Line)Object1;
Line l2 = (Line)Object2; l2 = (Line)Object2;
if(l1.lineState > l2.lineState) if(l1.lineState > l2.lineState)
{ {
return l1.getGradient(); return l1.getGradient();
...@@ -104,6 +133,17 @@ public class Constraint ...@@ -104,6 +133,17 @@ public class Constraint
{ {
return l2.getGradient(); return l2.getGradient();
} }
case ConstraintType.LineNormal:
l1 = (Line)Object1;
l2 = (Line)Object2;
if(l1.lineState > l2.lineState)
{
return new Gradient(l2.getUnknownPoint() ,l1.direction);
}
else
{
return new Gradient(l1.getUnknownPoint() ,l2.direction);
}
} }
return null; return null;
} }
...@@ -120,17 +160,23 @@ public class Constraint ...@@ -120,17 +160,23 @@ public class Constraint
public void checkState() public void checkState()
{ {
Line l1 = null;
Line l2 = null;
float length = 0;
switch (constraintType) switch (constraintType)
{ {
case ConstraintType.LineLength: case ConstraintType.LineLength:
Line l = (Line)Object1; l1 = (Line)Object1;
float length = (float)Object2; length = (float)Object2;
setState(l.lineState); setState(l1.lineState);
break; break;
case ConstraintType.LineEqual: case ConstraintType.LineEqual:
Line l1 = (Line)Object1; case ConstraintType.LineNormal:
Line l2 = (Line)Object2; l1 = (Line)Object1;
l2 = (Line)Object2;
if(l1.lineState == State.confirm && l2.lineState == State.confirm) if(l1.lineState == State.confirm && l2.lineState == State.confirm)
{ {
...@@ -150,25 +196,33 @@ public class Constraint ...@@ -150,25 +196,33 @@ public class Constraint
public void calculateConstraint(Point target) public void calculateConstraint(Point target)
{ {
Vector3 normal;
Vector3 direction; Vector3 direction;
Point tmp; Point tmp;
Line l1 = null;
Line l2 = null;
float length = 0;
float cos = 0;
float sin = 0;
switch (constraintType) switch (constraintType)
{ {
case ConstraintType.LineLength: case ConstraintType.LineLength:
Line l = (Line)Object1; l1 = (Line)Object1;
float length = (float)Object2; length = (float)Object2;
tmp = l.getAnotherPoint(target); tmp = l1.getAnotherPoint(target);
direction = l.getDirection(target); direction = l1.getDirection(target);
target.setPoint(tmp.add(direction * length)); target.setPoint(tmp.add(direction * length));
target.setState(State.confirm); target.setState(State.confirm);
break; break;
case ConstraintType.LineEqual: case ConstraintType.LineEqual:
Line l1 = (Line)Object1; l1 = (Line)Object1;
Line l2 = (Line)Object2; l2 = (Line)Object2;
if(l1.lineState == State.confirm) if(l1.lineState == State.confirm)
{ {
...@@ -185,6 +239,41 @@ public class Constraint ...@@ -185,6 +239,41 @@ public class Constraint
target.setPoint(tmp.add(direction * l2.getLength())); target.setPoint(tmp.add(direction * l2.getLength()));
} }
target.setState(State.confirm);
break;
case ConstraintType.LineNormal:
l1 = (Line)Object1;
l2 = (Line)Object2;
if(l1.lineState == State.confirm)
{
normal = l1.direction.normalized;
tmp = l2.getAnotherPoint(target);
direction = new Vector3(target.x - tmp.x, target.y - tmp.y, target.z - tmp.z);
sin = Vector3.Dot(l1.getVector3(), l2.getVector3()) / l1.getLength() / l2.getLength();
cos = Mathf.Sqrt(1 - cos * cos);
direction = direction * cos + Vector3.Cross(normal, direction) * sin + normal * Vector3.Dot(normal, direction) * (1 - cos);
target.setPoint(direction.x - tmp.x, direction.y - tmp.y, direction.z - tmp.z);
}
else if(l2.lineState == State.confirm)
{
normal = l2.direction.normalized;
tmp = l1.getAnotherPoint(target);
direction = new Vector3(target.x - tmp.x, target.y - tmp.y, target.z - tmp.z);
sin = Vector3.Dot(l2.getVector3(), l1.getVector3()) / l2.getLength() / l1.getLength();
cos = Mathf.Sqrt(1 - cos * cos);
direction = direction * cos + Vector3.Cross(normal, direction) * sin + normal * Vector3.Dot(normal, direction) * (1 - cos);
target.setPoint(direction.x - tmp.x, direction.y - tmp.y, direction.z - tmp.z);
}
target.setState(State.confirm); target.setState(State.confirm);
break; break;
} }
...@@ -195,11 +284,16 @@ public class Constraint ...@@ -195,11 +284,16 @@ public class Constraint
Gradient tmp; Gradient tmp;
float value = 0; float value = 0;
Line l1 = null;
Line l2 = null;
float length = 0;
switch (constraintType) switch (constraintType)
{ {
case ConstraintType.LineLength: case ConstraintType.LineLength:
Line l = (Line)Object1; l1 = (Line)Object1;
float length = (float)Object2; length = (float)Object2;
tmp = getGradient(); tmp = getGradient();
value = getConstraintValue(); value = getConstraintValue();
...@@ -208,8 +302,8 @@ public class Constraint ...@@ -208,8 +302,8 @@ public class Constraint
break; break;
case ConstraintType.LineEqual: case ConstraintType.LineEqual:
Line l1 = (Line)Object1; l1 = (Line)Object1;
Line l2 = (Line)Object2; l2 = (Line)Object2;
tmp = getGradient(); tmp = getGradient();
value = getConstraintValue(); value = getConstraintValue();
...@@ -217,6 +311,17 @@ public class Constraint ...@@ -217,6 +311,17 @@ public class Constraint
target += tmp.calculateGradient(value); target += tmp.calculateGradient(value);
break; break;
case ConstraintType.LineNormal:
l1 = (Line)Object1;
l2 = (Line)Object2;
tmp = getGradient();
value = getConstraintValue();
target += tmp.calculateGradient(value);
break;
} }
} }
} }
...@@ -58,6 +58,12 @@ public class Line ...@@ -58,6 +58,12 @@ public class Line
return null; return null;
} }
public Point getUnknownPoint(){
if(point2.getState() > point1.getState())
return point2;
return point1;
}
public Gradient getGradient() public Gradient getGradient()
{ {
if(point2.getState() > point1.getState()) if(point2.getState() > point1.getState())
...@@ -78,4 +84,8 @@ public class Line ...@@ -78,4 +84,8 @@ public class Line
point2.constraints.Add(c); point2.constraints.Add(c);
} }
public Vector3 getVector3(){
return new Vector3(point1.x - point2.x, point1.y - point2.y, point1.z - point2.z);
}
} }
...@@ -30,7 +30,8 @@ public class UIManager : MonoBehaviour ...@@ -30,7 +30,8 @@ public class UIManager : MonoBehaviour
none, none,
createLine, createLine,
linelength, linelength,
lineEqual lineEqual,
lineNormal
} }
UIState state = UIState.none; UIState state = UIState.none;
...@@ -68,6 +69,12 @@ public class UIManager : MonoBehaviour ...@@ -68,6 +69,12 @@ public class UIManager : MonoBehaviour
createConstraint(); createConstraint();
} }
break; break;
case UIState.lineNormal:
if (!chosen1.Equals("") && !chosen2.Equals(""))
{
createConstraint();
}
break;
} }
} }
...@@ -103,13 +110,13 @@ public class UIManager : MonoBehaviour ...@@ -103,13 +110,13 @@ public class UIManager : MonoBehaviour
public void chooseObject(string obj) public void chooseObject(string obj)
{ {
if(state != UIState.none) if(state != UIState.none)
{ {
if (chosen1.Equals("")) if (chosen1.Equals(""))
chosen1 = obj; chosen1 = obj;
if (!chosen1.Equals("") && !chosen1.Equals(obj) && chosen2.Equals("")) if (!chosen1.Equals("") && !chosen1.Equals(obj) && chosen2.Equals(""))
chosen2 = obj; chosen2 = obj;
Debug.Log(obj); Debug.Log(obj);
} }
} }
...@@ -122,43 +129,57 @@ public class UIManager : MonoBehaviour ...@@ -122,43 +129,57 @@ public class UIManager : MonoBehaviour
} }
} }
public void createConstraintButton() public void createConstraintButton()
{ {
constraintDropdown.SetActive(true); constraintDropdown.SetActive(true);
} }
public void constraintDropdownChange() public void constraintDropdownChange()
{ {
string text = constraintDropdown.GetComponent<Dropdown>().captionText.text; string text = constraintDropdown.GetComponent<Dropdown>().captionText.text;
ConstraintType type = (ConstraintType)System.Enum.Parse(typeof(ConstraintType), text); ConstraintType type = (ConstraintType)System.Enum.Parse(typeof(ConstraintType), text);
switch (type) switch (type)
{ {
case ConstraintType.LineLength: case ConstraintType.LineLength:
state = UIState.linelength; state = UIState.linelength;
constraintInput.SetActive(true); constraintInput.SetActive(true);
break; break;
case ConstraintType.LineEqual: case ConstraintType.LineEqual:
state = UIState.lineEqual; state = UIState.lineEqual;
break; break;
} case ConstraintType.LineNormal:
state = UIState.lineNormal;
break;
}
} }
void createConstraint() void createConstraint()
{ {
string n = ""; Line l1 = null;
switch (state) Line l2 = null;
{
case UIState.linelength: float length;
Line l = CM.GetLine(chosen1);
float length = float.Parse(constraintInput.GetComponent<InputField>().text); string n = "";
n = CM.createConstraint(l, length, ConstraintType.LineLength); switch (state)
constraintInput.SetActive(false); {
break; case UIState.linelength:
case UIState.lineEqual: l1 = CM.GetLine(chosen1);
Line l1 = CM.GetLine(chosen1); length = float.Parse(constraintInput.GetComponent<InputField>().text);
Line l2 = CM.GetLine(chosen2); n = CM.createConstraint(l1, length, ConstraintType.LineLength);
n = CM.createConstraint(l1, l2, ConstraintType.LineEqual); constraintInput.SetActive(false);
break; break;
case UIState.lineEqual:
l1 = CM.GetLine(chosen1);
l2 = CM.GetLine(chosen2);
n = CM.createConstraint(l1, l2, ConstraintType.LineEqual);
break;
case UIState.lineNormal:
l1 = CM.GetLine(chosen1);
l2 = CM.GetLine(chosen2);
n = CM.createConstraint(l1, l2, ConstraintType.LineNormal);
break;
} }
chosen1 = ""; chosen1 = "";
chosen2 = ""; chosen2 = "";
...@@ -167,8 +188,8 @@ public class UIManager : MonoBehaviour ...@@ -167,8 +188,8 @@ public class UIManager : MonoBehaviour
constraints.Add(tmp); constraints.Add(tmp);
tmp.transform.localPosition = new Vector3(100 + 70 * (constraints.Count - 1), 0, 0); tmp.transform.localPosition = new Vector3(100 + 70 * (constraints.Count - 1), 0, 0);
tmp.GetComponentInChildren<Text>().text = n; tmp.GetComponentInChildren<Text>().text = n;
state = UIState.none; state = UIState.none;
constraintDropdown.GetComponent<Dropdown>().captionText.text = "None"; constraintDropdown.GetComponent<Dropdown>().captionText.text = "None";
constraintDropdown.SetActive(false); constraintDropdown.SetActive(false);
} }
} }
No preview for this file type
No preview for this file type
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
File added
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment