/// <summary>
    /// Simplify the drawn line and assign it to the road editor.
    /// </summary>
    private void finalizeLine()
    {
        MeshRoad road = (MeshRoad)target;

        List <Vector3> simplifiedLine = LineSmoother.biasedMovingAverages(roadLinePoints, road.averageWindow);

        road.setRoadLinePoints(simplifiedLine);
        roadLinePoints.Clear(); // No need to keep this data twice

        road.debugRoadLine();
    }
    /// <summary>
    /// Create a new cross section.
    /// </summary>
    /// <param name="center">Center point of this cross section.</param>
    /// <param name="direction">Direction this cross section is facing.</param>
    /// <param name="parent">Road to which this cross section belongs.</param>
    public MeshRoadCrossSection(Vector3 center, Vector3 direction, MeshRoad parent)
    {
        float halfWidth  = parent.width / 2;
        float halfHeight = parent.thickness / 2;

        Vector3 perpendicular        = Vector3.Cross(direction, parent.gameObject.transform.up).normalized;
        Vector3 upwardsPerpendicular = Vector3.Cross(perpendicular, direction).normalized;

        Vector3 left = center + (perpendicular * halfWidth);

        topLeft    = left + (upwardsPerpendicular * halfHeight);
        bottomLeft = left - (upwardsPerpendicular * halfHeight);

        Vector3 right = center - (perpendicular * halfWidth);

        topRight    = right + (upwardsPerpendicular * halfHeight);
        bottomRight = right - (upwardsPerpendicular * halfHeight);
    }
    /// <summary>
    /// Draw custom inspector.
    /// </summary>
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        MeshRoad road = (MeshRoad)target;

        // Target layer dropdown
        targetLayer = EditorGUILayout.Popup("Target Layer", targetLayer, getLayerNames());

        // Add features to draw road
        drawingEnabled = EditorGUILayout.Toggle("Enable Drawing", drawingEnabled);

        // Add button to generate road
        if (GUILayout.Button("Generate Road"))
        {
            road.generate();
        }

        // Add button to fit associated terrains to this road
        if (GUILayout.Button("Fit Terrains"))
        {
            road.fitTerrains();
        }

        // Add button to clear road
        if (GUILayout.Button("Clear Road"))
        {
            road.clear();
        }

        // Add debug buttons
        if (GUILayout.Button("Debug Road Line"))
        {
            road.debugRoadLine();
        }
        if (GUILayout.Button("Debug Cross Sections"))
        {
            road.debugCrossSections();
        }
    }
    /// <summary>
    /// Draw a road line in the editor, if drawing is enabled.
    /// </summary>
    private void OnSceneGUI()
    {
        MeshRoad road = (MeshRoad)target;

        if (drawingEnabled)
        {
            // Maintain focus for this element while drawing is enabled
            int controlID = GUIUtility.GetControlID(FocusType.Passive);
            GUIUtility.hotControl = controlID;

            Vector2 mousePosition = Event.current.mousePosition;
            switch (Event.current.type)
            {
            case EventType.MouseDown:
                roadLinePoints.Clear();
                roadLinePoints.Add(editorToWorldPoint(mousePosition));

                Event.current.Use();
                break;

            case EventType.MouseDrag:
                roadLinePoints.Add(editorToWorldPoint(mousePosition));

                // Useful debug - draw the last line segment
                Debug.DrawLine(roadLinePoints[roadLinePoints.Count - 2], roadLinePoints[roadLinePoints.Count - 1], Color.cyan, 5, false);

                Event.current.Use();
                break;

            case EventType.MouseUp:
                // Finish drawing
                drawingEnabled = false;
                finalizeLine();

                Event.current.Use();
                break;
            }
        }
    }