123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215 |
- import * as THREE from "../libs/three.js/build/three.module.js";
- import {PointCloudTree, PointCloudTreeNode} from "./PointCloudTree.js";
- import {PointCloudOctreeGeometryNode} from "./PointCloudOctreeGeometry.js";
- import {Utils} from "./utils.js";
- import {PointCloudMaterial} from "./materials/PointCloudMaterial.js";
- export class PointCloudOctreeNode extends PointCloudTreeNode {
- constructor () {
- super();
- //this.children = {};
- this.children = [];
- this.sceneNode = null;
- this.octree = null;
-
-
-
-
- }
-
- getNumPoints () {
- return this.geometryNode.numPoints;
- }
- isLoaded () {
- return true;
- }
- isTreeNode () {
- return true;
- }
- isGeometryNode () {
- return false;
- }
- getLevel () {
- return this.geometryNode.level;
- }
- getBoundingSphere () {
- return this.geometryNode.boundingSphere;
- }
- getBoundingBox () {
- return this.geometryNode.boundingBox;
- }
- getChildren () {
- let children = [];
- for (let i = 0; i < 8; i++) {
- if (this.children[i]) {
- children.push(this.children[i]);
- }
- }
- return children;
- }
- getPointsInBox(boxNode){
- if(!this.sceneNode){
- return null;
- }
- let buffer = this.geometryNode.buffer;
- let posOffset = buffer.offset("position");
- let stride = buffer.stride;
- let view = new DataView(buffer.data);
- let worldToBox = boxNode.matrixWorld.clone().invert();
- let objectToBox = new THREE.Matrix4().multiplyMatrices(worldToBox, this.sceneNode.matrixWorld);
- let inBox = [];
- let pos = new THREE.Vector4();
- for(let i = 0; i < buffer.numElements; i++){
- let x = view.getFloat32(i * stride + posOffset + 0, true);
- let y = view.getFloat32(i * stride + posOffset + 4, true);
- let z = view.getFloat32(i * stride + posOffset + 8, true);
- pos.set(x, y, z, 1);
- pos.applyMatrix4(objectToBox);
- if(-0.5 < pos.x && pos.x < 0.5){
- if(-0.5 < pos.y && pos.y < 0.5){
- if(-0.5 < pos.z && pos.z < 0.5){
- pos.set(x, y, z, 1).applyMatrix4(this.sceneNode.matrixWorld);
- inBox.push(new THREE.Vector3(pos.x, pos.y, pos.z));
- }
- }
- }
- }
- return inBox;
- }
- get name () {
- return this.geometryNode.name;
- }
- };
- export class PointCloudOctree extends PointCloudTree {
- constructor (geometry, material) {
- super();
-
- this.pointBudget = Infinity;
- this.pcoGeometry = geometry;
- this.boundingBox = this.pcoGeometry.boundingBox;
- this.boundingSphere = this.boundingBox.getBoundingSphere(new THREE.Sphere());
- this.material = material || new PointCloudMaterial();
- this.visiblePointsTarget = 2 * 1000 * 1000;
- this.minimumNodePixelSize = 150;
- this.level = 0;
- this.position.copy(geometry.offset);
- this.updateMatrix();
-
-
- //add
- this.rotateMatrix = new THREE.Matrix4;
- this.transformMatrix = new THREE.Matrix4;// 数据集的变化矩阵
- this.transformInvMatrix = new THREE.Matrix4;
-
-
- {
- let priorityQueue = ["rgba", "rgb", "intensity", "classification"];
- let selected = "rgba";
- for(let attributeName of priorityQueue){
- let attribute = this.pcoGeometry.pointAttributes.attributes.find(a => a.name === attributeName);
- if(!attribute){
- continue;
- }
- let min = attribute.range[0].constructor.name === "Array" ? attribute.range[0] : [attribute.range[0]];
- let max = attribute.range[1].constructor.name === "Array" ? attribute.range[1] : [attribute.range[1]];
- let range_min = new THREE.Vector3(...min);
- let range_max = new THREE.Vector3(...max);
- let range = range_min.distanceTo(range_max);
- if(range === 0){
- continue;
- }
- selected = attributeName;
- break;
- }
- this.material.activeAttributeName = selected;
- }
- this.showBoundingBox = false;
- this.boundingBoxNodes = [];
- this.loadQueue = [];
- this.visibleBounds = new THREE.Box3();
- this.visibleNodes = [];
- this.visibleGeometry = [];
- this.generateDEM = false;
- this.profileRequests = [];
- this.name = '';
- this._visible = true;
- //this._isVisible = true//add
- //this.unvisibleReasons = []
-
- {
- let box = [this.pcoGeometry.tightBoundingBox, this.getBoundingBoxWorld()]
- .find(v => v !== undefined);
- this.updateMatrixWorld(true);
- box = Utils.computeTransformedBoundingBox(box, this.matrixWorld);
- let bMin = box.min.z;
- let bMax = box.max.z;
- this.material.heightMin = bMin;
- this.material.heightMax = bMax;
- }
- // TODO read projection from file instead
- this.projection = geometry.projection;
- this.fallbackProjection = geometry.fallbackProjection;
- this.root = this.pcoGeometry.root;
- }
- setName (name) {
- if (this.name !== name) {
- this.name = name;
- this.dispatchEvent({type: 'name_changed', name: name, pointcloud: this});
- }
- }
- getName () {
- return this.name;
- }
- getAttribute(name){
- const attribute = this.pcoGeometry.pointAttributes.attributes.find(a => a.name === name);
- if(attribute){
- return attribute;
- }else{
- return null;
- }
- }
- getAttributes(){
- return this.pcoGeometry.pointAttributes;
- }
- toTreeNode (geometryNode, parent) {
- let node = new PointCloudOctreeNode();
- // if(geometryNode.name === "r40206"){
- // console.log("creating node for r40206");
- // }
- let sceneNode = new THREE.Points(geometryNode.geometry, this.material);
- sceneNode.name = geometryNode.name;
- sceneNode.position.copy(geometryNode.boundingBox.min);
- sceneNode.frustumCulled = false;
- sceneNode.onBeforeRender = (_this, scene, camera, geometry, material, group) => {
- if (material.program) {
- _this.getContext().useProgram(material.program.program);
- if (material.program.getUniforms().map.level) {
- let level = geometryNode.getLevel();
- material.uniforms.level.value = level;
- material.program.getUniforms().map.level.setValue(_this.getContext(), level);
- }
- if (this.visibleNodeTextureOffsets && material.program.getUniforms().map.vnStart) {
- let vnStart = this.visibleNodeTextureOffsets.get(node);
- material.uniforms.vnStart.value = vnStart;
- material.program.getUniforms().map.vnStart.setValue(_this.getContext(), vnStart);
- }
- if (material.program.getUniforms().map.pcIndex) {
- let i = node.pcIndex ? node.pcIndex : this.visibleNodes.indexOf(node);
- material.uniforms.pcIndex.value = i;
- material.program.getUniforms().map.pcIndex.setValue(_this.getContext(), i);
- }
- }
- };
- // { // DEBUG
- // let sg = new THREE.SphereGeometry(1, 16, 16);
- // let sm = new THREE.MeshNormalMaterial();
- // let s = new THREE.Mesh(sg, sm);
- // s.scale.set(5, 5, 5);
- // s.position.copy(geometryNode.mean)
- // .add(this.position)
- // .add(geometryNode.boundingBox.min);
- //
- // viewer.scene.scene.add(s);
- // }
- node.geometryNode = geometryNode;
- node.sceneNode = sceneNode;
- node.pointcloud = this;
- node.children = [];
- //for (let key in geometryNode.children) {
- // node.children[key] = geometryNode.children[key];
- //}
- for(let i = 0; i < 8; i++){
- node.children[i] = geometryNode.children[i];
- }
- if (!parent) {
- this.root = node;
- this.add(sceneNode);
- } else {
- let childIndex = parseInt(geometryNode.name[geometryNode.name.length - 1]);
- parent.sceneNode.add(sceneNode);
- parent.children[childIndex] = node;
- }
- let disposeListener = function () {
- let childIndex = parseInt(geometryNode.name[geometryNode.name.length - 1]);
- parent.sceneNode.remove(node.sceneNode);
- parent.children[childIndex] = geometryNode;
- };
- geometryNode.oneTimeDisposeHandlers.push(disposeListener);
- return node;
- }
- updateVisibleBounds () {
- let leafNodes = [];
- for (let i = 0; i < this.visibleNodes.length; i++) {
- let node = this.visibleNodes[i];
- let isLeaf = true;
- for (let j = 0; j < node.children.length; j++) {
- let child = node.children[j];
- if (child instanceof PointCloudOctreeNode) {
- isLeaf = isLeaf && !child.sceneNode.visible;
- } else if (child instanceof PointCloudOctreeGeometryNode) {
- isLeaf = true;
- }
- }
- if (isLeaf) {
- leafNodes.push(node);
- }
- }
- this.visibleBounds.min = new THREE.Vector3(Infinity, Infinity, Infinity);
- this.visibleBounds.max = new THREE.Vector3(-Infinity, -Infinity, -Infinity);
- for (let i = 0; i < leafNodes.length; i++) {
- let node = leafNodes[i];
- this.visibleBounds.expandByPoint(node.getBoundingBox().min);
- this.visibleBounds.expandByPoint(node.getBoundingBox().max);
- }
- }
- updateMaterial (material, visibleNodes, camera, renderer) {
- material.fov = camera.fov * (Math.PI / 180);
- material.screenWidth = renderer.domElement.clientWidth;
- material.screenHeight = renderer.domElement.clientHeight;
- material.spacing = this.pcoGeometry.spacing; // * Math.max(this.scale.x, this.scale.y, this.scale.z);
- material.near = camera.near;
- material.far = camera.far;
- material.uniforms.octreeSize.value = this.pcoGeometry.boundingBox.getSize(new THREE.Vector3()).x;
- }
- computeVisibilityTextureData(nodes, camera){
- if(Potree.measureTimings) performance.mark("computeVisibilityTextureData-start");
- let data = new Uint8Array(nodes.length * 4);
- let visibleNodeTextureOffsets = new Map();
- // copy array
- nodes = nodes.slice();
- // sort by level and index, e.g. r, r0, r3, r4, r01, r07, r30, ...
- let sort = function (a, b) {
- let na = a.geometryNode.name;
- let nb = b.geometryNode.name;
- if (na.length !== nb.length) return na.length - nb.length;
- if (na < nb) return -1;
- if (na > nb) return 1;
- return 0;
- };
- nodes.sort(sort);
- let worldDir = new THREE.Vector3();
- let nodeMap = new Map();
- let offsetsToChild = new Array(nodes.length).fill(Infinity);
- for(let i = 0; i < nodes.length; i++){
- let node = nodes[i];
- nodeMap.set(node.name, node);
- visibleNodeTextureOffsets.set(node, i);
- if(i > 0){
- let index = parseInt(node.name.slice(-1));
- let parentName = node.name.slice(0, -1);
- let parent = nodeMap.get(parentName);
- let parentOffset = visibleNodeTextureOffsets.get(parent);
- let parentOffsetToChild = (i - parentOffset);
- offsetsToChild[parentOffset] = Math.min(offsetsToChild[parentOffset], parentOffsetToChild);
- data[parentOffset * 4 + 0] = data[parentOffset * 4 + 0] | (1 << index);
- data[parentOffset * 4 + 1] = (offsetsToChild[parentOffset] >> 8);
- data[parentOffset * 4 + 2] = (offsetsToChild[parentOffset] % 256);
- }
- let density = node.geometryNode.density;
-
- if(typeof density === "number"){
- let lodOffset = Math.log2(density) / 2 - 1.5;
- let offsetUint8 = (lodOffset + 10) * 10;
- data[i * 4 + 3] = offsetUint8;
- }else{
- data[i * 4 + 3] = 100;
- }
- }
- if(Potree.measureTimings){
- performance.mark("computeVisibilityTextureData-end");
- performance.measure("render.computeVisibilityTextureData", "computeVisibilityTextureData-start", "computeVisibilityTextureData-end");
- }
- return {
- data: data,
- offsets: visibleNodeTextureOffsets
- };
- }
- nodeIntersectsProfile (node, profile) {
- let bbWorld = node.boundingBox.clone().applyMatrix4(this.matrixWorld);
- let bsWorld = bbWorld.getBoundingSphere(new THREE.Sphere());
- let intersects = false;
- for (let i = 0; i < profile.points.length - 1; i++) {
- let start = new THREE.Vector3(profile.points[i + 0].x, profile.points[i + 0].y, bsWorld.center.z);
- let end = new THREE.Vector3(profile.points[i + 1].x, profile.points[i + 1].y, bsWorld.center.z);
- let closest = new THREE.Line3(start, end).closestPointToPoint(bsWorld.center, true, new THREE.Vector3());
- let distance = closest.distanceTo(bsWorld.center);
- intersects = intersects || (distance < (bsWorld.radius + profile.width));
- }
- //console.log(`${node.name}: ${intersects}`);
- return intersects;
- }
- deepestNodeAt(position){
-
- const toObjectSpace = this.matrixWorld.clone().invert();
- const objPos = position.clone().applyMatrix4(toObjectSpace);
- let current = this.root;
- while(true){
- let containingChild = null;
- for(const child of current.children){
- if(child !== undefined){
- if(child.getBoundingBox().containsPoint(objPos)){
- containingChild = child;
- }
- }
- }
- if(containingChild !== null && containingChild instanceof PointCloudOctreeNode){
- current = containingChild;
- }else{
- break;
- }
- }
- const deepest = current;
- return deepest;
- }
- nodesOnRay (nodes, ray) {
- let nodesOnRay = [];
- let _ray = ray.clone();
- for (let i = 0; i < nodes.length; i++) {
- let node = nodes[i];
- let sphere = node.getBoundingSphere().clone().applyMatrix4(this.matrixWorld);
- if (_ray.intersectsSphere(sphere)) {
- nodesOnRay.push(node);
- }
- }
- return nodesOnRay;
- }
- updateMatrixWorld (force) {
- if (this.matrixAutoUpdate === true) this.updateMatrix();
- if (this.matrixWorldNeedsUpdate === true || force === true) {
- if (!this.parent) {
- this.matrixWorld.copy(this.matrix);
- } else {
- this.matrixWorld.multiplyMatrices(this.parent.matrixWorld, this.matrix);
- }
- this.matrixWorldNeedsUpdate = false;
- force = true;
- }
- }
- hideDescendants (object) {
- let stack = [];
- for (let i = 0; i < object.children.length; i++) {
- let child = object.children[i];
- if (child.visible) {
- stack.push(child);
- }
- }
- while (stack.length > 0) {
- let object = stack.shift();
- object.visible = false;
- for (let i = 0; i < object.children.length; i++) {
- let child = object.children[i];
- if (child.visible) {
- stack.push(child);
- }
- }
- }
- }
- moveToOrigin () {
- this.position.set(0, 0, 0);
- this.updateMatrixWorld(true);
- let box = this.boundingBox;
- let transform = this.matrixWorld;
- let tBox = Utils.computeTransformedBoundingBox(box, transform);
- this.position.set(0, 0, 0).sub(tBox.getCenter(new THREE.Vector3()));
- };
- moveToGroundPlane () {
- this.updateMatrixWorld(true);
- let box = this.boundingBox;
- let transform = this.matrixWorld;
- let tBox = Utils.computeTransformedBoundingBox(box, transform);
- this.position.y += -tBox.min.y;
- };
- getBoundingBoxWorld () {
- this.updateMatrixWorld(true);
- let box = this.boundingBox;
- let transform = this.matrixWorld;
- let tBox = Utils.computeTransformedBoundingBox(box, transform);
- return tBox;
- };
- /**
- * returns points inside the profile points
- *
- * maxDepth: search points up to the given octree depth
- *
- *
- * The return value is an array with all segments of the profile path
- * let segment = {
- * start: THREE.Vector3,
- * end: THREE.Vector3,
- * points: {}
- * project: function()
- * };
- *
- * The project() function inside each segment can be used to transform
- * that segments point coordinates to line up along the x-axis.
- *
- *
- */
- getPointsInProfile (profile, maxDepth, callback) {
- if (callback) {
- let request = new Potree.ProfileRequest(this, profile, maxDepth, callback);
- this.profileRequests.push(request);
- return request;
- }
- let points = {
- segments: [],
- boundingBox: new THREE.Box3(),
- projectedBoundingBox: new THREE.Box2()
- };
- // evaluate segments
- for (let i = 0; i < profile.points.length - 1; i++) {
- let start = profile.points[i];
- let end = profile.points[i + 1];
- let ps = this.getProfile(start, end, profile.width, maxDepth);
- let segment = {
- start: start,
- end: end,
- points: ps,
- project: null
- };
- points.segments.push(segment);
- points.boundingBox.expandByPoint(ps.boundingBox.min);
- points.boundingBox.expandByPoint(ps.boundingBox.max);
- }
- // add projection functions to the segments
- let mileage = new THREE.Vector3();
- for (let i = 0; i < points.segments.length; i++) {
- let segment = points.segments[i];
- let start = segment.start;
- let end = segment.end;
- let project = (function (_start, _end, _mileage, _boundingBox) {
- let start = _start;
- let end = _end;
- let mileage = _mileage;
- let boundingBox = _boundingBox;
- let xAxis = new THREE.Vector3(1, 0, 0);
- let dir = new THREE.Vector3().subVectors(end, start);
- dir.y = 0;
- dir.normalize();
- let alpha = Math.acos(xAxis.dot(dir));
- if (dir.z > 0) {
- alpha = -alpha;
- }
- return function (position) {
- let toOrigin = new THREE.Matrix4().makeTranslation(-start.x, -boundingBox.min.y, -start.z);
- let alignWithX = new THREE.Matrix4().makeRotationY(-alpha);
- let applyMileage = new THREE.Matrix4().makeTranslation(mileage.x, 0, 0);
- let pos = position.clone();
- pos.applyMatrix4(toOrigin);
- pos.applyMatrix4(alignWithX);
- pos.applyMatrix4(applyMileage);
- return pos;
- };
- }(start, end, mileage.clone(), points.boundingBox.clone()));
- segment.project = project;
- mileage.x += new THREE.Vector3(start.x, 0, start.z).distanceTo(new THREE.Vector3(end.x, 0, end.z));
- mileage.y += end.y - start.y;
- }
- points.projectedBoundingBox.min.x = 0;
- points.projectedBoundingBox.min.y = points.boundingBox.min.y;
- points.projectedBoundingBox.max.x = mileage.x;
- points.projectedBoundingBox.max.y = points.boundingBox.max.y;
- return points;
- }
- /**
- * returns points inside the given profile bounds.
- *
- * start:
- * end:
- * width:
- * depth: search points up to the given octree depth
- * callback: if specified, points are loaded before searching
- *
- *
- */
- getProfile (start, end, width, depth, callback) {
- let request = new Potree.ProfileRequest(start, end, width, depth, callback);
- this.profileRequests.push(request);
- };
- getVisibleExtent () {
- return this.visibleBounds.applyMatrix4(this.matrixWorld);
- };
- intersectsPoint(position){
- let rootAvailable = this.pcoGeometry.root && this.pcoGeometry.root.geometry;
- if(!rootAvailable){
- return false;
- }
- if(typeof this.signedDistanceField === "undefined"){
- const resolution = 32;
- const field = new Float32Array(resolution ** 3).fill(Infinity);
- const positions = this.pcoGeometry.root.geometry.attributes.position;
- const boundingBox = this.boundingBox;
- const n = positions.count;
- for(let i = 0; i < n; i = i + 3){
- const x = positions.array[3 * i + 0];
- const y = positions.array[3 * i + 1];
- const z = positions.array[3 * i + 2];
- const ix = parseInt(Math.min(resolution * (x / boundingBox.max.x), resolution - 1));
- const iy = parseInt(Math.min(resolution * (y / boundingBox.max.y), resolution - 1));
- const iz = parseInt(Math.min(resolution * (z / boundingBox.max.z), resolution - 1));
- const index = ix + iy * resolution + iz * resolution * resolution;
- field[index] = 0;
- }
- const sdf = {
- resolution: resolution,
- field: field,
- };
- this.signedDistanceField = sdf;
- }
- {
- const sdf = this.signedDistanceField;
- const boundingBox = this.boundingBox;
- const toObjectSpace = this.matrixWorld.clone().invert();
- const objPos = position.clone().applyMatrix4(toObjectSpace);
- const resolution = sdf.resolution;
- const ix = parseInt(resolution * (objPos.x / boundingBox.max.x));
- const iy = parseInt(resolution * (objPos.y / boundingBox.max.y));
- const iz = parseInt(resolution * (objPos.z / boundingBox.max.z));
- if(ix < 0 || iy < 0 || iz < 0){
- return false;
- }
- if(ix >= resolution || iy >= resolution || iz >= resolution){
- return false;
- }
- const index = ix + iy * resolution + iz * resolution * resolution;
- const value = sdf.field[index];
- if(value === 0){
- return true;
- }
- }
- return false;
- }
- /**
- *
- *
- *
- * params.pickWindowSize: Look for points inside a pixel window of this size.
- * Use odd values: 1, 3, 5, ...
- *
- *
- * TODO: only draw pixels that are actually read with readPixels().
- *
- */
- pick(viewer, camera, ray, params = {}){
- let renderer = viewer.renderer;
- let pRenderer = viewer.pRenderer;
- performance.mark("pick-start");
- let getVal = (a, b) => a !== undefined ? a : b;
- let pickWindowSize = getVal(params.pickWindowSize, 65); //拾取像素边长
- let pickOutsideClipRegion = getVal(params.pickOutsideClipRegion, false);
- let size = renderer.getSize(new THREE.Vector2());
- let width = Math.ceil(getVal(params.width, size.width)); //renderTarget大小。影响识别精度
- let height = Math.ceil(getVal(params.height, size.height));
-
-
-
- let pointSizeType = getVal(params.pointSizeType, this.material.pointSizeType);
- let pointSize = getVal(params.pointSize, this.material.size);
- let nodes = this.nodesOnRay(this.visibleNodes, ray);
- if (nodes.length === 0) {
- return null;
- }
- if (!this.pickState) {
- let scene = new THREE.Scene();
- let material = new Potree.PointCloudMaterial();
- material.activeAttributeName = "indices";
- let renderTarget = new THREE.WebGLRenderTarget(
- 1, 1,
- { minFilter: THREE.LinearFilter,
- magFilter: THREE.NearestFilter,
- format: THREE.RGBAFormat }
- );
- this.pickState = {
- renderTarget: renderTarget,
- material: material,
- scene: scene
- };
- };
- let pickState = this.pickState;
- let pickMaterial = pickState.material;
- { // update pick material
- pickMaterial.pointSizeType = pointSizeType;
- //pickMaterial.shape = this.material.shape;
- pickMaterial.shape = Potree.PointShape.PARABOLOID;
- pickMaterial.uniforms.uFilterReturnNumberRange.value = this.material.uniforms.uFilterReturnNumberRange.value;
- pickMaterial.uniforms.uFilterNumberOfReturnsRange.value = this.material.uniforms.uFilterNumberOfReturnsRange.value;
- pickMaterial.uniforms.uFilterGPSTimeClipRange.value = this.material.uniforms.uFilterGPSTimeClipRange.value;
- pickMaterial.uniforms.uFilterPointSourceIDClipRange.value = this.material.uniforms.uFilterPointSourceIDClipRange.value;
- pickMaterial.activeAttributeName = "indices";
- pickMaterial.size = pointSize;
- pickMaterial.uniforms.minSize.value = this.material.uniforms.minSize.value;
- pickMaterial.uniforms.maxSize.value = this.material.uniforms.maxSize.value;
- pickMaterial.classification = this.material.classification;
- pickMaterial.recomputeClassification();
- if(params.pickClipped){
- pickMaterial.clipBoxes = this.material.clipBoxes;
- pickMaterial.uniforms.clipBoxes = this.material.uniforms.clipBoxes;
- if(this.material.clipTask === Potree.ClipTask.HIGHLIGHT){
- pickMaterial.clipTask = Potree.ClipTask.NONE;
- }else{
- pickMaterial.clipTask = this.material.clipTask;
- }
- pickMaterial.clipMethod = this.material.clipMethod;
- }else{
- pickMaterial.clipBoxes = [];
- }
- this.updateMaterial(pickMaterial, nodes, camera, renderer);
- }
- pickState.renderTarget.setSize(width, height);
- let pixelPos = new THREE.Vector2(params.x, params.y);
- let gl = renderer.getContext();
- gl.enable(gl.SCISSOR_TEST);
- gl.scissor(
- parseInt(pixelPos.x - (pickWindowSize - 1) / 2),
- parseInt(pixelPos.y - (pickWindowSize - 1) / 2),
- parseInt(pickWindowSize), parseInt(pickWindowSize));
- renderer.state.buffers.depth.setTest(pickMaterial.depthTest);
- renderer.state.buffers.depth.setMask(pickMaterial.depthWrite);
- renderer.state.setBlending(THREE.NoBlending);
- { // RENDER
- renderer.setRenderTarget(pickState.renderTarget);
- gl.clearColor(0, 0, 0, 0);
- renderer.clear(true, true, true);
- let tmp = this.material;
- this.material = pickMaterial;
- pRenderer.renderOctree(this, nodes, camera, pickState.renderTarget);
- this.material = tmp;
- }
- let clamp = (number, min, max) => Math.min(Math.max(min, number), max);
- let x = parseInt(clamp(pixelPos.x - (pickWindowSize - 1) / 2, 0, width));
- let y = parseInt(clamp(pixelPos.y - (pickWindowSize - 1) / 2, 0, height));
- /* let w = parseInt(Math.min(x + pickWindowSize, width) - x);
- let h = parseInt(Math.min(y + pickWindowSize, height) - y); */
-
- let pixelCount = pickWindowSize * pickWindowSize//w * h;
- let buffer = new Uint8Array(4 * pixelCount);
- //w<pickWindowSize会报错
-
- gl.readPixels(x, y, pickWindowSize, pickWindowSize, gl.RGBA, gl.UNSIGNED_BYTE, buffer);
- renderer.setRenderTarget(null);
- renderer.state.reset();
- renderer.setScissorTest(false);
- gl.disable(gl.SCISSOR_TEST);
- let pixels = buffer;
- let ibuffer = new Uint32Array(buffer.buffer);
- // find closest hit inside pixelWindow boundaries
- let min = Number.MAX_VALUE;
- let hits = [];
- for (let u = 0; u < pickWindowSize; u++) {
- for (let v = 0; v < pickWindowSize; v++) {
- let offset = (u + v * pickWindowSize);
- let distance = Math.pow(u - (pickWindowSize - 1) / 2, 2) + Math.pow(v - (pickWindowSize - 1) / 2, 2);
- let pcIndex = pixels[4 * offset + 3];
- pixels[4 * offset + 3] = 0;
- let pIndex = ibuffer[offset];
- if(!(pcIndex === 0 && pIndex === 0) && (pcIndex !== undefined) && (pIndex !== undefined)){
- let hit = {
- pIndex: pIndex,
- pcIndex: pcIndex,
- distanceToCenter: distance
- };
- if(params.all){
- hits.push(hit);
- }else{
- if(hits.length > 0){
- if(distance < hits[0].distanceToCenter){
- hits[0] = hit;
- }
- }else{
- hits.push(hit);
- }
- }
- }
- }
- }
-
- // { // DEBUG: show panel with pick image
- // let img = Utils.pixelsArrayToImage(buffer, w, h);
- // let screenshot = img.src;
-
- // if(!this.debugDIV){
- // this.debugDIV = $(`
- // <div id="pickDebug"
- // style="position: absolute;
- // right: 400px; width: 300px;
- // bottom: 44px; width: 300px;
- // z-index: 1000;
- // "></div>`);
- // $(document.body).append(this.debugDIV);
- // }
-
- // this.debugDIV.empty();
- // this.debugDIV.append($(`<img src="${screenshot}"
- // style="transform: scaleY(-1); width: 300px"/>`));
- // //$(this.debugWindow.document).append($(`<img src="${screenshot}"/>`));
- // //this.debugWindow.document.write('<img src="'+screenshot+'"/>');
- // }
- for(let hit of hits){
- let point = {};
- if (!nodes[hit.pcIndex]) {
- return null;
- }
- let node = nodes[hit.pcIndex];
- let pc = node.sceneNode;
- let geometry = node.geometryNode.geometry;
- for(let attributeName in geometry.attributes){
- let attribute = geometry.attributes[attributeName];
- if (attributeName === 'position') {
- let x = attribute.array[3 * hit.pIndex + 0];
- let y = attribute.array[3 * hit.pIndex + 1];
- let z = attribute.array[3 * hit.pIndex + 2];
- let position = new THREE.Vector3(x, y, z);
- position.applyMatrix4(pc.matrixWorld);
- point[attributeName] = position;
- } else if (attributeName === 'indices') {
- } else {
- let values = attribute.array.slice(attribute.itemSize * hit.pIndex, attribute.itemSize * (hit.pIndex + 1)) ;
- if(attribute.potree){
- const {scale, offset} = attribute.potree;
- values = values.map(v => v / scale + offset);
- }
- point[attributeName] = values;
- //debugger;
- //if (values.itemSize === 1) {
- // point[attribute.name] = values.array[hit.pIndex];
- //} else {
- // let value = [];
- // for (let j = 0; j < values.itemSize; j++) {
- // value.push(values.array[values.itemSize * hit.pIndex + j]);
- // }
- // point[attribute.name] = value;
- //}
- }
- }
- hit.point = point;
- }
- performance.mark("pick-end");
- performance.measure("pick", "pick-start", "pick-end");
- if(params.all){
- return hits.map(hit => hit.point);
- }else{
- if(hits.length === 0){
- return null;
- }else{
- return hits[0].point;
- //let sorted = hits.sort( (a, b) => a.distanceToCenter - b.distanceToCenter);
- //return sorted[0].point;
- }
- }
- };
- * getFittedBoxGen(boxNode){
- let start = performance.now();
- let shrinkedLocalBounds = new THREE.Box3();
- let worldToBox = boxNode.matrixWorld.clone().invert();
- for(let node of this.visibleNodes){
- if(!node.sceneNode){
- continue;
- }
- let buffer = node.geometryNode.buffer;
- let posOffset = buffer.offset("position");
- let stride = buffer.stride;
- let view = new DataView(buffer.data);
- let objectToBox = new THREE.Matrix4().multiplyMatrices(worldToBox, node.sceneNode.matrixWorld);
- let pos = new THREE.Vector4();
- for(let i = 0; i < buffer.numElements; i++){
- let x = view.getFloat32(i * stride + posOffset + 0, true);
- let y = view.getFloat32(i * stride + posOffset + 4, true);
- let z = view.getFloat32(i * stride + posOffset + 8, true);
- pos.set(x, y, z, 1);
- pos.applyMatrix4(objectToBox);
- if(-0.5 < pos.x && pos.x < 0.5){
- if(-0.5 < pos.y && pos.y < 0.5){
- if(-0.5 < pos.z && pos.z < 0.5){
- shrinkedLocalBounds.expandByPoint(pos);
- }
- }
- }
- }
- yield;
- }
- let fittedPosition = shrinkedLocalBounds.getCenter(new THREE.Vector3()).applyMatrix4(boxNode.matrixWorld);
- let fitted = new THREE.Object3D();
- fitted.position.copy(fittedPosition);
- fitted.scale.copy(boxNode.scale);
- fitted.rotation.copy(boxNode.rotation);
- let ds = new THREE.Vector3().subVectors(shrinkedLocalBounds.max, shrinkedLocalBounds.min);
- fitted.scale.multiply(ds);
- let duration = performance.now() - start;
- console.log("duration: ", duration);
- yield fitted;
- }
- getFittedBox(boxNode, maxLevel = Infinity){
- maxLevel = Infinity;
- let start = performance.now();
- let shrinkedLocalBounds = new THREE.Box3();
- let worldToBox = boxNode.matrixWorld.clone().invert();
- for(let node of this.visibleNodes){
- if(!node.sceneNode || node.getLevel() > maxLevel){
- continue;
- }
- let buffer = node.geometryNode.buffer;
- let posOffset = buffer.offset("position");
- let stride = buffer.stride;
- let view = new DataView(buffer.data);
- let objectToBox = new THREE.Matrix4().multiplyMatrices(worldToBox, node.sceneNode.matrixWorld);
- let pos = new THREE.Vector4();
- for(let i = 0; i < buffer.numElements; i++){
- let x = view.getFloat32(i * stride + posOffset + 0, true);
- let y = view.getFloat32(i * stride + posOffset + 4, true);
- let z = view.getFloat32(i * stride + posOffset + 8, true);
- pos.set(x, y, z, 1);
- pos.applyMatrix4(objectToBox);
- if(-0.5 < pos.x && pos.x < 0.5){
- if(-0.5 < pos.y && pos.y < 0.5){
- if(-0.5 < pos.z && pos.z < 0.5){
- shrinkedLocalBounds.expandByPoint(pos);
- }
- }
- }
- }
- }
- let fittedPosition = shrinkedLocalBounds.getCenter(new THREE.Vector3()).applyMatrix4(boxNode.matrixWorld);
- let fitted = new THREE.Object3D();
- fitted.position.copy(fittedPosition);
- fitted.scale.copy(boxNode.scale);
- fitted.rotation.copy(boxNode.rotation);
- let ds = new THREE.Vector3().subVectors(shrinkedLocalBounds.max, shrinkedLocalBounds.min);
- fitted.scale.multiply(ds);
- let duration = performance.now() - start;
- console.log("duration: ", duration);
- return fitted;
- }
- get progress () {
- return this.visibleNodes.length / this.visibleGeometry.length;
- }
- find(name){
- let node = null;
- for(let char of name){
- if(char === "r"){
- node = this.root;
- }else{
- node = node.children[char];
- }
- }
- return node;
- }
- get visible(){
- return this._visible;
- }
- set visible(value){
- if(value !== this._visible){
- this._visible = value;
- this.dispatchEvent({type: 'visibility_changed', pointcloud: this});
- }
- }
-
-
- //数据集的显示影响到其下的:点云、marker .不会影响地图上的显示
-
- /*
- updateVisible(reason, ifShow){//当所有加入的条件都不为false时才显示
- if(ifShow){
- var index = this.unvisibleReasons.indexOf(reason);
- index > -1 && this.unvisibleReasons.splice(index, 1);
- if(this.unvisibleReasons.length == 0){
- this.visible = true;
- this.dispatchEvent({
- type: 'isVisible'
- visible:true
- })
- }
- }else {
- if(!this.unvisibleReasons.includes(reason)) this.unvisibleReasons.push(reason);
- this.visible = false;
- this.dispatchEvent({
- type: 'isVisible'
- visible:false
- })
- }
-
- }
-
- getVisible(reason){//获取在某条件下是否可见. 注: 用户在数据集选择可不可见为"datasetSelection"
- if(this.visible)return true
- else{
- return !this.unvisibleReasons.includes(reason)
- }
- } */
-
-
- /* get isVisible(){//add 手动在数据集选择是否显示(和是否全景图、隐藏点云无关)
- return this._isVisible
- }
- set isVisible(visi){
-
- if(!visi)this.visible = false
-
-
-
- this._isVisible = visi
- } */
- }
|