Repository: godotengine/godot
File: modules/csg/csg.cpp (C++)

Code before:
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "csg.h"

#include "core/math/geometry_2d.h"
#include "core/math/math_funcs.h"
#include "core/templates/sort_array.h"

// Static helper functions.

inline static bool is_snapable(const Vector3 &p_point1, const Vector3 &p_point2, real_t p_distance) {
	return (p_point1 - p_point2).length_squared() < p_distance * p_distance;
}

inline static Vector2 interpolate_segment_uv(const Vector2 p_segment_points[2], const Vector2 p_uvs[2], const Vector2 &p_interpolation_point) {
	float segment_length = (p_segment_points[1] - p_segment_points[0]).length();
	if (p_segment_points[0].is_equal_approx(p_segment_points[1])) {
		return p_uvs[0];
	}

	float distance = (p_interpolation_point - p_segment_points[0]).length();
	float fraction = distance / segment_length;

	return p_uvs[0].lerp(p_uvs[1], fraction);
}

inline static Vector2 interpolate_triangle_uv(const Vector2 p_vertices[3], const Vector2 p_uvs[3], const Vector2 &p_interpolation_point) {
	if (p_interpolation_point.is_equal_approx(p_vertices[0])) {
		return p_uvs[0];
	}
	if (p_interpolation_point.is_equal_approx(p_vertices[1])) {
		return p_uvs[1];
	}
	if (p_interpolation_point.is_equal_approx(p_vertices[2])) {
		return p_uvs[2];
	}

	Vector2 edge1 = p_vertices[1] - p_vertices[0];
	Vector2 edge2 = p_vertices[2] - p_vertices[0];
	Vector2 interpolation = p_interpolation_point - p_vertices[0];

	float edge1_on_edge1 = edge1.dot(edge1);
	float edge1_on_edge2 = edge1.dot(edge2);
	float edge2_on_edge2 = edge2.dot(edge2);

Review comment (suggestion):
This can be improved further:

```suggestion
	if (p_segment_points[0].is_equal_approx(p_segment_points[1])) {
		return p_uvs[0];
	}
	float segment_length = p_segment_points[0].distance_to(p_segment_points[1]);
```

Code after:
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "csg.h"

#include "core/math/geometry_2d.h"
#include "core/math/math_funcs.h"
#include "core/templates/sort_array.h"

// Static helper functions.

inline static bool is_snapable(const Vector3 &p_point1, const Vector3 &p_point2, real_t p_distance) {
	return p_point2.distance_squared_to(p_point1) < p_distance * p_distance;
}

inline static Vector2 interpolate_segment_uv(const Vector2 p_segment_points[2], const Vector2 p_uvs[2], const Vector2 &p_interpolation_point) {
	if (p_segment_points[0].is_equal_approx(p_segment_points[1])) {
		return p_uvs[0];
	}

	float segment_length = p_segment_points[0].distance_to(p_segment_points[1]);
	float distance = p_segment_points[0].distance_to(p_interpolation_point);
	float fraction = distance / segment_length;

	return p_uvs[0].lerp(p_uvs[1], fraction);
}

inline static Vector2 interpolate_triangle_uv(const Vector2 p_vertices[3], const Vector2 p_uvs[3], const Vector2 &p_interpolation_point) {
	if (p_interpolation_point.is_equal_approx(p_vertices[0])) {
		return p_uvs[0];
	}
	if (p_interpolation_point.is_equal_approx(p_vertices[1])) {
		return p_uvs[1];
	}
	if (p_interpolation_point.is_equal_approx(p_vertices[2])) {
		return p_uvs[2];
	}

	Vector2 edge1 = p_vertices[1] - p_vertices[0];
	Vector2 edge2 = p_vertices[2] - p_vertices[0];
	Vector2 interpolation = p_interpolation_point - p_vertices[0];

	float edge1_on_edge1 = edge1.dot(edge1);
	float edge1_on_edge2 = edge1.dot(edge2);
	float edge2_on_edge2 = edge2.dot(edge2);