NVIDIA Index example code nvidia_logo_transpbg.gif Up
create_annotations.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2023 NVIDIA Corporation. All rights reserved.
3 *****************************************************************************/
6
7#include <mi/dice.h>
8
9// Include code shared by all examples.
10#include "utility/example_shared.h"
11
12#include <nv/index/icamera.h>
13#include <nv/index/ifont.h>
14#include <nv/index/iindex.h>
15#include <nv/index/ilabel.h>
16#include <nv/index/ilight.h>
17#include <nv/index/iline_set.h>
18#include <nv/index/imaterial.h>
19#include <nv/index/ipoint_set.h>
20#include <nv/index/iscene.h>
21
22#include <mi/math.h>
23
24#include <nv/index/app/string_dict.h>
25#include <nv/index/app/index_connect.h>
26
27#include "utility/canvas_utility.h"
28
29#include <iostream>
30#include <sstream>
31
32//----------------------------------------------------------------------
33// Create_annotations application class
35 public nv::index::app::Index_connect
36{
37public:
39 :
40 Index_connect(),
41 m_is_unittest(false)
42 {
43 // INFO_LOG << "DEBUG: create_annotations_index_connect() ctor";
44 }
45
47 {
48 // Note: Index_connect::~Index_connect() will be called after here.
49 // INFO_LOG << "DEBUG: ~create_annotations_index_connect() dtor";
50 }
51
52 // launch application
53 mi::Sint32 launch();
54
55protected:
56 virtual bool evaluate_options(nv::index::app::String_dict& sdict) CPP11_OVERRIDE;
57 // override for unittest support
59 mi::neuraylib::INetwork_configuration* network_configuration,
60 nv::index::app::String_dict& options) CPP11_OVERRIDE
61 {
62 check_success(network_configuration != 0);
63
64 check_success(options.is_defined("unittest"));
65 const bool is_unittest = nv::index::app::get_bool(options.get("unittest"));
66 if (is_unittest)
67 {
68 info_cout("NETWORK: disabled networking mode.", options);
69 network_configuration->set_mode(mi::neuraylib::INetwork_configuration::MODE_OFF);
70 return true;
71 }
72
73 return initialize_networking_as_default_udp(network_configuration, options);
74 }
75
76private:
77 // Create point set in the scene.
78 //
79 // \param[in] scene_edit IScene interface
80 // \param[in] dice_transaction dice transaction
81 // \return true when success
82 mi::neuraylib::Tag create_scene(
83 nv::index::IScene* scene_edit,
84 mi::neuraylib::IDice_transaction* dice_transaction);
85
86 // set a group node's transformation matrix
87 //
88 // \param[in] group_node_tag group node tag to be set
89 // \param[in] frame_id a frame id. The translation is depends on this id
90 // \param[in] dice_transaction dice transaction
91 void set_transformation(
92 const mi::neuraylib::Tag& group_node_tag,
93 mi::Uint32 frame_id,
94 mi::neuraylib::IDice_transaction* dice_transaction) const;
95
96 // setup camera to see this example scene
97 //
98 // \param[in] cam a camera
99 void setup_camera(nv::index::IPerspective_camera* cam) const;
100
101 // render a frame
102 //
103 // \param[in] output_fname output rendering image filename
104 // \return performance values
105 nv::index::IFrame_results* render_frame(
106 const std::string& output_fname) const;
107
108 // This session tag
109 mi::neuraylib::Tag m_session_tag;
110 // NVIDIA IndeX cluster configuration
111 mi::base::Handle<nv::index::ICluster_configuration> m_cluster_configuration;
112 // Application layer image file canvas (a render target)
113 mi::base::Handle<nv::index::app::canvas_infrastructure::IIndex_image_file_canvas> m_image_file_canvas;
114 // Create_icons options
115 std::string m_outfname;
116 bool m_is_unittest;
117 std::string m_verify_image_fname_sequence;
118 std::string m_font_fpath;
119};
120
121//----------------------------------------------------------------------
123{
124 mi::Sint32 exit_code = 0;
125
126 // Get DiCE database components
127 {
128 m_cluster_configuration =
129 get_index_interface()->get_api_component<nv::index::ICluster_configuration>();
130 check_success(m_cluster_configuration.is_valid_interface());
131
132 // create image canvas in application_layer
133 m_image_file_canvas = create_image_file_canvas(get_application_layer_interface());
134 check_success(m_image_file_canvas.is_valid_interface());
135
136 // Verifying that local host has joined
137 // This may fail when there is a license problem.
138 check_success(is_local_host_joined(m_cluster_configuration.get()));
139
140 mi::neuraylib::Tag scene_group_tag = mi::neuraylib::NULL_TAG;
141 mi::neuraylib::Tag camera_tag = mi::neuraylib::NULL_TAG;
142
143 {
144 // DiCE database access
145 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
146 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
147 check_success(dice_transaction.is_valid_interface());
148 {
149 // Setup session information
150 m_session_tag = m_index_session->create_session(dice_transaction.get());
151 check_success(m_session_tag.is_valid());
152 mi::base::Handle<const nv::index::ISession> session(
153 dice_transaction->access<const nv::index::ISession>(m_session_tag));
154 check_success(session.is_valid_interface());
155
156 mi::base::Handle< nv::index::IScene > scene_edit(
157 dice_transaction->edit<nv::index::IScene>(session->get_scene()));
158 check_success(scene_edit.is_valid_interface());
159
160 //----------------------------------------------------------------------
161 // Scene setup: add annotation shape, scene parameters, camera.
162 //----------------------------------------------------------------------
163 // Add an hierarchical scene description node to the scene
164 scene_group_tag = create_scene(scene_edit.get(), dice_transaction.get());
165 check_success(scene_group_tag.is_valid());
166
167 // Create and edit a camera. Data distribution is
168 // based on the camera. (Because only visible massive
169 // data are considered)
170 mi::base::Handle< nv::index::IPerspective_camera > cam(
171 scene_edit->create_camera<nv::index::IPerspective_camera>());
172 check_success(cam.is_valid_interface());
173 setup_camera(cam.get());
174 camera_tag = dice_transaction->store(cam.get());
175 check_success(camera_tag.is_valid());
176
177 const mi::math::Vector<mi::Uint32,2> buffer_resolution(512, 512);
178 m_image_file_canvas->set_resolution(buffer_resolution);
179
180 // Set up the scene
181 const mi::math::Bbox_struct< mi::Float32, 3 > xyz_roi_st = {
182 { 0.0f, 0.0f, 0.0f, },
183 { 500.0f, 500.0f, 500.0f, },
184 };
185
186 // set the region of interest
187 const mi::math::Bbox< mi::Float32, 3 > xyz_roi(xyz_roi_st);
188 check_success(xyz_roi.is_volume());
189 scene_edit->set_clipped_bounding_box(xyz_roi_st);
190
191 // Set the scene global transformation matrix.
192 // only change the coordinate system
193 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(
194 1.0f, 0.0f, 0.0f, 0.0f,
195 0.0f, 1.0f, 0.0f, 0.0f,
196 0.0f, 0.0f, -1.0f, 0.0f,
197 0.0f, 0.0f, 0.0f, 1.0f
198 );
199 scene_edit->set_transform_matrix(transform_mat);
200
201 // Set the current camera to the scene.
202 check_success(camera_tag.is_valid());
203 scene_edit->set_camera(camera_tag);
204 }
205 dice_transaction->commit();
206 }
207
208 // Rendering
209 {
210 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
211 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
212 check_success(dice_transaction.is_valid_interface());
213 {
214 const std::string fname = get_output_file_name(m_outfname, 0);
215 std::stringstream verify_image_fname;
216 if(!m_verify_image_fname_sequence.empty())
217 {
218 verify_image_fname << m_verify_image_fname_sequence << "000.ppm";
219 }
220 mi::base::Handle<nv::index::IFrame_results> frame_results(render_frame(fname));
221
222 const mi::base::Handle<nv::index::IError_set> err_set(frame_results->get_error_set());
223 if (err_set->any_errors())
224 {
225 std::ostringstream os;
226
227 const mi::Uint32 nb_err = err_set->get_nb_errors();
228 for (mi::Uint32 e = 0; e < nb_err; ++e)
229 {
230 if (e != 0) os << '\n';
231 const mi::base::Handle<nv::index::IError> err(err_set->get_error(e));
232 os << err->get_error_string();
233 }
234
235 ERROR_LOG << "IIndex_rendering rendering call failed with the following error(s): " << '\n'
236 << os.str();
237 }
238
239 // verify the generated frame
240 if (!(verify_canvas_result(get_application_layer_interface(),
241 m_image_file_canvas.get(), verify_image_fname.str(), get_options())))
242 {
243 exit_code = 1;
244 }
245 }
246 dice_transaction->commit();
247 }
248
249 for(mi::Uint32 i = 1; i < 20; ++i)
250 {
251 {
252 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
253 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
254 check_success(dice_transaction.is_valid_interface());
255 {
256 set_transformation(scene_group_tag, i, dice_transaction.get());
257 }
258 dice_transaction->commit();
259 }
260
261 const std::string fname = get_output_file_name(m_outfname, i);
262
263 std::stringstream verify_image_fname;
264 if(!m_verify_image_fname_sequence.empty())
265 {
266 const size_t BUFSZ = 64;
267 char buf[BUFSZ];
268 snprintf(buf, BUFSZ - 1, "%03d.ppm", i);
269 verify_image_fname << m_verify_image_fname_sequence << buf;
270 }
271
272 mi::base::Handle<nv::index::IFrame_results> frame_results(render_frame(fname));
273 const mi::base::Handle<nv::index::IError_set> err_set(frame_results->get_error_set());
274 if (err_set->any_errors())
275 {
276 std::ostringstream os;
277 const mi::Uint32 nb_err = err_set->get_nb_errors();
278 for (mi::Uint32 e = 0; e < nb_err; ++e)
279 {
280 if (e != 0) os << '\n';
281 const mi::base::Handle<nv::index::IError> err(err_set->get_error(e));
282 os << err->get_error_string();
283 }
284
285 ERROR_LOG << "IIndex_rendering rendering call failed with the following error(s): " << '\n'
286 << os.str();
287 exit_code = 1;
288 }
289
290 // verify the generated frame
291 if (!(verify_canvas_result(get_application_layer_interface(),
292 m_image_file_canvas.get(), verify_image_fname.str(), get_options())))
293 {
294 exit_code = 1;
295 }
296 }
297
298 }
299
300 return exit_code;
301}
302
303
304//----------------------------------------------------------------------
305bool Create_annotations::evaluate_options(nv::index::app::String_dict& sdict)
306{
307 const std::string com_name = sdict.get("command:", "<unknown_command>");
308 m_is_unittest = nv::index::app::get_bool(sdict.get("unittest", "false"));
309
310 if (m_is_unittest)
311 {
312 if (nv::index::app::get_bool(sdict.get("is_call_from_test", "false")))
313 {
314 sdict.insert("is_dump_comparison_image_when_failed", "0");
315 }
316 sdict.insert("outfname", ""); // turn off file output in the unit test mode
317 sdict.insert("dice::verbose", "2");
318 }
319
320 m_outfname = sdict.get("outfname", "");
321 m_verify_image_fname_sequence = sdict.get("verify_image_fname_sequence", "");
322 m_font_fpath = sdict.get("font_fpath");
323
324 info_cout(std::string("running ") + com_name, sdict);
325 info_cout("outfname = [" + m_outfname +
326 "], verify_image_fname_sequence = [" + m_verify_image_fname_sequence +
327 "], dice::verbose = " + sdict.get("dice::verbose"), sdict);
328
329 // print help and exit if -h
330 if(sdict.is_defined("h"))
331 {
332 std::cout
333 << "info: Usage: " << com_name << " [option]\n"
334 << "Option: [-h]\n"
335 << " printout this message\n"
336
337 << " [-dice::verbose severity_level]\n"
338 << " verbose severity level (3 is info.). (default: "
339 << sdict.get("dice::verbose") << ")\n"
340
341 << " [-font_fpath FONT_FILE_PATH]\n"
342 << " font file path. (default: " << m_font_fpath << ")\n"
343
344 << " [-outfname string]\n"
345 << " output ppm file base name. When empty, no output.\n"
346 << " A frame number and extension (.ppm) will be added.\n"
347 << " (default: [" << m_outfname << "])\n"
348
349 << " [-verify_image_fname_sequence [image_fname]]\n"
350 << " when image_fname exist, verify the rendering image. (default: ["
351 << sdict.get("verify_image_fname_sequence") << "])\n"
352
353 << " [-unittest bool]\n"
354 << " when true, unit test mode (create smaller volume). "
355 << "(default: " << sdict.get("unittest") << ")"
356 << std::endl;
357 exit(1);
358 }
359
360 return true;
361}
362
363//----------------------------------------------------------------------
364mi::neuraylib::Tag Create_annotations::create_scene(
365 nv::index::IScene* scene_edit,
366 mi::neuraylib::IDice_transaction* dice_transaction)
367{
368 check_success(scene_edit != 0);
369 check_success(dice_transaction != 0);
370
371 mi::base::Handle<nv::index::ITransformed_scene_group> group_node(
372 scene_edit->create_scene_group<nv::index::ITransformed_scene_group>());
373 check_success(group_node.is_valid_interface());
374
375 // Set up the transformation matrix
376 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(1.f);
377 // Add a translation
378 transform_mat.translate(mi::math::Vector<mi::Float32, 3>(5.0f, 0.0f, 0.0f));
379 group_node->set_transform(transform_mat);
380
381 // Add a light and a material
382 {
383 // Add a light
384 mi::base::Handle<nv::index::IDirectional_headlight> headlight(
385 scene_edit->create_attribute<nv::index::IDirectional_headlight>());
386 check_success(headlight.is_valid_interface());
387 const mi::math::Color_struct color_intensity = { 1.0f, 1.0f, 1.0f, 1.0f, };
388 headlight->set_intensity(color_intensity);
389 headlight->set_direction(mi::math::Vector<mi::Float32, 3>(1.0f, -1.0f, -1.0f));
390 const mi::neuraylib::Tag headlight_tag = dice_transaction->store_for_reference_counting(headlight.get());
391 check_success(headlight_tag.is_valid());
392 group_node->append(headlight_tag, dice_transaction);
393
394 // add material for 3D points shape (the material is only effective for 3D shape)
395 mi::base::Handle<nv::index::IPhong_gl> phong_1(scene_edit->create_attribute<nv::index::IPhong_gl>());
396 check_success(phong_1.is_valid_interface());
397 phong_1->set_ambient(mi::math::Color(0.3f, 0.3f, 0.3f, 1.0f));
398 phong_1->set_diffuse(mi::math::Color(0.4f, 0.4f, 0.4f, 1.0f));
399 phong_1->set_specular(mi::math::Color(0.4f));
400 phong_1->set_shininess(100.f);
401 const mi::neuraylib::Tag phong_1_tag = dice_transaction->store_for_reference_counting(phong_1.get());
402 check_success(phong_1_tag.is_valid());
403 group_node->append(phong_1_tag, dice_transaction);
404 }
405
406 // Add font to the scene description
407 mi::base::Handle<nv::index::IFont> font(scene_edit->create_attribute<nv::index::IFont>());
408 check_success(font.is_valid_interface());
409
410 if(font->set_file_name(m_font_fpath.c_str()))
411 {
412 INFO_LOG << "set the font path [" << m_font_fpath << "]";
413 }
414 else
415 {
416 ERROR_LOG << "Can not find the font path [" << m_font_fpath << "], "
417 << "the rendering result may not correct.";
418 }
419 font->set_font_resolution(64.0f);
420 const mi::neuraylib::Tag font_tag = dice_transaction->store_for_reference_counting(font.get());
421 check_success(font_tag.is_valid());
422 group_node->append(font_tag, dice_transaction);
423 // Add a label layout to the scene description
424 mi::base::Handle<nv::index::ILabel_layout> label_layout(scene_edit->create_attribute<nv::index::ILabel_layout>());
425 check_success(label_layout.is_valid_interface());
426 const mi::Float32 padding = 15.0f;
427 label_layout->set_padding(padding);
428 mi::math::Color_struct foreground; foreground.r = 0.9f; foreground.g = 0.8f; foreground.b = 0.8f; foreground.a = 0.8f;
429 mi::math::Color_struct background; background.r = 0.4f; background.g = 0.5f; background.b = 0.4f; background.a = 0.5f;
430 label_layout->set_color(foreground, background);
431 const mi::neuraylib::Tag label_layout_tag = dice_transaction->store_for_reference_counting(label_layout.get());
432 check_success(label_layout_tag.is_valid());
433 group_node->append(label_layout_tag, dice_transaction);
434
435 // Some constants used to setup the scene (lines, points, labels) later...
436 const mi::Float32 x_min = 0.f;
437 const mi::Float32 x_max = 400.f;
438 const mi::Float32 y_min = 0.f;
439 const mi::Float32 y_max = 400.f;
440
441 {
442 // Point coordinates and its attributes. According to the
443 // attributes, color and radius are defined.
444 std::vector< mi::math::Vector_struct< mi::Float32, 3> > point_pos_vec;
445 std::vector< mi::math::Color_struct> per_point_color;
446 std::vector< mi::Float32 > per_point_radius;
447
448 std::vector< mi::math::Vector_struct< mi::Float32, 3> > segment_vertices;
449 std::vector< mi::math::Color_struct> color_per_segment;
450 std::vector< mi::Float32 > width_per_segment;
451 mi::math::Vector_struct< mi::Float32, 3> v0; v0.x = x_min; v0.y = y_min; v0.z = 30.f;
452 mi::math::Vector_struct< mi::Float32, 3> v1; v1.x = x_max; v1.y = y_min; v1.z = 30.f;
453 mi::math::Vector_struct< mi::Float32, 3> v2; v2.x = x_max; v2.y = y_max; v2.z = 30.f;
454 mi::math::Vector_struct< mi::Float32, 3> v3; v3.x = x_min; v3.y = y_max; v3.z = 30.f;
455
456 segment_vertices.push_back(v0); segment_vertices.push_back(v1);
457 segment_vertices.push_back(v1); segment_vertices.push_back(v2);
458 segment_vertices.push_back(v2); segment_vertices.push_back(v3);
459 segment_vertices.push_back(v3); segment_vertices.push_back(v0);
460 point_pos_vec.push_back(v0);
461 point_pos_vec.push_back(v1);
462 point_pos_vec.push_back(v2);
463 point_pos_vec.push_back(v3);
464
465 mi::math::Color_struct color;
466 color.r = 0.8f; color.g = 0.8f; color.b = 0.8f; color.a = 1.f;
467 color_per_segment.push_back(color);
468 color_per_segment.push_back(color);
469 color_per_segment.push_back(color);
470 color_per_segment.push_back(color);
471 per_point_color.push_back(color); per_point_color.push_back(color);
472 per_point_color.push_back(color); per_point_color.push_back(color);
473
474 width_per_segment.push_back(2.f);
475 width_per_segment.push_back(2.f);
476 width_per_segment.push_back(2.f);
477 width_per_segment.push_back(2.f);
478 per_point_radius.push_back(3.f); per_point_radius.push_back(3.f);
479 per_point_radius.push_back(3.f); per_point_radius.push_back(3.f);
480
481 // Create line set using the scene's factory
482 mi::base::Handle<nv::index::ILine_set> line_set(scene_edit->create_shape<nv::index::ILine_set>());
483 check_success(line_set.is_valid_interface());
484 // ... set the line style such a dashed or dotted or solid linestyle (see ILine_set)
485 line_set->set_line_style(nv::index::ILine_set::LINE_STYLE_DOTTED);
486 // ... set the line type. Currently only line segments are supported (see ILine_set)
487 line_set->set_line_type(nv::index::ILine_set::LINE_TYPE_SEGMENTS);
488 // ... and the lines with colors and widths all in line segment order.
489 line_set->set_lines(&segment_vertices[0], segment_vertices.size());
490 line_set->set_colors(&color_per_segment[0], color_per_segment.size());
491 line_set->set_widths(&width_per_segment[0], width_per_segment.size());
492
493 // Add the points to the database
494 const mi::neuraylib::Tag line_set_tag = dice_transaction->store_for_reference_counting(line_set.get());
495 // if you are not registered Line_set, the next check fails.
496 check_success(line_set_tag.is_valid());
497 group_node->append(line_set_tag, dice_transaction);
498
499 // Create point set using the scene's factory.
500 mi::base::Handle<nv::index::IPoint_set> point_set(scene_edit->create_shape<nv::index::IPoint_set>());
501 check_success(point_set.is_valid_interface());
502 // ... set the point styles (see IPoint_set)
503 // ... and the vertices with colors and radii.
504 point_set->set_vertices(&point_pos_vec[0], point_pos_vec.size());
505 point_set->set_colors(&per_point_color[0], per_point_color.size());
506 point_set->set_radii(&per_point_radius[0], per_point_radius.size());
507
508 // Storing the point set in the database.
509 const mi::neuraylib::Tag point_set_scene_element_tag = dice_transaction->store_for_reference_counting(point_set.get());
510 check_success(point_set_scene_element_tag.is_valid());
511 group_node->append(point_set_scene_element_tag, dice_transaction);
512 }
513
514 {
515 mi::math::Color_struct color;
516 color.r = 0.8f; color.g = 0.8f; color.b = 0.9f; color.a = 1.f;
517 const mi::Float32 x_spacing = 25.f;
518 const mi::Float32 y_height = 10.f;
519 std::vector< mi::math::Vector_struct< mi::Float32, 3> > segment_vertices;
520 std::vector< mi::math::Color_struct> color_per_segment;
521 std::vector< mi::Float32 > width_per_segment;
522 mi::math::Vector_struct< mi::Float32, 3> v0; v0.x = x_min; v0.y = y_min; v0.z = 30.f;
523 mi::math::Vector_struct< mi::Float32, 3> v1; v1.x = x_max; v1.y = y_min; v1.z = 30.f;
524
525 for(mi::Float32 i=x_min; i<=x_max; i+=x_spacing)
526 {
527 v0.x = i; v0.y = y_max; v0.z = 30.f;
528 v1.x = i; v1.y = y_max+y_height; v1.z = 30.f;
529 if(mi::math::fmod(i,100.f) == 0) v1.y+=y_height;
530
531 segment_vertices.push_back(v0); segment_vertices.push_back(v1);
532 color_per_segment.push_back(color);
533 width_per_segment.push_back(2.f);
534
535 if(mi::math::fmod(i,100.f) == 0.f)
536 {
537 mi::base::Handle<nv::index::ILabel_2D> label(scene_edit->create_shape<nv::index::ILabel_2D>());
538 check_success(label.is_valid_interface());
539 std::stringstream sstr;
540 sstr << i;
541 label->set_text(sstr.str().c_str());
542 const mi::Float32 height = 40.f;
543 const mi::Float32 width = 40.f;
544 mi::math::Vector_struct<mi::Float32, 3> position; position.x = v1.x-(width/2); position.y = v1.y+y_height; position.z = v1.z;
545 mi::math::Vector_struct<mi::Float32, 2> right; right.x = 1.f; right.y = 0.f;
546 mi::math::Vector_struct<mi::Float32, 2> up; up.x = 0.f; up.y = 1.f;
547 // To test 'compute_label_width' create a label with width = -1 (auto width)
548 // and then call label->compute_label_width() with the current font and label layout
549 // the function will return the calculated label frame width for perfect fitting of the text
550 // taking into account the frame-height, font, text and padding.
551 label->set_geometry(position, right, up, height, width);//-1);
552 // INFO_LOG << "Label :" << sstr.str() << ", width = " << label->compute_label_width(font.get(), label_layout.get());
553 const mi::neuraylib::Tag label_tag = dice_transaction->store_for_reference_counting(label.get());
554 check_success(label_tag.is_valid());
555 group_node->append(label_tag, dice_transaction);
556 }
557 }
558
559 // Create line set using the scene's factory
560 mi::base::Handle<nv::index::ILine_set> line_set(scene_edit->create_shape<nv::index::ILine_set>());
561 check_success(line_set.is_valid_interface());
562 // ... set the line style such a dashed or dotted or solid linestyle (see ILine_set)
563 line_set->set_line_style(nv::index::ILine_set::LINE_STYLE_SOLID);
564 // ... set the line type. Currently only line segments are supported (see ILine_set)
565 line_set->set_line_type(nv::index::ILine_set::LINE_TYPE_SEGMENTS);
566 // ... and the lines with colors and widths all in line segment order.
567 line_set->set_lines(&segment_vertices[0], segment_vertices.size());
568 line_set->set_colors(&color_per_segment[0], color_per_segment.size());
569 line_set->set_widths(&width_per_segment[0], width_per_segment.size());
570
571 const mi::neuraylib::Tag line_set_tag = dice_transaction->store_for_reference_counting(line_set.get());
572 // if you are not registered Line_set, the next check fails.
573 check_success(line_set_tag.is_valid());
574 group_node->append(line_set_tag, dice_transaction);
575 }
576
577 {
578 mi::base::Handle<nv::index::ILabel_layout> label_layout(scene_edit->create_attribute<nv::index::ILabel_layout>());
579 check_success(label_layout.is_valid_interface());
580 const mi::Float32 padding = 10.f;
581 label_layout->set_padding(padding);
582 mi::math::Color_struct foreground; foreground.r = 0.9f; foreground.g = 0.95f; foreground.b = 0.99f; foreground.a = 1.0f;
583 mi::math::Color_struct background; background.r = 0.4f; background.g = 0.5f; background.b = 0.4f; background.a = 0.5f;
584 label_layout->set_color(foreground, background);
585 const mi::neuraylib::Tag label_layout_tag = dice_transaction->store_for_reference_counting(label_layout.get());
586 check_success(label_layout_tag.is_valid());
587 group_node->append(label_layout_tag, dice_transaction);
588
589 mi::base::Handle<nv::index::ILabel_3D> label(scene_edit->create_shape<nv::index::ILabel_3D>());
590 check_success(label.is_valid_interface());
591 label->set_text("slice...");
592 mi::math::Vector_struct<mi::Float32, 3> position; position.x = x_max; position.y = y_max; position.z = 30.f;
593 mi::math::Vector_struct<mi::Float32, 3> right; right.x = 0.f; right.y = -1.f; right.z = 0.f;
594 mi::math::Vector_struct<mi::Float32, 3> up; up.x = 1.f; up.y = 0.f; up.z = 0.f;
595 const mi::Float32 height = 40.f;
596 const mi::Float32 width = 120.f;
597 label->set_geometry(position, right, up, height, width);
598 const mi::neuraylib::Tag label_tag = dice_transaction->store_for_reference_counting(label.get());
599 check_success(label_tag.is_valid());
600 group_node->append(label_tag, dice_transaction);
601 }
602
603 mi::neuraylib::Tag group_node_tag = dice_transaction->store_for_reference_counting(group_node.get());
604 check_success(group_node_tag.is_valid());
605
606 // Define the root and attributes that are active globally
607 mi::base::Handle<nv::index::IDirectional_light> light_global(scene_edit->create_attribute<nv::index::IDirectional_light>());
608 check_success(light_global.is_valid_interface());
609 const mi::neuraylib::Tag light_global_tag = dice_transaction->store_for_reference_counting(light_global.get());
610 check_success(light_global_tag.is_valid());
611 scene_edit->append(light_global_tag, dice_transaction);
612
613 scene_edit->append(group_node_tag, dice_transaction);
614
615 INFO_LOG << "Hierarchical scene description creation complete.";
616 return group_node_tag;
617}
618
619//----------------------------------------------------------------------
620void Create_annotations::set_transformation(
621 const mi::neuraylib::Tag& group_node_tag,
622 mi::Uint32 frame_id,
623 mi::neuraylib::IDice_transaction* dice_transaction) const
624{
625 mi::base::Handle< nv::index::ITransformed_scene_group > group_node(
626 dice_transaction->edit<nv::index::ITransformed_scene_group>(group_node_tag));
627 check_success(group_node.is_valid_interface());
628
629 // Set up the transformation matrix
630 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(1.f);
631 // Add a translation
632 mi::Float32 z = static_cast<mi::Float32>(frame_id) * 10.0f;
633 transform_mat.translate(mi::math::Vector<mi::Float32, 3>(5.0f, 0.0f, z));
634 group_node->set_transform(transform_mat);
635}
636
637//----------------------------------------------------------------------
638void Create_annotations::setup_camera(
639 nv::index::IPerspective_camera* cam) const
640{
641 check_success(cam != 0);
642
643 // Set the camera parameters to see the whole scene
644 mi::math::Vector< mi::Float32, 3 > const from( 100.0f, 254.0f, 550.0f);
645 mi::math::Vector< mi::Float32, 3 > const to ( 255.0f, 255.0f, -255.0f);
646 mi::math::Vector< mi::Float32, 3 > const up ( 0.0f, 1.0f, 0.0f);
647 mi::math::Vector<mi::Float32, 3> viewdir = to - from;
648 viewdir.normalize();
649
650 cam->set(from, viewdir, up);
651 cam->set_aperture(0.033f);
652 cam->set_aspect(1.0f);
653 cam->set_focal(0.03f);
654 cam->set_clip_min(10.0f);
655 cam->set_clip_max(5000.0f);
656}
657
658//----------------------------------------------------------------------
659nv::index::IFrame_results* Create_annotations::render_frame(
660 const std::string& output_fname) const
661{
662 check_success(m_index_rendering.is_valid_interface());
663
664 // set output filename, empty string is valid
665 m_image_file_canvas->set_rgba_file_name(output_fname.c_str());
666
667 check_success(m_session_tag.is_valid());
668
669 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
670 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
671 check_success(dice_transaction.is_valid_interface());
672
673 m_index_session->update(m_session_tag, dice_transaction.get());
674
675 mi::base::Handle<nv::index::IFrame_results> frame_results(
676 m_index_rendering->render(
677 m_session_tag,
678 m_image_file_canvas.get(),
679 dice_transaction.get()));
680 check_success(frame_results.is_valid_interface());
681
682 dice_transaction->commit();
683
684 frame_results->retain();
685 return frame_results.get();
686}
687
688//----------------------------------------------------------------------
689// This example shows how to create annotation shapes in the scene.
690int main(int argc, const char* argv[])
691{
692 nv::index::app::String_dict sdict;
693 sdict.insert("dice::verbose", "3"); // log level
694 sdict.insert("dice::network::mode", "OFF"); // network mode
695 sdict.insert("font_fpath", "/usr/share/fonts/dejavu/DejaVuSans.ttf"); // font path
696 sdict.insert("outfname", "frame_create_annotations"); // output file base name
697 sdict.insert("verify_image_fname_sequence", ""); // for unit test
698 sdict.insert("unittest", "0"); // default mode
699 sdict.insert("is_dump_comparison_image_when_failed", "1"); // default: dump images when failed.
700 sdict.insert("is_call_from_test", "0"); // default: not call from make check.
701
702 // index setting
703 sdict.insert("index::config::set_monitor_performance_values", "yes");
704 sdict.insert("index::service", "rendering_and_compositing");
705 sdict.insert("index::cuda_debug_checks", "no");
706
707 // application_layer component loading
708 sdict.insert("index::app::components::application_layer::component_name_list",
709 "canvas_infrastructure image io");
710
711 // Initialize application
712 Create_annotations create_annotations;
713 create_annotations.initialize(argc, argv, sdict);
714 check_success(create_annotations.is_initialized());
715
716 // launch the application. creating the scene and rendering.
717 const mi::Sint32 exit_code = create_annotations.launch();
718 INFO_LOG << "Shutting down ...";
719
720 return exit_code;
721}
virtual bool evaluate_options(nv::index::app::String_dict &sdict) CPP11_OVERRIDE
virtual bool initialize_networking(mi::neuraylib::INetwork_configuration *network_configuration, nv::index::app::String_dict &options) CPP11_OVERRIDE
int main(int argc, const char *argv[])
#define check_success(expr)