]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.region/src/org/simantics/district/region/DiagramRegions.java
98a3ea4f47aa7d78df6a4e0f864f5ccc58e082f9
[simantics/district.git] / org.simantics.district.region / src / org / simantics / district / region / DiagramRegions.java
1 package org.simantics.district.region;
2
3 import java.awt.Shape;
4 import java.awt.geom.FlatteningPathIterator;
5 import java.awt.geom.Path2D;
6 import java.awt.geom.PathIterator;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collection;
10 import java.util.HashSet;
11 import java.util.List;
12 import java.util.Set;
13 import java.util.UUID;
14
15 import org.simantics.Simantics;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.request.UniqueRead;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.QueryIndexUtils;
23 import org.simantics.db.layer0.request.PossibleActiveModel;
24 import org.simantics.db.layer0.util.RemoverUtil;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.district.region.ontology.DiagramRegionsResource;
27 import org.simantics.layer0.Layer0;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public final class DiagramRegions {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(DiagramRegions.class);
34
35     public static Resource createRegionForDiagram(WriteGraph graph, Resource diagram, String label, double[] coordinates) throws DatabaseException {
36         if (coordinates == null || coordinates.length < 6 || coordinates.length % 2 != 0)
37             throw new IllegalArgumentException("Provided coordinates are not valid : " + Arrays.toString(coordinates));
38         Path2D path = new Path2D.Double();
39         double startX = coordinates[0];
40         double startY = coordinates[1];
41         path.moveTo(startX, startY);
42         for (int i = 2; i < coordinates.length; i++)
43             path.lineTo(coordinates[i], coordinates[++i]);
44
45         path.closePath(); // might not be needed
46         DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
47         Layer0 L0 = Layer0.getInstance(graph);
48         Resource region = graph.newResource();
49         graph.claim(region, L0.InstanceOf, DRR.Region);
50         graph.claim(diagram, DRR.hasRegion, region);
51         graph.claim(region, DRR.regionOf, diagram);
52         graph.claimLiteral(region, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);
53         graph.claimLiteral(region, L0.HasLabel, label, Bindings.STRING);
54         graph.claimLiteral(region, DRR.Region_area, coordinates, Bindings.DOUBLE_ARRAY);
55         if (LOGGER.isDebugEnabled())
56             LOGGER.debug("Created region for diagram {} with label {} and area {}", diagram, label, Arrays.toString(coordinates));
57         return region;
58     }
59
60     public static void removeRegion(WriteGraph graph, Resource region) throws DatabaseException {
61         RemoverUtil.remove(graph, region);
62     }
63
64     public static class DiagramRegion {
65         
66         private Resource resource;
67         private String label;
68         private Shape shape;
69         
70         public DiagramRegion(Resource resource, String label, Shape coordinatesToShape) {
71             this.resource = resource;
72             this.label = label;
73             this.shape = coordinatesToShape;
74         }
75         
76         public Resource getResource() {
77             return resource;
78         }
79         
80         public String getLabel() {
81             return label;
82         }
83         
84         public Shape getShape() {
85             return shape;
86         }
87     }
88     
89     public static Collection<DiagramRegion> findRegions(ReadGraph graph, Resource model) throws DatabaseException {
90         List<Resource> diagrams = QueryIndexUtils.searchByType(graph, model, DiagramResource.getInstance(graph).Diagram);
91         Set<DiagramRegion> regions = new HashSet<>();
92         
93         Layer0 L0 = Layer0.getInstance(graph);
94         
95         for (Resource diagram : diagrams) {
96             Collection<Resource> region = graph.getObjects(diagram, DiagramRegionsResource.getInstance(graph).hasRegion);
97             for (Resource r : region) {
98                 String label = graph.getRelatedValue(r, L0.HasLabel, Bindings.STRING);
99                 Shape coordinatesToShape = DiagramRegions.coordinatesToShape(graph, r);
100                 regions.add(new DiagramRegion(r, label, coordinatesToShape));
101             }
102         }
103         return regions;
104     }
105     
106     public static class DiagramRegionsRequest extends UniqueRead<Collection<DiagramRegion>> {
107
108         @Override
109         public Collection<DiagramRegion> perform(ReadGraph graph) throws DatabaseException {
110             return findRegions(graph, graph.syncRequest(new PossibleActiveModel(Simantics.getProjectResource())));
111         }
112     }
113
114     public static Shape coordinatesToShape(ReadGraph graph, Resource region) throws DatabaseException {
115         DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
116         double[] coordinates = graph.getRelatedValue(region, DRR.Region_area, Bindings.DOUBLE_ARRAY);
117         Path2D path = new Path2D.Double();
118         double startX = coordinates[0];
119         double startY = coordinates[1];
120         path.moveTo(startX, startY);
121         for (int i = 2; i < coordinates.length; i++)
122             path.lineTo(coordinates[i], coordinates[++i]);
123
124         path.closePath(); // might not be needed
125         return path;
126     }
127
128     public static double[] shapeToCoordinates(Shape shape) {
129         PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), 1);
130         double points[] = new double[6];
131         double moveX = 0, moveY = 0;
132         double thisX = 0, thisY = 0;
133         int type = 0;
134
135         List<Double> coords = new ArrayList<>();
136         // float factor = 1;
137
138         while (!it.isDone()) {
139             type = it.currentSegment(points);
140             switch (type) {
141             case PathIterator.SEG_MOVETO:
142                 moveX = points[0];
143                 moveY = points[1];
144                 coords.add(moveX);
145                 coords.add(moveY);
146                 break;
147
148             case PathIterator.SEG_CLOSE:
149                 points[0] = moveX;
150                 points[1] = moveY;
151                 // Fall into....
152                 break;
153             case PathIterator.SEG_LINETO:
154                 thisX = points[0];
155                 thisY = points[1];
156                 coords.add(thisX);
157                 coords.add(thisY);
158                 break;
159             }
160             it.next();
161         }
162         double[] finalCoords = new double[coords.size()];
163         for (int i = 0; i < coords.size(); i++) {
164             double d = coords.get(i);
165             finalCoords[i] = d;
166         }
167         return finalCoords;
168     }
169
170     public static Resource getDiagramForRegion(ReadGraph graph, Resource region) throws DatabaseException {
171         DiagramRegionsResource DRR = DiagramRegionsResource.getInstance(graph);
172         Resource diagram = graph.getSingleObject(region, DRR.regionOf);
173         return diagram;
174     }
175 }