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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
module Rubyvis::SvgScene
class PathBasis #:nodoc:
def initialize(p0,p1,p2,p3)
@p0=p0
@p1=p1
@p2=p2
@p3=p3
end
attr_accessor :p0,:p1,:p2,:p3
#
# Matrix to transform basis (b-spline) control points to bezier control
# points. Derived from FvD 11.2.8.
#
def basis
[
[ 1/6.0, 2/3.0, 1/6.0, 0 ],
[ 0, 2/3.0, 1/3.0, 0 ],
[ 0, 1/3.0, 2/3.0, 0 ],
[ 0, 1/6.0, 2/3.0, 1/6.0 ]
]
end
# Returns the point that is the weighted sum of the specified control points,
# using the specified weights. This method requires that there are four
# weights and four control points.
def weight(w)
OpenStruct.new({
:x=> w[0] * p0.left + w[1] * p1.left + w[2] * p2.left + w[3] * p3.left,
:y=> w[0] * p0.top + w[1] * p1.top + w[2] * p2.top + w[3] * p3.top
})
end
def convert
b1 = weight(basis[1])
b2 = weight(basis[2])
b3 = weight(basis[3])
"C#{b1.x},#{b1.y},#{b2.x},#{b2.y },#{b3.x},#{b3.y}"
end
def to_s
convert
end
def segment
b0 = weight(basis[0])
b1 = weight(basis[1])
b2 = weight(basis[2])
b3 = weight(basis[3])
"M#{b0.x},#{b0.y}C#{b1.x},#{b1.y},#{b2.x},#{b2.y},#{b3.x},#{b3.y}"
end
end
# Converts the specified b-spline curve segment to a bezier curve
# compatible with SVG "C".
# * @param p0 the first control point.
# * @param p1 the second control point.
# * @param p2 the third control point.
# * @param p3 the fourth control point.
def self.path_basis(p0,p1,p2,p3)
PathBasis.new(p0,p1,p2,p3)
end
# Interpolates the given points using the basis spline interpolation.
# Returns an SVG path without the leading M instruction to allow path
# appending.
def self.curve_basis(points)
return "" if (points.size <= 2)
path = ""
p0 = points[0]
p1 = p0
p2 = p0
p3 = points[1]
path += self.path_basis(p0, p1, p2, p3).to_s
2.upto(points.size-1) {|i|
p0 = p1
p1 = p2
p2 = p3
p3 = points[i]
path += self.path_basis(p0, p1, p2, p3).to_s
}
# Cycle through to get the last point.
path += self.path_basis(p1, p2, p3, p3).to_s
path += self.path_basis(p2, p3, p3, p3).to_s
path;
end
# Interpolates the given points using the basis spline interpolation.
# If points.length == tangents.length then a regular Hermite interpolation is
# performed, if points.length == tangents.length + 2 then the first and last
# segments are filled in with cubic bazier segments. Returns an array of path
# strings.
def self.curve_basis_segments(points)
return "" if (points.size <= 2)
paths = []
p0 = points[0]
p1 = p0
p2 = p0
p3 = points[1]
firstPath = self.path_basis(p0, p1, p2, p3).segment
p0 = p1;
p1 = p2;
p2 = p3;
p3 = points[2];
paths.push(firstPath + self.path_basis(p0, p1, p2, p3).to_s) # merge first & second path
3.upto(points.size-1) {|i|
p0 = p1;
p1 = p2;
p2 = p3;
p3 = points[i];
paths.push(path_basis(p0, p1, p2, p3).segment);
}
# merge last & second-to-last path
paths.push(path_basis(p1, p2, p3, p3).segment + path_basis(p2, p3, p3, p3).to_s)
paths
end
# Interpolates the given points with respective tangents using the cubic
# Hermite spline interpolation. If points.length == tangents.length then a regular
# Hermite interpolation is performed, if points.length == tangents.length + 2 then
# the first and last segments are filled in with cubic bazier segments.
# Returns an SVG path without the leading M instruction to allow path appending.
#
# * @param points the array of points.
# * @param tangents the array of tangent vectors.
#/
def self.curve_hermite(points, tangents)
return "" if (tangents.size < 1 or (points.size != tangents.size and points.size != tangents.size + 2))
quad = points.size != tangents.size
path = ""
p0 = points[0]
p = points[1]
t0 = tangents[0]
t = t0
pi = 1
if (quad)
path += "Q#{(p.left - t0.x * 2 / 3)},#{(p.top - t0.y * 2 / 3)},#{p.left},#{p.top}"
p0 = points[1];
pi = 2;
end
if (tangents.length > 1)
t = tangents[1]
p = points[pi]
pi+=1
path += "C#{(p0.left + t0.x)},#{(p0.top + t0.y) },#{(p.left - t.x) },#{(p.top - t.y)},#{p.left},#{p.top}"
2.upto(tangents.size-1) {|i|
p = points[pi];
t = tangents[i];
path += "S#{(p.left - t.x)},#{(p.top - t.y)},#{p.left},#{p.top}"
pi+=1
}
end
if (quad)
lp = points[pi];
path += "Q#{(p.left + t.x * 2 / 3)},#{(p.top + t.y * 2 / 3)},#{lp.left},#{lp.top}"
end
path;
end
# Interpolates the given points with respective tangents using the
# cubic Hermite spline interpolation. Returns an array of path strings.
#
# * @param points the array of points.
# * @param tangents the array of tangent vectors.
def self.curve_hermite_segments(points, tangents)
return [] if (tangents.size < 1 or (points.size != tangents.size and points.size != tangents.size + 2))
quad = points.size != tangents.size
paths = []
p0 = points[0]
p = p0
t0 = tangents[0]
t = t0
pi = 1
if (quad)
p = points[1]
paths.push("M#{p0.left},#{p0.top }Q#{(p.left - t.x * 2 / 3.0 )},#{(p.top - t.y * 2 / 3)},#{p.left},#{p.top}")
pi = 2
end
1.upto(tangents.size-1) {|i|
p0 = p;
t0 = t;
p = points[pi]
t = tangents[i]
paths.push("M#{p0.left },#{p0.top
}C#{(p0.left + t0.x) },#{(p0.top + t0.y)
},#{(p.left - t.x) },#{(p.top - t.y)
},#{p.left },#{p.top}")
pi+=1
}
if (quad)
lp = points[pi];
paths.push("M#{p.left },#{p.top
}Q#{(p.left + t.x * 2 / 3) },#{(p.top + t.y * 2 / 3) },#{lp.left },#{lp.top}")
end
paths
end
# Computes the tangents for the given points needed for cardinal
# spline interpolation. Returns an array of tangent vectors. Note: that for n
# points only the n-2 well defined tangents are returned.
#
# * @param points the array of points.
# * @param tension the tension of hte cardinal spline.
def self.cardinal_tangents(points, tension)
tangents = []
a = (1 - tension) / 2.0
p0 = points[0]
p1 = points[1]
p2 = points[2]
3.upto(points.size-1) {|i|
tangents.push(OpenStruct.new({:x=> a * (p2.left - p0.left), :y=> a * (p2.top - p0.top)}))
p0 = p1;
p1 = p2;
p2 = points[i];
}
tangents.push(OpenStruct.new({:x=> a * (p2.left - p0.left), :y=> a * (p2.top - p0.top)}))
return tangents;
end
# Interpolates the given points using cardinal spline interpolation.
# Returns an SVG path without the leading M instruction to allow path
# appending.
#
# * @param points the array of points.
# * @param tension the tension of hte cardinal spline.
def self.curve_cardinal(points, tension)
return "" if (points.size <= 2)
self.curve_hermite(points, self.cardinal_tangents(points, tension))
end
# Interpolates the given points using cardinal spline interpolation.
# Returns an array of path strings.
#
# @param points the array of points.
# @param tension the tension of hte cardinal spline.
def self.curve_cardinal_segments(points, tension)
return "" if (points.size <= 2)
self.curve_hermite_segments(points, self.cardinal_tangents(points, tension))
end
# Interpolates the given points using Fritsch-Carlson Monotone cubic
# Hermite interpolation. Returns an array of tangent vectors.
#
# *@param points the array of points.
def self.monotone_tangents(points)
tangents = []
d = []
m = []
dx = []
#k=0
#/* Compute the slopes of the secant lines between successive points. */
0.upto(points.size-2) do |k|
# while(k < points.size-1) do
d[k] = (points[k+1].top - points[k].top) / (points[k+1].left - points[k].left).to_f
k+=1
end
#/* Initialize the tangents at every point as the average of the secants. */
m[0] = d[0]
dx[0] = points[1].left - points[0].left
1.upto(points.size-2) {|k|
m[k] = (d[k-1]+d[k]) / 2.0
dx[k] = (points[k+1].left - points[k-1].left) / 2.0
}
k=points.size-1
m[k] = d[k-1];
dx[k] = (points[k].left - points[k-1].left);
# /* Step 3. Very important, step 3. Yep. Wouldn't miss it. */
(points.size-1).times {|kk|
if d[kk] == 0
m[ kk ] = 0;
m[kk + 1] = 0;
end
}
# /* Step 4 + 5. Out of 5 or more steps. */
(points.size-1).times {|kk|
next if ((m[kk].abs < 1e-5) or (m[kk+1].abs < 1e-5))
akk = m[kk] / d[kk].to_f
bkk = m[kk + 1] / d[kk].to_f
s = akk * akk + bkk * bkk; # monotone constant (?)
if (s > 9)
tkk = 3.0 / Math.sqrt(s)
m[kk] = tkk * akk * d[kk]
m[kk + 1] = tkk * bkk * d[kk]
end
}
len=nil;
points.size.times {|i|
len = 1 + m[i] * m[i]; #// pv.vector(1, m[i]).norm().times(dx[i]/3)
tangents.push(OpenStruct.new({:x=> dx[i] / 3.0 / len, :y=> m[i] * dx[i] / 3.0 / len}))
}
tangents;
end
# Interpolates the given points using Fritsch-Carlson Monotone cubic
# Hermite interpolation. Returns an SVG path without the leading M instruction
# to allow path appending.
#
# * @param points the array of points.
def self.curve_monotone(points)
return "" if (points.length <= 2)
return self.curve_hermite(points, self.monotone_tangents(points))
end
# Interpolates the given points using Fritsch-Carlson Monotone cubic
# Hermite interpolation.
# Returns an array of path strings.
#
# * @param points the array of points.
#/
def self.curve_monotone_segments(points)
return "" if (points.size <= 2)
self.curve_hermite_segments(points, self.monotone_tangents(points))
end
end
|