10#include "utility/example_shared.h"
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>
24#include <nv/index/app/string_dict.h>
25#include <nv/index/app/index_connect.h>
27#include "utility/canvas_utility.h"
35 public nv::index::app::Index_connect
56 virtual bool evaluate_options(nv::index::app::String_dict& sdict) CPP11_OVERRIDE;
59 mi::neuraylib::INetwork_configuration* network_configuration,
60 nv::index::app::String_dict& options) CPP11_OVERRIDE
65 const bool is_unittest = nv::index::app::get_bool(options.get(
"unittest"));
68 info_cout(
"NETWORK: disabled networking mode.", options);
69 network_configuration->set_mode(mi::neuraylib::INetwork_configuration:
:MODE_OFF);
73 return initialize_networking_as_default_udp(network_configuration, options);
82 mi::neuraylib::Tag create_scene(
83 nv::index::IScene* scene_edit,
84 mi::neuraylib::IDice_transaction* dice_transaction);
91 void set_transformation(
92 const mi::neuraylib::Tag& group_node_tag,
94 mi::neuraylib::IDice_transaction* dice_transaction)
const;
99 void setup_camera(nv::index::IPerspective_camera* cam)
const;
105 nv::index::IFrame_results* render_frame(
106 const std::string& output_fname)
const;
109 mi::neuraylib::Tag m_session_tag;
111 mi::base::Handle<nv::index::ICluster_configuration> m_cluster_configuration;
113 mi::base::Handle<nv::index::app::canvas_infrastructure::IIndex_image_file_canvas> m_image_file_canvas;
115 std::string m_outfname;
117 std::string m_verify_image_fname_sequence;
118 std::string m_font_fpath;
124 mi::Sint32 exit_code = 0;
128 m_cluster_configuration =
129 get_index_interface()->get_api_component<nv::index::ICluster_configuration>();
133 m_image_file_canvas = create_image_file_canvas(get_application_layer_interface());
138 check_success(is_local_host_joined(m_cluster_configuration.get()));
140 mi::neuraylib::Tag scene_group_tag = mi::neuraylib:
:NULL_TAG;
141 mi::neuraylib::Tag camera_tag = mi::neuraylib:
:NULL_TAG;
145 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
146 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
150 m_session_tag = m_index_session->create_session(dice_transaction.get());
152 mi::base::Handle<const nv::index::ISession> session(
153 dice_transaction->access<
const nv::index::ISession>(m_session_tag));
156 mi::base::Handle< nv::index::IScene > scene_edit(
157 dice_transaction->edit<nv::index::IScene>(session->get_scene()));
164 scene_group_tag = create_scene(scene_edit.get(), dice_transaction.get());
170 mi::base::Handle< nv::index::IPerspective_camera > cam(
171 scene_edit->create_camera<nv::index::IPerspective_camera>());
173 setup_camera(cam.get());
174 camera_tag = dice_transaction->store(cam.get());
177 const mi::math::Vector<mi::Uint32,2> buffer_resolution(512, 512);
178 m_image_file_canvas->set_resolution(buffer_resolution);
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, },
187 const mi::math::Bbox< mi::Float32, 3 > xyz_roi(xyz_roi_st);
189 scene_edit->set_clipped_bounding_box(xyz_roi_st);
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
199 scene_edit->set_transform_matrix(transform_mat);
203 scene_edit->set_camera(camera_tag);
205 dice_transaction->commit();
210 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
211 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
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())
218 verify_image_fname << m_verify_image_fname_sequence <<
"000.ppm";
220 mi::base::Handle<nv::index::IFrame_results> frame_results(render_frame(fname));
222 const mi::base::Handle<nv::index::IError_set> err_set(frame_results->get_error_set());
223 if (err_set->any_errors())
225 std::ostringstream os;
227 const mi::Uint32 nb_err = err_set->get_nb_errors();
228 for (mi::Uint32 e = 0; e < nb_err; ++e)
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();
235 ERROR_LOG <<
"IIndex_rendering rendering call failed with the following error(s): " <<
'\n'
240 if (!(verify_canvas_result(get_application_layer_interface(),
241 m_image_file_canvas.get(), verify_image_fname.str(), get_options())))
246 dice_transaction->commit();
249 for(mi::Uint32 i = 1; i < 20; ++i)
252 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
253 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
256 set_transformation(scene_group_tag, i, dice_transaction.get());
258 dice_transaction->commit();
261 const std::string fname = get_output_file_name(m_outfname, i);
263 std::stringstream verify_image_fname;
264 if(!m_verify_image_fname_sequence.empty())
266 const size_t BUFSZ = 64;
268 snprintf(buf, BUFSZ - 1,
"%03d.ppm", i);
269 verify_image_fname << m_verify_image_fname_sequence << buf;
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())
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)
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();
285 ERROR_LOG <<
"IIndex_rendering rendering call failed with the following error(s): " <<
'\n'
291 if (!(verify_canvas_result(get_application_layer_interface(),
292 m_image_file_canvas.get(), verify_image_fname.str(), get_options())))
307 const std::string com_name = sdict.get(
"command:",
"<unknown_command>");
308 m_is_unittest = nv::index::app::get_bool(sdict.get(
"unittest",
"false"));
312 if (nv::index::app::get_bool(sdict.get(
"is_call_from_test",
"false")))
314 sdict.insert(
"is_dump_comparison_image_when_failed",
"0");
316 sdict.insert(
"outfname",
"");
317 sdict.insert(
"dice::verbose",
"2");
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");
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);
330 if(sdict.is_defined(
"h"))
333 <<
"info: Usage: " << com_name <<
" [option]\n"
335 <<
" printout this message\n"
337 <<
" [-dice::verbose severity_level]\n"
338 <<
" verbose severity level (3 is info.). (default: "
339 << sdict.get(
"dice::verbose") <<
")\n"
341 <<
" [-font_fpath FONT_FILE_PATH]\n"
342 <<
" font file path. (default: " << m_font_fpath <<
")\n"
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"
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"
353 <<
" [-unittest bool]\n"
354 <<
" when true, unit test mode (create smaller volume). "
355 <<
"(default: " << sdict.get(
"unittest") <<
")"
364mi::neuraylib::Tag Create_annotations::create_scene(
365 nv::index::IScene* scene_edit,
366 mi::neuraylib::IDice_transaction* dice_transaction)
371 mi::base::Handle<nv::index::ITransformed_scene_group> group_node(
372 scene_edit->create_scene_group<nv::index::ITransformed_scene_group>());
376 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(1.f);
378 transform_mat.translate(mi::math::Vector<mi::Float32, 3>(5.0f, 0.0f, 0.0f));
379 group_node->set_transform(transform_mat);
384 mi::base::Handle<nv::index::IDirectional_headlight> headlight(
385 scene_edit->create_attribute<nv::index::IDirectional_headlight>());
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());
392 group_node->append(headlight_tag, dice_transaction);
395 mi::base::Handle<nv::index::IPhong_gl> phong_1(scene_edit->create_attribute<nv::index::IPhong_gl>());
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());
403 group_node->append(phong_1_tag, dice_transaction);
407 mi::base::Handle<nv::index::IFont> font(scene_edit->create_attribute<nv::index::IFont>());
410 if(font->set_file_name(m_font_fpath.c_str()))
412 INFO_LOG <<
"set the font path [" << m_font_fpath <<
"]";
416 ERROR_LOG <<
"Can not find the font path [" << m_font_fpath <<
"], "
417 <<
"the rendering result may not correct.";
419 font->set_font_resolution(64.0f);
420 const mi::neuraylib::Tag font_tag = dice_transaction->store_for_reference_counting(font.get());
422 group_node->append(font_tag, dice_transaction);
424 mi::base::Handle<nv::index::ILabel_layout> label_layout(scene_edit->create_attribute<nv::index::ILabel_layout>());
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());
433 group_node->append(label_layout_tag, dice_transaction);
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;
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;
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;
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);
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);
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);
482 mi::base::Handle<nv::index::ILine_set> line_set(scene_edit->create_shape<nv::index::ILine_set>());
485 line_set->set_line_style(nv::index::ILine_set:
:LINE_STYLE_DOTTED);
487 line_set->set_line_type(nv::index::ILine_set:
:LINE_TYPE_SEGMENTS);
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());
494 const mi::neuraylib::Tag line_set_tag = dice_transaction->store_for_reference_counting(line_set.get());
497 group_node->append(line_set_tag, dice_transaction);
500 mi::base::Handle<nv::index::IPoint_set> point_set(scene_edit->create_shape<nv::index::IPoint_set>());
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());
509 const mi::neuraylib::Tag point_set_scene_element_tag = dice_transaction->store_for_reference_counting(point_set.get());
511 group_node->append(point_set_scene_element_tag, dice_transaction);
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;
525 for(mi::Float32 i=x_min; i<=x_max; i+=x_spacing)
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;
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);
535 if(mi::math::fmod(i,100.f) == 0.f)
537 mi::base::Handle<nv::index::ILabel_2D> label(scene_edit->create_shape<nv::index::ILabel_2D>());
539 std::stringstream sstr;
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;
551 label->set_geometry(position, right, up, height, width);
553 const mi::neuraylib::Tag label_tag = dice_transaction->store_for_reference_counting(label.get());
555 group_node->append(label_tag, dice_transaction);
560 mi::base::Handle<nv::index::ILine_set> line_set(scene_edit->create_shape<nv::index::ILine_set>());
563 line_set->set_line_style(nv::index::ILine_set:
:LINE_STYLE_SOLID);
565 line_set->set_line_type(nv::index::ILine_set:
:LINE_TYPE_SEGMENTS);
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());
571 const mi::neuraylib::Tag line_set_tag = dice_transaction->store_for_reference_counting(line_set.get());
574 group_node->append(line_set_tag, dice_transaction);
578 mi::base::Handle<nv::index::ILabel_layout> label_layout(scene_edit->create_attribute<nv::index::ILabel_layout>());
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());
587 group_node->append(label_layout_tag, dice_transaction);
589 mi::base::Handle<nv::index::ILabel_3D> label(scene_edit->create_shape<nv::index::ILabel_3D>());
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());
600 group_node->append(label_tag, dice_transaction);
603 mi::neuraylib::Tag group_node_tag = dice_transaction->store_for_reference_counting(group_node.get());
607 mi::base::Handle<nv::index::IDirectional_light> light_global(scene_edit->create_attribute<nv::index::IDirectional_light>());
609 const mi::neuraylib::Tag light_global_tag = dice_transaction->store_for_reference_counting(light_global.get());
611 scene_edit->append(light_global_tag, dice_transaction);
613 scene_edit->append(group_node_tag, dice_transaction);
615 INFO_LOG <<
"Hierarchical scene description creation complete.";
616 return group_node_tag;
620void Create_annotations::set_transformation(
621 const mi::neuraylib::Tag& group_node_tag,
623 mi::neuraylib::IDice_transaction* dice_transaction)
const
625 mi::base::Handle< nv::index::ITransformed_scene_group > group_node(
626 dice_transaction->edit<nv::index::ITransformed_scene_group>(group_node_tag));
630 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(1.f);
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);
638void Create_annotations::setup_camera(
639 nv::index::IPerspective_camera* cam)
const
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;
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);
659nv::index::IFrame_results* Create_annotations::render_frame(
660 const std::string& output_fname)
const
665 m_image_file_canvas->set_rgba_file_name(output_fname.c_str());
669 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
670 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
673 m_index_session->update(m_session_tag, dice_transaction.get());
675 mi::base::Handle<nv::index::IFrame_results> frame_results(
676 m_index_rendering->render(
678 m_image_file_canvas.get(),
679 dice_transaction.get()));
682 dice_transaction->commit();
684 frame_results->retain();
685 return frame_results.get();
690int main(
int argc,
const char* argv[])
692 nv::index::app::String_dict sdict;
693 sdict.insert(
"dice::verbose",
"3");
694 sdict.insert(
"dice::network::mode",
"OFF");
695 sdict.insert(
"font_fpath",
"/usr/share/fonts/dejavu/DejaVuSans.ttf");
696 sdict.insert(
"outfname",
"frame_create_annotations");
697 sdict.insert(
"verify_image_fname_sequence",
"");
698 sdict.insert(
"unittest",
"0");
699 sdict.insert(
"is_dump_comparison_image_when_failed",
"1");
700 sdict.insert(
"is_call_from_test",
"0");
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");
708 sdict.insert(
"index::app::components::application_layer::component_name_list",
709 "canvas_infrastructure image io");
713 create_annotations.initialize(argc, argv, sdict);
717 const mi::Sint32 exit_code = create_annotations.
launch();
718 INFO_LOG <<
"Shutting down ...";
virtual bool evaluate_options(nv::index::app::String_dict &sdict) CPP11_OVERRIDE
virtual ~Create_annotations()
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)