Skip to content

Commit

Permalink
removed Obstacle, moved rayIntersect to MeshGeometry; vision: json me…
Browse files Browse the repository at this point in the history
…thods not working
  • Loading branch information
robp94 committed Feb 20, 2019
1 parent ccac49d commit 7692b13
Show file tree
Hide file tree
Showing 10 changed files with 844 additions and 1,088 deletions.
641 changes: 292 additions & 349 deletions build/yuka.js

Large diffs are not rendered by default.

642 changes: 293 additions & 349 deletions build/yuka.module.js

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions src/core/MeshGeometry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { AABB } from '../math/AABB.js';
import { BoundingSphere } from '../math/BoundingSphere.js';
import { Vector3 } from '../math/Vector3.js';
import { Ray } from "../math/Ray";
import { Plane } from "../math/Plane";
import { Matrix4 } from "../math/Matrix4";


const boundingSphere = new BoundingSphere();
const triangle = { a: new Vector3(), b: new Vector3(), c: new Vector3() };
const rayLocal = new Ray();
const plane = new Plane();
const inverseMatrix = new Matrix4();

/**
* Class for representing a polygon mesh. The faces consist of triangles.
Expand Down Expand Up @@ -66,6 +76,121 @@ class MeshGeometry {

}

/**
* Performs a ray intersection test with the geometry of the obstacle and stores
* the intersection point in the given result vector. If no intersection is detected,
* *null* is returned.
*
* @param {Ray} ray - The ray to test.
* @param {Vector3} intersectionPoint - The intersection point.
* @param {Matrix4} worldMatrix - The world matrix.
* @param {Vector3} normal - The normal vector of the respective triangle.
* @return {Vector3} The result vector.
*/
intersectRay( ray, intersectionPoint, worldMatrix, normal = null ) {

const geometry = this;

// check bounding sphere first in world space

boundingSphere.copy( geometry.boundingSphere ).applyMatrix4( worldMatrix );

if ( ray.intersectsBoundingSphere( boundingSphere ) ) {

// transform the ray into the local space of the obstacle

worldMatrix.getInverse( inverseMatrix );
rayLocal.copy( ray ).applyMatrix4( inverseMatrix );

// check AABB in local space since its more expensive to convert an AABB to world space than a bounding sphere

if ( rayLocal.intersectsAABB( geometry.aabb ) ) {

// now perform more expensive test with all triangles of the geometry

const vertices = geometry.vertices;
const indices = geometry.indices;

if ( indices === null ) {

// non-indexed geometry

for ( let i = 0, l = vertices.length; i < l; i += 9 ) {

triangle.a.set( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] );
triangle.b.set( vertices[ i + 3 ], vertices[ i + 4 ], vertices[ i + 5 ] );
triangle.c.set( vertices[ i + 6 ], vertices[ i + 7 ], vertices[ i + 8 ] );

if ( rayLocal.intersectTriangle( triangle, geometry.backfaceCulling, intersectionPoint ) !== null ) {

// transform intersection point back to world space

intersectionPoint.applyMatrix4( worldMatrix );

// compute normal of triangle in world space if necessary

if ( normal !== null ) {

plane.fromCoplanarPoints( triangle.a, triangle.b, triangle.c );
normal.copy( plane.normal );
normal.transformDirection( worldMatrix );

}

return intersectionPoint;

}

}

} else {

// indexed geometry

for ( let i = 0, l = indices.length; i < l; i += 3 ) {

const a = indices[ i ];
const b = indices[ i + 1 ];
const c = indices[ i + 2 ];

const stride = 3;

triangle.a.set( vertices[ ( a * stride ) ], vertices[ ( a * stride ) + 1 ], vertices[ ( a * stride ) + 2 ] );
triangle.b.set( vertices[ ( b * stride ) ], vertices[ ( b * stride ) + 1 ], vertices[ ( b * stride ) + 2 ] );
triangle.c.set( vertices[ ( c * stride ) ], vertices[ ( c * stride ) + 1 ], vertices[ ( c * stride ) + 2 ] );

if ( rayLocal.intersectTriangle( triangle, geometry.backfaceCulling, intersectionPoint ) !== null ) {

// transform intersection point back to world space

intersectionPoint.applyMatrix4( worldMatrix );

// compute normal of triangle in world space if necessary

if ( normal !== null ) {

plane.fromCoplanarPoints( triangle.a, triangle.b, triangle.c );
normal.copy( plane.normal );
normal.transformDirection( worldMatrix );

}

return intersectionPoint;

}

}

}

}

}

return null;

}

/**
* Transforms this instance into a JSON object.
*
Expand Down
187 changes: 0 additions & 187 deletions src/perception/vision/Obstacle.js

This file was deleted.

Loading

0 comments on commit 7692b13

Please sign in to comment.