本文整理匯總了Golang中github.com/ungerik/go3d/float64/vec3.T.Length方法的典型用法代碼示例。如果您正苦於以下問題:Golang T.Length方法的具體用法?Golang T.Length怎麽用?Golang T.Length使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/ungerik/go3d/float64/vec3.T
的用法示例。
在下文中一共展示了T.Length方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ClosestParam
func (this *NurbsCurve) ClosestParam(p vec3.T) float64 {
// We want to solve:
//
// C'(u) * ( C(u) - P ) = 0 = f(u)
//
// C(u) is the curve, p is the point, * is a dot product
//
// We'll use newton's method:
//
// u* = u - f / f'
//
// We use the product rule in order to form the derivative, f':
//
// f' = C"(u) * ( C(u) - p ) + C'(u) * C'(u)
//
// What is the conversion criteria? (Piegl & Tiller suggest)
//
// |C(u) - p| < e1
//
// |C'(u)*(C(u) - P)|
// ------------------ < e2
// |C'(u)| |C(u) - P|
//
// 1) first check 2 & 3
// 2) if at least one of these is not, compute new value, otherwise halt
// 3) ensure the parameter stays within range
// * if not closed, don't allow outside of range a-b
// * if closed (e.g. circle), allow to move back to beginning
// 4) if |(u* - u)C'(u)| < e1, halt
//
min := math.MaxFloat64
var u float64
pts := this.regularSample(len(this.controlPoints) * this.degree)
for i := 0; i < len(pts)-1; i++ {
u0, u1 := pts[i].U, pts[i+1].U
p0 := pts[i].Pt
p1 := pts[i+1].Pt
proj := segmentClosestPoint(&p, &p0, &p1, u0, u1)
dv := vec3.Sub(&p, &proj.Pt)
d := dv.Length()
if d < min {
min = d
u = proj.U
}
}
maxits := 5
var i int
var e []vec3.T
eps1, eps2 := 0.0001, 0.0005
var dif vec3.T
minu, maxu := this.knots[0], this.knots[len(this.knots)-1]
firstCtrlPt := this.controlPoints[0].Dehomogenized()
lastCtrlPt := this.controlPoints[len(this.controlPoints)-1].Dehomogenized()
closed := vec3.SquareDistance(&firstCtrlPt, &lastCtrlPt) < Epsilon
cu := u
f := func(u float64) []vec3.T {
return this.Derivatives(u, 2)
}
n := func(u float64, e []vec3.T, d vec3.T) float64 {
// C'(u) * ( C(u) - P ) = 0 = f(u)
f := vec3.Dot(&e[1], &d)
// f' = C"(u) * ( C(u) - p ) + C'(u) * C'(u)
s0 := vec3.Dot(&e[2], &d)
s1 := vec3.Dot(&e[1], &e[1])
df := s0 + s1
return u - f/df
}
for i < maxits {
e = f(cu)
dif = vec3.Sub(&e[0], &p)
// |C(u) - p| < e1
c1v := dif.Length()
// C'(u) * (C(u) - P)
// ------------------ < e2
// |C'(u)| |C(u) - P|
c2n := vec3.Dot(&e[1], &dif)
c2d := e[1].Length() * c1v
c2v := c2n / c2d
c1 := c1v < eps1
c2 := math.Abs(c2v) < eps2
// if both tolerances are met
//.........這裏部分代碼省略.........
示例2: EllipseArc
// Generate the control points, weights, and knots of an elliptical arc
//
// **params**
// + the center
// + the scaled x axis
// + the scaled y axis
// + start angle of the ellipse arc, between 0 and 2pi, where 0 points at the xaxis
// + end angle of the arc, between 0 and 2pi, greater than the start angle
//
// **returns**
// + a NurbsCurveData object representing a NURBS curve
func EllipseArc(center *vec3.T, xaxis, yaxis *vec3.T, startAngle, endAngle float64) *verb.NurbsCurve {
xradius, yradius := xaxis.Length(), yaxis.Length()
xaxisNorm, yaxisNorm := xaxis.Normalized(), yaxis.Normalized()
// if the end angle is less than the start angle, do a circle
if endAngle < startAngle {
endAngle = 2.0*math.Pi + startAngle
}
theta := endAngle - startAngle
// how many arcs?
var numArcs int
if theta <= math.Pi/2 {
numArcs = 1
} else {
if theta <= math.Pi {
numArcs = 2
} else if theta <= 3*math.Pi/2 {
numArcs = 3
} else {
numArcs = 4
}
}
dtheta := theta / float64(numArcs)
w1 := math.Cos(dtheta / 2)
xCompon := xaxisNorm.Scaled(xradius * math.Cos(startAngle))
yCompon := yaxisNorm.Scaled(yradius * math.Sin(startAngle))
P0 := vec3.Add(&xCompon, &yCompon)
temp0 := yaxisNorm.Scaled(math.Cos(startAngle))
temp1 := xaxisNorm.Scaled(math.Sin(startAngle))
T0 := vec3.Sub(&temp0, &temp1)
controlPoints := make([]vec3.T, 2*numArcs+1)
knots := make([]float64, 2*numArcs+3)
index := 0
angle := startAngle
weights := make([]float64, numArcs*2)
controlPoints[0] = P0
weights[0] = 1.0
for i := 1; i <= numArcs; i++ {
angle += dtheta
xCompon = xaxisNorm.Scaled(xradius * math.Cos(angle))
yCompon = yaxisNorm.Scaled(yradius * math.Sin(angle))
offset := vec3.Add(&xCompon, &yCompon)
P2 := vec3.Add(center, &offset)
weights[index+2] = 1
controlPoints[index+2] = P2
temp0 := yaxisNorm.Scaled(math.Cos(angle))
temp1 := xaxisNorm.Scaled(math.Sin(angle))
T2 := vec3.Sub(&temp0, &temp1)
T0Norm := T0.Normalized()
T2Norm := T2.Normalized()
inters := intersect.Rays(&P0, &T0Norm, &P2, &T2Norm)
T0Scaled := T0.Scaled(inters.U0)
P1 := vec3.Add(&P0, &T0Scaled)
weights[index+1] = w1
controlPoints[index+1] = P1
index += 2
if i < numArcs {
P0 = P2
T0 = T2
}
}
j := 2*numArcs + 1
for i := 0; i < 3; i++ {
knots[i] = 0.0
knots[i+j] = 1.0
}
switch numArcs {
case 2:
knots[3] = 0.5
knots[4] = 0.5
//.........這裏部分代碼省略.........