Commit c0bd973f authored by Alexander Kreibich's avatar Alexander Kreibich

Adding of the CreationScript and the GuidewireCreateManager also adding of the...

Adding of the CreationScript and the GuidewireCreateManager also adding of the passing method in the SimulationLoop that gains the sphere and cylinder arrays
parent 69d1f8c6
using UnityEngine;
using System.IO;
using GuidewireSim;
public class CreationScript : MonoBehaviour
{
public GameObject spherePrefab;
public GameObject cylinderPrefab;
private int spheresCount;
private int cylinderCount;
private GameObject[] spheres;
private GameObject[] cylinders;
private SimulationLoop simulationLoop;
void Start()
{
simulationLoop = GameObject.Find("Simulation").GetComponent<SimulationLoop>();
}
void Update()
{
SavePositionsToFile();
}
public void CreateGuidewire(int numberElements)
{
spheres = new GameObject[numberElements];
cylinders = new GameObject[numberElements - 1];
float rEL = simulationLoop.GetRodElementLength();
for (int i = 0; i < numberElements; ++i)
{
GameObject sphere = Instantiate(spherePrefab);
sphere.transform.position = new Vector3(0, 7, i * rEL);
sphere.transform.parent = this.transform;
spheres[i] = sphere;
if (i < numberElements - 1)
{
GameObject cylinder = Instantiate(cylinderPrefab);
cylinder.layer = 6;
cylinder.transform.parent = this.transform;
cylinders[i] = cylinder;
}
}
spheresCount = numberElements;
simulationLoop.SetSpheres(spheres);
simulationLoop.SetCylinders(cylinders);
}
public void SavePositionsToFile()
{
string path = "/home/akreibich/TestRobinCode20/Position.txt";
StreamWriter writer = new StreamWriter(path, true);
for (int i = 0; i < spheresCount; ++i)
{
Vector3 position = spheres[i].transform.position;
writer.WriteLine("Sphere " + i + ": " + position.x + "," + position.y + "," + position.z);
}
writer.Close();
}
public GameObject GetLastSphere()
{
if (spheres != null && spheres.Length > 0)
{
return spheres[spheres.Length - 1];
}
return null;
}
public GameObject[] GetSpheres()
{
return spheres;
}
public GameObject[] GetCylinders()
{
return cylinders;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GuidewireSim;
public class GuidewireCreateManager : MonoBehaviour
{
private CreationScript creationScript;
private SimulationLoop simulationLoop;
public GameObject Simulation;
public int L_0 = 100;
private float REL;
private void Start()
{
// Find the SimulationLoop component from the Simulation GameObject
simulationLoop = Simulation.GetComponent<SimulationLoop>();
if (simulationLoop == null)
{
Debug.LogError("SimulationLoop component not found in the Simulation GameObject!");
return; // Exit if SimulationLoop is not found
}
// Get the Rod Element Length
REL = simulationLoop.GetRodElementLength();
int numberOfElements = (int)(L_0 / REL) + 1; // Calculate the desired number of elements
// Find the CreationScript component in the scene
creationScript = FindObjectOfType<CreationScript>();
if (creationScript != null)
{
creationScript.CreateGuidewire(numberOfElements);
// Get the created spheres and cylinders from the CreationScript
GameObject[] createdSpheres = creationScript.GetSpheres();
GameObject[] createdCylinders = creationScript.GetCylinders();
// Link them to the arrays in the SimulationLoop script
simulationLoop.SetSpheres(createdSpheres);
simulationLoop.SetCylinders(createdCylinders);
}
else
{
Debug.LogError("CreationScript component not found in the scene!");
}
}
}
......@@ -78,6 +78,11 @@ public class SimulationLoop : MonoBehaviour
public bool solveBendTwistConstraints = true; //!< Whether or not to perform the constraint solving of the bend twist constraint.
public bool solveCollisionConstraints = true; //!< Whether or not to perform the constraint solving of collision constraints.
public float GetRodElementLength(){
return rodElementLength;
}
float rodElementLength = float.Parse(System.Environment.GetCommandLineArgs()[1]);/**< The distance between two spheres, also the distance between two orientations.
float rodElementLength = 10f; /**< The distance between two spheres, also the distance between two orientations.
* Also the length of one cylinder.
* @note This should be two times the radius of a sphere.
......@@ -336,5 +341,14 @@ public class SimulationLoop : MonoBehaviour
{
collisionHandler.SetCollidersToPredictions(SpheresCount, spherePositionPredictions, spherePositions);
}
public void SetSpheres(GameObject[] spheresArray)
{
spheres = spheresArray;
}
public void SetCylinders(GameObject[] cylindersArray)
{
cylinders = cylindersArray;
}
}
}
\ No newline at end of file
Markdown is supported
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