10#include "utility/example_shared.h"
12#include <nv/index/iindex.h>
13#include <nv/index/isession.h>
14#include <nv/index/iscene.h>
15#include <nv/index/icamera.h>
16#include <nv/index/ipolygon.h>
18#include <nv/index/app/index_connect.h>
19#include <nv/index/app/string_dict.h>
21#include "utility/canvas_utility.h"
28 public nv::index::app::Index_connect
48 virtual bool evaluate_options(nv::index::app::String_dict& sdict) CPP11_OVERRIDE;
51 mi::neuraylib::INetwork_configuration* network_configuration,
52 nv::index::app::String_dict& options) CPP11_OVERRIDE
57 const bool is_unittest = nv::index::app::get_bool(options.get(
"unittest"));
60 info_cout(
"NETWORK: disabled networking mode.", options);
61 network_configuration->set_mode(mi::neuraylib::INetwork_configuration:
:MODE_OFF);
65 return initialize_networking_as_default_udp(network_configuration, options);
71 nv::index::IScene* scene_edit,
72 mi::neuraylib::IDice_transaction* dice_transaction)
const;
77 void setup_camera(nv::index::IPerspective_camera* cam)
const;
82 nv::index::IFrame_results* render_frame(
const std::string& output_fname)
const;
85 mi::neuraylib::Tag m_session_tag;
87 mi::base::Handle<nv::index::ICluster_configuration> m_cluster_configuration;
89 mi::base::Handle<nv::index::app::canvas_infrastructure::IIndex_image_file_canvas> m_image_file_canvas;
91 std::string m_outfname;
93 std::string m_verify_image_fname;
99 mi::Sint32 exit_code = 0;
103 m_cluster_configuration =
104 get_index_interface()->get_api_component<nv::index::ICluster_configuration>();
108 m_image_file_canvas = create_image_file_canvas(get_application_layer_interface());
113 check_success(is_local_host_joined(m_cluster_configuration.get()));
117 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
118 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
123 m_index_session->create_session(dice_transaction.get());
125 mi::base::Handle< nv::index::ISession const > session(
126 dice_transaction->access< nv::index::ISession
const >(
130 mi::base::Handle< nv::index::IScene > scene_edit(
131 dice_transaction->edit< nv::index::IScene >(session->get_scene()));
138 check_success(create_polygons(scene_edit.get(), dice_transaction.get()));
143 mi::base::Handle< nv::index::IPerspective_camera > cam(
144 scene_edit->create_camera<nv::index::IPerspective_camera>());
146 setup_camera(cam.get());
147 const mi::neuraylib::Tag camera_tag = dice_transaction->store(cam.get());
150 const mi::math::Vector<mi::Uint32, 2> buffer_resolution(1025, 1024);
151 m_image_file_canvas->set_resolution(buffer_resolution);
154 mi::math::Bbox_struct< mi::Float32, 3 >
const xyz_roi_st = {
155 { 0.0f, 0.0f, 0.0f, },
156 { 500.0f, 500.0f, 500.0f, },
160 const mi::math::Bbox< mi::Float32, 3 > xyz_roi(xyz_roi_st);
162 scene_edit->set_clipped_bounding_box(xyz_roi_st);
166 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(
167 1.0f, 0.0f, 0.0f, 0.0f,
168 0.0f, 1.0f, 0.0f, 0.0f,
169 0.0f, 0.0f, -1.0f, 0.0f,
170 0.0f, 0.0f, 0.0f, 1.0f
172 scene_edit->set_transform_matrix(transform_mat);
176 scene_edit->set_camera(camera_tag);
178 dice_transaction->commit();
183 const std::string fname = get_output_file_name(m_outfname, 0);
184 mi::base::Handle<nv::index::IFrame_results> frame_results(render_frame(fname));
185 const mi::base::Handle<nv::index::IError_set> err_set(frame_results->get_error_set());
186 if (err_set->any_errors())
188 std::ostringstream os;
189 const mi::Uint32 nb_err = err_set->get_nb_errors();
190 for (mi::Uint32 e = 0; e < nb_err; ++e)
192 if (e != 0) os <<
'\n';
193 const mi::base::Handle<nv::index::IError> err(err_set->get_error(e));
194 os << err->get_error_string();
197 ERROR_LOG <<
"IIndex_rendering rendering call failed with the following error(s): " <<
'\n'
204 if (!(verify_canvas_result(get_application_layer_interface(),
205 m_image_file_canvas.get(), m_verify_image_fname, get_options())))
218 const std::string com_name = sdict.get(
"command:",
"<unknown_command>");
219 m_is_unittest = nv::index::app::get_bool(sdict.get(
"unittest",
"false"));
223 if (nv::index::app::get_bool(sdict.get(
"is_call_from_test",
"false")))
225 sdict.insert(
"is_dump_comparison_image_when_failed",
"0");
227 sdict.insert(
"outfname",
"");
228 sdict.insert(
"dice::verbose",
"2");
233 m_outfname = sdict.get(
"outfname",
"");
234 m_verify_image_fname = sdict.get(
"verify_image_fname",
"");
236 info_cout(std::string(
"running ") + com_name, sdict);
237 info_cout(
"outfname = [" + m_outfname +
238 "], verify_image_fname = [" + m_verify_image_fname +
239 "], dice::verbose = " + sdict.get(
"dice::verbose"), sdict);
242 if (sdict.is_defined(
"h"))
245 <<
"info: Usage: " << com_name <<
" [option]\n"
247 <<
" printout this message\n"
248 <<
" [-dice::verbose severity_level]\n"
249 <<
" verbose severity level (3 is info.). (default: " + sdict.get(
"dice::verbose")
252 <<
" [-outfname string]\n"
253 <<
" output ppm file base name. When empty, no output.\n"
254 <<
" A frame number and extension (.ppm) will be added.\n"
255 <<
" (default: [" << sdict.get(
"outfname") <<
"])\n"
256 <<
" [-verify_image_fname [image_fname]]\n"
257 <<
" when image_fname exist, verify the rendering image. (default: ["
258 << m_verify_image_fname <<
"])\n"
260 <<
" [-unittest bool]\n"
261 <<
" when true, unit test mode (create smaller volume). "
262 <<
"(default: " << m_is_unittest <<
")\n"
264 <<
" [-is_dump_comparison_image_when_failed bool]\n"
265 <<
" when true, dump the comparison images and generate the diff image for debug.\n"
266 <<
" You can also force to dump and set the filename.\n"
267 <<
" (default: " << sdict.get(
"is_dump_comparison_image_when_failed") <<
")"
276bool Create_polygons::create_polygons(
277 nv::index::IScene* scene_edit,
278 mi::neuraylib::IDice_transaction* dice_transaction)
const
283 mi::base::Handle<nv::index::ITransformed_scene_group> group_node(
284 scene_edit->create_scene_group<nv::index::ITransformed_scene_group>());
289 mi::base::Handle<nv::index::IPolygon> poly(scene_edit->create_shape<nv::index::IPolygon>());
291 mi::math::Vector_struct< mi::Float32, 3> pos;
296 mi::Uint32 num_vertices = 5;
297 mi::math::Vector_struct< mi::Float32, 2> vertices[5];
299 const mi::Float32 delta_phi = (2.0f*
static_cast<mi::Float32
>(M_PI))/
static_cast<mi::Float32
>(num_vertices);
300 const mi::Float32 radius = 120.f;
302 for (mi::Uint32 i=0; i<num_vertices; i++)
304 mi::math::sincos(delta_phi*i, vertices[i].x, vertices[i].y);
305 vertices[i].x *= radius;
306 vertices[i].y *= radius;
310 if (poly->set_geometry(vertices, num_vertices, pos))
312 INFO_LOG <<
"Pentagone created";
314 mi::math::Color_struct fill_color = {0.5f, 1.0f, 0.f, 1.f};
316 poly->set_fill_style(fill_color, nv::index::IPolygon:
:FILL_SOLID);
318 const mi::neuraylib::Tag poly_tag = dice_transaction->store_for_reference_counting(poly.get());
320 group_node->append(poly_tag, dice_transaction);
324 INFO_LOG <<
"Pentagone not created, bad shaped";
330 mi::base::Handle<nv::index::IPolygon> poly(scene_edit->create_shape<nv::index::IPolygon>());
332 mi::math::Vector_struct< mi::Float32, 3> pos;
337 mi::Uint32 num_vertices = 5;
338 mi::math::Vector_struct< mi::Float32, 2> vertices[10];
340 const mi::Float32 delta_phi = (2.0f *
static_cast<mi::Float32
>(M_PI)) /
static_cast<mi::Float32
>(num_vertices);
341 const mi::Float32 radius = 120.f;
342 const mi::Float32 inner_radius = 80.f;
344 for (mi::Uint32 i=0; i<num_vertices; i++)
346 mi::math::sincos(delta_phi*i, vertices[2*i].x, vertices[2*i].y);
347 vertices[2*i].x *= radius;
348 vertices[2*i].y *= radius;
350 mi::math::sincos(delta_phi*(i+0.5f), vertices[2*i + 1].x, vertices[2*i + 1].y);
351 vertices[2*i + 1].x *= inner_radius;
352 vertices[2*i + 1].y *= inner_radius;
356 if (poly->set_geometry(vertices, 2*num_vertices, pos))
358 INFO_LOG <<
"Star created";
360 mi::math::Color_struct fill_color = {0.39f, 0.58f, 0.93f, 1.f};
362 poly->set_fill_style(fill_color, nv::index::IPolygon:
:FILL_SOLID);
364 const mi::neuraylib::Tag poly_tag = dice_transaction->store_for_reference_counting(poly.get());
366 group_node->append(poly_tag, dice_transaction);
370 INFO_LOG <<
"Star not created, bad shaped";
376 mi::base::Handle<nv::index::IPolygon> poly(scene_edit->create_shape<nv::index::IPolygon>());
378 mi::math::Vector_struct< mi::Float32, 3> pos;
383 mi::Uint32 num_vertices = 8;
384 mi::math::Vector_struct< mi::Float32, 2> vertices[16];
386 const mi::Float32 delta_phi = (2.0f*
static_cast<mi::Float32
>(M_PI)) /
static_cast<mi::Float32
>(num_vertices);
387 const mi::Float32 radius = 120.f;
388 const mi::Float32 inner_radius = 60.f;
390 for (mi::Uint32 i=0; i<num_vertices; i++)
392 mi::math::sincos(delta_phi*i, vertices[2*i].x, vertices[2*i].y);
393 vertices[2*i].x *= radius;
394 vertices[2*i].y *= radius;
396 mi::math::sincos(delta_phi*(i+0.5f), vertices[2*i + 1].x, vertices[2*i + 1].y);
397 vertices[2*i + 1].x *= inner_radius;
398 vertices[2*i + 1].y *= inner_radius;
402 if (poly->set_geometry(vertices, 2*num_vertices, pos))
404 INFO_LOG <<
"Star created";
406 mi::math::Color_struct fill_color = {0.39f, 0.58f, 0.93f, 1.f};
408 poly->set_fill_style(fill_color, nv::index::IPolygon:
:FILL_SOLID);
410 const mi::neuraylib::Tag poly_tag = dice_transaction->store_for_reference_counting(poly.get());
412 group_node->append(poly_tag, dice_transaction);
416 INFO_LOG <<
"Star not created, bad shaped";
422 mi::base::Handle<nv::index::IPolygon> poly(scene_edit->create_shape<nv::index::IPolygon>());
424 mi::math::Vector_struct< mi::Float32, 3> pos;
429 mi::Uint32 num_vertices = 12;
430 mi::math::Vector_struct< mi::Float32, 2> vertices[12];
432 vertices[0].x = -60.f;
433 vertices[0].y = 120.f;
434 vertices[1].x = -60.f;
435 vertices[1].y = 60.f;
436 vertices[2].x = -120.f;
437 vertices[2].y = 60.f;
438 vertices[3].x = -120.f;
439 vertices[3].y = -30.f;
440 vertices[4].x = -60.f;
441 vertices[4].y = -30.f;
442 vertices[5].x = -60.f;
443 vertices[5].y = -120.f;
444 vertices[6].x = 60.f;
445 vertices[6].y = -120.f;
446 vertices[7].x = 60.f;
447 vertices[7].y = -30.f;
448 vertices[8].x = 120.f;
449 vertices[8].y = -30.f;
450 vertices[9].x = 120.f;
451 vertices[9].y = 60.f;
452 vertices[10].x = 60.f;
453 vertices[10].y = 60.f;
454 vertices[11].x = 60.f;
455 vertices[11].y = 120.f;
458 if (poly->set_geometry(vertices, num_vertices, pos))
460 INFO_LOG <<
"Cross created";
462 mi::math::Color_struct fill_color = {0.8f, 0.8f, 0.8f, 1.f};
464 poly->set_fill_style(fill_color, nv::index::IPolygon:
:FILL_SOLID);
466 const mi::neuraylib::Tag poly_tag = dice_transaction->store_for_reference_counting(poly.get());
468 group_node->append(poly_tag, dice_transaction);
472 INFO_LOG <<
"Cross not created, bad shaped";
478 mi::base::Handle<nv::index::IPolygon> poly(scene_edit->create_shape<nv::index::IPolygon>());
480 mi::math::Vector_struct< mi::Float32, 3> pos;
485 mi::Uint32 num_vertices = 10;
486 mi::math::Vector_struct< mi::Float32, 2> vertices[10];
488 vertices[0].x = -100.f;
489 vertices[0].y = -100.f;
490 vertices[1].x = -20.f;
491 vertices[1].y = -100.f;
492 vertices[2].x = -20.f;
493 vertices[2].y = -20.f;
494 vertices[3].x = 20.f;
495 vertices[3].y = -100.f;
496 vertices[4].x = 100.f;
497 vertices[4].y = -100.f;
498 vertices[5].x = 100.f;
499 vertices[5].y = 100.f;
500 vertices[6].x = 20.f;
501 vertices[6].y = 100.f;
502 vertices[7].x = 20.f;
503 vertices[7].y = 20.f;
504 vertices[8].x = -20.f;
505 vertices[8].y = 100.f;
506 vertices[9].x = -100.f;
507 vertices[9].y = 100.f;
510 if (poly->set_geometry(vertices, num_vertices, pos))
512 INFO_LOG <<
"N created";
514 mi::math::Color_struct fill_color = {0.0f, 1.0f, 0.f, 1.f};
516 poly->set_fill_style(fill_color, nv::index::IPolygon:
:FILL_SOLID);
518 const mi::neuraylib::Tag poly_tag = dice_transaction->store_for_reference_counting(poly.get());
520 group_node->append(poly_tag, dice_transaction);
524 INFO_LOG <<
"N not created, bad shaped";
530 mi::base::Handle<nv::index::IPolygon> poly(scene_edit->create_shape<nv::index::IPolygon>());
532 mi::math::Vector_struct< mi::Float32, 3> pos;
537 mi::Uint32 num_vertices = 6;
538 mi::math::Vector_struct< mi::Float32, 2> vertices[6];
543 vertices[1].y = 40.f;
544 vertices[2].x = -80.f;
545 vertices[2].y = -40.f;
546 vertices[3].x = -20.f;
549 vertices[4].y = -40.f;
550 vertices[5].x = 80.f;
551 vertices[5].y = 60.f;
554 if (poly->set_geometry(vertices, num_vertices, pos))
556 INFO_LOG <<
"Flash created";
558 mi::math::Color_struct fill_color = {0.0f, 1.0f, 0.f, 1.f};
560 poly->set_fill_style(fill_color, nv::index::IPolygon:
:FILL_SOLID);
562 const mi::neuraylib::Tag poly_tag = dice_transaction->store_for_reference_counting(poly.get());
564 group_node->append(poly_tag, dice_transaction);
568 INFO_LOG <<
"Flash not created, bad shaped";
574 mi::base::Handle<nv::index::IPolygon> poly(scene_edit->create_shape<nv::index::IPolygon>());
576 mi::math::Vector_struct< mi::Float32, 3> pos;
581 mi::Uint32 num_vertices = 26;
582 mi::math::Vector_struct< mi::Float32, 2> vertices[26];
585 vertices[0].y = -40.f;
586 vertices[1].x = 120.f;
587 vertices[1].y = -60.f;
588 vertices[2].x = 120.f;
589 vertices[2].y = -20.f;
590 vertices[3].x = 100.f;
591 vertices[3].y = -20.f;
592 vertices[4].x = 120.f;
593 vertices[4].y = 40.f;
594 vertices[5].x = 80.f;
595 vertices[5].y = 40.f;
596 vertices[6].x = 60.f;
597 vertices[6].y = -20.f;
598 vertices[7].x = 40.f;
599 vertices[7].y = -20.f;
600 vertices[8].x = 50.f;
601 vertices[8].y = 140.f;
602 vertices[9].x = 20.f;
603 vertices[9].y = 160.f;
604 vertices[10].x = -10.f;
605 vertices[10].y = 140.f;
606 vertices[11].x = 0.f;
607 vertices[11].y = 40.f;
608 vertices[12].x = -40.f;
609 vertices[12].y = 50.f;
610 vertices[13].x = -60.f;
611 vertices[13].y = 20.f;
612 vertices[14].x = -60.f;
613 vertices[14].y = -20.f;
614 vertices[15].x = -80.f;
615 vertices[15].y = -20.f;
616 vertices[16].x = -80.f;
617 vertices[16].y = 40.f;
618 vertices[17].x = -100.f;
619 vertices[17].y = 50.f;
620 vertices[18].x = -100.f;
621 vertices[18].y = -20.f;
622 vertices[19].x = -120.f;
623 vertices[19].y = -20.f;
624 vertices[20].x = -140.f;
625 vertices[20].y = -20.f;
626 vertices[21].x = -120.f;
627 vertices[21].y = 80.f;
628 vertices[22].x = -180.f;
629 vertices[22].y = 80.f;
630 vertices[23].x = -160.f;
631 vertices[23].y = -20.f;
632 vertices[24].x = -180.f;
633 vertices[24].y = -20.f;
634 vertices[25].x = -180.f;
635 vertices[25].y = -40.f;
639 if (poly->set_geometry(vertices, num_vertices, pos))
641 INFO_LOG <<
"Irregular shape created";
643 mi::math::Color_struct fill_color = {1.0f, 0.25f, 0.f, 1.f};
645 poly->set_fill_style(fill_color, nv::index::IPolygon:
:FILL_SOLID);
647 const mi::neuraylib::Tag poly_tag = dice_transaction->store_for_reference_counting(poly.get());
649 group_node->append(poly_tag, dice_transaction);
653 INFO_LOG <<
"Irregular shape not created, bad shaped";
657 mi::neuraylib::Tag group_node_tag = dice_transaction->store_for_reference_counting(group_node.get());
659 scene_edit->append(group_node_tag, dice_transaction);
665void Create_polygons::setup_camera(nv::index::IPerspective_camera* cam)
const
670 mi::math::Vector< mi::Float32, 3 >
const from( 254.0f, 254.0f, 550.0f);
671 mi::math::Vector< mi::Float32, 3 >
const to ( 255.0f, 255.0f, -255.0f);
672 mi::math::Vector< mi::Float32, 3 >
const up ( 0.0f, 1.0f, 0.0f);
673 mi::math::Vector<mi::Float32, 3> viewdir = to - from;
676 cam->set(from, viewdir, up);
677 cam->set_aperture(0.033f);
678 cam->set_aspect(1.0f);
679 cam->set_focal(0.03f);
680 cam->set_clip_min(10.0f);
681 cam->set_clip_max(5000.0f);
685nv::index::IFrame_results* Create_polygons::render_frame(
686 const std::string& output_fname)
const
691 m_image_file_canvas->set_rgba_file_name(output_fname.c_str());
695 mi::base::Handle<mi::neuraylib::IDice_transaction> dice_transaction(
696 m_global_scope->create_transaction<mi::neuraylib::IDice_transaction>());
699 m_index_session->update(m_session_tag, dice_transaction.get());
701 mi::base::Handle<nv::index::IFrame_results> frame_results(
702 m_index_rendering->render(
704 m_image_file_canvas.get(),
705 dice_transaction.get()));
708 dice_transaction->commit();
710 frame_results->retain();
711 return frame_results.get();
716int main(
int argc,
const char* argv[])
718 nv::index::app::String_dict sdict;
719 sdict.insert(
"dice::verbose",
"3");
720 sdict.insert(
"outfname",
"frame_create_polygons");
721 sdict.insert(
"verify_image_fname",
"");
722 sdict.insert(
"unittest",
"0");
723 sdict.insert(
"is_dump_comparison_image_when_failed",
"1");
724 sdict.insert(
"is_call_from_test",
"0");
727 sdict.insert(
"dice::network::mode",
"OFF");
730 sdict.insert(
"index::config::set_monitor_performance_values",
"true");
731 sdict.insert(
"index::service",
"rendering_and_compositing");
732 sdict.insert(
"index::cuda_debug_checks",
"false");
735 sdict.insert(
"index::app::components::application_layer::component_name_list",
736 "canvas_infrastructure image io");
740 create_polygons.initialize(argc, argv, sdict);
744 const mi::Sint32 exit_code = create_polygons.
launch();
745 INFO_LOG <<
"Shutting down ...";
virtual ~Create_polygons()
virtual bool initialize_networking(mi::neuraylib::INetwork_configuration *network_configuration, nv::index::app::String_dict &options) CPP11_OVERRIDE
virtual bool evaluate_options(nv::index::app::String_dict &sdict) CPP11_OVERRIDE
int main(int argc, const char *argv[])
#define check_success(expr)