⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linesegment.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
									* (s0 + (2.0f) * diffThisDot)
									+ lengthOfDiff;
						} else if (tempS1 <= test.getExtent()) {
							s1 = tempS1;
							squareDistance = -s1 * s1 + s0
									* (s0 + (2.0f) * diffThisDot)
									+ lengthOfDiff;
						} else {
							s1 = test.getExtent();
							squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0
									* (s0 + (2.0f) * diffThisDot)
									+ lengthOfDiff;
						}
					} else // region 4 (corner)
					{
						s1 = test.getExtent();
						tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
						if (tempS0 > extent) {
							s0 = extent;
							squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1
									* (s1 + (2.0f) * diffTestDot)
									+ lengthOfDiff;
						} else if (tempS0 >= -extent) {
							s0 = tempS0;
							squareDistance = -s0 * s0 + s1
									* (s1 + (2.0f) * diffTestDot)
									+ lengthOfDiff;
						} else {
							s0 = -extent;
							tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
							if (tempS1 < -test.getExtent()) {
								s1 = -test.getExtent();
								squareDistance = s1 * (s1 - (2.0f) * tempS1)
										+ s0 * (s0 + (2.0f) * diffThisDot)
										+ lengthOfDiff;
							} else if (tempS1 <= test.getExtent()) {
								s1 = tempS1;
								squareDistance = -s1 * s1 + s0
										* (s0 + (2.0f) * diffThisDot)
										+ lengthOfDiff;
							} else {
								s1 = test.getExtent();
								squareDistance = s1 * (s1 - (2.0f) * tempS1)
										+ s0 * (s0 + (2.0f) * diffThisDot)
										+ lengthOfDiff;
							}
						}
					}
				} else // region 6 (corner)
				{
					s1 = -test.getExtent();
					tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
					if (tempS0 > extent) {
						s0 = extent;
						squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1
								* (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
					} else if (tempS0 >= -extent) {
						s0 = tempS0;
						squareDistance = -s0 * s0 + s1
								* (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
					} else {
						s0 = -extent;
						tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
						if (tempS1 < -test.getExtent()) {
							s1 = -test.getExtent();
							squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0
									* (s0 + (2.0f) * diffThisDot)
									+ lengthOfDiff;
						} else if (tempS1 <= test.getExtent()) {
							s1 = tempS1;
							squareDistance = -s1 * s1 + s0
									* (s0 + (2.0f) * diffThisDot)
									+ lengthOfDiff;
						} else {
							s1 = test.getExtent();
							squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0
									* (s0 + (2.0f) * diffThisDot)
									+ lengthOfDiff;
						}
					}
				}
			}
		} else {
			// The segments are parallel. The average b0 term is designed to
			// ensure symmetry of the function. That is, dist(seg0,seg1) and
			// dist(seg1,seg0) should produce the same number.get
			float extentSum = extent + test.getExtent();
			float sign = (negativeDirectionDot > 0.0f ? -1.0f : 1.0f);
			float averageB0 = (0.5f) * (diffThisDot - sign * diffTestDot);
			float lambda = -averageB0;
			if (lambda < -extentSum) {
				lambda = -extentSum;
			} else if (lambda > extentSum) {
				lambda = extentSum;
			}

			squareDistance = lambda * (lambda + (2.0f) * averageB0)
					+ lengthOfDiff;
		}

		return FastMath.abs(squareDistance);
	}

	public float distanceSquared(Ray r) {
		Vector3f kDiff = r.getOrigin().subtract(origin);
		float fA01 = -r.getDirection().dot(direction);
		float fB0 = kDiff.dot(r.getDirection());
		float fB1 = -kDiff.dot(direction);
		float fC = kDiff.lengthSquared();
		float fDet = FastMath.abs(1.0f - fA01 * fA01);
		float fS0, fS1, fSqrDist, fExtDet;

		if (fDet >= FastMath.FLT_EPSILON) {
			// The ray and segment are not parallel.
			fS0 = fA01 * fB1 - fB0;
			fS1 = fA01 * fB0 - fB1;
			fExtDet = extent * fDet;

			if (fS0 >= (float) 0.0) {
				if (fS1 >= -fExtDet) {
					if (fS1 <= fExtDet) // region 0
					{
						// minimum at interior points of ray and segment
						float fInvDet = ((float) 1.0) / fDet;
						fS0 *= fInvDet;
						fS1 *= fInvDet;
						fSqrDist = fS0
								* (fS0 + fA01 * fS1 + ((float) 2.0) * fB0)
								+ fS1
								* (fA01 * fS0 + fS1 + ((float) 2.0) * fB1) + fC;
					} else // region 1
					{
						fS1 = extent;
						fS0 = -(fA01 * fS1 + fB0);
						if (fS0 > (float) 0.0) {
							fSqrDist = -fS0 * fS0 + fS1
									* (fS1 + ((float) 2.0) * fB1) + fC;
						} else {
							fS0 = (float) 0.0;
							fSqrDist = fS1 * (fS1 + ((float) 2.0) * fB1) + fC;
						}
					}
				} else // region 5
				{
					fS1 = -extent;
					fS0 = -(fA01 * fS1 + fB0);
					if (fS0 > (float) 0.0) {
						fSqrDist = -fS0 * fS0 + fS1
								* (fS1 + ((float) 2.0) * fB1) + fC;
					} else {
						fS0 = (float) 0.0;
						fSqrDist = fS1 * (fS1 + ((float) 2.0) * fB1) + fC;
					}
				}
			} else {
				if (fS1 <= -fExtDet) // region 4
				{
					fS0 = -(-fA01 * extent + fB0);
					if (fS0 > (float) 0.0) {
						fS1 = -extent;
						fSqrDist = -fS0 * fS0 + fS1
								* (fS1 + ((float) 2.0) * fB1) + fC;
					} else {
						fS0 = (float) 0.0;
						fS1 = -fB1;
						if (fS1 < -extent) {
							fS1 = -extent;
						} else if (fS1 > extent) {
							fS1 = extent;
						}
						fSqrDist = fS1 * (fS1 + ((float) 2.0) * fB1) + fC;
					}
				} else if (fS1 <= fExtDet) // region 3
				{
					fS0 = (float) 0.0;
					fS1 = -fB1;
					if (fS1 < -extent) {
						fS1 = -extent;
					} else if (fS1 > extent) {
						fS1 = extent;
					}
					fSqrDist = fS1 * (fS1 + ((float) 2.0) * fB1) + fC;
				} else // region 2
				{
					fS0 = -(fA01 * extent + fB0);
					if (fS0 > (float) 0.0) {
						fS1 = extent;
						fSqrDist = -fS0 * fS0 + fS1
								* (fS1 + ((float) 2.0) * fB1) + fC;
					} else {
						fS0 = (float) 0.0;
						fS1 = -fB1;
						if (fS1 < -extent) {
							fS1 = -extent;
						} else if (fS1 > extent) {
							fS1 = extent;
						}
						fSqrDist = fS1 * (fS1 + ((float) 2.0) * fB1) + fC;
					}
				}
			}
		} else {
			// ray and segment are parallel
			if (fA01 > (float) 0.0) {
				// opposite direction vectors
				fS1 = -extent;
			} else {
				// same direction vectors
				fS1 = extent;
			}

			fS0 = -(fA01 * fS1 + fB0);
			if (fS0 > (float) 0.0) {
				fSqrDist = -fS0 * fS0 + fS1 * (fS1 + ((float) 2.0) * fB1) + fC;
			} else {
				fS0 = (float) 0.0;
				fSqrDist = fS1 * (fS1 + ((float) 2.0) * fB1) + fC;
			}
		}
		return FastMath.abs(fSqrDist);
	}

	public Vector3f getDirection() {
		return direction;
	}

	public void setDirection(Vector3f direction) {
		this.direction = direction;
	}

	public float getExtent() {
		return extent;
	}

	public void setExtent(float extent) {
		this.extent = extent;
	}

	public Vector3f getOrigin() {
		return origin;
	}

	public void setOrigin(Vector3f origin) {
		this.origin = origin;
	}

	// P+e*D
	public Vector3f getPositiveEnd(Vector3f store) {
		if (store == null) {
			store = new Vector3f();
		}
		return origin.add((direction.mult(extent, store)), store);
	}

	// P-e*D
	public Vector3f getNegativeEnd(Vector3f store) {
		if (store == null) {
			store = new Vector3f();
		}
		return origin.subtract((direction.mult(extent, store)), store);
	}

    public void write(JMEExporter e) throws IOException {
        OutputCapsule capsule = e.getCapsule(this);
        capsule.write(origin, "origin", Vector3f.ZERO);
        capsule.write(direction, "direction", Vector3f.ZERO);
        capsule.write(extent, "extent", 0);
    }

    public void read(JMEImporter e) throws IOException {
        InputCapsule capsule = e.getCapsule(this);
        origin = (Vector3f)capsule.readSavable("origin", Vector3f.ZERO.clone());
        direction = (Vector3f)capsule.readSavable("direction", Vector3f.ZERO.clone());
        extent = capsule.readFloat("extent", 0);
    }

    public Class<? extends LineSegment> getClassTag() {
        return this.getClass();
    }

    @Override
    public LineSegment clone() {
        try {
            LineSegment segment = (LineSegment) super.clone();
            segment.direction = direction.clone();
            segment.origin = origin.clone();
            return segment;
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }

    /**
     * <p>Evaluates whether a given point is contained within the axis aligned bounding box
     * that contains this LineSegment.</p><p>This function is float error aware.</p>
     */
    public boolean isPointInsideBounds(Vector3f point) {
    	return isPointInsideBounds(point, Float.MIN_VALUE);
    }

    /**
     * <p>Evaluates whether a given point is contained within the axis aligned bounding box
     * that contains this LineSegment.</p><p>This function accepts an error parameter, which
     * is added to the extent of the bounding box.</p>
     */
    public boolean isPointInsideBounds(Vector3f point, float error) {

    	if (FastMath.abs(point.x - origin.x) > FastMath.abs(direction.x * extent) + error) return false;
    	if (FastMath.abs(point.y - origin.y) > FastMath.abs(direction.y * extent) + error) return false;
    	if (FastMath.abs(point.z - origin.z) > FastMath.abs(direction.z * extent) + error) return false;

    	return true;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -