working on quadtree algorithm - looks promising

transform or lookup seems wrong though or something haha
This commit is contained in:
Daniel Leone 2021-02-18 10:07:15 +08:00
parent e4d5c9fb54
commit ab8871858d
4 changed files with 261 additions and 9 deletions

View File

@ -329,13 +329,13 @@
if (details) {
viewer.entities.removeAll();
viewer.entities.add({
position: pickPosition,
ellipsoid: {
radii: new Cesium.Cartesian3(10.0, 10.0, 10.0),
material: rayColor,
},
});
// viewer.entities.add({
// position: pickPosition,
// ellipsoid: {
// radii: new Cesium.Cartesian3(10.0, 10.0, 10.0),
// material: rayColor,
// },
// });
var r = details.mesh.boundingSphere3D.radius;
var c = details.mesh.boundingSphere3D.center;
viewer.entities.add({
@ -388,6 +388,37 @@
}
}
// if (details.traceDetails && details.traceDetails.coolTriangles) {
// var trianglesasdf = details.traceDetails.coolTriangles;
// for (var asdf = 0; asdf < trianglesasdf.length; asdf++) {
// var triangleasdf = trianglesasdf[asdf];
// viewer.entities.add({
// polygon: {
// hierarchy: triangleasdf,
// perPositionHeight: true,
// material: Cesium.Color.PINK,
// outline: true,
// outlineColor: Cesium.Color.CYAN,
// outlineWidth: 10,
// },
// });
// }
// }
if (details.traceDetails && details.traceDetails.coolPoints) {
var trianglesasdf = details.traceDetails.coolPoints;
for (var asdf = 0; asdf < trianglesasdf.length; asdf++) {
var p = trianglesasdf[asdf];
viewer.entities.add({
position: p,
ellipsoid: {
radii: new Cesium.Cartesian3(10.0, 10.0, 10.0),
material: Cesium.Color.BLUE,
},
});
}
}
if (details.traceDetails.intersectedTriangle) {
viewer.entities.add({
polygon: {

View File

@ -154,6 +154,7 @@ function createPackedQuadtree(
new Point(-0.5, -0.5),
new Point(0.5, 0.5)
);
for (i = 0; i < axisAlignedPositions.length; i++) {
var pos = axisAlignedPositions[i];
var node = rootNode;
@ -610,8 +611,17 @@ HeightmapTessellator.computeVertices = function (options) {
var orientedBoundingBox;
var packedOctree;
var quadtree;
var transform;
var inverseTransform;
var packedPositions = new Float32Array(positions.length * 3);
for (var i = 0; i < positions.length; i++) {
var pos = positions[i];
packedPositions[i * 3] = pos.x;
packedPositions[i * 3 + 1] = pos.y;
packedPositions[i * 3 + 2] = pos.z;
}
if (defined(rectangle)) {
console.time("creating oriented bounding box");
@ -622,7 +632,7 @@ HeightmapTessellator.computeVertices = function (options) {
ellipsoid
);
var transform = OrientedBoundingBox.toTransformation(orientedBoundingBox);
transform = OrientedBoundingBox.toTransformation(orientedBoundingBox);
inverseTransform = Matrix4.inverse(transform, new Matrix4());
console.timeEnd("creating oriented bounding box");
@ -701,7 +711,9 @@ HeightmapTessellator.computeVertices = function (options) {
packedOctree: packedOctree,
packedQuadtree: {
inverseTransform: inverseTransform,
transform: transform,
quadtree: quadtree,
positions: packedPositions,
},
};
};

View File

@ -2,10 +2,13 @@ import Cartesian3 from "./Cartesian3.js";
import defined from "./defined.js";
import Matrix4 from "./Matrix4.js";
import Ray from "./Ray.js";
import IntersectionTests from "./IntersectionTests.js";
function QuadtreeTrianglePicker(packedQuadtree, triangleVerticesCallback) {
this._inverseTransform = Matrix4.unpack(packedQuadtree.inverseTransform);
this._transform = Matrix4.unpack(packedQuadtree.transform);
this._quadtree = packedQuadtree.quadtree;
this._positions = packedQuadtree.positions;
this._triangleVerticesCallback = triangleVerticesCallback;
}
@ -69,6 +72,20 @@ function rayIntersectsAABB(ray, minX, minY, minZ, maxX, maxY, maxZ) {
return { intersection: true, tMin: tMin, tMax: tMax };
}
var invalidIntersection = Number.MAX_VALUE;
function rayTriangleIntersect(ray, v0, v1, v2, cullBackFaces) {
var t = IntersectionTests.rayTriangleParametric(
ray,
v0,
v1,
v2,
cullBackFaces
);
var valid = defined(t) && t >= 0.0;
return valid ? t : invalidIntersection;
}
/**
* @param {Ray} ray
* @param {Boolean} cullBackFaces
@ -132,10 +149,202 @@ QuadtreeTrianglePicker.prototype.rayIntersect = function (
return a.tMin - b.tMin;
});
var width = 256;
var height = 256;
var positions = this._positions;
var halfWidth = width / 2;
/// closest intersection point
var minT = invalidIntersection;
var testedTriangles = [];
var testedPoints = [];
// for each intersected node - test every triangle which falls in that node
for (var ii = 0; ii < sortedTests.length; ii++) {
var test = sortedTests[ii];
var numberOfQuadsAtThisLevel = Math.pow(2, test.node.level);
var offsetX = test.node.topLeft.x * numberOfQuadsAtThisLevel;
var numberOfPositionsInANodeAtThisLevel = width / numberOfQuadsAtThisLevel;
var columnStart = numberOfPositionsInANodeAtThisLevel * offsetX + halfWidth;
var columnEnd = columnStart + numberOfPositionsInANodeAtThisLevel;
// y
var offsetY = test.node.topLeft.y * numberOfQuadsAtThisLevel;
var rowStart = numberOfPositionsInANodeAtThisLevel * offsetY + halfWidth;
var rowEnd = rowStart + numberOfPositionsInANodeAtThisLevel;
for (var row = rowStart; row < rowEnd; row++) {
for (var col = columnStart; col < columnEnd; col++) {
// find all triangles which have a vertex at this position
var pos0X = positions[3 * (row * width + col - row - 1)];
var pos0Y = positions[1 + 3 * (row * width + col - row - 1)];
var pos0Z = positions[2 + 3 * (row * width + col - row - 1)];
var pos1X = positions[3 * (row * width + col - row)];
var pos1Y = positions[1 + 3 * (row * width + col - row)];
var pos1Z = positions[2 + 3 * (row * width + col - row)];
var pos2X = positions[3 * (row * width + col - row + 1)];
var pos2Y = positions[1 + 3 * (row * width + col - row + 1)];
var pos2Z = positions[2 + 3 * (row * width + col - row + 1)];
var pos3X = positions[3 * (row * width + col - 1)];
var pos3Y = positions[1 + 3 * (row * width + col - 1)];
var pos3Z = positions[2 + 3 * (row * width + col - 1)];
var pos4X = positions[3 * (row * width + col)];
var pos4Y = positions[1 + 3 * (row * width + col)];
var pos4Z = positions[2 + 3 * (row * width + col)];
var pos5X = positions[3 * (row * width + col + 1)];
var pos5Y = positions[1 + 3 * (row * width + col + 1)];
var pos5Z = positions[2 + 3 * (row * width + col + 1)];
var pos6X = positions[3 * (row * width + col + row - 1)];
var pos6Y = positions[1 + 3 * (row * width + col + row - 1)];
var pos6Z = positions[2 + 3 * (row * width + col + row - 1)];
var pos7X = positions[3 * (row * width + col + row)];
var pos7Y = positions[1 + 3 * (row * width + col + row)];
var pos7Z = positions[2 + 3 * (row * width + col + row)];
var pos8X = positions[3 * (row * width + col + row + 1)];
var pos8Y = positions[1 + 3 * (row * width + col + row + 1)];
var pos8Z = positions[2 + 3 * (row * width + col + row + 1)];
var pos0 = new Cartesian3(pos0X, pos0Y, pos0Z);
var pos1 = new Cartesian3(pos1X, pos1Y, pos1Z);
var pos2 = new Cartesian3(pos2X, pos2Y, pos2Z);
var pos3 = new Cartesian3(pos3X, pos3Y, pos3Z);
var pos4 = new Cartesian3(pos4X, pos4Y, pos4Z);
var pos5 = new Cartesian3(pos5X, pos5Y, pos5Z);
var pos6 = new Cartesian3(pos6X, pos6Y, pos6Z);
var pos7 = new Cartesian3(pos7X, pos7Y, pos7Z);
var pos8 = new Cartesian3(pos8X, pos8Y, pos8Z);
var triangle0 = [pos0, pos1, pos3];
var triangle1 = [pos1, pos2, pos4];
var triangle2 = [pos1, pos3, pos4];
var triangle3 = [pos2, pos4, pos5];
var triangle4 = [pos3, pos4, pos6];
var triangle5 = [pos4, pos5, pos7];
var triangle6 = [pos4, pos6, pos7];
var triangle7 = [pos5, pos7, pos8];
testedTriangles.push(
triangle0,
triangle1,
triangle2,
triangle3,
triangle4,
triangle5,
triangle6,
triangle7
);
testedPoints.push(pos4);
if (traceDetails) {
traceDetails.coolTriangles = testedTriangles;
traceDetails.coolPoints = testedPoints;
}
var intersects0 = rayTriangleIntersect(
ray,
triangle0[0],
triangle0[1],
triangle0[2],
cullBackFaces
);
var intersects1 = rayTriangleIntersect(
ray,
triangle1[0],
triangle1[1],
triangle1[2],
cullBackFaces
);
var intersects2 = rayTriangleIntersect(
ray,
triangle2[0],
triangle2[1],
triangle2[2],
cullBackFaces
);
var intersects3 = rayTriangleIntersect(
ray,
triangle3[0],
triangle3[1],
triangle3[2],
cullBackFaces
);
var intersects4 = rayTriangleIntersect(
ray,
triangle4[0],
triangle4[1],
triangle4[2],
cullBackFaces
);
var intersects5 = rayTriangleIntersect(
ray,
triangle5[0],
triangle5[1],
triangle5[2],
cullBackFaces
);
var intersects6 = rayTriangleIntersect(
ray,
triangle6[0],
triangle6[1],
triangle6[2],
cullBackFaces
);
var intersects7 = rayTriangleIntersect(
ray,
triangle7[0],
triangle7[1],
triangle7[2],
cullBackFaces
);
if (intersects0 !== invalidIntersection) {
minT = Math.min(intersects0, minT);
}
if (intersects1 !== invalidIntersection) {
minT = Math.min(intersects1, minT);
}
if (intersects2 !== invalidIntersection) {
minT = Math.min(intersects2, minT);
}
if (intersects3 !== invalidIntersection) {
minT = Math.min(intersects3, minT);
}
if (intersects4 !== invalidIntersection) {
minT = Math.min(intersects4, minT);
}
if (intersects5 !== invalidIntersection) {
minT = Math.min(intersects5, minT);
}
if (intersects6 !== invalidIntersection) {
minT = Math.min(intersects6, minT);
}
if (intersects7 !== invalidIntersection) {
minT = Math.min(intersects7, minT);
}
}
}
}
if (minT !== invalidIntersection) {
var result = Ray.getPoint(ray, minT);
return result;
}
return undefined;
};

View File

@ -573,7 +573,7 @@ function nodeAddTriangleToChildren(
*/
// how many axis of the current triangle are within the node's axis-aligned-bounding-box
var smallOverlapCount = 2;
var smallOverlapCount = 3;
// var childIdxLevelTraversalBitMasks = new Uint8Array([
// 0, // 0b000 x y z