diff --git a/geometry/build.gradle b/geometry/build.gradle index fe89829..646994e 100644 --- a/geometry/build.gradle +++ b/geometry/build.gradle @@ -6,36 +6,67 @@ */ plugins { - // Apply the java-library plugin for API and implementation separation. - id 'java-library' + // Apply the java-library plugin for API and implementation separation. + id 'java-library' + id "com.diffplug.spotless" version "6.24.0" + } repositories { - // Use Maven Central for resolving dependencies. - mavenCentral() + // Use Maven Central for resolving dependencies. + mavenCentral() } +spotless { +// optional: limit format enforcement to just the files changed by this feature branch +ratchetFrom 'origin/main' + +format 'misc', { + // define the files to apply `misc` to + target '*.gradle', '.gitattributes', '.gitignore' + + // define the steps to apply to those files + trimTrailingWhitespace() + indentWithTabs() // or spaces. Takes an integer argument if you don't like 4 + endWithNewline() +} +java { + // don't need to set target, it is inferred from java + // Allow ignoring certain parts in formatting. + toggleOffOn() + // apply a specific flavor of google-java-format + googleJavaFormat('1.19.2').aosp().reflowLongStrings() + // fix formatting of type annotations + formatAnnotations() +} +} + +compileJava.dependsOn 'spotlessApply' + + dependencies { - // Use JUnit Jupiter for testing. - testImplementation libs.junit.jupiter + // Use JUnit Jupiter for testing. + testImplementation libs.junit.jupiter - testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - // This dependency is exported to consumers, that is to say found on their compile classpath. - api libs.commons.math3 + // This dependency is exported to consumers, that is to say found on their compile classpath. + api libs.commons.math3 - // This dependency is used internally, and not exposed to consumers on their own compile classpath. - implementation libs.guava + // This dependency is used internally, and not exposed to consumers on their own compile classpath. + implementation libs.guava } // Apply a specific Java toolchain to ease working on different environments. java { - toolchain { - languageVersion = JavaLanguageVersion.of(17) - } + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } } tasks.named('test') { - // Use JUnit Platform for unit tests. - useJUnitPlatform() + // Use JUnit Platform for unit tests. + useJUnitPlatform() } + +compileJava.dependsOn 'spotlessApply' diff --git a/math/build.gradle b/math/build.gradle new file mode 100644 index 0000000..646994e --- /dev/null +++ b/math/build.gradle @@ -0,0 +1,72 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * This generated file contains a sample Java library project to get you started. + * For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.7/userguide/building_java_projects.html in the Gradle documentation. + */ + +plugins { + // Apply the java-library plugin for API and implementation separation. + id 'java-library' + id "com.diffplug.spotless" version "6.24.0" + +} + +repositories { + // Use Maven Central for resolving dependencies. + mavenCentral() +} + +spotless { +// optional: limit format enforcement to just the files changed by this feature branch +ratchetFrom 'origin/main' + +format 'misc', { + // define the files to apply `misc` to + target '*.gradle', '.gitattributes', '.gitignore' + + // define the steps to apply to those files + trimTrailingWhitespace() + indentWithTabs() // or spaces. Takes an integer argument if you don't like 4 + endWithNewline() +} +java { + // don't need to set target, it is inferred from java + // Allow ignoring certain parts in formatting. + toggleOffOn() + // apply a specific flavor of google-java-format + googleJavaFormat('1.19.2').aosp().reflowLongStrings() + // fix formatting of type annotations + formatAnnotations() +} +} + +compileJava.dependsOn 'spotlessApply' + + +dependencies { + // Use JUnit Jupiter for testing. + testImplementation libs.junit.jupiter + + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + + // This dependency is exported to consumers, that is to say found on their compile classpath. + api libs.commons.math3 + + // This dependency is used internally, and not exposed to consumers on their own compile classpath. + implementation libs.guava +} + +// Apply a specific Java toolchain to ease working on different environments. +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + +tasks.named('test') { + // Use JUnit Platform for unit tests. + useJUnitPlatform() +} + +compileJava.dependsOn 'spotlessApply' diff --git a/math/src/main/java/InterpolateDouble.java b/math/src/main/java/InterpolateDouble.java new file mode 100644 index 0000000..cde5709 --- /dev/null +++ b/math/src/main/java/InterpolateDouble.java @@ -0,0 +1,87 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +public class InterpolateDouble { + private HashMap map; + private ArrayList sortedKeys; + + private final double minValue; + private final double maxValue; + + private final double minKey; + private final double maxKey; + + public InterpolateDouble(HashMap map) { + this(map, Double.MIN_VALUE, Double.MAX_VALUE); + } + + public InterpolateDouble(HashMap map, double minValue, double maxValue) { + this.map = map; + this.minValue = minValue; + this.maxValue = maxValue; + + sortedKeys = new ArrayList(); + for (Double k : map.keySet()) { + sortedKeys.add(k); + } + Collections.sort(sortedKeys); + + // Get lowest and highest keys of the HashMap + if (sortedKeys.size() > 0) { + minKey = sortedKeys.get(0); + maxKey = sortedKeys.get(sortedKeys.size() - 1); + } else { + throw new RuntimeException("Empty HashMap passed to InterpolateDouble"); + } + } + + /** + * Returns the interpolated value for the given key. If the key is not in the map, it will + * return the value for the closest key. + * + * @param key The key to interpolate + * @return The interpolated value + */ + public double getValue(double key) { + if (map.containsKey(key)) { + return map.get(key); + } + + // Ensure that key is within the bounds of the HashMap + if (key < minKey) { + return map.get(minKey); + } else if (key > maxKey) { + return map.get(maxKey); + } + + double lowerKey = 0; + double upperKey = 0; + for (double k : sortedKeys) { + if (k < key) { + lowerKey = k; + } else { + upperKey = k; + break; + } + } + + double lowerValue = map.get(lowerKey); + double upperValue = map.get(upperKey); + + // Edge case if keys equal each other + if (upperKey == lowerKey) { + upperKey += 0.01; + } + + double t = (key - lowerKey) / (upperKey - lowerKey); + double result = lowerValue * (1.0 - t) + t * upperValue; + if (result < minValue) { + return minValue; + } else if (result > maxValue) { + return maxValue; + } else { + return result; + } + } +} diff --git a/settings.gradle b/settings.gradle index 0d6bf5b..19ae3bf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -11,4 +11,4 @@ plugins { } rootProject.name = 'coppercore' -include('geometry', 'wpi_interface') +include('geometry', 'wpi_interface', 'math')