-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
280 lines (236 loc) · 7.51 KB
/
Copy pathUtil.java
File metadata and controls
280 lines (236 loc) · 7.51 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* This file is part of OpenDS (Open Source Driving Simulator).
* Copyright (C) 2015 Rafael Math
*
* OpenDS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenDS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenDS. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.opends.tools;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
*
* @author Rafael Math
*/
public class Util
{
public static Geometry findGeom(Spatial spatial, String name)
{
if (spatial instanceof Node)
{
Node node = (Node) spatial;
for (int i = 0; i < node.getQuantity(); i++)
{
Spatial child = node.getChild(i);
Geometry result = findGeom(child, name);
if (result != null)
return result;
}
} else if (spatial instanceof Geometry)
{
if (spatial.getName().startsWith(name))
return (Geometry) spatial;
}
return null;
}
public static Node findNode(Spatial spatial, String name)
{
if (spatial != null && spatial instanceof Node)
{
Node node = (Node) spatial;
if (node.getName() != null && node.getName().startsWith(name))
return (Node) spatial;
for (int i = 0; i < node.getQuantity(); i++)
{
Spatial child = node.getChild(i);
Node result = findNode(child, name);
if (result != null)
return result;
}
}
return null;
}
public static String printTree(Spatial spatial)
{
if (spatial instanceof Node)
{
Node node = (Node) spatial;
String resultList = "";
for (int i = 0; i < node.getQuantity(); i++)
{
Spatial child = node.getChild(i);
String result = printTree(child);
resultList = resultList + result + " ";
}
return resultList;
} else if (spatial instanceof Geometry)
{
return spatial.getName();
}
return null;
}
public static String getPath(Spatial spatial)
{
if (spatial != null)
return getPath(spatial.getParent()) + "/" + spatial.getName();
else
return "";
}
private static List<Geometry> resultListGeometries = new ArrayList<Geometry>();
private static void collectGeometries(Spatial spatial)
{
if (spatial instanceof Node)
{
for (Spatial child: ((Node) spatial).getChildren())
collectGeometries(child);
}
else if (spatial instanceof Geometry)
{
resultListGeometries.add((Geometry) spatial);
}
}
public static List<Geometry> getAllGeometries(Spatial spatial)
{
resultListGeometries.clear();
collectGeometries(spatial);
return resultListGeometries;
}
/*
//---------------------------------------------
private static List<Spatial> resultListSpatials = new ArrayList<Spatial>();
private static void collectSpatials(Spatial spatial)
{
resultListSpatials.add(spatial);
if (spatial instanceof Node)
{
for (Spatial child: ((Node) spatial).getChildren())
collectSpatials(child);
}
}
public static List<Spatial> getAllSpatials(Spatial spatial)
{
resultListSpatials.clear();
collectSpatials(spatial);
return resultListSpatials;
}
//---------------------------------------------
*/
public static void setFaceCullMode(Spatial spatial, FaceCullMode mode)
{
List<Geometry> geometryList = getAllGeometries(spatial);
for(Geometry geometry : geometryList)
geometry.getMaterial().getAdditionalRenderState().setFaceCullMode(mode);
}
public static void setWireFrame(Spatial spatial, boolean enable)
{
List<Geometry> geometryList = getAllGeometries(spatial);
for(Geometry geometry : geometryList)
geometry.getMaterial().getAdditionalRenderState().setWireframe(enable);
}
public static void open(String fileName)
{
try {
File file = new File(fileName);
if (file.exists())
{
if (Desktop.isDesktopSupported())
{
Desktop.getDesktop().open(file);
} else
{
System.err.println("Awt Desktop is not supported!");
}
} else {
System.err.println("File does not exist!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String getDateTimeString()
{
Calendar cal = Calendar.getInstance();
DecimalFormat form = new DecimalFormat("00");
String str = Integer.toString(cal.get(Calendar.YEAR)) + "_"
+ form.format(cal.get(Calendar.MONTH) + 1) + "_"
+ form.format(cal.get(Calendar.DAY_OF_MONTH)) + "-"
+ form.format(cal.get(Calendar.HOUR_OF_DAY)) + "_"
+ form.format(cal.get(Calendar.MINUTE)) + "_"
+ form.format(cal.get(Calendar.SECOND));
return str;
}
/**
* Makes the given directory if it not yet exists.
*/
public static void makeDirectory(String directory)
{
File dir = new File(directory);
if (!dir.exists())
dir.mkdir();
else if (!dir.isDirectory())
System.err.println("'" + directory + "' exists but is not a directory");
}
public static boolean isValidFilename(String filename)
{
File f = new File(filename);
try {
f.getCanonicalPath();
return true;
} catch (IOException e) {
return false;
}
}
/**
* Computes the angle between three given points A, B, and C at point B.
*
* @param pointA
* 3-dimensional point A
*
* @param pointB
* 3-dimensional point B where angle is measured
*
* @param pointC
* 3-dimensional point C
*
* @param is2DSpace
* Compute angle in 2D-space (y=0 in points A, B, C)
*
* @return
* Angle between vector BA and vector BC
*/
public static float getAngleBetweenPoints(Vector3f pointA, Vector3f pointB, Vector3f pointC, boolean is2DSpace)
{
// vector pointing from vehicle's center towards vehicle's front
Vector3f vectorBA = pointA.subtract(pointB);
if(is2DSpace)
vectorBA.setY(0);
vectorBA.normalizeLocal();
// vector pointing from vehicle's center towards obstacle
Vector3f vectorBC = pointC.subtract(pointB);
if(is2DSpace)
vectorBC.setY(0);
vectorBC.normalizeLocal();
// angle between both vectors
return vectorBA.angleBetween(vectorBC);
}
}