|
|
@ -1,5 +1,6 @@ |
|
|
|
package sig; |
|
|
|
package sig; |
|
|
|
import java.awt.Color; |
|
|
|
import java.awt.Color; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
|
|
|
|
public class Triangle { |
|
|
|
public class Triangle { |
|
|
|
Vector A,B,C; |
|
|
|
Vector A,B,C; |
|
|
@ -20,7 +21,7 @@ public class Triangle { |
|
|
|
} |
|
|
|
} |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
public String toString() { |
|
|
|
return "Triangle [A=" + A + ", B=" + B + ", C=" + C + "]"; |
|
|
|
return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", col=" + col + "]"; |
|
|
|
} |
|
|
|
} |
|
|
|
public Color getColor() { |
|
|
|
public Color getColor() { |
|
|
|
return col; |
|
|
|
return col; |
|
|
@ -30,14 +31,13 @@ public class Triangle { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static float dist(Vector plane_p,Vector plane_n,Vector p) { |
|
|
|
static float dist(Vector plane_p,Vector plane_n,Vector p) { |
|
|
|
Vector n = Vector.normalize(p); |
|
|
|
|
|
|
|
return plane_n.x*p.x+plane_n.y*p.y+plane_n.z*p.z-Vector.dotProduct(plane_n,plane_p); |
|
|
|
return plane_n.x*p.x+plane_n.y*p.y+plane_n.z*p.z-Vector.dotProduct(plane_n,plane_p); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static int ClipAgainstPlane(Vector plane_p,Vector plane_n,Triangle in,Triangle[] out_tri) { |
|
|
|
public static int ClipAgainstPlane(Vector plane_p,Vector plane_n,Triangle in,Triangle[] out_tri) { |
|
|
|
plane_n = Vector.normalize(plane_n); |
|
|
|
plane_n = Vector.normalize(plane_n); |
|
|
|
Vector[] inside_points = new Vector[3]; |
|
|
|
Vector[] inside_points = new Vector[]{new Vector(),new Vector(),new Vector()}; |
|
|
|
Vector[] outside_points = new Vector[3]; |
|
|
|
Vector[] outside_points = new Vector[]{new Vector(),new Vector(),new Vector()}; |
|
|
|
int insidePointCount=0,outsidePointCount=0; |
|
|
|
int insidePointCount=0,outsidePointCount=0; |
|
|
|
|
|
|
|
|
|
|
|
float d0=dist(plane_p,plane_n,in.A); |
|
|
|
float d0=dist(plane_p,plane_n,in.A); |
|
|
@ -63,15 +63,20 @@ public class Triangle { |
|
|
|
if (insidePointCount==0) { |
|
|
|
if (insidePointCount==0) { |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} else |
|
|
|
} else |
|
|
|
|
|
|
|
if (insidePointCount==3) { |
|
|
|
|
|
|
|
out_tri[0] = in; |
|
|
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
} else |
|
|
|
if (insidePointCount==1&&outsidePointCount==2) { |
|
|
|
if (insidePointCount==1&&outsidePointCount==2) { |
|
|
|
out_tri[0].col = in.col; |
|
|
|
out_tri[0].col = Color.BLUE; |
|
|
|
out_tri[0].A = inside_points[0]; |
|
|
|
out_tri[0].A = inside_points[0]; |
|
|
|
out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); |
|
|
|
out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); |
|
|
|
out_tri[0].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[1]); |
|
|
|
out_tri[0].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[1]); |
|
|
|
return 1; |
|
|
|
return 1; |
|
|
|
} else |
|
|
|
} else |
|
|
|
if (insidePointCount==2&&outsidePointCount==1) { |
|
|
|
if (insidePointCount==2&&outsidePointCount==1) { |
|
|
|
out_tri[0].col=out_tri[1].col=in.col; |
|
|
|
out_tri[0].col=Color.RED; |
|
|
|
|
|
|
|
out_tri[1].col=Color.GREEN; |
|
|
|
out_tri[0].A = inside_points[0]; |
|
|
|
out_tri[0].A = inside_points[0]; |
|
|
|
out_tri[0].B = inside_points[1]; |
|
|
|
out_tri[0].B = inside_points[1]; |
|
|
|
out_tri[0].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); |
|
|
|
out_tri[0].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); |
|
|
@ -79,10 +84,6 @@ public class Triangle { |
|
|
|
out_tri[1].B = out_tri[0].C; |
|
|
|
out_tri[1].B = out_tri[0].C; |
|
|
|
out_tri[1].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[1], outside_points[0]); |
|
|
|
out_tri[1].C = Vector.IntersectPlane(plane_p, plane_n, inside_points[1], outside_points[0]); |
|
|
|
return 2; |
|
|
|
return 2; |
|
|
|
} else |
|
|
|
|
|
|
|
if (insidePointCount==3) { |
|
|
|
|
|
|
|
out_tri[0] = in; |
|
|
|
|
|
|
|
return 1; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|