Skip to content

Commit

Permalink
use affine transformer to compute envelope of implicit geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
clausnagel committed Jan 1, 2025
1 parent 0921ac1 commit fce9a91
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import org.citydb.model.common.*;
import org.citydb.model.property.AppearanceProperty;
import org.citydb.model.util.AffineTransformer;
import org.citydb.model.util.matrix.Matrix;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -117,40 +119,31 @@ public ImplicitGeometry addAppearance(AppearanceProperty appearance) {
}

public Envelope getEnvelope(List<Double> transformationMatrix, Point referencePoint) {
if (geometry != null
&& transformationMatrix != null
if (transformationMatrix != null
&& transformationMatrix.size() > 15
&& referencePoint != null) {
double[][] matrix = new double[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
matrix[i][j] = transformationMatrix.get(i * 4 + j);
}
Envelope envelope;
if (geometry != null) {
envelope = AffineTransformer.of(new Matrix(transformationMatrix.subList(0, 16), 4)
.plusEquals(new Matrix(4, 4)
.set(0, 3, referencePoint.getCoordinate().getX())
.set(1, 3, referencePoint.getCoordinate().getY())
.set(2, 3, referencePoint.getCoordinate().getZ())))
.transform(geometry.getEnvelope());
} else {
envelope = Envelope.empty().include(Point.of(Coordinate.of(
referencePoint.getCoordinate().getX() + transformationMatrix.get(3),
referencePoint.getCoordinate().getY() + transformationMatrix.get(7),
referencePoint.getCoordinate().getZ() + transformationMatrix.get(11))));
}

matrix[0][3] += referencePoint.getCoordinate().getX();
matrix[1][3] += referencePoint.getCoordinate().getY();
matrix[2][3] += referencePoint.getCoordinate().getZ();

Envelope template = geometry.getEnvelope();
return Envelope.of(
multiply(matrix, template.getLowerCorner()),
multiply(matrix, template.getUpperCorner()))
.setSRID(referencePoint.getSRID().orElse(null))
return envelope.setSRID(referencePoint.getSRID().orElse(null))
.setSrsIdentifier(referencePoint.getSrsIdentifier().orElse(null));
} else {
return null;
}
}

private Coordinate multiply(double[][] matrix, Coordinate coordinate) {
double[] v = new double[]{coordinate.getX(), coordinate.getY(), coordinate.getZ(), 1};
return Coordinate.of(
matrix[0][0] * v[0] + matrix[0][1] * v[1] + matrix[0][2] * v[2] + matrix[0][3] * v[3],
matrix[1][0] * v[0] + matrix[1][1] * v[1] + matrix[1][2] * v[2] + matrix[1][3] * v[3],
matrix[2][0] * v[0] + matrix[2][1] * v[1] + matrix[2][2] * v[2] + matrix[2][3] * v[3]);
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static AffineTransformer ofRowMajor(List<Double> values) {
return ofRowMajor(values, 4);
}

public void transform(Coordinate coordinate) {
public Coordinate transform(Coordinate coordinate) {
Matrix transformed = matrix.times(new Matrix(new double[][]{
{coordinate.getX()},
{coordinate.getY()},
Expand All @@ -76,21 +76,27 @@ public void transform(Coordinate coordinate) {
if (coordinate.getDimension() == 3) {
coordinate.setZ(transformed.get(2, 0));
}

return coordinate;
}

public void transform(Feature feature) {
public Feature transform(Feature feature) {
feature.accept(processor);
return feature;
}

public void transform(Geometry<?> geometry) {
public Geometry<?> transform(Geometry<?> geometry) {
geometry.accept(processor);
return geometry;
}

public void transform(Envelope envelope) {
public Envelope transform(Envelope envelope) {
if (!envelope.isEmpty()) {
transform(envelope.getLowerCorner());
transform(envelope.getUpperCorner());
}

return envelope;
}

private class Processor extends ModelWalker {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private Envelope computeEnvelope(Feature feature, Map<String, ImplicitGeometry>
List<Double> transformationMatrix = property.getTransformationMatrix().orElse(null);
Point referencePoint = property.getReferencePoint().orElse(null);
if (transformationMatrix != null
&& transformationMatrix.size() > 11
&& transformationMatrix.size() > 15
&& referencePoint != null) {
ImplicitGeometry geometry = property.getObject().orElse(
implicitGeometries.get(property.getReference()
Expand Down

0 comments on commit fce9a91

Please sign in to comment.