Partial refactor
This commit is contained in:
2
core/base/README.md
Normal file
2
core/base/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
# MC-Mesh Core Base
|
||||
Contains some classes for constructing meshes from 3d data
|
||||
34
core/base/pom.xml
Normal file
34
core/base/pom.xml
Normal file
@@ -0,0 +1,34 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.aaaaahhhhhhh.bananapuncher714</groupId>
|
||||
<artifactId>mc-mesh-core</artifactId>
|
||||
<version>0.0.1</version>
|
||||
</parent>
|
||||
<artifactId>mc-mesh-core-base</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.aaaaahhhhhhh.bananapuncher714</groupId>
|
||||
<artifactId>mc-mesh-core-algorithm</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.14.1</version>
|
||||
<configuration>
|
||||
<release>8</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.base;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Facet {
|
||||
public Vector3d normal;
|
||||
public List< Vector3d > points = new ArrayList< Vector3d >();
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.base;
|
||||
|
||||
public class LineSegment {
|
||||
public Vector3d point1, point2;
|
||||
|
||||
public LineSegment( Vector3d point1, Vector3d point2 ) {
|
||||
if ( point1.getX() < point2.getX() ) {
|
||||
this.point1 = point1;
|
||||
this.point2 = point2;
|
||||
} else if ( point1.getX() > point2.getX() ) {
|
||||
this.point1 = point2;
|
||||
this.point2 = point1;
|
||||
} else if ( point1.getY() < point2.getY() ) {
|
||||
this.point1 = point1;
|
||||
this.point2 = point2;
|
||||
} else if ( point1.getY() > point2.getY() ) {
|
||||
this.point1 = point2;
|
||||
this.point2 = point1;
|
||||
} else if ( point1.getZ() < point2.getZ() ) {
|
||||
this.point1 = point1;
|
||||
this.point2 = point2;
|
||||
} else if ( point1.getZ() > point2.getZ() ) {
|
||||
this.point1 = point2;
|
||||
this.point2 = point1;
|
||||
} else {
|
||||
// point 1 and 2 are the same, I guess...
|
||||
this.point1 = point1;
|
||||
this.point2 = point2;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isPoint() {
|
||||
return point1.equals( point2 );
|
||||
}
|
||||
|
||||
public double length() {
|
||||
return point1.distance( point2 );
|
||||
}
|
||||
|
||||
public Vector3d getVector() {
|
||||
return new Vector3d( point2 ).subtract( point1 );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.base;
|
||||
|
||||
public class Matrix3d {
|
||||
protected Vector3d[] rows = new Vector3d[ 3 ];
|
||||
|
||||
public Matrix3d( Vector3d col1, Vector3d col2, Vector3d col3 ) {
|
||||
rows[ 0 ] = new Vector3d( col1.getX(), col2.getX(), col3.getX() );
|
||||
rows[ 1 ] = new Vector3d( col1.getY(), col2.getY(), col3.getY() );
|
||||
rows[ 2 ] = new Vector3d( col1.getZ(), col2.getZ(), col3.getZ() );
|
||||
}
|
||||
|
||||
public Matrix3d transpose() {
|
||||
return new Matrix3d(
|
||||
rows[ 0 ],
|
||||
rows[ 1 ],
|
||||
rows[ 2 ]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.base;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MeshBuilder {
|
||||
public List< Plane > planes = new ArrayList< Plane >();
|
||||
|
||||
public Plane addFacet( Facet facet ) {
|
||||
final Optional< Plane > opt = planes.stream().filter( p -> p.matches( facet.normal, facet.points.get( 0 ) ) ).findAny();
|
||||
if ( opt.isPresent() ) {
|
||||
final Plane plane = opt.get();
|
||||
plane.addShape( facet );
|
||||
return plane;
|
||||
}
|
||||
|
||||
Plane plane = new Plane();
|
||||
plane.normal = facet.normal;
|
||||
Optional< Vector3d > ref = facet.points.stream().filter( v -> v.lengthSquared() > 0.001 && Math.abs( new Vector3d( v ).normalize().dot( facet.normal ) ) < .999999 ).findAny();
|
||||
if ( ref.isPresent() ) {
|
||||
plane.point = ref.get();
|
||||
}
|
||||
plane.addShape( facet );
|
||||
planes.add( plane );
|
||||
return plane;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.base;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.algorithm.Point;
|
||||
import com.aaaaahhhhhhh.bananapuncher714.mesh.algorithm.Polygon;
|
||||
|
||||
public class Plane {
|
||||
public Vector3d normal;
|
||||
public Vector3d point;
|
||||
public double distance;
|
||||
|
||||
private boolean set = false;
|
||||
|
||||
public List< Polygon > polygons = new ArrayList< Polygon >();
|
||||
|
||||
private Matrix3d mat;
|
||||
private Matrix3d transpose;
|
||||
|
||||
public void addShape( Facet facet ) {
|
||||
final Matrix3d mat = getProjectionMatrix();
|
||||
|
||||
List< Point > polygon = new ArrayList< Point >();
|
||||
for ( Vector3d p : facet.points ) {
|
||||
double t = new Vector3d( point ).subtract( p ).dot( normal );
|
||||
Vector3d r = new Vector3d( normal ).multiply( t ).add( p ).multiply( mat );
|
||||
|
||||
polygon.add( new Point( r.getX(), r.getY() ) );
|
||||
|
||||
if ( !set ) {
|
||||
distance = r.getZ();
|
||||
|
||||
set = true;
|
||||
} else if ( Math.abs( r.getZ() - distance ) > 1e-7 ) {
|
||||
throw new IllegalStateException( "Bad point in facet!" );
|
||||
}
|
||||
}
|
||||
|
||||
polygons.add( new Polygon( polygon ) );
|
||||
}
|
||||
|
||||
public Vector3d convert( final Point p ) {
|
||||
final Vector3d plane = new Vector3d( p.getX(), p.getY(), distance );
|
||||
|
||||
final Vector3d res = plane.multiply( getInverseProjectionMatrix() );
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public Facet convert( final Polygon polygon ) {
|
||||
final Facet facet = new Facet();
|
||||
|
||||
facet.normal = normal;
|
||||
for ( final Point p : polygon.getPoints() ) {
|
||||
facet.points.add( convert( p ) );
|
||||
}
|
||||
|
||||
return facet;
|
||||
}
|
||||
|
||||
public Collection< Facet > convert( final Collection< Polygon > polygons ) {
|
||||
return polygons.parallelStream().map( this::convert ).collect( Collectors.toSet() );
|
||||
}
|
||||
|
||||
public boolean matches( Vector3d normal, Vector3d point ) {
|
||||
return Math.abs( this.normal.dot( normal ) ) > 0.999 && Math.abs( new Vector3d( this.point ).subtract( point ).dot( normal ) ) < 0.00001;
|
||||
}
|
||||
|
||||
private Matrix3d getProjectionMatrix() {
|
||||
if ( mat == null ) {
|
||||
final Vector3d b1 = new Vector3d( normal ).multiply( new Vector3d( point ).multiply( -1 ).dot( normal ) ).add( point ).normalize();
|
||||
final Vector3d b3 = new Vector3d( normal.getX(), normal.getY(), normal.getZ() );
|
||||
final Vector3d b2 = new Vector3d( b1 ).cross( b3 ).normalize();
|
||||
mat = new Matrix3d( b1, b2, b3 ).transpose();
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
private Matrix3d getInverseProjectionMatrix() {
|
||||
if ( transpose == null ) {
|
||||
transpose = getProjectionMatrix().transpose();
|
||||
}
|
||||
return transpose;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package com.aaaaahhhhhhh.bananapuncher714.mesh.base;
|
||||
|
||||
public class Vector3d {
|
||||
private double x, y, z;
|
||||
|
||||
public Vector3d( double x, double y, double z ) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public Vector3d( Vector3d copy ) {
|
||||
this.x = copy.x;
|
||||
this.y = copy.y;
|
||||
this.z = copy.z;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
public void setX( double x ) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public void setY( double y ) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public void setZ( double z ) {
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public double dot( Vector3d o ) {
|
||||
return x * o.x + y * o.y + z * o.z;
|
||||
}
|
||||
|
||||
public Vector3d cross( Vector3d o ) {
|
||||
return new Vector3d(
|
||||
y * o.z - o.y * z,
|
||||
z * o.x - o.z * x,
|
||||
x * o.y - o.x * y
|
||||
);
|
||||
}
|
||||
|
||||
public double length() {
|
||||
return Math.sqrt( lengthSquared() );
|
||||
}
|
||||
|
||||
public double lengthSquared() {
|
||||
return dot( this );
|
||||
}
|
||||
|
||||
public Vector3d normalize() {
|
||||
double length = length();
|
||||
|
||||
x /= length;
|
||||
y /= length;
|
||||
z /= length;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d multiply( Matrix3d mat ) {
|
||||
return new Vector3d(
|
||||
dot( mat.rows[ 0 ] ),
|
||||
dot( mat.rows[ 1 ] ),
|
||||
dot( mat.rows[ 2 ] )
|
||||
);
|
||||
}
|
||||
|
||||
public Vector3d multiply( double v ) {
|
||||
x *= v;
|
||||
y *= v;
|
||||
z *= v;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d add( double v ) {
|
||||
x += v;
|
||||
y += v;
|
||||
z += v;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d add( Vector3d o ) {
|
||||
x += o.x;
|
||||
y += o.y;
|
||||
z += o.z;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Vector3d subtract( Vector3d o ) {
|
||||
x -= o.x;
|
||||
y -= o.y;
|
||||
z -= o.z;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public double distance( Vector3d o ) {
|
||||
return new Vector3d( this ).subtract( o ).length();
|
||||
}
|
||||
|
||||
public double distanceSquared( Vector3d o ) {
|
||||
return new Vector3d( this ).subtract( o ).lengthSquared();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(x);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(y);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(z);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Vector3d other = (Vector3d) obj;
|
||||
if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(z) != Double.doubleToLongBits(other.z))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Vector3d{x=" + x + ",y=" + y + ",z=" + z + "}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user