-
Notifications
You must be signed in to change notification settings - Fork 1
/
implicit_group.cxx
150 lines (133 loc) · 4.27 KB
/
implicit_group.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "implicit_group.h"
#include "implicit_primitive.h"
/// passes on the update handler to the children
template <typename T>
void implicit_group<T>::set_update_handler(scene_update_handler* uh)
{
implicit_base<T>::set_update_handler(uh);
for (unsigned i = 0; i<get_nr_children(); ++i)
get_implicit_child(i)->set_update_handler(uh);
}
/// reflect members to expose them to serialization
template <typename T>
bool implicit_group<T>::self_reflect(cgv::reflect::reflection_handler& rh)
{
if (rh.reflect_member("cr", color[0]) &&
rh.reflect_member("cg", color[1]) &&
rh.reflect_member("cb", color[2]) &&
rh.reflect_member("ca", color[3])) {
for (size_t i = 0; i < get_nr_children(); ++i) {
if (!rh.reflect_member(std::string("child") + cgv::utils::to_string(i), (bool&)(child_visible_in_gui[i])))
return false;
}
}
return true;
}
/// access to implicit base interface of children
template <typename T>
implicit_base<T>* implicit_group<T>::get_implicit_child(unsigned i)
{
return get_child(i)->get_interface<implicit_base<T> >();
}
/// const access to implicit base interface of children
template <typename T>
const implicit_base<T>* implicit_group<T>::get_implicit_child(unsigned i) const
{
return get_child(i)->get_interface<implicit_base<T> >();
}
template <typename T>
void implicit_group<T>::on_set(void* member_ptr)
{
for (unsigned i = 0; i < get_nr_children(); ++i) {
bool& toggle = base::ref_tree_node_visible_flag(*get_implicit_child(i));
if (member_ptr == &child_visible_in_gui[i]) {
toggle = (bool&)child_visible_in_gui[i];
base::update_description();
return;
}
if (member_ptr == &toggle) {
(bool&)(child_visible_in_gui[i]) = toggle;
base::update_description();
return;
}
}
update_member(member_ptr);
update_scene();
}
template <typename T>
unsigned int implicit_group<T>::append_child(base_ptr child)
{
unsigned i = group::append_child(child);
child_visible_in_gui.push_back(1);
return i;
}
/// overload to compose the colors of the function children
template <typename T>
typename implicit_group<T>::clr_type implicit_group<T>::compose_color(const pnt_type& p) const
{
size_t n = get_nr_children();
switch (n) {
case 0: return color;
case 1: return get_implicit_child(0)->evaluate_color(p);
default: {
clr_type result(0, 0, 0, 0);
for (size_t i = 0; i < n; ++i)
result += T(1) / T(n) * get_implicit_child(unsigned(i))->evaluate_color(p);
return result;
}
}
}
/// evaluation of surface color based on color_mode
template <typename T>
typename implicit_group<T>::clr_type implicit_group<T>::evaluate_color(const pnt_type& p) const
{
switch (color_mode) {
case GCM_REPLACE: return color;
case GCM_COMPOSE: return compose_color(p);
default: {
if (get_nr_children()==0)
return color;
unsigned i = color_mode - GCM_CHILD_0;
if (i >= get_nr_children())
return color;
return get_implicit_child(i)->evaluate_color(p);
}
}
}
template <typename T>
void implicit_group<T>::create_gui()
{
base::align("\a");
for (unsigned i=0; i<get_nr_children(); ++i) {
implicit_base<T>* child_ptr = get_implicit_child(i);
base::ref_tree_node_visible_flag(*child_ptr) = (bool&)child_visible_in_gui[i];
// add tree node
std::string label = std::string("[") + cgv::utils::to_string(i) + "]";
bool node_visible = base::begin_tree_node(label, *child_ptr, (bool&)child_visible_in_gui[i], "level=3;options='w=20';align=' '");
// add gui for child name with gui color of child
std::string& child_name = const_cast<std::string&>(child_ptr->get_base()->get_named()->get_name());
base::add_member_control(child_ptr->get_base(), "name", child_name, "", "w=140;label=''", "");
base::find_control(child_name)->set("color", child_ptr->gui_color);
// add gui for color of child
base::add_member_control(child_ptr->get_base(), "color", child_ptr->color, "", "w=40;label=''");
if (node_visible) {
base::inline_object_gui(get_child(i));
base::end_tree_node(*child_ptr);
}
}
base::align("\b");
}
template <typename T>
std::string implicit_group<T>::get_type_name() const
{
return "implicit_group";
}
template <typename T>
bool implicit_group<T>::init(context& ctx)
{
bool success = true;
for (unsigned i=0; i<get_nr_children(); ++i)
success = get_implicit_child(i)->init(ctx) && success;
return success;
}
template class implicit_group<double>;