Skip to content

Commit

Permalink
Prepared fix for issue #1039.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsschmidt1337 committed Aug 2, 2024
1 parent ae4b0d9 commit 5afc3a4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 43 deletions.
91 changes: 48 additions & 43 deletions src/org/nschmidt/ldparteditor/dialog/primgen2/PrimGen2Dialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,32 +65,32 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
*/
public class PrimGen2Dialog extends PrimGen2Design {

private static final int CIRCLE = 0;
private static final int RING = 1;
private static final int CONE = 2;
private static final int TORUS = 3;
private static final int CYLINDER = 4;
private static final int DISC = 5;
private static final int DISC_NEGATIVE = 6;
private static final int DISC_NEGATIVE_TRUNCATED = 7;
private static final int CHORD = 8;
public static final int CIRCLE = 0;
public static final int RING = 1;
public static final int CONE = 2;
public static final int TORUS = 3;
public static final int CYLINDER = 4;
public static final int DISC = 5;
public static final int DISC_NEGATIVE = 6;
public static final int DISC_NEGATIVE_TRUNCATED = 7;
public static final int CHORD = 8;

private boolean doUpdate = false;
private boolean ok = false;

private final DecimalFormat decvformat4f = new DecimalFormat(View.NUMBER_FORMAT4F, new DecimalFormatSymbols(MyLanguage.getLocale()));
private final DecimalFormat decvformat0f = new DecimalFormat(View.NUMBER_FORMAT0F, new DecimalFormatSymbols(MyLanguage.getLocale()));
private static final DecimalFormat decvformat4f = new DecimalFormat(View.NUMBER_FORMAT4F, new DecimalFormatSymbols(MyLanguage.getLocale()));
private static final DecimalFormat decvformat0f = new DecimalFormat(View.NUMBER_FORMAT0F, new DecimalFormatSymbols(MyLanguage.getLocale()));

private final DecimalFormat decformat4f = new DecimalFormat(View.NUMBER_FORMAT4F, new DecimalFormatSymbols(Locale.ENGLISH));
private static final DecimalFormat decformat4f = new DecimalFormat(View.NUMBER_FORMAT4F, new DecimalFormatSymbols(Locale.ENGLISH));

private String name = "1-4edge.dat"; //$NON-NLS-1$
private static String name = "1-4edge.dat"; //$NON-NLS-1$

private enum EventType {
SPN,
CBO
}

private String resPrefix = ""; //$NON-NLS-1$
private static String resPrefix = ""; //$NON-NLS-1$

/**
* Create the dialog.
Expand All @@ -114,6 +114,8 @@ public int open() {

spnMajorPtr[0].setValue(2);
spnMinorPtr[0].setValue(BigDecimal.ONE);
name = "1-4edge.dat"; //$NON-NLS-1$
resPrefix = ""; //$NON-NLS-1$

Display.getCurrent().asyncExec(() -> {

Expand Down Expand Up @@ -500,7 +502,10 @@ private void rebuildPrimitive(EventType et, Widget w) {
}

doUpdate = false;

final UserSettingState user = WorkbenchManager.getUserSettingState();
final String ldrawName = user.getLdrawUserName();
final String realName = user.getRealUserName();
final int pType = cmbTypePtr[0].getSelectionIndex();
int divisions = spnDivisionsPtr[0].getValue();
int segments = spnSegmentsPtr[0].getValue();
int edgesPerCrossSections = spnEdgesPerCrossSectionsPtr[0].getValue();
Expand All @@ -509,6 +514,18 @@ private void rebuildPrimitive(EventType et, Widget w) {
double width = spnMinorPtr[0].getValue().doubleValue();
double size = spnSizePtr[0].getValue().doubleValue();

final String primitiveSource = buildPrimitiveSource(pType, divisions, segments, edgesPerCrossSections, major, minor, width, size, ccw, torusType, realName, ldrawName);

if (isOfficialRules(pType, size, divisions, segments, minor, ccw)) {
lblStandardPtr[0].setText(I18n.PRIMGEN_STANDARD);
} else {
lblStandardPtr[0].setText(I18n.PRIMGEN_NON_STANDARD);
}
txtDataPtr[0].setText(primitiveSource);
}

public static String buildPrimitiveSource(int pType, int divisions, int segments, int edgesPerCrossSections, int major, int minor, double width, double size, boolean ccw, int torusType, String realName, String ldrawName) {

int gcd = gcd(divisions, segments);

int upper = segments / gcd;
Expand Down Expand Up @@ -560,11 +577,6 @@ private void rebuildPrimitive(EventType et, Widget w) {
}

final StringBuilder sb = new StringBuilder();

final UserSettingState user = WorkbenchManager.getUserSettingState();

String ldrawName = user.getLdrawUserName();
String realName = user.getRealUserName();
if (ldrawName == null || ldrawName.isEmpty()) {
if (realName == null || realName.isEmpty()) {
sb.append("0 Author: Primitive Generator 2\n"); //$NON-NLS-1$
Expand All @@ -589,7 +601,6 @@ private void rebuildPrimitive(EventType et, Widget w) {
}

boolean truncated = false;
final int pType = cmbTypePtr[0].getSelectionIndex();
switch (pType) {
case CIRCLE:
name = upper + "-" + lower + "edge.dat"; //$NON-NLS-1$ //$NON-NLS-2$
Expand Down Expand Up @@ -857,16 +868,10 @@ private void rebuildPrimitive(EventType et, Widget w) {
}

sb.append("0 // Build by LDPartEditor (PrimGen 2.X)"); //$NON-NLS-1$

if (isOfficialRules(pType, size, divisions, segments, minor, ccw)) {
lblStandardPtr[0].setText(I18n.PRIMGEN_STANDARD);
} else {
lblStandardPtr[0].setText(I18n.PRIMGEN_NON_STANDARD);
}
txtDataPtr[0].setText(sb.toString());
return sb.toString();
}

private Object cylinder(int divisions, int segments, boolean ccw) {
private static Object cylinder(int divisions, int segments, boolean ccw) {

// Crazy Reverse Engineering from Mike's PrimGen2
// Thanks to Mr. Heidemann! :)
Expand Down Expand Up @@ -1002,7 +1007,7 @@ else if (num == 0)
return sb2.toString();
}

private Object cone(int divisions, int segments, double innerDiameter, boolean ccw, double width) {
private static Object cone(int divisions, int segments, double innerDiameter, boolean ccw, double width) {

// Crazy Reverse Engineering from Mike's PrimGen2
// Thanks to Mr. Heidemann! :)
Expand Down Expand Up @@ -1138,7 +1143,7 @@ else if (num == 0)
return sb2.toString();
}

private String ring(int divisions, int segments, double innerDiameter, boolean ccw, double width) {
private static String ring(int divisions, int segments, double innerDiameter, boolean ccw, double width) {

// Crazy Reverse Engineering from Mike's PrimGen2
// Thanks to Mr. Heidemann! :)
Expand Down Expand Up @@ -1205,7 +1210,7 @@ private String ring(int divisions, int segments, double innerDiameter, boolean c
return sb2.toString();
}

private String torus(int divisions, int segments, int edgesPerCrossSections, int type, int major, int minor, boolean ccw) {
private static String torus(int divisions, int segments, int edgesPerCrossSections, int type, int major, int minor, boolean ccw) {

// Crazy Reverse Engineering from Mike's PrimGen2
// Thanks to Mr. Heidemann! :)
Expand Down Expand Up @@ -1380,7 +1385,7 @@ private String torus(int divisions, int segments, int edgesPerCrossSections, int
return sb2.toString();
}

private void addCondlinesForTorusInside(List<Double[]> points, int num, int num5, int num8, StringBuilder sb2, boolean closed) {
private static void addCondlinesForTorusInside(List<Double[]> points, int num, int num5, int num8, StringBuilder sb2, boolean closed) {
int pointIndex = 0;
for (int num2 = 0; num2 <= num8; num2++)
{
Expand Down Expand Up @@ -1628,7 +1633,7 @@ private void addCondlinesForTorusInside(List<Double[]> points, int num, int num5
}
}

private void addCondlinesForTorusTube(List<Double[]> points, int num, int num5, int num8, StringBuilder sb2, boolean closed) {
private static void addCondlinesForTorusTube(List<Double[]> points, int num, int num5, int num8, StringBuilder sb2, boolean closed) {
int pointIndex = 0;
for (int num2 = 0; num2 <= num8; num2++)
{
Expand Down Expand Up @@ -1824,7 +1829,7 @@ private void addCondlinesForTorusTube(List<Double[]> points, int num, int num5,
}
}

private void addCondlinesForTorusOutside(List<Double[]> points, int num, int num5, int num8, StringBuilder sb2, boolean closed) {
private static void addCondlinesForTorusOutside(List<Double[]> points, int num, int num5, int num8, StringBuilder sb2, boolean closed) {
int pointIndex = 0;
for (int num2 = 0; num2 <= num8; num2++)
{
Expand Down Expand Up @@ -2073,15 +2078,15 @@ private void addCondlinesForTorusOutside(List<Double[]> points, int num, int num
}

@SuppressWarnings("java:S2111")
private double round4f(double d) {
private static double round4f(double d) {
return new BigDecimal(d).setScale(4, RoundingMode.HALF_EVEN).doubleValue();
}

private String formatDec(double d) {
private static String formatDec(double d) {
return bigDecimalToString(new BigDecimal(String.valueOf(d)).setScale(8, RoundingMode.HALF_UP).setScale(4, RoundingMode.HALF_UP));
}

private String bigDecimalToString(BigDecimal bd) {
private static String bigDecimalToString(BigDecimal bd) {
String result;
if (bd.compareTo(BigDecimal.ZERO) == 0)
return "0"; //$NON-NLS-1$
Expand All @@ -2090,7 +2095,7 @@ private String bigDecimalToString(BigDecimal bd) {
return result;
}

private int gcd(int a, int b) {
private static int gcd(int a, int b) {
while (b > 0)
{
int temp = b;
Expand All @@ -2100,7 +2105,7 @@ private int gcd(int a, int b) {
return a;
}

private String removeTrailingZeros(String str) {
private static String removeTrailingZeros(String str) {
StringBuilder sb = new StringBuilder();
if (str.contains(".")) { //$NON-NLS-1$
final int len = str.length();
Expand Down Expand Up @@ -2133,7 +2138,7 @@ private String removeTrailingZeros(String str) {
}
}

private String removeTrailingZeros2(String str) {
private static String removeTrailingZeros2(String str) {
StringBuilder sb = new StringBuilder();
String result = str;
if (str.contains(".")) { //$NON-NLS-1$
Expand Down Expand Up @@ -2168,7 +2173,7 @@ private String removeTrailingZeros2(String str) {
return result;
}

private String removeTrailingZeros3(String str) {
private static String removeTrailingZeros3(String str) {
StringBuilder sb = new StringBuilder();
String result = str;
if (str.contains(".")) { //$NON-NLS-1$
Expand Down Expand Up @@ -2206,7 +2211,7 @@ private String removeTrailingZeros3(String str) {
return result;
}

private String addExtraSpaces1(String str) {
private static String addExtraSpaces1(String str) {
final int len = str.length();
if (len == 1) {
return " " + str; //$NON-NLS-1$
Expand Down
49 changes: 49 additions & 0 deletions test/org/nschmidt/ldparteditor/PrimGen2Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* MIT - License
Copyright (c) 2012 - this year, Nils Schmidt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
package org.nschmidt.ldparteditor;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.nschmidt.ldparteditor.dialog.primgen2.PrimGen2Dialog;

@SuppressWarnings("java:S5960")
public class PrimGen2Test {

private static final String NAME = "Nils Schmidt"; //$NON-NLS-1$
private static final String LDRAW_USER = "BlackBrick89"; //$NON-NLS-1$

@Test(expected = ArithmeticException.class)
public void testZeroSizedCirlce() {
PrimGen2Dialog.buildPrimitiveSource(PrimGen2Dialog.CIRCLE, 0, 0, 0, 0, 0, 0, 0, false, 0, NAME, LDRAW_USER);
}

@Test
public void testOneSixteenthCirlce() {
final String result = PrimGen2Dialog.buildPrimitiveSource(PrimGen2Dialog.CIRCLE, 16, 1, 0, 0, 0, 0, 0, false, 0, NAME, LDRAW_USER);
assertEquals("""
0 Circle 0.0625
0 Name: 1-16edge.dat
0 Author: Nils Schmidt [BlackBrick89]
0 !LDRAW_ORG Unofficial_Primitive
0 !LICENSE Licensed under CC BY 4.0 : see CAreadme.txt
0 BFC CERTIFY CW
2 24 1 0 0 0.9239 0 0.3827
0 // Build by LDPartEditor (PrimGen 2.X)""", result); //$NON-NLS-1$
}
}

0 comments on commit 5afc3a4

Please sign in to comment.