BRL-CAD
hist.h
Go to the documentation of this file.
1/* H I S T . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 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
21#ifndef BU_HIST_H
22#define BU_HIST_H
23
24#include "common.h"
25
26#include <stddef.h> /* for size_t */
27#include "vmath.h"
28
29#include "bu/defines.h"
30#include "bu/magic.h"
31
32__BEGIN_DECLS
33
34/** @addtogroup bu_hist
35 *
36 * @brief
37 * General purpose histogram handling routines.
38 *
39 * The subroutine bu_hist_range() is used to record items that may
40 * extend across multiple "bin"s.
41 *
42 */
43/** @{ */
44/** @file bu/hist.h */
45
46/**
47 * histogram support
48 */
49struct bu_hist {
50 uint32_t magic; /**< magic # for id/check */
51 fastf_t hg_min; /**< minimum value */
52 fastf_t hg_max; /**< maximum value */
53 fastf_t hg_clumpsize; /**< (max-min+1)/nbins+1 */
54 size_t hg_nsamples; /**< total number of samples spread into histogram */
55 size_t hg_nbins; /**< # of bins in hg_bins[] */
56 long *hg_bins; /**< array of counters */
57};
58typedef struct bu_hist bu_hist_t;
59#define BU_HIST_NULL ((struct bu_hist *)0)
60
61/**
62 * assert the integrity of a bu_hist struct.
63 */
64#define BU_CK_HIST(_p) BU_CKMAG(_p, BU_HIST_MAGIC, "struct bu_hist")
65
66/**
67 * initialize a bu_hist struct without allocating any memory.
68 */
69#define BU_HIST_INIT(_hp) { \
70 (_hp)->magic = BU_HIST_MAGIC; \
71 (_hp)->hg_min = (_hp)->hg_max = (_hp)->hg_clumpsize = 0.0; \
72 (_hp)->hg_nsamples = (_hp)->hg_nbins = 0; \
73 (_hp)->hg_bins = NULL; \
74 }
75
76/**
77 * macro suitable for declaration statement initialization of a
78 * bu_hist struct. does not allocate memory.
79 */
80#define BU_HIST_INIT_ZERO {BU_HIST_MAGIC, 0.0, 0.0, 0.0, 0, 0, NULL}
81
82/**
83 * returns truthfully whether a bu_hist has been initialized via
84 * BU_HIST_INIT() or BU_HIST_INIT_ZERO.
85 */
86#define BU_HIST_IS_INITIALIZED(_hp) (((struct bu_hist *)(_hp) != BU_HIST_NULL) && LIKELY((_hp)->magic == BU_HIST_MAGIC))
87
88#define BU_HIST_TALLY(_hp, _val) { \
89 if ((_val) <= (_hp)->hg_min) { \
90 (_hp)->hg_bins[0]++; \
91 } else if ((_val) >= (_hp)->hg_max) { \
92 (_hp)->hg_bins[(_hp)->hg_nbins]++; \
93 } else { \
94 (_hp)->hg_bins[(int)(((_val)-(_hp)->hg_min)/(_hp)->hg_clumpsize)]++; \
95 } \
96 (_hp)->hg_nsamples++; }
97
98#define BU_HIST_TALLY_MULTIPLE(_hp, _val, _count) { \
99 int __count = (_count); \
100 if ((_val) <= (_hp)->hg_min) { \
101 (_hp)->hg_bins[0] += __count; \
102 } else if ((_val) >= (_hp)->hg_max) { \
103 (_hp)->hg_bins[(_hp)->hg_nbins] += __count; \
104 } else { \
105 (_hp)->hg_bins[(int)(((_val)-(_hp)->hg_min)/(_hp)->hg_clumpsize)] += __count; \
106 } \
107 (_hp)->hg_nsamples += __count; }
108
109/* hist.c */
110/* These are a set of data histogramming routines. */
111
112BU_EXPORT extern void bu_hist_free(struct bu_hist *histp);
113
114/**
115 * Initialize a bu_hist structure.
116 *
117 * It is expected that the structure is junk upon entry.
118 */
119BU_EXPORT extern void bu_hist_init(struct bu_hist *histp, fastf_t min, fastf_t max, size_t nbins);
120
121BU_EXPORT extern void bu_hist_range(struct bu_hist *hp, fastf_t low, fastf_t high);
122
123/**
124 * Print a histogram.
125 */
126BU_EXPORT extern void bu_hist_pr(const struct bu_hist *histp, const char *title);
127
128/** @} */
129
130__END_DECLS
131
132#endif /* BU_HIST_H */
133
134/*
135 * Local Variables:
136 * mode: C
137 * tab-width: 8
138 * indent-tabs-mode: t
139 * c-file-style: "stroustrup"
140 * End:
141 * ex: shiftwidth=4 tabstop=8
142 */
Header file for the BRL-CAD common definitions.
void bu_hist_range(struct bu_hist *hp, fastf_t low, fastf_t high)
void bu_hist_pr(const struct bu_hist *histp, const char *title)
void bu_hist_free(struct bu_hist *histp)
void bu_hist_init(struct bu_hist *histp, fastf_t min, fastf_t max, size_t nbins)
void int char int int double * min
Definition: tig.h:182
double fastf_t
fastest 64-bit (or larger) floating point type
Definition: vmath.h:330
Global registry of recognized magic numbers.
Definition: hist.h:49
fastf_t hg_max
Definition: hist.h:52
uint32_t magic
Definition: hist.h:50
fastf_t hg_min
Definition: hist.h:51
size_t hg_nsamples
Definition: hist.h:54
size_t hg_nbins
Definition: hist.h:55
fastf_t hg_clumpsize
Definition: hist.h:53
long * hg_bins
Definition: hist.h:56
fundamental vector, matrix, quaternion math macros