NVIDIA Index example code nvidia_logo_transpbg.gif Up
attribute_point_set.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2023 NVIDIA Corporation. All rights reserved.
3 *****************************************************************************/
4
6
7#include "utility/example_shared.h"
8
9//----------------------------------------------------------------------
11 const std::vector< mi::math::Vector_struct< mi::Float32, 3 > >& vertex_vec,
12 const std::vector< mi::Float32 >& attibute_vec,
13 nv::index::IPoint_set::Point_style point_style)
14 :
15 m_vertex_vec(vertex_vec),
16 m_per_vertex_color_values(),
17 m_per_vertex_radii(),
18 m_point_style(point_style),
19 m_rendering_enabled(true),
20 m_pickable(true),
21 m_meta_data(mi::neuraylib::NULL_TAG)
22{
23 this->set_attribute(attibute_vec);
24}
25
26//----------------------------------------------------------------------
27void Attribute_point_set::set_attribute(std::vector< mi::Float32 > const & attribute_vec)
28{
29 check_success(m_vertex_vec.size() == attribute_vec.size());
30
31 m_per_vertex_color_values.clear();
32 m_per_vertex_radii.clear();
33
34 mi::Size const attrib_count = attribute_vec.size();
35 check_success(attrib_count > 0);
36
37 for(mi::Size i = 0; i < attrib_count; ++i)
38 {
39 const mi::Float32 attrib = attribute_vec.at(i);
40 m_per_vertex_color_values.push_back(this->get_color_from_attribute(attrib));
41 m_per_vertex_radii. push_back(this->get_radius_from_attribute(attrib));
42 }
43}
44
45//----------------------------------------------------------------------
46mi::math::Vector_struct<mi::Float32, 3> * Attribute_point_set::peek_vertices()
47{
48 if(!(m_vertex_vec.empty()))
49 {
50 return &m_vertex_vec[0];
51 }
52 else{
53 return 0;
54 }
55}
56
57//----------------------------------------------------------------------
58const mi::math::Vector_struct<mi::Float32, 3>* Attribute_point_set::get_vertices() const
59{
60 if(!(m_vertex_vec.empty()))
61 {
62 return &m_vertex_vec[0];
63 }
64 else{
65 return 0;
66 }
67}
68
69//----------------------------------------------------------------------
70const mi::math::Color_struct* Attribute_point_set::get_colors() const
71{
72 if(!(m_per_vertex_color_values.empty()))
73 {
74 return &m_per_vertex_color_values[0];
75 }
76 else{
77 return 0;
78 }
79}
80
81//----------------------------------------------------------------------
82const mi::Float32* Attribute_point_set::get_radii() const
83{
84 if(!(m_per_vertex_radii.empty()))
85 {
86 return &m_per_vertex_radii[0];
87 }
88 else{
89 return 0;
90 }
91}
92
93//----------------------------------------------------------------------
94mi::neuraylib::IElement* Attribute_point_set::copy() const
95{
97 other->m_vertex_vec = this->m_vertex_vec;
98 other->m_per_vertex_color_values = this->m_per_vertex_color_values;
99 other->m_per_vertex_radii = this->m_per_vertex_radii;
100 other->m_point_style = this->m_point_style;
101 other->m_rendering_enabled = this->m_rendering_enabled;
102 other->m_pickable = this->m_pickable;
103 other->m_meta_data = this->m_meta_data;
104 return other;
105}
106
107//----------------------------------------------------------------------
109{
110 return "Attribute_point_set";
111}
112
113//----------------------------------------------------------------------
115 mi::neuraylib::ISerializer *serializer) const
116{
117 const mi::Size nb_elements = m_vertex_vec.size();
118 serializer->write(&nb_elements, 1);
119 if (nb_elements > 0)
120 {
121 serializer->write(&m_vertex_vec[0].x, nb_elements * 3);
122 serializer->write(&m_per_vertex_color_values[0].r, nb_elements * 4);
123 serializer->write(&m_per_vertex_radii[0], nb_elements);
124 }
125
126 const mi::Uint32 style = static_cast<mi::Uint32>(m_point_style);
127 serializer->write(&style, 1);
128
129 serializer->write(&m_rendering_enabled, 1);
130 serializer->write(&m_pickable, 1);
131 serializer->write(&m_meta_data.id, 1);
132}
133
134//----------------------------------------------------------------------
136 mi::neuraylib::IDeserializer* deserializer)
137{
138 mi::Size nb_elements = 0;
139 deserializer->read(&nb_elements, 1);
140
141 m_vertex_vec. resize(nb_elements);
142 m_per_vertex_color_values.resize(nb_elements);
143 m_per_vertex_radii. resize(nb_elements);
144
145 if (nb_elements > 0)
146 {
147 deserializer->read(&m_vertex_vec[0].x, nb_elements * 3);
148 deserializer->read(&m_per_vertex_color_values[0].r, nb_elements * 4);
149 deserializer->read(&m_per_vertex_radii[0], nb_elements);
150 }
151
152 mi::Uint32 style = 0;
153 deserializer->read(&style, 1);
154 m_point_style = static_cast<Point_style>(style);
155
156 deserializer->read(&m_rendering_enabled, 1);
157 deserializer->read(&m_pickable, 1);
158 deserializer->read(&m_meta_data.id, 1);
159}
160
161//----------------------------------------------------------------------
162mi::math::Color_struct Attribute_point_set::get_color_from_attribute(mi::Float32 attrib)
163{
164 mi::Sint32 const intval = static_cast< mi::Sint32 >(fabs(attrib));
165 mi::math::Color_struct col;
166 const mi::Float32 coef = 1.0f / 15.0f;
167 col.r = coef * static_cast< mi::Float32 >(intval & 15);
168 col.g = coef * static_cast< mi::Float32 >((intval >> 4) & 15);
169 col.b = coef * static_cast< mi::Float32 >((intval >> 8) & 15);
170 col.a = 1.0;
171
172 return col;
173}
174
175//----------------------------------------------------------------------
177{
178 const mi::Float32 rad =
179 static_cast< mi::Float32 >(static_cast< mi::Sint32 >(fabs(attrib)) % 12);
180 return rad;
181}
182
183//----------------------------------------------------------------------
184
attribute point set as an example implementation
An example simple point set shape implementation of IPoint_set.
void set_attribute(std::vector< mi::Float32 > const &attribute_vec)
set point attribute
virtual void serialize(mi::neuraylib::ISerializer *serializer) const
mi::Float32 get_radius_from_attribute(mi::Float32 attrib)
attribute to radius map example
virtual const mi::math::Vector_struct< mi::Float32, 3 > * get_vertices() const
Get the pointer to the array of vertices (points).
virtual const char * get_class_name() const
mi::math::Color_struct get_color_from_attribute(mi::Float32 attrib)
attribute to color map example
virtual mi::neuraylib::IElement * copy() const
Attribute_point_set()
Constructing the point set shape for serialization.
mi::math::Vector_struct< mi::Float32, 3 > * peek_vertices()
peek the vertex position for change.
virtual void deserialize(mi::neuraylib::IDeserializer *deserializer)
virtual const mi::Float32 * get_radii() const
Get the pointer to the array of per-vertex radii.
virtual const mi::math::Color_struct * get_colors() const
Get the pointer to the array of per-vertex color values.
#define check_success(expr)