kitchen-simulator
Version:
It is a kitchen simulator (self-contained micro-frontend).
890 lines (878 loc) • 38 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createMonldingGroup = createMonldingGroup;
exports.getAllMoldingGroups = getAllMoldingGroups;
exports.getItemGroupFromMolding = getItemGroupFromMolding;
exports.getItemRect = getItemRect;
exports.getLinesFromItems = getLinesFromItems;
exports.getLinesFromItems2 = getLinesFromItems2;
exports.getLinesOfItem = getLinesOfItem;
exports.hasMoldingLayout = hasMoldingLayout;
exports.isEnableItemForMolding = isEnableItemForMolding;
exports.isItemSameItemByLocation = isItemSameItemByLocation;
exports.isItemSnappedGroup = isItemSnappedGroup;
exports.isItemSnappedItem = isItemSnappedItem;
exports.isLinesOverlapped = isLinesOverlapped;
exports.isSameMoldingLayoutpos = isSameMoldingLayoutpos;
exports.mergeOverlappedLines = mergeOverlappedLines;
exports.sortItemsByDistance = sortItemsByDistance;
exports.tryMergeMDItem = tryMergeMDItem;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _constants = require("../constants");
var Three = _interopRequireWildcard(require("three"));
var _convertUnitsLite = require("./convert-units-lite");
var _idBroker = _interopRequireDefault(require("./id-broker"));
var _utils = require("../components/viewer2d/utils");
var _export = require("./export");
var _helper = require("./helper");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
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 getItemRect(item) {
var x = item.x;
var y = item.y;
var rotRad = item.rotation / 180 * Math.PI;
var itemWidth = item.properties.get('width').get('_length');
var itemWidthUnit = item.properties.get('width').get('_unit') || 'cm';
itemWidth = (0, _convertUnitsLite.convert)(itemWidth / 2).from(itemWidthUnit).to('cm');
var itemDepth = item.properties.get('depth').get('_length');
var itemDepthUnit = item.properties.get('depth').get('_unit') || 'cm';
itemDepth = (0, _convertUnitsLite.convert)(itemDepth / 2).from(itemDepthUnit).to('cm');
var mx = x - itemWidth * Math.cos(rotRad);
var my = y - itemWidth * Math.sin(rotRad);
var x0 = mx + itemDepth * Math.sin(rotRad);
var y0 = my - itemDepth * Math.cos(rotRad);
var x3 = mx * 2 - x0;
var y3 = my * 2 - y0;
var x1 = x * 2 - x3;
var y1 = y * 2 - y3;
var x2 = x * 2 - x0;
var y2 = y * 2 - y0;
return {
rect: [{
x: x0,
y: y0
}, {
x: x1,
y: y1
}, {
x: x2,
y: y2
}, {
x: x3,
y: y3
}]
};
}
function hasMoldingLayout(molding, layoutpos) {
var types = molding === null || molding === void 0 ? void 0 : molding.molding_type;
return (Array.isArray(types) || typeof types === 'string') && types.includes(layoutpos);
}
function isEnableItemForMolding(layer, selItem) {
var molding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var allLineRects = _export.GeometryUtils.buildRectFromLines(layer, _export.GeometryUtils.getAllLines(layer));
var result = !(0, _helper.isEmpty)(selItem) && selItem.category === 'cabinet' && (selItem.layoutpos === _constants.BASE_CABINET_LAYOUTPOS && !_export.GeometryUtils.isSnappedLine(getItemRect(selItem), allLineRects) || (selItem.layoutpos === _constants.WALL_CABINET_LAYOUTPOS || selItem.layoutpos === _constants.TALL_CABINET_LAYOUTPOS) && _export.GeometryUtils.isSnappedLine(getItemRect(selItem), allLineRects));
// check this item is enable for any molding
if ((0, _helper.isEmpty)(molding)) return result;
// check this item is enable for specified molding
else return result && hasMoldingLayout(molding, selItem.layoutpos);
}
/**
* Check two line segments are overlap. The direction of the two line segments must be opposite.
* s1 c2 s2 c1
* |--------|------|-----------|
* Decision Formular: L(s1,c1) + L(s2, c2) = L(s1, s2) + L(c1, c2)
* @param {*} selRectPos1
* @param {*} selRectPos2
* @param {*} curRectPos1
* @param {*} curRectPos2
* @returns L(s1,c1) + L(s2, c2) - L(s1, s2) - L(c1, c2)
*/
function getDelta(selRectPos1, selRectPos2, curRectPos1, curRectPos2) {
return (
// GeometryUtils.verticesDistance(curRectPos1, selRectPos2) +
// GeometryUtils.verticesDistance(selRectPos1, curRectPos2) -
// Math.abs(
// GeometryUtils.verticesDistance(curRectPos2, curRectPos1) -
// GeometryUtils.verticesDistance(selRectPos1, selRectPos2)
// )
Math.abs(_export.GeometryUtils.verticesDistance(curRectPos1, selRectPos1) + _export.GeometryUtils.verticesDistance(selRectPos2, curRectPos2) - _export.GeometryUtils.verticesDistance(curRectPos2, curRectPos1) - _export.GeometryUtils.verticesDistance(selRectPos1, selRectPos2))
);
}
function isItemSnappedItem(selItem, curItem) {
var selRect = getItemRect(selItem).rect;
var curRect = getItemRect(curItem).rect;
var flag30 = getDelta(selRect[3], selRect[0], curRect[1], curRect[2]) < _constants.EPSILON;
var flag21 = getDelta(selRect[2], selRect[1], curRect[0], curRect[3]) < _constants.EPSILON;
var flag23 = getDelta(selRect[2], selRect[3], curRect[2], curRect[3]) < _constants.EPSILON;
var flag01 = getDelta(selRect[1], selRect[0], curRect[0], curRect[3]) < _constants.EPSILON;
var flag03 = getDelta(selRect[0], selRect[3], curRect[1], curRect[0]) < _constants.EPSILON;
return flag30 || flag21 || flag23 || flag01 || flag03;
}
function isItemSnappedGroup(selItem, itemGroup) {
return itemGroup.some(function (curItem) {
return isItemSnappedItem(selItem, curItem);
});
}
function sortItemsByDistance(items, selItem) {
items.sort(function (a, b) {
return _export.GeometryUtils.pointsDistance(a.x, a.y, selItem.x, selItem.y) - _export.GeometryUtils.pointsDistance(b.x, b.y, selItem.x, selItem.y);
});
return items;
}
function isSameMoldingLayoutpos(curItem, item) {
return curItem.layoutpos === _constants.BASE_CABINET_LAYOUTPOS && curItem.layoutpos === item.layoutpos || [_constants.WALL_CABINET_LAYOUTPOS, _constants.TALL_CABINET_LAYOUTPOS].includes(curItem.layoutpos) && [_constants.WALL_CABINET_LAYOUTPOS, _constants.TALL_CABINET_LAYOUTPOS].includes(item.layoutpos);
}
function isItemSameItemByLocation(item1, item2, location) {
var item1Altitude = item1.properties.get('altitude').get('_length');
var item1AltitudeUnit = item1.properties.get('altitude').get('_unit');
item1Altitude = (0, _convertUnitsLite.convert)(item1Altitude).from(item1AltitudeUnit).to('cm');
var item1Height = item1.properties.get('height').get('_length');
var item1HeightUnit = item1.properties.get('height').get('_unit');
item1Height = (0, _convertUnitsLite.convert)(item1Height).from(item1HeightUnit).to('cm');
var item2Altitude = item2.properties.get('altitude').get('_length');
var item2AltitudeUnit = item2.properties.get('altitude').get('_unit');
item2Altitude = (0, _convertUnitsLite.convert)(item2Altitude).from(item2AltitudeUnit).to('cm');
var item2Height = item2.properties.get('height').get('_length');
var item2HeightUnit = item2.properties.get('height').get('_unit');
item2Height = (0, _convertUnitsLite.convert)(item2Height).from(item2HeightUnit).to('cm');
var flag = false;
switch (location) {
case _constants.TOP_MOLDING_LOCATION:
if (_export.GeometryUtils.sameDistances(item1Altitude + item1Height, item2Altitude + item2Height)) flag = true;
break;
case _constants.MIDDLE_MOLDING_LOCATION:
if (_export.GeometryUtils.sameDistances(item1Altitude + item1Height / 2, item2Altitude + item2Height / 2)) flag = true;
break;
case _constants.BOTTOM_MOLDING_LOCATION:
if (_export.GeometryUtils.sameDistances(item1Altitude, item2Altitude)) flag = true;
break;
}
return flag;
}
function tryMergeMDItem(layer, selItem, curItem, itemGroup, molding) {
return isSameMoldingLayoutpos(selItem, curItem) && isEnableItemForMolding(layer, curItem, molding) && isItemSnappedGroup(curItem, itemGroup) && !itemGroup.some(function (item) {
return item.id === curItem.id;
}) && isItemSameItemByLocation(selItem, curItem, molding.location_type);
}
function getItemGroupFromMolding(layer, curItem, molding) {
var selectedItem = layer.getIn(['items', layer.selected.toJS().items[0]]);
var itemGroup = [curItem];
var temp_layer_items = layer.items.toArray().filter(function (item) {
return item.category === 'cabinet' && item.id !== curItem.id && item.id !== (selectedItem === null || selectedItem === void 0 ? void 0 : selectedItem.id);
});
temp_layer_items = sortItemsByDistance(temp_layer_items, curItem);
for (var idx = 0; idx < temp_layer_items.length; idx++) {
if (tryMergeMDItem(layer, curItem, temp_layer_items[idx], itemGroup, molding)) {
itemGroup.push(temp_layer_items[idx]);
idx = -1;
}
}
return itemGroup;
}
function tryMergeItemWithLocation(curItem, itemGroup, location) {
return itemGroup.some(function (item) {
return isItemSnappedItem(curItem, item) && isItemSameItemByLocation(curItem, item, location);
});
}
/**
* Make the molding group array with [items].
* @param {*} layer
* @param {*} items - Mergable snapped item group, if [items] is null then get all MG array of layer
* @returns MG array
*/
function getAllMoldingGroups(layer) {
var items = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
if (items === null) items = layer.items.toArray();
var MGArray = [];
items.forEach(function (curItem) {
// let allLineRects = GeometryUtils.buildRectFromLines(
// layer,
// GeometryUtils.getAllLines(layer)
// );
// if (
// (curItem.category === 'cabinet' &&
// curItem.layoutpos === BASE_CABINET_LAYOUTPOS &&
// !GeometryUtils.isSnappedLine(getItemRect(curItem), allLineRects)) ||
// ((curItem.layoutpos === WALL_CABINET_LAYOUTPOS ||
// curItem.layoutpos === TALL_CABINET_LAYOUTPOS) &&
// GeometryUtils.isSnappedLine(getItemRect(curItem), allLineRects))
// ) {
if (curItem.category === 'cabinet') {
var temp_items = items.filter(function (it) {
return it.category === 'cabinet' && isSameMoldingLayoutpos(curItem, it) && it.id !== curItem.id;
});
temp_items = sortItemsByDistance(temp_items, curItem);
_constants.MOLDING_LOCATIONS.forEach(function (location) {
if (!MGArray.some(function (mg) {
return mg.items.some(function (it) {
return it.id === curItem.id;
}) && mg.location_type === location;
})) {
var itemGroup = [curItem];
var isChangedItemGroup = false;
do {
isChangedItemGroup = false;
var _loop = function _loop(idx) {
if (!itemGroup.some(function (it) {
return it.id === temp_items[idx].id;
}) && tryMergeItemWithLocation(temp_items[idx], itemGroup, location)) {
itemGroup.push(temp_items[idx]);
isChangedItemGroup = true;
}
};
for (var idx = 0; idx < temp_items.length; idx++) {
_loop(idx);
}
} while (isChangedItemGroup);
MGArray.push({
id: _idBroker["default"].acquireID(),
items: itemGroup,
location_type: location,
molding: null,
lines: null,
pointGroups: null,
meshes: [],
rot: 0,
pos: 0
});
}
});
}
// }
});
return MGArray;
}
function getLinesOfItem(item, allLineRects, catalog) {
var lines = [];
var outline = null;
var element = catalog.elements[item.get('type')];
if (!element) element = catalog.elements[(0, _utils.returnReplaceableDeepSearchType)(item.get('type'))];
// get edge lines
var newWidth = item.properties.get('width').get('_length');
var wUnit = item.properties.get('width').get('_unit') || 'cm';
newWidth = (0, _convertUnitsLite.convert)(newWidth).from(wUnit).to('cm');
var newDepth = item.properties.get('depth').get('_length');
var hUnit = item.properties.get('depth').get('_unit') || 'cm';
newDepth = (0, _convertUnitsLite.convert)(newDepth).from(hUnit).to('cm');
if (item) {
// Get Outline Data of Selected Item
outline = element.info.outline;
if (outline) {
// Extract Points from `outline`
var outlinePaths = outline.paths;
var outlineWidth = outline.svgWidth;
var outlineHeight = outline.svgHeight;
var outlinePoints = []; // Hold Points Of SVG
var _iterator = _createForOfIteratorHelper(outlinePaths),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var path = _step.value;
var _iterator2 = _createForOfIteratorHelper(path.subPaths),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var subPath = _step2.value;
outlinePoints = outlinePoints.concat(subPath.getPoints());
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
outline.reverse && outlinePoints.reverse();
for (var i = 0; i < outlinePoints.length - 1; i++) {
lines.push([_export.GeometryUtils.rotatePointAroundPoint((outlinePoints[i].x / outlineWidth - 0.5) * newWidth + item.x, (outlinePoints[i].y / outlineHeight - 0.5) * newDepth + item.y, item.x, item.y, item.rotation + 90), _export.GeometryUtils.rotatePointAroundPoint((outlinePoints[i + 1].x / outlineWidth - 0.5) * newWidth + item.x, (outlinePoints[i + 1].y / outlineHeight - 0.5) * newDepth + item.y, item.x, item.y, item.rotation + 90), _idBroker["default"].acquireID()]);
}
} else {
var pos = [[-1, -1], [1, -1], [1, 1], [-1, 1]];
for (var _i = 0; _i < 4; _i++) {
lines.push([_export.GeometryUtils.rotatePointAroundPoint(pos[_i][0] * newWidth / 2 + item.x, pos[_i][1] * newDepth / 2 + item.y, item.x, item.y, item.rotation), _export.GeometryUtils.rotatePointAroundPoint(pos[(_i + 1) % 4][0] * newWidth / 2 + item.x, pos[(_i + 1) % 4][1] * newDepth / 2 + item.y, item.x, item.y, item.rotation), _idBroker["default"].acquireID()]);
}
}
}
if (item.layoutpos !== _constants.BASE_CABINET_LAYOUTPOS) {
lines = lines.filter(function (line) {
return !_export.GeometryUtils.isSnappedLine({
rect: [{
x: 0,
y: 0
}, {
x: 0,
y: 0
}, line[0], line[1]]
}, allLineRects);
});
}
return lines;
}
function isParallelLines(line1, line2) {
var isParallel = false;
if (Math.abs(line1[0].y - line1[1].y) <= _constants.EPSILON && Math.abs(line2[0].y - line2[1].y) <= _constants.EPSILON) isParallel = true;
if (Math.abs(line1[0].x - line1[1].x) <= _constants.EPSILON && Math.abs(line2[0].x - line2[1].x) <= _constants.EPSILON) isParallel = true;
if (Math.abs((line1[0].x - line1[1].x) / (line1[0].y - line1[1].y) - (line2[0].x - line2[1].x) / (line2[0].y - line2[1].y)) <= _constants.EPSILON) isParallel = true;
if (isParallel) {
return true;
}
return false;
}
function isLinesOverlapped(line1, line2) {
return isParallelLines(line1, line2) && line1.some(function (l1) {
return line2.some(function (l2) {
return _export.GeometryUtils.samePoints(l1, l2);
});
});
}
function mergeOverlappedLines(line1, line2) {
var line1_idx = 0,
line2_idx = 0;
line1.forEach(function (l1, idx1) {
if (idx1 !== line1.length - 1) {
line2.forEach(function (l2, idx2) {
if (idx2 !== line2.length - 1) {
if (_export.GeometryUtils.samePoints(l1, l2)) {
line1_idx = idx1;
line2_idx = idx2;
}
}
});
}
});
//If line1 's end point and line2 's start point is same, two lines merge into a line with
//the line1's start point as the start point and line2's end point as the end point.
if (line1_idx === 1) {
return [line1[1 - line1_idx], line2[1 - line2_idx], _idBroker["default"].acquireID()];
} else {
return [line2[1 - line2_idx], line1[1 - line1_idx], _idBroker["default"].acquireID()];
}
}
/**
* Get the contour line array of moldingGroup items
* @param {*} moldingGroup
* @param {*} layer
* @param {*} catalog
* @returns contour line array of moldingGroup items
*/
function getLinesFromItems2(moldingGroup, layer, catalog) {
// wall lines
var allLineRects = _export.GeometryUtils.buildRectFromLines(layer, _export.GeometryUtils.getAllLines(layer));
var MGlines = [];
var MGlinesOtherSnapped = [];
var items = (0, _toConsumableArray2["default"])(moldingGroup.items);
// The other item's lines (these items are snapped to the moldingGroup items)
var itemsOtherSnapped = layer.items.toArray().filter(function (item) {
if (items.some(function (it) {
return item.id === it.id;
})) return false;
if (isItemSnappedGroup(item, items)) {
var itemLines = getLinesOfItem(item, allLineRects, catalog);
var itemAltitude = item.properties.get('altitude').get('_length');
var itemAltitudeUnit = item.properties.get('altitude').get('_unit');
itemAltitude = (0, _convertUnitsLite.convert)(itemAltitude).from(itemAltitudeUnit).to('cm');
var itemHeight = item.properties.get('height').get('_length');
var itemHeightUnit = item.properties.get('height').get('_unit');
itemHeight = (0, _convertUnitsLite.convert)(itemHeight).from(itemHeightUnit).to('cm');
itemLines.forEach(function (line) {
line.push({
altitude: itemAltitude,
height: itemHeight
});
});
MGlinesOtherSnapped = [].concat((0, _toConsumableArray2["default"])(MGlinesOtherSnapped), (0, _toConsumableArray2["default"])(itemLines));
return true;
} else return false;
});
// get all lines of moldingGroup items
items.forEach(function (item) {
var itemAltitude = item.properties.get('altitude').get('_length');
var itemAltitudeUnit = item.properties.get('altitude').get('_unit');
itemAltitude = (0, _convertUnitsLite.convert)(itemAltitude).from(itemAltitudeUnit).to('cm');
var itemHeight = item.properties.get('height').get('_length');
var itemHeightUnit = item.properties.get('height').get('_unit');
itemHeight = (0, _convertUnitsLite.convert)(itemHeight).from(itemHeightUnit).to('cm');
var itemLines = getLinesOfItem(item, allLineRects, catalog);
// remove the edge that overlapped with other snapped items
itemLines = itemLines.filter(function (itLine) {
var ret = true;
var srcLine = {
x1: itLine[0].x,
y1: itLine[0].y,
x2: itLine[1].x,
y2: itLine[1].y
};
for (var j = 0; j < MGlinesOtherSnapped.length; j++) {
var bCheck = false;
switch (moldingGroup.location_type) {
case _constants.TOP_MOLDING_LOCATION:
bCheck = MGlinesOtherSnapped[j][3].altitude + MGlinesOtherSnapped[j][3].height > itemAltitude + itemHeight;
break;
case _constants.MIDDLE_MOLDING_LOCATION:
bCheck = true;
break;
case _constants.BOTTOM_MOLDING_LOCATION:
bCheck = MGlinesOtherSnapped[j][3].altitude < itemAltitude;
break;
}
if (item.category !== 'cabinet' || ![_constants.BASE_CABINET_LAYOUTPOS, _constants.WALL_CABINET_LAYOUTPOS, _constants.TALL_CABINET_LAYOUTPOS].includes(item.layoutpos)) bCheck = true;
if (bCheck) {
var destLine = {
x1: MGlinesOtherSnapped[j][0].x,
y1: MGlinesOtherSnapped[j][0].y,
x2: MGlinesOtherSnapped[j][1].x,
y2: MGlinesOtherSnapped[j][1].y
};
var rst = _export.GeometryUtils.relationshipOfTwoOverlappedLines(srcLine, destLine);
if (rst.result === _constants.OVERLAP_SAME || rst.result === _constants.OVERLAP_INCLUDED) {
ret = false;
break;
}
}
}
return ret;
});
MGlines = [].concat((0, _toConsumableArray2["default"])(MGlines), (0, _toConsumableArray2["default"])(itemLines));
});
// Filtering overlapped edges and get contour edges
var removeLineIds = []; // remove the edge that fully overlapped to other edge
var newLines = []; // new countour edge segment that except the overlapped part
var _loop2 = function _loop2(i) {
var srcLine = {
x1: MGlines[i][0].x,
y1: MGlines[i][0].y,
x2: MGlines[i][1].x,
y2: MGlines[i][1].y
};
var _loop4 = function _loop4(j) {
if (i === j) return 0; // continue
var destLine = {
x1: MGlines[j][0].x,
y1: MGlines[j][0].y,
x2: MGlines[j][1].x,
y2: MGlines[j][1].y
};
var rst = _export.GeometryUtils.relationshipOfTwoOverlappedLines(srcLine, destLine);
if (rst.result === _constants.OVERLAP_SAME || rst.result === _constants.OVERLAP_INCLUDED) {
removeLineIds.push(i);
return 1; // break
} else if (rst.result === _constants.OVERLAP_SOME) {
removeLineIds.push(i);
var lineSegs = getTrimmedContourLineSegs(rst.trimmedSegs, MGlines.filter(function (v, idx) {
return idx !== i && idx !== j;
}), 0);
if (lineSegs.length > 0) newLines = [].concat((0, _toConsumableArray2["default"])(newLines), (0, _toConsumableArray2["default"])(lineSegs));
return 1; // break
}
},
_ret2;
for (var j = 0; j < MGlines.length; j++) {
_ret2 = _loop4(j);
if (_ret2 === 0) continue;
if (_ret2 === 1) break;
}
};
for (var i = 0; i < MGlines.length; i++) {
_loop2(i);
}
MGlines = MGlines.filter(function (line, idx) {
return !removeLineIds.some(function (id) {
return idx === id;
});
});
if (newLines.length > 0) MGlines = [].concat((0, _toConsumableArray2["default"])(MGlines), (0, _toConsumableArray2["default"])(newLines));
// merge the collinear linked lines to one line
var newMGlines = [];
var filteredMGlines = MGlines;
var _loop3 = function _loop3(_i2) {
if (filteredMGlines.length < 1) return 0; // break
if (!filteredMGlines.some(function (v) {
return v[2] === MGlines[_i2][2];
})) return 1; // continue
var mergedResult = getMergedLine(MGlines[_i2], filteredMGlines, 0);
if (mergedResult) {
newMGlines.push(mergedResult.mergedLine);
filteredMGlines = mergedResult.filteredMGlines;
}
},
_ret;
for (var _i2 = 0; _i2 < MGlines.length; _i2++) {
_ret = _loop3(_i2);
if (_ret === 0) break;
if (_ret === 1) continue;
}
return newMGlines;
}
/**
* Get the line that colinear linked with [line], and merge them to a new line
* @param {*} line
* @param {*} MGlines
* @returns
* - returns merged new line
* - returns filtered new line group (remove linked line from [MGlines])
*/
function getMergedLine(line, MGlines, cnt) {
try {
cnt++;
if (cnt > 5000) {
console.log('getMergedLine has limited!');
return null;
}
var newMGlines = MGlines.filter(function (v) {
return line[2] !== v[2];
});
var mergeResult = {
mergedLine: line,
filteredMGlines: newMGlines
};
var srcLine = {
x1: line[0].x,
y1: line[0].y,
x2: line[1].x,
y2: line[1].y
};
var _loop5 = function _loop5(i) {
var destLine = {
x1: newMGlines[i][0].x,
y1: newMGlines[i][0].y,
x2: newMGlines[i][1].x,
y2: newMGlines[i][1].y
};
var rst = _export.GeometryUtils.relationshipOfTwoOverlappedLines(srcLine, destLine);
if (rst.result === _constants.OVERLAP_LINK) {
var mergedLine = [{
x: rst.linkedLine.x1,
y: rst.linkedLine.y1
}, {
x: rst.linkedLine.x2,
y: rst.linkedLine.y2
}, _idBroker["default"].acquireID()];
mergeResult = getMergedLine(mergedLine, newMGlines.filter(function (v, idx) {
return idx !== i;
}), cnt);
}
};
for (var i = 0; i < newMGlines.length; i++) {
_loop5(i);
}
return mergeResult;
} catch (e) {
console.log('getMergedLine catched :', e);
return null;
}
}
/**
* Get the contour line segments from [lineSegs]
* @param {*} lineSegs
* @param {*} otherLines
* @returns
*/
function getTrimmedContourLineSegs(lineSegs, otherLines, cnt) {
try {
cnt++;
if (cnt > 5000) {
console.log('getTrimmedContourLineSegs has limited!');
return [];
}
var returnSegs = [];
for (var i = 0; i < lineSegs.length; i++) {
var srcLine = {
x1: lineSegs[i].x1,
y1: lineSegs[i].y1,
x2: lineSegs[i].x2,
y2: lineSegs[i].y2
};
var bContourSeg = true;
var _loop6 = function _loop6(j) {
var destLine = {
x1: otherLines[j][0].x,
y1: otherLines[j][0].y,
x2: otherLines[j][1].x,
y2: otherLines[j][1].y
};
var rst = _export.GeometryUtils.relationshipOfTwoOverlappedLines(srcLine, destLine);
if (rst.result == _constants.OVERLAP_SAME || rst.result == _constants.OVERLAP_INCLUDED) {
bContourSeg = false;
return 0; // break
} else if (rst.result == _constants.OVERLAP_SOME) {
var tLineSegs = getTrimmedContourLineSegs(rst.trimmedSegs, otherLines.filter(function (v, idx) {
return idx !== j;
}), cnt);
if (tLineSegs.length > 0) returnSegs = [].concat((0, _toConsumableArray2["default"])(returnSegs), (0, _toConsumableArray2["default"])(tLineSegs));
bContourSeg = false;
return 0; // break
}
},
_ret3;
for (var j = 0; j < otherLines.length; j++) {
_ret3 = _loop6(j);
if (_ret3 === 0) break;
}
if (bContourSeg) returnSegs.push([{
x: lineSegs[i].x1,
y: lineSegs[i].y1
}, {
x: lineSegs[i].x2,
y: lineSegs[i].y2
}, _idBroker["default"].acquireID()]);
}
return returnSegs;
} catch (e) {
console.log('getTrimmedContourLineSegs catched :', e);
return [];
}
}
function getLinesFromItems(moldingGroup, layer, catalog) {
var allLineRects = _export.GeometryUtils.buildRectFromLines(layer, _export.GeometryUtils.getAllLines(layer));
var items = (0, _toConsumableArray2["default"])(moldingGroup.items);
var MGlines = getLinesOfItem(items[0], allLineRects, catalog);
items = sortItemsByDistance(items, items[0]);
var _loop7 = function _loop7() {
var itemLines = getLinesOfItem(items[i], allLineRects, catalog);
var temp_MGLines = [];
MGlines.forEach(function (line) {
var idx = itemLines.findIndex(function (itemLine) {
return isLinesOverlapped(line, itemLine);
});
var curItemLine = itemLines[idx];
if (idx > -1) {
if (!(_export.GeometryUtils.samePoints(line[0], curItemLine[0]) && _export.GeometryUtils.samePoints(line[1], curItemLine[1]) || _export.GeometryUtils.samePoints(line[0], curItemLine[1]) && _export.GeometryUtils.samePoints(line[1], curItemLine[0]))) {
var MGLine = mergeOverlappedLines(line, curItemLine);
temp_MGLines.push(MGLine);
}
itemLines.splice(idx, 1);
} else {
temp_MGLines.push(line);
}
});
itemLines.forEach(function (itemLine) {
return temp_MGLines.push(itemLine);
});
MGlines = [].concat(temp_MGLines);
};
for (var i = 1; i < items.length; i++) {
_loop7();
}
// return MGlines;
var snapped_other_items = layer.items.toArray().filter(function (item) {
if (items.some(function (it) {
return item.id === it.id;
})) return false;
return isItemSnappedGroup(item, items);
});
snapped_other_items.forEach(function (item) {
var itemAltitude = item.properties.get('altitude').get('_length');
var itemAltitudeUnit = item.properties.get('altitude').get('_unit');
itemAltitude = (0, _convertUnitsLite.convert)(itemAltitude).from(itemAltitudeUnit).to('cm');
var itemHeight = item.properties.get('height').get('_length');
var itemHeightUnit = item.properties.get('height').get('_unit');
itemHeight = (0, _convertUnitsLite.convert)(itemHeight).from(itemHeightUnit).to('cm');
var mgroupAltitude = items[0].properties.get('altitude').get('_length');
var mgroupAltitudeUnit = items[0].properties.get('altitude').get('_unit');
mgroupAltitude = (0, _convertUnitsLite.convert)(mgroupAltitude).from(mgroupAltitudeUnit).to('cm');
var mgroupHeight = items[0].properties.get('height').get('_length');
var mgroupHeightUnit = items[0].properties.get('height').get('_unit');
mgroupHeight = (0, _convertUnitsLite.convert)(mgroupHeight).from(mgroupHeightUnit).to('cm');
var flag = false;
switch (moldingGroup.location_type) {
case _constants.TOP_MOLDING_LOCATION:
flag = itemAltitude + itemHeight > mgroupAltitude + mgroupHeight;
break;
case _constants.MIDDLE_MOLDING_LOCATION:
flag = true;
break;
case _constants.BOTTOM_MOLDING_LOCATION:
flag = itemAltitude < mgroupAltitude;
break;
}
if (item.category !== 'cabinet' || ![_constants.BASE_CABINET_LAYOUTPOS, _constants.WALL_CABINET_LAYOUTPOS, _constants.TALL_CABINET_LAYOUTPOS].includes(item.layoutpos)) flag = true;
if (flag) {
var itemLines = getLinesOfItem(item, allLineRects, catalog);
var temp_MGLines = [];
MGlines.forEach(function (mgl) {
var idx = itemLines.findIndex(function (itl) {
return isLinesOverlapped(mgl, itl);
});
var curITL = itemLines[idx];
if (idx > -1) {
if (getDelta(mgl[0], mgl[1], curITL[1], curITL[0]) < _constants.EPSILON || getDelta(mgl[0], mgl[1], curITL[0], curITL[1]) < _constants.EPSILON) {
if (_export.GeometryUtils.verticesDistance(mgl[0], mgl[1]) > _export.GeometryUtils.verticesDistance(curITL[0], curITL[1])) {
var MGLine = mergeOverlappedLines(mgl, curITL);
temp_MGLines.push(MGLine);
}
itemLines.splice(idx, 1);
} else {
temp_MGLines.push(mgl);
}
} else {
temp_MGLines.push(mgl);
}
});
MGlines = [].concat(temp_MGLines);
}
});
return MGlines;
}
function getMDPoints(newMD) {
if (newMD.lines.length < 1) return _objectSpread(_objectSpread({}, newMD), {}, {
pointGroups: []
});
// let maxX = newMD.lines[0][0].x,
var maxX = newMD.lines[0][0].x,
minX = newMD.lines[0][0].x;
var maxY = newMD.lines[0][0].y,
minY = newMD.lines[0][0].y;
newMD.lines.forEach(function (line) {
if (line[0].x > maxX) {
maxX = line[0].x;
}
if (line[0].x < minX) {
minX = line[0].x;
}
if (line[1].x > maxX) {
maxX = line[1].x;
}
if (line[1].x < minX) {
minX = line[1].x;
}
if (line[0].y > maxY) {
maxY = line[0].y;
}
if (line[0].y < minY) {
minY = line[0].y;
}
if (line[1].y > maxY) {
maxY = line[1].y;
}
if (line[1].y < minY) {
minY = line[1].y;
}
});
var cPos = {
x: (maxX + minX) / 2,
y: (maxY + minY) / 2
};
var newSize = _objectSpread(_objectSpread({}, newMD.size), {}, {
width: maxX - minX,
depth: maxY - minY
});
// get vertex points
var MDlines = (0, _toConsumableArray2["default"])(newMD.lines);
var pointGroups = [[]];
var flag = 1;
var i = 0;
var _loop8 = function _loop8() {
if (pointGroups[i].length === 0) {
pointGroups[i].push(new Three.Vector2(MDlines[0][0].x - cPos.x, MDlines[0][0].y - cPos.y), new Three.Vector2(MDlines[0][1].x - cPos.x, MDlines[0][1].y - cPos.y));
MDlines.splice(0, 1);
} else {
if (flag) {
var lastPoint = pointGroups[i][pointGroups[i].length - 1];
var res = MDlines.findIndex(function (a) {
return _export.GeometryUtils.samePoints({
x: a[0].x - cPos.x,
y: a[0].y - cPos.y
}, lastPoint) || _export.GeometryUtils.samePoints({
x: a[1].x - cPos.x,
y: a[1].y - cPos.y
}, lastPoint);
});
if (res > -1) {
var newPos = {
x: MDlines[res][0].x - cPos.x,
y: MDlines[res][0].y - cPos.y
};
if (_export.GeometryUtils.samePoints(newPos, lastPoint)) {
newPos = {
x: MDlines[res][1].x - cPos.x,
y: MDlines[res][1].y - cPos.y
};
}
pointGroups[i].push(new Three.Vector2(newPos.x, newPos.y));
MDlines.splice(res, 1);
} else {
flag = 0;
}
} else {
var firstPoint = pointGroups[i][0];
var _res = MDlines.findIndex(function (a) {
return _export.GeometryUtils.samePoints({
x: a[0].x - cPos.x,
y: a[0].y - cPos.y
}, firstPoint) || _export.GeometryUtils.samePoints({
x: a[1].x - cPos.x,
y: a[1].y - cPos.y
}, firstPoint);
});
if (_res > -1) {
var _newPos = {
x: MDlines[_res][0].x - cPos.x,
y: MDlines[_res][0].y - cPos.y
};
if (_export.GeometryUtils.samePoints(_newPos, firstPoint)) {
_newPos = {
x: MDlines[_res][1].x - cPos.x,
y: MDlines[_res][1].y - cPos.y
};
}
pointGroups[i] = [new Three.Vector2(_newPos.x, _newPos.y)].concat((0, _toConsumableArray2["default"])(pointGroups[i]));
MDlines.splice(_res, 1);
} else {
flag = 1;
i++;
if (MDlines.length !== 0) pointGroups.push([]);
}
}
}
};
while (MDlines.length !== 0) {
_loop8();
}
var z = newMD.items[0].properties.get('altitude').get('_length');
var zUnit = newMD.items[0].properties.get('altitude').get('_unit') || 'cm';
z = (0, _convertUnitsLite.convert)(z).from(zUnit).to('cm');
var height = newMD.items[0].properties.get('height').get('_length');
var heightUnit = newMD.items[0].properties.get('height').get('_unit') || 'cm';
height = (0, _convertUnitsLite.convert)(height).from(heightUnit).to('cm');
switch (newMD.location_type) {
case _constants.TOP_MOLDING_LOCATION:
z += height;
break;
case _constants.MIDDLE_MOLDING_LOCATION:
z += height / 2;
break;
case _constants.BOTTOM_MOLDING_LOCATION:
z += 0;
break;
default:
break;
}
return _objectSpread(_objectSpread({}, newMD), {}, {
pointGroups: pointGroups,
pos: _objectSpread(_objectSpread({}, cPos), {}, {
z: z
}),
size: newSize
});
}
function createMonldingGroup(oldMG, layer, molding, catalog) {
var newMG = _objectSpread({}, oldMG);
newMG.molding = molding;
newMG.lines = getLinesFromItems2(oldMG, layer, catalog);
newMG.lines.reverse();
newMG = getMDPoints(newMG);
return newMG;
}