autodesk forge – calculate the slope for 3D faces in a terrain mesh using AutoCAD Architecture
I have created a terrain mesh in AutoCAD Architecture using contour line. mesh is create successfully. But when I tried to calculate the slope angle of the each 3d face. I’m getting the wrong values. i have used below code
Vector3d v1 = P2 - P1;
Vector3d v2 = P3 - P1;
// Compute normal
Vector3d normal = v1.CrossProduct(v2);
double area = normal.Length / 2.0;
normal = normal.GetNormal();
// Vertical vector (Z up)
Vector3d vertical = new Vector3d(0, 0, 1);
// Dot product and clamp
double dot = normal.DotProduct(vertical);
dot = Math.Max(-1.0, Math.Min(1.0, dot));
// Compute slope angle in degrees (use absolute value to handle orientation)
double angle = Math.Acos(Math.Abs(dot)) * (180.0 / Math.PI);
// Compute slope percent (rise/run * 100)
double slopePercent = Math.Tan(angle * Math.PI / 180.0) * 100.0;
// Log all info for debugging
Console.WriteLine("Triangle vertices:");
Console.WriteLine($"P1: {P1}");
Console.WriteLine($"P2: {P2}");
Console.WriteLine($"P3: {P3}");
Console.WriteLine($"Normal Vector: {normal}");
Console.WriteLine($"Dot product with vertical: {dot}");
Console.WriteLine($"Slope angle (degrees): {angle:F3}");
Console.WriteLine($"Slope percent: {slopePercent:F3}%");
Console.WriteLine($"Triangle area: {area:F6} units²");
return angle;
the value I’m getting were
-
P1 = {(600727.0463154223,2244285.9532928583,582.1227712690642)}
-
P2 = {(600734.5463154223,2244285.9532928583,582.0305161676536)}
-
P3 = {(600727.0463154223,2244293.4532928583,582.1351201801201)}
-
Normal Vector = {(0.01229973303584751,-0.0016463946919902494,0.9999230000113833)}
-
Dot product with vertical = 0.99992300001138334
-
Slope angle (degrees) = 0.71102643496152385
-
Slope percent = 1.2410389440076321
-
Triangle area = 28.127165791445758
this are the value I’m getting for a 3d face. when i compare with other tools im not getting accurate values.
Read more here: Source link
