我必须用three.js 创建一个小型太阳系(1 个恒星、2 个行星、1 个卫星绕行星运行) 这是我第一次用three.js(以及一般的JavaScript)编程,所以我是一个完全的新手。
我设法创建了我的静态系统,这样我就有了太阳、火星、火卫一、地球和月球。
现在我不知道如何让行星绕太阳运行,卫星绕行星运行。
这是我到目前为止所做的
//global variables declaration
function init(){
/*starting code: cameras, declaring variables and so on*/
function celestialBodiesSetup(){
var geometry,material,image;
var celestialBodies=[sun,mars,phobos,earth,moon];
//sun, mars etc are declared as global variables before the init function
for(var i=0;i<celestialBodies.length;i++){
switch (celestialBodies[i]){
case sun:
material=new THREE.MeshPhongMaterial();
geometry =new THREE.SphereGeometry(35,32,32);
image="mysun.png";
sun=createSphere(geometry,material,image);
sun.position.set(0,0,20);
var pointLight = new THREE.PointLight( 0xFDFDFD, 2, 1800,70 );
sun.add(pointLight);
break;
case mars:
material=new THREE.MeshPhongMaterial();
geometry =new THREE.SphereGeometry(17,32,32);
image="mars.jpg";
mars=createSphere(geometry,material,image,sun);
mars.position.set(150,-15,0);
break;
/*repeat process for the …Run Code Online (Sandbox Code Playgroud) 我在初始化全局变量时遇到了麻烦。我的 C++ 有点生疏,所以我不记得我的代码不起作用的原因。
文件.cpp
const char * write_path = &(std::string(getenv("FIFO_PATH")) + "/pythonread_fifo")[0];
int main(int argc, char**argv)
{
std::cout << "printing a string: ";
std::cout << (std::string(getenv("FIFO_PATH")) + "/pythonread_fifo\n");
std::cout << "printing a const char*: ";
std::cout << &(std::string(getenv("FIFO_PATH")) + "/pythonread_fifo")[0] << std::endl;
std::cout << "printing write_path:";
std::cout << write_path;
std::cout << write_path << std::endl;
std::cout << "printing FIFO_PATH:" << std::string(getenv("FIFO_PATH"));
}
Run Code Online (Sandbox Code Playgroud)
作为前提:FIFO_PATH 已正确添加到 bashrc,并且它可以工作,但是,当我启动此程序时,这是输出:
printing a string: /home/Processes/FIFOs/pythonread_fifo
printing a const char*: /home/Processes/FIFOs/pythonread_fifo
printing write_path:
printing FIFO_PATH:/home/Processes/FIFOs
Run Code Online (Sandbox Code Playgroud)
如您所见write_path,完全是空的。 …
我需要我的对象“启动器”来检测其关联的进程是否正在运行。我最初的解决方案是简单地使用 psutil 运行 for 循环
def Launcher(self, processName):
self.processName=processName
def process_up(self, attempts=0):
if attempts <= 3:
try:
if self.processName in (p.name() for p in psutil.process_iter()):
return True
else:
return False
except:
self.process_up(attempts=1)
else:
logging.error("Psutil Fatal Error. Unable to check status of process {}".format(self.processName))
return False
Run Code Online (Sandbox Code Playgroud)
递归用于极少数情况,即在 for 循环中检测到进程 p 但在调用 .name() 之前死亡。
不管怎样,这一切听起来都很好,直到我用我的所有进程(大约 40 个进程,所以 40 个启动器正在运行)对其进行测试,问题是运行这个循环大约需要 0.1 秒,这意味着总共〜4秒。
但是,我需要瞄准 <1 秒。还有哪些其他超级快速的方法来查找给定进程是否正在运行?我不需要知道有关该进程的任何信息(我不关心它的 pid 或名称),只要它是否启动即可。
附带说明:我不能使用多线程或任何类型的并行性。我必须按顺序运行这些启动器。
编辑:我也尝试了以下代码,这绝对是更好的性能:
def process_up(self):
try:
call = subprocess.check_output("pgrep -f '{}'".format(self.processName), shell=True)
return True
except subprocess.CalledProcessError:
return …Run Code Online (Sandbox Code Playgroud)