Ejemplo n.º 1
0
  /**
   * The default constructor.
   *
   * @param edges
   *     <p>A collection of edges connecting a collection of vertices.
   * @param vertices
   *     <p>A collection of vertices connected by a collection of edges.
   */
  public Polygon(ArrayList<Edge> edges, ArrayList<Vertex> vertices) {

    // Initialize the defaults.
    this();

    // A Polygon has the following requirements:
    // Edge i must have as its start and end points vertices i and i+1.
    // (for the last edge, these are vertices i and 0).
    // The Vertex references stored in the Edge should be the same as stored
    // in the Polygon's list of vertices.

    // This does not check for simple/complex polygons, only that the outer
    // edges form a cycle.

    // Check the parameters for null values.
    if (vertices == null || vertices.contains(null) || vertices.size() < 3) {
      throw new IllegalArgumentException(
          "Polygon error: The vertex list must be a non-null list with at least 3 vertices.");
    } else if (edges == null || edges.contains(null) || edges.size() < 3) {
      throw new IllegalArgumentException(
          "Polygon error: The edge list must be a non-null list with at least 3 edges.");
    }
    // Get the sizes of the two lists.
    int size = vertices.size();

    // Use a Set of vertex IDs to make sure no vertex connects more than two
    // edges.
    TreeSet<Integer> vertexIds = new TreeSet<Integer>();
    TreeSet<Integer> edgeIds = new TreeSet<Integer>();

    // Make sure the edges form a cycle.
    for (int i = 0, j = 1; i < size; i++, j = (i + 1) % size) {
      int v1 = vertices.get(i).getId();
      int v2 = vertices.get(j).getId();
      int[] ids = edges.get(i).getVertexIds();

      // Add the current vertex ID to the Set.
      vertexIds.add(v1);

      // Make sure the two adjacent vertex IDs are not the same.
      if (v1 == v2) {
        throw new IllegalArgumentException("Polygon error: Same ID for adjacent vertices.");
      }
      // Make sure the IDs v1 and v2 are both in the edge's vertex ID
      // list.
      else if ((v1 != ids[0] && v1 != ids[1]) || (v2 != ids[0] && v2 != ids[1])) {
        throw new IllegalArgumentException("Polygon error: Edge " + i + " uses unknown vertex.");
      }
      edgeIds.add(edges.get(i).getId());
    }

    // Check the size of the Set of vertex IDs. Every ID should be unique!
    if (vertexIds.size() < size) {
      throw new IllegalArgumentException("Polygon error: A vertex connects more than two edges.");
    } else if (edgeIds.size() < size) {
      throw new IllegalArgumentException("Polygon error: Same edge ID used more than once.");
    }
    // If the vertices/edges supplied are valid, duplicate them. We also
    // must ensure the edges are registered with the correct vertex
    // instances.
    for (int i = 0; i < size; i++) {
      // Get the edge from the supplied list.
      Edge edge = edges.get(i);

      // Get the new start and end vertices for this edge.
      Vertex start = vertices.get(i);
      Vertex end = vertices.get((i + 1) % size);

      // Register the clone with the start and end.
      start.register(edge);
      end.register(edge);

      // Send the start and end vertices to the clone.
      edge.update(start);
      edge.update(end);

      // Add the edge and vertex to the Polygon's lists.
      this.edges.add(edge);
      this.vertices.add(start);

      // Create an entry for the edge in the map of edge properties.
      EdgeProperties properties = new EdgeProperties();
      edgeProperties.put(edge.getId(), properties);
      // Register with all of the boundary conditions in the properties.
      properties.getFluidBoundaryCondition().register(this);
      properties.getThermalBoundaryCondition().register(this);
      for (BoundaryCondition condition : properties.getOtherBoundaryConditions()) {
        condition.register(this);
      }
    }

    return;
  }
Ejemplo n.º 2
0
  /**
   * This operation copies the contents of a Polygon into the current object using a deep copy.
   *
   * @param polygon
   *     <p>The Object from which the values should be copied.
   */
  public void copy(Polygon polygon) {

    // Check the parameters.
    if (polygon == null) {
      return;
    }
    // Copy the super's data.
    super.copy(polygon);

    // Deep copy the vertices.
    vertices.clear();
    for (Vertex vertex : polygon.getVertices()) {
      vertices.add((Vertex) vertex.clone());
    }

    // Deep copy the edges.
    edges.clear();
    ArrayList<Edge> otherEdges = polygon.getEdges();
    int size = otherEdges.size();
    for (int i = 0; i < size; i++) {
      // Clone the edge. This deep copies the other edge's vertices, so we
      // must hook the clone up to our vertex instances.
      Edge clone = (Edge) otherEdges.get(i).clone();

      // Add the clone to the list of edges.
      edges.add(clone);

      // Get the new start and end vertices for this edge.
      Vertex start = vertices.get(i);
      Vertex end = vertices.get((i + 1) % size);

      // Register the clone with the start and end.
      start.register(clone);
      end.register(clone);

      // Send the start and end vertices to the clone.
      clone.update(start);
      clone.update(end);
    }

    /* ---- Deep copy the edge properties. ---- */
    // Unregister from any of the current boundary conditions.
    for (Entry<Integer, EdgeProperties> entry : edgeProperties.entrySet()) {
      EdgeProperties properties = entry.getValue();
      // Unregister from the fluid and thermal boundary conditions.
      properties.getFluidBoundaryCondition().unregister(this);
      properties.getThermalBoundaryCondition().unregister(this);
      // Unregister from all passive scalar boundary conditions.
      for (BoundaryCondition condition : properties.getOtherBoundaryConditions()) {
        condition.unregister(this);
      }
    }

    // Clone all of the edge properties from the other polygon and register
    // with their boundary conditions.
    for (Entry<Integer, EdgeProperties> entry : polygon.edgeProperties.entrySet()) {
      // Clone and add the edge's properties.
      EdgeProperties properties = (EdgeProperties) entry.getValue().clone();
      edgeProperties.put(entry.getKey(), properties);

      // Unregister from the fluid and thermal boundary conditions.
      properties.getFluidBoundaryCondition().register(this);
      properties.getThermalBoundaryCondition().register(this);
      // Unregister from all passive scalar boundary conditions.
      for (BoundaryCondition condition : properties.getOtherBoundaryConditions()) {
        condition.register(this);
      }
    }
    /* ---------------------------------------- */

    // Copy the PolygonProperties
    polygonProperties = (PolygonProperties) polygon.getPolygonProperties().clone();

    return;
  }