NVIDIA Index example code nvidia_logo_transpbg.gif Up
attribute_line_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> >& line_segments,
12 const std::vector< mi::Float32 > & attribute_vec)
13 :
14 m_line_segments(line_segments),
15 m_per_segment_colors(),
16 m_per_segment_widths(),
17 m_rendering_enabled(true),
18 m_pickable(true),
19 m_meta_data(mi::neuraylib::NULL_TAG)
20{
21 this->set_attribute(attribute_vec);
22}
23
24//----------------------------------------------------------------------
25void Attribute_line_set::set_attribute(const std::vector< mi::Float32 >& attribute_vec)
26{
27 check_success(m_line_segments.size() == attribute_vec.size());
28
29 m_per_segment_colors.clear();
30 m_per_segment_widths.clear();
31
32 mi::Size const attrib_count = attribute_vec.size();
33 check_success(attrib_count > 0);
34
35 for(mi::Size i = 0; i < attrib_count; ++i){
36 mi::Float32 const attrib = attribute_vec.at(i);
37 // DEBUG_LOG << "attrib = " << attrib;
38
39 m_per_segment_colors.push_back(this->get_color_from_attribute(attrib));
40 m_per_segment_widths.push_back(this->get_width_from_attribute(attrib));
41 // DEBUG_LOG << "color = " << m_per_segment_colors.back() << ", width = " << m_per_segment_widths.back();
42 }
43}
44
45nv::index::ILine_set::Cap_style Attribute_line_set::get_cap_style() const
46{
47 return nv::index::ILine_set::CAP_STYLE_FLAT;
48}
49
50void Attribute_line_set::set_cap_style(nv::index::ILine_set::Cap_style style)
51{
52
53}
54
55//----------------------------------------------------------------------
57{
58 return m_line_segments.size() / 2;
59}
60
61//----------------------------------------------------------------------
63{
64 return m_line_segments.size();
65}
66
67//----------------------------------------------------------------------
68const mi::math::Vector_struct<mi::Float32, 3>* Attribute_line_set::get_lines() const
69{
70 if (!m_line_segments.empty())
71 return &m_line_segments[0];
72 else
73 return NULL;
74}
75
76//----------------------------------------------------------------------
77const mi::math::Color_struct* Attribute_line_set::get_colors() const
78{
79 if (!m_per_segment_colors.empty())
80 return &m_per_segment_colors[0];
81 else
82 return NULL;
83}
84
85//----------------------------------------------------------------------
86const mi::Float32* Attribute_line_set::get_widths() const
87{
88 if (!m_per_segment_widths.empty())
89 return &m_per_segment_widths[0];
90 else
91 return NULL;
92}
93
94//----------------------------------------------------------------------
95nv::index::ILine_set::Line_style Attribute_line_set::get_line_style() const
96{
97 // LINE_STYLE_DASHED FIXME
98 // LINE_STYLE_DOTTED
99 return nv::index::ILine_set::LINE_STYLE_SOLID;
100}
101
102nv::index::ILine_set::Line_type Attribute_line_set::get_line_type() const
103{
104 return nv::index::ILine_set::LINE_TYPE_SEGMENTS;
105}
106
107//----------------------------------------------------------------------
109{
110 m_rendering_enabled = is_enable;
111}
112
113//----------------------------------------------------------------------
115{
116 return m_rendering_enabled;
117}
118
119//----------------------------------------------------------------------
120mi::neuraylib::IElement* Attribute_line_set::copy() const
121{
123 other->m_line_segments = this->m_line_segments;
124 other->m_per_segment_widths = this->m_per_segment_widths;
125 other->m_per_segment_colors = this->m_per_segment_colors;
126
127 other->m_rendering_enabled = this->m_rendering_enabled;
128 other->m_pickable = this->m_pickable;
129 other->m_meta_data = this->m_meta_data;
130
131 return other;
132}
133
134//----------------------------------------------------------------------
136{
137 return "Attribute_line_set";
138}
139
140//----------------------------------------------------------------------
142 mi::neuraylib::ISerializer *serializer) const
143{
144 const mi::Size nb_elements = m_line_segments.size() / 2;
145 serializer->write(&nb_elements, 1);
146
147 serializer->write(&m_line_segments[0].x, nb_elements * 3 * 2);
148 serializer->write(&m_per_segment_colors[0].r, nb_elements * 4);
149 serializer->write(&m_per_segment_widths[0], nb_elements);
150
151 serializer->write(&m_rendering_enabled, 1);
152 serializer->write(&m_pickable, 1);
153 serializer->write(&m_meta_data.id, 1);
154}
155
156//----------------------------------------------------------------------
158 mi::neuraylib::IDeserializer* deserializer)
159{
160 mi::Size nb_elements = 0;
161 deserializer->read(&nb_elements, 1);
162 m_line_segments.resize(nb_elements * 2);
163 deserializer->read(&m_line_segments[0].x, nb_elements * 3 * 2);
164
165 m_per_segment_colors.resize(nb_elements);
166 deserializer->read(&m_per_segment_colors[0].r, nb_elements * 4);
167
168 m_per_segment_widths.resize(nb_elements);
169 deserializer->read(&m_per_segment_widths[0], nb_elements);
170
171 deserializer->read(&m_rendering_enabled, 1);
172 deserializer->read(&m_pickable, 1);
173 deserializer->read(&m_meta_data.id, 1);
174}
175
176//----------------------------------------------------------------------
177mi::math::Color_struct Attribute_line_set::get_color_from_attribute(mi::Float32 attrib)
178{
179 mi::Sint32 const intval = static_cast< mi::Sint32 >(fabs(attrib));
180 mi::math::Color_struct col;
181 mi::Float32 const coef = 1.0f / 15.0f;
182 col.r = coef * static_cast< mi::Float32 >(intval & 15);
183 col.g = coef * static_cast< mi::Float32 >((intval >> 4) & 15);
184 col.b = coef * static_cast< mi::Float32 >((intval >> 8) & 15);
185 col.a = 1.0;
186
187 return col;
188}
189
190//----------------------------------------------------------------------
191mi::Float32 Attribute_line_set::get_width_from_attribute(mi::Float32 attrib)
192{
193 mi::Float32 const width =
194 static_cast< mi::Float32 >(static_cast< mi::Sint32 >(fabs(attrib)) % 12);
195 return width;
196}
197
198//----------------------------------------------------------------------
attribute line set as an example implementation
An example of line set that contains 3D line segments (stored as vertices in a array) and a color as ...
virtual const mi::Float32 * get_widths() const
Get the pointer to the array of per-segment widths (for rasterized lines) or radii (for ray traced cy...
virtual const mi::math::Vector_struct< mi::Float32, 3 > * get_lines() const
Get the pointer to the array of line vertices (two vertices per line segment)
virtual void serialize(mi::neuraylib::ISerializer *serializer) const
void set_attribute(std::vector< mi::Float32 > const &attribute_vec)
set line attribute
virtual mi::Size get_nb_vertices() const
Get number of line segment in this line set.
mi::math::Color_struct get_color_from_attribute(mi::Float32 attrib)
attribute to color map example
virtual void set_enabled(bool is_enable)
Set graphical representation can be enabled or disabled from rendering.
virtual nv::index::ILine_set::Line_type get_line_type() const
Get the line style.
virtual mi::neuraylib::IElement * copy() const
virtual Cap_style get_cap_style() const
Get the line cap style.
mi::Float32 get_width_from_attribute(mi::Float32 attrib)
attribute to width map example
Attribute_line_set()
Constructing the attribute line set shape for serialization.
virtual const char * get_class_name() const
virtual void set_cap_style(Cap_style style)
Set the line cap style.
virtual mi::Size get_nb_lines() const
Get number of line segment in this line set.
virtual const mi::math::Color_struct * get_colors() const
Get the pointer to the array of per-segment color values.
virtual void deserialize(mi::neuraylib::IDeserializer *deserializer)
virtual bool get_enabled() const
get rendering mode
virtual nv::index::ILine_set::Line_style get_line_style() const
Get the line style.
#define check_success(expr)