port plane logic from java

This commit is contained in:
Val Erastov 2014-08-18 22:50:08 -07:00
parent cb0187c622
commit f9ae20c167
12 changed files with 773 additions and 16 deletions

View file

@ -36,9 +36,7 @@ public class Polygon {
this.holes = holes;
checkPolygon(shell);
for (List<Vector> hole : holes) {
if (hole.size() < 3) {
checkPolygon(hole);
}
checkPolygon(hole);
}
}
@ -58,8 +56,8 @@ public class Polygon {
return new Vector[] {x, y, normal};
}
private void checkPolygon(List<Vector> shell) {
if (shell.size() < 3) {
private void checkPolygon(List<Vector> points) {
if (points.size() < 3) {
throw new IllegalArgumentException("Polygon should contain at least 3 point");
}
}

View file

@ -252,7 +252,7 @@ public class HMath {
return copy;
}
public static boolean areEquals(double v1, double v2, double tolerance) {
public static boolean areEqual(double v1, double v2, double tolerance) {
return abs(v1 - v2) < tolerance;
}
}

View file

@ -112,9 +112,9 @@ public class Vector {
public boolean equalTo(Vector vector, double tolerance) {
return
HMath.areEquals(x, vector.x, tolerance) &&
HMath.areEquals(y, vector.y, tolerance) &&
HMath.areEquals(z, vector.z, tolerance);
HMath.areEqual(x, vector.x, tolerance) &&
HMath.areEqual(y, vector.y, tolerance) &&
HMath.areEqual(z, vector.z, tolerance);
}
public Vector negate() {

View file

@ -0,0 +1,249 @@
TCAD.utils = {};
TCAD.utils.createSquare = function(width) {
width /= 2;
var shell = [
new TCAD.Vector(-width, -width, 0),
new TCAD.Vector( width, -width, 0),
new TCAD.Vector( width, width, 0),
new TCAD.Vector(-width, width, 0)
];
return new TCAD.Polygon(shell);
};
TCAD.utils.createBox = function(width) {
var square = TCAD.utils.createSquare(width);
return TCAD.geom.extrude(square, square.normal.multiply(width));
};
TCAD.utils.checkPolygon = function(poly) {
if (poly.length < 3) {
throw new Error('Polygon should contain at least 3 point');
}
};
TCAD.utils.fixCCW = function(shell) {
if (!TCAD.geom.isCCW(shell)) {
shell = shell.slice(0);
shell.reverse();
}
return shell;
};
TCAD.TOLERANCE = 0.000001;
TCAD.utils.areEqual = function(v1, v2, tolerance) {
return Math.abs(v1 - v2) < tolerance;
};
TCAD.utils.areVectorsEqual = function(v1, v2, tolerance) {
return TCAD.utils.areEqual(v1.x, v2.x, tolerance) &&
TCAD.utils.areEqual(v1.y, v2.y, tolerance) &&
TCAD.utils.areEqual(v1.z, v2.z, tolerance);
};
TCAD.utils.vectorsEqual = function(v1, v2) {
return TCAD.utils.areVectorsEqual(v1, v2, TCAD.TOLERANCE);
};
TCAD.utils.equal = function(v1, v2) {
return TCAD.utils.areEqual(v1, v2, TCAD.TOLERANCE);
};
TCAD.geom = {};
TCAD.geom.normalOfCCWSeq = function(ccwSequence) {
var a = ccwSequence[0];
var b = ccwSequence[1];
var c = ccwSequence[2];
return b.minus(a).cross(c.minus(a)).normalize();
};
TCAD.geom.normalOfCCWSeqTHREE = function(ccwSequence) {
var a = ccwSequence[0];
var b = ccwSequence[1].clone();
var c = ccwSequence[2].clone();
return b.sub(a).cross(c.sub(a)).normalize();
};
// http://en.wikipedia.org/wiki/Shoelace_formula
TCAD.geom.area = function (contour) {
var n = contour.length;
var a = 0.0;
for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
}
return a * 0.5;
};
TCAD.geom.isCCW = function(vertices) {
return TCAD.geom.area(vertices) >= 0;
};
TCAD.geom.extrude = function(source, target) {
var dotProduct = target.normalize().dot(source.normal);
if (dotProduct == 0) {
return [];
}
if (dotProduct > 0) {
source = source.flip();
}
var poly = [source];
var lid = source.shift(target).flip();
poly.push(lid);
var n = source.shell.length;
for ( var p = n - 1, i = 0; i < n; p = i ++ ) {
var face = new TCAD.Polygon([
source.shell[p],
source.shell[i],
lid.shell[i],
lid.shell[p]
]);
poly.push(face);
}
return poly;
};
TCAD.Solid = function(polygons) {
THREE.Geometry.call( this );
var scope = this;
function pushVertices(vertices) {
for ( var v = 0; v < vertices.length; ++ v ) {
scope.vertices.push( new THREE.Vector3( vertices[v].x, vertices[v].y, vertices[v].z ) );
}
}
var off = 0;
for (var p = 0; p < polygons.length; ++ p) {
var poly = polygons[p];
var faces = poly.triangulate();
pushVertices(poly.shell);
for ( var h = 0; h < poly.holes; ++ h ) {
pushVertices(poly.holes[ h ]);
}
var polyFace = {faces : [], polygon : poly};
for ( var i = 0; i < faces.length; ++ i ) {
var a = faces[i][0] + off;
var b = faces[i][1] + off;
var c = faces[i][2] + off;
var fNormal = TCAD.geom.normalOfCCWSeqTHREE([
this.vertices[a], this.vertices[b], this.vertices[c]]);
if (!TCAD.utils.vectorsEqual(fNormal, poly.normal)) {
console.log("ASSERT");
var _a = a;
a = c;
c = _a;
}
var face = new THREE.Face3( a, b, c );
polyFace.faces.push(face);
face.__TCAD_polyFace = polyFace;
face.normal = poly.normal.three();
face.materialIndex = p;
this.faces.push( face );
}
off = this.vertices.length;
}
this.mergeVertices();
};
TCAD.Solid.prototype = Object.create( THREE.Geometry.prototype );
/**
* Polygon
**/
TCAD.Polygon = function(shell, holes, normal) {
if (!holes) {
holes = [];
}
TCAD.utils.checkPolygon(shell);
for (var h = 0; h < holes.length; ++h) {
TCAD.utils.checkPolygon(holes[h]);
}
if (normal === undefined) {
normal = TCAD.geom.normalOfCCWSeq(shell);
} else {
shell = TCAD.utils.fixCCW(shell);
}
this.normal = normal;
this.shell = shell;
this.holes = holes;
};
TCAD.Polygon.prototype.someBasis = function() {
var a = this.shell[0];
var b = this.shell[1];
var x = b.minus(a).normalize();
var y = this.normal.cross(x).normalize();
return [x, y, this.normal];
};
TCAD.Polygon.prototype.reverse = function(triangle) {
var first = triangle[0];
triangle[0] = triangle[2];
triangle[2] = first;
};
TCAD.Polygon.prototype.flip = function() {
return new TCAD.Polygon(this.shell, this.holes, this.normal.negate());
};
TCAD.Polygon.prototype.shift = function(target) {
var shell = [];
var i;
for (i = 0; i < this.shell.length; ++i) {
shell[i] = this.shell[i].plus(target);
}
var holes = [];
for (var h = 0; h < this.holes.length; ++h) {
holes[h] = [];
for (i = 0; i < this.holes[h].length; ++i) {
holes[h][i] = this.holes[h][i].plus(target);
}
}
return new TCAD.Polygon(shell, holes, this.normal);
};
TCAD.Polygon.prototype.triangulate = function() {
var _3dTransformation = new TCAD.Matrix().setBasis(this.someBasis());
var _2dTransformation = _3dTransformation.invert();
var i, h;
var shell = [];
var holes = [];
for (i = 0; i < this.shell.length; ++i) {
shell[i] = _2dTransformation.apply(this.shell[i]).three();
}
for (h = 0; h < this.holes.length; ++h) {
holes[h] = [];
for (i = 0; i < this.holes[h].length; ++i) {
holes[h][i] = _2dTransformation.apply(this.holes[h][i]);
}
}
return THREE.Shape.Utils.triangulateShape( shell, holes );
};

View file

@ -5,4 +5,4 @@ TCAD.App = function() {
this.viewer = new TCAD.Viewer();
this.ui = new TCAD.UI();
}
};

210
web/app/vector.js Normal file
View file

@ -0,0 +1,210 @@
TCAD.Vector = function(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
};
TCAD.Vector.prototype.set = function(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
};
TCAD.Vector.prototype.set3 = function(data) {
this.x = data[0];
this.y = data[1];
this.z = data[2];
};
TCAD.Vector.prototype.multiply = function(factor) {
return new TCAD.Vector(this.x * factor, this.y * factor, this.z * factor);
};
TCAD.Vector.prototype.dot = function(vector) {
return this.x * vector.x + this.y * vector.y + this.z * vector.z;
};
TCAD.Vector.prototype.copy = function() {
return new TCAD.Vector(this.x, this.y, this.z);
};
TCAD.Vector.prototype.length = function() {
return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
};
TCAD.Vector.prototype.minus = function(vector) {
return new TCAD.Vector(this.x - vector.x, this.y - vector.y, this.z - vector.z);
};
TCAD.Vector.prototype.plus = function(vector) {
return new TCAD.Vector(this.x + vector.x, this.y + vector.y, this.z + vector.z);
};
TCAD.Vector.prototype.normalize = function() {
var mag = this.length();
if (mag == 0.0) {
return new TCAD.Vector(0.0, 0.0, 0.0);
}
return new TCAD.Vector(this.x / mag, this.y / mag, this.z / mag);
};
TCAD.Vector.prototype.cross = function(a) {
return new TCAD.Vector(
this.y * a.z - this.z * a.y,
this.z * a.x - this.x * a.z,
this.x * a.y - this.y * a.x
);
};
TCAD.Vector.prototype.negate = function() {
return this.multiply(-1);
};
TCAD.Vector.prototype.equals = function(vector) {
return TCAD.utils.vectorsEqual(this, vector);
};
TCAD.Vector.prototype.three = function() {
return new THREE.Vector3(this.x, this.y, this.z);
};
TCAD.Matrix = function() {
this.reset();
};
TCAD.Matrix.prototype.reset = function() {
this.mxx = 1; this.mxy = 0; this.mxz = 0; this.tx = 0;
this.myx = 0; this.myy = 1; this.myz = 0; this.ty = 0;
this.mzx = 0; this.mzy = 0; this.mzz = 1; this.tz = 0;
return this;
};
TCAD.Matrix.prototype.setBasis = function(basis) {
var b = basis;
this.mxx = b[0].x; this.mxy = b[1].x; this.mxz = b[2].x; this.tx = 0;
this.myx = b[0].y; this.myy = b[1].y; this.myz = b[2].y; this.ty = 0;
this.mzx = b[0].z; this.mzy = b[1].z; this.mzz = b[2].z; this.tz = 0;
return this;
};
TCAD.Matrix.prototype.set3 = function(
mxx, mxy, mxz,
myx, myy, myz,
mzx, mzy, mzz
) {
this.mxx = mxx; this.mxy = mxy; this.mxz = mxz;
this.myx = myx; this.myy = myy; this.myz = myz;
this.mzx = mzx; this.mzy = mzy; this.mzz = mzz;
return this;
};
TCAD.Matrix.prototype.set34 = function(
mxx, mxy, mxz, tx,
myx, myy, myz, ty,
mzx, mzy, mzz, tz
) {
this.mxx = mxx; this.mxy = mxy; this.mxz = mxz; this.tx = tx;
this.myx = myx; this.myy = myy; this.myz = myz; this.ty = ty;
this.mzx = mzx; this.mzy = mzy; this.mzz = mzz; this.tz = tz;
return this;
};
TCAD.Matrix.prototype.setMatrix = function(m) {
this.mxx = m.mxx; this.mxy = m.mxy; this.mxz = m.mxz; this.tx = m.tx;
this.myx = m.myx; this.myy = m.myy; this.myz = m.myz; this.ty = m.ty;
this.mzx = m.mzx; this.mzy = m.mzy; this.mzz = m.mzz; this.tz = m.tz;
return this;
};
TCAD.Matrix.prototype.invert = function() {
var det =
this.mxx * (this.myy * this.mzz - this.mzy * this.myz) +
this.mxy * (this.myz * this.mzx - this.mzz * this.myx) +
this.mxz * (this.myx * this.mzy - this.mzx * this.myy);
if (det == 0.0) {
return null;
}
var cxx = this.myy * this.mzz - this.myz * this.mzy;
var cyx = - this.myx * this.mzz + this.myz * this.mzx;
var czx = this.myx * this.mzy - this.myy * this.mzx;
var cxt = - this.mxy * (this.myz * this.tz - this.mzz * this.ty)
- this.mxz * (this.ty * this.mzy - this.tz * this.myy)
- this.tx * (this.myy * this.mzz - this.mzy * this.myz);
var cxy = - this.mxy * this.mzz + this.mxz * this.mzy;
var cyy = this.mxx * this.mzz - this.mxz * this.mzx;
var czy = - this.mxx * this.mzy + this.mxy * this.mzx;
var cyt = this.mxx * (this.myz * this.tz - this.mzz * this.ty)
+ this.mxz * (this.ty * this.mzx - this.tz * this.myx)
+ this.tx * (this.myx * this.mzz - this.mzx * this.myz);
var cxz = this.mxy * this.myz - this.mxz * this.myy;
var cyz = - this.mxx * this.myz + this.mxz * this.myx;
var czz = this.mxx * this.myy - this.mxy * this.myx;
var czt = - this.mxx * (this.myy * this.tz - this.mzy * this.ty)
- this.mxy * (this.ty * this.mzx - this.tz * this.myx)
- this.tx * (this.myx * this.mzy - this.mzx * this.myy);
var result = new TCAD.Matrix();
result.mxx = cxx / det;
result.mxy = cxy / det;
result.mxz = cxz / det;
result.tx = cxt / det;
result.myx = cyx / det;
result.myy = cyy / det;
result.myz = cyz / det;
result.ty = cyt / det;
result.mzx = czx / det;
result.mzy = czy / det;
result.mzz = czz / det;
result.tz = czt / det;
return result;
};
TCAD.Matrix.prototype.combine = function(transform) {
var txx = transform.mxx;
var txy = transform.mxy;
var txz = transform.mxz;
var ttx = transform.tx;
var tyx = transform.myx;
var tyy = transform.myy;
var tyz = transform.myz;
var tty = transform.ty;
var tzx = transform.mzx;
var tzy = transform.mzy;
var tzz = transform.mzz;
var ttz = transform.tz;
var m = new TCAD.Matrix();
m.mxx = (this.mxx * txx + this.mxy * tyx + this.mxz * tzx);
m.mxy = (this.mxx * txy + this.mxy * tyy + this.mxz * tzy);
m.mxz = (this.mxx * txz + this.mxy * tyz + this.mxz * tzz);
m.tx = (this.mxx * ttx + this.mxy * tty + this.mxz * ttz + this.tx);
m.myx = (this.myx * txx + this.myy * tyx + this.myz * tzx);
m.myy = (this.myx * txy + this.myy * tyy + this.myz * tzy);
m.myz = (this.myx * txz + this.myy * tyz + this.myz * tzz);
m.ty = (this.myx * ttx + this.myy * tty + this.myz * ttz + this.ty);
m.mzx = (this.mzx * txx + this.mzy * tyx + this.mzz * tzx);
m.mzy = (this.mzx * txy + this.mzy * tyy + this.mzz * tzy);
m.mzz = (this.mzx * txz + this.mzy * tyz + this.mzz * tzz);
m.tz = (this.mzx * ttx + this.mzy * tty + this.mzz * ttz + this.tz);
return m;
};
TCAD.Matrix.prototype.apply = function(vector) {
var x = vector.x;
var y = vector.y;
var z = vector.z;
return new TCAD.Vector(
this.mxx * x + this.mxy * y + this.mxz * z + this.tx,
this.myx * x + this.myy * y + this.myz * z + this.ty,
this.mzx * x + this.mzy * y + this.mzz * z + this.tz);
};

View file

@ -26,7 +26,12 @@ TCAD.Viewer = function() {
}
window.addEventListener( 'resize', onWindowResize, false );
var geometry = new THREE.BoxGeometry(1,1,1);
// var geometry = new THREE.BoxGeometry(1,1,1);
// var geometry = new TCAD.Solid([TCAD.utils.createSquare(1)]);
var geometry = new TCAD.Solid(TCAD.utils.createBox(1));
var material = new THREE.MeshPhongMaterial( new THREE.MeshPhongMaterial({
@ -106,9 +111,16 @@ TCAD.Viewer = function() {
var intersects = ray.intersectObjects( scene.children );
if (intersects.length > 0) {
// console.log("Face Index: " + intersects[0].faceIndex);
// console.log(intersects[0]);
intersects[0].face.color.setHex( 0x00FF00 );
cube.geometry.colorsNeedUpdate = true;
console.log(intersects[0]);
if (intersects[0].face.__TCAD_polyFace !== undefined) {
var poly = intersects[0].face.__TCAD_polyFace;
for (var i = 0; i < poly.faces.length; ++i) {
var face = poly.faces[i];
face.color.setHex( 0x00FF00 );
cube.geometry.colorsNeedUpdate = true;
}
}
render();
}
}
@ -129,4 +141,4 @@ TCAD.Viewer = function() {
render();
animate();
}
};

View file

@ -9,7 +9,7 @@
}
</style>
<script src="lib/three/three.min.js"></script>
<script src="lib/three/three.js"></script>
<script src="lib/three/TrackballControls.js"></script>
<script src="lib/three/OrbitControls.js"></script>
<script src="lib/dat.gui.min.js"></script>
@ -17,6 +17,7 @@
<script src="app/ctrl.js"></script>
<script src="app/viewer.js"></script>
<script src="app/engine.js"></script>
<script src="app/vector.js"></script>
<script>window.onload = function() {new TCAD.App();}</script>
</head>

82
web/lib/dat.gui.min.js vendored Normal file

File diff suppressed because one or more lines are too long

4
web/lib/jquery-2.1.0.min.js vendored Normal file

File diff suppressed because one or more lines are too long

178
web/raycast.html Normal file
View file

@ -0,0 +1,178 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - interactive cubes</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="lib/three/three.min.js"></script>
<script src="lib/three/TrackballControls.js"></script>
<script src="lib/three/stats.min.js"></script>
<script >
var container, stats;
var camera, scene, projector, raycaster, renderer;
var mouse = new THREE.Vector2(), INTERSECTED;
var radius = 100, theta = 0;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = '';
container.appendChild( info );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
scene = new THREE.Scene();
var light = new THREE.DirectionalLight( 0xffffff, 2 );
light.position.set( 1, 1, 1 ).normalize();
scene.add( light );
var light = new THREE.DirectionalLight( 0xffffff );
light.position.set( -1, -1, -1 ).normalize();
scene.add( light );
var geometry = new THREE.BoxGeometry( 20, 20, 20 );
for ( var i = 0; i < 2000; i ++ ) {
var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
object.position.x = Math.random() * 800 - 400;
object.position.y = Math.random() * 800 - 400;
object.position.z = Math.random() * 800 - 400;
object.rotation.x = Math.random() * 2 * Math.PI;
object.rotation.y = Math.random() * 2 * Math.PI;
object.rotation.z = Math.random() * 2 * Math.PI;
object.scale.x = Math.random() + 0.5;
object.scale.y = Math.random() + 0.5;
object.scale.z = Math.random() + 0.5;
scene.add( object );
}
projector = new THREE.Projector();
raycaster = new THREE.Raycaster();
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0xf0f0f0 );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.sortObjects = false;
container.appendChild(renderer.domElement);
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseMove( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
<!--theta += 0.1;-->
camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
camera.lookAt( scene.position );
// find intersections
var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
projector.unprojectVector( vector, camera );
raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
var intersects = raycaster.intersectObjects( scene.children );
if ( intersects.length > 0 ) {
if ( INTERSECTED != intersects[ 0 ].object ) {
if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
INTERSECTED = intersects[ 0 ].object;
INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
INTERSECTED.material.emissive.setHex( 0xff0000 );
}
} else {
if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
INTERSECTED = null;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>

23
web/sandbox.html Normal file
View file

@ -0,0 +1,23 @@
<html>
<head>
<title>Sandbox</title>
<style>
body {
font-family: Monospace;
margin: 0px;
overflow: hidden;
}
</style>
<script src="lib/three/three.min.js"></script>
<script src="lib/three/TrackballControls.js"></script>
<script src="lib/three/OrbitControls.js"></script>
<script src="lib/dat.gui.min.js"></script>
<script src="app/sandbox.js"></script>
<script>window.onload = init;</script>
</head>
<body>
</body>
</html>