2

I have a set of points in 3D space and I have defined a vector form one point to another. I want to find angle of each of this vector with reference to the vector defined as a reference.

The vector defined as a reference is between this two points,

head = [0.5806  0.50239 0.54057]
tail = [0.5806  0.50239 0.     ]

Reference vector is defined as,

v_reference = head - tail 

The set of points from which I have defined a vector from one point to another is as follows,

            x           y           z
0   0.722950    0.611143    0.154976
1   0.722887    0.611518    0.152955
2   0.722880    0.612001    0.150593
3   0.722910    0.612509    0.148238
4   0.723049    0.613053    0.146069
5   0.723113    0.613583    0.143714
6   0.722763    0.613838    0.141321
7   0.721956    0.613876    0.138467
8   0.721638    0.614167    0.136008
9   0.720665    0.614093    0.133143
10  0.719612    0.613956    0.130317
11  0.718672    0.613882    0.127562
12  0.717771    0.613870    0.124638
13  0.716533    0.613668    0.121512

I have defined vector from one point to another, the points used for defining those vectors is shown in the table above.

For defining the vectors I have used following approach,

vector[i] = data[i+1] - data[i]

I want to find angle of each of this vector with respect to the reference vector (v_reference).

I have used following approach for angle computation,

def dotproduct(v1, v2):
    """This function computes dot product of two vectors."""
    return sum((a*b) for a, b in zip(v1, v2))

def mag(v):
    """This function computes magnitude of two vectors."""
    return math.sqrt(dotproduct(v, v))

def angle(v1, v2):
    """This function computes angle between two vectors."""
    return (np.arccos(dotproduct(v1, v2) / (mag(v1) * mag(v2))))*(180/math.pi)

The angle should be continuously increasing but it fluctuating.

Upriser
  • 379
  • 3
  • 20

1 Answers1

2

I think your problem might be how you're defining your vectors. If I do everything exactly like you describe in your question, then I also get a sequence of fluctuating angles:

import pandas as pd
import numpy as np

def ang(u, v):
    # see https://stackoverflow.com/a/2827466/425458
    c = np.dot(u/np.linalg.norm(u), v/np.linalg.norm(v))
    return np.rad2deg(np.arccos(np.clip(c, -1, 1)))

d = '''            x           y           z
0   0.722950    0.611143    0.154976
1   0.722887    0.611518    0.152955
2   0.722880    0.612001    0.150593
3   0.722910    0.612509    0.148238
4   0.723049    0.613053    0.146069
5   0.723113    0.613583    0.143714
6   0.722763    0.613838    0.141321
7   0.721956    0.613876    0.138467
8   0.721638    0.614167    0.136008
9   0.720665    0.614093    0.133143
10  0.719612    0.613956    0.130317
11  0.718672    0.613882    0.127562
12  0.717771    0.613870    0.124638
13  0.716533    0.613668    0.121512'''

df = pd.read_csv(pd.compat.StringIO(d), sep='\s+')
xyz = df.values
u = np.diff(xyz, axis=0)

head = np.array([0.5806,  0.50239, 0.54057])
tail = np.array([0.5806,  0.50239, 0.     ])
v = head - tail

ang(u, v)
# output:
#     array([101.96059029, 104.01677172, 103.97438663, 102.85092705,
#            103.97438663, 104.20457158, 107.01708978, 104.604926  ,
#            107.08468905, 106.84512875, 106.40978005, 107.44768844,
#            108.69610224])

However, if you treat your list of xyz points as vectors (ie the vectors starting at the origin and going to each of the points), then you do see a constantly increasing angle between the reference vector and the sequence of vectors, like you expected:

ang(xyz, v)
# output:
#     array([87.51931013, 87.55167997, 87.58951053, 87.62722792, 87.66196546,
#            87.69968089, 87.73800388, 87.78370828, 87.82308596, 87.8689639 ,
#            87.91421599, 87.95832992, 88.0051486 , 88.05520021])

Might this be the actually correct way to interpret/analyze your data?

tel
  • 11,237
  • 2
  • 32
  • 53