/**
   * Gets the vertices.
   *
   * @param resolution the resolution
   * @return the vertices
   */
  protected Vertex[] getVertices(int resolution) {
    Vertex[] verts = new Vertex[resolution + 1];

    float t;
    float inc = degrees / (float) resolution;

    double cosTheta = Math.cos(theta);
    double sinTheta = Math.sin(theta);

    MTColor fillColor = this.getFillColor();

    for (int i = 0; i < resolution; i++) {
      t = 0 + (i * inc);
      //			float x = (float) (centerPoint.x + (radiusX * Math.cos(t) * cosTheta) //TODO remove theta
      // stuff? oder enablen als parameter?
      //						- (radiusY * Math.sin(t) * sinTheta) );
      //			float y = (float) (centerPoint.y + (radiusX * Math.cos(t) * sinTheta)
      //						+ (radiusY * Math.sin(t) * cosTheta) );
      float x =
          (float)
              (centerPoint.x
                  - (radiusX * Math.cos(t) * cosTheta)
                  + (radiusY * Math.sin(t) * sinTheta));
      float y =
          (float)
              (centerPoint.y
                  - (radiusX * Math.cos(t) * sinTheta)
                  - (radiusY * Math.sin(t) * cosTheta));

      verts[i] =
          new Vertex(
              x,
              y,
              centerPoint.z,
              fillColor.getR(),
              fillColor.getG(),
              fillColor.getB(),
              fillColor.getAlpha());
    }
    verts[verts.length - 1] = verts[0];
    //		System.out.println("Points: " + verts.length);

    // Create tex coords
    float width = radiusX * 2;
    float height = radiusY * 2;
    float upperLeftX = centerPoint.x - radiusX;
    float upperLeftY = centerPoint.y - radiusY;
    for (int i = 0; i < verts.length; i++) {
      Vertex vertex = verts[i];
      vertex.setTexCoordU((vertex.x - upperLeftX) / width);
      vertex.setTexCoordV((vertex.y - upperLeftY) / height);
      // System.out.println("TexU:" + vertex.getTexCoordU() + " TexV:" + vertex.getTexCoordV());
    }

    return verts;
  }
  /**
   * Static method for computing vertices of an annular segment.
   *
   * @param center
   * @param innerRadius
   * @param outerRadius
   * @param startAngle
   * @param endAngle
   * @param segments
   * @return
   */
  public static Vertex[] computeVertices(
      Vector3D center,
      float innerRadius,
      float outerRadius,
      float startAngle,
      float endAngle,
      int segments) {

    if (segments <= 0) {
      throw new IllegalArgumentException("segments must be positive: " + segments);
    }

    List<Vertex> vertices = new ArrayList<Vertex>();

    final float rmin = Math.min(innerRadius, outerRadius);
    final float rmax = Math.max(innerRadius, outerRadius);

    startAngle = fixAngle(startAngle);
    endAngle = fixAngle(endAngle);

    final float startRadians = (float) Math.toRadians(startAngle);
    final float endRadians = (float) Math.toRadians(endAngle);

    float arc = (float) Math.toRadians(computeArcDegrees(startAngle, endAngle));

    segments = (int) Math.round(segments * arc / (Math.PI / 2.0));
    if (segments == 0) {
      segments = 1;
    }

    final float radInc = arc / segments;

    float currentRadians = endRadians;

    float minX = Float.MAX_VALUE;
    float maxX = 0f;
    float minY = Float.MAX_VALUE;
    float maxY = 0f;

    for (int i = 0; i <= segments; i++, currentRadians -= radInc) {
      float x = -rmax * (float) Math.cos(currentRadians) + center.x;
      float y = -rmax * (float) Math.sin(currentRadians) + center.y;
      if (x < minX) minX = x;
      if (x > maxX) maxX = x;
      if (y < minY) minY = y;
      if (y > maxY) maxY = y;
      vertices.add(new Vertex(x, y, center.z));
    }

    currentRadians = startRadians;

    for (int i = 0; i <= segments; i++, currentRadians += radInc) {
      float x = -rmin * (float) Math.cos(currentRadians) + center.x;
      float y = -rmin * (float) Math.sin(currentRadians) + center.y;
      if (x < minX) minX = x;
      if (x > maxX) maxX = x;
      if (y < minY) minY = y;
      if (y > maxY) maxY = y;
      vertices.add(new Vertex(x, y, center.z));
    }

    Vertex[] varray = vertices.toArray(new Vertex[vertices.size()]);

    float xRange = maxX - minX;
    float yRange = maxY - minY;

    // Set the texture X and Y
    for (Vertex v : varray) {
      float texU = 0f, texV = 0f;
      if (xRange > 0) {
        texU = (v.x - minX) / xRange;
      }
      if (yRange > 0) {
        texV = (v.y - minY) / yRange;
      }
      v.setTexCoordU(texU);
      v.setTexCoordV(texV);
    }

    return varray;
  }
    /**
     * Uses the faces attached to this group during the parsing process and the lists with all
     * vertex and all texture coordinates of the obj file to create arrays for this group with
     * vertices and texture coords that only belong to this single group.
     *
     * @param allFileVerts
     * @param allTexCoords
     */
    public void compileItsOwnLists(Vertex[] allFileVerts, float[][] allTexCoords) {
      indexArray = new int[faces.size() * 3];

      if (allTexCoords.length > 0) {
        texCoordIndexArray = new int[faces.size() * 3];
      }

      for (int i = 0; i < faces.size(); i++) {
        AFace currentFace = faces.get(i);

        Vertex v0 = allFileVerts[currentFace.p0];
        Vertex v1 = allFileVerts[currentFace.p1];
        Vertex v2 = allFileVerts[currentFace.p2];

        if (allTexCoords.length > currentFace.t0
            && allTexCoords.length > currentFace.t1
            && allTexCoords.length > currentFace.t2) {
          float[] texV0 = allTexCoords[currentFace.t0];
          float[] texV1 = allTexCoords[currentFace.t1];
          float[] texV2 = allTexCoords[currentFace.t2];

          // Etwas redundant immer wieder zu machen beim gleichen vertex..whatever
          v0.setTexCoordU(texV0[0]);
          v0.setTexCoordV(texV0[1]);

          v1.setTexCoordU(texV1[0]);
          v1.setTexCoordV(texV1[1]);

          v2.setTexCoordU(texV2[0]);
          v2.setTexCoordV(texV2[1]);

          // Check if there is a texture index in the hashtable at the faces texture pointer
          // if not, create a new index = the end of thexcoords list, and put the pointer into the
          // hash
          // if yes, point the faces texture pointer to the pointer in the hash

          // This process maps the texture coords and indices of all the groups in the obj
          // file to only this groups texture coord list and texture indices, the indices
          // are created from the index in the thex coord list when they are put in
          // Only the texture coordinates are added to the list that have not been adressed
          // in the texture indices pointers in the faces
          // Same texture pointers will point to the same texcoord in the list
          Integer oldToNewT0 = oldTexIndexToNewTexIndex.get(currentFace.t0);
          if (oldToNewT0 != null) {
            currentFace.t0 = oldToNewT0;
          } else {
            int newIndex = texCoordsForGroup.size();
            texCoordsForGroup.add(texV0);
            oldTexIndexToNewTexIndex.put(currentFace.t0, newIndex);
            currentFace.t0 = newIndex;
          }

          Integer oldToNewT1 = oldTexIndexToNewTexIndex.get(currentFace.t1);
          if (oldToNewT1 != null) {
            currentFace.t1 = oldToNewT1;
          } else {
            int newIndex = texCoordsForGroup.size();
            texCoordsForGroup.add(texV1);
            oldTexIndexToNewTexIndex.put(currentFace.t1, newIndex);
            currentFace.t1 = newIndex;
          }

          Integer oldToNewT2 = oldTexIndexToNewTexIndex.get(currentFace.t2);
          if (oldToNewT2 != null) {
            currentFace.t2 = oldToNewT2;
          } else {
            int newIndex = texCoordsForGroup.size();
            texCoordsForGroup.add(texV2);
            oldTexIndexToNewTexIndex.put(currentFace.t2, newIndex);
            currentFace.t2 = newIndex;
          }
        }

        // Do the same for the vertices.
        // Create a new vertex pointer when adding the vertex to the list
        Integer oldToNewP0 = oldIndexToNewIndex.get(currentFace.p0);
        if (oldToNewP0 != null) {
          // index of the old vertex list has already been mapped to a new one here -> use the new
          // index in the face
          currentFace.p0 = oldToNewP0;
        } else {
          int newIndex = verticesForGroup.size();
          verticesForGroup.add(v0);
          // mark that the former index (for exmample 323) is now at new index (f.e. 1)
          oldIndexToNewIndex.put(currentFace.p0, newIndex);
          currentFace.p0 = newIndex;
        }

        Integer oldToNewP1 = oldIndexToNewIndex.get(currentFace.p1);
        if (oldToNewP1 != null) {
          currentFace.p1 = oldToNewP1;
        } else {
          int newIndex = verticesForGroup.size();
          verticesForGroup.add(v1);
          oldIndexToNewIndex.put(currentFace.p1, newIndex);
          currentFace.p1 = newIndex;
        }

        Integer oldToNewP2 = oldIndexToNewIndex.get(currentFace.p2);
        if (oldToNewP2 != null) {
          currentFace.p2 = oldToNewP2;
        } else {
          int newIndex = verticesForGroup.size();
          verticesForGroup.add(v2);
          oldIndexToNewIndex.put(currentFace.p2, newIndex);
          currentFace.p2 = newIndex;
        }

        indexArray[i * 3] = currentFace.p0;
        indexArray[i * 3 + 1] = currentFace.p1;
        indexArray[i * 3 + 2] = currentFace.p2;

        if (allTexCoords.length > 0) {
          texCoordIndexArray[i * 3] = currentFace.t0;
          texCoordIndexArray[i * 3 + 1] = currentFace.t1;
          texCoordIndexArray[i * 3 + 2] = currentFace.t2;
        }
      }
    }
Exemple #4
0
  /**
   * Gets the vertices.
   *
   * @param resolution the resolution
   * @return the vertices
   */
  protected Vertex[] getVertices(int resolution) {
    Vertex[] verts;
    if (degrees < (float) Math.toRadians(360) && arcMode == pie) verts = new Vertex[resolution + 2];
    else verts = new Vertex[resolution + 1];

    float t;
    float inc = degrees / (float) resolution;

    double cosTheta = Math.cos(theta);
    double sinTheta = Math.sin(theta);

    MTColor fillColor = this.getFillColor();

    for (int i = 0; i < resolution; i++) {
      t = 0 + (i * inc);
      //			float x = (float) (centerPoint.x + (radiusX * Math.cos(t) * cosTheta) //TODO remove theta
      // stuff? oder enablen als parameter?
      //						- (radiusY * Math.sin(t) * sinTheta) );
      //			float y = (float) (centerPoint.y + (radiusX * Math.cos(t) * sinTheta)
      //						+ (radiusY * Math.sin(t) * cosTheta) );
      float x =
          (float)
              (centerPoint.x
                  - (radiusX * Math.cos(t) * cosTheta)
                  + (radiusY * Math.sin(t) * sinTheta));
      float y =
          (float)
              (centerPoint.y
                  - (radiusX * Math.cos(t) * sinTheta)
                  - (radiusY * Math.sin(t) * cosTheta));

      verts[i] =
          new Vertex(
              x,
              y,
              centerPoint.z,
              fillColor.getR(),
              fillColor.getG(),
              fillColor.getB(),
              fillColor.getAlpha());
    }
    if (degrees < (float) Math.toRadians(360) && arcMode == pie)
      verts[verts.length - 2] =
          new Vertex(
              centerPoint.x,
              centerPoint.y,
              centerPoint.z,
              fillColor.getR(),
              fillColor.getG(),
              fillColor.getB(),
              fillColor.getAlpha());
    verts[verts.length - 1] =
        (Vertex)
            verts[0]
                .getCopy(); // NEED TO USE COPY BECAUSE TEX COORDS MAY GET SCALED DOUBLE IF SAME
                            // VERTEX OBJECT!
    //		System.out.println("Points: " + verts.length);

    // Create tex coords
    float width = radiusX * 2;
    float height = radiusY * 2;
    float upperLeftX = centerPoint.x - radiusX;
    float upperLeftY = centerPoint.y - radiusY;
    for (int i = 0; i < verts.length; i++) {
      Vertex vertex = verts[i];
      vertex.setTexCoordU((vertex.x - upperLeftX) / width);
      vertex.setTexCoordV((vertex.y - upperLeftY) / height);
      //			System.out.println("TexU:" + vertex.getTexCoordU() + " TexV:" + vertex.getTexCoordV());
    }

    return verts;
  }