From b76a81458c3c78a58472139635a585adb509f163 Mon Sep 17 00:00:00 2001 From: iobyte Date: Sun, 28 Jan 2024 18:38:30 +0000 Subject: [PATCH] trackeditor: calculate sub-segment heights for linear and spline profils git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@9291 30fe4595-0a0c-4342-8851-515496e4dcbd Former-commit-id: 17a2cb565bd2282b9941be8e98d938a645eb3f64 Former-commit-id: 7d9142507ca4bde4b62b003d22fbc5c2e82c88b1 --- .../trackeditor/plugin/torcs/XmlReader.java | 22 ++- src/tools/trackeditor/utils/Properties.java | 2 +- .../trackeditor/utils/SegmentVector.java | 11 -- .../trackeditor/utils/circuit/Curve.java | 94 +++++++--- .../trackeditor/utils/circuit/Segment.java | 177 +++++++++++------- .../trackeditor/utils/circuit/Straight.java | 54 +++++- 6 files changed, 238 insertions(+), 122 deletions(-) diff --git a/src/tools/trackeditor/plugin/torcs/XmlReader.java b/src/tools/trackeditor/plugin/torcs/XmlReader.java index 7510f72dd..f591f0880 100644 --- a/src/tools/trackeditor/plugin/torcs/XmlReader.java +++ b/src/tools/trackeditor/plugin/torcs/XmlReader.java @@ -846,18 +846,24 @@ public class XmlReader shape = new Curve(type, null); } shape = setSegment(e, shape, prev); - try - { - shape.calcShape(editorFrame); - } catch (Exception e1) - { - // TODO Auto-generated catch block - e1.printStackTrace(); - } trackData.add(shape); prev = shape; } + editorFrame.getTrackData().setSegments(trackData); + editorFrame.getTrackData().calculateSegmentValues(); + + for (Segment segment : editorFrame.getTrackData().getSegments()) + { + try + { + segment.calcShape(editorFrame); + } + catch (Exception e) + { + e.printStackTrace(); + } + } } private synchronized Segment setSegment(Element seg, Segment shape, diff --git a/src/tools/trackeditor/utils/Properties.java b/src/tools/trackeditor/utils/Properties.java index 856ab871e..412cb5d12 100644 --- a/src/tools/trackeditor/utils/Properties.java +++ b/src/tools/trackeditor/utils/Properties.java @@ -34,7 +34,7 @@ public class Properties private static Properties instance = new Properties(); private Vector propertiesListeners = new Vector(); public final String title = "sd2-trackeditor"; - public final String version = "1.4.22"; + public final String version = "1.4.23"; private String path; private double imageScale = 1; diff --git a/src/tools/trackeditor/utils/SegmentVector.java b/src/tools/trackeditor/utils/SegmentVector.java index 09678f508..5362bde0c 100644 --- a/src/tools/trackeditor/utils/SegmentVector.java +++ b/src/tools/trackeditor/utils/SegmentVector.java @@ -135,17 +135,6 @@ public class SegmentVector extends Vector } } - public void dumpCalculated(String indent) - { - System.out.println(indent + "Track Segments"); - - for (int i = 0; i < size(); i++) - { - System.out.println(indent + " segment[" + i + "]"); - get(i).dumpCalculated(indent + " "); - } - } - public void dumpLinks() { for (int i = 0; i < size(); i++) diff --git a/src/tools/trackeditor/utils/circuit/Curve.java b/src/tools/trackeditor/utils/circuit/Curve.java index 59d034cf6..58a93dad5 100644 --- a/src/tools/trackeditor/utils/circuit/Curve.java +++ b/src/tools/trackeditor/utils/circuit/Curve.java @@ -93,7 +93,7 @@ public class Curve extends Segment // don't use barrier points if ((i >= 12 && i <= 15) || (i >= 24 && i <= 27)) continue; - + if (minX > points[i].x) minX = points[i].x; if (maxX < points[i].x) @@ -124,13 +124,10 @@ public class Curve extends Segment double rightSideEndWidth = getValidRightSideEndWidth(editorFrame); double leftBarrierWidth = getValidLeftBarrierWidth(editorFrame); double rightBarrierWidth = getValidRightBarrierWidth(editorFrame); - - /** - * - * New code - * - * - */ + double leftStartHeight = this.getCalculatedHeightStartLeft(); + double rightStartHeight = this.getCalculatedHeightStartRight(); + double leftEndHeight = this.getCalculatedHeightEndLeft(); + double rightEndHeight = this.getCalculatedHeightEndRight(); // calc turn length double arc = getArcRad(); @@ -162,9 +159,13 @@ public class Curve extends Segment tmpRadius += deltaRadiusStep; } stepLength *= arc / tmpAngle; - } else + } + else + { deltaRadiusStep = (radiusEnd - radiusStart) / nbSteps; - } else + } + } + else { deltaRadiusStep = 0; } @@ -172,7 +173,7 @@ public class Curve extends Segment if (points == null || points.length != 4 * (7 + (showArrows > 0.0 ? 1 : 0)) * nbSteps) { points = new Point3D[4 * (7 + (showArrows > 0.0 ? 1 : 0)) * nbSteps]; - + for (int i = 0; i < points.length; i++) points[i] = new Point3D(); @@ -188,6 +189,24 @@ public class Curve extends Segment double leftSideDeltaStep = (leftSideEndWidth - leftSideStartWidth) / nbSteps; double rightSideDeltaStep = (rightSideEndWidth - rightSideStartWidth) / nbSteps; + double leftHeightDeltaStep = (leftEndHeight - leftStartHeight) / nbSteps; + double rightHeightDeltaStep = (rightEndHeight - rightStartHeight) / nbSteps; + + boolean linear = getValidProfil(editorFrame).equals("linear"); + + double T1l = getCalculatedStartTangentLeft() * getLength(); + double T2l = getCalculatedEndTangentLeft() * getLength(); + double tl = 0.0; + double dtl = 1.0 / nbSteps; + double T1r = getCalculatedStartTangentRight() * getLength(); + double T2r = getCalculatedEndTangentRight() * getLength(); + double tr = 0.0; + double dtr = 1.0 / nbSteps; + double curzsl = leftStartHeight; + double curzsr = rightStartHeight; + double curzel = leftStartHeight; + double curzer = rightStartHeight; + int currentSubSeg = 0; for (int nStep = 0; nStep < nbSteps; nStep++) @@ -204,7 +223,8 @@ public class Curve extends Segment xCenter = currentX - cosTrans * curRadius; yCenter = currentY - sinTrans * curRadius; thisStepArc = -stepLength / curRadius; - } else + } + else { xCenter = currentX + cosTrans * curRadius; yCenter = currentY + sinTrans * curRadius; @@ -212,8 +232,9 @@ public class Curve extends Segment } if (nStep == 0) - // center.setLocation( xCenter, -yCenter ); + { center.setLocation(xCenter, yCenter); + } double cos = Math.cos(thisStepArc); double sin = Math.sin(thisStepArc); @@ -241,6 +262,30 @@ public class Curve extends Segment points[currentSubSeg + 2].x = x * cos - y * sin + xCenter; points[currentSubSeg + 2].y = y * cos + x * sin + yCenter; + if (linear) + { + points[currentSubSeg + 0].z = leftStartHeight + leftHeightDeltaStep * nStep; + points[currentSubSeg + 1].z = leftStartHeight + leftHeightDeltaStep * (nStep + 1); + points[currentSubSeg + 2].z = rightStartHeight + rightHeightDeltaStep * (nStep + 1); + points[currentSubSeg + 3].z = rightStartHeight + rightHeightDeltaStep * nStep; + } + else + { + tl += dtl; + tr += dtr; + + curzsl = curzel; + curzel = trackSpline(leftStartHeight, leftEndHeight, T1l, T2l, tl); + + curzsr = curzer; + curzer = trackSpline(rightStartHeight, rightEndHeight, T1r, T2r, tr); + + points[currentSubSeg + 0].z = curzsl; + points[currentSubSeg + 1].z = curzel; + points[currentSubSeg + 2].z = curzer; + points[currentSubSeg + 3].z = curzsr; + } + currentSubSeg += 4; // left border @@ -286,7 +331,7 @@ public class Curve extends Segment currentSubSeg += 4; // left barrier - + points[currentSubSeg + 0].x = currentX + cosTransLeft * ((trackWidth / 2) + leftBorderWidth + leftSideStartWidth + leftBarrierWidth + (leftSideDeltaStep * nStep)); points[currentSubSeg + 0].y = currentY + sinTransLeft @@ -304,7 +349,7 @@ public class Curve extends Segment points[currentSubSeg + 2].y = points[currentSubSeg - 2].y; currentSubSeg += 4; - + // right border points[currentSubSeg + 0].x = currentX - cosTransLeft * (trackWidth / 2 + rightBorderWidth); @@ -350,9 +395,9 @@ public class Curve extends Segment points[currentSubSeg + 2].y = y * cos + x * sin + yCenter; currentSubSeg += 4; - + // right barrier - + points[currentSubSeg + 0].x = currentX - cosTransLeft * ((trackWidth / 2) + rightBorderWidth + rightSideStartWidth + rightBarrierWidth + (rightSideDeltaStep * nStep)); points[currentSubSeg + 0].y = currentY - sinTransLeft @@ -414,21 +459,16 @@ public class Curve extends Segment endTrackAlpha += EPMath.PI_MUL_2; while (endTrackAlpha > Math.PI) endTrackAlpha -= EPMath.PI_MUL_2; - + Editor.getProperties().setCurrentA(currentA); Editor.getProperties().setCurrentX(currentX); Editor.getProperties().setCurrentY(currentY); } - - -// public void draw(Graphics g, AffineTransform affineTransform) -// { -// calcShape(); -// } public void drag(Point2D.Double dragDelta) { } + /** * @return Returns the arc. */ @@ -501,7 +541,7 @@ public class Curve extends Segment { this.marks = marks; } - + public Object clone() { Curve s; @@ -509,7 +549,7 @@ public class Curve extends Segment s.arcDeg = this.arcDeg; s.radiusStart = this.radiusStart; s.radiusEnd = this.radiusEnd; - + return s; // return the clone } -} \ No newline at end of file +} diff --git a/src/tools/trackeditor/utils/circuit/Segment.java b/src/tools/trackeditor/utils/circuit/Segment.java index d4217c912..ae1ecceb4 100644 --- a/src/tools/trackeditor/utils/circuit/Segment.java +++ b/src/tools/trackeditor/utils/circuit/Segment.java @@ -308,6 +308,21 @@ public class Segment implements Cloneable return false; } + public double trackSpline(double p0, double p1, double t0, double t1, double t) + { + double t2, t3; + double h0, h1, h2, h3; + + t2 = t * t; + t3 = t * t2; + h1 = 3 * t2 - 2 * t3; + h0 = 1 - h1; + h2 = t3 - 2 * t2 + t; + h3 = t3 - t2; + + return h0 * p0 + h1 * p1 + h2 * t0 + h3 * t1; + } + public Rectangle2D.Double getBounds() { if (points == null || points.length == 0) @@ -2239,99 +2254,119 @@ public class Segment implements Cloneable right.setSideBankingType(getValidRightSideBankingType(editorFrame)); } - public void dumpCalculated(String indent) - { - System.out.println(indent + "name : " + name); - System.out.println(indent + "type : " + type); - System.out.println(indent + "calculatedHeightStart : " + calculatedHeightStart); - System.out.println(indent + "calculatedHeightStartLeft : " + calculatedHeightStartLeft); - System.out.println(indent + "calculatedHeightStartRight : " + calculatedHeightStartRight); - System.out.println(indent + "calculatedHeightEnd : " + calculatedHeightEndLeft); - System.out.println(indent + "calculatedHeightEndLeft : " + calculatedHeightEndLeft); - System.out.println(indent + "calculatedHeightEndRight : " + calculatedHeightEndRight); - System.out.println(indent + "calculatedGrade : " + calculatedGrade); - System.out.println(indent + "calculatedBankingStart : " + calculatedBankingStart); - System.out.println(indent + "calculatedBankingEnd : " + calculatedBankingEnd); - System.out.println(indent + "calculatedStartTangent : " + calculatedStartTangent); - System.out.println(indent + "calculatedStartTangentLeft : " + calculatedStartTangentLeft); - System.out.println(indent + "calculatedStartTangentRight : " + calculatedStartTangentRight); - System.out.println(indent + "calculatedEndTangent : " + calculatedEndTangent); - System.out.println(indent + "calculatedEndTangentLeft : " + calculatedEndTangentLeft); - System.out.println(indent + "calculatedEndTangentRight : " + calculatedEndTangentRight); - } - public void dump(String indent) + { + dump(indent, true, true, true, true); + } + + public void dump(String indent, boolean dumpCalculated, boolean dumpPoints, boolean dumpTrPoints, boolean dumpToDraw) { System.out.println(indent + "Segment"); - System.out.println(indent + " previousShape : " + (previousShape != null ? previousShape.name : "null")); - System.out.println(indent + " nextShape : " + (nextShape != null ? nextShape.name : "null")); - System.out.println(indent + " name : " + name); - System.out.println(indent + " type : " + type); - System.out.println(indent + " count : " + count); - System.out.println(indent + " calculatedHeightStart : " + calculatedHeightStart); - System.out.println(indent + " calculatedHeightStartLeft : " + calculatedHeightStartLeft); - System.out.println(indent + " calculatedHeightStartRight : " + calculatedHeightStartRight); - System.out.println(indent + " calculatedHeightEnd : " + calculatedHeightEndLeft); - System.out.println(indent + " calculatedHeightEndLeft : " + calculatedHeightEndLeft); - System.out.println(indent + " calculatedHeightEndRight : " + calculatedHeightEndRight); - System.out.println(indent + " calculatedGrade : " + calculatedGrade); - System.out.println(indent + " calculatedBankingStart : " + calculatedBankingStart); - System.out.println(indent + " calculatedBankingEnd : " + calculatedBankingEnd); - System.out.println(indent + " calculatedStartTangent : " + calculatedStartTangent); - System.out.println(indent + " calculatedStartTangentLeft : " + calculatedStartTangentLeft); - System.out.println(indent + " calculatedStartTangentRight : " + calculatedStartTangentRight); - System.out.println(indent + " calculatedEndTangent : " + calculatedEndTangent); - System.out.println(indent + " calculatedEndTangentLeft : " + calculatedEndTangentLeft); - System.out.println(indent + " calculatedEndTangentRight : " + calculatedEndTangentRight); + System.out.println(indent + " previousShape : " + (previousShape != null ? previousShape.name : "null")); + System.out.println(indent + " nextShape : " + (nextShape != null ? nextShape.name : "null")); + System.out.println(indent + " name : " + name); + System.out.println(indent + " type : " + type); + System.out.println(indent + " count : " + count); + System.out.println(indent + " length : " + length); + System.out.println(indent + " surface : " + surface); + System.out.println(indent + " heightStart : " + heightStart); + System.out.println(indent + " heightStartLeft : " + heightStartLeft); + System.out.println(indent + " heightStartRight : " + heightStartRight); + System.out.println(indent + " heightEnd : " + heightEnd); + System.out.println(indent + " heightEndLeft : " + heightEndLeft); + System.out.println(indent + " heightEndRight : " + heightEndRight); + System.out.println(indent + " grade : " + grade); + System.out.println(indent + " bankingStart : " + bankingStart); + System.out.println(indent + " bankingEnd : " + bankingEnd); + System.out.println(indent + " profil : " + profil); + System.out.println(indent + " profilSteps : " + profilSteps); + System.out.println(indent + " profilStepsLength : " + profilStepsLength); + System.out.println(indent + " profilStartTangent : " + profilStartTangent); + System.out.println(indent + " profilEndTangent : " + profilEndTangent); + System.out.println(indent + " profilStartTangentLeft : " + profilStartTangentLeft); + System.out.println(indent + " profilEndTangentLeft : " + profilEndTangentLeft); + System.out.println(indent + " profilStartTangentRight : " + profilStartTangentRight); + System.out.println(indent + " profilEndTangentRight : " + profilEndTangentRight); + if (dumpCalculated) + { + System.out.println(indent + " calculatedHeightStart : " + calculatedHeightStart); + System.out.println(indent + " calculatedHeightStartLeft : " + calculatedHeightStartLeft); + System.out.println(indent + " calculatedHeightStartRight : " + calculatedHeightStartRight); + System.out.println(indent + " calculatedHeightEnd : " + calculatedHeightEndLeft); + System.out.println(indent + " calculatedHeightEndLeft : " + calculatedHeightEndLeft); + System.out.println(indent + " calculatedHeightEndRight : " + calculatedHeightEndRight); + System.out.println(indent + " calculatedGrade : " + calculatedGrade); + System.out.println(indent + " calculatedBankingStart : " + calculatedBankingStart); + System.out.println(indent + " calculatedBankingEnd : " + calculatedBankingEnd); + System.out.println(indent + " calculatedStartTangent : " + calculatedStartTangent); + System.out.println(indent + " calculatedStartTangentLeft : " + calculatedStartTangentLeft); + System.out.println(indent + " calculatedStartTangentRight : " + calculatedStartTangentRight); + System.out.println(indent + " calculatedEndTangent : " + calculatedEndTangent); + System.out.println(indent + " calculatedEndTangentLeft : " + calculatedEndTangentLeft); + System.out.println(indent + " calculatedEndTangentRight : " + calculatedEndTangentRight); + } + if (points != null) { - System.out.println(indent + " points : " + points.length); - for (int i = 0; i < points.length; i++) + System.out.println(indent + " points : " + points.length); + if (dumpPoints) { - System.out.println(indent + " points[" + i + "] " + points[i].x + ", " + points[i].y + ", " + points[i].z); + for (int i = 0; i < points.length; i++) + { + System.out.println(indent + " points[" + i + "] " + + String.format("%12.7f", points[i].x) + ", " + + String.format("%12.7f", points[i].y) + ", " + + String.format("%12.7f", points[i].z)); + } } } else { - System.out.println(indent + " points : null"); + System.out.println(indent + " points : null"); } if (trPoints != null) { - System.out.println(indent + " trPoints : " + trPoints.length); - for (int i = 0; i < trPoints.length; i++) + System.out.println(indent + " trPoints : " + trPoints.length); + if (dumpTrPoints) { - System.out.println(indent + " trPoints[" + i + "] " + trPoints[i].x + ", " + trPoints[i].y); + for (int i = 0; i < trPoints.length; i++) + { + System.out.println(indent + " trPoints[" + i + "] " + trPoints[i].x + ", " + trPoints[i].y); + } } } else { - System.out.println(indent + " trPoints : null"); + System.out.println(indent + " trPoints : null"); } - if (xToDraw != null) + if (dumpToDraw) { - System.out.println(indent + " xToDraw : " + xToDraw.length); - for (int i = 0; i < xToDraw.length; i++) + if (xToDraw != null) { - System.out.println(indent + " xToDraw[" + i + "] " + xToDraw[i]); + System.out.println(indent + " xToDraw : " + xToDraw.length); + for (int i = 0; i < xToDraw.length; i++) + { + System.out.println(indent + " xToDraw[" + i + "] " + xToDraw[i]); + } + } + else + { + System.out.println(indent + " xToDraw : null"); + } + if (yToDraw != null) + { + System.out.println(indent + " yToDraw : " + yToDraw.length); + for (int i = 0; i < yToDraw.length; i++) + { + System.out.println(indent + " yToDraw[" + i + "] " + yToDraw[i]); + } + } + else + { + System.out.println(indent + " yToDraw : null"); } } - else - { - System.out.println(indent + " xToDraw : null"); - } - if (yToDraw != null) - { - System.out.println(indent + " yToDraw : " + yToDraw.length); - for (int i = 0; i < yToDraw.length; i++) - { - System.out.println(indent + " yToDraw[" + i + "] " + yToDraw[i]); - } - } - else - { - System.out.println(indent + " yToDraw : null"); - } - System.out.println(indent + " dx : " + dx); - System.out.println(indent + " dy : " + dy); + System.out.println(indent + " dx : " + dx); + System.out.println(indent + " dy : " + dy); } } diff --git a/src/tools/trackeditor/utils/circuit/Straight.java b/src/tools/trackeditor/utils/circuit/Straight.java index 3476c0f4e..26b02fc2b 100644 --- a/src/tools/trackeditor/utils/circuit/Straight.java +++ b/src/tools/trackeditor/utils/circuit/Straight.java @@ -69,7 +69,7 @@ public class Straight extends Segment // don't use barrier points if ((i >= 12 && i <= 15) || (i >= 24 && i <= 27)) continue; - + if (minX > points[i].x) minX = points[i].x; if (maxX < points[i].x) @@ -99,6 +99,10 @@ public class Straight extends Segment double rightSideEndWidth = getValidRightSideEndWidth(editorFrame); double leftBarrierWidth = getValidLeftBarrierWidth(editorFrame); double rightBarrierWidth = getValidRightBarrierWidth(editorFrame); + double leftStartHeight = this.getCalculatedHeightStartLeft(); + double rightStartHeight = this.getCalculatedHeightStartRight(); + double leftEndHeight = this.getCalculatedHeightEndLeft(); + double rightEndHeight = this.getCalculatedHeightEndRight(); int nbSteps = 1; if (hasProfilSteps()) @@ -127,12 +131,30 @@ public class Straight extends Segment double leftSideDeltaStep = (leftSideEndWidth - leftSideStartWidth) / nbSteps; double rightSideDeltaStep = (rightSideEndWidth - rightSideStartWidth) / nbSteps; + double leftHeightDeltaStep = (leftEndHeight - leftStartHeight) / nbSteps; + double rightHeightDeltaStep = (rightEndHeight - rightStartHeight) / nbSteps; + double cos = Math.cos(currentA) * stepLength; double sin = Math.sin(currentA) * stepLength; double cosTransLeft = Math.cos(currentA + Math.PI / 2); double sinTransLeft = Math.sin(currentA + Math.PI / 2); + boolean linear = getValidProfil(editorFrame).equals("linear"); + + double T1l = getCalculatedStartTangentLeft() * getLength(); + double T2l = getCalculatedEndTangentLeft() * getLength(); + double tl = 0.0; + double dtl = 1.0 / nbSteps; + double T1r = getCalculatedStartTangentRight() * getLength(); + double T2r = getCalculatedEndTangentRight() * getLength(); + double tr = 0.0; + double dtr = 1.0 / nbSteps; + double curzsl = leftStartHeight; + double curzsr = rightStartHeight; + double curzel = leftStartHeight; + double curzer = rightStartHeight; + for (int nStep = 0; nStep < nbSteps; nStep++) { // track @@ -148,6 +170,30 @@ public class Straight extends Segment points[currentSubSeg + 2].x = points[currentSubSeg + 3].x + cos; points[currentSubSeg + 2].y = points[currentSubSeg + 3].y + sin; + if (linear) + { + points[currentSubSeg + 0].z = leftStartHeight + leftHeightDeltaStep * nStep; + points[currentSubSeg + 1].z = leftStartHeight + leftHeightDeltaStep * (nStep + 1); + points[currentSubSeg + 2].z = rightStartHeight + rightHeightDeltaStep * (nStep + 1); + points[currentSubSeg + 3].z = rightStartHeight + rightHeightDeltaStep * nStep; + } + else + { + tl += dtl; + tr += dtr; + + curzsl = curzel; + curzel = trackSpline(leftStartHeight, leftEndHeight, T1l, T2l, tl); + + curzsr = curzer; + curzer = trackSpline(rightStartHeight, rightEndHeight, T1r, T2r, tr); + + points[currentSubSeg + 0].z = curzsl; + points[currentSubSeg + 1].z = curzel; + points[currentSubSeg + 2].z = curzer; + points[currentSubSeg + 3].z = curzsr; + } + // left border points[currentSubSeg + 4].x = currentX + cosTransLeft * (trackWidth / 2 + leftBorderWidth); @@ -273,13 +319,13 @@ public class Straight extends Segment { } - + public Object clone() { Straight s; s = (Straight) super.clone(); - + return s; // return the clone } -} \ No newline at end of file +}