BRL-CAD
sh_points.c
Go to the documentation of this file.
1 /* S H _ P O I N T S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1989-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file liboptical/sh_points.c
21  *
22  * Reads a file of u, v point locations and associated RGB color
23  * values. For each u, v texture mapping cell, this routine fills in
24  * the color of the "brightest" point contained in that cell (if any).
25  *
26  * This routine was born in order to environment map the Yale Bright
27  * Star catalog data without under or over sampling the point sources.
28  * It was soon realized that making it "star" specific limited its
29  * usefulness.
30  *
31  */
32 
33 #include "common.h"
34 
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <math.h>
39 
40 #include "vmath.h"
41 #include "raytrace.h"
42 #include "fb.h"
43 #include "spm.h"
44 #include "optical.h"
45 
46 
47 #define PT_NAME_LEN 128
49  char pt_file[PT_NAME_LEN]; /* Filename */
50  int pt_size; /* number of bins around equator */
51  bn_spm_map_t *pt_map; /* stuff */
52 };
53 #define POINTS_NULL ((struct points_specific *)0)
54 #define POINTS_O(m) bu_offsetof(struct points_specific, m)
55 
57  {"%s", PT_NAME_LEN, "file", POINTS_O(pt_file), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
58  {"%d", 1, "size", POINTS_O(pt_size), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
59  {"%d", 1, "w", POINTS_O(pt_size), BU_STRUCTPARSE_FUNC_NULL, NULL, NULL },
60  {"", 0, (char *)0, 0, BU_STRUCTPARSE_FUNC_NULL, NULL, NULL }
61 };
62 
63 
64 HIDDEN int points_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip);
65 HIDDEN int points_render(struct application *ap, const struct partition *partp, struct shadework *swp, void *dp);
66 HIDDEN void points_print(register struct region *rp, void *dp);
67 HIDDEN void points_mfree(void *cp);
68 
69 struct mfuncs points_mfuncs[] = {
70  {MF_MAGIC, "points", 0, MFI_UV, 0, points_setup, points_render, points_print, points_mfree },
71  {0, (char *)0, 0, 0, 0, 0, 0, 0, 0 }
72 };
73 
74 
75 struct points {
76  fastf_t u; /* u location */
77  fastf_t v; /* v location */
78  vect_t color; /* color of point */
79  struct points *next; /* next point in list */
80 };
81 
82 
83 /*
84  * Returns -
85  * <0 failed
86  * >0 success
87  */
88 HIDDEN int
89 points_setup(register struct region *UNUSED(rp), struct bu_vls *matparm, void **dpp, const struct mfuncs *UNUSED(mfp), struct rt_i *UNUSED(rtip))
90 
91 
92 /* New since 4.4 release */
93 {
94  register struct points_specific *ptp;
95  char buf[513];
96  FILE *fp;
97 
98  BU_CK_VLS(matparm);
99  BU_GET(ptp, struct points_specific);
100  *dpp = ptp;
101 
102  /* get or default shader parameters */
103  ptp->pt_file[0] = '\0';
104  ptp->pt_size = -1;
105  if (bu_struct_parse(matparm, points_parse, (char *)ptp, NULL) < 0) {
106  BU_PUT(ptp, struct points_specific);
107  return -1;
108  }
109  if (ptp->pt_size < 0)
110  ptp->pt_size = 512;
111  if (ptp->pt_file[0] == '\0')
112  bu_strlcpy(ptp->pt_file, "points.ascii", sizeof(ptp->pt_file));
113 
114  /* create a spherical data structure to bin point lists into */
115  if ((ptp->pt_map = bn_spm_init(ptp->pt_size, sizeof(struct points))) == BN_SPM_MAP_NULL)
116  goto fail;
117 
118  /* read in the data */
119  if ((fp = fopen(ptp->pt_file, "rb")) == NULL) {
120  bu_log("points_setup: can't open \"%s\"\n", ptp->pt_file);
121  goto fail;
122  }
123  while (bu_fgets(buf, 512, fp) != NULL) {
124  double u, v, mag;
125  struct points *headp, *pp;
126 
127  if (buf[0] == '#')
128  continue; /* comment */
129 
130  BU_ALLOC(pp, struct points);
131  sscanf(buf, "%lf%lf%lf", &u, &v, &mag);
132  pp->u = u;
133  pp->v = v;
134  pp->color[0] = mag;
135  pp->color[1] = mag;
136  pp->color[2] = mag;
137 
138  /* find a home for it */
139  headp = (struct points *)bn_spm_get(ptp->pt_map, u, v);
140  pp->next = headp->next;
141  headp->next = pp;
142  }
143  (void)fclose(fp);
144 
145  return 1;
146 fail:
147  BU_PUT(ptp, struct points_specific);
148  return -1;
149 }
150 
151 
152 /*
153  * Given a u, v coordinate within the texture (0 <= u, v <= 1.0),
154  * and a "size" of the pixel being rendered (du, dv), fill in the
155  * color of the "brightest" point (if any) within that region.
156  */
157 HIDDEN int
158 points_render(struct application *ap, const struct partition *UNUSED(partp), struct shadework *swp, void *dp)
159 {
160  register struct points_specific *ptp =
161  (struct points_specific *)dp;
162  register bn_spm_map_t *mapp;
163  fastf_t umin, umax, vmin, vmax;
164  int xmin, xmax, ymin, ymax;
165  register int x, y;
166  register struct points *pp;
167  fastf_t mag;
168 
169  swp->sw_uv.uv_du = ap->a_diverge;
170  swp->sw_uv.uv_dv = ap->a_diverge;
171  /*bu_log("du, dv = %g %g\n", swp->sw_uv.uv_du, swp->sw_uv.uv_dv);*/
172 
173  /* compute and clip bounds in u, v space */
174  umin = swp->sw_uv.uv_u - swp->sw_uv.uv_du;
175  umax = swp->sw_uv.uv_u + swp->sw_uv.uv_du;
176  vmin = swp->sw_uv.uv_v - swp->sw_uv.uv_dv;
177  vmax = swp->sw_uv.uv_v + swp->sw_uv.uv_dv;
178  if (umin < 0) umin = 0;
179  if (vmin < 0) vmin = 0;
180  if (umax > 1) umax = 1;
181  if (vmax > 1) vmax = 1;
182 
183  mapp = ptp->pt_map;
184 
185  mag = 0;
186  ymin = vmin * mapp->ny;
187  ymax = vmax * mapp->ny;
188  /* for each latitude band */
189  for (y = ymin; y < ymax; y++) {
190  xmin = umin * mapp->nx[y];
191  xmax = umax * mapp->nx[y];
192  /* for each bin spanned in that band */
193  for (x = xmin; x < xmax; x++) {
194  pp = (struct points *)&(mapp->xbin[y][x*mapp->elsize]);
195  while (pp != NULL) {
196  if (pp->u < umax && pp->u >= umin
197  && pp->v < vmax && pp->v >= vmin
198  && pp->color[0] > mag) {
199  mag = pp->color[0];
200  }
201  pp = pp->next;
202  }
203  }
204  }
205 
206  /*bu_log("points_render ([%g %g][%g %g]) = %g\n",
207  umin, umax, vmin, vmax, mag);*/
208 
209  if (ZERO(mag)) {
210  VSET(swp->sw_color, 0, 0, 0);
211  } else {
212  VSET(swp->sw_color, mag/255.0, mag/255.0, mag/255.0);
213  }
214 
215  return 1;
216 }
217 
218 
219 HIDDEN void
220 points_print(register struct region *UNUSED(rp), void *dp)
221 {
222  bu_struct_print("points_setup", points_parse, (char *)dp);
223  /* Should be more here */
224 }
225 
226 
227 HIDDEN void
228 points_mfree(void *cp)
229 {
230  /* XXX - free linked lists in every bin! */
231  bn_spm_free((bn_spm_map_t *)cp);
232 }
233 
234 
235 /*
236  * Local Variables:
237  * mode: C
238  * tab-width: 8
239  * indent-tabs-mode: t
240  * c-file-style: "stroustrup"
241  * End:
242  * ex: shiftwidth=4 tabstop=8
243  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
char pt_file[PT_NAME_LEN]
Definition: sh_points.c:49
fastf_t v
Definition: sh_points.c:77
char * bn_spm_get(register bn_spm_map_t *mapp, double u, double v)
Definition: sphmap.c:153
#define MF_MAGIC
Definition: magic.h:205
HIDDEN int points_render(struct application *ap, const struct partition *partp, struct shadework *swp, void *dp)
Definition: sh_points.c:158
#define VSET(a, b, c, d)
Definition: color.c:53
HIDDEN void points_mfree(void *cp)
Definition: sh_points.c:228
Header file for the BRL-CAD common definitions.
#define HIDDEN
Definition: common.h:86
void bu_struct_print(const char *title, const struct bu_structparse *parsetab, const char *base)
Definition: parse.c:1221
HIDDEN int points_setup(register struct region *rp, struct bu_vls *matparm, void **dpp, const struct mfuncs *mfp, struct rt_i *rtip)
Definition: sh_points.c:89
#define BU_CK_VLS(_vp)
Definition: vls.h:69
struct mfuncs points_mfuncs[]
Definition: sh_points.c:69
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
HIDDEN void points_print(register struct region *rp, void *dp)
Definition: sh_points.c:220
fastf_t a_diverge
slope of beam divergence/mm
Definition: raytrace.h:1600
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
bn_spm_map_t * pt_map
Definition: sh_points.c:51
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
#define UNUSED(parameter)
Definition: common.h:239
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
#define BU_STRUCTPARSE_FUNC_NULL
Definition: parse.h:153
#define PT_NAME_LEN
Definition: sh_points.c:47
struct bu_structparse points_parse[]
Definition: sh_points.c:56
#define ZERO(val)
Definition: units.c:38
bn_spm_map_t * bn_spm_init(int N, int elsize)
Definition: sphmap.c:62
int bu_struct_parse(const struct bu_vls *in_vls, const struct bu_structparse *desc, const char *base, void *data)
Definition: parse.c:878
char * bu_fgets(char *s, int size, FILE *stream)
Definition: fgets.c:31
void bn_spm_free(bn_spm_map_t *mp)
Definition: sphmap.c:36
struct points * next
Definition: sh_points.c:79
Definition: vls.h:56
vect_t color
Definition: sh_points.c:78
double fastf_t
Definition: defines.h:300
Header file for the BRL-CAD Optical Library, LIBOPTICAL.
#define POINTS_O(m)
Definition: sh_points.c:54
fastf_t u
Definition: sh_points.c:76