-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmls.cxx
219 lines (199 loc) · 5.8 KB
/
mls.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "mls.h"
#include <cgv_gl/gl/gl.h>
#include <cgv/utils/file.h>
#include <cgv/gui/file_dialog.h>
#include <cgv/utils/scan.h>
#include <cgv/media/mesh/obj_loader.h>
#include <cgv/media/axis_aligned_box.h>
using namespace cgv::utils::file;
using namespace cgv::utils;
using namespace cgv::signal;
template <typename T, typename P, typename C>
mls_surface<T,P,C>::mls_surface()
{
point_size = 3;
h = 0.01f;
extent = 1;
ann = 0;
skip_eval = false;
gui_color = 0xFF8888;
}
template <typename T, typename P, typename C>
mls_surface<T,P,C>::~mls_surface()
{
if (ann)
delete ann;
}
template <typename T, typename P, typename C>
void mls_surface<T,P,C>::build_ann()
{
axis_aligned_box<P,3> box;
if (ann)
delete ann;
ANNpointArray pa = new ANNpoint[points.size()];
for (unsigned i=0; i<points.size(); ++i) {
pa[i] = &points[i](0);
box.add_point(points[i]);
}
extent = box.get_extent().length();
base::update_member(&extent);
ann = new ANNkd_tree(pa, (int)points.size(), 3);
}
template <typename T, typename P, typename C>
void mls_surface<T,P,C>::open()
{
string _file_name = file_open_dialog("open point cloud", "Obj Files (obj,pobj):*.obj;*.pobj");
read(_file_name);
}
template <typename T, typename P, typename C>
bool mls_surface<T,P,C>::read(const string& _file_name)
{
if (!exists(_file_name)) {
cerr << "mls_surface::read ... could not find file " << _file_name << endl;
return false;
}
string ext = to_lower(cgv::utils::file::get_extension(_file_name));
if (ext == "obj" || ext == "pobj")
return read_obj(_file_name);
std::cerr << "mls_surface::read ... unknown file extension " << ext << endl;
return false;
}
template <typename T, typename P, typename C>
bool mls_surface<T,P,C>::read_obj(const string& _file_name)
{
mesh::obj_loader l;
if (!l.read_obj(_file_name))
return false;
points.resize(l.vertices.size());
for (unsigned i=0; i<l.vertices.size(); ++i) {
points[i].set((P)l.vertices[i](0),(P)l.vertices[i](1),(P)l.vertices[i](2));
}
if (!l.normals.empty()) {
normals.resize(l.normals.size());
for (unsigned i=0; i<l.normals.size(); ++i) {
normals[i].set((P)l.normals[i](0),(P)l.normals[i](1),(P)l.normals[i](2));
}
}
file_name = _file_name;
build_ann();
base::update_scene();
return true;
}
template <typename T, typename P, typename C>
bool mls_surface<T,P,C>::self_reflect(reflection_handler& rh)
{
return
rh.reflect_member("h", h) &&
rh.reflect_member("show_points", show_points) &&
rh.reflect_member("skip_eval", skip_eval) &&
rh.reflect_member("point_size", point_size) &&
rh.reflect_member("file_name", file_name) &&
implicit_primitive<T>::self_reflect(rh);
}
template <typename T, typename P, typename C>
void mls_surface<T,P,C>::on_set(void* member_ptr)
{
if (member_ptr == &file_name)
read(file_name);
else
if (!(member_ptr == &h || member_ptr == &file_name || member_ptr == &skip_eval)) {
base::update_description();
return;
}
implicit_primitive<T>::on_set(member_ptr);
}
template <typename T, typename P, typename C>
T mls_surface<T,P,C>::evaluate(const pnt_type& _p) const
{
if (points.empty() || skip_eval)
return 0;
P3d p((P)_p(0),(P)_p(1),(P)_p(2));
last_p = p;
static ANNidx idx_arr[10000];
static P dist_arr[10000];
P d = extent*h;
P dd = d*d;
P idd = 1.0f/dd;
int n = ann->annkFRSearch(p,9*dd,10000,idx_arr,dist_arr,0.01);
if (n==0){
// last_nml.set(1,0,0);
// return 3*d;
ann->annkSearch(p,1,idx_arr,dist_arr,0.01);
P d1 = sqrt(dist_arr[0]);
P d2 = dot(p-points[idx_arr[0]],normals[idx_arr[0]]);
if (10*fabs(d2) > d1) {
last_nml = normals[idx_arr[0]];
return d2;
}
else {
last_nml = points[idx_arr[0]]-p;
last_nml.normalize();
return d1;
}
// n = ann->annkFRSearch(p,sqr(sqrt(dist_arr[0])+3*d),10000,idx_arr,dist_arr,0.01);
}
P sum = 0;
P3d ctr(0,0,0);
V3d nml(0,0,0);
for (int i=0; i<n; ++i) {
P theta = exp(-(dist_arr[i]*dist_arr[i])*idd);
sum += theta;
ctr += theta*points[idx_arr[i]];
nml += theta*normals[idx_arr[i]];
}
ctr /= sum;
nml.normalize();
last_nml = nml;
return dot(p-ctr,nml);
}
template <typename T, typename P, typename C>
typename mls_surface<T,P,C>::vec_type mls_surface<T,P,C>::evaluate_gradient(const pnt_type& p) const
{
if (points.empty() || skip_eval)
return vec_type(1,0,0);
if (sqr_length(last_p - P3d((P)p(0),(P)p(1),(P)P(2))) > 1e-10)
evaluate(p);
return vec_type(last_nml(0),last_nml(1),last_nml(2));
}
template <typename T, typename P, typename C>
void mls_surface<T,P,C>::draw(context& ctx)
{
if (points.empty() || !show_points)
return;
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
glPushAttrib(GL_POINT_BIT|GL_LIGHTING);
glVertexPointer(3, GL_FLOAT, 0, &points[0]);
glEnableClientState(GL_VERTEX_ARRAY);
if (!colors.empty()) {
glColorPointer(3, GL_UNSIGNED_BYTE, 0, &colors[0]);
glEnableClientState(GL_COLOR_ARRAY);
}
if (!normals.empty()) {
glNormalPointer(GL_FLOAT, 0, &normals[0]);
glEnableClientState(GL_NORMAL_ARRAY);
glEnable(GL_LIGHTING);
if (!colors.empty())
glEnable(GL_COLOR_MATERIAL);
else
glDisable(GL_COLOR_MATERIAL);
}
else
glDisable(GL_LIGHTING);
glColor3f(0.5f,0.5f,0.5f);
glPointSize(point_size);
glEnable(GL_POINT_SMOOTH);
glDrawArrays(GL_POINTS, 0, (GLsizei)points.size());
glPopAttrib();
glPopClientAttrib();
}
template <typename T, typename P, typename C>
void mls_surface<T,P,C>::create_gui()
{
base::add_view("extent", extent);
add_member_control(this, "show_points", show_points, "check");
add_member_control(this, "skip_eval", skip_eval, "check");
add_member_control(this, "h", h, "value_slider", "min=0.001;max=1;ticks=true;log=true");
add_member_control(this, "point_size", point_size, "value_slider", "min=1;max=30;ticks=true;log=true");
connect_copy(base::add_button("open")->click, rebind(this, &mls_surface<T,P,C>::open));
}
scene_factory_registration<mls_surface<double,float,unsigned char> > sfr_mls("mls");