Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
Rename .cc files to .cpp (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Feb 19, 2024
1 parent 3d895cf commit 08bc6b6
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 394 deletions.
31 changes: 31 additions & 0 deletions node/.styleguide
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cppHeaderFileInclude {
\.h$
\.hpp$
\.inc$
}

cppSrcFileInclude {
\.cpp$
}

modifiableFileExclude {
jni/jni\.h$
jni/darwin/jni_md\.h$
jni/linux/jni_md\.h$
jni/win32/jni_md\.h$
\.jar$
}

licenseUpdateExclude {
java/src/main/java/org/team2363/util/DependencyExtractor\.java$
java/src/main/java/org/team2363/util/RuntimeDetector\.java$
java/src/main/java/org/team2363/util/RuntimeLoader\.java$
}

includeOtherLibs {
^Eigen/
^casadi/
^sleipnir/
^trajopt/
^napi\.h$
}
2 changes: 1 addition & 1 deletion node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ add_definitions(-DNAPI_VERSION=7)
include_directories(${CMAKE_JS_INC})

# Build a shared library named after the project from the files in `src/`
file(GLOB SOURCE_FILES "src/*.cc" "src/*.h")
file(GLOB SOURCE_FILES "src/*.cpp" "src/*.h")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})

# Gives our library file a .node extension without any "lib" prefix
Expand Down
5 changes: 4 additions & 1 deletion node/src/Addon.cc → node/src/Addon.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "SwervePathBuilderWrap.h"
// Copyright (c) TrajoptLib contributors

#include <napi.h>

#include "SwervePathBuilderWrap.h"

class TrajoptLibAddon : public Napi::Addon<TrajoptLibAddon> {
public:
explicit TrajoptLibAddon(Napi::Env env, Napi::Object exports) {
Expand Down
121 changes: 0 additions & 121 deletions node/src/SwervePathBuilderWrap.cc

This file was deleted.

148 changes: 148 additions & 0 deletions node/src/SwervePathBuilderWrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) TrajoptLib contributors

#include "SwervePathBuilderWrap.h"

#include <cmath>
#include <utility>
#include <vector>

#include <napi.h>
#include <trajopt/OptimalTrajectoryGenerator.h>
#include <trajopt/drivetrain/SwerveDrivetrain.h>
#include <trajopt/path/SwervePathBuilder.h>
#include <trajopt/solution/SwerveSolution.h>
#include <trajopt/trajectory/HolonomicTrajectory.h>

SwervePathBuilderWrap::SwervePathBuilderWrap(const Napi::CallbackInfo& info)
: ObjectWrap(info) {}

trajopt::SwerveDrivetrain _ToSwerveDrivetrain(
const Napi::Object& napiSwerveDrivetrain) {
double mass = napiSwerveDrivetrain.Get("mass").ToNumber().DoubleValue();
double moi = napiSwerveDrivetrain.Get("moi").ToNumber().DoubleValue();
Napi::Array napiSwerveModules =
napiSwerveDrivetrain.Get("modules").As<Napi::Array>();
std::vector<trajopt::SwerveModule> swerveModules;
swerveModules.reserve(napiSwerveModules.Length());
for (size_t idx = 0; idx < napiSwerveModules.Length(); ++idx) {
Napi::Object napiSwerveModule = napiSwerveModules.Get(idx).ToObject();
swerveModules.emplace_back(trajopt::SwerveModule{
napiSwerveModule.Get("x").ToNumber().DoubleValue(),
napiSwerveModule.Get("y").ToNumber().DoubleValue(),
napiSwerveModule.Get("wheelRadius").ToNumber().DoubleValue(),
napiSwerveModule.Get("wheelMaxAngularVelocity")
.ToNumber()
.DoubleValue(),
napiSwerveModule.Get("wheelMaxTorque").ToNumber().DoubleValue()});
}
return trajopt::SwerveDrivetrain{mass, moi, std::move(swerveModules)};
}

Napi::Value SwervePathBuilderWrap::SetDrivetrain(
const Napi::CallbackInfo& info) {
if (info.Length() != 1 || !info[0].IsObject()) {
Napi::TypeError::New(info.Env(),
"SwervePathBuilder.setDrivetrain() accepts exactly "
"one object parameter.")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
auto swerveDrivetrain = _ToSwerveDrivetrain(info[0].ToObject());
_path.SetDrivetrain(swerveDrivetrain);

return info.Env().Undefined();
}

Napi::Value SwervePathBuilderWrap::PoseWpt(const Napi::CallbackInfo& info) {
if (info.Length() != 4 || !info[0].IsNumber() || !info[1].IsNumber() ||
!info[2].IsNumber() || !info[3].IsNumber()) {
Napi::TypeError::New(
info.Env(),
"SwervePathBuilder.poseWpt() accepts exactly four number parameters")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
uint32_t idx = info[0].ToNumber().Uint32Value();
double x = info[1].ToNumber().DoubleValue();
double y = info[2].ToNumber().DoubleValue();
double heading = info[3].ToNumber().DoubleValue();
_path.PoseWpt(idx, x, y, heading);

return info.Env().Undefined();
}

Napi::Value SwervePathBuilderWrap::WptZeroVelocity(
const Napi::CallbackInfo& info) {
if (info.Length() != 1 || !info[0].IsNumber()) {
Napi::TypeError::New(info.Env(),
"SwervePathBuilder.wptZeroVelocity() accepts exactly "
"one number parameter")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
uint32_t idx = info[0].ToNumber().Uint32Value();
_path.WptZeroVelocity(idx);

return info.Env().Undefined();
}

Napi::Value SwervePathBuilderWrap::WptZeroAngularVelocity(
const Napi::CallbackInfo& info) {
if (info.Length() != 1 || !info[0].IsNumber()) {
Napi::TypeError::New(info.Env(),
"SwervePathBuilder.wptZeroAngularVelocity() accepts "
"exactly one number parameter")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
uint32_t idx = info[0].ToNumber().Uint32Value();
_path.WptZeroAngularVelocity(idx);

return info.Env().Undefined();
}

Napi::Value SwervePathBuilderWrap::Generate(const Napi::CallbackInfo& info) {
if (info.Length() != 0) {
Napi::TypeError::New(
info.Env(),
"SwervePathBuilder.generate() accepts exactly one number parameter")
.ThrowAsJavaScriptException();
return info.Env().Undefined();
}
trajopt::SwerveSolution solution =
trajopt::OptimalTrajectoryGenerator::Generate(_path);
trajopt::HolonomicTrajectory trajectory{solution};
auto napiSamples = Napi::Array::New(info.Env());
size_t sampCnt = trajectory.samples.size();
for (size_t idx = 0; idx < sampCnt; ++idx) {
Napi::Object napiSample = Napi::Object::New(info.Env());
napiSample.Set("timestamp", trajectory.samples.at(idx).timestamp);
napiSample.Set("x", trajectory.samples.at(idx).x);
napiSample.Set("y", trajectory.samples.at(idx).y);
napiSample.Set("heading", trajectory.samples.at(idx).heading);
napiSample.Set("velocityX", trajectory.samples.at(idx).velocityX);
napiSample.Set("velocityY", trajectory.samples.at(idx).velocityY);
napiSample.Set("angularVelocity",
trajectory.samples.at(idx).angularVelocity);
napiSamples.Set(idx, napiSample);
}
return napiSamples;
}

Napi::Function SwervePathBuilderWrap::GetClass(const Napi::Env env) {
return DefineClass(
env, "SwervePathBuilder",
{
SwervePathBuilderWrap::InstanceMethod(
"setDrivetrain", &SwervePathBuilderWrap::SetDrivetrain),
SwervePathBuilderWrap::InstanceMethod(
"poseWpt", &SwervePathBuilderWrap::PoseWpt),
SwervePathBuilderWrap::InstanceMethod(
"wptZeroVelocity", &SwervePathBuilderWrap::WptZeroVelocity),
SwervePathBuilderWrap::InstanceMethod(
"wptZeroAngularVelocity",
&SwervePathBuilderWrap::WptZeroAngularVelocity),
SwervePathBuilderWrap::InstanceMethod(
"generate", &SwervePathBuilderWrap::Generate),
});
}
30 changes: 30 additions & 0 deletions rust/.styleguide
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cppHeaderFileInclude {
\.h$
\.hpp$
\.inc$
}

cppSrcFileInclude {
\.cpp$
}

modifiableFileExclude {
jni/jni\.h$
jni/darwin/jni_md\.h$
jni/linux/jni_md\.h$
jni/win32/jni_md\.h$
\.jar$
}

licenseUpdateExclude {
java/src/main/java/org/team2363/util/DependencyExtractor\.java$
java/src/main/java/org/team2363/util/RuntimeDetector\.java$
java/src/main/java/org/team2363/util/RuntimeLoader\.java$
}

includeOtherLibs {
^Eigen/
^casadi/
^sleipnir/
^trajopt/
}
4 changes: 2 additions & 2 deletions rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ fn main() -> miette::Result<()> {
}

cxx_build::bridge("src/lib.rs") // returns a cc::Build
.file("src/trajoptlib.cc")
.file("src/trajoptlib.cpp")
.include("include")
.include(format!("{}/include", dst.display()))
.flag_if_supported("/std:c++20")
.flag_if_supported("-std=c++20")
.compile("trajoptlib-rust");

println!("cargo:rerun-if-changed=include/trajoptlib.h");
println!("cargo:rerun-if-changed=src/trajoptlib.cc");
println!("cargo:rerun-if-changed=src/trajoptlib.cpp");
println!("cargo:rerun-if-changed=src/lib.rs");
Ok(())
}
3 changes: 2 additions & 1 deletion rust/include/trajoptlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#pragma once

#include <rust/cxx.h>
#include <trajopt/path/SwervePathBuilder.h>

#include <cstddef>
#include <memory>

#include <trajopt/path/SwervePathBuilder.h>

namespace trajoptlibrust {

struct HolonomicTrajectory;
Expand Down
Loading

0 comments on commit 08bc6b6

Please sign in to comment.