Esempio n. 1
0
    def OnButton(self, event):
        try:
            length = Decimal(self.length.GetValue())
            name = self.name.GetValue().strip()

            if length <= Decimal(0):
                # we don't accept non-positive values
                return

            editor = self.GetParent().editor
            tf = ui.trackfc.TrackFactory(editor)
            t = tf.CreateStraight(length)
            if name.strip() != "":
                t.name = name.strip()

            # Remember entered values
            config = wx.FileConfig.Get()
            config.Write("/InsertStraightTrack/length", self.length.GetValue())

            editor = self.GetParent().editor
            editor.scenery.AddRailTracking(t)

            self.Destroy()
        except ValueError:
            # Swallow the exception
            pass
Esempio n. 2
0
def exportScenery(path, tracks, switches, callback):
    sectors = dict()

    __sortTrackings(sectors, tracks, 'tracks')
    __sortTrackings(sectors, switches, 'switches')

    progress = 0
    percent = 0
    callback(percent)

    for sector in sectors.itervalues():
        sector_name = "%+05d%+05d.sct" % sector.position
        with file(os.path.abspath(os.path.join(path, sector_name)),
                  "wb") as fout:
            position = Vec3(
                Decimal(sector.position[0]) * DEC_SECTOR_SIZE,
                Decimal(sector.position[1]) * DEC_SECTOR_SIZE, Decimal("0.0"))
            writeSector(fout, position, sector.tracks, sector.switches)

        progress += 1
        newPercent = (progress * 100) / len(sectors)
        if (newPercent > percent):
            percent = newPercent
            callback(percent)

    with file(os.path.join(path, "default.scv"), "wb") as fout:
        writeVariant(fout, 0, sectors.values())
Esempio n. 3
0
def transformVec3_4(m, vec3):
    """
    Transform Vec3 using 4x4 matrix.
    """
    x, y, z = float(vec3.x), float(vec3.y), float(vec3.z)

    vec3.x = Decimal(m[0][0] * x + m[0][1] * y + m[0][2] * z + m[0][3])
    vec3.y = Decimal(m[1][0] * x + m[1][1] * y + m[1][2] * z + m[1][3])
    vec3.z = Decimal(m[2][0] * x + m[2][1] * y + m[2][2] * z + m[2][3])
Esempio n. 4
0
def transformVec3(m, vec3):
    """
    Transforms Vec3 using matrix 3x3
    """
    x, y, z = float(vec3.x), float(vec3.y), float(vec3.z)

    vec3.x = Decimal(m[0][0] * x + m[0][1] * y + m[0][2] * z)
    vec3.y = Decimal(m[1][0] * x + m[1][1] * y + m[1][2] * z)
    vec3.z = Decimal(m[2][0] * x + m[2][1] * y + m[2][2] * z)
Esempio n. 5
0
    def OnButton(self, event):
        """
        Sets the scroll to the editor part.
        """
        try:
            px = Decimal(str(self.x.GetValue()))
            py = Decimal(str(self.y.GetValue()))
            pz = Decimal(str(self.z.GetValue()))
            pScale = float(self.zoom.GetValue())

            editor = self.GetParent().editor
            editor.parts[0].SetScale(ui.editor.Scale(pScale))
            (vx, vy) = editor.parts[0].ModelToView(Vec3(px, py, pz))
            editor.parts[0].CenterViewAt((vx, vy))

            self.Destroy()
        except ValueError:
            # Swallow number parsing error
            pass
Esempio n. 6
0
    def OnButton(self, event):
        '''
        Sets the scroll to the editor part.
        '''
        try:
            px = Decimal(self.x.GetValue())
            py = Decimal(self.y.GetValue())
            pz = Decimal(self.z.GetValue())
            alpha = float(self.alpha.GetValue())
            gradient = float(self.gradient.GetValue())

            editor = self.GetParent().editor
            editor.SetBasePoint(
                ui.editor.BasePoint(Vec3(px, py, pz), alpha, gradient), True)

            self.Destroy()
        except ValueError:
            # Swallow number parsing error
            pass
Esempio n. 7
0
    def CreateCurve(self, length, radius, isLeft):
        """
        Creates curve track
        """
        angle = length / radius
        half = 0.5 * angle
        sin_a = sin(half)
        cos_a = cos(half)

        p1 = Vec3()
        p2 = Vec3()
        v1 = Vec3()
        v2 = Vec3()

        p1.x = Decimal(-radius * cos_a)
        p1.y = Decimal(radius * sin_a)
        p2.x = Decimal(-radius * cos_a)
        p2.y = Decimal(-radius * sin_a)

        ctrlX = -radius * (4.0 - cos_a) / 3.0
        ctrlY = -radius * (1.0 - cos_a) * (cos_a - 3.0) / (3.0 * sin_a)

        v1.x = Decimal(ctrlX) - p1.x
        v1.y = Decimal(ctrlY) - p1.y
        v2.x = Decimal(ctrlX) - p2.x
        v2.y = Decimal(-ctrlY) - p2.y

        # Left or right
        tr = LeftTrackTransform(length, radius) if isLeft \
            else RightTrackTransform(length, radius)
        tr.Transform([p1, p2], [v1, v2])

        basePoint = self.editor.basePoint

        tr = BasePointTransform(basePoint)
        tr.Transform([p1, p2], [v1, v2])

        basePoint.point = Vec3(p2.x, p2.y, p2.z)
        if isLeft:
            basePoint.alpha -= degrees(angle)
        else:
            basePoint.alpha += degrees(angle)

        # Refresh editor
        self.editor.SetBasePoint(basePoint, True)

        return Track(p1, v1, v2, p2)
Esempio n. 8
0
    def Transform(self, points, vectors):
        half = 0.5 * self.angle
        cos_a = cos(-pi + half)
        sin_a = sin(-pi + half)

        matrix = [ \
            [cos_a, -sin_a, 0.0], \
            [sin_a, cos_a, 0.0], \
            [0.0, 0.0, 1.0]]

        for p in points:
            transformVec3(matrix, p)
            p.x = p.x + Decimal(-self.radius)
        for v in vectors:
            transformVec3(matrix, v)
Esempio n. 9
0
 def ViewToModel(self, point):
     """
     Converts 2D point of UI editor coordinates into 3D point
     of scenery coordinates.
     
     Examples:
     >>> layout = EditorBounds()
     >>> layout.ViewToModel((1100, 1100))
     (0.000,0.000,0.000)
     >>> layout.scale = Scale(SCALE_MIN)
     >>> layout.ViewToModel((1100, 1100))
     (249000.000,-249000.000,0.000)
     >>> layout.scale = Scale(SCALE_MAX)
     >>> layout.ViewToModel((1100, 1100))
     (-999.500,999.500,0.000)
     """
     p3d = Vec3(
         Decimal(
             str((point[0] - self.extentX) / self.scale.get() +
                 float(self.minX))),
         Decimal(
             str(-((point[1] - self.extentY) / self.scale.get() -
                   float(self.maxY)))), Decimal("0"))
     return p3d
Esempio n. 10
0
    def Transform(self, points, vectors):
        half = self.angle / 2
        cos_a = cos(half)
        sin_a = sin(half)

        matrix = [ \
            [cos_a, sin_a, 0.0], \
            [-sin_a, cos_a, 0.0], \
            [0.0, 0.0, 1.0]]

        for p in points:
            transformVec3(matrix, p)
            p.x = p.x + Decimal(self.radius)
        for v in vectors:
            transformVec3(matrix, v)

        self.Swap(points[0], points[1])
        self.Swap(vectors[0], vectors[1])
Esempio n. 11
0
import math
import os.path

from sptmath import Vec3, Decimal
from model.tracks import Track, Switch
from model.groups import RailContainer

from time import sleep

from sctwriter import writeSector, SECTOR_SIZE
from scvwriter import writeVariant

DEC_SECTOR_SIZE = Decimal(str(SECTOR_SIZE))

#SECTOR_CENTER = Vec3(SECTOR_SIZE / 2, SECTOR_SIZE / 2, 0)


def exportScenery(path, tracks, switches, callback):
    sectors = dict()

    __sortTrackings(sectors, tracks, 'tracks')
    __sortTrackings(sectors, switches, 'switches')

    progress = 0
    percent = 0
    callback(percent)

    for sector in sectors.itervalues():
        sector_name = "%+05d%+05d.sct" % sector.position
        with file(os.path.abspath(os.path.join(path, sector_name)),
                  "wb") as fout:
Esempio n. 12
0
    def testTrackingConnections(self):
        underTest = Switch( \
            Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0")), \
            Vec3(Decimal("-1.924"), Decimal("33.927"), Decimal("0.0")), \
            Vec3(Decimal("1.924"), Decimal("33.927"), Decimal("0.0")), \
            Vec3(Decimal("0.0"), Decimal("11.336"), Decimal("0.0")), \
            Vec3(Decimal("1.282"), Decimal("-11.263"), Decimal("0.0")), \
            Vec3(Decimal("0.0"), Decimal("11.336"), Decimal("0.0")), \
            Vec3(Decimal("-1.282"), Decimal("-11.263"), Decimal("0.0")))
        t1 = Track( \
            Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0.0")), \
            Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0")), \
            Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0")), \
            Vec3(Decimal("-10.292"), Decimal("106.952"), Decimal("0.0")))
        t2 = Track( \
            Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0")), \
            Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0")), \
            Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0")), \
            Vec3(Decimal("0.0"), Decimal("-74.0"), Decimal("0.0")))

        underTest.n1 = t1
        underTest.nc = t2

        self.assertEquals(Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0")),
                          underTest.tracking2point(t2))
        self.assertEquals(Vec3(Decimal("1.924"), Decimal("33.927"), Decimal("0.0")), \
            underTest.tracking2point(None))
        self.assertEquals(Vec3(Decimal("-1.924"), Decimal("33.927"), Decimal("0.0")), \
            underTest.tracking2point(t1))

        self.assertEquals(None, \
            underTest.point2tracking(Vec3(Decimal("1.924"), Decimal("33.927"), Decimal("0.0"))))
        self.assertEquals(t1, \
            underTest.point2tracking(Vec3(Decimal("-1.924"), Decimal("33.927"), Decimal("0.0"))))
        self.assertEquals(t2, \
            underTest.point2tracking(Vec3(Decimal("0.0"), Decimal("0.0"), Decimal("0.0"))))
Esempio n. 13
0
 def testTrackingConnectionsNull(self):
     r2 = Switch( \
         pc = Vec3(), \
         p1 = Vec3(Decimal("-1.924"), Decimal("33.927"), Decimal("0")), \
         p2 = Vec3(Decimal("1.924"), Decimal("33.927"), Decimal("0")), \
         vc1 = Vec3(Decimal("0.0"), Decimal("11.336"), Decimal("0")), \
         v1 = Vec3(Decimal("1.282"), Decimal("-11.263"), Decimal("0")), \
         vc2 = Vec3(Decimal("0.0"), Decimal("11.336"), Decimal("0")), \
         v2 = Vec3(Decimal("-1.282"), Decimal("-11.263"), Decimal("0")))
     underTest = Track( \
         Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0")), \
         Vec3(), \
         Vec3(), \
         Vec3(Decimal("-10.292"), Decimal("106.952"), Decimal("0")))
 
     underTest.n1 = r2
 
     self.assertEquals(Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0")), \
         underTest.tracking2point(r2))
     self.assertEquals(Vec3(Decimal("-10.292"), Decimal("106.952"), Decimal("0")), \
         underTest.tracking2point(None))
 
     self.assertEquals(None, \
         underTest.point2tracking(Vec3(Decimal("-10.292"), Decimal("106.952"), Decimal("0"))))
     self.assertEquals(r2, \
         underTest.point2tracking(Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0"))))
Esempio n. 14
0
    def testTrackingConnections(self):
        t1 = Track( \
            Vec3(Decimal("-10.293"), Decimal("106.952"), Decimal("0")), \
            Vec3(Decimal("-0.565"), Decimal("4.968"), Decimal("0")), \
            Vec3(Decimal("0.316"), Decimal("-4.99"), Decimal("0")), \
            Vec3(Decimal("-11.715"), Decimal("121.891"), Decimal("0")))
        r2 = Switch( \
            pc = Vec3(Decimal("0"), Decimal("0"), Decimal("0")), \
            p1 = Vec3(Decimal("-1.924"), Decimal("33.927"), Decimal("0")), \
            p2 = Vec3(Decimal("1.924"), Decimal("33.927"), Decimal("0")), \
            vc1 = Vec3(Decimal("0"), Decimal("11.336"), Decimal("0")), \
            vc2 = Vec3(Decimal("0"), Decimal("11.336"), Decimal("0")), \
            v1 = Vec3(Decimal("1.282"), Decimal("-11.263"), Decimal("0")), \
            v2 = Vec3(Decimal("-1.282"), Decimal("-11.263"), Decimal("0")))
        underTest = Track( \
            Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0")), \
            Vec3(), \
            Vec3(), \
            Vec3(Decimal("-10.292"), Decimal("106.952"), Decimal("0")))

        underTest.n1 = r2
        underTest.n2 = t1

        self.assertEquals(Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0")), \
            underTest.tracking2point(r2))
        self.assertEquals(Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0")), \
            underTest.tracking2point(r2))

        self.assertEquals(t1, underTest.point2tracking( \
            Vec3(Decimal("-10.292"), Decimal("106.952"), Decimal("0"))))
        self.assertEquals(r2, underTest.point2tracking( \
            Vec3(Decimal("-1.924"), Decimal("33.427"), Decimal("0"))))