BRL-CAD
Loading...
Searching...
No Matches
mater.h
Go to the documentation of this file.
1/* M A T E R . H
2 * BRL-CAD
3 *
4 * Copyright (c) 1993-2026 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 mater.h
21 *
22 * @brief Region-ID-based material/color table management for BRL-CAD databases.
23 *
24 * BRL-CAD supports a region-ID color table: a sorted, non-overlapping list of
25 * @c struct @c mater entries that map integer region-ID ranges to RGB colors.
26 * This table is stored per-database (in @c db_i) and is used by the ray tracer
27 * and display logic to assign colors to regions based on their region ID when no
28 * explicit color attribute is present on the object itself.
29 *
30 * The table is populated when the database is opened (from @c _GLOBAL attributes
31 * for v5 databases, or from @c ID_COLORTAB records for v4 databases) and is
32 * freed when the database is closed.
33 *
34 * Typical usage pattern:
35 * - At database load time, @c db_mater_add() is called for each stored color
36 * record, which calls @c db_mater_insert() to build the sorted in-memory list.
37 * - At ray-trace / display time, @c db_mater_color_region() walks the table to
38 * apply an override color to a region whose ID falls within a mapped range.
39 * - To inspect or serialize the current table, use @c db_mater_head() to obtain
40 * the list head and iterate via @c mt_forw, or call @c db_mater_to_vls() for
41 * a string representation suitable for storing in a @c _GLOBAL attribute.
42 * - @c db_mater_free() releases all in-memory table entries (called by
43 * @c db_close()).
44 */
45
46#ifndef RT_MATER_H
47#define RT_MATER_H
48
49#include "common.h"
50#include "rt/defines.h"
51#include "bu/vls.h"
52
54
55struct region; /* forward declaration */
56struct db_i; /* forward declaration */
57
58/**
59 * Per-region material and shading information carried on a @c struct @c region.
60 *
61 * This is separate from the region-ID color table (see @c struct @c mater
62 * below); it stores per-region shader parameters and color as parsed from
63 * object attributes.
64 */
65struct mater_info {
66 float ma_color[3]; /**< @brief explicit color: 0..1 */
67 float ma_temperature; /**< @brief positive ==> degrees Kelvin */
68 char ma_color_valid; /**< @brief non-0 ==> ma_color is non-default */
69 char ma_cinherit; /**< @brief color: DB_INH_LOWER / DB_INH_HIGHER */
70 char ma_minherit; /**< @brief mater: DB_INH_LOWER / DB_INH_HIGHER */
71 char *ma_shader; /**< @brief shader name & parms */
72};
73#define RT_MATER_INFO_INIT_ZERO { VINIT_ZERO, 0.0, 0, 0, 0, NULL }
74/* From MGED initial tree state */
75#define RT_MATER_INFO_INIT_IDN { {1.0, 0.0, 0.0} , -1.0, 0, 0, 0, NULL }
76
77/**
78 * One entry in the per-database region-ID-to-color mapping table.
79 *
80 * Entries are kept in a singly-linked list sorted by ascending @c mt_low value
81 * with no overlapping ranges. Use @c db_mater_insert() to add entries in a
82 * way that preserves this invariant.
83 */
84struct mater {
85 long mt_low; /**< @brief lower bound of region ID range (inclusive) */
86 long mt_high; /**< @brief upper bound of region ID range (inclusive) */
87 unsigned char mt_r; /**< @brief red component (0-255) */
88 unsigned char mt_g; /**< @brief green component (0-255) */
89 unsigned char mt_b; /**< @brief blue component (0-255) */
90 b_off_t mt_daddr; /**< @brief database address for v4 record updating */
91 struct mater *mt_forw; /**< @brief next entry in the sorted list */
92};
93#define MATER_NULL ((struct mater *)0)
94#define MATER_NO_ADDR ((b_off_t)0) /**< @brief sentinel: entry has no backing database record */
95
96
97/**
98 * Apply the region-ID color table to a region.
99 *
100 * If @p regp->reg_regionid falls within a mapped range in the table associated
101 * with @p dbip, the region's @c reg_mater.ma_color and @c ma_color_valid fields
102 * are updated accordingly. Has no effect if the region ID does not match any
103 * entry.
104 *
105 * @param dbip database whose material table is consulted
106 * @param regp region to color
107 */
108RT_EXPORT extern void db_mater_color_region(struct db_i *dbip, struct region *regp);
109
110/**
111 * Construct a @c struct @c mater entry from raw record data and insert it into
112 * the database's material table.
113 *
114 * This is the primary loader function called from database scan routines (e.g.,
115 * @c db_scan() for v4, @c db5_import_color_table() for v5) to populate the
116 * in-memory table from stored records.
117 *
118 * @param dbip database whose material table receives the new entry
119 * @param low lower bound of the region ID range
120 * @param hi upper bound of the region ID range
121 * @param r red component (0-255)
122 * @param g green component (0-255)
123 * @param b blue component (0-255)
124 * @param addr backing database address (@c MATER_NO_ADDR if none)
125 */
126RT_EXPORT extern void db_mater_add(struct db_i *dbip,
127 int low,
128 int hi,
129 int r,
130 int g,
131 int b,
132 b_off_t addr);
133
134/**
135 * Insert a @c struct @c mater entry into a database's material table, maintaining
136 * sorted order and resolving any overlaps with existing entries.
137 *
138 * Ownership of @p newp is transferred to the table; do not free it after
139 * calling this function.
140 *
141 * @param dbip database whose material table is modified
142 * @param newp entry to insert (must be heap-allocated)
143 */
144RT_EXPORT extern void db_mater_insert(struct db_i *dbip, struct mater *newp);
145
146/**
147 * Serialize the database's material table to a @c bu_vls string.
148 *
149 * Each entry is written as @c "{low high r g b} " and appended to @p str.
150 * The resulting string is suitable for storage in the @c regionid_colortable
151 * attribute of a v5 @c _GLOBAL object.
152 *
153 * @param str output string (appended to; caller must initialize)
154 * @param dbip database whose material table is serialized
155 */
156RT_EXPORT extern void db_mater_to_vls(struct bu_vls *str, struct db_i *dbip);
157
158/**
159 * Return the head of the material table list for a database.
160 *
161 * The returned pointer is the first entry of the sorted list; iterate using
162 * @c mt_forw to traverse all entries. Returns @c MATER_NULL if the table is
163 * empty.
164 *
165 * @param dbip database to query
166 * @return pointer to first @c struct @c mater entry, or @c MATER_NULL
167 */
168RT_EXPORT extern struct mater *db_mater_head(struct db_i *dbip);
169
170/**
171 * Replace the material table list head for a database.
172 *
173 * This is a low-level setter. It does @e not free the old list; call
174 * @c db_mater_free() first if the old entries should be released. Typically
175 * used when swapping in a pre-built list (e.g., after @c db_mater_dup()).
176 *
177 * @param dbip database to update
178 * @param newmat new list head (may be @c MATER_NULL to clear the table)
179 */
180RT_EXPORT extern void db_mater_set_head(struct db_i *dbip, struct mater *newmat);
181
182/**
183 * Return a deep copy of the material table for a database.
184 *
185 * All entries are duplicated; the caller owns the returned list and is
186 * responsible for freeing it (e.g., by iterating and calling @c bu_free(), or
187 * by inserting into another database's table via @c db_mater_set_head() and
188 * letting @c db_mater_free() handle cleanup at close time).
189 *
190 * @param dbip database whose material table is duplicated
191 * @return head of the new list, or @c MATER_NULL if the table is empty
192 */
193RT_EXPORT extern struct mater *db_mater_dup(struct db_i *dbip);
194
195/**
196 * Free all entries in the material table of a database.
197 *
198 * After this call the table is empty (@c db_mater_head() returns
199 * @c MATER_NULL). Called automatically by @c db_close().
200 *
201 * @param dbip database whose material table is freed
202 */
203RT_EXPORT extern void db_mater_free(struct db_i *dbip);
204
205/**
206 * @name Deprecated global material API (use db_mater_* instead)
207 *
208 * The following functions operated on a single process-wide (global) material
209 * table and are retained for backward compatibility only. New code must use
210 * the per-database @c db_mater_*() functions above.
211 *
212 * @deprecated since 7.42 - use db_mater_head(), db_mater_set_head(),
213 * db_mater_dup(), db_mater_free(), db_mater_insert(),
214 * db_mater_add(), db_mater_to_vls(), and db_mater_color_region()
215 * instead.
216 * @{
217 */
218
219/** @deprecated use db_mater_head() */
221
222/** @deprecated use db_mater_set_head() */
224
225/** @deprecated use db_mater_dup() */
227
228/** @deprecated use db_mater_free() */
230
231/** @deprecated use db_mater_insert() */
233
234/** @deprecated use db_mater_add() */
235DEPRECATED RT_EXPORT extern void rt_color_addrec(int low, int hi, int r, int g, int b, b_off_t addr);
236
237/** @deprecated use db_mater_to_vls() */
239
240/** @deprecated use db_mater_color_region() */
242
243/** @} */
244
246
247#endif /* RT_MATER_H */
248
249/*
250 * Local Variables:
251 * tab-width: 8
252 * mode: C
253 * indent-tabs-mode: t
254 * c-file-style: "stroustrup"
255 * End:
256 * ex: shiftwidth=4 tabstop=8
257 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
#define DEPRECATED
Definition common.h:433
#define b_off_t
Definition common.h:255
void db_mater_to_vls(struct bu_vls *str, struct db_i *dbip)
DEPRECATED void rt_vls_color_map(struct bu_vls *str)
struct mater * db_mater_dup(struct db_i *dbip)
DEPRECATED void rt_new_material_head(struct mater *newmat)
void db_mater_insert(struct db_i *dbip, struct mater *newp)
struct mater * db_mater_head(struct db_i *dbip)
DEPRECATED void rt_region_color_map(struct region *regp)
DEPRECATED struct mater * rt_dup_material_head(void)
DEPRECATED void rt_color_addrec(int low, int hi, int r, int g, int b, b_off_t addr)
void db_mater_color_region(struct db_i *dbip, struct region *regp)
void db_mater_set_head(struct db_i *dbip, struct mater *newmat)
void db_mater_add(struct db_i *dbip, int low, int hi, int r, int g, int b, b_off_t addr)
void db_mater_free(struct db_i *dbip)
DEPRECATED void rt_insert_color(struct mater *newp)
DEPRECATED struct mater * rt_material_head(void)
DEPRECATED void rt_color_free(void)
Definition vls.h:53
char * ma_shader
shader name & parms
Definition mater.h:71
float ma_color[3]
explicit color: 0..1
Definition mater.h:66
float ma_temperature
positive ==> degrees Kelvin
Definition mater.h:67
char ma_cinherit
color: DB_INH_LOWER / DB_INH_HIGHER
Definition mater.h:69
char ma_color_valid
non-0 ==> ma_color is non-default
Definition mater.h:68
char ma_minherit
mater: DB_INH_LOWER / DB_INH_HIGHER
Definition mater.h:70
Definition mater.h:84
b_off_t mt_daddr
database address for v4 record updating
Definition mater.h:90
unsigned char mt_g
green component (0-255)
Definition mater.h:88
unsigned char mt_b
blue component (0-255)
Definition mater.h:89
long mt_low
lower bound of region ID range (inclusive)
Definition mater.h:85
struct mater * mt_forw
next entry in the sorted list
Definition mater.h:91
unsigned char mt_r
red component (0-255)
Definition mater.h:87
long mt_high
upper bound of region ID range (inclusive)
Definition mater.h:86