qwc2
Version:
QGIS Web Client
319 lines (308 loc) • 16.9 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* Copyright 2025 Sourcepole AG
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import { connect } from 'react-redux';
import isEmpty from 'lodash.isempty';
import PropTypes from 'prop-types';
import { BufferAttribute, BufferGeometry, Float32BufferAttribute, Mesh, MeshStandardMaterial, Raycaster, Vector2 } from 'three';
import Icon from '../../components/Icon';
import SideBar from '../../components/SideBar';
import { TileMeshHelper } from '../../components/map3d/utils/MiscUtils3D';
import LocaleUtils from '../../utils/LocaleUtils';
import './style/HideObjects3D.css';
/**
* Hide objects in the 3D map.
*/
var HideObjects3D = /*#__PURE__*/function (_React$Component) {
function HideObjects3D() {
var _this;
_classCallCheck(this, HideObjects3D);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, HideObjects3D, [].concat(args));
_defineProperty(_this, "state", {
hiddenObjects: []
});
_defineProperty(_this, "renderBody", function () {
return /*#__PURE__*/React.createElement("div", {
className: "hideobjects3d-body"
}, isEmpty(_this.state.hiddenObjects) ? /*#__PURE__*/React.createElement("i", null, LocaleUtils.tr("hideobjects3d.clickonmap")) : /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
className: "hideobjects3d-list"
}, _this.state.hiddenObjects.map(function (entry) {
var _ref;
return /*#__PURE__*/React.createElement("div", {
key: entry.object.uuid + entry.featureId,
onMouseEnter: function onMouseEnter() {
return _this.showHighlight(entry);
},
onMouseLeave: function onMouseLeave() {
return _this.hideHighlight(entry);
}
}, /*#__PURE__*/React.createElement("span", null, (_ref = LocaleUtils.tr("hideobjects3d.object") + " " + entry.featureId) !== null && _ref !== void 0 ? _ref : ""), /*#__PURE__*/React.createElement(Icon, {
icon: "eye",
onClick: function onClick() {
return _this.restoreObject(entry);
},
title: LocaleUtils.tr("hideobjects3d.restore")
}));
})), /*#__PURE__*/React.createElement("div", {
className: "hideobjects3d-restorebutton"
}, /*#__PURE__*/React.createElement("button", {
className: "button",
onClick: _this.restoreAll
}, LocaleUtils.tr("hideobjects3d.restoreall")))));
});
_defineProperty(_this, "clear", function () {
_this.setState({
pickAttrs: null
});
_this.props.sceneContext.removeSceneObject("__identify3d_highlight");
});
_defineProperty(_this, "pickOnRelease", function (ev) {
if (ev.button !== 0) {
return;
}
ev.view.addEventListener('pointerup', _this.pick, {
once: true
});
ev.view.addEventListener('pointermove', function () {
ev.view.removeEventListener('pointerup', _this.pick);
}, {
once: true
});
});
_defineProperty(_this, "pick", function (ev) {
if (_this.props.enabled !== true) {
return;
}
_this.clear();
// Setup raycaster
var raycaster = new Raycaster();
raycaster.firstHitOnly = true;
var camera = _this.props.sceneContext.scene.view.camera;
var rect = ev.target.getBoundingClientRect();
var x = (ev.clientX - rect.left) / rect.width * 2 - 1;
var y = -((ev.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(new Vector2(x, y), camera);
var picks = [];
Object.entries(_this.props.sceneContext.sceneObjects).forEach(function (_ref2) {
var _object$tiles;
var _ref3 = _slicedToArray(_ref2, 2),
objId = _ref3[0],
options = _ref3[1];
if (!options.layertree || !options.visibility) {
return;
}
var object = _this.props.sceneContext.getSceneObject(objId);
if ((_object$tiles = object.tiles) !== null && _object$tiles !== void 0 && _object$tiles.raycast) {
var intersections = [];
object.tiles.raycast(raycaster, intersections);
intersections.forEach(function (entry) {
entry.isTilePick = true;
});
picks.push.apply(picks, intersections);
} else {
picks.push.apply(picks, _toConsumableArray(raycaster.intersectObjects([object], true)));
}
});
picks.sort(function (a, b) {
return a.distance - b.distance;
});
if (isEmpty(picks)) {
return;
} else if (picks[0].isTilePick) {
_this.hideTilePick(picks[0]);
} else {
_this.hideObjectPick(picks[0]);
}
});
_defineProperty(_this, "hideTilePick", function (pick) {
var _helper$getTileUserDa;
var posAttr = pick.object.geometry.getAttribute('position');
var norAttr = pick.object.geometry.getAttribute('normal');
// Ensure geometry is indexed
if (!pick.object.geometry.index) {
var indices = new Uint32Array(posAttr.count);
for (var i = 0; i < posAttr.count; i++) {
indices[i] = i;
}
pick.object.geometry.setIndex(new BufferAttribute(indices, 1));
}
if (!pick.object.userData.originalIndex) {
pick.object.userData.originalIndex = pick.object.geometry.index.array.slice();
pick.object.userData.hiddenIds = [];
}
var helper = new TileMeshHelper(pick.object);
var pickFeatureId = helper.getFeatureId(pick.face);
// Extract feature geometry
var pickPosition = [];
var pickNormal = [];
helper.forEachFeatureTriangle(pickFeatureId, function (i0, i1, i2) {
pickPosition.push(posAttr.getX(i0), posAttr.getY(i0), posAttr.getZ(i0));
pickPosition.push(posAttr.getX(i1), posAttr.getY(i1), posAttr.getZ(i1));
pickPosition.push(posAttr.getX(i2), posAttr.getY(i2), posAttr.getZ(i2));
pickNormal.push(norAttr.getX(i0), norAttr.getY(i0), norAttr.getZ(i0));
pickNormal.push(norAttr.getX(i1), norAttr.getY(i1), norAttr.getZ(i1));
pickNormal.push(norAttr.getX(i2), norAttr.getY(i2), norAttr.getZ(i2));
});
// Filter indices
var filteredIndices = pick.object.geometry.index.array.filter(function (idx) {
return helper.featureIdAttr.getX(idx) !== pickFeatureId;
});
pick.object.geometry.setIndex(new BufferAttribute(new Uint32Array(filteredIndices), 1));
// Hide label
var pickLabel = (_helper$getTileUserDa = helper.getTileUserData().tileLabels) === null || _helper$getTileUserDa === void 0 ? void 0 : _helper$getTileUserDa[pickFeatureId];
if (pickLabel) {
pickLabel.labelObject.visible = false;
}
_this.props.sceneContext.scene.notifyChange();
// Store hidden object metadata
_this.storeHiddenObject(pick, pickPosition, pickNormal, null, pickFeatureId);
});
_defineProperty(_this, "hideObjectPick", function (pick) {
pick.object.visible = false;
_this.props.sceneContext.scene.notifyChange();
var posAttr = pick.object.geometry.getAttribute('position');
var norAttr = pick.object.geometry.getAttribute('normal');
var index = pick.object.geometry.getIndex();
// Create highlight geometry
_this.storeHiddenObject(pick, posAttr.array, norAttr.array, index);
});
_defineProperty(_this, "storeHiddenObject", function (pick, position, normal) {
var index = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
var featureId = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null;
var material = new MeshStandardMaterial({
color: 0xff0000
});
var geometry = new BufferGeometry();
geometry.setAttribute('position', new Float32BufferAttribute(position, 3));
geometry.setAttribute('normal', new Float32BufferAttribute(normal, 3));
geometry.setIndex(index);
var mesh = new Mesh(geometry, material);
mesh.receiveShadow = true;
mesh.applyMatrix4(pick.object.matrixWorld);
mesh.updateMatrixWorld();
_this.setState(function (state) {
return {
hiddenObjects: [].concat(_toConsumableArray(state.hiddenObjects), [{
object: pick.object,
isTileObject: pick.isTilePick,
featureId: featureId,
highlight: mesh
}])
};
});
});
_defineProperty(_this, "showHighlight", function (entry) {
_this.props.sceneContext.addSceneObject(entry.highlight.uuid, entry.highlight);
});
_defineProperty(_this, "hideHighlight", function (entry) {
_this.props.sceneContext.removeSceneObject(entry.highlight.uuid);
});
_defineProperty(_this, "restoreObject", function (entry) {
if (entry.isTileObject) {
var _helper$getTileUserDa2;
var helper = new TileMeshHelper(entry.object);
// Re-add filtered indices
var filteredIndices = entry.object.userData.originalIndex.filter(function (i) {
return helper.featureIdAttr.getX(i) === entry.featureId;
});
var combined = new Uint32Array(entry.object.geometry.index.array.length + filteredIndices.length);
combined.set(entry.object.geometry.index.array, 0);
combined.set(new Uint32Array(filteredIndices), entry.object.geometry.index.array.length);
entry.object.geometry.setIndex(new BufferAttribute(combined, 1));
// Restore label
var pickLabel = (_helper$getTileUserDa2 = helper.getTileUserData().tileLabels) === null || _helper$getTileUserDa2 === void 0 ? void 0 : _helper$getTileUserDa2[entry.featureId];
if (pickLabel) {
pickLabel.labelObject.visible = true;
}
} else {
entry.object.visible = true;
}
_this.hideHighlight(entry);
_this.props.sceneContext.scene.notifyChange();
_this.setState(function (state) {
return {
hiddenObjects: state.hiddenObjects.filter(function (x) {
return x !== entry;
})
};
});
});
_defineProperty(_this, "restoreAll", function () {
_toConsumableArray(_this.state.hiddenObjects).forEach(_this.restoreObject);
});
return _this;
}
_inherits(HideObjects3D, _React$Component);
return _createClass(HideObjects3D, [{
key: "componentDidMount",
value: function componentDidMount() {
this.props.sceneContext.scene.viewport.addEventListener('pointerdown', this.pickOnRelease);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _prevProps$sceneConte;
if (this.props.sceneContext.scene !== ((_prevProps$sceneConte = prevProps.sceneContext) === null || _prevProps$sceneConte === void 0 ? void 0 : _prevProps$sceneConte.scene)) {
this.setState({
hiddenObjects: []
});
}
}
}, {
key: "render",
value: function render() {
var _this2 = this;
return /*#__PURE__*/React.createElement(SideBar, {
icon: "eye",
id: "HideObjects3D",
title: LocaleUtils.tr("appmenu.items.HideObjects3D"),
width: "20em"
}, function () {
return {
body: _this2.renderBody()
};
});
}
}]);
}(React.Component);
_defineProperty(HideObjects3D, "propTypes", {
enabled: PropTypes.bool,
sceneContext: PropTypes.object
});
export default connect(function (state) {
return {
enabled: state.task.id === "HideObjects3D"
};
}, {})(HideObjects3D);