2016-04-22 13:44:56 +00:00
|
|
|
var getAttachment = require('lib/features/modeling/behavior/util/LineAttachmentUtil').getAttachment;
|
|
|
|
|
2016-08-10 10:01:24 +00:00
|
|
|
var EPSILON = 0.1;
|
2016-04-22 13:44:56 +00:00
|
|
|
|
2017-03-21 08:01:21 +00:00
|
|
|
|
2016-04-22 13:44:56 +00:00
|
|
|
describe('modeling/behavior/util - LineAttachmentUtil#getAttachment', function() {
|
|
|
|
|
|
|
|
// test line
|
|
|
|
//
|
|
|
|
// *--*
|
|
|
|
// |
|
|
|
|
// *
|
|
|
|
// \
|
|
|
|
// *
|
|
|
|
//
|
|
|
|
var line = [
|
|
|
|
{ x: 10, y: 10 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2016-04-22 13:44:56 +00:00
|
|
|
// -
|
|
|
|
{ x: 30, y: 10 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2016-04-22 13:44:56 +00:00
|
|
|
// |
|
|
|
|
{ x: 30, y: 30 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2016-04-22 13:44:56 +00:00
|
|
|
// \
|
|
|
|
{ x: 130, y: 130 }
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
describe('should recognize segment', function() {
|
|
|
|
|
|
|
|
it('horizontal', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 20, y: 5 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'segment',
|
|
|
|
position: { x: 20, y: 10 },
|
|
|
|
segmentIndex: 0,
|
|
|
|
relativeLocation: 0.5
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('horizontal (on line)', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 20, y: 10 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'segment',
|
|
|
|
position: { x: 20, y: 10 },
|
|
|
|
segmentIndex: 0,
|
|
|
|
relativeLocation: 0.5
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('vertical', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 40, y: 20 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'segment',
|
|
|
|
position: { x: 30, y: 20 },
|
|
|
|
segmentIndex: 1,
|
|
|
|
relativeLocation: 0.5
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('diagonal', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 30, y: 40 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'segment',
|
|
|
|
position: { x: 35, y: 35 },
|
|
|
|
segmentIndex: 2,
|
|
|
|
relativeLocation: 0.05
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('diagonal (on line)', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 50, y: 50 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'segment',
|
|
|
|
position: { x: 50, y: 50 },
|
|
|
|
segmentIndex: 2,
|
|
|
|
relativeLocation: 0.2
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('horizontal (conflicting with vertical)', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 25, y: 15 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'segment',
|
|
|
|
position: { x: 25, y: 10 },
|
|
|
|
segmentIndex: 0,
|
|
|
|
relativeLocation: 0.75
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('should recognize bendpoint', function() {
|
|
|
|
|
|
|
|
it('horizontal', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 35, y: 5 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'bendpoint',
|
|
|
|
position: { x: 30, y: 10 },
|
|
|
|
bendpointIndex: 1,
|
|
|
|
segmentIndex: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('horizontal (segment start)', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 5, y: 5 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'bendpoint',
|
|
|
|
position: { x: 10, y: 10 },
|
|
|
|
bendpointIndex: 0,
|
|
|
|
segmentIndex: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('vertical', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 35, y: 10 }, line);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'bendpoint',
|
|
|
|
position: { x: 30, y: 10 },
|
|
|
|
bendpointIndex: 1,
|
|
|
|
segmentIndex: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('vertical (segment start)', function() {
|
|
|
|
|
|
|
|
var otherLine = [
|
|
|
|
{ x: 10, y: 10 },
|
|
|
|
{ x: 10, y: 50 }
|
|
|
|
];
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 5, y: 5 }, otherLine);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'bendpoint',
|
|
|
|
position: { x: 10, y: 10 },
|
|
|
|
bendpointIndex: 0,
|
|
|
|
segmentIndex: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-08-10 10:01:24 +00:00
|
|
|
|
|
|
|
describe('should handle float values', function() {
|
|
|
|
|
|
|
|
// test line
|
|
|
|
//
|
|
|
|
// *--*
|
|
|
|
// |
|
|
|
|
// *
|
|
|
|
// \
|
|
|
|
// *
|
|
|
|
//
|
2017-03-21 08:01:21 +00:00
|
|
|
var floatingPointLine = [
|
2016-08-10 10:01:24 +00:00
|
|
|
{ x: 10.141592, y: 10.653589 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2016-08-10 10:01:24 +00:00
|
|
|
// -
|
|
|
|
{ x: 30.793238, y: 10.462643 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2016-08-10 10:01:24 +00:00
|
|
|
// |
|
|
|
|
{ x: 30.383279, y: 30.502884 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2016-08-10 10:01:24 +00:00
|
|
|
// \
|
|
|
|
{ x: 130.197169, y: 130.399375 }
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
it('float value segment', function() {
|
|
|
|
|
|
|
|
// when
|
2017-03-21 08:01:21 +00:00
|
|
|
var attachment = getAttachment({ x: 20.197169, y: 5.399375 }, floatingPointLine);
|
2016-08-10 10:01:24 +00:00
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment.type).to.equal('segment');
|
|
|
|
expect(attachment.segmentIndex).to.equal(0);
|
|
|
|
|
|
|
|
// expect values to be roughly equal
|
|
|
|
expect(attachment.relativeLocation).to.be.within(0.5 - EPSILON, 0.5 + EPSILON);
|
|
|
|
expect(attachment.position.x).to.be.within(20.25 - EPSILON, 20.25 + EPSILON);
|
|
|
|
expect(attachment.position.y).to.be.within(10.5 - EPSILON, 10.5 + EPSILON);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('float value bendboint', function() {
|
|
|
|
|
|
|
|
// when
|
2017-03-21 08:01:21 +00:00
|
|
|
var attachment = getAttachment({ x: 35.197169, y: 5.399375 }, floatingPointLine);
|
2016-08-10 10:01:24 +00:00
|
|
|
|
|
|
|
// then
|
2017-04-27 21:31:00 +00:00
|
|
|
expect(attachment.type).to.equal('bendpoint');
|
|
|
|
expect(attachment.segmentIndex).to.equal(1);
|
|
|
|
expect(attachment.bendpointIndex).to.equal(1);
|
2016-08-10 10:01:24 +00:00
|
|
|
|
2017-04-27 21:31:00 +00:00
|
|
|
// expect values to be roughly equal
|
|
|
|
expect(attachment.position.x).to.be.within(30.793 - EPSILON, 30.793 + EPSILON);
|
|
|
|
expect(attachment.position.y).to.be.within(10.463 - EPSILON, 10.463 + EPSILON);
|
2016-08-10 10:01:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-03-21 08:01:21 +00:00
|
|
|
|
|
|
|
describe('handle zero-length line', function() {
|
|
|
|
|
|
|
|
var zeroLengthLine = [
|
|
|
|
{ x: 10, y: 10 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2017-03-21 08:01:21 +00:00
|
|
|
// -
|
|
|
|
{ x: 10, y: 10 }
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
var zeroLengthFloatingPointLine = [
|
|
|
|
{ x: 10.1, y: 10.12313 },
|
2019-08-19 08:39:20 +00:00
|
|
|
|
2017-03-21 08:01:21 +00:00
|
|
|
// -
|
|
|
|
{ x: 10, y: 10.112 }
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
it('should treat zero-length as bendpoint attach', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 15.1, y: 15.123 }, zeroLengthLine);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'bendpoint',
|
|
|
|
position: { x: 10, y: 10 },
|
|
|
|
segmentIndex: 0,
|
|
|
|
bendpointIndex: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should treat approx zero-length as bendpoint attach', function() {
|
|
|
|
|
|
|
|
// when
|
|
|
|
var attachment = getAttachment({ x: 15.1, y: 15.123 }, zeroLengthFloatingPointLine);
|
|
|
|
|
|
|
|
// then
|
|
|
|
expect(attachment).to.eql({
|
|
|
|
type: 'bendpoint',
|
|
|
|
position: { x: 10.1, y: 10.12313 },
|
|
|
|
segmentIndex: 0,
|
|
|
|
bendpointIndex: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-04-22 13:44:56 +00:00
|
|
|
});
|