Skip to content

Commit

Permalink
Move logic specific to ps command into ps.c
Browse files Browse the repository at this point in the history
  • Loading branch information
starseeker committed Nov 26, 2024
1 parent d9e0032 commit 5af5ed5
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 262 deletions.
256 changes: 0 additions & 256 deletions src/libged/display_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -1704,262 +1704,6 @@ dl_png(struct bu_list *hdlp, mat_t model2view, fastf_t perspective, vect_t eye_p
}


static void
ps_draw_header(FILE *fp, char *font, char *title, char *creator, int linewidth, fastf_t scale, int xoffset, int yoffset)
{
fprintf(fp, "%%!PS-Adobe-1.0\n\
%%begin(plot)\n\
%%%%DocumentFonts: %s\n",
font);

fprintf(fp, "%%%%Title: %s\n", title);

fprintf(fp, "\
%%%%Creator: %s\n\
%%%%BoundingBox: 0 0 324 324 %% 4.5in square, for TeX\n\
%%%%EndComments\n\
\n",
creator);

fprintf(fp, "\
%d setlinewidth\n\
\n", linewidth);

fprintf(fp, "\
%% Sizes, made functions to avoid scaling if not needed\n\
/FntH /%s findfont 80 scalefont def\n\
/DFntL { /FntL /%s findfont 73.4 scalefont def } def\n\
/DFntM { /FntM /%s findfont 50.2 scalefont def } def\n\
/DFntS { /FntS /%s findfont 44 scalefont def } def\n\
\n", font, font, font, font);

fprintf(fp, "\
%% line styles\n\
/NV { [] 0 setdash } def %% normal vectors\n\
/DV { [8] 0 setdash } def %% dotted vectors\n\
/DDV { [8 8 32 8] 0 setdash } def %% dot-dash vectors\n\
/SDV { [32 8] 0 setdash } def %% short-dash vectors\n\
/LDV { [64 8] 0 setdash } def %% long-dash vectors\n\
\n\
/NEWPG {\n\
%d %d translate\n\
%f %f scale %% 0-4096 to 324 units (4.5 inches)\n\
} def\n\
\n\
FntH setfont\n\
NEWPG\n\
",
xoffset, yoffset, scale, scale);
}


static void
ps_draw_solid(fastf_t perspective, FILE *fp, struct bv_scene_obj *sp, matp_t psmat)
{
static vect_t last;
point_t clipmin = {-1.0, -1.0, -MAX_FASTF};
point_t clipmax = {1.0, 1.0, MAX_FASTF};
struct bv_vlist *tvp;
point_t *pt_prev=NULL;
fastf_t dist_prev=1.0;
fastf_t dist;
struct bv_vlist *vp = (struct bv_vlist *)&sp->s_vlist;
fastf_t delta;

fprintf(fp, "%f %f %f setrgbcolor\n",
PS_COLOR(sp->s_color[0]),
PS_COLOR(sp->s_color[1]),
PS_COLOR(sp->s_color[2]));

/* delta is used in clipping to insure clipped endpoint is slightly
* in front of eye plane (perspective mode only).
* This value is a SWAG that seems to work OK.
*/
delta = psmat[15]*0.0001;
if (delta < 0.0)
delta = -delta;
if (delta < SQRT_SMALL_FASTF)
delta = SQRT_SMALL_FASTF;

for (BU_LIST_FOR(tvp, bv_vlist, &vp->l)) {
size_t i;
size_t nused = tvp->nused;
int *cmd = tvp->cmd;
point_t *pt = tvp->pt;
for (i = 0; i < nused; i++, cmd++, pt++) {
static vect_t start, fin;
switch (*cmd) {
case BV_VLIST_POLY_START:
case BV_VLIST_POLY_VERTNORM:
case BV_VLIST_TRI_START:
case BV_VLIST_TRI_VERTNORM:
continue;
case BV_VLIST_POLY_MOVE:
case BV_VLIST_LINE_MOVE:
case BV_VLIST_TRI_MOVE:
/* Move, not draw */
if (perspective > 0) {
/* cannot apply perspective transformation to
* points behind eye plane!!!!
*/
dist = VDOT(*pt, &psmat[12]) + psmat[15];
if (dist <= 0.0) {
pt_prev = pt;
dist_prev = dist;
continue;
} else {
MAT4X3PNT(last, psmat, *pt);
dist_prev = dist;
pt_prev = pt;
}
} else
MAT4X3PNT(last, psmat, *pt);
continue;
case BV_VLIST_POLY_DRAW:
case BV_VLIST_POLY_END:
case BV_VLIST_LINE_DRAW:
case BV_VLIST_TRI_DRAW:
case BV_VLIST_TRI_END:
/* draw */
if (perspective > 0) {
/* cannot apply perspective transformation to
* points behind eye plane!!!!
*/
dist = VDOT(*pt, &psmat[12]) + psmat[15];
if (dist <= 0.0) {
if (dist_prev <= 0.0) {
/* nothing to plot */
dist_prev = dist;
pt_prev = pt;
continue;
} else {
if (pt_prev) {
fastf_t alpha;
vect_t diff;
point_t tmp_pt;

/* clip this end */
VSUB2(diff, *pt, *pt_prev);
alpha = (dist_prev - delta) / (dist_prev - dist);
VJOIN1(tmp_pt, *pt_prev, alpha, diff);
MAT4X3PNT(fin, psmat, tmp_pt);
}
}
} else {
if (dist_prev <= 0.0) {
if (pt_prev) {
fastf_t alpha;
vect_t diff;
point_t tmp_pt;

/* clip other end */
VSUB2(diff, *pt, *pt_prev);
alpha = (-dist_prev + delta) / (dist - dist_prev);
VJOIN1(tmp_pt, *pt_prev, alpha, diff);
MAT4X3PNT(last, psmat, tmp_pt);
MAT4X3PNT(fin, psmat, *pt);
}
} else {
MAT4X3PNT(fin, psmat, *pt);
}
}
} else
MAT4X3PNT(fin, psmat, *pt);
VMOVE(start, last);
VMOVE(last, fin);
break;
}

if (bg_ray_vclip(start, fin, clipmin, clipmax) == 0)
continue;

fprintf(fp,
"newpath %d %d moveto %d %d lineto stroke\n",
PS_COORD(start[0] * 2047),
PS_COORD(start[1] * 2047),
PS_COORD(fin[0] * 2047),
PS_COORD(fin[1] * 2047));
}
}
}


static void
ps_draw_body(struct bu_list *hdlp, FILE *fp, mat_t model2view, fastf_t perspective, vect_t eye_pos)
{
struct display_list *gdlp;
struct display_list *next_gdlp;
mat_t newmat;
matp_t mat;
mat_t perspective_mat;
struct bv_scene_obj *sp;

mat = model2view;

if (0 < perspective) {
point_t l, h;

VSET(l, -1.0, -1.0, -1.0);
VSET(h, 1.0, 1.0, 200.0);

if (ZERO(eye_pos[Z] - 1.0)) {
/* This way works, with reasonable Z-clipping */
persp_mat(perspective_mat, perspective,
(fastf_t)1.0f, (fastf_t)0.01f, (fastf_t)1.0e10f, (fastf_t)1.0f);
} else {
/* This way does not have reasonable Z-clipping,
* but includes shear, for GDurf's testing.
*/
deering_persp_mat(perspective_mat, l, h, eye_pos);
}

bn_mat_mul(newmat, perspective_mat, mat);
mat = newmat;
}

gdlp = BU_LIST_NEXT(display_list, hdlp);
while (BU_LIST_NOT_HEAD(gdlp, hdlp)) {
next_gdlp = BU_LIST_PNEXT(display_list, gdlp);

for (BU_LIST_FOR(sp, bv_scene_obj, &gdlp->dl_head_scene_obj)) {
ps_draw_solid(perspective, fp, sp, mat);
}

gdlp = next_gdlp;
}
}


static void
ps_draw_border(FILE *fp, float red, float green, float blue)
{
fprintf(fp, "%f %f %f setrgbcolor\n", red, green, blue);
fprintf(fp, "newpath 0 0 moveto 4096 0 lineto stroke\n");
fprintf(fp, "newpath 4096 0 moveto 4096 4096 lineto stroke\n");
fprintf(fp, "newpath 4096 4096 moveto 0 4096 lineto stroke\n");
fprintf(fp, "newpath 0 4096 moveto 0 0 lineto stroke\n");
}


static void
ps_draw_footer(FILE *fp)
{
fputs("% showpage % uncomment to use raw file\n", fp);
fputs("%end(plot)\n", fp);
}


void
dl_ps(struct bu_list *hdlp, FILE *fp, int border, char *font, char *title, char *creator, int linewidth, fastf_t scale, int xoffset, int yoffset, mat_t model2view, fastf_t perspective, vect_t eye_pos, float red, float green, float blue)
{
ps_draw_header(fp, font, title, creator, linewidth, scale, xoffset, yoffset);
if (border)
ps_draw_border(fp, red, green, blue);
ps_draw_body(hdlp, fp, model2view, perspective, eye_pos);
ps_draw_footer(fp);

}


void
dl_print_schain(struct bu_list *hdlp, struct db_i *dbip, int lvl, int vlcmds, struct bu_vls *vls)
Expand Down
5 changes: 0 additions & 5 deletions src/libged/ged_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,6 @@ GED_EXPORT int dl_how(struct bu_list *hdlp, struct bu_vls *vls, struct directory
GED_EXPORT void dl_plot(struct bu_list *hdlp, FILE *fp, mat_t model2view, int floating, mat_t center, fastf_t scale, int Three_D, int Z_clip);
GED_EXPORT void dl_png(struct bu_list *hdlp, mat_t model2view, fastf_t perspective, vect_t eye_pos, size_t size, size_t half_size, unsigned char **image);

#define PS_COORD(_x) ((int)((_x)+2048))
#define PS_COLOR(_c) ((_c)*(1.0/255.0))
GED_EXPORT void dl_ps(struct bu_list *hdlp, FILE *fp, int border, char *font, char *title, char *creator, int linewidth, fastf_t scale, int xoffset, int yoffset, mat_t model2view, fastf_t perspective, vect_t eye_pos, float red, float green, float blue);


GED_EXPORT void dl_print_schain(struct bu_list *hdlp, struct db_i *dbip, int lvl, int vlcmds, struct bu_vls *vls);

GED_EXPORT void dl_bitwise_and_fullpath(struct bu_list *hdlp, int flag);
Expand Down
Loading

0 comments on commit 5af5ed5

Please sign in to comment.