NDEVR
API Documentation
MaterialRaster.h
1#pragma once
2#include "DLLInfo.h"
3#include <NDEVR/ImageFactory.h>
4#include <NDEVR/VectorFunctions.h>
5#include <NDEVR/RGBColor.h>
6#include <NDEVR/INIInterface.h>
7namespace NDEVR
8{
12 struct RasterInfo : public INIInterface
13 {
17 {}
18
21 RasterInfo(const File& ini_file)
22 : INIInterface(ini_file)
23 {}
24
27 bool isEnabled() const
28 {
29 return is_enabled && pixel_size > 0U;
30 }
37 bool is_enabled = false;
41 virtual void getINI(INIFactory& factory) override
42 {
43 factory.addOption("pixel_size", pixel_size);
44 factory.addOption("radial_distortion_scale", radial_distortion_scale);
45 factory.addOption("radial_distortion_offset", radial_distortion_offset);
46 factory.addOption("side_crop", side_crop);
47 factory.addOption("camera_drape_move_angle", camera_drape_move_angle);
48 factory.addOption("angle_offset", angle_offset);
49 factory.addOption("is_enabled", is_enabled);
50 }
51 };
52
56 {
57 public:
61 MaterialRasterBase(const RasterInfo& raster_info)
62 : m_raster_info(raster_info)
63 {}
64
66 virtual void updateImage() = 0;
72 virtual void addImageBuffer(const Buffer<uint01>& pixels, const Vector<2, uint04>& size, const Matrix<fltp08>& camera_matrix) = 0;
76 virtual StringView imageID() const = 0;
82 virtual void addPixelInfo(const RGBColor& pixel, const Vector<2, fltp08>& location, uint04 weight) = 0;
88 virtual void addPixelInfo(const RGBColor& pixel, const Vector<2, uint04>& location, uint08 weight) = 0;
94 virtual void addPixelInfo(const Vector<4, fltp04>& pixel, const Vector<2, fltp08>& location, uint08 weight) = 0;
101 virtual void addPixelInfo(const Vector<4, fltp04>& pixel, const Vector<2, uint04>& location, uint08 weight) = 0;
102 protected:
104 };
105
110 {
111 public:
117 MaterialRaster(const String& image_id, const RasterInfo& raster_info)
118 : MaterialRasterBase(raster_info)
119 , m_image_id(image_id)
120 , m_color_channels(raster_info.pixel_size[X] * raster_info.pixel_size[Y], Vector<3, uint08>(0))
121 , m_color_channel_average_count(raster_info.pixel_size[X] * raster_info.pixel_size[Y], 0)
122 , m_pixels(4 * raster_info.pixel_size[X] * raster_info.pixel_size[Y], 255)
124 {
126 }
127
130 StringView imageID() const override
131 {
132 return m_image_id;
133 }
134
135 virtual void clearRaster()
136 {
137 m_color_channels.setAllToValue(Vector<3, uint08>(0));
138 m_color_channel_average_count.setAllToValue(0);
139 }
140
144 virtual void setUnsetColor(RGBColor color)
145 {
146 for (uint04 y = m_update_bounds[MIN][Y]; y < m_update_bounds[MAX][Y]; y++)
147 {
148 for (uint04 x = m_update_bounds[MIN][X]; x < m_update_bounds[MAX][X]; x++)
149 {
150 uint04 pixel_index = convertToIndex({ x,y });
151 if (m_color_channel_average_count[pixel_index] == 0)
152 {
153 m_pixels[4 * pixel_index + RGBColor::r_pos] = color[RGBColor::r_pos];
154 m_pixels[4 * pixel_index + RGBColor::g_pos] = color[RGBColor::g_pos];
155 m_pixels[4 * pixel_index + RGBColor::b_pos] = color[RGBColor::b_pos];
156 }
157 }
158 }
159 }
160
163 virtual void updateImage() override
164 {
165 for (uint04 y = m_update_bounds[MIN][Y]; y <= m_update_bounds[MAX][Y]; y++)
166 {
167 for (uint04 x = m_update_bounds[MIN][X]; x <= m_update_bounds[MAX][X]; x++)
168 {
169 uint04 pixel_index = convertToIndex({ x,y });
170 if (m_color_channel_average_count[pixel_index] > 0)
171 {
175 }
176 }
177 }
179 }
180
188 static uint04 Weight(const Vertex<2, fltp08>& location, fltp08 y_dist)
189 {
190 uint04 weight = getMax(1U, cast<uint04>(1.0 / getMax(0.001, y_dist * location.magnitudeSquared())));
191 return weight;
192 }
193
198 virtual void addImageBuffer(const Buffer<uint01>& pixels, const Vector<2, uint04>& size, const Matrix<fltp08>& camera_matrix) override;
210 {
211 return location[Y] * m_raster_info.pixel_size[X] + location[X];
212 }
213
219 void addPixelInfo(const RGBColor& pixel, const Vector<2, fltp08>& location, uint04 weight) override
220 {
221 if (weight > 0)
222 {
223 addPixelInfo(pixel, convertLocation(location), weight);
224 }
225 }
226
232 void addPixelInfo(const RGBColor& pixel, const Vector<2, uint04>& location, uint08 weight) override
233 {
234 if (weight > 0)
235 {
236 m_update_bounds.addToBounds(location);
237 uint04 pixel_index = convertToIndex(location);
238
239 m_color_channels[pixel_index] += (weight * pixel.as<3, uint08>());
240 m_color_channel_average_count[pixel_index] += weight;
241 }
242 }
243
249 void addPixelInfo(const Vector<4, fltp04>& pixel, const Vector<2, fltp08>& location, uint08 weight) override
250 {
251 if(weight > 0)
252 addPixelInfo(pixel, convertLocation(location), weight);
253 }
254
261 void addPixelInfo(const Vector<4, fltp04>& pixel, const Vector<2, uint04>& location, uint08 weight) override
262 {
263 m_update_bounds.addToBounds(location);
264 uint04 pixel_index = convertToIndex(location);
265 if (weight != Constant<uint08>::Max)
266 {
267 m_color_channels[pixel_index] += (weight * (pixel * 255.0f).as<3, uint08>());
268 m_color_channel_average_count[pixel_index] += weight;
269 }
270 else
271 {
272 m_color_channels[pixel_index] = (pixel * 255.0f).as<3, uint08>();
273 m_color_channel_average_count[pixel_index] = 1;
274 }
275 }
276 protected:
282 };
283
286 class NDEVR_DESIGN_API MaterialRasterFactory
287 {
288 public:
294 static MaterialRasterBase* CreateRaster(const String& image_id, const RasterInfo& info);
299 protected:
306 virtual MaterialRasterBase* createRaster(const String& image_id, const RasterInfo& info) = 0;
308 };
309}
Stores an angle in an optimized internal format with support for efficient trigonometric operations.
Definition Angle.h:83
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:54
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
Contains methods for easily reading and writing to an INI file including efficient casting,...
Definition INIReader.h:107
void addOption(const StringView &label, Resource< Vector< t_dims, t_type > > &mem_loc)
Registers a Vector Resource option that will be read/written by the factory.
Definition INIReader.h:141
INIInterface()
Constructs an INIInterface with no default INI file.
void addImageUncompressed(const StringView &id, const Buffer< uint01 > &uncompressed, bool clear_other)
Adds an uncompressed (raw ARGB) image to the cache.
static ImageFactory & DefaultFactory()
Returns the singleton default ImageFactory instance.
Base class for painting pixels at a time into a 2D texture.
MaterialRasterBase(const RasterInfo &raster_info)
Constructs the base raster with the given raster configuration.
virtual void addPixelInfo(const RGBColor &pixel, const Vector< 2, fltp08 > &location, uint04 weight)=0
Adds a single pixel color at a normalized floating-point location with a weight.
RasterInfo m_raster_info
The raster configuration settings.
virtual void addPixelInfo(const Vector< 4, fltp04 > &pixel, const Vector< 2, fltp08 > &location, uint08 weight)=0
Adds a single pixel as a normalized float RGBA vector at a normalized location with a weight.
virtual void addImageBuffer(const Buffer< uint01 > &pixels, const Vector< 2, uint04 > &size, const Matrix< fltp08 > &camera_matrix)=0
Adds an entire image buffer to the raster, mapping pixels through a camera matrix.
virtual void addPixelInfo(const Vector< 4, fltp04 > &pixel, const Vector< 2, uint04 > &location, uint08 weight)=0
Adds a single pixel as a normalized float RGBA vector at an integer pixel location with a weight.
virtual StringView imageID() const =0
Returns the identifier string of the underlying image resource.
virtual void addPixelInfo(const RGBColor &pixel, const Vector< 2, uint04 > &location, uint08 weight)=0
Adds a single pixel color at an integer pixel location with a weight.
virtual void updateImage()=0
Commits accumulated pixel data to the underlying image.
Creates rasters for converting points and directions into 2D images.
static void SetDefaultMaterialRasterFactory(MaterialRasterFactory *factory)
Sets the default MaterialRasterFactory used by CreateRaster.
static MaterialRasterFactory * s_default_raster_factory
The singleton default factory used by CreateRaster.
static MaterialRasterBase * CreateRaster(const String &image_id, const RasterInfo &info)
Creates a MaterialRasterBase using the default factory implementation.
virtual MaterialRasterBase * createRaster(const String &image_id, const RasterInfo &info)=0
Creates a MaterialRasterBase instance.
void addPixelInfo(const Vector< 4, fltp04 > &pixel, const Vector< 2, uint04 > &location, uint08 weight) override
Adds pixel color data (as normalized floats) at an integer pixel location with a weight.
String m_image_id
The unique identifier for this raster's image resource.
void addPixelInfo(const RGBColor &pixel, const Vector< 2, fltp08 > &location, uint04 weight) override
Adds pixel color data at a normalized floating-point location with a given weight.
Buffer< uint08 > m_color_channel_average_count
Total accumulated weight per pixel (divisor for averaging).
Buffer< Vector< 3, uint08 > > m_color_channels
Accumulated weighted RGB color values per pixel (used for averaging).
void addPixelInfo(const RGBColor &pixel, const Vector< 2, uint04 > &location, uint08 weight) override
Adds pixel color data at an integer pixel location with a given weight.
virtual void updateImage() override
Commits accumulated pixel data to the image by averaging color channels, then uploads the result to t...
Buffer< uint01 > m_pixels
Raw RGBA pixel buffer (4 bytes per pixel) uploaded to the ImageFactory.
Vector< 2, uint04 > convertLocation(const Vector< 2, fltp08 > &location) const
Converts a normalized [0,1] floating-point location to an integer pixel coordinate,...
static uint04 Weight(const Vertex< 2, fltp08 > &location, fltp08 y_dist)
Computes a pixel contribution weight based on distance from the raster center.
void addPixelInfo(const Vector< 4, fltp04 > &pixel, const Vector< 2, fltp08 > &location, uint08 weight) override
Adds pixel color data (as normalized floats) at a normalized location with a weight.
MaterialRaster(const String &image_id, const RasterInfo &raster_info)
Constructs a MaterialRaster with a given image identifier and raster configuration.
Bounds< 2, uint04 > m_update_bounds
Bounding box of pixels that have been modified since the last update.
virtual void clearRaster()
Resets all accumulated color channel data and average counts to zero.
StringView imageID() const override
Returns the image identifier for this raster.
virtual void addImageBuffer(const Buffer< uint01 > &pixels, const Vector< 2, uint04 > &size, const Matrix< fltp08 > &camera_matrix) override
Adds an entire image buffer to the raster, mapping pixels through a camera matrix.
uint04 convertToIndex(const Vector< 2, uint04 > &location) const
Converts a 2D pixel coordinate to a linear buffer index.
virtual void setUnsetColor(RGBColor color)
Sets a fallback color for all pixels within the update bounds that have not received any pixel data (...
Templated logic for doing matrix multiplication.
Definition Matrix.hpp:182
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:57
static const uint01 r_pos
The index of the red channel in the RGBA color struct.
Definition RGBColor.h:59
static const uint01 g_pos
The index of the green channel in the RGBA color struct.
Definition RGBColor.h:60
static const uint01 b_pos
The index of the blue channel in the RGBA color struct.
Definition RGBColor.h:61
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
constexpr decltype(auto) as() const
Returns the vector as a new time of vector.
Definition Vector.hpp:301
constexpr t_type magnitudeSquared() const
Vectors are commonly used to model forces such as wind, sea current, gravity, and electromagnetism.
Definition Vector.hpp:448
A point in N-dimensional space, used primarily for spatial location information.
Definition Vertex.hpp:44
The primary namespace for the NDEVR SDK.
constexpr t_type getMax(const t_type &left, const t_type &right)
Finds the max of the given arguments using the > operator The only requirement is that t_type have > ...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
@ DEGREES
Angle measured in degrees (0 to 360 for a full circle).
Definition Angle.h:58
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Stores information for painting pixels at a time into a 2D texture used with MaterialRaster.
Vector< 2, Angle< fltp08 > > angle_offset
Angular offset applied to the camera drape direction.
fltp08 radial_distortion_scale
Scale factor applied to radial distortion correction.
RasterInfo()
Default constructor.
uint04 side_crop
Number of pixels to crop from each side of the source image.
Vector< 2, uint04 > pixel_size
The width and height of the raster texture in pixels.
bool isEnabled() const
Checks whether the raster is enabled and has a valid (non-zero) pixel size.
RasterInfo(const File &ini_file)
Constructs a RasterInfo from an INI configuration file.
Vector< 2, Angle< fltp08 > > camera_drape_move_angle
The angular range used when draping camera imagery onto the raster.
virtual void getINI(INIFactory &factory) override
Serializes or deserializes raster settings to/from an INI file via the factory.
bool is_enabled
Whether rasterization is currently enabled.
fltp08 radial_distortion_offset
Offset applied to radial distortion correction.