parent
baec7c6f38
commit
8f7d77dfa8
@ -0,0 +1,35 @@ |
||||
package sig.utils; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.File; |
||||
import java.io.FileReader; |
||||
import java.io.IOException; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class FileUtils { |
||||
public static String[] readFromFile(String filename) { |
||||
File file = new File(filename); |
||||
//System.out.println(file.getAbsolutePath());
|
||||
List<String> contents= new ArrayList<String>(); |
||||
if (file.exists()) { |
||||
try( |
||||
FileReader fw = new FileReader(filename); |
||||
BufferedReader bw = new BufferedReader(fw);) |
||||
{ |
||||
String readline = bw.readLine(); |
||||
do { |
||||
if (readline!=null) { |
||||
//System.out.println(readline);
|
||||
contents.add(readline); |
||||
readline = bw.readLine(); |
||||
}} while (readline!=null); |
||||
fw.close(); |
||||
bw.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
return contents.toArray(new String[contents.size()]); |
||||
} |
||||
} |
@ -0,0 +1,40 @@ |
||||
package sig.utils; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.regex.Pattern; |
||||
|
||||
import javax.vecmath.Vector3f; |
||||
|
||||
import sig.Triangle; |
||||
|
||||
public class OBJReader { |
||||
public static List<Triangle> ReadOBJFile(String f) { |
||||
String[] data = FileUtils.readFromFile(f); |
||||
List<float[]> vertices = new ArrayList<>(); |
||||
List<Triangle> tris = new ArrayList<>(); |
||||
for (String s : data) { |
||||
String[] split = s.split(Pattern.quote(" ")); |
||||
if (split[0].equalsIgnoreCase("v")) { |
||||
vertices.add(new float[]{Float.parseFloat(split[1]),Float.parseFloat(split[2]),Float.parseFloat(split[3])}); |
||||
} else |
||||
if (split[0].equalsIgnoreCase("f")) { |
||||
tris.add(new Triangle( |
||||
new Vector3f( |
||||
vertices.get(Integer.parseInt(split[1])-1)[0], |
||||
vertices.get(Integer.parseInt(split[1])-1)[1], |
||||
vertices.get(Integer.parseInt(split[1])-1)[2]), |
||||
new Vector3f( |
||||
vertices.get(Integer.parseInt(split[2])-1)[0], |
||||
vertices.get(Integer.parseInt(split[2])-1)[1], |
||||
vertices.get(Integer.parseInt(split[2])-1)[2]), |
||||
new Vector3f( |
||||
vertices.get(Integer.parseInt(split[3])-1)[0], |
||||
vertices.get(Integer.parseInt(split[3])-1)[1], |
||||
vertices.get(Integer.parseInt(split[3])-1)[2]))); |
||||
} |
||||
} |
||||
return tris; |
||||
} |
||||
} |
Loading…
Reference in new issue