-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaypointsExtractor.cs
185 lines (141 loc) · 6.92 KB
/
WaypointsExtractor.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public class WaypointsExtractor : MonoBehaviour
{
public GameObject TrackList;
private bool isValid;
private GameObject activeTrack;
public string savePath;
private string savePathDefault = "Assets/Waypoints/";
public string fileName;
private bool checkValid(GameObject TrackList)
{
int childCount = TrackList.transform.childCount;
int isActiveCount = 0;
for (int i = 0; i < childCount; i++) {
GameObject child = TrackList.transform.GetChild(i).gameObject;
if (child.activeSelf) {
isActiveCount += 1;
}
}
if (isActiveCount == 1) {
return true;
}
return false;
}
private GameObject findActiveTrack(GameObject TrackList)
{
int childCount = TrackList.transform.childCount;
for (int i = 0; i < childCount; i++) {
GameObject child = TrackList.transform.GetChild(i).gameObject;
if (child.activeSelf) {
return child;
}
}
return null;
}
private bool checkWaypointsValid(GameObject innerLine_obj, GameObject outerLine_obj)
{
int innerChildCount = innerLine_obj.transform.childCount;
int outerChildCount = outerLine_obj.transform.childCount;
if (innerChildCount != outerChildCount) {
return false;
}
for (int i = 0; i < innerChildCount; i++) {
GameObject innerChild = innerLine_obj.transform.GetChild(i).gameObject;
GameObject outerChild = outerLine_obj.transform.GetChild(i).gameObject;
if (innerChild.transform.Find("waypoints").gameObject.transform.childCount !=
outerChild.transform.Find("waypoints").gameObject.transform.childCount) {
return false;
}
}
return true;
}
private Vector3 calculateCenterPoint(Vector3 innerPos, Vector3 outerPos)
{
float x = (innerPos.x + outerPos.x) / 2;
float y = (innerPos.y + outerPos.y) / 2;
float z = (innerPos.z + outerPos.z) / 2;
return new Vector3(x, y, z);
}
private int recordWaypoints(int rowIndex, GameObject innerWaypoints_obj, GameObject outerWaypoints_obj,
Vector3[] innerWaypoints, Vector3[] outerWaypoints, Vector3[] centerWaypoints)
{
int childCount = innerWaypoints_obj.transform.childCount;
for (int i = 0; i < childCount; i++) {
GameObject innerChild = innerWaypoints_obj.transform.GetChild(i).gameObject;
GameObject outerChild = outerWaypoints_obj.transform.GetChild(i).gameObject;
Vector3 innerPos = innerChild.transform.position;
Vector3 outerPos = outerChild.transform.position;
innerWaypoints[rowIndex] = innerPos;
outerWaypoints[rowIndex] = outerPos;
centerWaypoints[rowIndex] = calculateCenterPoint(innerPos, outerPos);
rowIndex += 1;
}
return rowIndex;
}
private void writeToCsv(string path, int totalWaypointsCount,
Vector3[] innerWaypoints, Vector3[] outerWaypoints, Vector3[] centerWaypoints)
{
Debug.Log("Writing waypoints to \"" + path + "\"...");
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
string headers = "inner_x,inner_y,inner_z," +
"outer_x,outer_y,outer_z," +
"center_x,center_y,center_z";
file.WriteLine(headers);
for (int i = 0; i < totalWaypointsCount; i++) {
string line = innerWaypoints[i].x + "," + innerWaypoints[i].y + "," + innerWaypoints[i].z + ","
+ outerWaypoints[i].x + "," + outerWaypoints[i].y + "," + outerWaypoints[i].z + ","
+ centerWaypoints[i].x + "," + centerWaypoints[i].y + "," + centerWaypoints[i].z;
file.WriteLine(line);
}
file.Close();
}
public void ExtractWaypoints()
{
isValid = checkValid(TrackList);
activeTrack = findActiveTrack(TrackList);
if (isValid) {
Debug.Log("Extracting waypoints of \"" + activeTrack.name + "\"...");
GameObject innerLine_obj = activeTrack.transform.Find("waypoints").gameObject.transform.Find("inner line").gameObject;
GameObject outerLine_obj = activeTrack.transform.Find("waypoints").gameObject.transform.Find("outer line").gameObject;
if (checkWaypointsValid(innerLine_obj, outerLine_obj)) {
LineWaypointsCounter script = activeTrack.GetComponent<LineWaypointsCounter>();
Tuple<int,int> totalWaypointsCount = script.CalculateTotalWaypoints();
Vector3[] innerWaypoints = new Vector3[totalWaypointsCount.Item1];
Vector3[] outerWaypoints = new Vector3[totalWaypointsCount.Item1];
Vector3[] centerWaypoints = new Vector3[totalWaypointsCount.Item1];
int pointCount = innerLine_obj.transform.childCount;
int rowIndex = 0;
for (int i = 0; i < pointCount; i++) {
GameObject innerChild = innerLine_obj.transform.GetChild(i).gameObject;
GameObject outerChild = outerLine_obj.transform.GetChild(i).gameObject;
GameObject innerWaypoints_obj = innerChild.transform.Find("waypoints").gameObject;
GameObject outerWaypoints_obj = outerChild.transform.Find("waypoints").gameObject;
rowIndex = recordWaypoints(rowIndex, innerWaypoints_obj, outerWaypoints_obj,
innerWaypoints, outerWaypoints, centerWaypoints);
}
if (savePath.Trim() == "") {
savePath = savePathDefault;
}
if (savePath[savePath.Length - 1] != '/' && savePath[savePath.Length - 1] != '\\') {
savePath += '/';
}
if (fileName.Trim() == "") {
fileName = activeTrack.name;
}
string path = savePath + fileName + ".csv";
writeToCsv(path, totalWaypointsCount.Item1,
innerWaypoints, outerWaypoints, centerWaypoints);
Debug.Log("\"" + activeTrack.name + "\" waypoints extracted successfully to \"" + path + "\"!");
} else {
Debug.LogError("Invalid - Different number of inner and outer waypoints!");
}
} else {
Debug.LogError("Invalid - There was more than 1 active track.");
}
}
}