-
Notifications
You must be signed in to change notification settings - Fork 23
/
measure.cpp
420 lines (375 loc) · 12.7 KB
/
measure.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// vi: set expandtab shiftwidth=4 softtabstop=4:
/*
* === UCSF ChimeraX Copyright ===
* Copyright 2022 Regents of the University of California. All rights reserved.
* The ChimeraX application is provided pursuant to the ChimeraX license
* agreement, which covers academic and commercial uses. For more details, see
* <http://www.rbvi.ucsf.edu/chimerax/docs/licensing.html>
*
* This particular file is part of the ChimeraX library. You can also
* redistribute and/or modify it under the terms of the GNU Lesser General
* Public License version 2.1 as published by the Free Software Foundation.
* For more details, see
* <https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html>
*
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ADDITIONAL LIABILITY
* LIMITATIONS ARE DESCRIBED IN THE GNU LESSER GENERAL PUBLIC LICENSE
* VERSION 2.1
*
* This notice must be embedded in or attached to all copies, including partial
* copies, of the software or any revisions or derivations thereof.
* === UCSF ChimeraX Copyright ===
*/
// ----------------------------------------------------------------------------
//
#include <Python.h> // use PyObject
#include <math.h> // use sqrt()
#include <map> // use std::map
#include <set> // use std::set
#include <utility> // use std::pair
#include <vector> // use std::vector
#include <arrays/pythonarray.h> // use array_from_python()
#include <arrays/rcarray.h> // use FArray, IArray
typedef std::pair<int,int> Edge;
typedef std::set<Edge> Edge_Set;
typedef std::vector<int> Vertex_Loop;
typedef std::vector<Vertex_Loop> Vertex_Loops;
static float enclosed_volume(FArray &varray, const IArray &tarray,
int *hole_count);
static float tetrahedron_signed_volume(float v0[3], float v1[3], float v2[3],
float v3[3]);
static Edge_Set *boundary_edge_set(const IArray &tarray);
static Vertex_Loops *boundary_loops(const IArray &tarray);
static float cap_volume(float *v, const Vertex_Loops &vloops);
static void loop_center(float *v, const Vertex_Loop &vloop, float center[3]);
static float surface_area(FArray &varray, const IArray &tarray,
float *areas = NULL);
static float triangle_area(float v0[3], float v1[3], float v2[3]);
// ----------------------------------------------------------------------------
//
extern "C" PyObject *enclosed_volume(PyObject *, PyObject *args, PyObject *keywds)
{
FArray varray;
IArray tarray;
const char *kwlist[] = {"vertex_array", "triangle_array", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds,
const_cast<char *>("O&O&"), (char **)kwlist,
parse_float_n3_array, &varray,
parse_int_n3_array, &tarray))
return NULL;
int hole_count;
float vol = enclosed_volume(varray, tarray, &hole_count);
PyObject *vh = python_tuple(PyFloat_FromDouble(vol), PyLong_FromLong(hole_count));
return vh;
}
// ----------------------------------------------------------------------------
//
static float enclosed_volume(FArray &varray, const IArray &tarray,
int *hole_count)
{
Vertex_Loops *vloops = boundary_loops(tarray);
if (vloops == NULL)
{ *hole_count = 0; return -1.0; }
*hole_count = static_cast<int>(vloops->size());
FArray vc = varray.contiguous_array();
float *v = vc.values();
IArray tc = tarray.contiguous_array();
int m = tarray.size(0);
int *tv = tc.values();
float volume = 0;
for (int t = 0 ; t < m ; ++t)
{
int i0 = tv[3*t], i1 = tv[3*t+1], i2 = tv[3*t+2];
volume += tetrahedron_signed_volume(v, v+3*i0, v+3*i1, v+3*i2);
}
volume += cap_volume(v, *vloops);
delete vloops;
if (volume < 0)
volume = -volume; // Sign depends on triangle vertex order.
return volume;
}
// ----------------------------------------------------------------------------
//
static float tetrahedron_signed_volume(float v0[3], float v1[3], float v2[3],
float v3[3])
{
float e1x = v1[0]-v0[0], e1y = v1[1]-v0[1], e1z = v1[2]-v0[2];
float e2x = v2[0]-v0[0], e2y = v2[1]-v0[1], e2z = v2[2]-v0[2];
float e3x = v3[0]-v0[0], e3y = v3[1]-v0[1], e3z = v3[2]-v0[2];
float v = (e1x * (e2y * e3z - e2z * e3y) +
e1y * (e2z * e3x - e2x * e3z) +
e1z * (e2x * e3y - e2y * e3x)) / 6.0;
return v;
}
// ----------------------------------------------------------------------------
//
static Edge_Set *boundary_edge_set(const IArray &tarray)
{
IArray tc = tarray.contiguous_array();
int m = tarray.size(0);
int *tv = tc.values();
Edge_Set *eset = new Edge_Set;
Edge_Set::iterator ei;
for (int t = 0 ; t < m ; ++t)
{
int i0 = tv[3*t], i1 = tv[3*t+1], i2 = tv[3*t+2];
if ((ei = eset->find(Edge(i1,i0))) != eset->end()) eset->erase(ei);
else eset->insert(Edge(i0,i1));
if ((ei = eset->find(Edge(i2,i1))) != eset->end()) eset->erase(ei);
else eset->insert(Edge(i1,i2));
if ((ei = eset->find(Edge(i0,i2))) != eset->end()) eset->erase(ei);
else eset->insert(Edge(i2,i0));
}
return eset;
}
// ----------------------------------------------------------------------------
// Returns NULL if surface is not oriented.
//
static Vertex_Loops *boundary_loops(const IArray &tarray)
{
Edge_Set *eset = boundary_edge_set(tarray);
// Map one vertex to next along directed boundary.
std::map<int,int> vmap;
for (Edge_Set::iterator ei = eset->begin() ; ei != eset->end() ; ++ei)
{
if (vmap.find(ei->first) == vmap.end())
vmap[ei->first] = ei->second;
else
{ delete eset; return NULL; }
}
delete eset;
// Record boundary loops.
Vertex_Loops *vloops = new std::vector<Vertex_Loop>;
while (!vmap.empty())
{
std::map<int,int>::iterator vmi = vmap.begin();
Vertex_Loop vloop;
while (true)
{
vloop.push_back(vmi->first);
int v = vmi->second;
vmap.erase(vmi);
if (v == vloop[0])
break;
vmi = vmap.find(v);
if (vmi == vmap.end())
{ delete vloops; return NULL; }
}
vloops->push_back(vloop);
}
return vloops;
}
// ----------------------------------------------------------------------------
// Returns NULL if surface is not oriented.
//
static void boundary_edge_mask(const IArray &tarray, unsigned char *edge_mask)
{
Edge_Set *eset = boundary_edge_set(tarray);
int64_t s0 = tarray.stride(0), s1 = tarray.stride(1);
int n = tarray.size(0);
int *ta = tarray.values();
int *t = ta;
for (int i = 0 ; i < n ; ++i, t += s0)
{
int emask = 0;
int v0 = t[0], v1 = t[s1], v2 = t[2*s1];
if (eset->find(Edge(v0,v1)) != eset->end() || eset->find(Edge(v1,v0)) != eset->end())
emask |= 0x1;
if (eset->find(Edge(v1,v2)) != eset->end() || eset->find(Edge(v2,v1)) != eset->end())
emask |= 0x2;
if (eset->find(Edge(v2,v0)) != eset->end() || eset->find(Edge(v0,v2)) != eset->end())
emask |= 0x4;
edge_mask[i] = emask;
}
}
// ----------------------------------------------------------------------------
//
extern "C" PyObject *boundary_edge_mask(PyObject *, PyObject *args, PyObject *keywds)
{
IArray tarray;
const char *kwlist[] = {"triangle_array", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds,
const_cast<char *>("O&"), (char **)kwlist,
parse_int_n3_array, &tarray))
return NULL;
int n = tarray.size(0);
unsigned char *emask;
PyObject *edge_mask = python_uint8_array(n, &emask);
boundary_edge_mask(tarray, emask);
return edge_mask;
}
// ----------------------------------------------------------------------------
//
static float cap_volume(float *v, const Vertex_Loops &vloops)
{
float volume = 0;
for (Vertex_Loops::const_iterator li = vloops.begin() ; li != vloops.end() ; ++li)
{
const Vertex_Loop &vloop = *li;
int n = static_cast<int>(vloop.size());
if (n >= 3)
{
float c[3];
loop_center(v, vloop, c);
for (int k = 0 ; k < n ; ++k)
{
int i0 = vloop[k], i1 = vloop[(k+1)%n];
volume += tetrahedron_signed_volume(v, c, v+3*i1, v+3*i0);
}
}
}
return volume;
}
// ----------------------------------------------------------------------------
//
static void loop_center(float *v, const Vertex_Loop &vloop, float center[3])
{
center[0] = center[1] = center[2] = 0;
int n = static_cast<int>(vloop.size());
for (int k = 0 ; k < n ; ++k)
{
int i = vloop[k];
for (int a = 0 ; a < 3 ; ++a)
center[a] += v[3*i+a];
}
center[0] /= n; center[1] /= n; center[2] /= n;
}
// ----------------------------------------------------------------------------
//
extern "C" PyObject *surface_area(PyObject *, PyObject *args, PyObject *keywds)
{
FArray varray;
IArray tarray;
const char *kwlist[] = {"vertex_array", "triangle_array", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds,
const_cast<char *>("O&O&"), (char **)kwlist,
parse_float_n3_array, &varray,
parse_int_n3_array, &tarray))
return NULL;
float area = surface_area(varray, tarray);
PyObject *py_area = PyFloat_FromDouble(area);
return py_area;
}
// ----------------------------------------------------------------------------
//
extern "C" PyObject *vertex_areas(PyObject *, PyObject *args, PyObject *keywds)
{
FArray varray, areas;
IArray tarray;
const char *kwlist[] = {"vertex_array", "triangle_array", "areas", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds,
const_cast<char *>("O&O&|O&"), (char **)kwlist,
parse_float_n3_array, &varray,
parse_int_n3_array, &tarray,
parse_writable_float_n_array, &areas))
return NULL;
bool make_areas = (areas.dimension() == 0);
if (make_areas)
{
float *a;
int n = varray.size(0);
PyObject *pya = python_float_array(n, &a);
for (int k = 0 ; k < n ; ++k)
a[k] = 0;
parse_writable_float_n_array(pya, &areas);
}
else
{
if (areas.size(0) != varray.size(0))
{
PyErr_SetString(PyExc_TypeError,
"vertex_areas: return array size does not equal number of vertices");
return NULL;
}
if (!areas.is_contiguous())
{
PyErr_SetString(PyExc_TypeError,
"vertex_areas: return array is not contiguous");
return NULL;
}
}
surface_area(varray, tarray, areas.values());
PyObject *py_areas = array_python_source(areas, !make_areas);
return py_areas;
}
// ----------------------------------------------------------------------------
//
static float surface_area(FArray &varray, const IArray &tarray, float *areas)
{
FArray vc = varray.contiguous_array();
float *v = vc.values();
IArray tc = tarray.contiguous_array();
int m = tarray.size(0);
int *tv = tc.values();
float area = 0;
if (areas)
for (int t = 0 ; t < m ; ++t)
{
int i0 = tv[3*t], i1 = tv[3*t+1], i2 = tv[3*t+2];
float a = triangle_area(v+3*i0, v+3*i1, v+3*i2);
area += a;
areas[i0] += a/3.0;
areas[i1] += a/3.0;
areas[i2] += a/3.0;
}
else
for (int t = 0 ; t < m ; ++t)
{
int i0 = tv[3*t], i1 = tv[3*t+1], i2 = tv[3*t+2];
area += triangle_area(v+3*i0, v+3*i1, v+3*i2);
}
return area;
}
// ----------------------------------------------------------------------------
//
static float triangle_area(float v0[3], float v1[3], float v2[3])
{
float x1 = v1[0]-v0[0], y1 = v1[1]-v0[1], z1 = v1[2]-v0[2];
float x2 = v2[0]-v0[0], y2 = v2[1]-v0[1], z2 = v2[2]-v0[2];
float x12 = y1*z2-z1*y2, y12 = z1*x2-x1*z2, z12 = x1*y2-y1*x2;
float a2 = x12*x12 + y12*y12 + z12*z12;
float area = .5*sqrt(a2);
return area;
}
// ----------------------------------------------------------------------------
//
extern "C" PyObject *boundary_edges(PyObject *, PyObject *args, PyObject *keywds)
{
IArray tarray;
const char *kwlist[] = {"triangle_array", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds,
const_cast<char *>("O&"), (char **)kwlist,
parse_int_n3_array, &tarray))
return NULL;
Edge_Set *eset = boundary_edge_set(tarray);
int *ea, e = 0;
PyObject *edges = python_int_array(static_cast<int>(eset->size()), 2, &ea);
for (Edge_Set::iterator ei = eset->begin() ; ei != eset->end() ; ++ei)
{
ea[e++] = ei->first;
ea[e++] = ei->second;
}
delete eset;
return edges;
}
// ----------------------------------------------------------------------------
//
extern "C" PyObject *boundary_loops(PyObject *, PyObject *args, PyObject *keywds)
{
IArray tarray;
const char *kwlist[] = {"triangle_array", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds,
const_cast<char *>("O&"), (char **)kwlist,
parse_int_n3_array, &tarray))
return NULL;
Vertex_Loops *vloops = boundary_loops(tarray);
PyObject *loopy = PyTuple_New(vloops->size());
int l = 0;
for (Vertex_Loops::iterator vi = vloops->begin() ;
vi != vloops->end() ; ++vi, ++l)
PyTuple_SetItem(loopy, l, c_array_to_python(*vi));
delete vloops;
return loopy;
}