use of net.imglib2.RealLocalizable in project imagej-ops by imagej.
the class DefaultConvexHull3D method calculate.
@Override
public Mesh calculate(final Mesh input) {
DefaultMesh output = new DefaultMesh();
Set<Vertex> vertices = new LinkedHashSet<>();
for (final RealLocalizable v : input.getVertices()) {
vertices.add(new Vertex(v.getDoublePosition(0), v.getDoublePosition(1), v.getDoublePosition(2)));
}
List<TriangularFacet> facets = new ArrayList<>();
List<TriangularFacet> facetsWithPointInFront = new ArrayList<>();
final double epsilon = computeHull(vertices, facets, facetsWithPointInFront);
for (TriangularFacet f : facets) {
output.addFace(f);
}
output.setEpsilon(epsilon);
return output;
}
use of net.imglib2.RealLocalizable in project imagej-ops by imagej.
the class DefaultInertiaTensor3DMesh method calculate.
@Override
public RealMatrix calculate(final Mesh input) {
final RealLocalizable o = centroid.calculate(input);
BlockRealMatrix tensor = new BlockRealMatrix(3, 3);
final Iterator<Facet> c = input.getFacets().iterator();
while (c.hasNext()) {
final TriangularFacet tf = (TriangularFacet) c.next();
tensor = tensor.add(tetrahedronInertiaTensor(tf.getVertex(0), tf.getVertex(1), tf.getVertex(2), o));
}
return tensor;
}
use of net.imglib2.RealLocalizable in project imagej-ops by imagej.
the class DefaultVoxelization3D method calculate.
@Override
public RandomAccessibleInterval<BitType> calculate(Mesh input) {
Img<BitType> outImg = ops.create().img(new FinalInterval(width, height, depth), new BitType());
DefaultMesh dMesh = (DefaultMesh) input;
Set<RealLocalizable> verts = dMesh.getVertices();
RealPoint minPoint = new RealPoint(verts.iterator().next());
RealPoint maxPoint = new RealPoint(verts.iterator().next());
for (RealLocalizable v : verts) {
if (v.getDoublePosition(0) < minPoint.getDoublePosition(0))
minPoint.setPosition(v.getDoublePosition(0), 0);
if (v.getDoublePosition(1) < minPoint.getDoublePosition(1))
minPoint.setPosition(v.getDoublePosition(1), 1);
if (v.getDoublePosition(2) < minPoint.getDoublePosition(2))
minPoint.setPosition(v.getDoublePosition(2), 2);
if (v.getDoublePosition(0) > maxPoint.getDoublePosition(0))
maxPoint.setPosition(v.getDoublePosition(0), 0);
if (v.getDoublePosition(1) > maxPoint.getDoublePosition(1))
maxPoint.setPosition(v.getDoublePosition(1), 1);
if (v.getDoublePosition(2) > maxPoint.getDoublePosition(2))
maxPoint.setPosition(v.getDoublePosition(2), 2);
}
RealPoint dimPoint = new RealPoint((maxPoint.getDoublePosition(0) - minPoint.getDoublePosition(0)), (maxPoint.getDoublePosition(1) - minPoint.getDoublePosition(1)), (maxPoint.getDoublePosition(2) - minPoint.getDoublePosition(2)));
double[] stepSizes = new double[3];
stepSizes[0] = dimPoint.getDoublePosition(0) / width;
stepSizes[1] = dimPoint.getDoublePosition(1) / height;
stepSizes[2] = dimPoint.getDoublePosition(2) / depth;
double[] voxelHalfsize = new double[3];
for (int k = 0; k < stepSizes.length; k++) voxelHalfsize[k] = stepSizes[k] / 2.0;
for (Facet f : dMesh.getFacets()) {
TriangularFacet tri = (TriangularFacet) f;
Vector3D v1 = tri.getP0();
Vector3D v2 = tri.getP1();
Vector3D v3 = tri.getP2();
double[] minSubBoundary = new double[] { Math.min(Math.min(v1.getX(), v2.getX()), v3.getX()) - minPoint.getDoublePosition(0), Math.min(Math.min(v1.getY(), v2.getY()), v3.getY()) - minPoint.getDoublePosition(1), Math.min(Math.min(v1.getZ(), v2.getZ()), v3.getZ()) - minPoint.getDoublePosition(2) };
double[] maxSubBoundary = new double[] { Math.max(Math.max(v1.getX(), v2.getX()), v3.getX()) - minPoint.getDoublePosition(0), Math.max(Math.max(v1.getY(), v2.getY()), v3.getY()) - minPoint.getDoublePosition(1), Math.max(Math.max(v1.getZ(), v2.getZ()), v3.getZ()) - minPoint.getDoublePosition(2) };
// Should use the
RandomAccess<BitType> ra = outImg.randomAccess();
// interval
// implementation
// for speed
long[] indices = new long[3];
for (indices[0] = (long) Math.floor(minSubBoundary[0] / stepSizes[0]); indices[0] < Math.floor(maxSubBoundary[0] / stepSizes[0]); indices[0]++) {
for (indices[1] = (long) Math.floor(minSubBoundary[1] / stepSizes[1]); indices[1] < Math.floor(maxSubBoundary[1] / stepSizes[1]); indices[1]++) {
for (indices[2] = (long) Math.floor(minSubBoundary[2] / stepSizes[2]); indices[2] < Math.floor(maxSubBoundary[2] / stepSizes[2]); indices[2]++) {
ra.setPosition(indices);
if (// Don't check if voxel is already
!ra.get().get()) // filled
{
double[] voxelCenter = new double[3];
for (int k = 0; k < 3; k++) voxelCenter[k] = indices[k] * stepSizes[k] + voxelHalfsize[k];
if (triBoxOverlap(voxelCenter, voxelHalfsize, v1, v2, v3) == 1) {
ra.get().set(true);
}
}
}
}
}
}
return outImg;
}
use of net.imglib2.RealLocalizable in project imagej-ops by imagej.
the class DefaultMinorMajorAxis method calculate.
@Override
public Pair<DoubleType, DoubleType> calculate(final Polygon2D input) {
List<RealLocalizable> points = new ArrayList<>(GeomUtils.vertices(input));
// Sort RealLocalizables of P by x-coordinate (in case of a tie,
// sort by
// y-coordinate). Sorting is counter clockwise.
Collections.sort(points, new Comparator<RealLocalizable>() {
@Override
public int compare(final RealLocalizable o1, final RealLocalizable o2) {
final Double o1x = new Double(o1.getDoublePosition(0));
final Double o2x = new Double(o2.getDoublePosition(0));
final int result = o2x.compareTo(o1x);
if (result == 0) {
return new Double(o2.getDoublePosition(1)).compareTo(new Double(o1.getDoublePosition(1)));
}
return result;
}
});
points.add(points.get(0));
// calculate minor and major axis
double[] minorMajorAxis = getMinorMajorAxis(input, points);
return new ValuePair<>(new DoubleType(minorMajorAxis[0]), new DoubleType(minorMajorAxis[1]));
}
use of net.imglib2.RealLocalizable in project imagej-ops by imagej.
the class DefaultSmallestEnclosingRectangle method rotate.
/**
* Rotates the given Polygon2D consisting of a list of RealPoints by the
* given angle about the given center.
*
* @param inPoly A Polygon2D consisting of a list of RealPoint RealPoints
* @param angle the rotation angle
* @param center the rotation center
* @return a rotated polygon
*/
private Polygon2D rotate(final Polygon2D inPoly, final double angle, final RealLocalizable center) {
List<RealLocalizable> out = new ArrayList<>();
for (RealLocalizable RealPoint : GeomUtils.vertices(inPoly)) {
// double angleInRadians = Math.toRadians(angleInDegrees);
double cosTheta = Math.cos(angle);
double sinTheta = Math.sin(angle);
double x = cosTheta * (RealPoint.getDoublePosition(0) - center.getDoublePosition(0)) - sinTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(0);
double y = sinTheta * (RealPoint.getDoublePosition(0) - center.getDoublePosition(0)) + cosTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(1);
out.add(new RealPoint(x, y));
}
return new DefaultWritablePolygon2D(out);
}
Aggregations