/** {@inheritDoc} */
  @Override
  public boolean equals(Object other) {
    if (other == this) {
      return true;
    }
    if (!(other instanceof TFloatList)) return false;

    TFloatList that = (TFloatList) other;
    if (size() != that.size()) return false;

    for (int i = 0; i < size(); i++) {
      if (get(i) != that.get(i)) {
        return false;
      }
    }
    return true;
  }
Exemple #2
0
 /**
  * Loads a .obj file, storing the data in the provided lists. After loading, the input stream will
  * be closed.The number of components for each attribute is returned in a Vector3, x being the
  * number of position components, y the number of texture coord components and z the number of
  * normal components. Note that normal and/or texture coord attributes might be missing from the
  * .obj file. If this is the case, their lists will be empty. The indices are stored in the
  * indices list.
  *
  * @param stream The input stream for the .obj file
  * @param positions The list in which to store the positions
  * @param textureCoords The list in which to store the texture coords
  * @param normals The list in which to store the normals
  * @param indices The list in which to store the indices
  * @return A Vector3 containing, in order, the number of components for the positions, texture
  *     coords and normals
  * @throws MalformedObjFileException If any errors occur during loading
  */
 public static Vector3f load(
     InputStream stream,
     TFloatList positions,
     TFloatList textureCoords,
     TFloatList normals,
     TIntList indices) {
   int positionSize = -1;
   final TFloatList rawTextureCoords = new TFloatArrayList();
   int textureCoordSize = -1;
   final TFloatList rawNormalComponents = new TFloatArrayList();
   int normalSize = -1;
   final TIntList textureCoordIndices = new TIntArrayList();
   final TIntList normalIndices = new TIntArrayList();
   String line = null;
   try (Scanner scanner = new Scanner(stream)) {
     while (scanner.hasNextLine()) {
       line = scanner.nextLine();
       if (line.startsWith(POSITION_LIST_PREFIX + COMPONENT_SEPARATOR)) {
         parseComponents(positions, line);
         if (positionSize == -1) {
           positionSize = positions.size();
         }
       } else if (line.startsWith(TEXTURE_LIST_PREFIX + COMPONENT_SEPARATOR)) {
         parseComponents(rawTextureCoords, line);
         if (textureCoordSize == -1) {
           textureCoordSize = rawTextureCoords.size();
         }
       } else if (line.startsWith(NORMAL_LIST_PREFIX + COMPONENT_SEPARATOR)) {
         parseComponents(rawNormalComponents, line);
         if (normalSize == -1) {
           normalSize = rawNormalComponents.size();
         }
       } else if (line.startsWith(INDEX_LIST_PREFIX + COMPONENT_SEPARATOR)) {
         parseIndices(indices, textureCoordIndices, normalIndices, line);
       }
     }
     line = null;
     final boolean hasTextureCoords;
     final boolean hasNormals;
     if (!textureCoordIndices.isEmpty() && !rawTextureCoords.isEmpty()) {
       textureCoords.fill(0, positions.size() / positionSize * textureCoordSize, 0);
       hasTextureCoords = true;
     } else {
       hasTextureCoords = false;
     }
     if (!normalIndices.isEmpty() && !rawNormalComponents.isEmpty()) {
       normals.fill(0, positions.size() / positionSize * normalSize, 0);
       hasNormals = true;
     } else {
       hasNormals = false;
     }
     if (hasTextureCoords) {
       for (int i = 0; i < textureCoordIndices.size(); i++) {
         final int textureCoordIndex = textureCoordIndices.get(i) * textureCoordSize;
         final int positionIndex = indices.get(i) * textureCoordSize;
         for (int ii = 0; ii < textureCoordSize; ii++) {
           textureCoords.set(positionIndex + ii, rawTextureCoords.get(textureCoordIndex + ii));
         }
       }
     }
     if (hasNormals) {
       for (int i = 0; i < normalIndices.size(); i++) {
         final int normalIndex = normalIndices.get(i) * normalSize;
         final int positionIndex = indices.get(i) * normalSize;
         for (int ii = 0; ii < normalSize; ii++) {
           normals.set(positionIndex + ii, rawNormalComponents.get(normalIndex + ii));
         }
       }
     }
   } catch (Exception ex) {
     throw new MalformedObjFileException(line, ex);
   }
   return new Vector3f(positionSize, textureCoordSize, normalSize).max(0, 0, 0);
 }