use of raytracer.exception.LinearlyDependentException in project narchy by automenta.
the class PlyObject method loadPlyFile.
/**
* Liest die Informationen aus einer PLY-Datei und erzeugt daraus
* Dreiecke.
*
* @param fileName Dateiname der OFF-Datei.
* @param smoothNormals Gibt an, ob die Normalen interpoliert werden
* sollen, falls die PLY-Datei Informationen zu den Normalenvektoren
* enth�lt.
*
* @throws java.io.IOException
* @throws raytracer.exception.InvalidFormatException
*/
private void loadPlyFile(String fileName, boolean smoothNormals) throws InvalidFormatException {
// Datei �ffnen:
Scanner scanner = new Scanner(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(fileName)));
scanner.useLocale(Locale.US);
Tokenizer tokenizer = new Tokenizer(scanner);
Vector<Element> elements = new Vector<Element>();
try {
// PLY-Kennung pr�fen:
if (tokenizer.token() != Tokenizer.TOKEN_PLY)
throw new InvalidFormatException();
tokenizer.next();
// Format-Kennung pr�fen:
if (tokenizer.token() != Tokenizer.TOKEN_FORMAT)
throw new InvalidFormatException();
tokenizer.next();
if (tokenizer.token() != Tokenizer.TOKEN_ASCII)
throw new InvalidFormatException();
tokenizer.next();
if (tokenizer.token() != Tokenizer.TOKEN_NUMBER)
throw new InvalidFormatException();
if (tokenizer.doubleValue() != 1.0)
throw new InvalidFormatException();
tokenizer.next();
// Header-Informationen der PLY-Datei auslesen:
Property property = null;
Element currentElement = null;
loop: do {
switch(tokenizer.token()) {
case Tokenizer.TOKEN_COMMENT:
tokenizer.endLine();
tokenizer.next();
continue;
case Tokenizer.TOKEN_ELEMENT:
tokenizer.next();
// Neues Element erstellen:
currentElement = new Element();
elements.add(currentElement);
// Eigenschaften des Elements lesen:
if (tokenizer.token() != Tokenizer.TOKEN_NAME)
throw new InvalidFormatException();
currentElement.name = tokenizer.stringValue();
tokenizer.next();
if (tokenizer.token() != Tokenizer.TOKEN_NUMBER)
throw new InvalidFormatException();
currentElement.count = tokenizer.intValue();
tokenizer.next();
break;
case Tokenizer.TOKEN_PROPERTY:
if (currentElement == null)
throw new InvalidFormatException();
tokenizer.next();
// Neue Eigenschaft erstellen:
property = new Property();
currentElement.properties.add(property);
// Eigenschaften der Eigenschaft lesen:
if (!Tokenizer.isType(tokenizer.token()))
throw new InvalidFormatException();
property.type = tokenizer.token();
tokenizer.next();
if (Tokenizer.isTypeList(property.type)) {
if (!Tokenizer.isTypeInt(tokenizer.token()))
throw new InvalidFormatException();
property.listCountType = tokenizer.token();
tokenizer.next();
if (!Tokenizer.isType(tokenizer.token()))
throw new InvalidFormatException();
property.listDataType = tokenizer.token();
tokenizer.next();
}
if (tokenizer.token() != Tokenizer.TOKEN_NAME)
throw new InvalidFormatException();
property.name = tokenizer.stringValue();
tokenizer.next();
break;
case Tokenizer.TOKEN_END_HEADER:
break loop;
default:
throw new InvalidFormatException();
}
} while (true);
Vector<Vector3d> vertices = new Vector<Vector3d>();
Vector<Vector3d> normals = new Vector<Vector3d>();
Vector<Vector2d> textures = new Vector<Vector2d>();
Vector<Integer> faces = new Vector<Integer>();
// Daten auslesen und verarbeiten:
for (Element element : elements) {
currentElement = element;
switch(currentElement.name) {
case "vertex":
readVertices(scanner, currentElement, vertices, normals, textures);
break;
case "face":
readFaces(scanner, currentElement, faces);
break;
default:
readElement(scanner, currentElement);
break;
}
}
// Fl�chen der PLY-Datei triangulieren und Dreiecke daraus erzeugen:
Iterator<Integer> itFaces = faces.iterator();
while (itFaces.hasNext()) {
int count = -itFaces.next();
if (count < 3)
throw new InvalidFormatException();
// Ersten beiden Punkte ermitteln:
int firstId = itFaces.next();
int previousId = itFaces.next();
// Aus den restlichen Punkten der Fl�che Dreiecke erzeugen:
count -= 2;
do {
int currentId = itFaces.next();
try {
Triangle triangle = new Triangle(vertices.get(firstId), vertices.get(previousId), vertices.get(currentId), shader);
triangle.setNormalEffect(normalEffect);
// Normalenvektoren setzen:
if (smoothNormals) {
Vector3d normalA = normals.get(firstId);
Vector3d normalB = normals.get(previousId);
Vector3d normalC = normals.get(currentId);
if ((normalA != null) && (normalB != null) && (normalC != null))
triangle.setNormals(normalA, normalB, normalC);
}
// Texturkoordinaten setzen:
Vector2d textureA = textures.get(firstId);
Vector2d textureB = textures.get(previousId);
Vector2d textureC = textures.get(currentId);
if ((textureA != null) && (textureB != null) && (textureC != null))
triangle.setTextureCoords(textureA, textureB, textureC);
shapes.add(triangle);
} catch (LinearlyDependentException e) {
// Falls bei den Koordinaten lineare Abh�ngigkeit besteht,
// verwerfe deises Dreieck.
}
previousId = currentId;
} while (--count > 0);
}
} finally {
scanner.close();
}
}
use of raytracer.exception.LinearlyDependentException in project narchy by automenta.
the class OffObject method loadOffFile.
/**
* Liest die Fl�cheninformationen aus einer OFF-Datei und erzeugt daraus
* Dreiecke.
*
* @param fileName Dateiname der OFF-Datei.
*
* @throws java.io.IOException
* @throws raytracer.exception.InvalidFormatException
*/
private void loadOffFile(String fileName) throws InvalidFormatException {
// Datei �ffnen:
Scanner scanner = new Scanner(getClass().getClassLoader().getResourceAsStream(fileName));
scanner.useLocale(Locale.US);
Vector<Vector3d> vertices = new Vector<Vector3d>();
try {
// OFF-Format Kennung pr�fen:
if (!scanner.hasNext() || !scanner.next().equals("OFF"))
throw new InvalidFormatException();
// Anzahl der Punkte, Fl�chen und Kanten bestimmen:
int vertexCount = scanner.nextInt();
int faceCount = scanner.nextInt();
scanner.nextInt();
// Punkte auslesen:
int i;
for (i = 0; i < vertexCount; i++) {
double x = scanner.nextDouble();
double y = scanner.nextDouble();
double z = scanner.nextDouble();
vertices.add(new Vector3d(x, y, z));
}
// Fl�chen auslesen und trianguliert zu diesem Objekt hinzuf�gen:
for (i = 0; i < faceCount; i++) {
int count = scanner.nextInt();
if (count < 3)
throw new InvalidFormatException();
// Ersten beiden Punkte ermitteln:
Vector3d first = vertices.get(scanner.nextInt());
Vector3d previous = vertices.get(scanner.nextInt());
// Aus den restlichen Punkten der Fl�che Dreiecke erzeugen:
count -= 2;
do {
Vector3d v = vertices.get(scanner.nextInt());
try {
Triangle triangle = new Triangle(first, previous, v, shader);
triangle.setNormalEffect(normalEffect);
shapes.add(triangle);
} catch (LinearlyDependentException e) {
// Falls bei den Koordinaten lineare Abh�ngigkeit besteht,
// verwerfe deises Dreieck.
}
previous = v;
} while (--count > 0);
}
} finally {
scanner.close();
}
}
Aggregations