Skip to content

Commit 9077e59

Browse files
committed
Finalize implementation for RecalculateCurveDirections
1 parent 38d0c6f commit 9077e59

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Unity.Mathematics;
4+
5+
namespace GeometryGraph.Runtime.Curve {
6+
public static partial class CurveOperations {
7+
public static CurveData RecalculateDirectionVectors(CurveData originalCurve, RecalculateCurveDirectionsSettings settings) {
8+
if (originalCurve == null || originalCurve.Points == 0) return CurveData.Empty;
9+
10+
List<float3> position = originalCurve.Position.ToList();
11+
List<float3> tangent = originalCurve.Tangent.ToList();
12+
List<float3> normal = originalCurve.Normal.ToList();
13+
List<float3> binormal = originalCurve.Binormal.ToList();
14+
15+
float tangentMultiplier = settings.FlipTangents ? -1 : 1;
16+
float normalMultiplier = settings.FlipNormals ? -1 : 1;
17+
float binormalMultiplier = settings.FlipBinormals ? -1 : 1;
18+
19+
// Recalculate tangents
20+
for (int i = 0; i < originalCurve.Points - 1; i++) {
21+
tangent[i] = math.normalize(position[i + 1] - position[i]);
22+
}
23+
24+
if (originalCurve.IsClosed) {
25+
tangent[^1] = math.normalize(position[0] - position[^1]);
26+
} else {
27+
tangent[^1] = tangent[^2];
28+
}
29+
30+
// Recalculate normals
31+
for (int i = 0; i < originalCurve.Points; i++) {
32+
normal[i] = math.normalize(math.cross(binormal[i], tangent[i]));
33+
}
34+
35+
// Recalculate binormals
36+
for (int i = 0; i < originalCurve.Points; i++) {
37+
binormal[i] = math.normalize(math.cross(tangent[i], normal[i]));
38+
}
39+
40+
// Apply multipliers
41+
for (int i = 0; i < originalCurve.Points; i++) {
42+
tangent[i] *= tangentMultiplier;
43+
normal[i] *= normalMultiplier;
44+
binormal[i] *= binormalMultiplier;
45+
}
46+
47+
return new CurveData(originalCurve.Type, position.Count, originalCurve.IsClosed, position, tangent, normal, binormal);
48+
}
49+
}
50+
}

UnityGeometryGraph/Assets/GeometryGraph/Runtime/Curve/Operations/CurveOperations_RecalculateDirections.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GeometryGraph.Runtime.Curve {
2+
public readonly struct RecalculateCurveDirectionsSettings {
3+
public bool FlipTangents { get; }
4+
public bool FlipNormals { get; }
5+
public bool FlipBinormals { get; }
6+
7+
public RecalculateCurveDirectionsSettings(bool flipTangents, bool flipNormals, bool flipBinormals) {
8+
FlipTangents = flipTangents;
9+
FlipNormals = flipNormals;
10+
FlipBinormals = flipBinormals;
11+
}
12+
}
13+
}

UnityGeometryGraph/Assets/GeometryGraph/Runtime/Curve/Operations/RecalculateCurveDirectionsSettings.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy