Cycles: Add utility function to calculate triangle's normal

This commit is contained in:
2017-10-10 13:12:06 +05:00
parent 552d15c976
commit a421607569
2 changed files with 17 additions and 17 deletions

View File

@@ -107,6 +107,19 @@ void Mesh::Triangle::verts_for_step(const float3 *verts,
}
}
float3 Mesh::Triangle::compute_normal(const float3 *verts) const
{
const float3& v0 = verts[v[0]];
const float3& v1 = verts[v[1]];
const float3& v2 = verts[v[2]];
const float3 norm = cross(v1 - v0, v2 - v0);
const float normlen = len(norm);
if(normlen == 0.0f) {
return make_float3(1.0f, 0.0f, 0.0f);
}
return norm / normlen;
}
/* Curve */
void Mesh::Curve::bounds_grow(const int k, const float3 *curve_keys, const float *curve_radius, BoundBox& bounds) const
@@ -701,21 +714,6 @@ void Mesh::compute_bounds()
bounds = bnds;
}
static float3 compute_face_normal(const Mesh::Triangle& t, float3 *verts)
{
float3 v0 = verts[t.v[0]];
float3 v1 = verts[t.v[1]];
float3 v2 = verts[t.v[2]];
float3 norm = cross(v1 - v0, v2 - v0);
float normlen = len(norm);
if(normlen == 0.0f)
return make_float3(1.0f, 0.0f, 0.0f);
return norm / normlen;
}
void Mesh::add_face_normals()
{
/* don't compute if already there */
@@ -733,7 +731,7 @@ void Mesh::add_face_normals()
float3 *verts_ptr = verts.data();
for(size_t i = 0; i < triangles_size; i++) {
fN[i] = compute_face_normal(get_triangle(i), verts_ptr);
fN[i] = get_triangle(i).compute_normal(verts_ptr);
}
}
@@ -795,7 +793,7 @@ void Mesh::add_vertex_normals()
for(size_t i = 0; i < triangles_size; i++) {
for(size_t j = 0; j < 3; j++) {
float3 fN = compute_face_normal(get_triangle(i), mP);
float3 fN = get_triangle(i).compute_normal(mP);
mN[get_triangle(i).v[j]] += fN;
}
}

View File

@@ -70,6 +70,8 @@ public:
size_t num_steps,
size_t step,
float3 r_verts[3]) const;
float3 compute_normal(const float3 *verts) const;
};
Triangle get_triangle(size_t i) const