-
Notifications
You must be signed in to change notification settings - Fork 1
/
implicit_base.h
118 lines (106 loc) · 3.12 KB
/
implicit_base.h
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
#pragma once
#include <cgv/base/base.h>
#include <cgv/media/color.h>
#include <cgv/gui/provider.h>
#include <cgv/render/drawable.h>
using namespace cgv::base;
using namespace cgv::math;
using namespace cgv::render;
using namespace cgv::gui;
#define sqr(x) ((x)*(x))
template <typename T>
class implicit_group;
struct scene_update_handler
{
virtual void update_scene() = 0;
virtual void update_description() = 0;
};
/** base implementation for all group nodes*/
template <typename T>
class implicit_base :
public drawable,
public provider
{
public:
/// coordinate type
typedef T crd_type;
/// type of primitive color
typedef cgv::media::color<float, cgv::media::RGB, cgv::media::OPACITY> clr_type;
/// type of 3d vector
typedef cgv::math::fvec<double, 3> vec_type;
/// type of 3d point
typedef cgv::math::fvec<double, 3> pnt_type;
protected:
scene_update_handler * update_handler;
/// to be called if scene has changed due to gui interaction
void update_scene();
/// callback for functions that update the scene description without the implicit function
void update_description();
/// color of implicit primitive
clr_type color;
/// gui color
int gui_color;
/// give group access to color and gui_color
friend class implicit_group<T>;
public:
/// set new scene update handler
virtual void set_update_handler(scene_update_handler* uh);
/// constructor sets default gui color
implicit_base();
/// returns "implicit_primitive"
std::string get_type_name() const;
/// convert to cgv::base::base pointer
virtual cgv::base::base* get_base() = 0;
/// interface for evaluation of implicit function
virtual crd_type evaluate(const pnt_type& p) const = 0;
/// interface for evaluation of the gradient with central differences based default implementation
virtual vec_type evaluate_gradient(const pnt_type& p) const;
/// interface for the evaluation of surface color
virtual clr_type evaluate_color(const pnt_type& p) const;
};
struct abst_scene_factory
{
std::string names;
virtual void init_counter() = 0;
virtual base_ptr create_function() = 0;
};
extern void register_scene_factory(abst_scene_factory* _scene_factory);
template <typename T>
struct scene_factory : public abst_scene_factory
{
static unsigned int& ref_counter() {
static unsigned int counter = 1;
return counter;
}
static std::string& ref_base_name() {
static std::string base_name;
return base_name;
}
scene_factory(const std::string& _names, const std::string& base_name = "") {
names = _names;
ref_base_name() = base_name;
if (base_name.empty()) {
T dummy;
ref_base_name() = dummy.get_base()->get_type_name();
}
}
void init_counter() {
ref_counter() = 1;
}
base_ptr create_function() {
T* f = new T;
f->set_name(ref_base_name() + "_" + cgv::utils::to_string(ref_counter()));
++ref_counter();
return f;
};
};
/** use this registration struct to register a factory for your
implementation of an implicit function.
*/
template <typename T>
struct scene_factory_registration
{
scene_factory_registration(const std::string& _names, const std::string& _base_name = "") {
register_scene_factory(new scene_factory<T>(_names, _base_name));
}
};