Search in sources :

Example 1 with Pair

use of org.concord.energy3d.util.Pair in project energy3d by concord-consortium.

the class Scene method groupFoundations.

/* put overlapping foundations into separate groups */
private void groupFoundations() {
    if (foundationGroups == null) {
        foundationGroups = new ArrayList<List<Foundation>>();
    } else {
        foundationGroups.clear();
    }
    final List<Foundation> foundations = new ArrayList<Foundation>();
    for (final HousePart p : parts) {
        if (p instanceof Foundation) {
            foundations.add((Foundation) p);
        }
    }
    final int n = foundations.size();
    if (n > 1) {
        int count = 0;
        final List<Pair> pairs = new ArrayList<Pair>();
        for (int i = 0; i < n - 1; i++) {
            final Foundation fi = foundations.get(i);
            for (int j = i + 1; j < n; j++) {
                final Foundation fj = foundations.get(j);
                if (fi.overlap(fj)) {
                    final Pair p = new Pair(i, j);
                    if (!pairs.contains(p)) {
                        pairs.add(p);
                    }
                }
            }
        }
        int m = pairs.size();
        while (m > 0) {
            Pair p = pairs.get(0);
            final List<Integer> list = new ArrayList<Integer>();
            list.add(p.i());
            list.add(p.j());
            pairs.remove(p);
            m = pairs.size();
            if (m > 0) {
                final List<Pair> toRemove = new ArrayList<Pair>();
                for (int x = 0; x < m; x++) {
                    p = pairs.get(x);
                    final int i = p.i();
                    final int j = p.j();
                    if (list.contains(i) && list.contains(j)) {
                        if (!toRemove.contains(p)) {
                            toRemove.add(p);
                        }
                    } else if (!list.contains(i) && list.contains(j)) {
                        list.add(i);
                        if (!toRemove.contains(p)) {
                            toRemove.add(p);
                        }
                    } else if (!list.contains(j) && list.contains(i)) {
                        list.add(j);
                        if (!toRemove.contains(p)) {
                            toRemove.add(p);
                        }
                    }
                }
                if (!toRemove.isEmpty()) {
                    pairs.removeAll(toRemove);
                }
            }
            final List<Foundation> group = new ArrayList<Foundation>();
            for (final Integer a : list) {
                group.add(foundations.get(a));
            }
            foundationGroups.add(group);
            count += group.size();
            m = pairs.size();
        }
        if (count < foundations.size()) {
            for (final Foundation f : foundations) {
                boolean linked = false;
                for (final List<Foundation> g : foundationGroups) {
                    if (g.contains(f)) {
                        linked = true;
                        break;
                    }
                }
                if (!linked) {
                    final List<Foundation> g = new ArrayList<Foundation>();
                    g.add(f);
                    foundationGroups.add(g);
                }
            }
        }
    // System.out.println("###" + foundationGroups.size() + "," + (foundations.size() - count));
    } else {
        foundationGroups.add(foundations);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Foundation(org.concord.energy3d.model.Foundation) HousePart(org.concord.energy3d.model.HousePart) Pair(org.concord.energy3d.util.Pair)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 Foundation (org.concord.energy3d.model.Foundation)1 HousePart (org.concord.energy3d.model.HousePart)1 Pair (org.concord.energy3d.util.Pair)1