Repository: godotengine/godot
File: editor/plugins/gizmos/navigation_link_3d_gizmo_plugin.cpp (C++)

Code before:

void NavigationLink3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
	NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d());

	RID nav_map = link->get_world_3d()->get_navigation_map();
	real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map);
	Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map);
	Vector3::Axis up_axis = up_vector.max_axis_index();

	Vector3 start_position = link->get_start_position();
	Vector3 end_position = link->get_end_position();

	Ref<Material> link_material = get_material("navigation_link_material", p_gizmo);
	Ref<Material> link_material_disabled = get_material("navigation_link_material_disabled", p_gizmo);
	Ref<Material> handles_material = get_material("handles");

	p_gizmo->clear();

	// Draw line between the points.
	Vector<Vector3> lines;
	lines.append(start_position);
	lines.append(end_position);

	// Draw start position search radius
	for (int i = 0; i < 30; i++) {
		// Create a circle
		const float ra = Math::deg_to_rad((float)(i * 12));
		const float rb = Math::deg_to_rad((float)((i + 1) * 12));
		const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * search_radius;
		const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * search_radius;

		// Draw axis-aligned circle
		switch (up_axis) {
			case Vector3::AXIS_X:
				lines.append(start_position + Vector3(0, a.x, a.y));
				lines.append(start_position + Vector3(0, b.x, b.y));
				break;
			case Vector3::AXIS_Y:
				lines.append(start_position + Vector3(a.x, 0, a.y));
				lines.append(start_position + Vector3(b.x, 0, b.y));
				break;
			case Vector3::AXIS_Z:
				lines.append(start_position + Vector3(a.x, a.y, 0));
				lines.append(start_position + Vector3(b.x, b.y, 0));
				break;
		}
	}

	// Draw end position search radius
	for (int i = 0; i < 30; i++) {
		// Create a circle

Review comment (suggestion):
Prefer a more explicit naming like `lines_ptrw` instead of just `ptr` as most code views and reviews happen on (web) git without an IDE crutch at hand.

The name makes it clear when skimming over the code base what Vector / LocalVector it is and the ptr / ptrw at the end makes it clear if it is a read or write pointer instead of constantly having to scroll to look it up.

Code after:

void NavigationLink3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
	NavigationLink3D *link = Object::cast_to<NavigationLink3D>(p_gizmo->get_node_3d());

	RID nav_map = link->get_world_3d()->get_navigation_map();
	real_t search_radius = NavigationServer3D::get_singleton()->map_get_link_connection_radius(nav_map);
	Vector3 up_vector = NavigationServer3D::get_singleton()->map_get_up(nav_map);
	Vector3::Axis up_axis = up_vector.max_axis_index();

	Vector3 start_position = link->get_start_position();
	Vector3 end_position = link->get_end_position();

	Ref<Material> link_material = get_material("navigation_link_material", p_gizmo);
	Ref<Material> link_material_disabled = get_material("navigation_link_material_disabled", p_gizmo);
	Ref<Material> handles_material = get_material("handles");

	p_gizmo->clear();

	// Number of points in an octant. So there ill be 8 * points_in_octant points in total.
	// Correspond to the smoothness of the circle.
	const uint32_t points_in_octant = 4;
	real_t inc = (Math_PI / (4 * points_in_octant));

	Vector<Vector3> lines;
	// points_in_octant * 8 * 2 per circle * 2 circles. 2 for the start-end. 4 for the arrow, and another 4 if bidirectionnal.
	lines.resize(points_in_octant * 8 * 2 * 2 + 2 + 4 + (link->is_bidirectional() ? 4 : 0));
	uint32_t index = 0;
	Vector3 *lines_ptrw = lines.ptrw();
	// Draw line between the points.
	lines_ptrw[index++] = start_position;
	lines_ptrw[index++] = end_position;
	real_t search_radius_squared = search_radius * search_radius;

	// Draw circles at start and end positions in one go.
	real_t r = 0;
	Vector2 a = Vector2(search_radius, 0);
	for (uint32_t i = 0; i < points_in_octant; i++) {
		r += inc;
		real_t x = Math::cos(r) * search_radius;
		real_t y = Math::sqrt(search_radius_squared - (x * x));

		// Draw axis-aligned circle.
		switch (up_axis) {
			case Vector3::AXIS_X:
#define PUSH_OCTANT(_position, a, b)                          \
	lines_ptrw[index++] = _position + Vector3(0, a.x, a.y);   \
	lines_ptrw[index++] = _position + Vector3(0, x, y);       \
	lines_ptrw[index++] = _position + Vector3(0, -a.x, a.y);  \
	lines_ptrw[index++] = _position + Vector3(0, x, y);       \
	lines_ptrw[index++] = _position + Vector3(0, a.x, -a.y);  \
	lines_ptrw[index++] = _position + Vector3(0, x, -y);      \