scenepic 1.1.0
3D Visualization Made Easy
scene.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#ifndef _SCENEPIC_SCENE_H_
5#define _SCENEPIC_SCENE_H_
6
7#include "audio_track.h"
8#include "camera.h"
9#include "canvas2d.h"
10#include "canvas3d.h"
11#include "color.h"
12#include "drop_down_menu.h"
13#include "graph.h"
14#include "image.h"
15#include "mesh.h"
16#include "mesh_update.h"
17#include "shading.h"
18#include "text_panel.h"
19#include "ui_parameters.h"
20#include "video.h"
21
22#include <map>
23#include <memory>
24#include <string>
25#include <vector>
26
27namespace scenepic
28{
31 {
32 QuantizationInfo() = default;
33
36 std::uint32_t keyframe_count,
38 float mean_error,
39 float max_error);
40
42 std::uint32_t keyframe_count;
43
46
49
51 float max_error;
52
53 std::string to_string() const;
54 };
55
57 class Scene
58 {
59 public:
64 Scene(const std::string& scene_id = "");
65
76 void grid(
77 const std::string& width,
78 const std::string& grid_template_rows = "",
79 const std::string& grid_template_cols = "");
80
87 void place(
88 const std::string& canvas_id,
89 const std::string& grid_row,
90 const std::string& grid_column);
91
109 std::shared_ptr<Canvas3D> create_canvas_3d(
110 const std::string& canvas_id = "",
111 double width = 400,
112 double height = 400,
113 const std::string& html_id = "",
114 const Camera& camera = Camera::None(),
115 const Shading& shading = Shading(),
116 const UIParameters& ui_parameters = UIParameters(),
117 const std::string& media_id = "");
118
134 std::shared_ptr<Canvas2D> create_canvas_2d(
135 const std::string& canvas_id = "",
136 double width = 400,
137 double height = 400,
138 const std::string& html_id = "",
139 const Color& background_color = Colors::Black,
140 const std::string& media_id = "");
141
165 std::shared_ptr<Graph> create_graph(
166 const std::string& canvas_id = "",
167 double width = 400,
168 double height = 400,
169 const std::string& name_align = "left",
170 const std::string& value_align = "right",
171 const std::string& html_id = "",
172 const Color& background_color = Colors::White,
173 const Graph::Margin& margin = Graph::Margin(),
174 const std::string& font_family = "sans-serif",
175 float name_size = 12.0,
176 float value_size = 12.0,
177 const std::string& media_id = "");
178
184 std::shared_ptr<AudioTrack> create_audio(const std::string& audio_id = "");
185
191 std::shared_ptr<Video> create_video(const std::string& video_id = "");
192
220 std::shared_ptr<Mesh> create_mesh(
221 const std::string& mesh_id = "",
222 const std::string& layer_id = "",
223 bool double_sided = false,
224 bool camera_space = false,
225 const Color& shared_color = Color::None(),
226 const std::string& texture_id = "",
227 bool nn_texture = true,
228 bool use_texture_alpha = false,
229 bool is_billboard = false,
230 bool is_label = false);
231
247 std::shared_ptr<MeshUpdate> update_mesh(
248 const std::string& base_mesh_id,
249 const ConstVectorBufferRef& positions,
250 const ConstVectorBufferRef& normals,
251 const ConstVectorBufferRef& colors,
252 const std::string& mesh_id = "");
253
269 std::shared_ptr<MeshUpdate> update_instanced_mesh(
270 const std::string& base_mesh_id,
271 const ConstVectorBufferRef& positions,
272 const ConstQuaternionBufferRef& rotations,
273 const ConstVectorBufferRef& colors,
274 const std::string& mesh_id = "");
275
280 std::shared_ptr<MeshUpdate> update_mesh_without_normals(
281 const std::string& base_mesh_id,
282 const ConstVectorBufferRef& positions,
283 const std::string& mesh_id = "");
284
298 std::shared_ptr<MeshUpdate> update_mesh_positions(
299 const std::string& base_mesh_id,
300 const ConstVectorBufferRef& positions,
301 const std::string& mesh_id = "");
302
310 std::shared_ptr<Image> create_image(const std::string& image_id = "");
311
333 std::shared_ptr<Label> create_label(
334 const std::string& label_id = "",
335 const std::string& text = "Text",
336 const Color& color = Colors::White,
337 double size_in_pixels = 12.0,
338 const std::string& font_family = "sans-serif",
339 const std::string& layer_id = "",
340 const std::string& horizontal_align = "left",
341 const std::string& vertical_align = "middle",
342 double offset_distance = 0.1,
343 bool camera_space = false);
344
354 std::shared_ptr<TextPanel> create_text_panel(
355 const std::string& text_panel_id = "",
356 const std::string& html_id = "",
357 bool add_input_box = false);
358
369 std::shared_ptr<DropDownMenu> create_drop_down_menu(
370 const std::string& drop_down_menu_id = "",
371 const std::string& title = "",
372 const std::string& html_id = "");
373
378 void configure_user_interface(const UIParameters& ui_parameters);
379
384 void link_canvas_events(const std::vector<std::string>& canvas_ids);
385
392 const std::string& canvas_id, const std::vector<std::string>& keypresses);
393
398
405
411 std::string json() const;
412
418 std::string script() const;
419
421 float framerate() const;
422
423 void framerate(float fps);
424
426 const std::string& status_bar_visibility() const;
427
428 void status_bar_visibility(const std::string& visibility);
429
436 void save_as_json(const std::string& path) const;
437
445 void save_as_script(const std::string& path, bool standalone = false) const;
446
482 std::map<std::string, QuantizationInfo> quantize_updates(
483 float relative_error_threshold = 1e-5,
484 float absolute_error_threshold = -1.0,
485 const std::string& base_mesh_id = "",
486 bool per_frame_range = true);
487
489 std::map<std::string, std::size_t> measure_command_size() const;
490
502 const std::string& path,
503 const std::string& title = "ScenePic",
504 const std::string& head_html = "",
505 const std::string& body_html = "",
506 const std::string& script_path = "",
507 const std::string& library_path = "");
508
510 bool script_cleared() const;
511
512 private:
513 template<typename T>
514 static void add_commands(
515 JsonValue& commands, const std::vector<std::shared_ptr<T>>& entities)
516 {
517 for (const auto& entity : entities)
518 {
519 commands.append(entity->to_json());
520 }
521 }
522
523 float compute_mesh_range(const std::string& mesh_id);
524
525 std::string m_scene_id;
526 std::vector<JsonValue> m_display_order;
527 std::vector<std::shared_ptr<Canvas3D>> m_canvas3Ds;
528 std::vector<std::shared_ptr<Canvas2D>> m_canvas2Ds;
529 std::vector<std::shared_ptr<Graph>> m_graphs;
530 std::vector<std::shared_ptr<AudioTrack>> m_audios;
531 std::vector<std::shared_ptr<Video>> m_videos;
532 std::vector<std::shared_ptr<Mesh>> m_meshes;
533 std::map<std::string, std::uint32_t> m_update_counts;
534 std::vector<std::shared_ptr<MeshUpdate>> m_mesh_updates;
535 std::vector<std::shared_ptr<Image>> m_images;
536 std::vector<std::shared_ptr<Label>> m_labels;
537 std::vector<std::shared_ptr<TextPanel>> m_text_panels;
538 std::vector<std::shared_ptr<DropDownMenu>> m_drop_down_menus;
539 std::vector<JsonValue> m_misc;
540 float m_fps;
541 std::string m_status_bar_visibility;
542
543 // These will get out of sync with the above after a call to clear_script()
544 // DO NOT REMOVE THESE - they are important
545 std::size_t m_num_canvases; // shared UIDs between 2D and 3D
546 std::size_t m_num_meshes;
547 std::size_t m_num_images;
548 std::size_t m_num_audios;
549 std::size_t m_num_videos;
550 std::size_t m_num_labels;
551 std::size_t m_num_text_panels;
552 std::size_t m_num_drop_down_menus;
553 bool m_script_cleared;
554 };
555} // namespace scenepic
556
557#endif
A camera type.
Definition: camera.h:15
static Camera None()
A value indicating "no camera".
Class for representing color values.
Definition: color.h:19
static const Color None()
Constant value indicating "no color".
Representation of a JSON value according to the specification at https://www.json....
Definition: json_value.h:30
void append(JsonValue &&object)
Append the object to this array.
Top level container representing an entire ScenePic.
Definition: scene.h:58
std::string json() const
Returns a JSON-serialized representation of the Scene as a string If you use clear_script() then this...
void framerate(float fps)
void simulate_key_presses(const std::string &canvas_id, const std::vector< std::string > &keypresses)
Simulates one or more keypresses to fire off events in the ScenePic UI at startup,...
std::shared_ptr< Image > create_image(const std::string &image_id="")
Create a new Image and append to the Scene.
std::shared_ptr< DropDownMenu > create_drop_down_menu(const std::string &drop_down_menu_id="", const std::string &title="", const std::string &html_id="")
Create a new DropDownMenu and add directly to the HTML page.
void link_canvas_events(const std::vector< std::string > &canvas_ids)
Specify that all input events should be linked across the provided set of Canvases.
bool script_cleared() const
Whether the script has been cleared.
std::string script() const
Returns a JSONP (https://en.wikipedia.org/wiki/JSONP) script representing the Scene.
JsonValue to_json() const
Convert this object into ScenePic json.
std::shared_ptr< Canvas2D > create_canvas_2d(const std::string &canvas_id="", double width=400, double height=400, const std::string &html_id="", const Color &background_color=Colors::Black, const std::string &media_id="")
Create a new 2D Canvas and append to the Scene.
std::map< std::string, QuantizationInfo > quantize_updates(float relative_error_threshold=1e-5, float absolute_error_threshold=-1.0, const std::string &base_mesh_id="", bool per_frame_range=true)
Quantize the mesh updates.
std::shared_ptr< MeshUpdate > update_mesh_without_normals(const std::string &base_mesh_id, const ConstVectorBufferRef &positions, const std::string &mesh_id="")
float framerate() const
The number of frames per second that will be displayed by this scene.
void place(const std::string &canvas_id, const std::string &grid_row, const std::string &grid_column)
Places a canvas at a specific place in the ScenePic grid.
const std::string & status_bar_visibility() const
The CSS visibility of the status bar.
std::shared_ptr< Video > create_video(const std::string &video_id="")
Create a new Video and add directly to the Scene.
std::map< std::string, std::size_t > measure_command_size() const
Returns a breakdown of the number of bytes used by each command type.
void save_as_json(const std::string &path) const
Save the scene as a JSON file.
std::shared_ptr< TextPanel > create_text_panel(const std::string &text_panel_id="", const std::string &html_id="", bool add_input_box=false)
Create a new TextPanel and add directly to the HTML page.
void clear_script()
Call this if you are dumping a ScenePic script in multiple parts, e.g.
std::shared_ptr< AudioTrack > create_audio(const std::string &audio_id="")
Create a new AudioTrack and add directly to the Scene.
void status_bar_visibility(const std::string &visibility)
std::shared_ptr< MeshUpdate > update_instanced_mesh(const std::string &base_mesh_id, const ConstVectorBufferRef &positions, const ConstQuaternionBufferRef &rotations, const ConstVectorBufferRef &colors, const std::string &mesh_id="")
Create a new Mesh by updating a base mesh with new vertex positions.
void save_as_script(const std::string &path, bool standalone=false) const
Save the scene as a ScenePic script file (JavaScript JSONP format).
std::shared_ptr< MeshUpdate > update_mesh(const std::string &base_mesh_id, const ConstVectorBufferRef &positions, const ConstVectorBufferRef &normals, const ConstVectorBufferRef &colors, const std::string &mesh_id="")
Create a new Mesh by updating a base mesh with new vertex positions.
std::shared_ptr< MeshUpdate > update_mesh_positions(const std::string &base_mesh_id, const ConstVectorBufferRef &positions, const std::string &mesh_id="")
Create a new Mesh by updating a base mesh with new vertex positions.
std::shared_ptr< Mesh > create_mesh(const std::string &mesh_id="", const std::string &layer_id="", bool double_sided=false, bool camera_space=false, const Color &shared_color=Color::None(), const std::string &texture_id="", bool nn_texture=true, bool use_texture_alpha=false, bool is_billboard=false, bool is_label=false)
Create a new Mesh and append to the Scene.
void grid(const std::string &width, const std::string &grid_template_rows="", const std::string &grid_template_cols="")
Defines a CSS layout grid for the canvases of the ScenePic.
Scene(const std::string &scene_id="")
Constructor.
std::shared_ptr< Canvas3D > create_canvas_3d(const std::string &canvas_id="", double width=400, double height=400, const std::string &html_id="", const Camera &camera=Camera::None(), const Shading &shading=Shading(), const UIParameters &ui_parameters=UIParameters(), const std::string &media_id="")
Create a new 3D Canvas and append to the Scene.
void configure_user_interface(const UIParameters &ui_parameters)
Set user interface parameters across all Canvases with given UIParameters instance.
std::shared_ptr< Label > create_label(const std::string &label_id="", const std::string &text="Text", const Color &color=Colors::White, double size_in_pixels=12.0, const std::string &font_family="sans-serif", const std::string &layer_id="", const std::string &horizontal_align="left", const std::string &vertical_align="middle", double offset_distance=0.1, bool camera_space=false)
Create a new Label and append to the Scene.
std::shared_ptr< Graph > create_graph(const std::string &canvas_id="", double width=400, double height=400, const std::string &name_align="left", const std::string &value_align="right", const std::string &html_id="", const Color &background_color=Colors::White, const Graph::Margin &margin=Graph::Margin(), const std::string &font_family="sans-serif", float name_size=12.0, float value_size=12.0, const std::string &media_id="")
Create a new graph canvas and append to the Scene.
void save_as_html(const std::string &path, const std::string &title="ScenePic", const std::string &head_html="", const std::string &body_html="", const std::string &script_path="", const std::string &library_path="")
Save the scene as a html file.
Parameters of the shaders.
Definition: shading.h:15
Per-Canvas3D parameters of the ScenePic user interface.
Definition: ui_parameters.h:15
const Color White
Definition: color.h:96
const Color Black
Definition: color.h:95
Definition: audio_track.h:14
Represents the margin along the edges of a graph.
Definition: graph.h:23
Information about the results of quantization.
Definition: scene.h:31
float mean_error
The mean per-frame error.
Definition: scene.h:48
float estimated_size_ratio
The estimated size ratio after compression.
Definition: scene.h:45
std::uint32_t keyframe_count
The number of keyframes used.
Definition: scene.h:42
float max_error
The maximum per-frame error.
Definition: scene.h:51
QuantizationInfo(std::uint32_t keyframe_count, float estimated_size_ratio, float mean_error, float max_error)
Constructor.
std::string to_string() const