-
Notifications
You must be signed in to change notification settings - Fork 326
/
compute_normal.jl
61 lines (50 loc) · 1.57 KB
/
compute_normal.jl
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
function compute_normal(vertex,face)
"""
compute_normal - compute the normal of a triangulation
[normal,normalf] = compute_normal(vertex,face);
normal(i,:) is the normal at vertex i.
normalf(j,:) is the normal at face j.
Copyright (c) 2004 Gabriel Peyre
"""
epsilon = eps(Float64)
nface = size(face,2)
nvert = size(vertex,2)
normal = zeros(3,nvert)
# unit normals to the faces
normalf = crossp(vertex[:,face[2,:]] - vertex[:,face[1,:]],
vertex[:,face[3,:]] - vertex[:,face[1,:]])
d = sqrt(sum(normalf.^2,1))
d[d .< epsilon] = 1
normalf = normalf./repeat(d,outer=(3,1))
# unit normal to the vertex
for i in 1:nface
f = face[:,i]
for j in 1:3
normal[:,f[j]] = normal[:,f[j]] + normalf[:,i]
end
end
# normalize
d = sqrt(sum(normal.^2,1))
d[d .< epsilon] = 1
normal = normal./repeat(d,outer=(3,1))
# enforce that the normal are outward
v = vertex - repeat(mean(vertex,1),outer=(3,1))
s = sum(v.*normal,2)
if sum(s .> 0) < sum(s .< 0)
# flip
normal = -normal
normalf = -normalf
end
return normal
end
###################################################
###################################################
###################################################
function crossp(x,y)
# x and y are (m,3) dimensional
z = copy(x)
z[1,:] = x[2,:].*y[3,:] - x[3,:].*y[2,:]
z[2,:] = x[3,:].*y[1,:] - x[1,:].*y[3,:]
z[3,:] = x[1,:].*y[2,:] - x[2,:].*y[1,:]
return z
end