作为一个示例问题,我试图按照链接的建议,使用数学程序找到给定起始四元数、结束四元数和特定 dt 的角速度。
import numpy as np
from pydrake.all import Quaternion
from pydrake.all import MathematicalProgram, Solve, eq, le, ge, SolverOptions, SnoptSolver
from pydrake.all import Quaternion_, AutoDiffXd
import pdb
epsilon = 1e-9
quaternion_epsilon = 1e-9
# Following the suggestion from:
# /sf/answers/4445709201/
def apply_angular_velocity_to_quaternion(q, w, t):
# This currently returns a runtime warning of division by zero
# https://github.com/RobotLocomotion/drake/issues/10451
norm_w = np.linalg.norm(w)
if norm_w <= epsilon:
return q
norm_q = np.linalg.norm(q)
if abs(norm_q - 1.0) > quaternion_epsilon:
print(f"WARNING: Quaternion …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个结构
struct Obj {
id: i32,
value: i64,
}
Run Code Online (Sandbox Code Playgroud)
可以用在 aBinaryHeap和 a 中HashSet。
当与 一起使用时BinaryHeap,我希望它基于 进行排序value。当与 一起使用时HashSet,我希望它与 一起进行哈希处理id。
这两个都需要该特征的实现Eq,但我找不到一种方法来拥有两种不同的Eq.
我发现的最接近的是Rust 中同一类型的相同特征的多个实现,但这似乎只有在该特征接受通用参数时才有效,但事实并非如此Eq
鉴于我的函数将函数指针作为参数
void call_func(std::function<void()>func) {
func();
}
Run Code Online (Sandbox Code Playgroud)
最直接的方式是类似的
void no_op() {
;
}
void call_func(std::function<void()>func = no_op) {
func();
}
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法来做到这一点,以便我可以避免创建字面上无用的no_op功能?
为什么该行t_ptr = std::unique_ptr<Test>(t_ptr.get());导致析构函数被调用?
这条线似乎只是天真地分配t_ptr给了自己...
此外,为什么在假定的破坏之后我还能继续调用方法?
class Test
{
public:
Test()
{
printf("Constructor called: %p\n", this);
i = 0;
};
void print()
{
printf("%d\n", i++);
};
~Test()
{
printf("Destructor called: %p\n", this);
};
private:
int i;
};
int main(int argc, char** argv)
{
std::unique_ptr<Test> t_ptr = std::unique_ptr<Test>(new Test());
t_ptr->print();
t_ptr->print();
t_ptr->print();
t_ptr = std::unique_ptr<Test>(t_ptr.get());
t_ptr->print();
t_ptr->print();
t_ptr->print();
};
Run Code Online (Sandbox Code Playgroud)
输出是
Constructor called: 0x55c9811a1e70
0
1
2
Destructor called: 0x55c9811a1e70
0
1
2
Destructor called: 0x55c9811a1e70
Run Code Online (Sandbox Code Playgroud) 假设我想每隔一段时间my_func()定期调用CALL_PERIOD
auto last_call_time = CHRONO_NEGATIVE_INFINITY;
while (true)
{
if (std::chrono::system_clock::now() - last_call_time > CALL_PERIOD)
{
last_call_time = std::chrono::system_clock::now();
my_func();
}
}
Run Code Online (Sandbox Code Playgroud)
什么是合适的,CHRONO_NEGATIVE_INFINITY这样该行
std::chrono::system_clock::now() - last_call_time > CALL_PERIOD
Run Code Online (Sandbox Code Playgroud)
总是会true在第一次运行时进行评估?
我已经尝试过time_point::min(),但似乎不起作用
Static1.hpp
#include <string>
class Static1
{
public:
static const std::string my_string;
};
Run Code Online (Sandbox Code Playgroud)
Static1.cpp
#include "Static1.hpp"
const std::string Static1::my_string = "aaa";
Run Code Online (Sandbox Code Playgroud)
Static2.hpp
#include <string>
class Static2
{
public:
static const std::string my_string;
};
Run Code Online (Sandbox Code Playgroud)
Static2.cpp
#include "Static2.hpp"
const std::string Static2::my_string = Static1::my_string;
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "Static2.hpp"
#include <iostream>
int main(argc int, char** argv)
{
cout << to_string(Static2::my_string == "aaa") << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我放入add_executable(printMyString main.cpp Static2.cpp Static1.cpp)我的CMakeLists.txt,我会得到
0
Run Code Online (Sandbox Code Playgroud)
虽然add_executable(printMyString main.cpp Static2.cpp Static1.cpp)给了我预期的行为
1
Run Code Online (Sandbox Code Playgroud)
为了使我的代码更容易维护(这样我就不需要跟踪我列出源文件的顺序),有什么办法可以确保我得到的行为在哪里Static2::my_string == "aaa"?