Commit 66fc5807 authored by Thilo Rörig's avatar Thilo Rörig

Refactoring VectorFieldVisualizer

successfully removed code duplication
parent 5f325e08
......@@ -223,8 +223,6 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
initAppearance();
sceneGraphComponent.setAppearance(appearance);
coordinateAndIndexFactory = new VectorCoordinateAndIndexFactory();
coordinateAndIndexFactory.setAdapter(source);
}
@Override
......@@ -239,13 +237,13 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
AdapterSet aSet = getLayer().getEffectiveAdapters();
double meanEdgeLength = GeometryUtility.getMeanEdgeLength(hds, aSet);
// if (nodes.size() == 0) {
// IndexedLineSetFactory ilf = new IndexedLineSetFactory();
// ilf.update();
// ils = ilf.getIndexedLineSet();
// }
List<? extends Node> nodes = getNodes(hds);
coordinateAndIndexFactory = createCoordinateAndIndexFactory(nodes, aSet);
coordinateAndIndexFactory.setMeanEdgeLength(meanEdgeLength);
coordinateAndIndexFactory.updateCoordinatesAndIndices(getNodes(hds), aSet);
coordinateAndIndexFactory.updateCoordinatesAndIndices(nodes, aSet);
List<double[]> vertexCoordinates = coordinateAndIndexFactory.vertexCoordinates;
List<int[]> edgeIndices = coordinateAndIndexFactory.edgeIndices;
IndexedLineSet ils = lineSetCreator.createLineSet(vertexCoordinates, edgeIndices);
......@@ -255,6 +253,24 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
sceneGraphComponent.setVisible(true);
}
private <N extends Node> AbstractCoordinateAndIndexFactory createCoordinateAndIndexFactory(List<N> nodes, AdapterSet aSet) {
Object val = null;
Adapter<?> sourceAdapter = getSource();
for (N node : nodes) {
val = sourceAdapter.get(node, aSet);
if (val != null) break;
}
if (val instanceof double[]) {
coordinateAndIndexFactory = new VectorCoordinateAndIndexFactory();
} else if (val instanceof double[][]) {
coordinateAndIndexFactory = new MultiVectorCoordinateAndIndexFactory();
}
coordinateAndIndexFactory.setAdapter(getSource());
return coordinateAndIndexFactory;
}
private List<? extends Node> getNodes(HalfEdgeDataStructure<?, ?, ?> hds) {
List<? extends Node> nodes;
......@@ -312,7 +328,6 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
public void setDirected(boolean directed) {
this.directed = directed;
}
......@@ -334,13 +349,9 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
if (nodes.isEmpty()) {
return;
}
// COORDTYPE val = null;
// for (N node : nodes) {
// val = adapter.get(node, aSet);
// if (val != null) break;
// }
int numNullValue = getVectors(nodes, aSet);
if(numNullValue > 0) {
if (numNullValue > 0) {
System.err.println("Vector field adapter " + adapter + " has " + numNullValue + " null entries");
}
}
......@@ -359,12 +370,46 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
public void setMeanEdgeLength(double meanEdgeLength) {
this.meanEdgeLength = meanEdgeLength;
}
protected void add3DVector(double[] v, double[] p) throws NullPointerException {
double[] v3D = convertTo3DVector(v);
if (normalized) {
Rn.normalize(v3D, v3D);
Rn.times(v3D, meanEdgeLength, v3D);
}
if (centered) {
Rn.times(v3D, scale / 2., v3D);
vertexCoordinates.add(Rn.add(null, p, v3D));
Rn.times(v3D, -1, v3D);
vertexCoordinates.add(Rn.add(null, p, v3D));
} else {
Rn.times(v3D, scale / 2., v3D);
vertexCoordinates.add(Rn.add(null, p, v3D));
vertexCoordinates.add(p);
}
}
protected <V extends Vertex<V, E, F>,
E extends Edge<V, E, F>,
F extends Face<V, E, F>, N extends Node<V, E, F>>
double[] convertTo3DVector(double[] v) throws NullPointerException {
double[] vector3D = new double[3];
if (v.length == 3) {
System.arraycopy(v, 0, vector3D, 0, 3);
}
if (v.length == 4) {
vector3D = Pn.dehomogenize(null, v);
} else if (v.length != 3) {
throw new RuntimeException(
"Adapter does not return vectors in 3-space or homogeneous 4-space.");
}
return vector3D;
}
}
class MultiVectorCoordinateAndIndexFactory extends AbstractCoordinateAndIndexFactory<double[][]> {
<
V extends Vertex<V, E, F>,
<V extends Vertex<V, E, F>,
E extends Edge<V, E, F>,
F extends Face<V, E, F>,
N extends Node<V, E, F>
......@@ -373,36 +418,18 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
int numNullValues = 0;
for (N node : nodes) {
double[][] v = adapter.get(node, aSet);
if (v == null) {
numNullValues++;
} else {
try {
for (int i = 0; i < v.length; i++) {
if (v[i].length != 3)
throw new RuntimeException(
"Adapter does not return vectors in 3-space.");
v[i] = v[i].clone();
double[] p = aSet.getD(BaryCenter3d.class, node);
if (normalized) {
Rn.normalize(v[i], v[i]);
Rn.times(v[i], meanEdgeLength, v[i]);
}
if (centered) {
Rn.times(v[i], scale / 2., v[i]);
vertexCoordinates.add(Rn.add(null, p, v[i]));
Rn.times(v[i], -1, v[i]);
vertexCoordinates.add(Rn.add(null, p, v[i]));
} else {
Rn.times(v[i], scale / 2., v[i]);
vertexCoordinates.add(Rn.add(null, p, v[i]));
vertexCoordinates.add(p);
}
edgeIndices.add(new int[]{vertexCoordinates.size() - 1,
vertexCoordinates.size() - 2});
add3DVector(v[i], p);
}
} catch(NullPointerException e) {
numNullValues++;
}
}
return numNullValues;
}
}
class VectorCoordinateAndIndexFactory extends AbstractCoordinateAndIndexFactory<double[]> {
......@@ -416,31 +443,12 @@ public class VectorFieldVisualizer extends DataVisualizerPlugin implements
aSet.setParameter("alpha", .5);
int numNullValues = 0;
for (N node : nodes) {
double[] v = adapter.get(node, aSet);
if (v == null) {
try {
double[] v = adapter.get(node, aSet);
double[] p = aSet.getD(BaryCenter3d.class, node);
add3DVector(v, p);
} catch (NullPointerException e) {
numNullValues++;
continue;
} else if (v.length == 4) {
Pn.dehomogenize(v, v);
} else if (v.length != 3) {
throw new RuntimeException(
"Adapter does not return vectors in 3-space or homogeneous 4-space.");
}
v = new double[]{v[0], v[1], v[2]};
double[] p = aSet.getD(BaryCenter3d.class, node);
if (normalized) {
Rn.normalize(v, v);
Rn.times(v, meanEdgeLength, v);
}
if (centered) {
Rn.times(v, scale / 2., v);
vertexCoordinates.add(Rn.add(null, p, v));
Rn.times(v, -1, v);
vertexCoordinates.add(Rn.add(null, p, v));
} else {
Rn.times(v, scale / 2., v);
vertexCoordinates.add(Rn.add(null, p, v));
vertexCoordinates.add(p);
}
edgeIndices.add(new int[]{vertexCoordinates.size() - 1, vertexCoordinates.size() - 2});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment