Java,从文件中读取 x、y、z 坐标
Java, read x-,y-,z-coordinates from a file
我正在制作由三角形组成的 3D 对象。这些三角形有 3 个向量。
现在我有一个包含大量数字的文件...如果一行以 "v" 开头,则该行具有向量的 x、y 和 z 坐标。
如果一行以 "f" 开头,则该行包含 .txt 文件中的 Vector 行,这是我的三角形所需的。
该文件首先以所有 "v" 开始,然后以 "f" 继续。
例子:(开头的数字就是行)
21 v 1.2000 0.20000 -1.0000 -> Vector1(1.2, 0.2, -1)
22 v 1.2000 0.20000 1.00000 -> Vector2(1.2, 0.2, 1)
23 v -1.200 -0.2000 1.00000 -> Vector3(-1.2, -0.2, 1)
...
71 f 21 23 22 -> 三角形(向量 1、向量 3、向量 2)
这就是我尝试过的方法,显然没有用,因为我是 Java 新手 :P
public static ArrayList<Triangle> mesh = new ArrayList<>();
public static void loadObject(String fileName) {
try {
Scanner scan = new Scanner(fileName);
ArrayList<Vector> vectors = new ArrayList<>();
while (scan.hasNextLine()) {
if (scan.equals("v")) {
Vector v = new Vector();
int i = 0;
while (scan.hasNextDouble() && i < 3) {
if (i == 0) {
v.setX(scan.nextDouble());
}
if (i == 1) {
v.setY(scan.nextDouble());
}
if (i == 2) {
v.setZ(scan.nextDouble());
}
i++;
}
vectors.add(v);
}
if (scan.equals("f")) {
Triangle t = new Triangle();
int j = 0;
while (scan.hasNextInt() && j < 3) {
if (j == 0) {
t.setVec1(vectors.get(scan.nextInt() - 1));
}
if (j == 1) {
t.setVec2(vectors.get(scan.nextInt() - 1));
}
if (j == 2) {
t.setVec3(vectors.get(scan.nextInt() - 1));
}
j++;
}
mesh.add(t);
}
}
} catch (Exception e) {
}
}
感谢您的帮助
您描述的 Wavefron OBJ file format 已经存在许多装载程序。您应该考虑使用现有的加载器,而不是自己滚动。
通过谷歌搜索,我立即在 Github 上找到了三个 Java .obj 加载程序:
我没有使用过任何这些,因此您需要自己尝试一下,看看它们是否为您提供正确的 API 并解决您的具体问题。
这样的事情怎么样:
static List<Triangle> parse(String fileName)
{
List<Triangle> mesh = new ArrayList<>();
Map<String, Vector> vm = new HashMap<>();
try (Scanner scan = new Scanner(new FileReader(fileName)))
{
while(scan.hasNextLine())
{
String[] p = scan.nextLine().split("\s");
if("v".equals(p[1]))
{
vm.put(p[0], new Vector(Double.parseDouble(p[2]), Double.parseDouble(p[3]), Double.parseDouble(p[4])));
}
else if("f".equals(p[1]))
{
mesh.add(new Triangle(vm.get(p[2]), vm.get(p[3]), vm.get(p[4])));
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
return mesh;
}
我正在制作由三角形组成的 3D 对象。这些三角形有 3 个向量。 现在我有一个包含大量数字的文件...如果一行以 "v" 开头,则该行具有向量的 x、y 和 z 坐标。 如果一行以 "f" 开头,则该行包含 .txt 文件中的 Vector 行,这是我的三角形所需的。 该文件首先以所有 "v" 开始,然后以 "f" 继续。
例子:(开头的数字就是行)
21 v 1.2000 0.20000 -1.0000 -> Vector1(1.2, 0.2, -1)
22 v 1.2000 0.20000 1.00000 -> Vector2(1.2, 0.2, 1)
23 v -1.200 -0.2000 1.00000 -> Vector3(-1.2, -0.2, 1)
...
71 f 21 23 22 -> 三角形(向量 1、向量 3、向量 2)
这就是我尝试过的方法,显然没有用,因为我是 Java 新手 :P
public static ArrayList<Triangle> mesh = new ArrayList<>();
public static void loadObject(String fileName) {
try {
Scanner scan = new Scanner(fileName);
ArrayList<Vector> vectors = new ArrayList<>();
while (scan.hasNextLine()) {
if (scan.equals("v")) {
Vector v = new Vector();
int i = 0;
while (scan.hasNextDouble() && i < 3) {
if (i == 0) {
v.setX(scan.nextDouble());
}
if (i == 1) {
v.setY(scan.nextDouble());
}
if (i == 2) {
v.setZ(scan.nextDouble());
}
i++;
}
vectors.add(v);
}
if (scan.equals("f")) {
Triangle t = new Triangle();
int j = 0;
while (scan.hasNextInt() && j < 3) {
if (j == 0) {
t.setVec1(vectors.get(scan.nextInt() - 1));
}
if (j == 1) {
t.setVec2(vectors.get(scan.nextInt() - 1));
}
if (j == 2) {
t.setVec3(vectors.get(scan.nextInt() - 1));
}
j++;
}
mesh.add(t);
}
}
} catch (Exception e) {
}
}
感谢您的帮助
您描述的 Wavefron OBJ file format 已经存在许多装载程序。您应该考虑使用现有的加载器,而不是自己滚动。
通过谷歌搜索,我立即在 Github 上找到了三个 Java .obj 加载程序:
我没有使用过任何这些,因此您需要自己尝试一下,看看它们是否为您提供正确的 API 并解决您的具体问题。
这样的事情怎么样:
static List<Triangle> parse(String fileName)
{
List<Triangle> mesh = new ArrayList<>();
Map<String, Vector> vm = new HashMap<>();
try (Scanner scan = new Scanner(new FileReader(fileName)))
{
while(scan.hasNextLine())
{
String[] p = scan.nextLine().split("\s");
if("v".equals(p[1]))
{
vm.put(p[0], new Vector(Double.parseDouble(p[2]), Double.parseDouble(p[3]), Double.parseDouble(p[4])));
}
else if("f".equals(p[1]))
{
mesh.add(new Triangle(vm.get(p[2]), vm.get(p[3]), vm.get(p[4])));
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
return mesh;
}