generated from wpilibsuite/vendor-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18aeea1
commit 1d8f620
Showing
3 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
|
||
public class InterpolateDouble { | ||
private HashMap<Double, Double> map; | ||
private ArrayList<Double> sortedKeys; | ||
|
||
private final double minValue; | ||
private final double maxValue; | ||
|
||
private final double minKey; | ||
private final double maxKey; | ||
|
||
public InterpolateDouble(HashMap<Double, Double> map) { | ||
this(map, Double.MIN_VALUE, Double.MAX_VALUE); | ||
} | ||
|
||
public InterpolateDouble(HashMap<Double, Double> map, double minValue, double maxValue) { | ||
this.map = map; | ||
this.minValue = minValue; | ||
this.maxValue = maxValue; | ||
|
||
sortedKeys = new ArrayList<Double>(); | ||
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; | ||
} | ||
} | ||
} |