Commit d0b7f2bb authored by Zsanet Sueli's avatar Zsanet Sueli
Browse files

added missing files

parent 18eeff87
Loading
Loading
Loading
Loading

Assets/ArcLength.cs

deleted100644 → 0
+0 −210
Original line number Diff line number Diff line

using System;
using System.Collections.Generic;
using UnityEngine;

public class ArcLength
{

    private readonly double aMax;
    private readonly int aSize;
    private readonly double xMax;
    private readonly int xSize;
    private readonly double lMax;
    private readonly int lSize;
    private List<List<double>> lut;
    private List<List<double>> inverseLut;

    /// <summary>
    /// The ArcLength class approximates the arc length of a curve and its inverse using lookup tables.
    /// </summary>
    /// <param name="aMax">Maximum parameter of the function y = a * x^2.</param>
    /// <param name="aSize">Number of steps from 0 to aMax.</param>
    /// <param name="xMax">Maximum x-coordinate.</param>
    /// <param name="xSize">Number of steps from 0 to xMax.</param>
    /// <param name="lMax">Maximum curve length.</param>
    /// <param name="lSize">Number of steps from 0 to lMax.</param>
    /// <param name="epsilon">Precision of the inverse LUT.</param>
    public ArcLength(double aMax = 0.2, int aSize = 256, double xMax = 1000, int xSize = 256, double lMax = 1000, int lSize = 256, double epsilon = 1e-6)
    {
        this.aMax = aMax;
        this.aSize = aSize;
        this.xMax = xMax;
        this.xSize = xSize;
        this.lMax = lMax;
        this.lSize = lSize;
        lut = new List<List<double>>();
        inverseLut = new List<List<double>>();

        for (int i = 0; i < aSize; i++)
        {
            double aParam = i * aMax / (aSize - 1);
            lut.Add(new List<double>());
            inverseLut.Add(new List<double>());
            for (int j = 0; j < xSize; j++)
            {
                double x = j * xMax / (xSize - 1);
                lut[i].Add(ArcFunc(aParam, x));
            }
            for (int j = 0; j < lSize; j++)
            {
                double l = j * lMax / (lSize - 1);
                inverseLut[i].Add(BisectionSearch(aParam, l, epsilon));
            }
        }
    }

    /// <summary>
    /// Approximates the arc length of the curve y = a * x^2 from 0 to x.
    /// Values between two LUT entries are interpolated.
    /// </summary>
    /// <param name="aParam">Parameter of the function y = a * x^2.</param>
    /// <param name="x">X-coordinate of the function y = a * x^2.</param>
    /// <returns>Length of the arc.</returns>
    public double LengthFunction(double aParam, double x)
    {
        // Find the two closest values of a and x in the LUT
        int aIndex = (int)Math.Floor(aParam * (aSize - 1) / aMax);
        int xIndex = (int)Math.Floor(x * (xSize - 1) / xMax);

        // Compute the arc length using bilinear interpolation
        double a0 = aIndex * aMax / (aSize - 1);
        double a1 = (aIndex + 1) * aMax / (aSize - 1);
        double x0 = xIndex * xMax / (xSize - 1);
        double x1 = (xIndex + 1) * xMax / (xSize - 1);

        double l00 = lut[aIndex][xIndex];
        double l01 = lut[aIndex][xIndex + 1];
        double l10 = lut[aIndex + 1][xIndex];
        double l11 = lut[aIndex + 1][xIndex + 1];

        double l0 = l00 + (l01 - l00) * (x - x0) / (x1 - x0);
        double l1 = l10 + (l11 - l10) * (x - x0) / (x1 - x0);

        return l0 + (l1 - l0) * (aParam - a0) / (a1 - a0);
    }

    /// <summary>
    /// Approximates the x-coordinate of the curve y = a * x^2 given an arc length from 0 to l.
    /// </summary>
    /// <param name="aParam">Parameter of the function y = a * x^2.</param>
    /// <param name="l">Length of the arc.</param>
    /// <returns>X-coordinate of the function y = a * x^2.</returns>
    public double InverseLengthFunction(double aParam, double l)
    {
        // Find the two closest values of a and l in the LUT
        int aIndex = (int)Math.Floor(aParam * (aSize - 1) / aMax);
        int lIndex = (int)Math.Floor(l * (lSize - 1) / lMax);

        // Compute the x value using bilinear interpolation
        double a0 = aIndex * aMax / (aSize - 1);
        double a1 = (aIndex + 1) * aMax / (aSize - 1);
        double l0 = lIndex * lMax / (lSize - 1);
        double l1 = (lIndex + 1) * lMax / (lSize - 1);

        double x00 = inverseLut[aIndex][lIndex];
        double x01 = inverseLut[aIndex][lIndex + 1];
        double x10 = inverseLut[aIndex + 1][lIndex];
        double x11 = inverseLut[aIndex + 1][lIndex + 1];

        double x0 = x00 + (x01 - x00) * (l - l0) / (l1 - l0);
        double x1 = x10 + (x11 - x10) * (l - l0) / (l1 - l0);

        return x0 + (x1 - x0) * (aParam - a0) / (a1 - a0);
    }

    /// <summary>
    /// Arc length function for the curve y = a * x^2.
    /// </summary>
    /// <param name="aParam"></param>
    /// <param name="x"></param>
    /// <returns></returns>
    private double ArcFunc(double aParam, double x)
    {
        if (aParam == 0)
        {
            return x;
        }
        return (2 * aParam * x * Math.Sqrt(1 + Math.Pow(2 * aParam * x, 2)) + Math.Asinh(2 * aParam * x)) / (4 * aParam);
    }

    /// <summary>
    /// Bisection search to find the x-coordinate of the curve y = a * x^2 given an arc length from 0 to l.
    /// </summary>
    /// <param name="aParam"></param>
    /// <param name="l"></param>
    /// <param name="epsilon">Precision of the computation.</param>
    /// <returns></returns>
    private double BisectionSearch(double aParam, double l, double epsilon)
    {
        double lower_bound = 0.0;
        double upper_bound = l > 1 ? l : 2;

        while (upper_bound - lower_bound > epsilon)
        {
            double mid = (lower_bound + upper_bound) / 2;
            if (ArcFunc(aParam, mid) < l)
            {
                lower_bound = mid;
            }
            else
            {
                upper_bound = mid;
            }
        }

        return (lower_bound + upper_bound) / 2;
    }

    public void ExportArcLengthFile(string filename)
    {
        ExportLUTFile(lut, filename);
    }

    public void ExportInverseArcLengthFile(string filename)
    {
        ExportLUTFile(inverseLut, filename);
    }

    /// <summary>
    /// Exports the arc length LUT to an image file.
    /// </summary>
    /// <returns>True if the image was successfully saved, false otherwise.</returns>
    private void ExportLUTFile(List<List<double>> lut, string filename)
    {
        Texture2D texture = ExportLUTTexture(lut);
        byte[] bytes = texture.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat);
        System.IO.File.WriteAllBytes(filename, bytes);
    }

    public Texture2D GetArcLengthTexture()
    {
        return ExportLUTTexture(lut);
    }

    public Texture2D GetInverseArcLengthTexture()
    {
        return ExportLUTTexture(inverseLut);
    }

    /// <summary>
    /// Exports the arc length LUT to a Texture2D object.
    /// </summary>
    /// <returns>Texture2D object containing the LUT.</returns>
    private Texture2D ExportLUTTexture(List<List<double>> lut)
    {
        int width = lut[0].Count;
        int height = lut.Count;
        Texture2D texture = new(width, height, TextureFormat.RFloat, false);

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                double value = lut[i][j];
                texture.SetPixel(j, i, new Color((float)value, 0, 0, 1));
            }
        }
        return texture;
    }
}
 No newline at end of file

Assets/ArcLength.cs.meta

deleted100644 → 0
+0 −11
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 324b1fac8e1b91546b1c9f5bedaf8fda
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData: 
  assetBundleName: 
  assetBundleVariant: 

Assets/Brno.meta

deleted100644 → 0
+0 −8
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: b6517c3356ebbaf478ba2841600b8e5e
folderAsset: yes
DefaultImporter:
  externalObjects: {}
  userData: 
  assetBundleName: 
  assetBundleVariant: 
−118 MiB

File deleted.

+0 −109
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: b4bb9d1152ee4d74eba3ea2279aad501
ModelImporter:
  serializedVersion: 22200
  internalIDToNameTable: []
  externalObjects: {}
  materials:
    materialImportMode: 2
    materialName: 0
    materialSearch: 1
    materialLocation: 1
  animations:
    legacyGenerateAnimations: 4
    bakeSimulation: 0
    resampleCurves: 1
    optimizeGameObjects: 0
    removeConstantScaleCurves: 0
    motionNodeName: 
    rigImportErrors: 
    rigImportWarnings: 
    animationImportErrors: 
    animationImportWarnings: 
    animationRetargetingWarnings: 
    animationDoRetargetingWarnings: 0
    importAnimatedCustomProperties: 0
    importConstraints: 0
    animationCompression: 1
    animationRotationError: 0.5
    animationPositionError: 0.5
    animationScaleError: 0.5
    animationWrapMode: 0
    extraExposedTransformPaths: []
    extraUserProperties: []
    clipAnimations: []
    isReadable: 0
  meshes:
    lODScreenPercentages: []
    globalScale: 1
    meshCompression: 0
    addColliders: 0
    useSRGBMaterialColor: 1
    sortHierarchyByName: 1
    importPhysicalCameras: 1
    importVisibility: 1
    importBlendShapes: 1
    importCameras: 1
    importLights: 1
    nodeNameCollisionStrategy: 1
    fileIdsGeneration: 2
    swapUVChannels: 0
    generateSecondaryUV: 0
    useFileUnits: 1
    keepQuads: 0
    weldVertices: 1
    bakeAxisConversion: 0
    preserveHierarchy: 0
    skinWeightsMode: 0
    maxBonesPerVertex: 4
    minBoneWeight: 0.001
    optimizeBones: 1
    meshOptimizationFlags: -1
    indexFormat: 0
    secondaryUVAngleDistortion: 8
    secondaryUVAreaDistortion: 15.000001
    secondaryUVHardAngle: 88
    secondaryUVMarginMethod: 1
    secondaryUVMinLightmapResolution: 40
    secondaryUVMinObjectScale: 1
    secondaryUVPackMargin: 4
    useFileScale: 1
    strictVertexDataChecks: 0
  tangentSpace:
    normalSmoothAngle: 60
    normalImportMode: 0
    tangentImportMode: 3
    normalCalculationMode: 4
    legacyComputeAllNormalsFromSmoothingGroupsWhenMeshHasBlendShapes: 0
    blendShapeNormalImportMode: 1
    normalSmoothingSource: 0
  referencedClips: []
  importAnimation: 1
  humanDescription:
    serializedVersion: 3
    human: []
    skeleton: []
    armTwist: 0.5
    foreArmTwist: 0.5
    upperLegTwist: 0.5
    legTwist: 0.5
    armStretch: 0.05
    legStretch: 0.05
    feetSpacing: 0
    globalScale: 1
    rootMotionBoneName: 
    hasTranslationDoF: 0
    hasExtraRoot: 0
    skeletonHasParents: 1
  lastHumanDescriptionAvatarSource: {instanceID: 0}
  autoGenerateAvatarMappingIfUnspecified: 1
  animationType: 2
  humanoidOversampling: 1
  avatarSetup: 0
  addHumanoidExtraRootOnlyWhenUsingAvatar: 1
  importBlendShapeDeformPercent: 1
  remapMaterialsIfMaterialImportModeIsNone: 0
  additionalBone: 0
  userData: 
  assetBundleName: 
  assetBundleVariant: 
Loading