DXF文件-法线矢量和相对于此的位置如何提供对块位置的全面理解



几天来,我一直在为如何在DXF中完全描述要插入的块而绞尽脑汁。作为背景,我对如何做到这一点的规则和行为感兴趣,而不仅仅是试图找到一个现有的图书馆。

我可以用dxf创建一个块,假设这是一个简单的立方体。然后,我可以在"实体"部分插入此块。为此,我输入相对于对象坐标系代码的坐标:(10、20、30(,以及对象代码的法向量:(210220230(。还有一个值要绕法线向量代码旋转:50。

所以有两件事要做:

  1. 计算对象的法线向量
  2. 计算对象坐标,给定对象的世界坐标

要计算法向量,如果应用了偏航角、俯仰角或滚转角,我会使用四元数来计算应用于世界z轴(0,0,1(的旋转(如果没有,我只使用世界z轴(。我可以使用相同的四元数来计算任意的x和y轴,对现实世界中的x和y轴使用相同的旋转。对于那些寻找更多信息的人来说,这个链接对如何计算它们有一个非常清晰的解释。https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html

为了帮助您,这个计算器可以通过检查结果来帮助确认这是否已经正确实施:

旋转计算器

然后,为了计算对象坐标,我只需将世界坐标与每个任意轴/法线轴交叉相乘。

对于大多数情况,这似乎是可行的。然而,我对其他规则和要求感到不知所措,我在任何地方都看不到这些规则和要求:

  1. 如果只应用偏航角(绕z旋转(,则法线仍然是(0,0,1(。在俯仰和滚转为零的情况下,我们需要应用绕z的旋转。

  2. 如果我改变音高,法向量可以从负y坐标变为正坐标。这种斜率的变化似乎会影响矢量的方向,如果是正的,则需要绕180度的法线旋转来校正

  3. 由于某种未知的原因,当我应用滚转角时,我的对象绕法线旋转了90度,我需要在这里应用校正。

我很难在网上找到更明确的方向。有人对上述行为有任何详尽的解释吗,或者有任何指针材料吗?

正如另一个主题中所建议的,一点矩阵演算可以解决这个问题。

很快,它们主要是描述相同欧拉角的两种方法:

  • Tait-Bryan绕X、Y、Z轴的角度(偏航、俯仰、滚转(旋转
  • 绕Z、X、Z轴的适当欧拉角(进动、章动、内旋(旋转;AutoCAD使用第二个来描述块方向。进动和章动的组合提供OCS变换矩阵,并且固有旋转是块的旋转值

一个用3x3矩阵描述三维旋转的F#小例子。

type vector = float list
module Vector =
// Computes the dot product of two vectors
let dotProduct (v1: vector) (v2: vector) = List.map2 (*) v1 v2 |> List.sum
type matrix(rows: vector list) =
// Gets the row vectors
member _.Rows = rows
// Transposes a matrix
member _.Transpose = List.transpose rows |> matrix
// Applies a matrix to a vector
static member (*)(m: matrix, v: vector) =
List.map (Vector.dotProduct v) m.Rows
// Multipies two matrices
static member (*)(m: matrix, q: matrix) =
let trp = q.Transpose
List.map (fun r -> trp * r) m.Rows |> matrix
// Describes a coordinate system data
type CoordinateSystem =
{ WcsToOcs: matrix
Normal: vector
Rotation: float }
// Matrix 3x3
module Matrix3x3 =
// Gets the identity matrix
let identity =
matrix [ [ 1.0; 0.0; 0.0 ]
[ 0.0; 1.0; 0.0 ]
[ 0.0; 0.0; 1.0 ] ]
// Gets the rotation matrix about X axis
let xRotation a =
matrix [ [ 1.0; 0.0; 0.0 ]
[ 0.0; cos a; -sin a ]
[ 0.0; sin a; cos a ] ]
// Gets the rotation matrix about Y axis
let yRotation a =
matrix [ [ cos a; 0.0; sin a ]
[ 0.0; 1.0; 0.0 ]
[ -sin a; 0.0; cos a ] ]
// Gets the rotation matrix about Z axis
let zRotation a =
matrix [ [ cos a; -sin a; 0.0 ]
[ sin a; cos a; 0.0 ]
[ 0.0; 0.0; 1.0 ] ]
// Creates the matrix according to Yaw, Pitch and Roll values
let createFromYawPitchRoll yaw pitch roll =
zRotation yaw * yRotation pitch * xRotation roll
// Gets the coordinate system data from a matrix 3x3
let getCoordinateSystem (mat: matrix) =
match mat.Rows with
| [ [ m00; m01; m02 ]; [ m10; m11; m12 ]; [ m20; m21; m22 ] ] ->
let nutation = acos m22
if abs nutation < 1e-8 then
{ WcsToOcs = identity
Normal = [ 0.0; 0.0; 1.0 ]
Rotation = atan2 m10 m11 }
else
let precession = atan2 m02 -m12
let spin = atan2 m20 m21
let xform =
(zRotation precession * xRotation nutation)
.Transpose
let normal = xform.Rows.Item 2
{ WcsToOcs = xform
Normal = normal
Rotation = spin }
| _ -> invalidArg "mat" "Invalid 3x3 matrix"
// Testing
module test =
let radians x = x * System.Math.PI / 180.0
// Input yaw, pitch, roll angles and WCS point
let yaw = radians 10.0
let pitch = radians 20.0
let roll = radians 30.0
let wcsPoint = [ 8.0; 5.0; 3.0 ]
// Computation of the coordinate system
let ocs =
Matrix3x3.createFromYawPitchRoll yaw pitch roll
|> Matrix3x3.getCoordinateSystem
let ocsPoint = ocs.WcsToOcs * wcsPoint
// Print results
printfn "Normal X (210): %f" (ocs.Normal.Item 0)
printfn "Normal Y (220): %f" (ocs.Normal.Item 1)
printfn "Normal Z (230): %f" (ocs.Normal.Item 2)
printfn "Rotation (50): %f" ocs.Rotation
printfn "OCS point X (10): %f" (ocsPoint.Item 0)
printfn "OCS point Y (20): %f" (ocsPoint.Item 1)
printfn "OCS point Z (30): %f" (ocsPoint.Item 2)

C#实现:

using static System.Math;
using static System.Console;
namespace CsharpMatrix3d
{
class Program
{
/// <summary>
/// Console testing
/// </summary>
static void Main()
{
double yaw = D2R(10.0);
double pitch = D2R(20.0);
double roll = D2R(30.0);
var wcsPoint = new Vector(8.0, 5.0, 3.0);
var ocs = ObjectCoordinateSystem.FromYawPitchRoll(yaw, pitch, roll);
var ocsPoint = ocs.WorldToPlane * wcsPoint;
WriteLine($"Normal X (210): {ocs.Normal.X}");
WriteLine($"Normal Y (220): {ocs.Normal.Y}");
WriteLine($"Normal Z (230): {ocs.Normal.Z}");
WriteLine($"Rotation (50): {ocs.Rotation}");
WriteLine($"OCS point X (10): {ocsPoint.X}");
WriteLine($"OCS point Y (20): {ocsPoint.Y}");
WriteLine($"OCS point Z (30): {ocsPoint.Z}");
}
static double D2R(double x) => x * PI / 180.0;
}
/// <summary>
/// Provides properties and method for an object coordinate system
/// </summary>
struct ObjectCoordinateSystem
{
public ObjectCoordinateSystem(Matrix3x3 m)
{
double nutation = Acos(m.Row2.Z);
if (Abs(nutation) < 1e-8)
{
Normal = new Vector(0.0, 0.0, 1.0);
Rotation = Atan2(m.Row1.X, m.Row1.Y);
WorldToPlane = Matrix3x3.Identity;
}
else
{
var precession = Atan2(m.Row0.Z, -m.Row1.Z);
WorldToPlane = (Matrix3x3.ZRotate(precession) * Matrix3x3.XRotate(nutation)).Transpose();
Normal = WorldToPlane.Row2;
Rotation = Atan2(m.Row2.X, m.Row2.Y);
}
}
public Vector Normal { get; }
public Matrix3x3 WorldToPlane { get; }
public double Rotation { get; }
public static ObjectCoordinateSystem FromYawPitchRoll(double yaw, double pitch, double roll) =>
new ObjectCoordinateSystem(
Matrix3x3.ZRotate(yaw) * 
Matrix3x3.YRotate(pitch) * 
Matrix3x3.XRotate(roll));
}
/// <summary>
/// Provides methods for vector calculus
/// </summary>
struct Vector
{
public Vector(double x, double y, double z)
{ X = x; Y = y; Z = z; }
public double X { get; }
public double Y { get; }
public double Z { get; }
public double DotProduct(Vector v) =>
X * v.X + Y * v.Y + Z * v.Z;
public static Vector operator *(Matrix3x3 m, Vector v) =>
new Vector(m.Row0.DotProduct(v), m.Row1.DotProduct(v), m.Row2.DotProduct(v));
}
/// <summary>
/// Provides methods for matrix calculus
/// </summary>
struct Matrix3x3
{
public Matrix3x3(Vector row0, Vector row1, Vector row2)
{ Row0 = row0; Row1 = row1; Row2 = row2; }
public Vector Row0 { get; }
public Vector Row1 { get; }
public Vector Row2 { get; }
public static Matrix3x3 Identity =>
new Matrix3x3(
new Vector(1.0, 0.0, 0.0),
new Vector(0.0, 1.0, 0.0),
new Vector(0.0, 0.0, 1.0));
public Matrix3x3 Transpose() =>
new Matrix3x3(
new Vector(Row0.X, Row1.X, Row2.X),
new Vector(Row0.Y, Row1.Y, Row2.Y),
new Vector(Row0.Z, Row1.Z, Row2.Z));
public static Matrix3x3 XRotate(double a) =>
new Matrix3x3(
new Vector(1.0, 0.0, 0.0),
new Vector(0.0, Cos(a), -Sin(a)),
new Vector(0.0, Sin(a), Cos(a)));
public static Matrix3x3 YRotate(double a) =>
new Matrix3x3(
new Vector(Cos(a), 0.0, Sin(a)),
new Vector(0.0, 1.0, 0.0),
new Vector(-Sin(a), 0.0, Cos(a)));
public static Matrix3x3 ZRotate(double a) =>
new Matrix3x3(
new Vector(Cos(a), -Sin(a), 0.0),
new Vector(Sin(a), Cos(a), 0.0),
new Vector(0.0, 0.0, 1.0));
public static Matrix3x3 operator *(Matrix3x3 a, Matrix3x3 b)
{
var trp = b.Transpose();
return new Matrix3x3(trp * a.Row0, trp * a.Row1, trp * a.Row2);
}
}
}

最新更新