Catmull–Clark subdivision surface: Difference between revisions

Content added Content deleted
m (→‎{{header|OCaml}}: some comments)
(added a full description)
Line 1: Line 1:
{{task|3D}}
{{task|3D}}


Implement the Catmull-Clark surface subdivision.
Implement the Catmull-Clark surface subdivision ([[wp:Catmull–Clark_subdivision_surface|description on Wikipedia]]).

The process works as follow:

# for each face, a ''face point'' is created which is the average of all the points of the face.
# for each edge, an ''edge point'' is created which is the average between the center of the edge and the center of the segment made with the face points of the two adjacent faces.
# for each vertex point, its coordinates are updated from (new_coords):
## the old coordinates (old_coords),
## the average of the face points of the faces the point belongs to (avg_face_points),
## the average of the centers of edges the point belongs to (avg_mid_edges),
## how many faces a point belongs to (n), then use this formula:

m1 = (n - 3) / n
m2 = 1 / n
m3 = 2 / n
new_coords = (m1 * old_vertex)
+ (m2 * avg_face_points)
+ (m3 * avg_mid_edges)

Then each face is replaced by new faces made with the new points,
* for a triangle face (a,b,c):
(a, edge_point_ab, face_point, edge_point_ca)
(b, edge_point_bc, face_point, edge_point_ab)
(c, edge_point_ca, face_point, edge_point_bc)
* for a quad face (a,b,c,d):
(a, edge_point_ab, face_point, edge_point_da)
(b, edge_point_bc, face_point, edge_point_ab)
(c, edge_point_cd, face_point, edge_point_bc)
(d, edge_point_da, face_point, edge_point_cd)

This process is relevant when there are no holes in the surface.

When there is a hole, we can detect it as follow:
* an edge is the border of a hole if it belongs to only one face,
* a point is on the border of a hole if n1 != n2 with n1 the number of faces the point belongs to, and n2 the number of edges a point belongs to.

On the border of a hole the subdivision occurs as follow:
# for the edges that are on the border of a hole, the edge point is just the middle of the edge.
# for the vertex points that are on the border of a hole, the new coordinates are calculated as follow:
## in all the edges the point belongs to, only take in account the middles of the edges that are on the border of the hole then calculate the average between these points and the old coordinates.



([[wp:Catmull–Clark_subdivision_surface|Description on Wikipedia]].)


=={{header|OCaml}}==
=={{header|OCaml}}==