| 11 | using namespace PyMesh; |
| 12 | |
| 13 | void FaceAspectRatioAttribute::compute_from_mesh(Mesh& mesh) { |
| 14 | const size_t num_vertex_per_face = mesh.get_vertex_per_face(); |
| 15 | if (num_vertex_per_face != 3 && num_vertex_per_face != 4) { |
| 16 | throw NotImplementedError( |
| 17 | "Only triangle and quad aspect ratio is supported"); |
| 18 | } |
| 19 | |
| 20 | if (!mesh.has_attribute("edge_length")) { |
| 21 | mesh.add_attribute("edge_length"); |
| 22 | } |
| 23 | const VectorF& edge_length = mesh.get_attribute("edge_length"); |
| 24 | |
| 25 | const size_t num_faces = mesh.get_num_faces(); |
| 26 | |
| 27 | VectorF& aspect_ratios = m_values; |
| 28 | aspect_ratios = VectorF::Zero(num_faces); |
| 29 | |
| 30 | if (num_vertex_per_face == 3) { |
| 31 | // For triangle, aspect ratio is the ratio of circumradius to twice the |
| 32 | // incircle radius. |
| 33 | for (size_t i=0; i<num_faces; i++) { |
| 34 | const Float a = edge_length[i*3+1]; |
| 35 | const Float b = edge_length[i*3+2]; |
| 36 | const Float c = edge_length[i*3+0]; |
| 37 | const Float s = (a+b+c) / 2.0; |
| 38 | |
| 39 | if (s == a+b || s == b+c || s == c+a) { |
| 40 | aspect_ratios[i] = std::numeric_limits<Float>::infinity(); |
| 41 | } else { |
| 42 | aspect_ratios[i] = a*b*c/(8*(a+b-s)*(b+c-s)*(c+a-s)); |
| 43 | } |
| 44 | } |
| 45 | } else { |
| 46 | // For quad, aspect ratio is the ratio of the longest edge to the |
| 47 | // shortest edge. |
| 48 | for (size_t i=0; i<num_faces; i++) { |
| 49 | VectorF side_lengths = edge_length.segment( |
| 50 | i*num_vertex_per_face, num_vertex_per_face); |
| 51 | const Float min_edge = side_lengths.minCoeff(); |
| 52 | const Float max_edge = side_lengths.maxCoeff(); |
| 53 | if (min_edge == 0.0) { |
| 54 | aspect_ratios[i] = std::numeric_limits<Float>::infinity(); |
| 55 | } else { |
| 56 | aspect_ratios[i] = max_edge / min_edge; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (!aspect_ratios.allFinite()) { |
| 62 | std::cerr << "Warning: degenerate triangle has infinite aspect ratio" |
| 63 | << std::endl; |
| 64 | } |
| 65 | } |
nothing calls this directly
no test coverage detected