| 829 | |
| 830 | @utils.trace |
| 831 | def apply_mapping(self, data, mapping, from_disk=False): |
| 832 | if from_disk: |
| 833 | by_id = mapping |
| 834 | else: |
| 835 | by_id = defaultdict(lambda: (list(), list())) |
| 836 | for a, b, idxs in mapping: |
| 837 | fr, to = (" ".join(map(lambda n: n.to_string(), utils.to_tuple(x))) for x in (a,b)) |
| 838 | if fr == to: |
| 839 | continue |
| 840 | for idx in idxs: |
| 841 | by_id[idx][0].append(a) |
| 842 | by_id[idx][1].append(b) |
| 843 | |
| 844 | if self.settings.store_mapping: |
| 845 | # can be used to store global mapping and apply to individually extracted elements |
| 846 | mapping = defaultdict(list) |
| 847 | for k, vs in by_id.items(): |
| 848 | guid = data.convex_halfspace_trees[k][0].GlobalId |
| 849 | for ab in zip(*vs): |
| 850 | from_to = tuple(" ".join(map(lambda n: n.to_string(), utils.to_tuple(x))) for x in ab) |
| 851 | mapping[guid].append(from_to) |
| 852 | json.dump(mapping, open('epeck_mapping.json', 'w')) |
| 853 | |
| 854 | for i, (elem, ps) in enumerate(data.convex_halfspace_trees): |
| 855 | |
| 856 | if from_disk: |
| 857 | maps = by_id[elem.GlobalId] |
| 858 | else: |
| 859 | maps = by_id[i] |
| 860 | |
| 861 | for j, p in enumerate(ps): |
| 862 | |
| 863 | if self.settings.verbose: |
| 864 | pps = p.solid() |
| 865 | old_area = pps.area().to_double() |
| 866 | old_volume = pps.volume().to_double() |
| 867 | open(f'{i}_{j}_before.obj', 'w').write(pps.serialize_obj()) |
| 868 | |
| 869 | p.map(*maps) |
| 870 | |
| 871 | if self.settings.verbose: |
| 872 | for ab in zip(*maps): |
| 873 | c, d = map(utils.to_double, ab) |
| 874 | print(*c, '->', *d) |
| 875 | c, d = map(to_str, ab) |
| 876 | print(*c, '->', *d) |
| 877 | |
| 878 | pps = p.solid() |
| 879 | new_area = pps.area().to_double() |
| 880 | new_volume = pps.volume().to_double() |
| 881 | open(f'{i}_{j}_after.obj', 'w').write(pps.serialize_obj()) |
| 882 | if new_area: |
| 883 | print(i, j, new_area / old_area, old_area, new_area, old_volume, new_volume) |
| 884 | |
| 885 | if new_area / old_area > 100: |
| 886 | breakpoint() |
| 887 | |
| 888 | @staticmethod |