require matrix require bigdecimal require mathn print regular mesh Del

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require 'matrix'
require 'bigdecimal'
require 'mathn'
print("regular mesh\n")
#Delaunay = [[[0.0, 0.0, 10.0], [0.0, 800.0, 10.0], [160.0, 280.0, 200.0]], [[800.0, 800.0, 10.0], [800.0, 0.0, 10.0], [600.0, 280.0, 100.0]], [[800.0, 0.0, 10.0], [0.0, 0.0, 10.0], [600.0, 280.0, 100.0]], [[0.0, 800.0, 10.0], [800.0, 800.0, 10.0], [440.0, 600.0, 300.0]], [[440.0, 600.0, 300.0], [600.0, 280.0, 100.0], [160.0, 280.0, 200.0]], [[600.0, 280.0, 100.0], [0.0, 0.0, 10.0], [160.0, 280.0, 200.0]], [[800.0, 800.0, 10.0], [600.0, 280.0, 100.0], [440.0, 600.0, 300.0]], [[160.0, 280.0, 200.0], [0.0, 800.0, 10.0], [440.0, 600.0, 300.0]]]
#Delaunay = [[[518.0, 434.0, 719968.0], [557.0, 162.0, 924005.0], [353.0, 334.0, 653053.0]], [[800.0, 800.0, 0.0], [800.0, 0.0, 640000.0], [738.0, 716.0, 671988.0]], [[800.0, 0.0, 640000.0], [0.0, 0.0, 0.0], [580.0, 130.0, 959500.0]], [[0.0, 800.0, -640000.0], [800.0, 800.0, 0.0], [110.0, 691.0, 174619.0]], [[738.0, 716.0, 671988.0], [602.0, 180.0, 970004.0], [518.0, 434.0, 719968.0]], [[738.0, 716.0, 671988.0], [800.0, 0.0, 640000.0], [602.0, 180.0, 970004.0]], [[800.0, 0.0, 640000.0], [580.0, 130.0, 959500.0], [602.0, 180.0, 970004.0]], [[738.0, 716.0, 671988.0], [356.0, 479.0, 537295.0], [110.0, 691.0, 174619.0]], [[580.0, 130.0, 959500.0], [0.0, 0.0, 0.0], [557.0, 162.0, 924005.0]], [[0.0, 0.0, 0.0], [0.0, 800.0, -640000.0], [2.0, 439.0, 447283.0]], [[110.0, 691.0, 174619.0], [180.0, 408.0, 505936.0], [2.0, 439.0, 447283.0]], [[180.0, 408.0, 505936.0], [0.0, 0.0, 0.0], [2.0, 439.0, 447283.0]], [[602.0, 180.0, 970004.0], [580.0, 130.0, 959500.0], [557.0, 162.0, 924005.0]], [[0.0, 0.0, 0.0], [353.0, 334.0, 653053.0], [557.0, 162.0, 924005.0]], [[0.0, 0.0, 0.0], [180.0, 408.0, 505936.0], [353.0, 334.0, 653053.0]], [[800.0, 800.0, 0.0], [738.0, 716.0, 671988.0], [110.0, 691.0, 174619.0]], [[180.0, 408.0, 505936.0], [356.0, 479.0, 537295.0], [353.0, 334.0, 653053.0]], [[602.0, 180.0, 970004.0], [557.0, 162.0, 924005.0], [518.0, 434.0, 719968.0]], [[353.0, 334.0, 653053.0], [356.0, 479.0, 537295.0], [518.0, 434.0, 719968.0]], [[356.0, 479.0, 537295.0], [738.0, 716.0, 671988.0], [518.0, 434.0, 719968.0]], [[356.0, 479.0, 537295.0], [180.0, 408.0, 505936.0], [110.0, 691.0, 174619.0]], [[2.0, 439.0, 447283.0], [0.0, 800.0, -640000.0], [110.0, 691.0, 174619.0]]]
#Delaunay = [[[518.0, 434.0, 719968.0], [557.0, 162.0, 924005.0], [353.0, 334.0, 653053.0]], [[800.0, 800.0, 640000.0], [800.0, 0.0, 1280000.0], [738.0, 716.0, 671988.0]], [[800.0, 0.0, 1280000.0], [0.0, 0.0, 640000.0], [580.0, 130.0, 959500.0]], [[0.0, 800.0, 0.0], [800.0, 800.0, 640000.0], [110.0, 691.0, 174619.0]], [[738.0, 716.0, 671988.0], [602.0, 180.0, 970004.0], [518.0, 434.0, 719968.0]], [[738.0, 716.0, 671988.0], [800.0, 0.0, 1280000.0], [602.0, 180.0, 970004.0]], [[800.0, 0.0, 1280000.0], [580.0, 130.0, 959500.0], [602.0, 180.0, 970004.0]], [[738.0, 716.0, 671988.0], [356.0, 479.0, 537295.0], [110.0, 691.0, 174619.0]], [[580.0, 130.0, 959500.0], [0.0, 0.0, 640000.0], [557.0, 162.0, 924005.0]], [[0.0, 0.0, 640000.0], [0.0, 800.0, 0.0], [2.0, 439.0, 447283.0]], [[110.0, 691.0, 174619.0], [180.0, 408.0, 505936.0], [2.0, 439.0, 447283.0]], [[180.0, 408.0, 505936.0], [0.0, 0.0, 640000.0], [2.0, 439.0, 447283.0]], [[602.0, 180.0, 970004.0], [580.0, 130.0, 959500.0], [557.0, 162.0, 924005.0]], [[0.0, 0.0, 640000.0], [353.0, 334.0, 653053.0], [557.0, 162.0, 924005.0]], [[0.0, 0.0, 640000.0], [180.0, 408.0, 505936.0], [353.0, 334.0, 653053.0]], [[800.0, 800.0, 640000.0], [738.0, 716.0, 671988.0], [110.0, 691.0, 174619.0]], [[180.0, 408.0, 505936.0], [356.0, 479.0, 537295.0], [353.0, 334.0, 653053.0]], [[602.0, 180.0, 970004.0], [557.0, 162.0, 924005.0], [518.0, 434.0, 719968.0]], [[353.0, 334.0, 653053.0], [356.0, 479.0, 537295.0], [518.0, 434.0, 719968.0]], [[356.0, 479.0, 537295.0], [738.0, 716.0, 671988.0], [518.0, 434.0, 719968.0]], [[356.0, 479.0, 537295.0], [180.0, 408.0, 505936.0], [110.0, 691.0, 174619.0]], [[2.0, 439.0, 447283.0], [0.0, 800.0, 0.0], [110.0, 691.0, 174619.0]], ]
Delaunay = [[[0.0, 0.0, 640000.0], [0.0, 800.0, 640000.0], [67.0, 555.0, 726864.0]], [[800.0, 0.0, 640000.0], [552.0, 130.0, 590204.0], [590.0, 247.0, 652691.0]], [[246.0, 681.0, 584755.0], [140.0, 467.0, 703111.0], [67.0, 555.0, 726864.0]], [[800.0, 0.0, 640000.0], [0.0, 0.0, 640000.0], [552.0, 130.0, 590204.0]], [[0.0, 0.0, 640000.0], [349.0, 353.0, 640392.0], [552.0, 130.0, 590204.0]], [[0.0, 800.0, 640000.0], [523.0, 695.0, 568104.0], [246.0, 681.0, 584755.0]], [[246.0, 681.0, 584755.0], [346.0, 550.0, 620416.0], [161.0, 452.0, 694417.0]], [[800.0, 800.0, 640000.0], [800.0, 0.0, 640000.0], [590.0, 247.0, 652691.0]], [[161.0, 452.0, 694417.0], [140.0, 467.0, 703111.0], [246.0, 681.0, 584755.0]], [[349.0, 353.0, 640392.0], [0.0, 0.0, 640000.0], [108.0, 444.0, 723328.0]], [[590.0, 247.0, 652691.0], [552.0, 130.0, 590204.0], [349.0, 353.0, 640392.0]], [[108.0, 444.0, 723328.0], [140.0, 467.0, 703111.0], [161.0, 452.0, 694417.0]], [[0.0, 0.0, 640000.0], [67.0, 555.0, 726864.0], [108.0, 444.0, 723328.0]], [[67.0, 555.0, 726864.0], [140.0, 467.0, 703111.0], [108.0, 444.0, 723328.0]], [[349.0, 353.0, 640392.0], [346.0, 550.0, 620416.0], [590.0, 247.0, 652691.0]], [[67.0, 555.0, 726864.0], [0.0, 800.0, 640000.0], [246.0, 681.0, 584755.0]], [[346.0, 550.0, 620416.0], [349.0, 353.0, 640392.0], [161.0, 452.0, 694417.0]], [[349.0, 353.0, 640392.0], [108.0, 444.0, 723328.0], [161.0, 452.0, 694417.0]], [[346.0, 550.0, 620416.0], [246.0, 681.0, 584755.0], [523.0, 695.0, 568104.0]], [[0.0, 800.0, 640000.0], [800.0, 800.0, 640000.0], [523.0, 695.0, 568104.0]], [[346.0, 550.0, 620416.0], [523.0, 695.0, 568104.0], [590.0, 247.0, 652691.0]], [[523.0, 695.0, 568104.0], [800.0, 800.0, 640000.0], [590.0, 247.0, 652691.0]], ]
num_tri = Delaunay.length
=begin
def point_seg(s1, s2, p)
u = [s2[0] - s1[0], s2[1] - s1[1]] #s1s2
v = [p[0] - s1[0], p[1] - s1[0]] #s1p
#print "u = ", u, " v = ", v, "\n"
cross = u[0] * v[1] - v[0] * u[1]
if cross == 0
if (((s1[0] - p[0]) * (s2[0] - p[0])) <= 0) && (((s1[1] - p[1]) * (s2[1] - p[1])) <= 0)
#print "on edge\n"
return true
else
return false
end
else
return false
end
end
=end
def inside_tri(tri, p)
a = tri[0]
b = tri[1]
c = tri[2]
m = (a[0] - p[0]) * (b[1] - a[1]) - (b[0] - a[0]) * (a[1] - p[1])
n = (b[0] - p[0]) * (c[1] - b[1]) - (c[0] - b[0]) * (b[1] - p[1])
k = (c[0] - p[0]) * (a[1] - c[1]) - (a[0] - c[0]) * (c[1] - p[1])
#print "m: ", m, "n: ", n, "k: ", k, "\n"
if ((m > 0) && (n > 0) && (k > 0)) || ((m < 0) && (n < 0) && (k < 0)) || (m == 0) || (n == 0) || (k == 0)
#print "inside\n"
return true
end
return false
end
def in_tri(tri, p)
=begin
#print tri, "\n"
if point_seg(tri[0], tri[1], p)
#point on seg 0-1
return [true, 0]
elsif point_seg(tri[1], tri[2], p)
#point on seg 1-2
return [true, 1]
elsif point_seg(tri[0], tri[2], p)
#point on seg 0-2
return [true, 2]
elsif inside_tri(tri, p)
return [true, 3]
end
return [false, -1]
=end
if inside_tri(tri, p)
return [true, 3]
else
return [false, -1]
end
end
def count_lin(tri, p)
first = Matrix.row_vector([(tri[0][2]), (tri[1][2]), (tri[2][2])])
second = Matrix.rows([[tri[0][0], tri[1][0], tri[2][0]], [tri[0][1], tri[1][1], tri[2][1]], [1, 1, 1]])
secondi = second.inverse
third = Matrix.column_vector([p[0], p[1], 1])
#print first * secondi * third
return (first * secondi * third).element(0, 0)
end
#create grid
LOW = 0
HIGH = 800
kol = 40
step = (HIGH - LOW)/kol
print Delaunay, "\n"
print "step: ", step, "\n"
grid = Hash.new
npx = []
npy = []
npz = []
#i = 0
#j = 0
for i in (0 .. kol)
for j in (0 .. kol)
#print "i: ", i, "j: ", j, " istep ", i * step, " jstep ", j * step, "\n"
#print "will write: ", [i * step, j * step, 0], "\n"
grid[[i, j]] = [i * step, j * step]
#print "current grid: ", grid, "\n"
#j += 1
end
#i += 1
end
print "end grid: \n", grid, "\n"
for i in (0 .. kol)
for j in (0 .. kol)
#проходим в цикле по треугольникам, проверяем лежит ли точка внутри, если нет,
#проверяем лежит ли на каком то ребре,
#когда нашли где находится точка, то выполняем расчет
cur_p = grid[[i, j]]
for k in (0 .. (Delaunay.length - 1))
#print "ex cur_p: ", cur_p, "\n"
#print "ex in k ", k, "\n"
cur_tri = Delaunay[k]
#print "cur_p: ", cur_p, "\n"
res = in_tri(cur_tri, cur_p)
if res[0]
#посчитать высоту
#print "in k ", k, " triangle: ", cur_tri, "\n"
grid[[i, j]] = [grid[[i, j]][0], grid[[i, j]][1], count_lin(cur_tri, cur_p)]
npx += [grid[[i, j]][0]]
npy += [grid[[i, j]][1]]
npz += [grid[[i, j]][2]]
break
end
end
end
end
#print "grid with z: ", grid, "\n"
#grid.each { |key,value| print "vertex(", value[0], ", ", value[1], ", ", value[2], ")\n" }
#for i in (0 .. 66)
# print "vertex(", npx[i], ", ", npy[i], ", ", npz[i], ");\n"
#end
#print npx.length, "\n", npy.length, "\n", npz.length, "\n"
print npx, "\n"
print npy, "\n"
print npz, "\n"
=begin
print "test plot\n"
npxx = []
npyy = []
npzz = []
for i in Delaunay
npxx += i[0]
npyy += i[1]
npzz += i[2]
end
print npxx.length, npyy.length, npzz.length, "\n"
print npxx, "\n"
print npyy, "\n"
print npzz, "\n"
=end