我获得了使用数据的x,y,z坐标生成2D图像的代码,我想修改此代码以创建3D图像。 (代码来源:https://physiology.arizona.edu/people/secomb/netflow)
下面的代码创建2D图像, 使用数据中的XYZ坐标,我想修改代码以创建3D图像
例如: 转动此https://www.researchgate.net/figure/A-2D-meshed-model-of-the-flow-network-in-GAMBIT_fig12_266171604
这是C ++代码,我在Visual Studio 2019中使用它
/**********************************************************
picturenetwork.cpp - project network on z = 0 plane
Labels nodes with nodvar and segments with segvar (must be float)
Colors segments according to segvar
Generates a postscript file
Version for NetFlowV1, TWS Oct. 2012
***********************************************************/
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "nrutil.h"
void picturenetwork(float *nodvar, float *segvar, const char fname[])
{
extern int nseg,nnod;
extern int *segtyp,*ista,*iend;
extern float *diam,**cnode;
int k,iseg,inod,ilevel,nlevel = 100;
float xmin,xmax,ymin,ymax,xs,ys,zs,picfac,red,green,blue,xz,xzmin,xzmax, zzmin, zzmax;
float diamfac = 1.,zcoord,zbottom,ztop,zmin,zmax;
FILE *ofp;
//Determine range of x,y,z values
xmin = 1.e6;
xmax = -1.e6;
ymin = 1.e6;
ymax = -1.e6;
zmin = 1.e6;
zmax = -1.e6;
for(inod=1; inod<=nnod; inod++){
xmin = FMIN(xmin,cnode[1][inod]);
xmax = FMAX(xmax,cnode[1][inod]);
ymin = FMIN(ymin,cnode[2][inod]);
ymax = FMAX(ymax,cnode[2][inod]);
zmin = FMIN(zmin,cnode[3][inod]);
zmax = FMAX(zmax,cnode[3][inod]);
}
zmin -= 1.; //make sure everything is included
zmax += 1.;
picfac = FMIN(500./(xmax - xmin),700./(ymax - ymin));
ofp = fopen(fname, "w");
fprintf(ofp, "%%!PS-Adobe-2.0\n");
fprintf(ofp, "%%%%Pages: 1\n");
fprintf(ofp, "%%%%EndComments\n");
fprintf(ofp, "%%%%Page: 1 1\n");
fprintf(ofp, "/mx {%g sub %g mul 50 add} def\n",xmin,picfac);
fprintf(ofp, "/my {%g sub %g mul 50 add} def\n",ymin,picfac);
fprintf(ofp, "/cf {closepath fill} def\n");
fprintf(ofp, "/cs {closepath stroke} def\n");
fprintf(ofp, "/m {moveto} def\n");
fprintf(ofp, "/n {newpath} def\n");
fprintf(ofp, "/l {lineto} def\n");
fprintf(ofp, "/sl {setlinewidth} def\n");
fprintf(ofp, "/sc {setrgbcolor} def\n");
fprintf(ofp, "/s {stroke} def\n");
fprintf(ofp, "/Times-Roman findfont\n");
fprintf(ofp, "8 scalefont\n");
fprintf(ofp, "setfont\n");
fprintf(ofp, "newpath\n");
fprintf(ofp, "%g mx %g my m\n",xmin,ymin);
fprintf(ofp, "%g mx %g my l\n",xmax,ymin);
fprintf(ofp, "%g mx %g my l\n",xmax,ymax);
fprintf(ofp, "%g mx %g my l\n",xmin,ymax);
fprintf(ofp, "closepath\n");
fprintf(ofp, "stroke\n");
fprintf(ofp, "/Times-Roman findfont\n");
fprintf(ofp, "4 scalefont\n");
fprintf(ofp, "setfont\n");
//plot vessels colored according to segvar, in order from bottom to top according to z-coordinate
xzmin = 1.e6;
xzmax = -1.e6;
for(iseg=1; iseg<=nseg; iseg++) if(segtyp[iseg] == 4 || segtyp[iseg] == 5){
xzmin = FMIN(xzmin,segvar[iseg]);
xzmax = FMAX(xzmax,segvar[iseg]);
}
for(ilevel=1; ilevel<=nlevel; ilevel++){
zbottom = zmin + (ilevel-1)*(zmax - zmin)/nlevel;
ztop = zmin + ilevel*(zmax - zmin)/nlevel;
for(iseg=1; iseg<=nseg; iseg++) if(segtyp[iseg] == 4 || segtyp[iseg] == 5){
zcoord = (cnode[3][ista[iseg]] + cnode[3][iend[iseg]])/2.;
if(zcoord >= zbottom && zcoord < ztop){
if(xzmin != xzmax) xz = (segvar[iseg] - xzmin)/(xzmax - xzmin);
else xz = 0.75;
blue = FMIN(FMAX(1.5-4.*fabs(xz-0.25), 0.), 1.);//Set up colors using Matlab 'jet' scheme
green= FMIN(FMAX(1.5-4.*fabs(xz-0.5), 0.), 1.);
red = FMIN(FMAX(1.5-4.*fabs(xz-0.75), 0.), 1.);
fprintf(ofp,"%f %f %f sc\n",red,green,blue);
fprintf(ofp,"%g sl\n",picfac*diam[iseg]*diamfac);//line widths scaled up by diamfac
fprintf(ofp, "%g mx %g my m ", cnode[1][ista[iseg]],cnode[2][ista[iseg]],cnode[3][ista[iseg]]);
fprintf(ofp, "%g mx %g my l s \n", cnode[1][iend[iseg]],cnode[2][iend[iseg]],cnode[3][iend[iseg]]);
}
}
}
创建的图像是.ps文件,到目前为止没有错误。 主要问题是生成的图像是2D而非3D 。图像是各种颜色的容器。
答案 0 :(得分:0)
使用此代码无法创建3D模型。此代码输出的是不支持3D图形的.ps文件。