sumolib.net.netshiftadaptor

This class performs a network reprojection using barycentric coordinates of two triangles which share the same nodes of two networks.

This means: the class is initialized with two networks and two lists of node ids (should be exactly three). The according nodes should be the "same" nodes in both networks. When "reproject" is called, all nodes' positions of the second network are reprojected so that they match positions within the first network.

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2008-2026 German Aerospace Center (DLR) and others.
 3# This program and the accompanying materials are made available under the
 4# terms of the Eclipse Public License 2.0 which is available at
 5# https://www.eclipse.org/legal/epl-2.0/
 6# This Source Code may also be made available under the following Secondary
 7# Licenses when the conditions for such availability set forth in the Eclipse
 8# Public License 2.0 are satisfied: GNU General Public License, version 2
 9# or later which is available at
10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13# @file    netshiftadaptor.py
14# @author  Daniel Krajzewicz
15# @author  Michael Behrisch
16# @date    2008-09-01
17
18"""
19
20This class performs a network reprojection
21 using barycentric coordinates of two triangles
22 which share the same nodes of two networks.
23
24This means: the class is initialized with two
25 networks and two lists of node ids (should be
26 exactly three). The according nodes should be
27 the "same" nodes in both networks.
28When "reproject" is called, all nodes' positions
29 of the second network are reprojected so that
30 they match positions within the first network.
31"""
32
33
34class NetShiftAdaptor:
35
36    def __init__(self, net1, net2, nodes1, nodes2):
37        self._net1 = net1
38        self._net2 = net2
39        self._nodes1 = nodes1
40        self._nodes2 = nodes2
41        if len(nodes1) != 3 or len(nodes2) != 3:
42            raise "Both node lists must contain exactly 3 node ids"
43
44    def reproject(self, verbose=False):
45        x11 = self._net1._id2node[self._nodes1[0]]._coord[0]
46        y11 = self._net1._id2node[self._nodes1[0]]._coord[1]
47        x12 = self._net1._id2node[self._nodes1[1]]._coord[0]
48        y12 = self._net1._id2node[self._nodes1[1]]._coord[1]
49        x13 = self._net1._id2node[self._nodes1[2]]._coord[0]
50        y13 = self._net1._id2node[self._nodes1[2]]._coord[1]
51        x21 = self._net2._id2node[self._nodes2[0]]._coord[0]
52        y21 = self._net2._id2node[self._nodes2[0]]._coord[1]
53        x22 = self._net2._id2node[self._nodes2[1]]._coord[0]
54        y22 = self._net2._id2node[self._nodes2[1]]._coord[1]
55        x23 = self._net2._id2node[self._nodes2[2]]._coord[0]
56        y23 = self._net2._id2node[self._nodes2[2]]._coord[1]
57        b0 = (x22 - x21) * (y23 - y21) - (x23 - x21) * (y22 - y21)
58        for n in self._net2._nodes:
59            x0 = n._coord[0]
60            y0 = n._coord[1]
61            b1 = ((x22 - x0) * (y23 - y0) - (x23 - x0) * (y22 - y0)) / b0
62            b2 = ((x23 - x0) * (y21 - y0) - (x21 - x0) * (y23 - y0)) / b0
63            b3 = ((x21 - x0) * (y22 - y0) - (x22 - x0) * (y21 - y0)) / b0
64            n._coord = (
65                b1 * x11 + b2 * x12 + b3 * x13, b1 * y11 + b2 * y12 + b3 * y13)
66        for e in self._net2._edges:
67            for _lane in e.getLanes():
68                shape = []
69                for p in _lane.getShape3D():
70                    x0 = p[0]
71                    y0 = p[1]
72                    b1 = ((x22 - x0) * (y23 - y0) - (x23 - x0) * (y22 - y0)) / b0
73                    b2 = ((x23 - x0) * (y21 - y0) - (x21 - x0) * (y23 - y0)) / b0
74                    b3 = ((x21 - x0) * (y22 - y0) - (x22 - x0) * (y21 - y0)) / b0
75                    x = (b1 * x11 + b2 * x12 + b3 * x13)
76                    y = (b1 * y11 + b2 * y12 + b3 * y13)
77                    z = p[2]
78                    shape.append((x, y, z))
79                _lane.setShape(shape)
80            e.rebuildShape()
class NetShiftAdaptor:
35class NetShiftAdaptor:
36
37    def __init__(self, net1, net2, nodes1, nodes2):
38        self._net1 = net1
39        self._net2 = net2
40        self._nodes1 = nodes1
41        self._nodes2 = nodes2
42        if len(nodes1) != 3 or len(nodes2) != 3:
43            raise "Both node lists must contain exactly 3 node ids"
44
45    def reproject(self, verbose=False):
46        x11 = self._net1._id2node[self._nodes1[0]]._coord[0]
47        y11 = self._net1._id2node[self._nodes1[0]]._coord[1]
48        x12 = self._net1._id2node[self._nodes1[1]]._coord[0]
49        y12 = self._net1._id2node[self._nodes1[1]]._coord[1]
50        x13 = self._net1._id2node[self._nodes1[2]]._coord[0]
51        y13 = self._net1._id2node[self._nodes1[2]]._coord[1]
52        x21 = self._net2._id2node[self._nodes2[0]]._coord[0]
53        y21 = self._net2._id2node[self._nodes2[0]]._coord[1]
54        x22 = self._net2._id2node[self._nodes2[1]]._coord[0]
55        y22 = self._net2._id2node[self._nodes2[1]]._coord[1]
56        x23 = self._net2._id2node[self._nodes2[2]]._coord[0]
57        y23 = self._net2._id2node[self._nodes2[2]]._coord[1]
58        b0 = (x22 - x21) * (y23 - y21) - (x23 - x21) * (y22 - y21)
59        for n in self._net2._nodes:
60            x0 = n._coord[0]
61            y0 = n._coord[1]
62            b1 = ((x22 - x0) * (y23 - y0) - (x23 - x0) * (y22 - y0)) / b0
63            b2 = ((x23 - x0) * (y21 - y0) - (x21 - x0) * (y23 - y0)) / b0
64            b3 = ((x21 - x0) * (y22 - y0) - (x22 - x0) * (y21 - y0)) / b0
65            n._coord = (
66                b1 * x11 + b2 * x12 + b3 * x13, b1 * y11 + b2 * y12 + b3 * y13)
67        for e in self._net2._edges:
68            for _lane in e.getLanes():
69                shape = []
70                for p in _lane.getShape3D():
71                    x0 = p[0]
72                    y0 = p[1]
73                    b1 = ((x22 - x0) * (y23 - y0) - (x23 - x0) * (y22 - y0)) / b0
74                    b2 = ((x23 - x0) * (y21 - y0) - (x21 - x0) * (y23 - y0)) / b0
75                    b3 = ((x21 - x0) * (y22 - y0) - (x22 - x0) * (y21 - y0)) / b0
76                    x = (b1 * x11 + b2 * x12 + b3 * x13)
77                    y = (b1 * y11 + b2 * y12 + b3 * y13)
78                    z = p[2]
79                    shape.append((x, y, z))
80                _lane.setShape(shape)
81            e.rebuildShape()
NetShiftAdaptor(net1, net2, nodes1, nodes2)
37    def __init__(self, net1, net2, nodes1, nodes2):
38        self._net1 = net1
39        self._net2 = net2
40        self._nodes1 = nodes1
41        self._nodes2 = nodes2
42        if len(nodes1) != 3 or len(nodes2) != 3:
43            raise "Both node lists must contain exactly 3 node ids"
def reproject(self, verbose=False):
45    def reproject(self, verbose=False):
46        x11 = self._net1._id2node[self._nodes1[0]]._coord[0]
47        y11 = self._net1._id2node[self._nodes1[0]]._coord[1]
48        x12 = self._net1._id2node[self._nodes1[1]]._coord[0]
49        y12 = self._net1._id2node[self._nodes1[1]]._coord[1]
50        x13 = self._net1._id2node[self._nodes1[2]]._coord[0]
51        y13 = self._net1._id2node[self._nodes1[2]]._coord[1]
52        x21 = self._net2._id2node[self._nodes2[0]]._coord[0]
53        y21 = self._net2._id2node[self._nodes2[0]]._coord[1]
54        x22 = self._net2._id2node[self._nodes2[1]]._coord[0]
55        y22 = self._net2._id2node[self._nodes2[1]]._coord[1]
56        x23 = self._net2._id2node[self._nodes2[2]]._coord[0]
57        y23 = self._net2._id2node[self._nodes2[2]]._coord[1]
58        b0 = (x22 - x21) * (y23 - y21) - (x23 - x21) * (y22 - y21)
59        for n in self._net2._nodes:
60            x0 = n._coord[0]
61            y0 = n._coord[1]
62            b1 = ((x22 - x0) * (y23 - y0) - (x23 - x0) * (y22 - y0)) / b0
63            b2 = ((x23 - x0) * (y21 - y0) - (x21 - x0) * (y23 - y0)) / b0
64            b3 = ((x21 - x0) * (y22 - y0) - (x22 - x0) * (y21 - y0)) / b0
65            n._coord = (
66                b1 * x11 + b2 * x12 + b3 * x13, b1 * y11 + b2 * y12 + b3 * y13)
67        for e in self._net2._edges:
68            for _lane in e.getLanes():
69                shape = []
70                for p in _lane.getShape3D():
71                    x0 = p[0]
72                    y0 = p[1]
73                    b1 = ((x22 - x0) * (y23 - y0) - (x23 - x0) * (y22 - y0)) / b0
74                    b2 = ((x23 - x0) * (y21 - y0) - (x21 - x0) * (y23 - y0)) / b0
75                    b3 = ((x21 - x0) * (y22 - y0) - (x22 - x0) * (y21 - y0)) / b0
76                    x = (b1 * x11 + b2 * x12 + b3 * x13)
77                    y = (b1 * y11 + b2 * y12 + b3 * y13)
78                    z = p[2]
79                    shape.append((x, y, z))
80                _lane.setShape(shape)
81            e.rebuildShape()