From 1d8f6202f7178a1fcc0793237e66451f3d88469b Mon Sep 17 00:00:00 2001 From: minhnguyenbhs Date: Mon, 26 Aug 2024 19:03:31 -0400 Subject: [PATCH] HELP i don't know what I'm doing --- geometry/build.gradle | 50 +++++++++++++ math/build.gradle | 91 +++++++++++++++++++++++ math/src/main/java/InterpolateDouble.java | 87 ++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 math/build.gradle create mode 100644 math/src/main/java/InterpolateDouble.java diff --git a/geometry/build.gradle b/geometry/build.gradle index fe89829..c2e5b53 100644 --- a/geometry/build.gradle +++ b/geometry/build.gradle @@ -8,6 +8,8 @@ plugins { // Apply the java-library plugin for API and implementation separation. id 'java-library' + id "com.diffplug.spotless" version "6.24.0" + } repositories { @@ -15,6 +17,33 @@ repositories { 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 @@ -39,3 +68,24 @@ tasks.named('test') { // Use JUnit Platform for unit tests. useJUnitPlatform() } + +task (commitOnDeploy, dependsOn: "spotlessApply") { + description = 'Commits when deployed.' + doLast { + if (true) { + def date = new Date() + def sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss") + grgit.add(patterns: [ + 'src/', + 'vendordeps/', + 'build.gradle', + 'logs', + 'gradle', + '.wpilib' + ]) + grgit.commit(message: "Automated commit at " + sdf.format(date), all: true) + } + } +} + +// compileJava.dependsOn commitOnDeploy \ No newline at end of file diff --git a/math/build.gradle b/math/build.gradle new file mode 100644 index 0000000..c2e5b53 --- /dev/null +++ b/math/build.gradle @@ -0,0 +1,91 @@ +/* + * 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() +} + +task (commitOnDeploy, dependsOn: "spotlessApply") { + description = 'Commits when deployed.' + doLast { + if (true) { + def date = new Date() + def sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss") + grgit.add(patterns: [ + 'src/', + 'vendordeps/', + 'build.gradle', + 'logs', + 'gradle', + '.wpilib' + ]) + grgit.commit(message: "Automated commit at " + sdf.format(date), all: true) + } + } +} + +// compileJava.dependsOn commitOnDeploy \ No newline at end of file diff --git a/math/src/main/java/InterpolateDouble.java b/math/src/main/java/InterpolateDouble.java new file mode 100644 index 0000000..cc2aa30 --- /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; + } + } +} \ No newline at end of file