gpt4 book ai didi

c++ - linux/MSVC 2012 中的管道不稳定行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:28:25 25 4
gpt4 key购买 nike

我有一个基本上跟踪坐标的粒子系统。到目前为止,我一直在输出文件中的所有坐标,然后使用 gnuplot 脚本绘制它们并使用 ffmpeg 从 PNG 中创建视频。为了跳过所有文件 I/O,我首先尝试使用 OpenGL 但无济于事,所以我考虑设置一个到 gnuplot 的管道。

我在 MSVC 2012 中这样做:

FILE *gnuplotpipe = _popen(GNUPLOT_NAME, "w");
setvbuf(gnuplotpipe, NULL, _IONBF, NULL);
//setbuf(gnuplotpipe, NULL);
fprintf(gnuplotpipe,"set term png\n");
fprintf(gnuplotpipe,"set size 720, 480\n");
fprintf(gnuplotpipe,"set size ratio -1\n");
fprintf(gnuplotpipe,"unset key\n");
fprintf(gnuplotpipe,"set xrange [-%d:%d]\n", RANGE+100,RANGE+100);
fprintf(gnuplotpipe,"set yrange [-%d:%d]\n", RANGE+100,RANGE+100);
for(time-steps)
//do stuff
fprintf(gnuplotpipe,"plot '-'\n");
for(all particles)
// calculate coordinates
fprintf(gnuplotpipe,"%f %f\n", particle[i]->x, particle[i]->y);
end for
fprintf(gnuplotpipe, "e\n");
//fflush(gnuplotpipe);
end for
_pcloce(gnuplotpipe);

因此在 Linux 中使用 gnuplot -persist 而不是 pgnuplot -persistpopenpclose,以及setbuf 而不是 setvbuf。这已经经过大量试验和错误。

在 Linux 中,它非常一致。我可以在控制台中看到每个时间步的 plot '-' 命令,然后是难以理解的文本。什么都没有打开。在 Windows 中,它是惊人的,因为有时它会工作,我可以在 gnuplot“MS Windows”窗口上看到数据流(我的意思是不是控制台输出,这本身就很棒,因为我没有使用 wgnuplot_pipes.exe)。什么都没有绘制。窗口关闭。其他时候它会绘制几个时间步长,但每个时间步长在不同的窗口中。有时,它会崩溃或它会工作但发送无法理解的文本。

我大部分时间都在处理 100 个粒子。我可以有多达 10,000 个时间步长。我不知道这是不是很多。尽管我将缓冲区设置为 NULL,但我怀疑 Windows 中存在中断或其他问题。我在 VirtualBox 中的 Win7 x64 中的 MSVC 和 Linux Mint x64 中的 Qt 中工作。我正在使用 gnuplot 4.7 (win) 和 4.6.0.8 (linux)。我在 SO 中进行了相当广泛的研究,我发现的唯一问题是 -persist 在 Windows 中并没有真正持久化。任何帮助将不胜感激。

最佳答案

只是为了让问题没有得到解答..这是你的程序的 python 版本,使用 x11 终端。它会打开一个带有愚蠢动画的窗口。

不过请注意,如果其他一些驱动程序尝试为每一帧打开一个窗口,我一点也不会感到惊讶

import subprocess
f=subprocess.Popen(['/usr/bin/gnuplot','-persist'],
stdin=subprocess.PIPE,shell=False)
f.stdin.write('set term x11 \n')
f.stdin.write('set yrange [-2:2]\n')
import math
i=0
d=1
count=0
while count<1000 :
count+=1
if abs(i)==50 : d *= -1
i+=d
f.stdin.write('plot \'-\' with linespoints pt 4 ps 2 \n')
for j in range(0,500):
f.stdin.write('%f %f\n'%(float(j),math.sin(float(j)/float(abs(i)+1))))
f.stdin.write('e\n')

关于c++ - linux/MSVC 2012 中的管道不稳定行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15645628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com