SVG - linearGradient. Transformation ratio matrix (4x4) - svg

I would like to write a function that described the coefficients of the transformation matrix of linearGradient using a transformation matrix of dimension 4x4. Please help to fill in this matrix with the coefficients necessary to apply this matrix to some object.
For example, we have a description of the gradient:
<linearGradient
id="linearGradient2286"
x1="75.927002"
x2="136.64999"
y1="32.581001"
y2="127.08"
gradientTransform="matrix(0.20149,0,0,0.20149,-413.35,-155.75)"
gradientUnits="userSpaceOnUse">
<stop
stop-color="#6262f1"
offset="0"
id="stop2" />
<stop
stop-color="#1d1d34"
offset="1"
id="stop4" />
</linearGradient>
I want to fill in the transformation matrix of the view:
Matrix4 m1 = (
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0);

Depending on the layout of your matrix, I would expect it to be either:
0.20149, 0, 0, 0,
0, 0.20149, 0, 0,
0, 0, 1, 0,
-413.35, -155.75, 0, 1
or the transposed version
0.20149, 0, 0, -413.35,
0, 0.20149, 0, -155.75
0, 0, 1, 0,
0, 0, 0, 1

Related

Principal Component Analysis for 3D coordinates for alignment of principal axis in coordinate system

I am struggling with PCA.
Problem statement: I have two geometries. Both are same, but one of them is rotated around Y axis as shown below.
X1 = [0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 2, 1, 2, 0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 2, 1, 2]
Y1 = [0, 1, 1, 2, 2, 3, 3, 2, 1, 0, 0, 0, 1, 0, 1, 1, 2, 2, 3, 3, 2, 1, 0, 0, 0, 1]
Z1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
X2 = [0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 2.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 2.0, 1.0, 0.0, 0.0, 0.0, 1.0]
Y2 = [0.0, 0.94, 0.94, 1.88, 1.88, 2.82, 2.82, 1.88, 0.94, 0.0, 0.0, 0.0, 0.94, -0.34, 0.6, 0.6, 1.54, 1.54, 2.48, 2.48, 1.54, 0.6, -0.34, -0.34, -0.34, 0.6]
Z2 = [0.0, 0.34, 0.34, 0.68, 0.68, 1.03, 1.03, 0.68, 0.34, 0.0, 0.0, 0.0, 0.34, 0.94, 1.28, 1.28, 1.62, 1.62, 1.97, 1.97, 1.62, 1.28, 0.94, 0.94, 0.94, 1.28]
I need both two geometries are placed at same orientation after I apply PCA on both geometries.
What I have done:
First I Centered the data.
df_1 = df_1.drop('node_number', axis=1)
Then I applied PCA from sklearn
from sklearn.decomposition import PCA
pca = PCA()
pca.fit(data_centered)
pca_scores = pca.transform(data_centered)
scores_df = pd.DataFrame(pca_scores)
scores_df = scores_df.set_axis(['PC1', 'PC2', 'PC3'], axis=1)
scores_df = scores_df.round(2)
I applied this process on first geometry. After first geometry, I have applied this process similary on second geometry also.
But after applying PCA, I have got different new coordinates (X, Y, Z).
coordinate of First geometry after PCA,
X1_new = [-2.12, -1.41, -0.71, 0.0, 0.71, 1.41, 2.12, 1.41, 0.71, -0.0, -0.71, -1.41, -0.0, -2.12, -1.41, -0.71, 0.0, 0.71, 1.41, 2.12, 1.41, 0.71, -0.0, -0.71, -1.41, -0.0]
Y1_new = [-0.38, -1.09, -0.38, -1.09, -0.38, -1.09, -0.38, 0.33, 1.03, 1.74, 1.03, 0.33, 0.33, -0.38, -1.09, -0.38, -1.09, -0.38, -1.09, -0.38, 0.33, 1.03, 1.74, 1.03, 0.33, 0.33]
Z1_new = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5]
coordinate of second geometry after PCA,
X2__new =[-1.74, -0.33, -0.33, 1.09, 1.09, 2.5, 2.5, 1.09, -0.33, -1.74, -1.74, -1.74, -0.33, -1.74, -0.33, -0.33, 1.09, 1.09, 2.5, 2.5, 1.09, -0.33, -1.74, -1.74, -1.74, -0.33]
Y2_new = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5]
Z2_new = [0.0, -0.0, -0.0, -0.0, -0.0, 0.0, 0.0, -0.0, -0.0, 0.0, 0.0, 0.0, -0.0, 0.0, -0.0, -0.0, -0.0, -0.0, 0.0, 0.0, -0.0, -0.0, 0.0, 0.0, 0.0, -0.0]
What I am doing wrong? I need both geometries oriented in a same way. And I cant understand why there is all 0 in Z2_new of 2nd geometry after PCA.

Change colors in colormap based on range of values

Is it possible to set the lower and/or upper parts of a colorbar based on ranges of values? For example, given the ROYGBIV colormap below and optionally an offset and a range value, I'd like to change the colors below offset and/or above range. In other words, suppose offset = 20 and range = 72, I'd like to color all the values less than or equal to 20 in black and all values greater than or equal to 72 in white. I'm aware of the methods set_under and set_over, but they require changing the parameters vmin and vmax (as far as I know), which is not what I want. I want to keep the original minimum and maximum values (e.g., vmin = 0 and vmax = 100), and only (optionally) change the colors of the extremities.
ROYGBIV = {
"blue": ((0.0, 1.0, 1.0),
(0.167, 1.0, 1.0),
(0.333, 1.0, 1.0),
(0.5, 0.0, 0.0),
(0.667, 0.0, 0.0),
(0.833, 0.0, 0.0),
(1.0, 0.0, 0.0)),
"green": ((0.0, 0.0, 0.0),
(0.167, 0.0, 0.0),
(0.333, 0.0, 0.0),
(0.5, 1.0, 1.0),
(0.667, 1.0, 1.0),
(0.833, 0.498, 0.498),
(1.0, 0.0, 0.0)),
"red": ((0.0, 0.5608, 0.5608),
(0.167, 0.4353, 0.4353),
(0.333, 0.0, 0.0),
(0.5, 0.0, 0.0),
(0.667, 1.0, 1.0),
(0.833, 1.0, 1.0),
(1.0, 1.0, 1.0))
}
rainbow_mod = matplotlib.colors.LinearSegmentedColormap("rainbow_mod", ROYGBIV, 256)
I found one way to do it using ListedColormap as explained here. The basic idea is to obtain the RGBA lists/tuples of the colors in the LinearSegmentedColormap object (numpy array) and replace the first or last few lists with replicates of the desired color.
It looks something like this:
under_color = [0.0, 0.0, 0.0, 1.0] # black (alpha = 1.0)
over_color = [1.0, 1.0, 1.0, 1.0] # white (alpha = 1.0)
all_colors = rainbow_mod(np.linspace(0, 1, 256))
vmin = 0.0
vmax = 100.0
all_colors[:int(np.round((20.0 - vmin) / (vmax - vmin) * 256)), :] = under_color
all_colors[int(np.round((72.0 - vmin) / (vmax - vmin) * 256)):, :] = over_color
rainbow_mod_list = matplotlib.colors.ListedColormap(all_colors.tolist())

Gaussian elimination with partial pivoting (column)

I cannot find out the mistake I made, could anyone help me? Thanks very much!
import math
def GASSEM():
a0 = [12,-2,1,0,0,0,0,0,0,0,13.97]
a1 = [-2,12,-2,1,0,0,0,0,0,0,5.93]
a2 = [1,-2,12,-2,1,0,0,0,0,0,-6.02]
a3 = [0,1,-2,12,-2,1,0,0,0,0,8.32]
a4 = [0,0,1,-2,12,-2,1,0,0,0,-23.75]
a5 = [0,0,0,1,-2,12,-2,1,0,0,28.45]
a6 = [0,0,0,0,1,-2,12,-2,1,0,-8.9]
a7 = [0,0,0,0,0,1,-2,12,-2,1,-10.5]
a8 = [0,0,0,0,0,0,1,-2,12,-2,10.34]
a9 = [0,0,0,0,0,0,0,1,-2,12,-38.74]
A = [a0,a1,a2,a3,a4,a5,a6,a7,a8,a9] # 10x11 matrix
interchange=[0,0,0,0,0,0,0,0,0,0,0]
for i in range (1,10):
median = abs(A[i-1][i-1])
for m in range (i,10): #pivoting
if abs(A[m][i-1]) > median:
median = abs(A[m][i-1])
interchange = A[i-1]
A[i-1] = A[m]
A[m] = interchange
for j in range(i,10): #creating upper triangle matrix
A[j] = [A[j][k]-(A[j][i-1]/A[i-1][i-1])*A[i-1][k] for k in range(0,11)]
for t in range (0,10): #print the upper triangle matrix
print(A[t])
The output is not an upper triangle matrix, I'm getting lost in the for loops...
When I run this code, the output is
[12, -2, 1, 0, 0, 0, 0, 0, 0, 0, 13.97]
[0.0, 11.666666666666666, -1.8333333333333333, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 8.258333333333333]
[0.0, 0.0, 11.628571428571428, -1.842857142857143, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, -5.886428571428571]
[0.0, 0.0, -2.220446049250313e-16, 11.622235872235873, -1.8415233415233416, 1.0, 0.0, 0.0, 0.0, 0.0, 6.679281326781327]
[0.0, 0.0, -3.518258683818212e-17, 0.0, 11.622218698800275, -1.8415517150256329, 1.0, 0.0, 0.0, 0.0, -22.185475397706252]
[0.0, 0.0, 1.3530439218911067e-17, 0.0, 0.0, 11.62216239813737, -1.841549039580908, 1.0, 0.0, 0.0, 24.359991632712457]
[0.0, 0.0, 5.171101701700419e-18, 0.0, 0.0, 0.0, 11.622161705324444, -1.84154850220678, 1.0, 0.0, -3.131238144426707]
[0.0, 0.0, -3.448243038110395e-19, 0.0, 0.0, 0.0, 0.0, 11.62216144141611, -1.8415485389982904, 1.0, -13.0921440313208]
[0.0, 0.0, -4.995725026226573e-19, 0.0, 0.0, 0.0, 0.0, 0.0, 11.622161418001749, -1.8415485322346454, 8.534950160892514]
[0.0, 0.0, -4.9488445836100553e-20, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 11.622161417603511, -36.26114362292296]
This effectively is upper triangular. The absolute value of the 'non-zero' entries in the third column of the lower triangle are all less than 10e-15. Given that other values are 1 or greater, these small numbers look like floating point subtraction errors in A[j][k] - (A[j][i-1]/A[i-1][i-1])*A[i-1][k] that can be considered to be 0. Without more investigation, I don't know why the non-zero values are limited to this column.
For this data, the condition abs(A[m][i-1]) > median is never true, so the if block code is not tested.

Turtle graphics plotting

when I plot a list of data(freqList):
[0.09090909090909091, 0.0, 0.0, 0.09090909090909091, 0.18181818181818182, 0.0, 0.0, 0.0, 0.045454545454545456, 0.0, 0.0, 0.0, 0.0, 0.045454545454545456, 0.045454545454545456, 0.045454545454545456, 0.045454545454545456, 0.18181818181818182, 0.045454545454545456, 0.09090909090909091, 0.045454545454545456, 0.0, 0.045454545454545456, 0.0, 0.0, 0.0]
When I try to fill in the bar with color, only half of the bar gets filled, resembling a right triangle. Also I can't figure out how to label the x and y- axis to have the letters a-z on the x axis representing each decimal. And the frequency from 0.0 - max(freqList) on the y - axis that increments up based on each decimal number. I know I have to use t.write(), but I don't know where to place it
def letterFreqPlot(freqList):
border = 5
t = turtle.Turtle()
t.pensize(3)
screen = t.getscreen()
maxheight = max(freqList)
numbers = len(freqList)
screen.setworldcoordinates(0-border,-.05,numbers+1,maxheight)
t.goto(0,0)
t.speed(0)
t.lt(90)
t.fd(maxheight)
t.fd(-maxheight)
t.right(90)
for item in freqList:
t.fillcolor("blue")
for dist in [1, item, 1, item]:
t.begin_fill()
t.fd(dist)
t.lt(90)
t.fd(1)
t.end_fill()
Looks like you're making four begin_fill calls for every end_fill call. Try moving the begin call outside of the loop.
for item in freqList:
t.fillcolor("blue")
t.begin_fill()
for dist in [1, item, 1, item]:
t.fd(dist)
t.lt(90)
t.fd(1)
t.end_fill()

vtkLookupTable specifing colors with opacities

I am trying to use a vtkLookupTable in order to set the colors for my mesh. I started with vtkColorTransferFunction which worked fined except that it does not enable me to set opacities for various colors.
vtkLookupTable offers this opportunity, but I am having problems to get it working.
My vtkPolyData have scalars value set that determine the color they shall get. Overall 7 values, set with the scalars 0 to 6.
My vtkLookupTable looks like this:
vtkLookupTable lut = new vtkLookupTable();
lut.SetNumberOfColors(7);
double opacity = 0.3;
lut.SetTableValue(0, 0, 0, 1, opacity);
lut.SetTableValue(1, 0, 1.0, 0, opacity);
lut.SetTableValue(2, 0.6, 1.0, 0.0, opacity);
lut.SetTableValue(3, 1.0, 1.0, 0.0, 0.7);
lut.SetTableValue(4, 1.0, 0.8, 0.0, opacity);
lut.SetTableValue(5, 1.0, 0.4, 0.0, opacity);
lut.SetTableValue(6, 1.0, 0.0, 0.0, 1);
If I use a vtkColorTransferFunction with the same values (just no opacity) it works. Anyone any idea why this is not working? I should be same?
Thanks for help.
From your pseudocode, two lines may be missing.
lut.SetTableRange(0.0,6.0)
lut.Build()
Do you call these functions?
If it is not this problem then it may be because of the polydatamapper you are using. Can you submit a whole pipeline for your problem?
As a guide the following code will need to be called to allow your scalar values to be
added to the mapper and visualised correctly.
mapper.SetLookupTable(lut)
mapper.SetScalarVisibility(1)
mapper.SetScalarRange(0.0,6.0)

Resources